[MLton] an analysis to flatten refs into tuples

Daniel C. Wang danwang@CS.Princeton.EDU
Mon, 17 May 2004 19:29:20 -0400


The ref flattening is really a nice optimization, but as a MLton user I'd 
like some help from the compiler to warn me when refs that I expect to be 
flattenable aren't.

For example as a user who knows MLTon is doing ref flattening I'd probably 
end up doing the following so that I'm sure that things get flattened the 
way I'd like.

signature M_PAIR :> sig
   type ('a,'b) mpair
   val cons : ('a * 'b) -> ('a, 'b) mpair
   val car : ('a , 'b) mpair -> 'a
   val cdr : ('a , 'b) mpair -> 'b
   val set_car : ('a ,'b) mpair -> 'a -> unit
   val set_cdr : ('a ,'b) mpair -> 'b -> unit
end =
structure MPair :> M_PAIR =
   struct
     type ('a,'b) mpair = ('a ref * 'b ref) (*: mutable tuple :*)
     fun cons (x,y) = (ref x,ref y)
     fun car (ref x,_) = x
     fun cdr (_,ref y) = y
     fun set_car (x,_) x' = x := x'
     fun set_cdr (_,y) y' = y := y'
   end

It would be nice if my mutable tuple comment was some how checked by the ref 
flattening code so that if for some reason it is not smart enough to flatten 
  the refs, I get a warning, and as a user I think about how to structure my 
code so it is more obvious.

In fact given the code above, I expect MLton to be able to flatten any value 
of type ('a,'b) mpair. Of course the reason I expect this is because of 
type-abstraction. I'm not so clear if MLton can exploit this 
type-abstraction info in the ref-flattening analysis. If would be very nice 
if it did.