1 implementation module StdDynSet
7 isEqual:: Dynamic t -> Bool | Set t
8 isEqual (x :: t^) a = x == a
11 class Set a | TC, ==, toString a
13 :: Set = Set [(Dynamic, Dynamic -> Bool, String)]
19 where toString (Set [(_,_,a):as]) = "{" +++ a +++ (foldl (+++) "" ["," +++ s \\ (_,_,s) <- as]) +++ "}"
22 where == a b = nrOfElts a == nrOfElts b && isSubset a b
24 toSet :: a -> Set | Set a
25 toSet e = Set [(dynamic e, \x = isEqual x e, toString e)]
27 nrOfElts :: Set -> Int
28 nrOfElts (Set a) = length a
30 isEmptySet :: Set -> Bool
31 isEmptySet a = (nrOfElts a) == 0
33 memberOfSet :: a Set -> Bool | Set a
34 memberOfSet _ (Set []) = False
35 memberOfSet x (Set [(y,_,_):ys]) = isEqual y x || memberOfSet x (Set ys)
37 dynMemberOfSet :: Dynamic Set -> Bool
38 dynMemberOfSet _ (Set []) = False
39 dynMemberOfSet x (Set [(_,eq,_):ys]) = eq x || dynMemberOfSet x (Set ys)
41 isSubset :: Set Set -> Bool
42 isSubset a b = (nrOfElts a) == (nrOfElts (intersection a b))
44 isStrictSubset :: Set Set -> Bool
45 isStrictSubset a b = isSubset a b && nrOfElts a < nrOfElts b
47 union :: Set Set -> Set
48 union (Set a) (Set b) = Set (a ++ (fromSet (without (Set b) (Set a))))
50 fromSet :: Set -> [(Dynamic, Dynamic -> Bool, String)]
53 intersection :: Set Set -> Set
54 intersection as (Set []) = as
55 intersection (Set as) (Set bs) = Set [(a,eq,ts) \\ (a,eq,ts) <- as | dynMemberOfSet a (Set bs)]
57 without :: Set Set -> Set
58 without (Set as) (Set bs) = Set [(a,eq,ts) \\ (a,eq,ts) <- as | not (dynMemberOfSet a (Set bs))]
60 Start = toString (union (toSet 1) (toSet 2))