From: Thomas Walker Lynch Date: Tue, 10 Feb 2026 08:57:41 +0000 (+0000) Subject: first try at the 66 first order TM variations X-Git-Url: https://git.reasoningtechnology.com/sitemap.xml?a=commitdiff_plain;h=a02daf651e089bfecf1aaeecddab4f0ead309709;p=Epimetheus%2F.git first try at the 66 first order TM variations --- diff --git a/developer/authored/TM.py b/developer/authored/TM.py index a20fb31..24a262e 100755 --- a/developer/authored/TM.py +++ b/developer/authored/TM.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 import sys -from Symbol import Symbol try: import TM_module @@ -9,71 +8,49 @@ except ImportError: sys.exit(1) # ========================================== -# 1. The TM Stub (Callable Proxy) +# TM Command Language (Explicit Types) # ========================================== -class TM_Factory_Stub: - """ - A Proxy object that acts as both: - 1. The Namespace Root (holding .feature) - 2. The Callable Factory (delegating to an implementation) - """ - def __init__(self): - self._impl = None - - def __call__(self, *args, **kwargs): - if self._impl is None: - raise TypeError("TM factory logic not yet bound.") - return self._impl(*args, **kwargs) - -# Create the persistent TM object immediately -TM = TM_Factory_Stub() - -# ========================================== -# 2. Define Features (Directly on TM) -# ========================================== - -# Create the namespace root -TM.feature = Symbol() - -# Allocating features directly onto the persistent object. -# Now we don't need temporary variables like _SR or _L. -TM.feature.SR = TM.feature.alloc() # Step Right -TM.feature.ND = TM.feature.alloc() # Non-Destructive -TM.feature.L = TM.feature.alloc() # Mirror View +# 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)) # ========================================== -# 3. The Implementation Logic +# Defaults (Aliasing) # ========================================== -def _tm_implementation(data_obj ,feature_seq=None): - """ - The actual factory logic. - Now allows us to refer to 'TM.feature' directly inside the function. - """ - # Optimization: Default Case - if not feature_seq: return TM_module.TM_Array_SR_ND(data_obj) - - # Normalize - fs = set(feature_seq) - - # Remove Defaults (Referring to the authoritative symbols directly) - fs.discard(TM.feature.SR) - fs.discard(TM.feature.ND) +# Pattern: TM_[Container]_[Direction]_[Entanglement] +# Default Container: Arr +# Default Entanglement: ND +# Default Direction: SR - # Check Empty - if not fs: return TM_module.TM_Array_SR_ND(data_obj) +# --- Global Default --- +TM = TM_module.TM_Arr_SR_ND - # Check Mirror View - if TM.feature.L in fs: - fs.remove(TM.feature.L) - if not fs: return TM_module.TM_Array_ND(data_obj) +# --- 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 - # Unknowns - raise ValueError(f"Unrecognized features: {fs}") +# --- Direction Defaults (Cont=Arr, Ent=ND) --- +TM_SR = TM_module.TM_Arr_SR_ND +TM_SL = TM_module.TM_Arr_SL_ND -# ========================================== -# 4. Bind the Logic -# ========================================== +# --- 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 -TM._impl = _tm_implementation +# --- Common Partials --- +TM_Arr_SL = TM_module.TM_Arr_SL_ND +TM_ASCII_SL = TM_module.TM_ASCII_SL_ND diff --git a/developer/authored/TM_module.c b/developer/authored/TM_module.c index 81be861..0330947 100644 --- a/developer/authored/TM_module.c +++ b/developer/authored/TM_module.c @@ -1,204 +1,511 @@ /* TM_module.c - CPython Extension: Tape Machine Types + CPython Extension: Tape Machine Factory + Implements: - - TM·Array_SR_ND - - TM·Array_ND + - 66 Concrete Machine Types (TM·[Container]·[Dir]·[Ent]) + - TMA_NaturalNumber (Abstract Machine) + + Namespaces: + - TM·Arr· : Implementation logic for Array/List backed machines. + - TM·Nat· : Implementation logic for the Abstract Natural Number machine. */ -/* #define · _ */ +/* 1. COMPILER COMPATIBILITY */ +/* If your compiler does not support UTF-8 identifiers, + uncomment the line below to map dot to underscore. +*/ +// #define · _ #define PY_SSIZE_T_CLEAN #include #include "structmember.h" -#include /* ========================================================= */ -/* 1. DATA LAYOUT */ +/* SHARED IMPLEMENTATION: ARRAY / LIST BACKED */ /* ========================================================= */ +/* TM·Arr (Instance Data) + Previously 'FastTM'. Represents the state for all array-backed types. +*/ typedef struct { PyObject_HEAD PyObject* tape_obj; /* The Container (List) */ - PyObject** head_ptr; /* Current Cell */ - PyObject** start_ptr; /* Leftmost Cell (Cache) */ - PyObject** end_ptr; /* Rightmost Cell + 1 (Sentinel) */ -} FastTM; + PyObject** head_ptr; /* Pointer to Current Item */ + PyObject** start_ptr; /* Pointer to Start */ + PyObject** end_ptr; /* Pointer to End (Sentinel) */ + Py_ssize_t head_idx; /* Index Cache (Required for Resizing containers) */ +} TM·Arr; -static void FastTM_dealloc(FastTM* self){ +static void TM·Arr·dealloc(TM·Arr* self){ Py_XDECREF(self->tape_obj); Py_TYPE(self)->tp_free((PyObject*)self); } -static int refresh_pointers(FastTM* self){ - if (!PyList_Check(self->tape_obj)) return -1; +static int TM·Arr·init(TM·Arr* self, PyObject* args, PyObject* kwds){ + PyObject* input_obj = NULL; + if( !PyArg_ParseTuple(args, "O", &input_obj) ) return -1; + + if( !PyList_Check(input_obj) ){ + PyErr_SetString(PyExc_TypeError, "This implementation requires a Python List."); + return -1; + } + if (PyList_Size(input_obj) == 0) { + PyErr_SetString(PyExc_ValueError, "TM cannot be empty."); + return -1; + } + + self->tape_obj = input_obj; + Py_INCREF(self->tape_obj); + + /* Initialize Pointers */ Py_ssize_t len = PyList_GET_SIZE(self->tape_obj); PyObject** items = ((PyListObject*)self->tape_obj)->ob_item; + self->start_ptr = items; self->end_ptr = items + len; - self->head_ptr = items; + self->head_ptr = items; + self->head_idx = 0; + return 0; } +/* --- Helper: Sync Pointers after Resize --- */ +static void TM·Arr·sync(TM·Arr* self){ + Py_ssize_t len = PyList_GET_SIZE(self->tape_obj); + PyObject** items = ((PyListObject*)self->tape_obj)->ob_item; + self->start_ptr = items; + self->end_ptr = items + len; + self->head_ptr = items + self->head_idx; +} + /* ========================================================= */ -/* 2. MIXINS */ +/* NAMESPACE: TM·Arr (Methods) */ /* ========================================================= */ -/* --- Navigation --- */ -static PyObject* Mixin·s(FastTM* self){ - self->head_ptr++; - Py_RETURN_NONE; +/* --- PRIMITIVES: NAVIGATION --- */ +static PyObject* TM·Arr·s(TM·Arr* self){ + self->head_ptr++; self->head_idx++; Py_RETURN_NONE; +} +static PyObject* TM·Arr·ls(TM·Arr* self){ + self->head_ptr--; self->head_idx--; Py_RETURN_NONE; +} +static PyObject* TM·Arr·sn(TM·Arr* self, PyObject* args){ + Py_ssize_t n; if(!PyArg_ParseTuple(args, "n", &n)) return NULL; + self->head_ptr += n; self->head_idx += n; Py_RETURN_NONE; +} +static PyObject* TM·Arr·lsn(TM·Arr* self, PyObject* args){ + Py_ssize_t n; if(!PyArg_ParseTuple(args, "n", &n)) return NULL; + self->head_ptr -= n; self->head_idx -= n; Py_RETURN_NONE; +} +static PyObject* TM·Arr·sR(TM·Arr* self){ + self->head_idx = (self->end_ptr - self->start_ptr) - 1; + self->head_ptr = self->end_ptr - 1; + Py_RETURN_NONE; +} +static PyObject* TM·Arr·LsR(TM·Arr* self){ + self->head_ptr = self->start_ptr; self->head_idx = 0; Py_RETURN_NONE; +} + +/* --- PRIMITIVES: I/O --- */ +static PyObject* TM·Arr·r(TM·Arr* self){ + PyObject* item = *(self->head_ptr); Py_INCREF(item); return item; } +static PyObject* TM·Arr·w(TM·Arr* self, PyObject* val){ + PyObject* old = *(self->head_ptr); Py_INCREF(val); *(self->head_ptr) = val; Py_DECREF(old); Py_RETURN_NONE; +} + +/* --- PRIMITIVES: QUERY --- */ +static PyObject* TM·Arr·qR(TM·Arr* self){ return (self->head_ptr >= self->end_ptr - 1) ? Py_True : Py_False; } +static PyObject* TM·Arr·qL(TM·Arr* self){ return (self->head_ptr <= self->start_ptr) ? Py_True : Py_False; } + + +/* --- PRIMITIVES: DESTRUCTIVE (Variable Array Only) --- */ -static PyObject* Mixin·sn(FastTM* self, PyObject* arg_tuple){ - Py_ssize_t n_val; - if( !PyArg_ParseTuple(arg_tuple, "n", &n_val) ) return NULL; - self->head_ptr += n_val; +/* d(): Delete current cell */ +static PyObject* TM·Arr·d(TM·Arr* self){ + if (PyList_SetSlice(self->tape_obj, self->head_idx, self->head_idx+1, NULL) < 0) return NULL; + + TM·Arr·sync(self); + + /* Safety: If we deleted the last element */ + if (self->head_ptr >= self->end_ptr && self->head_idx > 0) { + if (self->start_ptr == self->end_ptr) { + PyErr_SetString(PyExc_RuntimeError, "TM Empty: First Order Invariant Broken."); + return NULL; + } + self->head_idx--; + self->head_ptr--; + } Py_RETURN_NONE; } -static PyObject* Mixin·ls(FastTM* self){ - self->head_ptr--; +/* a(v): Insert value at current head */ +static PyObject* TM·Arr·a(TM·Arr* self, PyObject* val){ + if (PyList_Insert(self->tape_obj, self->head_idx, val) < 0) return NULL; + TM·Arr·sync(self); Py_RETURN_NONE; } -static PyObject* Mixin·LsR(FastTM* self){ - self->head_ptr = self->start_ptr; +/* esd(): Delete Right Neighbor */ +static PyObject* TM·Arr·esd(TM·Arr* self){ + if (self->head_ptr >= self->end_ptr - 1) { + PyErr_SetString(PyExc_IndexError, "esd: No right neighbor."); + return NULL; + } + if (PyList_SetSlice(self->tape_obj, self->head_idx+1, self->head_idx+2, NULL) < 0) return NULL; + TM·Arr·sync(self); Py_RETURN_NONE; } -static PyObject* Mixin·sR(FastTM* self){ - self->head_ptr = self->end_ptr - 1; +/* Lesd(): Delete Left Neighbor */ +static PyObject* TM·Arr·Lesd(TM·Arr* self){ + if (self->head_ptr <= self->start_ptr) { + PyErr_SetString(PyExc_IndexError, "Lesd: No left neighbor."); + return NULL; + } + /* Delete at head_idx - 1 */ + if (PyList_SetSlice(self->tape_obj, self->head_idx-1, self->head_idx, NULL) < 0) return NULL; + + self->head_idx--; /* We shifted left */ + TM·Arr·sync(self); Py_RETURN_NONE; } -/* --- I/O --- */ -static PyObject* Mixin·r(FastTM* self){ - PyObject* item = *(self->head_ptr); - Py_INCREF(item); - return item; + +/* ========================================================= */ +/* METHOD TABLES */ +/* ========================================================= */ + +/* 1. NON-DESTRUCTIVE (ND) TABLES */ + +static PyMethodDef Table·SR·ND[] = { + {"s", (PyCFunction)TM·Arr·s, METH_NOARGS, ""}, + {"sn",(PyCFunction)TM·Arr·sn,METH_VARARGS,""}, + {"r", (PyCFunction)TM·Arr·r, METH_NOARGS, ""}, + {"w", (PyCFunction)TM·Arr·w, METH_O, ""}, + {"qR",(PyCFunction)TM·Arr·qR,METH_NOARGS, ""}, + {"LsR",(PyCFunction)TM·Arr·LsR,METH_NOARGS,""}, + {NULL} +}; + +static PyMethodDef Table·SL·ND[] = { + {"s", (PyCFunction)TM·Arr·s, METH_NOARGS, ""}, + {"sn",(PyCFunction)TM·Arr·sn,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, ""}, + {"sR",(PyCFunction)TM·Arr·sR,METH_NOARGS, ""}, + {"LsR",(PyCFunction)TM·Arr·LsR,METH_NOARGS,""}, + {NULL} +}; + +/* 2. DESTRUCTIVE (SO) TABLES - For ArrV */ + +static PyMethodDef Table·SR·SO[] = { + {"s", (PyCFunction)TM·Arr·s, METH_NOARGS, ""}, + {"sn",(PyCFunction)TM·Arr·sn,METH_VARARGS,""}, + {"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, ""}, + {"esd",(PyCFunction)TM·Arr·esd,METH_NOARGS,""}, + {"qR",(PyCFunction)TM·Arr·qR,METH_NOARGS, ""}, + {"LsR",(PyCFunction)TM·Arr·LsR,METH_NOARGS,""}, + {NULL} +}; + +static PyMethodDef Table·SL·SO[] = { + {"s", (PyCFunction)TM·Arr·s, 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, ""}, + {"esd",(PyCFunction)TM·Arr·esd,METH_NOARGS,""}, + {"Lesd",(PyCFunction)TM·Arr·Lesd,METH_NOARGS,""}, + {"qR",(PyCFunction)TM·Arr·qR,METH_NOARGS, ""}, + {"qL",(PyCFunction)TM·Arr·qL,METH_NOARGS, ""}, + {"sR",(PyCFunction)TM·Arr·sR,METH_NOARGS, ""}, + {"LsR",(PyCFunction)TM·Arr·LsR,METH_NOARGS,""}, + {NULL} +}; + +/* ========================================================= */ +/* IMPLEMENTATION: TMA·NaturalNumber (TM·Nat·) */ +/* ========================================================= */ + +typedef struct { + PyObject_HEAD + unsigned long long state; +} TM·Nat; + +static int TM·Nat·init(TM·Nat* self, PyObject* args, PyObject* kwds){ + self->state = 0; + return 0; } -static PyObject* Mixin·w(FastTM* self, PyObject* val_obj){ - PyObject* old_val = *(self->head_ptr); - Py_INCREF(val_obj); - *(self->head_ptr) = val_obj; - Py_DECREF(old_val); +static PyObject* TM·Nat·s(TM·Nat* self){ + self->state++; Py_RETURN_NONE; } -/* --- Queries --- */ -static PyObject* Mixin·qR(FastTM* self){ - if( self->head_ptr >= (self->end_ptr - 1) ) Py_RETURN_TRUE; - Py_RETURN_FALSE; +static PyObject* TM·Nat·sn(TM·Nat* self, PyObject* args){ + unsigned long long n; + if(!PyArg_ParseTuple(args, "K", &n)) return NULL; + self->state += n; + Py_RETURN_NONE; } -static PyObject* Mixin·qL(FastTM* self){ - if( self->head_ptr <= self->start_ptr ) Py_RETURN_TRUE; - Py_RETURN_FALSE; +static PyObject* TM·Nat·ls(TM·Nat* self){ + if(self->state > 0) self->state--; + Py_RETURN_NONE; } -static PyObject* Mixin·address(FastTM* self){ - Py_ssize_t idx = self->head_ptr - self->start_ptr; - return PyLong_FromSsize_t(idx); +static PyObject* TM·Nat·LsR(TM·Nat* self){ + self->state = 0; + Py_RETURN_NONE; } -/* ========================================================= */ -/* 3. INITIALIZATION */ -/* ========================================================= */ +static PyObject* TM·Nat·r(TM·Nat* self){ + return PyLong_FromUnsignedLongLong(self->state); +} -static int Common·init(FastTM* self, PyObject* args, PyObject* kwds){ - PyObject* input_obj = NULL; - /* Accepts 1 argument: The List */ - if( !PyArg_ParseTuple(args, "O", &input_obj) ) return -1; - - if( !PyList_Check(input_obj) || PyList_Size(input_obj) == 0 ){ - PyErr_SetString(PyExc_ValueError, "TM requires non-empty list."); - return -1; - } +static PyObject* TM·Nat·w(TM·Nat* self, PyObject* val){ + PyErr_SetString(PyExc_TypeError, "Cannot write to Abstract Natural Number tape."); + return NULL; +} - self->tape_obj = input_obj; - Py_INCREF(self->tape_obj); - refresh_pointers(self); - return 0; +static PyObject* TM·Nat·qR(TM·Nat* self){ + Py_RETURN_FALSE; } -/* ========================================================= */ -/* 4. TYPES */ -/* ========================================================= */ +static PyObject* TM·Nat·qL(TM·Nat* self){ + if(self->state == 0) Py_RETURN_TRUE; + Py_RETURN_FALSE; +} -/* TM·Array_SR_ND */ -static PyMethodDef TM·Array_SR_ND·methods[] = { - {"s", (PyCFunction)Mixin·s, METH_NOARGS, ""}, - {"sn", (PyCFunction)Mixin·sn, METH_VARARGS, ""}, - {"r", (PyCFunction)Mixin·r, METH_NOARGS, ""}, - {"w", (PyCFunction)Mixin·w, METH_O, ""}, - {"qR", (PyCFunction)Mixin·qR, METH_NOARGS, ""}, - {"LsR", (PyCFunction)Mixin·LsR, METH_NOARGS, ""}, - {"address", (PyCFunction)Mixin·address, METH_NOARGS, ""}, +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, ""}, + {"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, ""}, + {"LsR", (PyCFunction)TM·Nat·LsR, METH_NOARGS, ""}, {NULL} }; -static PyTypeObject TM·Array_SR_ND·Type = { +static PyTypeObject TMA_NaturalNumber·Type = { PyVarObject_HEAD_INIT(NULL, 0) - .tp_name = "TM_module.TM_Array_SR_ND", - .tp_doc = "Array TM: Pointer-based, Solitary, SR, ND", - .tp_basicsize = sizeof(FastTM), + .tp_name = "TM_module.TMA_NaturalNumber", + .tp_doc = "Abstract TM: Natural Numbers", + .tp_basicsize = sizeof(TM·Nat), .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, .tp_new = PyType_GenericNew, - .tp_init = (initproc)Common·init, - .tp_dealloc = (destructor)FastTM_dealloc, - .tp_methods = TM·Array_SR_ND·methods, + .tp_init = (initproc)TM·Nat·init, + .tp_methods = TM·Nat·methods, }; -/* TM·Array_ND */ -static PyMethodDef TM·Array_ND·methods[] = { - {"s", (PyCFunction)Mixin·s, METH_NOARGS, ""}, - {"sn", (PyCFunction)Mixin·sn, METH_VARARGS, ""}, - {"ls", (PyCFunction)Mixin·ls, METH_NOARGS, ""}, - {"sR", (PyCFunction)Mixin·sR, METH_NOARGS, ""}, - {"LsR", (PyCFunction)Mixin·LsR, METH_NOARGS, ""}, - {"qR", (PyCFunction)Mixin·qR, METH_NOARGS, ""}, - {"qL", (PyCFunction)Mixin·qL, METH_NOARGS, ""}, - {"r", (PyCFunction)Mixin·r, METH_NOARGS, ""}, - {"w", (PyCFunction)Mixin·w, METH_O, ""}, - {"address", (PyCFunction)Mixin·address, METH_NOARGS, ""}, - {NULL} -}; -static PyTypeObject TM·Array_ND·Type = { - PyVarObject_HEAD_INIT(NULL, 0) - .tp_name = "TM_module.TM_Array_ND", - .tp_doc = "Array TM: Pointer-based, Solitary, Bidirectional, ND", - .tp_basicsize = sizeof(FastTM), - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, - .tp_new = PyType_GenericNew, - .tp_init = (initproc)Common·init, - .tp_dealloc = (destructor)FastTM_dealloc, - .tp_methods = TM·Array_ND·methods, +/* ========================================================= */ +/* TYPE DEFINITIONS (The 66 Concrete Types) */ +/* ========================================================= */ + +#define DEFINE_TYPE(NAME, METHODS) \ +static PyTypeObject NAME##·Type = { \ + PyVarObject_HEAD_INIT(NULL, 0) \ + .tp_name = "TM_module." #NAME, \ + .tp_basicsize = sizeof(TM·Arr), \ + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, \ + .tp_new = PyType_GenericNew, \ + .tp_init = (initproc)TM·Arr·init, \ + .tp_dealloc = (destructor)TM·Arr·dealloc, \ + .tp_methods = METHODS, \ }; +/* --------------------------------------------------------- + 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_SL_ND, Table·SL·ND) +DEFINE_TYPE(TM_Arr_SL_SO, Table·SL·ND) +DEFINE_TYPE(TM_Arr_SL_EA, Table·SL·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_SL_ND, Table·SL·ND) +DEFINE_TYPE(TM_ArrV_SL_SO, Table·SL·SO) /* Destructive */ +DEFINE_TYPE(TM_ArrV_SL_EA, Table·SL·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) + +/* --------------------------------------------------------- + 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) + +/* --------------------------------------------------------- + 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) + +/* --------------------------------------------------------- + 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) + +/* --------------------------------------------------------- + 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) + +/* --------------------------------------------------------- + 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) + +/* --------------------------------------------------------- + 9. ASCII (String) - Acronym + --------------------------------------------------------- */ +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) + +/* --------------------------------------------------------- + 10. UTF8 (String) - Acronym + --------------------------------------------------------- */ +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) - Acronym + --------------------------------------------------------- */ +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) + + /* ========================================================= */ -/* 5. MODULE INIT */ +/* MODULE INIT */ /* ========================================================= */ static PyModuleDef TM_module = { - PyModuleDef_HEAD_INIT, "TM_module", "Fast TM Types", -1, NULL + PyModuleDef_HEAD_INIT, "TM_module", "TM Type Exports", -1, NULL }; -PyMODINIT_FUNC PyInit_TM_module(void){ - PyObject* m_obj; - if( PyType_Ready(&TM·Array_SR_ND·Type) < 0 ) return NULL; - if( PyType_Ready(&TM·Array_ND·Type) < 0 ) return NULL; +#define ADD_TYPE(NAME) \ + if (PyType_Ready(&NAME##·Type) < 0) return NULL; \ + Py_INCREF(&NAME##·Type); \ + PyModule_AddObject(m, #NAME, (PyObject*)&NAME##·Type); - m_obj = PyModule_Create(&TM_module); - if( !m_obj ) return NULL; +PyMODINIT_FUNC PyInit_TM_module(void){ + PyObject* m = PyModule_Create(&TM_module); + if(!m) return NULL; - Py_INCREF(&TM·Array_SR_ND·Type); - PyModule_AddObject(m_obj, "TM_Array_SR_ND", (PyObject*)&TM·Array_SR_ND·Type); - - Py_INCREF(&TM·Array_ND·Type); - PyModule_AddObject(m_obj, "TM_Array_ND", (PyObject*)&TM·Array_ND·Type); + /* 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) - return m_obj; + /* 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) + + /* 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) + + /* 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) + + /* 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) + + /* 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) + + /* 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) + + /* 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) + + /* 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) + + /* 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) + + /* Abstract */ + if (PyType_Ready(&TMA_NaturalNumber·Type) < 0) return NULL; + Py_INCREF(&TMA_NaturalNumber·Type); + PyModule_AddObject(m, "TMA_NaturalNumber", (PyObject*)&TMA_NaturalNumber·Type); + + return m; } 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 a5bf02c..3c8313f 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/developer/authored/example_machines.py b/developer/authored/example_machines.py new file mode 100755 index 0000000..1bed6b4 --- /dev/null +++ b/developer/authored/example_machines.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +import TM as tm_lib + +def example_machine_grammar(): + print("--- TM Machine Grammar Example ---") + + data = [10, 20, 30, 40, 50] + + # 1. The Default Machine (TM_Arr_SR_ND) + t_def = tm_lib.TM(data) + print(f"\n[Default] {type(t_def).__name__}") + print(f" Read: {t_def.r()}") + t_def.s() + print(f" Step, Read: {t_def.r()}") + + # 2. The Mirror Machine (TM_Arr_SL_ND) + t_mir = tm_lib.TM_SL(data) + print(f"\n[Mirror] {type(t_mir).__name__}") + t_mir.sR() + print(f" Cue Rightmost, Read: {t_mir.r()} (Exp: 50)") + t_mir.ls() + print(f" Left Step, Read: {t_mir.r()} (Exp: 40)") + + # 3. Abstract Natural Number Machine (TMA) + t_nat = tm_lib.TMA_NaturalNumber() + print(f"\n[Abstract] {type(t_nat).__name__}") + print(f" Read: {t_nat.r()} (Exp: 0)") + t_nat.sn(1000) + print(f" Step 1000, Read: {t_nat.r()} (Exp: 1000)") + + # 4. Explicit Grammar Construction + # TM_ASCII_SR_SO (ASCII, Step Right, Solitary) + # Uses the Array backing but with ASCII type name + ascii_data = [65, 66, 67] # A, B, C + t_asc = tm_lib.TM_ASCII_SR_SO(ascii_data) + print(f"\n[Grammar Explicit] {type(t_asc).__name__}") + print(f" Read: {t_asc.r()} (Exp: 65)") + +if __name__ == "__main__": + example_machine_grammar() diff --git a/developer/deprecated_examples/example_TM_0.py b/developer/deprecated_examples/example_TM_0.py new file mode 100755 index 0000000..46b06c5 --- /dev/null +++ b/developer/deprecated_examples/example_TM_0.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python3 +import sys +from TM import TM ,Topology + +def CLI(): + print("--- TM Happy Path (example_TM_0) ---") + + # 1. Initialization + # --------------------------------------------------------- + print("\n[1] Initialization") + data_list = [10 ,20 ,30 ,40 ,50] + tm_0 = TM(data_list) + print(f"Created TM with: {data_list}") + print(f"Address: {tm_0.address()} (Should be 0)") + print(f"Length: {tm_0.len()} (Should be 5)") + + # 2. Read / Step / Write + # --------------------------------------------------------- + print("\n[2] Movement and IO") + + # Read current (0) + val = tm_0.r() + print(f"r(): {val} (Exp: 10)") + + # Step 1, Read + tm_0.s() + print(f"s() -> r(): {tm_0.r()} (Exp: 20)") + + # Write + tm_0.w(99) + print(f"w(99) -> r(): {tm_0.r()} (Exp: 99)") + + # Step N + tm_0.sn(2) + print(f"sn(2) -> r(): {tm_0.r()} (Exp: 40)") # 0->1->3 (Indices: 0, 1, 2, 3) + + # Step Left + tm_0.ls() + print(f"ls() -> r(): {tm_0.r()} (Exp: 30)") + + # Bulk Write (wn) + # Current head is at index 2 (val 30) + # Write [33, 44] -> overwrites 30, 40 + tm_0.wn([33 ,44]) + print("wn([33 ,44])") + + # Bulk Read (rn) + read_back = tm_0.rn(2) + print(f"rn(2): {read_back} (Exp: [33, 44])") + + # 3. Allocation + # --------------------------------------------------------- + print("\n[3] Allocation") + + # Append Right (aR) + tm_0.aR(60) + print(f"aR(60) -> len: {tm_0.len()} (Exp: 6)") + + # Append Left (aL) + # Head is at index 2. + # aL inserts at 0. Indices shift right. + # Head should increment to 3 to stay on '33'. + print(f"Pre-aL Head: {tm_0.address()}") + tm_0.aL(0) + print(f"aL(0) -> Head: {tm_0.address()} (Exp: 3)") + print(f"Value at Head: {tm_0.r()} (Exp: 33)") + + # Check 0 index + tm_0.lsn(3) # Go to 0 + print(f"Value at 0: {tm_0.r()} (Exp: 0)") + + # 4. Deletion + # --------------------------------------------------------- + print("\n[4] Deletion") + + # Current Tape: [0, 10, 99, 33, 44, 50, 60] + # Head at 0. + + # esd (Delete Neighbor -> 10) + tm_0.esd() + print(f"esd() -> Tape[1] should be 99. r(2): {tm_0.rn(2)} (Exp: [0, 99])") + + # d (Delete Current -> 0) + tm_0.d() + # Head stays at 0, which is now 99 + print(f"d() -> Current: {tm_0.r()} (Exp: 99)") + + # 5. Cloning (Entanglement) + # --------------------------------------------------------- + print("\n[5] Cloning") + tm_clone = tm_0.e() + print("Created tm_clone from tm_0") + + # Modify tm_0, check tm_clone + tm_0.w(999) + print(f"tm_0.w(999)") + print(f"tm_clone.r(): {tm_clone.r()} (Exp: 999)") + + print("\n--- end of examples ---") + +if __name__ == "__main__": + CLI() + + diff --git a/developer/deprecated_examples/example_TM_1.py b/developer/deprecated_examples/example_TM_1.py new file mode 100755 index 0000000..73795d2 --- /dev/null +++ b/developer/deprecated_examples/example_TM_1.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 +import sys +from TM import TM + +def CLI(): + print("--- TM Edge Cases (example_TM_1) ---") + + # 1. Entanglement Violation (Safety Check) + # --------------------------------------------------------- + print("\n[1] Entanglement Violation (Peer on Victim)") + + t1 = TM(['A' ,'B' ,'C']) + t2 = t1.e() # Entangled Clone + + # Setup: + # t1 at 0 ('A') + # t2 moves to 1 ('B') + t2.s() + print(f"Setup: t1@{t1.address()}, t2@{t2.address()}") + + print("Action: t1.esd() -> Tries to delete 'B'") + print("Expect: RuntimeError (Entanglement Violation)") + + try: + t1.esd() + print("!! FAILED: Operation succeeded (Should have failed)") + except RuntimeError as e: + print(f">> CAUGHT EXPECTED ERROR: {e}") + + # 2. Entanglement Violation (Peer on Current) + # --------------------------------------------------------- + print("\n[2] Entanglement Violation (Peer on Current)") + + # t1 at 0. t2 at 1. + # Move t2 back to 0. Both at 0. + t2.ls() + print(f"Setup: t1@{t1.address()}, t2@{t2.address()}") + + print("Action: t1.d() -> Tries to delete 'A'") + print("Expect: RuntimeError") + + try: + t1.d() + print("!! FAILED: Operation succeeded") + except RuntimeError as e: + print(f">> CAUGHT EXPECTED ERROR: {e}") + + # 3. Safe Deletion (No Peer Collision) + # --------------------------------------------------------- + print("\n[3] Safe Deletion (Peer Safe)") + + # t1 at 0 ('A'), t2 at 0 ('A'). + # Move t2 to 2 ('C'). + t2.sn(2) + print(f"Setup: t1@{t1.address()} ('A'), t2@{t2.address()} ('C')") + + # t1 deletes 'B' (neighbor). 'B' is at index 1. + # t2 is at index 2. Safe? + # Yes. t2 is not ON the cell being deleted. + # Note: t2's data will shift left index-wise, but Entanglement check + # only cares if t2 is *on* the deleted cell. + + print("Action: t1.esd() -> Delete 'B'") + t1.esd() + print(">> Success (Operation Permitted)") + print(f"Tape is now: {t1.rn(2)}") + + # 4. Map Input (Materialization) + # --------------------------------------------------------- + print("\n[4] Map Input") + data_map = {'key1': 1 , 'key2': 2} + tm_map = TM(data_map) + print(f"TM from Map keys: {tm_map.rn(2)}") + + print("\n--- Finished ---") + +if __name__ == "__main__": + CLI() + diff --git a/developer/deprecated_examples/example_TM_ND.py b/developer/deprecated_examples/example_TM_ND.py new file mode 100755 index 0000000..2b54cd2 --- /dev/null +++ b/developer/deprecated_examples/example_TM_ND.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +from TM import TM + +def example_differentiation(): + print("--- TM Factory Differentiation Example ---") + + data = [10, 20, 30] + + # 1. Default (No Features) + t1 = TM(data) + print(f"\nRequest: Default") + print(f"Result Type: {type(t1).__name__} (Expected: TM_Array_SR_ND)") + + # 2. Mirror View (Feature L) + # Use the underscore syntax + feat_L = TM.feature_L + + t2 = TM(data, [feat_L]) + print(f"\nRequest: Feature L ({feat_L})") + print(f"Result Type: {type(t2).__name__} (Expected: TM_Array_ND)") + + # 3. Explicit Defaults (Should be ignored/stripped) + feat_SR = TM.feature_SR + t3 = TM(data, [feat_SR, feat_L]) + print(f"\nRequest: Features SR + L") + print(f"Result Type: {type(t3).__name__} (Expected: TM_Array_ND)") + +if __name__ == "__main__": + example_differentiation() diff --git a/developer/deprecated_examples/example_TM_SR_ND.py b/developer/deprecated_examples/example_TM_SR_ND.py new file mode 100755 index 0000000..7b0d145 --- /dev/null +++ b/developer/deprecated_examples/example_TM_SR_ND.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +from TM import TM, Features + +def example_bidirectional_machine(): + print("--- TM·Array_ND (Bidirectional) Example ---") + + data = [10, 20, 30, 40, 50] + + # Request 'L' feature to get the ND machine + tm = TM(data, [Features.L]) + print(f"Created TM with Feature 'L'. Data: {data}") + + # 1. Forward to Middle + tm.sn(2) + print(f"sn(2) -> Read: {tm.r()} (Exp: 30)") + + # 2. Left Step (ls) - Only available on ND + print("\n[Left Step]") + tm.ls() + print(f"ls() -> Read: {tm.r()} (Exp: 20)") + + # 3. Query Left (qL) + print("\n[Query Left]") + print(f"At 20. qL(): {tm.qL()} (Exp: False)") + tm.ls() # At 10 (Start) + print(f"ls() -> At 10. qL(): {tm.qL()} (Exp: True)") + + # 4. Cue Rightmost (sR) + print("\n[Cue Rightmost]") + tm.sR() + print(f"sR() -> Read: {tm.r()} (Exp: 50)") + print(f"qR(): {tm.qR()} (Exp: True)") + +if __name__ == "__main__": + example_bidirectional_machine() diff --git a/developer/deprecated_examples/example_TM_SR_ND_aR.py b/developer/deprecated_examples/example_TM_SR_ND_aR.py new file mode 100755 index 0000000..290774a --- /dev/null +++ b/developer/deprecated_examples/example_TM_SR_ND_aR.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +from TM import TMS, Features + +def test_ar_machine(): + print("--- TM_ARRAY_SR_ND_aR Verification ---") + + # 1. Initialization with Feature (Mandatory Data) + data = [10, 20, 30] + t = TMS(data, features=[Features.APPEND_RIGHT]) + + print(f"Created Machine t1. Len: {t.len()}") + + # 2. Test Append on t1 + t.s() # Step to 20 + print("Executing t1.aR(99)...") + t.aR(99) + print(f"t1 Len: {t.len()} (Expected: 4)") + print(f"t1 Content from Head: {t.rn(3)} (Expected: [20, 30, 99])") + + # 3. Test Entanglement (Shared Tape) + # --------------------------------------------------------- + t2 = t.e() + print("\nCreated entangled machine t2 from t1.") + + print("Executing t2.aR(100)...") + t2.aR(100) + + # Verify t2 (The actor) + print(f"t2 Len: {t2.len()} (Expected: 5)") + + # Verify t1 (The observer) - PROOF OF ENTANGLEMENT + # t1 should see the change immediately because the tape is shared. + print(f"t1 Len: {t.len()} (Expected: 5)") + + if t.len() == 5: + print(">> SUCCESS: t1 reflects change made by t2 (Tape is shared).") + else: + print("!! FAIL: t1 did not see the change (Tape is copied?).") + + # 4. Verify Base Restrictions + try: + t.ls() + print("!! FAIL: ls() succeeded") + except AttributeError: + print(">> SUCCESS: ls() blocked.") + +if __name__ == "__main__": + test_ar_machine() diff --git a/developer/deprecated_examples/example_features.py b/developer/deprecated_examples/example_features.py new file mode 100755 index 0000000..7233dc6 --- /dev/null +++ b/developer/deprecated_examples/example_features.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +from TM import TM, TMS, Features + +def test_base_machine(): + print("\n--- Test 1: Base Machine (TM_SR_ND) ---") + data = [10, 20, 30] + + # No features requested + t = TMS(data) + print(f"Created Base Machine: {t.len()} items.") + + # 1. Step & Read (Should work) + t.s() + print(f"Step -> Val: {t.r()} (Expected: 20)") + + # 2. Try Append (Should FAIL) + print("Attempting aR()...") + try: + t.aR(99) + print("!! FAIL: aR() succeeded on Base Machine") + except AttributeError: + print(">> SUCCESS: Caught expected AttributeError (Feature missing)") + +def test_ar_machine(): + print("\n--- Test 2: Feature Machine (TM_SR_ND_AR) ---") + data = [10, 20, 30] + + # Feature requested + t = TMS(data, features=[Features.APPEND_RIGHT]) + print(f"Created Feature Machine: {t.len()} items.") + + # 1. Step + t.s() + + # 2. Append (Should WORK) + print("Attempting aR(99)...") + try: + t.aR(99) + print(f">> SUCCESS: aR() completed. New Len: {t.len()}") + + # Verify data + print(f"Tape content (from head): {t.rn(5)}") + except AttributeError as e: + print(f"!! FAIL: {e}") + + # 3. Entanglement Check + print("Entangling...") + t2 = t.e() + print("Attempting aR(100) on Clone...") + try: + t2.aR(100) + print(f">> SUCCESS: Clone inherited aR feature. Len: {t2.len()}") + except AttributeError: + print("!! FAIL: Clone lost the feature!") + +if __name__ == "__main__": + test_base_machine() + test_ar_machine() + diff --git a/developer/deprecated_examples/example_queries.py b/developer/deprecated_examples/example_queries.py new file mode 100755 index 0000000..3801579 --- /dev/null +++ b/developer/deprecated_examples/example_queries.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 +from SymbolSpace import SymbolSpace +from Binder import Binder +from DiscreteFunction import DiscreteFunction +from Namespace import DifferentiatedSymbol ,OrderedNamespace + +def example_queries(): + print("--- Epimetheus Architecture Example ---") + + # 1. System Initialization + binder = Binder() + graph = DiscreteFunction() + + # 2. Ontology Definition (The Factories) + Frame = DifferentiatedSymbol("FrameMaterial") + Price = OrderedNamespace("Price") + + # 3. Data Ingestion (Transient Objects) + data_source = [ + ("Bike_A" ,"Carbon" ,3500), + ("Bike_B" ,"Steel" ,800), + ("Bike_C" ,"Alum" ,1200), + ("Bike_D" ,"Carbon" ,1500), + ] + + print(f"Ingesting {len(data_source)} items...") + + # Keep references to prevent GC during ingestion for this example + start_objects = [] + + for label, material, cost in data_source: + # A. Create the Python Object + obj = type("Bike", (), {"label": label})() + start_objects.append(obj) + + # B. Bind: Object -> Symbol + sym_bike = binder.get_symbol(obj) + + # C. Describe: Symbol -> Properties + graph.set(sym_bike ,Frame(material)) + graph.set(sym_bike ,Price(cost)) + + # --------------------------------------------------------- + # Example 1: Exact Query ("Find Carbon Frames") + # --------------------------------------------------------- + print("\n[Example 1] Exact Match: Frame('Carbon')") + + # We ask the Factory for the symbol representing 'Carbon' + sym_carbon = Frame("Carbon") + + # We ask the Graph for entities with that symbol + results = graph.find(sym_carbon) + print(f" -> Found {len(results)} bikes with Carbon frames.") + + # --------------------------------------------------------- + # Example 2: Range Query ("Find Price 1000..2000") + # --------------------------------------------------------- + print("\n[Example 2] Range Match: Price(1000..2000)") + + # Step A: Ask Namespace for symbols in range + # The OrderedNamespace uses bisect to find symbols efficiently + price_symbols = Price.find_range(1000 ,2000) + print(f" -> Namespace identified {len(price_symbols)} relevant price points.") + + # Step B: Ask Graph for objects having ANY of those symbols + matches = set() + for p_sym in price_symbols: + found = graph.find(p_sym) + matches.update(found) + + print(f" -> Graph resolved {len(matches)} bikes in price range.") + + # --------------------------------------------------------- + # Example 3: Hierarchy Query ("Find Priced Things") + # --------------------------------------------------------- + print("\n[Example 3] Hierarchy Match: Has Price") + + # We query the Root Symbol of the Price namespace. + # This works because the Graph automatically 'posts' up to the parent. + # FIXED: Changed .root to .root_symbol to match Namespace.py + all_priced = graph.find(Price.root_symbol) + print(f" -> Found {len(all_priced)} objects that have a price.") + +def CLI(): + example_queries() + +if __name__ == "__main__": + CLI() diff --git a/developer/deprecated_examples/example_region.py b/developer/deprecated_examples/example_region.py new file mode 100644 index 0000000..1ab3aba --- /dev/null +++ b/developer/deprecated_examples/example_region.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +from TM import TMS, RegionMachine + +def test_region(): + print("--- Region Machine Verification ---") + + # Tape: [10, 20, 30, 40, 50] + t = TMS([10, 20, 30, 40, 50]) + + # Create Region. Initially Boundaries are at 0 (start). + # Region is [0, 0] (Single Cell). + rm = RegionMachine(t) + + print("Region Initialized [0, 0].") + print(f"qL: {rm.qL()} (Exp: True)") + print(f"qR: {rm.qR()} (Exp: True)") + + # Expand Region: Move Right Boundary to index 2 + print("Expanding Right Boundary to index 2...") + rm.t_right.sn(2) + + # Region is now [0, 2] (Indices 0, 1, 2) + print(f"qR (Active at 0): {rm.qR()} (Exp: False)") + + # Move Active Head + rm.s() # To 1 + print(f"Active at 1. Val: {rm.r()}") + rm.s() # To 2 + print(f"Active at 2. Val: {rm.r()}") + + print(f"qR (Active at 2): {rm.qR()} (Exp: True)") + + # Try to step out of region + print("Attempting to step past Right Boundary...") + try: + rm.s() + print("!! FAIL: Stepped out of region!") + except RuntimeError as e: + print(f">> SUCCESS: Blocked by Boundary. ({e})") + +if __name__ == "__main__": + test_region() + diff --git a/developer/old_code.tar b/developer/old_code.tar deleted file mode 100644 index c8cf219..0000000 Binary files a/developer/old_code.tar and /dev/null differ diff --git a/document/TM.html b/document/TM.html index ebe8d5b..13555e3 100644 --- a/document/TM.html +++ b/document/TM.html @@ -365,6 +365,45 @@
  • wn(list) writes a list of values.
  • +

    Destructive Primitives

    + +

    + Machines configured with the SO (Solitary) entanglement approach may expose primitives that modify the tape structure. + These are distinct from w (which modifies cell content) because they change the cell count or sequence. +

    + +

    + 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. + Mnemonic: Entangle-Step-Delete (conceptually acts on the neighbor). +

    +
      +
    • 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().
    • +
    + +

    Insert/Append (a)

    +
      +
    • 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.
    • +
    +

    Query primitives

    @@ -573,6 +612,69 @@

    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).
    • +
    +