<div dir="ltr">that additional representation invariant requirement is why i ultimately chose not to include the user space version of davids work in primitive <div><br></div><div><a href="https://github.com/haskell/primitive/pull/194">https://github.com/haskell/primitive/pull/194</a><br></div><div><br></div><div>theres some really clever ideas, but i couldnt tease apart a natural example of where i'd want to use it</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Oct 11, 2019 at 12:56 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_-6914723076349748819WordSection1">
<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_-6914723076349748819Code">atomicModifyMutVar2# mv $ \a -><u></u><u></u></p>
<p class="gmail-m_-6914723076349748819Code">  let foo = f a<u></u><u></u></p>
<p class="gmail-m_-6914723076349748819Code">  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-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<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:6pt;margin-left:0cm">
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-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" 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_-6914723076349748819m-3683590175518582062code">atomicModifyMutVar2# :: MutVar# s a<u></u><u></u></p>
<p class="gmail-m_-6914723076349748819m-3683590175518582062code">                     -> (a -> (a,b))<u></u><u></u></p>
<p class="gmail-m_-6914723076349748819m-3683590175518582062code">                     -> State# s<u></u><u></u></p>
<p class="gmail-m_-6914723076349748819m-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-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 class="gmail-m_-6914723076349748819m-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_-6914723076349748819m-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_-6914723076349748819m-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_-6914723076349748819m-3683590175518582062msolistparagraph">
The result <b><span style="font-family:"Courier New"">r</span></b> is t<u></u><u></u></li><li class="gmail-m_-6914723076349748819m-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-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 class="gmail-m_-6914723076349748819m-3683590175518582062code">atomicModifyMutVar2# :: MutVar# s a<u></u><u></u></p>
<p class="gmail-m_-6914723076349748819m-3683590175518582062code">                     -> (a -> Box a)<u></u><u></u></p>
<p class="gmail-m_-6914723076349748819m-3683590175518582062code">                     -> State# s<u></u><u></u></p>
<p class="gmail-m_-6914723076349748819m-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_-6914723076349748819m-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 -><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-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>
<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-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>

_______________________________________________<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>