Ganesh Sittampalam
Thu, 19 Feb 2009 14:29:42 -0800
Hi,I have a problem in GHC 6.10 with functions in a class instance calling other functions in the same class instance. It seems that the dictionary is freshly constructed for this call, despite being already available.
The reason I care is that I want to memoise some expensive computations inside the dictionary for each instance. [Obviously I also have to make sure that the dictionary isn't constructed multiple times by external callers, but I can make other arrangements to ensure that.]
To see the problem in action, run main from the attached code. In GHC 6.8 and before, this only executes the trace statement once. In GHC 6.10, the trace statement executes twice, at all optimisation levels.
This seems related to http://hackage.haskell.org/trac/ghc/ticket/2902, but I'm a little unclear on whether it's the same problem or not. Cheers, Ganesh
{-# LANGUAGE ScopedTypeVariables #-}
module RecDict where
import Debug.Trace
class Foo a where
tname :: a -> String
mem1 :: a -> Int
mem2 :: a -> Int
mem2 = let opt = trace ("Ouch! Imagine this is really expensive! Called for " ++ tname (undefined :: a)) (2 * mem1 (undefined :: a))
in \a -> opt
mem3 :: a -> Int
instance Foo () where
tname _ = "()"
mem1 _ = 0
mem3 _ = 0
data T a = T a
instance Foo a => Foo (T a) where
tname ~(T a) = "T (" ++ tname a ++ ")"
mem1 ~(T a) = 1 + mem1 a
mem3 a = 1 + mem2 a
add :: Foo a => a -> Int
add a = mem2 a + mem3 a
main = print $ add (T ())
_______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users