Hi all
With overlapping instances, I'm allowed
class OK x y
instance Functor f = OK (f x) (f y)
instance Functor f = OK x (f y)
but I'm not allowed
class Bad x y z | x y - z
instance Functor f = Bad (f x) (f y) Bool
instance Functor f = Bad x (f y) Int
I don't quite see why.
C T McBride writes:
:
| but I'm not allowed
|
| class Bad x y z | x y - z
|
| instance Functor f = Bad (f x) (f y) Bool
|
| instance Functor f = Bad x (f y) Int
|
| I don't quite see why. Naively, I imagine that if the OK instances are
| effectively prioritized, then Bad's
| class C a b c | a b - c where
| f :: a - b - c
|
| instance C a b c = C a (x,y,b) c where
| f a (_,_,b) = f a b
|
| instance C a (a,c,b) c where
| f _ (_,c,_) = c
| ghci -fglasgow-exts -fallow-overlapping-instances compiles it without
| complaint but hugs
| With overlapping instances, I'm allowed
|
| class OK x y
|
| instance Functor f = OK (f x) (f y)
|
| instance Functor f = OK x (f y)
|
| but I'm not allowed
|
| class Bad x y z | x y - z
|
| instance Functor f = Bad (f x) (f y) Bool
|
| instance Functor f = Bad x (f y) Int
|
Wolfgang Jeltsch has observed:
I have this code:
class C a b c | a b - c where
f :: a - b - c
instance C a b c = C a (x,y,b) c where
f a (_,_,b) = f a b
instance C a (a,c,b) c where
f _ (_,c,_) = c
ghci -fglasgow-exts -fallow-overlapping-instances
Hello,
I think, I realized now what my mistake was. The handling of overlapping
instances comes into play when the compiler has to decide which method
definition to choose for a specific instance. It is not for choosing one of
more possible instances.
In my example, C Int (Int,Char,Bool) Int
I wrote on Saturday, 2003-08-09, 01:32, CEST:
Hello,
I have this code:
class C a b c | a b - c where
f :: a - b - c
instance C a b c = C a (x,y,b) c where
f a (_,_,b) = f a b
instance C a (a,c,b) c where
f _ (_,c,_) = c
ghci -fglasgow-exts
G'day all.
On Sat, Aug 09, 2003 at 01:32:49AM +0200, Wolfgang Jeltsch wrote:
ghci -fglasgow-exts -fallow-overlapping-instances compiles it without
complaint
If it helps, ghci will complain the first time you actually try to use
it.
Cheers,
Andrew Bromage
Suppose somewhere we have an instance:
instance C Int Bool Int
when the first instance decl you have says we also have
instance C Int (x,y,Bool) Int
in this case, Int + (x,y,Bool) should uniq. specify Int.
however, we also have:
instance C a (a,c,b) c
where, if we let a=Int, b=Bool,