-implementation module StdSet\r
+definition module StdSet\r
\r
-import StdEnv\r
+import StdClass\r
\r
:: Set a\r
+\r
+toSet :: [a] -> Set a | Eq a\r
+fromSet :: (Set a) -> [a]\r
+\r
+isEmptySet :: (Set a) -> Bool\r
+isDisjoint :: (Set a) (Set a) -> Bool | Eq a\r
+isSubset :: (Set a) (Set a) -> Bool | Eq a\r
+isStrictSubset :: (Set a) (Set a) -> Bool | Eq a\r
+memberOfSet :: a (Set a) -> Bool | Eq a\r
+union :: (Set a) (Set a) -> Set a | Eq a\r
+intersection :: (Set a) (Set a) -> Set a | Eq a\r
+nrOfElements :: (Set a) -> Int\r
+without :: (Set a) (Set a) -> Set a | Eq a\r
+\r
+product :: (Set a) (Set b) -> Set (a,b)\r
+\r
+instance zero (Set a)\r
+instance == (Set a) | Eq a\r
+\r
+powerSet :: (Set a) -> Set (Set a)\r
--- /dev/null
+definition module StdSet\r
+\r
+import StdClass\r
+\r
+:: Set a\r
+\r
+toSet :: [a] -> Set a | Eq a\r
+fromSet :: (Set a) -> [a]\r
+\r
+isEmptySet :: (Set a) -> Bool\r
+isDisjoint :: (Set a) (Set a) -> Bool | Eq a\r
+isSubset :: (Set a) (Set a) -> Bool | Eq a\r
+isStrictSubset :: (Set a) (Set a) -> Bool | Eq a\r
+memberOfSet :: a (Set a) -> Bool | Eq a\r
+union :: (Set a) (Set a) -> Set a | Eq a\r
+intersection :: (Set a) (Set a) -> Set a | Eq a\r
+nrOfElements :: (Set a) -> Int\r
+without :: (Set a) (Set a) -> Set a | Eq a\r
+\r
+product :: (Set a) (Set b) -> Set (a,b)\r
+\r
+instance zero (Set a)\r
+instance == (Set a) | Eq a\r
+\r
+powerSet :: (Set a) -> Set (Set a)\r
--- /dev/null
+implementation module StdSet\r
+\r
+import StdEnv\r
+import StdClass\r
+\r
+:: Set a :== [a]\r
+\r
+toSet :: [a] -> Set a | Eq a\r
+toSet l = toSet` l []\r
+where \r
+ toSet` [] s = s\r
+ toSet` [x:xs] s = toSet` xs (join x s)\r
+ where\r
+ join :: a (Set a) -> Set a | Eq a\r
+ join e s\r
+ | memberOfSet e s = s\r
+ | otherwise = s ++ [e]\r
+\r
+fromSet :: (Set a) -> [a]\r
+fromSet s = s\r
+\r
+isEmptySet :: (Set a) -> Bool\r
+isEmptySet [] = True\r
+isEmptySet _ = False\r
+\r
+isDisjoint :: (Set a) (Set a) -> Bool | Eq a\r
+isDisjoint s1 s2 = length (intersection s1 s2) == 0\r
+\r
+isSubset :: (Set a) (Set a) -> Bool | Eq a\r
+isSubset s1 s2 = nrOfElements (intersection s1 s2) == nrOfElements s1\r
+\r
+isStrictSubset :: (Set a) (Set a) -> Bool | Eq a\r
+isStrictSubset s1 s2 = isSubset s1 s2 && s1 <> s2\r
+\r
+memberOfSet :: a (Set a) -> Bool | Eq a\r
+memberOfSet e [] = False\r
+memberOfSet e [x:xs] \r
+ | e == x = True\r
+ | otherwise = memberOfSet e xs\r
+\r
+union :: (Set a) (Set a) -> Set a | Eq a\r
+union s1 s2 = toSet (s1 ++ s2)\r
+\r
+intersection :: (Set a) (Set a) -> Set a | Eq a\r
+intersection s1 s2 = [e \\ e <- s1 | memberOfSet e s2]\r
+\r
+nrOfElements :: (Set a) -> Int\r
+nrOfElements s = length (fromSet s)\r
+\r
+without :: (Set a) (Set a) -> Set a | Eq a\r
+without s1 s2 = [e \\ e <- s1 | (memberOfSet e s2) == False]\r
+\r
+product :: (Set a) (Set b) -> Set (a,b)\r
+product s1 s2 = [(e1,e2) \\ e1 <- s1, e2 <- s2]\r
+\r
+instance zero (Set a)\r
+where zero = []\r
+\r
+instance == (Set a) | Eq a \r
+where (==) s1 s2 = isSubset s1 s2 && isSubset s2 s1\r
+\r
+powerSet :: (Set a) -> Set (Set a)\r
+powerSet [] = [zero]\r
+powerSet [e:es] = map ((++) [e]) (powerSet es) ++ powerSet es\r
+\r
+powerSet` :: (Set a) -> [Int]\r
+powerSet` s = [0 .. 2^(nrOfElements s) - 1]\r
+\r
+takeMod :: (Set a) Int -> Set a\r
+takeMod s m = [e \\ e <- s & k <- [0..] | k rem m == 0]\r