voici TMS
authorThomas Walker Lynch <eknp9n@reasoningtechnology.com>
Fri, 13 Feb 2026 13:54:17 +0000 (13:54 +0000)
committerThomas Walker Lynch <eknp9n@reasoningtechnology.com>
Fri, 13 Feb 2026 13:54:17 +0000 (13:54 +0000)
developer/authored/Symbol.py
developer/authored/TMS.py [new file with mode: 0644]
developer/authored_2026-02-09.tar [deleted file]
document/TMS.html [new file with mode: 0644]

index e893c6f..75edb0f 100755 (executable)
@@ -5,205 +5,275 @@ from __future__ import annotations
 
 import threading
 import weakref
+from enum import Enum, auto
 from typing import Optional, Sequence, Tuple
 
 
-class SymbolSpace:
-  """
-  A SymbolSpace provides:
-  - A Null symbol (identity 0).
-  - A ContextMap from symbol context keys to Factories.
-  - Factories that issue symbols and reclaim identities on GC.
-  """
+class Symbol:
+  __slots__ = (
+    "_identity"
+    ,"_factory_ref"
+    ,"_finalizer"
+    ,"_factory_id"
+    ,"__weakref__"
+  )
 
-  class Symbol:
-    __slots__ = (
-      "_identity"
-      ,"_factory_ref"
-      ,"_finalizer"
-      ,"_factory_id"
-      ,"__weakref__"
-    )
-
-    def __init__(self ,identity: int ,factory: Optional["SymbolSpace.Factory"]):
-      self._identity = int(identity)
-
-      if(self._identity == 0):
-        self._factory_ref = None
-        self._finalizer = None
-        self._factory_id = 0
-        return
+  def __init__(self ,identity: int ,factory: Optional["FactoryS"]):
+    self._identity = int(identity)
 
-      if(factory is None):
-        raise ValueError("Non-null symbols require a factory.")
+    if(factory is None):
+      self._factory_id = 0
+      self._factory_ref = None
+      self._finalizer = None
+      return
 
-      self._factory_id = factory.factory_id
-      self._factory_ref = weakref.ref(factory)
+    self._factory_id = factory.factory_id
+    self._factory_ref = weakref.ref(factory)
 
-      identity_copy = self._identity
-      factory_ref = self._factory_ref
+    # Capture state for finalizer to avoid keeping strong ref to self
+    identity_copy = self._identity
+    factory_ref = self._factory_ref
+
+    def _finalize():
+      f = factory_ref()
+      if(f is None):
+        return
+      try:
+        f._reclaim_identity(identity_copy)
+      except Exception:
+        # Finalizers must never raise.
+        return
 
-      def _finalize():
-        f = factory_ref()
-        if(f is None):
-          return
-        try:
-          f._reclaim_identity(identity_copy)
-        except Exception:
-          # Finalizers must never raise.
-          return
+    self._finalizer = weakref.finalize(self ,_finalize)
 
-      self._finalizer = weakref.finalize(self ,_finalize)
+  @property
+  def identity(self) -> int:
+    return self._identity
 
-    @property
-    def identity(self) -> int:
-      return self._identity
+  @property
+  def factory(self) -> Optional["FactoryS"]:
+    if(self._factory_ref is None):
+      return None
+    return self._factory_ref()
 
-    def __repr__(self) -> str:
-      if(self._identity == 0):
-        return "Symbol(0)"
-      return f"Symbol({self._identity} ,factory_id={self._factory_id})"
+  def __repr__(self) -> str:
+    return f"Symbol({self._identity} ,factory_id={self._factory_id})"
 
-    def __hash__(self) -> int:
-      return hash( (self._factory_id ,self._identity) )
+  def __hash__(self) -> int:
+    return hash( (self._factory_id ,self._identity) )
 
