<div dir="ltr"><div><div><div><div><div><div><div><div>I have found it useful to use Dynamic-like existential types such as (fake example):<br><br></div>> data Number = forall a. (Num a, Typeable a) => Number a<br><br></div>which supports precisely the same API as Dynamic, but with a Num constraint on everything.  However, writing that API for this particular type requires copy-pasting the definitions of fromDyn and fromDynamic as boilerplate, which is (ahem) stupid.  Now that we have the Constraint kind, it is possible to generalize this in a form suitable for Data.Dynamic:<br><br></div>> data CDynamic (c :: Type -> Constraint) = forall a. (c a) => CDynamic (TypeRep a) a</div><div>><br></div>> toCDyn :: (c a, Typeable a) => a -> CDynamic c<br></div>> fromCDyn :: (c a, Typeable a) => CDynamic c -> a -> a<br></div>> fromCDynamic :: (c a, Typeable a) => CDynamic c -> Maybe a</div><div>> relaxCDyn :: CDynamic c -> Dynamic<br></div><div>> dynApplyC :: CDynamic c1 -> CDynamic c2 -> Maybe Dynamic<br></div><div>> dynAppC :: CDynamic c1 -> CDynamic c2 -> Dynamic<br></div><div>> dynTypeRepC :: CDynamic c -> SomeTypeRep<br></div><div>><br></div>> instance Show (CDynamic c)</div><div>> instance Exception (CDynamic c)<br></div><div>></div><div>> class NoC a where -- intentionally empty, can be used unsaturated<br></div><div>> instance NoC a where -- also empty<br></div><div>> type Dynamic = CDynamic NoC</div><div>></div><div>> -- Specializations of all the above functions<br></div><div><br></div><div>I think it's clear how to fill in the definitions.  I added an apparently necessary function relaxCDyn that also does the obvious thing; I'm not really happy with its type signature but offhand I can't say how to generalize it.<br></div><div><br></div><div>I think this is useful, and if I'm not missing anything (obvious or subtle) about it, I want to propose that it goes into Data.Dynamic.</div></div>