haskell - How to apply a polymorphic function to a Dynamic value -


is there sane way apply polymorphic function value of type dynamic?

for instance, have value of type dynamic , want apply just value inside dynamic. if value constructed todyn true want result todyn (just true). number of different types can occur inside dynamic not bounded.

(i have solution when types involved come closed universe, it's unpleasant.)

this perhaps not sanest approach, can abuse reflection package lie typerep.

{-# language rank2types, flexiblecontexts, scopedtypevariables #-} import data.dynamic import data.proxy import data.reflection import ghc.prim (any) import unsafe.coerce  newtype withrep s = withrep { withrep :: }  instance reifies s typerep => typeable (withrep s a)   typeof s = reflect (proxy :: proxy s) 

given can peek @ typerep of our dynamic argument , instantiate our dynamic function appropriately.

apd :: forall f. typeable1 f => (forall a. -> f a) -> dynamic -> dynamic apd f = dynapp df   t = dyntyperep         df = reify (mkfunty t (typeof1 (undefined :: f ()) `mkappty` t)) $                    \(_ :: proxy s) -> todyn (withrep f :: withrep s (() -> f ())) 

it lot easier if base supplied apd us, requires rank 2 type, , typeable/dynamic manage avoid them, if data not.

another path exploit implementation of dynamic:

data dynamic = dynamic typerep 

and unsafecoerce own dynamic' data type, need typerep in internals, , after applying function, unsafecoerce back.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -