Source/Packages

Std.Foundation

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

325 lines52 declarations11.6 KiBSHA-256 3dcbd3fa87ff

Complete file · line 237

Foundation.alpha

Definition view
1module Std.Foundation
2
3-- Foundation of the standard library (Language Platform PRD §28.2, LP-800):
4-- booleans, ordering, optional values, results, pairs, and equality helpers.
5-- Every type here is an ordinary checked family: the trusted core stays small
6-- and usability comes from the library (§28.1).
7-- A decision. Distinct from Nat so a decision cannot be mistaken for a count.
8family StdBool : Type 0
9constructor StdTrue
10constructor StdFalse
11
12end-family
13
14-- The result of comparing two values.
15family StdOrder : Type 0
16constructor StdLess
17constructor StdEqual
18constructor StdGreater
19
20end-family
21
22-- A value that may be absent. The parameter is the value's type, so
23-- `StdOption` is one family used at every element type rather than one family
24-- per type.
25family StdOption : Type 0
26parameter erased stdOptionElement : Type 0
27constructor StdNone
28constructor StdSome
29field unrestricted stdSomeValue : stdOptionElement
30
31end-family
32
33-- Either an error or a value. The error type is a parameter too, so a caller
34-- chooses how rich its errors are.
35family StdResult : Type 0
36parameter erased stdResultError : Type 0
37parameter erased stdResultValue : Type 0
38constructor StdFailure
39field unrestricted stdFailureError : stdResultError
40constructor StdSuccess
41field unrestricted stdSuccessValue : stdResultValue
42
43end-family
44
45-- An ordinary pair. The dependent pair is `sigma` in the core; this is the
46-- non-dependent case, which is what most library code wants.
47family StdPair : Type 0
48parameter erased stdPairLeftType : Type 0
49parameter erased stdPairRightType : Type 0
50constructor StdPairOf
51field unrestricted stdPairLeft : stdPairLeftType
52field unrestricted stdPairRight : stdPairRightType
53
54end-family
55
56-- Negation.
57def stdBoolNot =
58  (lambda unrestricted value : (family StdBool) .
59    eliminate
60      StdBool
61      (lambda unrestricted current : (family StdBool) . family StdBool)
62      value
63      (branch StdTrue . constructor StdBool StdFalse)
64      (branch StdFalse . constructor StdBool StdTrue))
65
66-- Conjunction, evaluating both arguments (there is no short-circuiting to
67-- observe: the language is total).
68def stdBoolAnd =
69  (lambda unrestricted left : (family StdBool) . unrestricted right : (family StdBool) .
70    eliminate
71      StdBool
72      (lambda unrestricted current : (family StdBool) . family StdBool)
73      left
74      (branch StdTrue . right)
75      (branch StdFalse . constructor StdBool StdFalse))
76
77-- Disjunction.
78def stdBoolOr =
79  (lambda unrestricted left : (family StdBool) . unrestricted right : (family StdBool) .
80    eliminate
81      StdBool
82      (lambda unrestricted current : (family StdBool) . family StdBool)
83      left
84      (branch StdTrue . constructor StdBool StdTrue)
85      (branch StdFalse . right))
86
87-- Exclusive disjunction.
88def stdBoolExclusiveOr =
89  (lambda unrestricted left : (family StdBool) . unrestricted right : (family StdBool) .
90    eliminate
91      StdBool
92      (lambda unrestricted current : (family StdBool) . family StdBool)
93      left
94      (branch StdTrue . stdBoolNot right)
95      (branch StdFalse . right))
96
97-- The remaining binary Boolean functions. Together with `stdBoolAnd`,
98-- `stdBoolExclusiveOr`, and `stdBoolOr`, these name all sixteen possible
99-- truth tables over two decisions; callers never need to encode a table as a
100-- numeric flag or duplicate the logic in another package.
101def stdBoolFalse =
102  (lambda unrestricted left : (family StdBool) . unrestricted right : (family StdBool) .
103    constructor StdBool StdFalse)
104
105def stdBoolLeftAndNotRight =
106  (lambda unrestricted left : (family StdBool) . unrestricted right : (family StdBool) .
107    stdBoolAnd left (stdBoolNot right))
108
109def stdBoolLeft =
110  (lambda unrestricted left : (family StdBool) . unrestricted right : (family StdBool) . left)
111
112def stdBoolNotLeftAndRight =
113  (lambda unrestricted left : (family StdBool) . unrestricted right : (family StdBool) .
114    stdBoolAnd (stdBoolNot left) right)
115
116def stdBoolRight =
117  (lambda unrestricted left : (family StdBool) . unrestricted right : (family StdBool) . right)
118
119def stdBoolNor =
120  (lambda unrestricted left : (family StdBool) . unrestricted right : (family StdBool) .
121    stdBoolNot (stdBoolOr left right))
122
123def stdBoolEquivalent =
124  (lambda unrestricted left : (family StdBool) . unrestricted right : (family StdBool) .
125    stdBoolNot (stdBoolExclusiveOr left right))
126
127def stdBoolNotRight =
128  (lambda unrestricted left : (family StdBool) . unrestricted right : (family StdBool) .
129    stdBoolNot right)
130
131def stdBoolLeftOrNotRight =
132  (lambda unrestricted left : (family StdBool) . unrestricted right : (family StdBool) .
133    stdBoolOr left (stdBoolNot right))
134
135def stdBoolNotLeft =
136  (lambda unrestricted left : (family StdBool) . unrestricted right : (family StdBool) .
137    stdBoolNot left)
138
139def stdBoolNotLeftOrRight =
140  (lambda unrestricted left : (family StdBool) . unrestricted right : (family StdBool) .
141    stdBoolOr (stdBoolNot left) right)
142
143def stdBoolNand =
144  (lambda unrestricted left : (family StdBool) . unrestricted right : (family StdBool) .
145    stdBoolNot (stdBoolAnd left right))
146
147def stdBoolTrue =
148  (lambda unrestricted left : (family StdBool) . unrestricted right : (family StdBool) .
149    constructor StdBool StdTrue)
150
151-- A decision as a natural: zero is false, one is true. The conversion is
152-- explicit, so a count is never treated as a decision by accident.
153def stdBoolToNatural =
154  (lambda unrestricted value : (family StdBool) .
155    eliminate
156      StdBool
157      (lambda unrestricted current : (family StdBool) . Nat)
158      value
159      (branch StdTrue . 1)
160      (branch StdFalse . 0))
161
162-- Zero is false, every other natural is true.
163def stdBoolFromNatural =
164  (lambda unrestricted value : Nat .
165    nat-eliminate
166      (lambda unrestricted current : Nat . family StdBool)
167      (constructor StdBool StdFalse)
168      (lambda unrestricted predecessor : Nat . unrestricted induction : (family StdBool) .
169        constructor StdBool StdTrue)
170      value)
171
172-- The opposite comparison: what `stdOrderCompare y x` would say.
173def stdOrderReverse =
174  (lambda unrestricted value : (family StdOrder) .
175    eliminate
176      StdOrder
177      (lambda unrestricted current : (family StdOrder) . family StdOrder)
178      value
179      (branch StdLess . constructor StdOrder StdGreater)
180      (branch StdEqual . constructor StdOrder StdEqual)
181      (branch StdGreater . constructor StdOrder StdLess))
182
183-- Is this comparison equality?
184def stdOrderIsEqual =
185  (lambda unrestricted value : (family StdOrder) .
186    eliminate
187      StdOrder
188      (lambda unrestricted current : (family StdOrder) . family StdBool)
189      value
190      (branch StdLess . constructor StdBool StdFalse)
191      (branch StdEqual . constructor StdBool StdTrue)
192      (branch StdGreater . constructor StdBool StdFalse))
193
194-- Is the left value the smaller one?
195def stdOrderIsLess =
196  (lambda unrestricted value : (family StdOrder) .
197    eliminate
198      StdOrder
199      (lambda unrestricted current : (family StdOrder) . family StdBool)
200      value
201      (branch StdLess . constructor StdBool StdTrue)
202      (branch StdEqual . constructor StdBool StdFalse)
203      (branch StdGreater . constructor StdBool StdFalse))
204
205-- Compare two naturals, answering with an order rather than a flag.
206def stdOrderCompareNatural =
207  (lambda unrestricted left : Nat . unrestricted right : Nat .
208    eliminate
209      StdBool
210      (lambda unrestricted current : (family StdBool) . family StdOrder)
211      (stdBoolFromNatural (nat-less-than left right))
212      (branch StdTrue . constructor StdOrder StdLess)
213      (branch
214        StdFalse
215        .
216        eliminate
217          StdBool
218          (lambda unrestricted current : (family StdBool) . family StdOrder)
219          (stdBoolFromNatural (nat-less-than right left))
220          (branch StdTrue . constructor StdOrder StdGreater)
221          (branch StdFalse . constructor StdOrder StdEqual)))
222
223-- The value an option holds, or the supplied default. The element type is
224-- explicit, because the library never guesses a type.
225def stdOptionValueOr =
226  (lambda erased element : Type 0 .
227    unrestricted fallback : element .
228    unrestricted value : (family StdOption element) .
229    eliminate
230      StdOption
231      (lambda unrestricted current : (family StdOption element) . element)
232      value
233      (branch StdNone . fallback)
234      (branch StdSome held . held))
235
236-- Does this option hold a value?
237def stdOptionIsSome =
238  (lambda erased element : Type 0 . unrestricted value : (family StdOption element) .
239    eliminate
240      StdOption
241      (lambda unrestricted current : (family StdOption element) . family StdBool)
242      value
243      (branch StdNone . constructor StdBool StdFalse)
244      (branch StdSome held . constructor StdBool StdTrue))
245
246-- The value a result holds, or the supplied default.
247def stdResultValueOr =
248  (lambda erased errorType : Type 0 .
249    erased valueType : Type 0 .
250    unrestricted fallback : valueType .
251    unrestricted value : (family StdResult errorType valueType) .
252    eliminate
253      StdResult
254      (lambda unrestricted current : (family StdResult errorType valueType) . valueType)
255      value
256      (branch StdFailure held . fallback)
257      (branch StdSuccess held . held))
258
259-- Did this result succeed?
260def stdResultIsSuccess =
261  (lambda erased errorType : Type 0 .
262    erased valueType : Type 0 .
263    unrestricted value : (family StdResult errorType valueType) .
264    eliminate
265      StdResult
266      (lambda unrestricted current : (family StdResult errorType valueType) . family StdBool)
267      value
268      (branch StdFailure held . constructor StdBool StdFalse)
269      (branch StdSuccess held . constructor StdBool StdTrue))
270
271-- Compose checked operations without replacing an earlier error with a later
272-- default. The continuation receives a value only in the success branch.
273def stdResultBind =
274  (lambda erased errorType : Type 0 .
275    erased inputType : Type 0 .
276    erased outputType : Type 0 .
277    unrestricted input : (family StdResult errorType inputType) .
278    unrestricted next : (pi unrestricted value : inputType . family StdResult errorType outputType) .
279    eliminate
280      StdResult
281      (lambda unrestricted current : (family StdResult errorType inputType) .
282        family StdResult errorType outputType)
283      input
284      (branch StdFailure error . constructor StdResult StdFailure errorType outputType error)
285      (branch StdSuccess value . next value))
286
287-- A result as an option, dropping the error.
288def stdResultToOption =
289  (lambda erased errorType : Type 0 .
290    erased valueType : Type 0 .
291    unrestricted value : (family StdResult errorType valueType) .
292    eliminate
293      StdResult
294      (lambda unrestricted current : (family StdResult errorType valueType) .
295        family StdOption valueType)
296      value
297      (branch StdFailure held . constructor StdOption StdNone valueType)
298      (branch StdSuccess held . constructor StdOption StdSome valueType held))
299
300-- The left component of a pair.
301def stdPairLeftOf =
302  (lambda erased leftType : Type 0 .
303    erased rightType : Type 0 .
304    unrestricted value : (family StdPair leftType rightType) .
305    eliminate
306      StdPair
307      (lambda unrestricted current : (family StdPair leftType rightType) . leftType)
308      value
309      (branch StdPairOf left right . left))
310
311-- The right component of a pair.
312def stdPairRightOf =
313  (lambda erased leftType : Type 0 .
314    erased rightType : Type 0 .
315    unrestricted value : (family StdPair leftType rightType) .
316    eliminate
317      StdPair
318      (lambda unrestricted current : (family StdPair leftType rightType) . rightType)
319      value
320      (branch StdPairOf left right . right))
321
322-- Equality is reflexive by construction: this is the proof that a value
323-- equals itself, named so library code can pass it around.
324def stdEqualReflexive =
325  (lambda erased element : Type 0 . unrestricted value : element . refl element value)

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.