Language constructs
Identifiers
Variables and type variables
In regular expression:
[a-z_]([-]*[a-zA-Z0-9_]+)*['?]*
seq,x',some?,foo-barfooBar,a0,take_while
Constructor, type constructor, and trait names
In regular expression:
[A-Z][a-zA-Z0-9_]*
List,Nil,Eq,Iter
Module names
In regular expression:
[a-z_]([-]*[a-zA-Z0-9_]+)*
core,cmp,iter
Module path (qualified module names)
A sequence of module names separated by
::
::core::cmp,::core::listAbsolute pathcmp,list,foo::barRelative path
Qualified names
A module path followed by a variable, constructor, type constructor, or trait name
::core::iter::seq,::core::iter::Seq,::core::iter::Iteriter::seq,iter::Seq,iter::Iter
Literals
()Unit value literaltrue,falseBoolean value literals0,100,-1Integer value literals0u8,100u8,0xFFu88-bit unsigned integer literals0u16,100u16,0xFFFFu1616-bit unsigned integer literals0u32,100u32,0xFFFFFFFFu3232-bit unsigned integer literals0u64,100u64,0xFFFFFFFFFFFFFFFFu6464-bit unsigned integer literals'A','\n','🎉'Unicode Scalar Value literals"abc","🎉🤣👍🍺"Unicode UTF-8 string literals
Primitive types
()Unit typeBoolBoolean typeIntInteger typeu88-bit unsigned integer typeu1616-bit unsigned integer typeu3232-bit unsigned integer typeu6432-bit unsigned integer typeScalarValueUnicode Scalar Value typeScalarStringUnicode UTF-8 string type (synonym ofStr ScalarValue)@[t]or@[] tArray type(t,),(t1, t2)Tuple type@{x:t1, y:t2}Record typet1 -> t2Function typeT t,t1 t2Type-level function application
Operators
Infix (binary) operators
==,!=Equality / Non-equality operators<=,<,>,>=Comparison operators&&,||Logical operators+,-,*,/,%Arithmetic operators|>,<|Pipeline operators>>,<<Function composition operators
Prefix operators
nagete,-Arithmetic negationnot,!Boolean not
User defined infix operators
In regular expression:
[*+\-/!$%&=^?<>]+
?>,===,<+>
Operators as functions
(==) 1 1(+) 1 2
Functions and constructors as operators
1 `Cons` Nilx `@{Eq Int}.(==)` y
Declarations
At the top level of a module:
modDefine sub-moduleuseImport identifierstypeDefine Algebraic Data Type (ADT)traitDefine Multi-Parameter Type Class (MPTC)implDefine MPTC instance implementation*letDefine generic/overloaded function templateletDefine a variable or functionlet recDefine a recursive function
Statements
Within a local scope:
letVariable / function bindinglet recRecursive function binding
Note
In the case of
let rec p = e;,
pmust be a variable pattern.In the case of
let p = e;,
- At the top level of a module,
pmust be a variable pattern.- Within a local scope, any pattern can be used for
p.
Expressions
Value expressions
(evaluate to themselves; safe to generalize)
(),true,0Literalsλp.e/\p.eLambda abstraction (function)@[],@[e1],@[e1, e2]Array value(e1,),(e1, e2)Tuple value@{x = e1, y = e2}Record value@{T t1 t2}Trait record value|(e op)/|(e op _)Infix-operator partial application (bind the 1st argument)|(op e)/|(_ op e)Infix-operator partial application (bind the 2nd argument)
Infix-operator partial applications (a.k.a. section syntax) are desugared into lambda abstraction.
Note
Old section syntax
#(e op)/#(e op _)and#(op e)/#(_ op e)are deprecated.
Use new syntax|(e op)/|(e op _)and|(op e)/|(_ op e)instead.
Important
Old record value syntax
@{x: e1, y: e2}are deprecated and no longer supported.
Use new syntax@{x = e1, y = e2}instead.
Non-value expressions
(require evaluation; not eligible for generalization)
xEvaluate variablee1 e2Function applicatione1[e2]Array index accesse.0Tuple index accesse.xRecord field access{ stmt1; stmt2; expr }Block expression (evaluates to the last expression; introduces a new scope)if (e1) e2 else e3If then elsematch (e) { p1 => e1, .. }Pattern matching
Minumul Type Annotaions
-
e: tOptional type annotations for expressions
This adds a type equality constraint between the inferred type ofeand the annotated typet.
This helps type inference and the constraint solver resolve the last mile of ambiguity. -
Type annotations as signatures are required only in
traitdeclarations.
Other bindings (such asletand lambda parameters) do not accept type signatures.
Note
The
expr: tsyntax is only permitted for atomic expressions.
If you need to type-annotate a complex expression,
enclose the expression in parentheses and follow it with the type annotation.
Examples:
let add = \a.\b. (a + b): Int;
add 2 + 3
// => 5: Int
(cast 10): u8
// => 10: u8
(cast 10): (Result e u8)
// => Ok 10: Result RuntimeError u8
Patterns
_Wildcard pattern(),true,0Literal pattern'A','🎉'Unicode Scalar Value literal patternx,fooVariable patternNil,Cons p psConstructor pattern@[],@[p1, p2],@[p, ps..],@[p, ..],@[ps..],@[..]Array pattern(p1,),(p1, p2)Tuple pattern@{x = p1, y = p2},@{x, y}Record pattern
Important
Old record pattern syntax
@{x: p1, y: p2}are deprecated and no longer supported.
Use new syntax@{x = p1, y = p2}instead.