<div dir="ltr"><div class="markdown-here-wrapper" style=""><p style="margin:0px 0px 1.2em!important">Hi all,</p>
<p style="margin:0px 0px 1.2em!important">I tried compiling this file:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="hljs language-haskell" 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;white-space:pre;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;overflow-x:auto;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,248)"><span class="hljs-pragma" style="color:rgb(153,153,153);font-weight:bold">{-# LANGUAGE NoImplicitPrelude #-}</span>
<span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">-- | Demonstrate various use of the FFI.</span>
<span class="hljs-module"><span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">module</span> Foreign <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">where</span></span>
<span class="hljs-import"><span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">import</span> Foreign.C</span>
<span class="hljs-foreign"><span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">foreign</span> <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">import</span> <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">ccall</span> <span class="hljs-string" style="color:rgb(221,17,68)">"math.h sin"</span> sin :: <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">CDouble</span> -> <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">CDouble</span></span>
<span class="hljs-title" style="color:rgb(153,0,0);font-weight:bold">it</span> :: <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">CDouble</span>
<span class="hljs-title" style="color:rgb(153,0,0);font-weight:bold">it</span> = sin <span class="hljs-number" style="color:rgb(0,128,128)">1</span>
</code></pre>
<p style="margin:0px 0px 1.2em!important">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!important">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!important">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!important">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!important">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!important">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!important">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!important">Cheers</p>
<p style="margin:0px 0px 1.2em!important">Chris</p>
<div title="MDH:SGkgYWxsLDxicj48ZGl2Pjxicj48L2Rpdj48ZGl2PkkgdHJpZWQgY29tcGlsaW5nIHRoaXMgZmls
ZTo8L2Rpdj48ZGl2Pjxicj48L2Rpdj48ZGl2PmBgYGhhc2tlbGw8L2Rpdj48ZGl2PnstIyBMQU5H
VUFHRSBOb0ltcGxpY2l0UHJlbHVkZSAjLX08YnI+LS0gfCBEZW1vbnN0cmF0ZSB2YXJpb3VzIHVz
ZSBvZiB0aGUgRkZJLjxicj5tb2R1bGUgRm9yZWlnbiB3aGVyZTxicj5pbXBvcnQgRm9yZWlnbi5D
PGJyPmZvcmVpZ24gaW1wb3J0IGNjYWxsICJtYXRoLmggc2luIiBzaW4gOjogQ0RvdWJsZSAtJmd0
OyBDRG91YmxlPGJyPml0IDo6IENEb3VibGU8YnI+aXQgPSBzaW4gMTxicj48L2Rpdj48ZGl2PmBg
YDwvZGl2PjxkaXY+PGJyPjwvZGl2PjxkaXY+QW5kIEkndmUgbm90aWNlZCB0aGF0IHRoZSBhbm5v
dGF0ZWQgdHlwZSBnaXZlbiBmb3IgdGhpcyBmb3JlaWdu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