The translation of Mathlib's `ToPartrec` codes into the ambient model produces closed programs
ProvedMIPRE.Cost.Prog.ofCode_wellScopedMathlib'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). A program is closed (WellScoped 1) if it reads no variable other than its input, variable , every variable of the program being bound in its binding context.
Statement. For every code ,
that is, the translation of any code is a closed program.
Closedness is the hypothesis under which calls of translated subprograms can be lifted into larger environments (callVar_eval, Eval.append_of_wellScoped) and restricted back (callVar_runs_rev, Eval.of_append_of_wellScoped); it is therefore a premise of both correctness directions ofCode_sound and ofCode_complete, and of the s-m-n transfer in the compiler exists_compile. It serves the halting form of the compression lemmas (blueprint lem:halting-form).
Formalization Note: WellScoped n p is the syntactic predicate "every variable of is below in its binding context", with elim adding two binders and let_ one and loop requiring a nonempty environment; the translation uses the calling convention callVar i q of the closure library, which keeps callees closed.
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.ofCode_wellScoped : ∀ c : Code, (ofCode c).WellScoped 1 := by sorry