{-# OPTIONS_FRONTEND -Wno-missing-signatures #-} import Prelude hiding (elem) -- Returns the last element of a list: last :: Data a => [a] -> a last xs | _ ++ [e] === xs = e where e free data Nat = Z | S Nat deriving Show instance Eq Nat where x == y = True -- unintended instance! {- Z == Z = True S x == S y = x == y Z == S y = False S x == Z = False Z =:= Z = True S x =:= S y = x =:= y -} -- Occurs an element in a list? elem :: Eq a => a -> [a] -> Bool elem x [] = False elem x (y:ys) = x == y || elem x ys -- Complex data with a unique index/hash value data IVal a = IVal Int a deriving Show instance Eq a => Eq (IVal a) where IVal i1 _ == IVal i2 _ = i1 == i2 -- Addition on natural numbers add :: Nat -> Nat -> Nat add Z y = y add (S x) y = S (add x y)