Skip to content

Instantly share code, notes, and snippets.

@ggreif
Created February 14, 2017 10:00
Show Gist options
  • Save ggreif/de20c861309fc6b84ac89bc0e7aad658 to your computer and use it in GitHub Desktop.
Save ggreif/de20c861309fc6b84ac89bc0e7aad658 to your computer and use it in GitHub Desktop.
module Set where
import Data.List (nub)
--import qualified Data.List.NonEmpty as NE
--import Bag
-- a set is a functor with a splitter
class Applicative f => Set f where
split :: f a -> (f a, f a) -- non-deterministic (instance-defined) monotonic splitter
join :: Eq a => f a -> f a -> f a
empty :: f a -> Bool
-- Laws:
-- a) uncurry join . split == id
-- b) join a a = a
-- c) empty . snd . split implies that it was either empty or singleton
singleton :: Set f => f a -> Bool
singleton s = case split s of
(some, rest) -> empty rest && not (empty some)
instance Set [] where
empty [] = True
empty _ = False
split [] = ([], [])
split (h:t) = ([h], t)
join as bs = nub (as ++ bs)
--instance Set Bag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment