Thanks to Vlad and Jaro, your solution of `apD` compiles, I think it should work.
But unfortunately my real case is a little different / more complex, a MWE appears like this:
holdEvent :: Dynamic -> Dynamic
holdEvent (Dynamic t evs') =
withTypeable t $ Dynamic typeRep (hcHoldEvent evs')
where
hcHoldEvent :: forall a. EventSink a -> IO (TimeSeries a)
hcHoldEvent !evs = do
!holder <- newIORef Nothing
listenEvents evs $ writeIORef holder . Just
return $ TimeSeries $ readIORef holder
data EventSink a = EventSink
{ listenEvents :: (a -> IO ()) -> IO (),
closeStream :: IO ()
}
instance Functor EventSink where
fmap = undefined
newtype TimeSeries a = TimeSeries {readTimeSeries :: IO (Maybe a)}
instance Functor TimeSeries where
fmap = undefined
Now I'm clueless how to use the `withTypeable` trick to apply my polymorphic `hcHoldEvent` to `Dynamic`, naively written as in above, the error is:
src/PoC/DynPoly.hs:20:49: error:
• Couldn't match expected type ‘EventSink a0’ with actual type ‘a’
‘a’ is a rigid type variable bound by
a pattern with constructor:
Dynamic :: forall a.
base-4.13.0.0:Data.Typeable.Internal.TypeRep a -> a -> Dynamic,
in an equation for ‘holdEvent’
at src/PoC/DynPoly.hs:19:12-25
• In the first argument of ‘hcHoldEvent’, namely ‘evs'’
In the second argument of ‘Dynamic’, namely ‘(hcHoldEvent evs')’
In the second argument of ‘($)’, namely
‘Dynamic typeRep (hcHoldEvent evs')’
• Relevant bindings include
evs' :: a (bound at src/PoC/DynPoly.hs:19:22)
t :: base-4.13.0.0:Data.Typeable.Internal.TypeRep a
(bound at src/PoC/DynPoly.hs:19:20)
|
20 | withTypeable t $ Dynamic typeRep (hcHoldEvent evs')
| ^^^^