unsafe code in mlton

Norman Ramsey nr@eecs.harvard.edu
Wed, 19 Sep 2001 17:15:24 -0400


For diagnostic purposes, I've written some unsafe NJ code that
enables me to implement a function of type 'a ref -> int:

structure Refnum : sig val refnum : 'a ref -> int end = struct
  structure O = Unsafe.Object
  type object = O.object
  val n = ref (0, [] : object ref list)

  fun append r = case !n of (k, l) => k+1 before n := (k+1, r :: l)

  fun refnum r =
    let val r = O.toRef (O.toObject r)
        fun look (_, []) = append r 
          | look (k, h::t) = if r = h then k else look(k-1, t)
    in  look (!n)
    end
end


Any idea how I might do something similar in MLton?
Unsafe.cast would probably be enough...


Norman