[Haskell-cafe] Implementation of "Dynamic" datatype

Isaac Dupree isaacdupree at charter.net
Fri Mar 2 18:37:55 EST 2007

Hash: SHA1

Currently we have
data Dynamic = Dynamic TypeRep Obj
where Obj is
{-in GHC up to 6.6-} type Obj = forall a . a
{-in GHC 6.7.something-} type Obj = Any
 -- Use GHC's primitive 'Any' type to hold the dynamically typed value.
{-in some other compilers (like Hugs)-} data Obj = Obj

(	According to this comment, it is not data Obj = Obj
	in GHC now because:
	 -- In GHC's new eval/apply execution model this type must not
	 -- look like a data type.  If it did, GHC would use the
	 -- constructor convention when evaluating it, and this will go
	 -- wrong if the object is really a function.  Using Any [or,
	 -- earlier comments said, a polymorphic type] forces GHC to use
	 -- a fallback convention for evaluating it that works for all
	 -- types.

So, my question is: why doesn't it just use an existential? :
data Dynamic = forall a. Dynamic TypeRep a
According to
http://hackage.haskell.org/trac/haskell-prime/wiki/HaskellExtensions ,
all important implementations support existentials (and it will probably
go into Haskell'). It seems semantically the most accurate, doesn't
require an unsafeCoerce when _constructing_ a Dynamic, and doesn't
require unsafeCoercing between unequal types (which had caused problems
in GHC as noted above), so it seems like it could be a portable
implementation.  A quick copy of Dynamic.hs with these modifications
seemed to work fine in ghc6.4.2, ghc6.6, and hugs20050308 (which is all
haskell implementations that are working for me at the moment (nhc98 is
broken for me) and that have Data.Typeable (not yhc currently, it seems)).

(	Also I wonder about the performance implications of changing it
	data Dynamic = forall a. Typeable a => Dynamic a
	or making the TypeRep be a strict field (which shouldn't have
	any semantic impact since typeOf always succeeds(assuming no-one
	defines a horrible instance of Typeable) and typeOf's result is
	always what is put into that field. (Separately, should seq'ing
	a Dynamic seq the encapsulated object, or should Dynamic act as
	a box as it does now?)).

Anyway, how would I go about seriously testing this in ghc, hugs... to
see if it breaks anything (and also see how it affects Dynamic's
performance)?  (Since I am interested in the refactoring of the
base/core libraries, a little something like this seems like a good
place to start learning how to test what I have.)

- - Was this a good mailing list to choose for this question? (And was it
a good question? what about all the little question-ideas branching off
of it? I can never be sure...)
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org


More information about the Haskell-Cafe mailing list