MLton 20070826 DefineTypeBeforeUse
Home  Index  
Standard ML requires types to be defined before they are used. Because of type inference, the use of a type can be implicit; hence, this requirement is more subtle than it might appear. For example, the following program is not type correct, because the type of r is t option ref, but t is defined after r.
val r = ref NONE
datatype t = A | B
val () = r := SOME A

MLton reports the following error, indicating that the type defined on line 2 is used on line 1.

Error: z.sml 1.1.
  Type escapes the scope of its definition at z.sml 2.10.
    type: t
    in: val r = ref NONE

While the above example is benign, the following example shows how to cast an integer to a function by (implicitly) using a type before it is defined. In the example, the ref cell r is of type t option ref, where t is defined after r, as a parameter to functor F. This example causes PolyML 4.1.3 to seg fault.

val r = ref NONE
functor F (type t
           val x: t) =
   struct
      val () = r := SOME x
      fun get () = valOf (!r)
   end
structure S1 = F (type t = unit -> unit
                  val x = fn () => ())
structure S2 = F (type t = int
                  val x = 13)
val () = S1.get () ()

MLton reports the following error.

Warning: z.sml 1.1.
  Unable to locally determine type of variable: r.
    type: ??? option ref
    in: val r = ref NONE
Error: z.sml 1.1.
  Type escapes the scope of its definition at z.sml 2.17.
    type: t
    in: val r = ref NONE


Last edited on 2005-12-01 03:38:39 by StephenWeeks.