SML# Document Version 4.1.0
23 Declarations of the core language and their interfaces

23.3 datatype declaration : datatypeDecl

datatype declarations define new type constructors. Its syntax is given below.

datatypeDecl ::= datatype datbind (withtype tybind)?
datbind ::= (tyvarSeq)? tycon = conbind (and datbind)?
conbind ::= (op)? vid (of ty)? (| conbind)?
tybind ::= tyvarSeq tycon = ty (and tybind)?

The set of type constructor names tycon must be pair-wise distinct, and the data constructor names in the same conbind must be pair-wise distinct. This defines the set of mutually recursive new type constructors tycon, and data constructors vid. If tycon has optional (tyvarSeq)? declaration, then it is a polymorphic type constructor with type parameters tyvarSeq. The scope of tycon include the entire datatypeDecl.

23.3.1 datatype declaration interface

Interface of datatype declaration is either datatype specification or opaque type specification. Syntax of datatype specification is the same as datatype declarations. Opaque type specification has the following syntax.

opequeTypeSpec ::= type (tyvarSeq)? tycon ( = runtimeTypeSpec )
runtimeTypeSpec := unit  | contag  | boxed

A datatype specification defines the type constructors and data constructors of the corresponding datatype declaration, and makes them available to the compilation units that reference this interface through _require. An opaque type specification must defines the type constructor of the corresponding datatype declaration as an opaque type, and makes it available to the compilation units that reference this interface through _require. The operand of a opaque type specification specifies the runtime representation of values of the type. It is either unit, contag, or boxed. unit and contag indicates that all the datatype constructors have no argument. unit indicates that the type consists only of a single constructor. contag means that there are multiple constructors. boxed indicates that the runtime representation is a pointer. This must be specified for a datatype constructor that has a constructor with augment.

23.3.2 Examples

The following are examples of datatype declarations and their interface.

Data.sml file: datatype ’a list = nil | :: of ’a * ’a list
datatype ’a queue = QUEUE of ’a list * ’a list
Data.smi file: datatype ’a list = nil | :: of ’a * ’a list
type ’a queue (= boxed)