Arrows and GHC rewrite rules
Simon PeytonJones
simonpj at microsoft.com
Fri Mar 30 10:37:53 EDT 2007
Well observed. There are two difficulties here.
First is a persistent bugbear: GHC compiles
foo :: SF (Int,Int) (Int,Int)
foo = first (arr (+1)) >>> first (arr (+2) >>> arr (+3))
thus:
dsf :: Arrow SF
dsf = ...
first_1 = Control.Arrow.first SF dsf
arr_1 = Control.Arrow.arr SF dsf
foo = first_1 (arr_1 (+1)) ...etc...
GHC is trying to get more sharing by sharing the computation for selecting the 'first' method of dsf. But getting this sharing (which is worth little) defeats the rule, since there is no occurrence of the pattern the rule looks for.
You can switch off this (dubious) sharing optimisation with fnomethodsharing.
Would someone care to document this at
http://haskell.org/haskellwiki/GHC/Using_rules
The second is a plain bug: RULES for class methods are not being exported at all. So they aren't getting outside of Control.Arrow at the moment. I'm committing a patch right now.
Simon
 Original Message
 From: glasgowhaskellusersbounces at haskell.org [mailto:glasgowhaskellusersbounces at haskell.org] On
 Behalf Of Eric Cheng
 Sent: 30 March 2007 03:35
 To: glasgowhaskellusers at haskell.org
 Subject: Arrows and GHC rewrite rules

 In GHC's Control.Arrow implementation, there are several rewrite rules
 which exploit some properties of arrows. For example,

 > "compose/arr" forall f g . arr f >>> arr g = arr (f >>> g)
 > "first/arr" forall f . first (arr f) = arr (first f)
 > "second/arr" forall f . second (arr f) = arr (second f)

 ... and so forth.

 Now say I have a simple arrow instance:

 > newtype SF a b = SF ([a] > [b])
 >
 > instance Arrow SF where
 > arr f = SF (map f)
 >
 > first (SF f) = SF g
 > where g l = let (x, y) = unzip l
 > in zip (f x) y
 >
 > (SF f) >>> (SF g) = SF (g . f)

 and some arrow composition:

 > foo :: SF (Int,Int) (Int,Int)
 > foo = first (arr (+1)) >>> first (arr (+2) >>> arr (+3))

 I was expecting that ghc would at least make use of compose/arr or
 even first/arr, but it didn't appear to be the case. I was using
 ghc6.6, and ddumpsimplstats showed a bunch of listspecific rules
 were fired (map, mapFB, fold/build, etc), but I did not see any
 arrowrelated rules being applied.

 Were the rewrite rules subsumed by inlining or betareduction? Am I
 missing something here?

 Thanks,
 Eric
 _______________________________________________
 Glasgowhaskellusers mailing list
 Glasgowhaskellusers at haskell.org
 http://www.haskell.org/mailman/listinfo/glasgowhaskellusers
More information about the Glasgowhaskellusers
mailing list