<div dir="ltr"><div>Your function is not `forall a. a -> f a`, as in your initial example, but requires its argument to be an `EventSink`. The value you unwrap from the `Dynamic` is any existential type, not necessarily an `EventSink`. You'll have to compare the TypeReps (with something like `eqTypeRep`[1], or wrap your function in a `Dynamic` and use `dynApply` [2], which does the comparison for you.<br></div><div><br></div><div>Cheers,</div><div><br></div><div>Erik</div><div><br></div><div>[1] <a href="https://hackage.haskell.org/package/base-4.15.0.0/docs/Type-Reflection.html#v:eqTypeRep">https://hackage.haskell.org/package/base-4.15.0.0/docs/Type-Reflection.html#v:eqTypeRep</a></div><div>[2] <a href="https://hackage.haskell.org/package/base-4.15.0.0/docs/Data-Dynamic.html#v:dynApply">https://hackage.haskell.org/package/base-4.15.0.0/docs/Data-Dynamic.html#v:dynApply</a></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, 12 Apr 2021 at 16:15, YueCompl via ghc-devs <<a href="mailto:ghc-devs@haskell.org">ghc-devs@haskell.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><div>Thanks to Vlad and Jaro, your solution of `apD` compiles, I think it should work.</div><div><br></div><div>But unfortunately my real case is a little different / more complex, a MWE appears like this:</div><div><br></div><div>```hs</div><div><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-wrap"><div><span style="color:rgb(102,168,157)">holdEvent</span><span style="color:rgb(150,185,70)"> </span><span style="color:rgb(204,82,75);font-weight:bold">::</span><span style="color:rgb(150,185,70)"> </span><span style="color:rgb(107,149,197)">Dynamic</span><span style="color:rgb(150,185,70)"> </span><span style="color:rgb(204,82,75);font-weight:bold">-></span><span style="color:rgb(150,185,70)"> </span><span style="color:rgb(107,149,197)">Dynamic</span></div><div><span style="color:rgb(212,191,160)">holdEvent </span>(<span style="color:rgb(204,161,67)">Dynamic</span><span style="color:rgb(212,191,160)"> t evs'</span>)<span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,82,75);font-weight:bold">=</span></div><div><span style="color:rgb(212,191,160)"> withTypeable t </span><span style="color:rgb(163,211,73)">$</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,161,67)">Dynamic</span><span style="color:rgb(212,191,160)"> typeRep </span>(<span style="color:rgb(212,191,160)">hcHoldEvent evs'</span>)</div><div><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,82,75)">where</span></div><div><span style="color:rgb(150,185,70)"> </span><span style="color:rgb(102,168,157)">hcHoldEvent</span><span style="color:rgb(150,185,70)"> </span><span style="color:rgb(204,82,75);font-weight:bold">::</span><span style="color:rgb(150,185,70)"> </span><span style="color:rgb(204,82,75);font-style:italic">forall</span><span style="color:rgb(150,185,70)"> </span><span style="color:rgb(130,174,216)">a</span><span style="color:rgb(204,82,75);font-weight:bold">.</span><span style="color:rgb(150,185,70)"> </span><span style="color:rgb(107,149,197)">EventSink</span><span style="color:rgb(150,185,70)"> </span><span style="color:rgb(130,174,216)">a</span><span style="color:rgb(150,185,70)"> </span><span style="color:rgb(204,82,75);font-weight:bold">-></span><span style="color:rgb(150,185,70)"> </span><span style="color:rgb(107,149,197)">IO</span><span style="color:rgb(150,185,70)"> </span>(<span style="color:rgb(107,149,197)">TimeSeries</span><span style="color:rgb(150,185,70)"> </span><span style="color:rgb(130,174,216)">a</span>)</div><div><span style="color:rgb(212,191,160)"> hcHoldEvent </span><span style="color:rgb(204,82,75);font-weight:bold">!</span><span style="color:rgb(212,191,160)">evs </span><span style="color:rgb(204,82,75);font-weight:bold">=</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,82,75)">do</span></div><div><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,82,75);font-weight:bold">!</span><span style="color:rgb(212,191,160)">holder </span><span style="color:rgb(204,82,75);font-weight:bold"><-</span><span style="color:rgb(212,191,160)"> newIORef </span><span style="color:rgb(204,161,67)">Nothing</span></div><div><span style="color:rgb(212,191,160)"> listenEvents evs </span><span style="color:rgb(163,211,73)">$</span><span style="color:rgb(212,191,160)"> writeIORef holder </span><span style="color:rgb(163,211,73)">.</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,161,67)">Just</span></div><div><span style="color:rgb(212,191,160)"> return </span><span style="color:rgb(163,211,73)">$</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,161,67)">TimeSeries</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(163,211,73)">$</span><span style="color:rgb(212,191,160)"> readIORef holder</span></div><br><div><span style="color:rgb(204,82,75)">data</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(107,149,197)">EventSink</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(130,174,216)">a</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,82,75);font-weight:bold">=</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,161,67)">EventSink</span></div><div><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,161,67)">{</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,161,67)">listenEvents</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,82,75);font-weight:bold">::</span><span style="color:rgb(212,191,160)"> </span>(<span style="color:rgb(130,174,216)">a</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,82,75);font-weight:bold">-></span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(107,149,197)">IO</span><span style="color:rgb(212,191,160)"> </span>())<span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,82,75);font-weight:bold">-></span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(107,149,197)">IO</span><span style="color:rgb(212,191,160)"> </span>(),</div><div><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,161,67)">closeStream</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,82,75);font-weight:bold">::</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(107,149,197)">IO</span><span style="color:rgb(212,191,160)"> </span>()</div><div><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,161,67)">}</span></div><br><div><span style="color:rgb(204,82,75)">instance</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(107,149,197)">Functor</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(107,149,197)">EventSink</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,82,75)">where</span></div><div><span style="color:rgb(212,191,160)"> fmap </span><span style="color:rgb(204,82,75);font-weight:bold">=</span><span style="color:rgb(212,191,160)"> undefined</span></div><br><div><span style="color:rgb(204,82,75)">newtype</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(107,149,197)">TimeSeries</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(130,174,216)">a</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,82,75);font-weight:bold">=</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,161,67)">TimeSeries</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,161,67)">{readTimeSeries</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,82,75);font-weight:bold">::</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(107,149,197)">IO</span><span style="color:rgb(212,191,160)"> </span>(<span style="color:rgb(107,149,197)">Maybe</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(130,174,216)">a</span>)<span style="color:rgb(204,161,67)">}</span></div><br><div><span style="color:rgb(204,82,75)">instance</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(107,149,197)">Functor</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(107,149,197)">TimeSeries</span><span style="color:rgb(212,191,160)"> </span><span style="color:rgb(204,82,75)">where</span></div><div><span style="color:rgb(212,191,160)"> fmap </span><span style="color:rgb(204,82,75);font-weight:bold">=</span><span style="color:rgb(212,191,160)"> undefined</span></div><br></div></div><div>```</div><div><br></div><div>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><br></div><div>```log</div><div><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)"><span style="font-variant-ligatures:no-common-ligatures"><b>src/PoC/DynPoly.hs:20:49: </b></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(252,53,34)"><b>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)"><span style="font-variant-ligatures:no-common-ligatures"> • 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)"><span style="font-variant-ligatures:no-common-ligatures"> ‘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)"><span style="font-variant-ligatures:no-common-ligatures"> 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)"><span style="font-variant-ligatures:no-common-ligatures"> 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)"><span style="font-variant-ligatures:no-common-ligatures"> 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)"><span style="font-variant-ligatures:no-common-ligatures"> 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)"><span style="font-variant-ligatures:no-common-ligatures"> 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)"><span style="font-variant-ligatures:no-common-ligatures"> • 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)"><span style="font-variant-ligatures:no-common-ligatures"> 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)"><span style="font-variant-ligatures:no-common-ligatures"> 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)"><span style="font-variant-ligatures:no-common-ligatures"> ‘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)"><span style="font-variant-ligatures:no-common-ligatures"> • 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)"><span style="font-variant-ligatures:no-common-ligatures"> 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)"><span style="font-variant-ligatures:no-common-ligatures"> 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)"><span style="font-variant-ligatures:no-common-ligatures"> (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)"><span style="font-variant-ligatures:no-common-ligatures"><b> |</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)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(207,125,255)"><b>20 |</b></span><span style="font-variant-ligatures:no-common-ligatures"> withTypeable t $ Dynamic typeRep (hcHoldEvent </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(252,53,34)"><b>evs'</b></span><span style="font-variant-ligatures:no-common-ligatures">)</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)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(207,125,255)"><b> |</b></span><span style="font-variant-ligatures:no-common-ligatures"><b> ^^^^</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"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div></div><div>```</div><div><br></div><div>Thanks with best regards,</div><div>Compl</div><div><br></div><div><br><blockquote type="cite"><div>On 2021-04-12, at 22:04, Jaro Reinders <<a href="mailto:jaro.reinders@gmail.com" target="_blank">jaro.reinders@gmail.com</a>> wrote:</div><br><div><span style="float:none;display:inline">I have no experience in this area, but this compiles:</span><br><br><span style="float:none;display:inline">```</span><br><span style="float:none;display:inline">{-# LANGUAGE RankNTypes, ScopedTypeVariables #-}</span><br><span style="float:none;display:inline">import Type.Reflection</span><br><span style="float:none;display:inline">import Data.Dynamic</span><br><br><span style="float:none;display:inline">appD :: forall f. Typeable f => (forall a. a -> f a) -> Dynamic -> Dynamic</span><br><span style="float:none;display:inline">appD f (Dynamic rep (x :: a)) = withTypeable rep (toDyn (f x))</span><br><span style="float:none;display:inline">```</span><br><br><span style="float:none;display:inline">Cheers,</span><br><br><span style="float:none;display:inline">Jaro</span><br></div></blockquote><br><div><br><blockquote type="cite"><div>On 2021-04-12, at 21:06, Vladislav Zavialov <<a href="mailto:vladislav@serokell.io" target="_blank">vladislav@serokell.io</a>> wrote:</div><br><div><div>Would something like this work for you?<br><br> import Type.Reflection<br> import Data.Dynamic<br><br> apD :: Typeable f => (forall a. a -> f a) -> Dynamic -> Dynamic<br> apD f (Dynamic t a) = withTypeable t $ Dynamic typeRep (f a)<br><br>- Vlad<br><br><blockquote type="cite">On 12 Apr 2021, at 14:34, YueCompl via ghc-devs <<a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a>> wrote:<br><br>Dear Cafe and GHC devs,<br><br><br>There used to be a "principled way with pattern match on the constructor":<br><br>```hs<br>data Dynamic where<br> Dynamic :: Typeable a => a -> Dynamic<br><br>apD :: Typeable f => (forall a. a -> f a) -> Dynamic -> Dynamic<br>apD f (Dynamic a) = Dynamic $ f a<br>```<br>Source: <a href="https://www.reddit.com/r/haskell/comments/2kdcca/q_how_to_apply_a_polymorphic_function_to_a/" target="_blank">https://www.reddit.com/r/haskell/comments/2kdcca/q_how_to_apply_a_polymorphic_function_to_a/</a><br><br><br>But now with GHC 8.8 as in my case, `Dynamic` constructor has changed its signature to: <br><br>```hs<br>Dynamic :: forall a. TypeRep a -> a -> Dynamic<br>```<br><br>Which renders the `apD` not working anymore. <br><br><br>And it seems missing dependencies now for an older solution Edward KMETT provides:<br><br>```hs<br>apD :: forall f. Typeable1 f => (forall a. a -> f a) -> Dynamic -> Dynamic<br>apD f a = dynApp df a<br> where t = dynTypeRep a<br> df = reify (mkFunTy t (typeOf1 (undefined :: f ()) `mkAppTy` t)) $ <br> \(_ :: Proxy s) -> toDyn (WithRep f :: WithRep s (() -> f ()))<br>```<br>Source: <a href="https://stackoverflow.com/questions/10889682/how-to-apply-a-polymorphic-function-to-a-dynamic-value" target="_blank">https://stackoverflow.com/questions/10889682/how-to-apply-a-polymorphic-function-to-a-dynamic-value</a><br><br><br>So, how can I do that nowadays?<br><br>Thanks,<br>Compl<br><br>_______________________________________________<br>ghc-devs mailing list<br><a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs</a><br></blockquote><br></div></div></blockquote></div><br></div></div>_______________________________________________<br>
ghc-devs mailing list<br>
<a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs</a><br>
</blockquote></div>