diff --git a/LAWS.bend b/LAWS.bend index 9fd3092..a0b6cda 100644 --- a/LAWS.bend +++ b/LAWS.bend @@ -50,6 +50,7 @@ import ./laws/HasFindsKey.bend as HasFindsKey import ./laws/IsArrIsObjTellKinds.bend as IsArrIsObjTellKinds import ./laws/ItemsGivesElements.bend as ItemsGivesElements import ./laws/LenCountsItems.bend as LenCountsItems +import ./laws/LexersAgree.bend as LexersAgree import ./laws/NamesCompareDecoded.bend as NamesCompareDecoded import ./laws/NumBuildsOrNull.bend as NumBuildsOrNull import ./laws/NumOkIsNumberRule.bend as NumOkIsNumberRule diff --git a/PROOF.bend b/PROOF.bend index 99aaea3..2473e97 100644 --- a/PROOF.bend +++ b/PROOF.bend @@ -23,6 +23,7 @@ import ./proofs/HasFindsKey.bend as PHasFindsKey import ./proofs/IsArrIsObjTellKinds.bend as PIsArrIsObjTellKinds import ./proofs/ItemsGivesElements.bend as PItemsGivesElements import ./proofs/LenCountsItems.bend as PLenCountsItems +import ./proofs/LexersAgree.bend as PLexersAgree import ./proofs/NamesCompareDecoded.bend as PNamesCompareDecoded import ./proofs/NumBuildsOrNull.bend as PNumBuildsOrNull import ./proofs/NumOkIsNumberRule.bend as PNumOkIsNumberRule diff --git a/bolt.bend b/bolt.bend index 542d55c..07c09ea 100644 --- a/bolt.bend +++ b/bolt.bend @@ -1,5 +1,5 @@ -# bolt.bend: how bolt lints this repo. Every rule is an error except -# `coverage`, which warns until the laws exist: the gate must see no errors. +# bolt.bend: how bolt lints this repo. Every rule is an error: the gate must +# see no errors. # See bolt/config.bend for the groups and levels. import Base @@ -15,18 +15,14 @@ def suspicious() -> String: def style() -> String: "error" -# a def named by no quantified law: advice while guarantees are pending, -# since deleting the closed laws left defs no law names. That rule -# is `coverage` (L001). +# the law rules: `coverage` (a def no quantified law reaches) and `closed` def laws() -> String: - "warn" + "error" # a law with no binder def closed() -> String: "error" -# a def no quantified law reaches (L001); off since bolt 1.26 unless named. -# warn, not error: bolt 1.27's coverage finds defs bolt 1.22 passed (listed -# in the PR that set this); back to error once they are covered +# a def no quantified law reaches (L001); off since bolt 1.26 unless named def coverage() -> String: - "warn" + "error" diff --git a/ez.lock.toml b/ez.lock.toml index d7ec2c3..6b24bba 100644 --- a/ez.lock.toml +++ b/ez.lock.toml @@ -4,8 +4,8 @@ hub = "https://hub.bend-lang.com" [tools] [tools.bolt] git = "https://github.com/Emerging-Patterns/bolt" -rev = "9700daf3a447089a8795d311912014382a74a2a9" -tag = "v1.27.0" +rev = "dfb06d0e5d6362739231b90bb090535af493b8f0" +tag = "v1.65.0" root = "." -narHash = "sha256-0S48j2dongVcvP5Oms5a+s5I12sIy91KAgwW0bP7pxM=" +narHash = "sha256-39sCydxFW7dWmf69CTJOXuC+j884fEhHWFKctdWCkAg=" entry = "main.bend" diff --git a/ez.toml b/ez.toml index e8b7f8a..4778c75 100644 --- a/ez.toml +++ b/ez.toml @@ -4,8 +4,8 @@ entry = "main.bend" [tools] [tools.bolt] git = "https://github.com/Emerging-Patterns/bolt" -rev = "9700daf3a447089a8795d311912014382a74a2a9" -tag = "v1.27.0" +rev = "dfb06d0e5d6362739231b90bb090535af493b8f0" +tag = "v1.65.0" root = "." -narHash = "sha256-0S48j2dongVcvP5Oms5a+s5I12sIy91KAgwW0bP7pxM=" +narHash = "sha256-39sCydxFW7dWmf69CTJOXuC+j884fEhHWFKctdWCkAg=" entry = "main.bend" diff --git a/flake.lock b/flake.lock index bd0339c..1e44901 100644 --- a/flake.lock +++ b/flake.lock @@ -31,11 +31,11 @@ ] }, "locked": { - "lastModified": 1794163393, - "narHash": "sha256-RwsUSTKjNvU7qGYKdaFItKGjtI/dy3cEjdDhZuN49tE=", + "lastModified": 1794759086, + "narHash": "sha256-DgFFeqCgFFWGXV+gFMOpvDKHLaVEiI73mxJQud7GUTU=", "owner": "Emerging-Patterns", "repo": "ez", - "rev": "35ace28d9578d8809a55d16941b0633e8fa774f5", + "rev": "c9f485554a0e11e40ee559d27875b052c22cdf65", "type": "github" }, "original": { diff --git a/laws/LexersAgree.bend b/laws/LexersAgree.bend new file mode 100644 index 0000000..d4a57c8 --- /dev/null +++ b/laws/LexersAgree.bend @@ -0,0 +1,36 @@ +# For texts shorter than 2^32 - 1 characters: the span scanner `tokens`, the +# lexer `parse` reads, gives the tokens the character machine `run` gives from +# the idle state, once every span is read out into the characters it covers. +# The character machine is the lexer said one character at a time; the span +# scanner is the fast path, which keeps spans of the source instead of +# copying them. Internal: a law about the lexer holds it in place without +# making it interface +import Base +import ../src/value.bend as V +import ../src/lex.bend as Lex +import ./defs/CursorAndNumberRules.bend as D1 + +# a token with its span read out into the characters it covers +def tok.den(tok: Lex.Tok) -> Lex.Tok: + match tok: + case Lex.TStrS{cut, +nn}: + Lex.TStr{V.span.str(cut, nn, U32.is_zero(nn))} + case Lex.TWordS{cut, +nn}: + Lex.TWord{V.span.str(cut, nn, U32.is_zero(nn))} + case other: + other + +# tokens with their spans read out +def toks.den(toks: List<&2, Lex.Tok>) -> List<&2, Lex.Tok>: + match toks: + case Nil{}: + Nil{} + case hd <> tl: + tok.den(hd) <> toks.den(tl) + +# LAW: the span scanner and the character machine give the same tokens +law lexers_agree: + for +txt: String + for h_s: {D1.short(txt) == True{} : Bool} + {toks.den(Lex.tokens(txt)) == toks.den(Lex.finish(Lex.run(String.to_list(txt), Lex.Lex{Lex.Idle{}, [], []}))) : + List<&2, Lex.Tok>} diff --git a/proofs/CursorEventsMatchParse.bend b/proofs/CursorEventsMatchParse.bend index b35edbf..15c8484 100644 --- a/proofs/CursorEventsMatchParse.bend +++ b/proofs/CursorEventsMatchParse.bend @@ -1377,7 +1377,7 @@ law dig_u: def dig_u(ch, h, tl, left, acc, bb, stack, done, ff): hs = Equal.sym(Bool, D2.hexd(ch), True{}, h) - %hs : {Pull.next.go(1n+ff, tl, Pull.uni.hit.go(ch, tl, left, acc, bb, stack, Pull.GNext{}, done, _)) + %hs : {Pull.next.go(1n+ff, tl, Pull.uni.hit.go(ch, tl, left, acc, bb, Pull.C{stack, Pull.GNext{}, done}, _)) == Pull.next.go(1n+ff, tl, Pull.uni.put(tl, left, acc, Lex.hex(ch), bb, stack, Pull.GNext{}, done)) : Pull.Out} {==} @@ -1394,12 +1394,14 @@ law dig_l: for +done: Bool for +ff: Nat {Pull.next.go(1n+ff, SCon{ch, tl}, Pull.S{Pull.MLo{left, acc, hi, bb}, stack, Pull.GNext{}, done, False{}}) - == Pull.next.go(1n+ff, tl, Pull.lo.put(tl, left, acc, hi, Lex.hex(ch), bb, stack, Pull.GNext{}, done)) : Pull.Out} + == Pull.next.go(1n+ff, tl, Pull.lo.put(tl, left, acc, hi, Lex.hex(ch), bb, Pull.C{stack, Pull.GNext{}, + done})) : Pull.Out} def dig_l(ch, h, tl, left, acc, hi, bb, stack, done, ff): hs = Equal.sym(Bool, D2.hexd(ch), True{}, h) - %hs : {Pull.next.go(1n+ff, tl, Pull.lo.hit.go(ch, tl, left, acc, hi, bb, stack, Pull.GNext{}, done, _)) - == Pull.next.go(1n+ff, tl, Pull.lo.put(tl, left, acc, hi, Lex.hex(ch), bb, stack, Pull.GNext{}, done)) : Pull.Out} + %hs : {Pull.next.go(1n+ff, tl, Pull.lo.hit.go(ch, tl, left, acc, hi, bb, Pull.C{stack, Pull.GNext{}, done}, _)) + == Pull.next.go(1n+ff, tl, Pull.lo.put(tl, left, acc, hi, Lex.hex(ch), bb, Pull.C{stack, Pull.GNext{}, + done})) : Pull.Out} {==} # a \u escape's four digits, once the backslash is in: its character is copied @@ -1562,7 +1564,7 @@ def et_pair(h1, h2, h3, h4, l1, l2, l3, l4, h, rest, bf, stack, done, ff): Equal.trans(Pull.Out, Pull.next.go(1n+ff, SCon{l4, rest}, Pull.S{Pull.MLo{0n, D2.hex.acc(D2.hex.acc(D2.hex.acc(0, l1), l2), l3), D2.hex4(h1, h2, h3, h4), bf}, stack, Pull.GNext{}, done, False{}}), Pull.next.go(1n+ff, rest, Pull.lo.put(rest, 0n, D2.hex.acc(D2.hex.acc(D2.hex.acc(0, l1), - l2), l3), D2.hex4(h1, h2, h3, h4), Lex.hex(l4), bf, stack, Pull.GNext{}, done)), Pull.next.go(1n+ff, rest, + l2), l3), D2.hex4(h1, h2, h3, h4), Lex.hex(l4), bf, Pull.C{stack, Pull.GNext{}, done})), Pull.next.go(1n+ff, rest, Pull.S{Pull.MCopy{D2.piece.char(D2.PPair{h1, h2, h3, h4, l1, l2, l3, l4}) <> bf}, stack, Pull.GNext{}, done, False{}}), dig_l(l4, M3.and4_4(D2.hexd(l1), D2.hexd(l2), D2.hexd(l3), D2.hexd(l4), hl), rest, 0n, D2.hex.acc(D2.hex.acc(D2.hex.acc(0, l1), l2), l3), D2.hex4(h1, h2, h3, h4), bf, stack, done, ff), @@ -5860,7 +5862,7 @@ law ss_uni: for +hp: {M6.prel(stack, False{}, M6.pst(Lex.Lex{Lex.Uni{left, acc}, bf, tk})) == True{} : Bool} for hx: Bool for +ehx: {Lex.hex.ok(Char.to_u32(ch)) == hx : Bool} - M4.sinv(tt, tl, Pull.uni.hit.go(ch, tl, left, acc, buf, stack, Pull.GNext{}, False{}, hx)) + M4.sinv(tt, tl, Pull.uni.hit.go(ch, tl, left, acc, buf, Pull.C{stack, Pull.GNext{}, False{}}, hx)) def ss_uni(tt, ch, tl, left, acc, buf, stack, bf, tk, h_s, e, hp, hx, ehx): match hx: @@ -5980,7 +5982,7 @@ law ss_lput: for +h_s: {D1.short(tl) == True{} : Bool} for +e: M6.lx_e(tt, tl, Lex.unicode.low.put(left, acc, hi, dig, bf, tk)) for +hp: {M6.prel(stack, False{}, M6.pst(Lex.Lex{Lex.Esc{}, bf, tk})) == True{} : Bool} - M4.sinv(tt, tl, Pull.lo.put(tl, left, acc, hi, dig, buf, stack, Pull.GNext{}, False{})) + M4.sinv(tt, tl, Pull.lo.put(tl, left, acc, hi, dig, buf, Pull.C{stack, Pull.GNext{}, False{}})) def ss_lput(tt, tl, left, acc, hi, dig, buf, stack, bf, tk, h_s, e, hp): match left: @@ -6007,7 +6009,7 @@ law ss_lo: for +hp: {M6.prel(stack, False{}, M6.pst(Lex.Lex{Lex.Lo{left, acc, hi}, bf, tk})) == True{} : Bool} for hx: Bool for +ehx: {Lex.hex.ok(Char.to_u32(ch)) == hx : Bool} - M4.sinv(tt, tl, Pull.lo.hit.go(ch, tl, left, acc, hi, buf, stack, Pull.GNext{}, False{}, hx)) + M4.sinv(tt, tl, Pull.lo.hit.go(ch, tl, left, acc, hi, buf, Pull.C{stack, Pull.GNext{}, False{}}, hx)) def ss_lo(tt, ch, tl, left, acc, hi, buf, stack, bf, tk, h_s, e, hp, hx, ehx): match hx: @@ -6039,14 +6041,14 @@ law sm_copy.l: for +e: M6.lx_e(tt, SCon{ch, tl}, lx) for +hm: {M4.lxmode(lx) == M4.mo(Pull.MCopy{buf}) : Lex.Mode} for +hp: {M6.prel(stack, False{}, M6.pst(lx)) == True{} : Bool} - M4.sinv(tt, tl, Pull.copy.hit(ch, tl, buf, stack, Pull.GNext{}, done, False{}, Lex.classify(ch), + M4.sinv(tt, tl, Pull.copy.hit(ch, tl, buf, Pull.C{stack, Pull.GNext{}, done}, False{}, Lex.classify(ch), U32.is_lt(Char.to_u32(ch), 32))) def sm_copy.l(tt, ch, tl, buf, stack, done, h_s, hd, lx, e, hm, hp): match lx: case Lex.Lex{+md, +bf, +tk}: es = Equal.sym(Bool, done, False{}, hd) - %es : M4.sinv(tt, tl, Pull.copy.hit(ch, tl, buf, stack, Pull.GNext{}, _, False{}, Lex.classify(ch), + %es : M4.sinv(tt, tl, Pull.copy.hit(ch, tl, buf, Pull.C{stack, Pull.GNext{}, _}, False{}, Lex.classify(ch), U32.is_lt(Char.to_u32(ch), 32))) ss_copy(tt, ch, tl, buf, stack, bf, tk, h_s, lxfix_e(tt, SCon{ch, tl}, md, Lex.Str{}, bf, tk, hm, e), lxfix_p(stack, md, Lex.Str{}, bf, tk, hm, hp), Lex.classify(ch), {==}, U32.is_lt(Char.to_u32(ch), 32), {==}) @@ -6060,7 +6062,7 @@ law sm_copy: for +stack: List<&2, Pull.Frame> for +done: Bool for hh: M4.sminv(tt, SCon{ch, tl}, Pull.MCopy{buf}, stack, done) - M4.sinv(tt, tl, Pull.copy.hit(ch, tl, buf, stack, Pull.GNext{}, done, False{}, Lex.classify(ch), + M4.sinv(tt, tl, Pull.copy.hit(ch, tl, buf, Pull.C{stack, Pull.GNext{}, done}, False{}, Lex.classify(ch), U32.is_lt(Char.to_u32(ch), 32))) def sm_copy(tt, ch, tl, buf, stack, done, hh): @@ -6200,13 +6202,13 @@ law sm_uni.l: for +e: M6.lx_e(tt, SCon{ch, tl}, lx) for +hm: {M4.lxmode(lx) == M4.mo(Pull.MUni{left, acc, buf}) : Lex.Mode} for +hp: {M6.prel(stack, False{}, M6.pst(lx)) == True{} : Bool} - M4.sinv(tt, tl, Pull.uni.hit(ch, tl, left, acc, buf, stack, Pull.GNext{}, done, False{})) + M4.sinv(tt, tl, Pull.uni.hit(ch, tl, left, acc, buf, Pull.C{stack, Pull.GNext{}, done}, False{})) def sm_uni.l(tt, ch, tl, left, acc, buf, stack, done, h_s, hd, lx, e, hm, hp): match lx: case Lex.Lex{+md, +bf, +tk}: es = Equal.sym(Bool, done, False{}, hd) - %es : M4.sinv(tt, tl, Pull.uni.hit(ch, tl, left, acc, buf, stack, Pull.GNext{}, _, False{})) + %es : M4.sinv(tt, tl, Pull.uni.hit(ch, tl, left, acc, buf, Pull.C{stack, Pull.GNext{}, _}, False{})) ss_uni(tt, ch, tl, left, acc, buf, stack, bf, tk, h_s, lxfix_e(tt, SCon{ch, tl}, md, Lex.Uni{left, acc}, bf, tk, hm, e), lxfix_p(stack, md, Lex.Uni{left, acc}, bf, tk, hm, hp), Lex.hex.ok(Char.to_u32(ch)), {==}) @@ -6221,7 +6223,7 @@ law sm_uni: for +stack: List<&2, Pull.Frame> for +done: Bool for hh: M4.sminv(tt, SCon{ch, tl}, Pull.MUni{left, acc, buf}, stack, done) - M4.sinv(tt, tl, Pull.uni.hit(ch, tl, left, acc, buf, stack, Pull.GNext{}, done, False{})) + M4.sinv(tt, tl, Pull.uni.hit(ch, tl, left, acc, buf, Pull.C{stack, Pull.GNext{}, done}, False{})) def sm_uni(tt, ch, tl, left, acc, buf, stack, done, hh): (h_s, (hd, (+lx, (e, (hm, hp))))) = hh @@ -6326,13 +6328,13 @@ law sm_lo.l: for +e: M6.lx_e(tt, SCon{ch, tl}, lx) for +hm: {M4.lxmode(lx) == M4.mo(Pull.MLo{left, acc, hi, buf}) : Lex.Mode} for +hp: {M6.prel(stack, False{}, M6.pst(lx)) == True{} : Bool} - M4.sinv(tt, tl, Pull.lo.hit(ch, tl, left, acc, hi, buf, stack, Pull.GNext{}, done, False{})) + M4.sinv(tt, tl, Pull.lo.hit(ch, tl, left, acc, hi, buf, Pull.C{stack, Pull.GNext{}, done}, False{})) def sm_lo.l(tt, ch, tl, left, acc, hi, buf, stack, done, h_s, hd, lx, e, hm, hp): match lx: case Lex.Lex{+md, +bf, +tk}: es = Equal.sym(Bool, done, False{}, hd) - %es : M4.sinv(tt, tl, Pull.lo.hit(ch, tl, left, acc, hi, buf, stack, Pull.GNext{}, _, False{})) + %es : M4.sinv(tt, tl, Pull.lo.hit(ch, tl, left, acc, hi, buf, Pull.C{stack, Pull.GNext{}, _}, False{})) ss_lo(tt, ch, tl, left, acc, hi, buf, stack, bf, tk, h_s, lxfix_e(tt, SCon{ch, tl}, md, Lex.Lo{left, acc, hi}, bf, tk, hm, e), lxfix_p(stack, md, Lex.Lo{left, acc, hi}, bf, tk, hm, hp), Lex.hex.ok(Char.to_u32(ch)), {==}) @@ -6348,7 +6350,7 @@ law sm_lo: for +stack: List<&2, Pull.Frame> for +done: Bool for hh: M4.sminv(tt, SCon{ch, tl}, Pull.MLo{left, acc, hi, buf}, stack, done) - M4.sinv(tt, tl, Pull.lo.hit(ch, tl, left, acc, hi, buf, stack, Pull.GNext{}, done, False{})) + M4.sinv(tt, tl, Pull.lo.hit(ch, tl, left, acc, hi, buf, Pull.C{stack, Pull.GNext{}, done}, False{})) def sm_lo(tt, ch, tl, left, acc, hi, buf, stack, done, hh): (h_s, (hd, (+lx, (e, (hm, hp))))) = hh @@ -6705,7 +6707,7 @@ def pgo_inv(tt, ff, xx, st, hh, hst): Empty.absurd(oinv(tt, Pull.next.go(1n+pp, SCon{ch, tl}, Pull.S{Pull.MSpan{c1, n1}, stack, Pull.GSkip{g1}, done, True{}})), hh) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MCopy{+b1}, +stack, Pull.GNext{}, +done, False{}}: - pgo_inv(tt, 1n+pp, tl, Pull.copy.hit(ch, tl, b1, stack, Pull.GNext{}, done, False{}, Lex.classify(ch), + pgo_inv(tt, 1n+pp, tl, Pull.copy.hit(ch, tl, b1, Pull.C{stack, Pull.GNext{}, done}, False{}, Lex.classify(ch), U32.is_lt(Char.to_u32(ch), 32)), sm_copy(tt, ch, tl, b1, stack, done, hh), hst) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MCopy{+b1}, +stack, Pull.GNext{}, +done, True{}}: @@ -6730,7 +6732,7 @@ def pgo_inv(tt, ff, xx, st, hh, hst): Empty.absurd(oinv(tt, Pull.next.go(1n+pp, SCon{ch, tl}, Pull.S{Pull.MEsc{b1}, stack, Pull.GSkip{g1}, done, True{}})), hh) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MUni{+l1, +a1, +b1}, +stack, Pull.GNext{}, +done, False{}}: - pgo_inv(tt, 1n+pp, tl, Pull.uni.hit(ch, tl, l1, a1, b1, stack, Pull.GNext{}, done, False{}), + pgo_inv(tt, 1n+pp, tl, Pull.uni.hit(ch, tl, l1, a1, b1, Pull.C{stack, Pull.GNext{}, done}, False{}), sm_uni(tt, ch, tl, l1, a1, b1, stack, done, hh), hst) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MUni{+l1, +a1, +b1}, +stack, Pull.GNext{}, +done, True{}}: Empty.absurd(oinv(tt, Pull.next.go(1n+pp, SCon{ch, tl}, Pull.S{Pull.MUni{l1, a1, b1}, stack, Pull.GNext{}, done, @@ -6766,7 +6768,7 @@ def pgo_inv(tt, ff, xx, st, hh, hst): Empty.absurd(oinv(tt, Pull.next.go(1n+pp, SCon{ch, tl}, Pull.S{Pull.MHiEsc{h1, b1}, stack, Pull.GSkip{g1}, done, True{}})), hh) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MLo{+l1, +a1, +h1, +b1}, +stack, Pull.GNext{}, +done, False{}}: - pgo_inv(tt, 1n+pp, tl, Pull.lo.hit(ch, tl, l1, a1, h1, b1, stack, Pull.GNext{}, done, False{}), + pgo_inv(tt, 1n+pp, tl, Pull.lo.hit(ch, tl, l1, a1, h1, b1, Pull.C{stack, Pull.GNext{}, done}, False{}), sm_lo(tt, ch, tl, l1, a1, h1, b1, stack, done, hh), hst) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MLo{+l1, +a1, +h1, +b1}, +stack, Pull.GNext{}, +done, True{}}: Empty.absurd(oinv(tt, Pull.next.go(1n+pp, SCon{ch, tl}, Pull.S{Pull.MLo{l1, a1, h1, b1}, stack, Pull.GNext{}, diff --git a/proofs/LexersAgree.bend b/proofs/LexersAgree.bend new file mode 100644 index 0000000..3c5096a --- /dev/null +++ b/proofs/LexersAgree.bend @@ -0,0 +1,57 @@ +# proofs of laws/LexersAgree.bend +import Base +import ../src/lex.bend as Lex +import ../laws/LexersAgree.bend as Laws +import ./lemmas/CharacterClasses.bend as M1 +import ./lemmas/LexemeOrder.bend as M2 + +# the lemmas' span read-out is the law's, token by token +law den_same: + for +tok: Lex.Tok + {M2.den(tok) == Laws.tok.den(tok) : Lex.Tok} + +def den_same(tok): + match tok: + case Lex.TOpenArr{}: + {==} + case Lex.TCloseArr{}: + {==} + case Lex.TOpenObj{}: + {==} + case Lex.TCloseObj{}: + {==} + case Lex.TColon{}: + {==} + case Lex.TComma{}: + {==} + case Lex.TStr{s}: + {==} + case Lex.TWord{raw}: + {==} + case Lex.TStrS{cut, nn}: + {==} + case Lex.TWordS{cut, nn}: + {==} + case Lex.TBad{}: + {==} + +# and over a list +law dens_same: + for +toks: List<&2, Lex.Tok> + {M2.dens(toks) == Laws.toks.den(toks) : List<&2, Lex.Tok>} + +def dens_same(toks): + match toks: + case Nil{}: + {==} + case hd <> tl: + %dens_same(tl) : {M2.den(hd) <> M2.dens(tl) == Laws.tok.den(hd) <> _ : List<&2, Lex.Tok>} + %den_same(hd) : {M2.den(hd) <> M2.dens(tl) == _ <> M2.dens(tl) : List<&2, Lex.Tok>} + {==} + +def Laws.lexers_agree(txt, h_s): + %dens_same(Lex.tokens(txt)) : {_ == Laws.toks.den(Lex.finish(Lex.run(String.to_list(txt), Lex.Lex{Lex.Idle{}, [], + []}))) : List<&2, Lex.Tok>} + %dens_same(Lex.finish(Lex.run(String.to_list(txt), Lex.Lex{Lex.Idle{}, [], []}))) : {M2.dens(Lex.tokens(txt)) == _ : + List<&2, Lex.Tok>} + M1.lexers_agree(txt, h_s) diff --git a/proofs/SkipPassesOneValue.bend b/proofs/SkipPassesOneValue.bend index 1c3b3a2..2b5a35a 100644 --- a/proofs/SkipPassesOneValue.bend +++ b/proofs/SkipPassesOneValue.bend @@ -318,19 +318,19 @@ def go_add(aa, xx, st, bb): case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MSpan{+cut, +nn}, +stack, +goal, +done, True{}}: go_add(1n+pp, tl, Pull.err.st(goal), bb) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MCopy{+buf}, +stack, +goal, +done, False{}}: - go_add(1n+pp, tl, Pull.copy.hit(ch, tl, buf, stack, goal, done, False{}, Lex.classify(ch), + go_add(1n+pp, tl, Pull.copy.hit(ch, tl, buf, Pull.C{stack, goal, done}, False{}, Lex.classify(ch), U32.is_lt(Char.to_u32(ch), 32)), bb) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MCopy{+buf}, +stack, +goal, +done, True{}}: - go_add(1n+pp, tl, Pull.copy.hit(ch, tl, buf, stack, goal, done, True{}, Lex.classify(ch), + go_add(1n+pp, tl, Pull.copy.hit(ch, tl, buf, Pull.C{stack, goal, done}, True{}, Lex.classify(ch), U32.is_lt(Char.to_u32(ch), 32)), bb) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MEsc{+buf}, +stack, +goal, +done, False{}}: go_add(1n+pp, tl, Pull.esc.hit(ch, tl, buf, stack, goal, done, False{}), bb) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MEsc{+buf}, +stack, +goal, +done, True{}}: go_add(1n+pp, tl, Pull.esc.hit(ch, tl, buf, stack, goal, done, True{}), bb) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MUni{+left2, +acc, +buf}, +stack, +goal, +done, False{}}: - go_add(1n+pp, tl, Pull.uni.hit(ch, tl, left2, acc, buf, stack, goal, done, False{}), bb) + go_add(1n+pp, tl, Pull.uni.hit(ch, tl, left2, acc, buf, Pull.C{stack, goal, done}, False{}), bb) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MUni{+left2, +acc, +buf}, +stack, +goal, +done, True{}}: - go_add(1n+pp, tl, Pull.uni.hit(ch, tl, left2, acc, buf, stack, goal, done, True{}), bb) + go_add(1n+pp, tl, Pull.uni.hit(ch, tl, left2, acc, buf, Pull.C{stack, goal, done}, True{}), bb) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MHi{+hi, +buf}, +stack, +goal, +done, False{}}: go_add(1n+pp, tl, Pull.hi.hit(tl, hi, buf, stack, goal, done, False{}, Lex.classify(ch)), bb) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MHi{+hi, +buf}, +stack, +goal, +done, True{}}: @@ -340,9 +340,9 @@ def go_add(aa, xx, st, bb): case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MHiEsc{+hi, +buf}, +stack, +goal, +done, True{}}: go_add(1n+pp, tl, Pull.hiesc.hit(ch, tl, hi, buf, stack, goal, done, True{}), bb) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MLo{+left2, +acc, +hi, +buf}, +stack, +goal, +done, False{}}: - go_add(1n+pp, tl, Pull.lo.hit(ch, tl, left2, acc, hi, buf, stack, goal, done, False{}), bb) + go_add(1n+pp, tl, Pull.lo.hit(ch, tl, left2, acc, hi, buf, Pull.C{stack, goal, done}, False{}), bb) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MLo{+left2, +acc, +hi, +buf}, +stack, +goal, +done, True{}}: - go_add(1n+pp, tl, Pull.lo.hit(ch, tl, left2, acc, hi, buf, stack, goal, done, True{}), bb) + go_add(1n+pp, tl, Pull.lo.hit(ch, tl, left2, acc, hi, buf, Pull.C{stack, goal, done}, True{}), bb) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MSkStr{}, +stack, +goal, +done, False{}}: go_add(1n+pp, tl, Pull.sk.advance(Pull.span.kind.of(ch), stack, goal, done), bb) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MSkStr{}, +stack, +goal, +done, True{}}: @@ -364,9 +364,11 @@ def go_add(aa, xx, st, bb): case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MSkHiEsc{+hi}, +stack, +goal, +done, True{}}: go_add(1n+pp, tl, Pull.sk.hiesc(stack, goal, done, hi, U32.is_eq(Char.to_u32(ch), 117)), bb) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MSkLo{+left2, +acc, +hi}, +stack, +goal, +done, False{}}: - go_add(1n+pp, tl, Pull.sk.lo.go(ch, tl, left2, acc, hi, stack, goal, done, Lex.hex.ok(Char.to_u32(ch))), bb) + go_add(1n+pp, tl, Pull.sk.lo.go(ch, tl, left2, acc, hi, Pull.C{stack, goal, done}, + Lex.hex.ok(Char.to_u32(ch))), bb) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MSkLo{+left2, +acc, +hi}, +stack, +goal, +done, True{}}: - go_add(1n+pp, tl, Pull.sk.lo.go(ch, tl, left2, acc, hi, stack, goal, done, Lex.hex.ok(Char.to_u32(ch))), bb) + go_add(1n+pp, tl, Pull.sk.lo.go(ch, tl, left2, acc, hi, Pull.C{stack, goal, done}, + Lex.hex.ok(Char.to_u32(ch))), bb) case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MDone{+stop, +saved}, +stack, +goal, +done, False{}}: {==} case 1n+pp SCon{+ch, +tl} Pull.S{Pull.MDone{+stop, +saved}, +stack, +goal, +done, True{}}: @@ -1781,7 +1783,7 @@ law ls_uh: for ih: @m2: Pull.Mode -> @g2: Nat -> {isstr(m2) == True{} : Bool} -> kq(sel, Pull.next.go(1n+fn, tl, Pull.S{m2, stack, Pull.GNext{}, False{}, False{}}), Pull.next.go(1n+fu(tl, g2), tl, Pull.S{skm(m2), stack, Pull.GSkip{ee}, False{}, False{}}), ee) - kq(sel, Pull.next.go(1n+fn, tl, Pull.uni.hit.go(ch, tl, left2, acc, buf, stack, Pull.GNext{}, False{}, ok)), + kq(sel, Pull.next.go(1n+fn, tl, Pull.uni.hit.go(ch, tl, left2, acc, buf, Pull.C{stack, Pull.GNext{}, False{}}, ok)), Pull.next.go(1n+fu(tl, 2n+gg), tl, Pull.sk.uni.go(ch, tl, left2, acc, stack, Pull.GSkip{ee}, False{}, ok)), ee) def ls_uh(tl, fn, ch, left2, acc, buf, ok, stack, ee, gg, sel, hsel, ih): @@ -1892,7 +1894,7 @@ law ls_lp: for ih: @m2: Pull.Mode -> @g2: Nat -> {isstr(m2) == True{} : Bool} -> kq(sel, Pull.next.go(1n+fn, tl, Pull.S{m2, stack, Pull.GNext{}, False{}, False{}}), Pull.next.go(1n+fu(tl, g2), tl, Pull.S{skm(m2), stack, Pull.GSkip{ee}, False{}, False{}}), ee) - kq(sel, Pull.next.go(1n+fn, tl, Pull.lo.put(tl, left2, acc, hi, dig, buf, stack, Pull.GNext{}, False{})), + kq(sel, Pull.next.go(1n+fn, tl, Pull.lo.put(tl, left2, acc, hi, dig, buf, Pull.C{stack, Pull.GNext{}, False{}})), Pull.next.go(1n+fu(tl, 2n+gg), tl, Pull.sk.lo.put(tl, left2, acc, hi, dig, stack, Pull.GSkip{ee}, False{})), ee) def ls_lp(tl, fn, left2, acc, hi, dig, buf, stack, ee, gg, sel, hsel, ih): @@ -1921,8 +1923,10 @@ law ls_lh: for ih: @m2: Pull.Mode -> @g2: Nat -> {isstr(m2) == True{} : Bool} -> kq(sel, Pull.next.go(1n+fn, tl, Pull.S{m2, stack, Pull.GNext{}, False{}, False{}}), Pull.next.go(1n+fu(tl, g2), tl, Pull.S{skm(m2), stack, Pull.GSkip{ee}, False{}, False{}}), ee) - kq(sel, Pull.next.go(1n+fn, tl, Pull.lo.hit.go(ch, tl, left2, acc, hi, buf, stack, Pull.GNext{}, False{}, ok)), - Pull.next.go(1n+fu(tl, 2n+gg), tl, Pull.sk.lo.go(ch, tl, left2, acc, hi, stack, Pull.GSkip{ee}, False{}, ok)), ee) + kq(sel, Pull.next.go(1n+fn, tl, Pull.lo.hit.go(ch, tl, left2, acc, hi, buf, Pull.C{stack, Pull.GNext{}, False{}}, + ok)), + Pull.next.go(1n+fu(tl, 2n+gg), tl, Pull.sk.lo.go(ch, tl, left2, acc, hi, Pull.C{stack, Pull.GSkip{ee}, False{}}, + ok)), ee) def ls_lh(tl, fn, ch, left2, acc, hi, buf, ok, stack, ee, gg, sel, hsel, ih): match ok: diff --git a/src/lex.bend b/src/lex.bend index d772c7e..57047da 100644 --- a/src/lex.bend +++ b/src/lex.bend @@ -345,7 +345,7 @@ def tokens.hit( +ch: Char, tail: String, cut: String, - +nn: U32, + +size: U32, mode: Span, toks: List<&2, Tok>, cls: Class, @@ -353,35 +353,35 @@ def tokens.hit( ) -> Scan: match mode cls ctrl: case GIdle{} CSpace{} b: - Go{cut, nn, GIdle{}, toks} + Go{cut, size, GIdle{}, toks} case GIdle{} CPunct{p} b: - Go{cut, nn, GIdle{}, p <> toks} + Go{cut, size, GIdle{}, p <> toks} case GIdle{} CQuote{} b: Go{tail, 0, GStr{}, toks} case GIdle{} CBack{} b: - Go{cut, nn, GBad{}, toks} + Go{cut, size, GBad{}, toks} case GIdle{} COther{} b: Go{SCon{ch, tail}, 1, GWord{}, toks} case GWord{} COther{} b: - Go{cut, U32.add(nn, 1), GWord{}, toks} + Go{cut, U32.add(size, 1), GWord{}, toks} case GWord{} CPunct{p} b: - Go{"", 0, GIdle{}, p <> tokens.push(cut, nn, True{}, toks)} + Go{"", 0, GIdle{}, p <> tokens.push(cut, size, True{}, toks)} case GWord{} CQuote{} b: - Go{tail, 0, GStr{}, tokens.push(cut, nn, True{}, toks)} + Go{tail, 0, GStr{}, tokens.push(cut, size, True{}, toks)} case GWord{} CBack{} b: - Go{cut, nn, GBad{}, toks} + Go{cut, size, GBad{}, toks} case GWord{} k b: - Go{"", 0, GIdle{}, tokens.push(cut, nn, True{}, toks)} + Go{"", 0, GIdle{}, tokens.push(cut, size, True{}, toks)} case GStr{} k True{}: - Go{cut, nn, GBad{}, toks} + Go{cut, size, GBad{}, toks} case GStr{} CQuote{} False{}: - Go{"", 0, GIdle{}, tokens.push(cut, nn, False{}, toks)} + Go{"", 0, GIdle{}, tokens.push(cut, size, False{}, toks)} case GStr{} CBack{} False{}: - Old{feed('\\', Lex{Str{}, tokens.rev(cut, nn, U32.is_zero(nn), []), toks})} + Old{feed('\\', Lex{Str{}, tokens.rev(cut, size, U32.is_zero(size), []), toks})} case GStr{} k False{}: - Go{cut, U32.add(nn, 1), GStr{}, toks} + Go{cut, U32.add(size, 1), GStr{}, toks} case GBad{} k b: - Go{cut, nn, GBad{}, toks} + Go{cut, size, GBad{}, toks} def tokens.done(cut: String, nn: U32, mode: Span, toks: List<&2, Tok>) -> List<&2, Tok>: match mode: diff --git a/src/print.bend b/src/print.bend index 7fc6892..1f7ec69 100644 --- a/src/print.bend +++ b/src/print.bend @@ -147,11 +147,3 @@ def pretty.go(val: V.Json, +pad: String, +close: String) -> String: # a value as JSON indented two spaces a level, one element or member a line def pretty(val: V.Json) -> String: pretty.go(val, "", "") - -# a parse result: the value, or "" -def maybe(mb: Maybe<&2, V.Json>) -> String: - match mb: - case None{}: - "" - case Some{j}: - print(j) diff --git a/src/pull.bend b/src/pull.bend index 6a0e806..4d966ee 100644 --- a/src/pull.bend +++ b/src/pull.bend @@ -115,6 +115,12 @@ type Mode is Data: type St is Data: S{mode: Mode, stack: List<&2, Frame>, goal: Goal, done: Bool, bad: Bool} +# what a string step carries through unchanged: the open frames, the goal, +# and whether the top value is done. A step inside a string hands these to +# the state it builds as they came +type Ctx is Data: + C{stack: List<&2, Frame>, goal: Goal, done: Bool} + # what a walk hands back: a finished step, or a budget used up, to resume on # `txt` in state `st` type Out is Type: @@ -972,17 +978,15 @@ def copy.hit( +ch: Char, +tl: String, buf: List<&2, Char>, - stack: List<&2, Frame>, - goal: Goal, - done: Bool, + ctx: Ctx, bad: Bool, cls: Lex.Class, ctrl: Bool ) -> St: - match bad: - case True{}: + match ctx bad: + case C{stack, goal, done} True{}: err.st(goal) - case False{}: + case C{stack, goal, done} False{}: copy.hit.go(ch, tl, buf, stack, goal, done, cls, ctrl) # the character after a backslash, once `u` has been noticed @@ -1085,15 +1089,13 @@ def uni.hit.go( left: Nat, acc: U32, buf: List<&2, Char>, - stack: List<&2, Frame>, - goal: Goal, - done: Bool, + ctx: Ctx, ok: Bool ) -> St: - match ok: - case True{}: + match ctx ok: + case C{stack, goal, done} True{}: uni.put(tl, left, acc, Lex.hex(ch), buf, stack, goal, done) - case False{}: + case C{stack, goal, done} False{}: err.st(goal) # one hex digit of a `\u` escape @@ -1103,16 +1105,14 @@ def uni.hit( left: Nat, acc: U32, buf: List<&2, Char>, - stack: List<&2, Frame>, - goal: Goal, - done: Bool, + ctx: Ctx, bad: Bool ) -> St: - match bad: - case True{}: + match ctx bad: + case C{stack, goal, done} True{}: err.st(goal) - case False{}: - uni.hit.go(ch, tl, left, acc, buf, stack, goal, done, Lex.hex.ok(Char.to_u32(ch))) + case C{stack, goal, done} False{}: + uni.hit.go(ch, tl, left, acc, buf, C{stack, goal, done}, Lex.hex.ok(Char.to_u32(ch))) # the character after a high surrogate, on a live cursor def hi.hit.go( @@ -1217,14 +1217,12 @@ def lo.put( hi: U32, dig: U32, buf: List<&2, Char>, - stack: List<&2, Frame>, - goal: Goal, - done: Bool + ctx: Ctx ) -> St: - match left: - case 0n: + match left ctx: + case 0n C{stack, goal, done}: lo.end(tl, (acc * 16 + dig : U32), hi, buf, stack, goal, done) - case 1n+p: + case 1n+p C{stack, goal, done}: S{MLo{p, (acc * 16 + dig : U32), hi, buf}, stack, goal, done, False{}} # one hex digit of the low surrogate @@ -1235,15 +1233,13 @@ def lo.hit.go( acc: U32, hi: U32, buf: List<&2, Char>, - stack: List<&2, Frame>, - goal: Goal, - done: Bool, + ctx: Ctx, ok: Bool ) -> St: - match ok: - case True{}: - lo.put(tl, left, acc, hi, Lex.hex(ch), buf, stack, goal, done) - case False{}: + match ctx ok: + case C{stack, goal, done} True{}: + lo.put(tl, left, acc, hi, Lex.hex(ch), buf, C{stack, goal, done}) + case C{stack, goal, done} False{}: err.st(goal) # one hex digit of the low surrogate @@ -1254,16 +1250,14 @@ def lo.hit( acc: U32, hi: U32, buf: List<&2, Char>, - stack: List<&2, Frame>, - goal: Goal, - done: Bool, + ctx: Ctx, bad: Bool ) -> St: - match bad: - case True{}: + match ctx bad: + case C{stack, goal, done} True{}: err.st(goal) - case False{}: - lo.hit.go(ch, tl, left, acc, hi, buf, stack, goal, done, Lex.hex.ok(Char.to_u32(ch))) + case C{stack, goal, done} False{}: + lo.hit.go(ch, tl, left, acc, hi, buf, C{stack, goal, done}, Lex.hex.ok(Char.to_u32(ch))) # a decoded short escape puts the skip back in the string def sk.esc.put( @@ -1434,15 +1428,13 @@ def sk.lo.go( left: Nat, acc: U32, hi: U32, - stack: List<&2, Frame>, - goal: Goal, - done: Bool, + ctx: Ctx, ok: Bool ) -> St: - match ok: - case True{}: + match ctx ok: + case C{stack, goal, done} True{}: sk.lo.put(tl, left, acc, hi, Lex.hex(ch), stack, goal, done) - case False{}: + case C{stack, goal, done} False{}: err.st(goal) # the levels of the walk's budget: next.grow runs levels 0 to 32 of leaves of @@ -1491,7 +1483,7 @@ def next.go(fuel: Nat, txt: String, st: St) -> Out: case 1n+left SNil{} S{MCopy{buf}, stack, goal, done, bad}: Fin{bad.cur()} case 1n+left SCon{+ch, +tl} S{MCopy{buf}, stack, goal, done, bad}: - next.go(1n+left, tl, copy.hit(ch, tl, buf, stack, goal, done, bad, Lex.classify(ch), + next.go(1n+left, tl, copy.hit(ch, tl, buf, C{stack, goal, done}, bad, Lex.classify(ch), U32.is_lt(Char.to_u32(ch), 32))) case 1n+left SNil{} S{MEsc{buf}, stack, goal, done, bad}: Fin{bad.cur()} @@ -1500,7 +1492,7 @@ def next.go(fuel: Nat, txt: String, st: St) -> Out: case 1n+left SNil{} S{MUni{left2, acc, buf}, stack, goal, done, bad}: Fin{bad.cur()} case 1n+left SCon{+ch, +tl} S{MUni{left2, acc, buf}, stack, goal, done, bad}: - next.go(1n+left, tl, uni.hit(ch, tl, left2, acc, buf, stack, goal, done, bad)) + next.go(1n+left, tl, uni.hit(ch, tl, left2, acc, buf, C{stack, goal, done}, bad)) case 1n+left SNil{} S{MHi{hi, buf}, stack, goal, done, bad}: Fin{bad.cur()} case 1n+left SCon{ch, tl} S{MHi{hi, buf}, stack, goal, done, bad}: @@ -1512,7 +1504,7 @@ def next.go(fuel: Nat, txt: String, st: St) -> Out: case 1n+left SNil{} S{MLo{left2, acc, hi, buf}, stack, goal, done, bad}: Fin{bad.cur()} case 1n+left SCon{+ch, +tl} S{MLo{left2, acc, hi, buf}, stack, goal, done, bad}: - next.go(1n+left, tl, lo.hit(ch, tl, left2, acc, hi, buf, stack, goal, done, bad)) + next.go(1n+left, tl, lo.hit(ch, tl, left2, acc, hi, buf, C{stack, goal, done}, bad)) case 1n+left SNil{} S{MSkStr{}, stack, goal, done, bad}: Fin{bad.cur()} case 1n+left SCon{ch, tl} S{MSkStr{}, stack, goal, done, True{}}: @@ -1538,7 +1530,7 @@ def next.go(fuel: Nat, txt: String, st: St) -> Out: case 1n+left SNil{} S{MSkLo{left2, acc, hi}, stack, goal, done, bad}: Fin{bad.cur()} case 1n+left SCon{+ch, tl} S{MSkLo{left2, acc, hi}, stack, goal, done, bad}: - next.go(1n+left, tl, sk.lo.go(ch, tl, left2, acc, hi, stack, goal, done, + next.go(1n+left, tl, sk.lo.go(ch, tl, left2, acc, hi, C{stack, goal, done}, Lex.hex.ok(Char.to_u32(ch)))) # the jumps one leaf of the budget allows. A plain word takes two (the burst