summaryrefslogtreecommitdiff
path: root/fp2/week3/camil/StdDynSet.icl
blob: 576bec9e4ac312e485be966fb926d96019044a9b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Mart Lubbers s4109503, Camil Staps s4498062

implementation module StdDynSet

import StdEnv
import StdMaybe
import StdDynamic

isEqual:: Dynamic t -> Bool | Set t
isEqual (x :: t^) a = x == a
isEqual _ _ = False

class Set a | TC, ==, toString a

:: Set = Set [(Dynamic, Dynamic -> Bool, String)]

instance zero     Set
where zero = Set []

instance toString Set
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, \x = isEqual x e, toString e)]

nrOfElts :: Set -> Int
nrOfElts (Set a) = length a

isEmptySet :: Set -> Bool
isEmptySet a = (nrOfElts a) == 0

memberOfSet :: a Set -> Bool | Set a
memberOfSet _ (Set []) = False
memberOfSet x (Set [(y,_,_):ys]) = isEqual y x || memberOfSet x (Set ys)

dynMemberOfSet :: Dynamic Set -> Bool
dynMemberOfSet _ (Set []) = False
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 = isSubset a b && nrOfElts a < nrOfElts b

union :: Set Set -> Set
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 as (Set []) = as
intersection (Set as) (Set bs) = Set [(a,eq,ts) \\ (a,eq,ts) <- as | dynMemberOfSet a (Set bs)]

without :: Set Set -> Set
without (Set as) (Set bs) = Set [(a,eq,ts) \\ (a,eq,ts) <- as | not (dynMemberOfSet a (Set bs))]

Start = toString (union (toSet 1) (toSet 2))