Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 121 additions & 1 deletion laws/SizeReportsOversizedDefs.bend
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))
Expand Down Expand Up @@ -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}
73 changes: 73 additions & 0 deletions proofs/SizeReportsOversizedDefs.bend
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
{==}
7 changes: 6 additions & 1 deletion src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 70 additions & 4 deletions src/rules/style/size.bend
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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{}

Expand Down
Loading