[core libraries] RE: Data.Dynamic: Any vs existential

Dan Doel dan.doel at gmail.com
Mon Mar 10 15:54:34 UTC 2014


On Mon, Mar 10, 2014 at 7:26 AM, Simon Peyton Jones
<simonpj at microsoft.com>wrote:

> Dear Core Libraries committee
>
> I think Roman is right here. Moreover we have known this for at least I
> think seven years
> http://comments.gmane.org/gmane.comp.lang.haskell.cafe/20097, where I
> wrote:
>
> | Yes, Dynamic preceded the Typeable class, I think.
> | Were we to do it today, I think we'd have
> |
> | data Dynamic = forall a . (Typeable a) => Dynamic a
> |
> | Whether it's worth changing, I'm not sure.  It's a library so,
> | if a change desirable, anyone could take a lead.
>
> The new representation for Dynamic would be good because it's less
> insecure than all this "Obj" nonsense, instead relying on Typeable, which
> is pretty good these days.
>
> Pedro is the most recent visitor to this territory and may have views.
>
> Roman's point about the method for the Typeable class is a good one too,
> and not one I've seen discussed.
>
> Over to you
>

I, at least, am in favor of this. I've thought for a while that the ideal
form of all this is something like:

    data TypeRep a -- indexed by a

    class Typeable a where
      typeRep :: TypeRep a

    data Dynamic = forall a. Dynamic (TypeRep a) a


Where, ideally, TypeRep is an indexed type, and you can match on it to
refine its argument. Then Typeable just becomes the implicit version of
this evidence, for convenience. TypeRep versions of cast would also have to
be primitive probably (since one can not, a priori, enumerate the diagonal
of all possible monotypes, now and in the future).

Anyhow, I don't know if this entire interface is or will be implementable
in GHC (the idea comes from a document about JHC way back, although I don't
know if even it implements this), but using Typeable in an existential
captures the Dynamic part of it, at least.

Also, I hadn't seen the Tagged version of Typeable before (and hadn't
thought of it myself). I expect it's an oversight that this wasn't the form
chosen, as it seems superior to me. In fact, it seems like every situation
where the 0-width Proxy# could be used, Tagged could also be used instead,
and is automatically 0-width, and has better sharing behavior? Unless you
explicitly don't want sharing, that is.

It seems like any relative inconvenience could probably be alleviated by
the proxy function in tagged, while keeping the nicer behavior.

-- Dan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20140310/a9423c4f/attachment.html>


More information about the Libraries mailing list