-
Notifications
You must be signed in to change notification settings - Fork 0
/
workSheet2.lhs
65 lines (50 loc) · 2.18 KB
/
workSheet2.lhs
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
> import Data.List
Deep embedding -----------------------------------------------------------------
> data Graph = Empty
> | Vertex Int
> | Overlay Graph Graph
> | Connect Graph Graph
> deriving Show
> vertices :: Graph -> [Int]
> vertices Empty = []
> vertices (Vertex x) = [x]
> vertices (Overlay gl gr) = nub (vertices gl ++ vertices gr)
> vertices (Connect gl gr) = nub (vertices gl ++ vertices gr)
> edges :: Graph -> Int
> edges Empty = 0
> edges (Vertex x) = 0
> edges (Overlay gl gr) = edges gl + edges gr
> edges (Connect gl gr) = (length(vertices gl) * length(vertices gr)) + edges gl + edges gr
> roots :: Graph -> [Int]
> roots Empty = []
> roots (Vertex x) = [x]
> roots (Overlay gl gr) = roots gl ++ roots gr
> roots (Connect gl gr) = (roots gl \\ vertices gr)
Shallow embedding --------------------------------------------------------------
> type GraphShal = ([Int], Int)
> empty :: GraphShal
> empty = ([], 0)
> vertex :: Int -> GraphShal
> vertex n = ([n], 0)
> overlay :: GraphShal -> GraphShal -> GraphShal
> overlay gl gr = (fst(gl) ++ fst(gr), snd(gl) + snd(gr))
> connect :: GraphShal -> GraphShal -> GraphShal
> connect gl gr = (fst(gl) ++ fst(gr), (length (fst gl)) * (length (fst gr)) + snd gl + snd gr)
Classy embedding ---------------------------------------------------------------
> class Graphy graph where
> empty' :: graph
> vertex' :: Int -> graph
> overlay' :: graph -> graph -> graph
> connect' :: graph -> graph -> graph
> newtype Vertices = Vertices [Int]
> instance Graphy Vertices where
> empty' = Vertices []
> vertex' n = Vertices [n]
> overlay' (Vertices gl) (Vertices gr) = Vertices (gl ++ gr)
> connect' (Vertices gl) (Vertices gr) = Vertices (gl ++ gr)
> newtype Edges = Edges ([Int], Int)
> instance Graphy Edges where
> empty' = Edges ([], 0)
> vertex' n = Edges ([n], 0)
> overlay' (Edges gl) (Edges gr) = Edges (fst(gl) ++ fst(gr), snd(gl) + snd(gr))
> connect' (Edges gl) (Edges gr) = Edges (fst(gl) ++ fst(gr), (length(fst(gl)) * length(fst(gr))) + (snd(gl) + snd(gr)))