yet another very simple overlapping instance example

Razvan Musaloiu-E. razvanm@mail.com
Wed, 25 Jun 2003 20:37:04 +0800


Hi!

Koen Claessen wrote:
>  | instance (Num a) => Op_plus a MyInt where
>  |      i `plus` (MyInt b) = i + b
> 
> Remember that b is of type Int, but you also say that i is
> of any Num type. This clashes, since + requires both if its
> arguments to hve the same types.

You are right! I did't notice that error. Here is a version of the 
example free (if a remove the last instance compiles cleanly) of this 
mistake.

	data MyInt = MyInt Integer deriving Show

	class Op_plus a b where
	    plus :: a -> b -> Integer

	instance Op_plus MyInt MyInt where
	    (MyInt a) `plus` (MyInt b) = a + b

	instance (Integral a) => Op_plus a MyInt where
	    i `plus` (MyInt b) = (toInteger i) + b

Obs: perhaps MyInt should be now MyInteger. :-)

--
Razvan ME