Langlib

Langlib.Automata.Turing.DSL.HetFoldBlockRealizability

Heterogeneous Fold via Reverse + While Loop #

This file decomposes tm0Het_fold_blockRealizable into two TM0RealizesBlock operations:

  1. Reverse: TM0RealizesBlock _ List.reverse (from tm0_reverse_block)
  2. While loop: TM0RealizesBlock _ (hetFoldWhile f), where the body pops the leftmost Sum.inl tag and applies f to the accumulator.

Architecture #

The while-loop body is expressed as a TM0RealizesBlockCond, which extends TM0RealizesBlock with conditional halting: the body machine halts at a designated continuation state q_cont when the loop should continue (an inl tag was processed), and at a different state when no inl tag remains (loop terminates).

The general combinator tm0RealizesBlock_while builds a TM0RealizesBlock from a TM0RealizesBlockCond body using tm0WhileLoop.

Tape Layout #

Throughout the while loop, the tape holds a single contiguous non-default block of the form:

  [some(inl tⱼ), ..., some(inl t₁), some(inr g₁), ..., some(inr gₖ)]

where tⱼ, ..., t₁ are the remaining unprocessed tags and g₁, ..., gₖ is the current accumulator. All elements are some _, hence non-default (none), so this is a valid block for TM0RealizesBlock (Option (T ⊕ Γ₀)).

Fold Identity #

The fold identity List.foldr f [] w = List.foldl (fun a t => f t a) [] w.reverse connects the right-to-left fold to the left-to-right while loop on the reversed input.

Key results #

While-loop TM0 combinator #

noncomputable def tm0WhileLoop {Γ Λ : Type} [Inhabited Γ] [Inhabited Λ] [DecidableEq Λ] (M : Turing.TM0.Machine Γ Λ) (q_continue : Λ) :

While-loop TM0 machine. Runs M repeatedly; when M halts at q_continue, it restarts M. When M halts at any other state, it halts.

