Documentation

MIPRE.Foundations.Cost.Universal

The self-interpreter, II: the universal machines #

The two universal machines of the toolkit (Cost/Toolkit.lean), built from the step program stepProg of Cost/Interpreter.lean:

This discharges exists_efficient_universal and exists_clocked_universal (blueprint lem:universal-tm).

The final-configuration test #

On the encoding of a configuration: cons nil r if it is final with value r, nil otherwise.

Equations
  • One or more equations did not get rendered due to their size.
Instances For
    theorem MIPRE.Cost.Machine.isFinalProg_final (r : Data) (e : Env) :
    tr.size + 8, isFinalProg.Runs { ctrl := Ctrl.ret r, env := e, kont := [] }.toData (Data.nil.cons r) t

    The interpreter loop #

    Body of the interpreter loop: on a configuration, stop with its value if it is final, otherwise continue with the next configuration.

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

      Cost of one iteration of the interpreter loop on a configuration of size at most S.

      Equations
      Instances For
        theorem MIPRE.Cost.Machine.interpBody_final (r : Data) (e env : Env) :
        t3 * { ctrl := Ctrl.ret r, env := e, kont := [] }.toData.size + 30, Eval ({ ctrl := Ctrl.ret r, env := e, kont := [] }.toData :: env) interpBody (Data.nil.cons r) t
        theorem MIPRE.Cost.Machine.interpLoop_runs (c₀ : Cfg) (S : ) (hS : ∀ (n : ), (step^[n] c₀).toData.size S) (r : Data) (e : Env) (N : ) (hN : step^[N] c₀ = { ctrl := Ctrl.ret r, env := e, kont := [] }) (env : Env) (m n : ) :
        n + m = Nt ≤ (m + 1) * (interpBodyBound S + 1), Eval ((step^[n] c₀).toData :: env) interpBody.loop r t

        The interpreter loop, from the n-th configuration of a run that reaches a final configuration at step N, all of whose configurations have size at most S.

        theorem MIPRE.Cost.Machine.interpLoop_rev (t : ) (c : Cfg) (env : Env) (r : Data) :
        Eval (c.toData :: env) interpBody.loop r t∃ (N : ) (e : Env), step^[N] c = { ctrl := Ctrl.ret r, env := e, kont := [] }

        The loop, backwards: a run of the interpreter loop from a configuration reaches a final configuration with the same value.

        The universal program #

        On cons c v: the initial configuration of c on v, as data.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          theorem MIPRE.Cost.Machine.univPrelude_runs (c v : Data) :
          tc.size + v.size + 10, Eval [c.cons v] univPrelude (initData c v) t

          The universal program: on cons (encode c) v, build the initial configuration of c on v and run the interpreter loop.

          Equations
          Instances For

            The size bound on the configurations of a halting run, in terms of X = esize c + v.size + t.

            Equations
            Instances For

              The time bound of the universal program, in terms of X = esize c + v.size + t.

              Equations
              Instances For
                theorem MIPRE.Cost.Machine.univProg_time_le (c : Prog) (v r : Data) (t : ) (h : c.Runs v r t) :
                t'univBoundFun (esize c + v.size + t), univProg.Runs ((encode c).cons v) r t'
                theorem MIPRE.Cost.Machine.univProg_halts_of (c : Prog) (v r : Data) (t' : ) (h : univProg.Runs ((encode c).cons v) r t') :
                ∃ (t : ), c.Runs v r t

                Unary arithmetic for the clock #

                Loop body of subProg: on cons a b (unary), stop with the flagged difference cons (cons nil nil) (b - a) if a ≤ b, with nil otherwise.

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

                  Unary subtraction with comparison: subProg on cons (ofNat a) (ofNat b) computes subResult a b.

                  Equations
                  Instances For

                    The value of subProg: the flagged difference, or nil when b < a.

                    Equations
                    Instances For

                      Cost bound of subProg on cons (ofNat a) (ofNat b).

                      Equations
                      Instances For
                        theorem MIPRE.Cost.Machine.subBound_mono {a b a' b' : } (ha : a a') (hb : b b') :
                        theorem MIPRE.Cost.Machine.subProg_runs (a b : ) (env : Env) :
                        tsubBound a b, Eval ((Data.ofNat a).cons (Data.ofNat b) :: env) subProg (subResult a b) t

                        Loop body of tripleProg: on cons (ofNat n) (ofNat m), ofNat (3 n + m).

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

                          tripleProg on cons (ofNat n) (ofNat m) computes ofNat (3 n + m).

                          Equations
                          Instances For
                            theorem MIPRE.Cost.Machine.tripleProg_runs (n m : ) (env : Env) :
                            t ≤ (n + 1) * (8 * n + 2 * m + 30), Eval ((Data.ofNat n).cons (Data.ofNat m) :: env) tripleProg (Data.ofNat (3 * n + m)) t

                            Loop body of repProg: on cons (ofNat n) (cons d acc), n copies of d consed onto acc.

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

                              repProg on cons (ofNat n) (cons d (list acc)) computes list (replicate n d ++ acc).

                              Equations
                              Instances For

                                Cost bound of repProg for n copies of a value of size d onto an accumulator of size a.

                                Equations
                                Instances For
                                  theorem MIPRE.Cost.Machine.repProg_runs (n : ) (d : Data) (acc : List Data) (env : Env) :
                                  trepBound n d.size (Data.list acc).size, Eval ((Data.ofNat n).cons (d.cons (Data.list acc)) :: env) repProg (Data.list (List.replicate n d ++ acc)) t

                                  The cost of a step, as a program #

                                  Cost of a var step: cons body env ↦ ofNat ((getList env body).size + 1).

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

                                    The case programs of the cost of an "evaluate" step, indexed by the tag.

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

                                      The cost of an "evaluate" step; environment [tag, payload, ctrl, rest, d].

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

                                        The cost of a "return" step: 1 at a loop1 frame, 0 otherwise; environment [t₁, t₂, tag, payload, ctrl, rest, d].

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

                                          On the encoding of a configuration c: ofNat (stepCost c).

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

                                            Cost bound of stepCostProg on a configuration of size S.

                                            Equations
                                            Instances For
                                              theorem MIPRE.Cost.Machine.stepCostProg_ev (k : ) (body env kont r : Data) {t : } (hk : k < costCases.length) (h : Eval [body.cons env] (costCases.getD k Prog.nil) r t) :
                                              st + 2 * body.size + 2 * env.size + kont.size + k + 14, Eval [(Data.nil.cons ((Data.ofNat k).cons body)).cons (env.cons kont)] stepCostProg r s

                                              The prefix of stepCostProg on an "evaluate" configuration, up to the dispatch.

                                              stepCostProg computes the cost of a step, at a cost quadratic in the size of the configuration.

                                              The clocked interpreter loop #

                                              def MIPRE.Cost.Machine.clockState (d rem budget theta : Data) :

                                              The state of the clocked loop: the configuration, the remaining cost budget, the remaining step budget and the size guard (the last three in unary).

                                              Equations
                                              Instances For

                                                Second half of a step of the clocked loop, after the cost subtraction succeeded: step, then check the size of the new configuration against the guard; environment [cons nil nil, rem', sub, cr, nil, budget', nil, budget, theta, rem, r₂, d, r₁, s].

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

                                                  The step of the clocked loop on a non-final configuration; environment [flag, budget, theta, rem, r₂, d, r₁, s] with flag = nil: stop with nil if the step budget is exhausted or the cost of the step exceeds the remaining cost budget, else clockRest.

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

                                                    Body of the clocked loop: stop with the flagged value of a final configuration, otherwise clockStep.

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

                                                      Cost bound of sizeProg on a value of size s (as in sizeProg_runs).

                                                      Equations
                                                      Instances For

                                                        Bound on the second half of a step of the clocked loop, in terms of the configuration size S, the guard Θ, the remaining cost rem and the step budget b.

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

                                                          Bound of one iteration of the clocked loop, on configurations of size at most Θ, with cost budget at most k (and step budget at most 3 k + 1).

                                                          Equations
                                                          • One or more equations did not get rendered due to their size.
                                                          Instances For
                                                            theorem MIPRE.Cost.Machine.clockBody_final (r : Data) (e : Env) (rem budget theta : Data) (env : Env) :
                                                            t3 * { ctrl := Ctrl.ret r, env := e, kont := [] }.toData.size + 30, Eval (clockState { ctrl := Ctrl.ret r, env := e, kont := [] }.toData rem budget theta :: env) clockBody (Data.nil.cons ((Data.nil.cons Data.nil).cons r)) t
                                                            theorem MIPRE.Cost.Machine.clockBody_budget0 (c : Cfg) (hc : ¬IsFinal c) (rem theta : Data) (env : Env) :
                                                            tc.toData.size + 20, Eval (clockState c.toData rem (Data.ofNat 0) theta :: env) clockBody (Data.nil.cons Data.nil) t
                                                            def MIPRE.Cost.Machine.clockEnv (c : Cfg) (rem b Θ : ) (env : Env) :

                                                            The environment of clockStep after the cost subtraction, on a configuration c with remaining cost rem, step budget b + 1 and guard Θ.

                                                            Equations
                                                            • One or more equations did not get rendered due to their size.
                                                            Instances For
                                                              theorem MIPRE.Cost.Machine.clockBody_prefix (c : Cfg) (hc : ¬IsFinal c) (rem b Θ : ) (env : Env) (r : Data) {t : } (hrest : Eval (clockEnv c rem b Θ env) (Prog.elim 0 (Prog.nil.cons Prog.nil) clockRest) r t) :
                                                              st + stepCostBound c.toData.size + subBound (stepCost c) rem + 4 * c.toData.size + 4 * rem + 30, Eval (clockState c.toData (Data.ofNat rem) (Data.ofNat (b + 1)) (Data.ofNat Θ) :: env) clockBody r s

                                                              The first half of a step of the clocked loop on a non-final configuration with a positive step budget: the finality test, the cost of the step and its subtraction.

                                                              theorem MIPRE.Cost.Machine.clockRest_stop (c : Cfg) (rem b Θ : ) (env : Env) (hle : stepCost c rem) ( : Θ < (step c).toData.size) :
                                                              theorem MIPRE.Cost.Machine.clockRest_continue (c : Cfg) (rem b Θ : ) (env : Env) (hle : stepCost c rem) ( : (step c).toData.size Θ) :
                                                              theorem MIPRE.Cost.Machine.clockBody_cost_le {S Θ k rem b cost t₂ t₁ : } (hS : S Θ) (hrem : rem k) (hb : b 3 * k) (hcost : cost S) (ht₂ : t₂ clockRestBound S Θ rem b) (ht₁ : t₁ t₂ + stepCostBound S + subBound cost rem + 4 * S + 4 * rem + 30) :

                                                              The cost of one non-final iteration is at most clockBodyBound Θ k.

                                                              The semantics of the clocked loop #

                                                              The value of a configuration: the returned value, nil on an "evaluate" control.

                                                              Equations
                                                              Instances For
                                                                noncomputable def MIPRE.Cost.Machine.clockRun (c₀ : Cfg) (k Θ : ) :
                                                                Data

                                                                The result of the clocked loop from the n-th configuration of the run of c₀, with step budget b, cost budget k and size guard Θ.

                                                                Equations
                                                                • One or more equations did not get rendered due to their size.
                                                                Instances For
                                                                  theorem MIPRE.Cost.Machine.clockLoop_runs (c₀ : Cfg) (k Θ : ) (env : Env) (b n : ) :
                                                                  b 3 * k + 1costSum c₀ n k(step^[n] c₀).toData.size Θt ≤ (b + 1) * (clockBodyBound Θ k + 1), Eval (clockState (step^[n] c₀).toData (Data.ofNat (k - costSum c₀ n)) (Data.ofNat b) (Data.ofNat Θ) :: env) clockBody.loop (clockRun c₀ k Θ b n) t

                                                                  The clocked loop computes clockRun.

                                                                  theorem MIPRE.Cost.Machine.clockRun_of_final (c₀ : Cfg) (k Θ N : ) (r : Data) (e : Env) (hN : step^[N] c₀ = { ctrl := Ctrl.ret r, env := e, kont := [] }) (hcost : costSum c₀ N k) ( : ∀ (n : ), (step^[n] c₀).toData.size Θ) (b n : ) :
                                                                  n NN n + bclockRun c₀ k Θ b n = (Data.nil.cons Data.nil).cons r

                                                                  On a run reaching a final configuration at step N within the cost budget, whose configurations are all within the guard, the clocked loop returns the flagged value.

                                                                  theorem MIPRE.Cost.Machine.clockRun_of_not (c : Prog) (v : Data) (k Θ : ) (hno : ¬∃ (r : Data), tk, c.Runs v r t) (b n : ) :
                                                                  costSum { ctrl := Ctrl.ev c, env := [v], kont := [] } n kclockRun { ctrl := Ctrl.ev c, env := [v], kont := [] } k Θ b n = Data.nil

                                                                  If c does not halt on v within cost k, the clocked loop returns nil.

                                                                  theorem MIPRE.Cost.Machine.clockRun_eq (c : Prog) (v : Data) (k Θ : ) ( : ∀ (r : Data), tk, c.Runs v r t∀ (n : ), (step^[n] { ctrl := Ctrl.ev c, env := [v], kont := [] }).toData.size Θ) :
                                                                  clockRun { ctrl := Ctrl.ev c, env := [v], kont := [] } k Θ (3 * k + 1) 0 = clockedResult c v k

                                                                  The clocked loop from the initial configuration, with step budget 3 k + 1, computes clockedResult c v k, provided the guard Θ bounds the configurations of any run of c on v of cost at most k.

                                                                  The clocked universal program #

                                                                  The size guard of the clocked simulation, as a function of X = k + esize c + v.size: Y = 9 X + 4 (two applications of tripleProg)...

                                                                  Equations
                                                                  Instances For

                                                                    ...the size of Y copies of ofNat Y...

                                                                    Equations
                                                                    Instances For

                                                                      ...and the size of Y copies of that list: the guard Θ.

                                                                      Equations
                                                                      Instances For

                                                                        The guard dominates the size bound of the configurations of a run of cost at most k.

                                                                        The clocked universal program: on cons (ofNat k) (cons (encode c) v), compute X = k + esize c + v.size and the guard Θ in unary, build the initial configuration and the state of the clocked loop, and run it with step budget 3 k + 1.

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

                                                                          Cost bound of addProg (as in addProg_runs).

                                                                          Equations
                                                                          Instances For
                                                                            theorem MIPRE.Cost.Machine.addBound_mono {a b a' b' : } (ha : a a') (hb : b b') :

                                                                            Cost bound of tripleProg (as in tripleProg_runs).

                                                                            Equations
                                                                            Instances For

                                                                              The time bound of the clocked universal program, as a function of X = k + esize c + v.size.

                                                                              Equations
                                                                              • One or more equations did not get rendered due to their size.
                                                                              Instances For
                                                                                theorem MIPRE.Cost.Machine.univTProg_runs (c : Prog) (v : Data) (k : ) :
                                                                                tunivTBoundFun (k + esize c + v.size), univTProg.Runs ((Data.ofNat k).cons ((encode c).cons v)) (clockedResult c v k) t

                                                                                Polynomial packaging #

                                                                                stepBound as a polynomial.

                                                                                Equations
                                                                                Instances For

                                                                                  sigmaFun as a polynomial.

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

                                                                                    univBoundFun as a polynomial.

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

                                                                                      sizeBound of a polynomial.

                                                                                      Equations
                                                                                      Instances For

                                                                                        addBound of polynomials.

                                                                                        Equations
                                                                                        Instances For

                                                                                          repBound of polynomials.

                                                                                          Equations
                                                                                          Instances For

                                                                                            subBound of polynomials.

                                                                                            Equations
                                                                                            Instances For

                                                                                              clockBodyBound of polynomials.

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

                                                                                                univTBoundFun as a polynomial.

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

                                                                                                  The universal machine given by the self-interpreter.

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

                                                                                                    Existence of an efficient universal machine (blueprint lem:universal-tm).

                                                                                                    The clocked universal machine given by the self-interpreter.

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

                                                                                                      Existence of a clocked universal machine (blueprint lem:universal-tm).