The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V32. Logic, model theory and computability

Mathlib.Computability.TuringMachine.Config

112 declarations. Mathlib docs · Source

Modelling partial recursive functions using Turing machines

The files Config and ToPartrec 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

  • ToPartrec.Code: a simplified basis for partial recursive functions, valued in List ℕ →. List ℕ.
    • ToPartrec.Code.eval: semantics for a ToPartrec.Code program

A simplified basis for partrec

This section constructs the type Code, which is a data type of programs with List ℕ input and output, with enough expressivity to write any partial recursive function. The primitives are:

  • zero' appends a 0 to the input. That is, zero' v = 0 :: v.
  • succ returns the successor of the head of the input, defaulting to zero if there is no head:
    • succ [] = [1]
    • succ (n :: v) = [n + 1]
  • tail returns the tail of the input
    • tail [] = []
    • tail (n :: v) = v
  • cons f fs calls f and fs on the input and conses the results:
    • cons f fs v = (f v).head :: fs v
  • comp f g calls f on the output of g:
    • comp f g v = f (g v)
  • case f g cases on the head of the input, calling f or g depending on whether it is zero or a successor (similar to Nat.casesOn).
    • case f g [] = f []
    • case f g (0 :: v) = f v
    • case f g (n+1 :: v) = g (n :: v)
  • fix f calls f repeatedly, using the head of the result of f to decide whether to call f again or finish:
    • fix f v = [] if f v = []
    • fix f v = w if f v = 0 :: w
    • fix f v = fix f w if f v = n+1 :: w (the exact value of n is discarded)

This basis is convenient because it is closer to the Turing machine model - the key operations are splitting and merging of lists of unknown length, while the messy n-ary composition operation from the traditional basis for partial recursive functions is absent - but it retains a compositional semantics. The first step in transitioning to Turing machines is to make a sequential evaluator for this basis, which we take up in the next section.

From compositional semantics to sequential semantics

Our initial sequential model is designed to be as similar as possible to the compositional semantics in terms of its primitives, but it is a sequential semantics, meaning that rather than defining an eval c : List ℕ →. List ℕ function for each program, defined by recursion on programs, we have a type Cfg with a step function step : Cfg → Option cfg that provides a deterministic evaluation order. In order to do this, we introduce the notion of a continuation, which can be viewed as a Code with a hole in it where evaluation is currently taking place. Continuations can be assigned a List ℕ →. List ℕ semantics as well, with the interpretation being that given a List ℕ result returned from the code in the hole, the remainder of the program will evaluate to a List ℕ final value.

The continuations are:

  • halt: the empty continuation: the hole is the whole program, whatever is returned is the final result. In our notation this is just _.
  • cons₁ fs v k: evaluating the first part of a cons, that is k (_ :: fs v), where k is the outer continuation.
  • cons₂ ns k: evaluating the second part of a cons: k (ns.headI :: _). (Technically we don't need to hold on to all of ns here since we are already committed to taking the head, but this is more regular.)
  • comp f k: evaluating the first part of a composition: k (f _).
  • fix f k: waiting for the result of f in a fix f expression: k (if _.headI = 0 then _.tail else fix f (_.tail))

The type Cfg of evaluation states is:

  • ret k v: we have received a result, and are now evaluating the continuation k with result v; that is, k v where k is ready to evaluate.
  • halt v: we are done and the result is v.

The main theorem of this section is that for each code c, the state stepNormal c halt v steps to v' in finitely many steps if and only if Code.eval c v = some v'.

theorem Turing.ToPartrec.Cont.eval.match_1.congr_eq_5 E1 #

∀ (motive : Turing.ToPartrec.Cont → Sort u_1) (x : Turing.ToPartrec.Cont)
  (h_1 : Unit → motive Turing.ToPartrec.Cont.halt)
  (h_2 :
    (fs : Turing.ToPartrec.Code) →
      (as : List ℕ) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.cons₁ fs as k))
  (h_3 : (ns : List ℕ) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.cons₂ ns k))
  (h_4 : (f : Turing.ToPartrec.Code) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.comp f k))
  (h_5 : (f : Turing.ToPartrec.Code) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.fix f k))
  (f : Turing.ToPartrec.Code) (k : Turing.ToPartrec.Cont),
  x = Turing.ToPartrec.Cont.fix f k →
    (match x with
      | Turing.ToPartrec.Cont.halt => h_1 ()
      | Turing.ToPartrec.Cont.cons₁ fs as k => h_2 fs as k
      | Turing.ToPartrec.Cont.cons₂ ns k => h_3 ns k
      | Turing.ToPartrec.Cont.comp f k => h_4 f k
      | Turing.ToPartrec.Cont.fix f k => h_5 f k) ≍
      h_5 f k

theorem Turing.ToPartrec.Cfg.ret.injEq E0 #

∀ (a : Turing.ToPartrec.Cont) (a_1 : List ℕ) (a_2 : Turing.ToPartrec.Cont) (a_3 : List ℕ),
  (Turing.ToPartrec.Cfg.ret a a_1 = Turing.ToPartrec.Cfg.ret a_2 a_3) = (a = a_2 ∧ a_1 = a_3)

theorem Turing.ToPartrec.Code.fix.injEq E0 #

∀ (a a_1 : Turing.ToPartrec.Code), (a.fix = a_1.fix) = (a = a_1)

abbrev Turing.ToPartrec.Code.brecOn.go E1 #

{motive : Turing.ToPartrec.Code → Sort u} →
  (t : Turing.ToPartrec.Code) →
    ((t : Turing.ToPartrec.Code) → Turing.ToPartrec.Code.below t → motive t) → motive t ×' Turing.ToPartrec.Code.below t

def Turing.ToPartrec.instInhabitedCfg.default E1 #

Turing.ToPartrec.Cfg

theorem Turing.ToPartrec.Code.case.inj E1 #

∀ {a a_1 a_2 a_3 : Turing.ToPartrec.Code}, a.case a_1 = a_2.case a_3 → a = a_2 ∧ a_1 = a_3

theorem Turing.ToPartrec.Cont.cons₁.inj E1 #

∀ {a : Turing.ToPartrec.Code} {a_1 : List ℕ} {a_2 : Turing.ToPartrec.Cont} {a_3 : Turing.ToPartrec.Code} {a_4 : List ℕ}
  {a_5 : Turing.ToPartrec.Cont},
  Turing.ToPartrec.Cont.cons₁ a a_1 a_2 = Turing.ToPartrec.Cont.cons₁ a_3 a_4 a_5 → a = a_3 ∧ a_1 = a_4 ∧ a_2 = a_5

abbrev Turing.ToPartrec.Code.ctorElimType E1 #

{motive : Turing.ToPartrec.Code → Sort u} → ℕ → Sort (max 1 u)

def Turing.ToPartrec.Code.ctorIdx E1 #

Turing.ToPartrec.Code → ℕ

