Source/Packages

Std.Bytes

packages/foundation/standard/src/Std/Bytes.alpha

71 lines7 declarations3.2 KiBSHA-256 6f979afb4a88

Complete file · line 18

Bytes.alpha

Definition view
1module Std.Bytes
2
3-- #242: source-order traversal with explicit state, not suspended functions.
4-- The cursor owns an immutable suffix view; the accumulator type is erased.
5family BytesTraversalState : Type 0
6parameter erased state : Type 0
7constructor BytesTraversalStateValue
8field unrestricted traversalRemaining : Bytes
9field unrestricted traversalAccumulated : state
10
11end-family
12
13-- Visit each byte exactly once in increasing offset order. The consumer is
14-- pure and may be non-associative. Empty input returns the initial state.
15-- Count the input once; exactly that many head/tail steps cannot overrun it.
16-- Storage retained by the consumer is its own contract. The traversal itself
17-- retains one suffix and one accumulator, rather than a per-byte closure chain.
18def bytesFoldFromStart =
19  (lambda erased state : Type 0 .
20    unrestricted consume : (pi unrestricted offset : Nat . unrestricted value : Byte . unrestricted accumulated : state . state) .
21    unrestricted initial : state .
22    unrestricted input : Bytes .
23    eliminate
24      BytesTraversalState
25      (lambda unrestricted cursor : (family BytesTraversalState state) . state)
26      (nat-eliminate
27        (lambda unrestricted count : Nat . family BytesTraversalState state)
28        (constructor BytesTraversalState BytesTraversalStateValue state input initial)
29        (lambda unrestricted offset : Nat .
30          unrestricted previous : (family BytesTraversalState state) .
31          eliminate
32            BytesTraversalState
33            (lambda unrestricted cursor : (family BytesTraversalState state) .
34              family BytesTraversalState state)
35            previous
36            (branch BytesTraversalStateValue remaining accumulated .
37              constructor
38                BytesTraversalState
39                BytesTraversalStateValue
40                state
41                (bytes-tail remaining)
42                (consume offset (bytes-head remaining) accumulated)))
43        (bytes-length input))
44      (branch BytesTraversalStateValue remaining accumulated . accumulated))
45
46-- Total range mechanisms: dropping past the end returns empty; the prefix
47-- builder pads with zero bytes past the end. Bounded codecs must admit ranges
48-- before calling these. Structural compiler lowering preserves these laws.
49def bytesDrop =
50  (lambda unrestricted count : Nat .
51    nat-eliminate
52      (lambda unrestricted current : Nat . pi unrestricted input : Bytes . Bytes)
53      (lambda unrestricted input : Bytes . input)
54      (lambda unrestricted predecessor : Nat .
55        unrestricted induction : (pi unrestricted input : Bytes . Bytes) .
56        unrestricted input : Bytes .
57        induction (bytes-tail input))
58      count)
59
60def bytesPrefixBuilder =
61  (lambda unrestricted count : Nat .
62    nat-eliminate
63      (lambda unrestricted current : Nat . pi unrestricted input : Bytes . BytesBuilder)
64      (lambda unrestricted input : Bytes . (bytes-builder-empty))
65      (lambda unrestricted predecessor : Nat .
66        unrestricted induction : (pi unrestricted input : Bytes . BytesBuilder) .
67        unrestricted input : Bytes .
68        bytes-builder-append
69          (pipe input bytes-head (bytes-cons b"") bytes-builder-chunk)
70          (induction (bytes-tail input)))
71      count)

The compiler supplied declaration spans and resolved links from this source snapshot. This page does not assert that this file belongs to a checked closure.