<div dir="ltr"><div>Hi,</div><div><br></div><div>I've also observed that in the final lowered STG the State# is always passed to effectful primops and FFI calls, but the returning new State# is removed from the result type. The State# has VoidRep representation in in cmm, so no register gets allocated for it and eventually the State# function argument is compiled to nothing in the machine code. i.e. the compilation steps for the code above is:<br><pre style="font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;white-space:pre-wrap;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);display:block;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,248) none repeat scroll 0% 0%"><span><span style="color:rgb(51,51,51);font-weight:bold">foreign</span> <span style="color:rgb(51,51,51);font-weight:bold">import</span> <span style="color:rgb(51,51,51);font-weight:bold">ccall</span> <span style="color:rgb(221,17,68)">"math.h sin"</span> sin :: <span style="color:rgb(68,85,136);font-weight:bold">CDouble</span> -> <span style="color:rgb(68,85,136);font-weight:bold">CDouble<br></span></span></code></pre></div><div><ol><li> Initial STG type is: <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">CDouble -> State# RealWorld -> (# State# RealWorld, CDouble #)</code></li><li><div> Lowered STG type is: <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">CDouble -> State# RealWorld -> (# CDouble #)</code></div></li><li><div> FFI C function should be: <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">double sin(double x);</code></div></li></ol></div><div>Regards,</div><div>Csaba<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Oct 28, 2019 at 10:59 AM Christopher Done <<a href="mailto:chrisdone@gmail.com">chrisdone@gmail.com</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 dir="ltr"><div><p style="margin:0px 0px 1.2em">Hi all,</p>
<p style="margin:0px 0px 1.2em">I tried compiling this file:</p>
<pre style="font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;white-space:pre-wrap;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);display:block;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,248) none repeat scroll 0% 0%"><span style="color:rgb(153,153,153);font-weight:bold">{-# LANGUAGE NoImplicitPrelude #-}</span>
<span style="color:rgb(153,153,136);font-style:italic">-- | Demonstrate various use of the FFI.</span>
<span><span style="color:rgb(51,51,51);font-weight:bold">module</span> Foreign <span style="color:rgb(51,51,51);font-weight:bold">where</span></span>
<span><span style="color:rgb(51,51,51);font-weight:bold">import</span> Foreign.C</span>
<span><span style="color:rgb(51,51,51);font-weight:bold">foreign</span> <span style="color:rgb(51,51,51);font-weight:bold">import</span> <span style="color:rgb(51,51,51);font-weight:bold">ccall</span> <span style="color:rgb(221,17,68)">"math.h sin"</span> sin :: <span style="color:rgb(68,85,136);font-weight:bold">CDouble</span> -> <span style="color:rgb(68,85,136);font-weight:bold">CDouble</span></span>
<span style="color:rgb(153,0,0);font-weight:bold">it</span> :: <span style="color:rgb(68,85,136);font-weight:bold">CDouble</span>
<span style="color:rgb(153,0,0);font-weight:bold">it</span> = sin <span style="color:rgb(0,128,128)">1</span>
</code></pre>
<p style="margin:0px 0px 1.2em">And I’ve noticed that the annotated type given for this foreign op in Core, is <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">(# State# RealWorld, CDouble #)</code>, whereas I would have expected e.g. <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">CDouble</code>. </p>
<p style="margin:0px 0px 1.2em">Meanwhile, the foreign op call is passed a <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">RealWorld</code> argument.</p>
<p style="margin:0px 0px 1.2em">Additionally, code that consumes the result of this foreign call expects a <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">(# CDouble #)</code> as a return value.</p>
<p style="margin:0px 0px 1.2em">So there are some assumptions I put in my interpreter to test this FFI call out:</p>
<ol style="margin:1.2em 0px;padding-left:2em">
<li style="margin:0.5em 0px">Despite claiming to return the real world in a tuple, it actually should just return an unboxed tuple of the value.</li>
<li style="margin:0.5em 0px">It should ignore the RealWorld argument entirely.</li>
</ol>
<p style="margin:0px 0px 1.2em">I assume, if I were to lift this function into returning <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">IO</code>, that I should indeed return the <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">RealWorld</code> argument given. So the lesson is:</p>
<p style="margin:0px 0px 1.2em">All FFI functions accept a RealWorld, and may return a 2-tuple of <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">State# RealWorld</code> <em>if</em> it’s impure, else it’ll return a 1-tuple of the value. Correct?</p>
<p style="margin:0px 0px 1.2em">Can someone confirm that my observations are right? Also, if so, is there somewhere I can read more about this?</p>
<p style="margin:0px 0px 1.2em">Cheers</p>
<p style="margin:0px 0px 1.2em">Chris</p>
<div title="MDH:SGkgYWxsLDxicj48ZGl2Pjxicj48L2Rpdj48ZGl2PkkgdHJpZWQgY29tcGlsaW5nIHRoaXMgZmls
ZTo8L2Rpdj48ZGl2Pjxicj48L2Rpdj48ZGl2PmBgYGhhc2tlbGw8L2Rpdj48ZGl2PnstIyBMQU5H
VUFHRSBOb0ltcGxpY2l0UHJlbHVkZSAjLX08YnI+LS0gfCBEZW1vbnN0cmF0ZSB2YXJpb3VzIHVz
ZSBvZiB0aGUgRkZJLjxicj5tb2R1bGUgRm9yZWlnbiB3aGVyZTxicj5pbXBvcnQgRm9yZWlnbi5D
PGJyPmZvcmVpZ24gaW1wb3J0IGNjYWxsICJtYXRoLmggc2luIiBzaW4gOjogQ0RvdWJsZSAtJmd0
OyBDRG91YmxlPGJyPml0IDo6IENEb3VibGU8YnI+aXQgPSBzaW4gMTxicj48L2Rpdj48ZGl2PmBg
YDwvZGl2PjxkaXY+PGJyPjwvZGl2PjxkaXY+QW5kIEkndmUgbm90aWNlZCB0aGF0IHRoZSBhbm5v
dGF0ZWQgdHlwZSBnaXZlbiBmb3IgdGhpcyBmb3JlaWduIG9wIGluIENvcmUsIGlzIGAoIyBTdGF0
ZSMgUmVhbFdvcmxkLCBDRG91YmxlJm5ic3A7IylgLCB3aGVyZWFzIEkgd291bGQgaGF2ZSBleHBl
Y3RlZCBlLmcuIGBDRG91YmxlYC4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ByZXR1cm4g
YSAyLXR1cGxlIG9mIGBTdGF0ZSMgUmVhbFdvcmxkYCBfaWZfIGl0J3MgaW1wdXJlLCBlbHNlIGl0
J2xsIHJldHVybiBhIDEtdHVwbGUgb2YgdGhlIHZhbHVlLiBDb3JyZWN0PzwvZGl2PjxkaXY+PGJy
PjwvZGl2PjxkaXY+Q2FuIHNvbWVvbmUgY29uZmlybSB0aGF0IG15IG9ic2VydmF0aW9ucyBhcmUg
cmlnaHQ/IEFsc28sIGlmIHNvLCBpcyB0aGVyZSBzb21ld2hlcmUgSSBjYW4gcmVhZCBtb3JlIGFi
b3V0IHRoaXM/PC9kaXY+PGRpdj48YnI+PC9kaXY+PGRpdj5DaGVlcnM8L2Rpdj48ZGl2Pjxicj48
L2Rpdj48ZGl2PkNocmlzPC9kaXY+" style="height:0px;width:0px;max-height:0px;max-width:0px;overflow:hidden;font-size:0em;padding:0px;margin:0px"></div></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>