Previous Up Next

Chapter 8  Type declarations

TypeDecl
::= TypeNameDecl
| DataTypeDecl
| EnumTypeDecl
| TagTypeDecl
| ObjectTypeDecl

8.1  Type name declarations

TypeNameDecl
::= type TypeId TypeParamsopt = Type

8.2  Object type declarations

ObjectTypeDecl
::= objtype TypeId TypeParamsopt ObjectMembers
| objtype TypeId TypeParamsopt = NamedType
ObjectMembers
::= { ObjectMember* }
ObjectMember
::= extends NamedType
| field Label : ExtendedType
| method Label : TypeScheme

8.3  Datatype declarations

DataTypeDecl
::= datatype TypeId TypeParamsopt DataTypeDef
| datatype TypeId TypeParamsopt = NamedType
DataTypeDef
::= { DataConDef (, DataConDef)* }
DataConDef
::= DataCon (of Types)opt

8.4  Enumeration type declarations

EnumTypeDecl
::= enumtype TypeId EnumTypeDef
| enumtype TypeId = Pathopt TypeId
EnumTypeDef
::= { DataCon (, DataCon)* }
Enumeration type declarations introduce an ordered collection of nullary data constructors (or data constants). Because the constants are ordered, two enumeration types are considered equal only when they have the same constants in the same order.

In addition to its constants, an enumeration type declaration introduces a collection of operations. These operations allow efficient mapping between integers and the enumeration type, as well as testing the order of elements. To avoid namespace pollution, we treat the enumeration type as a pseudo-module and use dot notation for most of the operations. For an enumeration type declaration
enumtype T { C1, ..., Cn }
the following operations are defined:
Operation Type Description
T.toInt T -> Int Convert to integer; C1 maps to 0 and Cn maps to n-1.
T.fromInt Int -> T Convert an integer to T; 0 maps to C1 and n-1 maps to Cn. Out of range values cause the exception Range to be raised.
T.first T The first element of the enumeration (i.e., C1).
T.last T The last element of the enumeration (i.e., Cn).
T.succ T -> T Returns the successor of its argument; raises the exception Range on Cn.
T.pred T -> T Returns the predecessor of its argument; raises the exception Range on C0.
T.compare T -> Order Compares the order of two elements.
In addition to these operations, all enumeration types have the standard infix relational and equality operations defined on them.

8.5  Tagtype declarations

TagTypeDecl
::= tagtype TypeId TypeParamsopt (of Types)opt (extends NamedType)opt
| tagtype TypeId TypeParamsopt = NamedType
Tagtypes provide the programmer with a mechanism for defining tree-structured type hierarchies, with checked downward coercions. A tagtype declaration of the form
tagtype T of (t1, ..., tn)
defines a root tagtype T. The name T also serves as a data constructor. A tagtype declaration of the form
tagtype S of (s1, ..., sm) extends T
defines a tagtype S that is a subtype of the type T. In order that the declaration of S be valid, we require that m ³ n and that for 1 £ i £ n we have G |- si <: ti.

There is a standard predefined tagtype, called Exn, that is used to represent exceptions.


Previous Up Next