Skip to content

Notation and quotation

A notation declaration lets the terms of a syntax declaration be written the way they are written on paper: λx. λk. k x instead of Lam(Lam(App(Lambda.Term.var(0), Lambda.Term.var(1)))). A notation is a module item: a module exports it like any other declaration, and it is active only in a module that opens it, so two libraries may give the same symbols different meanings. A quotation [| ... |] writes a term in the active notations, as a value or as a pattern, with $x splicing in a value of the program. The rules are in Notation and quotation; this page gives the surface.

Notation declarations

syntax Lambda
    sorts
        Term : object
        Name : data
    constructors
        Const : Name -> Term
        Lam : (x : Term).Term -> Term
        App : Term, Term -> Term
        Pair : Term, Term -> Term
        Let : Term, (x : Term).Term -> Term

notation LambdaNotation for Lambda
    lam : "λ" x ". " M => Lam(x. M)
    app : M " " N [assoc=left] => App(M, N)
    pair : "⟨" A ", " B "⟩" => Pair(A, B)
    let_ : "let " x " = " A " in " B => Let(A, x. B)
    const : n => Const(n)
    app > lam
    lam = let_

notation N for Sig declares productions over the terms of the syntax Sig. Each production line is name : shape [options] => template:

Part Meaning
name the production's name, which precedence relations and tooling use
shape quoted tokens and holes, in order; a hole is a name
[assoc=left] for a production with an operand hole at each end: left, right, or none (the default)
template the term the production stands for, a term of Sig over the holes

A hole's role follows from where the template uses it:

The template uses the hole The hole is It reads
as a term, App(M, N) an operand an expression of the argument's sort
as the name an abstraction binds, Lam(x. M) a binder one name, which is bound in the holes the abstraction scopes over
as a data argument, Const(n) a data hole one string or integer literal

A quoted token's surrounding spaces are printing space: ". " reads the token . and prints a space after it, and a token of spaces alone (" " in M " " N) only prints. Two operand holes may stand side by side only in a shape of exactly two holes, which is juxtaposition. A name in the template that is neither a hole nor a name the template binds must be a nullary constructor of the signature; a name the template binds itself (y in Lam(y. App(F, App(F, y)))) is internal to the production and never captures a name the quotation's author writes.

Precedence

Precedence is a directed acyclic graph, not a ladder of numbers. a > b says production a binds tighter than b; a = b puts them at one level; the order is the transitive closure. A production may name a production of another notation by qualifying it, L.LambdaNotation.app, so a notation can relate its productions to an imported one's.

An operand at the end of a production may be a production of a strictly tighter level, or of the same level when it chains the production's way: a prefix or right-associative production's last operand, and a postfix or left-associative production's first. Operands between tokens may be any expression. So λx. f x reads as λx. (f x) because app > lam, and f (λx. x) needs its parentheses. Productions at one level must associate the same way.

Activating notations

open notation LambdaNotation

open notation X activates notations for the rest of the module. X names a notation declared in the module, a notation of an imported module (open notation L.LambdaNotation, or bare when the import selects it), or an imported module, whose exported notations all become active:

import std.syntax.lambda as L

open notation L

Activation never reaches an importing module. Two active productions with the same shape are refused when a quotation needs them, naming both productions with their notations and modules.

Quotations as values

define compose() : Lambda.Term !{} =
    return [| λf. λg. λx. f (g x) |]

define apply_to(t : Lambda.Term) : Lambda.Term !{} =
    return [| λx. $t x |]

define paired(a : Lambda.Term) : Lambda.Term !{} =
    return [| let p = ⟨$a, "c"⟩ in p |]

A quotation elaborates to the constructor tree of the signature's family: compose() returns Lam(Lam(Lam(App(Lambda.Term.var(2), App(Lambda.Term.var(1), Lambda.Term.var(0)))))), exactly what the hand-written tree is, so the checker and every target treat it as that tree. Its sort is the sort of the production that reads it; when nothing in the quotation decides it (a lone variable of a signature with several variable sorts), annotate it: ([| x ⊢ x |] : Lambda.Term).

