Skip to content

Commit f262e9c

Browse files
strubGustavo2622
andcommitted
feat(unroll-for): propagate constants after unrolling
Refactor unroll-for and add a variant that runs constant folding immediately after unrolling to propagate constants introduced by the transformation. Co-Authored-By: Gustavo Delerue <gxdelerue@proton.me> Co-Authored-By: Pierre-Yves Strub <pierre-yves@strub.nu>
1 parent 6f80687 commit f262e9c

4 files changed

Lines changed: 20 additions & 14 deletions

File tree

src/ecParser.mly

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3019,8 +3019,11 @@ interleave_info:
30193019
| FUSION s=side? o=codepos NOT i=word AT d1=word COMMA d2=word
30203020
{ Pfusion (s, o, (i, (d1, d2))) }
30213021

3022-
| UNROLL b=boption(FOR) s=side? o=codepos
3023-
{ Punroll (s, o, b) }
3022+
| UNROLL s=side? o=codepos
3023+
{ Punroll (s, o, `While) }
3024+
3025+
| UNROLL FOR b=boption(STAR) s=side? o=codepos
3026+
{ Punroll (s, o, `For (not b)) }
30243027

30253028
| SPLITWHILE s=side? o=codepos COLON c=expr %prec prec_tactic
30263029
{ Psplitwhile (c, s, o) }

src/ecParsetree.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ type phltactic =
731731
| Pasyncwhile of async_while_info
732732
| Pfission of (oside * pcodepos * (int * (int * int)))
733733
| Pfusion of (oside * pcodepos * (int * (int * int)))
734-
| Punroll of (oside * pcodepos * bool)
734+
| Punroll of (oside * pcodepos * [`While | `For of bool])
735735
| Psplitwhile of (pexpr * oside * pcodepos)
736736
| Pcall of oside * call_info gppterm
737737
| Pcallconcave of (pformula * call_info gppterm)

src/phl/ecPhlLoopTx.ml

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module TTC = EcProofTyping
2020
(* -------------------------------------------------------------------- *)
2121
type fission_t = oside * pcodepos * (int * (int * int))
2222
type fusion_t = oside * pcodepos * (int * (int * int))
23-
type unroll_t = oside * pcodepos * bool
23+
type unroll_t = oside * pcodepos * [`While | `For of bool]
2424
type splitwhile_t = pexpr * oside * pcodepos
2525

2626
(* -------------------------------------------------------------------- *)
@@ -220,7 +220,7 @@ let process_splitwhile (b, side, cpos) tc =
220220
t_splitwhile b side cpos tc
221221

222222
(* -------------------------------------------------------------------- *)
223-
let process_unroll_for side cpos tc =
223+
let process_unroll_for ~cfold side cpos tc =
224224
let env = FApi.tc1_env tc in
225225
let hyps = FApi.tc1_hyps tc in
226226
let (goal_m, _), c = EcLowPhlGoal.tc1_get_stmt side tc in
@@ -327,16 +327,19 @@ let process_unroll_for side cpos tc =
327327
let tcenv = t_doit 0 pos zs tc in
328328
let tcenv = FApi.t_onalli doi tcenv in
329329

330-
let cpos = EcMatching.Position.shift ~offset:(-1) cpos in
331-
let clen = blen * (List.length zs - 1) in
330+
if cfold then begin
331+
let cpos = EcMatching.Position.shift ~offset:(-1) cpos in
332+
let clen = blen * (List.length zs - 1) in
332333

333-
FApi.t_last (EcPhlCodeTx.t_cfold side cpos (Some clen)) tcenv
334+
FApi.t_last (EcPhlCodeTx.t_cfold side cpos (Some clen)) tcenv
335+
end else tcenv
334336

335337
(* -------------------------------------------------------------------- *)
336338
let process_unroll (side, cpos, for_) tc =
337-
if for_ then
338-
process_unroll_for side cpos tc
339-
else begin
339+
match for_ with
340+
| `While ->
340341
let cpos = EcLowPhlGoal.tc1_process_codepos tc (side, cpos) in
341342
t_unroll side cpos tc
342-
end
343+
344+
| `For cfold ->
345+
process_unroll_for ~cfold side cpos tc

src/phl/ecPhlLoopTx.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ val t_splitwhile : expr -> oside -> codepos -> backward
1313
(* -------------------------------------------------------------------- *)
1414
type fission_t = oside * pcodepos * (int * (int * int))
1515
type fusion_t = oside * pcodepos * (int * (int * int))
16-
type unroll_t = oside * pcodepos * bool
16+
type unroll_t = oside * pcodepos * [`While | `For of bool]
1717
type splitwhile_t = pexpr * oside * pcodepos
1818

19-
val process_unroll_for : oside -> pcodepos -> backward
19+
val process_unroll_for : cfold:bool -> oside -> pcodepos -> backward
2020
val process_fission : fission_t -> backward
2121
val process_fusion : fusion_t -> backward
2222
val process_unroll : unroll_t -> backward

0 commit comments

Comments
 (0)