-
Notifications
You must be signed in to change notification settings - Fork 85
/
timeSemScript.sml
390 lines (331 loc) · 10.7 KB
/
timeSemScript.sml
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
(*
semantics for timeLang
*)
open preamble
timeLangTheory
val _ = new_theory "timeSem";
Datatype:
panic = PanicTimeout
| PanicInput in_signal
End
Datatype:
label = LDelay time
| LAction ioAction
| LPanic panic
End
Datatype:
state =
<| clocks : clock |-> time
; location : loc
; ioAction : ioAction option
; waitTime : time option
|>
End
Definition mkState_def:
mkState cks loc io wt =
<| clocks := cks
; location := loc
; ioAction := io
; waitTime := wt
|>
End
Definition resetOutput_def:
resetOutput st =
st with
<| ioAction := NONE
; waitTime := NONE
|>
End
Definition resetClocks_def:
resetClocks fm xs =
fm |++ ZIP (xs,MAP (λx. 0:time) xs)
End
(* TODO: rephrase this def *)
Definition list_min_option_def:
(list_min_option ([]:num list) = NONE) /\
(list_min_option (x::xs) =
case list_min_option xs of
| NONE => SOME x
| SOME y => SOME (if x < y then x else y))
End
Definition delay_clocks_def:
delay_clocks fm (d:num) = FEMPTY |++
(MAP (λ(x,y). (x,y+d))
(fmap_to_alist fm))
End
Definition minusT_def:
minusT (t1:time) (t2:time) = t1 - t2
End
(* inner case for generating induction theorem *)
Definition evalExpr_def:
evalExpr st e =
case e of
| ELit t => SOME t
| EClock c => FLOOKUP st.clocks c
| ESub e1 e2 =>
case (evalExpr st e1, evalExpr st e2) of
| SOME t1,SOME t2 =>
if t2 ≤ t1 then SOME (minusT t1 t2)
else NONE
| _=> NONE
End
Definition evalCond_def:
(evalCond st (CndLe e1 e2) =
case (evalExpr st e1,evalExpr st e2) of
| SOME t1,SOME t2 => t1 ≤ t2
| _ => F) ∧
(evalCond st (CndLt e1 e2) =
case (evalExpr st e1,evalExpr st e2) of
| SOME t1,SOME t2 => t1 < t2
| _ => F)
End
Definition evalDiff_def:
evalDiff st ((t,c): time # clock) =
evalExpr st (ESub (ELit t) (EClock c))
End
Definition calculate_wtime_def:
calculate_wtime st clks diffs =
let
st = st with clocks := resetClocks st.clocks clks
in
list_min_option (MAP (THE o evalDiff st) diffs)
End
Inductive evalTerm:
(∀st in_signal cnds clks dest diffs.
EVERY (λck. ck IN FDOM st.clocks) clks ∧
EVERY (λ(t,c).
∃v. FLOOKUP st.clocks c = SOME v ∧
v ≤ t) diffs ==>
evalTerm st (SOME in_signal)
(Tm (Input in_signal)
cnds
clks
dest
diffs)
(st with <| clocks := resetClocks st.clocks clks
; ioAction := SOME (Input in_signal)
; location := dest
; waitTime := calculate_wtime st clks diffs|>)) /\
(∀st out_signal cnds clks dest diffs.
EVERY (λck. ck IN FDOM st.clocks) clks ∧
EVERY (λ(t,c).
∃v. FLOOKUP st.clocks c = SOME v ∧
v ≤ t) diffs ==>
evalTerm st NONE
(Tm (Output out_signal)
cnds
clks
dest
diffs)
(st with <| clocks := resetClocks st.clocks clks
; ioAction := SOME (Output out_signal)
; location := dest
; waitTime := calculate_wtime st clks diffs|>))
End
Definition max_clocks_def:
max_clocks fm (m:num) ⇔
∀ck n.
FLOOKUP fm ck = SOME n ⇒
n < m
End
Definition tm_conds_eval_def:
tm_conds_eval s tm =
EVERY (λcnd.
EVERY (λe. case (evalExpr s e) of
| SOME n => T
| _ => F) (destCond cnd))
(termConditions tm)
End
Definition tms_conds_eval_def:
tms_conds_eval s tms =
EVERY (tm_conds_eval s) tms
End
Definition tm_conds_eval_limit_def:
tm_conds_eval_limit m s tm =
EVERY (λcnd.
EVERY (λe. case (evalExpr s e) of
| SOME n => n < m
| _ => F) (destCond cnd))
(termConditions tm)
End
Definition conds_eval_lt_dimword_def:
conds_eval_lt_dimword m s tms =
EVERY (tm_conds_eval_limit m s) tms
End
Definition time_range_def:
time_range wt (m:num) ⇔
EVERY (λ(t,c). t < m) wt
End
Definition term_time_range_def:
term_time_range m tm =
time_range (termWaitTimes tm) m
End
Definition terms_time_range_def:
terms_time_range m tms =
EVERY (term_time_range m) tms
End
Definition input_terms_actions_def:
input_terms_actions m tms =
EVERY (λn. n+1 < m)
(terms_in_signals tms)
End
Definition terms_wtimes_ffi_bound_def:
terms_wtimes_ffi_bound m s tms =
EVERY (λtm.
case calculate_wtime (resetOutput s) (termClks tm) (termWaitTimes tm) of
| NONE => T
| SOME wt => wt < m
) tms
End
(* max is dimword *)
(* m is m+n *)
Inductive pickTerm:
(!st m cnds in_signal clks dest diffs tms st'.
EVERY (λcnd. evalCond st cnd) cnds ∧
conds_eval_lt_dimword m st (Tm (Input in_signal) cnds clks dest diffs::tms) ∧
max_clocks st.clocks m ∧
terms_time_range m (Tm (Input in_signal) cnds clks dest diffs::tms) ∧
input_terms_actions m (Tm (Input in_signal) cnds clks dest diffs::tms) ∧
terms_wtimes_ffi_bound m st (Tm (Input in_signal) cnds clks dest diffs::tms) ∧
evalTerm st (SOME in_signal) (Tm (Input in_signal) cnds clks dest diffs) st' ⇒
pickTerm st m (SOME in_signal) (Tm (Input in_signal) cnds clks dest diffs::tms) st'
(LAction (Input in_signal))) ∧
(!st m cnds out_signal clks dest diffs tms st'.
EVERY (λcnd. evalCond st cnd) cnds ∧
conds_eval_lt_dimword m st (Tm (Output out_signal) cnds clks dest diffs::tms) ∧
max_clocks st.clocks m ∧
terms_time_range m (Tm (Output out_signal) cnds clks dest diffs::tms) ∧
input_terms_actions m tms ∧
terms_wtimes_ffi_bound m st (Tm (Output out_signal) cnds clks dest diffs::tms) ∧
evalTerm st NONE (Tm (Output out_signal) cnds clks dest diffs) st' ⇒
pickTerm st m NONE (Tm (Output out_signal) cnds clks dest diffs::tms) st'
(LAction (Output out_signal))) ∧
(!st m cnds event ioAction clks dest diffs tms st' lbl.
EVERY (λcnd. EVERY (λe. ∃t. evalExpr st e = SOME t) (destCond cnd)) cnds ∧
~(EVERY (λcnd. evalCond st cnd) cnds) ∧
tm_conds_eval_limit m st (Tm ioAction cnds clks dest diffs) ∧
term_time_range m (Tm ioAction cnds clks dest diffs) ∧
input_terms_actions m [(Tm ioAction cnds clks dest diffs)] ∧
terms_wtimes_ffi_bound m st (Tm ioAction cnds clks dest diffs :: tms) ∧
pickTerm st m event tms st' lbl ⇒
pickTerm st m event (Tm ioAction cnds clks dest diffs :: tms) st' lbl) ∧
(!st m cnds event in_signal clks dest diffs tms st' lbl.
event <> SOME in_signal ∧
tm_conds_eval_limit m st (Tm (Input in_signal) cnds clks dest diffs) ∧
term_time_range m (Tm (Input in_signal) cnds clks dest diffs) ∧
terms_wtimes_ffi_bound m st (Tm (Input in_signal) cnds clks dest diffs :: tms) ∧
in_signal + 1 < m ∧
pickTerm st m event tms st' lbl ⇒
pickTerm st m event (Tm (Input in_signal) cnds clks dest diffs :: tms) st' lbl) ∧
(!st m cnds event out_signal clks dest diffs tms st' lbl.
event <> NONE ∧
tm_conds_eval_limit m st (Tm (Output out_signal) cnds clks dest diffs) ∧
term_time_range m (Tm (Output out_signal) cnds clks dest diffs) ∧
terms_wtimes_ffi_bound m st (Tm (Output out_signal) cnds clks dest diffs :: tms) ∧
pickTerm st m event tms st' lbl ⇒
pickTerm st m event (Tm (Output out_signal) cnds clks dest diffs :: tms) st' lbl) ∧
(!st m.
max_clocks st.clocks m ⇒
pickTerm st m NONE [] st (LPanic PanicTimeout)) ∧
(!st m in_signal.
max_clocks st.clocks m ∧
in_signal + 1 < m ⇒
pickTerm st m (SOME in_signal) [] st (LPanic (PanicInput in_signal)))
End
(* d + n ≤ m ∧ *)
(* m ≤ w + n *)
(* n would be FST (seq 0), or may be systime time *)
Inductive step:
(!p m n st d.
st.waitTime = NONE ∧
d + n < m ∧
max_clocks (delay_clocks (st.clocks) (d + n)) m ⇒
step p (LDelay d) m n st
(mkState
(delay_clocks (st.clocks) d)
st.location
NONE
NONE)) ∧
(!p m n st d w.
st.waitTime = SOME w ∧
d ≤ w ∧ w < m ∧ d + n < m ∧
max_clocks (delay_clocks (st.clocks) (d + n)) m ⇒
step p (LDelay d) m n st
(mkState
(delay_clocks (st.clocks) d)
st.location
NONE
(SOME (w - d)))) ∧
(!p m n st tms st' in_signal.
n < m ∧
ALOOKUP p st.location = SOME tms ∧
(case st.waitTime of
| NONE => T
| SOME wt => wt ≠ 0 ∧ wt < m) ∧
pickTerm (resetOutput st) m (SOME in_signal) tms st' (LAction (Input in_signal)) ∧
st'.ioAction = SOME (Input in_signal) ⇒
step p (LAction (Input in_signal)) m n st st') ∧
(!p m n st tms st' out_signal.
n < m ∧
ALOOKUP p st.location = SOME tms ∧
st.waitTime = SOME 0 ∧
pickTerm (resetOutput st) m NONE tms st' (LAction (Output out_signal)) ∧
st'.ioAction = SOME (Output out_signal) ⇒
step p (LAction (Output out_signal)) m n st st') ∧
(!p m n st tms st'.
n < m ∧
ALOOKUP p st.location = SOME tms ∧
st.waitTime = SOME 0 ∧
pickTerm (resetOutput st) m NONE tms st' (LPanic PanicTimeout) ⇒
step p (LPanic PanicTimeout) m n st st') ∧
(!p m n st tms st' in_signal.
n < m ∧
ALOOKUP p st.location = SOME tms ∧
(case st.waitTime of
| NONE => T
| SOME wt => wt ≠ 0 ∧ wt < m) ∧
pickTerm (resetOutput st) m (SOME in_signal) tms st' (LPanic (PanicInput in_signal)) ⇒
step p (LPanic (PanicInput in_signal)) m n st st')
End
Definition steps_def:
(steps prog [] m n s [] ⇔ T) ∧
(steps prog (lbl::lbls) m n s (st::sts) ⇔
step prog lbl m n s st ∧
let n' =
case lbl of
| LDelay d => d + n
| _ => n
in
steps prog lbls m n' st sts) ∧
(steps prog _ m _ s _ ⇔ F)
End
(*
Definition steps_def:
(steps prog [] m n s [] ⇔
n < m ∧
(case s.waitTime of
| SOME w => (* w ≠ 0 ∧ *) w < m
| NONE => T)) ∧
(steps prog (lbl::lbls) m n s (st::sts) ⇔
step prog lbl m n s st ∧
let n' =
case lbl of
| LDelay d => d + n
| _ => n
in
steps prog lbls m n' st sts) ∧
(steps prog _ m _ s _ ⇔ F)
End
*)
Inductive stepTrace:
(!p m n st.
stepTrace p m n st st []) ∧
(!p lbl m n st st' st'' tr.
step p lbl m n st st' ∧
stepTrace p m (case lbl of
| LDelay d => d + n
| LAction _ => n)
st' st'' tr ⇒
stepTrace p m n st st'' (lbl::tr))
End
val _ = export_theory();