<div dir="auto">Having more examples and docs in the corresponding module might be the best first step toward that.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Apr 13, 2021, 10:00 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:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space">A followup wish I have:<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:#d4bfa0">    </span><span style="color:#cc524b">case</span><span style="color:#d4bfa0"> io </span><span style="color:#96b946;font-weight:bold">`</span><span style="color:#96b946">eqTypeRep</span><span style="color:#96b946;font-weight:bold">`</span><span style="color:#d4bfa0"> typeRep </span><span style="color:#b3ddf5">@</span><span style="color:#6b95c5">IO</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b">of</span></div><div><span style="color:#d4bfa0">      </span><span style="color:#cca143">Just</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">HRefl</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> </span><span style="color:#cca143">Dynamic</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">TypeRep</span><span style="color:#d4bfa0"> </span><span style="color:#a3d349"><$></span><span style="color:#d4bfa0"> monotypedAct</span></div><div><span style="color:#d4bfa0">      </span><span style="color:#cca143">Nothing</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> naAlt </span><span style="color:#7e6b5f">-- not an IO action</span></div><div><span style="color:#d4bfa0"></span></div></div></div><div>```</div><div><div><br></div><div>The `Just HRefl` part as in above remains hard to understand for me, I had glanced it in doc of the 'Type.Reflection' module earlier, but had no chance to figure out the usage of `eqTypeRep` to be like this, at least on my own. The community is very helpful in this regards, in leading me to it. But may there can be better surface syntax / usage hints that more intuitive, i.e. costing less effort to reach the solution? I anticipate improvements but apparently lack expertise for progress, I tried `Just {}` and it won't compile already...</div><div><br></div><div>I mean, things are already great as far, well, maybe the learning experience can be made even better.</div><div><br></div><div>Best,</div><div>Compl</div><div><br></div><div><br><blockquote type="cite"><div>On 2021-04-13, at 22:07, YueCompl via ghc-devs <<a href="mailto:ghc-devs@haskell.org" target="_blank" rel="noreferrer">ghc-devs@haskell.org</a>> wrote:</div><br><div><div style="word-wrap:break-word;line-break:after-white-space"><div>After struggled this far, I decide that I can neither trivially understand `pattern TypeRep`, nor the `withTypeable` at core. But this is what really amazing with Haskell, GHC and the community here - I can get my job done even without full understanding of what's going on under the hood, so long as the compiler says it's okay! The warning has gone due to unknown reason after I refactored the code a bit, surprisingly but well, I feel safe and comfort to use it now.</div><div><br></div><div>Thanks to Erik, Vlad and Jaro again for your help.</div><div><br></div><div>u/Iceland_jack made a ticket to [add pattern TypeRep to Type.Reflection](<a href="https://gitlab.haskell.org/ghc/ghc/-/issues/19691" target="_blank" rel="noreferrer">https://gitlab.haskell.org/ghc/ghc/-/issues/19691</a>) and appears it's very welcomed. Though I don't expect it get shipped very soon or even could be back ported to GHC 8.8, so I end up with this shim:</div><div><br></div><div>(there `PolyKinds` appears some unusual to be put into my `.cabal` due to its syntax change can break some of my existing code)</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:#9b84da">{-# </span><span style="color:#9b84da;font-weight:bold">LANGUAGE</span><span style="color:#9b84da"> </span><span style="color:#9b84da;text-decoration:underline">PolyKinds</span><span style="color:#9b84da"> #-}</span></div><br><div><span style="color:#cc524b">module</span><span style="color:#73a36c"> Dyn.Shim</span></div><div><span style="color:#73a36c">  </span>(<span style="color:#73a36c"> </span><span style="color:#cc524b">pattern</span><span style="color:#73a36c"> </span><span style="color:#cca143">TypeRep</span>,</div><div><span style="color:#73a36c">    </span><span style="color:#66a89d">dynPerformIO</span>,</div><div><span style="color:#73a36c">    </span><span style="color:#66a89d">dynPerformSTM</span>,</div><div><span style="color:#73a36c">    </span><span style="color:#66a89d">dynContSTM</span>,</div><div><span style="color:#73a36c">  </span>)</div><div><span style="color:#cc524b">where</span></div><br><div><span style="color:#cc524b">import</span><span style="color:#73a36c"> Control.Concurrent.STM </span>(<span style="color:#6b95c5">STM</span>)</div><div><span style="color:#cc524b">import</span><span style="color:#73a36c"> Data.Dynamic </span>(<span style="color:#6b95c5">Dynamic</span><span style="color:#73a36c"> </span>(<span style="color:#cca143">..</span>),<span style="color:#73a36c"> </span><span style="color:#6b95c5">Typeable</span>)</div><div><span style="color:#cc524b">import</span><span style="color:#73a36c"> Type.Reflection</span></div><div><span style="color:#73a36c">  </span>(<span style="color:#73a36c"> </span><span style="color:#6b95c5">TypeRep</span>,</div><div><span style="color:#73a36c">    </span><span style="color:#66a89d">eqTypeRep</span>,</div><div><span style="color:#73a36c">    </span><span style="color:#66a89d">typeRep</span>,</div><div><span style="color:#73a36c">    </span><span style="color:#66a89d">withTypeable</span>,</div><div><span style="color:#73a36c">    </span><span style="color:#cc524b">pattern</span><span style="color:#73a36c"> </span><span style="color:#cca143">App</span>,</div><div><span style="color:#73a36c">    </span><span style="color:#cc524b">type</span><span style="color:#73a36c"> </span>(<span style="color:#6b95c5">:~~:</span>)<span style="color:#73a36c"> </span>(<span style="color:#6b95c5">HRefl</span>),</div><div><span style="color:#73a36c">  </span>)</div><div><span style="color:#cc524b">import</span><span style="color:#73a36c"> Prelude</span></div><br><div><span style="color:#cc524b">data</span><span style="color:#d4bfa0"> </span><span style="color:#6b95c5">TypeableInstance</span><span style="color:#d4bfa0"> </span><span style="color:#82aed8">a</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b">where</span></div><div><span style="color:#d4bfa0">  </span><span style="color:#cca143">TypeableInstance</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">::</span><span style="color:#d4bfa0"> </span><span style="color:#6b95c5">Typeable</span><span style="color:#d4bfa0"> </span><span style="color:#82aed8">a</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">=></span><span style="color:#d4bfa0"> </span><span style="color:#6b95c5">TypeableInstance</span><span style="color:#d4bfa0"> </span><span style="color:#82aed8">a</span></div><br><div><span style="color:#66a89d">typeableInstance</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">::</span><span style="color:#96b946"> </span><span style="color:#6b95c5">TypeRep</span><span style="color:#96b946"> </span><span style="color:#82aed8">a</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#96b946"> </span><span style="color:#6b95c5">TypeableInstance</span><span style="color:#96b946"> </span><span style="color:#82aed8">a</span></div><div><span style="color:#d4bfa0">typeableInstance tr </span><span style="color:#cc524b;font-weight:bold">=</span><span style="color:#d4bfa0"> withTypeable tr </span><span style="color:#cca143">TypeableInstance</span></div><br><div><span style="color:#7e6b5f">{- ORMOLU_DISABLE -}</span></div><br><div><span style="color:#7e6b5f">-- | Shim for the proposed one at:</span></div><div><span style="color:#7e6b5f">--   <a href="https://gitlab.haskell.org/ghc/ghc/-/issues/19691" target="_blank" rel="noreferrer">https://gitlab.haskell.org/ghc/ghc/-/issues/19691</a></span></div><div><span style="color:#cc524b">pattern</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">TypeRep</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">::</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-style:italic">forall</span><span style="color:#d4bfa0"> </span><span style="color:#82aed8">k</span><span style="color:#d4bfa0"> </span>(<span style="color:#82aed8">a</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">::</span><span style="color:#d4bfa0"> </span><span style="color:#82aed8">k</span>)<span style="color:#cc524b;font-weight:bold">.</span><span style="color:#d4bfa0"> </span>()<span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">=></span><span style="color:#d4bfa0"> </span><span style="color:#6b95c5">Typeable</span><span style="color:#d4bfa0"> </span><span style="color:#82aed8">a</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">=></span><span style="color:#d4bfa0"> </span><span style="color:#6b95c5">TypeRep</span><span style="color:#d4bfa0"> </span><span style="color:#82aed8">a</span></div><div><span style="color:#cc524b">pattern</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">TypeRep</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold"><-</span><span style="color:#d4bfa0"> </span>(<span style="color:#d4bfa0">typeableInstance </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> </span><span style="color:#cca143">TypeableInstance</span>)</div><div><span style="color:#d4bfa0">  </span><span style="color:#cc524b">where</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">TypeRep</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">=</span><span style="color:#d4bfa0"> typeRep</span></div><br><div><span style="color:#7e6b5f">{- ORMOLU_ENABLE -}</span></div><br><div><span style="color:#7e6b5f">-- | Perform a polymorphic IO action which is wrapped in a 'Dynamic'</span></div><div><span style="color:#7e6b5f">--</span></div><div><span style="color:#7e6b5f">-- The specified 'naAlt' action will be performed instead, if the wrapped</span></div><div><span style="color:#7e6b5f">-- computation is not applicable, i.e. not really an IO action.</span></div><div><span style="color:#66a89d">dynPerformIO</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">::</span><span style="color:#96b946"> </span><span style="color:#6b95c5">IO</span><span style="color:#96b946"> </span><span style="color:#6b95c5">Dynamic</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#96b946"> </span><span style="color:#6b95c5">Dynamic</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#96b946"> </span><span style="color:#6b95c5">IO</span><span style="color:#96b946"> </span><span style="color:#6b95c5">Dynamic</span></div><div><span style="color:#d4bfa0">dynPerformIO naAlt </span>(<span style="color:#cca143">Dynamic</span><span style="color:#d4bfa0"> trAct monotypedAct</span>)<span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">=</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b">case</span><span style="color:#d4bfa0"> trAct </span><span style="color:#cc524b">of</span></div><div><span style="color:#d4bfa0">  </span><span style="color:#cca143">App</span><span style="color:#d4bfa0"> io </span><span style="color:#cca143">TypeRep</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span></div><div><span style="color:#d4bfa0">    </span><span style="color:#cc524b">case</span><span style="color:#d4bfa0"> io </span><span style="color:#96b946;font-weight:bold">`</span><span style="color:#96b946">eqTypeRep</span><span style="color:#96b946;font-weight:bold">`</span><span style="color:#d4bfa0"> typeRep </span><span style="color:#b3ddf5">@</span><span style="color:#6b95c5">IO</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b">of</span></div><div><span style="color:#d4bfa0">      </span><span style="color:#cca143">Just</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">HRefl</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> </span><span style="color:#cca143">Dynamic</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">TypeRep</span><span style="color:#d4bfa0"> </span><span style="color:#a3d349"><$></span><span style="color:#d4bfa0"> monotypedAct</span></div><div><span style="color:#d4bfa0">      </span><span style="color:#cca143">Nothing</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> naAlt </span><span style="color:#7e6b5f">-- not an IO action</span></div><div><span style="color:#d4bfa0">  _ </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> naAlt </span><span style="color:#7e6b5f">-- not even a poly-type</span></div><br><div><span style="color:#7e6b5f">-- | Perform a polymorphic STM action which is wrapped in a 'Dynamic'</span></div><div><span style="color:#7e6b5f">--</span></div><div><span style="color:#7e6b5f">-- The specified 'naAlt' action will be performed instead, if the wrapped</span></div><div><span style="color:#7e6b5f">-- computation is not applicable, i.e. not really an STM action.</span></div><div><span style="color:#66a89d">dynPerformSTM</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">::</span><span style="color:#96b946"> </span><span style="color:#6b95c5">STM</span><span style="color:#96b946"> </span><span style="color:#6b95c5">Dynamic</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#96b946"> </span><span style="color:#6b95c5">Dynamic</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#96b946"> </span><span style="color:#6b95c5">STM</span><span style="color:#96b946"> </span><span style="color:#6b95c5">Dynamic</span></div><div><span style="color:#d4bfa0">dynPerformSTM naAlt </span>(<span style="color:#cca143">Dynamic</span><span style="color:#d4bfa0"> trAct monotypedAct</span>)<span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">=</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b">case</span><span style="color:#d4bfa0"> trAct </span><span style="color:#cc524b">of</span></div><div><span style="color:#d4bfa0">  </span><span style="color:#cca143">App</span><span style="color:#d4bfa0"> io </span><span style="color:#cca143">TypeRep</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span></div><div><span style="color:#d4bfa0">    </span><span style="color:#cc524b">case</span><span style="color:#d4bfa0"> io </span><span style="color:#96b946;font-weight:bold">`</span><span style="color:#96b946">eqTypeRep</span><span style="color:#96b946;font-weight:bold">`</span><span style="color:#d4bfa0"> typeRep </span><span style="color:#b3ddf5">@</span><span style="color:#6b95c5">STM</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b">of</span></div><div><span style="color:#d4bfa0">      </span><span style="color:#cca143">Just</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">HRefl</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> </span><span style="color:#cca143">Dynamic</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">TypeRep</span><span style="color:#d4bfa0"> </span><span style="color:#a3d349"><$></span><span style="color:#d4bfa0"> monotypedAct</span></div><div><span style="color:#d4bfa0">      </span><span style="color:#cca143">Nothing</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> naAlt </span><span style="color:#7e6b5f">-- not an STM action</span></div><div><span style="color:#d4bfa0">  _ </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> naAlt </span><span style="color:#7e6b5f">-- not even a poly-type</span></div><br><div><span style="color:#7e6b5f">-- | Perform a polymorphic STM action which is wrapped in a 'Dynamic'</span></div><div><span style="color:#7e6b5f">--</span></div><div><span style="color:#7e6b5f">-- The specified 'naAlt' action will be performed instead, if the wrapped</span></div><div><span style="color:#7e6b5f">-- computation is not applicable, i.e. not really an STM action.</span></div><div><span style="color:#66a89d">dynContSTM</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">::</span><span style="color:#96b946"> </span><span style="color:#6b95c5">STM</span><span style="color:#96b946"> </span>()<span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#96b946"> </span><span style="color:#6b95c5">Dynamic</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#96b946"> </span>(<span style="color:#6b95c5">Dynamic</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#96b946"> </span><span style="color:#6b95c5">STM</span><span style="color:#96b946"> </span>())<span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#96b946"> </span><span style="color:#6b95c5">STM</span><span style="color:#96b946"> </span>()</div><div><span style="color:#d4bfa0">dynContSTM naAlt </span>(<span style="color:#cca143">Dynamic</span><span style="color:#d4bfa0"> trAct monotypedAct</span>)<span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">!</span><span style="color:#d4bfa0">exit </span><span style="color:#cc524b;font-weight:bold">=</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b">case</span><span style="color:#d4bfa0"> trAct </span><span style="color:#cc524b">of</span></div><div><span style="color:#d4bfa0">  </span><span style="color:#cca143">App</span><span style="color:#d4bfa0"> io </span><span style="color:#cca143">TypeRep</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span></div><div><span style="color:#d4bfa0">    </span><span style="color:#cc524b">case</span><span style="color:#d4bfa0"> io </span><span style="color:#96b946;font-weight:bold">`</span><span style="color:#96b946">eqTypeRep</span><span style="color:#96b946;font-weight:bold">`</span><span style="color:#d4bfa0"> typeRep </span><span style="color:#b3ddf5">@</span><span style="color:#6b95c5">STM</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b">of</span></div><div><span style="color:#d4bfa0">      </span><span style="color:#cca143">Just</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">HRefl</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> exit </span><span style="color:#a3d349">.</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">Dynamic</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">TypeRep</span><span style="color:#d4bfa0"> </span><span style="color:#a3d349">=<<</span><span style="color:#d4bfa0"> monotypedAct</span></div><div><span style="color:#d4bfa0">      </span><span style="color:#cca143">Nothing</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> naAlt </span><span style="color:#7e6b5f">-- not an STM action</span></div><div><span style="color:#d4bfa0">  _ </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> naAlt </span><span style="color:#7e6b5f">-- not even a poly-type</span></div><br></div></div><div>```</div><div><br></div><div>And my test case being a little more complex than the very first example, might be easier for others to grasp the usage, it runs like this:</div><div><br></div><div>```console</div><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:#5a23f9">λ> </span><span style="font-variant-ligatures:no-common-ligatures">import PoC.DynPoly</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:#5a23f9">λ> </span><span style="font-variant-ligatures:no-common-ligatures">testDynHold </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">First got Nothing</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">Then got Just 3</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(90,35,249);background-color:rgb(22,27,24)"><span style="font-variant-ligatures:no-common-ligatures">λ> </span></div></div><div>```</div><div><br></div><div>With the code:</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:#cc524b">module</span><span style="color:#73a36c"> PoC.DynPoly </span><span style="color:#cc524b">where</span></div><br><div><span style="color:#cc524b">import</span><span style="color:#73a36c"> Control.Monad </span>(<span style="color:#66a89d">void</span>)</div><div><span style="color:#cc524b">import</span><span style="color:#73a36c"> Data.Dynamic </span>(<span style="color:#6b95c5">Dynamic</span><span style="color:#73a36c"> </span>(<span style="color:#cca143">..</span>),<span style="color:#73a36c"> </span><span style="color:#66a89d">fromDynamic</span>,<span style="color:#73a36c"> </span><span style="color:#66a89d">toDyn</span>)</div><div><span style="color:#cc524b">import</span><span style="color:#73a36c"> Data.IORef </span>(<span style="color:#66a89d">modifyIORef'</span>,<span style="color:#73a36c"> </span><span style="color:#66a89d">newIORef</span>,<span style="color:#73a36c"> </span><span style="color:#66a89d">readIORef</span>,<span style="color:#73a36c"> </span><span style="color:#66a89d">writeIORef</span>)</div><div><span style="color:#cc524b">import</span><span style="color:#73a36c"> Dyn.Shim</span></div><div><span style="color:#cc524b">import</span><span style="color:#73a36c"> Type.Reflection </span>(<span style="color:#66a89d">eqTypeRep</span>,<span style="color:#73a36c"> </span><span style="color:#66a89d">typeRep</span>,<span style="color:#73a36c"> </span><span style="color:#cc524b">pattern</span><span style="color:#73a36c"> </span><span style="color:#cca143">App</span>,<span style="color:#73a36c"> </span><span style="color:#cc524b">type</span><span style="color:#73a36c"> </span>(<span style="color:#6b95c5">:~~:</span>)<span style="color:#73a36c"> </span>(<span style="color:#6b95c5">HRefl</span>))</div><div><span style="color:#cc524b">import</span><span style="color:#73a36c"> Prelude</span></div><br><div><span style="color:#66a89d">dynHoldEvent</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">::</span><span style="color:#96b946"> </span><span style="color:#6b95c5">Dynamic</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#96b946"> </span><span style="color:#6b95c5">Dynamic</span></div><div><span style="color:#d4bfa0">dynHoldEvent </span>(<span style="color:#cca143">Dynamic</span><span style="color:#d4bfa0"> trEvs monotypedEvs</span>)<span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">=</span></div><div><span style="color:#d4bfa0">  </span><span style="color:#cc524b">case</span><span style="color:#d4bfa0"> trEvs </span><span style="color:#cc524b">of</span></div><div><span style="color:#d4bfa0">    </span><span style="color:#cca143">App</span><span style="color:#d4bfa0"> trEs </span><span style="color:#cca143">TypeRep</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span></div><div><span style="color:#d4bfa0">      </span><span style="color:#cc524b">case</span><span style="color:#d4bfa0"> trEs </span><span style="color:#96b946;font-weight:bold">`</span><span style="color:#96b946">eqTypeRep</span><span style="color:#96b946;font-weight:bold">`</span><span style="color:#d4bfa0"> typeRep </span><span style="color:#b3ddf5">@</span><span style="color:#6b95c5">EventSink</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b">of</span></div><div><span style="color:#d4bfa0">        </span><span style="color:#cca143">Just</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">HRefl</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> </span><span style="color:#cca143">Dynamic</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">TypeRep</span><span style="color:#d4bfa0"> </span>(<span style="color:#d4bfa0">holdEvent monotypedEvs</span>)</div><div><span style="color:#d4bfa0">        </span><span style="color:#cca143">Nothing</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> error </span><span style="color:#97a2a7">"not an EventSink"</span><span style="color:#d4bfa0"> </span><span style="color:#7e6b5f">-- to be handled properly</span></div><div><span style="color:#d4bfa0">    _ </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> error </span><span style="color:#97a2a7">"even not a poly-type"</span><span style="color:#d4bfa0"> </span><span style="color:#7e6b5f">-- to be handled properly</span></div><div><span style="color:#d4bfa0">  </span><span style="color:#cc524b">where</span></div><div><span style="color:#96b946">    </span><span style="color:#66a89d">holdEvent</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">::</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-style:italic">forall</span><span style="color:#96b946"> </span><span style="color:#82aed8">a</span><span style="color:#cc524b;font-weight:bold">.</span><span style="color:#96b946"> </span><span style="color:#6b95c5">EventSink</span><span style="color:#96b946"> </span><span style="color:#82aed8">a</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#96b946"> </span><span style="color:#6b95c5">IO</span><span style="color:#96b946"> </span>(<span style="color:#6b95c5">TimeSeries</span><span style="color:#96b946"> </span><span style="color:#82aed8">a</span>)</div><div><span style="color:#d4bfa0">    holdEvent </span><span style="color:#cc524b;font-weight:bold">!</span><span style="color:#d4bfa0">evs </span><span style="color:#cc524b;font-weight:bold">=</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b">do</span></div><div><span style="color:#d4bfa0">      </span><span style="color:#cc524b;font-weight:bold">!</span><span style="color:#d4bfa0">holder </span><span style="color:#cc524b;font-weight:bold"><-</span><span style="color:#d4bfa0"> newIORef </span><span style="color:#cca143">Nothing</span></div><div><span style="color:#d4bfa0">      listenEvents evs </span><span style="color:#a3d349">$</span><span style="color:#d4bfa0"> writeIORef holder </span><span style="color:#a3d349">.</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">Just</span></div><div><span style="color:#d4bfa0">      return </span><span style="color:#a3d349">$</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">TimeSeries</span><span style="color:#d4bfa0"> </span><span style="color:#a3d349">$</span><span style="color:#d4bfa0"> readIORef holder</span></div><br><div><span style="color:#cc524b">data</span><span style="color:#d4bfa0"> </span><span style="color:#6b95c5">EventSink</span><span style="color:#d4bfa0"> </span><span style="color:#82aed8">a</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">=</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">EventSink</span></div><div><span style="color:#d4bfa0">  </span><span style="color:#cca143">{</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">listenEvents</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">::</span><span style="color:#d4bfa0"> </span>(<span style="color:#82aed8">a</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> </span><span style="color:#6b95c5">IO</span><span style="color:#d4bfa0"> </span>())<span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> </span><span style="color:#6b95c5">IO</span><span style="color:#d4bfa0"> </span>(),</div><div><span style="color:#d4bfa0">    </span><span style="color:#cca143">publishEvent</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">::</span><span style="color:#d4bfa0"> </span><span style="color:#82aed8">a</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> </span><span style="color:#6b95c5">IO</span><span style="color:#d4bfa0"> </span>()</div><div><span style="color:#d4bfa0">  </span><span style="color:#cca143">}</span></div><br><div><span style="color:#cc524b">newtype</span><span style="color:#d4bfa0"> </span><span style="color:#6b95c5">TimeSeries</span><span style="color:#d4bfa0"> </span><span style="color:#82aed8">a</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">=</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">TimeSeries</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">{readTimeSeries</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">::</span><span style="color:#d4bfa0"> </span><span style="color:#6b95c5">IO</span><span style="color:#d4bfa0"> </span>(<span style="color:#6b95c5">Maybe</span><span style="color:#d4bfa0"> </span><span style="color:#82aed8">a</span>)<span style="color:#cca143">}</span></div><br><div><span style="color:#66a89d">newEventSink</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">::</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-style:italic">forall</span><span style="color:#96b946"> </span><span style="color:#82aed8">a</span><span style="color:#cc524b;font-weight:bold">.</span><span style="color:#96b946"> </span><span style="color:#6b95c5">IO</span><span style="color:#96b946"> </span>(<span style="color:#6b95c5">EventSink</span><span style="color:#96b946"> </span><span style="color:#82aed8">a</span>)</div><div><span style="color:#d4bfa0">newEventSink </span><span style="color:#cc524b;font-weight:bold">=</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b">do</span></div><div><span style="color:#d4bfa0">  </span><span style="color:#cc524b;font-weight:bold">!</span><span style="color:#d4bfa0">listeners </span><span style="color:#cc524b;font-weight:bold"><-</span><span style="color:#d4bfa0"> newIORef </span>[]</div><div><span style="color:#d4bfa0">  </span><span style="color:#cc524b">let</span><span style="color:#d4bfa0"> listen listener </span><span style="color:#cc524b;font-weight:bold">=</span><span style="color:#d4bfa0"> modifyIORef' listeners </span>(<span style="color:#d4bfa0">listener </span><span style="color:#cca143">:</span>)</div><div><span style="color:#d4bfa0">      publish a </span><span style="color:#cc524b;font-weight:bold">=</span><span style="color:#d4bfa0"> readIORef listeners </span><span style="color:#a3d349">>>=</span><span style="color:#d4bfa0"> void </span><span style="color:#a3d349">.</span><span style="color:#d4bfa0"> mapM </span>(<span style="color:#a3d349">$</span><span style="color:#d4bfa0"> a</span>)</div><div><span style="color:#d4bfa0">  return </span><span style="color:#a3d349">$</span><span style="color:#d4bfa0"> </span><span style="color:#cca143">EventSink</span><span style="color:#d4bfa0"> listen publish</span></div><br><div><span style="color:#66a89d">testDynHold</span><span style="color:#96b946"> </span><span style="color:#cc524b;font-weight:bold">::</span><span style="color:#96b946"> </span><span style="color:#6b95c5">IO</span><span style="color:#96b946"> </span>()</div><div><span style="color:#d4bfa0">testDynHold </span><span style="color:#cc524b;font-weight:bold">=</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b">do</span></div><div><span style="color:#d4bfa0">  </span>(<span style="color:#d4bfa0">evs </span><span style="color:#cc524b;font-weight:bold">::</span><span style="color:#d4bfa0"> </span><span style="color:#6b95c5">EventSink</span><span style="color:#d4bfa0"> </span><span style="color:#6b95c5">Int</span>)<span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold"><-</span><span style="color:#d4bfa0"> newEventSink</span></div><div><span style="color:#d4bfa0">  </span><span style="color:#cc524b">let</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">!</span><span style="color:#d4bfa0">dynEvs </span><span style="color:#cc524b;font-weight:bold">=</span><span style="color:#d4bfa0"> toDyn evs</span></div><div><span style="color:#d4bfa0">      </span><span style="color:#cc524b;font-weight:bold">!</span><span style="color:#d4bfa0">dynHold </span><span style="color:#cc524b;font-weight:bold">=</span><span style="color:#d4bfa0"> dynHoldEvent dynEvs</span></div><div><span style="color:#d4bfa0">  </span><span style="color:#cc524b;font-weight:bold">!</span><span style="color:#d4bfa0">dynTs </span><span style="color:#cc524b;font-weight:bold"><-</span><span style="color:#d4bfa0"> dynPerformIO </span>(<span style="color:#d4bfa0">error </span><span style="color:#97a2a7">"bug: dyn type mismatch?"</span>)<span style="color:#d4bfa0"> dynHold</span></div><div><span style="color:#d4bfa0">  </span><span style="color:#cc524b">case</span><span style="color:#d4bfa0"> fromDynamic dynTs </span><span style="color:#cc524b">of</span></div><div><span style="color:#d4bfa0">    </span><span style="color:#cca143">Nothing</span><span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> error </span><span style="color:#97a2a7">"bug: unexpected dyn result type"</span></div><div><span style="color:#d4bfa0">    </span><span style="color:#cca143">Just</span><span style="color:#d4bfa0"> </span>(<span style="color:#d4bfa0">ts </span><span style="color:#cc524b;font-weight:bold">::</span><span style="color:#d4bfa0"> </span><span style="color:#6b95c5">TimeSeries</span><span style="color:#d4bfa0"> </span><span style="color:#6b95c5">Int</span>)<span style="color:#d4bfa0"> </span><span style="color:#cc524b;font-weight:bold">-></span><span style="color:#d4bfa0"> </span><span style="color:#cc524b">do</span></div><div><span style="color:#d4bfa0">      v0 </span><span style="color:#cc524b;font-weight:bold"><-</span><span style="color:#d4bfa0"> readTimeSeries ts</span></div><div><span style="color:#d4bfa0">      putStrLn </span><span style="color:#a3d349">$</span><span style="color:#d4bfa0"> </span><span style="color:#97a2a7">"First got "</span><span style="color:#d4bfa0"> </span><span style="color:#a3d349"><></span><span style="color:#d4bfa0"> show v0</span></div><div><span style="color:#d4bfa0">      publishEvent evs </span><span style="color:#97a2a7">3</span></div><div><span style="color:#d4bfa0">      v1 </span><span style="color:#cc524b;font-weight:bold"><-</span><span style="color:#d4bfa0"> readTimeSeries ts</span></div><div><span style="color:#d4bfa0">      putStrLn </span><span style="color:#a3d349">$</span><span style="color:#d4bfa0"> </span><span style="color:#97a2a7">"Then got "</span><span style="color:#d4bfa0"> </span><span style="color:#a3d349"><></span><span style="color:#d4bfa0"> show v1</span></div></div></div><div>```</div><div><br></div><div>Thanks with best regards,</div><div>Compl</div><div><br><div><br><blockquote type="cite"><div>On 2021-04-13, at 02:50, Erik Hesselink <<a href="mailto:hesselink@gmail.com" target="_blank" rel="noreferrer">hesselink@gmail.com</a>> wrote:</div><br><div><div dir="ltr"><div>That is a lot, I'm not sure I understand that pattern synonym. Using `withTypeable` instead works for me:</div><div><br></div><div>holdEvent :: Dynamic -> Dynamic<br>holdEvent (Dynamic tr x) =<br>  case tr of<br>    App ft at -><br>      case ft `eqTypeRep` typeRep @EventSink of<br>        Just HRefl -> withTypeable at $ toDyn (hcHoldEvent x)<br>        Nothing -> error "to handle"<br>    _ -> error "to handle"</div><div><br></div><div>Cheers,</div><div><br></div><div>Erik<br></div></div></div></blockquote></div><br></div></div>_______________________________________________<br>ghc-devs mailing list<br><a href="mailto:ghc-devs@haskell.org" target="_blank" rel="noreferrer">ghc-devs@haskell.org</a><br><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" target="_blank" rel="noreferrer">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs</a><br></div></blockquote></div><br></div></div>_______________________________________________<br>
ghc-devs mailing list<br>
<a href="mailto:ghc-devs@haskell.org" target="_blank" rel="noreferrer">ghc-devs@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs</a><br>
</blockquote></div>