| 1 | {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, |
|---|
| 2 | UndecidableInstances, FlexibleInstances #-} |
|---|
| 3 | |
|---|
| 4 | module D where |
|---|
| 5 | |
|---|
| 6 | instance (Num a, Num b, TypeCast b a, TypeCast a b) => Num (a,b) where |
|---|
| 7 | (x,y) * (u,v) = (typeCast x * typeCast u - |
|---|
| 8 | typeCast y * typeCast v, |
|---|
| 9 | typeCast x * typeCast v + |
|---|
| 10 | typeCast y * typeCast u) |
|---|
| 11 | |
|---|
| 12 | test1 = (1,1) * (2,2) -- (1+i)*(2+2*i) where i^2 = -1 |
|---|
| 13 | |
|---|
| 14 | class TypeCast a b | a -> b, b->a where |
|---|
| 15 | typeCast :: a -> b |
|---|
| 16 | class TypeCast' t a b | t a -> b, t b -> a where |
|---|
| 17 | typeCast' :: t->a->b |
|---|
| 18 | class TypeCast'' t a b | t a -> b, t b -> a where |
|---|
| 19 | typeCast'' :: t->a->b |
|---|
| 20 | |
|---|
| 21 | instance TypeCast' () a b => TypeCast a b where |
|---|
| 22 | typeCast x = typeCast' () x |
|---|
| 23 | instance TypeCast'' t a b => TypeCast' t a b where |
|---|
| 24 | typeCast' = typeCast'' |
|---|
| 25 | instance TypeCast'' () a a where typeCast'' _ x = x |
|---|