From 4cfd44a4cf93fbc8ea9958d9500a3e4ce57c5097 Mon Sep 17 00:00:00 2001 From: noah-emp <182041600+noah-emp@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:17:36 -0500 Subject: [PATCH] fix(rules): length does not measure a list-literal table A def whose body is one `[..]` list of data and nothing else is a table, as a `match` of one-statement arms is since #381: a generated list of records, such as ezhttp's src/entities.bend `table` (the HTML Standard's 2231 named character references, 2234 lines). Its length is how many rows there are, and the one edit that silenced `length` was to cut the list in two at some line, which helps no reader. Data is numbers, strings, chars, commas, capitalized and dotted names and `[..]` and `{..}` groups of them. A plain lowercase name (a variable or a call's head), an operator, a keyword, a `(..)` group, a token after the list's `]` or a line under it makes the def no table, and it is measured as before. laws/SizeReportsOversizedDefs.bend: `table_takes_data_list`, `table_list_needs_data`, `table_list_holds_nothing_under`, `table_list_is_whole_statement`, `data_reads_token`, `data_reads_group` and `data_rejects_statement` are new; the prose names the new table. `length_skips_table` and `length_measures_other_def` hold as stated. Co-Authored-By: Claude Opus 5.5 --- laws/SizeReportsOversizedDefs.bend | 122 ++++++++++++++++++++++++++- proofs/SizeReportsOversizedDefs.bend | 73 ++++++++++++++++ src/README.md | 7 +- src/rules/style/size.bend | 74 +++++++++++++++- 4 files changed, 270 insertions(+), 6 deletions(-) diff --git a/laws/SizeReportsOversizedDefs.bend b/laws/SizeReportsOversizedDefs.bend index e01563a..97af508 100644 --- a/laws/SizeReportsOversizedDefs.bend +++ b/laws/SizeReportsOversizedDefs.bend @@ -12,7 +12,14 @@ # whose body is one statement that opens a block, with nothing under it but # `case` arms, each holding at most one statement, which opens no block: a # second statement in the body, a second in an arm, or a block in an arm -# makes it no table. depth puts the statements under one that opens a block +# makes it no table. A def whose body is one statement that is a `[` list of +# data and nothing else, with nothing under it, is a table too. Data is +# numbers, strings, chars, commas, capitalized and dotted names (a +# constructor's, or a def's named and not called) and `[..]` and `{..}` +# groups of data: a plain lowercase name (a variable, or a call's head), an +# operator, a keyword, a `(..)` group or a statement anywhere in the list +# makes it no table, and so does a token after the list's `]` or a line +# under it. depth puts the statements under one that opens a block # (a `:` among # its own tokens) one level down, and those under one that does not (a line # an expression wrapped onto) at the same level. @@ -33,6 +40,29 @@ import ../src/syntax/outline.bend as Outline import ../src/rules/calls.bend as Calls import ../src/rules/style/size.bend as Size +# a token a list of data holds: a number, a string, a char, a comma, or a +# capitalized or dotted name +def literal(k: Lex.TokKind) -> Bool: + match k: + case Lex.TNum{}: + True{} + case Lex.TStr{}: + True{} + case Lex.TChar{}: + True{} + case Lex.TComma{}: + True{} + case Lex.TUpper{}: + True{} + case Lex.TDotted{}: + True{} + case _other: + False{} + +# a bracket a group of data opens: `[` or `{` +def bracket(+t: String) -> Bool: + Bool.or(String.eq(t, "["), String.eq(t, "{")) + # is the def one the rule reports: not exempt, and over the limit? def over(dim: Size.Dim, path: String, +sig: Tree.Node, body: Tree.Node, max: U32) -> Bool: Bool.and(Bool.not(Calls.exempt(path, sig)), U32.is_gt(Size.measure(dim, sig, body), max)) @@ -190,3 +220,93 @@ law table_arm_holds_one_statement: for second: Tree.Node for rest: Tree.Node {Size.table.arm(Tree.NCons{first, Tree.NCons{second, rest}}) == False{} : Bool} + +# LAW: a body that is one statement holding a `[` list of data, and nothing +# under it, is a table +law table_takes_data_list: + for kind: Tree.StmtKind + for k: Lex.TokKind + for l: U32 + for c: U32 + for elems: Tree.Node + for close: Maybe<&2, Lex.Tok> + for e: {Size.data(elems) == True{} : Bool} + {Size.table(Tree.NCons{Tree.Stmt{kind, Tree.NCons{Tree.Group{Lex.Tok{k, "[", l, c}, elems, close}, Tree.NNil{}}, + Tree.NNil{}}, Tree.NNil{}}) == True{} : Bool} + +# LAW: a body that is one `[` list holding what is not data, a call or a +# match, is no table +law table_list_needs_data: + for kind: Tree.StmtKind + for k: Lex.TokKind + for t: String + for l: U32 + for c: U32 + for elems: Tree.Node + for close: Maybe<&2, Lex.Tok> + for e: {Size.data(elems) == False{} : Bool} + {Size.table(Tree.NCons{Tree.Stmt{kind, Tree.NCons{Tree.Group{Lex.Tok{k, t, l, c}, elems, close}, Tree.NNil{}}, + Tree.NNil{}}, Tree.NNil{}}) == False{} : Bool} + +# LAW: a `[` list with a line under it is no table +law table_list_holds_nothing_under: + for kind: Tree.StmtKind + for k: Lex.TokKind + for t: String + for l: U32 + for c: U32 + for elems: Tree.Node + for close: Maybe<&2, Lex.Tok> + for first: Tree.Node + for rest: Tree.Node + {Size.table(Tree.NCons{Tree.Stmt{kind, Tree.NCons{Tree.Group{Lex.Tok{k, t, l, c}, elems, close}, Tree.NNil{}}, + Tree.NCons{first, rest}}, Tree.NNil{}}) == False{} : Bool} + +# LAW: a `[` list with a token after its `]`, as `[..] ++ more`, is no table +# of data (it is a table only as a `match` is, when a `:` opens a block) +law table_list_is_whole_statement: + for kind: Tree.StmtKind + for k: Lex.TokKind + for t: String + for l: U32 + for c: U32 + for elems: Tree.Node + for close: Maybe<&2, Lex.Tok> + for next: Tree.Node + for more: Tree.Node + for under: Tree.Node + for e: {Calls.has_colon(Tree.NCons{next, more}) == False{} : Bool} + {Size.table(Tree.NCons{Tree.Stmt{kind, Tree.NCons{Tree.Group{Lex.Tok{k, t, l, c}, elems, close}, + Tree.NCons{next, more}}, under}, Tree.NNil{}}) == False{} : Bool} + +# LAW: a token is data exactly when it is a number, a string, a char, a +# comma, or a capitalized or dotted name, and what follows it is data +law data_reads_token: + for k: Lex.TokKind + for t: String + for l: U32 + for c: U32 + for rest: Tree.Node + {Size.data(Tree.NCons{Tree.Leaf{Lex.Tok{k, t, l, c}}, rest}) == Bool.and(literal(k), Size.data(rest)) : Bool} + +# LAW: a group is data exactly when it opens with `[` or `{` and what it +# holds and what follows it are data; a `(..)` group, a call's arguments or +# an expression, is none +law data_reads_group: + for k: Lex.TokKind + for t: String + for l: U32 + for c: U32 + for kids: Tree.Node + for close: Maybe<&2, Lex.Tok> + for rest: Tree.Node + {Size.data(Tree.NCons{Tree.Group{Lex.Tok{k, t, l, c}, kids, close}, rest}) + == Bool.and(bracket(t), Bool.and(Size.data(kids), Size.data(rest))) : Bool} + +# LAW: a statement among a list's elements is no data +law data_rejects_statement: + for kind: Tree.StmtKind + for kids: Tree.Node + for body: Tree.Node + for rest: Tree.Node + {Size.data(Tree.NCons{Tree.Stmt{kind, kids, body}, rest}) == False{} : Bool} diff --git a/proofs/SizeReportsOversizedDefs.bend b/proofs/SizeReportsOversizedDefs.bend index 163f108..700643a 100644 --- a/proofs/SizeReportsOversizedDefs.bend +++ b/proofs/SizeReportsOversizedDefs.bend @@ -10,6 +10,7 @@ import ../src/rules/calls.bend as Calls import ../src/rules/style/size.bend as Size import ../laws/SizeReportsOversizedDefs.bend as Laws import ./lemmas/LawFileExemptions.bend as M1 +import ./lemmas/HoleEarlyAnd.bend as M2 # a def that is exempt adds nothing, and one that is not adds one exactly # when it is over @@ -154,3 +155,75 @@ def Laws.table_arm_holds_one_statement(first, _second, _rest): {==} case Tree.NCons{head, tail}: {==} + +def Laws.table_takes_data_list(_kind, _k, _l, _c, elems, _close, e): + s = Equal.sym(Bool, Size.data(elems), True{}, e) + %s : {Bool.and(_, True{}) == True{} : Bool} + {==} + +def Laws.table_list_needs_data(_kind, _k, t, _l, _c, elems, _close, e): + s = Equal.sym(Bool, Size.data(elems), False{}, e) + %s : {Bool.and(_, String.eq(t, "[")) == False{} : Bool} + {==} + +def Laws.table_list_holds_nothing_under(_kind, _k, _t, _l, _c, _elems, _close, _first, _rest): + {==} + +def Laws.table_list_is_whole_statement(_kind, _k, _t, _l, _c, _elems, _close, next, more, under, e): + s = Equal.sym(Bool, Calls.has_colon(Tree.NCons{next, more}), False{}, e) + %s : {Lazy.or_else(Lazy.and_then(_, _u => Size.table.arms(under)), _u => False{}) == False{} : Bool} + {==} + +def Laws.data_reads_token(k, _t, _l, _c, _rest): + match k: + case Lex.TName{}: + {==} + case Lex.TUpper{}: + {==} + case Lex.TDotted{}: + {==} + case Lex.TWild{}: + {==} + case Lex.TKey{}: + {==} + case Lex.TNum{}: + {==} + case Lex.TStr{}: + {==} + case Lex.TChar{}: + {==} + case Lex.TComment{}: + {==} + case Lex.TSpace{}: + {==} + case Lex.TNewline{}: + {==} + case Lex.TOp{}: + {==} + case Lex.TColon{}: + {==} + case Lex.TEq{}: + {==} + case Lex.TBind{}: + {==} + case Lex.TArrow{}: + {==} + case Lex.TLam{}: + {==} + case Lex.TAll{}: + {==} + case Lex.TAmp{}: + {==} + case Lex.TOpen{}: + {==} + case Lex.TClose{}: + {==} + case Lex.TComma{}: + {==} + +def Laws.data_reads_group(_k, t, _l, _c, kids, _close, rest): + M2.hole.and(Size.data.bracket(t), Lazy.and_then(Size.data(kids), _v => Size.data(rest)), + Bool.and(Size.data(kids), Size.data(rest)), M2.hole.and(Size.data(kids), Size.data(rest), Size.data(rest), {==})) + +def Laws.data_rejects_statement(_kind, _kids, _body, _rest): + {==} diff --git a/src/README.md b/src/README.md index 52f961d..badd55c 100644 --- a/src/README.md +++ b/src/README.md @@ -411,7 +411,12 @@ statement. and a def that is a proof are left alone. `length` does not measure a table: a def whose body is one `match` with nothing under it but `case` arms, each holding at most one statement, which opens no block (an - opcode dispatch, a glyph for each character). A table has no step to + opcode dispatch, a glyph for each character), or whose body is one `[..]` + list of data and nothing else: numbers, strings, chars, capitalized and + dotted names and `[..]` and `{..}` groups of them, with no plain name (so + no variable and no call), no operator and no `(..)` (a generated list of + records, such as the HTML Standard's named character references). A + table has no step to move into a helper; the one way to shorten it is to cut it in two at some line, which helps no reader. The limits sit where public Bend 2 code stops (99 in 100 defs have 9 parameters or fewer, 19 lines or diff --git a/src/rules/style/size.bend b/src/rules/style/size.bend index a1e9d68..3f78484 100644 --- a/src/rules/style/size.bend +++ b/src/rules/style/size.bend @@ -6,7 +6,12 @@ # length its lines, from the `def` line to the line of its last token: # over 40. A table is not measured: a def whose body is one # `match` with nothing under it but `case` arms, each holding at -# most one statement, which opens no block +# most one statement, which opens no block; or a def whose body is +# one list of data, a `[..]` holding nothing but numbers, strings, +# chars, commas, capitalized and dotted names and `[..]` and +# `{..}` groups of them (no plain name, so no variable and no +# call; no operator; no `(..)`), with nothing after its `]` and +# no line under it # depth how deep its statements nest. The statements right under the def # are level 1, and a statement that opens a block (its own tokens # hold a `:`, as `match x:` and `case A:` do) puts the statements @@ -25,7 +30,9 @@ # inner match of a deep one, becomes a helper. A table has no step to move: # its length is how many cases there are, and the one way to shorten it is # to cut it in two at some line, which helps no reader. Of the 116 defs -# `length` reported in 82 public repos, 22 were tables. +# `length` reported in 82 public repos, 22 were tables. A generated list of +# records (a standard's 2231 named character references) is a table too: +# its length is how many rows there are. # The limits are where public Bend 2 code stops: 99 in 100 defs have 9 # parameters or fewer, 19 lines or fewer, and nest 6 levels or fewer. import Base @@ -141,12 +148,71 @@ def table.arms(ns: Tree.Node) -> Bool: case _other: False{} +# is this token one a list of data holds: a number, a string, a char, a +# comma, or a capitalized or dotted name (a constructor's, or a def's named +# and not called)? +def data.token(k: Lex.TokKind) -> Bool: + match k: + case Lex.TNum{}: + True{} + case Lex.TStr{}: + True{} + case Lex.TChar{}: + True{} + case Lex.TComma{}: + True{} + case Lex.TUpper{}: + True{} + case Lex.TDotted{}: + True{} + case _other: + False{} + +# does this bracket open a group of data: `[` or `{`, not a call's `(`? +def data.bracket(+t: String) -> Bool: + Bool.or(String.eq(t, "["), String.eq(t, "{")) + +# are these the elements of a list of data: tokens a list of data holds, and +# `[..]` and `{..}` groups of them, nothing else? +def data(ns: Tree.Node) -> Bool: + match ns: + case Tree.NNil{}: + True{} + case Tree.NCons{Tree.Leaf{Lex.Tok{k, _t, _l, _c}}, rest}: + Lazy.and_then(data.token(k), _u => data(rest)) + case Tree.NCons{Tree.Group{Lex.Tok{_k, t, _l, _c}, kids, _close}, rest}: + Lazy.and_then(data.bracket(t), _u => Lazy.and_then(data(kids), _v => data(rest))) + case _other: + False{} + +# is a chain empty? +def none(n: Tree.Node) -> Bool: + match n: + case Tree.NNil{}: + True{} + case _other: + False{} + +# is a statement a list of data: its own tokens one `[..]` group of data, +# with nothing under it? +def table.list(kids: Tree.Node, under: Tree.Node) -> Bool: + match kids: + case Tree.NCons{Tree.Group{Lex.Tok{_k, t, _l, _c}, elems, _close}, Tree.NNil{}}: + Bool.and(none(under), Bool.and(data(elems), String.eq(t, "["))) + case _other: + False{} + +# is a statement, a def's whole body, a table: it opens a block with the arms +# of a table under it, or it is a list of data +def table.stmt(+kids: Tree.Node, +under: Tree.Node) -> Bool: + Lazy.or_else(Lazy.and_then(Calls.has_colon(kids), _u => table.arms(under)), _u => table.list(kids, under)) + # is a def's body a table: one statement that opens a block, with the arms -# of a table under it? +# of a table under it, or one statement that is a list of data? def table(body: Tree.Node) -> Bool: match body: case Tree.NCons{Tree.Stmt{_kind, kids, arms}, Tree.NNil{}}: - Lazy.and_then(Calls.has_colon(kids), _u => table.arms(arms)) + table.stmt(kids, arms) case _other: False{}