Documentation

CslibFrontier.Computability.Machines.Turing.MultiTape.Internal.NativeCompiler.List

Named native binary-indexed list access #

This module retains a single-pass scan of a standard framed list with a binary countdown. The scan keeps the remaining binary index and an optional selected value, processes every encoded occurrence, and returns an explicit default when the index is out of range.

Main definitions #

Main results #

Retained standard-list tail, dropping exactly the first framed occurrence when present.

def Turing.MultiTapeTM.Internal.NativeCompiler.listConsCompiler {Value : Type u} (encValue : Value ↪ List Bool) :
NativeCompiler (fun (input : Value × List Value) => input.1 :: input.2) (encProd encValue (encList encValue)) (encList encValue)

Retained standard-list constructor, framing the new head and appending the encoded tail.

def Turing.MultiTapeTM.Internal.NativeCompiler.listAppendCompiler {Value : Type u} (encValue : Value ↪ List Bool) :
NativeCompiler (fun (input : List Value × List Value) => input.1 ++ input.2) (encProd (encList encValue) (encList encValue)) (encList encValue)

Retained standard-list append. Both encoded lists are emitted once, without another framing layer around either list.

Retained conversion of an option to its zero-or-one-element standard list.

@[reducible, inline]

State for binary-indexed access: the remaining index and an optional prior selection.

Advance the binary countdown, retaining the first value selected at remaining index zero.

def Turing.MultiTapeTM.Internal.NativeCompiler.listGetNatFinish {Value : Type u} (default : Value) (state : ListGetNatState Value) :
Value

Return a selected value, or the explicit default if the index was out of range.

theorem Turing.MultiTapeTM.Internal.NativeCompiler.listGetNatFinish_streamState {Value : Type u} (default : Value) (list : List Value) (index : ℕ) :
listGetNatFinish default (streamState (index, none) listGetNatNext list list.length) = list.getD index default

The completed countdown scan implements total list access.

theorem Turing.MultiTapeTM.Internal.NativeCompiler.listGetNatOption_streamState {Value : Type u} (list : List Value) (index : ℕ) :
(streamState (index, none) listGetNatNext list list.length).2 = list[index]?

The completed countdown scan also implements optional list access without a sentinel.

Retained per-occurrence state callback for binary-indexed list access.

A list scan emits no per-occurrence output; selection remains in the stream state.

def Turing.MultiTapeTM.Internal.NativeCompiler.listGetNatFinishCompiler {Value : Type u} (default : Value) (encValue : Value ↪ List Bool) :
NativeCompiler (listGetNatFinish default) (encProd encNat (encOption encValue)) encValue

Retained final selection/default callback.

Retained final optional selection callback, with none marking an out-of-range index.

def Turing.MultiTapeTM.Internal.NativeCompiler.listGetNatSMax {Value : Type u} (default : Value) (encValue : Value ↪ List Bool) (input : ListGetNatState Value × List Value) :

Explicit peak lifted-callback workspace over the actual list scan.

Explicit peak lifted-callback workspace for optional binary-indexed access.

def Turing.MultiTapeTM.Internal.NativeCompiler.listGetNatLMax {Value : Type u} (encValue : Value ↪ List Bool) (input : ListGetNatState Value × List Value) :

Explicit peak encoded state/element length over the actual list scan.

def Turing.MultiTapeTM.Internal.NativeCompiler.listGetNatStreamCompiler {Value : Type u} (default : Value) (encValue : Value ↪ List Bool) :
NativeCompiler (fun (input : ListGetNatState Value × List Value) => listGetNatFinish default (streamState input.1 listGetNatNext input.2 input.2.length)) (encProd (encProd encNat (encOption encValue)) (encList encValue)) encValue

Retained binary-countdown scan before list/index input marshalling.

def Turing.MultiTapeTM.Internal.NativeCompiler.listGetNatOptionStreamCompiler {Value : Type u} (encValue : Value ↪ List Bool) :
NativeCompiler (fun (input : ListGetNatState Value × List Value) => (streamState input.1 listGetNatNext input.2 input.2.length).2) (encProd (encProd encNat (encOption encValue)) (encList encValue)) (encOption encValue)

Retained binary-countdown scan returning none exactly for an out-of-range index.

