Changes to Typeable

Ben Millwood haskell at
Thu Oct 4 19:48:42 CEST 2012

On Thu, Oct 4, 2012 at 5:03 PM, Tyson Whitehead <twhitehead at> wrote:
> On October 4, 2012 07:59:29 Ganesh Sittampalam wrote:
>> I have some nebulous concerns about abstraction violation, but since
>> Typeable only lets you find out what the structure is, not actually take
>> apart the contained data, it doesn't seem too much of a problem.
> Ummm.  Isn't that what cast (in Data.Typeable) does?
> cast :: (Typeable a, Typeable b) => a -> Maybe b
> For example, the following function
> import Data.Typeable
> import Data.Maybe
> magic :: Typeable a => a -> a
> magic x =
>   case cast x of
>     Just x -> cast $ fromJust $ x ++ " (ta da)"
>     _  -> x
> gives you id except for strings.

ITYM fromJust $ cast rather than the other way around. But the ability
to cast like this does not give you access to data structure you
didn't already have – it does not violate abstraction.

On the other hand, were we to do the same with Data, we /would/
violate abstraction, since Data is capable of synthesising
constructors and retrieving fields and so forth. Hence manual
instances of Data are fairly common, e.g. for Map and Set. Typeable
doesn't seem to give you anything like that, so it seems safe.

More information about the Libraries mailing list