summaryrefslogtreecommitdiff
path: root/src/Grasp/Graph.hs
blob: bc08bbed7fcaa327530907e86220ba1ee6984ff2 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
module Grasp.Graph (
	Node,
	LNode,
	UNode,
	
	Edge,
	LEdge,
	UEdge,
	
	Adj,
	
	Context,
	MContext,
	UContext,
	
	Decomp,
	GDecomp,
	UDecomp,

	Path,
	LPath,
	UPath,

    Gr,

	empty,
	isEmpty,
	match,
	mkGraph,
	labNodes,
	noNodes,
	labEdges,
	nodes
    ) where




import Data.List
import qualified Data.Maybe as Maybe




type Node = String

type LNode a = (Node, a)

type UNode = LNode ()

type Edge = (Node, Node)

type LEdge a = (Node, Node, a)

type UEdge = LEdge ()

type Adj b = [(b, Node)]

type Context a b = (Adj b, Node, a, Adj b)

type MContext a b = Maybe (Context a b)

type UContext = ([Node], Node, [Node])

type Decomp a b = (MContext a b, Gr a b)

type GDecomp a b = (Context a b, Gr a b)

type UDecomp = (Maybe UContext, Gr () ())

type Path = [Node]

newtype LPath a = LP [LNode a]

type UPath = [UNode]




data Gr a b = Gr { labNodes :: [LNode a]
                 , labEdges :: [LEdge b] }
    deriving (Show)




instance (Eq a, Eq b) => Eq (Gr a b) where
	a == b   =   (labNodes a == labNodes b) && (labEdges a == labEdges b)




empty :: Gr a b
empty = Gr [] []



isEmpty :: Gr a b -> Bool
isEmpty gr = (length (labNodes gr) == 0) && (length (labEdges gr) == 0)



match :: Node -> Gr a b -> Decomp a b
match n gr =
    if (n `notElem` nodes gr)
    	then (Nothing, gr)
    	else (Just (to, n, label, from), gr)
    where
    	to = edgesToAdjTo (filter (edgeTo n) (labEdges gr))
    	label = snd . head $ (filter (\(x,y) -> x == n) (labNodes gr))
    	from = edgesToAdjFrom (filter (edgeFrom n) (labEdges gr))



mkGraph :: [LNode a] -> [LEdge b] -> Gr a b
mkGraph lnodes ledges =
	let nodes = map fst lnodes
	    edgeNodes = (map (\(x,y,z) -> x) ledges) `union` (map (\(x,y,z) -> y) ledges)

	in if (all (`elem` nodes) edgeNodes)
		then Gr lnodes ledges
		else error "Edge Exception"



noNodes :: Gr a b -> Int
noNodes = length . labNodes



nodes :: Gr a b -> [Node]
nodes gr = (map fst) . labNodes $ gr



edgesToAdjFrom :: [LEdge a] -> Adj a
edgesToAdjFrom = map (\(x,y,z) -> (z,x))



edgesToAdjTo :: [LEdge a] -> Adj a
edgesToAdjTo = map (\(x,y,z) -> (z,y))



edgeFrom :: Node -> LEdge a -> Bool
edgeFrom n (x,y,z) = (x == n)



edgeTo :: Node -> LEdge a -> Bool
edgeTo n (x,y,z) = (y == n)