Skip to content

Instantly share code, notes, and snippets.

@lspitzner
Last active June 2, 2017 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lspitzner/f8d3eac8c94138e9863a133a42818a96 to your computer and use it in GitHub Desktop.
Save lspitzner/f8d3eac8c94138e9863a133a42818a96 to your computer and use it in GitHub Desktop.
-- question: how could we define this type:
data DynamicFrom ts = ???
-- where this datatype needs to support the following interface:
-- wrap a HList-uncurried function. We'll probably need to somehow pack the
-- Dict (Typeable a) (rather: the TypeRep) as well.
packDynamicFrom :: Typeable a => (HList ts -> a) -> DynamicFrom ts
-- Wrap an arbitrary Dynamic. Note how in this case we have a TypeRep in
-- the input, but no Dict (Typeable a) for some a, like we do in the other
-- packing function above.
packDynamicFromNil :: Dynamic -> DynamicFrom HNil
-- works similar to `fromDynamic` - reverses the packing presuming that the
-- type(reps) align.
unpackDynamicFrom :: forall ts a . Typeable a => DynamicFrom ts -> Maybe (HList ts -> a)
-- curry the function and drop the wrapper, returning simply Dynamic.
-- these classes come from the mechanics for HList-currying in the HList package.
curryDynamicFrom :: (ArityRev f n, ArityFwd f n, HCurry' n f ts Dynamic) => DynamicFrom ts -> f
-- returns typeOf (undefined :: a) for the a matching when packing, i.e. the
-- output type.
outputTypeDynamicFrom :: DynamicFrom ts -> TypeRep
-- turn list-of-DynamicFrom into DynamicFrom-of-list, presuming that the list
-- is homogenous. An important aspect is that the same is not even possible
-- on regular `Dynamic`s.
wrapHomogenous :: [DynamicFrom ts] -> Maybe (DynamicFrom ts)
-----------
-- possible solutions and issues:
data DynamicFrom ts = DynamicFrom TypeRep (HList ts -> Dynamic)
-- issues:
-- - unpackDynamicFrom requires unsafe internals (minor)
-- - cannot implement wrapHomogenous
-- - the invariant that the TypeRep matches the Dynamic's one is not
-- encoded in the types.
data DynamicFrom ts = forall a . DynamicFrom (Dict (Typeable a)) (HList ts -> a)
-- issues:
-- - cannot implement packDynamicFromNil
data DynamicFrom ts where
DynamicFrom :: Dict (Typeable a) -> (HList ts -> a) -> DynamicFrom ts
LiftedDynamic :: Dynamic -> DynamicFrom '[]
-- issues:
-- - cannot implement wrapHomogenous
-- - the representation is morally redundant and needs more code to implement
-- the interface.
-----------
-- my conclusion: reimplementing Dynamic is the only option that would allow
-- fully supporting the interface.
-- any errors/counter-arguments to my conclusion?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment