Source/Packages

Std.List

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

317 lines21 declarations13.5 KiBSHA-256 0d04bb5f90d2

Complete file · line 116

List.alpha

Definition view
1module Std.List
2
3import Std.Foundation
4
5-- Structural lists (Language Platform PRD §28.2 Collections, LP-802).
6--
7-- This is the small structural sequence: cheap to build, cheap to walk, and
8-- shared rather than copied — appending to a list keeps the original intact
9-- because the tail is shared, which is the sharing property §28.4 requires.
10-- Iteration is deterministic by construction: a list has one order, the one
11-- it was built in.
12--
13-- Asymptotic contract (§28.4 requires it to be explicit):
14--   stdListCons     O(1)
15--   stdListHead     O(1)
16--   stdListLength   O(n)
17--   stdListAppend   O(n) in the left list, sharing the right one
18--   stdListReverse  O(n)
19--   stdListMap      O(n)
20--   stdListFold     O(n)
21--   stdListFoldFromHead O(n)
22--   stdListZipWith  O(min(n, m))
23--   stdListIndex    O(i)
24family StdList : Type 0
25parameter erased stdListElement : Type 0
26constructor StdListEmpty
27constructor StdListCons
28field unrestricted stdListHeadValue : stdListElement
29recursive unrestricted stdListTailValue
30
31end-family
32
33-- The number of elements.
34def stdListLength =
35  (lambda erased element : Type 0 . unrestricted values : (family StdList element) .
36    eliminate
37      StdList
38      (lambda unrestricted current : (family StdList element) . Nat)
39      values
40      (branch StdListEmpty . 0)
41      (branch StdListCons head tail induction . succ induction))
42
43-- The first element, or the supplied default for an empty list.
44def stdListHeadOr =
45  (lambda erased element : Type 0 .
46    unrestricted fallback : element .
47    unrestricted values : (family StdList element) .
48    eliminate
49      StdList
50      (lambda unrestricted current : (family StdList element) . element)
51      values
52      (branch StdListEmpty . fallback)
53      (branch StdListCons head tail induction . head))
54
55-- The first element as an option, so the empty case is visible in the type.
56def stdListHead =
57  (lambda erased element : Type 0 . unrestricted values : (family StdList element) .
58    eliminate
59      StdList
60      (lambda unrestricted current : (family StdList element) . family StdOption element)
61      values
62      (branch StdListEmpty . constructor StdOption StdNone element)
63      (branch StdListCons head tail induction . constructor StdOption StdSome element head))
64
65-- Everything after the first element; the empty list has an empty tail.
66def stdListTail =
67  (lambda erased element : Type 0 . unrestricted values : (family StdList element) .
68    eliminate
69      StdList
70      (lambda unrestricted current : (family StdList element) . family StdList element)
71      values
72      (branch StdListEmpty . constructor StdList StdListEmpty element)
73      (branch StdListCons head tail induction . tail))
74
75-- Is the list empty?
76def stdListIsEmpty =
77  (lambda erased element : Type 0 . unrestricted values : (family StdList element) .
78    eliminate
79      StdList
80      (lambda unrestricted current : (family StdList element) . family StdBool)
81      values
82      (branch StdListEmpty . constructor StdBool StdTrue)
83      (branch StdListCons head tail induction . constructor StdBool StdFalse))
84
85-- The left list followed by the right one. The right list is SHARED, not
86-- copied: only the left spine is rebuilt.
87def stdListAppend =
88  (lambda erased element : Type 0 .
89    unrestricted left : (family StdList element) .
90    unrestricted right : (family StdList element) .
91    eliminate
92      StdList
93      (lambda unrestricted current : (family StdList element) . family StdList element)
94      left
95      (branch StdListEmpty . right)
96      (branch StdListCons head tail induction .
97        constructor StdList StdListCons element head induction))
98
99-- The list in the opposite order.
100def stdListReverse =
101  (lambda erased element : Type 0 . unrestricted values : (family StdList element) .
102    app
103      (eliminate
104        StdList
105        (lambda unrestricted current : (family StdList element) .
106          pi unrestricted accumulator : (family StdList element) . family StdList element)
107        values
108        (branch StdListEmpty .
109          lambda unrestricted accumulator : (family StdList element) . accumulator)
110        (branch StdListCons head tail induction .
111          lambda unrestricted accumulator : (family StdList element) .
112          induction (constructor StdList StdListCons element head accumulator)))
113      (constructor StdList StdListEmpty element))
114
115-- Every element transformed, in order.
116def stdListMap =
117  (lambda erased element : Type 0 .
118    erased target : Type 0 .
119    unrestricted transform : (pi unrestricted value : element . target) .
120    unrestricted values : (family StdList element) .
121    eliminate
122      StdList
123      (lambda unrestricted current : (family StdList element) . family StdList target)
124      values
125      (branch StdListEmpty . constructor StdList StdListEmpty target)
126      (branch StdListCons head tail induction .
127        constructor StdList StdListCons target (transform head) induction))
128
129-- Fold from the right: `stdListFold f z [a, b] = f a (f b z)`.
130def stdListFold =
131  (lambda erased element : Type 0 .
132    erased accumulator : Type 0 .
133    unrestricted step : (pi unrestricted value : element . unrestricted carried : accumulator . accumulator) .
134    unrestricted initial : accumulator .
135    unrestricted values : (family StdList element) .
136    eliminate
137      StdList
138      (lambda unrestricted current : (family StdList element) . accumulator)
139      values
140      (branch StdListEmpty . initial)
141      (branch StdListCons head tail induction . step head induction))
142
143-- Fold from the head: `step (... (step (step initial x0) x1) ...) xn`.
144-- stdListFold visits the LAST element first (its step sees the folded tail);
145-- this visits the FIRST element first and threads the accumulator forward,
146-- which is the order a floating-point accumulation or a reverse-time pass over
147-- a newest-first history must respect (the results differ when the step does
148-- not associate, e.g. f32 addition). The list is eliminated into a function of
149-- the accumulator, so the recursion is still structural.
150def stdListFoldFromHead =
151  (lambda erased element : Type 0 .
152    erased accumulator : Type 0 .
153    unrestricted step : (pi unrestricted carried : accumulator . unrestricted value : element . accumulator) .
154    unrestricted initial : accumulator .
155    unrestricted values : (family StdList element) .
156    app
157      (eliminate
158        StdList
159        (lambda unrestricted current : (family StdList element) .
160          pi unrestricted carried : accumulator . accumulator)
161        values
162        (branch StdListEmpty . lambda unrestricted carried : accumulator . carried)
163        (branch StdListCons head tail induction .
164          lambda unrestricted carried : accumulator . induction (step carried head)))
165      initial)
166
167-- Combine two lists position by position, keeping their order; the result is
168-- as long as the SHORTER list (the rest of the longer one is dropped -- callers
169-- that need equal lengths must establish them). The left list is eliminated
170-- into a function of the right one, so both spines are walked together.
171def stdListZipWith =
172  (lambda erased left : Type 0 .
173    erased right : Type 0 .
174    erased result : Type 0 .
175    unrestricted combine : (pi unrestricted l : left . unrestricted r : right . result) .
176    unrestricted lefts : (family StdList left) .
177    unrestricted rights : (family StdList right) .
178    app
179      (eliminate
180        StdList
181        (lambda unrestricted current : (family StdList left) .
182          pi unrestricted others : (family StdList right) . family StdList result)
183        lefts
184        (branch StdListEmpty .
185          lambda unrestricted others : (family StdList right) .
186          constructor StdList StdListEmpty result)
187        (branch StdListCons head tail induction .
188          lambda unrestricted others : (family StdList right) .
189          eliminate
190            StdList
191            (lambda unrestricted inner : (family StdList right) . family StdList result)
192            others
193            (branch StdListEmpty . constructor StdList StdListEmpty result)
194            (branch StdListCons otherHead otherTail otherInduction .
195              constructor StdList StdListCons result (combine head otherHead) (induction otherTail))))
196      rights)
197
198-- The first `count` elements (all of them when the list is shorter); the
199-- structural recursion is on the count, the list is eliminated one cell at a
200-- time under it (Language & Testing Evolution L5: shrinkers halve lists).
201def stdListTake =
202  (lambda erased element : Type 0 .
203    unrestricted count : Nat .
204    unrestricted values : (family StdList element) .
205    app
206      (nat-eliminate
207        (lambda unrestricted current : Nat .
208          pi unrestricted remaining : (family StdList element) . family StdList element)
209        (lambda unrestricted remaining : (family StdList element) .
210          constructor StdList StdListEmpty element)
211        (lambda unrestricted predecessor : Nat .
212          unrestricted induction : (pi unrestricted remaining : (family StdList element) . family StdList element) .
213          unrestricted remaining : (family StdList element) .
214          eliminate
215            StdList
216            (lambda unrestricted current : (family StdList element) . family StdList element)
217            remaining
218            (branch StdListEmpty . constructor StdList StdListEmpty element)
219            (branch StdListCons head tail tailInduction .
220              constructor StdList StdListCons element head (induction tail)))
221        count)
222      values)
223
224-- Everything after the first `count` elements.
225def stdListDrop =
226  (lambda erased element : Type 0 .
227    unrestricted count : Nat .
228    unrestricted values : (family StdList element) .
229    app
230      (nat-eliminate
231        (lambda unrestricted current : Nat .
232          pi unrestricted remaining : (family StdList element) . family StdList element)
233        (lambda unrestricted remaining : (family StdList element) . remaining)
234        (lambda unrestricted predecessor : Nat .
235          unrestricted induction : (pi unrestricted remaining : (family StdList element) . family StdList element) .
236          unrestricted remaining : (family StdList element) .
237          eliminate
238            StdList
239            (lambda unrestricted current : (family StdList element) . family StdList element)
240            remaining
241            (branch StdListEmpty . constructor StdList StdListEmpty element)
242            (branch StdListCons head tail tailInduction . induction tail))
243        count)
244      values)
245
246-- The element at a position, as an option: an index past the end is absent,
247-- never an error and never a wrong element.
248def stdListIndex =
249  (lambda erased element : Type 0 .
250    unrestricted values : (family StdList element) .
251    unrestricted position : Nat .
252    app
253      (eliminate
254        StdList
255        (lambda unrestricted current : (family StdList element) .
256          pi unrestricted remaining : Nat . family StdOption element)
257        values
258        (branch StdListEmpty .
259          lambda unrestricted remaining : Nat . constructor StdOption StdNone element)
260        (branch StdListCons head tail induction .
261          lambda unrestricted remaining : Nat .
262          nat-eliminate
263            (lambda unrestricted current : Nat . family StdOption element)
264            (constructor StdOption StdSome element head)
265            (lambda unrestricted predecessor : Nat .
266              unrestricted inner : (family StdOption element) .
267              induction (nat-subtract remaining 1))
268            -- a branch on zero, not a recursion on `remaining`: a
269            -- strict evaluator would evaluate every smaller position's
270            -- lookup, doubling the cost with each position
271            (nat-less-than 0 remaining)))
272      position)
273
274-- A list of the naturals below a bound, in increasing order: `[0, 1, ..., n-1]`.
275def stdListNaturalsBelow =
276  (lambda unrestricted bound : Nat .
277    nat-eliminate
278      (lambda unrestricted current : Nat . family StdList Nat)
279      (constructor StdList StdListEmpty Nat)
280      (lambda unrestricted predecessor : Nat . unrestricted induction : (family StdList Nat) .
281        stdListAppend
282          Nat
283          induction
284          (constructor StdList StdListCons Nat predecessor (constructor StdList StdListEmpty Nat)))
285      bound)
286
287-- Structural equality of two lists of naturals: same length, same elements in
288-- the same order. The first list is eliminated into a FUNCTION of the second,
289-- so the recursion walks both spines together.  A proof about two computed
290-- lists states this closed verdict rather than an equation between them: an
291-- equation between two lists built by the same builder is compared
292-- symbolically first (the builders' element functions, under a binder),
293-- which costs far more than computing them (Proof.CheckedHMMATile's instance
294-- words: 580 s against 34 s).
295def stdListNaturalEqual =
296  (lambda unrestricted expected : (family StdList Nat) .
297    unrestricted actual : (family StdList Nat) .
298    app
299      (eliminate
300        StdList
301        (lambda unrestricted current : (family StdList Nat) .
302          pi unrestricted other : (family StdList Nat) . family StdBool)
303        expected
304        (branch StdListEmpty .
305          lambda unrestricted other : (family StdList Nat) . stdListIsEmpty Nat other)
306        (branch StdListCons head tail induction .
307          lambda unrestricted other : (family StdList Nat) .
308          eliminate
309            StdList
310            (lambda unrestricted current : (family StdList Nat) . family StdBool)
311            other
312            (branch StdListEmpty . constructor StdBool StdFalse)
313            (branch StdListCons otherHead otherTail otherInduction .
314              stdBoolAnd
315                (stdOrderIsEqual (stdOrderCompareNatural head otherHead))
316                (induction otherTail))))
317      actual)

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.