-
Notifications
You must be signed in to change notification settings - Fork 84
/
stackProgScript.sml
240 lines (219 loc) · 7 KB
/
stackProgScript.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
(*
An example of a stack data structure implemented using CakeML arrays, verified
using CF.
*)
open preamble basis
val _ = new_theory "stackProg";
val _ = translation_extends"basisProg";
Datatype:
exn_type = EmptyStack
End
val _ = register_exn_type ``:exn_type``;
val stack_decls = process_topdecs
‘fun empty_stack u = Ref (Array.arrayEmpty (), 0)
fun push q e =
case !q of (a,i) =>
let val l = Array.length a in
if i >= l then
let val a' = Array.array (2*l+1) e in
(Array.copy a a' 0;
q := (a', i+1))
end
else
(Array.update a i e;
q := (a,i+1))
end
fun pop q =
case !q of
(a,i) => if i = 0 then raise Emptystack
else let val x = Array.sub a (i-1) in (q := (a, i-1); x) end’;
val _ = append_prog stack_decls;
Definition EmptyStack_exn_def:
EmptyStack_exn v = STACKPROG_EXN_TYPE_TYPE EmptyStack v
End
val EmptyStack_exn_def = EVAL ``EmptyStack_exn v``;
(* Heap predicate for stacks:
STACK A vs qv means qv is a reference to a stack of
elements vs, with A the refinement invariant satsfied by the elements of the stack *)
Definition STACK_def:
STACK A vs qv =
SEP_EXISTS av iv vvs junk.
REF qv (Conv NONE [av;iv]) *
& NUM (LENGTH vs) iv *
ARRAY av (vvs ++ junk) *
& LIST_REL A vs vvs
End
(* Some simple auto tactics *)
val xsimpl_tac = rpt(FIRST [xcon, (CHANGED_TAC xsimpl), xif, xmatch, xapp]);
val xs_auto_tac = rpt (FIRST [xcon, (CHANGED_TAC xsimpl), xif, xmatch, xapp, xlet_auto, xref]);
val st = get_ml_prog_state ();
Theorem empty_stack_spec':
!uv. app (p:'ffi ffi_proj) ^(fetch_v "empty_stack" st) [uv]
emp (POSTv qv. STACK A [] qv)
Proof
strip_tac \\
xcf "empty_stack" st \\
xlet `POSTv v. &UNIT_TYPE () v` THEN1(xcon \\ xsimpl) \\
xlet `POSTv av. ARRAY av []` THEN1(xapp \\ fs[]) \\
xlet `POSTv pv. SEP_EXISTS av iv.
&(pv = Conv NONE [av; iv]) * ARRAY av [] * &NUM 0 iv`
THEN1(xcon \\ xsimpl) \\
xref >> simp[STACK_def] >> xsimpl
QED
Theorem empty_stack_spec:
!uv. app (p:'ffi ffi_proj) ^(fetch_v "empty_stack" st) [uv]
emp (POSTv qv. STACK A [] qv)
Proof
strip_tac >>
xcf "empty_stack" st >> xs_auto_tac >> simp[STACK_def] >>
xs_auto_tac
QED
Theorem push_spec':
!qv xv vs x. app (p:'ffi ffi_proj) ^(fetch_v "push" st) [qv; xv]
(STACK A vs qv * & A x xv)
(POSTv uv. STACK A (vs ++ [x]) qv)
Proof
rpt strip_tac >>
xcf "push" st >>
simp[STACK_def] >>
xpull >>
xlet_auto >-(xsimpl)>>
xmatch >> reverse(rw[]) >- EVAL_TAC >>
xlet_auto >-(xsimpl) >>
xlet_auto >-(xsimpl) >>
xif
>-(
xlet_auto >-(xsimpl) >>
xlet_auto >-(xsimpl) >>
xlet_auto >-(xsimpl) >>
xlet_auto >-(xsimpl >> fs[REPLICATE, REPLICATE_APPEND_DECOMPOSE_SYM, LENGTH_REPLICATE]) >>
xlet_auto >-(xsimpl) >>
xlet_auto >-(xcon >> xsimpl) >>
xapp >>
xsimpl >>
fs[UNIT_TYPE_def] >>
(* Should be partially automatized *)
qexists_tac `vvs ++ [xv]` >>
`LENGTH junk = 0` by (
`LENGTH vs = LENGTH vvs` by metis_tac[LIST_REL_LENGTH] >>
bossLib.DECIDE_TAC
) >>
fs[LENGTH_NIL] >>
fs[REPLICATE, REPLICATE_PLUS_ONE] >>
fs (get_retract_thms())
(*---------------------------------*)
) >>
xlet_auto >-(xsimpl) >>
xlet_auto >-(xsimpl) >>
xlet_auto >-(xcon >> xsimpl) >>
xapp >>
xsimpl >>
(* Should be partially automatized *)
fs[UNIT_TYPE_def] >>
qexists_tac `vvs ++ [xv]` >>
qexists_tac `TL junk` >>
fs (get_retract_thms()) >>
`LENGTH vs = LENGTH vvs` by metis_tac[LIST_REL_LENGTH] >>
Cases_on `junk:v list` >-(fs[LENGTH_NIL]) >>
`vvs++[h]++t = vvs++h::t` by rw[] >>
POP_ASSUM (fn x => fs[x, LUPDATE_LENGTH])
QED
Theorem push_spec:
!qv xv vs x. app (p:'ffi ffi_proj) ^(fetch_v "push" st) [qv; xv]
(STACK A vs qv * & A x xv)
(POSTv uv. STACK A (vs ++ [x]) qv)
Proof
rpt strip_tac >>
xcf "push" st >>
simp[STACK_def] >>
xpull >>
xs_auto_tac >>
reverse(rw[]) >- EVAL_TAC >>
xs_auto_tac
(* 3 subgoals *)
>-(fs[REPLICATE, REPLICATE_APPEND_DECOMPOSE_SYM, LENGTH_REPLICATE])
>-(
fs[UNIT_TYPE_def] >>
qexists_tac `vvs ++ [xv]` >>
`LENGTH junk = 0` by (
`LENGTH vs = LENGTH vvs` by metis_tac[LIST_REL_LENGTH] >>
bossLib.DECIDE_TAC
) >>
fs[LENGTH_NIL] >>
fs[REPLICATE, REPLICATE_PLUS_ONE] >>
fs (get_retract_thms())
) >>
fs[UNIT_TYPE_def] >>
qexists_tac `vvs ++ [xv]` >>
qexists_tac `TL junk` >>
fs (get_retract_thms()) >>
`LENGTH vs = LENGTH vvs` by metis_tac[LIST_REL_LENGTH] >>
Cases_on `junk:v list` >-(fs[LENGTH_NIL]) >>
`vvs++[h]++t = vvs++h::t` by rw[] >>
POP_ASSUM (fn x => fs[x, LUPDATE_LENGTH])
QED
val eq_num_v_thm =
mlbasicsProgTheory.eq_v_thm
|> DISCH_ALL
|> C MATCH_MP (EqualityType_NUM_BOOL |> CONJUNCT1);
Theorem pop_spec:
!qv.
EqualityType A ==>
app (p:'ffi ffi_proj) ^(fetch_v "pop" st) [qv]
(STACK A vs qv)
(POSTve (\v. &(not(NULL vs) /\ A (LAST vs) v) * STACK A (FRONT vs) qv)
(\e. &(NULL vs /\ EmptyStack_exn e) * STACK A vs qv))
Proof
rpt strip_tac >>
xcf "pop" st >>
simp[STACK_def] >>
xpull >>
xlet_auto >-(xsimpl)>>
xmatch >>
reverse(rw[]) >- EVAL_TAC >>
xlet_auto >-(xsimpl) >>
xif
>-(
xlet_auto >-(xcon >> xsimpl) >>
xraise >>
xsimpl >>
fs[EmptyStack_exn_def] >>
rw[] >>
irule FALSITY >>
fs[computeLib.EVAL_CONV ``not T``]
) >>
xlet_auto >-(xsimpl) >>
xlet_auto
>-(
xsimpl >>
`vvs <> []` by metis_tac[LIST_REL_LENGTH, LENGTH_NIL] >>
`LENGTH vs <> 0 /\ LENGTH vvs <> 0` by metis_tac[LENGTH_NIL] >>
`LENGTH vs = LENGTH vvs` by metis_tac[LIST_REL_LENGTH] >>
bossLib.DECIDE_TAC
) >>
xlet_auto >-(xsimpl) >>
xlet_auto >-(xcon >> xsimpl) >>
xlet_auto >-(xsimpl) >>
xvar
>-(
xsimpl >>
qexists_tac `FRONT vvs` >>
qexists_tac `[LAST vvs] ++ junk` >>
fs[mlbasicsProgTheory.not_def, NULL_EQ] >>
`vvs <> [] /\ LENGTH vs <> 0 /\ LENGTH vvs <> 0` by metis_tac[LIST_REL_LENGTH, LENGTH_NIL] >>
`LENGTH vs = LENGTH vvs` by metis_tac[LIST_REL_LENGTH] >>
FIRST_ASSUM (fn x => PURE_REWRITE_TAC[x]) >>
`LENGTH vvs - 1 = PRE(LENGTH vvs)` by rw[] >>
FIRST_ASSUM (fn x => fs[x]) >>
fs[EL_APPEND_EQN, GSYM LAST_EL, LIST_REL_FRONT_LAST] >>
fs[APPEND_FRONT_LAST] >>
rw[LENGTH_FRONT] >>
`PRE(LENGTH vvs) = LENGTH vvs - 1` by rw[] >>
POP_ASSUM(fn x => fs[x]) >>
fs[NUM_def, int_arithTheory.INT_NUM_SUB]
) >>
xsimpl >>
rw[] >>
fs[NULL_EQ]
QED
val _ = export_theory ()