src/rules/correctness/arms.bend checks
raw source on the hub · import bolt@1.44.0.0/src/rules/correctness/arms.bend as Arms
rule arms: a Nat arm no value can reach. case kn+p: matches every Nat
from k up, so a later case jn+q: with j >= k, or a later case mn: with
m >= k, never runs. The checker accepts it and the match quietly returns
the earlier arm (nohzafk pat_bad: 0n, 1n+p, 2n+p gives f(2n) = 1; bend
2.0.16 still does). Put the narrower arms first: the literals, then the
larger n+p. Succ{p} and Succ{_} count as 1n+p. Only a
single-scrutinee match is read: a column of a multi-scrutinee match is
skipped.
The same holds past Nat (cover, below): the checker takes any arm an
earlier arm of its match already matches, and the match quietly answers
with the earlier one. A lone binder (case other:) above a constructor
arm, the same literal twice (case 139: in a table of opcodes), and two
arms of one shape under different names (two six-item list patterns, each
meant for its own mode) all check and run. An earlier arm covers a later
one when their patterns read the same part for part, save that a binder of
the earlier (a plain name or _) stands for a whole item of the later: a
binder, a literal, a constructor with its {..}, a group, a kn+ and
what it adds to, and, when no <> follows the binder, every <> item
after that one too. A match of any number of scrutinees is read, column by
column. An arm the Nat check above reports is not reported again. What is
not understood is not judged: a string literal is never the same as
another (what a string says is not code), and any other operator, an angle
bracket or a keyword inside a pattern covers nothing and is covered only
inside an item a binder stands for; and a pattern that wraps onto the next
line, which the tree holds only in part, is not read at all (mylsm's
36-item list patterns, each cut at the same <>, would else read alike).
7 imports
import Base import ../../src.bend as Src import ../../finding.bend as F import ../../syntax/lex.bend as Lex import ../../syntax/tree.bend as Tree import ../../lazy/lazy.bend as Lazy import ../tokens.bend as T
Types
type Arm source · line 37 · raw
Data
what an arm matches: from k up (text is its pattern), exactly k, or other
APlus@k:U32 -> @text:String -> @line:U32 -> @col:U32 -> @len:U32 -> Arm
ALit@k:U32 -> @line:U32 -> @col:U32 -> @len:U32 -> Arm
AOtherArm
type Wide source · line 43 · raw
Data
the widest n+p arm so far: its k and its pattern
Wide@k:U32 -> @text:String -> Wide
type Part source · line 168 · raw
Data
one part of a pattern, as the cover check reads it: a binder; a word (a
constructor's name, or a number or char literal), the same as another only
by its text; kn+, which adds to the item after it; a string literal, the
same as nothing; a bracket; a comma; <>; and anything else, which is
not understood
PAnyPart
PWord@text:String -> Part
PPlus@text:String -> Part
PStrPart
POpen@text:String -> Part
PClose@text:String -> Part
PCommaPart
PConsPart
PBadPart
type Skip source · line 301 · raw
Data
where a walk over one item of a pattern is: an item starts here; one just
ended; a word just ended, whose {..} may follow; inside brackets, this
many deep past the first; or the parts were no item
SWantSkip
SGotSkip
SWordSkip
SIn@depth:Nat -> Skip
SFailSkip
type Seen source · line 414 · raw
Data
an arm above: its line and its pattern's parts
Seen@line:U32 -> @parts:List<&2, Part> -> Seen
Definitions
def binder source · line 47 · raw
@kk:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/lex.TokKind -> Bool
a lone binder, inside Succ{..} or after kn+: a name or _
def lit.shape source · line 57 · raw
@rest:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/tree.Node -> @+kk:U32 -> @+tt:String -> @+line:U32 -> @+col:U32 -> Arm
kn+p: (p a name or _) or kn:
def lit.of source · line 69 · raw
@mm:Maybe<&2, U32> -> @+tt:String -> @+rest:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/tree.Node -> @+line:U32 -> @+col:U32 -> Arm
a Nat literal token's arm, once its value is read
def arm source · line 77 · raw
@pat:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/tree.Node -> Arm
a case pattern's arm
def widen source · line 89 · raw
@ww:Maybe<&2, Wide> -> @aa:Arm -> Maybe<&2, Wide>
the widest n+p arm after this one
def verdict.on source · line 99 · raw
@+kk:U32 -> @+text:String -> @aa:Arm -> @+path:String -> @+more:List<&2, 0x4fbe18d4c1bba3b659645f7db7caf56f/src/finding.Finding> -> List<&2, 0x4fbe18d4c1bba3b659645f7db7caf56f/src/finding.Finding>
a finding when the arm is inside the widest one above
def verdict source · line 117 · raw
@ww:Maybe<&2, Wide> -> @aa:Arm -> @+path:String -> @more:List<&2, 0x4fbe18d4c1bba3b659645f7db7caf56f/src/finding.Finding> -> List<&2, 0x4fbe18d4c1bba3b659645f7db7caf56f/src/finding.Finding>
the arm against the widest one above, if any
def arms source · line 125 · raw
@body:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/tree.Node -> @+wide:Maybe<&2, Wide> -> @+path:String -> List<&2, 0x4fbe18d4c1bba3b659645f7db7caf56f/src/finding.Finding>
a match's arms in order, against the widest n+p arm above each
def single source · line 136 · raw
@kids:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/tree.Node -> Bool
match x:: one scrutinee
def walk source · line 145 · raw
@nn:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/tree.Node -> @+path:String -> List<&2, 0x4fbe18d4c1bba3b659645f7db7caf56f/src/finding.Finding>
every statement, at any depth; a single-scrutinee match's arms are read
def check source · line 156 · raw
@ss:0x4fbe18d4c1bba3b659645f7db7caf56f/src/src.Src -> List<&2, 0x4fbe18d4c1bba3b659645f7db7caf56f/src/finding.Finding>
the rule
def cut source · line 180 · raw
@pat:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/tree.Node -> 0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/tree.Node
a pattern's nodes up to the : that ends it, an inline body left out
def ended source · line 191 · raw
@pat:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/tree.Node -> Bool
does the pattern end among its own nodes, at a :? One that wraps onto
the next line does not: its last part is a child statement in the tree
def opens.as source · line 201 · raw
@want:Bool -> @+tt:String -> @+text:String -> Bool
do the tokens open with this text, as a token of the kind wanted?
def opens.open source · line 205 · raw
@kk:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/lex.TokKind -> Bool
is it a bracket that opens a group?
def opens.op source · line 213 · raw
@kk:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/lex.TokKind -> Bool
is it an operator?
def opens.brace source · line 221 · raw
@toks:List<&2, 0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/lex.Tok> -> Bool
do the tokens open with {?
def opens.plus source · line 229 · raw
@toks:List<&2, 0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/lex.Tok> -> Bool
do the tokens open with the operator +?
def part.open source · line 237 · raw
@+tt:String -> Part
an opening bracket's part: (, [ or {; an angle bracket is not read
def part.op source · line 243 · raw
@+tt:String -> @+more:List<&2, Part> -> List<&2, Part>
an operator's part before the parts after it: <> is itself, + (a
quantity mark, or the plus of kn+, which the number reads) is nothing,
and any other is not understood
def part source · line 250 · raw
@kk:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/lex.TokKind -> @+tt:String -> @brace:Bool -> @plus:Bool -> @more:List<&2, Part> -> List<&2, Part>
a token's part before the parts after it. brace: a { follows it (a
plain name is then a constructor); plus: a + does (a number is then
kn+)
def parts source · line 278 · raw
@toks:List<&2, 0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/lex.Tok> -> List<&2, Part>
a pattern's tokens as parts, in order
def read.on source · line 285 · raw
@ok:Bool -> @pat:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/tree.Node -> List<&2, Part>
def read source · line 295 · raw
@+pat:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/tree.Node -> List<&2, Part>
a case pattern's parts: its tokens up to the : that ends it, read; one
part that is not understood when it has no : (it wraps onto the next
line), so a pattern read only in part is never judged
def skip.want source · line 309 · raw
@pb:Part -> Skip
where the walk is after the part an item starts with
def skip.close source · line 325 · raw
@dd:Nat -> Skip
where the walk is after a closing bracket this deep
def skip.in source · line 333 · raw
@pb:Part -> @dd:Nat -> Skip
where the walk is after a part inside brackets this deep
def skip source · line 344 · raw
@pb:List<&2, Part> -> @st:Skip -> @chain:Bool -> Maybe<&2, List<&2, Part>>
the parts left once one item is walked over, and, when chain says so,
every <> item after it; none when the parts open with no item
def step.same source · line 368 · raw
@+ta:String -> @+tb:String -> @rest:List<&2, Part> -> Maybe<&2, List<&2, Part>>
the parts, when the two texts are the same
def step source · line 375 · raw
@pa:Part -> @one:Bool -> @pb:List<&2, Part> -> Maybe<&2, List<&2, Part>>
what is left of a later pattern once one part of an earlier one is read
against it: a binder walks over an item (and the <> items after it
unless one is true: a <> follows the binder); any other part takes the
same part; none when the later pattern has neither
def consed source · line 395 · raw
@ps:List<&2, Part> -> Bool
do the parts open with <>?
def covers source · line 404 · raw
@pa:List<&2, Part> -> @pb:Maybe<&2, List<&2, Part>> -> Bool
does an earlier pattern cover a later one? pb: what is left of the later, none once the earlier could not go on
def above source · line 419 · raw
@seen:List<&2, Seen> -> @+pb:List<&2, Part> -> @hit:Bool -> @+at:U32 -> Maybe<&2, U32>
the line of the nearest arm above whose pattern covers these parts, if any. hit carries whether the last one read did, at its line
def told source · line 429 · raw
@ww:Maybe<&2, Wide> -> @aa:Arm -> Bool
does the Nat check report the arm against the widest n+p arm above it?
def cover.hit source · line 441 · raw
@mm:Maybe<&2, U32> -> @+path:String -> @ll:U32 -> @cc:U32 -> @more:List<&2, 0x4fbe18d4c1bba3b659645f7db7caf56f/src/finding.Finding> -> List<&2, 0x4fbe18d4c1bba3b659645f7db7caf56f/src/finding.Finding>
the finding a covered arm makes, if any, before the rest
def cover.find source · line 452 · raw
@skip:Bool -> @+seen:List<&2, Seen> -> @+ps:List<&2, Part> -> Maybe<&2, U32>
the line of the arm above that covers this one, unless the Nat check reports it
def cover.arms source · line 461 · raw
@body:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/tree.Node -> @+seen:List<&2, Seen> -> @+wide:Maybe<&2, Wide> -> @+nat:Bool -> @+path:String -> List<&2, 0x4fbe18d4c1bba3b659645f7db7caf56f/src/finding.Finding>
a match's arms in order, each against the arms above it. nat: the match has one scrutinee, so the Nat check reads it, wide its widest n+p arm so far
def cover.walk source · line 481 · raw
@nn:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/tree.Node -> @+path:String -> List<&2, 0x4fbe18d4c1bba3b659645f7db7caf56f/src/finding.Finding>
every statement, at any depth; a match's arms are read
def blank.text source · line 494 · raw
@kk:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/lex.TokKind -> @tt:String -> String
a token's text where it is code: what a string literal or a comment says is left out
def blank.tok source · line 504 · raw
@tk:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/lex.Tok -> 0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/lex.Tok
a token with what it says left out when it is a string or a comment
def blank source · line 511 · raw
@nn:0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/tree.Node -> 0x4fbe18d4c1bba3b659645f7db7caf56f/src/syntax/tree.Node
a tree with each leaf read that way: what the cover check reads, so what a string says can never change what it finds
def cover source · line 525 · raw
@ss:0x4fbe18d4c1bba3b659645f7db7caf56f/src/src.Src -> List<&2, 0x4fbe18d4c1bba3b659645f7db7caf56f/src/finding.Finding>
the rule's second check: an arm an earlier arm of its match covers