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!!

有帮助吗?

解决方案 2

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

其他提示

No, there is no way. By the types involved in the creating Dynamic there cannot be. If you want to be impure you can use StablePtr, but that's the best I can think of.

But by changing the type of toDyn to include Ord it can be done.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top