theorem Turing.ToPartrec.Cont.eval.match_1.congr_eq_3 E1 #

∀ (motive : Turing.ToPartrec.Cont → Sort u_1) (x : Turing.ToPartrec.Cont)
  (h_1 : Unit → motive Turing.ToPartrec.Cont.halt)
  (h_2 :
    (fs : Turing.ToPartrec.Code) →
      (as : List ℕ) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.cons₁ fs as k))
  (h_3 : (ns : List ℕ) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.cons₂ ns k))
  (h_4 : (f : Turing.ToPartrec.Code) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.comp f k))
  (h_5 : (f : Turing.ToPartrec.Code) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.fix f k))
  (ns : List ℕ) (k : Turing.ToPartrec.Cont),
  x = Turing.ToPartrec.Cont.cons₂ ns k →
    (match x with
      | Turing.ToPartrec.Cont.halt => h_1 ()
      | Turing.ToPartrec.Cont.cons₁ fs as k => h_2 fs as k
      | Turing.ToPartrec.Cont.cons₂ ns k => h_3 ns k
      | Turing.ToPartrec.Cont.comp f k => h_4 f k
      | Turing.ToPartrec.Cont.fix f k => h_5 f k) ≍
      h_3 ns k

theorem Turing.ToPartrec.stepRet.match_1.congr_eq_4 E1 #

∀ (motive : Turing.ToPartrec.Cont → List ℕ → Sort u_1) (x : Turing.ToPartrec.Cont) (x_1 : List ℕ)
  (h_1 : (v : List ℕ) → motive Turing.ToPartrec.Cont.halt v)
  (h_2 :
    (fs : Turing.ToPartrec.Code) →
      (as : List ℕ) → (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.cons₁ fs as k) v)
  (h_3 : (ns : List ℕ) → (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.cons₂ ns k) v)
  (h_4 :
    (f : Turing.ToPartrec.Code) →
      (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.comp f k) v)
  (h_5 :
    (f : Turing.ToPartrec.Code) → (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.fix f k) v)
  (f : Turing.ToPartrec.Code) (k : Turing.ToPartrec.Cont) (v : List ℕ),
  x = Turing.ToPartrec.Cont.comp f k →
    x_1 = v →
      (match x, x_1 with
        | Turing.ToPartrec.Cont.halt, v => h_1 v
        | Turing.ToPartrec.Cont.cons₁ fs as k, v => h_2 fs as k v
        | Turing.ToPartrec.Cont.cons₂ ns k, v => h_3 ns k v
        | Turing.ToPartrec.Cont.comp f k, v => h_4 f k v
        | Turing.ToPartrec.Cont.fix f k, v => h_5 f k v) ≍
        h_4 f k v

theorem Turing.ToPartrec.Cont.eval.match_1.congr_eq_1 E1 #

∀ (motive : Turing.ToPartrec.Cont → Sort u_1) (x : Turing.ToPartrec.Cont)
  (h_1 : Unit → motive Turing.ToPartrec.Cont.halt)
  (h_2 :
    (fs : Turing.ToPartrec.Code) →
      (as : List ℕ) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.cons₁ fs as k))
  (h_3 : (ns : List ℕ) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.cons₂ ns k))
  (h_4 : (f : Turing.ToPartrec.Code) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.comp f k))
  (h_5 : (f : Turing.ToPartrec.Code) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.fix f k)),
  x = Turing.ToPartrec.Cont.halt →
    (match x with
      | Turing.ToPartrec.Cont.halt => h_1 ()
      | Turing.ToPartrec.Cont.cons₁ fs as k => h_2 fs as k
      | Turing.ToPartrec.Cont.cons₂ ns k => h_3 ns k
      | Turing.ToPartrec.Cont.comp f k => h_4 f k
      | Turing.ToPartrec.Cont.fix f k => h_5 f k) ≍
      h_1 ()

theorem Turing.ToPartrec.Cont.comp.injEq E0 #

∀ (a : Turing.ToPartrec.Code) (a_1 : Turing.ToPartrec.Cont) (a_2 : Turing.ToPartrec.Code) (a_3 : Turing.ToPartrec.Cont),
  (Turing.ToPartrec.Cont.comp a a_1 = Turing.ToPartrec.Cont.comp a_2 a_3) = (a = a_2 ∧ a_1 = a_3)

theorem Turing.ToPartrec.Code.comp.inj E1 #

∀ {a a_1 a_2 a_3 : Turing.ToPartrec.Code}, a.comp a_1 = a_2.comp a_3 → a = a_2 ∧ a_1 = a_3

theorem Turing.ToPartrec.Code.eval.match_1.congr_eq_4 E1 #

