[Haskell-cafe] Re: Overlapping instances

Ashley Yakeley ashley at semantic.org
Wed Aug 24 04:03:32 EDT 2005


In article <200508110639.j7B6dSqP004876 at mx02.kabsi.at>,
 "Frank" <frank at geoinfo.tuwien.ac.at> wrote:

> I would like to state that a class Sup is exhaustively broken down in two
> subclasses Sub1 and Sub2 (meaning, for every instance of Sub1 and every
> instance of Sub2, the methods in Sup apply). 
> 
> I try to code this as:
> 
> instance Sub1 x => Sup x 
> instance Sub2 x => Sup x 
> 
> And get the (expected) error message
> ExhaustiveClass.hs:22:0:
>     Duplicate instance declarations:
>       ExhaustiveClass.hs:22:0: instance (Sub1 x) => Sup x
>       ExhaustiveClass.hs:25:0: instance (Sub2 x) => Sup x
> (I have allowed overlapping instances, undecidable instances)
> 
> Is there another way of achieving this? 
> Any help appreciated!
> 
> Andrew
> 
> 
> 
> ----------------- complete code I tried with:
> class Sub1 a where
>     op1 :: a -> a
>     
> class Sub2 a where
>     op2 :: a -> a -> a
>     
> class Sup a where
>     op3 :: a -> Bool
> 
> 
> instance Sub1 Int where
>     op1 = id
> instance Sub2 Float where
>     op2 x y = y
> 
> instance Sub1 x => Sup x where
>     op3 _ = True
>     
> instance Sub2 x => Sup x where
>     op3 _ = False

Try this:

class Sup a where
    op3 :: a -> Bool
class (Sup a) => Sub1 a where
    op1 :: a -> a
class (Sup a) => Sub2 a where
    op2 :: a -> a -> a

instance Sup Int where
    op3 _ = True
instance Sup Float where
    op3 _ = False
instance Sub1 Int where
    op1 = id
instance Sub2 Float where
    op2 x y = y

-- 
Ashley Yakeley, Seattle WA



More information about the Haskell-Cafe mailing list