[MLton] expansive expressions yielding polymorphic values

Henry Cejtin henry.cejtin at sbcglobal.net
Thu Feb 28 10:01:54 PST 2008


I  have  been  doing  a  ML  wrapper for the Cairo drawing library, and using
phantom types to keep the different types of surfaces and contexts  distinct.
In  doing  this,  it  was  a  bit  painful because I needed almost all of the
functions to be polymorphic (so that they could work on all kinds of surfaces
or all kinds of contexts), which meant lots of (small) code duplication.

Much  to  my  surprise,  I  realized  that  it  is  actually legal to have an
expansive expression produce something which is polymorphic.  What I mean  by
this is illustrated by the following code:

    signature Z = sig
       type 'a t
       val f: 'a t -> int
       val g: unit -> 'a t
    end

    structure Z = struct

       type 'a t = unit

       fun id x = x

       val f = id o (fn () => 99)

       val g = id
    end:> Z


    val t1: int Z.t = Z.g ()
    val t2: real Z.t = Z.g ()
    val i1 = Z.f t1
    val i2 = Z.f t2

Note,  even  though  f  is  the  result  of an expansive expression (function
composition), the signature Z can make its type polymorphic.

Can people confirm that this really is supposed to be legal?




More information about the MLton mailing list