PROOF.bend source
PROOF.bend on the hub · documented module
# bend-safetensors-reader: the proofs of LAWS.bend.# tile_agrees: by induction on the tensors; one step is a case split on the two checks (starts at pos, right size).# parse_sound: parse ends in accept, which builds a header only from tile's end and dup's None.import Baseimport ./w64.bend as Wimport ./header.bend as Himport ./lex.bend as Ximport ./LAWS.bend as Laws# one tensor: tile's check and the spec's fits test the same two Bools, x (starts at pos) and y (right size)def step(x: Bool, y: Bool, nm: String, b: W.W64, pos: W.W64, r: Result<&2, &2, String, W.W64>, e: Maybe<&2, W.W64>, h: {Laws.Ok(r) == e : Maybe<&2, W.W64>}) -> {Laws.Ok(H.tile_join(H.why_at(x, nm, b, pos, y), r)) == H.ends_if(Bool.and(x, y), e) : Maybe<&2, W.W64>}: match x y: case True{} True{}: h case True{} False{}: {==} case False{} _: {==}def Laws.tile_agrees(es, pos): match es: case [] : {==} case H.Ent{nm, d, s, b, f} <> t: step(W.W64.is_eq(b, pos), H.same(H.end_of(H.nbytes(d, s), b), f), nm, b, pos, H.tile(t, f), H.Ends(t, f), Laws.tile_agrees(t, f))def Laws.w64_edges(): ({==}, ({==}, ({==}, ({==}, ({==}, ({==}, ({==}, ({==}, ({==}, ({==}, ({==}, ({==}, ({==}, ({==}, ({==}, ({==}, ({==}, ({==}, {==}))))))))))))))))))# finish builds a header only from a Done end, which tile_agrees ties to Ends, and only after dup said Nonedef fin(+xs: List<&2, H.Ent>, m: List<&2, H.KV>, r: Result<&2, &2, String, W.W64>) -> {Laws.Ok(r) == H.Ends(xs, W.W64.zero()) : Maybe<&2, W.W64>} -> {None{} == H.dup(List.sort(~H.Ent, ~H.le_name, xs)) : Maybe<&2, String>} -> Laws.Sound(H.finish(xs, m, r)): match r: case Fail{why}: hr => hd => Unit{} case Done{n}: hr => hd => (Equal.sym(Maybe<&2, W.W64>, Some{n}, H.Ends(xs, W.W64.zero()), hr), Equal.sym(Maybe<&2, String>, None{}, H.dup(List.sort(~H.Ent, ~H.le_name, xs)), hd))def uniq(d: Maybe<&2, String>, +xs: List<&2, H.Ent>, m: List<&2, H.KV>) -> {d == H.dup(List.sort(~H.Ent, ~H.le_name, xs)) : Maybe<&2, String>} -> Laws.Sound(H.unique(d, xs, m)): match d: case None{}: hd => fin(xs, m, H.tile(xs, W.W64.zero()), Laws.tile_agrees(xs, W.W64.zero()), hd) case Some{nm}: hd => Unit{}def acc(a: H.Acc) -> Laws.Sound(H.accept(a)): match a: case H.Acc{+es, m}: uniq(H.dup(List.sort(~H.Ent, ~H.le_name, List.sort(~H.Ent, ~H.le_begin, es))), List.sort(~H.Ent, ~H.le_begin, es), H.meta_of(m), {==})# every parser state but PEnd ends in a refusaldef prs(st: H.Ps) -> Laws.Sound(H.parsed(st)): match st: case H.PEnd{a}: acc(a) case H.PStart{}: Unit{} case H.PKey{a, f}: Unit{} case H.PColon{a, k}: Unit{} case H.PVal{a, k}: Unit{} case H.PNext{a}: Unit{} case H.MKey{a, m, f}: Unit{} case H.MColon{a, m, k}: Unit{} case H.MVal{a, m, k}: Unit{} case H.MNext{a, m}: Unit{} case H.TKey{a, k, p, f}: Unit{} case H.TColon{a, k, p, f}: Unit{} case H.TVal{a, k, p, f}: Unit{} case H.TList{a, k, p, f, xs, fs}: Unit{} case H.TLNext{a, k, p, f, xs}: Unit{} case H.TNext{a, k, p}: Unit{} case H.PBad{why}: Unit{}def Laws.parse_sound(bs): prs(H.run(X.tokens(bs), H.PStart{}))def Laws.nbytes_edges(): ({==}, ({==}, ({==}, ({==}, ({==}, ({==}, {==}))))))def Laws.dtype_names(d): match d: case H.DBool{}: {==} case H.DU8{}: {==} case H.DI8{}: {==} case H.DF8E5M2{}: {==} case H.DF8E4M3{}: {==} case H.DI16{}: {==} case H.DU16{}: {==} case H.DF16{}: {==} case H.DBF16{}: {==} case H.DI32{}: {==} case H.DU32{}: {==} case H.DF32{}: {==} case H.DF64{}: {==} case H.DI64{}: {==} case H.DU64{}: {==}def Laws.dtype_sizes(): {==}def Laws.parse_example(): {==}