~/bend-docscommunity

src/rules/correctness/pick.bend checks

raw source on the hub · import bolt@1.62.0.0/src/rules/correctness/pick.bend as Pick

rule pick: a def calls itself in a branch of a Bool.pick. Bool.pick is a function, so both branches are evaluated whatever the condition. In both branches, two recursive calls a step is 2^n work where one was meant (a per-token scan that took 20 s this way took 20 ms as one pass): bind the call once above the pick (+more = go(rest)) and pick between x <> more and more. In one branch, the recursion runs even when the other branch was the answer: a search never stops early and walks the whole input (a lookup by key, a sorted insert). Match on the Bool instead, in a helper that takes it as a parameter. A self-call is the def's name followed by (..): a parameter or a value named like the def is not one. A pick nested in a branch of one already reported is not reported again; one nested in its condition is. A search can hide the same bug by binding its recursion above the pick (+more = go(rest), then Bool.pick(T, hit, x, more)): the walk still runs when the head is the answer (ez's text.at, native 8.81 s -> 4.15 s once it stopped). search reports that let, when the first statement after it in the same chain that reads the name is a pick alone that reads it only in its else branch: a filter, which reads it in both, is not a search. A do-bind, a let and a pick in different case arms, and a self-call that holds a pick on the same condition (which makes the call cheap on purpose) are left alone. The fix is to carry the test as a Bool into the next call and match hit first when the else branch is the bound name alone (a tail call), else a helper that matches on the condition; not a Lazy thunk, which stops early but leaves the loop on every step. rules.bend runs check and search both, as one rule. A law file, a proof file and a def that is a proof never run, so they are exempt: a def that returns a proof, or one written with no type at all (def f(x, y):), which fills the law named f.

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 ../calls.bend as Calls
import ../../lazy/lazy.bend as Lazy

Definitions

def both source · line 38 · raw

@+as:List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node> -> @+name:String -> Bool

do both branches (the third and fourth arguments) call the def?

def once source · line 42 · raw

@+as:List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node> -> @+name:String -> Bool

does either branch call the def?

def mute source · line 47 · raw

@+quiet:Bool -> @+hush:Bool -> @nth:Nat -> Bool

is argument nth of a group quiet: the group's own quiet, or, in a branch (the third or fourth argument) of a reported pick, hushed

def next source · line 61 · raw

@k:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/lex.TokKind -> @nth:Nat -> Nat

the argument a token leaves the walk in: one further past a top-level comma

def walk source · line 72 · raw

@nn:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+name:String -> @+path:String -> @+quiet:Bool -> @+hush:Bool -> @+nth:Nat -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>

every Bool.pick(..) in a chain, at any depth, that recurses in both branches or in one. The chain is argument nth of a group with quiet (inside a branch of a reported pick) and hush (the group is a reported pick, so its branches are quiet); a pick in the condition still counts.

def picks source · line 106 · raw

@nn:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+name:String -> @+path:String -> @+quiet:Bool -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>

every reported pick in a chain walked at quiet, outside any pick's arguments

def check.go source · line 109 · raw

@ds:List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/rules/calls.Def> -> @+path:String -> @acc:List<&2, List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>> -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>

def check source · line 119 · raw

@s:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/src.Src -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>

the rule

def search.reads source · line 134 · raw

@n:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+x:String -> Bool

does a name leaf under the node spell x?

def search.text source · line 152 · raw

@n:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> String

the node's tokens, each then a space (a group's open, not its close)

def search.cheap source · line 171 · raw

@nn:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+cond:String -> Bool

does a Bool.pick(..) under the node, at any depth, have the condition whose text is cond?

def search.empty source · line 194 · raw

@n:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> Bool

is the chain empty?

def search.lone source · line 202 · raw

@n:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+x:String -> Bool

is the chain the one name x alone?

def search.cite source · line 211 · raw

@+l:U32 -> @+c:U32 -> @+name:String -> @+path:String -> @tail:Bool -> 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding

the finding at the bound self-call; tail: the else branch is the bound name alone, so the call can carry the test instead

def search.judge source · line 223 · raw

@s:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+x:String -> @+args:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+l:U32 -> @+c:U32 -> @+name:String -> @+path:String -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>

the statement that reads the bound name first: a finding when it is a Bool.pick(..) alone that reads the name only in its else branch, and the self-call (its arguments args) holds no pick on the same condition

def search.first source · line 249 · raw

@n:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+x:String -> @prev:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @hit:Bool -> 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node

the first statement of the chain that reads x, the chain's end (NNil) when none does; hit: prev, the one before the chain, did

def search.call source · line 267 · raw

@kx:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/lex.TokKind -> @+x:String -> @+e:String -> @callee:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/lex.Tok -> @r3:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @rest:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+name:String -> @+path:String -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>

past the bound name, = and the callee (the token that names the call): a ( group and nothing after it, and then the finding the first reader of the name gives

def search.callee source · line 288 · raw

@kx:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/lex.TokKind -> @+x:String -> @+e:String -> @r2:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @rest:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+name:String -> @+path:String -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>

past the bound name and =: the leaf that names the call

def search.eq source · line 304 · raw

@kx:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/lex.TokKind -> @+x:String -> @r1:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @rest:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+name:String -> @+path:String -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>

past the bound name: the leaf that should be =

def search.bound source · line 319 · raw

@n:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @rest:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+name:String -> @+path:String -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>

a let's own tokens past any +: the leaf of the bound name

def search.lead source · line 327 · raw

@kids:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @rest:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+name:String -> @+path:String -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>

a let's own tokens, a leading + skipped

def search.stmt source · line 337 · raw

@kind:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.StmtKind -> @kids:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @rest:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+name:String -> @+path:String -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>

a statement of the kind and own tokens, the chain after it rest: a let is read, anything else gives nothing

def search.walk source · line 352 · raw

@n:0x6d68cd62cf70fccf6858a2f9885ddbe4/src/syntax/tree.Node -> @+name:String -> @+path:String -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>

every search bound above its pick in a chain of statements and in the chains under them

def search.go source · line 362 · raw

@ds:List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/rules/calls.Def> -> @+path:String -> @acc:List<&2, List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>> -> List<&2, 0x6d68cd62cf70fccf6858a2f9885ddbe4/src/finding.Finding>