∀ (motive : Turing.ToPartrec.Code → Sort u_1) (x : Turing.ToPartrec.Code)
  (h_1 : Unit → motive Turing.ToPartrec.Code.zero') (h_2 : Unit → motive Turing.ToPartrec.Code.succ)
  (h_3 : Unit → motive Turing.ToPartrec.Code.tail) (h_4 : (f fs : Turing.ToPartrec.Code) → motive (f.cons fs))
  (h_5 : (f g : Turing.ToPartrec.Code) → motive (f.comp g)) (h_6 : (f g : Turing.ToPartrec.Code) → motive (f.case g))
  (h_7 : (f : Turing.ToPartrec.Code) → motive f.fix) (f fs : Turing.ToPartrec.Code),
  x = f.cons fs →
    (match x with
      | Turing.ToPartrec.Code.zero' => h_1 ()
      | Turing.ToPartrec.Code.succ => h_2 ()
      | Turing.ToPartrec.Code.tail => h_3 ()
      | f.cons fs => h_4 f fs
      | f.comp g => h_5 f g
      | f.case g => h_6 f g
      | f.fix => h_7 f) ≍
      h_4 f fs

theorem Turing.ToPartrec.Code.fix.inj E1 #

∀ {a a_1 : Turing.ToPartrec.Code}, a.fix = a_1.fix → a = a_1

theorem Turing.ToPartrec.Code.eval.match_1.congr_eq_3 E1 #

∀ (motive : Turing.ToPartrec.Code → Sort u_1) (x : Turing.ToPartrec.Code)
  (h_1 : Unit → motive Turing.ToPartrec.Code.zero') (h_2 : Unit → motive Turing.ToPartrec.Code.succ)
  (h_3 : Unit → motive Turing.ToPartrec.Code.tail) (h_4 : (f fs : Turing.ToPartrec.Code) → motive (f.cons fs))
  (h_5 : (f g : Turing.ToPartrec.Code) → motive (f.comp g)) (h_6 : (f g : Turing.ToPartrec.Code) → motive (f.case g))
  (h_7 : (f : Turing.ToPartrec.Code) → motive f.fix),
  x = Turing.ToPartrec.Code.tail →
    (match x with
      | Turing.ToPartrec.Code.zero' => h_1 ()
      | Turing.ToPartrec.Code.succ => h_2 ()
      | Turing.ToPartrec.Code.tail => h_3 ()
      | f.cons fs => h_4 f fs
      | f.comp g => h_5 f g
      | f.case g => h_6 f g
      | f.fix => h_7 f) ≍
      h_3 ()

theorem Turing.ToPartrec.step.match_1.congr_eq_2 E1 #

∀ (motive : Turing.ToPartrec.Cfg → Sort u_1) (x : Turing.ToPartrec.Cfg)
  (h_1 : (a : List ℕ) → motive (Turing.ToPartrec.Cfg.halt a))
  (h_2 : (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cfg.ret k v)) (k : Turing.ToPartrec.Cont)
  (v : List ℕ),
  x = Turing.ToPartrec.Cfg.ret k v →
    (match x with
      | Turing.ToPartrec.Cfg.halt a => h_1 a
      | Turing.ToPartrec.Cfg.ret k v => h_2 k v) ≍
      h_2 k v

theorem Turing.ToPartrec.Cont.brecOn.eq E1 #

∀ {motive : Turing.ToPartrec.Cont → Sort u} (t : Turing.ToPartrec.Cont)
  (F_1 : (t : Turing.ToPartrec.Cont) → Turing.ToPartrec.Cont.below t → motive t),
  Turing.ToPartrec.Cont.brecOn t F_1 = F_1 t (Turing.ToPartrec.Cont.brecOn.go t F_1).2

theorem Turing.ToPartrec.stepRet.match_1.congr_eq_2 E1 #

∀ (motive : Turing.ToPartrec.Cont → List ℕ → Sort u_1) (x : Turing.ToPartrec.Cont) (x_1 : List ℕ)
  (h_1 : (v : List ℕ) → motive Turing.ToPartrec.Cont.halt v)
  (h_2 :
    (fs : Turing.ToPartrec.Code) →
      (as : List ℕ) → (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.cons₁ fs as k) v)
  (h_3 : (ns : List ℕ) → (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.cons₂ ns k) v)
  (h_4 :
    (f : Turing.ToPartrec.Code) →
      (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.comp f k) v)
  (h_5 :
    (f : Turing.ToPartrec.Code) → (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.fix f k) v)
  (fs : Turing.ToPartrec.Code) (as : List ℕ) (k : Turing.ToPartrec.Cont) (v : List ℕ),
  x = Turing.ToPartrec.Cont.cons₁ fs as k →
    x_1 = v →
      (match x, x_1 with
        | Turing.ToPartrec.Cont.halt, v => h_1 v
        | Turing.ToPartrec.Cont.cons₁ fs as k, v => h_2 fs as k v
        | Turing.ToPartrec.Cont.cons₂ ns k, v => h_3 ns k v
        | Turing.ToPartrec.Cont.comp f k, v => h_4 f k v
        | Turing.ToPartrec.Cont.fix f k, v => h_5 f k v) ≍
        h_2 fs as k v

theorem Turing.ToPartrec.Code.eval.match_1.congr_eq_1 E1 #

∀ (motive : Turing.ToPartrec.Code → Sort u_1) (x : Turing.ToPartrec.Code)
  (h_1 : Unit → motive Turing.ToPartrec.Code.zero') (h_2 : Unit → motive Turing.ToPartrec.Code.succ)
  (h_3 : Unit → motive Turing.ToPartrec.Code.tail) (h_4 : (f fs : Turing.ToPartrec.Code) → motive (f.cons fs))
  (h_5 : (f g : Turing.ToPartrec.Code) → motive (f.comp g)) (h_6 : (f g : Turing.ToPartrec.Code) → motive (f.case g))
  (h_7 : (f : Turing.ToPartrec.Code) → motive f.fix),
  x = Turing.ToPartrec.Code.zero' →
    (match x with
      | Turing.ToPartrec.Code.zero' => h_1 ()
      | Turing.ToPartrec.Code.succ => h_2 ()
      | Turing.ToPartrec.Code.tail => h_3 ()
      | f.cons fs => h_4 f fs
      | f.comp g => h_5 f g
      | f.case g => h_6 f g
      | f.fix => h_7 f) ≍
      h_1 ()

theorem Turing.ToPartrec.Cont.eval.match_1.congr_eq_2 E1 #

∀ (motive : Turing.ToPartrec.Cont → Sort u_1) (x : Turing.ToPartrec.Cont)
  (h_1 : Unit → motive Turing.ToPartrec.Cont.halt)
  (h_2 :
    (fs : Turing.ToPartrec.Code) →
      (as : List ℕ) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.cons₁ fs as k))
  (h_3 : (ns : List ℕ) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.cons₂ ns k))
  (h_4 : (f : Turing.ToPartrec.Code) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.comp f k))
  (h_5 : (f : Turing.ToPartrec.Code) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.fix f k))
  (fs : Turing.ToPartrec.Code) (as : List ℕ) (k : Turing.ToPartrec.Cont),
  x = Turing.ToPartrec.Cont.cons₁ fs as k →
    (match x with
      | Turing.ToPartrec.Cont.halt => h_1 ()
      | Turing.ToPartrec.Cont.cons₁ fs as k => h_2 fs as k
      | Turing.ToPartrec.Cont.cons₂ ns k => h_3 ns k
      | Turing.ToPartrec.Cont.comp f k => h_4 f k
      | Turing.ToPartrec.Cont.fix f k => h_5 f k) ≍
      h_2 fs as k

theorem Turing.ToPartrec.Cfg.halt.inj E1 #

∀ {a a_1 : List ℕ}, Turing.ToPartrec.Cfg.halt a = Turing.ToPartrec.Cfg.halt a_1 → a = a_1

abbrev Turing.ToPartrec.Cont.brecOn.go E1 #

{motive : Turing.ToPartrec.Cont → Sort u} →
  (t : Turing.ToPartrec.Cont) →
    ((t : Turing.ToPartrec.Cont) → Turing.ToPartrec.Cont.below t → motive t) → motive t ×' Turing.ToPartrec.Cont.below t

theorem Turing.ToPartrec.Code.eval.match_1.congr_eq_5 E1 #

