Completeness of the `ToPartrec` translation: halting runs of return values of on
ProvedMIPRE.Cost.Prog.ofCode_completeMathlib's Turing.ToPartrec.Code is a small first-order language on lists of natural numbers, with the partial semantics : ; , the head of the empty list being ; ; ; ; , , ; and is the least fixed point (PFun.fix) that returns if and recurses on otherwise. Write for " on is defined, with value ". In the ambient cost model, lists of naturals are encoded as -chains of unary numerals, (encL; the numeral is the list of copies of ), and (p.Runs x r t) denotes a run of the program on the input with result at cost . The translation (Prog.ofCode) maps each construct to a program of the model — elims on the input for the primitives and for , let_ binders with calls of the translated subprograms for and , and a loop for .
Statement. For every code , list of naturals , datum and cost ,
In particular the translated program halts on an encoded list only if is defined there, and its output is always an encoded list.
This is the converse of ofCode_sound; together they give ofCode_halts_iff and the halting bridge exists_compile for blueprint lem:halting-form. It is the direction that makes the reduction from Mathlib's halting problem sound: a code that does not halt compiles to a program that does not halt.
Formalization Note: The statement quantifies over an arbitrary result datum and asserts that it is an encoded list; the cost is only used as the measure of the strong induction in the fix case. The translation is computability-only, with no time bounds.
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_complete : ∀ (c : Code) (v : List ℕ) (r : Data) (t : ℕ),
(ofCode c).Runs (encL v) r t → ∃ w, r = encL w ∧ w ∈ c.eval v := by sorry