def Turing.MultiTapeTM.Internal.NativeCompiler.listGetNatMarshalCompiler {Value : Type u} (encValue : Value ↪ List Bool) :
NativeCompiler (fun (input : List Value × ℕ) => ((input.2, none), input.1)) (encProd (encList encValue) encNat) (encProd (encProd encNat (encOption encValue)) (encList encValue))

Retained marshalling from (list, binary index) to countdown stream state.

def Turing.MultiTapeTM.Internal.NativeCompiler.listGet?NatCompiler {Value : Type u} (encValue : Value ↪ List Bool) :
NativeCompiler (fun (input : List Value × ℕ) => input.1[input.2]?) (encProd (encList encValue) encNat) (encOption encValue)

Optional binary-indexed standard-list access, including marshalling and every scanned entry.

def Turing.MultiTapeTM.Internal.NativeCompiler.listGetDNatCompiler {Value : Type u} (default : Value) (encValue : Value ↪ List Bool) :
NativeCompiler (fun (input : List Value × ℕ) => input.1.getD input.2 default) (encProd (encList encValue) encNat) encValue

Total binary-indexed standard-list access, including marshalling and every scanned entry.

theorem Turing.MultiTapeTM.Internal.NativeCompiler.time_listGetDNatCompiler {Value : Type u} (default : Value) (encValue : Value ↪ List Bool) (input : List Value × ℕ) :
(listGetDNatCompiler default encValue).time input = (listGetNatMarshalCompiler encValue).time input + (listGetNatStreamCompiler default encValue).time ((input.2, none), input.1) + 4 * ((encProd (encProd encNat (encOption encValue)) (encList encValue)) ((input.2, none), input.1)).length + 9

The complete lookup clock separates marshalling, scan, intermediate-copy and composition costs.

theorem Turing.MultiTapeTM.Internal.NativeCompiler.listBits_le_time_listGetNatStreamCompiler {Value : Type u} (default : Value) (encValue : Value ↪ List Bool) (input : ListGetNatState Value × List Value) :
((encList encValue) input.2).length ≤ (listGetNatStreamCompiler default encValue).time input

Every encoded list occurrence is charged by the binary-indexed scan.

theorem Turing.MultiTapeTM.Internal.NativeCompiler.listBits_le_time_listGetDNatCompiler {Value : Type u} (default : Value) (encValue : Value ↪ List Bool) (input : List Value × ℕ) :
((encList encValue) input.1).length ≤ (listGetDNatCompiler default encValue).time input

Every encoded list occurrence is also charged by the complete marshalled lookup.

Standard-list map #

Map keeps no semantic state between framed list elements.

def Turing.MultiTapeTM.Internal.NativeCompiler.listMapEmitCompiler {Input : Type u} {Output : Type v} {mapValue : Input → Output} (encInput : Input ↪ List Bool) (encOutput : Output ↪ List Bool) (compiler : NativeCompiler mapValue encInput encOutput) :
NativeCompiler (fun (input : Unit × Input) => mapValue input.2) (encProd encUnit encInput) (encDelimited encOutput)

Apply and frame the named element compiler for one list-map occurrence.

def Turing.MultiTapeTM.Internal.NativeCompiler.listMapSMax {Input : Type u} {Output : Type v} {mapValue : Input → Output} (encInput : Input ↪ List Bool) (encOutput : Output ↪ List Bool) (compiler : NativeCompiler mapValue encInput encOutput) (input : Unit × List Input) :

Explicit peak lifted-callback workspace over the actual list-map rounds.

def Turing.MultiTapeTM.Internal.NativeCompiler.listMapLMax {Input : Type u} (encInput : Input ↪ List Bool) (input : Unit × List Input) :

Explicit peak encoded state/element length over the actual list-map rounds.

def Turing.MultiTapeTM.Internal.NativeCompiler.listMapStreamCompiler {Input : Type u} {Output : Type v} {mapValue : Input → Output} (encInput : Input ↪ List Bool) (encOutput : Output ↪ List Bool) (compiler : NativeCompiler mapValue encInput encOutput) :
NativeCompiler (fun (input : Unit × List Input) => List.map mapValue input.2) (encProd encUnit (encList encInput)) (encList encOutput)

Retained list-map scan before input marshalling.

