dynamic types

Fergus Henderson fjh@cs.mu.OZ.AU
Tue, 14 Jan 2003 19:13:57 +1100


On 08-Jan-2003, Andrew J Bromage <ajb@spamcop.net> wrote:
> On Wed, Jan 08, 2003 at 08:42:20AM +1100, Thomas Conway wrote:
> 
> > Mercury has a type "univ" which might be declared something like:
> 
> 	data Univ = forall a. Univ a
> 
> > I believe (nth hand) that something similar has been done in haskell, but
> > my understanding is that it isn't in the standard library.
> 
> That would be Data.Dynamic:
> 
> 	data Dynamic		-- abstract
> 	toDyn :: (Typeable a) => a -> Dynamic
> 	fromDynamic :: (Typeable a) => Dynamic -> Maybe a
...
> The Typeable class basically implements RTTI explicitly:
> 
> 	class Typeable a where
> 	  typeOf :: a -> TypeRep
> 
> Unfortunately, Typeable can't be derived automatically.

That's not the only problem.  The other problem is that because
`Typeable' instances aren't built-in, `fromDynamic' is not type-safe.
The implementation of `fromDynamic' calls `typeOf' and then if the types
match, it does an unsafe cast.  If `typeOf' lies, then `fromDynamic' may
break type safety.  So it is not safe to allow the use of `fromDynamic'
if you are executing untrusted code.

> Maybe in Haskell 2.

Yes, it would be nice to have a built-in, type-safe, version of Dynamic
in Haskell 2.

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.