From 1cbde15525b5ef75927e34f785e05f4891192653 Mon Sep 17 00:00:00 2001
From: Thomas Walker Lynch
Date: Fri, 6 Feb 2026 07:12:36 +0000
Subject: [PATCH] more TM command language changes, deprecating primives
section
---
document/TM.html | 661 +++++++++++++++++++++++++++++++++++++++++------
1 file changed, 586 insertions(+), 75 deletions(-)
diff --git a/document/TM.html b/document/TM.html
index e31b67f..c71e12a 100644
--- a/document/TM.html
+++ b/document/TM.html
@@ -79,130 +79,641 @@
Caller code uses those predicates to guard against structure changes that would break machines.
+Command language
- Command reference for the TM_ND
+
+ The command language is not used directly. It describes the letter patterns used to form interface function names.
+ This approach began with LISP, and its extension is described in the paper "Towards a Better Understanding of CAR, CDR, CADR and the Others".
+
-
- The following commands are available for the Nondestructive First Order Machine. In this version the notation uses strictly ASCII characters.
-
+
+ The actual function names found on TM interfaces are listed in later sections of this guide. Reading the source code can also help.
+ Later we can add a compiler for creating new functions based on this language. Such a compiler would be a function that adds functions to a TM interface.
+
+
+
+ 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 address space.
+ Feature symbols can enable step left behavior (via the mirror view) and can independently enable address space.
+
+
+
+For the non-destructive step right machine
+
+grammar
+
+statement :: ( command [arg]* )+
+command :: e | q | r | s | w
+arg :: R | n
+
+
+statement
+
+ A statement is parsed left to right. Each command letter begins a new command group.
+ All following arg descriptors attach to that command until the next command letter appears.
+ There are no spaces. Arguments are given to the resulting function in the same order that their descriptors occur in the statement.
+
+
+
+ Accordingly, a function name such as snr(i) denotes two command groups:
+ sn(i) steps right i times, then r copies out the current cell.
+
+
+command
+
+ - e entangle
+ - q query
+ - r read
+ - s step
+ - w write
+
+
+
+ Two or more machines are entangled when they share one tape.
+ Entangle differs from clone because the tape is shared rather than copied.
+ What is duplicated is the head state, so entangled heads move independently.
+ A machine produced by e begins with its head on the same cell as the machine it was created from.
+
+
+
+ The query command, q, reports head state.
+ With no arg descriptor it returns a boolean predicate.
+ With an R arg descriptor it answers the rightmost predicate.
+
+
+
+ TM uses a guard approach for end cases. Query commands appear in control flow so caller code can select the guards it needs and handle end cases explicitly.
+ When guards are unnecessary, caller code can omit them.
+
+
+
+ The r and w commands copy values to and from the current cell.
+ Today they are cell local operations, so direction does not enter.
+
+
+
+ The s step command moves the head to the right neighbor cell.
+ When s is given an n arg descriptor, it performs n right steps.
+ On a step right only machine, n must be positive.
+
+
+arg
+
+
+
+ The n arg descriptor supplies a repetition count.
+
+
+
+ The R arg descriptor focuses the command on the rightmost cell.
+ For qR, the result is true iff the head is on rightmost.
+ For sR, the effect is to cue the head to rightmost.
+
+
+Examples
+Relative head movement
+
+s ; step to right neighbor
+sn(1) ; step to right neighbor
+sn(2) ; step to right neighbor twice
+
+Absolute head movement
+
+sR ; cue the head to rightmost
+
+
+Query
+
+qR ; true iff head is on rightmost
+
- Grammar
+Copying to and from tape cells and python variables
-statement :: ( [machine] command arg* )+
+r ; copies cell contents to the LHS of an assignment
+w(v) ; copies v into the cell
+
+
+
+For the non-destructive machine that has step left
+
+
+ The step right grammar extends to left going behavior by introducing a mirror view of a machine.
+ In the mirror view, every rightward operation corresponds to the base machineâs leftward operation, so new command letters are unnecessary.
+ What is rightmost in the base view becomes leftmost in the mirror view, and vice versa.
+
+
+
+ In this extended grammar, the L prefix selects the mirror view for the command group that follows it.
+ L affects direction sensitive operations such as stepping, moving to rightmost, and scans.
+ Today r and w are cell local, so L does not change their behavior.
+
+
+
+ Step left support does not imply address space.
+ A machine can support L while still omitting address operations.
+ When address space is enabled as an additional feature, the A arg descriptor becomes available and address 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.
+
+
+grammar
+
+statement :: ( [machine] command [arg]* )+
machine :: L
command :: e | q | r | s | w
-arg :: R | A '(' int ')' | n '(' int ')'
+arg :: R | A | n
- Definitions
+
+ Address 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 address 4.
+ In the mirror view we write the same cells as [c_4 ,c_3 ,c_2 ,c_1 ,c_0].
+ In that view c_4 sits at the left edge, and it still has address 4.
+ 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.
+
- Machine
-
+Examples
+
+
+ The example descriptions state effects in the base view.
+ Effects in the mirror view match the same command without the L prefix.
+
+
+Relative head movement
+
+Ls ; step to left neighbor
+Lsn(2) ; step to left neighbor twice
+sn(-2) ; step to left neighbor twice
+sn(2) ; step to right neighbor twice
+
+
+Head movement relative to leftmost
+
+sA(0) ; cue the head to the leftmost cell by address
+sA(3) ; cue the head to tape[3]
+
+
+
+ LsA(k) and sA(k) cue the head to the same addressed cell.
+ L flips direction and the meaning of R, not the address to cell correspondence.
+ Thus LsA(0) cues the cell with address zero, which is rightmost in the mirror view and leftmost in the base view.
+ For that reason there is no primitive family for LsA distinct from sA.
+
+
+Absolute head movement
+
+LsR ; cue the head to leftmost
+sR ; cue the head to rightmost
+
+
+
+ Without code optimization LsR will be faster than sA(0), because sA requires passing and processing an argument.
+
+
+Query
+
+LqR ; head is on leftmost
+qR ; head is on rightmost
+qA ; address of the head
+qAR ; address of rightmost
+LqAR ; address of leftmost, typically zero
+
+
+
+ In q, the A arg descriptor selects an address valued result.
+ By default q returns a boolean predicate.
+ In other commands, an A arg descriptor indicates that an address 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 qAR, the result is the address of rightmost.
+ Under L, that rightmost cell is the base view leftmost cell, so LqAR returns the address of leftmost, typically zero.
+
+
+Copying to and from tape cells and python variables
+
+r ; copies cell contents to the LHS of an assignment
+w(v) ; copies v into the cell
+
+
+
+ The current r and w are cell local and are unaffected by L.
+ If we later support list forms or repetition forms for copy, L will affect the scan direction for those operations.
+
+
+
+
+Command language
+
+
+ The command language is not used directly. It describes the letter patterns used to form interface function names.
+ This approach began with LISP, and its extension is described in the paper "Towards a Better Understanding of CAR, CDR, CADR and the Others".
+
+
+
+ The actual function names found on TM interfaces are listed in later sections of this guide. Reading the source code can also help.
+ Later we can add a compiler for creating new functions based on this language. Such a compiler would be a function that adds methods to a TM instance.
+
+
+For the non-destructive step right machine
+
+grammar
+
+statement :: ( command [arg]* )+
+command :: e | q | r | s | w
+arg :: R | A | n
+
+
+statement
+
+ A statement is parsed left to right. Each command letter begins a new command group.
+ All following arg descriptors attach to that command until the next command letter appears.
+ There are no spaces. Arguments are given to the resulting function in the same order that their descriptors occur in the statement.
+
+
+
+ Accordingly, a function name such as sAr(i) denotes two command groups:
+ sA(i) cues the head to address i, then r copies out the current cell.
+
+
+command
+
+ - e entangle
+ - q query
+ - r read
+ - s step
+ - w write
+
+
+
+ Two or more machines are entangled when they share one tape.
+ Entangle differs from clone because the tape is shared rather than copied.
+ What is duplicated is the head state, so entangled heads move independently.
+ A machine produced by e begins with its head on the same cell as the machine it was created from.
+
+
+
+ The query command, q, reports head state.
+ With no arg descriptor it returns a boolean predicate.
+ With an A arg descriptor it returns an address.
+ With an R arg descriptor it answers the rightmost predicate.
+ With AR it returns the address of rightmost.
+
+
+
+ TM uses a guard approach for end cases. Query commands appear in control flow so caller code can select the guards it needs and handle end cases explicitly.
+ When guards are unnecessary, caller code can omit them.
+
+
+
+ The r and w commands copy values to and from the current cell.
+ Today they are cell local operations, so direction does not enter.
+
+
+
+ The s step command moves the head to the right neighbor cell.
+ When s is given an n arg descriptor, it performs n right steps.
+
+
+arg
+
+ - A Address
+ - n n
+ - R Rightmost
+
+
+
+ In q, the A arg descriptor selects an address valued result.
+ By default q returns a boolean predicate.
+ In other commands, an A arg descriptor indicates that an address argument is supplied.
+
+
+
+ The n arg descriptor supplies a repetition count.
+
+
+
+ The R arg descriptor focuses the command on the rightmost cell.
+ For qR, the result is true iff the head is on rightmost.
+ For sR, the effect is to cue the head to rightmost.
+ For qAR, the result is the address of rightmost.
+
+
+Examples
+
+Relative head movement
+
+s ; step to right neighbor
+sn(0) ; no-op
+sn(2) ; step to right neighbor twice
+
+
+Head movement relative to leftmost
+
+sA(0) ; cue the head to the leftmost cell by address
+sA(3) ; cue the head to tape[3]
+
+
+Absolute head movement
+
+sR ; cue the head to rightmost
+
+Query
+
+qR ; true iff head is on rightmost
+qA ; address of the head
+qAR ; address of rightmost
+
+
+Copying to and from tape cells and python variables
+
+r ; copies cell contents to the LHS of an assignment
+w(v) ; copies v into the cell
+
+
+
+For the non-destructive machine that has step left
+
+
+ The step right grammar extends to left going behavior by introducing a mirror view of a machine.
+ In the mirror view, every rightward operation corresponds to the base machineâs leftward operation, so new command letters are unnecessary.
+ What is rightmost in the base view becomes leftmost in the mirror view, and vice versa.
+
+
+
+ In this extended grammar, the L prefix selects the mirror view for the command group that follows it.
+ L affects direction sensitive operations such as stepping, moving to rightmost, and scans.
+ Today r and w are cell local, so L does not change their behavior.
+
+
+grammar
+
+statement :: ( [machine] command [arg]* )+
+machine :: L
+command :: e | q | r | s | w
+arg :: R | A | n
+
+
+
+ Address 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 address 4.
+ In the mirror view we write the same cells as [c_4 ,c_3 ,c_2 ,c_1 ,c_0].
+ In that view c_4 sits at the left edge, and it still has address 4.
+ 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.
+
+
+Examples
+
+
+ The example descriptions state effects in the base view.
+ Effects in the mirror view match the same command without the L prefix.
+
+
+Relative head movement
+
+Ls ; step to left neighbor
+Lsn(2) ; step to left neighbor twice
+
+
+Head movement relative to leftmost
+
+sA(0) ; cue the head to the leftmost cell by address
+sA(3) ; cue the head to tape[3]
+
+
+
+ LsA(k) and sA(k) cue the head to the same addressed cell.
+ L flips direction and the meaning of R, not the address to cell correspondence.
+ Thus LsA(0) cues the cell with address zero, which is rightmost in the mirror view and leftmost in the base view.
+ For that reason there is no primitive family for LsA distinct from sA.
+
+
+Absolute head movement
+
+LsR ; cue the head to leftmost
+
+
+
+ Without code optimization LsR will be faster than sA(0), because sA requires passing and processing an argument.
+
+
+Query
+
+LqR ; head is on leftmost
+LqAR ; address of leftmost, typically zero
+
- The L prefix selects the mirror view of the tape.
- In the mirror view, stepping right moves to the original tapeâs left neighbor.
- The mirror view shares the same head and the same tape cells; only the interpretation of direction and ârightmostâ is flipped.
+ qAR returns the address of the selected view rightmost cell.
+ Under L, that rightmost cell is the base view leftmost cell, so LqAR returns the address of leftmost, typically zero.
+Copying to and from tape cells and python variables
+
+r ; copies cell contents to the LHS of an assignment
+w(v) ; copies v into the cell
+
+
- Address space, as defined in TTCA, is provided by a natural number sequence machine that walks in lock step with the right going machine.
- That address to cell correspondence is fixed for the tape.
- The L prefix flips only the view.
- Under the mirror view, the same addresses appear in reverse order across the page, so the mirror view rightmost cell is the cell with address zero.
+ The current r and w are cell local and are unaffected by L.
+ If we later support list forms or repetition forms for copy, L will affect the scan direction for those operations.
- command
+
+ Primitive method reference
+
+
+ This section lists the concrete methods exposed by the Python/C implementation.
+ These methods implement a selected subset of the command grammar.
+ The grammar defines a command language, while the primitive set defines the operations the library exposes directly.
+ Later sections can introduce a compile layer that accepts additional command combinations.
+
+
+
+ The L prefix is not implemented as a distinct family of primitives.
+ It selects the mirror view in the command language.
+ In the API, the same effect is provided by explicit left variants such as ls() and lsn(n).
+
+
+ Navigation primitives
+
+
+ The navigation primitives implement the s command with the argument forms ε and n(int).
+ The left variants implement the mirror view behavior directly.
+
+
- - e entangle
- - q query
- - r read
- - s step.
- - w write
+ - s() steps to the right neighbor. (Command form: s.)
+ - sn(n) steps right n times. (Command form: sn(n).)
+ - ls() steps to the left neighbor. (Command form: Ls.)
+ - lsn(n) steps left n times. (Command form: Lsn(n).)
+ - Rs() cues the selected machine rightmost. (Command form: sR.)
+ - Ls() cues the selected machine leftmost. (Command form: LsR.)
- The result of the entangle command is a new TM that is entangled with the interfaced machine. By definition, the new entangled machine shares the tape with the original interface machine. Both machines initially have the head on the same cell.
+ Note the correspondence: sR cues rightmost in the right going view, and LsR cues leftmost in that same underlying tape.
+ The primitives Rs() and Ls() are optimized forms of those cues.
+
+
+ Input and output primitives
+
+
+ The I/O primitives implement r and w(value).
+ In the command language, r reads the current cell payload, and w(v) writes a payload to the current cell.
- arg_type
- - A Adress
- - n n
- - R Rightmost
+ - r() reads the current cell.
+ - w(v) writes v to the current cell.
- R always refers to the rightmost cell of the selected machine.
+ Bulk forms exist in the API, but they are not represented in the current command grammar.
+ They are library level conveniences built on top of repeated primitive behavior.
+
+ - rn(n) reads n cells.
+ - wn(list) writes a list of values.
+
- Examples
-
- Relative head movement
+ Query primitives
-
-s ; step to right neighbor
-Ls ; step to left neighbor (stepping right on the mirror image machine)
-sn(0) ; no-op
-sn(2) ; step to right neighbor twice
-Lsn(2) ; step to left neighbor twice (stepping right on the mirror image machine)
-
+
+ Query primitives expose the q command.
+ In the command language, query arguments refine what is being queried.
+
- Head movement relative to leftmost
+
+ - qA() returns the current head address. (Command form: qA.)
+ - qR() returns whether the head is on the selected view rightmost cell. (Command form: qR.)
+ - qAR() returns the address of the selected view rightmost cell. (Command form: qAR.)
+
+
+
+ The mirror view query forms follow the same rule.
+ For example, LqR tests the leftmost of the underlying tape, and LqAR returns its address, which is zero.
+
+
+ Entanglement primitives
+
+
+ Entanglement primitives implement the e command.
+ The entangled machine shares the tape with the original and starts on the same cell.
+
+
+
+ - e() returns a new entangled machine sharing the same tape and safety catalog. (Command form: e.)
+
+
+
+ Combined command forms such as es or els are legal in the grammar, but they are not required as primitives.
+ The library can expose such combinations as convenience calls, or generate them through a compile layer.
+ The core model remains that a statement is a sequence of commands applied to a selected view.
+
+
+ Usage
+
+ First order TM properties
+
+
+ For a first order TM these properties are always true.
+
+
+
+ - The head is always on a valid cell.
+ - All cells return a value when read.
+
+
+
+ In order to maintain these properties, a TM uses inclusive bounds for intervals.
+ An interval is marked by placing a head on the leftmost cell of the interval, and placing an entangled head on the rightmost cell of the interval.
+
+
+ Contract with the programmer
+
+
+ Some first order TM types rely upon the caller to maintain a contract.
+ Such machines typically have debug variants that add contract checks.
+ For some machines, breaking the contract limits compiler optimization.
+ For other machines, breaking the contract causes program bugs.
+
+
+
+ Contract:
+
+
+
+ - When there is a right bound, the caller guarantees they will never command the machine to step beyond the rightmost cell.
+ - When there is a left bound, the caller guarantees they will never command the machine to step left of the leftmost cell.
+
+
+
+ When traversing a tape, the contract is fulfilled by a First Rest pattern.
+
+
+ First Rest pattern with initialization
-sA(0) ; step to the leftmost cell by address
-sA(3) ; step to tape[3]
+# Pattern 1: First Rest (Initialize with First)
+# Useful when the accumulator starts with the first value.
+total = tm.r() # Process First
+while not tm.qR(): # Guard: Is there a Rest?
+ tm.s() # Step (Protected)
+ total += tm.r() # Process Next
-
- LsA(k) and sA(k) cue the head to the same addressed cell.
- The L prefix changes direction and the meaning of R, not the address to cell correspondence.
- Thus LsA(0) cues the cell with address zero, which is rightmost in the mirror view and leftmost in the right going view.
-
+
+ Here tm.qR() queries whether the head is on the selected view rightmost cell.
+ It guards the step in the loop.
+ No redundant tests are done, and the loop ends without stepping off the tape.
+
+
+ First Rest pattern without initialization
+
+
+ Sometimes the accumulator is initialized to an identity value (such as zero) before the loop starts.
+ In that form, the loop and a half structure is natural.
+
-
- Absolute head movement
-sR ; step to rightmost
-LsR ; step to leftmost (rightmost on the mirror machine)
+# Conceptual "do" loop
+do:
+ total += tm.r() # Always process the current cell
+ if tm.qR() break # Middle break: if at rightmost, stop
+ tm.s() # Step only when not at the end
- Without code optimization LsR will
- be faster than its counterpart sA(0) because the latter
- form requires passing and processing an argument.
-
+
+ Python has no do statement, so the same logic is written explicitly:
+
- Query
-qR ; head is on rightmost
-LqR ; head is on leftmost
-qA ; returns address of the head
-qAR ; returns address of rightmost
-LqAR ; returns address of leftmost, zero
-
-
-
- qAR returns the address of the selected view rightmost cell.
- Under L, that rightmost cell is the original leftmost cell, so LqAR returns zero.
-
+# Python implementation
+while True:
+ total += tm.r()
+ if tm.qR(): break
+ tm.s()
+
+
+ The middle test tm.qR() guards tm.s(), so the machine never steps beyond rightmost.
+
- Copying to and from the tape machine cells and python variables.
-
-r ; copies cell contents to the LHS of an assignment
-w(v) ; copies v into the cell
-
-
+
Primitive method reference
--
2.20.1