-- Eq class Eq a where { eq :: a -> a -> Bool; } instance Eq Int where { eq x y = x == y; } -- Float data Float = MkFloat Int; instance Eq Float where { eq (MkFloat x) (MkFloat y) = eq x y; } -- Num class Num a where { zero :: a; add :: a -> a -> a; } instance Num Int where { zero = 0; add x y = x + y; } instance Num Float where { zero = MkFloat 0; add (MkFloat x) (MkFloat y) = MkFloat (x + y); } sum :: Num a => List a -> a; sum l = case l of Nil -> zero; Cons x xs -> add x (sum xs); ; -- class Collects c where { type Elem c; empty :: c; insert :: Elem c -> c -> c; toList :: c -> List (Elem c); } instance Eq e => Collects (List e) where { type Elem (List e) = e; empty = Nil; insert = Cons; toList c = c; } data BitSet = MkBitSet; instance Collects BitSet where { type Elem BitSet = Char; empty = MkBitSet; insert = insert; toList = toList; } --sumColl :: Collects c, Elem c = Int => c -> Int; -- ERROR (now ok) --sumColl :: Collects c, Elem c = Int => c -> Elem c; -- ERROR (now ok) --sumColl :: Collects c, Num (Elem c) => c -> Elem c; -- ok (still ok) --sumColl :: List Int -> Int; -- ok (still ok) sumColl c = sum (toList c); --sumColl = sumColl; main = 42;