<div dir="ltr">sounds lovely! (the current type is certainly strange and not very sound :) )</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Oct 14, 2019 at 6:35 PM Simon Peyton Jones via ghc-devs <<a href="mailto:ghc-devs@haskell.org">ghc-devs@haskell.org</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 lang="EN-GB">
<div class="gmail-m_4238152172691227913WordSection1">
<p class="MsoNormal">OK.   I propose:<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<ul style="margin-top:0cm" type="disc">
<li class="gmail-m_4238152172691227913MsoListParagraph" style="margin-left:0cm">To give atomicModieyMutVarOf# its proper type, with a  pair, as in the proposal.<u></u><u></u></li><li class="gmail-m_4238152172691227913MsoListParagraph" style="margin-left:0cm">To do that I’ll fiddle with genprimopcode, to allow it to parse tuples as well as unboxed tuples; not hard.<u></u><u></u></li><li class="gmail-m_4238152172691227913MsoListParagraph" style="margin-left:0cm">This would disallow all this stuff about “any type that has a first field looking like a”, restricting to pairs alone.  This didn’t form part of the proposal, and was never documented.<u></u><u></u></li><li class="gmail-m_4238152172691227913MsoListParagraph" style="margin-left:0cm">Add a bit more clarity to the documentation, so it’d clear what must be forced.<u></u><u></u></li></ul>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Any objections?<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Simon<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div style="border-top:none;border-right:none;border-bottom:none;border-left:1.5pt solid blue;padding:0cm 0cm 0cm 4pt">
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 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" target="_blank">david.feuer@gmail.com</a>>
<br>
<b>Sent:</b> 12 October 2019 00:00<br>
<b>To:</b> Simon Peyton Jones <<a href="mailto:simonpj@microsoft.com" target="_blank">simonpj@microsoft.com</a>><br>
<b>Cc:</b> ghc-devs <<a href="mailto:ghc-devs@haskell.org" 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>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
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:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
<u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
atomicModifyMutVarQ# :: MutVar# s a -> (q -> a) -> (a -> q)<span style="font-family:Arial,sans-serif">  -> State# s -> (# State# s, a, q #)</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
<u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:12pt;margin-left:0cm">
<span style="font-family:Arial,sans-serif">where there's a special rule that (q -> a) is "obviously" a selector that selects a pointer.</span><u></u><u></u></p>
<div>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
On Fri, Oct 11, 2019, 12:56 PM Simon Peyton Jones <<a href="mailto:simonpj@microsoft.com" target="_blank">simonpj@microsoft.com</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<p class="MsoNormal" style="margin-left:36pt">
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 class="gmail-m_4238152172691227913m-2251622308724748719code">atomicModifyMutVar2# mv $ \a -><u></u><u></u></p>
<p class="gmail-m_4238152172691227913m-2251622308724748719code">  let foo = f a<u></u><u></u></p>
<p class="gmail-m_4238152172691227913m-2251622308724748719code">  in (g foo, foo)<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">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></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Simon<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<div style="border-top:none;border-right:none;border-bottom:none;border-left:1.5pt solid blue;padding:0cm 0cm 0cm 4pt">
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 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" 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" target="_blank">simonpj@microsoft.com</a>><br>
<b>Cc:</b> ghc-devs <<a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a>><br>
<b>Subject:</b> Re: atomicModifyMutVar2</span><u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"> <u></u><u></u></p>
<div>
<div>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:6pt">On Fri, Oct 11, 2019, 11:08 AM Simon Peyton Jones <<a href="mailto:simonpj@microsoft.com" target="_blank">simonpj@microsoft.com</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin:5pt 0cm 5pt 4.8pt">
<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%7C0b9a79c810d7453ce16008d74e9ebfc1%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637064316038353079&sdata=LGyYiZC9b9g3lZpzTdvQgyEwKCaEsIzFDBcacVE2N5M%3D&reserved=0" 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 class="gmail-m_4238152172691227913m-2251622308724748719m-3683590175518582062code">atomicModifyMutVar2# :: MutVar# s a<u></u><u></u></p>
<p class="gmail-m_4238152172691227913m-2251622308724748719m-3683590175518582062code">                     -> (a -> (a,b))<u></u><u></u></p>
<p class="gmail-m_4238152172691227913m-2251622308724748719m-3683590175518582062code">                     -> State# s<u></u><u></u></p>
<p class="gmail-m_4238152172691227913m-2251622308724748719m-3683590175518582062code">                     -> (# 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-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin:5pt 0cm 5pt 4.8pt">
<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 class="gmail-m_4238152172691227913m-2251622308724748719m-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 type="disc">
<li class="gmail-m_4238152172691227913m-2251622308724748719m-3683590175518582062msolistparagraph">
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="gmail-m_4238152172691227913m-2251622308724748719m-3683590175518582062msolistparagraph">
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="gmail-m_4238152172691227913m-2251622308724748719m-3683590175518582062msolistparagraph">
The result <b><span style="font-family:"Courier New"">r</span></b> is t<u></u><u></u></li><li class="gmail-m_4238152172691227913m-2251622308724748719m-3683590175518582062msolistparagraph">
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-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin:5pt 0cm 5pt 4.8pt">
<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 class="gmail-m_4238152172691227913m-2251622308724748719m-3683590175518582062code">atomicModifyMutVar2# :: MutVar# s a<u></u><u></u></p>
<p class="gmail-m_4238152172691227913m-2251622308724748719m-3683590175518582062code">                     -> (a -> Box a)<u></u><u></u></p>
<p class="gmail-m_4238152172691227913m-2251622308724748719m-3683590175518582062code">                     -> State# s<u></u><u></u></p>
<p class="gmail-m_4238152172691227913m-2251622308724748719m-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="gmail-m_4238152172691227913m-2251622308724748719m-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?<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 -></span><u></u><u></u></p>
<p><span style="font-family:Arial,sans-serif">  let foo = expensive_computation a</span><u></u><u></u></p>
<p><span style="font-family:Arial,sans-serif">  in ([3,foo], foo)</span><u></u><u></u></p>
</div>
<div>
<div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin:5pt 0cm 5pt 4.8pt">
<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-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin:5pt 0cm 5pt 4.8pt">
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</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>