
I'm looking at GHC's overlapping instances docs here: http://web.mit.edu/ghc/www/users_guide/type-extensions.html#instance-decls and I've ran into the incoherent instances problem. Basically, I have a "catch all" instance that handles all types in a generic manner using SYB introspection, and then I have type specific instances that specialize behavior for certain types. This works with overlapping instances extension but whenever I take advantage of this functionality from polymorphic functions I run into the incoherent instances. If I enable incoherent instances GHC always picks the general case which seems like the wrong thing to do. What I want it to do is delay comitting to an instance until it's processing a specific invocation of a polymorphic function. All the information is available at compile time but I found no way to do this. Is there a way to get around this problem? Thanks, - Slava.

Hello Vyacheslav, Wednesday, December 27, 2006, 5:29:37 PM, you wrote:
If I enable incoherent instances GHC always picks the general case which seems like the wrong thing to do. What I want it to do is delay comitting to an instance until it's processing a specific invocation of a polymorphic function. All the information is available at compile time but I found no way to do this.
are you seen http://haskell.org/haskellwiki/OOP_vs_type_classes ? it contains example where compiler choose general instance just because function calling polymorhic code don't get full dictionary of type. smth like this: class F a where f :: a -> Int instance (Num a) => F a ... instance F Int ... g :: (Num a) => a -> a g x = f x main = print (g (1::Int)) -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Ah, so the moment something is passed through a polymorphic function
its type information is lost... This seems like a bug in the
specification/implementation, no? This is most certainly not the
desired behavior. It seems like the compiler has all the information
it needs but still can't select the right instance.
Are there ways to get around this problem?
On 12/27/06, Bulat Ziganshin
Hello Vyacheslav,
Wednesday, December 27, 2006, 5:29:37 PM, you wrote:
If I enable incoherent instances GHC always picks the general case which seems like the wrong thing to do. What I want it to do is delay comitting to an instance until it's processing a specific invocation of a polymorphic function. All the information is available at compile time but I found no way to do this.
are you seen http://haskell.org/haskellwiki/OOP_vs_type_classes ? it contains example where compiler choose general instance just because function calling polymorhic code don't get full dictionary of type. smth like this:
class F a where f :: a -> Int
instance (Num a) => F a ... instance F Int ...
g :: (Num a) => a -> a g x = f x
main = print (g (1::Int))
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
participants (2)
-
Bulat Ziganshin
-
Vyacheslav Akhmechet