<div dir="auto"><div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Oct 11, 2019, 11:08 AM Simon Peyton Jones <<a href="mailto:simonpj@microsoft.com">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="#0563C1" vlink="#954F72">
<div class="m_-3683590175518582062WordSection1">
<p class="MsoNormal">David<u></u><u></u></p>
<p class="MsoNormal">I’m deeply puzzled atomicModifyMutVar2#.  I have read <a href="https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0149-atomicModifyMutVar.rst" target="_blank" rel="noreferrer">
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 class="m_-3683590175518582062Code">atomicModifyMutVar2# :: MutVar# s a<u></u><u></u></p>
<p class="m_-3683590175518582062Code">                     -> (a -> (a,b))<u></u><u></u></p>
<p class="m_-3683590175518582062Code">                     -> State# s<u></u><u></u></p>
<p class="m_-3683590175518582062Code">                     -> (# State# s, a, (a, b) #)</p></div></div></blockquote></div></div><div dir="auto"><br></div><div dir="auto">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.</div><div dir="auto"><br></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-GB" link="#0563C1" vlink="#954F72"><div class="m_-3683590175518582062WordSection1"><p class="m_-3683590175518582062Code"><u></u><u></u></p>
<p class="MsoNormal">Nowhere is this explicitly stated, but I believe that the intended semantics of a call<u></u><u></u></p>
<p class="m_-3683590175518582062Code">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 style="margin-top:0cm" type="disc">
<li class="m_-3683590175518582062MsoListParagraph" style="margin-left:0cm">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 class="m_-3683590175518582062MsoListParagraph" style="margin-left:0cm">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 class="m_-3683590175518582062MsoListParagraph" style="margin-left:0cm">The result
<b><span style="font-family:"Courier New"">r</span></b> is t<u></u><u></u></li><li class="m_-3683590175518582062MsoListParagraph" style="margin-left:0cm">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.</p></div></div></blockquote></div></div><div dir="auto">Yes, that sounds right.</div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-GB" link="#0563C1" vlink="#954F72"><div class="m_-3683590175518582062WordSection1"><p class="MsoNormal"><u></u><u></u></p>
<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 class="m_-3683590175518582062Code">atomicModifyMutVar2# :: MutVar# s a<u></u><u></u></p>
<p class="m_-3683590175518582062Code">                     -> (a -> Box a)<u></u><u></u></p>
<p class="m_-3683590175518582062Code">                     -> State# s<u></u><u></u></p>
<p class="m_-3683590175518582062Code">                     -> (# 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 class="m_-3683590175518582062Code">    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?</p></div></div></blockquote></div></div><div dir="auto">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</div><div dir="auto"><br></div><div dir="auto"><p style="font-family:sans-serif">atomicModifyMutVar2# mv $ \a -></p><p style="font-family:sans-serif">  let foo = expensive_computation a</p><p style="font-family:sans-serif">  in ([3,foo], foo)</p></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-GB" link="#0563C1" vlink="#954F72"><div class="m_-3683590175518582062WordSection1"><p class="MsoNormal"><u></u><u></u></p>
<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.</p></div></div></blockquote></div></div><div dir="auto">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.</div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-GB" link="#0563C1" vlink="#954F72"><div class="m_-3683590175518582062WordSection1"><p class="MsoNormal"><u></u><u></u></p>
<p class="MsoNormal"><u></u><br></p>
</div>
</div>

</blockquote></div></div></div>