-- | Sets, represented by their characteristic functions -- An example of using functions to represent data -- Functional Programming course 2016. -- Thomas Hallgren module Set(Set(..), empty, singleton, insert, union, intersection, difference, complement, member) where newtype Set a = Set (a->Bool) empty :: Set b singleton :: Eq a => a -> Set a insert :: Eq a => a -> Set a -> Set a union :: Set a -> Set a -> Set a intersection :: Set a -> Set a -> Set a complement :: Set a -> Set a difference :: Set a -> Set a -> Set a member :: a -> Set a -> Bool empty = Set (const False) singleton x = Set (==x) insert x s = union (singleton x) s union (Set f) (Set g) = Set (\x->f x || g x) intersection (Set f) (Set g) = Set (\x->f x && g x) complement (Set f) = Set (not . f) difference s1 s2 = intersection s1 (complement s2) member x (Set f) = f x