~/bend-docscommunity

src/rules/suspicious/thunk.bend checks

raw source on the hub · import bolt@1.52.0.0/src/rules/suspicious/thunk.bend as Thunk

rule thunk: a lambda whose body is exactly a self-call of its def, and whose parameter the call does not read, when it is the one lambda among the arguments of the call it is passed to: a Unit -> T thunk such as Lazy.or_else(hit, _u => go(rest, k)). The thunk is a closure allocated on every step, and the call inside it is not a tail call of the def, so the search leaves the loop each time round (bend 2.0.34, 500 misses over 100k cells: JS 2.40 s against 0.28 s, native 1.18 s against 0.76 s). Carry the test as a Bool into the next call instead, go(rest, k, test(h)), and match on it first: the step is then a tail call and compiles to a loop. Lazy.* stays right for guarding work that does not recurse. A dispatch, a call given two or more lambdas (Lazy.either(T, c, _u => go(a), _v => go(b))), is left alone: one Bool carried into the next call does not replace it. Exactly: in the kids of a group opened by ( (its arguments: the kids split at their comma leaves), exactly one argument holding, among its own nodes and not inside a group, a leaf whose text is =>, four nodes in a row of those kids, a leaf whose kind is a name (the parameter: _, _u, u), a leaf whose text is =>, a leaf whose text is the def's name, and a ( group; the kids end right after the group or go on with a comma; and no name leaf in the group, at any depth, has the parameter's text or starts with it and a dot (u.x). Groups are found at any depth, under statements and inside other groups, and a lambda inside another lambda's body is judged by the group it is passed to. One finding per such lambda, on the def's name. A body that does more than the call (_u => go(t) ++ x, _u => Some{go(t)}), a call of another def, a lambda whose parameter the call reads (an IO continuation a => go(f, a)), and a lambda that is not an argument of a call (x = _u => go(t)) are left alone; one that ignores its value (_ => loop(n)) is not, since the rule reads the shape and not the type. Laws and proofs never run: a law file, a proof file and a def that is a proof are exempt.

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 reads source · line 40 · raw

@nn:0xfb394c8f685b7b3a6abb1443099643d8/src/syntax/tree.Node -> @+pp:String -> Bool

does a name leaf under the node read the parameter: its text, or its text then a dot?

def ends source · line 58 · raw

@nn:0xfb394c8f685b7b3a6abb1443099643d8/src/syntax/tree.Node -> Bool

does the lambda's body end after the call: the chain ends, or a comma?

def hit source · line 69 · raw

@+lead:Bool -> @+arrow:Bool -> @+own:Bool -> @+call:Bool -> @+end:Bool -> Bool

are the parts a thunk of a self-call: the lead (a name parameter the call does not read), =>, the def's name, a ( group, then the body's end?

def cite source · line 73 · raw

@+ll:U32 -> @+cc:U32 -> @+name:String -> @+path:String -> 0xfb394c8f685b7b3a6abb1443099643d8/src/finding.Finding

the finding at the self-call's name

def site.group source · line 79 · raw

@kp:0xfb394c8f685b7b3a6abb1443099643d8/src/syntax/lex.TokKind -> @+pp:String -> @+aa:String -> @+nm:String -> @+ll:U32 -> @+cc:U32 -> @r3:0xfb394c8f685b7b3a6abb1443099643d8/src/syntax/tree.Node -> @+name:String -> @+path:String -> List<&2, 0xfb394c8f685b7b3a6abb1443099643d8/src/finding.Finding>

after the parameter, =>, the name and its ( group: the finding when they make a thunk of a self-call

def site.name source · line 100 · raw

@kp:0xfb394c8f685b7b3a6abb1443099643d8/src/syntax/lex.TokKind -> @+pp:String -> @+aa:String -> @r2:0xfb394c8f685b7b3a6abb1443099643d8/src/syntax/tree.Node -> @+name:String -> @+path:String -> List<&2, 0xfb394c8f685b7b3a6abb1443099643d8/src/finding.Finding>

after the parameter and =>: the leaf that names the call

def site.lam source · line 115 · raw

@kp:0xfb394c8f685b7b3a6abb1443099643d8/src/syntax/lex.TokKind -> @+pp:String -> @rest:0xfb394c8f685b7b3a6abb1443099643d8/src/syntax/tree.Node -> @+name:String -> @+path:String -> List<&2, 0xfb394c8f685b7b3a6abb1443099643d8/src/finding.Finding>

after the parameter: the leaf that should be =>

def site source · line 123 · raw

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

the finding when a node and the chain after it open a thunk of a self-call

def lams source · line 132 · raw

@nn:0xfb394c8f685b7b3a6abb1443099643d8/src/syntax/tree.Node -> @+seen:Bool -> Nat

how many arguments of a group's kids, from here on, hold a => leaf among their own nodes; seen: the argument under way does

def only source · line 144 · raw

@+oo:String -> @+kids:0xfb394c8f685b7b3a6abb1443099643d8/src/syntax/tree.Node -> Bool

is a group with this open and these kids a call given exactly one lambda?

def walk source · line 149 · raw

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

every thunk of a self-call under the node, at any depth; lone: the node is the rest of the kids of a call given exactly one lambda

def check.go source · line 162 · raw

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

def check source · line 171 · raw

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

the rule