Desugaring instances

Simon Peyton-Jones simonpj at microsoft.com
Thu Mar 5 08:42:33 EST 2009


| Might this be another option? Desugar the instance as:
|
| {{{
| Rec {
|   dCList :: C a -> C [a]
|   dCList d_a = letrec {
|                  d_as = DC opfl opgl
|                  opfl = opF_aux d_a
|                  opgl = $dmopG d_as
|                } in d_as
|
|   opF_aux :: C a -> a -> Int
|   opF_aux d_a = let { d_as = dCList d_a } in
|                 \xs -> case xs of
|                      []     -> 0
|                      (y:ys) -> opF d_a y + opF d_as ys
| }
| }}}
|
| I.e. make sure the let binding the list dictionary is outside the
| lambda. You still have that recursive calls via the dictionary become
| direct recursion:

I think that suffers from the same lack-of-memoisation as (A).

After all, each recursive call is to (opF_aux d_a), and each such call builds a new d_as.

Simon

|
| {{{
| opF_aux d_a = let { d_as = dCList d_a } in
|                 \xs -> case xs of
|                      []     -> 0
|                      (y:ys) -> opF d_a y + opF d_as ys
| =
| opF_aux d_a = letrec {
|                      d_as = DC opfl opgl
|                  opfl = opF_aux d_a
|                  opgl = $dmopG d_as
|                } in \xs -> case xs of
|                      []     -> 0
|                      (y:ys) -> opF d_a y + opF d_as ys
| =
| opF_aux d_a = \xs -> case xs of
|                      []     -> 0
|                      (y:ys) -> opF d_a y + opF_aux d_a ys
| }}}
|
| To prevent reconstruction of the dictionary in general, I think that
| CSE needs to be able to do:
|
| f x = ... f x ...
| =>
| f x = letrec n = ... n ...
|         in n
|
| We've discussed this transformation before, in the context of arity
| improvement. I'm not sure it's a good idea to rely on the
| infrequently-run CSE transformation to get your dictionaries right,
| however. This scheme also fails if the user turns on -fdicts-cheap
| (but then they probably deserve it :-).
|
| Cheers,
| Max



More information about the Cvs-ghc mailing list