dynamic types

Thomas Conway conway@mds.rmit.edu.au
Wed, 8 Jan 2003 08:42:20 +1100


On Wed, 8 Jan 2003 07:19 am, Hal Daume III wrote:
> Another way to do it would be to define a "universal" type:
> > data Univ =3D UUnit () | UInteger Integer | UDouble Double | ...
>

I'm fairly new to Haskell, but worked on Mercury until recently.
Mercury has a type "univ" which might be declared something like:

data Univ =3D Univ a

(I don't know the correct syntax for existentially quantified types in=20
haskell, but the intent of the declaration is that the monomorphic type U=
niv=20
has a single constructor is a value of existentially quantified type)

The library then has functions that do the equivalent of

mkUniv :: a -> Univ

and

getValue :: Univ -> Maybe a

The interesting bit is getValue. In implementation terms, what it does is
compare the dictionary stored in the Univ value (values containing=20
existentially quantified components also contain the dictionary for the=20
existentially quantified type variables) with the one that is implicitly=20
passed in for the result type.  As it happens, almost all of this can be
done in Mercury using some of the reflection libraries.

I believe (nth hand) that something similar has been done in haskell, but=
 my=20
understanding is that it isn't in the standard library.

Tom
--=20
    Dr Thomas Conway      Multimedia Database Systems, RMIT University
 <conway@mds.rmit.edu.au> 499 User error! Replace user, and press any key=
=2E