<div dir="auto">I think the usual approach for defining these sorts of primitive operations is to use unsafeCoerce.</div><br><div class="gmail_quote"><div dir="ltr">On Wed, May 23, 2018, 7:39 PM Conal Elliott <<a href="mailto:conal@conal.net">conal@conal.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>When programming with GHC's type-level natural numbers and `KnownNat` constraints, how can one construct *evidence* of the result of comparisons to be used in further computations? For instance, we might define a type for augmenting the results of `compare` with evidence:</div><div><br></div><div><font face="monospace, monospace">> data CompareEv u v</font></div><div><font face="monospace, monospace">>   = (u < v) => CompareLT</font></div><div><font face="monospace, monospace">>   | (u ~ v) => CompareEQ</font></div><div><font face="monospace, monospace">>   | (u > v) => CompareGT</font></div><div><br></div><div>Then I'd like to define a comparison operation (to be used with `AllowAmbiguousTypes` and `TypeApplications`, alternatively taking proxy arguments):</div><div><br></div><div><font face="monospace, monospace">> compareEv :: (KnownNat m, KnownNat n) => CompareEv u v</font></div><div><br></div><div>With `compareEv`, we can bring evidence into scope in `case` expressions.</div><div><br></div><div>I don't know how to implement `compareEv`. The following attempt fails to type-check, since `compare` doesn't produce evidence (which is the motivation for `compareEv` over `compare`):</div><div><br></div><div><font face="monospace, monospace">> compareEv = case natVal (Proxy @ u) `compare` natVal (Proxy @ v) of</font></div><div><font face="monospace, monospace">>               LT -> CompareLT</font></div><div><font face="monospace, monospace">>               EQ -> CompareEQ</font></div><div><font face="monospace, monospace">>               GT -> CompareGT</font></div><div><br></div><div>Can `compareEv` be implemented in GHC Haskell? Is there already an implementation of something similar? Any other advice?</div><div><br></div><div>Thanks,  -- Conal</div><div><br></div></div>
_______________________________________________<br>
Glasgow-haskell-users mailing list<br>
<a href="mailto:Glasgow-haskell-users@haskell.org" target="_blank" rel="noreferrer">Glasgow-haskell-users@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users</a><br>
</blockquote></div>