Equations
Instances For
    theorem tm0WhileLoop_step_some {Γ Λ : Type} [Inhabited Γ] [Inhabited Λ] [DecidableEq Λ] (M : Turing.TM0.Machine Γ Λ) (q_continue q : Λ) (a : Γ) (q' : Λ) (s : Turing.TM0.Stmt Γ) (h : M q a = some (q', s)) :
    tm0WhileLoop M q_continue q a = some (q', s)
    theorem tm0WhileLoop_halt {Γ Λ : Type} [Inhabited Γ] [Inhabited Λ] [DecidableEq Λ] (M : Turing.TM0.Machine Γ Λ) (q_continue q : Λ) (a : Γ) (hne : q q_continue) (h : M q a = none) :
    tm0WhileLoop M q_continue q a = none
    theorem tm0WhileLoop_reaches_of_M {Γ Λ : Type} [Inhabited Γ] [Inhabited Λ] [DecidableEq Λ] (M : Turing.TM0.Machine Γ Λ) (q_continue : Λ) (c₁ c₂ : Turing.TM0.Cfg Γ Λ) (h : Turing.Reaches (Turing.TM0.step M) c₁ c₂) :
    Turing.Reaches (Turing.TM0.step (tm0WhileLoop M q_continue)) c₁ c₂

    Conditional Block Realizability #

    def TM0RealizesBlockCond {Γ : Type} [Inhabited Γ] (step : List ΓList Γ) (cond : List ΓProp) [DecidablePred cond] :

    A TM0 block step with conditional continuation.

    When cond block holds: transforms block to step block, halts at q_cont. When ¬cond block: leaves block unchanged, halts at state ≠ q_cont.

    This models a single iteration of a while loop body. The halting state signals whether the loop should continue or terminate.

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

      Fuel-Bounded While-Loop Iteration #

      def blockIterateWhile {Γ : Type} (step : List ΓList Γ) (cond : List ΓProp) [DecidablePred cond] :
      List ΓList Γ

      Iterate a step function while a condition holds, bounded by fuel. Returns the block after at most n applications of step.

      Equations
      Instances For
        theorem blockIterateWhile_succ_true {Γ : Type} (step : List ΓList Γ) (cond : List ΓProp) [DecidablePred cond] (n : ) (block : List Γ) (hcond : cond block) :
        blockIterateWhile step cond (n + 1) block = blockIterateWhile step cond n (step block)
        theorem blockIterateWhile_succ_false {Γ : Type} (step : List ΓList Γ) (cond : List ΓProp) [DecidablePred cond] (n : ) (block : List Γ) (hcond : ¬cond block) :
        blockIterateWhile step cond (n + 1) block = block

        While-Loop Helper Lemmas #

        theorem whileLoop_eval_not_cont {Γ Λ : Type} [Inhabited Γ] [Inhabited Λ] [DecidableEq Λ] (M : Turing.TM0.Machine Γ Λ) (q_cont : Λ) (l : List Γ) (h_body_dom : (TM0Seq.evalCfg M l).Dom) (h_body_q : ((TM0Seq.evalCfg M l).get h_body_dom).q q_cont) :
        (TM0Seq.evalCfg (tm0WhileLoop M q_cont) l).Dom ∀ (h : (TM0Seq.evalCfg (tm0WhileLoop M q_cont) l).Dom), ((TM0Seq.evalCfg (tm0WhileLoop M q_cont) l).get h).Tape = ((TM0Seq.evalCfg M l).get h_body_dom).Tape
        theorem whileLoop_step_restart_eq {Γ Λ : Type} [Inhabited Γ] [Inhabited Λ] [DecidableEq Λ] (M : Turing.TM0.Machine Γ Λ) (q_cont : Λ) (T : Turing.Tape Γ) (h_halt : M q_cont T.head = none) :
        Turing.TM0.step (tm0WhileLoop M q_cont) { q := q_cont, Tape := T } = Turing.TM0.step (tm0WhileLoop M q_cont) { q := default, Tape := T }
        theorem whileLoop_eval_cont {Γ Λ : Type} [Inhabited Γ] [Inhabited Λ] [DecidableEq Λ] (M : Turing.TM0.Machine Γ Λ) (q_cont : Λ) (l l' : List Γ) (h_body_dom : (TM0Seq.evalCfg M l).Dom) (h_body_q : ((TM0Seq.evalCfg M l).get h_body_dom).q = q_cont) (h_body_tape : ((TM0Seq.evalCfg M l).get h_body_dom).Tape = Turing.Tape.mk₁ l') (h_W_dom' : (TM0Seq.evalCfg (tm0WhileLoop M q_cont) l').Dom) :
        (TM0Seq.evalCfg (tm0WhileLoop M q_cont) l).Dom ∀ (h : (TM0Seq.evalCfg (tm0WhileLoop M q_cont) l).Dom), ((TM0Seq.evalCfg (tm0WhileLoop M q_cont) l).get h).Tape = ((TM0Seq.evalCfg (tm0WhileLoop M q_cont) l').get h_W_dom').Tape

        While-Loop Block Realizability Combinator #

        theorem tm0RealizesBlock_while {Γ : Type} [Inhabited Γ] [DecidableEq Γ] [Fintype Γ] (step result : List ΓList Γ) (cond : List ΓProp) [DecidablePred cond] (hbody : TM0RealizesBlockCond step cond) (hstep_nd : ∀ (block : List Γ), (∀ gblock, g default)cond blockgstep block, g default) (hresult_eq : ∀ (block : List Γ), (∀ gblock, g default)∃ (n : ), result block = blockIterateWhile step cond n block ¬cond (blockIterateWhile step cond n block)) (_hresult_nd : ∀ (block : List Γ), (∀ gblock, g default)gresult block, g default) :

        While-loop block realizability combinator.

        Given a conditionally block-realizable body (TM0RealizesBlockCond), a result function that equals the iterated application of step with sufficient fuel, the result function is block-realizable.

        The TM0 machine is built using tm0WhileLoop M_body q_cont, which reruns M_body whenever it halts at q_cont, and halts when M_body halts at any other state.

        Generic Prefix Fold #

        noncomputable def prefixFoldStep {Gamma Tag : Type} (tagOf : GammaOption Tag) (F : TagList GammaList Gamma) :
        List GammaList Gamma

        A generic fold step over one ambient alphabet.

        The fold layer only knows how to decode the first cell as a tag. The body already operates on the same alphabet as the tape block.

        Equations
        Instances For
          def hasTagHead {Gamma Tag : Type} (tagOf : GammaOption Tag) :
          List GammaProp

          Generic head condition for the prefix fold.

          Equations
          Instances For
            def isTagCell {Gamma Tag : Type} (tagOf : GammaOption Tag) (c : Gamma) :

            Boolean predicate used for the fuel bound of the prefix fold.

            Equations
            Instances For
              noncomputable def prefixFoldWhile {Gamma Tag : Type} (tagOf : GammaOption Tag) (F : TagList GammaList Gamma) (block : List Gamma) :
              List Gamma

              Generic while-style fold over a leading tag prefix.

              This is the folding mechanism. It is deliberately independent of any embedding/decoding between alphabets.

              Equations
              Instances For

                Generic Separated Layout Mapping #

                def separatedMix {Gamma Tag Acc : Type} (tagEmb : TagGamma) (sep : Gamma) (accEmb : AccGamma) (tags : List Tag) (acc : List Acc) :
                List Gamma

                Generic separated block layout: remaining tags, one explicit separator, then accumulator content. The concrete alphabet is supplied by the embeddings.

                Equations
                Instances For
                  noncomputable def separatedAccLift {Gamma Tag Acc : Type} (isTag : GammaBool) (sep : Gamma) (decodeAcc : GammaOption Acc) (accEmb : AccGamma) (f : TagList AccList Acc) (t : Tag) :
                  List GammaList Gamma

                  Generic layout/mapping adapter over a separated block.

                  It keeps the tag prefix, consumes exactly the first non-tag cell as separator, decodes the accumulator suffix, applies the accumulator operation, and writes the result back using accEmb.

                  This is not the fold. It is the alphabet/layout bridge that turns an accumulator-alphabet operation into an ambient-alphabet block operation.

                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For
                    noncomputable def separatedFoldAdapt {Gamma Tag Acc : Type} (isTag : GammaBool) (sep : Gamma) (decodeAcc : GammaOption Acc) (accEmb : AccGamma) (f : TagList AccList Acc) (t : Tag) :
                    List GammaList Gamma

                    Backwards-compatible name for the separated layout adapter.

                    Equations
                    Instances For
                      theorem tm0_separatedAccLift_block_of_sameAlphabet {Gamma Tag Acc : Type} [Inhabited Gamma] [DecidableEq Gamma] [Fintype Gamma] [Inhabited Acc] [DecidableEq Acc] [Fintype Acc] (isTag : GammaBool) (sep : Gamma) (decodeAcc : GammaOption Acc) (accEmb : AccGamma) (f : TagList AccList Acc) (t : Tag) (hf : TM0RealizesBlock Gamma (separatedAccLift isTag sep decodeAcc accEmb f t)) :
                      TM0RealizesBlock Gamma (separatedAccLift isTag sep decodeAcc accEmb f t)

                      Trivial same-alphabet interface for the generic separated adapter.

                      The old statement tried to derive this from an accumulator-alphabet block machine. That is not valid without stronger assumptions on the concrete encoding/decoding and on suffix preservation. Callers should provide the same-alphabet block machine for the actual adapter they use.

                      theorem tm0_separatedFoldAdapt_block_of_sameAlphabet {Gamma Tag Acc : Type} [Inhabited Gamma] [DecidableEq Gamma] [Fintype Gamma] [Inhabited Acc] [DecidableEq Acc] [Fintype Acc] (isTag : GammaBool) (sep : Gamma) (decodeAcc : GammaOption Acc) (accEmb : AccGamma) (f : TagList AccList Acc) (t : Tag) (hf : TM0RealizesBlock Gamma (separatedFoldAdapt isTag sep decodeAcc accEmb f t)) :
                      TM0RealizesBlock Gamma (separatedFoldAdapt isTag sep decodeAcc accEmb f t)

                      Trivial same-alphabet interface for the separated layout adapter.

                      Heterogeneous Fold Definitions #

                      def isHetInl {T Γ₀ : Type} (x : Option (T Γ₀)) :

                      Check if a het block element is an inl tag.

                      Equations
                      Instances For
                        def hetTagDecode {T Γ₀ : Type} :
                        Option (T Γ₀)Option T

                        Decode fold tags from the concrete heterogeneous alphabet.

                        Equations
                        Instances For
                          def hasHetInlHead {T Γ₀ : Type} :
                          List (Option (T Γ₀))Prop

                          Condition: block starts with an inl tag (signals the while loop should continue).

                          Equations
                          Instances For
                            def hetSep {T Γ₀ : Type} [Inhabited Γ₀] :
                            Option (T Γ₀)

                            Explicit separator between the remaining inl tags and the inr accumulator. The accumulator invariant excludes default : Γ₀, so some (Sum.inr default) is fresh for accumulator contents while still being a nonblank tape symbol.

                            Equations
                            Instances For
                              def hetTagEmb {T Γ₀ : Type} (t : T) :
                              Option (T Γ₀)

                              Embed fold tags into the concrete heterogeneous alphabet.

                              Equations
                              Instances For
                                def hetAccEmb {T Γ₀ : Type} (g : Γ₀) :
                                Option (T Γ₀)

                                Embed accumulator symbols into the concrete heterogeneous alphabet.

                                Equations
                                Instances For
                                  def hetAccDecode {T Γ₀ : Type} :
                                  Option (T Γ₀)Option Γ₀

                                  Decode accumulator cells from the concrete heterogeneous alphabet.

                                  Equations
                                  Instances For
                                    def hetMixSep {T Γ₀ : Type} (sep : Option (T Γ₀)) (ts : List T) (acc : List Γ₀) :
                                    List (Option (T Γ₀))

                                    Mixed het block representation with an explicit separator: inl tags, then sep, then the inr accumulator.

                                    Equations
                                    Instances For
                                      def hetMix {T Γ₀ : Type} [Inhabited Γ₀] (ts : List T) (acc : List Γ₀) :
                                      List (Option (T Γ₀))

                                      Default concrete het layout separator. Prefer proving generic facts about hetMixSep when the separator matters.

                                      Equations
                                      Instances For
                                        noncomputable def hetFoldStep {T Γ₀ : Type} (F : TList (Option (T Γ₀))List (Option (T Γ₀))) :
                                        List (Option (T Γ₀))List (Option (T Γ₀))

                                        One step of the het fold while loop. Pops the leftmost inl t tag and runs the same-alphabet step F t on the remaining block.

                                        The fact that F t preserves the remaining tags and updates only the accumulator is a function-level obligation, not part of this generic fold construction.

                                        Equations
                                        Instances For
                                          noncomputable def hetFoldWhile {T Γ₀ : Type} (F : TList (Option (T Γ₀))List (Option (T Γ₀))) (block : List (Option (T Γ₀))) :
                                          List (Option (T Γ₀))

                                          The full while-loop result function, defined as iterated application of hetFoldStep with fuel equal to the number of leading inl tags.

                                          This directly models what the tm0WhileLoop machine computes: iterate hetFoldStep while hasHetInlHead, stopping when no more inl tags remain at the head.

                                          Equations
                                          Instances For
                                            noncomputable def hetFoldAdaptSep {T Γ₀ : Type} (sep : Option (T Γ₀)) (f : TList Γ₀List Γ₀) (t : T) :
                                            List (Option (T Γ₀))List (Option (T Γ₀))

                                            Function-level adapter from an accumulator operation to a homogeneous het-tape operation.

                                            This is intentionally not part of the generic fold theorem. Any machine realizability proof for this adapter must account for the alphabet/layout boundary explicitly.

                                            Equations
                                            Instances For
                                              noncomputable def hetFoldAdapt {T Γ₀ : Type} [Inhabited Γ₀] (f : TList Γ₀List Γ₀) (t : T) :
                                              List (Option (T Γ₀))List (Option (T Γ₀))

                                              Concrete default separator instantiation of hetFoldAdaptSep.

                                              Equations
                                              Instances For
                                                theorem tm0_hetFoldAdaptSep_block_of_sameAlphabet {T Γ₀ : Type} (sep : Option (T Γ₀)) (f : TList Γ₀List Γ₀) (t : T) (hf : TM0RealizesBlock (Option (T Γ₀)) (hetFoldAdaptSep sep f t)) :

                                                Same-alphabet machine obligation for hetFoldAdaptSep.

                                                This theorem deliberately no longer claims that an accumulator-alphabet machine is enough. Preserving the leading inl tail is a concrete same-alphabet obligation.

                                                theorem tm0_hetFoldAdapt_block_of_sameAlphabet {T Γ₀ : Type} [Inhabited Γ₀] (f : TList Γ₀List Γ₀) (t : T) (hf : TM0RealizesBlock (Option (T Γ₀)) (hetFoldAdapt f t)) :

                                                Mathematical Correctness #

                                                theorem hasHetInlHead_hetMix_cons {T Γ₀ : Type} [Inhabited Γ₀] (t : T) (ts : List T) (acc : List Γ₀) :
                                                hasHetInlHead (hetMix (t :: ts) acc)

                                                hetMix with a non-empty tag list starts with inl.

                                                theorem not_hasHetInlHead_hetMix_nil {T Γ₀ : Type} [Inhabited Γ₀] (acc : List Γ₀) :

                                                hetMix with empty tag list does NOT start with inl.

                                                theorem hetFoldStep_hetMix {T Γ₀ : Type} [Inhabited Γ₀] (F : TList (Option (T Γ₀))List (Option (T Γ₀))) (f : TList Γ₀List Γ₀) (hF : ∀ (t : T) (ts : List T) (acc : List Γ₀), F t (hetMix ts acc) = hetMix ts (f t acc)) (t : T) (ts : List T) (acc : List Γ₀) :
                                                hetFoldStep F (hetMix (t :: ts) acc) = hetMix ts (f t acc)

                                                One step is correct on hetMix when the same-alphabet step has the intended function-level behavior.

                                                theorem hetFoldAdapt_hetMix {T Γ₀ : Type} [Inhabited Γ₀] (f : TList Γ₀List Γ₀) (t : T) (ts : List T) (acc : List Γ₀) :
                                                hetFoldAdapt f t (hetMix ts acc) = hetMix ts (f t acc)
                                                theorem takeWhile_isHetInl_hetMix {T Γ₀ : Type} [Inhabited Γ₀] (ts : List T) (acc : List Γ₀) :
                                                theorem hetFoldWhile_hetMix {T Γ₀ : Type} [Inhabited Γ₀] (F : TList (Option (T Γ₀))List (Option (T Γ₀))) (f : TList Γ₀List Γ₀) (hF : ∀ (t : T) (ts : List T) (acc : List Γ₀), F t (hetMix ts acc) = hetMix ts (f t acc)) (ts : List T) (acc : List Γ₀) :
                                                hetFoldWhile F (hetMix ts acc) = hetMix [] (List.foldl (fun (a : List Γ₀) (t : T) => f t a) acc ts)

                                                hetFoldWhile is correct on hetMix: computes foldl.

                                                theorem foldl_flip_reverse_eq_foldr {α β : Type} (f : αββ) (z : β) (w : List α) :
                                                List.foldl (fun (a : β) (t : α) => f t a) z w.reverse = List.foldr f z w

                                                The fold identity: foldl on the reversed list equals foldr.

                                                theorem hetFold_decomp {T Γ₀ : Type} [Inhabited Γ₀] (F : TList (Option (T Γ₀))List (Option (T Γ₀))) (f : TList Γ₀List Γ₀) (hF : ∀ (t : T) (ts : List T) (acc : List Γ₀), F t (hetMix ts acc) = hetMix ts (f t acc)) (w : List T) :

                                                Decomposition identity: hetFoldWhile ∘ reverse on a pure inl block equals the foldr result.

                                                Non-Defaultness #

                                                theorem hetMix_ne_default {T Γ₀ : Type} [Inhabited Γ₀] (ts : List T) (acc : List Γ₀) (_hacc : gacc, g default) (g : Option (T Γ₀)) :
                                                g hetMix ts accg default

                                                All elements of hetMix are non-default (some _).

                                                theorem map_inl_ne_default {T Γ₀ : Type} (w : List T) (g : Option (T Γ₀)) :

                                                Elements of w.map (some ∘ Sum.inl) are non-default.

                                                theorem hetFoldStep_ne_default {T Γ₀ : Type} (F : TList (Option (T Γ₀))List (Option (T Γ₀))) (hF_nd : ∀ (t : T) (block : List (Option (T Γ₀))), (∀ gblock, g default)gF t block, g default) (block : List (Option (T Γ₀))) (hblock : gblock, g default) (hcond : hasHetInlHead block) (g : Option (T Γ₀)) :
                                                g hetFoldStep F blockg default

                                                hetFoldStep preserves non-defaultness when the condition holds.

                                                theorem hetFoldWhile_ne_default {T Γ₀ : Type} (F : TList (Option (T Γ₀))List (Option (T Γ₀))) (hF_nd : ∀ (t : T) (block : List (Option (T Γ₀))), (∀ gblock, g default)gF t block, g default) (block : List (Option (T Γ₀))) (hblock : gblock, g default) (g : Option (T Γ₀)) :
                                                g hetFoldWhile F blockg default

                                                Invariant-Restricted Block Realizability #

                                                def TM0RealizesBlockCondInv {Γ : Type} [Inhabited Γ] (step : List ΓList Γ) (cond : List ΓProp) [DecidablePred cond] (blockInv : List ΓProp) :

                                                A conditional block step with an invariant restricting which blocks the machine must handle.

                                                This weakens TM0RealizesBlockCond by only requiring the machine to work on blocks satisfying blockInv, with empty suffix. This is necessary for the het fold step because each conditional step is only specified on well-formed blocks, where inl heads and inr payloads are properly separated. Keeping the suffix empty prevents the block machine contract from needing to preserve unrelated heterogeneous tape cells beyond the active block.

                                                Equations
                                                • One or more equations did not get rendered due to their size.
                                                Instances For
                                                  def isWellFormedHetBlock {T Γ₀ : Type} [Inhabited Γ₀] (block : List (Option (T Γ₀))) :

                                                  Well-formedness predicate for het blocks: the block is of the form hetMix ts acc for some tag list ts and accumulator acc with all accumulator elements non-default.

                                                  Equations
                                                  Instances For
                                                    def TM0RealizesHetFoldBody {T Γ₀ : Type} [Inhabited Γ₀] (F : TList (Option (T Γ₀))List (Option (T Γ₀))) :

                                                    A fold body machine only needs to run on well-formed het blocks.

                                                    This is intentionally weaker than TM0RealizesBlock: the fold step erases one leading tag and then invokes the tag-specific body on the remaining hetMix ts acc. It never invokes that body on arbitrary malformed blocks.

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

                                                      While-Loop with Invariant #

                                                      theorem tm0RealizesBlock_while_inv {Γ : Type} [Inhabited Γ] [DecidableEq Γ] [Fintype Γ] (step result : List ΓList Γ) (cond : List ΓProp) [DecidablePred cond] (blockInv : List ΓProp) (hbody : TM0RealizesBlockCondInv step cond blockInv) (hinv_step : ∀ (block : List Γ), blockInv block(∀ gblock, g default)cond blockblockInv (step block)) (hstep_nd : ∀ (block : List Γ), (∀ gblock, g default)cond blockgstep block, g default) (hresult_eq : ∀ (block : List Γ), (∀ gblock, g default)blockInv block∃ (n : ), result block = blockIterateWhile step cond n block ¬cond (blockIterateWhile step cond n block)) (_hresult_nd : ∀ (block : List Γ), (∀ gblock, g default)blockInv blockgresult block, g default) :
                                                      ∃ (Λ : Type) (x : Inhabited Λ) (x_1 : Fintype Λ) (M : Turing.TM0.Machine Γ Λ), ∀ (block : List Γ), blockInv block(∀ gblock, g default)(∀ gresult block, g default)(TM0Seq.evalCfg M (block ++ [default])).Dom ∀ (h : (TM0Seq.evalCfg M (block ++ [default])).Dom), ((TM0Seq.evalCfg M (block ++ [default])).get h).Tape = Turing.Tape.mk₁ (result block ++ [default])

                                                      While-loop combinator with block invariant (empty suffix).

                                                      Like tm0RealizesBlock_while but:

                                                      • The body only needs to work on blocks satisfying blockInv
                                                      • Only empty suffix is required
                                                      • The invariant must be preserved by step

                                                      Block Realizability #

                                                      @[reducible, inline]
                                                      abbrev HetFoldStepState (T : Type) (Λ : TType) :
                                                      Equations
                                                      Instances For
                                                        def HetFoldStepState.start {T : Type} {Λ : TType} :
                                                        Equations
                                                        Instances For
                                                          def HetFoldStepState.cont {T : Type} {Λ : TType} :
                                                          Equations
                                                          Instances For
                                                            def HetFoldStepState.move {T : Type} {Λ : TType} (t : T) :
                                                            Equations
                                                            Instances For
                                                              def HetFoldStepState.run {T : Type} {Λ : TType} (t : T) (q : Λ t) :
                                                              Equations
                                                              Instances For
                                                                noncomputable def tm0HetFoldStepMachine {T Γ₀ : Type} [DecidableEq T] (Λ : TType) [(t : T) → Inhabited (Λ t)] (M : (t : T) → Turing.TM0.Machine (Option (T Γ₀)) (Λ t)) :
                                                                Equations
                                                                Instances For
                                                                  theorem tm0HetFoldStepMachine_run_step {T Γ₀ : Type} [DecidableEq T] (Λ : TType) [(t : T) → Inhabited (Λ t)] (M : (t : T) → Turing.TM0.Machine (Option (T Γ₀)) (Λ t)) (t : T) {c c' : Turing.TM0.Cfg (Option (T Γ₀)) (Λ t)} (h : c' Turing.TM0.step (M t) c) :
                                                                  theorem tm0HetFoldStepMachine_run_reaches {T Γ₀ : Type} [DecidableEq T] (Λ : TType) [(t : T) → Inhabited (Λ t)] (M : (t : T) → Turing.TM0.Machine (Option (T Γ₀)) (Λ t)) (t : T) {l : List (Option (T Γ₀))} {c : Turing.TM0.Cfg (Option (T Γ₀)) (Λ t)} (h : Turing.Reaches (Turing.TM0.step (M t)) (Turing.TM0.init l) c) :

                                                                  Het fold step is conditionally block-realizable (with invariant).

                                                                  This is the local machine-construction obligation for one fold-body step.

                                                                  theorem isWellFormedHetBlock_step {T Γ₀ : Type} [Inhabited Γ₀] (F : TList (Option (T Γ₀))List (Option (T Γ₀))) (f : TList Γ₀List Γ₀) (hF : ∀ (t : T) (ts : List T) (acc : List Γ₀), F t (hetMix ts acc) = hetMix ts (f t acc)) (hf_nd : ∀ (t : T) (block : List Γ₀), (∀ gblock, g default)gf t block, g default) (block : List (Option (T Γ₀))) (hInv : isWellFormedHetBlock block) (hblock : gblock, g default) (hcond : hasHetInlHead block) :

                                                                  isWellFormedHetBlock is preserved by hetFoldStep when the condition holds.

                                                                  theorem hetFoldWhile_eq_iterateWhile_wf {T Γ₀ : Type} [Inhabited Γ₀] (F : TList (Option (T Γ₀))List (Option (T Γ₀))) (f : TList Γ₀List Γ₀) (hF : ∀ (t : T) (ts : List T) (acc : List Γ₀), F t (hetMix ts acc) = hetMix ts (f t acc)) (block : List (Option (T Γ₀))) (hInv : isWellFormedHetBlock block) :

                                                                  hetFoldWhile equals an iteration that has actually stopped on well-formed blocks. The semantic hF is the important assumption: each successful step removes one leading tag from the hetMix block.

                                                                  theorem tm0RealizesBlock_hetFoldWhile_inv {T : Type} [DecidableEq T] [Fintype T] {Γ₀ : Type} [Inhabited Γ₀] [DecidableEq Γ₀] [Fintype Γ₀] (F : TList (Option (T Γ₀))List (Option (T Γ₀))) (f : TList Γ₀List Γ₀) (hF : ∀ (t : T) (ts : List T) (acc : List Γ₀), F t (hetMix ts acc) = hetMix ts (f t acc)) (hf_body : TM0RealizesHetFoldBody F) (hF_nd : ∀ (t : T) (block : List (Option (T Γ₀))), (∀ gblock, g default)gF t block, g default) (hf_nd : ∀ (t : T) (block : List Γ₀), (∀ gblock, g default)gf t block, g default) :
                                                                  ∃ (Λ : Type) (x : Inhabited Λ) (x_1 : Fintype Λ) (M : Turing.TM0.Machine (Option (T Γ₀)) Λ), ∀ (block : List (Option (T Γ₀))), isWellFormedHetBlock block(∀ gblock, g default)(∀ ghetFoldWhile F block, g default)(TM0Seq.evalCfg M (block ++ [default])).Dom ∀ (h : (TM0Seq.evalCfg M (block ++ [default])).Dom), ((TM0Seq.evalCfg M (block ++ [default])).get h).Tape = Turing.Tape.mk₁ (hetFoldWhile F block ++ [default])

                                                                  Het fold while loop machine exists for well-formed blocks.

                                                                  Derived from:

                                                                  w.map (some ∘ Sum.inl) ++ [hetSep] forms a well-formed het block.

                                                                  Reversed well-formed het block is still well-formed.

                                                                  Deriving the Original Fold Spec #

                                                                  theorem tm0Het_fold_blockRealizable' (T : Type) [DecidableEq T] [Fintype T] {Γ₀ : Type} [Inhabited Γ₀] [DecidableEq Γ₀] [Fintype Γ₀] (F : TList (Option (T Γ₀))List (Option (T Γ₀))) (f : TList Γ₀List Γ₀) (hF : ∀ (t : T) (ts : List T) (acc : List Γ₀), F t (hetMix ts acc) = hetMix ts (f t acc)) (hf_body : TM0RealizesHetFoldBody F) (hF_nd : ∀ (t : T) (block : List (Option (T Γ₀))), (∀ gblock, g default)gF t block, g default) (hf_nd : ∀ (t : T) (block : List Γ₀), (∀ gblock, g default)gf t block, g default) :
                                                                  ∃ (Λ : Type) (x : Inhabited Λ) (x_1 : Fintype Λ) (M : Turing.TM0.Machine (Option (T Γ₀)) Λ), ∀ (w : List T), (TM0Seq.evalCfg M (List.map (some Sum.inl) w)).Dom ∀ (h : (TM0Seq.evalCfg M (List.map (some Sum.inl) w)).Dom), ((TM0Seq.evalCfg M (List.map (some Sum.inl) w)).get h).Tape = Turing.Tape.mk₁ (List.map (some Sum.inr) (List.foldr f [] w))