ReferenceCode

Language specification

Runtime

Guides

Background

The Flow Book

Code

On this page

This specification owns std's code module: the code model, the typed description of a program that a program can read; how the model names a declaration and a place in source; the checked references, six forms that take a path in the source instead of a value; values that carry their type, and code.decode, which turns one back into a typed value.

It does not own what the model describes. Source text and its forms are Grammar's, types and generics Types's, evaluation Evaluation's, and which Tools a run reaches Tools. Declaration identity and signatures as they cross the Tool boundary are The host boundary's, and the diagnostic shape is Diagnostics's.

§1The code model is std's

The code model is defined here and declared in std, not taken from a compiler. A compiler maps its own internal form onto it. The model changes only with a new std version, which is a new language version (Standard library), so a program that reads code keeps working under a newer compiler of the same language version.

The model describes a checked program, not a parsed one:

  • Every expression carries its type.
  • Every name is resolved. A use of a local is Local(LocalId), the one binding it refers to, even under shadowing. A use of a top-level item is Global(DeclId). A direct call of a Tool is ToolCall with the Tool's DeclId; a call through a function value is Call, since which Tool it reaches is known only when it runs.
  • Spellings are not kept. Parentheses, method syntax, field shorthand, qualified and bare constructor names, and else if are ways of writing one meaning. The model records the meaning: v.f(a) is a call of f with v as its first argument, and Report { id } sets the field id to the local id. How it was written is one call away, through the span every node carries and the module's source text.

§2Programs and modules

pub type Program { pub entry: DeclId, pub modules: List<Module> }pub type Module { pub path: ModulePath, pub tag: Option<Tag>, pub text: Text, pub imports: List<Import>, pub decls: List<Decl> }pub type Import { pub name: Name, pub module: ModulePath, pub span: Span }pub type ModulePath { pub tree: Tree, pub library: Option<path.Path>, pub segments: List<Text> }pub type Tree { Std, Remote(Repository), Local }pub type Repository(pub Text)pub type Tag { pub major: Int, pub minor: Option<Int>, pub patch: Option<Int> }pub type DeclId { pub module: ModulePath, pub name: Name, pub block: Option<Int> }pub type Name(pub Text)pub type Span { pub module: ModulePath, pub start: Position, pub end: Position }pub type Position { pub line: Int, pub column: Int, pub offset: Int }
Open snippet in Playground (new tab) ↗
  • A program holds one Module for every module it contains, std's included, in the structural order of their paths. entry is the function the run started with.
  • A module holds its source text exactly as stored for the run, comments and layout included, its imports in source order, and its declarations in source order. tag is the tag its repository's import named, as its numbers (Modules and imports): v1.2 is major 1, minor 2 and no patch. std's is its major version, v1, and a module of the local tree has None. An import's name is the name the module is used by.
  • A module path says where a module sits. tree is the tree that holds it: std, a remote repository named by its host path such as github.com/acme/tools, or the local tree or bundle the program was formed from. library is the directory of the library root that holds the module, from the tree's root, and . when the tree's root is the library; it is None for a module outside every library. segments is the module path inside that library (Modules and imports), one segment per part between /s and with no .flow extension, empty for a library's root module; outside every library it is the module's path from the tree's root. A module path is not a file's path, since a contract module's file sits under contract/, so it is a list of segments and not a path.Path.
  • A name is a name as the grammar writes it: a declaration's, a binding's, an import's, a field's, a constructor's or a type parameter's (Grammar). It is a Name, not bare text, so a name is never taken for a value's text or a module's source.
  • A declaration's id is its module path and its name. A type declared in a block adds block, the n its module counts for it (The host boundary); every other declaration has None. Two ids are equal exactly when the declarations have one identity as The host boundary defines it, with the tag left out, so a new tag of a repository names its declarations alike.
  • A span is a range of one module's text. Lines and columns count from 1, and a column counts code points, as a diagnostic's do (Diagnostics). offset is the code-point index into the module's text, from 0. end is the position just past the last character. The site of a task.Call is a span of the program evaluating the call.

