linter/api/spec.bend open laws/TODOs
raw source on the hub · import bend-lint-api@0.3.7.0/linter/api/spec.bend as Spec
What a rule sees. A rule file imports this file, and defines rule().
Design
- The rules run in an ordinary Bend program that seam.ts writes from bend-lint.json, builds once and keeps. The C
lane is the target; the JS lane must work too.
- Bend's checker is TS for now: seam.ts makes each check's program of what Bend's parser and checker give, and answers
the questions rules ask about its types, over local TCP (see Seam in cli.bend). When the checker is Bend, glue in
Bend makes the same program, and the questions become calls.
- A file is a tree of syntax nodes. A rule walks it, and reads what a node is from the node and the nodes above it.
- Rules are pure, so they run in parallel. The runner answers every rule's questions of a round in one batch.
- A check is read once and shared by every rule. Facts and declarations are collected only if a rule needs them.
- A rule requests a fix as typed operations, and the linter plans them. Until Bend's checker is Bend, no fix is
proven to keep the program: --fix-dangerously makes them all, and --fix makes none.
2 imports
import Base import ../../shared.bend as Shared
Laws
law Imported provedin api.bendsource · line 167 · raw
Data
What uses a file's imports.
law Uses provedin api.bendsource · line 171 · raw
Data
What the declarations of a top-level declaration are read with.
law Answer provedin api.bendsource · line 215 · raw
Data
What the checker answers to a question.
law Answer.shown provedin api.bendsource · line 219 · raw
@text:String -> Answer
The answer to a Show: the type, as text spells it.
law Answer.equal provedin api.bendsource · line 224 · raw
@same:Bool -> Answer
The answer to a Same: whether the two types are the same (same).
law Answer.normalized provedin api.bendsource · line 229 · raw
@handle:U32 -> Answer
The answer to a Normal: its normal form, by the number the checker gives it (handle).
law Syntax.declares provedin api.bendsource · line 362 · raw
@syntax:Syntax -> Maybe<&2, Bool>
Whether a node of this kind declares a global name (Some{True{}}), a binder of its top-level declaration (Some{False{}}), or nothing (an import declares an alias, which Declarations below handles).
law Node.target provedin api.bendsource · line 367 · raw
@node:Node -> String
"" for a node of a kind that names nothing.
law Node.kids provedin api.bendsource · line 371 · raw
@node:Node -> List<&2, Node>
law Node.name provedin api.bendsource · line 376 · raw
@node:Maybe<&2, Node> -> String
"" for no node, as are the next two.
law Node.span provedin api.bendsource · line 380 · raw
@node:Maybe<&2, Node> -> Maybe<&2, Span>
law Span.text provedin api.bendsource · line 384 · raw
@source:String -> @span:Maybe<&2, Span> -> String
law Name.namespace provedin api.bendsource · line 390 · raw
@name:String -> String
The namespace of a global name: what comes before its first ":", or "" for Base and the checked file's own names.
law Node.is_bare provedin api.bendsource · line 396 · raw
@node:Node -> Bool
Whether a parameter is written bare (def f(x), filling a law): its quantity is the law's, which its node does not
give.
law Node.scrutinees provedin api.bendsource · line 401 · raw
@node:Node -> List<&2, Node>
The scrutinees of a match, in order: its nodes before its first case.
law Node.bound provedin api.bendsource · line 406 · raw
@nodes:List<&2, Node> -> @out:List<&2, U32> -> List<&2, U32>
The binders of the pattern variables in nodes, patterns, and under them, onto out.
law top provedin api.bendsource · line 412 · raw
@path:List<&2, Above> -> @node:Node -> Node
The top-level declaration a node is in: the outermost node above it, or the node itself.
law is_recursive provedin api.bendsource · line 419 · raw
@path:List<&2, Above> -> @+node:Node -> Bool
Whether a node is an application of the definition it is in, below path: a step of a loop, as Bend repeats work by
recursion.
law is_pair provedin api.bendsource · line 425 · raw
@kids:List<&2, Node> -> Bool
Whether an application's function is Pair: it builds a pair type (A & B), whose parts are types.
law is_live provedin api.bendsource · line 432 · raw
@path:List<&2, Above> -> Bool
Whether a node below path is in live code: a definition's body, outside types: an annotation's type, a rewrite's
motive, an equality, a function or pair type, a type's arguments. Types and equations are dead: the checker sees them,
the program does not run them.
law definitions provedin api.bendsource · line 438 · raw
@nodes:List<&2, Node> -> @out:Map<&2, Maybe<&2, Node>> -> Map<&2, Maybe<&2, Node>>
The definitions of nodes, by the name the check gives them: read one with Shared.Names.find. Of two with one name, the
later is kept.
law parameter provedin api.bendsource · line 444 · raw
@nodes:List<&2, Node> -> @binder:U32 -> Maybe<&2, Nat>
Where the parameter that declares binder is among nodes, a definition's, counting from 0.
law finding provedin api.bendsource · line 450 · raw
@severity:Severity -> @hit:Bool -> @span:Maybe<&2, Span> -> @text:String -> List<&2, Finding>
A finding at span, if hit and span is known.
law Value.number provedin api.bendsource · line 458 · raw
@value:Maybe<&2, Value> -> U32
An option's value as a number, a flag or text; a value of another kind, or none, gives 0, False, "".
law Value.text provedin api.bendsource · line 462 · raw
@value:Maybe<&2, Value> -> String
law Program.own provedin api.bendsource · line 467 · raw
@sources:List<&2, Source> -> List<&2, Source>
The program's own files: not hub packages.
law type_name provedin api.bendsource · line 472 · raw
@nodes:List<&2, Node> -> String
The type the first of nodes writes, as one name, or "" for another form (an application, a list).
law forks provedin api.bendsource · line 477 · raw
@nodes:List<&2, Node> -> @found:Bool -> Bool
Whether a parallel let (two binders, at least) is among nodes or under them, or found.
law Def.forks provedin api.bendsource · line 482 · raw
@info:Maybe<&2, Def> -> Bool
law Program.defs provedin api.bendsource · line 487 · raw
@sources:List<&2, Source> -> @out:Map<&2, Maybe<&2, Def>> -> Map<&2, Maybe<&2, Def>>
The definitions of the program's files, by the name the check gives them, onto out: read one with Shared.Names.find.
law Program.callers provedin api.bendsource · line 496 · raw
@sources:List<&2, Source> -> @+defs:Map<&2, Maybe<&2, Def>> -> @seeds:Set -> Set
seeds, and the definitions of the program (defs, of sources) that call one of them, directly or through others.
A definition calls only those above it, unless it is @unsafe, and the check gives a file's imports before it, so the
first pass, in order, finds nearly all. Each pass but the last adds one at least, so as many passes as there are
definitions find them all.
law Here.of provedin api.bendsource · line 504 · raw
@program:Program -> @+source:Source -> @declare:Bool -> Here
The program from one of its files, built once for every file rule that runs on that file: its declarations, if a rule
needs them (declare). The runner gives each rule its options.
law Whole.of provedin api.bendsource · line 512 · raw
@program:Program -> @declare:Bool -> Whole
The whole program, built once for every program rule: the declarations of its own files, if a rule needs them
(declare).
law Here.path provedin api.bendsource · line 517 · raw
@here:Here -> String
law Here.namespace provedin api.bendsource · line 522 · raw
@here:Here -> String
"" for the checked file.
law Here.text provedin api.bendsource · line 526 · raw
@here:Here -> String
law Here.nodes provedin api.bendsource · line 531 · raw
@here:Here -> List<&2, Node>
The file's nodes: its imports, then its declarations.
law Here.sources provedin api.bendsource · line 536 · raw
@here:Here -> List<&2, Source>
Every file of the check, hub packages too.
law Here.base provedin api.bendsource · line 542 · raw
@here:Here -> List<&2, Node>
Base's declarations, which no file of the check holds: each with its parameters and the type it gives, without its body, and each constructor under its type.
law Here.declarations provedin api.bendsource · line 546 · raw
@here:Here -> List<&2, Declaration>
law Here.option provedin api.bendsource · line 551 · raw
@here:Here -> @name:String -> Maybe<&2, Value>
The config's value, or the default.
law Whole.declarations provedin api.bendsource · line 556 · raw
@whole:Whole -> List<&2, Declaration>
law done provedin api.bendsource · line 560 · raw
@findings:List<&2, Finding> -> Ask(List<&2, Finding>)
law show_all provedin api.bendsource · line 565 · raw
@types:List<&2, TypeRef> -> Ask(List<&2, String>)
How each type of types is shown, in order, asked in one round.
Types
type Quantity source · line 17 · raw
Data
ErasedQuantity
OnceQuantity
ManyQuantity
TemplateQuantity
type Span source · line 24 · raw
Data
Where something is: its file, and offsets in code points.
Span@path:String -> @beg:U32 -> @end:U32 -> Span
type Syntax source · line 32 · raw
Data
What a syntax node is, as Bend's parser reads it: a declaration, a part of one, a term, or a pattern; with what only
nodes of its kind have: the global name it declares or uses (target, as the check names it; an import's is the path
of the file it loads), the binder it declares or a variable uses (numbered per top-level declaration), and its
quantity as written (a ~ parameter's is Template). An import's written path is an ImportPath under it; import
Base writes none. A Definition is unsafe if Bend's checker trusts its body (@unsafe def f, or def f?).
Import@target:String -> Syntax
ImportPathSyntax
Definition@target:String -> @unsafe:Bool -> Syntax
Datatype@target:String -> Syntax
Constructor@target:String -> Syntax
Law@target:String -> Syntax
Parameter@binder:U32 -> @quantity:Quantity -> Syntax
Field@binder:U32 -> @quantity:Quantity -> Syntax
ReturnsSyntax
BodySyntax
Variable@binder:U32 -> Syntax
Reference@target:String -> Syntax
SubstitutionSyntax
LetSyntax
Binding@binder:U32 -> @quantity:Quantity -> Syntax
SortSyntax
QuantityTypeSyntax
QuantityLiteral@quantity:Quantity -> Syntax
QuantityMinimumSyntax
Forall@binder:U32 -> @quantity:Quantity -> Syntax
Lambda@binder:U32 -> @quantity:Maybe<&2, Quantity> -> Syntax
ApplicationSyntax
TypeApplication@target:String -> Syntax
Construction@target:String -> Syntax
Literal@target:String -> Syntax
Elimination@target:String -> Syntax
AbsurdSyntax
EqualitySyntax
ReflexivitySyntax
RewriteSyntax
HoleSyntax
AnnotationSyntax
MatchSyntax
CaseSyntax
Destructure@quantity:Quantity -> Syntax
PatternVariable@binder:U32 -> @quantity:Quantity -> Syntax
PatternConstructor@target:String -> Syntax
type TypeRef source · line 73 · raw
Data
A type the checker holds: a handle, good in the check it came from. Rules ask about it (show, same, normal). Two types
are the same only over the binders their scopes share: A of one def is not A of another.
TypeRef@check:U32 -> @handle:U32 -> TypeRef
type Role source · line 78 · raw
Data
What a fact says of its node: the type its term has, or the type its binder declares (a parameter, field, pattern variable, lambda, forall or let binding).
OfTermRole
OfBinderRole
type Fact source · line 88 · raw
Data
What the checker found for a node of the source: what the fact says of it, the quantity (how often the term is demanded, or the binder's), for a binder of a parameter, lambda, let or case, how often its body uses it (the most of its checked copies; none for a parameter a match takes apart), and the type. A fact is never wrong, but a node can have none: a term the checker rewrote into another (a scrutinee used again, a call a template took) is not typed as itself. A node has one fact per distinct type: pattern compilation can check one term in several branches, under different refinements.
Fact@role:Role -> @quantity:Quantity -> @used:Maybe<&2, Quantity> -> @checked:TypeRef -> Fact
type Node source · line 94 · raw
Data
A syntax node: what it is, its name as written, where the file writes it (none for what the parser adds), the nodes under it, in order, and the checker's facts on it, if a rule needs them. An application's nodes are its function, then its arguments; a block's, its binders, then their values, then the rest of the block.
Node@syntax:Syntax -> @name:String -> @span:Maybe<&2, Span> -> @kids:List<&2, Node> -> @facts:List<&2, Fact> -> Node
type Source source · line 105 · raw
Data
A file the check parsed: its path, its namespace in the check ("" for the checked file), its text, and its nodes: its imports, then its declarations, each constructor under its type.
Source@path:String -> @namespace:String -> @text:String -> @nodes:List<&2, Node> -> Source
type Program source · line 109 · raw
Data
The files of a check, and Base's declarations (see Here.base).
Program@sources:List<&2, Source> -> @base:List<&2, Node> -> Program
type Declaration source · line 113 · raw
Data
A name a node declares, and the nodes that use it, in order.
Declaration@node:Node -> @references:List<&2, Node> -> Declaration
type Above source · line 117 · raw
Data
A node above another: the node, and the place among its nodes (from 0) of the one on the way down.
Above@node:Node -> @at:U32 -> Above
type Severity source · line 120 · raw
Data
ErrorSeverity
WarningSeverity
InformationSeverity
HintSeverity
type Operation source · line 132 · raw
Data
An edit a fix asks for: replace a span's text; rename what the declaration written at at declares, with each of its
uses; or rename only its uses, to point them at a name the program declares already (to drop a duplicate import,
say). A rename changes an import's alias, the first part of each use (U of U.two), and any other name's last part.
The linter finds the uses, and drops the fix if the text does not write the name where one of them is. It drops a
Rename of a global name (a def, type, constructor or law), as files the check did not read can use it, and one whose
new name is taken where the declaration is.
Replace@span:Span -> @text:String -> Operation
Rename@at:Span -> @to:String -> Operation
RenameUses@at:Span -> @to:String -> Operation
type Fix source · line 139 · raw
Data
A way to fix a finding: what it does, and its operations, in any of the program's own files (not Base, nor a hub package).
Fix@title:String -> @operations:List<&2, Operation> -> Fix
type Finding source · line 142 · raw
Data
Finding@severity:Severity -> @span:Span -> @message:String -> @fixes:List<&2, Fix> -> Finding
type Found source · line 145 · raw
Data
Found@rule:String -> @finding:Finding -> Found
type Value source · line 148 · raw
Data
Num@n:U32 -> Value
Flag@on:Bool -> Value
Chars@text:String -> Value
type Spec source · line 155 · raw
Data
What an option accepts: a whole number in a range, a flag, text (one of choices, unless there are none), or what
one of specs accepts.
NumberOption@min:U32 -> @max:U32 -> Spec
FlagOptionSpec
TextOption@choices:List<&2, String> -> Spec
AnyOption@specs:List<&2, Spec> -> Spec
type Declared source · line 163 · raw
Data
An option a rule declares: its name, its value when the config gives none, and what it accepts. The config gives
values as "rule/id": {"name": value}; a name the rule does not declare, or a value it does not accept, is an error.
Declared@name:String -> @default:Value -> @spec:Spec -> Declared
type Param source · line 175 · raw
Data
A runtime parameter of a definition: its name, and its type as one name ("" if it is not one name).
Param@name:String -> @ty:String -> Param
type Def source · line 180 · raw
Data
A definition of the program: its name as the check names it, its runtime parameters, whether it forks, the definitions it calls by name, and the type it returns, as one name ("" if it is not one name).
Def@target:String -> @params:List<&2, Param> -> @forks:Bool -> @calls:List<&2, String> -> @returns:String -> Def
type Here source · line 191 · raw
Data
What a file rule sees: the program, from one of its files; that file's declarations, with their uses in it, if a rule needs them; and the rule's options, each declared one with its value.
Here@program:Program -> @source:Source -> @declarations:List<&2, Declaration> -> @options:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/shared.Names.Named<Value>> -> Here
type Whole source · line 201 · raw
Data
What a program rule sees: the program (its files, hub packages aside); every declaration of its files, with their uses in all of them, if a rule needs them; and the rule's options.
Whole@program:Program -> @declarations:List<&2, Declaration> -> @options:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/shared.Names.Named<Value>> -> Whole
type Question source · line 209 · raw
Data
Show spells a type as its file does; Same compares as the checker does.
Show@of:TypeRef -> Question
Same@a:TypeRef -> @b:TypeRef -> Question
Normal@of:TypeRef -> Question
type Step source · line 234 · raw
@-R:Type -> Type
A run so far: finished, or waiting for the answers to its questions, in order.
Finished@-R:Type -> @value:R -> Step<R>
Pending@-R:Type -> @questions:List<&2, Question> -> @then:(@_:List<&2, Answer> -> Step<R>) -> Step<R>
type Needs source · line 240 · raw
Data
What a rule needs collected for it, as each costs time and memory: the checker's facts, and the declarations with their uses. A rule that does not ask for them gets none.
Needs@facts:Bool -> @declarations:Bool -> Needs
type Run source · line 247 · raw
Type
How a rule runs: once per linted file, reporting only in it; or once per check, on the whole program, reporting in any
file (what it finds in a file that is not linted is dropped). When several entries check the named file, a program
rule's finding stays if some check finds it, or, if every, if every check finds it; then, if an entry that reads the
named file fails its check, the rule does not run.
OnFile@run:(@+here:Here -> Ask(List<&2, Finding>)) -> Run
OnProgram@every:Bool -> @run:(@+whole:Whole -> Ask(List<&2, Finding>)) -> Run
type Rule source · line 253 · raw
Type
A rule: what it needs, the options it declares, and how it runs. Its id is its path in bend-lint-rules/, without .bend, as bend-lint.json names it.
Rule@needs:Needs -> @options:List<&2, Declared> -> @run:Run -> Rule
Definitions
def Above.of source · line 256 · raw
@parent:Maybe<&2, Node> -> @at:U32 -> @up:List<&2, Above> -> List<&2, Above>
def Severity.show source · line 335 · raw
@s:Severity -> String
def Severity.names source · line 346 · raw
List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/shared.Names.Named<Severity>>
def Ask source · line 351 · raw
@-A:Type -> Type
A computation that may ask the checker, in do Lint.Ask<A>: blocks. Continuation-passing, so binding needs no
recursion. A run may ask only cli.bend's budget() rounds, so ask for everything at once (show_all).
def Ask.pure source · line 354 · raw
@-A:Type -> @x:A -> Ask(A)
def Ask.bind source · line 357 · raw
@-A:Type -> @-B:Type -> @m:Ask(A) -> @f:(@_:A -> Ask(B)) -> Ask(B)
Templates
template visit.go source · line 263 · raw
@-C:Data -> @-A:Data -> @-f:(@+c:C -> @+path:List<&2, Above> -> @+node:Node -> List<&2, A>) -> @nodes:List<&2, Node> -> @+c:C -> @+up:List<&2, Above> -> @+parent:Maybe<&2, Node> -> @+at:U32 -> @out:List<&2, A> -> List<&2, A>
template visit source · line 304 · raw
@-C:Data -> @-A:Data -> @-f:(@+c:C -> @+path:List<&2, Above> -> @+node:Node -> List<&2, A>) -> @c:C -> @nodes:List<&2, Node> -> List<&2, A>
f(c, path, node) for each node of nodes and under them, parents first, in order: path holds the nodes above the
node, innermost first.
template fold source · line 314 · raw
@-C:Data -> @-S:Data -> @-f:(@+c:C -> @+node:Node -> @_:S -> S) -> @+c:C -> @nodes:List<&2, Node> -> @s:S -> S
f(c, node, s) for each node of nodes and under them, parents first, in order, from s.