[Template-haskell] Instance declarations

Derek Elkins ddarius86@hotmail.com
Thu, 13 Feb 2003 15:29:13 -0500


My first email seems to have gotten lost, if it meanders it's way here, then 
sorry for the repeat.

Anyways, generating a set of pattern matching functions in an instance 
declaration (maybe in general, though I haven't tested it) causes a 
"conflicting definitions error".  Rewriting as a case expression is one 
work-around.

E.g.

instance (NFData a) => NFData (Tree a) where
    rnf (Leaf a) = rnf a
    rnf (Branch a b) = rnf a `seq` rnf b

doesn't work, but rewriting as

instance (NFData a) => NFData (Tree a) where
    rnf x = case x of
              (Leaf a) -> rnf a
              (Branch a b) -> rnf a `seq` rnf b

does.

I suspect that it may have to do with the generated code being considered at 
a lower level (i.e. the transformation of the above into the below is 
already considered done).  It may just be I'm creating the functions 
incorrectly, so if you can't get the error to happen I can provide the code 
I'm using.

Separately: I've written a variety of fold algebras over data declarations.  
I'm not sure how useful they'll be, but if you're interested email me, and 
I'll email the source.  I haven't tested them, but it's pretty mechanical 
code so I don't suspect any major problems and any should be relatively 
straightforward to fix.  See `Dealing with Large Bananas` for the concept 
(and for documentation, as it has no documentation yet).

_________________________________________________________________
STOP MORE SPAM with the new MSN 8 and get 2 months FREE*  
http://join.msn.com/?page=features/junkmail