Search code examples
haskelldictionarydynamictype

Haskell -- any easy way to put Data.Dynamic's in a Map?


I want to map Data.Dynamics to other Data.Dynamics. But, I can't write

Map Dynamic Dynamic

because there's no Ord Dynamic. Is there any solution (like a version of Dynamic whose toDyn function only accepts Ord things, and reifies the typeclass upon boxing?)

The motivation for this is to have a map from Variables --> Values inside an EDSL compiler. (Clearly the variables can have different types). edit: Sorry, it's not necessary for this problem, I can/should just store the variable name as the key. But, I'm still interested in the question.

Thanks in advance!!


Solution

  • I hacked a solution if anyone is interested; it's a little tricky / fun :)

    Code here: http://pastebin.com/KiJqqmpj .

    (I also wrote one for higher-order types, which saves you the need of writing a Typeable1, if you always have the same higher-order type function: http://pastebin.com/aqjwFv9p . In some cases, writing Typeable1 instances can be hard.)

    Some values:

    float1 = mk_ord_dyn (1 :: Float)
    float2 = mk_ord_dyn (2 :: Float)
    int1 = mk_ord_dyn (1 :: Int)
    int2 = mk_ord_dyn (2 :: Int)
    

    A little test,

    *OrdDynamic> int1 == int1
    True
    *OrdDynamic> int2 == int2
    True
    *OrdDynamic> int1 < int2
    True
    *OrdDynamic> int2 < float1
    False
    *OrdDynamic> float1 < int2
    True
    *OrdDynamic> int1 == float1
    False