§3Declarations

pub type Decl {    Function(Function),    Builtin(BodylessFunction),  // a bodyless `@lang` function in std    Tool(BodylessFunction),     // a bodyless function in a library's generated contract    Type(TypeDecl),    Constant(Constant),}pub type Signature { pub id: DeclId, pub span: Span, pub public: Bool, pub doc: Option<Text>, pub generic: List<Name>, pub params: List<Param>, pub result: Type }pub type Param { pub span: Span, pub pattern: Pattern, pub default: Option<Expr> }pub type Function { pub signature: Signature, pub annotations: List<Annotation>, pub bindings: List<Binding>, pub body: Expr }pub type BodylessFunction { pub signature: Signature, pub annotations: List<Annotation>, pub bindings: List<Binding> }pub type Constant { pub names: List<DeclId>, pub span: Span, pub public: Bool, pub doc: Option<Text>, pub pattern: Pattern, pub bindings: List<Binding>, pub value: Expr }pub type Annotation { Lang(List<Name>), Test, Fake(DeclId) }pub type LocalId { pub owner: DeclId, pub index: Int }pub type Binding { pub id: LocalId, pub name: Name, pub ty: Type, pub span: Span }pub type TypeDecl { pub id: DeclId, pub span: Span, pub public: Bool, pub is_opaque: Bool, pub doc: Option<Text>, pub annotations: List<Annotation>, pub generic: List<Name>, pub body: TypeBody }pub type TypeBody { Record(List<FieldDecl>), Enum(List<ConstructorDecl>), Newtype { public: Bool, base: Type }, Alias(Type), Bodyless }pub type FieldDecl { pub name: Name, pub public: Bool, pub ty: Type, pub doc: Option<Text>, pub span: Span }pub type ConstructorDecl { pub name: Name, pub payload: PayloadDecl, pub doc: Option<Text>, pub span: Span }pub type PayloadDecl { Empty, Positional(List<Type>), Named(List<FieldDecl>) }
Open snippet in Playground (new tab) ↗
  • A parameter is a pattern, as the grammar writes it; its type is the pattern's. generic lists the declaration's type parameters in order.
  • Bindings are numbered per top-level declaration. A function's bindings are every name bound anywhere in it: its parameters, its let, guard let and pattern bindings, and the parameters and bindings of the anonymous and local functions inside it, in source order. A Tool or a bodyless @lang function has no body, but its parameters still bind: its bindings are those of its parameters, so every Bind in a Param.pattern has its binding. A LocalId is the owner's declaration, that Tool or function included, and the binding's position in that list. A local function is a binding of the function it is written in. A constant's bindings are numbered the same way, and its owner is the first name its pattern binds; its names are the ids of the names it declares, which other code uses as Global.
  • A named payload's fields are public, since a pub enum exposes them (Types).
  • A Newtype's public is its constructor's visibility, the inner pub of pub type N(pub B), as a field's is; the type's own pub and opaque are the TypeDecl's public and is_opaque, as for a record.
  • A Bodyless type is a @lang type in std or a host type in a library's generated contract (Types).
  • Lang holds a @lang annotation's arguments, empty for a bare @lang. They are names and not an enum: which ones std uses is Standard library's list, and an enum here would be a second copy of it.

§4Expressions, statements and patterns

These lists are exact. Each grammar form has the variant given here and no other, so a change to Grammar's expressions, statements or patterns changes these types, and with them std.

pub type Expr { pub span: Span, pub ty: Type, pub kind: ExprKind }pub type ExprKind {    Literal(Literal),    Interpolation(List<TextPart>),    Local(LocalId),    Global(DeclId),    Constructor { of: DeclId, name: Name },    Call { callee: Expr, args: List<Arg> },    ToolCall { tool: DeclId, args: List<Arg> },    Reference(CheckedReference),    Field { of: Expr, name: Name },    Record { of: DeclId, constructor: Option<Name>, fields: List<FieldValue>, base: Option<Expr> },    List(List<Expr>),    Dict(List<(Expr, Expr)>),    Tuple(List<Expr>),    Unary { op: UnaryOp, operand: Expr },    Binary { op: BinaryOp, left: Expr, right: Expr },    Lambda { params: List<Param>, body: Expr },    Block { statements: List<Statement>, result: Option<Expr> },    If { condition: Expr, then: Expr, otherwise: Option<Expr> },    Match { head: Expr, arms: List<Arm> },    Try(Expr),    Return(Expr),}pub type CheckedReference { Program, Function(DeclId), Tool(DeclId), Entry(DeclId), Local(LocalId), Bundle(DeclId) }pub type Literal { Int(Int), Decimal(Decimal), Text(Text), Bool(Bool), Unit }pub type TextPart { Chars(Text), Hole(Expr) }pub type Arg { Positional(Expr), Named { name: Name, value: Expr }, Path { span: Span, names: DeclId } }pub type FieldValue { pub path: List<Name>, pub value: Expr, pub span: Span }pub type Arm { pub span: Span, pub pattern: Pattern, pub condition: Option<Expr>, pub body: Expr }pub type UnaryOp { Not, Negate }pub type BinaryOp { Or, And, Equal, NotEqual, Less, LessOrEqual, Greater, GreaterOrEqual, Add, Subtract, Concat, Multiply, Divide, Remainder }pub type Statement { pub span: Span, pub kind: StatementKind }pub type StatementKind {    Let { pattern: Pattern, value: Expr },    Guard { condition: Expr, otherwise: Expr },    GuardLet { pattern: Pattern, value: Expr, otherwise: Expr },    Function(LocalFunction),    Type(TypeDecl),    Expression(Expr),}pub type LocalFunction { pub binding: LocalId, pub span: Span, pub doc: Option<Text>, pub generic: List<Name>, pub params: List<Param>, pub result: Type, pub body: Expr }pub type Pattern { pub span: Span, pub ty: Type, pub kind: PatternKind }pub type PatternKind {    Wildcard,    Bind(LocalId),    Literal(Literal),    Tuple(List<Pattern>),    List { items: List<Pattern>, rest: ListRest },    Record { of: DeclId, fields: List<FieldPattern>, open: Bool },    Constructor { of: DeclId, name: Name, payload: PayloadPattern },    Or(List<Pattern>),    As { pattern: Pattern, binding: LocalId },}pub type ListRest { Exact, Leading(Option<LocalId>), Trailing(Option<LocalId>) }pub type FieldPattern { pub name: Name, pub pattern: Pattern }pub type PayloadPattern { Empty, Positional(List<Pattern>), Named { fields: List<FieldPattern>, open: Bool } }
Open snippet in Playground (new tab) ↗
Source Model
1, 2.5, true, (), a text literal with no ${...} Literal, holding the literal's value: escapes applied, a multi-line literal's margin removed
a text literal with a ${...} hole Interpolation: its characters and holes in order
a name bound by a parameter, let, pattern or local function Local
a top-level function, constant or Tool used as a value Global
a constructor used as a value, such as None or the function in x.map_err(Rejected) Constructor, naming its enum and itself
a checked reference: code.program(), code.function(f), code.tool(t), code.entry(f), code.local(f.x), code.bundle(f) Reference, naming the form and what its path resolved to: a DeclId, or a LocalId for code.local; it is not a Call, since its path is not an expression (Checked references)
any other call ToolCall when the callee is a Tool named directly, otherwise Call; method syntax puts the receiver first, and a positional payload is a Call of its Constructor; the source path task.calls(t) and task.calls_as(t, tag) take is an Arg Path, naming what it resolved to
e.name reading a field Field
T { ... } building a record or a named payload Record; constructor names the payload's constructor, each written field is a FieldValue whose path holds more than one name for a dotted update, and base is the ..base
[a, b], ["k": v], (a, b) List, Dict, Tuple
!e, -e Unary; a negative number is Negate of the literal
a op b Binary, one BinaryOp per operator in Grammar's order
flow(x) = e Lambda
{ ... } Block; result is None when the block ends in a statement, and the block's value is then ()
if c { a } else { b } If; else if is an If in otherwise, and a missing else is None
match, x?, return e Match, Try, Return
let, guard, guard let, a local flow, a local type, an expression in a block the matching StatementKind; a let's written type is not kept, since its pattern carries the type
_, a lowercase name, a literal pattern, (p, q) Wildcard, Bind, Literal, Tuple
[x, ..rest], [..init, x], [x, y] List with Leading, Trailing or Exact; .. with no name is None
a record pattern, a constructor pattern Record, Constructor; open is true when the field list ends with .., and a field written alone binds its own name
p | q, p as x Or, As

§5Types

pub type Type { Named { decl: DeclId, args: List<Type>, base: Option<Type> }, Tuple(List<Type>), Function { params: List<Type>, result: Type }, Param { of: Owner, index: Int, name: Name } }pub type Owner { Decl(DeclId), Local(LocalId) }
Open snippet in Playground (new tab) ↗

Type follows Types's formers, after aliases are expanded. A built-in type is Named, by the declaration std makes for it, so List<Int> is the Named of List with the Named of Int as its argument. Param is a type parameter by the declaration that declares it and its position, from 0, in that declaration's generic list: Decl for a top-level or block declaration, Local for a local function, by its binding. name is its name as written. So T of f<T> and T of g<T> are different values, as they are different types (Types), wherever the Type is read.

A Named newtype carries base: its base with the newtype's arguments applied, so a reader of the type knows what it erases to (Types). Every other type has None. Inside its own base, a newtype that refers to itself has None, so the type stays finite: type Tree(List<Tree>) is Named of Tree with the base List<Tree>, whose Tree has no base. That inner None means "the base given further out", not "not a newtype": recursion keeps the type's own parameters unchanged (Types), so the inner Tree erases to the same base as the outer one.

§6Reading the model

@lang(code_program) pub flow program() -> Programpub flow text(program: Program, span: Span) -> Option<Text>pub flow subexprs(e: Expr) -> List<Expr>pub flow show(id: DeclId) -> Textpub flow show_type(t: Type) -> Text
Open snippet in Playground (new tab) ↗
  • code.program() is the program being evaluated, with the run's entry.
  • program.text(span) is the source a span covers, or None when the program holds no module at the span's path or the span lies outside its text. Sending a function's source somewhere is one call: program.text(f.signature.span).
  • code.subexprs(e) is e and every expression inside it, statements and arms included, in source order: each expression before the expressions inside it.
  • code.show(id) writes an id as The host boundary writes a declaration's identity, without a repository's tag: shared.Plan, std@v1/task.Stop, github.com/acme/tools/pay.Receipt.
  • code.show_type(t) writes a type for a person to read. A declaration the prelude exports, found by the declaration its alias names (Modules and imports), is its name alone: Int, Bool, List<Int>, Result<T, E>, ToolProblem. Every other declaration is written as code.show writes it, so Option<std@v1/time.Instant>, and a newtype is its own name, not its base. Arguments follow in <...>. A tuple is (A, B), and a Tuple a program built with zero or one part is () or (A). A function is (A, B) -> R, so -> groups to the right and a function result is not parenthesized: (Int) -> (Int) -> Int. A type parameter is its name. The text is for display only: two different types can read the same, as two owners' T both read T, so a program compares Type values, never this text.

The model holds every module's text, so a program that calls code.program() or reads a code.Function carries a description of its own program.

§7Checked references

Six forms take a path in the program's source, not a value. The checker resolves the path when it checks the program, so a misspelt name is a compile error, and the result's type comes from the declaration the path names.

@lang(source_path) pub type SourcePath@lang(code_function) pub flow function(f: SourcePath) -> Function@lang(code_tool) pub flow tool<A, R>(t: SourcePath) -> ToolRef<A, R>@lang(code_entry) pub flow entry<I, O>(f: SourcePath) -> EntryRef<I, O>@lang(code_local) pub flow local<T>(x: SourcePath) -> LocalRef<T>@lang(code_bundle) pub flow bundle(f: SourcePath) -> Bundlepub opaque type ToolRef<A, R> { ToolRef(Signature) }pub opaque type EntryRef<I, O> { EntryRef(Signature) }pub opaque type LocalRef<T> { LocalRef(Binding) }pub type Bundle { pub entry: DeclId, pub files: List<SourceFile> }pub type SourceFile { pub path: ModulePath, pub text: Text }pub type Checked { pub entry: Signature, bundle: Bundle }
Open snippet in Playground (new tab) ↗
Form Path Gives
code.program() none this program, as a Program (Reading the model)
code.function(f) a top-level function with a Flow body its Function
code.tool(t) a Tool a ToolRef<A, R>
code.entry(f) a function that can start a run an EntryRef<I, O>
code.local(f.x) a binding x of a top-level function f, a Tool or a bodyless one included a LocalRef<T>
code.bundle(f) a function that can start a run a Bundle: the modules f reaches, with f as entry

§7.1Paths

A parameter of type SourcePath takes a source path: a dotted name, written where an argument would be (Grammar). It is not evaluated. SourcePath has no values, and it may appear only as the parameter type of a checked reference or of task.calls and task.calls_as (Concurrency) in std; a program that writes it anywhere else, or passes anything but a source path to one, does not check. The spelling is C#'s nameof(x): a call whose argument is a name, not a value, rather than a separate form for the few calls that take one.

  • A path resolves like a qualified name. Its first name is an import or a top-level item of the module it is written in, never a local binding; support.turn is the function turn of the imported module support, and turn alone is this module's own. For code.local, the last name is the binding, so harness.step.plan is the binding plan in harness.step.
  • The declaration must be visible, as it must be for a call (Types, Modules and imports). A binding has no visibility of its own; its function's is what counts.
  • Each form names one kind of thing. code.function refuses a Tool and a @lang function, and code.tool refuses everything but a Tool. A local function or a local type has no path.

§7.2What each form gives

  • ToolRef<A, R>. A is the Tool's parameters: Unit for none, the type of one, or a tuple of several in declaration order, optional ones included. R is its declared result, not the Result<R, ToolProblem> a call has. A generic Tool's type parameters are fixed by inference or an annotation, as at a generic call (Types); one that appears in neither its parameters nor its result can never be fixed, so such a Tool cannot be named.
  • EntryRef<I, O>. f must be able to start a run (Evaluation), and the reference checks it at once. I is its parameters as A is a Tool's, optional ones included, since a typed input has no way to leave a part out: leaving a key out is how a JSON input, from a person or an outside caller, asks for a default (Evaluation), and a program that starts a run of an entry passes every argument, writing a default when it wants one. O is its result.
  • LocalRef<T>. x must be bound exactly once among f's bindings (Declarations). Where x is bound more than once, the error points at every binding. T is the binding's type, which may not mention a type parameter of f. When x is written without a type, T is what f's body infers for it, so a reference reached while checking f's own body through another function's code.local, before that body has fixed x's type, is refused.
  • Bundle. f must be able to start a run and must be declared in the local tree. The bundle holds every module of the local tree that f's module reaches through its imports, f's module included, each at its ModulePath with its text, in the structural order of their paths. std and repository modules are not copied: an import names them. A library's flow.toml is not part of the program (Program checking), so a bundle holds none; each file's library says where a library root is. entry is f's id.

ToolRef, EntryRef and LocalRef are opaque: only the checker makes them, and no decoder can (Tools). Their type parameters are phantom, used by no field (Types). They are data, so they can be stored, compared, printed and sent to a Tool. A ToolRef is not callable: it names a declaration and is not the Tool, and nothing in the language turns one into a call. Holding a reference grants nothing.

§7.3Naming is not a use

A name inside a source path, a checked reference's or the one task.calls and task.calls_as take, names code; it does not use it. No rule that follows the names a declaration uses follows a path:

  • Naming a Tool does not reach it. A program that only names a Tool, in a checked reference or in task.calls, or a function that calls one, needs no binding for it (Tools). Importing a module only to name its code binds none of its Tools.
  • A constant may hold a reference. A path is not a name a constant's initializer references, so let ask = code.tool(review.ask) is an admissible constant (Evaluation).

§7.4Checked bundles

A Checked says that a bundle checked once, and which signature its entry had. It is not opaque, so that a Tool can answer one, and so it proves nothing about the bundle it holds: whatever starts or continues a run from a Checked checks its bundle again, and a bundle that no longer checks, or whose entry no longer has the recorded signature, is refused.

§8What the code model depends on

The code model depends on the program alone. code.program() describes the program the run is formed from, and every checked reference is fixed when the program is checked. None of them reads anything about the run: its history, its position, its id or how it is being evaluated.

The entry is part of that program, not of the run. A program to run is its sources and the function to start with, as a Bundle is its files and its entry (What each form gives), and the entry is recorded with the program in its first Program fact (The history format). A run continued on edited code keeps its entry (History), so every segment's program has the same one.

Replay, and resume on the same program Resume onto edited code Fork
code.program() and the checked references identical the edited program's the fork's program

A value that differs after an edit never causes a wrong reuse. It matters only as the argument of a later call, and continuing compares arguments like any other (History).

§9Values that carry their type

A value whose type the program did not fix in advance, such as one a Tool reads from another run, travels as a code.Value: its type and its shape.

pub type Value { pub ty: Type, pub shape: Shape }pub type Shape {    Unit, Bool(Bool), Int(Int), Decimal(Decimal), Text(Text), Bytes(Bytes),    Duration(duration.Duration), Instant(time.Instant),    List(List<Value>), Set(List<Value>), Dict(List<(Value, Value)>), Tuple(List<Value>),    Record(List<(Name, Value)>),    Variant { name: Name, payload: Payload },    Host,    Function(Span),    Task,}pub type Payload { Empty, Positional(List<Value>), Named(List<(Name, Value)>) }pub type TypeMismatch { pub wanted: Type, pub found: Type, pub path: List<Step> }pub type Step { Field(Name), Element(Int), Key(Value), Constructor(Name) }@lang(code_decode) pub flow decode<T>(v: Value) -> Result<T, TypeMismatch>pub flow type_of<T>(_: T) -> Typepub flow to_json(v: Value) -> Option<json.Value>
Open snippet in Playground (new tab) ↗
  • Value is an ordinary std enum, like json.Value, not a dynamic type. Every value's type is written out, so an empty list still says what it is a list of.
  • A shape follows its type. A set's elements and a dictionary's entries are in their type's order (Data); a record's fields and a named payload's are in declaration order; Variant names a constructor of the value's enum. Host is a host value, whose type is known and whose contents are not; Function is a function value, by the span where it was written; Task is a task. Those three are not data, and their shapes hold nothing that could rebuild them. A newtype's value has its base's shape, as its encoding is its base's (Types); its ty still names the newtype, so decode tells a UserId from a Text. A path.Path, code.RunId or code.TaskPath decodes only from text in its normal form, as binary decoding reads it (The host boundary).
  • code.decode<T>(v) gives v as a T, or the first difference. Decoding is nominal: at each place, v's type must equal the wanted one under Types's rule, so a billing.Receipt never decodes as a shipping.Weight with the same fields. The shape must also fit the type. TypeMismatch holds the type wanted, the type found, and the path from the top of the value to the first place they differ; a shape that does not fit its own type is a mismatch whose found is that type. Decoding walks the value in order and never faults.
  • A Step reads as a json.Step does, one step at a time from the whole value: Field for a record or named payload field, Element(i) for the part at index i, from 0, of a list, a set, a tuple or a positional payload, Key(k) for the value under the key k of a dictionary, and Constructor for the payload of an enum constructor. It is code's own type, not json.Step, because a dictionary's key here is any value rather than JSON text, and a field or constructor is a Name.
  • decode reads its type argument at run time, as json.decode does: the compiler passes it T's description (Types). T must be a data type with no opaque and no host type in it, checked where T is fixed, as for json.decode (Tools).
  • code.type_of(x) is the type x has where the call is made, with every type parameter replaced by what it is instantiated at: type_of([1]) is List<Int>, and inside f<T> called with a Text, type_of of a T is Text. It names what decode already lets ordinary Flow observe: decoding any value as a private type with no values whose argument is T is a nominal mismatch, and its wanted holds T as instantiated. std's type_of is written so, in ordinary Flow, and any library could write the same function with the same calls. The argument is evaluated once, as any argument is, so type_of(clock.now()) makes and records that call. It describes the type, never the value: a function gives its parameter and result types, not what it captures; a task gives task.Task<R>, not which task; a host type gives its declaration, not the handle. Aliases are expanded, phantom arguments are kept, and a newtype carries its base as Type does, a recursive one finitely (Types).
  • code.to_json(v) is the value in its JSON form, or None when it holds a host value, a function or a task, or a shape that does not fit its type. JSON is an export; a typed value goes through decode.

§10Check reports as data

code declares the records a check reports, field for field in the shape Diagnostics gives, which also says what each field holds. A Tool that checks a program, such as runs.check, answers them (The runs library).

pub type Diagnostic { pub code: RuleName, pub message: Text, pub reason: Option<Text>, pub at: Location, pub label: Option<Text>, pub notes: List<Note>, pub help: List<Help>, pub fixes: List<Fix> }pub type RuleName(pub Text)pub type Location {    pub file: Option<path.Path>, pub line: Option<Int>, pub col: Option<Int>, pub end_line: Option<Int>, pub end_col: Option<Int>,    pub run: Option<RunId>, pub fact: Option<FactNumber>, pub call: Option<CallAt>, pub task: Option<TaskPath>,    pub path: Option<path.Path>, pub offset: Option<Int>,}pub type Note { pub at: Location, pub label: Option<Text>, pub message: Text }pub type Help { pub message: Text, pub command: Option<List<Text>> }pub type Fix { pub title: Text, pub safe: Bool, pub edits: List<Edit> }pub type Edit { pub at: Location, pub expected: Text, pub insert: Text }
Open snippet in Playground (new tab) ↗
  • code is a RuleName: the name of the rule that refused the program, such as unhandled-value. What that name is, and that it is never a number, is Diagnostics's. The constructor is public, so a program can match a report by its rule.
  • A location's file and path are path.Paths: the source file, and a storage problem's file.
  • A location's run, fact, call and task are the places in a run of the next section.

§11Places in a run

code declares the types that name a place in a run, so that a diagnostic located in a run, and a library that reads runs, such as runs, uses one type for each. What each one means is History's.

@lang(run_id) pub type RunId(Text)@lang(task_path) pub type TaskPath(Text)pub type Segment(pub Int)pub type FactNumber(pub Int)pub type CallPosition { pub task: TaskPath, pub index: Int }pub type CallAt { pub segment: Segment, pub call: CallPosition }pub type Moment { pub segment: Segment, pub call: CallPosition, pub steps: Int }pub flow parse_run(text: Text) -> Option<RunId>pub flow show_position(p: CallPosition) -> Textpub flow show_call(c: CallAt) -> Textpub flow show_moment(m: Moment) -> Textpub flow parse_moment(text: Text) -> Option<Moment>
Open snippet in Playground (new tab) ↗
  • RunId is a run's id, and its text is that id written in full as History writes it: "${id}" writes it in full. Its constructor is private, so a program never makes one from parts. One reaches it decoded, in a Tool's reply, or from code.parse_run(t), which is Some when t is a run id in that form, as a person types it, and None otherwise.
  • TaskPath is a task's id, and its text is that id as history.md writes it: root, root/2, root/2/1. It is a newtype over that text rather than a list of counters, because a newtype's text and encoding are its base's (Types), and only a text base prints and crosses as history.md writes the id. Its constructor is private.
  • CallPosition is a call's position: its task, and index, the task's counter of Tool calls. code.show_position(p) writes it as history.md does, root/2#4.
  • Segment is a segment of a run, by its number, 0 for the first. FactNumber is a fact, by its number, 1 for the first (History).
  • CallAt is a call's id: its position, and segment, the segment in which the call was first recorded. code.show_call(c) writes it as history.md writes a call's id: root/2#4 in segment 0, and p1:root#6 in segment 1.
  • Moment is a place in one task's evaluation: segment, the segment whose program is evaluating, which may be later than the segment that recorded the call; call, the task's last call, index 0 before its first; and steps, how many steps the task has taken since. What a step is, is Evaluation's. code.show_moment(m) writes the call's position with the segment's prefix, as a call's id is written, and + and the steps: root/2#4+17, and p1:root#4+5, five steps after root#4 as segment 1's program runs. code.parse_moment(t) reads that form back, and is None for any other text.
  • Each crosses a boundary in its printed form. RunId and TaskPath are @lang newtypes with a form (Standard library): each is encoded as its text, and text that is not of its form does not decode as one. Segment and FactNumber cross as their numbers, and CallPosition, CallAt and Moment are records of those parts.

Nothing in code gives a program its own run's id, which it may not see, or the task or call it is at (History). These types describe places in other runs, which a program receives as data.

§12Refusals

The compiler rejects, with the diagnostic code given:

  • a checked reference whose argument is not a source path, or that is named as a value (not-a-source-path); whose path resolves to nothing or to something not visible (the codes of an unresolved or private name, such as unknown-name, unknown-member and private-member); or whose path names the wrong kind of declaration (wrong-reference-kind);
  • code.local(f.x) where f binds x not at all (unknown-binding) or more than once (ambiguous-binding), where x's type mentions a type parameter of f (local-type-not-fixed), or where x's type is still being inferred from f's body (local-reference-cycle);
  • code.entry(f) and code.bundle(f) where f cannot start a run (the codes of Evaluation's entry refusals, such as entry-not-a-flow), and code.bundle(f) where f is outside the local tree (bundle-outside-local-tree);
  • code.tool(t) where a type parameter of t cannot be fixed (tool-type-not-fixed);
  • SourcePath written anywhere but the parameter of a checked reference, task.calls or task.calls_as in std (source-path-misplaced);
  • code.decode at a type decoding cannot build (requirement-not-met).

§13Considered and set aside

The compiler's internal form as the model. It changes with every compiler build, and everything built on it breaks with each release.

Naming declarations by text. A misspelling would fail late, or silently. The checker resolves a path instead.

A callable Tool reference. Tools would become values a program receives and looks up. A ToolRef names a declaration and nothing more.

Matching values by shape. billing.Receipt { cents } would pass as shipping.Weight { cents }. Decoding compares types by declaration.

A built-in dynamic type. Value is an ordinary enum, and decode checks at the boundary.

  • Grammar owns the forms the model describes.
  • Types owns types, generics and phantom parameters.
  • Tools owns which Tools a run reaches, and decoding.
  • Standard library lists code among the std modules.
  • The host boundary owns declaration identity and signatures.

Serves foundations: closed, deterministic evaluation, stable meaning and typed Tool calls.