<div dir="ltr"><div>We can formulate SF as a classic Stream that needs an `a` to produce its next element of type `b` like this (SF2 below):</div><div style="margin-left:40px"><br></div><div><div style="margin-left:40px"><span style="font-family:monospace">{-# LANGUAGE BangPatterns #-}<br>{-# LANGUAGE GADTs #-}<br><br>module Lib where<br><br>newtype SF a b = SF { runSF :: a -> (b, SF a b) }<br><br>inc1 :: SF Int Int<br>inc1 = SF $ \a -> let !b = a+1 in (b, inc1)<br><br>data Step s a = Yield !s a<br><br>data SF2 a b where<br> SF2 :: !(a -> s -> Step s b) -> !s -> SF2 a b<br><br>inc2 :: SF2 Int Int<br>inc2 = SF2 go ()<br> where<br> go a _ = let !b = a+1 in Yield () b<br><br>runSF2 :: SF2 a b -> a -> (b, SF2 a b)<br>runSF2 (SF2 f s) a = case f a s of<br> Yield s' b -> (b, (SF2 f s'))</span></div><div style="margin-left:40px"><span style="font-family:monospace"></span><br></div></div><div>Note the absence of recursion in inc2. This resolves the tension around having to specialise for a function argument that is recursive and having to do the unrolling. I bet that similar to stream fusion, we can arrange that only the consumer has to be explicitly recursive. Indeed, I think this will help you inline mapping combinators such as `second`, because it won't be recursive itself anymore.</div><div>Now we "only" have to solve the same problems as with good old stream fusion.</div><div><br></div><div>The tricky case (after realising that we need to add `Skip` to `Step` for `filterSF2`) is when we want to optimise a signal of signals, e.g. something like `concatMapSF2 :: (b -> SF2 a c) -> SF2 a b -> SF2 a c` or some such. And here we are again in #855/#915.</div><div><br></div><div><br></div><div><br></div><div>Also if you need convincing that we can embed any SF into SF2, look at this:</div><div><br></div><div><div style="margin-left:40px"><span style="font-family:monospace">embed :: SF Int Int -> SF2 Int Int<br>embed origSF = SF2 go origSF<br> where<br> go a sf = case runSF sf a of<br> (b, sf') -> Yield sf' b</span><br></div><br></div><div>Please do open a ticket about this, though. It's an interesting data point!</div><div><br></div><div>Cheers,</div><div>Sebastian<br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Am Di., 31. März 2020 um 13:12 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_2658566582636399381WordSection1">
<p class="MsoNormal"><span>Wow – tricky stuff! I would never have thought of trying to optimise that program, but it’s fascinating that you get lots and lots of them from FRP.<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<ul style="margin-top:0cm" type="disc">
<li class="gmail-m_2658566582636399381MsoListParagraph" style="margin-left:0cm"><span>Don’t lose this thread! Make a ticket, or a wiki page. If the former, put the main payload (including Alexis’s examples) into the Descriptions,
not deep in the discussion. <u></u><u></u></span></li><li class="gmail-m_2658566582636399381MsoListParagraph" style="margin-left:0cm"><span>I wonder whether it’d be possible to adjust the FRP library to generate easier-to-optimise code. Probably not, but worth asking.<u></u><u></u></span></li><li class="gmail-m_2658566582636399381MsoListParagraph" style="margin-left:0cm"><span>Alexis’s proposed solution relies on
<u></u><u></u></span></li><ul style="margin-top:0cm" type="circle">
<li class="gmail-m_2658566582636399381MsoListParagraph" style="margin-left:0cm"><span>Specialising on a function argument. Clearly this must be possible, and it’d be very beneficial.<u></u><u></u></span></li><li class="gmail-m_2658566582636399381MsoListParagraph" style="margin-left:0cm"><span>Unrolling one layer of a recursive function. That seems harder: how we know to *<b>stop</b>* unrolling as we successively simplify? One idea:
do one layer of unrolling by hand, perhaps even in FRP source code:<u></u><u></u></span></li></ul>
</ul>
<p class="gmail-m_2658566582636399381Code"><span>add1rec = SF (\a -> let !b = a+1 in (b,add1rec))<u></u><u></u></span></p>
<p class="gmail-m_2658566582636399381Code"><span>add1 = SF (\a -> let !b = a+1 in (b,add1rec))<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-color:currentcolor currentcolor currentcolor blue;border-style:none none none solid;border-width:medium medium medium 1.5pt;padding:0cm 0cm 0cm 4pt">
<div>
<div style="border-color:rgb(225,225,225) currentcolor currentcolor;border-style:solid none none;border-width:1pt medium medium;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> 29 March 2020 15:34<br>
<b>To:</b> Alexis King <<a href="mailto:lexi.lambda@gmail.com" target="_blank">lexi.lambda@gmail.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: Fusing loops by specializing on functions with SpecConstr?<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">
Hi Alexis,<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">
I've been wondering the same things and have worked on it on and off. See my progress in
<a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitlab.haskell.org%2Fghc%2Fghc%2Fissues%2F855%23note_149482&data=02%7C01%7Csimonpj%40microsoft.com%7Cab7afece6b43485f5e5508d7d3ee4cfc%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637210892758857668&sdata=BWptTEUj%2BcKu1cEkYiFtDuBRHKKzl%2BkVxUzV%2FRIje1c%3D&reserved=0" target="_blank">
https://gitlab.haskell.org/ghc/ghc/issues/855#note_149482</a> and <a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitlab.haskell.org%2Fghc%2Fghc%2Fissues%2F915%23note_241520&data=02%7C01%7Csimonpj%40microsoft.com%7Cab7afece6b43485f5e5508d7d3ee4cfc%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637210892758867663&sdata=w5cJDvwz0e1RWq3c%2BrG12McHTt9H%2FkMzRnUlyAS22bM%3D&reserved=0" target="_blank">
https://gitlab.haskell.org/ghc/ghc/issues/915#note_241520</a>.<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">
The big problem with solving the higher-order specialisation problem through SpecConstr (which is what I did in my reports in #855) is indeed that it's hard to<u></u><u></u></p>
<ol type="1" start="1">
<li class="MsoNormal">
Anticipate what the rewritten program looks like without doing a Simplifier pass after each specialisation, so that we can see and exploit new specialisation opportunities. SpecConstr does use the simple Core optimiser but, that often is not enough IIRC (think
of ArgOccs from recursive calls). In particular, it will not do RULE rewrites. Interleaving SpecConstr with the Simplifier, apart from nigh impossible conceptually, is computationally intractable and would quickly drift off into Partial Evaluation swamp.<u></u><u></u></li><li class="MsoNormal">
Make the RULE engine match and rewrite call sites in all call patterns they can apply.<br>
I.e., `f (\x -> Just (x +1))` calls its argument with one argument and scrutinises the resulting Maybe (that's what is described by the argument's `ArgOcc`), so that we want to specialise to a call pattern `f (\x -> Just <some expression using x>)`, giving
rise to the specialisation `$sf ctx`, where `ctx x` describes the `<some expression using x>` part. In an ideal world, we want a (higher-order pattern unification) RULE for `forall f ctx. f (\x -> Just (ctx x)) ==> $sf ctx`. But from what I remember, GHC's
RULE engine works quite different from that and isn't even concerned with finding unifiers (rather than just matching concrete call sites without meta variables against RULEs with meta variables) at all.<u></u><u></u></li></ol>
</div>
<div>
<p class="MsoNormal">Note that matching on specific Ids binding functions is just an approximation using representional equality (on the Id's Unique) rather than some sort of more semantic equality. My latest endeavour into the matter in #915 from December
was using types as the representational entity and type class specialisation. I think I got ultimately blocked on thttps://<a href="https://nam06.safelinks.protection.outlook.com/?url=http%3A%2F%2Fgitlab.haskell.org%2Fghc%2Fghc%2Fissues%2F17548&data=02%7C01%7Csimonpj%40microsoft.com%7Cab7afece6b43485f5e5508d7d3ee4cfc%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637210892758877658&sdata=x6YJBWtNzzX2ad05yr2KoAE7G42A7agIFINws0VI%2BeY%3D&reserved=0" target="_blank">gitlab.haskell.org/ghc/ghc/issues/17548</a>,
but apparently I didn't document the problematic program.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Maybe my failure so far is that I want it to apply and optimise all cases and for more complex stream pipelines, rather than just doing a better best effort job.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Hope that helps. Anyway, I'm also really keen on nailing this! It's one of my high-risk, high-reward research topics. So if you need someone to collaborate/exchange ideas with, I'm happy to help!<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">All the best,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Sebastian<u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">Am So., 29. März 2020 um 10:39 Uhr schrieb Alexis King <<a href="mailto:lexi.lambda@gmail.com" target="_blank">lexi.lambda@gmail.com</a>>:<u></u><u></u></p>
</div>
<blockquote style="border-color:currentcolor currentcolor currentcolor rgb(204,204,204);border-style:none none none solid;border-width:medium medium medium 1pt;padding:0cm 0cm 0cm 6pt;margin-left:4.8pt;margin-right:0cm">
<p class="MsoNormal">Hi all,<br>
<br>
I have recently been toying with FRP, and I’ve noticed that<br>
traditional formulations generate a lot of tiny loops that GHC does<br>
a very poor job optimizing. Here’s a simplified example:<br>
<br>
newtype SF a b = SF { runSF :: a -> (b, SF a b) }<br>
<br>
add1_snd :: SF (String, Int) (String, Int)<br>
add1_snd = second add1 where<br>
add1 = SF $ \a -> let !b = a + 1 in (b, add1)<br>
second f = SF $ \(a, b) -><br>
let !(c, f') = runSF f b<br>
in ((a, c), second f')<br>
<br>
Here, `add1_snd` is defined in terms of two recursive bindings,<br>
`add1` and `second`. Because they’re both recursive, GHC doesn’t<br>
know what to do with them, and the optimized program still has two<br>
separate recursive knots. But this is a missed optimization, as<br>
`add1_snd` is equivalent to the following definition, which fuses<br>
the two loops together and consequently has just one recursive knot:<br>
<br>
add1_snd_fused :: SF (String, Int) (String, Int)<br>
add1_snd_fused = SF $ \(a, b) -><br>
let !c = b + 1<br>
in ((a, c), add1_snd_fused)<br>
<br>
How could GHC get from `add1_snd` to `add1_snd_fused`? In theory,<br>
SpecConstr could do it! Suppose we specialize `second` at the call<br>
pattern `second add1`:<br>
<br>
{-# RULE "second/add1" second add1 = second_add1 #-}<br>
<br>
second_add1 = SF $ \(a, b) -><br>
let !(c, f') = runSF add1 b<br>
in ((a, c), second f')<br>
<br>
This doesn’t immediately look like an improvement, but we’re<br>
actually almost there. If we unroll `add1` once on the RHS of<br>
`second_add1`, the simplifier will get us the rest of the way. We’ll<br>
end up with<br>
<br>
let !b1 = b + 1<br>
!(c, f') = (b1, add1)<br>
in ((a, c), second f')<br>
<br>
and after substituting f' to get `second add1`, the RULE will tie<br>
the knot for us.<br>
<br>
This may look like small potatoes in isolation, but real programs<br>
can generate hundreds of these tiny, tiny loops, and fusing them<br>
together would be a big win. The only problem is SpecConstr doesn’t<br>
currently specialize on functions! The original paper, “Call-pattern<br>
Specialisation for Haskell Programs,” mentions this as a possibility<br>
in Section 6.2, but it points out that actually doing this in<br>
practice would be pretty tricky:<br>
<br>
> Specialising for function arguments is more slippery than for<br>
> constructor arguments. In the example above the argument was a<br>
> simple variable, but what if it was instead a lambda term? [...]<br>
><br>
> The trouble is that lambda abstractions are much more fragile than<br>
> constructor applications, in the sense that simple transformations<br>
> may make two abstractions look different although they have the<br>
> same value.<br>
<br>
Still, the difference this could make in a program of mine is so<br>
large that I am interested in exploring it anyway. I am wondering if<br>
anyone has investigated this possibility any further since the paper<br>
was published, or if anyone knows of other use cases that would<br>
benefit from this capability.<br>
<br>
Thanks,<br>
Alexis<br>
_______________________________________________<br>
ghc-devs mailing list<br>
<a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
<a href="https://nam06.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmail.haskell.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fghc-devs&data=02%7C01%7Csimonpj%40microsoft.com%7Cab7afece6b43485f5e5508d7d3ee4cfc%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637210892758877658&sdata=tTFc4DHgkLgTxAomYoFk7xsNGp8oiOWH8Hd4KcDrqvc%3D&reserved=0" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs</a><u></u><u></u></p>
</blockquote>
</div>
</div>
</div>
</div>
</blockquote></div>