-
Notifications
You must be signed in to change notification settings - Fork 1
/
Graph.hs
60 lines (51 loc) · 2.03 KB
/
Graph.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
-- http://www.graphviz.org/doc/info/lang.html
module Graph (
-- types
Attribute(..),
Directed(..),
Statement(..),
Graph(..),
-- classes
AsGraph(..),
) where
data Attribute = Label String |
Style String |
Color String |
Fillcolor String |
Shape String |
Fontname String |
Fontsize Int |
Fontcolor String
data Directed = Directed | NonDirected
data Statement = Node String [Attribute] |
Edge Directed String String [Attribute] |
Attributes String [Attribute] |
SubGraph String [Statement]
data Graph = Graph Directed String [Statement]
class AsGraph a where
graph :: a -> Graph
instance Show Attribute where
show (Label label) = "label=\"" ++ label ++ "\""
show (Style style) = "style=" ++ style
show (Color color) = "color=\"" ++ color ++ "\""
show (Fillcolor fillcolor) = "fillcolor=\"" ++ fillcolor ++ "\""
show (Shape shape) = "shape=" ++ shape
show (Fontname fontname) = "fontname=\"" ++ fontname ++ "\""
show (Fontsize fontsize) = "fontsize=\"" ++ (show fontsize) ++ "\""
show (Fontcolor fontcolor) = "fontcolor=\"" ++ fontcolor ++ "\""
instance Show Statement where
show (Node id list) = id ++ " " ++ (show list) ++ "\n"
show (Edge Directed a b list) = a ++ " -> " ++ b ++ (show list) ++ "\n"
show (Edge NonDirected a b list) = a ++ " -- " ++ b ++ (show list) ++ "\n"
show (Attributes element list) = element ++ " " ++ (show list) ++ "\n"
show (SubGraph id list) =
"subgraph " ++ id ++ " {\n" ++ (show list) ++ "}\n"
showList (x:xs) = shows x . showl xs
where showl (x:xs) = shows x . showl xs
showl [] = showString ""
showList [] = showString ""
instance Show Graph where
show (Graph Directed id list) =
"digraph " ++ id ++ " {\n" ++ (show list) ++ "}\n"
show (Graph NonDirected id list) =
"graph " ++ id ++ " {\n" ++ (show list) ++ "}\n"