Quantitative streaming folds #
Ordinary List.foldl expressions over raw bit strings use Christian Reitwiessner's streaming
backend. A round holds the accumulator and one bit, and does not copy the input list. Time, work
space and encoded accumulator size remain separate in the contract. Space counts visited work-tape
cells, excluding the read-only input and write-only output; retained accumulators are charged.
The invariant wrappers use the same pure prefix-fold rule, keeping their distinct resource
contracts separate from invariant propagation.
Main results #
Turing.MultiTapeTM.ComputableTidilyInTimeAndSpace.foldl_bits_stream_sum: cumulative computation and copying costs, with peak workspace bounds.Turing.MultiTapeTM.ComputableTidilyInTimeAndSpace.foldl_bits_stream_sum_spec: an invariant supplies the cumulative cost and postcondition.Turing.MultiTapeTM.ComputableTidilyInTimeAndSpace.foldl_bits_pair_stream_sum: stream a paired initial accumulator and bit list(s₀, l)onencPair enc encBitswith cumulative round costs and workspace independent ofl.length.Turing.MultiTapeTM.ComputableTidilyInTimeAndSpace.foldl_bits_pair_stream_sum_spec: invariant wrapper for paired bit-list streaming folds.Turing.MultiTapeTM.ComputableTidilyInTimeAndSpace.iterate_unary_stream_sum: streamkrounds of a state map from(s, k), emitting per round, with cumulative round costs and workspace independent ofk.Turing.MultiTapeTM.ComputableTidilyInTimeAndSpace.foldl_bits_stream: the uniform-round specialization.Turing.MultiTapeTM.ComputableTidilyInTimeAndSpace.foldl_bits_stream_spec: an invariant supplies the bounds and a postcondition.Turing.MultiTapeTM.ComputableTidilyInO.foldl_bits_finite: a fixed finite-state fold takes linear time and constant work space, including a bound on its encoded final state.Turing.MultiTapeTM.ComputableTidilyInTimeAndSpace.map_bits: a bitwise map emits its output as it scans, in linear time and zero work space.Turing.MultiTapeTM.ComputableTidilyInTimeAndSpace.reverse_bits,Turing.MultiTapeTM.ComputableTidilyInTimeAndSpace.length_bits,Turing.MultiTapeTM.ComputableTidilyInTimeAndSpace.dropLast_bits,Turing.MultiTapeTM.ComputableTidilyInTimeAndSpace.getLast?_toList_bits,Turing.MultiTapeTM.ComputableTidilyInTimeAndSpace.getLast?_bits: linear-time zero-workspace bit-list reversal, unary length, prefix (List.dropLast), and last-bit (List.getLast?.toList,List.getLast?) primitives.
References #
Uses Christian Reitwiessner's ComputableTidilyInTimeAndSpace.streamFold from the
crei/tidy-universal branch; see the source marker in
CslibFrontier/Computability/Machines/Turing/MultiTape/Encodings/StreamFold.lean.
Stream a bit list with cumulative round costs. R l i includes the step, the current and next
encoded state lengths, and control. Q and B bound peak step workspace and encoded state size.
The input is scanned without copying; final output and tape restoration are charged too.
The constant is independent of the input, and the bounds need not be computable.
Stream a bit list through a tidy step computation. Each round charges the step and the stored
state, without copying the input list. R, Q and B bound the time of a round, its work space
and the encoded state size, respectively. The final output and tape restoration are charged too.
The constant is independent of the input; the three bounds need not be computable.
An invariant supplies cumulative computation and copying costs, peak workspace and a postcondition. Each round's bound includes both the old and the new encoded state.
An invariant supplies the streaming fold's resource bounds and postcondition.
A fold with a fixed finite state type takes linear time and constant work space. The constant may depend on the state type, its encoding and the transition function; this is not a uniform bound for a family of growing state types. The encoded final state is bounded as well.
Emit a bitwise map directly, without a growing accumulator on a work tape. Unlike InO,
this numerical certificate does not include the emitted output in its space bound.
Stream a paired initial accumulator and bit list (s₀, l) on encPair enc encBits with
cumulative round costs. The framed initial accumulator frame (enc s₀) is copied once to the
state tape by rawFrameReader, and the remaining bits l are streamed directly from the input
tape without copying l onto work tapes, so workspace depends only on peak step workspace Q and
peak encoded accumulator length B.
An invariant supplies cumulative computation and copying costs, peak workspace, and a
postcondition for a paired streaming fold over (s₀, l) : σ × List Bool.
A fold whose step ignores its token iterates the state map, j times after j tokens.
Stream k rounds of a state map f from (s, k) on encPair enc encUnary. Round i emits
emit (f^[i] s) directly to the output, and the final state is finished by finish. The unary
count is read from the input tape one bit per round and never copied, so workspace holds only the
current state and the callbacks. R x i charges round i: the step, the emission, and the current
and next encoded states; Q and B bound peak callback workspace and encoded state size.
Reverse a bit list tidily in 2 * l.length + 2 steps and zero work-tape space, using the
two-state zero-work-tape primitive Turing.MultiTapeTM.reverseInput.
The length of a bit list, emitted in unary while scanning, in 4 * l.length + 8 steps and
zero work-tape space.
Drop the last bit of a bit list (List.dropLast) tidily in 4 * l.length + 8 steps and zero
work-tape space, using a three-state (Option Bool) finite-state transducer that buffers one
lookahead symbol.
Extract the last bit of a bit list as a 0-or-1-element list (l.getLast?.toList) tidily in
4 * l.length + 8 steps and zero work-tape space.
Extract the last bit of a bit list as an Option Bool (l.getLast?) tidily in
4 * l.length + 8 steps and zero work-tape space.