<div dir="ltr"><div style="font-size:12.8px">I'm writing a book, I'd like to get this nailed down and to get it right. If anyone on here that's familiar with the various ways in which IO/State#/realWorld# work in GHC and you have time to reply, anything at all would be welcome. <span style="font-size:12.8px">Any pointers, links, references, details, anecdotes, or faint memories of GHC bugs will be greatly appreciated! Getting this written up (possibly for addition to Michael Snoyman's wiki article?) would make me, and I imagine others, a lot happier with trying to understand how the different bits and bobs fit together.</span></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">I will be dumping my notes as I don't want to get linked to stuff that can be googled because I've already lost 10-15 hours to just that in the past 3-4 days. Digging it up in the compiler is hard because compiler behavior that influences how IO actions are treated don't necessarily have "IO" or "realWorld" mentioned in the relevant parts of the compiler, optimizations, etc.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">What I'm hoping for is answers on what specifically preserves the listed properties we want from IO in the compiler, prims, or structure of how we write IO actions.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">What we expect IO to do:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">- Disable sharing of results, even when it's not a lambda and is evaluated multiple times by the same name. ie, getCurrentTime :: IO UTCTime   should get evaluated more than once.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">- Not reorder sequential IO actions, such as in a do-block. Called "linearity" below</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">- Not duplicate the effects of IO actions. Effects shouldn't be spuriously duplicated during optimization passes.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">- Effects should not be discarded separately of the value returned by an IO action, merged, or elided.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><span style="font-size:12.8px"># Sharing</span><div style="font-size:12.8px"><br><div><div dir="ltr"><div dir="ltr"><div dir="ltr"></div></div></div></div><div>A friend suggested that perhaps one-shot semantics via the state hack for State# in the IO type is responsible for disabling sharing, I don't believe so, but here are my notes.</div></div><div style="font-size:12.8px"><div><br></div><div><div>>-fno-state-hack</div><div>>Turn off the "state hack" whereby any lambda with a State# token as argument is considered to be single-entry, hence it is considered OK to inline things inside it. This can improve performance of IO and ST monad code, but it runs the risk of reducing sharing.</div></div><div><br></div><div>>A one shot lambda </div><div>>State hack, makes the lambda over State# assume it's one-shot universally by default.</div><div>>one-shot/state hack is an anti-inlining heuristic, suggesting that inlining is costly.<br></div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Also I found this on Trac, does anyone know the answer to this? Is the summary above accurate?</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">>Can the IO state hack be avoided if oneShot is used in the right places in library code, e.g. in IO’s definition of >>=?</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">This seems related how the state token works, for differentiating which IO action is which and how many times an IO action should run, when it should run, etc.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><div>From the prims:</div><div><br></div><div><div>>data State# s</div><div><br></div><div>>State# is the primitive, unlifted type of states. It has one type parameter, thus State# RealWorld, or State# s, where s is a type variable. The only purpose of the type parameter is to keep different state threads separate. It is represented by nothing at all.</div><div><br></div><div>>data RealWorld</div><div><br></div><div>>RealWorld is deeply magical. It is primitive, but it is not unlifted (hence ptrArg). We never manipulate values of type RealWorld; it's only used in the type system, to parameterise State#.</div></div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"># Linearity</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Is this from the nesting of lambdas? It doesn't seem like that's enough based on the various examples using State/State# in GHC Trac bug tickets. The RealWorld token seems to be what's driving this but precisely how that works hasn't been easy to find.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"># Discarding, not inlining effects</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">I believe these are addressed by has_side_effects in the prim ops. I could very well be wrong.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><div>            can_fail     has_side_effects</div><div>Discard        NO            NO</div><div>Float in       YES           YES</div><div>Float out      NO            NO</div><div>Duplicate      YES           NO</div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><div>* Duplication.  You cannot duplicate a has_side_effect primop.  You</div><div>  might wonder how this can occur given the state token threading, but</div><div>  just look at Control.Monad.ST.Lazy.Imp.strictToLazy!  We get</div><div>  something like this</div><div>        p = case readMutVar# s v of</div><div>              (# s', r #) -> (S# s', r)</div><div>        s' = case p of (s', r) -> s'</div><div>        r  = case p of (s', r) -> r</div></div><div style="font-size:12.8px"><br>I believe duplication addresses inlining IO actions more generally but I could be wrong. Here's a note I found regarding elision/merging:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><div>  * Use the compiler flag @-fno-cse@ to prevent common sub-expression</div><div>        elimination being performed on the module, which might combine</div><div>        two side effects that were meant to be separate.  A good example</div><div>        is using multiple global variables (like @test@ in the example below).</div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Any help or pointers for nailing down and documenting this would be greatly appreciated. Also if there's a more detailed explanation of what behavior is expected out of each unsafe function, that would help as well. There are bits and pieces I've been able to aggregate from the GHC trac tickets.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">References used (not exhaustive):</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><div>- Referential Transparency; Haskell Wiki<br></div><div><a href="https://wiki.haskell.org/Referential_transparency" target="_blank">https://wiki.haskell.org/Referential_transparency</a></div><div><br></div><div>- IO Inside; Haskell Wiki</div><div><a href="https://wiki.haskell.org/IO_inside" target="_blank">https://wiki.haskell.org/IO_inside</a></div><div><br></div><div>- Unraveling the mystery of the IO Monad; Edward Z. Yang</div><div><a href="http://blog.ezyang.com/2011/05/unraveling-the-mystery-of-the-io-monad/" target="_blank">http://blog.ezyang.com/2011/05/unraveling-the-mystery-of-the-io-monad/</a></div><div><br></div><div>- Evaluation order and state tokens; Michael Snoyman</div><div><a href="https://wiki.haskell.org/Evaluation_order_and_state_tokens" target="_blank">https://wiki.haskell.org/Evaluation_order_and_state_tokens</a></div><div><br></div><div>- Haskell GHC Illustrated; Takenobu Tani</div><div><br></div><div>- Tackling the Awkward Squad; Simon Peyton Jones</div><div><a href="http://research.microsoft.com/en-us/um/people/simonpj/papers/marktoberdorf/mark.pdf" target="_blank">http://research.microsoft.com/en-us/um/people/simonpj/papers/marktoberdorf/mark.pdf</a></div><div><br></div><div>- Note [IO hack in the demand analyser]; GHC source code</div><div><br></div><div>- Monadic I/O in Haskell 1.3; Andrew D. Gordon and Kevin Hammond</div><div><br></div><div>- Haskell Report 1.2</div><div><a href="http://haskell.cs.yale.edu/wp-content/uploads/2011/01/haskell-report-1.2.pdf" target="_blank">http://haskell.cs.yale.edu/wp-content/uploads/2011/01/haskell-report-1.2.pdf</a></div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Thank you for your time,</div><div style="font-size:12.8px">Chris</div>
</div>