-    def __eq__(self ,other: object) -> bool:
-      if( not isinstance(other ,SymbolSpace.Symbol) ):
+  def __eq__(self ,other: object) -> bool:
+    if( not isinstance(other ,Symbol) ):
+      return False
+    return (self._factory_id == other._factory_id) and (self._identity == other._identity)
+
+
+class Factory:
+  """
+  Base First-Order Factory.
+  - created with a known context.
+  - strictly increases counter.
+  - manages free set.
+  """
+  __slots__ = (
+    "_lock"
+    ,"_counter"
+    ,"_free_set"
+    ,"_space"
+    ,"_context_key"
+    ,"factory_id"
+  )
+
+  def __init__(self ,space: "SymbolSpace" ,context_key: Tuple["Symbol" ,...] ,factory_id: int):
+    self._lock = threading.RLock()
+    self._counter = 0
+    self._free_set = set()
+    self._space = space
+    self._context_key = context_key
+    self.factory_id = int(factory_id)
+
+  def make(self ,wrapper: "FactoryS") -> "Symbol":
+    with self._lock:
+      if(self._free_set):
+        identity = min(self._free_set)
+        self._free_set.remove(identity)
+        return Symbol(identity ,wrapper)
+
+      identity = self._counter
+      self._counter += 1
+      return Symbol(identity ,wrapper)
+
+  def has(self ,symbol: "Symbol") -> bool:
+    # FactoryS handles the ID check; we handle logic.
+    with self._lock:
+      if(symbol.identity >= self._counter):
+        return False
+      if(symbol.identity in self._free_set):
         return False
-      return (self._factory_id == other._factory_id) and (self._identity == other._identity)
-
-  class Factory:
-    """
-    Issues identities from a monotonically increasing counter and a free set.
-
-    Reclaim policy (from the spec):
-    - When a reclaimed identity equals counter, decrement counter.
-      Continue decrementing while counter is in the free set.
-    - Otherwise, add the identity to the free set.
-    """
-
-    __slots__ = (
-      "_lock"
-      ,"_counter"
-      ,"_free_set"
-      ,"_space"
-      ,"_context_key"
-      ,"factory_id"
-      ,"__weakref__"
-    )
-
-    def __init__(self ,space: "SymbolSpace" ,context_key: Tuple["SymbolSpace.Symbol" ,...] ,factory_id: int):
-      self._lock = threading.RLock()
-      self._counter = 0  # 0 is reserved for space.null
-      self._free_set = set()  # Set[int]
-      self._space = space
-      self._context_key = context_key
-      self.factory_id = int(factory_id)
-
-    @property
-    def counter(self) -> int:
-      with self._lock:
-        return self._counter
-
-    @property
-    def free_set_size(self) -> int:
-      with self._lock:
-        return len(self._free_set)
-
-    def make(self) -> "SymbolSpace.Symbol":
-      with self._lock:
-        if(self._free_set):
-          identity = min(self._free_set)
-          self._free_set.remove(identity)
-          return self._space.Symbol(identity ,self)
-
-        self._counter += 1
-        return self._space.Symbol(self._counter ,self)
-
-    def _reclaim_identity(self ,identity: int) -> None:
-      identity = int(identity)
-      if(identity == 0):
-        raise ValueError("Attempted to reclaim the null symbol identity 0.")
-
-      with self._lock:
-        if(identity > self._counter):
-          raise ValueError(f"Attempted to reclaim identity {identity} above counter {self._counter}.")
-
-        if(identity in self._free_set):
-          raise ValueError(f"Identity {identity} already reclaimed.")
-
-        if(identity == self._counter):
+    return True
+
+  def reclaim(self ,identity: int) -> None:
+    with self._lock:
+      if(identity >= self._counter):
+        raise ValueError(f"Identity {identity} not issued (counter {self._counter}).")
+      
+      if(identity in self._free_set):
+        raise ValueError(f"Identity {identity} already reclaimed.")
+
+      if(identity == self._counter - 1):
+        self._counter -= 1
+        while( (self._counter > 0) and ((self._counter - 1) in self._free_set) ):
           self._counter -= 1