∀ (motive : Turing.ToPartrec.Code → Sort u_1) (x : Turing.ToPartrec.Code)
  (h_1 : Unit → motive Turing.ToPartrec.Code.zero') (h_2 : Unit → motive Turing.ToPartrec.Code.succ)
  (h_3 : Unit → motive Turing.ToPartrec.Code.tail) (h_4 : (f fs : Turing.ToPartrec.Code) → motive (f.cons fs))
  (h_5 : (f g : Turing.ToPartrec.Code) → motive (f.comp g)) (h_6 : (f g : Turing.ToPartrec.Code) → motive (f.case g))
  (h_7 : (f : Turing.ToPartrec.Code) → motive f.fix) (f g : Turing.ToPartrec.Code),
  x = f.comp g →
    (match x with
      | Turing.ToPartrec.Code.zero' => h_1 ()
      | Turing.ToPartrec.Code.succ => h_2 ()
      | Turing.ToPartrec.Code.tail => h_3 ()
      | f.cons fs => h_4 f fs
      | f.comp g => h_5 f g
      | f.case g => h_6 f g
      | f.fix => h_7 f) ≍
      h_5 f g

theorem Turing.ToPartrec.Code.comp.injEq E0 #

∀ (a a_1 a_2 a_3 : Turing.ToPartrec.Code), (a.comp a_1 = a_2.comp a_3) = (a = a_2 ∧ a_1 = a_3)

def Turing.ToPartrec.instInhabitedCont.default E1 #

Turing.ToPartrec.Cont

theorem Turing.ToPartrec.Cont.eval.match_1.congr_eq_4 E1 #

∀ (motive : Turing.ToPartrec.Cont → Sort u_1) (x : Turing.ToPartrec.Cont)
  (h_1 : Unit → motive Turing.ToPartrec.Cont.halt)
  (h_2 :
    (fs : Turing.ToPartrec.Code) →
      (as : List ℕ) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.cons₁ fs as k))
  (h_3 : (ns : List ℕ) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.cons₂ ns k))
  (h_4 : (f : Turing.ToPartrec.Code) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.comp f k))
  (h_5 : (f : Turing.ToPartrec.Code) → (k : Turing.ToPartrec.Cont) → motive (Turing.ToPartrec.Cont.fix f k))
  (f : Turing.ToPartrec.Code) (k : Turing.ToPartrec.Cont),
  x = Turing.ToPartrec.Cont.comp f k →
    (match x with
      | Turing.ToPartrec.Cont.halt => h_1 ()
      | Turing.ToPartrec.Cont.cons₁ fs as k => h_2 fs as k
      | Turing.ToPartrec.Cont.cons₂ ns k => h_3 ns k
      | Turing.ToPartrec.Cont.comp f k => h_4 f k
      | Turing.ToPartrec.Cont.fix f k => h_5 f k) ≍
      h_4 f k

theorem Turing.ToPartrec.Code.eval.match_1.congr_eq_2 E1 #

