Could not deduce ... using functional dependencies with GHC7

Hello, when moving to GHC7 a body of code that I'm not immensely familiar with, I got an error compiling with GHC 7.0.2. Can somebody kindly explain to me what it means and how to get around it? This is in the scion code base so is using some GHC types. I have a class with a functional dependency (I think :-p): class Search id a | a -> id where search :: (SrcSpan -> Bool) -> SrcSpan -> a -> SearchResults id And some instances, notably: instance Search Id Id where search _ _ i = only (FoundId i) And the error occurs on another instance: instance (Search id id) => Search id (Sig id) where search p s (IdSig i) = search p s i ... other cases here, that work The error is: Could not deduce (id ~ Id) from the context (Search id id) bound by the instance declaration at lib\Scion\Inspect\Find.hs:477:10-45 `id' is a rigid type variable bound by the instance declaration at lib\Scion\Inspect\Find.hs:477:18 When using functional dependencies to combine Search Id Id, arising from the dependency `a -> id' in the instance declaration at lib\Scion\Inspect\Find.hs:183:10 Search id Id, arising from a use of `search' at lib\Scion\Inspect\Find.hs:479:32-37 In the expression: search p s i In an equation for `search': search p s (IdSig i) = search p s i If I replace the call to search with the actual implementation of search for Id, it compiles, so at some level the code "makes sense". I much prefer when error messages end with "Possible fix:..."! Thanks! -- JP Moresmau http://jpmoresmau.blogspot.com/

Hi, can you elaborate a bit? What is Id and what is (Sig id) and IdSig in your example? Can you reproduce an example that you believe should compile but doesn't? thanks d- -----Original Message----- From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of JP Moresmau Sent: 16 March 2011 21:46 To: Haskell Cafe Subject: [Haskell-cafe] Could not deduce ... using functional dependencies with GHC7 Hello, when moving to GHC7 a body of code that I'm not immensely familiar with, I got an error compiling with GHC 7.0.2. Can somebody kindly explain to me what it means and how to get around it? This is in the scion code base so is using some GHC types. I have a class with a functional dependency (I think :-p): class Search id a | a -> id where search :: (SrcSpan -> Bool) -> SrcSpan -> a -> SearchResults id And some instances, notably: instance Search Id Id where search _ _ i = only (FoundId i) And the error occurs on another instance: instance (Search id id) => Search id (Sig id) where search p s (IdSig i) = search p s i ... other cases here, that work The error is: Could not deduce (id ~ Id) from the context (Search id id) bound by the instance declaration at lib\Scion\Inspect\Find.hs:477:10-45 `id' is a rigid type variable bound by the instance declaration at lib\Scion\Inspect\Find.hs:477:18 When using functional dependencies to combine Search Id Id, arising from the dependency `a -> id' in the instance declaration at lib\Scion\Inspect\Find.hs:183:10 Search id Id, arising from a use of `search' at lib\Scion\Inspect\Find.hs:479:32-37 In the expression: search p s i In an equation for `search': search p s (IdSig i) = search p s i If I replace the call to search with the actual implementation of search for Id, it compiles, so at some level the code "makes sense". I much prefer when error messages end with "Possible fix:..."! Thanks! -- JP Moresmau http://jpmoresmau.blogspot.com/ _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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.
Thanks,
JP
On Fri, Mar 18, 2011 at 12:56 PM, Dimitrios Vytiniotis
Hi, can you elaborate a bit? What is Id and what is (Sig id) and IdSig in your example? Can you reproduce an example that you believe should compile but doesn't?
thanks d-
-----Original Message----- From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of JP Moresmau Sent: 16 March 2011 21:46 To: Haskell Cafe Subject: [Haskell-cafe] Could not deduce ... using functional dependencies with GHC7
Hello, when moving to GHC7 a body of code that I'm not immensely familiar with, I got an error compiling with GHC 7.0.2. Can somebody kindly explain to me what it means and how to get around it? This is in the scion code base so is using some GHC types.
I have a class with a functional dependency (I think :-p): class Search id a | a -> id where search :: (SrcSpan -> Bool) -> SrcSpan -> a -> SearchResults id
And some instances, notably: instance Search Id Id where search _ _ i = only (FoundId i)
And the error occurs on another instance: instance (Search id id) => Search id (Sig id) where search p s (IdSig i) = search p s i ... other cases here, that work
The error is:
Could not deduce (id ~ Id) from the context (Search id id) bound by the instance declaration at lib\Scion\Inspect\Find.hs:477:10-45 `id' is a rigid type variable bound by the instance declaration at lib\Scion\Inspect\Find.hs:477:18 When using functional dependencies to combine Search Id Id, arising from the dependency `a -> id' in the instance declaration at lib\Scion\Inspect\Find.hs:183:10 Search id Id, arising from a use of `search' at lib\Scion\Inspect\Find.hs:479:32-37 In the expression: search p s i In an equation for `search': search p s (IdSig i) = search p s i
If I replace the call to search with the actual implementation of search for Id, it compiles, so at some level the code "makes sense". I much prefer when error messages end with "Possible fix:..."!
Thanks!
-- JP Moresmau http://jpmoresmau.blogspot.com/
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- JP Moresmau http://jpmoresmau.blogspot.com/

On Fri, Mar 18, 2011 at 13:35, 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.
This doesn't even work in GHC 6.12.3. The search call in the second case, IdSig, restricts the result to Result Id, which is less polymorphic than Result id. Either of the following are valid: instance Search Id (Sig Id) where search (SigGen g) = search g search (IdSig i) = search i instance (Search id id) => Search id (Sig id) where search (SigGen g) = search g Sean

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).

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
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/

On Friday 18 March 2011 14:40:40, JP Moresmau wrote:
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)
Not sure what GHC does with that, but at least in spirit that violates the FunDep of class Search id a | a -> id where ... Even if it works now, it may well not work in the future. I'd go for a more stable solution respecting the intent of FunDeps (i.e. there should only be one type t with an instance Search t Id).
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

Hi, that's a bug in GHC---it erroneously accepts polymorphic instances which violate the FD of a class. -Iavor On Fri, Mar 18, 2011 at 7:08 AM, Daniel Fischer < daniel.is.fischer@googlemail.com> wrote:
On Friday 18 March 2011 14:40:40, JP Moresmau wrote:
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)
Not sure what GHC does with that, but at least in spirit that violates the FunDep of
class Search id a | a -> id where ...
Even if it works now, it may well not work in the future. I'd go for a more stable solution respecting the intent of FunDeps (i.e. there should only be one type t with an instance Search t Id).
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
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (5)
-
Daniel Fischer
-
Dimitrios Vytiniotis
-
Iavor Diatchki
-
JP Moresmau
-
Sean Leather