Chapter 2 Functional programming in Moby
Moby is a value-oriented language in the tradition
of the ML-family of languages, and it supports common features of
these languages, such as polymorphism, datatypes, pattern matching,
higher-order functions, and exceptions.
In this chapter, we survey these features in Moby.
2.1 Basic values and expressions
Moby supports multiple function and constructor arguments and
multiple return values using tuples.
Unlike many functional languages, tuples are not first-class.
It is not possible to bind a variable to a tuple value or to
instantiate a type variable with a tuple type.
The Moby basis will provide type constructors for pairs
and triples, and possibly a more general mechanism.
2.2 Modules
2.3 Functions
2.4 Exceptions
2.5 Data constructors and pattern matching
As in most functional languages, data constructors
are used both to construct values and to deconstruct them.
2.5.1 Pattern matching
2.5.2 Constant definitions
2.5.3 Expression forms
Because of the importance of pattern matching in deconstructing
data structures, Moby provides several expression forms as
shorthands.
One can test the structure of a value in a couple of ways.
The ``?'' modifier can be applied to a data constructor
or constant to create a predicate.
For example, ``? None'' defines a predicate
function that returns True when applied to None.
It is equivalent to the function
fn : [t] Option(t) -> Bool { None => True, _ => False }
It is also possible to test a value against a more complicated pattern.
For example,
(x is Some(_::_))
evaluates to True when x matches the pattern
``Some(_::_)''.
Lastly, Moby provides syntactic sugar for deconstructing values.
The ``#'' modifier can be applied to a data constructor
to create a projection function.
For example, ``# Some'' defines a projection
function that returns the argument of the Some constructor.
It is equivalent to the function
fn : [t] Option(t) -> t { Some(x) => x, _ => raise Match }
2.6 Concrete types
Most constants and data constructors are defined as part of
concrete type definitions.
Moby provides three kinds of concrete type definition: datatypes,
enumerations, and tagtypes.
2.6.2 Enumerations