∀ (motive : Turing.ToPartrec.Code → Sort u_1) (x : Turing.ToPartrec.Code)
  (h_1 : Unit → motive Turing.ToPartrec.Code.zero') (h_2 : Unit → motive Turing.ToPartrec.Code.succ)
  (h_3 : Unit → motive Turing.ToPartrec.Code.tail) (h_4 : (f fs : Turing.ToPartrec.Code) → motive (f.cons fs))
  (h_5 : (f g : Turing.ToPartrec.Code) → motive (f.comp g)) (h_6 : (f g : Turing.ToPartrec.Code) → motive (f.case g))
  (h_7 : (f : Turing.ToPartrec.Code) → motive f.fix),
  x = Turing.ToPartrec.Code.succ →
    (match x with
      | Turing.ToPartrec.Code.zero' => h_1 ()
      | Turing.ToPartrec.Code.succ => h_2 ()
      | Turing.ToPartrec.Code.tail => h_3 ()
      | f.cons fs => h_4 f fs
      | f.comp g => h_5 f g
      | f.case g => h_6 f g
      | f.fix => h_7 f) ≍
      h_2 ()

theorem Turing.ToPartrec.Cfg.halt.injEq E0 #

∀ (a a_1 : List ℕ), (Turing.ToPartrec.Cfg.halt a = Turing.ToPartrec.Cfg.halt a_1) = (a = a_1)

def Turing.ToPartrec.Cont.ctorIdx E1 #

Turing.ToPartrec.Cont → ℕ

theorem Turing.ToPartrec.Code.case.injEq E0 #

∀ (a a_1 a_2 a_3 : Turing.ToPartrec.Code), (a.case a_1 = a_2.case a_3) = (a = a_2 ∧ a_1 = a_3)

theorem Turing.ToPartrec.stepRet.match_1.congr_eq_5 E1 #

∀ (motive : Turing.ToPartrec.Cont → List ℕ → Sort u_1) (x : Turing.ToPartrec.Cont) (x_1 : List ℕ)
  (h_1 : (v : List ℕ) → motive Turing.ToPartrec.Cont.halt v)
  (h_2 :
    (fs : Turing.ToPartrec.Code) →
      (as : List ℕ) → (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.cons₁ fs as k) v)
  (h_3 : (ns : List ℕ) → (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.cons₂ ns k) v)
  (h_4 :
    (f : Turing.ToPartrec.Code) →
      (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.comp f k) v)
  (h_5 :
    (f : Turing.ToPartrec.Code) → (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.fix f k) v)
  (f : Turing.ToPartrec.Code) (k : Turing.ToPartrec.Cont) (v : List ℕ),
  x = Turing.ToPartrec.Cont.fix f k →
    x_1 = v →
      (match x, x_1 with
        | Turing.ToPartrec.Cont.halt, v => h_1 v
        | Turing.ToPartrec.Cont.cons₁ fs as k, v => h_2 fs as k v
        | Turing.ToPartrec.Cont.cons₂ ns k, v => h_3 ns k v
        | Turing.ToPartrec.Cont.comp f k, v => h_4 f k v
        | Turing.ToPartrec.Cont.fix f k, v => h_5 f k v) ≍
        h_5 f k v

theorem Turing.ToPartrec.Cont.cons₁.injEq E0 #

∀ (a : Turing.ToPartrec.Code) (a_1 : List ℕ) (a_2 : Turing.ToPartrec.Cont) (a_3 : Turing.ToPartrec.Code) (a_4 : List ℕ)
  (a_5 : Turing.ToPartrec.Cont),
  (Turing.ToPartrec.Cont.cons₁ a a_1 a_2 = Turing.ToPartrec.Cont.cons₁ a_3 a_4 a_5) = (a = a_3 ∧ a_1 = a_4 ∧ a_2 = a_5)

theorem Turing.ToPartrec.Cont.cons₂.injEq E0 #

∀ (a : List ℕ) (a_1 : Turing.ToPartrec.Cont) (a_2 : List ℕ) (a_3 : Turing.ToPartrec.Cont),
  (Turing.ToPartrec.Cont.cons₂ a a_1 = Turing.ToPartrec.Cont.cons₂ a_2 a_3) = (a = a_2 ∧ a_1 = a_3)

theorem Turing.ToPartrec.Code.eval.match_1.congr_eq_6 E1 #

∀ (motive : Turing.ToPartrec.Code → Sort u_1) (x : Turing.ToPartrec.Code)
  (h_1 : Unit → motive Turing.ToPartrec.Code.zero') (h_2 : Unit → motive Turing.ToPartrec.Code.succ)
  (h_3 : Unit → motive Turing.ToPartrec.Code.tail) (h_4 : (f fs : Turing.ToPartrec.Code) → motive (f.cons fs))
  (h_5 : (f g : Turing.ToPartrec.Code) → motive (f.comp g)) (h_6 : (f g : Turing.ToPartrec.Code) → motive (f.case g))
  (h_7 : (f : Turing.ToPartrec.Code) → motive f.fix) (f g : Turing.ToPartrec.Code),
  x = f.case g →
    (match x with
      | Turing.ToPartrec.Code.zero' => h_1 ()
      | Turing.ToPartrec.Code.succ => h_2 ()
      | Turing.ToPartrec.Code.tail => h_3 ()
      | f.cons fs => h_4 f fs
      | f.comp g => h_5 f g
      | f.case g => h_6 f g
      | f.fix => h_7 f) ≍
      h_6 f g

theorem Turing.ToPartrec.Cont.fix.injEq E0 #

∀ (a : Turing.ToPartrec.Code) (a_1 : Turing.ToPartrec.Cont) (a_2 : Turing.ToPartrec.Code) (a_3 : Turing.ToPartrec.Cont),
  (Turing.ToPartrec.Cont.fix a a_1 = Turing.ToPartrec.Cont.fix a_2 a_3) = (a = a_2 ∧ a_1 = a_3)

def Turing.ToPartrec.Cfg.ctorIdx E1 #

Turing.ToPartrec.Cfg → ℕ

theorem Turing.ToPartrec.stepRet.match_1.congr_eq_1 E1 #

∀ (motive : Turing.ToPartrec.Cont → List ℕ → Sort u_1) (x : Turing.ToPartrec.Cont) (x_1 : List ℕ)
  (h_1 : (v : List ℕ) → motive Turing.ToPartrec.Cont.halt v)
  (h_2 :
    (fs : Turing.ToPartrec.Code) →
      (as : List ℕ) → (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.cons₁ fs as k) v)
  (h_3 : (ns : List ℕ) → (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.cons₂ ns k) v)
  (h_4 :
    (f : Turing.ToPartrec.Code) →
      (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.comp f k) v)
  (h_5 :
    (f : Turing.ToPartrec.Code) → (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.fix f k) v)
  (v : List ℕ),
  x = Turing.ToPartrec.Cont.halt →
    x_1 = v →
      (match x, x_1 with
        | Turing.ToPartrec.Cont.halt, v => h_1 v
        | Turing.ToPartrec.Cont.cons₁ fs as k, v => h_2 fs as k v
        | Turing.ToPartrec.Cont.cons₂ ns k, v => h_3 ns k v
        | Turing.ToPartrec.Cont.comp f k, v => h_4 f k v
        | Turing.ToPartrec.Cont.fix f k, v => h_5 f k v) ≍
        h_1 v

def Turing.ToPartrec.instInhabitedCode.default E1 #

Turing.ToPartrec.Code

theorem Turing.ToPartrec.Cont.fix.inj E1 #

∀ {a : Turing.ToPartrec.Code} {a_1 : Turing.ToPartrec.Cont} {a_2 : Turing.ToPartrec.Code} {a_3 : Turing.ToPartrec.Cont},
  Turing.ToPartrec.Cont.fix a a_1 = Turing.ToPartrec.Cont.fix a_2 a_3 → a = a_2 ∧ a_1 = a_3

theorem Turing.ToPartrec.Code.cons.injEq E0 #

∀ (a a_1 a_2 a_3 : Turing.ToPartrec.Code), (a.cons a_1 = a_2.cons a_3) = (a = a_2 ∧ a_1 = a_3)

theorem Turing.ToPartrec.Code.eval.match_1.congr_eq_7 E1 #

∀ (motive : Turing.ToPartrec.Code → Sort u_1) (x : Turing.ToPartrec.Code)
  (h_1 : Unit → motive Turing.ToPartrec.Code.zero') (h_2 : Unit → motive Turing.ToPartrec.Code.succ)
  (h_3 : Unit → motive Turing.ToPartrec.Code.tail) (h_4 : (f fs : Turing.ToPartrec.Code) → motive (f.cons fs))
  (h_5 : (f g : Turing.ToPartrec.Code) → motive (f.comp g)) (h_6 : (f g : Turing.ToPartrec.Code) → motive (f.case g))
  (h_7 : (f : Turing.ToPartrec.Code) → motive f.fix) (f : Turing.ToPartrec.Code),
  x = f.fix →
    (match x with
      | Turing.ToPartrec.Code.zero' => h_1 ()
      | Turing.ToPartrec.Code.succ => h_2 ()
      | Turing.ToPartrec.Code.tail => h_3 ()
      | f.cons fs => h_4 f fs
      | f.comp g => h_5 f g
      | f.case g => h_6 f g
      | f.fix => h_7 f) ≍
      h_7 f

abbrev Turing.ToPartrec.Cfg.ctorElimType E1 #

{motive : Turing.ToPartrec.Cfg → Sort u} → ℕ → Sort (max 1 u)

theorem Turing.ToPartrec.step.match_1.congr_eq_1 E1 #

∀ (motive : Turing.ToPartrec.Cfg → Sort u_1) (x : Turing.ToPartrec.Cfg)
  (h_1 : (a : List ℕ) → motive (Turing.ToPartrec.Cfg.halt a))
  (h_2 : (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cfg.ret k v)) (a : List ℕ),
  x = Turing.ToPartrec.Cfg.halt a →
    (match x with
      | Turing.ToPartrec.Cfg.halt a => h_1 a
      | Turing.ToPartrec.Cfg.ret k v => h_2 k v) ≍
      h_1 a

abbrev Turing.ToPartrec.Cont.ctorElimType E1 #

{motive : Turing.ToPartrec.Cont → Sort u} → ℕ → Sort (max 1 u)

theorem Turing.ToPartrec.Code.cons.inj E1 #

∀ {a a_1 a_2 a_3 : Turing.ToPartrec.Code}, a.cons a_1 = a_2.cons a_3 → a = a_2 ∧ a_1 = a_3

theorem Turing.ToPartrec.Cont.cons₂.inj E1 #

∀ {a : List ℕ} {a_1 : Turing.ToPartrec.Cont} {a_2 : List ℕ} {a_3 : Turing.ToPartrec.Cont},
  Turing.ToPartrec.Cont.cons₂ a a_1 = Turing.ToPartrec.Cont.cons₂ a_2 a_3 → a = a_2 ∧ a_1 = a_3

theorem Turing.ToPartrec.stepRet.match_1.congr_eq_3 E1 #

∀ (motive : Turing.ToPartrec.Cont → List ℕ → Sort u_1) (x : Turing.ToPartrec.Cont) (x_1 : List ℕ)
  (h_1 : (v : List ℕ) → motive Turing.ToPartrec.Cont.halt v)
  (h_2 :
    (fs : Turing.ToPartrec.Code) →
      (as : List ℕ) → (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.cons₁ fs as k) v)
  (h_3 : (ns : List ℕ) → (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.cons₂ ns k) v)
  (h_4 :
    (f : Turing.ToPartrec.Code) →
      (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.comp f k) v)
  (h_5 :
    (f : Turing.ToPartrec.Code) → (k : Turing.ToPartrec.Cont) → (v : List ℕ) → motive (Turing.ToPartrec.Cont.fix f k) v)
  (ns : List ℕ) (k : Turing.ToPartrec.Cont) (v : List ℕ),
  x = Turing.ToPartrec.Cont.cons₂ ns k →
    x_1 = v →
      (match x, x_1 with
        | Turing.ToPartrec.Cont.halt, v => h_1 v
        | Turing.ToPartrec.Cont.cons₁ fs as k, v => h_2 fs as k v
        | Turing.ToPartrec.Cont.cons₂ ns k, v => h_3 ns k v
        | Turing.ToPartrec.Cont.comp f k, v => h_4 f k v
        | Turing.ToPartrec.Cont.fix f k, v => h_5 f k v) ≍
        h_3 ns k v

theorem Turing.ToPartrec.Cont.comp.inj E1 #

∀ {a : Turing.ToPartrec.Code} {a_1 : Turing.ToPartrec.Cont} {a_2 : Turing.ToPartrec.Code} {a_3 : Turing.ToPartrec.Cont},
  Turing.ToPartrec.Cont.comp a a_1 = Turing.ToPartrec.Cont.comp a_2 a_3 → a = a_2 ∧ a_1 = a_3

theorem Turing.ToPartrec.Code.brecOn.eq E1 #

∀ {motive : Turing.ToPartrec.Code → Sort u} (t : Turing.ToPartrec.Code)
  (F_1 : (t : Turing.ToPartrec.Code) → Turing.ToPartrec.Code.below t → motive t),
  Turing.ToPartrec.Code.brecOn t F_1 = F_1 t (Turing.ToPartrec.Code.brecOn.go t F_1).2

theorem Turing.ToPartrec.Cfg.ret.inj E1 #

∀ {a : Turing.ToPartrec.Cont} {a_1 : List ℕ} {a_2 : Turing.ToPartrec.Cont} {a_3 : List ℕ},
  Turing.ToPartrec.Cfg.ret a a_1 = Turing.ToPartrec.Cfg.ret a_2 a_3 → a = a_2 ∧ a_1 = a_3

inductive Turing.ToPartrec.Code E3 #

Type

The type of codes for primitive recursive functions. Unlike Nat.Partrec.Code, this uses a set of operations on List ℕ. See Code.eval for a description of the behavior of the primitives.

ctor Turing.ToPartrec.Code.zero' E0 #

Turing.ToPartrec.Code

ctor Turing.ToPartrec.Code.succ E0 #

Turing.ToPartrec.Code

ctor Turing.ToPartrec.Code.tail E0 #

Turing.ToPartrec.Code

ctor Turing.ToPartrec.Code.cons E0 #

Turing.ToPartrec.Code → Turing.ToPartrec.Code → Turing.ToPartrec.Code

ctor Turing.ToPartrec.Code.comp E0 #

Turing.ToPartrec.Code → Turing.ToPartrec.Code → Turing.ToPartrec.Code

ctor Turing.ToPartrec.Code.case E0 #

Turing.ToPartrec.Code → Turing.ToPartrec.Code → Turing.ToPartrec.Code

ctor Turing.ToPartrec.Code.fix E0 #

Turing.ToPartrec.Code → Turing.ToPartrec.Code

def Turing.ToPartrec.instDecidableEqCode.decEq E1 #

(x x_1 : Turing.ToPartrec.Code) → Decidable (x = x_1)

instance Turing.ToPartrec.instDecidableEqCode E0 #

DecidableEq Turing.ToPartrec.Code

instance Turing.ToPartrec.instInhabitedCode E0 #

Inhabited Turing.ToPartrec.Code

def Turing.ToPartrec.Code.eval E3 #

Turing.ToPartrec.Code → List ℕ →. List ℕ

The semantics of the Code primitives, as partial functions List ℕ →. List ℕ. By convention, functions that return a single result return a singleton [n], or in some cases n :: v where v will be ignored by a subsequent function.

  • zero' appends a 0 to the input. That is, zero' v = 0 :: v.
  • succ returns the successor of the head of the input, defaulting to zero if there is no head:
    • succ [] = [1]
    • succ (n :: v) = [n + 1]
  • tail returns the tail of the input
    • tail [] = []
    • tail (n :: v) = v
  • cons f fs calls f and fs on the input and conses the results:
    • cons f fs v = (f v).head :: fs v
  • comp f g calls f on the output of g:
    • comp f g v = f (g v)
  • case f g cases on the head of the input, calling f or g depending on whether it is zero or a successor (similar to Nat.casesOn).
    • case f g [] = f []
    • case f g (0 :: v) = f v
    • case f g (n+1 :: v) = g (n :: v)
  • fix f calls f repeatedly, using the head of the result of f to decide whether to call f again or finish:
    • fix f v = [] if f v = []
    • fix f v = w if f v = 0 :: w
    • fix f v = fix f w if f v = n+1 :: w (the exact value of n is discarded)

theorem Turing.ToPartrec.Code.zero'_eval E0 #

Turing.ToPartrec.Code.zero'.eval = fun v ↦ pure (0 :: v)

theorem Turing.ToPartrec.Code.succ_eval E0 #

Turing.ToPartrec.Code.succ.eval = fun v ↦ pure [v.headI.succ]

theorem Turing.ToPartrec.Code.tail_eval E0 #

Turing.ToPartrec.Code.tail.eval = fun v ↦ pure v.tail

theorem Turing.ToPartrec.Code.cons_eval E0 #

∀ (f fs : Turing.ToPartrec.Code),
  (f.cons fs).eval = fun v ↦ do
    let n ← f.eval v
    let ns ← fs.eval v
    pure (n.headI :: ns)

theorem Turing.ToPartrec.Code.comp_eval E0 #

∀ (f g : Turing.ToPartrec.Code), (f.comp g).eval = fun v ↦ g.eval v >>= f.eval

theorem Turing.ToPartrec.Code.case_eval E0 #

∀ (f g : Turing.ToPartrec.Code),
  (f.case g).eval = fun v ↦ Nat.rec (f.eval v.tail) (fun y x ↦ g.eval (y :: v.tail)) v.headI

theorem Turing.ToPartrec.Code.fix_eval E0 #

∀ (f : Turing.ToPartrec.Code),
  f.fix.eval = PFun.fix fun v ↦ Part.map (fun v ↦ if v.headI = 0 then Sum.inl v.tail else Sum.inr v.tail) (f.eval v)

def Turing.ToPartrec.Code.nil E1 #

Turing.ToPartrec.Code

nil is the constant nil function: nil v = [].

theorem Turing.ToPartrec.Code.nil_eval E0 #

∀ (v : List ℕ), Turing.ToPartrec.Code.nil.eval v = pure []

def Turing.ToPartrec.Code.id E1 #

Turing.ToPartrec.Code

id is the identity function: id v = v.

theorem Turing.ToPartrec.Code.id_eval E0 #

∀ (v : List ℕ), Turing.ToPartrec.Code.id.eval v = pure v

def Turing.ToPartrec.Code.head E1 #

Turing.ToPartrec.Code

head gets the head of the input list: head [] = [0], head (n :: v) = [n].

theorem Turing.ToPartrec.Code.head_eval E0 #

∀ (v : List ℕ), Turing.ToPartrec.Code.head.eval v = pure [v.headI]

def Turing.ToPartrec.Code.zero E1 #

Turing.ToPartrec.Code

zero is the constant zero function: zero v = [0].

theorem Turing.ToPartrec.Code.zero_eval E0 #

∀ (v : List ℕ), Turing.ToPartrec.Code.zero.eval v = pure [0]

def Turing.ToPartrec.Code.pred E1 #

Turing.ToPartrec.Code

pred returns the predecessor of the head of the input: pred [] = [0], pred (0 :: v) = [0], pred (n+1 :: v) = [n].

theorem Turing.ToPartrec.Code.pred_eval E0 #

∀ (v : List ℕ), Turing.ToPartrec.Code.pred.eval v = pure [v.headI.pred]

def Turing.ToPartrec.Code.rfind E1 #

Turing.ToPartrec.Code → Turing.ToPartrec.Code

rfind f performs the function of the rfind primitive of partial recursive functions. rfind f v returns the smallest n such that (f (n :: v)).head = 0.

It is implemented as:

rfind f v = pred (fix (fun (n::v) => f (n::v) :: n+1 :: v) (0 :: v))

The idea is that the initial state is 0 :: v, and the fix keeps n :: v as its internal state; it calls f (n :: v) as the exit test and n+1 :: v as the next state. At the end we get n+1 :: v where n is the desired output, and pred (n+1 :: v) = [n] returns the result.

def Turing.ToPartrec.Code.prec E1 #

Turing.ToPartrec.Code → Turing.ToPartrec.Code → Turing.ToPartrec.Code

prec f g implements the prec (primitive recursion) operation of partial recursive functions. prec f g evaluates as:

  • prec f g [] = [f []]
  • prec f g (0 :: v) = [f v]
  • prec f g (n+1 :: v) = [g (n :: prec f g (n :: v) :: v)]

It is implemented as:

G (a :: b :: IH :: v) = (b :: a+1 :: b-1 :: g (a :: IH :: v) :: v)
F (0 :: f_v :: v) = (f_v :: v)
F (n+1 :: f_v :: v) = (fix G (0 :: n :: f_v :: v)).tail.tail
prec f g (a :: v) = [(F (a :: f v :: v)).head]

Because fix always evaluates its body at least once, we must special case the 0 case to avoid calling g more times than necessary (which could be bad if g diverges). If the input is 0 :: v, then F (0 :: f v :: v) = (f v :: v) so we return [f v]. If the input is n+1 :: v, we evaluate the function from the bottom up, with initial state 0 :: n :: f v :: v. The first number counts up, providing arguments for the applications to g, while the second number counts down, providing the exit condition (this is the initial b in the return value of G, which is stripped by fix). After the fix is complete, the final state is n :: 0 :: res :: v where res is the desired result, and the rest reduces this to [res].

theorem Turing.ToPartrec.Code.exists_code.comp E1 #

∀ {m n : ℕ} {f : List.Vector ℕ n →. ℕ} {g : Fin n → List.Vector ℕ m →. ℕ},
  (∃ c, ∀ (v : List.Vector ℕ n), c.eval ↑v = pure <$> f v) →
    (∀ (i : Fin n), ∃ c, ∀ (v : List.Vector ℕ m), c.eval ↑v = pure <$> g i v) →
      ∃ c, ∀ (v : List.Vector ℕ m), c.eval ↑v = pure <$> ((List.Vector.mOfFn fun i ↦ g i v) >>= f)

theorem Turing.ToPartrec.Code.exists_code E1 #

∀ {n : ℕ} {f : List.Vector ℕ n →. ℕ}, Nat.Partrec' f → ∃ c, ∀ (v : List.Vector ℕ n), c.eval ↑v = pure <$> f v

inductive Turing.ToPartrec.Cont E1 #

Type

The type of continuations, built up during evaluation of a Code expression.

ctor Turing.ToPartrec.Cont.halt E0 #

Turing.ToPartrec.Cont

ctor Turing.ToPartrec.Cont.cons₁ E0 #

Turing.ToPartrec.Code → List ℕ → Turing.ToPartrec.Cont → Turing.ToPartrec.Cont

ctor Turing.ToPartrec.Cont.cons₂ E0 #

List ℕ → Turing.ToPartrec.Cont → Turing.ToPartrec.Cont

ctor Turing.ToPartrec.Cont.comp E0 #

Turing.ToPartrec.Code → Turing.ToPartrec.Cont → Turing.ToPartrec.Cont

ctor Turing.ToPartrec.Cont.fix E0 #

Turing.ToPartrec.Code → Turing.ToPartrec.Cont → Turing.ToPartrec.Cont

instance Turing.ToPartrec.instInhabitedCont E0 #

Inhabited Turing.ToPartrec.Cont

def Turing.ToPartrec.Cont.eval E1 #

Turing.ToPartrec.Cont → List ℕ →. List ℕ

The semantics of a continuation.

inductive Turing.ToPartrec.Cfg E1 #

Type

The set of configurations of the machine:

  • halt v: The machine is about to stop and v : List ℕ is the result.
  • ret k v: The machine is about to pass v : List ℕ to continuation k : Cont.

We don't have a state corresponding to normal evaluation because these are evaluated immediately to a ret "in zero steps" using the stepNormal function.

ctor Turing.ToPartrec.Cfg.halt E0 #

List ℕ → Turing.ToPartrec.Cfg

ctor Turing.ToPartrec.Cfg.ret E0 #

Turing.ToPartrec.Cont → List ℕ → Turing.ToPartrec.Cfg

instance Turing.ToPartrec.instInhabitedCfg E0 #

Inhabited Turing.ToPartrec.Cfg

def Turing.ToPartrec.stepNormal E1 #

Turing.ToPartrec.Code → Turing.ToPartrec.Cont → List ℕ → Turing.ToPartrec.Cfg

Evaluating c : Code in a continuation k : Cont and input v : List ℕ. This goes by recursion on c, building an augmented continuation and a value to pass to it.

  • zero' v = 0 :: v evaluates immediately, so we return it to the parent continuation
  • succ v = [v.headI.succ] evaluates immediately, so we return it to the parent continuation
  • tail v = v.tail evaluates immediately, so we return it to the parent continuation
  • cons f fs v = (f v).headI :: fs v requires two sub-evaluations, so we evaluate f v in the continuation k (_.headI :: fs v) (called Cont.cons₁ fs v k)
  • comp f g v = f (g v) requires two sub-evaluations, so we evaluate g v in the continuation k (f _) (called Cont.comp f k)
  • case f g v = v.head.casesOn (f v.tail) (fun n => g (n :: v.tail)) has the information needed to evaluate the case statement, so we do that and transition to either f v or g (n :: v.tail).
  • fix f v = let v' := f v; if v'.headI = 0 then k v'.tail else fix f v'.tail needs to first evaluate f v, so we do that and leave the rest for the continuation (called Cont.fix f k)

def Turing.ToPartrec.stepRet E1 #

Turing.ToPartrec.Cont → List ℕ → Turing.ToPartrec.Cfg

Evaluating a continuation k : Cont on input v : List ℕ. This is the second part of evaluation, when we receive results from continuations built by stepNormal.

  • Cont.halt v = v, so we are done and transition to the Cfg.halt v state
  • Cont.cons₁ fs as k v = k (v.headI :: fs as), so we evaluate fs as now with the continuation k (v.headI :: _) (called cons₂ v k).
  • Cont.cons₂ ns k v = k (ns.headI :: v), where we now have everything we need to evaluate ns.headI :: v, so we return it to k.
  • Cont.comp f k v = k (f v), so we call f v with k as the continuation.
  • Cont.fix f k v = k (if v.headI = 0 then k v.tail else fix f v.tail), where v is a value, so we evaluate the if statement and either call k with v.tail, or call fix f v with k as the continuation (which immediately calls f with Cont.fix f k as the continuation).

def Turing.ToPartrec.step E1 #

Turing.ToPartrec.Cfg → Option Turing.ToPartrec.Cfg

If we are not done (in Cfg.halt state), then we must be still stuck on a continuation, so this main loop calls stepRet with the new continuation. The overall step function transitions from one Cfg to another, only halting at the Cfg.halt state.

def Turing.ToPartrec.Cont.then E1 #

Turing.ToPartrec.Cont → Turing.ToPartrec.Cont → Turing.ToPartrec.Cont

In order to extract a compositional semantics from the sequential execution behavior of configurations, we observe that continuations have a monoid structure, with Cont.halt as the unit and Cont.then as the multiplication. Cont.then k₁ k₂ runs k₁ until it halts, and then takes the result of k₁ and passes it to k₂.

We will not prove it is associative (although it is), but we are instead interested in the associativity law k₂ (eval c k₁) = eval c (k₁.then k₂). This holds at both the sequential and compositional levels, and allows us to express running a machine without the ambient continuation and relate it to the original machine's evaluation steps. In the literature this is usually where one uses Turing machines embedded inside other Turing machines, but this approach allows us to avoid changing the ambient type Cfg in the middle of the recursion.

theorem Turing.ToPartrec.Cont.then_eval E1 #

∀ {k k' : Turing.ToPartrec.Cont} {v : List ℕ}, (k.then k').eval v = k.eval v >>= k'.eval

def Turing.ToPartrec.Cfg.then E1 #

Turing.ToPartrec.Cfg → Turing.ToPartrec.Cont → Turing.ToPartrec.Cfg

The then k function is a "configuration homomorphism". Its operation on states is to append k to the continuation of a Cfg.ret state, and to run k on v if we are in the Cfg.halt v state.

theorem Turing.ToPartrec.stepNormal_then E1 #

∀ (c : Turing.ToPartrec.Code) (k k' : Turing.ToPartrec.Cont) (v : List ℕ),
  Turing.ToPartrec.stepNormal c (k.then k') v = (Turing.ToPartrec.stepNormal c k v).then k'

The stepNormal function respects the then k' homomorphism. Note that this is an exact equality, not a simulation; the original and embedded machines move in lock-step until the embedded machine reaches the halt state.

theorem Turing.ToPartrec.stepRet_then E1 #

∀ {k k' : Turing.ToPartrec.Cont} {v : List ℕ},
  Turing.ToPartrec.stepRet (k.then k') v = (Turing.ToPartrec.stepRet k v).then k'

The stepRet function respects the then k' homomorphism. Note that this is an exact equality, not a simulation; the original and embedded machines move in lock-step until the embedded machine reaches the halt state.

def Turing.ToPartrec.Code.Ok E1 #

Turing.ToPartrec.Code → Prop

This is a temporary definition, because we will prove in code_is_ok that it always holds. It asserts that c is semantically correct; that is, for any k and v, eval (stepNormal c k v) = eval (Cfg.ret k (Code.eval c v)), as an equality of partial values (so one diverges iff the other does).

In particular, we can let k = Cont.halt, and then this asserts that stepNormal c Cont.halt v evaluates to Cfg.halt (Code.eval c v).

theorem Turing.ToPartrec.Code.Ok.zero E1 #

∀ {c : Turing.ToPartrec.Code},
  c.Ok →
    ∀ {v : List ℕ},
      StateTransition.eval Turing.ToPartrec.step (Turing.ToPartrec.stepNormal c Turing.ToPartrec.Cont.halt v) =
        Turing.ToPartrec.Cfg.halt <$> c.eval v

theorem Turing.ToPartrec.stepNormal.is_ret E1 #

∀ (c : Turing.ToPartrec.Code) (k : Turing.ToPartrec.Cont) (v : List ℕ),
  ∃ k' v', Turing.ToPartrec.stepNormal c k v = Turing.ToPartrec.Cfg.ret k' v'

theorem Turing.ToPartrec.cont_eval_fix E1 #

∀ {f : Turing.ToPartrec.Code} {k : Turing.ToPartrec.Cont} {v : List ℕ},
  f.Ok →
    StateTransition.eval Turing.ToPartrec.step (Turing.ToPartrec.stepNormal f (Turing.ToPartrec.Cont.fix f k) v) = do
      let v ← f.fix.eval v
      StateTransition.eval Turing.ToPartrec.step (Turing.ToPartrec.Cfg.ret k v)

theorem Turing.ToPartrec.code_is_ok E1 #

∀ (c : Turing.ToPartrec.Code), c.Ok

theorem Turing.ToPartrec.stepNormal_eval E1 #

∀ (c : Turing.ToPartrec.Code) (v : List ℕ),
  StateTransition.eval Turing.ToPartrec.step (Turing.ToPartrec.stepNormal c Turing.ToPartrec.Cont.halt v) =
    Turing.ToPartrec.Cfg.halt <$> c.eval v

theorem Turing.ToPartrec.stepRet_eval E1 #

∀ {k : Turing.ToPartrec.Cont} {v : List ℕ},
  StateTransition.eval Turing.ToPartrec.step (Turing.ToPartrec.stepRet k v) = Turing.ToPartrec.Cfg.halt <$> k.eval v