Modelling partial recursive functions using Turing machines #
The files TMConfig and TMToPartrec define a simplified basis for partial recursive functions,
and a Turing.TM2 model
Turing machine for evaluating these functions. This amounts to a constructive proof that every
Partrec function can be evaluated by a Turing machine.
Main definitions #
PartrecToTM2.tr: A TM2 turing machine which can evaluatecodeprograms
Simulating sequentialized partial recursive functions in TM2 #
At this point we have a sequential model of partial recursive functions: the Cfg type and
step : Cfg → Option Cfg function from TMConfig.lean. The key feature of this model is that
it does a finite amount of computation (in fact, an amount which is statically bounded by the size
of the program) between each step, and no individual step can diverge (unlike the compositional
semantics, where every sub-part of the computation is potentially divergent). So we can utilize the
same techniques as in the other TM simulations in Computability.TuringMachine to prove that
each step corresponds to a finite number of steps in a lower level model. (We don't prove it here,
but in anticipation of the complexity class P, the simulation is actually polynomial-time as well.)
The target model is Turing.TM2, which has a fixed finite set of stacks, a bit of local storage,
with programs selected from a potentially infinite (but finitely accessible) set of program
positions, or labels Λ, each of which executes a finite sequence of basic stack commands.
For this program we will need four stacks, each on an alphabet Γ' like so:
inductive Γ' | consₗ | cons | bit0 | bit1
We represent a number as a bit sequence, lists of numbers by putting cons after each element, and
lists of lists of natural numbers by putting consₗ after each list. For example:
0 ~> []
1 ~> [bit1]
6 ~> [bit0, bit1, bit1]
[1, 2] ~> [bit1, cons, bit0, bit1, cons]
[[], [1, 2]] ~> [consₗ, bit1, cons, bit0, bit1, cons, consₗ]
The four stacks are main, rev, aux, stack. In normal mode, main contains the input to the
current program (a List ℕ) and stack contains data (a List (List ℕ)) associated to the
current continuation, and in ret mode main contains the value that is being passed to the
continuation and stack contains the data for the continuation. The rev and aux stacks are
usually empty; rev is used to store reversed data when e.g. moving a value from one stack to
another, while aux is used as a temporary for a main/stack swap that happens during cons₁
evaluation.
The only local store we need is Option Γ', which stores the result of the last pop
operation. (Most of our working data are natural numbers, which are too large to fit in the local
store.)
The continuations from the previous section are data-carrying, containing all the values that have
been computed and are awaiting other arguments. In order to have only a finite number of
continuations appear in the program so that they can be used in machine states, we separate the
data part (anything with type List ℕ) from the Cont type, producing a Cont' type that lacks
this information. The data is kept on the stack stack.
Because we want to have subroutines for e.g. moving an entire stack to another place, we use an
infinite inductive type Λ' so that we can execute a program and then return to do something else
without having to define too many different kinds of intermediate states. (We must nevertheless
prove that only finitely many labels are accessible.) The labels are:
move p k₁ k₂ q: move elements from stackk₁tok₂whilepholds of the value being moved. The last element, that failsp, is placed in neither stack but left in the local store. At the end of the operation,k₂will have the elements ofk₁in reverse order. Then doq.clear p k q: delete elements from stackkuntilpis true. Likemove, the last element is left in the local storage. Then doq.copy q: Move all elements fromrevto bothmainandstack(in reverse order), then doq. That is, it takes(a, b, c, d)to(b.reverse ++ a, [], c, b.reverse ++ d).push k f q: pushf s, wheresis the local store, to stackk, then doq. This is a duplicate of thepushinstruction that is part of the TM2 model, but by having a subroutine just for this purpose we can build up programs to execute inside agotostatement, where we have the flexibility to be general recursive.read (f : Option Γ' → Λ'): go to statef swheresis the local store. Again this is only here for convenience.succ q: perform a successor operation. Assuming[n]is encoded onmainbefore,[n+1]will be on main after. This implements successor for binary natural numbers.pred q₁ q₂: perform a predecessor operation orcasestatement. If[]is encoded onmainbefore, then we transition toq₁with[]on main; if(0 :: v)is onmainbefore thenvwill be onmainafter and we transition toq₁; and if(n+1 :: v)is onmainbefore thenn :: vwill be onmainafter and we transition toq₂.ret k: call continuationk. Each continuation has its own interpretation of the data instackand sets up the data for the next continuation.ret (cons₁ fs k):v :: KDataonstackandnsonmain, and the next step expectsvonmainandns :: KDataonstack. So we have to do a little dance here with six reverse-moves using theauxstack to perform a three-point swap, each of which involves two reversals.ret (cons₂ k):ns :: KDatais onstackandvis onmain, and we have to putns.headI :: vonmainandKDataonstack. This is done using theheadsubroutine.ret (fix f k): This stores no data, so we just check ifmainstarts with0and if so, remove it and callk, otherwiseclearthe first value and callf.ret halt: the stack is empty, andmainhas the output. Do nothing and halt.
In addition to these basic states, we define some additional subroutines that are used in the above:
push',peek',pop'are special versions of the builtins that use the local store to supply inputs and outputs.unrev: special casemove false rev mainto move everything fromrevback tomain. Used as a cleanup operation in several functions.moveExcl p k₁ k₂ q: same asmovebut pushes the last value read back onto the source stack.move₂ p k₁ k₂ q: doublemove, so that the result comes out in the right order at the target stack. Implemented asmoveExcl p k rev; move false rev k₂. Assumes that neitherk₁nork₂isrevandrevis initially empty.head k q: get the first natural number from stackkand reverse-move it torev, then clear the rest of the list atkand thenunrevto reverse-move the head value tomain. This is used withk = mainto implement regularhead, i.e. ifvis onmainbefore then[v.headI]will be onmainafter; and also withk = stackfor theconsoperation, which hasvonmainandns :: KDataonstack, and results inKDataonstackandns.headI :: vonmain.trNormalis the main entry point, defining states that perform a givencodecomputation. It mostly just dispatches to functions written above.
The main theorem of this section is tr_eval, which asserts that for each that for each code c,
the state init c v steps to halt v' in finitely many steps if and only if
Code.eval c v = some v'.
Equations
Equations
- One or more equations did not get rendered due to their size.
The four stacks used by the program. main is used to store the input value in trNormal
mode and the output value in Λ'.ret mode, while stack is used to keep all the data for the
continuations. rev is used to store reversed lists when transferring values between stacks, and
aux is only used once in cons₁. See the section documentation.
Instances For
Equations
Continuations as in ToPartrec.Cont but with the data removed. This is done because we want
the set of all continuations in the program to be finite (so that it can ultimately be encoded into
the finite state machine of a Turing machine), but a continuation can handle a potentially infinite
number of data values during execution.
- halt : Cont'
- cons₁ : ToPartrec.Code → Cont' → Cont'
- cons₂ : Cont' → Cont'
- comp : ToPartrec.Code → Cont' → Cont'
- fix : ToPartrec.Code → Cont' → Cont'
Instances For
Equations
The set of program positions. We make extensive use of inductive types here to let us describe
"subroutines"; for example clear p k q is a program that clears stack k, then does q where
q is another label. In order to prevent this from resulting in an infinite number of distinct
accessible states, we are careful to be non-recursive (although loops are okay). See the section
documentation for a description of all the programs.
- move (p : Γ' → Bool) (k₁ k₂ : K') (q : Λ') : Λ'
- clear (p : Γ' → Bool) (k : K') (q : Λ') : Λ'
- copy (q : Λ') : Λ'
- push (k : K') (s : Option Γ' → Option Γ') (q : Λ') : Λ'
- read (f : Option Γ' → Λ') : Λ'
- succ (q : Λ') : Λ'
- pred (q₁ q₂ : Λ') : Λ'
- ret (k : Cont') : Λ'
Instances For
Equations
Equations
- One or more equations did not get rendered due to their size.
The type of TM2 statements used by this machine.
Equations
Instances For
The type of TM2 configurations used by this machine.
Equations
Instances For
Pop a value from the stack and place the result in local store.
Equations
- Turing.PartrecToTM2.pop' k = Turing.TM2.Stmt.pop k fun (x v : Option Turing.PartrecToTM2.Γ') => v
Instances For
Peek a value from the stack and place the result in local store.
Equations
- Turing.PartrecToTM2.peek' k = Turing.TM2.Stmt.peek k fun (x v : Option Turing.PartrecToTM2.Γ') => v
Instances For
Push the value in the local store to the given stack.
Equations
- Turing.PartrecToTM2.push' k = Turing.TM2.Stmt.push k fun (x : Option Turing.PartrecToTM2.Γ') => x.iget
Instances For
Move elements from k₁ to k₂ while p holds, with the last element being left on k₁.
Equations
- Turing.PartrecToTM2.moveExcl p k₁ k₂ q = Turing.PartrecToTM2.Λ'.move p k₁ k₂ (Turing.PartrecToTM2.Λ'.push k₁ id q)
Instances For
The program that evaluates code c with continuation k. This expects an initial state where
trList v is on main, trContStack k is on stack, and aux and rev are empty.
See the section documentation for details.
Equations
- One or more equations did not get rendered due to their size.
- Turing.PartrecToTM2.trNormal Turing.ToPartrec.Code.succ x✝ = Turing.PartrecToTM2.head Turing.PartrecToTM2.K'.main (Turing.PartrecToTM2.Λ'.ret x✝).succ
- Turing.PartrecToTM2.trNormal Turing.ToPartrec.Code.tail x✝ = Turing.PartrecToTM2.Λ'.clear Turing.PartrecToTM2.natEnd Turing.PartrecToTM2.K'.main (Turing.PartrecToTM2.Λ'.ret x✝)
- Turing.PartrecToTM2.trNormal (f.comp g) x✝ = Turing.PartrecToTM2.trNormal g (Turing.PartrecToTM2.Cont'.comp f x✝)
- Turing.PartrecToTM2.trNormal (f.case g) x✝ = (Turing.PartrecToTM2.trNormal f x✝).pred (Turing.PartrecToTM2.trNormal g x✝)
- Turing.PartrecToTM2.trNormal f.fix x✝ = Turing.PartrecToTM2.trNormal f (Turing.PartrecToTM2.Cont'.fix f x✝)
Instances For
The main program. See the section documentation for details.
Equations
- One or more equations did not get rendered due to their size.
- Turing.PartrecToTM2.tr (Turing.PartrecToTM2.Λ'.read q) = Turing.TM2.Stmt.goto q
- Turing.PartrecToTM2.tr (Turing.PartrecToTM2.Λ'.ret (Turing.PartrecToTM2.Cont'.comp f k)) = Turing.TM2.Stmt.goto fun (x : Option Turing.PartrecToTM2.Γ') => Turing.PartrecToTM2.trNormal f k
- Turing.PartrecToTM2.tr (Turing.PartrecToTM2.Λ'.ret Turing.PartrecToTM2.Cont'.halt) = Turing.TM2.Stmt.load (fun (x : Option Turing.PartrecToTM2.Γ') => none) Turing.TM2.Stmt.halt
Instances For
Translating a Cont continuation to a Cont' continuation simply entails dropping all the
data. This data is instead encoded in trContStack in the configuration.
Equations
- Turing.PartrecToTM2.trCont Turing.ToPartrec.Cont.halt = Turing.PartrecToTM2.Cont'.halt
- Turing.PartrecToTM2.trCont (Turing.ToPartrec.Cont.cons₁ c a k) = Turing.PartrecToTM2.Cont'.cons₁ c (Turing.PartrecToTM2.trCont k)
- Turing.PartrecToTM2.trCont (Turing.ToPartrec.Cont.cons₂ a k) = (Turing.PartrecToTM2.trCont k).cons₂
- Turing.PartrecToTM2.trCont (Turing.ToPartrec.Cont.comp c k) = Turing.PartrecToTM2.Cont'.comp c (Turing.PartrecToTM2.trCont k)
- Turing.PartrecToTM2.trCont (Turing.ToPartrec.Cont.fix c k) = Turing.PartrecToTM2.Cont'.fix c (Turing.PartrecToTM2.trCont k)
Instances For
We use PosNum to define the translation of binary natural numbers. A natural number is
represented as a little-endian list of bit0 and bit1 elements:
1 = [bit1]
2 = [bit0, bit1]
3 = [bit1, bit1]
4 = [bit0, bit0, bit1]
In particular, this representation guarantees no trailing bit0's at the end of the list.
Equations
Instances For
Lists are translated with a cons after each encoded number.
For example:
[] = []
[0] = [cons]
[1] = [bit1, cons]
[6, 0] = [bit0, bit1, bit1, cons, cons]
Equations
Instances For
Lists of lists are translated with a consₗ after each encoded list.
For example:
[] = []
[[]] = [consₗ]
[[], []] = [consₗ, consₗ]
[[0]] = [cons, consₗ]
[[1, 2], [0]] = [bit1, cons, bit0, bit1, cons, consₗ, cons, consₗ]
Equations
Instances For
The data part of a continuation is a list of lists, which is encoded on the stack stack
using trLList.
Equations
- Turing.PartrecToTM2.contStack Turing.ToPartrec.Cont.halt = []
- Turing.PartrecToTM2.contStack (Turing.ToPartrec.Cont.cons₁ c a k) = a :: Turing.PartrecToTM2.contStack k
- Turing.PartrecToTM2.contStack (Turing.ToPartrec.Cont.cons₂ a k) = a :: Turing.PartrecToTM2.contStack k
- Turing.PartrecToTM2.contStack (Turing.ToPartrec.Cont.comp c k) = Turing.PartrecToTM2.contStack k
- Turing.PartrecToTM2.contStack (Turing.ToPartrec.Cont.fix c k) = Turing.PartrecToTM2.contStack k
Instances For
This is the nondependent eliminator for K', but we use it specifically here in order to
represent the stack data as four lists rather than as a function K' → List Γ', because this makes
rewrites easier. The theorems K'.elim_update_main et. al. show how such a function is updated
after an update to one of the components.
Equations
Instances For
The halting state corresponding to a List ℕ output value.
Equations
- Turing.PartrecToTM2.halt v = { l := none, var := none, stk := Turing.PartrecToTM2.K'.elim (Turing.PartrecToTM2.trList v) [] [] [] }
Instances For
The Cfg states map to Cfg' states almost one to one, except that in normal operation the
local store contains an arbitrary garbage value. To make the final theorem cleaner we explicitly
clear it in the halt state so that there is exactly one configuration corresponding to output v.
Equations
- One or more equations did not get rendered due to their size.
- Turing.PartrecToTM2.TrCfg (Turing.ToPartrec.Cfg.halt v) x✝ = (x✝ = Turing.PartrecToTM2.halt v)
Instances For
This could be a general list definition, but it is also somewhat specialized to this
application. splitAtPred p L will search L for the first element satisfying p.
If it is found, say L = l₁ ++ a :: l₂ where a satisfies p but l₁ does not, then it returns
(l₁, some a, l₂). Otherwise, if there is no such element, it returns (L, none, []).
Equations
Instances For
The initial state, evaluating function c on input v.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The set of machine states reachable via downward label jumps, discounting jumps via ret.
Equations
- One or more equations did not get rendered due to their size.
- Turing.PartrecToTM2.trStmts₁ (Turing.PartrecToTM2.Λ'.move p k₁ k₂ q) = insert (Turing.PartrecToTM2.Λ'.move p k₁ k₂ q) (Turing.PartrecToTM2.trStmts₁ q)
- Turing.PartrecToTM2.trStmts₁ (Turing.PartrecToTM2.Λ'.push k s q) = insert (Turing.PartrecToTM2.Λ'.push k s q) (Turing.PartrecToTM2.trStmts₁ q)
- Turing.PartrecToTM2.trStmts₁ (Turing.PartrecToTM2.Λ'.clear p k q) = insert (Turing.PartrecToTM2.Λ'.clear p k q) (Turing.PartrecToTM2.trStmts₁ q)
- Turing.PartrecToTM2.trStmts₁ q.copy = insert q.copy (Turing.PartrecToTM2.trStmts₁ q)
- Turing.PartrecToTM2.trStmts₁ q.succ = insert q.succ (insert (Turing.PartrecToTM2.unrev q) (Turing.PartrecToTM2.trStmts₁ q))
- Turing.PartrecToTM2.trStmts₁ (q₁.pred q₂) = insert (q₁.pred q₂) (Turing.PartrecToTM2.trStmts₁ q₁ ∪ insert (Turing.PartrecToTM2.unrev q₂) (Turing.PartrecToTM2.trStmts₁ q₂))
- Turing.PartrecToTM2.trStmts₁ (Turing.PartrecToTM2.Λ'.ret k) = {Turing.PartrecToTM2.Λ'.ret k}
Instances For
The (finite!) set of machine states visited during the course of evaluation of c,
including the state ret k but not any states after that (that is, the states visited while
evaluating k).
Equations
- One or more equations did not get rendered due to their size.
- Turing.PartrecToTM2.codeSupp' Turing.ToPartrec.Code.zero' x✝ = Turing.PartrecToTM2.trStmts₁ (Turing.PartrecToTM2.trNormal Turing.ToPartrec.Code.zero' x✝)
- Turing.PartrecToTM2.codeSupp' Turing.ToPartrec.Code.succ x✝ = Turing.PartrecToTM2.trStmts₁ (Turing.PartrecToTM2.trNormal Turing.ToPartrec.Code.succ x✝)
- Turing.PartrecToTM2.codeSupp' Turing.ToPartrec.Code.tail x✝ = Turing.PartrecToTM2.trStmts₁ (Turing.PartrecToTM2.trNormal Turing.ToPartrec.Code.tail x✝)
Instances For
The (finite!) set of machine states visited during the course of evaluation of a continuation
k, not including the initial state ret k.
Equations
- One or more equations did not get rendered due to their size.
- Turing.PartrecToTM2.contSupp k.cons₂ = Turing.PartrecToTM2.trStmts₁ (Turing.PartrecToTM2.head Turing.PartrecToTM2.K'.stack (Turing.PartrecToTM2.Λ'.ret k)) ∪ Turing.PartrecToTM2.contSupp k
- Turing.PartrecToTM2.contSupp (Turing.PartrecToTM2.Cont'.comp f k) = Turing.PartrecToTM2.codeSupp' f k ∪ Turing.PartrecToTM2.contSupp k
- Turing.PartrecToTM2.contSupp (Turing.PartrecToTM2.Cont'.fix f k) = Turing.PartrecToTM2.codeSupp' f.fix k ∪ Turing.PartrecToTM2.contSupp k
- Turing.PartrecToTM2.contSupp Turing.PartrecToTM2.Cont'.halt = ∅
Instances For
The (finite!) set of machine states visited during the course of evaluation of c in
continuation k. This is actually closed under forward simulation (see tr_supports), and the
existence of this set means that the machine constructed in this section is in fact a proper
Turing machine, with a finite set of states.
Equations
Instances For
The statement Λ'.Supports S q means that contSupp k ⊆ S for any ret k
reachable from q.
(This is a technical condition used in the proof that the machine is supported.)
Equations
- Turing.PartrecToTM2.Λ'.Supports S (Turing.PartrecToTM2.Λ'.move p k₁ k₂ q) = Turing.PartrecToTM2.Λ'.Supports S q
- Turing.PartrecToTM2.Λ'.Supports S (Turing.PartrecToTM2.Λ'.push k s q) = Turing.PartrecToTM2.Λ'.Supports S q
- Turing.PartrecToTM2.Λ'.Supports S (Turing.PartrecToTM2.Λ'.read q) = ∀ (s : Option Turing.PartrecToTM2.Γ'), Turing.PartrecToTM2.Λ'.Supports S (q s)
- Turing.PartrecToTM2.Λ'.Supports S (Turing.PartrecToTM2.Λ'.clear p k q) = Turing.PartrecToTM2.Λ'.Supports S q
- Turing.PartrecToTM2.Λ'.Supports S q.copy = Turing.PartrecToTM2.Λ'.Supports S q
- Turing.PartrecToTM2.Λ'.Supports S q.succ = Turing.PartrecToTM2.Λ'.Supports S q
- Turing.PartrecToTM2.Λ'.Supports S (q₁.pred q₂) = (Turing.PartrecToTM2.Λ'.Supports S q₁ ∧ Turing.PartrecToTM2.Λ'.Supports S q₂)
- Turing.PartrecToTM2.Λ'.Supports S (Turing.PartrecToTM2.Λ'.ret k) = (Turing.PartrecToTM2.contSupp k ⊆ S)
Instances For
A shorthand for the predicate that we are proving in the main theorems trStmts₁_supports,
codeSupp'_supports, contSupp_supports, codeSupp_supports. The set S is fixed throughout
the proof, and denotes the full set of states in the machine, while K is a subset that we are
currently proving a property about. The predicate asserts that every state in K is closed in S
under forward simulation, i.e. stepping forward through evaluation starting from any state in K
stays entirely within S.
Equations
- Turing.PartrecToTM2.Supports K S = ∀ q ∈ K, Turing.TM2.SupportsStmt S (Turing.PartrecToTM2.tr q)
Instances For
The set codeSupp c k is a finite set that witnesses the effective finiteness of the tr
Turing machine. Starting from the initial state trNormal c k, forward simulation uses only
states in codeSupp c k, so this is a finite state machine. Even though the underlying type of
state labels Λ' is infinite, for a given partial recursive function c and continuation k,
only finitely many states are accessed, corresponding roughly to subterms of c.