From: Thomas Walker Lynch
Date: Wed, 11 Feb 2026 09:56:16 +0000 (+0000)
Subject: TM_tutorial.py running, a first array based full TM implementation for Python
X-Git-Url: https://git.reasoningtechnology.com/sitemap.xml?a=commitdiff_plain;h=5d7158f09c35413df50ff8fa60d0ed6661ac3390;p=Epimetheus%2F.git
TM_tutorial.py running, a first array based full TM implementation for Python
---
diff --git a/developer/authored/TM.py b/developer/authored/TM.py
index 24a262e..693b77b 100755
--- a/developer/authored/TM.py
+++ b/developer/authored/TM.py
@@ -1,56 +1,120 @@
-#!/usr/bin/env python3
-import sys
-
-try:
- import TM_module
-except ImportError:
- print("Error: Import failed. Run 'python3 setup.py build_ext --inplace'")
- sys.exit(1)
-
-# ==========================================
-# TM Command Language (Explicit Types)
-# ==========================================
-
-# Import all C-defined types into this namespace
-_this_module = sys.modules[__name__]
-for name in dir(TM_module):
- if name.startswith("TM_") or name.startswith("TMA_"):
- setattr(_this_module, name, getattr(TM_module, name))
-
-# ==========================================
-# Defaults (Aliasing)
-# ==========================================
-
-# Pattern: TM_[Container]_[Direction]_[Entanglement]
-# Default Container: Arr
-# Default Entanglement: ND
-# Default Direction: SR
-
-# --- Global Default ---
-TM = TM_module.TM_Arr_SR_ND
-
-# --- Container Defaults (Dir=SR, Ent=ND) ---
-TM_Arr = TM_module.TM_Arr_SR_ND
-TM_ArrV = TM_module.TM_ArrV_SR_ND
-TM_Gr = TM_module.TM_Gr_SR_ND
-TM_Glr = TM_module.TM_Glr_SR_ND
-TM_Set = TM_module.TM_Set_SR_ND
-TM_Map = TM_module.TM_Map_SR_ND
-TM_MapK = TM_module.TM_MapK_SR_ND
-TM_MapV = TM_module.TM_MapV_SR_ND
-TM_ASCII= TM_module.TM_ASCII_SR_ND
-TM_UTF8 = TM_module.TM_UTF8_SR_ND
-TM_BCD = TM_module.TM_BCD_SR_ND
-
-# --- Direction Defaults (Cont=Arr, Ent=ND) ---
-TM_SR = TM_module.TM_Arr_SR_ND
-TM_SL = TM_module.TM_Arr_SL_ND
-
-# --- Entanglement Defaults (Cont=Arr, Dir=SR) ---
-TM_ND = TM_module.TM_Arr_SR_ND
-TM_SO = TM_module.TM_Arr_SR_SO
-TM_EA = TM_module.TM_Arr_SR_EA
-
-# --- Common Partials ---
-TM_Arr_SL = TM_module.TM_Arr_SL_ND
-TM_ASCII_SL = TM_module.TM_ASCII_SL_ND
+"""
+TM.py
+Wrapper for the C extension TM_module.
+"""
+import TM_module
+
+# -----------------------------------------------------------------------------
+# 1. Arr (Fixed Array)
+# -----------------------------------------------------------------------------
+TM_Arr_CR_ND = TM_module.TM_Arr_CR_ND
+TM_Arr_CR_SO = TM_module.TM_Arr_CR_SO
+TM_Arr_CR_EA = TM_module.TM_Arr_CR_EA
+
+TM_Arr_CLR_ND = TM_module.TM_Arr_CLR_ND
+TM_Arr_CLR_SO = TM_module.TM_Arr_CLR_SO
+TM_Arr_CLR_EA = TM_module.TM_Arr_CLR_EA
+
+# -----------------------------------------------------------------------------
+# 2. ArrV (Variable Array / Vector)
+# -----------------------------------------------------------------------------
+TM_ArrV_CR_ND = TM_module.TM_ArrV_CR_ND
+TM_ArrV_CR_SO = TM_module.TM_ArrV_CR_SO
+TM_ArrV_CR_EA = TM_module.TM_ArrV_CR_EA
+
+TM_ArrV_CLR_ND = TM_module.TM_ArrV_CLR_ND
+TM_ArrV_CLR_SO = TM_module.TM_ArrV_CLR_SO
+TM_ArrV_CLR_EA = TM_module.TM_ArrV_CLR_EA
+
+# -----------------------------------------------------------------------------
+# 3. Gr (Graph Right / Linked List)
+# -----------------------------------------------------------------------------
+TM_Gr_CR_ND = TM_module.TM_Gr_CR_ND
+TM_Gr_CR_SO = TM_module.TM_Gr_CR_SO
+TM_Gr_CR_EA = TM_module.TM_Gr_CR_EA
+
+TM_Gr_CLR_ND = TM_module.TM_Gr_CLR_ND
+TM_Gr_CLR_SO = TM_module.TM_Gr_CLR_SO
+TM_Gr_CLR_EA = TM_module.TM_Gr_CLR_EA
+
+# -----------------------------------------------------------------------------
+# 4. Glr (Graph Left Right / Doubly Linked List)
+# -----------------------------------------------------------------------------
+TM_Glr_CR_ND = TM_module.TM_Glr_CR_ND
+TM_Glr_CR_SO = TM_module.TM_Glr_CR_SO
+TM_Glr_CR_EA = TM_module.TM_Glr_CR_EA
+
+TM_Glr_CLR_ND = TM_module.TM_Glr_CLR_ND
+TM_Glr_CLR_SO = TM_module.TM_Glr_CLR_SO
+TM_Glr_CLR_EA = TM_module.TM_Glr_CLR_EA
+
+# -----------------------------------------------------------------------------
+# 5. Set (Unordered)
+# -----------------------------------------------------------------------------
+TM_Set_CR_ND = TM_module.TM_Set_CR_ND
+TM_Set_CR_SO = TM_module.TM_Set_CR_SO
+TM_Set_CR_EA = TM_module.TM_Set_CR_EA
+
+TM_Set_CLR_ND = TM_module.TM_Set_CLR_ND
+TM_Set_CLR_SO = TM_module.TM_Set_CLR_SO
+TM_Set_CLR_EA = TM_module.TM_Set_CLR_EA
+
+# -----------------------------------------------------------------------------
+# 6. Map (Items)
+# -----------------------------------------------------------------------------
+TM_Map_CR_ND = TM_module.TM_Map_CR_ND
+TM_Map_CR_SO = TM_module.TM_Map_CR_SO
+TM_Map_CR_EA = TM_module.TM_Map_CR_EA
+
+TM_Map_CLR_ND = TM_module.TM_Map_CLR_ND
+TM_Map_CLR_SO = TM_module.TM_Map_CLR_SO
+TM_Map_CLR_EA = TM_module.TM_Map_CLR_EA
+
+# -----------------------------------------------------------------------------
+# 7. MapK (Keys)
+# -----------------------------------------------------------------------------
+TM_MapK_CR_ND = TM_module.TM_MapK_CR_ND
+TM_MapK_CR_SO = TM_module.TM_MapK_CR_SO
+TM_MapK_CR_EA = TM_module.TM_MapK_CR_EA
+
+TM_MapK_CLR_ND = TM_module.TM_MapK_CLR_ND
+TM_MapK_CLR_SO = TM_module.TM_MapK_CLR_SO
+TM_MapK_CLR_EA = TM_module.TM_MapK_CLR_EA
+
+# -----------------------------------------------------------------------------
+# 8. MapV (Values)
+# -----------------------------------------------------------------------------
+TM_MapV_CR_ND = TM_module.TM_MapV_CR_ND
+TM_MapV_CR_SO = TM_module.TM_MapV_CR_SO
+TM_MapV_CR_EA = TM_module.TM_MapV_CR_EA
+
+TM_MapV_CLR_ND = TM_module.TM_MapV_CLR_ND
+TM_MapV_CLR_SO = TM_module.TM_MapV_CLR_SO
+TM_MapV_CLR_EA = TM_module.TM_MapV_CLR_EA
+
+# -----------------------------------------------------------------------------
+# 9. ASCII (String)
+# -----------------------------------------------------------------------------
+TM_ASCII_CR_ND = TM_module.TM_ASCII_CR_ND
+TM_ASCII_CR_SO = TM_module.TM_ASCII_CR_SO
+TM_ASCII_CR_EA = TM_module.TM_ASCII_CR_EA
+
+TM_ASCII_CLR_ND = TM_module.TM_ASCII_CLR_ND
+TM_ASCII_CLR_SO = TM_module.TM_ASCII_CLR_SO
+TM_ASCII_CLR_EA = TM_module.TM_ASCII_CLR_EA
+
+# -----------------------------------------------------------------------------
+# 10. UTF8 (String)
+# -----------------------------------------------------------------------------
+TM_UTF8_CR_ND = TM_module.TM_UTF8_CR_ND
+TM_UTF8_CR_SO = TM_module.TM_UTF8_CR_SO
+TM_UTF8_CR_EA = TM_module.TM_UTF8_CR_EA
+
+TM_UTF8_CLR_ND = TM_module.TM_UTF8_CLR_ND
+TM_UTF8_CLR_SO = TM_module.TM_UTF8_CLR_SO
+TM_UTF8_CLR_EA = TM_module.TM_UTF8_CLR_EA
+
+# -----------------------------------------------------------------------------
+# 11. Abstract
+# -----------------------------------------------------------------------------
+TMA_NaturalNumber = TM_module.TMA_NaturalNumber
diff --git a/developer/authored/TM_module.c b/developer/authored/TM_module.c
index abf3a9a..10c0f2a 100644
--- a/developer/authored/TM_module.c
+++ b/developer/authored/TM_module.c
@@ -3,7 +3,7 @@
CPython Extension: Tape Machine Factory
Implements:
- - 66 Concrete Machine Types
+ - 60 Concrete Machine Types (Chiral naming: CR/CLR)
- TMA_NaturalNumber (Abstract Machine)
Namespaces:
@@ -23,25 +23,6 @@
/* UNIVERSAL HEAD STATE */
/* ========================================================= */
-/* LIMITATION OF C VOID POINTERS:
-
- In Standard ISO C, there is no way to "bind" a type to a void* pointer
- temporarily to perform arithmetic. A cast expression like '(Type*)ptr'
- yields a value (r-value), not a storage location (l-value), so we
- cannot perform operations like '((Type*)ptr)++'.
-
- To remain strictly ISO C compliant, we would have to define multiple
- structs (one for PyObject**, one for char*, etc.), which breaks our
- generic "Universal Head" architecture.
-
- DECISION: THE GCC/CLANG WAY
- We utilize the common GCC/Clang extension that treats arithmetic on
- void* as byte-level arithmetic (i.e., sizeof(void) == 1).
-
- This allows us to write clean, polymorphic code:
- self->head_ptr += sizeof(Element);
-*/
-
typedef struct{
PyObject_HEAD
PyObject* tape_obj; /* The Container */
@@ -88,53 +69,59 @@ static int TM·Arr·init(TM·Head* self, PyObject* args, PyObject* kwds){
return 0;
}
-/* Sync: Re-bases pointers after a List Reallocation */
static void TM·Arr·sync(TM·Head* self){
- /* 1. Calculate Logical Offset (Byte Distance) */
Py_ssize_t byte_offset = self->head_ptr - self->start_ptr;
-
- /* 2. Get NEW pointers */
Py_ssize_t len = PyList_GET_SIZE(self->tape_obj);
PyObject** new_items = ((PyListObject*)self->tape_obj)->ob_item;
-
- /* 3. Rebase */
self->start_ptr = (void*)new_items;
self->end_ptr = (void*)(new_items + len);
-
- /* 4. Restore Head (Byte Arithmetic) */
self->head_ptr = self->start_ptr + byte_offset;
}
-/* --- PRIMITIVES: NAVIGATION (The GCC Way) --- */
+static inline void TM·Arr·lazysync(TM·Head* self){
+ Py_ssize_t known_len = (self->end_ptr - self->start_ptr) / sizeof(PyObject*);
+ Py_ssize_t actual_len = PyList_GET_SIZE(self->tape_obj);
+ if (known_len != actual_len) {
+ TM·Arr·sync(self);
+ }
+}
+
+/* --- PRIMITIVES: NAVIGATION --- */
static PyObject* TM·Arr·s(TM·Head* self){
+ TM·Arr·lazysync(self);
self->head_ptr += sizeof(PyObject*);
Py_RETURN_NONE;
}
-static PyObject* TM·Arr·ls(TM·Head* self){
+static PyObject* TM·Arr·Ls(TM·Head* self){
+ TM·Arr·lazysync(self);
self->head_ptr -= sizeof(PyObject*);
Py_RETURN_NONE;
}
static PyObject* TM·Arr·sn(TM·Head* self, PyObject* args){
+ TM·Arr·lazysync(self);
Py_ssize_t n; if(!PyArg_ParseTuple(args, "n", &n)) return NULL;
self->head_ptr += n * sizeof(PyObject*);
Py_RETURN_NONE;
}
-static PyObject* TM·Arr·lsn(TM·Head* self, PyObject* args){
+static PyObject* TM·Arr·Lsn(TM·Head* self, PyObject* args){
+ TM·Arr·lazysync(self);
Py_ssize_t n; if(!PyArg_ParseTuple(args, "n", &n)) return NULL;
self->head_ptr -= n * sizeof(PyObject*);
Py_RETURN_NONE;
}
static PyObject* TM·Arr·sR(TM·Head* self){
+ TM·Arr·lazysync(self);
self->head_ptr = self->end_ptr - sizeof(PyObject*);
Py_RETURN_NONE;
}
static PyObject* TM·Arr·LsR(TM·Head* self){
+ TM·Arr·lazysync(self);
self->head_ptr = self->start_ptr;
Py_RETURN_NONE;
}
@@ -142,16 +129,14 @@ static PyObject* TM·Arr·LsR(TM·Head* self){
/* --- PRIMITIVES: ENTANGLEMENT --- */
static PyObject* TM·Arr·e(TM·Head* self){
- /* Create a new object of the same type */
+ TM·Arr·lazysync(self);
PyTypeObject* type = Py_TYPE(self);
TM·Head* new_tm = (TM·Head*)type->tp_alloc(type, 0);
if (!new_tm) return NULL;
- /* Share the tape */
new_tm->tape_obj = self->tape_obj;
Py_INCREF(new_tm->tape_obj);
- /* Copy the pointers (Entangled start at same position) */
new_tm->start_ptr = self->start_ptr;
new_tm->end_ptr = self->end_ptr;
new_tm->head_ptr = self->head_ptr;
@@ -162,13 +147,14 @@ static PyObject* TM·Arr·e(TM·Head* self){
/* --- PRIMITIVES: I/O --- */
static PyObject* TM·Arr·r(TM·Head* self){
- /* Must cast to dereference the data */
+ TM·Arr·lazysync(self);
PyObject* item = *(PyObject**)self->head_ptr;
Py_INCREF(item);
return item;
}
static PyObject* TM·Arr·w(TM·Head* self, PyObject* val){
+ TM·Arr·lazysync(self);
PyObject** p = (PyObject**)self->head_ptr;
PyObject* old = *p;
Py_INCREF(val);
@@ -180,78 +166,133 @@ static PyObject* TM·Arr·w(TM·Head* self, PyObject* val){
/* --- PRIMITIVES: QUERY --- */
static PyObject* TM·Arr·qR(TM·Head* self){
- /* GCC Extension: void* comparison and arithmetic */
+ TM·Arr·lazysync(self);
if(self->head_ptr >= self->end_ptr - sizeof(PyObject*)){
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}
-static PyObject* TM·Arr·qL(TM·Head* self){
+static PyObject* TM·Arr·LqR(TM·Head* self){
+ TM·Arr·lazysync(self);
if(self->head_ptr <= self->start_ptr){
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}
+static PyObject* TM·Arr·qnR(TM·Head* self){
+ TM·Arr·lazysync(self);
+ Py_ssize_t byte_diff = self->end_ptr - self->head_ptr;
+ /* If at Rightmost, diff is 1 item size. Result 0. */
+ Py_ssize_t count = (byte_diff / sizeof(PyObject*)) - 1;
+ return PyLong_FromSsize_t(count);
+}
-/* --- PRIMITIVES: DESTRUCTIVE (Variable Array Only) --- */
+static PyObject* TM·Arr·LqnR(TM·Head* self){
+ TM·Arr·lazysync(self);
+ Py_ssize_t byte_diff = self->head_ptr - self->start_ptr;
+ /* If at Leftmost, diff is 0. Result 0. */
+ Py_ssize_t count = byte_diff / sizeof(PyObject*);
+ return PyLong_FromSsize_t(count);
+}
-static PyObject* TM·Arr·d(TM·Head* self){
- /* 1. Calc Index (Byte Diff / Element Size) */
- Py_ssize_t idx = (self->head_ptr - self->start_ptr) / sizeof(PyObject*);
+/* --- PRIMITIVES: DESTRUCTIVE (SO Only) --- */
- /* 2. API Call */
- if (PyList_SetSlice(self->tape_obj, idx, idx+1, NULL) < 0) return NULL;
-
- /* 3. SYNC */
- TM·Arr·sync(self);
-
- /* 4. Safety */
- if (self->head_ptr >= self->end_ptr && self->start_ptr != self->end_ptr) {
- self->head_ptr -= sizeof(PyObject*);
- } else if (self->start_ptr == self->end_ptr) {
- PyErr_SetString(PyExc_RuntimeError, "TM Empty: First Order Invariant Broken.");
+static PyObject* TM·Arr·dR(TM·Head* self){
+ TM·Arr·lazysync(self);
+ /* Delete from Current to Rightmost (inclusive) */
+ /* Guard: Must have a left neighbor to step back to. */
+ if (self->head_ptr <= self->start_ptr) {
+ PyErr_SetString(PyExc_RuntimeError, "Invariant Violation: Cannot dR from leftmost cell (tape would empty).");
return NULL;
}
+ Py_ssize_t idx = (self->head_ptr - self->start_ptr) / sizeof(PyObject*);
+ Py_ssize_t len = PyList_GET_SIZE(self->tape_obj);
+
+ if (PyList_SetSlice(self->tape_obj, idx, len, NULL) < 0) return NULL;
+
+ /* Current cell is gone. Step Left to valid neighbor. */
+ self->head_ptr -= sizeof(PyObject*);
+ TM·Arr·sync(self);
Py_RETURN_NONE;
}
-static PyObject* TM·Arr·a(TM·Head* self, PyObject* val){
+static PyObject* TM·Arr·LdR(TM·Head* self){
+ TM·Arr·lazysync(self);
+ /* Delete from Current to Leftmost (inclusive) */
+ /* Guard: Must have a right neighbor to step 'right' (reset) to. */
+ if (self->head_ptr >= self->end_ptr - sizeof(PyObject*)) {
+ PyErr_SetString(PyExc_RuntimeError, "Invariant Violation: Cannot LdR from rightmost cell (tape would empty).");
+ return NULL;
+ }
Py_ssize_t idx = (self->head_ptr - self->start_ptr) / sizeof(PyObject*);
-
- if (PyList_Insert(self->tape_obj, idx, val) < 0) return NULL;
+ if (PyList_SetSlice(self->tape_obj, 0, idx+1, NULL) < 0) return NULL;
+
+ /* Head resets to 0 (the element that was to the right of deleted chunk) */
+ self->head_ptr = self->start_ptr;
TM·Arr·sync(self);
Py_RETURN_NONE;
}
static PyObject* TM·Arr·esd(TM·Head* self){
+ TM·Arr·lazysync(self);
if (self->head_ptr >= self->end_ptr - sizeof(PyObject*)) {
PyErr_SetString(PyExc_IndexError, "esd: No right neighbor.");
return NULL;
}
-
Py_ssize_t idx = (self->head_ptr - self->start_ptr) / sizeof(PyObject*);
-
if (PyList_SetSlice(self->tape_obj, idx+1, idx+2, NULL) < 0) return NULL;
-
TM·Arr·sync(self);
Py_RETURN_NONE;
}
-static PyObject* TM·Arr·Lesd(TM·Head* self){
+static PyObject* TM·Arr·eLsd(TM·Head* self){
+ TM·Arr·lazysync(self);
if (self->head_ptr <= self->start_ptr) {
- PyErr_SetString(PyExc_IndexError, "Lesd: No left neighbor.");
+ PyErr_SetString(PyExc_IndexError, "eLsd: No left neighbor.");
return NULL;
}
-
Py_ssize_t idx = (self->head_ptr - self->start_ptr) / sizeof(PyObject*);
if (PyList_SetSlice(self->tape_obj, idx-1, idx, NULL) < 0) return NULL;
- /* Adjustment: Shift Left */
+ /* We removed item at idx-1. The item at idx is now at idx-1. */
self->head_ptr -= sizeof(PyObject*);
+ TM·Arr·sync(self);
+ Py_RETURN_NONE;
+}
+
+static PyObject* TM·Arr·esa(TM·Head* self, PyObject* val){
+ TM·Arr·lazysync(self);
+ /* Insert after current (at idx+1) */
+ Py_ssize_t idx = (self->head_ptr - self->start_ptr) / sizeof(PyObject*);
+ if (PyList_Insert(self->tape_obj, idx+1, val) < 0) return NULL;
+ TM·Arr·sync(self);
+ Py_RETURN_NONE;
+}
+static PyObject* TM·Arr·eLsa(TM·Head* self, PyObject* val){
+ TM·Arr·lazysync(self);
+ /* Insert before current (at idx) */
+ Py_ssize_t idx = (self->head_ptr - self->start_ptr) / sizeof(PyObject*);
+ if (PyList_Insert(self->tape_obj, idx, val) < 0) return NULL;
+
+ /* Current item shifts right to idx+1. Increment head to track it. */
+ self->head_ptr += sizeof(PyObject*);
+ TM·Arr·sync(self);
+ Py_RETURN_NONE;
+}
+
+static PyObject* TM·Arr·aR(TM·Head* self, PyObject* val){
+ if (PyList_Append(self->tape_obj, val) < 0) return NULL;
+ TM·Arr·sync(self);
+ Py_RETURN_NONE;
+}
+
+static PyObject* TM·Arr·LaR(TM·Head* self, PyObject* val){
+ if (PyList_Insert(self->tape_obj, 0, val) < 0) return NULL;
+ self->head_ptr += sizeof(PyObject*);
TM·Arr·sync(self);
Py_RETURN_NONE;
}
@@ -262,59 +303,70 @@ static PyObject* TM·Arr·Lesd(TM·Head* self){
/* ========================================================= */
/* 1. NON-DESTRUCTIVE (ND) */
-static PyMethodDef Table·SR·ND[] = {
+static PyMethodDef Table·CR·ND[] = {
{"s", (PyCFunction)TM·Arr·s, METH_NOARGS, ""},
{"sn",(PyCFunction)TM·Arr·sn,METH_VARARGS,""},
{"e", (PyCFunction)TM·Arr·e, METH_NOARGS, ""},
{"r", (PyCFunction)TM·Arr·r, METH_NOARGS, ""},
{"w", (PyCFunction)TM·Arr·w, METH_O, ""},
{"qR",(PyCFunction)TM·Arr·qR,METH_NOARGS, ""},
+ {"qnR",(PyCFunction)TM·Arr·qnR,METH_NOARGS,""},
+ {"LqnR",(PyCFunction)TM·Arr·LqnR,METH_NOARGS,""},
{"LsR",(PyCFunction)TM·Arr·LsR,METH_NOARGS,""},
{NULL}
};
-static PyMethodDef Table·SL·ND[] = {
+static PyMethodDef Table·CLR·ND[] = {
{"s", (PyCFunction)TM·Arr·s, METH_NOARGS, ""},
{"sn",(PyCFunction)TM·Arr·sn,METH_VARARGS,""},
{"e", (PyCFunction)TM·Arr·e, METH_NOARGS, ""},
- {"ls",(PyCFunction)TM·Arr·ls,METH_NOARGS, ""},
- {"lsn",(PyCFunction)TM·Arr·lsn,METH_VARARGS,""},
+ {"Ls",(PyCFunction)TM·Arr·Ls,METH_NOARGS, ""},
+ {"Lsn",(PyCFunction)TM·Arr·Lsn,METH_VARARGS,""},
{"r", (PyCFunction)TM·Arr·r, METH_NOARGS, ""},
{"w", (PyCFunction)TM·Arr·w, METH_O, ""},
{"qR",(PyCFunction)TM·Arr·qR,METH_NOARGS, ""},
- {"qL",(PyCFunction)TM·Arr·qL,METH_NOARGS, ""},
+ {"LqR",(PyCFunction)TM·Arr·LqR,METH_NOARGS, ""},
+ {"qnR",(PyCFunction)TM·Arr·qnR,METH_NOARGS,""},
+ {"LqnR",(PyCFunction)TM·Arr·LqnR,METH_NOARGS,""},
{"sR",(PyCFunction)TM·Arr·sR,METH_NOARGS, ""},
{"LsR",(PyCFunction)TM·Arr·LsR,METH_NOARGS,""},
{NULL}
};
-/* 2. DESTRUCTIVE (SO) - For ArrV */
-static PyMethodDef Table·SR·SO[] = {
+/* 2. DESTRUCTIVE (SO) - No Entangle 'e' */
+static PyMethodDef Table·CR·SO[] = {
{"s", (PyCFunction)TM·Arr·s, METH_NOARGS, ""},
{"sn",(PyCFunction)TM·Arr·sn,METH_VARARGS,""},
- {"e", (PyCFunction)TM·Arr·e, METH_NOARGS, ""},
{"r", (PyCFunction)TM·Arr·r, METH_NOARGS, ""},
{"w", (PyCFunction)TM·Arr·w, METH_O, ""},
- {"d", (PyCFunction)TM·Arr·d, METH_NOARGS, ""},
- {"a", (PyCFunction)TM·Arr·a, METH_O, ""},
+ {"dR",(PyCFunction)TM·Arr·dR,METH_NOARGS, ""},
{"esd",(PyCFunction)TM·Arr·esd,METH_NOARGS,""},
+ {"esa",(PyCFunction)TM·Arr·esa,METH_O, ""},
+ {"aR",(PyCFunction)TM·Arr·aR,METH_O, ""},
{"qR",(PyCFunction)TM·Arr·qR,METH_NOARGS, ""},
+ {"qnR",(PyCFunction)TM·Arr·qnR,METH_NOARGS,""},
+ {"LqnR",(PyCFunction)TM·Arr·LqnR,METH_NOARGS,""},
{"LsR",(PyCFunction)TM·Arr·LsR,METH_NOARGS,""},
{NULL}
};
-static PyMethodDef Table·SL·SO[] = {
+static PyMethodDef Table·CLR·SO[] = {
{"s", (PyCFunction)TM·Arr·s, METH_NOARGS, ""},
- {"ls",(PyCFunction)TM·Arr·ls,METH_NOARGS, ""},
- {"e", (PyCFunction)TM·Arr·e, METH_NOARGS, ""},
+ {"Ls",(PyCFunction)TM·Arr·Ls,METH_NOARGS, ""},
{"r", (PyCFunction)TM·Arr·r, METH_NOARGS, ""},
{"w", (PyCFunction)TM·Arr·w, METH_O, ""},
- {"d", (PyCFunction)TM·Arr·d, METH_NOARGS, ""},
- {"a", (PyCFunction)TM·Arr·a, METH_O, ""},
+ {"dR",(PyCFunction)TM·Arr·dR,METH_NOARGS, ""},
+ {"LdR",(PyCFunction)TM·Arr·LdR,METH_NOARGS,""},
{"esd",(PyCFunction)TM·Arr·esd,METH_NOARGS,""},
- {"Lesd",(PyCFunction)TM·Arr·Lesd,METH_NOARGS,""},
+ {"eLsd",(PyCFunction)TM·Arr·eLsd,METH_NOARGS,""},
+ {"esa",(PyCFunction)TM·Arr·esa,METH_O, ""},
+ {"eLsa",(PyCFunction)TM·Arr·eLsa,METH_O, ""},
+ {"aR",(PyCFunction)TM·Arr·aR,METH_O, ""},
+ {"LaR",(PyCFunction)TM·Arr·LaR,METH_O, ""},
{"qR",(PyCFunction)TM·Arr·qR,METH_NOARGS, ""},
- {"qL",(PyCFunction)TM·Arr·qL,METH_NOARGS, ""},
+ {"LqR",(PyCFunction)TM·Arr·LqR,METH_NOARGS, ""},
+ {"qnR",(PyCFunction)TM·Arr·qnR,METH_NOARGS,""},
+ {"LqnR",(PyCFunction)TM·Arr·LqnR,METH_NOARGS,""},
{"sR",(PyCFunction)TM·Arr·sR,METH_NOARGS, ""},
{"LsR",(PyCFunction)TM·Arr·LsR,METH_NOARGS,""},
{NULL}
@@ -341,7 +393,7 @@ static PyObject* TM·Nat·sn(TM·Nat* self, PyObject* args){
self->state += n; Py_RETURN_NONE;
}
-static PyObject* TM·Nat·ls(TM·Nat* self){
+static PyObject* TM·Nat·Ls(TM·Nat* self){
if(self->state > 0) self->state--;
Py_RETURN_NONE;
}
@@ -354,21 +406,36 @@ static PyObject* TM·Nat·w(TM·Nat* self, PyObject* val){
}
static PyObject* TM·Nat·qR(TM·Nat* self){ Py_RETURN_FALSE; }
-static PyObject* TM·Nat·qL(TM·Nat* self){
+static PyObject* TM·Nat·LqR(TM·Nat* self){
if(self->state == 0){
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}
+/* Nat Query N:
+ qnR -> Infinite? Or not supported. For now, max int?
+ LqnR -> Distance from 0. Equal to state.
+*/
+static PyObject* TM·Nat·qnR(TM·Nat* self){
+ /* Abstract number line is infinite. */
+ PyErr_SetString(PyExc_RuntimeError, "Natural Number tape is infinite to the right.");
+ return NULL;
+}
+static PyObject* TM·Nat·LqnR(TM·Nat* self){
+ return PyLong_FromUnsignedLongLong(self->state);
+}
+
static PyMethodDef TM·Nat·methods[] = {
{"s", (PyCFunction)TM·Nat·s, METH_NOARGS, ""},
{"sn",(PyCFunction)TM·Nat·sn,METH_VARARGS,""},
- {"ls",(PyCFunction)TM·Nat·ls,METH_NOARGS, ""},
+ {"Ls",(PyCFunction)TM·Nat·Ls,METH_NOARGS, ""},
{"r", (PyCFunction)TM·Nat·r, METH_NOARGS, ""},
{"w", (PyCFunction)TM·Nat·w, METH_O, ""},
{"qR",(PyCFunction)TM·Nat·qR,METH_NOARGS, ""},
- {"qL",(PyCFunction)TM·Nat·qL,METH_NOARGS, ""},
+ {"LqR",(PyCFunction)TM·Nat·LqR,METH_NOARGS, ""},
+ {"qnR",(PyCFunction)TM·Nat·qnR,METH_NOARGS, ""},
+ {"LqnR",(PyCFunction)TM·Nat·LqnR,METH_NOARGS, ""},
{"LsR",(PyCFunction)TM·Nat·LsR,METH_NOARGS, ""},
{NULL}
};
@@ -386,7 +453,7 @@ static PyTypeObject TMA_NaturalNumber·Type = {
/* ========================================================= */
-/* TYPE DEFINITIONS (The 66 Concrete Types) */
+/* TYPE DEFINITIONS (The 60 Concrete Types) */
/* ========================================================= */
#define DEFINE_TYPE(NAME, METHODS) \
@@ -404,114 +471,104 @@ static PyTypeObject NAME##·Type = { \
/* ---------------------------------------------------------
1. Arr (Fixed Array)
--------------------------------------------------------- */
-DEFINE_TYPE(TM_Arr_SR_ND, Table·SR·ND)
-DEFINE_TYPE(TM_Arr_SR_SO, Table·SR·ND)
-DEFINE_TYPE(TM_Arr_SR_EA, Table·SR·ND)
+DEFINE_TYPE(TM_Arr_CR_ND, Table·CR·ND)
+DEFINE_TYPE(TM_Arr_CR_SO, Table·CR·ND)
+DEFINE_TYPE(TM_Arr_CR_EA, Table·CR·ND)
-DEFINE_TYPE(TM_Arr_SL_ND, Table·SL·ND)
-DEFINE_TYPE(TM_Arr_SL_SO, Table·SL·ND)
-DEFINE_TYPE(TM_Arr_SL_EA, Table·SL·ND)
+DEFINE_TYPE(TM_Arr_CLR_ND, Table·CLR·ND)
+DEFINE_TYPE(TM_Arr_CLR_SO, Table·CLR·ND)
+DEFINE_TYPE(TM_Arr_CLR_EA, Table·CLR·ND)
/* ---------------------------------------------------------
2. ArrV (Variable Array / Vector)
--------------------------------------------------------- */
-DEFINE_TYPE(TM_ArrV_SR_ND, Table·SR·ND)
-DEFINE_TYPE(TM_ArrV_SR_SO, Table·SR·SO) /* Destructive */
-DEFINE_TYPE(TM_ArrV_SR_EA, Table·SR·ND)
+DEFINE_TYPE(TM_ArrV_CR_ND, Table·CR·ND)
+DEFINE_TYPE(TM_ArrV_CR_SO, Table·CR·SO) /* Destructive */
+DEFINE_TYPE(TM_ArrV_CR_EA, Table·CR·ND)
-DEFINE_TYPE(TM_ArrV_SL_ND, Table·SL·ND)
-DEFINE_TYPE(TM_ArrV_SL_SO, Table·SL·SO) /* Destructive */
-DEFINE_TYPE(TM_ArrV_SL_EA, Table·SL·ND)
+DEFINE_TYPE(TM_ArrV_CLR_ND, Table·CLR·ND)
+DEFINE_TYPE(TM_ArrV_CLR_SO, Table·CLR·SO) /* Destructive */
+DEFINE_TYPE(TM_ArrV_CLR_EA, Table·CLR·ND)
/* ---------------------------------------------------------
3. Gr (Graph Right / Linked List)
--------------------------------------------------------- */
-DEFINE_TYPE(TM_Gr_SR_ND, Table·SR·ND)
-DEFINE_TYPE(TM_Gr_SR_SO, Table·SR·ND)
-DEFINE_TYPE(TM_Gr_SR_EA, Table·SR·ND)
-DEFINE_TYPE(TM_Gr_SL_ND, Table·SL·ND)
-DEFINE_TYPE(TM_Gr_SL_SO, Table·SL·ND)
-DEFINE_TYPE(TM_Gr_SL_EA, Table·SL·ND)
+DEFINE_TYPE(TM_Gr_CR_ND, Table·CR·ND)
+DEFINE_TYPE(TM_Gr_CR_SO, Table·CR·ND)
+DEFINE_TYPE(TM_Gr_CR_EA, Table·CR·ND)
+DEFINE_TYPE(TM_Gr_CLR_ND, Table·CLR·ND)
+DEFINE_TYPE(TM_Gr_CLR_SO, Table·CLR·ND)
+DEFINE_TYPE(TM_Gr_CLR_EA, Table·CLR·ND)
/* ---------------------------------------------------------
4. Glr (Graph Left Right / Doubly Linked List)
--------------------------------------------------------- */
-DEFINE_TYPE(TM_Glr_SR_ND, Table·SR·ND)
-DEFINE_TYPE(TM_Glr_SR_SO, Table·SR·ND)
-DEFINE_TYPE(TM_Glr_SR_EA, Table·SR·ND)
-DEFINE_TYPE(TM_Glr_SL_ND, Table·SL·ND)
-DEFINE_TYPE(TM_Glr_SL_SO, Table·SL·ND)
-DEFINE_TYPE(TM_Glr_SL_EA, Table·SL·ND)
+DEFINE_TYPE(TM_Glr_CR_ND, Table·CR·ND)
+DEFINE_TYPE(TM_Glr_CR_SO, Table·CR·ND)
+DEFINE_TYPE(TM_Glr_CR_EA, Table·CR·ND)
+DEFINE_TYPE(TM_Glr_CLR_ND, Table·CLR·ND)
+DEFINE_TYPE(TM_Glr_CLR_SO, Table·CLR·ND)
+DEFINE_TYPE(TM_Glr_CLR_EA, Table·CLR·ND)
/* ---------------------------------------------------------
5. Set (Unordered)
--------------------------------------------------------- */
-DEFINE_TYPE(TM_Set_SR_ND, Table·SR·ND)
-DEFINE_TYPE(TM_Set_SR_SO, Table·SR·ND)
-DEFINE_TYPE(TM_Set_SR_EA, Table·SR·ND)
-DEFINE_TYPE(TM_Set_SL_ND, Table·SL·ND)
-DEFINE_TYPE(TM_Set_SL_SO, Table·SL·ND)
-DEFINE_TYPE(TM_Set_SL_EA, Table·SL·ND)
+DEFINE_TYPE(TM_Set_CR_ND, Table·CR·ND)
+DEFINE_TYPE(TM_Set_CR_SO, Table·CR·ND)
+DEFINE_TYPE(TM_Set_CR_EA, Table·CR·ND)
+DEFINE_TYPE(TM_Set_CLR_ND, Table·CLR·ND)
+DEFINE_TYPE(TM_Set_CLR_SO, Table·CLR·ND)
+DEFINE_TYPE(TM_Set_CLR_EA, Table·CLR·ND)
/* ---------------------------------------------------------
6. Map (Items)
--------------------------------------------------------- */
-DEFINE_TYPE(TM_Map_SR_ND, Table·SR·ND)
-DEFINE_TYPE(TM_Map_SR_SO, Table·SR·ND)
-DEFINE_TYPE(TM_Map_SR_EA, Table·SR·ND)
-DEFINE_TYPE(TM_Map_SL_ND, Table·SL·ND)
-DEFINE_TYPE(TM_Map_SL_SO, Table·SL·ND)
-DEFINE_TYPE(TM_Map_SL_EA, Table·SL·ND)
+DEFINE_TYPE(TM_Map_CR_ND, Table·CR·ND)
+DEFINE_TYPE(TM_Map_CR_SO, Table·CR·ND)
+DEFINE_TYPE(TM_Map_CR_EA, Table·CR·ND)
+DEFINE_TYPE(TM_Map_CLR_ND, Table·CLR·ND)
+DEFINE_TYPE(TM_Map_CLR_SO, Table·CLR·ND)
+DEFINE_TYPE(TM_Map_CLR_EA, Table·CLR·ND)
/* ---------------------------------------------------------
7. MapK (Keys)
--------------------------------------------------------- */
-DEFINE_TYPE(TM_MapK_SR_ND, Table·SR·ND)
-DEFINE_TYPE(TM_MapK_SR_SO, Table·SR·ND)
-DEFINE_TYPE(TM_MapK_SR_EA, Table·SR·ND)
-DEFINE_TYPE(TM_MapK_SL_ND, Table·SL·ND)
-DEFINE_TYPE(TM_MapK_SL_SO, Table·SL·ND)
-DEFINE_TYPE(TM_MapK_SL_EA, Table·SL·ND)
+DEFINE_TYPE(TM_MapK_CR_ND, Table·CR·ND)
+DEFINE_TYPE(TM_MapK_CR_SO, Table·CR·ND)
+DEFINE_TYPE(TM_MapK_CR_EA, Table·CR·ND)
+DEFINE_TYPE(TM_MapK_CLR_ND, Table·CLR·ND)
+DEFINE_TYPE(TM_MapK_CLR_SO, Table·CLR·ND)
+DEFINE_TYPE(TM_MapK_CLR_EA, Table·CLR·ND)
/* ---------------------------------------------------------
8. MapV (Values)
--------------------------------------------------------- */
-DEFINE_TYPE(TM_MapV_SR_ND, Table·SR·ND)
-DEFINE_TYPE(TM_MapV_SR_SO, Table·SR·ND)
-DEFINE_TYPE(TM_MapV_SR_EA, Table·SR·ND)
-DEFINE_TYPE(TM_MapV_SL_ND, Table·SL·ND)
-DEFINE_TYPE(TM_MapV_SL_SO, Table·SL·ND)
-DEFINE_TYPE(TM_MapV_SL_EA, Table·SL·ND)
+DEFINE_TYPE(TM_MapV_CR_ND, Table·CR·ND)
+DEFINE_TYPE(TM_MapV_CR_SO, Table·CR·ND)
+DEFINE_TYPE(TM_MapV_CR_EA, Table·CR·ND)
+DEFINE_TYPE(TM_MapV_CLR_ND, Table·CLR·ND)
+DEFINE_TYPE(TM_MapV_CLR_SO, Table·CLR·ND)
+DEFINE_TYPE(TM_MapV_CLR_EA, Table·CLR·ND)
/* ---------------------------------------------------------
9. ASCII (String)
--------------------------------------------------------- */
-DEFINE_TYPE(TM_ASCII_SR_ND, Table·SR·ND)
-DEFINE_TYPE(TM_ASCII_SR_SO, Table·SR·ND)
-DEFINE_TYPE(TM_ASCII_SR_EA, Table·SR·ND)
-DEFINE_TYPE(TM_ASCII_SL_ND, Table·SL·ND)
-DEFINE_TYPE(TM_ASCII_SL_SO, Table·SL·ND)
-DEFINE_TYPE(TM_ASCII_SL_EA, Table·SL·ND)
+DEFINE_TYPE(TM_ASCII_CR_ND, Table·CR·ND)
+DEFINE_TYPE(TM_ASCII_CR_SO, Table·CR·ND)
+DEFINE_TYPE(TM_ASCII_CR_EA, Table·CR·ND)
+DEFINE_TYPE(TM_ASCII_CLR_ND, Table·CLR·ND)
+DEFINE_TYPE(TM_ASCII_CLR_SO, Table·CLR·ND)
+DEFINE_TYPE(TM_ASCII_CLR_EA, Table·CLR·ND)
/* ---------------------------------------------------------
10. UTF8 (String)
--------------------------------------------------------- */
-DEFINE_TYPE(TM_UTF8_SR_ND, Table·SR·ND)
-DEFINE_TYPE(TM_UTF8_SR_SO, Table·SR·ND)
-DEFINE_TYPE(TM_UTF8_SR_EA, Table·SR·ND)
-DEFINE_TYPE(TM_UTF8_SL_ND, Table·SL·ND)
-DEFINE_TYPE(TM_UTF8_SL_SO, Table·SL·ND)
-DEFINE_TYPE(TM_UTF8_SL_EA, Table·SL·ND)
-
-/* ---------------------------------------------------------
- 11. BCD (String/Array)
- --------------------------------------------------------- */
-DEFINE_TYPE(TM_BCD_SR_ND, Table·SR·ND)
-DEFINE_TYPE(TM_BCD_SR_SO, Table·SR·ND)
-DEFINE_TYPE(TM_BCD_SR_EA, Table·SR·ND)
-DEFINE_TYPE(TM_BCD_SL_ND, Table·SL·ND)
-DEFINE_TYPE(TM_BCD_SL_SO, Table·SL·ND)
-DEFINE_TYPE(TM_BCD_SL_EA, Table·SL·ND)
+DEFINE_TYPE(TM_UTF8_CR_ND, Table·CR·ND)
+DEFINE_TYPE(TM_UTF8_CR_SO, Table·CR·ND)
+DEFINE_TYPE(TM_UTF8_CR_EA, Table·CR·ND)
+DEFINE_TYPE(TM_UTF8_CLR_ND, Table·CLR·ND)
+DEFINE_TYPE(TM_UTF8_CLR_SO, Table·CLR·ND)
+DEFINE_TYPE(TM_UTF8_CLR_EA, Table·CLR·ND)
/* ========================================================= */
@@ -532,48 +589,44 @@ PyMODINIT_FUNC PyInit_TM_module(void){
if(!m) return NULL;
/* Arr */
- ADD_TYPE(TM_Arr_SR_ND) ADD_TYPE(TM_Arr_SR_SO) ADD_TYPE(TM_Arr_SR_EA)
- ADD_TYPE(TM_Arr_SL_ND) ADD_TYPE(TM_Arr_SL_SO) ADD_TYPE(TM_Arr_SL_EA)
+ ADD_TYPE(TM_Arr_CR_ND) ADD_TYPE(TM_Arr_CR_SO) ADD_TYPE(TM_Arr_CR_EA)
+ ADD_TYPE(TM_Arr_CLR_ND) ADD_TYPE(TM_Arr_CLR_SO) ADD_TYPE(TM_Arr_CLR_EA)
/* ArrV */
- ADD_TYPE(TM_ArrV_SR_ND) ADD_TYPE(TM_ArrV_SR_SO) ADD_TYPE(TM_ArrV_SR_EA)
- ADD_TYPE(TM_ArrV_SL_ND) ADD_TYPE(TM_ArrV_SL_SO) ADD_TYPE(TM_ArrV_SL_EA)
+ ADD_TYPE(TM_ArrV_CR_ND) ADD_TYPE(TM_ArrV_CR_SO) ADD_TYPE(TM_ArrV_CR_EA)
+ ADD_TYPE(TM_ArrV_CLR_ND) ADD_TYPE(TM_ArrV_CLR_SO) ADD_TYPE(TM_ArrV_CLR_EA)
/* Gr */
- ADD_TYPE(TM_Gr_SR_ND) ADD_TYPE(TM_Gr_SR_SO) ADD_TYPE(TM_Gr_SR_EA)
- ADD_TYPE(TM_Gr_SL_ND) ADD_TYPE(TM_Gr_SL_SO) ADD_TYPE(TM_Gr_SL_EA)
+ ADD_TYPE(TM_Gr_CR_ND) ADD_TYPE(TM_Gr_CR_SO) ADD_TYPE(TM_Gr_CR_EA)
+ ADD_TYPE(TM_Gr_CLR_ND) ADD_TYPE(TM_Gr_CLR_SO) ADD_TYPE(TM_Gr_CLR_EA)
/* Glr */
- ADD_TYPE(TM_Glr_SR_ND) ADD_TYPE(TM_Glr_SR_SO) ADD_TYPE(TM_Glr_SR_EA)
- ADD_TYPE(TM_Glr_SL_ND) ADD_TYPE(TM_Glr_SL_SO) ADD_TYPE(TM_Glr_SL_EA)
+ ADD_TYPE(TM_Glr_CR_ND) ADD_TYPE(TM_Glr_CR_SO) ADD_TYPE(TM_Glr_CR_EA)
+ ADD_TYPE(TM_Glr_CLR_ND) ADD_TYPE(TM_Glr_CLR_SO) ADD_TYPE(TM_Glr_CLR_EA)
/* Set */
- ADD_TYPE(TM_Set_SR_ND) ADD_TYPE(TM_Set_SR_SO) ADD_TYPE(TM_Set_SR_EA)
- ADD_TYPE(TM_Set_SL_ND) ADD_TYPE(TM_Set_SL_SO) ADD_TYPE(TM_Set_SL_EA)
+ ADD_TYPE(TM_Set_CR_ND) ADD_TYPE(TM_Set_CR_SO) ADD_TYPE(TM_Set_CR_EA)
+ ADD_TYPE(TM_Set_CLR_ND) ADD_TYPE(TM_Set_CLR_SO) ADD_TYPE(TM_Set_CLR_EA)
/* Map */
- ADD_TYPE(TM_Map_SR_ND) ADD_TYPE(TM_Map_SR_SO) ADD_TYPE(TM_Map_SR_EA)
- ADD_TYPE(TM_Map_SL_ND) ADD_TYPE(TM_Map_SL_SO) ADD_TYPE(TM_Map_SL_EA)
+ ADD_TYPE(TM_Map_CR_ND) ADD_TYPE(TM_Map_CR_SO) ADD_TYPE(TM_Map_CR_EA)
+ ADD_TYPE(TM_Map_CLR_ND) ADD_TYPE(TM_Map_CLR_SO) ADD_TYPE(TM_Map_CLR_EA)
/* MapK */
- ADD_TYPE(TM_MapK_SR_ND) ADD_TYPE(TM_MapK_SR_SO) ADD_TYPE(TM_MapK_SR_EA)
- ADD_TYPE(TM_MapK_SL_ND) ADD_TYPE(TM_MapK_SL_SO) ADD_TYPE(TM_MapK_SL_EA)
+ ADD_TYPE(TM_MapK_CR_ND) ADD_TYPE(TM_MapK_CR_SO) ADD_TYPE(TM_MapK_CR_EA)
+ ADD_TYPE(TM_MapK_CLR_ND) ADD_TYPE(TM_MapK_CLR_SO) ADD_TYPE(TM_MapK_CLR_EA)
/* MapV */
- ADD_TYPE(TM_MapV_SR_ND) ADD_TYPE(TM_MapV_SR_SO) ADD_TYPE(TM_MapV_SR_EA)
- ADD_TYPE(TM_MapV_SL_ND) ADD_TYPE(TM_MapV_SL_SO) ADD_TYPE(TM_MapV_SL_EA)
+ ADD_TYPE(TM_MapV_CR_ND) ADD_TYPE(TM_MapV_CR_SO) ADD_TYPE(TM_MapV_CR_EA)
+ ADD_TYPE(TM_MapV_CLR_ND) ADD_TYPE(TM_MapV_CLR_SO) ADD_TYPE(TM_MapV_CLR_EA)
/* ASCII */
- ADD_TYPE(TM_ASCII_SR_ND) ADD_TYPE(TM_ASCII_SR_SO) ADD_TYPE(TM_ASCII_SR_EA)
- ADD_TYPE(TM_ASCII_SL_ND) ADD_TYPE(TM_ASCII_SL_SO) ADD_TYPE(TM_ASCII_SL_EA)
+ ADD_TYPE(TM_ASCII_CR_ND) ADD_TYPE(TM_ASCII_CR_SO) ADD_TYPE(TM_ASCII_CR_EA)
+ ADD_TYPE(TM_ASCII_CLR_ND) ADD_TYPE(TM_ASCII_CLR_SO) ADD_TYPE(TM_ASCII_CLR_EA)
/* UTF8 */
- ADD_TYPE(TM_UTF8_SR_ND) ADD_TYPE(TM_UTF8_SR_SO) ADD_TYPE(TM_UTF8_SR_EA)
- ADD_TYPE(TM_UTF8_SL_ND) ADD_TYPE(TM_UTF8_SL_SO) ADD_TYPE(TM_UTF8_SL_EA)
-
- /* BCD */
- ADD_TYPE(TM_BCD_SR_ND) ADD_TYPE(TM_BCD_SR_SO) ADD_TYPE(TM_BCD_SR_EA)
- ADD_TYPE(TM_BCD_SL_ND) ADD_TYPE(TM_BCD_SL_SO) ADD_TYPE(TM_BCD_SL_EA)
+ ADD_TYPE(TM_UTF8_CR_ND) ADD_TYPE(TM_UTF8_CR_SO) ADD_TYPE(TM_UTF8_CR_EA)
+ ADD_TYPE(TM_UTF8_CLR_ND) ADD_TYPE(TM_UTF8_CLR_SO) ADD_TYPE(TM_UTF8_CLR_EA)
/* Abstract */
if (PyType_Ready(&TMA_NaturalNumber·Type) < 0) return NULL;
diff --git a/developer/authored/TM_tutorial.py b/developer/authored/TM_tutorial.py
index 3af9f14..6ce3a0d 100755
--- a/developer/authored/TM_tutorial.py
+++ b/developer/authored/TM_tutorial.py
@@ -4,17 +4,17 @@ import sys
def print_tape(tm):
"""
- Prints tape contents using the First/Rest pattern and vertical comma format.
+ Prints tape contents using the First/Rest pattern.
+
+ WARNING: This function moves the head!
+ It rewinds to the start (LsR) and traverses to the end.
+ To preserve head state on ND machines, pass 'tm.e()'.
"""
- # Reset head to start
+ # Reset head to start (Leftmost)
tm.LsR()
- if tm.qR() and tm.qL():
- # Handle single element or empty case (qL returns true at start)
- # Check if we have at least one element by trying to read?
- # Actually qL and qR being true means 1 element or 0?
- # qL=True (at start). qR=True (at end). => 1 element.
- # We rely on 'r' to throw/work.
+ # Handle single element case
+ if tm.qR() and tm.LqR():
try:
print(tm.r())
except:
@@ -33,7 +33,7 @@ def print_tape(tm):
def tm_loops_tutorial():
print("--- 1. The Standard Traversal (Read -> Process -> Step) ---")
data = [10, 20, 30, 40, 50]
- tm = TM.TM_Arr_SR_ND(data)
+ tm = TM.TM_Arr_CR_ND(data)
while True:
val = tm.r()
@@ -42,7 +42,7 @@ def tm_loops_tutorial():
tm.s()
print("\n--- 2. The Accumulator (Initialized First) ---")
- tm = TM.TM_Arr_SR_ND([1, 2, 3, 4, 5])
+ tm = TM.TM_Arr_CR_ND([1, 2, 3, 4, 5])
total = tm.r() # Process First
while not tm.qR(): # Guard
@@ -51,51 +51,77 @@ def tm_loops_tutorial():
print(f"Total: {total}")
- print("\n--- 3. Destructive Filter (Entangled Copy) ---")
- # We use TM_ArrV_SR_SO because we need 'd' (delete).
- # We added 'e' (entangle) to the SO table to support this specific workflow.
+ print("\n--- 3. Destructive Filter (Variable Tape) ---")
+ # We use TM_ArrV_CLR_SO (Chiral Left Right, Solitary).
+ # We need CLR to support 'eLsd' (Delete Left Neighbor).
original_data = [1, 2, 3, 4, 5, 6, 7]
- tm_master = TM.TM_ArrV_SR_SO(original_data)
+ tm = TM.TM_ArrV_CLR_SO(original_data)
+
+ # 1. Process First (Index 0) - Special Case
+ # We check the start. If odd, we must delete it.
+ # Since we are at the start, we cannot use eLsd (no left neighbor).
+ # We must Step then eLsd? No, that deletes 0.
+ # If we Step, 0 becomes the left neighbor.
+
+ tm.LsR() # Ensure start
- # Create an entangled copy for the deletion work
- tm_worker = tm_master.e()
+ first_val = tm.r()
+ if first_val % 2 != 0:
+ print(f"Deleting odd start: {first_val}")
+ if tm.qR():
+ # Single element list. Cannot step.
+ print("(Cannot delete last remaining element)")
+ else:
+ tm.s() # Step to 1
+ tm.eLsd() # Delete Left Neighbor (Index 0)
+ # Head is now at Index 0 (the element that shifted down).
+ # We must NOT step in the loop immediately, or we skip the new First.
+ # To handle this cleanly in a loop, we align the head to "Previous" logic?
+ # Easier: Just restart logic from the new current.
+
+ # Since we deleted 0, the loop below starts at the *new* 0.
+ # We just need to make sure we don't double step.
+ # The simplest way is to handle the deletion, then let the loop
+ # run normally from the current head position.
- # Work on the worker
+ # Main Loop
+ # Invariant: Head is on a valid cell we want to inspect.
while True:
- val = tm_worker.r()
-
- if val % 2 != 0:
- print(f"Deleting odd: {val}")
- # Unguarded delete: We must guarantee we don't delete the final cell
- # in a way that breaks the invariant for the *other* machine if it were
- # elsewhere. But here they are entangled.
- # Note: If we delete the last element, the C logic steps back.
- tm_worker.d()
-
- # Since d() is in-place, the 'next' element slides into the current spot.
- # We do NOT step. But we must check if we hit the end of the tape
- # (i.e. we deleted the tail and stepped back, or the tape is empty).
-
- # Safety check for end of tape after delete
- if tm_worker.qR():
- # We are at the right edge. Check if the current (last) element
- # also needs deleting.
- val = tm_worker.r()
- if val % 2 != 0:
- print(f"Deleting last odd: {val}")
- try:
- tm_worker.d()
- except RuntimeError:
- print("(Tape became empty)")
- break
- break
- else:
- print(f"Keeping even: {val}")
- if tm_worker.qR(): break
- tm_worker.s()
+ val = tm.r()
+ if val % 2 != 0:
+ # Found odd. Delete it.
+ print(f"Deleting odd: {val}")
+
+ if not tm.qR():
+ # Case: Not at end.
+ # Step Right, then delete the item to our Left (which is 'val').
+ tm.s()
+ tm.eLsd()
+ # Head is now on the next item (shifted left into current slot).
+ # We continue loop to inspect *this* item.
+ else:
+ # Case: At End (Tail).
+ # Cannot use eLsd strategy because we can't step right.
+ # Must use esd strategy: Step Left, delete Right Neighbor.
+ if tm.LqR():
+ print("(Last item, cannot empty tape)")
+ break
+
+ tm.Ls() # Step Left
+ tm.esd() # Delete Right Neighbor (Tail)
+ # Tail deleted. We are at new tail.
+ # We inspected this even number already (presumably), so we break?
+ # Actually, if we stepped left, we are on a number we already kept.
+ break
+ else:
+ print(f"Keeping even: {val}")
+ if tm.qR(): break
+ tm.s()
print("Final Tape Content:")
- print_tape(tm_master)
+ # Pass the SO machine directly.
+ # This will consume/rewind the head, but that is acceptable here.
+ print_tape(tm)
if __name__ == "__main__":
tm_loops_tutorial()
diff --git a/developer/authored/build/temp.linux-x86_64-cpython-311/TM_module.o b/developer/authored/build/temp.linux-x86_64-cpython-311/TM_module.o
index d4993d1..0a4bc2f 100644
Binary files a/developer/authored/build/temp.linux-x86_64-cpython-311/TM_module.o and b/developer/authored/build/temp.linux-x86_64-cpython-311/TM_module.o differ
diff --git a/document/TM.html b/document/TM.html
index 13555e3..d6e2a40 100644
--- a/document/TM.html
+++ b/document/TM.html
@@ -14,7 +14,7 @@
@@ -42,10 +42,6 @@
A defining invariant of all Tape Machines is that cell contents are cargo. The TM reads and writes cell payload, but it does not interpret that payload as control. The decisions are made above the TM, by the programmerâs algorithm.
-
- A TM is constructed with a set of feature symbols that select which parts of the interface are available. When no feature symbols are supplied, the result is a non-destructive step right machine with no indexing. Feature symbols can enable step left behavior (via the mirror view), enable random access indexing functions, and accounting for a shared tape, among other things.
-
-
A first order TM is defined by a single invariant: the head is always on a valid cell. This invariant makes the primitive interface total: every move, read, write, and query has a clean, well defined meaning, and caller code stays free of scattered end case tests.
@@ -63,6 +59,68 @@
These are selected at TM construction time. In one approach, no methods are placed on the interface that can destroy tape cells. This supports a non-destructive programming paradigm, so such machines are said to be non-destructive. Without destructive operations, entangled machines simply do not have the ability to break each other. In a second approach, the interface is configured so there are no functions for entangling machines in the first place. Each solitary machine can safely perform destructive operations because no other machine shares its tape. As a third approach, each group of entangled machines shares a catalog listing all machines in the group. Caller code can guard operations by querying whether a planned operation is safe before attempting it. This third strategy is called entanglement accounting. During the time of execution of the guard and operation, each tape machine must be able to lockout others from access to the catalog. Yet another approach is for the programmer, compiler, or external function to prove code is safe, which is the same as proving that guard code is not needed. An alternative is to use a compiler that only produces correct code in the first place. This is the approach of correct by construction.
+ Machine Naming Convention
+
+
+ Concrete Tape Machine classes follow a strict naming grammar. This grammar acts as a feature descriptor, allowing the programmer to select a machine by constructing its name.
+
+
+
+ The name is composed of three feature segments: the Container Type, the Chirality, and the Entanglement Approach.
+
+
+ grammar
+
+ class_name :: TM [ _container ] [ _chirality ] [ _entanglement ]
+ container :: ARR | ARRV | GR | GLR | SET | MAP | MAPK | MAPV
+ | ASCII | UTF8
+ chirality :: CR | CLR
+ entanglement :: ND | SO | EA
+
+
+
+ Segments are separated by underscores. The default configuration for a segment is used if that segment is omitted.
+ The default machine is TM_ARR_CR_ND (Fixed Array, Chiral Right, Non-Destructive).
+
+
+ container
+
+ - ARR Array Fixed (Default). A contiguous sequence of fixed length (e.g., Python List, C Array).
+ - ARRV Array Variable. A contiguous sequence that may resize (e.g., Vector).
+ - GR Graph Right. A node-based sequence with a single right neighbor (Singly Linked List).
+ - GLR Graph Left Right. A node-based sequence with left and right neighbors (Doubly Linked List).
+ - SET Set. Iteration over the elements of a Set.
+ - MAP Map. Iteration over the items (Key-Value pairs) of a Map.
+ - MAPK Map Keys. Iteration over the keys of a Map.
+ - MAPV Map Values. Iteration over the values of a Map.
+ - ASCII Ascii. A sequence of 1-byte characters.
+ - UTF8 Utf8. A sequence of variable-width Unicode characters.
+
+
+ chirality
+
+ - CR Chiral Right (Default). The machine head can only be moved to the right.
+ - CLR Chiral Left Right. The machine is bidirectional, supports stepping either right or left.
+
+
+ entanglement
+
+ - ND Non-Destructive (Default). Safe for entanglement.
+ - SO Solitary. Exclusive ownership; supports destructive ops.
+ - EA Entanglement Accounting. Shared access guarded by a catalog.
+
+
+ Abstract and Traversal Machines
+
+ The naming convention extends to Abstract and Traversal machines using specific prefixes.
+
+
+ - TMA TM Abstract. Virtual tapes computed over state.
+
Example: TMA_Natural_Number (Iterates 0, 1, 2...).
+ - TMT TM Traversal. Algorithmic traversals over non-linear structures.
+
Example: TMT_Tree_DepthFirst (Linearizes a tree via stack/recursion).
+
+
Command language
@@ -75,12 +133,9 @@
- A TM is constructed with a set of feature symbols that select which parts of the language are available.
- When no feature symbols are supplied, the result is a non-destructive step right machine with no indexing.
- Feature symbols can enable step left behavior (via the mirror view) and can independently enable indexing commands.
+ The grammar selects which parts of the language are available based on the machine's configuration (CR or CLR).
-
For the non-destructive step right machine
grammar
@@ -119,7 +174,7 @@
The query command, q, reports head state.
With no arg descriptor it returns a boolean value.
- With an R arg descriptor it returns true iff the head is on the rightmost cell. With an I arg descriptor it returns an index.
+ With an R arg descriptor it returns true iff the head is on the rightmost cell.
@@ -145,7 +200,7 @@
- The n arg descriptor supplies a repetition count.
+ The n arg descriptor supplies a repetition count or implies a distance query.
@@ -171,6 +226,7 @@
Query
qR ; true iff head is on rightmost
+ qnR ; returns number of steps to rightmost
Copying to and from tape cells and python variables
@@ -195,9 +251,6 @@
- Step left support does not imply index space.
- A machine can support L while still omitting index operations.
- When index space is enabled as an additional feature, the I arg descriptor becomes available and index based cue and query forms become legal.
When L is available, n can also be negative, with the sign selecting the direction in the base view.
@@ -206,15 +259,13 @@
statement :: ( [machine] command [arg]* )+
machine :: L
command :: e | q | r | s | w
- arg :: R | I | n
+ arg :: R | n
- Index space, as defined in the TTCA book, is a natural number sequence machine that walks the tape of a base machine and establishes a one to one correspondence between natural numbers and tape cells.
- That correspondence belongs to the tape and stays fixed when we change the view.
- For example, consider a tape with cells [c_0 ,c_1 ,c_2 ,c_3 ,c_4], where c_4 is rightmost and has index 4.
+ Consider a tape with cells [c_0 ,c_1 ,c_2 ,c_3 ,c_4], where c_4 is rightmost.
In the mirror view we write the same cells as [c_4 ,c_3 ,c_2 ,c_1 ,c_0].
- In the mirror view c_4 sits at the left edge, and it still has index 4.
+ In the mirror view c_4 sits at the left edge.
A right step from c_4 in the mirror view lands on c_3, which matches a left step from c_4 in the base view.
@@ -232,48 +283,24 @@
sn(2) ; step to right neighbor twice
- Head movement relative to leftmost
-
- sI(0) ; cue the head to the leftmost cell by index
- sI(3) ; cue the head to tape[3]
-
-
-
- LsI(k) and sI(k) cue the head to the same indexed cell.
- L flips direction and the meaning of R, not the index to cell correspondence.
- Thus LsI(0) cues the cell with index zero, which is rightmost in the mirror view and leftmost in the base view.
-
-
Absolute head movement
LsR ; cue the head to leftmost
sR ; cue the head to rightmost
-
- Without code optimization LsR will be faster than sI(0), because sI requires passing and processing an argument.
-
-
Query
LqR ; head is on leftmost
qR ; head is on rightmost
- qI ; index of the head
- qIR ; index of rightmost
- LqIR ; index of leftmost, typically zero
+ qnR ; steps to rightmost
+ LqnR ; steps to leftmost
-
- The I arg descriptor causes q to return an index.
- By default q returns a boolean value.
- In other commands, an I arg descriptor indicates that an index argument is supplied.
-
-
The R arg descriptor focuses the command on the selected view rightmost cell.
For qR, the result is true iff the head is on rightmost.
- For qIR, the result is the index of rightmost.
- Under L, that rightmost cell is the base view leftmost cell, so LqIR returns the index of leftmost, typically zero.
+ Under L, that rightmost cell is the base view leftmost cell, so LqR returns true iff the head is on the leftmost cell.
Copying to and from tape cells and python variables
@@ -299,16 +326,7 @@
The L prefix is not implemented as a distinct family of primitives.
In the command language it selects the mirror view.
- In the API the same effect is provided by explicit left primitives such as ls() and lsn(n), or by allowing a signed n on machines that support step left.
-
-
- Feature symbols and primitive availability
-
-
- A TM is constructed with feature symbols that select which primitive families are present.
- With no feature symbols, the result is a non-destructive step right machine with no indexing.
- Enabling step left adds left motion primitives (or signed n support) and mirror view semantics in the command language.
- Indexing is an independent feature: when enabled, index based query and cue forms become legal and corresponding primitives become available.
+ In the API the same effect is provided by explicit left primitives such as Ls() and Lsn(n), or by allowing a signed n on machines that support step left.
Navigation primitives
@@ -331,14 +349,14 @@
- - ls() steps to the left neighbor. (Command form: Ls.)
- - lsn(n) steps left n times. (Command form: Lsn(n).)
- - lsR() cues the head to leftmost. (Command form: LsR.)
+ - Ls() steps to the left neighbor. (Command form: Ls.)
+ - Lsn(n) steps left n times. (Command form: Lsn(n).)
+ - LsR() cues the head to leftmost. (Command form: LsR.)
Note the correspondence: sR cues rightmost in the base view, and LsR cues leftmost of that same underlying tape.
- The primitives sR() and lsR() are optimized cues.
+ The primitives sR() and LsR() are optimized cues.
Input and output primitives
@@ -365,7 +383,7 @@
wn(list) writes a list of values.
-Destructive Primitives
+ Destructive Primitives
Machines configured with the SO (Solitary) entanglement approach may expose primitives that modify the tape structure.
@@ -376,17 +394,6 @@
Note: Not all container types support all destructive operations. For example, ARR (Fixed Array) does not support deletion or insertion, whereas ARRV (Variable Array) does.
- Delete (d)
-
- Removes cells from the tape. The head typically lands on the right neighbor of the deleted segment.
-
-
- - d(): Deletes the current cell.
- - dn(n): Deletes n cells starting from current.
- - dR(): Deletes from current to Rightmost (inclusive).
- - dL(): Deletes from current to Leftmost (inclusive).
-
-
Delete Neighbor (esd)
A compound-style primitive that deletes a neighbor without moving the active head.
@@ -394,14 +401,21 @@
- esd(): Deletes the immediate right neighbor. Guard: Requires not qR().
- - esdn(n): Deletes n right neighbors.
- - Lesd(): Deletes the immediate left neighbor. Guard: Requires not qL().
+ - eLsd(): Deletes the immediate left neighbor. Guard: Requires not LqR().
+
+
+ Delete to Bound (dR)
+
+ - dR(): Deletes from current to Rightmost (inclusive). Head steps left. Guard: Requires not LqR().
+ - LdR(): Deletes from current to Leftmost (inclusive). Head lands on right neighbor. Guard: Requires not qR().
- Insert/Append (a)
+ Insert/Append (esa)
- - a(v): Inserts value v at the current head position. The old current cell shifts right.
- - an(list): Splices a list of values at the current position.
+ - esa(v): Appends value v after the current head position.
+ - eLsa(v): Appends value v before the current head position.
+ - aR(v): Appends value v to the Rightmost end of the tape.
+ - LaR(v): Appends value v to the Leftmost end of the tape.
Query primitives
@@ -409,28 +423,23 @@
Query primitives are built from the q command.
On the step right only machine, the primitive set covers the boolean query forms.
- When indexing is enabled as an additional feature, index valued query forms become available.
- qR() returns whether the head is on the selected view rightmost cell. (Command form: qR.)
+ - qnR() returns number of steps to rightmost cell. (Command form: qnR.)
- If indexing is enabled, the API also exposes index query primitives.
- These correspond to the I arg descriptor in the command language.
+ Mirror view query forms follow the same rule in the command language.
+ For example, LqR tests the leftmost of the underlying tape.
-
+
- - qI() returns the current head index. (Command form: qI.)
- - qIR() returns the index of the selected view rightmost cell. (Command form: qIR.)
+ - LqR() returns whether the head is on the leftmost cell. (Command form: LqR.)
+ - LqnR() returns number of steps to leftmost cell. (Command form: LqnR.)
-
- Mirror view query forms follow the same rule in the command language.
- For example, LqR tests the leftmost of the underlying tape, and LqIR returns its index, typically zero.
-
-
Entanglement primitives
@@ -463,7 +472,7 @@
- In order to maintain these properties, a TM uses inclusive bounds for intervals, including intervals of cells, and intervals of indexes.
+ In order to maintain these properties, a TM uses inclusive bounds for intervals, including intervals of cells.
Contract with the programmer
@@ -550,7 +559,7 @@
The Region Machine implements the standard TM interface but constrains movement based on the boundary heads.
- - Step (s): The Active Head performs a step only if it is not currently on the Right Boundary cell.
- Step Left (ls): The Active Head performs a left step only if it is not currently on the Left Boundary cell.
- Query Bounds (qR, qL):
- qR() returns true if the Active Head is on the same cell as the Right Boundary Head.
- qL() returns true if the Active Head is on the same cell as the Left Boundary Head.
- Cue Bounds (sR, lsR):
- sR() moves the Active Head to the cell occupied by the Right Boundary Head.
- lsR() moves the Active Head to the cell occupied by the Left Boundary Head.
+ - Step (s): The Active Head performs a step only if it is not currently on the Right Boundary cell.
- Step Left (Ls): The Active Head performs a left step only if it is not currently on the Left Boundary cell.
- Query Bounds (qR, LqR):
- qR() returns true if the Active Head is on the same cell as the Right Boundary Head.
- LqR() returns true if the Active Head is on the same cell as the Left Boundary Head.
- Cue Bounds (sR, LsR):
- sR() moves the Active Head to the cell occupied by the Right Boundary Head.
- LsR() moves the Active Head to the cell occupied by the Left Boundary Head.
Boundary Management
@@ -612,70 +621,6 @@
Because the TMS implements the TM interface, it can be passed directly to these algorithms. However, if the status is Empty, the first operation attempted by the algorithm (usually r()) will trigger a "second order" error, alerting the programmer that they neglected the guard.
- Machine Naming Convention
-
-
- Concrete Tape Machine classes follow a strict naming grammar. This grammar acts as a feature descriptor, allowing the programmer to select a machine by constructing its name.
-
-
-
- The name is composed of three feature segments: the Container Type, the Step Direction, and the Entanglement Approach.
-
-
- grammar
-
- class_name :: TM [ _container ] [ _direction ] [ _entanglement ]
- container :: ARR | ARRV | GR | GLR | SET | MAP | MAPK | MAPV
- | ASCII | UTF8 | BCD
- direction :: SR | SL
- entanglement :: ND | SO | EA
-
-
-
- Segments are separated by underscores. The default configuration for a segment is used if that segment is omitted.
- The default machine is TM_ARR_SR_ND (Fixed Array, Step Right, Non-Destructive).
-
-
- container
-
- - ARR Array Fixed (Default). A contiguous sequence of fixed length (e.g., Python List, C Array).
- - ARRV Array Variable. A contiguous sequence that may resize (e.g., Vector).
- - GR Graph Right. A node-based sequence with a single right neighbor (Singly Linked List).
- - GLR Graph Left Right. A node-based sequence with left and right neighbors (Doubly Linked List).
- - SET Set. Iteration over the elements of a Set.
- - MAP Map. Iteration over the items (Key-Value pairs) of a Map.
- - MAPK Map Keys. Iteration over the keys of a Map.
- - MAPV Map Values. Iteration over the values of a Map.
- - ASCII Ascii. A sequence of 1-byte characters.
- - UTF8 Utf8. A sequence of variable-width Unicode characters.
- - BCD Bcd. A sequence of Binary Coded Decimal values.
-
-
- direction
-
- - SR Step Right (Default). The machine head can only be moved to the right.
- - SL Step Left. The machine is bidirectional, supports stepping either right or left.
-
-
- entanglement
-
- - ND Non-Destructive (Default). Safe for entanglement.
- - SO Solitary. Exclusive ownership; supports destructive ops.
- - EA Entanglement Accounting. Shared access guarded by a catalog.
-
-
- Abstract and Traversal Machines
-
- The naming convention extends to Abstract and Traversal machines using specific prefixes.
-
-
- - TMA TM Abstract. Virtual tapes computed over state.
-
Example: TMA_Natural_Number (Iterates 0, 1, 2...).
- - TMT TM Traversal. Algorithmic traversals over non-linear structures.
-
Example: TMT_Tree_DepthFirst (Linearizes a tree via stack/recursion).
-
-
-