-          while(self._counter in self._free_set):
-            self._free_set.remove(self._counter)
-            self._counter -= 1
-          return
-
-        self._free_set.add(identity)
-
-  class ContextMap:
-    """
-    Map: context_key -> Factory
-    A context_key is an ordered list of symbols, represented as a tuple.
-    """
-
-    __slots__ = (
-      "_space"
-      ,"_lock"
-      ,"_factory_map"
-      ,"_factory_id_counter"
-    )
-
-    def __init__(self ,space: "SymbolSpace"):
-      self._space = space
-      self._lock = threading.RLock()
-      self._factory_map = {}  # Dict[Tuple[Symbol] ,Factory]
-      self._factory_id_counter = 0
-
-    def _normalize(self ,context_key: Optional[Sequence["SymbolSpace.Symbol"]]) -> Tuple["SymbolSpace.Symbol" ,...]:
-      if(context_key is None):
-        return tuple()
-      if( isinstance(context_key ,tuple) ):
-        ctx = context_key
-      else:
-        ctx = tuple(context_key)
-
-      for s in ctx:
-        if( not isinstance(s ,SymbolSpace.Symbol) ):
-          raise TypeError("context_key entries must be SymbolSpace.Symbol values.")
-
-      return ctx
-
-    def read(self ,context_key: Optional[Sequence["SymbolSpace.Symbol"]] = None) -> Optional["SymbolSpace.Factory"]:
-      ctx = self._normalize(context_key)
-      with self._lock:
-        return self._factory_map.get(ctx)
-
-    def make(self ,context_key: Optional[Sequence["SymbolSpace.Symbol"]] = None) -> "SymbolSpace.Factory":
-      ctx = self._normalize(context_key)
-      with self._lock:
-        if(ctx in self._factory_map):
-          raise KeyError("Factory already exists for this context_key.")
-
-        self._factory_id_counter += 1
-        f = SymbolSpace.Factory(self._space ,ctx ,self._factory_id_counter)
-        self._factory_map[ctx] = f
-        return f
-
-    def delete(self ,context_key: Optional[Sequence["SymbolSpace.Symbol"]] = None) -> None:
-      ctx = self._normalize(context_key)
-      with self._lock:
-        if(ctx not in self._factory_map):
-          raise KeyError("No factory exists for this context_key.")
-        del self._factory_map[ctx]
-
-    def __len__(self) -> int:
-      with self._lock:
-        return len(self._factory_map)
+          self._free_set.remove(self._counter)
+        return
+
+      self._free_set.add(identity)
+
+
+class FactoryS:
+  """
+  Factory with Status (Second Order).
+  Wraps the base Factory using VTable swapping.
+  """
+  __slots__ = (
+    "_space"
+    ,"_context_key"
+    ,"_factory_id"
+    ,"_base"
+    # VTable slots
+    ,"make"
+    ,"has"
+    ,"_reclaim_identity"
+  )
+
+  def __init__(self ,space: "SymbolSpace" ,context_key: Tuple["Symbol" ,...] ,factory_id: int):
+    self._space = space
+    self._context_key = context_key
+    self._factory_id = factory_id
+    self._base = None
+    
+    # Initialize in Empty state
+    self._change_state_empty()
+
+  @property
+  def factory_id(self) -> int:
+    return self._factory_id
+
+  # --- State Transitions ---
+
+  def _change_state_empty(self):
+    self.make = self._make_empty
+    self.has = self._has_empty
+    self._reclaim_identity = self._reclaim_empty
+
+  def _change_state_active(self ,base_factory: "Factory"):
+    self._base = base_factory
+    # Hot-swap to active methods
+    self.make = self._make_active
+    self.has = self._has_active
+    self._reclaim_identity = self._reclaim_active
+
+  # --- Empty State Methods ---
+
+  def _make_empty(self) -> "Symbol":
+    # JIT creation of the base factory
+    base = Factory(self._space ,self._context_key ,self._factory_id)
+    
+    # Transition to Active
+    self._change_state_active(base)
+    
+    # Delegate to the new base (which will issue identity 0)
+    return base.make(self)
+
+  def _has_empty(self ,symbol: "Symbol") -> bool:
+    return False
+
+  def _reclaim_empty(self ,identity: int) -> None:
+    # Should not happen if logic is correct, as no symbols exist to be reclaimed.
+    raise RuntimeError("Attempted to reclaim symbol from an Empty FactoryS.")
+
+  # --- Active State Methods ---
+
+  def _make_active(self) -> "Symbol":
+    # Direct delegation
+    return self._base.make(self)
+
+  def _has_active(self ,symbol: "Symbol") -> bool:
+    if(symbol._factory_id != self._factory_id):
+      return False
+    return self._base.has(symbol)
+
+  def _reclaim_active(self ,identity: int) -> None:
+    self._base.reclaim(identity)
+
+
+class ContextMap:
+  """
+  Map: context_key -> FactoryS
+  """
+  __slots__ = (
+    "_space"
+    ,"_lock"
+    ,"_factory_map"
+    ,"_factory_id_counter"
+  )
+
+  def __init__(self ,space: "SymbolSpace"):
+    self._space = space
+    self._lock = threading.RLock()
+    self._factory_map = {}
+    self._factory_id_counter = 0
+
+  def _normalize(self ,context_key: Optional[Sequence["Symbol"]]) -> Tuple["Symbol" ,...]:
+    if(context_key is None):
+      return tuple()
+    if( isinstance(context_key ,tuple) ):
+      ctx = context_key
+    else:
+      ctx = tuple(context_key)
+
+    for s in ctx:
+      if( not isinstance(s ,Symbol) ):
+        raise TypeError("context_key entries must be Symbol values.")
+
+    return ctx
+
+  def read(self ,context_key: Optional[Sequence["Symbol"]] = None) -> Optional["FactoryS"]:
+    ctx = self._normalize(context_key)
+    with self._lock:
+      return self._factory_map.get(ctx)
+
+  def make(self ,context_key: Optional[Sequence["Symbol"]] = None) -> "FactoryS":
+    ctx = self._normalize(context_key)
+    with self._lock:
+      if(ctx in self._factory_map):
+        raise KeyError("FactoryS already exists for this context_key.")
+
+      self._factory_id_counter += 1
+      # Create FactoryS (starts Empty)
+      f = FactoryS(self._space ,ctx ,self._factory_id_counter)
+      self._factory_map[ctx] = f
+      return f
+
+  def delete(self ,context_key: Optional[Sequence["Symbol"]] = None) -> None:
+    ctx = self._normalize(context_key)
+    with self._lock:
+      if(ctx not in self._factory_map):
+        raise KeyError("No factory exists for this context_key.")
+      del self._factory_map[ctx]
+
+  def __len__(self) -> int:
+    with self._lock:
+      return len(self._factory_map)
 
