05 Variant Syntax and Semantics

Variant Syntax

The syntax for defining a variant uses the type declaration syntax.

type t =
    | C1 of t1
    | ...
    | Cn of tn

Each variant needs to start with an uppercase letter:

The of tn clause is optional, variants do not need to carry data.

The C1 part is called a "constructor," or sometimes a "tag."

We call a constructor "constant" if it carry data, and a constructor "non-constant" if it does not carry data.

Variant Evaluation

If e ==> v, then C e ==> C v.

Type Checking

C e : t if t = ... | C of t' | ... and e : t'

Pattern Matching

C p is a pattern.