September 22, 1999 Strong Typing Slide #19

Type Elaboration Example 3: Union Types

        datatype MyNum = IV of int | FV of real;
        val n = FV(1.5);
                val n = FV 1.5 : MyNum

This is really useful as a union type:

        fun Num_to_real (IV i) = real i
          | Num_to_real (FV f) = f ;
                val Num_to_real = fn : MyNum -> real
        fun Numsqrt n = FV(sqrt(Num_to_real n));
                val Numsqrt = fn : MyNum -> MyNum
        fun Numtrunc (IV i) = i
          | Numtrunc (FV f) = trunc f ;
                val Numtrunc = fn : MyNum -> int


Next Copyright © 1999 M-J. Dominus