StdDynSet
[fp1415.git] / fp2 / week3 / camil / StdDynSet.icl
index 2d3f95a..c4e90fe 100644 (file)
@@ -4,70 +4,57 @@ import StdEnv
 import StdMaybe
 import StdDynamic
 
-fromDynamic :: Dynamic -> Maybe t | TC t
-fromDynamic (x :: t^) = Just x
-fromDynamic other = Nothing
-
 isEqual:: Dynamic t -> Bool | Set t
 isEqual (x :: t^) a = x == a
 isEqual _ _ = False
 
-//isEqual`:: Dynamic Dynamic -> Bool | Set t
-//isEqual` (x :: t^) (y :: t^) = x == y
-//isEqual` _ _ = False
-
-isEqual`` :: Dynamic Dynamic -> Bool
-isEqual`` x y = (fromJust(fromDynamic x)) == (fromJust(fromDynamic y))
-
 class Set a | TC, ==, toString a
 
-:: Set = Set [Dynamic]
+:: Set = Set [(Dynamic, Dynamic -> Bool, String)]
 
 instance zero     Set
 where zero = Set []
 
 instance toString Set
-where toString (Set a) = abort "toString not implemented"
+where toString (Set [(_,_,a):as]) = "{" +++ a +++ (foldl (+++) "" ["," +++ s \\ (_,_,s) <- as]) +++ "}"
        
 instance == Set
 where == a b = nrOfElts a == nrOfElts b && isSubset a b
 
-toSet :: a -> Set | Set, == a
-toSet e = Set [dynamic e]
+toSet :: a -> Set | Set a
+toSet e = Set [(dynamic e, \x = isEqual x e, toString e)]
 
 nrOfElts :: Set -> Int
 nrOfElts (Set a) = length a
 
 isEmptySet :: Set -> Bool
-isEmptySet (Set []) = True
-isEmptySet _ = False
+isEmptySet a = (nrOfElts a) == 0
 
-memberOfSet :: a Set -> Bool | Set, == a
+memberOfSet :: a Set -> Bool | Set a
 memberOfSet _ (Set []) = False
-memberOfSet x (Set [(y :: a^):xs]) = x == y || memberOfSet x (Set xs)
-memberOfSet x (Set [y:xs]) = memberOfSet x (Set xs)
+memberOfSet x (Set [(y,_,_):ys]) = isEqual y x || memberOfSet x (Set ys)
 
 dynMemberOfSet :: Dynamic Set -> Bool
 dynMemberOfSet _ (Set []) = False
-dynMemberOfSet x (Set [y:xs]) = isEqual x (fromJust(fromDynamic y)) || dynMemberOfSet x (Set xs)
-//dynMemberOfSet x (Set [y:xs]) = memberOfSet x (Set xs)
+dynMemberOfSet x (Set [(_,eq,_):ys]) = eq x || dynMemberOfSet x (Set ys)
 
 isSubset :: Set Set -> Bool
 isSubset a b = (nrOfElts a) == (nrOfElts (intersection a b))
 
 isStrictSubset :: Set Set -> Bool
-isStrictSubset a b = abort "isStrictSubset nog niet geimplementeerd.\n"
+isStrictSubset a b = isSubset a b && nrOfElts a < nrOfElts b
 
 union :: Set Set -> Set
-union (Set a) (Set b) = Set (a ++ b)
+union (Set a) (Set b) = Set (a ++ (fromSet (without (Set b) (Set a))))
+where 
+       fromSet :: Set -> [(Dynamic, Dynamic -> Bool, String)]
+       fromSet (Set x) = x
 
 intersection :: Set Set -> Set
-intersection (Set []) bs = bs
 intersection as (Set []) = as
-intersection (Set [a:as]) (Set bs) = Set [a \\ a <- as | not (dynMemberOfSet a (Set bs))]
+intersection (Set as) (Set bs) = Set [(a,eq,ts) \\ (a,eq,ts) <- as | dynMemberOfSet a (Set bs)]
 
 without :: Set Set -> Set
-without a b = abort "without nog niet geimplementeerd.\n"
+without (Set as) (Set bs) = Set [(a,eq,ts) \\ (a,eq,ts) <- as | not (dynMemberOfSet a (Set bs))]
 
-//Start :: Set
-Start = memberOfSet 2 (toSet 1)
+Start = toString (union (toSet 1) (toSet 2))