<div dir="ltr"><div>Hi Simon,</div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><ol start="1" type="1"><li class="MsoNormal">Judging from SimplCore, we probably want to `splitUniqSupply` after each iteration/transformation, either through a call to `splitUniqSupply` or `getUniqueSupplyM`. Is that right?</li></ol><span style="font-size:12pt">I don’t understand the question.   If you use the same supply twice, you’ll get (precisely) the same uniques.  That may or may not be ok</span></blockquote><div><br></div><div>I guess this was wrt. threading UniqSupply through each transformation vs. splitting it before a transformation. We want to split or to thread, otherwise we possibly re-use some Uniques, because it's a regular purely functional data structure, as you noted. Each transformation will do its own splitting/taking after that, so my question was probably bogus to begin with.</div><div><br></div><div>Thanks, that cleared up a lot of things for me!<br></div><p class="MsoNormal"><span style="font-size:12pt"><br></span></p><div class="gmail_quote"><div dir="ltr">Am Mo., 23. Juli 2018 um 14:21 Uhr schrieb Simon Peyton Jones <<a href="mailto:simonpj@microsoft.com">simonpj@microsoft.com</a>>:<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_-4438942829762318944WordSection1">
<p class="MsoNormal">Some quick responses<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">1. <b>Splitting</b><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:36pt">What's the need for splitting anyway?<u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt">Just so you can use uniques in a tree-like way, without threading the supply around.  No more than that. 
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt">This is not needed everywhere.  For example, the Simplifier threads it thus:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt"><u></u> <u></u></span></p>
<p class="gmail-m_-4438942829762318944Code">newtype SimplM result<u></u><u></u></p>
<p class="gmail-m_-4438942829762318944Code">  =  SM  { unSM :: SimplTopEnv  -- Envt that does not change much<u></u><u></u></p>
<p class="gmail-m_-4438942829762318944Code">                -> UniqSupply   -- We thread the unique supply because<u></u><u></u></p>
<p class="gmail-m_-4438942829762318944Code">                                -- constantly splitting it is rather expensive<u></u><u></u></p>
<p class="gmail-m_-4438942829762318944Code">                -> SimplCount<u></u><u></u></p>
<p class="gmail-m_-4438942829762318944Code">                -> IO (result, UniqSupply, SimplCount)}<u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:12pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt">I suspect that (now that SimplM is in IO anyway) we could use an IORef instead, and maybe speed up the compiler.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt">But perhaps not all uses are so simple to change.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt">2. <b>The</b> <b>tree</b><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt">The crucial thing is that there /is/ a data structure – a tree, that is the unique supply. So if you have<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt">     f u s = ….(splitUniqueSupply us)…..(splitUniqueSupply us)….<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt">you’ll get the same trees in the two calls.  The supply is just a purely-functional tree. 
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt">So, for example <u></u><u></u></span></p>
<ul type="disc">
<li class="MsoNormal">
The `unsafeInterleaveIO` makes it so that `genSym` is actually forced before any of the recursive calls to `mk_split` force their `genSym`, regardless of evaluation order<u></u><u></u></li></ul>
<p class="MsoNormal"><span style="font-size:12pt">I don’t think this is important, except perhaps to avoid creating a thunk.<u></u><u></u></span></p>
<ul type="disc">
<li class="MsoNormal">
This guarentees a certain partial order on produced uniques: Any parent `UniqSupply`'s `Unique` is calculated by a call to compiler/cbits/genSym.c#genSym() before any `Unique`s of its offsprings are]<u></u><u></u></li><li class="MsoNormal">
The order of `Unique`s on different off-springs of the same `UniqSupply` is determined by evaluation order as a result of `unsafeInterleaveIO`, much the same as when we create two different `UniqSupply`s by calls to `mkSplitUniqSupply`<u></u><u></u></li><li class="MsoNormal">
So, `unfoldr (Just . takeUniqFromSupply) us) !! n` is always deterministic and strictly monotone, in the sense that even forcing the expression for n=2 before n=1 will have a lower `Unique` for n=1 than for n=2.<u></u><u></u></li></ul>
<p class="MsoNormal"><span style="font-size:12pt">I don’t think any of these points are important or relied on.  A different impl could behave differently.<u></u><u></u></span></p>
<ol start="1" type="1">
<li class="MsoNormal">
`takeUniqSupply` returns as 'tail' its first off-spring, whereas `uniqsFromSupply` always recurses into its second off-spring. By my intuition above, this shouldn't really make much of a difference, so what is the motivation for that?<u></u><u></u></li></ol>
<p class="MsoNormal"><span style="font-size:12pt">I think this is unimportant. I.e. it should be fine to change it.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt"><u></u> <u></u></span></p>
<ol start="1" type="1">
<li class="MsoNormal">
Judging from SimplCore, we probably want to `splitUniqSupply` after each iteration/transformation, either through a call to `splitUniqSupply` or `getUniqueSupplyM`. Is that right?<u></u><u></u></li></ol>
<p class="MsoNormal"><span style="font-size:12pt">I don’t understand the question.   If you use the same supply twice, you’ll get (precisely) the same uniques.  That may or may not be ok<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt">SImon<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt"><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"> ghc-devs <<a href="mailto:ghc-devs-bounces@haskell.org" target="_blank">ghc-devs-bounces@haskell.org</a>>
<b>On Behalf Of </b>Sebastian Graf<br>
<b>Sent:</b> 23 July 2018 12:06<br>
<b>To:</b> ghc-devs <<a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a>><br>
<b>Subject:</b> Understanding UniqSupply<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal" style="margin-right:0cm;margin-bottom:6pt;margin-left:0cm">
Hi all,<u></u><u></u></p>
<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">
I'm trying to understand when it is necessary to `splitUniqSupply`, or even to create my own supply with `mkSplitUniqSupply`.<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">
First, my understanding of how `mkSplitUniqSupply` (<a href="https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fhackage.haskell.org%2Fpackage%2Fghc-8.4.1%2Fdocs%2Fsrc%2FUniqSupply.html%23mkSplitUniqSupply&data=02%7C01%7Csimonpj%40microsoft.com%7C58b620e0f148448bbe0608d5f08c485e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636679407654961407&sdata=cA6N%2FGzMVYKD0fAVkbG%2BC%2F%2FuXuhenR95CwhHQAZrUQ4%3D&reserved=0" target="_blank">https://hackage.haskell.org/package/ghc-8.4.1/docs/src/UniqSupply.html#mkSplitUniqSupply</a>)
 works is as follows:<u></u><u></u></p>
</div>
<div>
<ul type="disc">
<li class="MsoNormal">
The `unsafeInterleaveIO` makes it so that `genSym` is actually forced before any of the recursive calls to `mk_split` force their `genSym`, regardless of evaluation order<u></u><u></u></li><li class="MsoNormal">
This guarentees a certain partial order on produced uniques: Any parent `UniqSupply`'s `Unique` is calculated by a call to compiler/cbits/genSym.c#genSym() before any `Unique`s of its offsprings are<u></u><u></u></li><li class="MsoNormal">
The order of `Unique`s on different off-springs of the same `UniqSupply` is determined by evaluation order as a result of `unsafeInterleaveIO`, much the same as when we create two different `UniqSupply`s by calls to `mkSplitUniqSupply`<u></u><u></u></li><li class="MsoNormal">
So, `unfoldr (Just . takeUniqFromSupply) us) !! n` is always deterministic and strictly monotone, in the sense that even forcing the expression for n=2 before n=1 will have a lower `Unique` for n=1 than for n=2.<u></u><u></u></li><li class="MsoNormal">
This is of course all an implementation detail<u></u><u></u></li></ul>
<div>
<p class="MsoNormal">These are the questions that bother me:<u></u><u></u></p>
</div>
</div>
<div>
<ol start="1" type="1">
<li class="MsoNormal">
`takeUniqSupply` returns as 'tail' its first off-spring, whereas `uniqsFromSupply` always recurses into its second off-spring. By my intuition above, this shouldn't really make much of a difference, so what is the motivation for that?<u></u><u></u></li><li class="MsoNormal">
The docs state that the character tag/domain/prefix in the call to `mkSplitUniqSupply` should be unique to guarantee actual uniqueness of produced `Unique`s. Judging from the implementation of `genSym`, which is unaware of the specific domain to draw the next
 unique from, this is an unnecessarily strong condition?! Also there are multiple places in the code base spread over module boundaries even (e.g. CorePrep, SimplCore) that call `mkSplitUniqSupply` with the same character anyway. Maybe there should at least
 be some clarifying comment on why this isn't a problem?<u></u><u></u></li><li class="MsoNormal">
Judging from SimplCore, we probably want to `splitUniqSupply` after each iteration/transformation, either through a call to `splitUniqSupply` or `getUniqueSupplyM`. Is that right?<u></u><u></u></li><li class="MsoNormal">
What's the need for splitting anyway? I suspect it's a trick to avoid state threading that would be necessary if we just had `type UniqSupply = [Unique]`. Would that really be a bad thing, considering we mostly work in `UniqSM` anyway? Is there anything else
 to it?<u></u><u></u></li></ol>
<div>
<p class="MsoNormal">Happy to hear from you!<u></u><u></u></p>
</div>
</div>
<div>
<p class="MsoNormal">Cheers<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Sebastian<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>

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