The body of the translated `fix` loop dispatches on the head of the output of as the loop flag
ProvedMIPRE.Cost.Prog.fixBody_runsMathlib's Turing.ToPartrec.Code is a small first-order language on lists of natural numbers, with constructors , , , , , and , in which every partial recursive function is computable. The translation (Prog.ofCode) maps it construct by construct into the language of the ambient cost model: lists of naturals are encoded as -chains of unary numerals (encL; the numeral is the list of copies of ), prepends , and are elims on the input, and are let_ binders with calls of the translated subprograms, is a double elim on the head of the input, and is a loop whose body runs the translated and dispatches on the head of its result (fixBody). Write for a run of the program on the input with result at cost , and for a run in the environment (Eval). For a program , the loop body is
which runs on the loop state and inspects the head of the result. The loop protocol of the model is that a body value stops the loop with result , while continues with the state . The intended body value is (fixFlag)
mirroring Mathlib's semantics of : stop with if returns , stop with if it returns , and iterate from if it returns .
Statement. Let be a closed program and lists of naturals such that for some . Then there is a cost with
This is the single-iteration lemma for the fix case of both correctness directions of the translation: ofCode_sound uses it to build the run of the loop, and ofCode_complete uses it, with determinism, to show that any run of the body returns a fixFlag value. It serves blueprint lem:halting-form through exists_compile.
Formalization Note: No cost is tracked ( is existential): the translation from Mathlib's codes is a computability-only bridge. The encoding uses unary numerals, so that the head test is a single elim; the environment is the singleton , the loop state being variable .
import Mathlib.Algebra.Polynomial.Eval.Defs import Mathlib.Computability.PartrecCode import Mathlib.Computability.Primrec.List import Mathlib.Computability.TuringMachine.Config import Mathlib.Data.List.Basic import Mathlib.Data.Nat.Bits import Mathlib.Data.Nat.Notation import Mathlib.Data.Nat.Size import Mathlib.Logic.Denumerable import Mathlib.Logic.Function.Iterate import Mathlib.Tactic.Linarith import Mathlib.Tactic.Ring import Definitions.Def_MIPRE_Foundations_Cost_Basic import Definitions.Def_MIPRE_Foundations_Cost_Encoding import Definitions.Def_MIPRE_Foundations_Cost_PolyTime import Definitions.Def_MIPRE_Foundations_Cost_Closure import Definitions.Def_MIPRE_Foundations_Cost_Loops import Definitions.Def_MIPRE_Foundations_Cost_Unary import Definitions.Def_MIPRE_Foundations_Cost_Toolkit import Definitions.Def_MIPRE_Foundations_Cost_Machine import Definitions.Def_MIPRE_Foundations_Cost_Codable import Definitions.Def_MIPRE_Foundations_Cost_MachineData import Definitions.Def_MIPRE_Foundations_Cost_Interpreter import Definitions.Def_MIPRE_Foundations_Cost_MachineBound import Definitions.Def_MIPRE_Foundations_Cost_Universal import Definitions.Def_MIPRE_Foundations_Cost_Kleene import Definitions.Def_MIPRE_Foundations_Cost_FromPartrec open MIPRE open MIPRE.Cost open MIPRE.Cost.Prog open Turing.ToPartrec (Code)
theorem MIPRE.Cost.Prog.fixBody_runs {pf : Prog} (hpf : pf.WellScoped 1) (v fv : List ℕ) {t : ℕ}
(h : pf.Runs (encL v) (encL fv) t) :
∃ s, Eval [encL v] (fixBody pf) (fixFlag fv) s := by sorry