Skip to content

Commit 01d4e41

Browse files
authored
reduce when needed in proc and call rules (#849)
* reduce in t_hF_or_bhF_or_eF * add reduction in call tactic
1 parent 9f8d176 commit 01d4e41

22 files changed

+224
-142
lines changed

src/ecLowGoal.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ let t_hred_with_info ?target (ri : reduction_info) (tc : tcenv1) =
406406
FApi.tcenv_of_tcenv1 (t_change_r ~fail:true ?target action tc)
407407

408408
(* -------------------------------------------------------------------- *)
409-
let rec t_lazy_match ?(reduce = `Full) (tx : form -> FApi.backward)
409+
let rec t_lazy_match ?(reduce = `Full) ?(texn = fun _ -> raise InvalidGoalShape)
410+
(tx : form -> FApi.backward)
410411
(tc : tcenv1) =
411412
let concl = FApi.tc1_goal tc in
412413
try tx concl tc
@@ -416,7 +417,7 @@ let rec t_lazy_match ?(reduce = `Full) (tx : form -> FApi.backward)
416417
| `None -> raise InvalidGoalShape
417418
| `Full -> EcReduction.full_red
418419
| `NoDelta -> EcReduction.nodelta in
419-
FApi.t_seq (t_hred_with_info strategy) (t_lazy_match ~reduce tx) tc
420+
FApi.t_seq (FApi.t_or (t_hred_with_info strategy) texn) (t_lazy_match ~reduce tx) tc
420421
421422
(* -------------------------------------------------------------------- *)
422423
type smode = [ `Cbv | `Cbn ]
@@ -2598,8 +2599,8 @@ let t_solve ?(canfail = true) ?(bases = [EcEnv.Auto.dname]) ?(mode = fmdelta) ?(
25982599
let pt = PT.pt_of_uglobal !!tc (FApi.tc1_hyps tc) p in
25992600
try
26002601
Apply.t_apply_bwd_r ~ri ~mode ~canview:false pt tc
2601-
with Apply.NoInstance _ ->
2602-
t_fail tc
2602+
with Apply.NoInstance _ ->
2603+
t_fail tc
26032604
in
26042605
26052606
let rec t_apply ctn ip tc =

src/ecLowGoal.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ val t_change : ?ri:EcReduction.reduction_info -> ?target:ident -> form -> FApi.
6969

7070
(* -------------------------------------------------------------------- *)
7171
val t_lazy_match:
72-
?reduce:lazyred -> (form -> FApi.backward)-> FApi.backward
72+
?reduce:lazyred -> ?texn:EcCoreGoal.FApi.backward -> (form -> FApi.backward)-> FApi.backward
7373

7474
(* -------------------------------------------------------------------- *)
7575
val t_reflex : ?mode:[`Alpha | `Conv] -> ?reduce:lazyred -> FApi.backward
@@ -362,4 +362,4 @@ val pp_tc :tcenv -> unit
362362
[@@ocaml.alert debug "Debug function, remove uses before merging"]
363363

364364
val pp_tc1 :tcenv1 -> unit
365-
[@@ocaml.alert debug "Debug function, remove uses before merging"]
365+
[@@ocaml.alert debug "Debug function, remove uses before merging"]

src/ecLowPhlGoal.ml

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,27 @@ let tc1_get_stmt side tc =
206206
| _ ->
207207
tc_error_noXhl ~kinds:(hlkinds_Xhl_r `Stmt) !!tc
208208

209+
(* ------------------------------------------------------------------ *)
210+
let tc1_process_codepos_range tc (side, cpr) =
211+
let me, _ = tc1_get_stmt side tc in
212+
let env = FApi.tc1_env tc in
213+
let env = EcEnv.Memory.push_active_ss me env in
214+
EcTyping.trans_codepos_range env cpr
215+
216+
(* ------------------------------------------------------------------ *)
217+
let tc1_process_codepos tc (side, cpos) =
218+
let me, _ = tc1_get_stmt side tc in
219+
let env = FApi.tc1_env tc in
220+
let env = EcEnv.Memory.push_active_ss me env in
221+
EcTyping.trans_codepos env cpos
222+
223+
(* ------------------------------------------------------------------ *)
224+
let tc1_process_codepos1 tc (side, cpos) =
225+
let me, _ = tc1_get_stmt side tc in
226+
let env = FApi.tc1_env tc in
227+
let env = EcEnv.Memory.push_active_ss me env in
228+
EcTyping.trans_codepos1 env cpos
229+
209230
(* -------------------------------------------------------------------- *)
210231
let hl_set_stmt (side : side option) (f : form) (s : stmt) =
211232
match side, f.f_node with
@@ -256,28 +277,28 @@ let tc1_get_post tc =
256277
(* -------------------------------------------------------------------- *)
257278
let set_pre ~pre f =
258279
match f.f_node, pre with
259-
| FhoareF hf, Inv_ss pre ->
280+
| FhoareF hf, Inv_ss pre ->
260281
let pre = ss_inv_rebind pre hf.hf_m in
261282
f_hoareF pre hf.hf_f (hf_po hf)
262-
| FhoareS hs, Inv_ss pre ->
283+
| FhoareS hs, Inv_ss pre ->
263284
let pre = ss_inv_rebind pre (fst hs.hs_m) in
264285
f_hoareS (snd hs.hs_m) pre hs.hs_s (hs_po hs)
265-
| FeHoareF hf, Inv_ss pre ->
286+
| FeHoareF hf, Inv_ss pre ->
266287
let pre = ss_inv_rebind pre hf.ehf_m in
267288
f_eHoareF pre hf.ehf_f (ehf_po hf)
268-
| FeHoareS hs, Inv_ss pre ->
289+
| FeHoareS hs, Inv_ss pre ->
269290
let pre = ss_inv_rebind pre (fst hs.ehs_m) in
270291
f_eHoareS (snd hs.ehs_m) pre hs.ehs_s (ehs_po hs)
271292
| FbdHoareF hf, Inv_ss pre ->
272293
let pre = ss_inv_rebind pre hf.bhf_m in
273294
f_bdHoareF pre hf.bhf_f (bhf_po hf) hf.bhf_cmp (bhf_bd hf)
274-
| FbdHoareS hs, Inv_ss pre ->
295+
| FbdHoareS hs, Inv_ss pre ->
275296
let pre = ss_inv_rebind pre (fst hs.bhs_m) in
276297
f_bdHoareS (snd hs.bhs_m) pre hs.bhs_s (bhs_po hs) hs.bhs_cmp (bhs_bd hs)
277-
| FequivF ef, Inv_ts pre ->
298+
| FequivF ef, Inv_ts pre ->
278299
let pre = ts_inv_rebind pre ef.ef_ml ef.ef_mr in
279300
f_equivF pre ef.ef_fl ef.ef_fr (ef_po ef)
280-
| FequivS es, Inv_ts pre ->
301+
| FequivS es, Inv_ts pre ->
281302
let pre = ts_inv_rebind pre (fst es.es_ml) (fst es.es_mr) in
282303
f_equivS (snd es.es_ml) (snd es.es_mr) pre es.es_sl es.es_sr (es_po es)
283304
| _ -> assert false
@@ -307,33 +328,33 @@ let t_hS_or_bhS_or_eS ?th ?teh ?tbh ?te tc =
307328
| FeHoareS _ when EcUtils.is_some teh -> (oget teh) tc
308329
| FbdHoareS _ when EcUtils.is_some tbh -> (oget tbh) tc
309330
| FequivS _ when EcUtils.is_some te -> (oget te ) tc
310-
311331
| _ ->
312332
let kinds = List.flatten [
313-
if EcUtils.is_some th then [`Hoare `Stmt] else [];
314-
if EcUtils.is_some teh then [`EHoare `Stmt] else [];
315-
if EcUtils.is_some tbh then [`PHoare `Stmt] else [];
316-
if EcUtils.is_some te then [`Equiv `Stmt] else []]
317-
333+
if EcUtils.is_some th then [`Hoare `Stmt] else [];
334+
if EcUtils.is_some teh then [`EHoare `Stmt] else [];
335+
if EcUtils.is_some tbh then [`PHoare `Stmt] else [];
336+
if EcUtils.is_some te then [`Equiv `Stmt] else []]
318337
in tc_error_noXhl ~kinds !!tc
319338

320339
let t_hF_or_bhF_or_eF ?th ?teh ?tbh ?te ?teg tc =
321-
match (FApi.tc1_goal tc).f_node with
322-
| FhoareF _ when EcUtils.is_some th -> (oget th ) tc
323-
| FeHoareF _ when EcUtils.is_some teh -> (oget teh) tc
324-
| FbdHoareF _ when EcUtils.is_some tbh -> (oget tbh) tc
325-
| FequivF _ when EcUtils.is_some te -> (oget te ) tc
326-
| FeagerF _ when EcUtils.is_some teg -> (oget teg) tc
327-
328-
| _ ->
340+
let texn tc =
329341
let kinds = List.flatten [
330342
if EcUtils.is_some th then [`Hoare `Pred] else [];
331343
if EcUtils.is_some teh then [`EHoare `Pred] else [];
332344
if EcUtils.is_some tbh then [`PHoare `Pred] else [];
333345
if EcUtils.is_some te then [`Equiv `Pred] else [];
334346
if EcUtils.is_some teg then [`Eager ] else []]
347+
in tc_error_noXhl ~kinds !!tc in
348+
let tx f tc =
349+
match f.f_node with
350+
| FhoareF _ when EcUtils.is_some th -> (oget th ) tc
351+
| FeHoareF _ when EcUtils.is_some teh -> (oget teh) tc
352+
| FbdHoareF _ when EcUtils.is_some tbh -> (oget tbh) tc
353+
| FequivF _ when EcUtils.is_some te -> (oget te ) tc
354+
| FeagerF _ when EcUtils.is_some teg -> (oget teg) tc
355+
| _ -> raise EcProofTyping.NoMatch in
356+
EcLowGoal.t_lazy_match ~texn tx tc
335357

336-
in tc_error_noXhl ~kinds !!tc
337358

338359
(* -------------------------------------------------------------------- *)
339360
let tag_sym_with_side ?mc name m =
@@ -672,7 +693,7 @@ let t_code_transform (side : oside) ?(bdhoare = false) cpos tr tx tc =
672693
let pr, po = bhs_pr bhs, bhs_po bhs in
673694
let (me, stmt, cs) =
674695
tx (pf, hyps) cpos (pr.inv, po.inv) (bhs.bhs_m, bhs.bhs_s) in
675-
let concl = f_bdHoareS (snd me) (bhs_pr bhs) stmt (bhs_po bhs)
696+
let concl = f_bdHoareS (snd me) (bhs_pr bhs) stmt (bhs_po bhs)
676697
bhs.bhs_cmp (bhs_bd bhs) in
677698
FApi.xmutate1 tc (tr None) (cs @ [concl])
678699

src/ecProofTyping.ml

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ let tc1_process_stmt ?map hyps tc c =
132132

133133
let tc1_process_prhl_stmt ?map tc side c =
134134
let concl = FApi.tc1_goal tc in
135-
let ml, mr = match concl.f_node with
135+
let ml, mr = match concl.f_node with
136136
| FequivS {es_ml=ml; es_mr=mr} -> (ml, mr)
137137
| FeagerF {eg_ml=ml; eg_mr=mr} ->
138138
EcMemory.abstract ml, EcMemory.abstract mr
@@ -188,27 +188,6 @@ let tc1_process_Xhl_formula ?side tc pf =
188188
let tc1_process_Xhl_formula_xreal tc pf =
189189
tc1_process_Xhl_form tc txreal pf
190190

191-
(* ------------------------------------------------------------------ *)
192-
let tc1_process_codepos_range tc (side, cpr) =
193-
let me, _ = EcLowPhlGoal.tc1_get_stmt side tc in
194-
let env = FApi.tc1_env tc in
195-
let env = EcEnv.Memory.push_active_ss me env in
196-
EcTyping.trans_codepos_range env cpr
197-
198-
(* ------------------------------------------------------------------ *)
199-
let tc1_process_codepos tc (side, cpos) =
200-
let me, _ = EcLowPhlGoal.tc1_get_stmt side tc in
201-
let env = FApi.tc1_env tc in
202-
let env = EcEnv.Memory.push_active_ss me env in
203-
EcTyping.trans_codepos env cpos
204-
205-
(* ------------------------------------------------------------------ *)
206-
let tc1_process_codepos1 tc (side, cpos) =
207-
let me, _ = EcLowPhlGoal.tc1_get_stmt side tc in
208-
let env = FApi.tc1_env tc in
209-
let env = EcEnv.Memory.push_active_ss me env in
210-
EcTyping.trans_codepos1 env cpos
211-
212191
(* ------------------------------------------------------------------ *)
213192
(* FIXME: factor out to typing module *)
214193
(* FIXME: TC HOOK - check parameter constraints *)

src/ecProofTyping.mli

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ open EcIdent
44
open EcDecl
55
open EcEnv
66
open EcCoreGoal
7-
open EcMatching.Position
87
open EcAst
98

109
(* -------------------------------------------------------------------- *)
@@ -59,10 +58,6 @@ val tc1_process_prhl_stmt :
5958
val tc1_process_Xhl_stmt :
6059
?map:EcTyping.ismap -> tcenv1 -> pstmt -> stmt
6160

62-
val tc1_process_codepos_range : tcenv1 -> oside * pcodepos_range -> codepos_range
63-
val tc1_process_codepos : tcenv1 -> oside * pcodepos -> codepos
64-
val tc1_process_codepos1 : tcenv1 -> oside * pcodepos1 -> codepos1
65-
6661
(* -------------------------------------------------------------------- *)
6762
exception NoMatch
6863

src/ecReduction.ml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,23 +1258,23 @@ let rec simplify ri env f =
12581258
match f.f_node with
12591259
| FhoareF hf when ri.ri.modpath ->
12601260
let hf_f = EcEnv.NormMp.norm_xfun env hf.hf_f in
1261-
f_map (fun ty -> ty) (simplify ri env)
1261+
f_map (fun ty -> ty) (simplify ri env)
12621262
(f_hoareF (hf_pr hf) hf_f (hf_po hf))
12631263

12641264
| FeHoareF hf when ri.ri.modpath ->
12651265
let ehf_f = EcEnv.NormMp.norm_xfun env hf.ehf_f in
1266-
f_map (fun ty -> ty) (simplify ri env)
1266+
f_map (fun ty -> ty) (simplify ri env)
12671267
(f_eHoareF (ehf_pr hf) ehf_f (ehf_po hf))
12681268

12691269
| FbdHoareF hf when ri.ri.modpath ->
12701270
let bhf_f = EcEnv.NormMp.norm_xfun env hf.bhf_f in
1271-
f_map (fun ty -> ty) (simplify ri env)
1271+
f_map (fun ty -> ty) (simplify ri env)
12721272
(f_bdHoareF (bhf_pr hf) bhf_f (bhf_po hf) hf.bhf_cmp (bhf_bd hf))
12731273

12741274
| FequivF ef when ri.ri.modpath ->
12751275
let ef_fl = EcEnv.NormMp.norm_xfun env ef.ef_fl in
12761276
let ef_fr = EcEnv.NormMp.norm_xfun env ef.ef_fr in
1277-
f_map (fun ty -> ty) (simplify ri env)
1277+
f_map (fun ty -> ty) (simplify ri env)
12781278
(f_equivF (ef_pr ef) ef_fl ef_fr (ef_po ef))
12791279

12801280
| FeagerF eg when ri.ri.modpath ->
@@ -1666,6 +1666,12 @@ let h_red_opt ri hyps f =
16661666
try Some (h_red ri hyps f)
16671667
with NotReducible -> None
16681668

1669+
let rec h_red_until ?(until = fun _ -> false) ri hyps f =
1670+
if until f then f
1671+
else match h_red ri hyps f with
1672+
| f -> h_red_until ~until ri hyps f
1673+
| exception NotReducible -> f
1674+
16691675
(* -------------------------------------------------------------------- *)
16701676
type xconv = [`Eq | `AlphaEq | `Conv]
16711677

src/ecReduction.mli

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ val reduce_logic : reduction_info -> env -> LDecl.hyps -> form -> form
8888
val h_red_opt : reduction_info -> LDecl.hyps -> form -> form option
8989
val h_red : reduction_info -> LDecl.hyps -> form -> form
9090

91+
(* [hred_until ~until ri hyps f] performs head reduction on [f]
92+
until [test f] is true or that no more head reduction is possible.
93+
If no [until] argument is provided then head reduction is performed
94+
until it is possible.
95+
*)
96+
val h_red_until :
97+
?until:(form -> bool) -> reduction_info -> LDecl.hyps -> form -> form
98+
9199
val reduce_user_gen :
92100
(EcFol.form -> EcFol.form) ->
93101
reduction_info ->
@@ -109,4 +117,4 @@ type xconv = [`Eq | `AlphaEq | `Conv]
109117
val xconv : xconv -> LDecl.hyps -> form -> form -> bool
110118

111119
val ss_inv_alpha_eq : LDecl.hyps -> ss_inv -> ss_inv -> bool
112-
val ts_inv_alpha_eq : LDecl.hyps -> ts_inv -> ts_inv -> bool
120+
val ts_inv_alpha_eq : LDecl.hyps -> ts_inv -> ts_inv -> bool

src/phl/ecPhlApp.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ let t_bdhoare_app_r_low i (phi, pR, f1, f2, g1, g2) tc =
6767
let (ir1, ir2) = EcIdent.create "r", EcIdent.create "r" in
6868
let (r1 , r2 ) = f_local ir1 treal, f_local ir2 treal in
6969
let condnm =
70-
let eqs = map_ss_inv2 f_and (map_ss_inv1 ((EcUtils.flip f_eq) r1) f2)
70+
let eqs = map_ss_inv2 f_and (map_ss_inv1 ((EcUtils.flip f_eq) r1) f2)
7171
(map_ss_inv1 ((EcUtils.flip f_eq) r2) g2) in
7272
f_forall
7373
[(ir1, GTty treal); (ir2, GTty treal)]
@@ -124,11 +124,11 @@ let t_equiv_app_onesided side i pre post tc =
124124
let (ml, mr) = fst es.es_ml, fst es.es_mr in
125125
let s, s', p', q' =
126126
match side with
127-
| `Left ->
127+
| `Left ->
128128
let p' = ss_inv_generalize_right (EcSubst.ss_inv_rebind pre ml) mr in
129129
let q' = ss_inv_generalize_right (EcSubst.ss_inv_rebind post ml) mr in
130130
es.es_sl, es.es_sr, p', q'
131-
| `Right ->
131+
| `Right ->
132132
let p' = ss_inv_generalize_left (EcSubst.ss_inv_rebind pre mr) ml in
133133
let q' = ss_inv_generalize_left (EcSubst.ss_inv_rebind post mr) ml in
134134
es.es_sr, es.es_sl, p', q'
@@ -227,13 +227,13 @@ let process_app (side, dir, k, phi, bd_info) tc =
227227
| Single i, PAppNone when is_hoareS concl ->
228228
check_side side;
229229
let _, phi = TTC.tc1_process_Xhl_formula tc (get_single phi) in
230-
let i = EcProofTyping.tc1_process_codepos1 tc (side, i) in
230+
let i = EcLowPhlGoal.tc1_process_codepos1 tc (side, i) in
231231
t_hoare_app i phi tc
232232

233233
| Single i, PAppNone when is_eHoareS concl ->
234234
check_side side;
235235
let _, phi = TTC.tc1_process_Xhl_formula_xreal tc (get_single phi) in
236-
let i = EcProofTyping.tc1_process_codepos1 tc (side, i) in
236+
let i = EcLowPhlGoal.tc1_process_codepos1 tc (side, i) in
237237
t_ehoare_app i phi tc
238238

239239
| Single i, PAppNone when is_equivS concl ->
@@ -248,21 +248,21 @@ let process_app (side, dir, k, phi, bd_info) tc =
248248
match side with
249249
| None -> tc_error !!tc "seq onsided: side information expected"
250250
| Some side -> side in
251-
let i = EcProofTyping.tc1_process_codepos1 tc (Some side, i) in
251+
let i = EcLowPhlGoal.tc1_process_codepos1 tc (Some side, i) in
252252
t_equiv_app_onesided side i pre post tc
253253

254254
| Single i, _ when is_bdHoareS concl ->
255255
check_side side;
256256
let _, pia = TTC.tc1_process_Xhl_formula tc (get_single phi) in
257257
let (ra, f1, f2, f3, f4) = process_phl_bd_info dir bd_info tc in
258-
let i = EcProofTyping.tc1_process_codepos1 tc (side, i) in
258+
let i = EcLowPhlGoal.tc1_process_codepos1 tc (side, i) in
259259
t_bdhoare_app i (ra, pia, f1, f2, f3, f4) tc
260260

261261
| Double (i, j), PAppNone when is_equivS concl ->
262262
check_side side;
263263
let phi = TTC.tc1_process_prhl_formula tc (get_single phi) in
264-
let i = EcProofTyping.tc1_process_codepos1 tc (Some `Left, i) in
265-
let j = EcProofTyping.tc1_process_codepos1 tc (Some `Left, j) in
264+
let i = EcLowPhlGoal.tc1_process_codepos1 tc (Some `Left, i) in
265+
let j = EcLowPhlGoal.tc1_process_codepos1 tc (Some `Left, j) in
266266
t_equiv_app (i, j) phi tc
267267

268268
| Single _, PAppNone

0 commit comments

Comments
 (0)