-
Notifications
You must be signed in to change notification settings - Fork 16
/
Clase8.WP.fst
267 lines (236 loc) · 8.42 KB
/
Clase8.WP.fst
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
module Clase8.WP
open FStar.Mul
type var = string
type state = var -> int
type cond = state -> prop
type expr =
| Var : var -> expr
| Const : int -> expr
| Plus : expr -> expr -> expr
| Minus : expr -> expr -> expr
| Times : expr -> expr -> expr
| Eq : expr -> expr -> expr
| Lt : expr -> expr -> expr
| Not : expr -> expr
noeq
type stmt =
| Assign : var -> expr -> stmt
| IfZ : expr -> stmt -> stmt -> stmt
| Seq : stmt -> stmt -> stmt
| Skip : stmt
| While : inv:cond -> expr -> stmt -> stmt
let rec eval_expr (s : state) (e : expr) : int =
match e with
| Var x -> s x
| Const n -> n
| Plus e1 e2 -> eval_expr s e1 + eval_expr s e2
| Minus e1 e2 -> eval_expr s e1 - eval_expr s e2
| Times e1 e2 -> eval_expr s e1 * eval_expr s e2
| Eq e1 e2 -> if eval_expr s e1 = eval_expr s e2 then 0 else 1
| Lt e1 e2 -> if eval_expr s e1 < eval_expr s e2 then 0 else 1
| Not e -> if eval_expr s e = 0 then 1 else 0
let override (#a:eqtype) (#b:Type) (f : a -> b) (x:a) (y:b) : a -> b =
fun z -> if z = x then y else f z
noeq
type runsto : (s0:state) -> (p:stmt) -> (s1:state) -> Type u#1 =
| R_Skip : s:state -> runsto s Skip s
| R_Assign : s:state -> x:var -> e:expr -> runsto s (Assign x e) (override s x (eval_expr s e))
| R_Seq :
#p:stmt -> #q:stmt ->
#s:state -> #t:state -> #u:state ->
runsto s p t ->
runsto t q u ->
runsto s (Seq p q) u
| R_IfZ_True :
#c:expr -> #t:stmt -> #e:stmt ->
#s:state -> #s':state -> runsto s t s' ->
squash (eval_expr s c == 0) ->
runsto s (IfZ c t e) s'
| R_IfZ_False :
#c:expr -> #t:stmt -> #e:stmt ->
#s:state -> #s':state -> runsto s e s' ->
squash (eval_expr s c =!= 0) ->
runsto s (IfZ c t e) s'
| R_While_True :
#inv:cond ->
#c:expr -> #b:stmt -> #s:state -> #s':state -> #s'':state ->
runsto s b s' ->
squash (eval_expr s c == 0) ->
runsto s' (While inv c b) s'' ->
runsto s (While inv c b) s''
| R_While_False :
#inv:cond -> #c:expr -> #b:stmt -> #s:state ->
squash (eval_expr s c =!= 0) ->
runsto s (While inv c b) s
noeq
type hoare : (pre:cond) -> (p:stmt) -> (post:cond) -> Type u#1 =
| H_Skip : pre:cond -> hoare pre Skip pre
| H_Seq :
#p:stmt -> #q:stmt ->
#pre:cond -> #mid:cond -> #post:cond ->
hoare pre p mid ->
hoare mid q post ->
hoare pre (Seq p q) post
// | H_If :
// | H_Assign :
| H_While :
#inv':cond -> #c:expr -> #b:stmt ->
inv:cond ->
hoare (fun s -> inv s /\ eval_expr s c == 0) b inv ->
hoare inv (While inv' c b) (fun s -> inv s /\ eval_expr s c =!= 0)
| H_Weaken :
#pre:cond -> #p:stmt -> #post:cond ->
pre':cond -> post':cond ->
hoare pre p post ->
squash (forall x. pre' x ==> pre x) ->
squash (forall x. post x ==> post' x) ->
hoare pre' p post'
| H_Pure :
#pre:cond -> #p:stmt -> #post:cond ->
pre0:prop ->
hoare (fun s -> pre s /\ pre0) p post ->
hoare (fun s -> pre s /\ pre0) p (fun s -> post s /\ pre0)
// Demostrar que esta regla es admisible, es decir, que
// podemos "asumir" que la tenemos para demostrar, pero no
// necesitamos analizarla cuando destruímos una prueba:
//
// | R_While :
// #c:expr -> #b:stmt ->
// #s:state -> #s':state ->
// runsto (IfZ c (Seq b (While c b)) Skip) s s' ->
// runsto (While c b) s s'
let r_while (#inv:cond) (#c:expr) (#b:stmt) (#s #s' : state)
(pf : runsto s (IfZ c (Seq b (While inv c b)) Skip) s')
: runsto s (While inv c b) s'
= admit()
let hoare_ok (p:stmt) (pre:cond) (post:cond) (s0 s1 : state) (e_pf : runsto s0 p s1) (pf : hoare pre p post)
: Lemma (requires pre s0)
(ensures post s1)
= admit()
type wp = cond -> cond
let hoare_strengthen_pre (pre pre' : cond) (p:stmt) (post:cond)
(_ : squash (forall x. pre' x ==> pre x))
(pf : hoare pre p post)
: hoare pre' p post
= H_Weaken pre' post pf () ()
let hoare_weaken_post (pre:cond) (p:stmt) (post post' : cond)
(_ : squash (forall x. post x ==> post' x))
(pf : hoare pre p post)
: hoare pre p post'
= H_Weaken pre post' pf () ()
(* Cómputo de WPs *)
let assign_wp (x:var) (e:expr) : wp =
admit()
let ite_wp (c:expr) (wp_t wp_e : wp) : wp =
admit()
let while_wp (inv:cond) (c:expr) (wp_b:wp) : wp =
fun post s ->
inv s
/\ (forall s. eval_expr s c == 0 /\ inv s ==> wp_b inv s) // invariante es induc
/\ (forall s'. inv s' /\ eval_expr s' c =!= 0 ==> post s') // al finalizar implica post
let rec cwp (p:stmt) : wp =
match p with
| Assign x e -> assign_wp x e
| Skip -> admit()
| Seq p q -> admit()
| IfZ c t e -> ite_wp c (cwp t) (cwp e)
| While inv c b -> while_wp inv c (cwp b)
(* Correctitud de WPs *)
let rec cwp_ok (p:stmt) (post : cond)
: (hoare (cwp p post) p post)
= match p with
| While inv c b ->
let wp_b = cwp b in
let pf_b : hoare (wp_b inv) b inv = cwp_ok b inv in
let inv_is_inductive : prop = forall s. inv s /\ eval_expr s c == 0 ==> wp_b inv s in
let implies_post : prop = forall s'. inv s' /\ eval_expr s' c =!= 0 ==> post s' in
let pf_b : hoare (fun s -> (inv s /\ (inv_is_inductive /\ implies_post)) /\ eval_expr s c == 0)
b
(fun s -> inv s /\ (inv_is_inductive /\ implies_post))
=
H_Pure #(fun s -> inv s /\ eval_expr s c == 0) #b #inv
(inv_is_inductive /\ implies_post) (
hoare_strengthen_pre
(wp_b inv)
(fun s -> inv s /\ eval_expr s c == 0 /\ (inv_is_inductive /\ implies_post))
b
inv
()
pf_b)
|> hoare_strengthen_pre _ _ _ _ ()
in
let pf0 : hoare (fun s -> inv s /\ (inv_is_inductive /\ implies_post))
(While inv c b)
(fun s -> inv s /\ (inv_is_inductive /\ implies_post) /\ eval_expr s c =!= 0)
=
H_While #inv #c #b (fun s -> inv s /\ (inv_is_inductive /\ implies_post)) pf_b
in
let pf1 : hoare (while_wp inv c (cwp b) post)
(While inv c b)
post
=
H_Weaken _ _ pf0 () ()
in
pf1
| _ -> admit()
(* Agregar 1 a x. *)
let add1 =
Assign "x" (Plus (Var "x") (Const 1))
let wp_add1 : wp = cwp add1
//let _ = assert (forall s. s "x" = 10 <==> wp_add1 (fun s -> s "x" = 11) s)
(* DESCOMENTAR *)
(* Arriba garantizamos que la WP para x=11 es x=10. Debería andar luego de completar
definiciones. *)
(* Agregando 2 a x, mediante y. *)
let add2 =
Assign "y" (Plus (Var "x") (Const 1)) `Seq`
Assign "x" (Plus (Var "y") (Const 1))
let wp_add2 : wp = cwp add2
// let _ = assert (forall s. ?????????? <==> wp_add2 (fun s -> s "x" = 12) s)
(* Encontrar la WP para la postcondición x=12. ¿Qué pasa con y? *)
(* Intercambiando dos variables via una tercera. *)
let swap : stmt =
Assign "t" (Var "x") `Seq`
Assign "x" (Var "y") `Seq`
Assign "y" (Var "t")
let wp_swap : wp = cwp swap
(* Demuestre que el programa intercambia x e y, demostrando un teorema sobre
la WP *paramétrico* sobre x e y. *)
// let _ = assert (forall s x0 y0. ???????? ==> wp_swap (fun s -> s "x" = y0 /\ s "y" = x0) s)
(* Opcional: escriba el programa siguiente
x = x + y;
y = x - y;
x = x - y;
y demuestra que también intercambia los valores de x e y. *)
(* Mover x a y. *)
let move_x_y : stmt =
(* y := 0;
while (y < x)
y := y + 1 *)
Assign "y" (Const 0) `Seq`
While
(admit()) // invariante
(Lt (Var "y") (Var "x"))
(Assign "y" (Plus (Var "y") (Const 1)))
let wp_move_x_y : wp = cwp move_x_y
let pre_move = wp_move_x_y (fun s -> s "x" == s "y")
(* Encuentre la WP para la postcondición x=y. *)
// let _ = assert (forall s. ?????????? <==> pre_move s)
// let move_x_y_ok :
// hoare (fun s -> s "x" >= 0) move_x_y (fun s -> s "y" = s "x")
// = hoare_strengthen_pre _ _ _ _ () <| cwp_ok move_x_y (fun s -> s "y" = s "x")
(* Armando una tripleta a partir del lema anterior. Esto puede hacerse para
cada uno de los programas anteriores. Descomentar, debería andar. *)
(* Cuenta regresiva. Encuentre la WP para la postcondición x=0. ¿Cuál invariante debe usar? *)
let countdown_inv : cond = admit()
let countdown : stmt =
While
countdown_inv
(Not (Eq (Var "x") (Const 0)))
(Assign "x" (Plus (Var "x") (Const (-1))))
let wp_countdown : wp = cwp countdown
let pre_countdown = wp_countdown (fun s -> s "x" == 0)
let monotonia (p:stmt) (q1 q2 : cond)
: Lemma (requires forall s. q1 s ==> q2 s)
(ensures forall s. cwp p q1 s ==> cwp p q2 s)
= admit()