Go to the Next or Previous section, the Detailed Contents, or the FS(E)L Home Page.


A.3.3 Datatypes

Syntax

datatype T = A.{0..3} | B.Set({0,1}) | C definition of type
A.0, B.{0}, B.{0,1}, C four uses of type

Remarks

Datatypes may not be parameterised (T may not have arguments).

The datatype corresponds to the variant-record construct of languages like Pascal. At the simplest level it can be used to define a number of atomic constants

datatype SimpleColour = Red | Green | Blue

but values can also be associated with the tags

Gun = {0..15}
datatype ComplexColour = RGB.Gun.Gun.Gun | Grey.Gun | Black | White

Values are combined with `.' and labelled using the appropriate tag, so that we could write

make_colour((r.g.b)@@x) =
  if r!=g or g!=b then RGB.x else
  if r==0 then Black else
  if r==15 then White else Grey.r

to encode a colour as briefly as possible.

Note that while it is possible to write

datatype SlowComplexColour = RGB.{r.g.b | r<-Gun, g<-Gun, b<-Gun} | ...

this is less efficient and the resulting type must still be rectangular, i.e., expressible as a simple product type. Hence it is not legal to write

datatype BrokenComplexColour = -- NOT RECTANGULAR
  RGB.{r.g.b | r<-Gun, g<-Gun, b<-Gun, r+g+b < 128 } | ...


Go to the Next or Previous section, the Detailed Contents, or the FS(E)L Home Page.