~/bend-docscommunity

src/syntax/bind.bend checks

raw source on the hub · import bolt@1.40.0.0/src/syntax/bind.bend as Bind

src/syntax/bind: where every name in a source is bound, and what every other name refers to. One walk over the tree threads an environment (the binders in scope, innermost first) through Bend's binding forms: - an item's name (def f, type T, law l, a constructor line); - a telescope: a def's parameters, a type's parameters, a constructor's fields, each type seeing the names before it; - case p: patterns and for x: T / exs x: T where P, for the lines under them; - a let (x = v, +x = v, x : T = v, x : T <- m, a b = f g, (a, K{b}) = p) for the lines after it in its block -- the right side sees the outer names, so st = step(st) reads the old st; - x => e, @x: A -> B and &x: A -> B, for the rest of their sequence. The walk branches on token and statement kinds, never on a computed test, so each step recurses on one subtree and the checker accepts it. A binder shadows an item of the same name; a dotted name is an item of the file (text.of) or, through an import alias, of another file (Lex.Tok); anything else is free (Base, or unknown).

4 imports
import Base
import ./lex.bend as Lex
import ../lazy/lazy.bend as Lazy
import ./tree.bend as Tree

Types

type BindKind source · line 26 · raw

Data

what a binder is: an item's or a constructor's name, a parameter, a type's parameter, a constructor's field, a let name, a pattern binder, a law's for, or a type-level @x: / &x:

type Bind source · line 40 · raw

Data

KLocal is a name a let, a do-bind or a lambda binds; KPat one bound by a pattern (a case, or a destructuring let). note: what to show for it -- a declaration's own text (+x: U32), else the line that bound it

type Target source · line 45 · raw

Data

what a use refers to: a binder of the file (by position), an item of the file, an item behind an import alias, or nothing known (Base, or unknown)

type Use source · line 52 · raw

Data

a name that is not a binder, with what it refers to

type Scope source · line 56 · raw

Data

the names in scope at a statement's line

type Bound source · line 60 · raw

Data

everything the binder knows about a source, each list in source order

type Pos source · line 64 · raw

Data

a line and a column

type Index source · line 255 · raw

Data

The index of a file's items. Every use no binder holds asks whether its name is an item, and a scan of the items for each one is the uses times the items: a file of 6,500 items spent 14.5 s there and 0.1 s on all the rest of its binding. The index is a binary trie on the low bits of a number folded from the name's chars, a leaf holding the names that reach it; a lookup walks down by the asked name's number and scans that one leaf. It answers as the scan does (laws/BinderIndexFindsItems.bend).

type Names source · line 341 · raw

Data

the names an environment holds: the file's items, its aliases, and the index of the items, which is what a lookup reads

type Out source · line 368 · raw

Data

what the walk has gathered so far, each list reversed

type Mode source · line 375 · raw

Data

what the walk is reading; MLhs and MLhsType carry the environment the right side of the let will see, and MLhs the kind its names bind with. MSkip steps over one node (a name a form already bound), then reads on in next; MStop reads nothing more

type W source · line 391 · raw

Data

the walk's result: the environment after, and the output

type Site source · line 993 · raw

Data

where a name is: a binder, or a use of one

Definitions

def declared.is_name source · line 71 · raw

@kk:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/lex.TokKind -> Bool

a name that can be declared: plain, capitalized or dotted

def declared.name source · line 83 · raw

@nn:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> Maybe<&2, String>

a leaf's name, when its kind is one: the kind is tested, the text only kept

def declared.go source · line 91 · raw

@kids:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> Maybe<&2, String>

the item a statement declares: the first name after its keyword

def declared.is_key source · line 99 · raw

@kk:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/lex.TokKind -> Bool

a keyword?

def declared.key source · line 107 · raw

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

a keyword leaf? its kind is read, never its text

def declared source · line 115 · raw

@kids:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> Maybe<&2, String>

the item a statement declares: the first name after its keyword

def push_name.again source · line 123 · raw

@acc:List<&2, String> -> @+name:String -> Bool

is the name at the head of the list?

def push_name source · line 133 · raw

@mm:Maybe<&2, String> -> @+acc:List<&2, String> -> List<&2, String>

a name, when there is one, onto a list, unless it is the name just put there: a proof file's law x and its def x are one item, and every use no binder holds is looked for in this list

