# bend-bpe-tokenizer: the proofs of LAWS.bend. # The literal laws compute. split_partition, bpe_conserves and added_partition go by induction on the fuel: each step # cuts the text with take and drop (or joins two neighbours), and take_append_drop / append_assoc put it back. import Base import bend-mathlib@0.8.1.0/list.bend as ML import ./utf8.bend as U import ./split.bend as S import ./bpe.bend as B import ./hmap.bend as H import ./specials.bend as A import ./tok.bend as TK import ./LAWS.bend as Laws # ---- split_partition ---- # a step: the piece x <> take(t, n) and the rest's pieces, drop(t, n), concatenate to x <> t def split_cat(fuel: Nat, +fam: U32, xs: List<&2, S.CC>) -> {List.concat(&2, S.CC, S.split_go(fuel, fam, xs)) == xs : List<&2, S.CC>}: match fuel xs: case _ Nil{}: {==} case 0n x <> t: ML.append_nil(&2, S.CC, x <> t) case 1n+f +x <> +t: %Equal.sym(List<&2, S.CC>, List.concat(&2, S.CC, S.split_go(f, fam, List.drop(&2, S.CC, t, S.more(fam, x, t)))), List.drop(&2, S.CC, t, S.more(fam, x, t)), split_cat(f, fam, List.drop(&2, S.CC, t, S.more(fam, x, t)))) : {x <> List.append(&2, S.CC, List.take(&2, S.CC, t, S.more(fam, x, t)), _) == x <> t : List<&2, S.CC>} %Equal.sym(List<&2, S.CC>, List.append(&2, S.CC, List.take(&2, S.CC, t, S.more(fam, x, t)), List.drop(&2, S.CC, t, S.more(fam, x, t))), t, ML.take_append_drop(&2, S.CC, t, S.more(fam, x, t))) : {x <> _ == x <> t : List<&2, S.CC>} {==} def Laws.split_partition(fam, xs): split_cat(List.length(&2, S.CC, xs), fam, xs) # ---- bpe_conserves ---- def merge_head_cat(a: List<&2, U32>, t: List<&2, List<&2, U32>>) -> {List.concat(&2, U32, B.merge_head(a, t)) == List.append(&2, U32, a, List.concat(&2, U32, t)) : List<&2, U32>}: match t: case []: {==} case b <> t2: ML.append_assoc(&2, U32, a, b, List.concat(&2, U32, t2)) # joining the pair at i keeps the concatenation def merge_cat(ps: List<&2, List<&2, U32>>, i: Nat) -> {List.concat(&2, U32, B.merge_at(ps, i)) == List.concat(&2, U32, ps) : List<&2, U32>}: match ps i: case Nil{} _: {==} case a <> t 0n: merge_head_cat(a, t) case +a <> +t 1n+ +j: %Equal.sym(List<&2, U32>, List.concat(&2, U32, B.merge_at(t, j)), List.concat(&2, U32, t), merge_cat(t, j)) : {List.append(&2, U32, a, _) == List.append(&2, U32, a, List.concat(&2, U32, t)) : List<&2, U32>} {==} def bpe_if_cat(z: Bool, +at: Nat, +f: Nat, +ranks: H.HM, +ps: List<&2, List<&2, U32>>, ih: @x:List<&2, List<&2, U32>> -> {List.concat(&2, U32, B.bpe_go(f, ranks, x)) == List.concat(&2, U32, x) : List<&2, U32>}) -> {List.concat(&2, U32, B.bpe_if(z, at, ps, x => B.bpe_go(f, ranks, x))) == List.concat(&2, U32, ps) : List<&2, U32>}: match z: case True{}: {==} case False{}: Equal.trans(List<&2, U32>, List.concat(&2, U32, B.bpe_go(f, ranks, B.merge_at(ps, at))), List.concat(&2, U32, B.merge_at(ps, at)), List.concat(&2, U32, ps), ih(B.merge_at(ps, at)), merge_cat(ps, at)) def bpe_next_cat(b: B.BR, +f: Nat, +ranks: H.HM, +ps: List<&2, List<&2, U32>>, ih: @x:List<&2, List<&2, U32>> -> {List.concat(&2, U32, B.bpe_go(f, ranks, x)) == List.concat(&2, U32, x) : List<&2, U32>}) -> {List.concat(&2, U32, B.bpe_next(b, ps, x => B.bpe_go(f, ranks, x))) == List.concat(&2, U32, ps) : List<&2, U32>}: match b: case B.BR{at, rank}: bpe_if_cat(U32.is_eq(rank, 0), at, f, ranks, ps, ih) def Laws.bpe_conserves(fuel, ranks, ps): match fuel: case 0n: {==} case 1n+ +f: bpe_next_cat(B.best(ranks, ps), f, ranks, ps, x => Laws.bpe_conserves(f, ranks, x)) # ---- added_partition ---- # the plain text so far comes out as it went in def flush_text(buf: List<&2, U32>) -> {A.text_of(A.flush(buf)) == List.reverse(&2, U32, buf) : List<&2, U32>}: match buf: case []: {==} case c <> t: ML.append_nil(&2, U32, List.reverse(&2, U32, c <> t)) def text_of_append(xs: List<&2, A.Seg>, ys: List<&2, A.Seg>) -> {A.text_of(List.append(&2, A.Seg, xs, ys)) == List.append(&2, U32, A.text_of(xs), A.text_of(ys)) : List<&2, U32>}: match xs: case []: {==} case +s <> t: %Equal.sym(List<&2, U32>, A.text_of(List.append(&2, A.Seg, t, ys)), List.append(&2, U32, A.text_of(t), A.text_of(ys)), text_of_append(t, ys)) : {List.append(&2, U32, A.seg_cs(s), _) == List.append(&2, U32, List.append(&2, U32, A.seg_cs(s), A.text_of(t)), A.text_of(ys)) : List<&2, U32>} Equal.sym(List<&2, U32>, List.append(&2, U32, List.append(&2, U32, A.seg_cs(s), A.text_of(t)), A.text_of(ys)), List.append(&2, U32, A.seg_cs(s), List.append(&2, U32, A.text_of(t), A.text_of(ys))), ML.append_assoc(&2, U32, A.seg_cs(s), A.text_of(t), A.text_of(ys))) def cut2_cat(none: Bool, +n: Nat, id: U32, +buf: List<&2, U32>, +x: U32, +t: List<&2, U32>, +f: Nat, +fs: List<&2, U32>, +added: List<&2, A.AT>, ih: @b:List<&2, U32> -> @r:List<&2, U32> -> {A.text_of(A.split_go(f, fs, added, b, r)) == List.append(&2, U32, List.reverse(&2, U32, b), r) : List<&2, U32>}) -> {A.text_of(A.cut2(none, n, id, buf, x, t, b => r => A.split_go(f, fs, added, b, r))) == List.append(&2, U32, List.reverse(&2, U32, buf), x <> t) : List<&2, U32>}: match none: case True{}: %Equal.sym(List<&2, U32>, A.text_of(A.split_go(f, fs, added, x <> buf, t)), List.append(&2, U32, List.reverse(&2, U32, x <> buf), t), ih(x <> buf, t)) : {_ == List.append(&2, U32, List.reverse(&2, U32, buf), x <> t) : List<&2, U32>} %Equal.sym(List<&2, U32>, List.reverse(&2, U32, x <> buf), List.append(&2, U32, List.reverse(&2, U32, buf), [x]), ML.reverse_cons(&2, U32, x, buf)) : {List.append(&2, U32, _, t) == List.append(&2, U32, List.reverse(&2, U32, buf), x <> t) : List<&2, U32>} ML.append_assoc(&2, U32, List.reverse(&2, U32, buf), [x], t) case False{}: %Equal.sym(List<&2, U32>, A.text_of(List.append(&2, A.Seg, A.flush(buf), A.SAdded{id, List.take(&2, U32, x <> t, n)} <> A.split_go(f, fs, added, [], List.drop(&2, U32, x <> t, n)))), List.append(&2, U32, A.text_of(A.flush(buf)), A.text_of(A.SAdded{id, List.take(&2, U32, x <> t, n)} <> A.split_go(f, fs, added, [], List.drop(&2, U32, x <> t, n)))), text_of_append(A.flush(buf), A.SAdded{id, List.take(&2, U32, x <> t, n)} <> A.split_go(f, fs, added, [], List.drop(&2, U32, x <> t, n)))) : {_ == List.append(&2, U32, List.reverse(&2, U32, buf), x <> t) : List<&2, U32>} %Equal.sym(List<&2, U32>, A.text_of(A.flush(buf)), List.reverse(&2, U32, buf), flush_text(buf)) : {List.append(&2, U32, _, List.append(&2, U32, List.take(&2, U32, x <> t, n), A.text_of(A.split_go(f, fs, added, [], List.drop(&2, U32, x <> t, n))))) == List.append(&2, U32, List.reverse(&2, U32, buf), x <> t) : List<&2, U32>} %Equal.sym(List<&2, U32>, A.text_of(A.split_go(f, fs, added, [], List.drop(&2, U32, x <> t, n))), List.drop(&2, U32, x <> t, n), ih([], List.drop(&2, U32, x <> t, n))) : {List.append(&2, U32, List.reverse(&2, U32, buf), List.append(&2, U32, List.take(&2, U32, x <> t, n), _)) == List.append(&2, U32, List.reverse(&2, U32, buf), x <> t) : List<&2, U32>} %Equal.sym(List<&2, U32>, List.append(&2, U32, List.take(&2, U32, x <> t, n), List.drop(&2, U32, x <> t, n)), x <> t, ML.take_append_drop(&2, U32, x <> t, n)) : {List.append(&2, U32, List.reverse(&2, U32, buf), _) == List.append(&2, U32, List.reverse(&2, U32, buf), x <> t) : List<&2, U32>} {==} def cut_cat(b: A.BA, +buf: List<&2, U32>, +x: U32, +t: List<&2, U32>, +f: Nat, +fs: List<&2, U32>, +added: List<&2, A.AT>, ih: @b:List<&2, U32> -> @r:List<&2, U32> -> {A.text_of(A.split_go(f, fs, added, b, r)) == List.append(&2, U32, List.reverse(&2, U32, b), r) : List<&2, U32>}) -> {A.text_of(A.cut(b, buf, x, t, b => r => A.split_go(f, fs, added, b, r))) == List.append(&2, U32, List.reverse(&2, U32, buf), x <> t) : List<&2, U32>}: match b: case A.BA{+n, id}: cut2_cat(Nat.is_eq(n, 0n), n, id, buf, x, t, f, fs, added, ih) def added_cat(fuel: Nat, +fs: List<&2, U32>, +added: List<&2, A.AT>, +buf: List<&2, U32>, xs: List<&2, U32>) -> {A.text_of(A.split_go(fuel, fs, added, buf, xs)) == List.append(&2, U32, List.reverse(&2, U32, buf), xs) : List<&2, U32>}: match fuel xs: case _ Nil{}: Equal.trans(List<&2, U32>, A.text_of(A.flush(buf)), List.reverse(&2, U32, buf), List.append(&2, U32, List.reverse(&2, U32, buf), []), flush_text(buf), Equal.sym(List<&2, U32>, List.append(&2, U32, List.reverse(&2, U32, buf), []), List.reverse(&2, U32, buf), ML.append_nil(&2, U32, List.reverse(&2, U32, buf)))) case 0n +x <> +t: %Equal.sym(List<&2, U32>, A.text_of(A.flush(List.append(&2, U32, List.reverse(&2, U32, x <> t), buf))), List.reverse(&2, U32, List.append(&2, U32, List.reverse(&2, U32, x <> t), buf)), flush_text(List.append(&2, U32, List.reverse(&2, U32, x <> t), buf))) : {_ == List.append(&2, U32, List.reverse(&2, U32, buf), x <> t) : List<&2, U32>} %Equal.sym(List<&2, U32>, List.reverse(&2, U32, List.append(&2, U32, List.reverse(&2, U32, x <> t), buf)), List.append(&2, U32, List.reverse(&2, U32, buf), List.reverse(&2, U32, List.reverse(&2, U32, x <> t))), ML.reverse_append(&2, U32, List.reverse(&2, U32, x <> t), buf)) : {_ == List.append(&2, U32, List.reverse(&2, U32, buf), x <> t) : List<&2, U32>} %Equal.sym(List<&2, U32>, List.reverse(&2, U32, List.reverse(&2, U32, x <> t)), x <> t, ML.reverse_reverse(&2, U32, x <> t)) : {List.append(&2, U32, List.reverse(&2, U32, buf), _) == List.append(&2, U32, List.reverse(&2, U32, buf), x <> t) : List<&2, U32>} {==} case 1n+ +f +x <> +t: cut_cat(A.at_pos(A.has(fs, x), added, x <> t), buf, x, t, f, fs, added, b => r => added_cat(f, fs, added, b, r)) def Laws.added_partition(added, xs): added_cat(List.length(&2, U32, xs), A.firsts(added, []), added, [], xs) def Laws.added_longest(): {==} def Laws.utf8_round(): {==} def Laws.utf8_refusals(): {==} def Laws.byte_level_inverse(): {==} def Laws.family_known(): {==} def Laws.family_unknown(): {==} def Laws.blocks_agree(): {==} def Laws.round_trip(): {==} def Laws.encode_refuses(): {==}