[Haskell-cafe] Could not deduce ... using functional dependencies with GHC7
JP Moresmau
jpmoresmau at gmail.com
Fri Mar 18 14:40:40 CET 2011
Thanks to you all, I think I understand better.
instance Search Id Id where
search _ _ i = only (FoundId i)
Is too restrictive on the first type, so declaring instead:
instance Search id Id where
search _ _ i = only (FoundId i)
Fixed the issue!! Now the initial "id" is not Id and everybody is
happy (and the code still seems to work as intended)
thanks again
JP
On Fri, Mar 18, 2011 at 2:17 PM, Daniel Fischer
<daniel.is.fischer at googlemail.com> wrote:
> 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).
>
--
JP Moresmau
http://jpmoresmau.blogspot.com/
More information about the Haskell-Cafe
mailing list