From: Thomas Walker Lynch
Date: Fri, 6 Feb 2026 10:36:42 +0000 (+0000)
Subject: command language section of doc looking good
X-Git-Url: https://git.reasoningtechnology.com/sitemap.xml?a=commitdiff_plain;h=9718a7760e1c35aa88573575f5fba448caa331e5;p=Epimetheus%2F.git
command language section of doc looking good
---
diff --git a/document/TM.html b/document/TM.html
index c71e12a..3634c02 100644
--- a/document/TM.html
+++ b/document/TM.html
@@ -20,7 +20,6 @@
-
Introduction
@@ -42,7 +41,7 @@
A TM can have a concrete tape or a virtual tape.
A concrete tape is backed by a container.
- A virtual tape exposes the same tape machine interface, but its cells and motion are implemented by functions over some internal state.
+ A virtual tape presents the same TM interface, but its cells and motion are implemented by functions over some internal state.
For example, an abstract Counting Number tape machine can use a big integer as its state and implement stepping and reading over that state, instead of traversing an array of numbers.
@@ -52,6 +51,12 @@
The decisions are made above the TM, by the programmerâs algorithm.
+
+ A TM is constructed with a set of feature symbols that select which parts of the interface are available.
+ When no feature symbols are supplied, the result is a non-destructive step right machine with no indexing.
+ Feature symbols can enable step left behavior (via the mirror view), and can independently enable indexing commands.
+
+
A first order TM is defined by a single invariant: the head is always on a valid cell.
This invariant makes the primitive interface total: every move, read, write, and query has a clean, well defined meaning, and caller code stays free of scattered end case tests.
@@ -65,468 +70,240 @@
- When multiple machines share one tape, the machines are entangled.
- The TM_EA maintains first order analysis by managing a catalog of the entangled machines, and providing a guard predicate for checking it. Any questionable operations must be guard checked before being called. This is in analogy to the first rest pattern where
- qR() guards s(), as seen further below.
-
-
-
- Thus, the library provides three TM variations related to tape entanglement:
- TM_ND disallows tape structure changes and thus the machines run independently.
- TM_SOLO disallows more than one machine per tape, precluding entanglement issues entirely.
- TM_EA uses entanglement accounting.
- With entanglement accounting, all machines on a shared tape share one catalog of entangled machines, and the interface exposes predicates that query the catalog.
- Caller code uses those predicates to guard against structure changes that would break machines.
-
-
-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 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
-
-
-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.
-
-
-
- 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 | 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
-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
-
-
-
- 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
-
-
-
- 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.
-
+ TM machines incorporate various approaches for controlling entanglement, depending on which interface features are selected at construction time.
+ In one approach, no methods are placed on the interface that can destroy tape cells. This supports a non-destructive programming paradigm, so such machines are said to be non-destructive. Without destructive operations, one machine cannot break another by changing its tape structure.
+ In a second approach, the interface is configured so there are no functions for entangling machines in the first place. Each solitary machine can safely perform destructive operations because no other machine shares its tape.
+ As a third approach, each group of entangled machines shares a catalog listing all machines in the group. Caller code can guard operations by querying whether a planned operation is safe before attempting it. This third strategy is called entanglement accounting.
+ Yet another approach is for the programmer, compiler, or external function to prove code is safe, which is the same as proving that guard code is not needed.
+
+
+
+ Command language
+
+
+ The command language is not used directly. It describes the letter patterns used to form TM 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 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 indexing.
+ Feature symbols can enable step left behavior (via the mirror view) and can independently enable indexing commands.
+
+
+
+ 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 order that the command and its descriptors require them.
+
+
+
+ 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 value.
+ With an R arg descriptor it returns true iff the head is on the rightmost cell.
+
+
+
+ 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.
+ As of the time of this writing 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
+
+
+ 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.
+
+
+
+ Step left support does not imply index space.
+ A machine can support L while still omitting index operations.
+ When index space is enabled as an additional feature, the I arg descriptor becomes available and index based cue and query forms become legal.
+ When L is available, n can also be negative, with the sign selecting the direction in the base view.
+
+
+ grammar
+
+ statement :: ( [machine] command [arg]* )+
+ machine :: L
+ command :: e | q | r | s | w
+ arg :: R | I | n
+
+
+
+ Index space, as defined in the TTCA book, is a natural number sequence machine that walks the tape of a base machine and establishes a one to one correspondence between natural numbers and tape cells.
+ That correspondence belongs to the tape and stays fixed when we change the view.
+ For example, consider a tape with cells [c_0 ,c_1 ,c_2 ,c_3 ,c_4], where c_4 is rightmost and has index 4.
+ In the mirror view we write the same cells as [c_4 ,c_3 ,c_2 ,c_1 ,c_0].
+ In the mirror view c_4 sits at the left edge, and it still has index 4.
+ 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
+ sn(-2) ; step to left neighbor twice
+ sn(2) ; step to right neighbor twice
+
+
+ Head movement relative to leftmost
+
+ sI(0) ; cue the head to the leftmost cell by index
+ sI(3) ; cue the head to tape[3]
+
+
+
+ LsI(k) and sI(k) cue the head to the same indexed cell.
+ L flips direction and the meaning of R, not the index to cell correspondence.
+ Thus LsI(0) cues the cell with index zero, which is rightmost in the mirror view and leftmost in the base view.
+ For that reason there is no primitive family for LsI distinct from sI.
+
+
+ Absolute head movement
+
+ LsR ; cue the head to leftmost
+ sR ; cue the head to rightmost
+
+
+
+ Without code optimization LsR will be faster than sI(0), because sI requires passing and processing an argument.
+
+
+ Query
+
+ LqR ; head is on leftmost
+ qR ; head is on rightmost
+ qI ; index of the head
+ qIR ; index of rightmost
+ LqIR ; index of leftmost, typically zero
+
+
+
+ In q, the I arg descriptor selects an index valued result.
+ By default q returns a boolean value.
+ In other commands, an I arg descriptor indicates that an index argument is supplied.
+
+
+
+ The R arg descriptor focuses the command on the selected view rightmost cell.
+ For qR, the result is true iff the head is on rightmost.
+ For qIR, the result is the index of rightmost.
+ Under L, that rightmost cell is the base view leftmost cell, so LqIR returns the index of leftmost, typically zero.
+
+
+ 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.
+
Primitive method reference
@@ -595,14 +372,14 @@ w(v) ; copies v into the cell
- - qA() returns the current head address. (Command form: qA.)
+ - qI() returns the current head index. (Command form: qI.)
- 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.)
+ - qIR() returns the index of the selected view rightmost cell. (Command form: qIR.)
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.
+ For example, LqR tests the leftmost of the underlying tape, and LqIR returns its index, which is zero.
Entanglement primitives
@@ -712,142 +489,11 @@ while True:
-
-
- Primitive method reference
-
-
- This section details the actual methods exposed by the First Order TM implementation (Python/C). These primitives map directly to the command language.
-
-
- Navigation primitives
-
- - s() steps right.
- - sn(n) steps right n times.
- - ls() steps left.
- - lsn(n) steps left n times.
- - Ls() cues leftmost.
- - Rs() cues rightmost.
- - Lsn(n) cues leftmost with offset n.
- - Rsn(n) cues rightmost with offset n.
-
-
- Input and output primitives
-
- - r() reads the current cell.
- - rn(n) reads n cells.
- - w(v) writes a value to the current cell.
- - wn(list) writes a list of values.
-
-
- Query primitives
-
- Introspection commands used to check machine state without side effects.
-
-
- - qA() queries the address to return the current head index.
-
-
- Entanglement primitives
-
- - e() entangles a new machine instance sharing the same tape and safety catalog.
- - es() entangles and steps right.
- - esn(n) entangles and steps right n times.
- - els() entangles and steps left.
- - elsn(n) entangles and steps left n times.
- - eLs() entangles and cues leftmost.
- - eRs() entangles and cues rightmost.
- - eLsn(n) entangles and cues leftmost with an offset.
- - eRsn(n) entangles and cues rightmost with an offset.
-
-
- 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 will make use of inclusive bounds for intervals. Thus an interval is marked by placing a heads on the leftmost cell of
- the interval, and on an entangled machine on the rightmost cell of the interval.
-
-
- Contract with the programmer
-
-
- Some first order TM types rely upon the programmer to maintain a contract in order to maintain the first order TM properties. Such machines typically have debug variants that add contract checks. For some machines breaking the contract can limit the ability of the compiler to optimize code, for other machines breaking the contract can lead
- to 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, this contract can be fulfilled by using a first rest pattern.
-
-
- First-Rest pattern with initialization
-
-
-# Pattern 1: First-Rest (Initialize with First)
-# Useful when the accumulator must start 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
-
-
-
- Here tm.qR() is a command that queries the machine to find out if
- the head is on the rightmost cell. It guards the step within the loop. Note no redundant tests are done, and the loop ends are clean without stepping off the end of a tape. (And thus not stepping off the end of a memory page or often, of a cache line.)
-
-
- First-Rest pattern without initialization
-
-
- Sometimes we initialize the accumulator to an identity value (like 0) before the loop starts. In this case, the most natural structure is the "Loop-and-a-Half" (middle-exit loop).
-
-
-
-# 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 if not at the end.
-
-
-
- Infamously, Python does not have a `do` statement, but it can be emulated:
-
-
-
-# Python Implementation
-while True:
- total += tm.r()
- if tm.qR(): break
- tm.s()
-
-
-
- The middle test of tm.qR() guards the tm.s() so that the machine will never step beyond rightmost.
-
-
+