<div dir="auto">Actually, maybe we can do better! We don't inherently need the function to be a selector. But to make it more general, we'll need to take some care to make sure to produce good code when it *is* a selector.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Oct 11, 2019, 6:59 PM David Feuer <<a href="mailto:david.feuer@gmail.com">david.feuer@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>I don't remember what documentation, if any, it has. You're right that taking advantage of it is potentially risky. Here's what I think we really want:</div><div dir="auto"><br></div><div dir="auto">atomicModifyMutVarQ# :: MutVar# s a -> (q -> a) -> (a -> q)<span style="font-family:sans-serif"> </span><font face="sans-serif"> -> State# s</font><span style="font-family:sans-serif"> -> (# State# s, a, q #)</span></div><div dir="auto"><font face="sans-serif"><br></font></div><div dir="auto"><font face="sans-serif">where there's a special rule that (q -> a) is "obviously" a selector that selects a pointer.<br></font><br><div class="gmail_quote" dir="auto"><div dir="ltr" class="gmail_attr">On Fri, Oct 11, 2019, 12:56 PM Simon Peyton Jones <<a href="mailto:simonpj@microsoft.com" target="_blank" rel="noreferrer">simonpj@microsoft.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="EN-GB" link="blue" vlink="purple">
<div>
<p class="MsoNormal" style="margin-left:36.0pt">The result doesn't have to be a pair. It can be a tuple of any size at all. Indeed, it can even be an arbitrary record type whose first pointer field has the appropriate type.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I think that is 100.0% undocumented, in the code, or in the proposal. Are you sure this is a settled consensus among the interested parties?<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Adopting it would impose new invariants on the representation of values in GHC that I am deeply reluctant to impose. I would much much prefer to stick with the pair that is (somewhat) documented.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">About pair vs Unit, yes, I can see (just) your point about why a pair might be useful. Here’s a better example:<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Suppose mv :: MutVar# Int<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p>atomicModifyMutVar2# mv $ \a -><u></u><u></u></p>
<p> let foo = f a<u></u><u></u></p>
<p> in (g foo, foo)<u></u><u></u></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>Now, if f is expensive,
<i>and g is not invertible</i>, then sharing foo might be useful. It’s hard to think of a credible example, though. Regardless, we should document it.<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>Simon<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #e1e1e1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> David Feuer <<a href="mailto:david.feuer@gmail.com" rel="noreferrer noreferrer" target="_blank">david.feuer@gmail.com</a>>
<br>
<b>Sent:</b> 11 October 2019 17:03<br>
<b>To:</b> Simon Peyton Jones <<a href="mailto:simonpj@microsoft.com" rel="noreferrer noreferrer" target="_blank">simonpj@microsoft.com</a>><br>
<b>Cc:</b> ghc-devs <<a href="mailto:ghc-devs@haskell.org" rel="noreferrer noreferrer" target="_blank">ghc-devs@haskell.org</a>><br>
<b>Subject:</b> Re: atomicModifyMutVar2<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6.0pt;margin-left:0cm">
On Fri, Oct 11, 2019, 11:08 AM Simon Peyton Jones <<a href="mailto:simonpj@microsoft.com" rel="noreferrer noreferrer" target="_blank">simonpj@microsoft.com</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<p class="MsoNormal">David<u></u><u></u></p>
<p class="MsoNormal">I’m deeply puzzled atomicModifyMutVar2#. I have read
<a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fghc-proposals%2Fghc-proposals%2Fblob%2Fmaster%2Fproposals%2F0149-atomicModifyMutVar.rst&data=02%7C01%7Csimonpj%40microsoft.com%7C10950014fe9d46a6682508d74e649537%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637064066214164213&sdata=2phccb9r%2Fcg4xs7beKByn%2F1Ivxz7kvN5xkejhOpDfg8%3D&reserved=0" rel="noreferrer noreferrer" target="_blank">
the proposal</a>, and the comments in primops.txt.pp (reproduced below).<u></u><u></u></p>
<h1>Question 1<u></u><u></u></h1>
<p class="MsoNormal">I think the “real” type of atomicModifyMutVar2 is
<u></u><u></u></p>
<p>atomicModifyMutVar2# :: MutVar# s a<u></u><u></u></p>
<p> -> (a -> (a,b))<u></u><u></u></p>
<p> -> State# s<u></u><u></u></p>
<p> -> (# State# s, a, (a, b) #)<u></u><u></u></p>
</div>
</div>
</blockquote>
</div>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Close, but not quite. The result doesn't have to be a pair. It can be a tuple of any size at all. Indeed, it can even be an arbitrary record type whose first pointer field has the appropriate type.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<div>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<p class="MsoNormal">Nowhere is this explicitly stated, but I believe that the intended semantics of a call<u></u><u></u></p>
<p>case (atomicModifyMutVar2# mv f s) of (# s’, x, r #) -> blah<u></u><u></u></p>
<p class="MsoNormal">Then, suppose the old value of the MutVar was ‘<b><span style="font-family:"Courier New"">old’</span></b><u></u><u></u></p>
<ul type="disc">
<li>
The primop builds a thunk <b><span style="font-family:"Courier New"">t </span></b>=
<b><span style="font-family:"Courier New"">f old</span></b><u></u><u></u></li><li>
The new value of the mutable variable is <b><span style="font-family:"Courier New"">(fst t)</span></b><u></u><u></u></li><li>
The result <b><span style="font-family:"Courier New"">r</span></b> is t<u></u><u></u></li><li>
The result <b><span style="font-family:"Courier New"">x</span></b> is <b><span style="font-family:"Courier New"">old</span></b><u></u><u></u></li></ul>
<p class="MsoNormal">Question: is that correct? We should state it explicitly.<u></u><u></u></p>
</div>
</div>
</blockquote>
</div>
</div>
<div>
<p class="MsoNormal">Yes, that sounds right.<u></u><u></u></p>
</div>
<div>
<div>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<h1>Question 2<u></u><u></u></h1>
<p class="MsoNormal">Next question: Why does f have to return a pair? So far as I can tell, it’s only so that a client can force it. The ‘b’ part never seems to play a useful role. So we could
equally well have had<u></u><u></u></p>
<p>atomicModifyMutVar2# :: MutVar# s a<u></u><u></u></p>
<p> -> (a -> Box a)<u></u><u></u></p>
<p> -> State# s<u></u><u></u></p>
<p> -> (# State# s, a, Unit a #)<u></u><u></u></p>
<p class="MsoNormal">where Unit is defined in Data.Tuple<u></u><u></u></p>
<p> data Unit a = Unit a<u></u><u></u></p>
<p class="MsoNormal">Now you can force the result of (f old), just as with a pair. But the ‘b’ would no longer complicate matters.
<u></u><u></u></p>
<p class="MsoNormal">Question: is the ‘b’ in the pair significant? Or could we use Unit?<u></u><u></u></p>
</div>
</div>
</blockquote>
</div>
</div>
<div>
<p class="MsoNormal">Yes, it's somewhat significant. You actually can use Unit with the new primop (it's a tuple of arity 1), so that option is free. But using a pair gets you a bit more: you can build a thunk that's *shared* between the value installed in
the MutVar and the one returned to the outside. Consider<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p><span style="font-family:"Arial",sans-serif">atomicModifyMutVar2# mv $ \a -><u></u><u></u></span></p>
<p><span style="font-family:"Arial",sans-serif"> let foo = expensive_computation a<u></u><u></u></span></p>
<p><span style="font-family:"Arial",sans-serif"> in ([3,foo], foo)<u></u><u></u></span></p>
</div>
<div>
<div>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<h1>Question 3<u></u><u></u></h1>
<p class="MsoNormal">In the comments below you say "but we don't know about pairs here”. Are you sure? What stops you importing Data.Tuple into GHC.Prim? This fancy footwork is one more complication,
if it could be avoided.<u></u><u></u></p>
</div>
</div>
</blockquote>
</div>
</div>
<div>
<p class="MsoNormal">That whole regime came before my time, but since we win a bit by *not* fixing it, o wouldn't jump on it too quick.<u></u><u></u></p>
</div>
<div>
<div>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote></div></div></div>
</blockquote></div>