Form Meaning
$x splices the value of the local x
$(e) splices the value of the expression e
x, y ⊢ t quotes t as an open term whose free variables are x and y, outermost first
Op(a, x. b) a constructor applied directly, for constructors no production covers
( ... ) grouping

A splice stands in the context its author sees: under λx. it may mention x. When a production places the splice under a binder of its own, the splice is weakened past it (with the generated Sig.S.shift), so the production's binder never captures the splice's variables. A splice is checked against the field it fills, so a value of the wrong type is refused where it is written.

Quotations as patterns

define body_of(t : Lambda.Term) : Lambda.Term !{} =
    case t
        [| λx. $b |] =>
            return b
        [| ⟨$a, $_⟩ |] =>
            return a
        other =>
            return other

In a pattern, $x binds x to the matched subterm and $_ matches anything; $(e) is not a pattern. A quotation pattern is an ordinary constructor pattern, covered and checked like any other. A splice under a binder a production introduces would have to be strengthened past it, which a pattern cannot do, so it is refused.

Code quotations

A quotation annotated with one of the meta phase's quoted-syntax families quotes QVR itself and elaborates to that family's constructor tree, the representation the meta phase splices:

meta define double_body(x : SynValue) : SynComp !{} =
    return ([| return $x * 2 |] : SynComp)

meta define gen() : Seq[SynDecl] !{} =
    let body <- double_body(([| n + 1 |] : SynValue))
    let ty = ([| Int |] : SynType)
    return SeqCons(DeclComputation("double", SeqNil, SeqCons(Param("n", ty), SeqNil), ty, RowClosed(SeqNil), body), SeqNil)

$gen()

A quotation may span lines. Its first line ends with [|, its contents are indented below it, and |] stands on its own line at the indentation of the line that opens it, so a whole declaration is quoted with its layout:

meta define shifted(name : String, offset : SynValue) : SynDecl !{} =
    return ([|
        define $name(x : Int) : Int !{} =
            let y = x + $offset
            if y > 10 then
                return y
            else
                return 0
    |] : SynDecl)

$shifted("bumped", ([| 7 |] : SynValue))
Annotation The contents are
SynDecl one declaration: a computation, a family, an index, a pattern synonym, or a property
SynComp a computation, on one line or several
SynValue a value, on one line
SynType a type, on one line
SynPattern a pattern, on one line

A splice stands for a value of the family its position takes: where a value is written, a SynValue; a type, a SynType; a pattern, a SynPattern; a name (as $name and $f in $f(x)), a String. The quoted contents are checked by the meta phase like any other value of the family, and the spliced declaration is checked like a hand-written one.

Printing

A program that opens notations prints its terms through them. The Show instance of each sort they cover, Sig.S.show, is generated from the root module's open notations instead of from the signature alone, so Lambda.Term.show of the term above returns λx0. λx1. λx2. x0 (x1 x2), with each bound variable named by its de Bruijn level. A failing test or property renders a family value through its show, and the language server's hover on a quotation shows the term it elaborates to and its printing. Printed text reads back to the same term in the root module, so a constructor no open notation covers is written as the root module writes it: L.Lam under import lam as L, Lam once L is opened. A derived Show instead displays a value apart from any scope, printing each constructor's declared name.

Tokens

Inside a quotation, a name is an ASCII identifier, a literal is a string or an integer, and every other run of characters is split into the active notations' tokens, longest first; , and . are always tokens. A name spelled like a word token of an active production (let, in) is that token. A dotted name splits at its dots, so λx.x reads as λ x . x. Parentheses group; square and curly brackets are tokens a production may use. ⊢ ends a quotation's context and cannot be a token. A quotation of terms may span lines; its line breaks and indentation are space.