main.bend
bend-ml-bpe-tokenizer@0.1.3.0 · 658 lines · 72 definitions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17import Base
18import bend-ml-nat-lemmas@0.1.0.0/main.bend as NL
19
20
21type Tk is Data:
22 B{n: Nat}
23 M{k: Nat}
24
25
26type Rule is Data:
27 Rule{id: Nat, a: Tk, b: Tk}
28
29def Tk.eq(+x: Tk, +y: Tk) -> Bool:
30 match x y:
31 case B{+m} B{+n}:
32 Nat.is_eq(m, n)
33 case M{+i} M{+j}:
34 Nat.is_eq(i, j)
35 case B{m} M{j}:
36 False{}
37 case M{i} B{n}:
38 False{}
39
40
41def peek(+a: Tk, +b: Tk, xs: List<&2, Tk>) -> Bool:
42 match xs:
43 case Nil{}:
44 False{}
45 case Con{+x, t}:
46 match t:
47 case Nil{}:
48 False{}
49 case Con{+y, u}:
50 Bool.and(Tk.eq(x, a), Tk.eq(y, b))
51
52
53
54
55def merge.go(xs: List<&2, Tk>, hit: Bool, +a: Tk, +b: Tk, +c: Tk) -> List<&2, Tk>:
56 match xs hit:
57 case Con{x, Con{y, +t}} True{}:
58 c <> merge.go(t, peek(a, b, t), a, b, c)
59 case Con{x, Nil{}} True{}:
60 x <> Nil{}
61 case Con{x, +t} False{}:
62 x <> merge.go(t, peek(a, b, t), a, b, c)
63 case Nil{} _:
64 Nil{}
65
66def merge(+a: Tk, +b: Tk, +c: Tk, +xs: List<&2, Tk>) -> List<&2, Tk>:
67 merge.go(xs, peek(a, b, xs), a, b, c)
68
69
70
71
72
73def encode.go(todo: List<&2, Rule>, -done: List<&2, Rule>, xs: List<&2, Tk>) -> List<&2, Tk>:
74 match todo:
75 case Nil{}:
76 xs
77 case Con{+r, rest}:
78 match r:
79 case Rule{+i, +a, +b}:
80 encode.go(rest, Rule{i, a, b} <> done, merge(a, b, M{i}, xs))
81
82
83def encode(+table: List<&2, Rule>, xs: List<&2, Tk>) -> List<&2, Tk>:
84 encode.go(table, Nil{}, xs)
85
86
87def hits(table: List<&2, Rule>, +tk: Tk) -> Bool:
88 match table:
89 case Nil{}:
90 False{}
91 case Con{Rule{+i, a, b}, rest}:
92 match tk:
93 case B{n}:
94 False{}
95 case M{+j}:
96 Nat.is_eq(i, j)
97
98
99
100
101def exp.go(table: List<&2, Rule>, hit: Bool, tk: Tk) -> List<&2, Nat>:
102 match table hit tk:
103 case Nil{} _ B{n}:
104 n <> Nil{}
105 case Nil{} _ M{j}:
106 Nil{}
107 case Con{r, rest} _ B{n}:
108 n <> Nil{}
109 case Con{Rule{i, +a, +b}, +rest} True{} M{j}:
110 List.append(&2, Nat, exp.go(rest, hits(rest, a), a), exp.go(rest, hits(rest, b), b))
111 case Con{r, +rest} False{} M{+j}:
112 exp.go(rest, hits(rest, M{j}), M{j})
113
114def exp(+table: List<&2, Rule>, +tk: Tk) -> List<&2, Nat>:
115 exp.go(table, hits(table, tk), tk)
116
117
118def dec(+table: List<&2, Rule>, ids: List<&2, Tk>) -> List<&2, Nat>:
119 match ids:
120 case Nil{}:
121 Nil{}
122 case Con{+x, t}:
123 List.append(&2, Nat, exp(table, x), dec(table, t))
124
125def decode(+table: List<&2, Rule>, ids: List<&2, Tk>) -> List<&2, Nat>:
126 dec(List.reverse(&2, Rule, table), ids)
127
128
129def lift(bs: List<&2, Nat>) -> List<&2, Tk>:
130 match bs:
131 case Nil{}:
132 Nil{}
133 case Con{n, t}:
134 B{n} <> lift(t)
135
136
137
138
139def has(+D: List<&2, Rule>, +i: Nat) -> Bool:
140 match D:
141 case Nil{}:
142 False{}
143 case Con{Rule{+j, a, b}, t}:
144 Bool.or(Nat.is_eq(i, j), has(t, i))
145
146
147def kn(+D: List<&2, Rule>, tk: Tk) -> Bool:
148 match tk:
149 case B{n}:
150 True{}
151 case M{+j}:
152 has(D, j)
153
154def knl(+D: List<&2, Rule>, xs: List<&2, Tk>) -> Bool:
155 match xs:
156 case Nil{}:
157 True{}
158 case Con{x, t}:
159 Bool.and(kn(D, x), knl(D, t))
160
161
162def wf.go(todo: List<&2, Rule>, +done: List<&2, Rule>) -> Bool:
163 match todo:
164 case Nil{}:
165 True{}
166 case Con{Rule{+i, +a, +b}, rest}:
167 Bool.and(Bool.not(has(done, i)), wf.go(rest, Rule{i, a, b} <> done))
168
169def wf(table: List<&2, Rule>) -> Bool:
170 wf.go(table, Nil{})
171
172
173
174
175
176
177
178
179type Cnt is Data:
180 Cnt{a: Tk, b: Tk, n: Nat}
181
182
183def same_head(cs: List<&2, Cnt>, +a: Tk, +b: Tk) -> Bool:
184 match cs:
185 case Nil{}:
186 False{}
187 case Con{Cnt{+x, +y, k}, rest}:
188 Bool.and(Tk.eq(x, a), Tk.eq(y, b))
189
190
191def bump.go(cs: List<&2, Cnt>, hit: Bool, +a: Tk, +b: Tk) -> List<&2, Cnt>:
192 match cs hit:
193 case Nil{} _:
194 Cnt{a, b, 1n} <> Nil{}
195 case Con{Cnt{x, y, +k}, rest} True{}:
196 Cnt{x, y, 1n+k} <> rest
197 case Con{c, +rest} False{}:
198 c <> bump.go(rest, same_head(rest, a, b), a, b)
199
200def bump(+cs: List<&2, Cnt>, +a: Tk, +b: Tk) -> List<&2, Cnt>:
201 bump.go(cs, same_head(cs, a, b), a, b)
202
203def count.go(xs: List<&2, Tk>, cs: List<&2, Cnt>) -> List<&2, Cnt>:
204 match xs:
205 case Nil{}:
206 cs
207 case Con{+x, +t}:
208 match t:
209 case Nil{}:
210 cs
211 case Con{+y, u}:
212 count.go(Con{y, u}, bump(cs, x, y))
213
214def pick2(better: Bool, c: Cnt, cur: Cnt) -> Cnt:
215 match better:
216 case True{}:
217 c
218 case False{}:
219 cur
220
221def pick(c: Cnt, cur: Cnt) -> Cnt:
222 match c cur:
223 case Cnt{+a, +b, +k} Cnt{+x, +y, +m}:
224 pick2(Nat.is_lt(m, k), Cnt{a, b, k}, Cnt{x, y, m})
225
226def best.go(cs: List<&2, Cnt>, cur: Cnt) -> Cnt:
227 match cs:
228 case Nil{}:
229 cur
230 case Con{c, rest}:
231 best.go(rest, pick(c, cur))
232
233def best(cs: List<&2, Cnt>) -> Maybe<&1, Cnt>:
234 match cs:
235 case Nil{}:
236 None{}
237 case Con{c, rest}:
238 Some{best.go(rest, c)}
239
240
241def train.go(n: Nat, +next: Nat, +xs: List<&2, Tk>, m: Maybe<&1, Cnt>) -> List<&2, Rule>:
242 match n m:
243 case 0n _:
244 Nil{}
245 case 1n+p None{}:
246 Nil{}
247 case 1n+p Some{Cnt{+a, +b, k}}:
248 +ys = merge(a, b, M{next}, xs)
249 Rule{next, a, b} <> train.go(p, 1n+next, ys, best(count.go(ys, Nil{})))
250
251
252def train(n: Nat, +next: Nat, +xs: List<&2, Tk>) -> List<&2, Rule>:
253 train.go(n, next, xs, best(count.go(xs, Nil{})))
254
255
256
257
258
259
260def BD(b: Bool, t: Type, f: Type) -> Type:
261 match b:
262 case True{}:
263 t
264 case False{}:
265 f
266
267def nat_eq_sound(a: Nat, b: Nat, e: {True{} == Nat.is_eq(a, b) : Bool}) -> {a == b : Nat}:
268 match a b:
269 case 0n 0n:
270 {==}
271 case 0n 1n+q:
272 %e : BD(_, Unit, {0n == 1n+q : Nat})
273 Unit{}
274 case 1n+p 0n:
275 %e : BD(_, Unit, {1n+p == 0n : Nat})
276 Unit{}
277 case 1n+p 1n+q:
278 %nat_eq_sound(p, q, e) : {1n+p == 1n+_ : Nat}
279 {==}
280
281def nat_eq_refl(a: Nat) -> {True{} == Nat.is_eq(a, a) : Bool}:
282 match a:
283 case 0n:
284 {==}
285 case 1n+p:
286 nat_eq_refl(p)
287
288def and_true(p: Bool, q: Bool, e: {True{} == Bool.and(p, q) : Bool}) -> {True{} == p : Bool} & {True{} == q : Bool}:
289 match p:
290 case True{}:
291 ({==}, e)
292 case False{}:
293 %e : BD(_, Unit, {True{} == False{} : Bool} & {True{} == q : Bool})
294 Unit{}
295
296def and_intro(p: Bool, q: Bool, ep: {True{} == p : Bool}, eq: {True{} == q : Bool}) -> {True{} == Bool.and(p, q) : Bool}:
297 match p:
298 case True{}:
299 eq
300 case False{}:
301 %ep : BD(_, Unit, {True{} == False{} : Bool})
302 Unit{}
303
304def or_left(p: Bool, q: Bool, ep: {True{} == p : Bool}) -> {True{} == Bool.or(p, q) : Bool}:
305 match p:
306 case True{}:
307 {==}
308 case False{}:
309 %ep : BD(_, Unit, {True{} == q : Bool})
310 Unit{}
311
312def or_right(p: Bool, q: Bool, eq: {True{} == q : Bool}) -> {True{} == Bool.or(p, q) : Bool}:
313 match p:
314 case True{}:
315 {==}
316 case False{}:
317 eq
318
319
320def not_true(p: Bool, e: {True{} == Bool.not(p) : Bool}) -> {False{} == p : Bool}:
321 match p:
322 case True{}:
323 %e : BD(_, Unit, {False{} == True{} : Bool})
324 Unit{}
325 case False{}:
326 {==}
327
328def tk_eq_sound(+x: Tk, +y: Tk, e: {True{} == Tk.eq(x, y) : Bool}) -> {x == y : Tk}:
329 match x y:
330 case B{+m} B{+n}:
331 %nat_eq_sound(m, n, e) : {B{m} == B{_} : Tk}
332 {==}
333 case M{+i} M{+j}:
334 %nat_eq_sound(i, j, e) : {M{i} == M{_} : Tk}
335 {==}
336 case B{m} M{j}:
337 %e : BD(_, Unit, {B{m} == M{j} : Tk})
338 Unit{}
339 case M{i} B{n}:
340 %e : BD(_, Unit, {M{i} == B{n} : Tk})
341 Unit{}
342
343
344
345def ext_m(+D: List<&2, Rule>, +i: Nat, +a: Tk, +b: Tk, +j: Nat, h: Bool, eh: {h == Nat.is_eq(i, j) : Bool}, nf: {True{} == Bool.not(has(D, i)) : Bool}, kt: {True{} == has(D, j) : Bool}) -> {exp(Rule{i, a, b} <> D, M{j}) == exp(D, M{j}) : List<&2, Nat>}:
346 match h:
347 case True{}:
348
349 +ij = nat_eq_sound(i, j, eh)
350 +hij = Equal.cong(Nat, Bool, k => has(D, k), i, j, ij)
351 +f1 = Equal.trans(Bool, False{}, has(D, i), has(D, j), not_true(has(D, i), nf), hij)
352 +tf = Equal.trans(Bool, True{}, has(D, j), False{}, kt, Equal.sym(Bool, False{}, has(D, j), f1))
353 %tf : BD(_, Unit, {exp.go(Rule{i, a, b} <> D, Cmp.is_eq(Nat.cmp(i, j)), M{j}) == exp.go(D, hits(D, M{j}), M{j}) : List<&2, Nat>})
354 Unit{}
355 case False{}:
356 %eh : {exp.go(Rule{i, a, b} <> D, _, M{j}) == exp(D, M{j}) : List<&2, Nat>}
357 {==}
358
359def and_true_l(p: Bool, q: Bool, e: {True{} == Bool.and(p, q) : Bool}) -> {True{} == p : Bool}:
360 match p:
361 case True{}:
362 {==}
363 case False{}:
364 %e : BD(_, Unit, {True{} == False{} : Bool})
365 Unit{}
366
367def and_true_r(p: Bool, q: Bool, e: {True{} == Bool.and(p, q) : Bool}) -> {True{} == q : Bool}:
368 match p:
369 case True{}:
370 e
371 case False{}:
372 %e : BD(_, Unit, {True{} == q : Bool})
373 Unit{}
374
375
376def ext_tok(+D: List<&2, Rule>, +i: Nat, +a: Tk, +b: Tk, t: Tk, kt: {True{} == kn(D, t) : Bool}, nf: {True{} == Bool.not(has(D, i)) : Bool}) -> {exp(Rule{i, a, b} <> D, t) == exp(D, t) : List<&2, Nat>}:
377 match D t:
378 case Nil{} B{n}:
379 {==}
380 case Con{Rule{j, a2, b2}, rest} B{n}:
381 {==}
382 case _ M{+j}:
383 ext_m(D, i, a, b, j, Nat.is_eq(i, j), {==}, nf, kt)
384
385def ext_dec.step(+D: List<&2, Rule>, +i: Nat, +a: Tk, +b: Tk, +x: Tk, +t: List<&2, Tk>, k1: {True{} == kn(D, x) : Bool}, nf: {True{} == Bool.not(has(D, i)) : Bool}, ih: {dec(Rule{i, a, b} <> D, t) == dec(D, t) : List<&2, Nat>}) -> {dec(Rule{i, a, b} <> D, x <> t) == dec(D, x <> t) : List<&2, Nat>}:
386 %ext_tok(D, i, a, b, x, k1, nf) : {List.append(&2, Nat, exp(Rule{i, a, b} <> D, x), dec(Rule{i, a, b} <> D, t)) == List.append(&2, Nat, _, dec(D, t)) : List<&2, Nat>}
387 %ih : {List.append(&2, Nat, exp(Rule{i, a, b} <> D, x), dec(Rule{i, a, b} <> D, t)) == List.append(&2, Nat, exp(Rule{i, a, b} <> D, x), _) : List<&2, Nat>}
388 {==}
389
390
391def ext_dec(+D: List<&2, Rule>, +i: Nat, +a: Tk, +b: Tk, xs: List<&2, Tk>, +kx: {True{} == knl(D, xs) : Bool}, +nf: {True{} == Bool.not(has(D, i)) : Bool}) -> {dec(Rule{i, a, b} <> D, xs) == dec(D, xs) : List<&2, Nat>}:
392 match xs:
393 case Nil{}:
394 {==}
395 case Con{+x, +t}:
396 ext_dec.step(D, i, a, b, x, t, and_true_l(kn(D, x), knl(D, t), kx), nf, ext_dec(D, i, a, b, t, and_true_r(kn(D, x), knl(D, t), kx), nf))
397
398
399
400def exp_new(+D: List<&2, Rule>, +i: Nat, +a: Tk, +b: Tk, h: Bool, eh: {h == Nat.is_eq(i, i) : Bool}) -> {exp(Rule{i, a, b} <> D, M{i}) == List.append(&2, Nat, exp(D, a), exp(D, b)) : List<&2, Nat>}:
401 match h:
402 case True{}:
403 %eh : {exp.go(Rule{i, a, b} <> D, _, M{i}) == List.append(&2, Nat, exp(D, a), exp(D, b)) : List<&2, Nat>}
404 {==}
405 case False{}:
406 +f = Equal.trans(Bool, True{}, Nat.is_eq(i, i), False{}, nat_eq_refl(i), Equal.sym(Bool, False{}, Nat.is_eq(i, i), eh))
407 %f : BD(_, Unit, {exp(Rule{i, a, b} <> D, M{i}) == List.append(&2, Nat, exp(D, a), exp(D, b)) : List<&2, Nat>})
408 Unit{}
409
410
411
412def mp_core(+D: List<&2, Rule>, +i: Nat, +a: Tk, +b: Tk, +t: List<&2, Tk>, +m: List<&2, Tk>, ka: {True{} == kn(D, a) : Bool}, kb: {True{} == kn(D, b) : Bool}, +nf: {True{} == Bool.not(has(D, i)) : Bool}, ihm: {dec(Rule{i, a, b} <> D, m) == dec(Rule{i, a, b} <> D, t) : List<&2, Nat>}) -> {dec(Rule{i, a, b} <> D, M{i} <> m) == dec(Rule{i, a, b} <> D, a <> b <> t) : List<&2, Nat>}:
413 %Equal.sym(List<&2, Nat>, exp(Rule{i, a, b} <> D, M{i}), List.append(&2, Nat, exp(D, a), exp(D, b)), exp_new(D, i, a, b, Nat.is_eq(i, i), {==})) : {List.append(&2, Nat, _, dec(Rule{i, a, b} <> D, m)) == List.append(&2, Nat, exp(Rule{i, a, b} <> D, a), List.append(&2, Nat, exp(Rule{i, a, b} <> D, b), dec(Rule{i, a, b} <> D, t))) : List<&2, Nat>}
414 %Equal.sym(List<&2, Nat>, dec(Rule{i, a, b} <> D, m), dec(Rule{i, a, b} <> D, t), ihm) : {List.append(&2, Nat, List.append(&2, Nat, exp(D, a), exp(D, b)), _) == List.append(&2, Nat, exp(Rule{i, a, b} <> D, a), List.append(&2, Nat, exp(Rule{i, a, b} <> D, b), dec(Rule{i, a, b} <> D, t))) : List<&2, Nat>}
415 %Equal.sym(List<&2, Nat>, exp(Rule{i, a, b} <> D, a), exp(D, a), ext_tok(D, i, a, b, a, ka, nf)) : {List.append(&2, Nat, List.append(&2, Nat, exp(D, a), exp(D, b)), dec(Rule{i, a, b} <> D, t)) == List.append(&2, Nat, _, List.append(&2, Nat, exp(Rule{i, a, b} <> D, b), dec(Rule{i, a, b} <> D, t))) : List<&2, Nat>}
416 %Equal.sym(List<&2, Nat>, exp(Rule{i, a, b} <> D, b), exp(D, b), ext_tok(D, i, a, b, b, kb, nf)) : {List.append(&2, Nat, List.append(&2, Nat, exp(D, a), exp(D, b)), dec(Rule{i, a, b} <> D, t)) == List.append(&2, Nat, exp(D, a), List.append(&2, Nat, _, dec(Rule{i, a, b} <> D, t))) : List<&2, Nat>}
417 NL.append_assoc(Nat, exp(D, a), exp(D, b), dec(Rule{i, a, b} <> D, t))
418
419
420def kn_sub(+D: List<&2, Rule>, +x: Tk, +a: Tk, ex: {x == a : Tk}, kx: {True{} == kn(D, x) : Bool}) -> {True{} == kn(D, a) : Bool}:
421 %ex : {True{} == kn(D, _) : Bool}
422 kx
423
424
425def mp_false(+D: List<&2, Rule>, +i: Nat, +a: Tk, +b: Tk, +x: Tk, +t: List<&2, Tk>, +mg: List<&2, Tk>, ih: {dec(Rule{i, a, b} <> D, mg) == dec(Rule{i, a, b} <> D, t) : List<&2, Nat>}) -> {dec(Rule{i, a, b} <> D, x <> mg) == dec(Rule{i, a, b} <> D, x <> t) : List<&2, Nat>}:
426 %ih : {List.append(&2, Nat, exp(Rule{i, a, b} <> D, x), dec(Rule{i, a, b} <> D, mg)) == List.append(&2, Nat, exp(Rule{i, a, b} <> D, x), _) : List<&2, Nat>}
427 {==}
428
429
430def mp_hit(+D: List<&2, Rule>, +i: Nat, +a: Tk, +b: Tk, +x: Tk, +y: Tk, +t: List<&2, Tk>, +m: List<&2, Tk>, +ex: {x == a : Tk}, +ey: {y == b : Tk}, +kxx: {True{} == kn(D, x) : Bool}, +kyy: {True{} == kn(D, y) : Bool}, +nf: {True{} == Bool.not(has(D, i)) : Bool}, ihm: {dec(Rule{i, a, b} <> D, m) == dec(Rule{i, a, b} <> D, t) : List<&2, Nat>}) -> {dec(Rule{i, a, b} <> D, M{i} <> m) == dec(Rule{i, a, b} <> D, x <> y <> t) : List<&2, Nat>}:
431 %Equal.sym(Tk, x, a, ex) : {dec(Rule{i, a, b} <> D, M{i} <> m) == dec(Rule{i, a, b} <> D, _ <> y <> t) : List<&2, Nat>}
432 %Equal.sym(Tk, y, b, ey) : {dec(Rule{i, a, b} <> D, M{i} <> m) == dec(Rule{i, a, b} <> D, a <> _ <> t) : List<&2, Nat>}
433 mp_core(D, i, a, b, t, m, kn_sub(D, x, a, ex, kxx), kn_sub(D, y, b, ey, kyy), nf, ihm)
434
435
436def mp(+D: List<&2, Rule>, +i: Nat, +a: Tk, +b: Tk, xs: List<&2, Tk>, hit: Bool, +eh: {hit == peek(a, b, xs) : Bool}, +kx: {True{} == knl(D, xs) : Bool}, +nf: {True{} == Bool.not(has(D, i)) : Bool}) -> {dec(Rule{i, a, b} <> D, merge.go(xs, hit, a, b, M{i})) == dec(Rule{i, a, b} <> D, xs) : List<&2, Nat>}:
437 match xs hit:
438 case Nil{} _:
439 {==}
440 case Con{x, Nil{}} True{}:
441 %eh : BD(_, Unit, {dec(Rule{i, a, b} <> D, merge.go(x <> Nil{}, True{}, a, b, M{i})) == dec(Rule{i, a, b} <> D, x <> Nil{}) : List<&2, Nat>})
442 Unit{}
443 case Con{+x, Con{+y, +t}} True{}:
444 mp_hit(D, i, a, b, x, y, t, merge.go(t, peek(a, b, t), a, b, M{i}), tk_eq_sound(x, a, and_true_l(Tk.eq(x, a), Tk.eq(y, b), eh)), tk_eq_sound(y, b, and_true_r(Tk.eq(x, a), Tk.eq(y, b), eh)), and_true_l(kn(D, x), Bool.and(kn(D, y), knl(D, t)), kx), and_true_l(kn(D, y), knl(D, t), and_true_r(kn(D, x), Bool.and(kn(D, y), knl(D, t)), kx)), nf, mp(D, i, a, b, t, peek(a, b, t), {==}, and_true_r(kn(D, y), knl(D, t), and_true_r(kn(D, x), Bool.and(kn(D, y), knl(D, t)), kx)), nf))
445 case Con{+x, +t} False{}:
446 mp_false(D, i, a, b, x, t, merge.go(t, peek(a, b, t), a, b, M{i}), mp(D, i, a, b, t, peek(a, b, t), {==}, and_true_r(kn(D, x), knl(D, t), kx), nf))
447
448
449
450
451def kn_mono(+D: List<&2, Rule>, +i: Nat, +a: Tk, +b: Tk, t: Tk, kt: {True{} == kn(D, t) : Bool}) -> {True{} == kn(Rule{i, a, b} <> D, t) : Bool}:
452 match t:
453 case B{n}:
454 {==}
455 case M{+j}:
456 or_right(Nat.is_eq(j, i), has(D, j), kt)
457
458
459def kn_new(+D: List<&2, Rule>, +i: Nat, +a: Tk, +b: Tk) -> {True{} == kn(Rule{i, a, b} <> D, M{i}) : Bool}:
460 or_left(Nat.is_eq(i, i), has(D, i), nat_eq_refl(i))
461
462def kp(+D: List<&2, Rule>, +i: Nat, +a: Tk, +b: Tk, xs: List<&2, Tk>, hit: Bool, +eh: {hit == peek(a, b, xs) : Bool}, +kx: {True{} == knl(D, xs) : Bool}) -> {True{} == knl(Rule{i, a, b} <> D, merge.go(xs, hit, a, b, M{i})) : Bool}:
463 match xs hit:
464 case Nil{} _:
465 {==}
466 case Con{x, Nil{}} True{}:
467 %eh : BD(_, Unit, {True{} == knl(Rule{i, a, b} <> D, merge.go(x <> Nil{}, True{}, a, b, M{i})) : Bool})
468 Unit{}
469 case Con{+x, Con{+y, +t}} True{}:
470 and_intro(kn(Rule{i, a, b} <> D, M{i}), knl(Rule{i, a, b} <> D, merge.go(t, peek(a, b, t), a, b, M{i})), kn_new(D, i, a, b), kp(D, i, a, b, t, peek(a, b, t), {==}, and_true_r(kn(D, y), knl(D, t), and_true_r(kn(D, x), Bool.and(kn(D, y), knl(D, t)), kx))))
471 case Con{+x, +t} False{}:
472 and_intro(kn(Rule{i, a, b} <> D, x), knl(Rule{i, a, b} <> D, merge.go(t, peek(a, b, t), a, b, M{i})), kn_mono(D, i, a, b, x, and_true_l(kn(D, x), knl(D, t), kx)), kp(D, i, a, b, t, peek(a, b, t), {==}, and_true_r(kn(D, x), knl(D, t), kx)))
473
474
475
476
477def enc_known(todo: List<&2, Rule>, +done: List<&2, Rule>, +xs: List<&2, Tk>, +wfe: {True{} == wf.go(todo, done) : Bool}, +kx: {True{} == knl(done, xs) : Bool}) -> {True{} == knl(List.reverse.go(&2, Rule, todo, done), encode.go(todo, done, xs)) : Bool}:
478 match todo:
479 case Nil{}:
480 kx
481 case Con{Rule{+i, +a, +b}, +rest}:
482 enc_known(rest, Rule{i, a, b} <> done, merge.go(xs, peek(a, b, xs), a, b, M{i}), and_true_r(Bool.not(has(done, i)), wf.go(rest, Rule{i, a, b} <> done), wfe), kp(done, i, a, b, xs, peek(a, b, xs), {==}, kx))
483
484
485def enc_dec(todo: List<&2, Rule>, +done: List<&2, Rule>, +xs: List<&2, Tk>, +wfe: {True{} == wf.go(todo, done) : Bool}, +kx: {True{} == knl(done, xs) : Bool}) -> {dec(List.reverse.go(&2, Rule, todo, done), encode.go(todo, done, xs)) == dec(done, xs) : List<&2, Nat>}:
486 match todo:
487 case Nil{}:
488 {==}
489 case Con{Rule{+i, +a, +b}, +rest}:
490 Equal.trans(List<&2, Nat>, dec(List.reverse.go(&2, Rule, rest, Rule{i, a, b} <> done), encode.go(rest, Rule{i, a, b} <> done, merge.go(xs, peek(a, b, xs), a, b, M{i}))), dec(Rule{i, a, b} <> done, xs), dec(done, xs), Equal.trans(List<&2, Nat>, dec(List.reverse.go(&2, Rule, rest, Rule{i, a, b} <> done), encode.go(rest, Rule{i, a, b} <> done, merge.go(xs, peek(a, b, xs), a, b, M{i}))), dec(Rule{i, a, b} <> done, merge.go(xs, peek(a, b, xs), a, b, M{i})), dec(Rule{i, a, b} <> done, xs), enc_dec(rest, Rule{i, a, b} <> done, merge.go(xs, peek(a, b, xs), a, b, M{i}), and_true_r(Bool.not(has(done, i)), wf.go(rest, Rule{i, a, b} <> done), wfe), kp(done, i, a, b, xs, peek(a, b, xs), {==}, kx)), mp(done, i, a, b, xs, peek(a, b, xs), {==}, kx, and_true_l(Bool.not(has(done, i)), wf.go(rest, Rule{i, a, b} <> done), wfe))), ext_dec(done, i, a, b, xs, kx, and_true_l(Bool.not(has(done, i)), wf.go(rest, Rule{i, a, b} <> done), wfe)))
491
492
493
494
495def lift_dec(bs: List<&2, Nat>) -> {dec(Nil{}, lift(bs)) == bs : List<&2, Nat>}:
496 match bs:
497 case Nil{}:
498 {==}
499 case Con{+n, +t}:
500 %lift_dec(t) : {n <> dec(Nil{}, lift(t)) == n <> _ : List<&2, Nat>}
501 {==}
502
503def lift_known(bs: List<&2, Nat>) -> {True{} == knl(Nil{}, lift(bs)) : Bool}:
504 match bs:
505 case Nil{}:
506 {==}
507 case Con{n, t}:
508 lift_known(t)
509
510
511
512
513
514
515
516law roundtrip:
517 for +table: List<&2, Rule>
518 for +bs: List<&2, Nat>
519 for +w: {True{} == wf(table) : Bool}
520 {decode(table, encode(table, lift(bs))) == bs : List<&2, Nat>}
521
522def roundtrip(table, bs, w):
523 Equal.trans(List<&2, Nat>, decode(table, encode(table, lift(bs))), dec(Nil{}, lift(bs)), bs, enc_dec(table, Nil{}, lift(bs), w, lift_known(bs)), lift_dec(bs))
524
525
526
527law vocab_bound:
528 for +table: List<&2, Rule>
529 for +bs: List<&2, Nat>
530 for +w: {True{} == wf(table) : Bool}
531 {True{} == knl(List.reverse(&2, Rule, table), encode(table, lift(bs))) : Bool}
532
533def vocab_bound(table, bs, w):
534 enc_known(table, Nil{}, lift(bs), w, lift_known(bs))
535
536
537
538law dec_append:
539 for +D: List<&2, Rule>
540 for xs: List<&2, Tk>
541 for +ys: List<&2, Tk>
542 {dec(D, List.append(&2, Tk, xs, ys)) == List.append(&2, Nat, dec(D, xs), dec(D, ys)) : List<&2, Nat>}
543
544def dec_append(D, xs, ys):
545 match xs:
546 case Nil{}:
547 {==}
548 case Con{+x, +t}:
549 %Equal.sym(List<&2, Nat>, dec(D, List.append(&2, Tk, t, ys)), List.append(&2, Nat, dec(D, t), dec(D, ys)), dec_append(D, t, ys)) : {List.append(&2, Nat, exp(D, x), _) == List.append(&2, Nat, List.append(&2, Nat, exp(D, x), dec(D, t)), dec(D, ys)) : List<&2, Nat>}
550 Equal.sym(List<&2, Nat>, List.append(&2, Nat, List.append(&2, Nat, exp(D, x), dec(D, t)), dec(D, ys)), List.append(&2, Nat, exp(D, x), List.append(&2, Nat, dec(D, t), dec(D, ys))), NL.append_assoc(Nat, exp(D, x), dec(D, t), dec(D, ys)))
551
552
553
554
555
556
557
558
559
560
561
562def lt_succ(n: Nat) -> {True{} == Nat.is_lt(n, 1n+n) : Bool}:
563 match n:
564 case 0n:
565 {==}
566 case 1n+p:
567 lt_succ(p)
568
569
570def lt_ne(j: Nat, i: Nat, e: {True{} == Nat.is_lt(j, i) : Bool}) -> {False{} == Nat.is_eq(i, j) : Bool}:
571 match j i:
572 case 0n 0n:
573 %e : BD(_, Unit, {False{} == Nat.is_eq(0n, 0n) : Bool})
574 Unit{}
575 case 0n 1n+q:
576 {==}
577 case 1n+p 0n:
578 %e : BD(_, Unit, {False{} == Nat.is_eq(0n, 1n+p) : Bool})
579 Unit{}
580 case 1n+p 1n+q:
581 lt_ne(p, q, e)
582
583
584def lt_up(j: Nat, i: Nat, e: {True{} == Nat.is_lt(j, i) : Bool}) -> {True{} == Nat.is_lt(j, 1n+i) : Bool}:
585 match j i:
586 case 0n 0n:
587 %e : BD(_, Unit, {True{} == Nat.is_lt(0n, 1n) : Bool})
588 Unit{}
589 case 0n 1n+q:
590 {==}
591 case 1n+p 0n:
592 %e : BD(_, Unit, {True{} == Nat.is_lt(1n+p, 1n) : Bool})
593 Unit{}
594 case 1n+p 1n+q:
595 lt_up(p, q, e)
596
597
598def below(D: List<&2, Rule>, +i: Nat) -> Bool:
599 match D:
600 case Nil{}:
601 True{}
602 case Con{Rule{+j, a, b}, t}:
603 Bool.and(Nat.is_lt(j, i), below(t, i))
604
605def below_has(D: List<&2, Rule>, +i: Nat, +e: {True{} == below(D, i) : Bool}) -> {False{} == has(D, i) : Bool}:
606 match D:
607 case Nil{}:
608 {==}
609 case Con{Rule{+j, a, b}, +t}:
610 %lt_ne(j, i, and_true_l(Nat.is_lt(j, i), below(t, i), e)) : {False{} == Bool.or(_, has(t, i)) : Bool}
611 below_has(t, i, and_true_r(Nat.is_lt(j, i), below(t, i), e))
612
613def below_up(D: List<&2, Rule>, +i: Nat, +e: {True{} == below(D, i) : Bool}) -> {True{} == below(D, 1n+i) : Bool}:
614 match D:
615 case Nil{}:
616 {==}
617 case Con{Rule{+j, a, b}, +t}:
618 and_intro(Nat.is_lt(j, 1n+i), below(t, 1n+i), lt_up(j, i, and_true_l(Nat.is_lt(j, i), below(t, i), e)), below_up(t, i, and_true_r(Nat.is_lt(j, i), below(t, i), e)))
619
620
621def below_cons(+a: Tk, +b: Tk, +D: List<&2, Rule>, +i: Nat, +e: {True{} == below(D, i) : Bool}) -> {True{} == below(Rule{i, a, b} <> D, 1n+i) : Bool}:
622 and_intro(Nat.is_lt(i, 1n+i), below(D, 1n+i), lt_succ(i), below_up(D, i, e))
623
624
625def below_fresh(+D: List<&2, Rule>, +i: Nat, +e: {True{} == below(D, i) : Bool}) -> {True{} == Bool.not(has(D, i)) : Bool}:
626 %below_has(D, i, e) : {True{} == Bool.not(_) : Bool}
627 {==}
628
629
630def tw(n: Nat, +next: Nat, +xs: List<&2, Tk>, m: Maybe<&1, Cnt>, +done: List<&2, Rule>, +bl: {True{} == below(done, next) : Bool}) -> {True{} == wf.go(train.go(n, next, xs, m), done) : Bool}:
631 match n m:
632 case 0n _:
633 {==}
634 case 1n++p None{}:
635 {==}
636 case 1n++p Some{Cnt{+a, +b, k}}:
637 and_intro(Bool.not(has(done, next)), wf.go(train.go(p, 1n+next, merge(a, b, M{next}, xs), best(count.go(merge(a, b, M{next}, xs), Nil{}))), Rule{next, a, b} <> done), below_fresh(done, next, bl), tw(p, 1n+next, merge(a, b, M{next}, xs), best(count.go(merge(a, b, M{next}, xs), Nil{})), Rule{next, a, b} <> done, below_cons(a, b, done, next, bl)))
638
639
640law train_wf:
641 for n: Nat
642 for +next: Nat
643 for +xs: List<&2, Tk>
644 {True{} == wf(train(n, next, xs)) : Bool}
645
646def train_wf(n, next, xs):
647 tw(n, next, xs, best(count.go(xs, Nil{})), Nil{}, {==})
648
649
650
651law roundtrip_trained:
652 for +n: Nat
653 for +corpus: List<&2, Nat>
654 for +bs: List<&2, Nat>
655 {decode(train(n, 0n, lift(corpus)), encode(train(n, 0n, lift(corpus)), lift(bs))) == bs : List<&2, Nat>}
656
657def roundtrip_trained(n, corpus, bs):
658 roundtrip(train(n, 0n, lift(corpus)), bs, train_wf(n, 0n, lift(corpus)))