def Turing.MultiTapeTM.Internal.NativeCompiler.listMapMarshalCompiler {Input : Type u} (encInput : Input ↪ List Bool) :
NativeCompiler (fun (input : List Input) => ((), input)) (encList encInput) (encProd encUnit (encList encInput))

Retained marshalling from a standard list to the unit-state stream layout.

def Turing.MultiTapeTM.Internal.NativeCompiler.listMapCompiler {Input : Type u} {Output : Type v} {mapValue : Input → Output} (encInput : Input ↪ List Bool) (encOutput : Output ↪ List Bool) (compiler : NativeCompiler mapValue encInput encOutput) :
NativeCompiler (List.map mapValue) (encList encInput) (encList encOutput)

Retained standard-list map, including marshalling and every element callback.

theorem Turing.MultiTapeTM.Internal.NativeCompiler.listBits_le_time_listMapStreamCompiler {Input : Type u} {Output : Type v} {mapValue : Input → Output} (encInput : Input ↪ List Bool) (encOutput : Output ↪ List Bool) (compiler : NativeCompiler mapValue encInput encOutput) (input : Unit × List Input) :
((encList encInput) input.2).length ≤ (listMapStreamCompiler encInput encOutput compiler).time input

Every encoded input occurrence is charged by the retained list map.

Left-length zip #

def Turing.MultiTapeTM.Internal.NativeCompiler.listZipWithLeft {Left : Type u} {Right : Type v} {Zipped : Type w} (default : Right) (f : Left × Right → Zipped) :
List Left → List Right → List Zipped

Zip to the left list's length, using default after the right list is exhausted.

def Turing.MultiTapeTM.Internal.NativeCompiler.listZipWithLeftNextCompiler {Left : Type u} {Right : Type v} (encLeft : Left ↪ List Bool) (encRight : Right ↪ List Bool) :
NativeCompiler (fun (input : List Right × Left) => input.1.tail) (encProd (encList encRight) encLeft) (encList encRight)

The right-list stream state advances by one framed occurrence per left element.

def Turing.MultiTapeTM.Internal.NativeCompiler.listZipWithLeftEmitCompiler {Left : Type u} {Right : Type v} {Zipped : Type w} {zipValue : Left × Right → Zipped} (default : Right) (encLeft : Left ↪ List Bool) (encRight : Right ↪ List Bool) (encZipped : Zipped ↪ List Bool) (compiler : NativeCompiler zipValue (encProd encLeft encRight) encZipped) :
NativeCompiler (fun (input : List Right × Left) => zipValue (input.2, input.1.headD default)) (encProd (encList encRight) encLeft) (encDelimited encZipped)

Apply and frame the named zip callback for one left-list occurrence.

Left-length zip finalization emits no additional bits.

def Turing.MultiTapeTM.Internal.NativeCompiler.listZipWithLeftSMax {Left : Type u} {Right : Type v} {Zipped : Type w} {zipValue : Left × Right → Zipped} (default : Right) (encLeft : Left ↪ List Bool) (encRight : Right ↪ List Bool) (encZipped : Zipped ↪ List Bool) (compiler : NativeCompiler zipValue (encProd encLeft encRight) encZipped) (input : List Right × List Left) :

Explicit peak callback workspace over the actual left-length zip rounds.

def Turing.MultiTapeTM.Internal.NativeCompiler.listZipWithLeftLMax {Left : Type u} {Right : Type v} (encLeft : Left ↪ List Bool) (encRight : Right ↪ List Bool) (input : List Right × List Left) :

Explicit peak state/element length over the actual left-length zip rounds.

def Turing.MultiTapeTM.Internal.NativeCompiler.listZipWithLeftStreamCompiler {Left : Type u} {Right : Type v} {Zipped : Type w} {zipValue : Left × Right → Zipped} (default : Right) (encLeft : Left ↪ List Bool) (encRight : Right ↪ List Bool) (encZipped : Zipped ↪ List Bool) (compiler : NativeCompiler zipValue (encProd encLeft encRight) encZipped) :
NativeCompiler (fun (input : List Right × List Left) => listZipWithLeft default zipValue input.2 input.1) (encProd (encList encRight) (encList encLeft)) (encList encZipped)

Retained left-length zip scan before input reordering.

