Skip to content

Commit 6e0cab7

Browse files
bgregoirstrub
authored andcommitted
add mu_has_le in rewrite Pr
1 parent e1f90b0 commit 6e0cab7

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

src/phl/ecPhlPrRw.ml

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,35 @@ let pr_mu1_le_eq_mu1 m f args resv k fresh_id d =
9696
f_eq (f_pr m f args (f_eq resv k)) (f_mu_x d k) in
9797
f_imp f_ll (f_imp f_le_mu1 concl)
9898

99+
let p_List = [EcCoreLib.i_top; "List"]
100+
let p_BRA = [EcCoreLib.i_top; "StdBigop"; "Bigreal"; "BRA"]
101+
let p_list_has = EcPath.fromqsymbol (p_List, "has")
102+
let p_BRA_big = EcPath.fromqsymbol (p_BRA, "big")
103+
104+
let destr_pr_has pr =
105+
match pr.pr_event.f_node with
106+
| Fapp ({ f_node = Fop(op, [ty_elem]) }, [f_f; f_l]) ->
107+
if EcPath.p_equal p_list_has op then
108+
Some(ty_elem, f_f, f_l)
109+
else None
110+
| _ -> None
111+
(*
112+
lemma mu_has_le ['a 'b] (P : 'a -> 'b -> bool) (d : 'a distr) (s : 'b list) :
113+
mu d (fun a => has (P a) s) <= BRA.big predT (fun b => mu d (fun a => P a b)) s.
114+
Pr [f(args)@ &m : has P s] <= BRA.big predT (fun b => Pr [f(args) &m : P b])
115+
*)
116+
let pr_has_le f_pr =
117+
let pr = destr_pr f_pr in
118+
let ty_elem, f_f, f_l = oget (destr_pr_has pr) in
119+
let idx = EcIdent.create "x" in
120+
let x = f_local idx ty_elem in
121+
let pr_event = f_app f_f [x] EcTypes.tbool in
122+
let f_pr1 = f_pr_r {pr with pr_event} in
123+
let f_fsum = f_lambda [idx, GTty ty_elem] f_pr1 in
124+
let f_sum =
125+
f_app (f_op p_BRA_big [ty_elem] EcTypes.treal) [f_predT ty_elem; f_fsum; f_l] EcTypes.treal in
126+
f_real_le f_pr f_sum
127+
99128
(* -------------------------------------------------------------------- *)
100129
exception FoundPr of form
101130

@@ -140,6 +169,18 @@ let select_pr_le1 sid f =
140169
else false
141170
| _ -> false
142171

172+
let select_pr_muhasle sid f =
173+
match f.f_node with
174+
| Fapp ({ f_node = Fop (op, _) }, [ { f_node = Fpr pr } as f_pr; _ ]) ->
175+
if EcPath.p_equal EcCoreLib.CI_Real.p_real_le op then
176+
match destr_pr_has pr with
177+
| Some (_, _, f_l) when
178+
Mid.set_disjoint f_l.f_fv (Mid.add EcCoreFol.mhr () sid) ->
179+
raise (FoundPr f_pr)
180+
| _ -> false
181+
else false
182+
| _ -> false
183+
143184
(* -------------------------------------------------------------------- *)
144185
let pr_rewrite_lemma =
145186
[
@@ -154,6 +195,7 @@ let pr_rewrite_lemma =
154195
("mu_or", `MuOr);
155196
("mu_split", `MuSplit);
156197
("mu_sub", `MuSub);
198+
("mu_has_le", `MuHasLe)
157199
]
158200

159201
(* -------------------------------------------------------------------- *)
@@ -181,6 +223,7 @@ let t_pr_rewrite_low (s, dof) tc =
181223
| `MuSplit -> select_pr (fun _ev -> true)
182224
| `MuSub -> select_pr_cmp (EcPath.p_equal EcCoreLib.CI_Real.p_real_le)
183225
| `MuSum -> select_pr (fun _ev -> true)
226+
| `MuHasLe -> select_pr_muhasle
184227
in
185228

186229
let select xs fp = if select xs fp then `Accept (-1) else `Continue in
@@ -193,7 +236,7 @@ let t_pr_rewrite_low (s, dof) tc =
193236

194237
let lemma, args =
195238
match kind with
196-
| `Mu1LeEqMu1 ->
239+
| `Mu1LeEqMu1 ->
197240
let { pr_mem; pr_fun; pr_args; pr_event } = destr_pr torw in
198241
let (resv, k) = destr_eq pr_event in
199242
let k_id = EcEnv.LDecl.fresh_id hyps "k" in
@@ -256,6 +299,9 @@ let t_pr_rewrite_low (s, dof) tc =
256299
| `MuSum ->
257300
(pr_sum env (destr_pr torw), 0)
258301

302+
| `MuHasLe ->
303+
(pr_has_le torw, 0)
304+
259305
in
260306

261307
let rwpt = EcCoreGoal.ptcut ~args:(List.make args (PASub None)) lemma in
@@ -269,7 +315,7 @@ let t_pr_rewrite_i (s, f) tc =
269315
t_pr_rewrite_low (s, do_ev) tc
270316

271317
let t_pr_rewrite (s, f) tc =
272-
let to_env f tc torw ty =
318+
let to_env f tc torw ty =
273319
let env, hyps, _ = FApi.tc1_eflat tc in
274320
let pr = destr_pr torw in
275321
let mp = EcEnv.Fun.prF_memenv EcFol.mhr pr.pr_fun env in

tests/rw_mu_has_le.ec

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require import AllCore List Distr DInterval StdBigop.
2+
import Bigreal.
3+
4+
module M = {
5+
proc f () : int = {
6+
var x : int;
7+
x <$ [0..10];
8+
return x;
9+
}
10+
}.
11+
12+
op l : int list.
13+
14+
lemma foo &m :
15+
Pr [ M.f() @ &m : has (fun x => res = x) l] <=
16+
BRA.big predT (fun x => Pr [ M.f() @ &m : res = x]) l.
17+
proof.
18+
by rewrite Pr [mu_has_le].
19+
qed.
20+

0 commit comments

Comments
 (0)