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:
univProgruns the evaluation machine (Cost/Machine.lean) from the initial configuration ofconvuntil a final configuration, as aloopwhose body tests finality (isFinalProg) and otherwise steps. A halting run of costttakes at most3 tmachine steps (eval_steps_count), each on a configuration of size polynomial inesize c + v.size + t(eval_steps_bound_forever,size_toData_le), which gives the polynomial overhead (UniversalMachine.time_le); conversely a run of the loop is a machine run reaching a final configuration, hence a derivation (halts_iff).univTProgis the clocked variant: the loop additionally carries the remaining cost budget and a step budget3 k + 1, both in unary, and a size guardΘ(a polynomial ink + esize c + v.sizecomputed in unary). The step budget makes the loop terminate; the cost budget, decremented by the exact cost of each step (stepCostProg, matchingstepCost), decides whether the simulated run halts within costk; the size guard keeps the configurations handled by the loop polynomially bounded even when the simulated run does not halt — it never fires on a run that halts within the budget, whose configurations are bounded byΘ.
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
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
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.
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
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
- MIPRE.Cost.Machine.sigmaFun X = MIPRE.Cost.Machine.cfgSizeBound X (3 * X) X (2 * X)
Instances For
The time bound of the universal program, in terms of X = esize c + v.size + t.
Equations
- MIPRE.Cost.Machine.univBoundFun X = (3 * X + 1) * (MIPRE.Cost.Machine.interpBodyBound (MIPRE.Cost.Machine.sigmaFun X) + 1) + 2 * X + 11
Instances For
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
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).
Instances For
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).
Instances For
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
Cost of a const step: cons d env ↦ ofNat d.size.
Equations
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.
Instances For
stepCostProg computes the cost of a step, at a cost quadratic in the size of the
configuration.
The clocked interpreter loop #
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
- MIPRE.Cost.Machine.clockState d rem budget theta = d.cons (rem.cons (budget.cons theta))
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
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
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.
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
- { ctrl := MIPRE.Cost.Machine.Ctrl.ret v, env := env, kont := kont }.val = v
- { ctrl := MIPRE.Cost.Machine.Ctrl.ev p, env := env, kont := kont }.val = MIPRE.Cost.Data.nil
Instances For
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.
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)...
Instances For
...the size of Y copies of ofNat Y...
Equations
- MIPRE.Cost.Machine.t1Fun X = MIPRE.Cost.Machine.guardFun X * (2 * MIPRE.Cost.Machine.guardFun X + 1 + 1) + 1
Instances For
...and the size of Y copies of that list: the guard Θ.
Equations
Instances For
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 tripleProg (as in tripleProg_runs).
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
Polynomial packaging #
stepBound as a polynomial.
Equations
- MIPRE.Cost.Machine.stepPoly = (Polynomial.X + Polynomial.C 1) * (Polynomial.X + Polynomial.C 40) + Polynomial.C 200
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
- MIPRE.Cost.Machine.sizePoly p = (p + Polynomial.C 1) * (Polynomial.C 5 * p + Polynomial.C 20) + p + Polynomial.C 6
Instances For
addBound of polynomials.
Equations
- MIPRE.Cost.Machine.addPoly p q = (p + Polynomial.C 1) * (Polynomial.C 4 * p + Polynomial.C 2 * q + Polynomial.C 15)
Instances For
tripleBound of polynomials.
Equations
- MIPRE.Cost.Machine.triplePoly p q = (p + Polynomial.C 1) * (Polynomial.C 8 * p + Polynomial.C 2 * q + Polynomial.C 30)
Instances For
repBound of polynomials.
Equations
- MIPRE.Cost.Machine.repPoly p q r = (p + Polynomial.C 1) * (Polynomial.C 4 * p + Polynomial.C 4 * q + Polynomial.C 2 * r + Polynomial.C 30) + p * (p + Polynomial.C 1) * (q + Polynomial.C 1)
Instances For
subBound of polynomials.
Equations
- MIPRE.Cost.Machine.subPoly p q = (p + Polynomial.C 1) * (Polynomial.C 2 * p + Polynomial.C 2 * q + Polynomial.C 14)
Instances For
stepCostBound of a polynomial.
Equations
- MIPRE.Cost.Machine.stepCostPoly p = (p + Polynomial.C 1) * (Polynomial.C 7 * p + Polynomial.C 32) + Polynomial.C 13 * p + Polynomial.C 50
Instances For
clockBodyBound of polynomials.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Instances For
Equations
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).