-
Notifications
You must be signed in to change notification settings - Fork 0
/
test4.hs
215 lines (170 loc) · 5.75 KB
/
test4.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import Data.List
import Data.Maybe
type Id = String
type State = Int
type Transition = ((State, State), Id)
type LTS = [Transition]
type Alphabet = [Id]
data Process = STOP | Ref Id | Prefix Id Process | Choice [Process]
deriving (Eq, Show)
type ProcessDef = (Id, Process)
type StateMap = [((State, State), State)]
------------------------------------------------------
-- PART I
lookUp :: Eq a => a -> [(a, b)] -> b
--Pre: The item is in the table
lookUp x xs
= fromJust (lookup x xs)
states :: LTS -> [State]
states
= nub . (concatMap (\((s, s'), _) -> [s,s']))
transitions :: State -> LTS -> [Transition]
transitions state
= filter (\((s,_),_) -> s == state )
alphabet :: LTS -> Alphabet
alphabet
= nub . map(\((_,_), id) -> id)
------------------------------------------------------
-- PART II
actions :: Process -> [Id]
actions (STOP)
= []
actions (Ref _)
= []
actions (Prefix id process)
= id : (actions process)
actions (Choice processes)
= concatMap (\x -> (actions x)) processes
accepts :: [Id] -> [ProcessDef] -> Bool
--Pre: The first item in the list of process definitions is
-- that of the start process.
accepts ids defs@((_, process) : xs)
= accepts' ids process
where
accepts' :: [Id] -> Process -> Bool
accepts' [] _
= True
accepts' i (Choice pros)
= or (map (\x -> (accepts' i x)) pros)
accepts' i (Ref x)
= accepts' i (lookUp x defs)
accepts' (id : ids) (Prefix x process)
= (id == x) && (accepts' ids process)
accepts' _ (STOP) = False
------------------------------------------------------
-- PART III
--composeTransitions :: Transition -> Transition
-- -> Alphabet -> Alphabet
-- -> StateMap
-- -> [Transition]
--Pre: The first alphabet is that of the LTS from which the first transition is
-- drawn; likewise the second.
--Pre: All (four) pairs of source and target states drawn from the two transitions
-- are contained in the given StateMap.
composeTransitions ((s,t), a) ((s',t'), a') alfa alfa' smap
| a == a'
= [((map (s, s'), (map (t, t'))),a)]
| (elem a alfa') && (elem a' alfa)
= []
| (elem a' alfa)
= [((map (s, s'), (map (t, s'))),a)]
| (elem a alfa')
= [((map (s, s'), (map (s, t'))),a')]
| otherwise
= [((map (s, s'), (map (t, s'))),a), ((map (s, s'), (map (s, t'))),a')]
where
map (x, y) = lookUp (x,y) smap
pruneTransitions :: [Transition] -> LTS
pruneTransitions ts
= visit 0 []
where
visit :: State -> [State] -> [Transition]
visit s ss
| not (s `elem` ss)
= solution ++ (concatMap (\((f, t), id) -> visit t (f : ss)) solution)
| otherwise
= []
where
solution = transitions s ts
------------------------------------------------------
-- PART IV
compose :: LTS -> LTS -> LTS
compose l l' = (nub . pruneTransitions) result
where
product
= cartesian (states l) (states l')
transPair
= [((transitions x l), (transitions y l')) | x <- (states l), y <- (states l')]
transCart
= [(cartesian x y) | (x,y) <- transPair]
result
= concatMap (\(t,t') -> compose t t') (concat transCart)
compose t t'
| (null t) && (null t')
= composeTransitions s s' ("$" : (alphabet l)) ("$'" : (alphabet l')) mapS
| otherwise = composeTransitions t t' (alphabet l) (alphabet l') mapS
mapS
= map (\x -> (x, fromJust (elemIndex x product))) product
cartesian xs ys
= [(x,y) | x <- xs, y <- ys]
s = ((0,0),"$")
s' = ((0,0),"$'")
------------------------------------------------------
-- PART V
buildLTS :: [ProcessDef] -> LTS
-- Pre: All process references (Ref constructor) have a corresponding
-- definition in the list of ProcessDefs.
buildLTS
= undefined
------------------------------------------------------
-- Sample process definitions...
vendor, clock, play, maker, user, p, q, switch, off, on :: ProcessDef
vendor
= ("VENDOR", Choice [Prefix "red" (Prefix "coffee" (Ref "VENDOR")),
Prefix "blue" (Prefix "tea" (Ref "VENDOR")),
Prefix "off" STOP])
clock
= ("CLOCK", Prefix "tick" (Prefix "tock" (Ref "CLOCK")))
play
= ("PLAY", Choice [Prefix "think" (Prefix "move" (Ref "PLAY")),
Prefix "end" STOP])
maker
= ("MAKER", Prefix "make" (Prefix "ready" (Ref "MAKER")))
user
= ("USER", Prefix "ready" (Prefix "use" (Ref "USER")))
p = ("P", Prefix "a" (Prefix "b" (Prefix "c" STOP)))
q = ("Q", Prefix "d" (Prefix "c" (Prefix "b" (Ref "Q"))))
switch
= ("SWITCH", Ref "OFF")
off
= ("OFF", Choice [Prefix "on" (Ref "ON")])
on
= ("ON", Choice [Prefix "off" (Ref "OFF")])
------------------------------------------------------
-- Sample LTSs...
vendorLTS, clockLTS, playLTS, clockPlayLTS, makerLTS, userLTS, makerUserLTS,
pLTS, qLTS, pqLTS, switchLTS :: LTS
vendorLTS
= [((0,1),"off"),((0,2),"blue"),((0,3),"red"),((2,0),"tea"),((3,0),"coffee")]
clockLTS
= [((0,1),"tick"),((1,0),"tock")]
playLTS
= [((0,1),"end"),((0,2),"think"),((2,0),"move")]
clockPlayLTS
= [((0,1),"end"),((1,4),"tick"),((4,1),"tock"),((0,3),"tick"),
((3,4),"end"),((3,0),"tock"),((3,5),"think"),((5,3),"move"),
((5,2),"tock"),((2,0),"move"),((2,5),"tick"),((0,2),"think")]
makerLTS
= [((0,1),"make"),((1,0),"ready")]
userLTS
= [((0,1),"ready"),((1,0),"use")]
makerUserLTS
= [((0,2),"make"),((2,1),"ready"),((1,0),"use"),((1,3),"make"),((3,2),"use")]
pLTS
= [((0,1),"a"),((1,2),"b"),((2,3),"c")]
qLTS
= [((0,1),"d"),((1,2),"c"),((2,0),"b")]
pqLTS
= [((0,1),"d"),((1,4),"a"),((0,3),"a"),((3,4),"d")]
switchLTS
= [((0,1),"on"),((1,0),"off")]