[MLton] HOL's Moscow ML implementation, and pushing MLton to emulate it

Daniel C. Wang danwang@CS.Princeton.EDU
Thu, 07 Apr 2005 15:37:47 -0400


Here's one example of how to deal with polymorphic recursive structures in a 
uniform way without recursively coercing the whole structure into a 
univerisal rep.

(*--------------------------------------------------------------------------*)
structure U :> sig
   datatype ('a,'b) T  =
     I of int
   | S of string
   | P of ('a * 'b)
   val mapF : ('a -> 'c) -> ('b -> 'd) -> ('a,'b) T -> ('c,'d) T
end =
struct
   datatype ('a,'b) T  =
     I of int
   | S of string
   | P of ('a * 'b)
   fun mapF f g (I i) = (I i)
     | mapF f g (S s) = (S s)
     | mapF f g (P(x,y)) = P(f x,g y)
end

structure ToString =
   struct
     fun toStringU f g l =
       case (U.mapF f g l) of
	(U.I i) => (Int.toString i)
       | (U.S s) => "\""^s^"\""
       | (U.P(x,y)) =>
         "<"^(toStringU f g x)^(", ")^(toStringU f g y)^">"

     fun copyU f g l =
       case (U.mapF f g l) of
	(U.I i) => (U.I i)
       | (U.S s) => (U.S s)
       | (U.P(x,y)) => U.P(copyU f g x,copyU f g y)

     fun injInt i = (U.I i)
     fun injList [] = (U.I 0)
       | injList (x::xs) = U.P(x,xs)

     fun toStringList t l =  toStringU t injList (injList l)
     fun toStringIntList l =  toStringList injInt l
   end