Expressions & Patterns
Expressions
In Pinafore, the main program is a single expression. These are the different kind of expressions:
string literals:
"abc\ndef"
. Use backslash for certain escapes.numeric literals:
34.5
,~34.5
. The first is a rational, the second is an inexact number (internally represented as a double).names:
x
. These are bound by declarators (e.glet
) and patterns in function bindings (fn
andmatch
).functions:
fn x => x + 2 * x
,match Just x => x; None => 0; end
. These are constructed byfn
(which have one “case”, matching a pattern to an expression) or bymatch
(with multiple cases)if expressions:
if t then "yes" else "no"
.declarated expressions:
let x = 3 in x + x
,with N in x
. The declarator modifies the expressions to pass declarations.do-notation:
do x <- f; return x + x; end
. This notation makes it easier to work with monad-like types.applicative notation:
{.List %x + %y}
. This notation makes it easier to work with applicative-like types.function applications:
sort mylist
. This is applying an expression (the function) to an expression (the argument).list constructors:
[1,2,3]
.tuple constructors:
(1,"xyz",())
. Note that there are no tuple types larger than pairs, so this is equivalent to(1,("xyz",()))
.
Patterns
Patterns are for matching against values in function cases (fn
and match
) and in let
-bindings.
An underscore matches everything.
A lower-case name matches everything, and binds to the matched value.
A capitalised name (followed by patterns) matches a data constructor.
The form
P@Q
matches when both patternsP
andQ
match.The form
P : T
matches whenP
matches. Pinafore will reject this if patternP
cannot be subsumed to typeT
.The form
P :? T
matches when the value can be dynamically cast toT
, andP
matches that.The form
P as N
matches whenP
matches. Names matched inP
will be put in namespaceN
.