def Turing.MultiTapeTM.Internal.NativeCompiler.listZipWithLeftCompiler {Left : Type u} {Right : Type v} {Zipped : Type w} {zipValue : Left × Right → Zipped} (default : Right) (encLeft : Left ↪ List Bool) (encRight : Right ↪ List Bool) (encZipped : Zipped ↪ List Bool) (compiler : NativeCompiler zipValue (encProd encLeft encRight) encZipped) :
NativeCompiler (fun (input : List Left × List Right) => listZipWithLeft default zipValue input.1 input.2) (encProd (encList encLeft) (encList encRight)) (encList encZipped)

Retained left-length zip, including the charged input reordering pass.

Unary-indexed replacement #

@[reducible, inline]

Control state for unary-indexed list replacement.

Update the unary countdown after emitting one list element.

Emit the replacement at the selected occurrence and the original element elsewhere.

theorem Turing.MultiTapeTM.Internal.NativeCompiler.streamEmit_listReplace_done {Value : Type u} (encValue : Value ↪ List Bool) (remaining : ℕ) (replacement : Value) (list : List Value) :
streamEmit listReplaceNext (fun (input : ListReplaceState Value × Value) => delimit (encValue (listReplaceEmit input))) (remaining, true, replacement) list = (encList encValue) list

Once replacement is complete, the stream emits each remaining element unchanged.

theorem Turing.MultiTapeTM.Internal.NativeCompiler.streamEmit_listReplace {Value : Type u} (encValue : Value ↪ List Bool) (replacement : Value) (index : ℕ) (list : List Value) :
streamEmit listReplaceNext (fun (input : ListReplaceState Value × Value) => delimit (encValue (listReplaceEmit input))) (index, false, replacement) list = (encList encValue) (list.modify index fun (x : Value) => replacement)

The replacement stream agrees with List.modify; an out-of-range index changes nothing.

Retained per-occurrence state update for unary-indexed replacement.

Retained per-occurrence emitter for unary-indexed replacement.

Replacement finalization emits no extra bits.

def Turing.MultiTapeTM.Internal.NativeCompiler.listReplaceSMax {Value : Type u} (encValue : Value ↪ List Bool) (input : ListReplaceState Value × List Value) :

Explicit peak lifted-callback workspace over the actual replacement scan.

def Turing.MultiTapeTM.Internal.NativeCompiler.listReplaceLMax {Value : Type u} (encValue : Value ↪ List Bool) (input : ListReplaceState Value × List Value) :

Explicit peak encoded state/element length over the actual replacement scan.

def Turing.MultiTapeTM.Internal.NativeCompiler.listReplaceStreamCompiler {Value : Type u} (encValue : Value ↪ List Bool) :
NativeCompiler (fun (x : (ℕ × Bool × Value) × List Value) => match x with | ((index, done, replacement), list) => if done = true then list else list.modify index fun (x : Value) => replacement) (encProd (encProd encUnary (encProd encBool encValue)) (encList encValue)) (encList encValue)

Retained unary-indexed replacement scan before input marshalling.

def Turing.MultiTapeTM.Internal.NativeCompiler.listReplaceMarshalCompiler {Value : Type u} (encValue : Value ↪ List Bool) :
NativeCompiler (fun (x : List Value × ℕ × Value) => match x with | (list, index, replacement) => ((index, false, replacement), list)) (encProd (encList encValue) (encProd encUnary encValue)) (encProd (encProd encUnary (encProd encBool encValue)) (encList encValue))

Retained marshalling from (list, unary index, replacement) to replacement stream state.

def Turing.MultiTapeTM.Internal.NativeCompiler.listReplaceUnaryCompiler {Value : Type u} (encValue : Value ↪ List Bool) :
NativeCompiler (fun (x : List Value × ℕ × Value) => match x with | (list, index, replacement) => list.modify index fun (x : Value) => replacement) (encProd (encList encValue) (encProd encUnary encValue)) (encList encValue)

Unary-indexed replacement, including marshalling and a full charged list scan.

theorem Turing.MultiTapeTM.Internal.NativeCompiler.listBits_le_time_listReplaceStreamCompiler {Value : Type u} (encValue : Value ↪ List Bool) (input : ListReplaceState Value × List Value) :
((encList encValue) input.2).length ≤ (listReplaceStreamCompiler encValue).time input

Every input occurrence is charged even when the replacement index is out of range.