6.1 Tuples
6.2 Imperative features
Unlike traditional imperative languages (e.g., C), Moby
does not have mutable variables, but it does support mutable
data structures.
Moby allows the fields of object and record types to be
declared as mutable (using the var keyword) and also
provides several types of mutable arrays.
6.2.1 Assignment and L-values
Moby uses the special operator := for all forms of
assignment to mutable data structures.
To make the assignment syntax uniform, we distinguish those
expressions that can appear on the left-hand-side of the
:= operator in the typing rules.
We note this distinction using the judgement form
G |- e |> lval t
when e denotes an L-value of type t.
With this notation, the typing rule for assignment is
|
|
|
G |- e1 |> lval t1
G |- e2 |> t2
G |- t2 <: t1
|
|
|
G |- e1 := e2 |> ()
|
|
L-values are not first-class; for example, it is not possible to pass
an l-value as a function argument.
When an l-value is used in a non-assignment context, we just forget the
annotation, as expressed in the following typing rule:
|
|
|
G |- e |> lval t
|
|
|
G |- e |> t
|
|
6.2.2 Mutable fields
6.2.3 Mutable arrays
|
|
|
G |- e1 |> Array(t)
G |- e2 |> Int
|
|
|
G |- e1 [ e2 ] |> lval t
|
|
While l-values are not first-class, it is possible to reify an l-value
into a pointer value.
The Moby pervasive environment includes the following definitions
for pointers:
type Ptr(t)
const NULL : [t] Ptr(t)
val isNull : [t] Ptr(t) -> Bool
Pointer values are created by taking the address of an l-value using
the prefix & operator.
This expression form has the following typing rule:
|
|
|
G |- e |> lval t
|
|
|
G |- & e |> Ptr(t)
|
|
A pointer can be used as an l-vlaue by applying the prefix *
operator, which has the following typing rule:
|
|
|
G |- e |> Ptr(t)
|
|
|
G |- * e |> lval t
|
|
6.2.5 References
Since reference cells are a common case of mutable data structures,
the Moby pervasive environment provides the Ref type constructor
and supporting operations.
type Ref(t)
val ref : [t] t -> Ref(t)
val deref : [t] Ref(t) -> t
Note that the deref function returns a value, instead of an l-value,
which means that it cannot be used for assignment.
To support assignment of reference cells, Moby overloads the prefix *
operator with the following typing rule:
|
|
|
G |- e |> Ref(t)
|
|
|
G |- * e |> lval t
|
|
6.3 Exceptions
Like most modern languages, Moby provides a mechanism for
signalling and handling exceptional and error conditions.
There are actually two mechanisms for supporting exceptions:
a control-flow mechanism for aborting the current computation
and escaping to some enclosing handler, and a mechanism for
representing information about the situation that caused the
abort.
6.3.1 Raising and handling exceptions
6.3.2 Exception values
6.3.3 The standard exceptions
tagtype Exn
tagtype System of (String) extends Exn
tagtype MatchFail extends Exn
tagtype BindFail extends Exn
tagtype Range extends Exn
tagtype Subscript of (Int) extends Exn
tagtype DivByZero extends Exn
tagtype Fail of String extends Exn
6.4 Threads
6.5 Synchronization