Heterogeneous Fold via Reverse + While Loop #
This file decomposes tm0Het_fold_blockRealizable into two
TM0RealizesBlock operations:
- Reverse:
TM0RealizesBlock _ List.reverse(fromtm0_reverse_block) - While loop:
TM0RealizesBlock _ (hetFoldWhile f), where the body pops the leftmostSum.inltag and appliesfto 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 #
tm0RealizesBlock_while: generic TM0 while-loop combinator for blocks.hetFold_decomp: decomposition of the heterogeneous fold into reverse plus while-loop iteration.tm0Het_fold_blockRealizable': block-realizability of the heterogeneous fold.
While-loop TM0 combinator #
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
Conditional Block Realizability #
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 #
Iterate a step function while a condition holds, bounded by fuel.
Returns the block after at most n applications of step.
Equations
- blockIterateWhile step cond 0 x✝ = x✝
- blockIterateWhile step cond n.succ x✝ = if cond x✝ then blockIterateWhile step cond n (step x✝) else x✝
Instances For
While-Loop Helper Lemmas #
While-Loop Block Realizability Combinator #
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 #
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
Equations
- instDecidablePredListHasTagHead tagOf [] = isFalse ⋯
- instDecidablePredListHasTagHead tagOf (c :: tail) = id inferInstance
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
- prefixFoldWhile tagOf F block = blockIterateWhile (prefixFoldStep tagOf F) (hasTagHead tagOf) (List.takeWhile (isTagCell tagOf) block).length block
Instances For
Generic Separated Layout Mapping #
Generic separated block layout: remaining tags, one explicit separator, then accumulator content. The concrete alphabet is supplied by the embeddings.
Equations
Instances For
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
Backwards-compatible name for the separated layout adapter.
Equations
- separatedFoldAdapt isTag sep decodeAcc accEmb f t = separatedAccLift isTag sep decodeAcc accEmb f t
Instances For
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.
Trivial same-alphabet interface for the separated layout adapter.
Heterogeneous Fold Definitions #
Decode fold tags from the concrete heterogeneous alphabet.
Equations
- hetTagDecode (some (Sum.inl val)) = some val
- hetTagDecode x✝ = none
Instances For
Equations
- instDecidablePredListOptionSumHasHetInlHead (some (Sum.inl val) :: tail) = isTrue trivial
- instDecidablePredListOptionSumHasHetInlHead [] = isFalse ⋯
- instDecidablePredListOptionSumHasHetInlHead (none :: tail) = isFalse ⋯
- instDecidablePredListOptionSumHasHetInlHead (some (Sum.inr val) :: tail) = isFalse ⋯
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.
Instances For
Decode accumulator cells from the concrete heterogeneous alphabet.
Equations
- hetAccDecode (some (Sum.inr g)) = some g
- hetAccDecode x✝ = none
Instances For
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
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
- hetFoldWhile F block = blockIterateWhile (hetFoldStep F) hasHetInlHead (List.takeWhile isHetInl block).length block
Instances For
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
- hetFoldAdaptSep sep f t = separatedAccLift isHetInl sep hetAccDecode hetAccEmb f t
Instances For
Concrete default separator instantiation of hetFoldAdaptSep.
Equations
- hetFoldAdapt f t = hetFoldAdaptSep hetSep f t
Instances For
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.
Mathematical Correctness #
hetMix with a non-empty tag list starts with inl.
One step is correct on hetMix when the same-alphabet step has the
intended function-level behavior.
hetFoldWhile is correct on hetMix: computes foldl.
The fold identity: foldl on the reversed list equals foldr.
Decomposition identity: hetFoldWhile ∘ reverse on a pure inl
block equals the foldr result.
Non-Defaultness #
hetFoldStep preserves non-defaultness when the condition holds.
Invariant-Restricted Block Realizability #
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
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
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 #
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 #
Equations
Instances For
Instances For
Equations
- HetFoldStepState.move t = Sum.inr (Sum.inr (Sum.inl t))
Instances For
Instances For
Equations
- HetFoldStepState.instInhabited = { default := HetFoldStepState.start }
Equations
- One or more equations did not get rendered due to their size.
- tm0HetFoldStepMachine Λ M (Sum.inl PUnit.unit) (some (Sum.inl val)) = some (HetFoldStepState.move val, Turing.TM0.Stmt.write default)
- tm0HetFoldStepMachine Λ M (Sum.inl PUnit.unit) a = none
- tm0HetFoldStepMachine Λ M (Sum.inr (Sum.inl PUnit.unit)) a = none
- tm0HetFoldStepMachine Λ M (Sum.inr (Sum.inr (Sum.inl t))) a = some (HetFoldStepState.run t default, Turing.TM0.Stmt.move Turing.Dir.right)
Instances For
Het fold step is conditionally block-realizable (with invariant).
This is the local machine-construction obligation for one fold-body step.
isWellFormedHetBlock is preserved by hetFoldStep when the
condition holds.
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.
Het fold while loop machine exists for well-formed blocks.
Derived from:
- a supplied conditional body machine for
hetFoldStep tm0RealizesBlock_while_inv(while-loop combinator with invariant)hetFoldWhile_eq_iterateWhile_wf(iteration equals definition)