-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRelation.hs
93 lines (67 loc) · 2.98 KB
/
Relation.hs
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
module Relation ( Relation
, image
, setImage
, addImage
, addChildren
, compose
, setProduct
, adjoin
, tClosure
, limit
, connect
, classes
, breadthFirst
, depthFirst ) where
import SetADT
import Data.List ( nub )
type Relation a = Set (a, a)
image :: Ord a => Relation a -> a -> Set a
image rel val = mapSet snd (filterSet ((== val) . fst) rel)
setImage :: Ord a => Relation a -> Set a -> Set a
setImage rel = setUnion . mapSet (image rel)
addImage :: Ord a => Relation a -> Set a -> Set a
addImage ret st = st `union` setImage ret st
compose :: Ord a => Relation a -> Relation a -> Relation a
compose rel1 rel2 = mapSet (\((a, _), (_, d)) -> (a, d))
(filterSet (\((_, b), (c, _)) -> b == c) (setProduct rel1 rel2))
setProduct :: (Ord a, Ord b) => Set a -> Set b -> Set (a, b)
setProduct st1 = setUnion . mapSet (adjoin st1)
adjoin :: (Ord a, Ord b) => Set a -> b -> Set (a, b)
adjoin sett targ = mapSet (\x -> (x, targ)) sett
tClosure :: Ord a => Relation a -> Relation a
tClosure rela = limit (\x -> x `union` (x `compose` rela)) rela
limit :: Eq a => (a -> a) -> a -> a
limit f x
| x == f x = x
| otherwise = limit f (f x)
type People = String
isParents :: Relation People
isParents = makeSet [("Ben", "Sue"), ("Ben", "Leo"), ("Sue", "Joe")]
isGrandParents :: Relation People
isGrandParents = isParents `compose` isParents
inverser :: Ord a => Relation a -> Relation a
inverser = mapSet (\(a, b) -> (b, a))
addChildren :: Set People -> Set People
addChildren = addImage isParents
connect :: Ord a => Relation a -> Relation a
connect rel = tClosure rel `union` (inverser . tClosure) rel
elems :: Ord a => Relation a -> Set a
elems rel = mapSet fst rel `union` mapSet snd rel
addImages :: Ord a => Relation a -> Set (Set a) -> Set (Set a)
addImages rel = mapSet (addImage rel)
classes :: Ord a => Relation a -> Set (Set a)
classes rel = limit (addImages rel) (mapSet sing (elems rel))
newDescs :: Ord a => Relation a -> Set a -> a -> Set a
newDescs rel st v = image rel v `diff` st
findDescs :: Ord a => Relation a -> [a] -> a -> [a]
findDescs rel xs v = flatten (newDescs rel (makeSet xs) v)
breadthFirst :: Ord a => Relation a -> a -> [a]
breadthFirst rel val = limit (\x -> x ++ (nub . concatMap (findDescs rel x)) x) [val]
depthFirst :: Ord a => Relation a -> a -> [a]
depthFirst rel v = depthSearch rel v []
depthSearch :: Ord a => Relation a -> a -> [a] -> [a]
depthSearch rel v used = v : depthList rel (findDescs rel (v : used) v) (v : used)
depthList :: Ord a => Relation a -> [a] -> [a] -> [a]
depthList _ [] _ = []
depthList rel (val : rest) used = next ++ depthList rel rest (used ++ next)
where next = if val `elem` used then [] else depthSearch rel val used