src/Keys.bend
mylsm-lsm-store@0.6.0.1 · 135 lines · 22 definitions
1import Base
2import bend-mathlib@0.7.5.0/string.bend as MString
3import bend-mathlib@0.7.5.0/bool.bend as MBool
4import bend-kit-bytes@0.3.2.0/bytes.bend as PackedBytes
5
6def Bytes.cmp.order(pair: PackedBytes.Bytes & PackedBytes.Bytes & Cmp) -> Cmp:
7 match pair:
8 case (_, _, order):
9 order
10
11def Bytes.cmp.with_inputs(
12 left: PackedBytes.Bytes,
13 right: PackedBytes.Bytes
14) -> PackedBytes.Bytes & PackedBytes.Bytes & Cmp:
15 PackedBytes.cmp(left, right)
16
17def Bytes.cmp(left: PackedBytes.Bytes, right: PackedBytes.Bytes) -> Cmp:
18 Bytes.cmp.order(Bytes.cmp.with_inputs(left, right))
19
20def Bytes.eq(left: PackedBytes.Bytes, right: PackedBytes.Bytes) -> Bool:
21 Cmp.is_eq(Bytes.cmp(left, right))
22
23
24
25
26def unpack(pair: (String & String) & Cmp) -> Cmp:
27 match pair:
28 case ((_, _), c):
29 c
30
31
32def cmp(+s1: String, +s2: String) -> Cmp:
33 unpack(String.cmp(s1, s2))
34
35
36def eq(+s1: String, +s2: String) -> Bool:
37 String.eq(s1, s2)
38
39
40def cmp_is_lt(ord: Cmp) -> Bool:
41 Cmp.is_lt(ord)
42
43
44def cmp_is_eq(ord: Cmp) -> Bool:
45 Cmp.is_eq(ord)
46
47
48def cmp_is_gt(ord: Cmp) -> Bool:
49 Cmp.is_gt(ord)
50
51
52def cmp_is_le(ord: Cmp) -> Bool:
53 Cmp.is_le(ord)
54
55
56def lt(+s1: String, +s2: String) -> Bool:
57 cmp_is_lt(cmp(s1, s2))
58
59
60def le(+s1: String, +s2: String) -> Bool:
61 cmp_is_le(cmp(s1, s2))
62
63
64def implies(s1: Bool, s2: Bool) -> Bool:
65 match s1:
66 case False{}:
67 True{}
68 case True{}:
69 s2
70
71
72def one_hot3(first: Bool, second: Bool, third: Bool) -> Bool:
73 match first second third:
74 case True{} False{} False{}:
75 True{}
76 case False{} True{} False{}:
77 True{}
78 case False{} False{} True{}:
79 True{}
80 case _ _ _:
81 False{}
82
83
84def cmp_one_hot(+ord: Cmp) -> Bool:
85 one_hot3(cmp_is_lt(ord), cmp_is_eq(ord), cmp_is_gt(ord))
86
87
88def cmp_one_hot_proof(+ord: Cmp) -> {cmp_one_hot(ord) == True{} : Bool}:
89 match ord:
90 case LT{}:
91 {==}
92 case EQ{}:
93 {==}
94 case GT{}:
95 {==}
96
97
98
99
100def cmp_eq_pair(
101 pair: (String & String) & Cmp
102) -> {cmp_is_eq(unpack(pair)) == Cmp.is_eq(Pair.snd(String & String, Cmp, pair)) : Bool}:
103 match pair:
104 case ((a, b), c):
105 match c:
106 case LT{}:
107 {==}
108 case EQ{}:
109 {==}
110 case GT{}:
111 {==}
112
113
114def cmp_eq_bridge(+s1: String, +s2: String) -> {cmp_is_eq(cmp(s1, s2)) == eq(s1, s2) : Bool}:
115 %MString.internal_order_cmp(s1, s2) : {cmp_is_eq(cmp(s1, s2)) == Cmp.is_eq(_) : Bool}
116 cmp_eq_pair(String.cmp(s1, s2))
117
118
119
120
121
122
123
124
125def s_cmp_pair_refl(+str: String) -> {String.cmp(str, str) == ((str, str), EQ{}) : (String & String) & Cmp}:
126 MString.cmp_refl(str)
127
128
129def str_refl(+str: String) -> {cmp(str, str) == EQ{} : Cmp}:
130 %Equal.sym((String & String) & Cmp, String.cmp(str, str), ((str, str), EQ{}), s_cmp_pair_refl(str)) : {unpack(_) == EQ{} : Cmp}
131 {==}
132
133
134def str_eq_refl(+str: String) -> {eq(str, str) == True{} : Bool}:
135 MString.eq_refl(str)