Documentation

MIPRE.TM.Interp.Instr

The interpreter machine: alphabet, tapes, instructions and programs #

The interpreter U (planning/succinct-cook-levin.md, S2) is a multi-input Turing machine that runs the evaluation machine of the ambient model (MIPRE.Cost.Machine) on a configuration held on its work tapes. Its control is a small language of routine instructions — copy a subtree, copy to a marker, push and pop, locate an environment entry, charge the budget — each executed over a few phases, organized in short straight-line programs, one per case of the evaluation step. This file has the alphabet, the tape names, the instructions and the programs; Machine.lean interprets them.

Tapes #

Seven input tapes: the program 𝒟 as the preorder bits of 𝒟.toData; the bits of encode n, encode x, encode y; the budget T in unary; the answers a', b' as raw bit strings. Six work tapes: C (the control), E (the environment), K (the stack), X (scratch), CNT (a unary depth counter), BUD (the unary budget).

Representation (Repr.lean) #

Values v : Data as v.toBits, over the symbols 0, 1. The control on C: 0 · S(p.toData) for ev p, 1 · S(v) for ret v, from cell 0. The environment [v₁, …, vₙ] (v₁ the innermost) on E as S(vₙ) # … # S(v₁) #, head after the last #. The stack on K, frames bottom to top, each tag · payload · $, closures carrying their environment in the same form after a .

The work alphabet: the two bits, the environment separator #, the frame end $ and the environment mark of a closure frame.

