Fun with GHC's optimiser

Simon Peyton-Jones simonpj@microsoft.com
Tue, 19 Dec 2000 00:46:32 -0800


Manuel

| I have found a way of rephrasing the definition so that it
| is properly optimised by GHC.  However, I think, it should
| be possible to do this automatically and it is maybe not
| unlike the optimisation done by simplCore/LiberateCase.

I agree with your example.  But I think the way to handle
it is this.  Suppose we have

	f x y = ....(f ex (C a b))...

where C is a constructor, and where y is scrutinised by a 
case expression somewhere in f's body.  Then, in this recursive
call to f, we know what y will be, and we could save the case in 
the recursive call.  So make a specialised version of f, thus:

	fs x a b = let y = C a b in (...original body of f...)

Now add a RULE to f, saying

	f x (C a b) ===> fs x a b

This is very much the way type specialisation works.  Now every
call to f that has a (C a b) argument will benefit.  Including
the recursive call to f in fs's RHS, so fs will become the self-recursive
tight loop you want, leaving f as a sort of 'impedence matcher' for
callers elsewhere.

I'll try to find time to implement this.

Simon