[Haskell-cafe] Dynamic and equality

adam vogt vogt.adam at gmail.com
Fri Jul 19 18:54:55 CEST 2013


On Fri, Jul 19, 2013 at 5:19 AM, Jose A. Lopes <jabolopes at google.com> wrote:
> Hello,
>
> How to define equality for Data.Dynamic ?

Hi Jose,

You could try casting the values to different types that do have an
(==). You can treat the case where you have the types matching, but
didn't list that type beforehand differently.


eqTys a b
    | Just a' <- fromDynamic a, Just b' <- fromDynamic b = a' == (b' :: Int)
    | Just a' <- fromDynamic a, Just b' <- fromDynamic b = a' == (b' :: Integer)
    | show a == show b = error "equal types, but don't know if there's an (==)!"
    | otherwise = False


{-

> eqTys (toDyn 4) (toDyn 5)
False

> eqTys (toDyn 4) (toDyn 4)
True

> eqTys (toDyn 4) (toDyn 4.5)
False

> eqTys (toDyn 4.5) (toDyn 4.5)
*** Exception: equal types, but don't know if there's an (==)!

-}


--
Adam




More information about the Haskell-Cafe mailing list