Ticket #89: D.hs

File D.hs, 0.8 KB (added by guest, 4 years ago)
Line 
1{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies,
2  UndecidableInstances, FlexibleInstances #-}
3
4module D where
5
6instance (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
12test1 = (1,1) * (2,2)    -- (1+i)*(2+2*i) where i^2 = -1
13
14class TypeCast   a b   | a -> b, b->a   where
15        typeCast :: a -> b
16class TypeCast'  t a b | t a -> b, t b -> a where
17        typeCast'  :: t->a->b
18class TypeCast'' t a b | t a -> b, t b -> a where
19        typeCast'' :: t->a->b
20
21instance TypeCast'  () a b => TypeCast a b where
22        typeCast x = typeCast' () x
23instance TypeCast'' t a b => TypeCast' t a b where
24        typeCast' = typeCast''
25instance TypeCast'' () a a where typeCast'' _ x  = x