[MLton] Universal type wiki page wrong?

Wesley W. Terpstra wesley@terpstra.ca
Wed, 26 Jan 2005 21:35:32 +0100


The page http://mlton.org/UniversalType seems to suggest that the following
UNIVERSAL_TYPE implementation would be correct, just leaky:

structure U2:> UNIVERSAL_TYPE =
  struct
    type t = unit -> unit
    fun embed () =
      let
        val r = ref NONE
        fun inject a () = r := SOME a
        fun project f = (f (); !r)
      in (inject, project) end
  end

I think this is wrong... Try the following:

fun out (SOME x) = print (Int.toString x ^ "\n")
  | out NONE = print "NONE\n"

val (i1, p1) = U2.embed ()
val (i2, p2) = U2.embed ()
val (x, y) = (i1 5, i2 8)
val () = (out (p1 x); out (p2 x); out (p1 y); out (p2 y))

The output should be "5 NONE NONE 8", but is "5 NONE 5 8".
Without the clearing the cell, I think you see stale values.

-- 
Wesley W. Terpstra