Skip to content

Commit 9ee04bf

Browse files
committed
Proc change multiline to range + deep code position
1 parent 777719d commit 9ee04bf

File tree

7 files changed

+103
-53
lines changed

7 files changed

+103
-53
lines changed

src/ecMatching.ml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,14 @@ module Zipper = struct
268268
end
269269
| `Offset cp1 -> zpr, cp1
270270

271+
let zipper_and_split_of_cpos_range env cpr s =
272+
let zpr, cp = zipper_of_cpos_range env cpr s in
273+
match zpr.z_tail with
274+
| [] -> raise InvalidCPos
275+
| i :: tl ->
276+
let s, tl = split_at_cpos1 ~after:`Auto env cp (stmt tl) in
277+
(zpr, cp), ((i::s), tl)
278+
271279
let split_at_cpos1 env cpos1 s =
272280
split_at_cpos1 ~after:`Auto env cpos1 s
273281

@@ -308,14 +316,10 @@ module Zipper = struct
308316
List.rev after
309317

310318
let fold_range env cenv cpr f state s =
311-
let zpr, cp = zipper_of_cpos_range env cpr s in
312-
match zpr.z_tail with
313-
| [] -> raise InvalidCPos
314-
| i :: tl ->
315-
let s, tl = split_at_cpos1 env cp (stmt tl) in
316-
let env = odfl env zpr.z_env in
317-
let state', si' = f env cenv state (i :: s) in
318-
state', zip { zpr with z_tail = si' @ tl }
319+
let (zpr, _), (s, tl) = zipper_and_split_of_cpos_range env cpr s in
320+
let env = odfl env zpr.z_env in
321+
let state', si' = f env cenv state s in
322+
state', zip { zpr with z_tail = si' @ tl }
319323

320324
let map_range env cpr f s =
321325
snd (fold_range env () cpr (fun env () _ si -> (), f env si) () s)

src/ecMatching.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ module Zipper : sig
9999
* Raise [InvalidCPos] if [codepos_range] is not a valid range for [stmt].
100100
*)
101101
val zipper_of_cpos_range : env -> codepos_range -> stmt -> zipper * codepos1
102+
val zipper_and_split_of_cpos_range : env -> codepos_range -> stmt -> (zipper * codepos1) * (instr list * instr list)
102103

103104
(* Zip the zipper, returning the corresponding statement *)
104105
val zip : zipper -> stmt

src/ecParser.mly

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3194,10 +3194,8 @@ interleave_info:
31943194
| LOSSLESS
31953195
{ Plossless }
31963196

3197-
| PROC CHANGE side=side? pos=loc(codepos) offset=codeoffset1 COLON s=brace(stmt)
3198-
{ if not (List.is_empty (fst (unloc pos))) then
3199-
parse_error (loc pos) (Some "only top-level positions are supported");
3200-
Pchangestmt (side, (snd (unloc pos), offset), s) }
3197+
| PROC CHANGE side=side? pos=loc(codepos_or_range) COLON s=brace(stmt)
3198+
{ Pchangestmt (side, (unloc pos), s) }
32013199

32023200
| PROC REWRITE side=side? pos=codepos f=pterm
32033201
{ Pprocrewrite (side, pos, `Rw f) }

src/ecParsetree.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,9 +770,9 @@ type phltactic =
770770
| Prw_equiv of rw_eqv_info
771771
| Psymmetry
772772
| Pbdhoare_split of bdh_split
773-
| Pchangestmt of side option * (pcodepos1 * pcodeoffset1) * pstmt
774-
| Pprocrewrite of side option * pcodepos * prrewrite
775773
| Prwprgm of rwprgm
774+
| Pprocrewrite of side option * pcodepos * prrewrite
775+
| Pchangestmt of side option * pcodepos_range * pstmt
776776

777777

778778
(* Eager *)

src/phl/ecPhlRewrite.ml

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,14 @@ let process_rewrite
172172
(* -------------------------------------------------------------------- *)
173173
let t_change_stmt
174174
(side : side option)
175-
(hd, stmt, tl : instr list * instr list * instr list)
175+
(pos : EcMatching.Position.codepos_range)
176176
(s : stmt)
177177
(tc : tcenv1)
178178
=
179179
let env = FApi.tc1_env tc in
180-
let me, _ = EcLowPhlGoal.tc1_get_stmt side tc in
180+
let me, stmt = EcLowPhlGoal.tc1_get_stmt side tc in
181+
182+
let (zpr, _), (stmt, epilog) = EcMatching.Zipper.zipper_and_split_of_cpos_range env pos stmt in
181183

