Hi Alexis,
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
- 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.
- Make the RULE engine match and rewrite call sites in all call patterns they can apply.
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.
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://
gitlab.haskell.org/ghc/ghc/issues/17548, but apparently I didn't document the problematic program.
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.
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!
All the best,
Sebastian