Landin library reference discuss source
1--  Comparison evidence shared by every module that compares, hashes or
2--  orders its items: the map, the sort and the text layer import it, and
3--  a later type declares its conformances against it.
4
5--- Equality evidence. `equal` must be an equivalence relation, and stay one
6--- while an item is stored, for example as a map key.
7public equatable: type = concept (item: type)
8    --- Report whether two items are equal under a stable equivalence.
9    equal: (left: item, right: item) -> (yes: bool)
10end equatable
11
12seed_value: type = struct
13    first: u64
14    second: u64
15end seed_value
16
17--- The key of a keyed hash: 128 bits that choose which of SipHash's
18--- functions `hash_bytes` and `hash_word` compute.  Whoever does not know
19--- the seed cannot tell which keys will collide, so a map seeded from
20--- entropy cannot be filled with keys chosen to share one probe chain.
21--- Make one with `seed_from`; a hosted program draws one with
22--- `random.hash_seed`.
23public hash_seed: type = seed_value
24
25--- The seed whose SipHash key is `first` and then `second`, each as its
26--- eight little-endian bytes: SipHash's k0 and k1.  The same words give
27--- the same hashes on every target.
28public seed_from: (first: u64, second: u64) -> (seed: hash_seed) =
29    seed = (first: first, second: second)
30end seed_from
31
32--- Hash evidence, which composes `equatable`. Items that are `equal` must
33--- have equal hashes under the same seed, and both answers must stay
34--- stable while an item is stored. A hash should depend on the seed as
35--- well as the item, so that one who does not know the seed cannot choose
36--- items that collide: the hashes this module ships are SipHash-1-3 keyed
37--- by it, and a composite key builds its hash from `hash_bytes` or
38--- `hash_word` with the seed it is given. They are not cryptographic
39--- digests, and the seed is a secret only as long as the program keeps
40--- it one.
41---
42--- Although `hashable` composes `equatable`, [1340] still requires a
43--- concrete type to declare both conformances explicitly.
44public hashable: type = concept (item: type) is equatable
45    --- Return a hash of `value` keyed by `seed`; items `equal` to each
46    --- other hash equally under one seed.
47    hash: (value: item, seed: hash_seed) -> (result: u64)
48end hashable
49
50--  The compiler does not prove the semantic laws: equal must be an
51--  equivalence relation, equal items must hash alike, and both answers must
52--  stay stable while an item is stored, including across mutation of
53--  anything a pointer or reference item reaches.
54
55--  Types the library supplies evidence for: the integer scalars, bool and
56--  byte slices here, and utf8 in core/text. There is one register and no
57--  override [1280]; a program wanting another equality or hash for one of
58--  these wraps the type in a distinct type and declares evidence for that.
59--  A scalar hashes as `hash_word` of its two's-complement bits widened,
60--  computed without a narrowing conversion, and a bool as the word 0 or 1;
61--  byte slices are `hash_bytes`.
62equal_u8: (left: u8, right: u8) -> (yes: bool) =
63    yes = left == right
64end equal_u8
65hash_u8: (value: u8, seed: hash_seed) -> (result: u64) =
66    result = hash_word(u64(value), seed)
67end hash_u8
68equal_u16: (left: u16, right: u16) -> (yes: bool) =
69    yes = left == right
70end equal_u16
71hash_u16: (value: u16, seed: hash_seed) -> (result: u64) =
72    result = hash_word(u64(value), seed)
73end hash_u16
74equal_u32: (left: u32, right: u32) -> (yes: bool) =
75    yes = left == right
76end equal_u32
77hash_u32: (value: u32, seed: hash_seed) -> (result: u64) =
78    result = hash_word(u64(value), seed)
79end hash_u32
80equal_u64: (left: u64, right: u64) -> (yes: bool) =
81    yes = left == right
82end equal_u64
83hash_u64: (value: u64, seed: hash_seed) -> (result: u64) =
84    result = hash_word(u64(value), seed)
85end hash_u64
86equal_usize: (left: usize, right: usize) -> (yes: bool) =
87    yes = left == right
88end equal_usize
89hash_usize: (value: usize, seed: hash_seed) -> (result: u64) =
90    result = hash_word(u64(value), seed)
91end hash_usize
92equal_i8: (left: i8, right: i8) -> (yes: bool) =
93    yes = left == right
94end equal_i8
95hash_i8: (value: i8, seed: hash_seed) -> (result: u64) =
96    if value >= 0 then
97        result = hash_word(u64(value), seed)
98    else
99        result = hash_word(~u64(0 - (value + 1)), seed)
100    end if
101end hash_i8
102equal_i16: (left: i16, right: i16) -> (yes: bool) =
103    yes = left == right
104end equal_i16
105hash_i16: (value: i16, seed: hash_seed) -> (result: u64) =
106    if value >= 0 then
107        result = hash_word(u64(value), seed)
108    else
109        result = hash_word(~u64(0 - (value + 1)), seed)
110    end if
111end hash_i16
112equal_i32: (left: i32, right: i32) -> (yes: bool) =
113    yes = left == right
114end equal_i32
115hash_i32: (value: i32, seed: hash_seed) -> (result: u64) =
116    if value >= 0 then
117        result = hash_word(u64(value), seed)
118    else
119        result = hash_word(~u64(0 - (value + 1)), seed)
120    end if
121end hash_i32
122equal_i64: (left: i64, right: i64) -> (yes: bool) =
123    yes = left == right
124end equal_i64
125hash_i64: (value: i64, seed: hash_seed) -> (result: u64) =
126    if value >= 0 then
127        result = hash_word(u64(value), seed)
128    else
129        result = hash_word(~u64(0 - (value + 1)), seed)
130    end if
131end hash_i64
132equal_isize: (left: isize, right: isize) -> (yes: bool) =
133    yes = left == right
134end equal_isize
135hash_isize: (value: isize, seed: hash_seed) -> (result: u64) =
136    if value >= 0 then
137        result = hash_word(u64(value), seed)
138    else
139        result = hash_word(~u64(0 - (value + 1)), seed)
140    end if
141end hash_isize
142equal_bool: (left: bool, right: bool) -> (yes: bool) =
143    yes = left == right
144end equal_bool
145hash_bool: (value: bool, seed: hash_seed) -> (result: u64) =
146    result = hash_word(if value then 1 else 0 end if, seed)
147end hash_bool
148equal_bytes: (left: []u8, right: []u8) -> (yes: bool) =
149    if lenof left <> lenof right then
150        yes = false
151        return
152    end if
153    mut index: usize = 0
154    while index < lenof left do
155        if left[index] <> right[index] then
156            yes = false
157            return
158        end if
159        inc index
160    end while
161    yes = true
162end equal_bytes
163--- SipHash-1-3 of `value` keyed by `seed`: Aumasson and Bernstein's
164--- SipHash with one compression round per eight-byte word and three
165--- finalization rounds, the variant Rust's and CPython's hash tables use.
166--- It is `[]u8`'s hash, and `utf8`'s, and gives the same result on every
167--- target.  It is not a cryptographic digest.
168public hash_bytes: (value: []u8, seed: hash_seed) -> (result: u64) =
169    result = siphash(value, 0, false, seed)
170end hash_bytes
171
172--- SipHash-1-3 keyed by `seed` of `value`'s eight bytes, least significant
173--- first: what `hash_bytes` gives for those bytes, without storing them.
174--- Every integer scalar's hash is this word's.
175public hash_word: (value: u64, seed: hash_seed) -> (result: u64) =
176    result = siphash(no_bytes[0..<0], value, true, seed)
177end hash_word
178
179no_bytes: [1]u8 = [0]
180
181--  SipHash-1-3 (Aumasson and Bernstein, "SipHash: a fast short-input PRF",
182--  INDOCRYPT 2012) after the authors' reference siphash.c built with
183--  cROUNDS 1 and dROUNDS 3.  The message is VALUE's bytes, or WORD's eight
184--  when WHOLE.  Each eight-byte word, read least significant byte first,
185--  and then the last word, holding the bytes left over and the length's
186--  low byte at the top, takes one round; three finish.  The round and the
187--  byte loop are written once, so the code stays small on Cortex-M0.
188siphash: (value: []u8, word: u64, whole: bool, seed: hash_seed)
189         -> (result: u64) =
190    mut v0: u64 = seed.first ^ 0x736f6d6570736575
191    mut v1: u64 = seed.second ^ 0x646f72616e646f6d
192    mut v2: u64 = seed.first ^ 0x6c7967656e657261
193    mut v3: u64 = seed.second ^ 0x7465646279746573
194    length: usize = if whole then 8 else lenof value end if
195    mut at: usize = 0
196    mut m: u64 = 0
197    mut finishing: bool = false
198    loop do
199        mut rounds: usize = 1
200        if at <= length then
201            m = 0
202            mut shift: u64 = 0
203            if whole and at == 0 then
204                m = word
205                shift = 64
206                at = 8
207            end if
208            while shift < 64 and at < length do
209                m = m | (u64(value[at]) << shift)
210                shift += 8
211                inc at
212            end while
213            if shift < 64 then
214                --  The last word: fewer than eight bytes were left.
215                m = m | (u64(length & 255) << 56)
216                at = length + 1
217            end if
218            v3 ^= m
219        else
220            finishing = true
221            rounds = 3
222            v2 ^= 0xff
223        end if
224        while rounds > 0 do
225            v0 = v0 +% v1
226            v1 = (v1 << 13) | (v1 >> 51)
227            v1 ^= v0
228            v0 = (v0 << 32) | (v0 >> 32)
229            v2 = v2 +% v3
230            v3 = (v3 << 16) | (v3 >> 48)
231            v3 ^= v2
232            v0 = v0 +% v3
233            v3 = (v3 << 21) | (v3 >> 43)
234            v3 ^= v0
235            v2 = v2 +% v1
236            v1 = (v1 << 17) | (v1 >> 47)
237            v1 ^= v2
238            v2 = (v2 << 32) | (v2 >> 32)
239            dec rounds
240        end while
241        break when finishing
242        v0 ^= m
243    end loop
244    result = v0 ^ v1 ^ v2 ^ v3
245end siphash
246u8 is equatable (equal: equal_u8)
247u8 is hashable (hash: hash_u8)
248u16 is equatable (equal: equal_u16)
249u16 is hashable (hash: hash_u16)
250u32 is equatable (equal: equal_u32)
251u32 is hashable (hash: hash_u32)
252u64 is equatable (equal: equal_u64)
253u64 is hashable (hash: hash_u64)
254usize is equatable (equal: equal_usize)
255usize is hashable (hash: hash_usize)
256i8 is equatable (equal: equal_i8)
257i8 is hashable (hash: hash_i8)
258i16 is equatable (equal: equal_i16)
259i16 is hashable (hash: hash_i16)
260i32 is equatable (equal: equal_i32)
261i32 is hashable (hash: hash_i32)
262i64 is equatable (equal: equal_i64)
263i64 is hashable (hash: hash_i64)
264isize is equatable (equal: equal_isize)
265isize is hashable (hash: hash_isize)
266bool is equatable (equal: equal_bool)
267bool is hashable (hash: hash_bool)
268[]u8 is equatable (equal: equal_bytes)
269[]u8 is hashable (hash: hash_bytes)
270
271--- Strict ordering evidence. `less` must be irreflexive and transitive, with
272--- consistent equivalence classes, for the duration of a sort.
273public ordered: type = concept (item: type)
274    --- Return true exactly when left precedes right under the strict weak
275    --- ordering.
276    less: (left: item, right: item) -> (yes: bool)
277end ordered
278
279--  The library supplies the ordering of every integer scalar, and core/text
280--  that of utf8. One register, no override [1280]: another ordering for one
281--  of these goes on a distinct wrapper.
282less_u8: (left: u8, right: u8) -> (yes: bool) =
283    yes = left < right
284end less_u8
285less_u16: (left: u16, right: u16) -> (yes: bool) =
286    yes = left < right
287end less_u16
288less_u32: (left: u32, right: u32) -> (yes: bool) =
289    yes = left < right
290end less_u32
291less_u64: (left: u64, right: u64) -> (yes: bool) =
292    yes = left < right
293end less_u64
294less_usize: (left: usize, right: usize) -> (yes: bool) =
295    yes = left < right
296end less_usize
297less_i8: (left: i8, right: i8) -> (yes: bool) =
298    yes = left < right
299end less_i8
300less_i16: (left: i16, right: i16) -> (yes: bool) =
301    yes = left < right
302end less_i16
303less_i32: (left: i32, right: i32) -> (yes: bool) =
304    yes = left < right
305end less_i32
306less_i64: (left: i64, right: i64) -> (yes: bool) =
307    yes = left < right
308end less_i64
309less_isize: (left: isize, right: isize) -> (yes: bool) =
310    yes = left < right
311end less_isize
312--  Bytes order as `compare` orders them.
313less_bytes: (left: []u8, right: []u8) -> (yes: bool) =
314    yes = compare(left, right) == less
315end less_bytes
316u8 is ordered (less: less_u8)
317u16 is ordered (less: less_u16)
318u32 is ordered (less: less_u32)
319u64 is ordered (less: less_u64)
320usize is ordered (less: less_usize)
321i8 is ordered (less: less_i8)
322i16 is ordered (less: less_i16)
323i32 is ordered (less: less_i32)
324i64 is ordered (less: less_i64)
325isize is ordered (less: less_isize)
326[]u8 is ordered (less: less_bytes)
327
328--- The left bytes order before the right ones.
329public less: atom
330--- The two byte strings are the same bytes.
331public equal: atom
332--- The left bytes order after the right ones.
333public greater: atom
334--- How two byte strings order: `less`, `equal` or `greater`.
335public order: type = less | equal | greater
336
337--- Order two byte strings as unsigned bytes, the first difference
338--- deciding, and a string that is a proper prefix of the other ordering
339--- first: C's `memcmp` with the lengths taken into account, and the order
340--- `ls` gives names in the C locale.  `[]u8`'s `ordered` evidence is this
341--- order's `less`.
342public compare: (left: []u8, right: []u8) -> (result: order) =
343    mut at: usize = 0
344    while at < lenof left and at < lenof right do
345        if left[at] <> right[at] then
346            result = if left[at] < right[at] then less else greater end if
347            return
348        end if
349        inc at
350    end while
351    if lenof left < lenof right then
352        result = less
353    elsif lenof left > lenof right then
354        result = greater
355    else
356        result = equal
357    end if
358end compare