[Haskell-cafe] Could not deduce ... using functional dependencies with GHC7

Daniel Fischer daniel.is.fischer at googlemail.com
Fri Mar 18 14:17:32 CET 2011


On Friday 18 March 2011 13:35:22, JP Moresmau wrote:
> These are GHC types, but here is a self-contained example:
> {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies,
> FlexibleInstances #-}
> 
> data Id=Id String
> 
> data Result id =ResultId Id
>     | ResultGen id
> 
> data Sig id=IdSig Id
>     | SigGen id
> 
> class Search id a | a -> id where
>   search :: a -> Result id
> 
> instance Search Id Id where
>   search i = ResultId i
> 
> instance (Search id id) => Search id (Sig id) where
>   search (SigGen g)   = search g
>   search (IdSig i)    = search i
> 
> The last line fails. I don't understand why this doesn't compile.

In (IdSig i), i has type Id, hence

search i :: Result Id

but you want something of type `Result id'. Fortunately it's easy to 
transform, since search i is a ResultId, so


instance (Search id id) => Search id (Sig id) where
  search (SigGen g)   = search g
  search (IdSig i)    =  case search i of
                            ResultId y -> ResultId y
                            _ -> error "foo"

compiles (and probably does what you want).



More information about the Haskell-Cafe mailing list