Landin library reference source

compiler/tests/fixtures/runtime/library-shared-cmp/main.ldn

1import core/cmp
2
3--  The shipped evidence answers through the concepts alone, without a map
4--  or a sort to carry it.
5same: (item: type is cmp.equatable, left: item, right: item) -> (yes: bool) =
6    yes = item.equal(left, right)
7end same
8
9digest: (item: type is cmp.hashable, value: item) -> (result: u64) =
10    result = item.hash(value)
11end digest
12
13before: (item: type is cmp.ordered, left: item, right: item) -> (yes: bool) =
14    yes = item.less(left, right)
15end before
16
17exercise: () -> (ok: bool) =
18    ok = false
19    bytes: []u8 = "ab"
20    copy: []u8 = "ab"
21    other: []u8 = "ba"
22    return when not same(u32(7), u32(7)) or same(true, false)
23    return when not same(bytes, copy) or same(bytes, other)
24    return when digest(u16(9)) <> 9 or digest(bytes) == digest(other)
25    ok = before(i32(-1), i32(0)) and not before(u8(2), u8(2))
26end exercise
27
28public main: () -> (code: i32) =
29    code = 1
30    ok := exercise()
31    if ok then
32        code = 42
33    end if
34end main