[Haskell-cafe] Recursive instance dictionaries in GHC

Benja Fallenstein benja.fallenstein at gmail.com
Tue Jun 19 16:17:44 EDT 2007


Hi,

I can't figure out why the following program compiles with this
instance declaration

> instance Size (Maybe [a]) => Size [a] where size x = size (foo x)

but has GHC loop forever with this one:

> instance (Foo a b, Size b) => Size a where size x = size (foo x)

Anybody here know?

Thanks,
- Benja

> {-# OPTIONS_GHC -fglasgow-exts -fallow-overlapping-instances
> -fallow-undecidable-instances #-}
>
> class Foo a b | a -> b where foo :: a -> b
> instance Foo [a] (Maybe [a]) where foo (x:xs) = Just xs; foo [] = Nothing
>
> class Size a where size :: a -> Int
>
> instance Size a => Size (Maybe a) where
>     size (Just x) = 1 + size x; size Nothing = 0
>
> --instance (Foo a b, Size b) => Size a where size x = size (foo x)
> -- OR
> --instance Size (Maybe [a]) => Size [a] where size x = size (foo x)
>
> main = print $ size "foo"


More information about the Haskell-Cafe mailing list