<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">Thanks to Vlad and Jaro, your solution of `apD` compiles, I think it should work.</div><div class=""><br class=""></div><div class="">But unfortunately my real case is a little different / more complex, a MWE appears like this:</div><div class=""><br class=""></div><div class="">```hs</div><div class=""><div style="color: rgb(212, 190, 152); background-color: rgb(23, 24, 24); font-family: Menlo, Monaco, "Courier New", monospace; line-height: 18px; white-space: pre;" class=""><div class=""><span style="color: #66a89d;" class="">holdEvent</span><span style="color: #96b946;" class=""> </span><span style="color: #cc524b;font-weight: bold;" class="">::</span><span style="color: #96b946;" class=""> </span><span style="color: #6b95c5;" class="">Dynamic</span><span style="color: #96b946;" class=""> </span><span style="color: #cc524b;font-weight: bold;" class="">-></span><span style="color: #96b946;" class=""> </span><span style="color: #6b95c5;" class="">Dynamic</span></div><div class=""><span style="color: #d4bfa0;" class="">holdEvent </span>(<span style="color: #cca143;" class="">Dynamic</span><span style="color: #d4bfa0;" class=""> t evs'</span>)<span style="color: #d4bfa0;" class=""> </span><span style="color: #cc524b;font-weight: bold;" class="">=</span></div><div class=""><span style="color: #d4bfa0;" class="">  withTypeable t </span><span style="color: #a3d349;" class="">$</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cca143;" class="">Dynamic</span><span style="color: #d4bfa0;" class=""> typeRep </span>(<span style="color: #d4bfa0;" class="">hcHoldEvent evs'</span>)</div><div class=""><span style="color: #d4bfa0;" class="">  </span><span style="color: #cc524b;" class="">where</span></div><div class=""><span style="color: #96b946;" class="">    </span><span style="color: #66a89d;" class="">hcHoldEvent</span><span style="color: #96b946;" class=""> </span><span style="color: #cc524b;font-weight: bold;" class="">::</span><span style="color: #96b946;" class=""> </span><span style="color: #cc524b;font-style: italic;" class="">forall</span><span style="color: #96b946;" class=""> </span><span style="color: #82aed8;" class="">a</span><span style="color: #cc524b;font-weight: bold;" class="">.</span><span style="color: #96b946;" class=""> </span><span style="color: #6b95c5;" class="">EventSink</span><span style="color: #96b946;" class=""> </span><span style="color: #82aed8;" class="">a</span><span style="color: #96b946;" class=""> </span><span style="color: #cc524b;font-weight: bold;" class="">-></span><span style="color: #96b946;" class=""> </span><span style="color: #6b95c5;" class="">IO</span><span style="color: #96b946;" class=""> </span>(<span style="color: #6b95c5;" class="">TimeSeries</span><span style="color: #96b946;" class=""> </span><span style="color: #82aed8;" class="">a</span>)</div><div class=""><span style="color: #d4bfa0;" class="">    hcHoldEvent </span><span style="color: #cc524b;font-weight: bold;" class="">!</span><span style="color: #d4bfa0;" class="">evs </span><span style="color: #cc524b;font-weight: bold;" class="">=</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cc524b;" class="">do</span></div><div class=""><span style="color: #d4bfa0;" class="">      </span><span style="color: #cc524b;font-weight: bold;" class="">!</span><span style="color: #d4bfa0;" class="">holder </span><span style="color: #cc524b;font-weight: bold;" class=""><-</span><span style="color: #d4bfa0;" class=""> newIORef </span><span style="color: #cca143;" class="">Nothing</span></div><div class=""><span style="color: #d4bfa0;" class="">      listenEvents evs </span><span style="color: #a3d349;" class="">$</span><span style="color: #d4bfa0;" class=""> writeIORef holder </span><span style="color: #a3d349;" class="">.</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cca143;" class="">Just</span></div><div class=""><span style="color: #d4bfa0;" class="">      return </span><span style="color: #a3d349;" class="">$</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cca143;" class="">TimeSeries</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #a3d349;" class="">$</span><span style="color: #d4bfa0;" class=""> readIORef holder</span></div><br class=""><div class=""><span style="color: #cc524b;" class="">data</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #6b95c5;" class="">EventSink</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #82aed8;" class="">a</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cc524b;font-weight: bold;" class="">=</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cca143;" class="">EventSink</span></div><div class=""><span style="color: #d4bfa0;" class="">  </span><span style="color: #cca143;" class="">{</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cca143;" class="">listenEvents</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cc524b;font-weight: bold;" class="">::</span><span style="color: #d4bfa0;" class=""> </span>(<span style="color: #82aed8;" class="">a</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cc524b;font-weight: bold;" class="">-></span><span style="color: #d4bfa0;" class=""> </span><span style="color: #6b95c5;" class="">IO</span><span style="color: #d4bfa0;" class=""> </span>())<span style="color: #d4bfa0;" class=""> </span><span style="color: #cc524b;font-weight: bold;" class="">-></span><span style="color: #d4bfa0;" class=""> </span><span style="color: #6b95c5;" class="">IO</span><span style="color: #d4bfa0;" class=""> </span>(),</div><div class=""><span style="color: #d4bfa0;" class="">    </span><span style="color: #cca143;" class="">closeStream</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cc524b;font-weight: bold;" class="">::</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #6b95c5;" class="">IO</span><span style="color: #d4bfa0;" class=""> </span>()</div><div class=""><span style="color: #d4bfa0;" class="">  </span><span style="color: #cca143;" class="">}</span></div><br class=""><div class=""><span style="color: #cc524b;" class="">instance</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #6b95c5;" class="">Functor</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #6b95c5;" class="">EventSink</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cc524b;" class="">where</span></div><div class=""><span style="color: #d4bfa0;" class="">  fmap </span><span style="color: #cc524b;font-weight: bold;" class="">=</span><span style="color: #d4bfa0;" class=""> undefined</span></div><br class=""><div class=""><span style="color: #cc524b;" class="">newtype</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #6b95c5;" class="">TimeSeries</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #82aed8;" class="">a</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cc524b;font-weight: bold;" class="">=</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cca143;" class="">TimeSeries</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cca143;" class="">{readTimeSeries</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cc524b;font-weight: bold;" class="">::</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #6b95c5;" class="">IO</span><span style="color: #d4bfa0;" class=""> </span>(<span style="color: #6b95c5;" class="">Maybe</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #82aed8;" class="">a</span>)<span style="color: #cca143;" class="">}</span></div><br class=""><div class=""><span style="color: #cc524b;" class="">instance</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #6b95c5;" class="">Functor</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #6b95c5;" class="">TimeSeries</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cc524b;" class="">where</span></div><div class=""><span style="color: #d4bfa0;" class="">  fmap </span><span style="color: #cc524b;font-weight: bold;" class="">=</span><span style="color: #d4bfa0;" class=""> undefined</span></div><br class=""></div></div><div class="">```</div><div class=""><br class=""></div><div class="">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:</div><div class=""><br class=""></div><div class="">```log</div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(152, 244, 245); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">src/PoC/DynPoly.hs:20:49: </b></span><span style="font-variant-ligatures: no-common-ligatures; color: #fc3522" class=""><b class="">error:</b></span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">    • Couldn't match expected type ‘EventSink a0’ with actual type ‘a’</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">      ‘a’ is a rigid type variable bound by</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">        a pattern with constructor:</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">          Dynamic :: forall a.</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">                     base-4.13.0.0:Data.Typeable.Internal.TypeRep a -> a -> Dynamic,</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">        in an equation for ‘holdEvent’</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">        at src/PoC/DynPoly.hs:19:12-25</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">    • In the first argument of ‘hcHoldEvent’, namely ‘evs'’</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">      In the second argument of ‘Dynamic’, namely ‘(hcHoldEvent evs')’</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">      In the second argument of ‘($)’, namely</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">        ‘Dynamic typeRep (hcHoldEvent evs')’</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">    • Relevant bindings include</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">        evs' :: a (bound at src/PoC/DynPoly.hs:19:22)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">        t :: base-4.13.0.0:Data.Typeable.Internal.TypeRep a</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">          (bound at src/PoC/DynPoly.hs:19:20)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(207, 125, 255); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">   |</b></span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #cf7dff" class=""><b class="">20 |</b></span><span style="font-variant-ligatures: no-common-ligatures" class="">   withTypeable t $ Dynamic typeRep (hcHoldEvent </span><span style="font-variant-ligatures: no-common-ligatures; color: #fc3522" class=""><b class="">evs'</b></span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(252, 53, 34); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #cf7dff" class=""><b class="">   |</b></span><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">                                                 ^^^^</b></span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24); min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div></div><div class="">```</div><div class=""><br class=""></div><div class="">Thanks with best regards,</div><div class="">Compl</div><div class=""><br class=""></div><div class=""><br class=""><blockquote type="cite" class=""><div class="">On 2021-04-12, at 22:04, Jaro Reinders <<a href="mailto:jaro.reinders@gmail.com" class="">jaro.reinders@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><span class="" style="caret-color: rgb(0, 0, 0); float: none; display: inline !important;">I have no experience in this area, but this compiles:</span><br class="" style="caret-color: rgb(0, 0, 0);"><br class="" style="caret-color: rgb(0, 0, 0);"><span class="" style="caret-color: rgb(0, 0, 0); float: none; display: inline !important;">```</span><br class="" style="caret-color: rgb(0, 0, 0);"><span class="" style="caret-color: rgb(0, 0, 0); float: none; display: inline !important;">{-# LANGUAGE RankNTypes, ScopedTypeVariables #-}</span><br class="" style="caret-color: rgb(0, 0, 0);"><span class="" style="caret-color: rgb(0, 0, 0); float: none; display: inline !important;">import Type.Reflection</span><br class="" style="caret-color: rgb(0, 0, 0);"><span class="" style="caret-color: rgb(0, 0, 0); float: none; display: inline !important;">import Data.Dynamic</span><br class="" style="caret-color: rgb(0, 0, 0);"><br class="" style="caret-color: rgb(0, 0, 0);"><span class="" style="caret-color: rgb(0, 0, 0); float: none; display: inline !important;">appD :: forall f. Typeable f => (forall a. a -> f a) -> Dynamic -> Dynamic</span><br class="" style="caret-color: rgb(0, 0, 0);"><span class="" style="caret-color: rgb(0, 0, 0); float: none; display: inline !important;">appD f (Dynamic rep (x :: a)) = withTypeable rep (toDyn (f x))</span><br class="" style="caret-color: rgb(0, 0, 0);"><span class="" style="caret-color: rgb(0, 0, 0); float: none; display: inline !important;">```</span><br class="" style="caret-color: rgb(0, 0, 0);"><br class="" style="caret-color: rgb(0, 0, 0);"><span class="" style="caret-color: rgb(0, 0, 0); float: none; display: inline !important;">Cheers,</span><br class="" style="caret-color: rgb(0, 0, 0);"><br class="" style="caret-color: rgb(0, 0, 0);"><span class="" style="caret-color: rgb(0, 0, 0); float: none; display: inline !important;">Jaro</span><br class="" style="caret-color: rgb(0, 0, 0);"></div></blockquote><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 2021-04-12, at 21:06, Vladislav Zavialov <<a href="mailto:vladislav@serokell.io" class="">vladislav@serokell.io</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Would something like this work for you?<br class=""><br class="">  import Type.Reflection<br class="">  import Data.Dynamic<br class=""><br class="">  apD :: Typeable f => (forall a. a -> f a) -> Dynamic -> Dynamic<br class="">  apD f (Dynamic t a) = withTypeable t $ Dynamic typeRep (f a)<br class=""><br class="">- Vlad<br class=""><br class=""><blockquote type="cite" class="">On 12 Apr 2021, at 14:34, YueCompl via ghc-devs <<a href="mailto:ghc-devs@haskell.org" class="">ghc-devs@haskell.org</a>> wrote:<br class=""><br class="">Dear Cafe and GHC devs,<br class=""><br class=""><br class="">There used to be a "principled way with pattern match on the constructor":<br class=""><br class="">```hs<br class="">data Dynamic where<br class=""> Dynamic :: Typeable a => a -> Dynamic<br class=""><br class="">apD :: Typeable f => (forall a. a -> f a) -> Dynamic -> Dynamic<br class="">apD f (Dynamic a) = Dynamic $ f a<br class="">```<br class="">Source: <a href="https://www.reddit.com/r/haskell/comments/2kdcca/q_how_to_apply_a_polymorphic_function_to_a/" class="">https://www.reddit.com/r/haskell/comments/2kdcca/q_how_to_apply_a_polymorphic_function_to_a/</a><br class=""><br class=""><br class="">But now with GHC 8.8 as in my case, `Dynamic` constructor has changed its signature to: <br class=""><br class="">```hs<br class="">Dynamic :: forall a. TypeRep a -> a -> Dynamic<br class="">```<br class=""><br class="">Which renders the `apD` not working anymore. <br class=""><br class=""><br class="">And it seems missing dependencies now for an older solution Edward KMETT provides:<br class=""><br class="">```hs<br class="">apD :: forall f. Typeable1 f => (forall a. a -> f a) -> Dynamic -> Dynamic<br class="">apD f a = dynApp df a<br class=""> where t = dynTypeRep a<br class="">       df = reify (mkFunTy t (typeOf1 (undefined :: f ()) `mkAppTy` t)) $ <br class="">                 \(_ :: Proxy s) -> toDyn (WithRep f :: WithRep s (() -> f ()))<br class="">```<br class="">Source: <a href="https://stackoverflow.com/questions/10889682/how-to-apply-a-polymorphic-function-to-a-dynamic-value" class="">https://stackoverflow.com/questions/10889682/how-to-apply-a-polymorphic-function-to-a-dynamic-value</a><br class=""><br class=""><br class="">So, how can I do that nowadays?<br class=""><br class="">Thanks,<br class="">Compl<br class=""><br class="">_______________________________________________<br class="">ghc-devs mailing list<br class=""><a href="mailto:ghc-devs@haskell.org" class="">ghc-devs@haskell.org</a><br class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs<br class=""></blockquote><br class=""></div></div></blockquote></div><br class=""></div></body></html>