dynset
[fp1415.git] / fp2 / week3 / camil / StdDynSet.icl
1 implementation module StdDynSet
2
3 import StdEnv
4 import StdMaybe
5 import StdDynamic
6
7 fromDynamic :: Dynamic -> Maybe t | TC t
8 fromDynamic (x :: t^) = Just x
9 fromDynamic other = Nothing
10
11 isEqual:: Dynamic t -> Bool | Set t
12 isEqual (x :: t^) a = x == a
13 isEqual _ _ = False
14
15 class Set a | TC, ==, toString a
16
17 :: Set = Set [Dynamic]
18
19 instance zero Set
20 where zero = Set []
21
22 instance toString Set
23 where toString (Set a) = abort "toString not implemented"
24
25 instance == Set
26 where == a b = nrOfElts a == nrOfElts b && isSubset a b
27
28 toSet :: a -> Set | Set, == a
29 toSet e = Set [dynamic e]
30
31 nrOfElts :: Set -> Int
32 nrOfElts (Set a) = length a
33
34 isEmptySet :: Set -> Bool
35 isEmptySet (Set []) = True
36 isEmptySet _ = False
37
38 memberOfSet :: a Set -> Bool | Set, == a
39 memberOfSet _ (Set []) = False
40 memberOfSet x (Set [(y :: a^):xs]) = x == y || memberOfSet x (Set xs)
41 memberOfSet x (Set [y:xs]) = memberOfSet x (Set xs)
42
43 dynMemberOfSet :: Dynamic Set -> Bool
44 dynMemberOfSet _ (Set []) = False
45 dynMemberOfSet x (Set [y:xs]) = isEqual x (fromJust(fromDynamic y)) || dynMemberOfSet x (Set xs)
46 //dynMemberOfSet x (Set [y:xs]) = memberOfSet x (Set xs)
47
48 isSubset :: Set Set -> Bool
49 isSubset a b = (nrOfElts a) == (nrOfElts (intersection a b))
50
51 isStrictSubset :: Set Set -> Bool
52 isStrictSubset a b = abort "isStrictSubset nog niet geimplementeerd.\n"
53
54 union :: Set Set -> Set
55 union (Set a) (Set b) = Set (a ++ b)
56
57 intersection :: Set Set -> Set
58 intersection (Set []) bs = bs
59 intersection as (Set []) = as
60 intersection (Set [a:as]) (Set [(b::t):bs]) = Set [a \\ a <- as | not (dynMemberOfSet a (Set bs))]
61
62 without :: Set Set -> Set
63 without a b = abort "without nog niet geimplementeerd.\n"
64
65 //Start :: Set
66 Start = memberOfSet 2 (toSet 1)