182184
let pvs = EcPV.is_write env (stmt @ s.s_node) in
183185
let pvs, globs = EcPV.PV.elements pvs in
@@ -187,8 +189,8 @@ let t_change_stmt
187189
(EcPV.is_read env s.s_node)
188190
in
189191

190-
let mleft = EcIdent.create "1" in (* FIXME: PR: is this how we want to do this? *)
191-
let mright = EcIdent.create "2" in
192+
let mleft = EcIdent.create "&1" in (* FIXME: PR: is this how we want to do this? *)
193+
let mright = EcIdent.create "&2" in
192194

193195
let eq =
194196
List.map
@@ -217,17 +219,19 @@ let t_change_stmt
217219
{ml=mleft; mr=mright; inv=f_ands eq}
218220
in
219221

222+
let stmt = EcMatching.Zipper.zip { zpr with z_tail = s.s_node @ epilog } in
223+
220224
let goal2 =
221225
EcLowPhlGoal.hl_set_stmt
222226
side (FApi.tc1_goal tc)
223-
(EcAst.stmt (List.flatten [hd; s.s_node; tl])) in
227+
stmt in
224228

225229
FApi.xmutate1 tc `ProcChangeStmt [goal1; goal2]
226230

227231
(* -------------------------------------------------------------------- *)
228232
let process_change_stmt
229233
(side : side option)
230-
((p, o) : pcodepos1 * pcodeoffset1)
234+
(pos : pcodepos_range)
231235
(s : pstmt)
232236
(tc : tcenv1)
233237
=
@@ -249,30 +253,16 @@ let process_change_stmt
249253
| _ -> tc_error !!tc "Wrong goal shape, expecting hoare or equiv goal with inlined code"
250254
end;
251255

252-
let me, stmt = EcLowPhlGoal.tc1_get_stmt side tc in
253-
254-
let p, o =
255-
let env = EcEnv.Memory.push_active_ss me env in
256-
let pos = EcTyping.trans_codepos1 ~memory:(fst me) env p in
257-
let off = EcTyping.trans_codeoffset1 ~memory:(fst me) env o in
258-
let off = EcMatching.Position.resolve_offset ~base:pos ~offset:off in
259-
260-
let start = EcMatching.Zipper.offset_of_position env pos stmt in
261-
let end_ = EcMatching.Zipper.offset_of_position env off stmt in
262-
263-
if (end_ < start) then
264-
tc_error !!tc "end position cannot be before start position";
256+
let me, _ = EcLowPhlGoal.tc1_get_stmt side tc in
265257

266-
(start - 1, end_ - start)
258+
let pos =
259+
let env = EcEnv.Memory.push_active_ss me env in
260+
EcTyping.trans_codepos_range ~memory:(fst me) env pos
267261
in
268262

269-
let stmt = stmt.s_node in
270-
let hd, stmt = List.takedrop p stmt in
271-
let stmt, tl = List.takedrop o stmt in
272-
273263
let s = match side with
274264
| Some side -> EcProofTyping.tc1_process_prhl_stmt tc side s
275265
| None -> EcProofTyping.tc1_process_Xhl_stmt tc s
276266
in
277267

278-
t_change_stmt side (hd, stmt, tl) s tc
268+
t_change_stmt side pos s tc

src/phl/ecPhlRewrite.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ val process_change : side option -> pcodepos -> pexpr -> backward
88
val process_rewrite_rw : side option -> pcodepos -> ppterm -> backward
99
val process_rewrite_simpl : side option -> pcodepos -> backward
1010
val process_rewrite : side option -> pcodepos -> prrewrite -> backward
11-
val process_change_stmt : side option -> pcodepos1 * pcodeoffset1 -> pstmt -> backward
11+
val process_change_stmt : side option -> pcodepos_range -> pstmt -> backward

tests/procchange.ec

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ require import AllCore Distr.
44
theory ProcChangeAssignEquiv.
55
module M = {
66
proc f(x : int) = {
7-
x <- x + 0;
7+
x <- 0;
8+
x <- x + 1;
9+
x <- x + 2;
10+
x <- x + 3;
811
}
912
}.
1013

1114
lemma L : equiv[M.f ~ M.f: true ==> true].
1215
proof.
1316
proc.
14-
proc change {1} 1 1 : { x <- x; }.
17+
proc change {1} [1..3] : { x <- 3; }.
1518

1619
wp. skip. smt().
1720
abort.
@@ -27,7 +30,7 @@ theory ProcChangeAssignHoareEquiv.
2730
lemma L : hoare[M.f : true ==> true].
2831
proof.
2932
proc.
30-
proc change 1 1 : { x <- x ; }. wp. skip. smt().
33+
proc change [1..1] : { x <- x ; }. wp. skip. smt().
3134
abort.
3235
end ProcChangeAssignHoareEquiv.
3336

@@ -42,7 +45,7 @@ theory ProcChangeSampleEquiv.
4245
lemma L : equiv[M.f ~ M.f : true ==> true].
4346
proof.
4447
proc.
45-
proc change {1} 1 1 : { x <$ (dunit x); }.
48+
proc change {1} [1..1] : { x <$ (dunit x); }.
4649
rnd. skip. smt().
4750
abort.
4851
end ProcChangeSampleEquiv.
@@ -62,7 +65,7 @@ theory ProcChangeIfEquiv.
6265
lemma L : equiv[M.f ~ M.f : true ==> true].
6366
proof.
6467
proc.
65-
proc change {1} 1 1 : {
68+
proc change {1} [1..1] : {
6669
if (x = y) {
6770
x <- y;
6871
} else {
@@ -85,15 +88,47 @@ theory ProcChangeWhileEquiv.
8588
lemma L : equiv[M.f ~ M.f : true ==> true].
8689
proof.
8790
proc.
88-
proc change {1} 1 1 : {
91+
proc change {1} [1..1] : {
8992
while (x <> y) {
90-
x <- x + 1;
93+
x <- x + 1 + 0;
9194
}
9295
}.
93-
admit. (* FIXME *)
96+
proc rewrite {1} 1 /=.
97+
proc rewrite {2} 1.1 /=.
98+
sim.
9499
abort.
95100
end ProcChangeWhileEquiv.
96101

102+
103+
(* -------------------------------------------------------------------- *)
104+
theory ProcChangeInWhileEquiv.
105+
module M = {
106+
proc f(x : int, y : int) = {
107+
while (x + 0 <> y) {
108+
x <- 1;
109+
x <- x + 1;
110+
x <- 2;
111+
}
112+
}
113+
}.
114+
115+
lemma L : equiv[M.f ~ M.f : true ==> true].
116+
proof.
117+
proc.
118+
proc change {1} ^while.2 : {
119+
x <- x + 0 + 1;
120+
}.
121+
wp; skip. smt().
122+
proc change {1} [^while.1..^while.2] : {
123+
x <- 2;
124+
}. wp; skip. smt().
125+
proc change {2} [^while.1-1] : {
126+
x <- 2;
127+
}. wp; skip. smt().
128+
abort.
129+
end ProcChangeInWhileEquiv.
130+
131+
97132
(* -------------------------------------------------------------------- *)
98133
theory ProcChangeAssignHoare.
99134
module M = {
@@ -105,7 +140,7 @@ theory ProcChangeAssignHoare.
105140
lemma L : hoare[M.f: true ==> true].
106141
proof.
107142
proc.
108-
proc change 1 1 : { x <- x; }.
143+
proc change [1..1] : { x <- x; }.
109144
wp; skip; smt().
110145
abort.
111146
end ProcChangeAssignHoare.
@@ -121,7 +156,7 @@ theory ProcChangeSampleHoare.
121156
lemma L : hoare[M.f: true ==> true].
122157
proof.
123158
proc.
124-
proc change 1 1 : { x <$ (dunit x); }.
159+
proc change 1 : { x <$ (dunit x); }.
125160
rnd; skip; smt().
126161
abort.
127162
end ProcChangeSampleHoare.
@@ -141,7 +176,7 @@ theory ProcChangeIfHoare.
141176
lemma L : hoare[M.f: true ==> true].
142177
proof.
143178
proc.
144-
proc change 1 1 : {
179+
proc change 1 : {
145180
if (x = y) {
146181
x <- y;
147182
} else {
@@ -164,11 +199,33 @@ theory ProcChangeWhileHoare.
164199
lemma L : hoare[M.f: true ==> true].
165200
proof.
166201
proc.
167-
proc change 1 1 : {
202+
proc change 1 : {
168203
while (x <> y) {
169204
x <- x + 1;
170205
}
171206
}.
172-
admit. (* FIXME *)
207+
proc rewrite {1} ^while /=; sim.
173208
abort.
174209
end ProcChangeWhileHoare.
210+
211+
212+
(* -------------------------------------------------------------------- *)
213+
theory ProcChangeInWhileHoare.
214+
module M = {
215+
proc f(x : int, y : int) = {
216+
while (x + 0 <> y) {
217+
x <- x + 1;
218+
}
219+
}
220+
}.
221+
222+
lemma L : hoare[M.f : true ==> true].
223+
proof.
224+
proc.
225+
proc change ^while.1 : {
226+
x <- x + 0 + 1;
227+
}.
228+
wp; skip. smt().
229+
abort.
230+
end ProcChangeInWhileHoare.
231+

0 commit comments

Comments
 (0)