Skip to content

Commit 22df840

Browse files
committed
add Reflection theory
1 parent e6c14f7 commit 22df840

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(* Theory abstracting over the structure of procedures *)
2+
abstract theory Proc.
3+
type a. (* type of argument to the procedure *)
4+
type r. (* type of result of the procedure *)
5+
6+
(* Module type for procedure. Use wrapper to conform to the interface if needed *)
7+
module type Proc = {
8+
proc p(a: a): r
9+
}.
10+
end Proc.

theories/modules/Reflection.eca

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
require import Distr Real List StdBigop ModuleStructure.
2+
(*---*) import Bigreal.BRA.
3+
4+
clone import ModuleStructure.Proc.
5+
6+
section.
7+
8+
declare module P <: Proc.
9+
10+
local module P2 = {
11+
proc sampleFrom (d : (r * (glob P)) distr) = {
12+
var r;
13+
r <$ d;
14+
return r;
15+
}
16+
}.
17+
18+
local lemma gen_fact &m a (l : (r * (glob P)) list): uniq l
19+
=> Pr[ P.p(a) @ &m : (res , (glob P)) \in l ]
20+
= big predT (fun (x : (r * (glob P))) =>
21+
Pr[P.p(a) @ &m: res=x.`1 /\ (glob P) = x.`2])
22+
l.
23+
proof.
24+
move: l; apply list_ind => /= [|x l p1 [x_nin uniq_l]].
25+
- by rewrite Pr[mu_false] big_nil.
26+
rewrite Pr[mu_disjoint] 1:/# big_cons {1}/predT /= (p1 uniq_l).
27+
congr.
28+
rewrite Pr[mu_eq] /#.
29+
qed.
30+
31+
lemma reflection :
32+
exists (D : (glob P) -> a -> (r * glob P) distr),
33+
forall &m po a, mu (D (glob P){m} a) po = Pr[P.p(a) @ &m : po (res, glob P)].
34+
proof.
35+
pose PR := fun (g : glob P) (a : a) (x : r * glob P) =>
36+
choicebd (fun p => forall &m, (glob P){m} = g =>
37+
Pr[P.p(a) @ &m : res=x.`1 /\ (glob P) = x.`2 ] = p).
38+
pose D := (fun (g : glob P) (a : a) => mk (PR g a)).
39+
exists D.
40+
move => &m po a.
41+
have PRP: (PR (glob P){m}) a = fun (x : (r * (glob P)))
42+
=> Pr[P.p(a) @ &m: res = x.`1 /\ (glob P) = x.`2 ].
43+
- apply fun_ext => x.
44+
have /=chs:=choicebdP (fun p =>
45+
Pr[P.p(a) @ &m : res = x.`1 /\ (glob P) = x.`2] = p).
46+
rewrite chs.
47+
- by exists (Pr[P.p(a) @ &m : res = x.`1 /\ (glob P) = x.`2]).
48+
rewrite /PR.
49+
congr.
50+
apply fun_ext => p.
51+
rewrite eq_iff; split => [/#|<- &n gl_eq].
52+
byequiv (_: ={arg, glob P} ==> ={res, glob P})=> //; 1:sim; smt().
53+
have distr_PR: isdistr (PR (glob P){m} a).
54+
- have : (forall (s : ((r * (glob P)) list)), uniq s =>
55+
big predT (PR (glob P){m} a) s <= 1%r).
56+
+ rewrite PRP.
57+
apply list_ind => /=[|x l q1 q2].
58+
* by rewrite big_nil.
59+
by rewrite -(gen_fact &m a (x :: l)) 2:Pr[mu_le1]; 1:apply q2.
60+
move => fact1.
61+
have: (forall (x : r * (glob P)), 0%r <= PR (glob P){m} a x).
62+
+ by move => x; rewrite PRP /= Pr[mu_ge0].
63+
by move => fact2; split.
64+
have <-: Pr[ P2.sampleFrom((D (glob P){m} a)) @ &m : po res ]
65+
= mu (D (glob P){m} a) po.
66+
- byphoare (_ : d = (D (glob P){m} a) ==> _) => //.
67+
proc; rnd; auto => &hr /=.
68+
byequiv => //.
69+
conseq (_: _ ==> res{1}.`1 = res{2} /\ res{1}.`2 = (glob P){2}); 1:smt().
70+
bypr (res{1}) (res, glob P){2} => // &1 &2 aa [->][gl_eq ->].
71+
have <-: Pr[P.p(a) @ &m : res = aa.`1 /\ (glob P) = aa.`2]
72+
= Pr[P.p(a) @ &2 : (res , glob P) = aa].
73+
- byequiv (_: ={arg, glob P} ==> ={res, glob P})=> //; 1:sim; smt().
74+
byphoare (_ : d = (D (glob P){m} a) ==> _) => //.
75+
proc; rnd; skip => &hr /= -> />.
76+
rewrite muK //#.
77+
qed.
78+
79+
lemma reflection_glob : exists (D : (glob P) -> a -> (glob P) distr),
80+
forall &m po a, mu (D (glob P){m} a) po = Pr[P.p(a) @ &m : po (glob P)].
81+
proof.
82+
elim reflection => /> D DP.
83+
exists (fun ga i => dmap (D ga i) (fun (x : r * (glob P)) => x.`2)) => /> &m po a.
84+
by rewrite -(DP &m (fun (x : r * (glob P)) => po x.`2) a) dmapE.
85+
qed.
86+
87+
lemma reflection_res : exists (D : (glob P) -> a -> r distr),
88+
forall &m po a, mu (D (glob P){m} a) po = Pr[P.p(a) @ &m : po res ].
89+
proof.
90+
elim reflection => /> D DP.
91+
exists (fun ga i => dmap (D ga i) (fun (x : r * (glob P)) => x.`1)) => /> &m po a.
92+
by rewrite -(DP &m (fun (x : r * (glob P)) => po x.`1) a) dmapE.
93+
qed.
94+
95+
end section.

0 commit comments

Comments
 (0)