Instances For
    @[instance_reducible]
    Equations
    @[reducible, inline]

    The work tapes.

    Equations
    Instances For
      @[reducible, inline]

      The input tapes.

      Equations
      Instances For
        @[reducible, inline]

        The control tape.

        Equations
        Instances For
          @[reducible, inline]

          The environment tape.

          Equations
          Instances For
            @[reducible, inline]

            The stack tape.

            Equations
            Instances For
              @[reducible, inline]

              The scratch tape.

              Equations
              Instances For
                @[reducible, inline]

                The depth counter.

                Equations
                Instances For
                  @[reducible, inline]

                  The budget.

                  Equations
                  Instances For
                    @[reducible, inline]

                    The program 𝒟.

                    Equations
                    Instances For
                      @[reducible, inline]

                      encode n.

                      Equations
                      Instances For
                        @[reducible, inline]

                        T in unary.

                        Equations
                        Instances For
                          @[reducible, inline]

                          encode x.

                          Equations
                          Instances For
                            @[reducible, inline]

                            encode y.

                            Equations
                            Instances For
                              @[reducible, inline]

                              The answer a'.

                              Equations
                              Instances For
                                @[reducible, inline]

                                The answer b'.

                                Equations
                                Instances For

                                  The programs: one per case of the evaluation step, the dispatcher, the initialization and the final check.

                                  Instances For
                                    @[instance_reducible]
                                    Equations
                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For
                                      @[instance_reducible]
                                      Equations
                                      • One or more equations did not get rendered due to their size.

                                      The routine instructions. Every instruction leaves the heads it does not name where they are.

                                      • copyTree (src : WT) (dst : Option WT) (charge : Bool) : Instr

                                        Copy the subtree (a preorder-serialized Data) under the head of src to dst (skip it if dst = none), advancing both heads past it; with charge, pop one budget cell per bit (halting if the budget is exhausted). Uses CNT, which must be empty with its head at 0, and leaves it so.

                                      • copyUntil (m : Option Sym) (src dst : WT) : Instr

                                        Copy from src to dst up to the symbol m (none the blank), which is not copied; the src head ends on it.

                                      • write (t : WT) (s : Sym) : Instr

                                        Write s and move right.

                                      • move (t : WT) (dir : Bool) : Instr

                                        Move right (dir = true) or left.

                                      • rewind (t : WT) : Instr

                                        One left, then left to the blank, then one right: to cell 0 when cells 0 … pos - 1 are not blank (the head may sit on the blank after the content).

                                      • toEnd (t : WT) : Instr

                                        Move right to the first blank.

                                      • eraseRight (t : WT) : Instr

                                        Erase rightwards to the first blank (the cells erased must not contain $, which marks the start meanwhile), ending where it started.

                                      • leftToMarker (m : Sym) (t : WT) : Instr

                                        Move one left, then left to the marker m or a blank, then one right.

                                      • popBack (m : Sym) (t : WT) : Instr

                                        From the end: erase the last cell and everything before it back to the marker m or a blank (exclusive), ending right after the marker; nothing on an empty tape.

                                      • getEnv : Instr

                                        With the head of C on the unary index S(ofNat i) and the head of E at the end of the environment: write S(env.get i) on X from its head, leave C past the index and E at its end.

                                      • charge (two : Bool) : Instr

                                        Pop one (or two) budget cells; halt if the budget is exhausted.

                                      • branch (t : WT) (s : Option Sym) (target : ProgId) : Instr

                                        Jump to the start of target if the symbol under the head of t is s.

                                      • jump (target : ProgId) : Instr

                                        Jump to the start of target.

                                      • emit (s : Sym) : Instr

                                        Emit an output symbol.

                                      • halt : Instr

                                        Halt.

                                      • copyUnary (j : IT) (dst : WT) : Instr

                                        Write one 1 on dst per symbol of input j, up to the blank.

                                      • copyInputBits (j : IT) (dst : WT) : Instr

                                        Copy the bits of input j to dst, up to the blank.

                                      • buildAnswer (j : IT) (dst : WT) : Instr

                                        Write S(encode l) on dst for the bit string l on input j: 1 0 per 0, 1 1 0 0 per 1, then 0.

                                      • checkLen (j ref : IT) : Instr

                                        Halt unless input j is at most as long as input ref; both heads advance.

                                      • rewindInput (j : IT) : Instr

                                        Move the head of input j to the blank before the input.

                                      • moveInput (j : IT) (dir : Bool) : Instr

                                        Move the head of input j right or left.

                                      Instances For
                                        def MIPRE.TM.Interp.instDecidableEqInstr.decEq (x✝ x✝¹ : Instr) :
                                        Decidable (x✝ = x✝¹)
                                        Equations
                                        • One or more equations did not get rendered due to their size.
                                        Instances For
                                          Equations
                                          • One or more equations did not get rendered due to their size.
                                          Instances For

                                            The phases of an instruction.

                                            Instances For
                                              @[instance_reducible]
                                              Equations
                                              Equations
                                              Instances For

                                                The initialization: the budget, the length checks of the answers, and the initial configuration ⟨ev 𝒟, [encode (n, x, y, a', b')], []⟩.

                                                Equations
                                                • One or more equations did not get rendered due to their size.
                                                Instances For

                                                  The dispatcher of a step: on ev, read the tag; on ret, the top frame's tag, or finish when the stack is empty.

                                                  Equations
                                                  • One or more equations did not get rendered due to their size.
                                                  Instances For
                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For

                                                      The tag of the program to evaluate: (10)^k 0 for k = 0 … 6; every case starts with the head of C on the final 0 of the tag.

                                                      Equations
                                                      • One or more equations did not get rendered due to their size.
                                                      Instances For

                                                        ev (var i): return env.get i, at cost its size plus one.

                                                        Equations
                                                        • One or more equations did not get rendered due to their size.
                                                        Instances For

                                                          ev nil: return nil, at cost one.

                                                          Equations
                                                          • One or more equations did not get rendered due to their size.
                                                          Instances For

                                                            ev (const d): return d, at cost its size.

                                                            Equations
                                                            • One or more equations did not get rendered due to their size.
                                                            Instances For

                                                              ev (cons h t): push the frame cons1 t env, evaluate h, at cost one.

                                                              Equations
                                                              • One or more equations did not get rendered due to their size.
                                                              Instances For

                                                                ev (elim i n c): fetch env.get i and dispatch on it.

                                                                Equations
                                                                • One or more equations did not get rendered due to their size.
                                                                Instances For

                                                                  env.get i = nil: evaluate n, at cost one.

                                                                  Equations
                                                                  • One or more equations did not get rendered due to their size.
                                                                  Instances For

                                                                    env.get i = cons a b: push b then a, evaluate c, at cost one.

                                                                    Equations
                                                                    • One or more equations did not get rendered due to their size.
                                                                    Instances For

                                                                      ev (let_ e b): push the frame let1 b env, evaluate e, at cost one.

                                                                      Equations
                                                                      • One or more equations did not get rendered due to their size.
                                                                      Instances For

                                                                        ev (loop b): push the frame loop1 b env, evaluate b, at no cost.

                                                                        Equations
                                                                        • One or more equations did not get rendered due to their size.
                                                                        Instances For

                                                                          ret v to cons1 t env': environment env', push cons2 v, evaluate t.

                                                                          Equations
                                                                          • One or more equations did not get rendered due to their size.
                                                                          Instances For

                                                                            ret v to cons2 a: return cons a v.

                                                                            Equations
                                                                            • One or more equations did not get rendered due to their size.
                                                                            Instances For

                                                                              ret v to let1 b env': environment v :: env', evaluate b.

                                                                              Equations
                                                                              • One or more equations did not get rendered due to their size.
                                                                              Instances For

                                                                                ret v to loop1 b env': dispatch on the shape of v.

                                                                                Equations
                                                                                • One or more equations did not get rendered due to their size.
                                                                                Instances For

                                                                                  v = nil: return nil in env', at cost one.

                                                                                  Equations
                                                                                  • One or more equations did not get rendered due to their size.
                                                                                  Instances For

                                                                                    v = cons nil r: return r in env', at cost one.

                                                                                    Equations
                                                                                    • One or more equations did not get rendered due to their size.
                                                                                    Instances For

                                                                                      v = cons (cons _ _) v': environment v' :: env'.tail, the frame updated, evaluate b, at cost one.

                                                                                      Equations
                                                                                      • One or more equations did not get rendered due to their size.
                                                                                      Instances For
                                                                                        Equations
                                                                                        • One or more equations did not get rendered due to their size.
                                                                                        Instances For
                                                                                          Equations
                                                                                          • One or more equations did not get rendered due to their size.
                                                                                          Instances For

                                                                                            The programs.

                                                                                            Equations
                                                                                            Instances For

                                                                                              The length bound on the programs.

                                                                                              Equations
                                                                                              Instances For