def ctors source · line 141 · raw

@body:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> @acc:List<&2, String> -> List<&2, String>

a type's constructors: the first name of each line under it

def items source · line 149 · raw

@root:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> @acc:List<&2, String> -> List<&2, String>

the defs, laws, types and constructors of a source

def last_text source · line 163 · raw

@kids:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> String

the text of a chain's last leaf

def aliases source · line 173 · raw

@root:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> @+acc:List<&2, String> -> List<&2, String>

the import aliases of a source: the last token of each import .. as X

def find.go source · line 188 · raw

@env:List<&2, Bind> -> @+name:String -> @hit:Maybe<&2, Bind> -> Maybe<&2, Bind>

the innermost binder of a name in an environment; hit: the binder before env was the name, so the search stops there

def find source · line 200 · raw

@env:List<&2, Bind> -> @+name:String -> Maybe<&2, Bind>

the innermost binder of a name in an environment

def head_of.go source · line 203 · raw

@cs:List<&2, Char> -> List<&2, Char>

def head_of source · line 211 · raw

@cs:List<&2, Char> -> String

a dotted name's part before the first dot

def rest_of source · line 215 · raw

@cs:List<&2, Char> -> String

a dotted name's part after the first dot

def has source · line 223 · raw

@names:List<&2, String> -> @+name:String -> Bool

is the name among these?

def same source · line 228 · raw

@aa:String -> @bb:String -> Bool

two names alike, char by char, stopping at the first that differs: String.eq, without the pairs String.cmp builds on every step

def has_item source · line 241 · raw

@names:List<&2, String> -> @+name:String -> Bool

is the name among the file's items? has, stopping at the first hit, as every use no binder holds asks it of every item

def index.depth source · line 261 · raw

Nat

how many bits of a name's number the index branches on

def key.go source · line 265 · raw

@name:String -> @+acc:U32 -> U32

a name's number: its chars folded, each onto 31 times the number so far

def key source · line 273 · raw

@name:String -> U32

a name's number

def index.names source · line 277 · raw

@ix:Index -> List<&2, String>

the names at a leaf, none elsewhere

def index.pick source · line 287 · raw

@bb:Bool -> @lo:Index -> @hi:Index -> Index

the low half for a set bit, the high half else

def index.near source · line 295 · raw

@bb:Bool -> @ix:Index -> Index

the half of a node a bit goes down, empty elsewhere

def index.join source · line 303 · raw

@bb:Bool -> @sub:Index -> @far:Index -> Index

a node whose half down a bit is sub, and the other half far

def index.put source · line 311 · raw

@dd:Nat -> @+kk:U32 -> @+ix:Index -> @name:String -> Index

the index with one more name, down the low bits of its number

def index.has source · line 320 · raw

@dd:Nat -> @+kk:U32 -> @ix:Index -> @+name:String -> Bool

is the name at the end of its number's way down the index?

def index source · line 328 · raw

@names:List<&2, String> -> Index

the index of these names

def indexed source · line 336 · raw

@ix:Index -> @+name:String -> Bool

is the name among the names the index was built from?

def names_of source · line 345 · raw

@+its:List<&2, String> -> @als:List<&2, String> -> Names

the names of a file from its items and aliases

def resolve.local source · line 348 · raw

@mm:Maybe<&2, Bind> -> @+name:String -> @ns:Names -> Target

def resolve source · line 361 · raw

@env:List<&2, Bind> -> @+name:String -> @ns:Names -> Target

what a name at a point refers to, given what is in scope

def spaced source · line 396 · raw

@+tt:String -> String

a declaration as written: +x: List<&2, U32>, ~f: U32 -> U32. A space follows : and ,, spaces surround ->, everything else is glued

def render_close source · line 401 · raw

@close:Maybe<&2, 0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/lex.Tok> -> String

a group's close bracket, "" when it never closed

def render_all source · line 409 · raw

@nn:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> String

the text of a whole chain

def render source · line 419 · raw

@nn:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> String

the text of a chain up to its first comma

def use source · line 431 · raw

@+name:String -> @line:U32 -> @col:U32 -> @+env:List<&2, Bind> -> @ns:Names -> @out:Out -> W

a use of a name, resolved against the environment