-  def __init__(self):
-    self.null = SymbolSpace.Symbol(0 ,None)
-    self.context_map = SymbolSpace.ContextMap(self)
 
-    # Default root factory for empty context.
+class SymbolSpace:
+  """
+  Container for the ContextMap and Root FactoryS.
+  """
+  def __init__(self):
+    self.context_map = ContextMap(self)
+    
+    # Create the root factory (empty context)
+    # Returns a FactoryS (in Empty state)
     self.root_factory = self.context_map.make(tuple())
diff --git a/developer/authored/TMS.py b/developer/authored/TMS.py
new file mode 100644 (file)
index 0000000..c0a18d7
--- /dev/null
@@ -0,0 +1,278 @@
+# TMS.py
+# RT format: 2-space indent, PascalCase type names, snake_case identifiers.
+
+from enum import Enum, auto
+
+class Status(Enum):
+  EMPTY_TYPED = auto()    # No instance, knows type.
+  EMPTY_STASHED = auto()  # Instance exists (1 zombie cell), logically empty.
+  PARKED = auto()         # Instance exists, head virtual -1.
+  SINGLETON = auto()      # Instance exists, size 1, Head at 0.
+  ACTIVE = auto()         # Instance exists, size > 1 OR Head > 0.
+
+
+class TMS:
+  """
+  Turing Machine with Status (Second Order Machine).
+  - Projective Parked State (Connects to both ends).
+  - Late Binding & Zombie Cell optimization.
+  - Partial Direct Binding for performance.
+  """
+  __slots__ = (
+    "tm"
+    ,"tm_type"
+    ,"_status"
+    # VTable Methods
+    ,"s"
+    ,"Ls"
+    ,"r"
+    ,"w"
+    ,"d"
+    ,"append"
+    ,"cue_leftmost"
+    ,"cue_rightmost"
+    ,"park"
+    ,"dealloc"
+    ,"is_empty"
+  )
+
+  def __init__(self ,tm=None ,tm_type=None):
+    self.tm = None
+    self.tm_type = None
+
+    if(tm is not None):
+      self.tm = tm
+      self.tm_type = type(tm)
+      self._change_state(Status.PARKED)
+    elif(tm_type is not None):
+      self.tm_type = tm_type
+      self._change_state(Status.EMPTY_TYPED)
+    else:
+      self._change_state(Status.EMPTY_TYPED)
+
+  @property
+  def status(self):
+    return self._status
+
+  def _change_state(self ,new_status):
+    self._status = new_status
+    
+    # ---------------------------------------------------------
+    # STATE: ACTIVE
+    # ---------------------------------------------------------
+    if(new_status == Status.ACTIVE):
+      # Direct Binding (Max Performance)
+      self.r = self.tm.r
+      self.w = self.tm.w
+      self.append = self._active_append
+      
+      # Navigation: Wrapped Binding (Guarded)
+      self.s = self._active_s
+      self.Ls = self._active_Ls
+      
+      self.d = self._active_d
+      self.cue_leftmost = self._active_cue_leftmost
+      self.cue_rightmost = self._active_cue_rightmost
+      self.park = self._active_park
+      self.dealloc = self._generic_dealloc
+      self.is_empty = self._return_false
+      
+    # ---------------------------------------------------------
+    # STATE: SINGLETON (Size == 1, Head == 0)
+    # ---------------------------------------------------------
+    elif(new_status == Status.SINGLETON):
+      self.r = self.tm.r
+      self.w = self.tm.w
+      
+      # Navigation
+      self.s = self._singleton_s      # Falls off 0 -> Active
+      self.Ls = self._singleton_Ls    # Falls off 0 -> Parked
+      
+      self.d = self._singleton_d      # Becomes Stashed
+      self.append = self._singleton_append
+      
+      self.cue_leftmost = self._noop  # Already at 0
+      self.cue_rightmost = self._noop # Already at 0 (Rightmost=Leftmost)
+      self.park = self._active_park
+      self.dealloc = self._generic_dealloc
+      self.is_empty = self._return_false
+
+    # ---------------------------------------------------------
+    # STATE: PARKED (Projective, Head Virtual)
+    # ---------------------------------------------------------
+    elif(new_status == Status.PARKED):
+      self.s = self._parked_s     # Enter Left
+      self.Ls = self._parked_Ls   # Enter Right (Projective)
+      
+      self.r = self._error_io
+      self.w = self._error_io
+      self.d = self._parked_d
+      self.append = self._error_append
+      
+      self.cue_leftmost = self._parked_cue_leftmost
+      self.cue_rightmost = self._parked_cue_rightmost
+      self.park = self._noop
+      self.dealloc = self._generic_dealloc
+      self.is_empty = self._return_false
+
+    # ---------------------------------------------------------
+    # STATE: EMPTY STASHED
+    # ---------------------------------------------------------
+    elif(new_status == Status.EMPTY_STASHED):
+      self.s = self._error_step
+      self.Ls = self._error_step
+      self.r = self._error_io
+      self.w = self._error_io
+      self.d = self._error_op
+      
+      self.append = self._stashed_append
+      
+      self.cue_leftmost = self._noop
+      self.cue_rightmost = self._noop
+      self.park = self._noop
+      self.dealloc = self._generic_dealloc
+      self.is_empty = self._return_true
+
+    # ---------------------------------------------------------
+    # STATE: EMPTY TYPED
+    # ---------------------------------------------------------
+    elif(new_status == Status.EMPTY_TYPED):
+      self.s = self._error_step
+      self.Ls = self._error_step
+      self.r = self._error_io
+      self.w = self._error_io
+      self.d = self._error_op
+      
+      self.append = self._typed_append
+      
+      self.cue_leftmost = self._noop
+      self.cue_rightmost = self._noop
+      self.park = self._noop
+      self.dealloc = self._noop
+      self.is_empty = self._return_true
+
+  # --- ACTIVE wrappers ---
+
+  def _active_s(self):
+    # Guard against stepping off the right end
+    if(self.tm.qR()):
+      raise RuntimeError("Cannot step right: At Rightmost cell.")
+    self.tm.s()
+
+  def _active_Ls(self):
+    # Guard: If at Leftmost, transition to Parked
+    if(self.tm.Lq()):
+      self._change_state(Status.PARKED)
+    else:
+      self.tm.Ls()
+
+  def _active_d(self):
+    self.tm.d()
+    if(self.tm.is_singleton()):
+      self._change_state(Status.SINGLETON)
+
+  def _active_append(self ,val):
+    self.tm.append(val)
+
+  def _active_cue_leftmost(self):
+    self.tm.cue_leftmost()
+
+  def _active_cue_rightmost(self):
+    self.tm.cue_rightmost()
+
+  def _active_park(self):
+    self.tm.cue_leftmost()
+    self._change_state(Status.PARKED)
+
+  # --- SINGLETON wrappers ---
+
+  def _singleton_s(self):
+    # Step Right from Singleton(0).
+    # If successful, we are no longer at 0 (or size > 1).
+    # Thus, we must transition to ACTIVE.
+    self.tm.s()
+    self._change_state(Status.ACTIVE)
+
+  def _singleton_Ls(self):
+    # Left of Singleton(0) is Parked
+    self._change_state(Status.PARKED)
+
+  def _singleton_d(self):
+    # Stash zombie
+    self._change_state(Status.EMPTY_STASHED)
+
+  def _singleton_append(self ,val):
+    self.tm.append(val)
+    self._change_state(Status.ACTIVE)
+
+  # --- PARKED wrappers ---
+
+  def _parked_s(self):
+    # Enter tape at Leftmost (0)
+    self.tm.cue_leftmost()
+    if(self.tm.is_singleton()):
+      self._change_state(Status.SINGLETON)
+    else:
+      self._change_state(Status.ACTIVE)
+
+  def _parked_Ls(self):
+    # Enter tape at Rightmost (Projective)
+    self.tm.cue_rightmost()
+    if(self.tm.is_singleton()):
+      self._change_state(Status.SINGLETON)
+    else:
+      self._change_state(Status.ACTIVE)
+
+  def _parked_d(self):
+    # Delete Leftmost (right neighbor)
+    self.tm.cue_leftmost()
+    if(self.tm.is_singleton()):
+      self._change_state(Status.EMPTY_STASHED)
+    else:
+      self.tm.d()
+      if(self.tm.is_singleton()):
+        self._change_state(Status.SINGLETON)
+      else:
+        self._change_state(Status.ACTIVE)
+
+  def _parked_cue_leftmost(self):
+    self._parked_s()
+
+  def _parked_cue_rightmost(self):
+    self._parked_Ls()
+
+  # --- SPECIAL APPENDS ---
+
+  def _stashed_append(self ,val):
+    self.tm.w(val)
+    self._change_state(Status.SINGLETON)
+
+  def _typed_append(self ,val):
+    if(self.tm_type is None):
+      raise RuntimeError("No TM type known.")
+    self.tm = self.tm_type(val)
+    self._change_state(Status.SINGLETON)
+
+  # --- GENERIC ---
+
+  def _generic_dealloc(self):
+    if(self.tm is not None):
+      self.tm_type = type(self.tm)
+      self.tm = None
+    self._change_state(Status.EMPTY_TYPED)
+
+  def _return_true(self): return True
+  def _return_false(self): return False
+  def _noop(self): pass
+  
+  def _error_io(self ,*args):
+    raise RuntimeError(f"I/O illegal in {self._status.name} state.")
+    
+  def _error_step(self):
+    raise RuntimeError(f"Stepping illegal in {self._status.name} state.")
+
+  def _error_op(self):
+    raise RuntimeError(f"Operation illegal in {self._status.name} state.")
+  
+  def _error_append(self ,val):
+     raise RuntimeError(f"Cannot append in {self._status.name} state.")
diff --git a/developer/authored_2026-02-09.tar b/developer/authored_2026-02-09.tar
deleted file mode 100644 (file)
index 91a53ef..0000000
Binary files a/developer/authored_2026-02-09.tar and /dev/null differ
diff --git a/document/TMS.html b/document/TMS.html
new file mode 100644 (file)
index 0000000..7a5c064
--- /dev/null
@@ -0,0 +1,160 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="UTF-8">
+    <title>Tape Machine with Status</title>
+    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+JP&display=swap" rel="stylesheet">
+    
+    <script src="style/body_visibility_hidden.js"></script>
+    <script>
+      window.StyleRT.body_visibility_hidden();
+    </script>
+  </head>
+  <body>
+    <RT-article>
+      <RT-title 
+        author="Thomas Walker Lynch" 
+        date="2026-02-13" 
+        title="Tape Machine with Status (TMS)">
+      </RT-title>
+
+      <RT-TOC level="1"></RT-TOC>
+
+      <h1>Introduction</h1>
+
+      <p>
+        The <RT-term>Tape Machine with Status</RT-term> (TMS) is a <RT-term>Second Order Machine</RT-term>. It wraps a standard <RT-term>First Order</RT-term> Tape Machine (TM) to provide state management and advanced lifecycle features.
+      </p>
+
+      <p>
+        While a First Order TM must always reside on a valid cell (the <RT-term>Head</RT-term> must be at a valid address), the TMS allows the machine to be:
+        <ul>
+          <li><RT-code>Empty</RT-code> (containing no data)</li>
+          <li><RT-code>Parked</RT-code> (the head is conceptually "off tape")</li>
+          <li><RT-code>Active</RT-code> (functioning as a standard TM)</li>
+        </ul>
+      </p>
+
+      <p>
+        The TMS implementation uses the <RT-term>State Pattern</RT-term> (vtable swapping) to ensure that the <RT-code>Active</RT-code> state incurs zero overhead. Method calls in the active state are directly bound to the underlying First Order TM's methods.
+      </p>
+
+      <h1>States</h1>
+
+      <p>
+        The TMS manages five distinct states to handle lifecycle, late binding, and memory optimization.
+      </p>
+
+      <h2>1. EmptyTyped</h2>
+      <p>
+        The machine is logically empty and has no underlying TM instance allocated. It holds a reference to a TM <RT-term>class type</RT-term>. This supports <RT-term>Late Binding</RT-term>, allowing the TMS to be created before data is available.
+      </p>
+      <p>
+        <strong>Transitions:</strong>
+        <ul>
+          <li><RT-code>append(v)</RT-code> &rarr; Instantiates the TM with value <RT-code>v</RT-code>, transitions to <RT-code>Singleton</RT-code>.</li>
+        </ul>
+      </p>
+
+      <h2>2. EmptyStashed</h2>
+      <p>
+        The machine is logically empty, but the underlying TM instance is preserved with exactly one "zombie" cell. This is a performance optimization to avoid deallocation and reallocation costs when a machine is cleared and immediately reused.
+      </p>
+      <p>
+        <strong>Transitions:</strong>
+        <ul>
+          <li><RT-code>append(v)</RT-code> &rarr; Overwrites the zombie cell with <RT-code>v</RT-code>, transitions to <RT-code>Singleton</RT-code>.</li>
+          <li><RT-code>dealloc()</RT-code> &rarr; Destroys the TM instance, transitions to <RT-code>EmptyTyped</RT-code>.</li>
+        </ul>
+      </p>
+
+      <h2>3. Parked</h2>
+      <p>
+        The underlying TM exists, but the TMS Head is conceptually located "outside" the tape. This state is <RT-term>projective</RT-term>, meaning it connects to both ends of the tape.
+      </p>
+      <p>
+        <strong>Transitions:</strong>
+        <ul>
+          <li><RT-code>s()</RT-code> (Step Right) &rarr; Enters the tape at the <RT-term>Leftmost</RT-term> cell. Transitions to <RT-code>Singleton</RT-code> or <RT-code>Active</RT-code>.</li>
+          <li><RT-code>Ls()</RT-code> (Step Left) &rarr; Enters the tape at the <RT-term>Rightmost</RT-term> cell. Transitions to <RT-code>Singleton</RT-code> or <RT-code>Active</RT-code>.</li>
+          <li><RT-code>d()</RT-code> &rarr; Deletes the "right neighbor" (the Leftmost cell). Logic handles degradation to Empty or Singleton.</li>
+        </ul>
+      </p>
+
+      <h2>4. Singleton</h2>
+      <p>
+        The underlying TM has exactly one cell. The Head is at Address 0. In this state, the underlying TM reports both <RT-code>Lq</RT-code> (Left Query) and <RT-code>qR</RT-code> (Query Right) as true.
+      </p>
+      <p>
+        <strong>Transitions:</strong>
+        <ul>
+          <li><RT-code>Ls()</RT-code> &rarr; Transitions to <RT-code>Parked</RT-code> (exits left).</li>
+          <li><RT-code>s()</RT-code> &rarr; Transitions to <RT-code>Active</RT-code> (extends right) or raises error if bounded.</li>
+          <li><RT-code>d()</RT-code> &rarr; Does not physically delete the cell. Transitions to <RT-code>EmptyStashed</RT-code> (preserving the zombie cell).</li>
+        </ul>
+      </p>
+
+      <h2>5. Active</h2>
+      <p>
+        The underlying TM has more than one cell. Methods are <RT-term>directly bound</RT-term> to the underlying TM for maximum performance.
+      </p>
+      <p>
+        <strong>Transitions:</strong>
+        <ul>
+          <li><RT-code>Ls()</RT-code> &rarr; If <RT-code>Lq</RT-code> is true (at Leftmost), transitions to <RT-code>Parked</RT-code>. Otherwise, steps left.</li>
+          <li><RT-code>d()</RT-code> &rarr; If the size degrades to 1, transitions to <RT-code>Singleton</RT-code>.</li>
+        </ul>
+      </p>
+
+      <h1>Interface</h1>
+
+      <h2>Navigation</h2>
+      <p>
+        <RT-code>s()</RT-code>: Step Right.
+        <ul>
+          <li>In <RT-code>Parked</RT-code>: Lands on <strong>Leftmost</strong> cell.</li>
+          <li>In <RT-code>Active</RT-code>: Steps right (guarded by <RT-code>qR</RT-code>).</li>
+        </ul>
+      </p>
+      <p>
+        <RT-code>Ls()</RT-code>: Step Left.
+        <ul>
+          <li>In <RT-code>Parked</RT-code>: Lands on <strong>Rightmost</strong> cell.</li>
+          <li>In <RT-code>Active</RT-code>: Steps left. If at Leftmost, exits to <RT-code>Parked</RT-code>.</li>
+        </ul>
+      </p>
+      <p>
+        <RT-code>park()</RT-code>: Explicitly moves the machine to the <RT-code>Parked</RT-code> state and cues the underlying TM to the leftmost cell.
+      </p>
+
+      <h2>I/O</h2>
+      <p>
+        <RT-code>r()</RT-code> and <RT-code>w(val)</RT-code>: Read and Write operations are passed directly to the underlying TM. These are valid only in <RT-code>Active</RT-code> or <RT-code>Singleton</RT-code> states.
+      </p>
+
+      <h2>Lifecycle</h2>
+      <p>
+        <RT-code>append(val)</RT-code>: Adds data to the end of the tape. Handles the transition from Empty states to Singleton.
+      </p>
+      <p>
+        <RT-code>d()</RT-code>: Delete. Removes the current cell.
+        <ul>
+          <li>In <RT-code>Active</RT-code>: Removes current cell.</li>
+          <li>In <RT-code>Parked</RT-code>: Removes the Leftmost cell.</li>
+        </ul>
+      </p>
+      <p>
+        <RT-code>dealloc()</RT-code>: Forces the release of the underlying TM instance, returning the TMS to the <RT-code>EmptyTyped</RT-code> state.
+      </p>
+
+    </RT-article>
+  </body>
+</html>
+
+  <script src="style/style_orchestrator.js"></script>
+  <script>
+   window.StyleRT.style_orchestrator();
+  </script>
+
+</body>
+</html>