<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="">Hi devs,<div class=""><br class=""></div><div class="">In a perfect world, Coercible would relate any two types with the same representation. But the world is not perfect! So we fall well short of this goal.</div><div class=""><br class=""></div><div class="">Specifically, Coercible allows conversions between a newtype and its representation, and additionally between datatypes where the roles work out. Section 2 of the JFP paper (<a href="https://repository.brynmawr.edu/cgi/viewcontent.cgi?referer=&httpsredir=1&article=1010&context=compsci_pubs" class="">https://repository.brynmawr.edu/cgi/viewcontent.cgi?referer=&httpsredir=1&article=1010&context=compsci_pubs</a>) is an accessible description of how this works.</div><div class=""><br class=""></div><div class="">In the specific cases of Int and Word: there is no guarantee (that I know of) that the tags on the sole constructors of these types are the same. So we don't know that they have the same representation. Even if we did know that the tags are the same, there is no guarantee that Int# and Word# have the same representation. I *think* GHC is general enough that it could cope on a system that used, say, different registers to store signed and unsigned machine integers.</div><div class=""><br class=""></div><div class="">More generally, it might be nice if we could coerce between</div><div class=""><br class=""></div><div class="">> data Booly = Bad | Good</div><div class=""><br class=""></div><div class="">and Bool, but we can't. It would additionally be good to coerce between A and B, where:</div><div class=""><br class=""></div><div class="">> data A = A1 | A2 | A3</div><div class="">> data B = B1 | B2 !C</div><div class="">> data C = C1 | C2</div><div class=""><br class=""></div><div class="">Both A and B have precisely 3 possibilities. But we don't do that yet either. Maybe someday.</div><div class=""><br class=""></div><div class="">In contrast to Brandon, I don't know of anyone working on this. That's not to say that it's no good to work on it, of course!</div><div class=""><br class=""></div><div class="">Richard</div><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Oct 5, 2019, at 5:51 PM, Brandon Allbery <<a href="mailto:allbery.b@gmail.com" class="">allbery.b@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">I think there's some work going on to expose the representations, which would enable some ability to coerce. But possibly not this much, as they're separate RuntimeReps so you don't combine signed and unsigned numbers inadvertently; currently that's a little magical inside ghc iirc, with the RuntimeRep the only way to distinguish at all and that vanishing post-typechecking.</div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Oct 5, 2019 at 12:30 PM Michal Terepeta <<a href="mailto:michal.terepeta@gmail.com" class="">michal.terepeta@gmail.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class=""><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Adding <a class="gmail_plusreply" id="gmail-m_3768077059678054180plusReplyChip-0" href="mailto:ghc-devs@haskell.org" target="_blank">+ghc-devs</a> to continue the thread<br class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Hi Sandy,</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Thanks for the answer! Do you think there is some fundamental reason for this? Or just a matter of implementing this in GHC? It seems to me that this should work just fine as long as the runtime representation is the same.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">And a related question--is it safe to `unsafeCoerce` an `Int` to a `Word`? The only reason for why this could be problematic that comes to my mind is that there could be an assumption that different `data`s do not alias each other (although `newtype`s can due to `Coercible` functionality). But I'm not sure this is ever used by GHC? Are there any other reasons why this could be problematic?</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Thanks!</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">- Michal</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br class=""></div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Oct 5, 2019 at 5:27 PM Sandy Maguire <<a href="mailto:sandy@sandymaguire.me" target="_blank" class="">sandy@sandymaguire.me</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class=""><div class=""><div class="">Hi Michal,<br class=""><br class=""></div>Datas aren't coercible, only newtypes. This is why you can't coerce Ints and Words, and why Foo and Bar don't work.<br class=""><br class=""></div>Sandy<br class=""></div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Oct 5, 2019 at 4:17 PM Michal Terepeta <<a href="mailto:michal.terepeta@gmail.com" target="_blank" class="">michal.terepeta@gmail.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class=""><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Hi,</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">I've started looking into using `Data.Coerce` (and the `Coercible` type-class) for a personal project and was wondering why coercing between `Int` and `Word` is not allowed? I don't see any fundamental reason why this shouldn't work...</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Perhaps, it's just a matter of GHC's implementation details leaking out? IIRC internally GHC has separate `RuntimeRep`/`PrimRep` for a `Word#` and for an `Int#`. If that's the case, would it make sense to unify these? Their actual runtime representation should be the same and I'd expect most (all?) of their differences should be attached to `PrimOp`s.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">And that leads me to another question--what exactly goes wrong here:</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">```</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">data Foo = Foo Int#<br class="">data Bar = Bar Int#<br class=""><br class="">test :: Bar<br class="">test = coerce (Foo 42#)<br class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">```</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Which fails with: "<span style="font-family:Arial,Helvetica,sans-serif" class="">Couldn't match representation of type ‘Foo’ with that of ‘Bar’</span><span style="font-family:Arial,Helvetica,sans-serif" class=""> arising from a use of ‘coerce’"</span></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><span style="font-family:Arial,Helvetica,sans-serif" class=""><br class=""></span></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><span style="font-family:Arial,Helvetica,sans-serif" class="">Perhaps I'm just misunderstanding exactly how `Coercible` works?</span></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><span style="font-family:Arial,Helvetica,sans-serif" class=""><br class=""></span></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Thanks in advance!</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">- Michal</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">PS. The ability to coerce through things like lists is amazing :)</div></div>
_______________________________________________<br class="">
ghc-devs mailing list<br class="">
<a href="mailto:ghc-devs@haskell.org" target="_blank" class="">ghc-devs@haskell.org</a><br class="">
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" rel="noreferrer" target="_blank" class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs</a><br class="">
</blockquote></div><br clear="all" class=""><br class="">-- <br class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class="">I'm currently travelling the world, sleeping on people's couches and doing full-time collaboration on Haskell projects. If this seems interesting to you, please consider signing up as a host! <a href="https://isovector.github.io/erdos/" target="_blank" class="">https://isovector.github.io/erdos/</a><br class=""></div></div></div></div></div>
</blockquote></div>
_______________________________________________<br class="">
ghc-devs mailing list<br class="">
<a href="mailto:ghc-devs@haskell.org" target="_blank" class="">ghc-devs@haskell.org</a><br class="">
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" rel="noreferrer" target="_blank" class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs</a><br class="">
</blockquote></div><br clear="all" class=""><div class=""><br class=""></div>-- <br class=""><div dir="ltr" class="gmail_signature"><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class="">brandon s allbery kf8nh</div><div class=""><a href="mailto:allbery.b@gmail.com" target="_blank" class="">allbery.b@gmail.com</a></div></div></div></div></div>
_______________________________________________<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=""></div></blockquote></div><br class=""></div></body></html>