def mark source · line 436 · raw

@line:U32 -> @+env:List<&2, Bind> -> @out:Out -> Out

the names in scope at a statement's line, recorded

def tele_kind source · line 442 · raw

@+oo:String -> BindKind

what the names of a telescope bind as, by its bracket: ( parameters, < type parameters, { fields

def env_of source · line 447 · raw

@ww:W -> List<&2, Bind>

a result's environment

def out_of source · line 452 · raw

@ww:W -> Out

a result's output

def plan.lam source · line 468 · raw

@rest:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> Bool

does the chain start with a leaf of this kind?

def plan.group source · line 476 · raw

@rest:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> Bool

does the chain start with a group?

def plan.named source · line 484 · raw

@rest:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> Bool

does the chain start with a name (lower or upper case)?

def plan.typed source · line 495 · raw

@rest:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> @upper:Bool -> Bool

does the chain start with a name and a :? An upper-case name counts when upper says so

def plan.peek source · line 507 · raw

@rest:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> 0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/lex.Tok

the chain's first token; a blank one when it does not start with a leaf

def plan.tail source · line 515 · raw

@rest:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> 0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node

the chain after its first node

def plan.skip source · line 523 · raw

@mode:Mode -> Mode

the mode the rest is read in after a head that means nothing in this mode

def plan.lmode source · line 553 · raw

@mode:Mode -> @kk:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/lex.TokKind -> @+rest:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> Mode

the mode the rest is read in after a leaf of kind k

def plan.rec source · line 587 · raw

@mode:Mode -> @kk:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/lex.TokKind -> @+rest:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> Bool

does a leaf of kind k record a binder?

def plan.push source · line 620 · raw

@mode:Mode -> @kk:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/lex.TokKind -> @rest:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> Bool

does the binder a leaf records go into the environment too? All do but an item's name and a constructor's, which the file's items hold

def plan.use source · line 630 · raw

@mode:Mode -> @kk:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/lex.TokKind -> @+rest:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> Bool

does a leaf of kind k use a name?

def plan.keep source · line 659 · raw

@mode:Mode -> @kk:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/lex.TokKind -> Bool

is a leaf of kind k the = or <- of a let, whose right side sees the names outside the let?

def plan.outer source · line 673 · raw

@mode:Mode -> List<&2, Bind>

the environment outside a let's left side

def plan.ahead source · line 683 · raw

@tok:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/lex.Tok -> @kind:BindKind -> @+note:String -> Bind

a binder at the next token, with the note its kind shows

def plan.ahead_name source · line 689 · raw

@tok:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/lex.Tok -> String

the name of the next token, "" when there is none

def plan.binder source · line 696 · raw

@mode:Mode -> @kk:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/lex.TokKind -> @+tt:String -> @ll:U32 -> @cc:U32 -> @+rest:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> Bind

the binder a leaf records: its own name, or for op name, @x: and &x: the name after it. A telescope's binder notes its declaration

def plan.pushed source · line 718 · raw

@push:Bool -> @+bb:Bind -> @env:List<&2, Bind> -> List<&2, Bind>

a binder onto the environment, when there is one

def plan.recorded source · line 726 · raw

@rec:Bool -> @+bb:Bind -> @out:Out -> Out

a binder onto the output, when there is one

def plan.used source · line 735 · raw

@usd:Bool -> @+name:String -> @line:U32 -> @col:U32 -> @env:List<&2, Bind> -> @ns:Names -> @out:Out -> Out

a use onto the output, when there is one

def plan.enter source · line 744 · raw

@keep:Bool -> @outer:List<&2, Bind> -> @push:Bool -> @+bb:Bind -> @env:List<&2, Bind> -> List<&2, Bind>

the environment the rest is read with after a leaf: the one outside a let's left side at its =, else this one with the leaf's binder on it

def plan.ginner source · line 758 · raw

@mode:Mode -> @+gt:String -> Mode

the mode a group's insides are read in; MStop when they mean nothing here

def plan.gnext source · line 774 · raw

@mode:Mode -> Mode

the mode the rest is read in after a group

def plan.gkeeps source · line 785 · raw

@mode:Mode -> Bool

do the names a group binds stay bound after it? A signature's parameters and a pattern's binders do

def plan.kmode source · line 795 · raw

@mode:Mode -> @sk:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.StmtKind -> @+env:List<&2, Bind> -> Mode

the mode a statement's header is read in

def plan.bmode source · line 819 · raw

@mode:Mode -> @sk:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.StmtKind -> Mode

the mode a statement's body is read in

def plan.mark_head source · line 831 · raw

@mode:Mode -> @sk:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.StmtKind -> Bool

is the scope recorded before a statement's header?

def plan.mark_body source · line 847 · raw

@mode:Mode -> @sk:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.StmtKind -> Bool

is the scope recorded between a statement's header and its body? A def's is

def plan.marked source · line 855 · raw

@yes:Bool -> @line:U32 -> @+env:List<&2, Bind> -> @out:Out -> Out

the scope recorded onto the output, when it is

def plan.scoped source · line 864 · raw

@mode:Mode -> @sk:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.StmtKind -> Bool

does a statement close its scope for the statements after it? All but a let and a for in a block do

def plan.snext source · line 874 · raw

@mode:Mode -> Mode

the mode the statements after a statement are read in

def walk source · line 887 · raw

@nn:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> @+mode:Mode -> @+env:List<&2, Bind> -> @+ns:Names -> @out:Out -> W

the walk: one node in one mode, against an environment. It takes the node apart as far as its head, and the plan above says what the head does in this mode. Every recursive call is on a subtree (kids, body, gkids or rest), never on n itself

def lstrip source · line 917 · raw

@cs:List<&2, Char> -> List<&2, Char>

leading spaces dropped

def stripped source · line 925 · raw

@+text:String -> String

a line's text without its indentation

def note_for source · line 930 · raw

@kind:BindKind -> @+note:String -> @text:(@_:Unit -> String) -> String

a declaration carries its own note; anything else shows the line that bound it, which text gives only when it is asked for

def annotate.ahead source · line 943 · raw

@+line:U32 -> @+at:U32 -> @cur:List<&2, String> -> List<&2, String>

the lines from line line on, given the lines from line at on: a step forward from where the last binder was, not a walk from the first line

def annotate.head source · line 947 · raw

@+cur:List<&2, String> -> String

the first line of these, or ""

def annotate.back source · line 951 · raw

@lines:List<&2, String> -> @+line:U32 -> String

a line read from the first, for a binder behind the last one

def annotate.go source · line 958 · raw

@binds:List<&2, Bind> -> @+lines:List<&2, String> -> @+at:U32 -> @+cur:List<&2, String> -> List<&2, Bind>

every binder with its note filled. Binders come in source order, so the lines are read once, front to back, from where the last binder was (cur holds the lines from line at on); a binder behind that point reads its line from the start

def annotate source · line 970 · raw

@binds:List<&2, Bind> -> @+lines:List<&2, String> -> List<&2, Bind>

every binder with its note filled

def finish source · line 977 · raw

@oo:Out -> @lines:List<&2, String> -> Bound

the gathered output, in source order and annotated

def of_tree source · line 982 · raw

@+root:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> @lines:List<&2, String> -> Bound

the binding structure of a parsed source

def bound source · line 986 · raw

@+source:String -> Bound

the binding structure of a source

def covers source · line 998 · raw

@+l2:U32 -> @+c2:U32 -> @+name:String -> @+line:U32 -> @+col:U32 -> Bool

does the name at (l, c) cover the position (line, col)?

def bind_at.go source · line 1004 · raw

@binds:List<&2, Bind> -> @+line:U32 -> @+col:U32 -> @hit:Maybe<&2, Site> -> Maybe<&2, Site>

the binder whose name covers a position; hit: the one before binds did, so the search stops there

def bind_at source · line 1018 · raw

@binds:List<&2, Bind> -> @+line:U32 -> @+col:U32 -> Maybe<&2, Site>

the binder whose name covers a position

def use_at.go source · line 1023 · raw

@uses:List<&2, Use> -> @+line:U32 -> @+col:U32 -> @hit:Maybe<&2, Site> -> Maybe<&2, Site>

the use whose name covers a position; hit: the one before uses did, so the search stops there

def use_at source · line 1036 · raw

@uses:List<&2, Use> -> @+line:U32 -> @+col:U32 -> Maybe<&2, Site>

the use whose name covers a position

def at.or source · line 1039 · raw

@mm:Maybe<&2, Site> -> @uses:List<&2, Use> -> @line:U32 -> @col:U32 -> Maybe<&2, Site>

def at source · line 1047 · raw

@bb:Bound -> @+line:U32 -> @+col:U32 -> Maybe<&2, Site>

the site at a position

def binder.go source · line 1053 · raw

@binds:List<&2, Bind> -> @+line:U32 -> @+col:U32 -> @hit:Maybe<&2, Bind> -> Maybe<&2, Bind>

the binder at a position (a use's target, or the binder itself); hit: the one before binds was, so the search stops there

def binder source · line 1066 · raw

@binds:List<&2, Bind> -> @+line:U32 -> @+col:U32 -> Maybe<&2, Bind>

the binder at a position (a use's target, or the binder itself)

def visible.go source · line 1071 · raw

@scopes:List<&2, Scope> -> @+qline:U32 -> @best:List<&2, Bind> -> List<&2, Bind>

the names in scope at a line: those of the statement on it, else of the nearest statement above

def noted source · line 1080 · raw

@mm:Maybe<&2, Bind> -> @bb:Bind -> Bind

the scopes hold binders before their notes were filled: read each back

def before source · line 1088 · raw

@+l1:U32 -> @+c1:U32 -> @+l2:U32 -> @+c2:U32 -> Bool

does (l1, c1) come before (l2, c2) in the source?

def seek.stop source · line 1092 · raw

@cur:List<&2, Bind> -> @+line:U32 -> @+col:U32 -> Bool

does a cursor's head sit at or after a position (the seek stops there)?

def seek source · line 1102 · raw

@binds:List<&2, Bind> -> @+line:U32 -> @+col:U32 -> @+stop:Bool -> List<&2, Bind>

the binders from the first one not before a position on; the binders are in source order, so none this drops sits at the position. stop is seek.stop of binds, carried so the step stays a loop

def seek.head source · line 1114 · raw

@cur:List<&2, Bind> -> @+line:U32 -> @+col:U32 -> Maybe<&2, Bind>

the binder at the head of a cursor, when it sits at the position

def refresh.go source · line 1128 · raw

@env:List<&2, Bind> -> @+binds:List<&2, Bind> -> @+cur:List<&2, Bind> -> @+tl:U32 -> @+tc:U32 -> @acc:List<&2, Bind> -> List<&2, Bind>

each binder of an environment, outermost first, with the note its annotated twin carries, pushed onto acc (so acc ends innermost first again). cur is a cursor into binds that drops only binders before (tl, tc), the last one read: an environment's binders were pushed in source order, so each lookup picks up where the last stopped and binds is read once, not once per binder. A binder before the last one read starts the cursor over

def refresh source · line 1145 · raw

@env:List<&2, Bind> -> @+binds:List<&2, Bind> -> List<&2, Bind>

each binder of an environment, with the note its annotated twin carries

def visible source · line 1150 · raw

@bb:Bound -> @qline:U32 -> List<&2, Bind>

the names in scope at a line: those of the statement on it, else of the nearest statement above

def sites.go source · line 1155 · raw

@uses:List<&2, Use> -> @+line:U32 -> @+col:U32 -> List<&2, Pos>

every position a binder is named at: its own, then its uses'

def sites source · line 1166 · raw

@bb:Bound -> @+line:U32 -> @+col:U32 -> List<&2, Pos>

every position a binder is named at: its own, then its uses'

def item_sites.binds source · line 1172 · raw

@binds:List<&2, Bind> -> @+name:String -> List<&2, Pos>

every position an item of the file is named at: its declaration, then its uses

def item_sites.uses source · line 1185 · raw

@uses:List<&2, Use> -> @+name:String -> List<&2, Pos>

def item_sites source · line 1197 · raw

@bb:Bound -> @+name:String -> List<&2, Pos>

every position an item of the file is named at: its declaration, then its uses

def named_sites.go source · line 1203 · raw

@uses:List<&2, Use> -> @+name:String -> List<&2, Pos>

every position a name not of this file is used at (Base, or through an alias)

def named_sites source · line 1215 · raw

@bb:Bound -> @+name:String -> List<&2, Pos>

every position a name not of this file is used at (Base, or through an alias)