Skip to content

Commit 634cefb

Browse files
committed
Add exception declaration, raise, and update logic
1 parent 48a21c3 commit 634cefb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1153
-393
lines changed

examples/exception.ec

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
require import AllCore.
2+
3+
exception assume.
4+
exception assert.
5+
6+
module M' ={
7+
proc truc (x:int) : int = {
8+
if (x = 3) {
9+
raise assume;
10+
}
11+
if (x=3) {
12+
raise assert;
13+
}
14+
return x;
15+
}
16+
}.
17+
18+
op p7: bool.
19+
op p1: bool.
20+
op p2: bool.
21+
op p3: bool.
22+
23+
lemma assume_assert :
24+
hoare [M'.truc : p7 ==> p3 | assume:p1 |assert: p2 ].
25+
proof.
26+
admitted.
27+
28+
op p8: bool.
29+
op q1: bool.
30+
op q2: bool.
31+
op q3: bool.
32+
33+
lemma assert_assume :
34+
hoare [M'.truc : p8 ==> q3 | assume:q1 |assert: q2 ].
35+
proof.
36+
admitted.
37+
38+
op p4: bool.
39+
op p5: bool.
40+
op p6: bool.
41+
op p9: bool.
42+
lemma assert_assume' :
43+
hoare [M'.truc : p9 ==> p4 | assume:p6 |assert: p5 ].
44+
proof.
45+
conseq (assume_assert) (assert_assume).
46+
+ admit.
47+
+ admit.
48+
+ admit.
49+
+ admit.
50+
qed.
51+
52+
exception e1.
53+
exception e2.
54+
exception e3.
55+
56+
module M ={
57+
proc f1 (x:int) : int = {
58+
if (x = 3) {
59+
raise e1;
60+
} else {
61+
x <- 5;
62+
}
63+
return x;
64+
}
65+
66+
proc f2 (x:int) : int = {
67+
if (x = 3) {
68+
x <- x;
69+
x <@ f1(x);
70+
} else {
71+
x <@ f1(x);
72+
}
73+
return x;
74+
}
75+
}.
76+
77+
78+
(* Multiple time post for the same exception *)
79+
80+
lemma l_f1 (_x: int):
81+
hoare [M.f1 : _x = x ==> (4 < res) | e1:_x = 3 | e2:false ].
82+
proof.
83+
proc.
84+
conseq (: _ ==> x = 5 | e1: p1 | e2: p2).
85+
+ auto.
86+
+ auto. admit.
87+
+ admit.
88+
+ admit.
89+
qed.
90+
91+
lemma l_f2 (_x: int):
92+
hoare [M.f2 : p9 ==> p4 | e1: p2 | e2:p1 | e3: p6 ].
93+
proof.
94+
proc.
95+
if.
96+
+ call (: p5 ==> p3 | e1 : p9 | e2: p7).
97+
proc.
98+
wp.
99+
admit.
100+
+ admit.
101+
+ call (l_f1 _x);auto. admit.
102+
qed.

src/ecAst.ml

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ and instr_node =
115115
| Sif of expr * stmt * stmt
116116
| Swhile of expr * stmt
117117
| Smatch of expr * ((EcIdent.t * ty) list * stmt) list
118-
| Sassert of expr
118+
| Sraise of EcPath.path * expr list
119119
| Sabstract of EcIdent.t
120120

121121
and stmt = {
@@ -230,18 +230,22 @@ and equivS = {
230230
es_sr : stmt;
231231
es_po : form; }
232232

233+
and post = (EcPath.path * form) list
234+
233235
and sHoareF = {
234236
hf_pr : form;
235237
hf_f : EcPath.xpath;
236238
hf_po : form;
239+
hf_poe : post
237240
}
238241

239242
and sHoareS = {
240-
hs_m : memenv;
241-
hs_pr : form;
242-
hs_s : stmt;
243-
hs_po : form; }
244-
243+
hs_m : memenv;
244+
hs_pr : form;
245+
hs_s : stmt;
246+
hs_po : form;
247+
hs_poe : post
248+
}
245249

246250
and eHoareF = {
247251
ehf_pr : form;
@@ -615,13 +619,25 @@ let b_hash (bs : bindings) =
615619
Why3.Hashcons.combine_list b1_hash 0 bs
616620

617621
(*-------------------------------------------------------------------- *)
622+
623+
let post_equal (e1, f1) (e2,f2) =
624+
EcPath.p_equal e1 e2 &&
625+
f_equal f1 f2
626+
627+
let posts_equal posts1 posts2 =
628+
List.equal post_equal posts1 posts2
629+
630+
(*-------------------------------------------------------------------- *)
631+
618632
let hf_equal hf1 hf2 =
619633
f_equal hf1.hf_pr hf2.hf_pr
634+
&& posts_equal hf1.hf_poe hf2.hf_poe
620635
&& f_equal hf1.hf_po hf2.hf_po
621636
&& EcPath.x_equal hf1.hf_f hf2.hf_f
622637

623638
let hs_equal hs1 hs2 =
624639
f_equal hs1.hs_pr hs2.hs_pr
640+
&& posts_equal hs1.hs_poe hs2.hs_poe
625641
&& f_equal hs1.hs_po hs2.hs_po
626642
&& s_equal hs1.hs_s hs2.hs_s
627643
&& me_equal hs1.hs_m hs2.hs_m
@@ -680,14 +696,27 @@ let pr_equal pr1 pr2 =
680696
&& f_equal pr1.pr_event pr2.pr_event
681697
&& f_equal pr1.pr_args pr2.pr_args
682698

699+
(*-------------------------------------------------------------------- *)
700+
701+
let post_hash (e, f) =
702+
Why3.Hashcons.combine
703+
(EcPath.p_hash e)
704+
(f_hash f)
705+
706+
let posts_hash posts =
707+
Why3.Hashcons.combine_list post_hash 0 posts
708+
683709
(* -------------------------------------------------------------------- *)
684710
let hf_hash hf =
685711
Why3.Hashcons.combine2
686-
(f_hash hf.hf_pr) (f_hash hf.hf_po) (EcPath.x_hash hf.hf_f)
712+
(f_hash hf.hf_pr)
713+
(Why3.Hashcons.combine (f_hash hf.hf_po) (posts_hash hf.hf_poe))
714+
(EcPath.x_hash hf.hf_f)
687715

688716
let hs_hash hs =
689717
Why3.Hashcons.combine3
690-
(f_hash hs.hs_pr) (f_hash hs.hs_po)
718+
(f_hash hs.hs_pr)
719+
(Why3.Hashcons.combine (f_hash hs.hs_po) (posts_hash hs.hs_poe))
691720
(s_hash hs.hs_s)
692721
(me_hash hs.hs_m)
693722

@@ -1036,6 +1065,11 @@ module Hsform = Why3.Hashcons.Make (struct
10361065

10371066
let fv_mlr = Sid.add mleft (Sid.singleton mright)
10381067

1068+
let posts_fv init posts =
1069+
List.fold
1070+
(fun acc (_,f) -> fv_union (f_fv f) acc)
1071+
init posts
1072+
10391073
let fv_node f =
10401074
let union ex nodes =
10411075
List.fold_left (fun s a -> fv_union s (ex a)) Mid.empty nodes
@@ -1063,11 +1097,13 @@ module Hsform = Why3.Hashcons.Make (struct
10631097
fv_union (f_fv f1) fv2
10641098

10651099
| FhoareF hf ->
1066-
let fv = fv_union (f_fv hf.hf_pr) (f_fv hf.hf_po) in
1100+
let fv = f_fv hf.hf_po in
1101+
let fv = fv_union (f_fv hf.hf_pr) (posts_fv fv hf.hf_poe) in
10671102
EcPath.x_fv (Mid.remove mhr fv) hf.hf_f
10681103

10691104
| FhoareS hs ->
1070-
let fv = fv_union (f_fv hs.hs_pr) (f_fv hs.hs_po) in
1105+
let fv = f_fv hs.hs_po in
1106+
let fv = fv_union (f_fv hs.hs_pr) (posts_fv fv hs.hs_poe) in
10711107
fv_union (s_fv hs.hs_s) (Mid.remove (fst hs.hs_m) fv)
10721108

10731109
| FeHoareF hf ->
@@ -1162,8 +1198,9 @@ module Hinstr = Why3.Hashcons.Make (struct
11621198
in List.all2 forbs bs1 bs2 && s_equal s1 s2
11631199
in e_equal e1 e2 && List.all2 forb b1 b2
11641200

1165-
| Sassert e1, Sassert e2 ->
1166-
(e_equal e1 e2)
1201+
| Sraise (e1, es1), Sraise (e2, es2) ->
1202+
(EcPath.p_equal e1 e2)
1203+
&& (List.all2 e_equal es1 es2)
11671204

11681205
| Sabstract id1, Sabstract id2 -> EcIdent.id_equal id1 id2
11691206

@@ -1201,7 +1238,10 @@ module Hinstr = Why3.Hashcons.Make (struct
12011238
in Why3.Hashcons.combine_list forbs (s_hash s) bds
12021239
in Why3.Hashcons.combine_list forb (e_hash e) b
12031240

1204-
| Sassert e -> e_hash e
1241+
| Sraise (e, tys) ->
1242+
Why3.Hashcons.combine_list e_hash
1243+
(EcPath.p_hash e)
1244+
tys
12051245

12061246
| Sabstract id -> EcIdent.id_hash id
12071247

@@ -1235,8 +1275,10 @@ module Hinstr = Why3.Hashcons.Make (struct
12351275
(fun s b -> EcIdent.fv_union s (forb b))
12361276
(e_fv e) b
12371277

1238-
| Sassert e ->
1239-
e_fv e
1278+
| Sraise (_, args) ->
1279+
List.fold_left
1280+
(fun s a -> EcIdent.fv_union s (e_fv a))
1281+
Mid.empty args
12401282

12411283
| Sabstract id ->
12421284
EcIdent.fv_singleton id

src/ecAst.mli

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ and instr_node =
111111
| Sif of expr * stmt * stmt
112112
| Swhile of expr * stmt
113113
| Smatch of expr * ((EcIdent.t * ty) list * stmt) list
114-
| Sassert of expr
114+
| Sraise of EcPath.path * expr list
115115
| Sabstract of EcIdent.t
116116

117117
and stmt = private {
@@ -225,17 +225,22 @@ and equivS = {
225225
es_sr : stmt;
226226
es_po : form; }
227227

228+
and post = (EcPath.path * form) list
229+
228230
and sHoareF = {
229231
hf_pr : form;
230232
hf_f : EcPath.xpath;
231233
hf_po : form;
234+
hf_poe : post;
232235
}
233236

234237
and sHoareS = {
235-
hs_m : memenv;
236-
hs_pr : form;
237-
hs_s : stmt;
238-
hs_po : form; }
238+
hs_m : memenv;
239+
hs_pr : form;
240+
hs_s : stmt;
241+
hs_po : form;
242+
hs_poe : post;
243+
}
239244

240245

241246
and eHoareF = {

src/ecCallbyValue.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ and try_reduce_record_projection
222222

223223
try
224224
if not (
225-
st.st_ri.iota
225+
st.st_ri.iota
226226
&& EcEnv.Op.is_projection st.st_env p
227227
&& not (Args.isempty args)
228228
) then raise Bailout;
@@ -483,7 +483,8 @@ and cbv (st : state) (s : subst) (f : form) (args : args) : form =
483483
let hf_pr = norm st s hf.hf_pr in
484484
let hf_po = norm st s hf.hf_po in
485485
let hf_f = norm_xfun st s hf.hf_f in
486-
f_hoareF_r { hf_pr; hf_f; hf_po }
486+
let hf_poe = List.map (fun (e,f) -> e, norm st s f) hf.hf_poe in
487+
f_hoareF_r { hf_pr; hf_f; hf_po; hf_poe}
487488

488489
| FhoareS hs ->
489490
assert (Args.isempty args);
@@ -492,7 +493,8 @@ and cbv (st : state) (s : subst) (f : form) (args : args) : form =
492493
let hs_po = norm st s hs.hs_po in
493494
let hs_s = norm_stmt s hs.hs_s in
494495
let hs_m = norm_me s hs.hs_m in
495-
f_hoareS_r { hs_pr; hs_po; hs_s; hs_m }
496+
let hs_poe = List.map (fun (e,f) -> e, norm st s f) hs.hs_poe in
497+
f_hoareS_r { hs_pr; hs_po; hs_s; hs_m; hs_poe}
496498

497499
| FeHoareF hf ->
498500
assert (Args.isempty args);

0 commit comments

Comments
 (0)