File size: 7,050 Bytes
dd14c26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import OAI.Probability.MatroidProphet.BatchReveal
import OAI.Probability.MatroidProphet.Certificates.Execution

namespace OAI

namespace MatroidProphet
open Finset
variable {α β γ : Type*} [DecidableEq α]

def RevealTree.bind (tree : RevealTree α β) (cont : β → RevealTree α γ) : RevealTree α γ :=
  match tree with
  | .leaf b => cont b
  | .query e no yes => .query e (no.bind cont) (yes.bind cont)

lemma RevealTree.run_bind (tree : RevealTree α β) (cont : β → RevealTree α γ) (S : Finset α) :
    (tree.bind cont).run S = (cont (tree.run S)).run (S \ tree.queried S) := by
  induction tree generalizing S with
  | leaf b => simp [bind, run, queried]
  | query e no yes ihn ihy =>
    by_cases heS : e ∈ S
    · simp only [bind, run, queried, ite_eq_left heS, ihy, erase_sdiff_eq_sdiff_insert]
    · simp only [bind, run, queried, ite_eq_right heS, ihn, erase_sdiff_eq_sdiff_insert]

lemma RevealTree.queried_bind (tree : RevealTree α β) (cont : β → RevealTree α γ) (S : Finset α) :
    (tree.bind cont).queried S =
      tree.queried S ∪ (cont (tree.run S)).queried (S \ tree.queried S) := by
  induction tree generalizing S with
  | leaf b => simp [bind, run, queried]
  | query e no yes ihn ihy =>
    by_cases heS : e ∈ S
    · simp only [bind, run, queried, ite_eq_left heS, ihy, erase_sdiff_eq_sdiff_insert, insert_union]
    · simp only [bind, run, queried, ite_eq_right heS, ihn, erase_sdiff_eq_sdiff_insert, insert_union]

lemma RevealTree.fresh_bind (tree : RevealTree α β) (cont : β → RevealTree α γ)
    (V : Finset α) (fresh : tree.Fresh V)
    (hcont : ∀ S ⊆ V, (cont (tree.run S)).Fresh (V \ tree.queried S)) :
    (tree.bind cont).Fresh V := by
  induction tree generalizing V with
  | leaf b => simpa [bind, run, queried] using hcont ∅ (empty_subset V)
  | query e no yes ihn ihy =>
    rcases fresh with ⟨he, hn, hy⟩
    refine ⟨he, ihn (V.erase e) hn ?_, ihy (V.erase e) hy ?_⟩
    · intro S hS
      have heS : e ∉ S := fun hs => notMem_erase e V (hS hs)
      have hc := hcont S (hS.trans (erase_subset e V))
      simpa only [run, queried, ite_eq_right heS, erase_eq_of_notMem heS,
        erase_sdiff_eq_sdiff_insert] using hc
    · intro S hS
      have heS : e ∉ S := fun hs => notMem_erase e V (hS hs)
      have hc := hcont (insert e S) (insert_subset he (hS.trans (erase_subset e V)))
      simpa only [run, queried, mem_insert_self, ite_true, erase_insert heS,
        erase_eq_of_notMem heS, erase_sdiff_eq_sdiff_insert] using hc

lemma RevealTree.run_congr (tree : RevealTree α β) (V S S' : Finset α)
    (fresh : tree.Fresh V) (agree : ∀ e ∈ V, (e ∈ S ↔ e ∈ S')) :
    tree.run S = tree.run S' := by
  induction tree generalizing V S S' with
  | leaf b => rfl
  | query e no yes ihn ihy =>
    rcases fresh with ⟨he, hn, hy⟩
    have ha := agree e he
    have har : ∀ x ∈ V.erase e, (x ∈ S.erase e ↔ x ∈ S'.erase e) := by
      intro x hx
      simp only [mem_erase, agree x (mem_of_mem_erase hx)]
    by_cases heS : e ∈ S
    · simp only [run, ite_eq_left heS, ite_eq_left (ha.mp heS)]
      exact ihy _ _ _ hy har
    · have heS' : e ∉ S' := fun h => heS (ha.mpr h)
      simp only [run, ite_eq_right heS, ite_eq_right heS']
      exact ihn _ _ _ hn har

lemma RevealTree.queried_congr (tree : RevealTree α β) (V S S' : Finset α)
    (fresh : tree.Fresh V) (agree : ∀ e ∈ V, (e ∈ S ↔ e ∈ S')) :
    tree.queried S = tree.queried S' := by
  induction tree generalizing V S S' with
  | leaf b => rfl
  | query e no yes ihn ihy =>
    rcases fresh with ⟨he, hn, hy⟩
    have ha := agree e he
    have har : ∀ x ∈ V.erase e, (x ∈ S.erase e ↔ x ∈ S'.erase e) := by
      intro x hx
      simp only [mem_erase, agree x (mem_of_mem_erase hx)]
    by_cases heS : e ∈ S
    · simp only [queried, ite_eq_left heS, ite_eq_left (ha.mp heS)]
      rw [ihy _ _ _ hy har]
    · have heS' : e ∉ S' := fun h => heS (ha.mpr h)
      simp only [queried, ite_eq_right heS, ite_eq_right heS']
      rw [ihn _ _ _ hn har]

def RevealTree.transaction (tree : RevealTree α β) (cont : β → QueryProgram α) : QueryProgram α :=
  match tree with
  | .leaf b => .finish (cont b)
  | .query e no yes => .query e (no.transaction cont) (yes.transaction cont)

lemma RevealTree.transaction_fresh (tree : RevealTree α β) (cont : β → QueryProgram α)
    (V : Finset α) (fresh : tree.Fresh V)
    (hcont : ∀ S ⊆ V, (cont (tree.run S)).Fresh (V \ tree.queried S)) :
    (tree.transaction cont).Fresh V := by
  induction tree generalizing V with
  | leaf b => simpa [transaction, QueryProgram.Fresh, run, queried] using hcont ∅ (empty_subset V)
  | query e no yes ihn ihy =>
    rcases fresh with ⟨he, hn, hy⟩
    refine ⟨he, ihn (V.erase e) hn ?_, ihy (V.erase e) hy ?_⟩
    · intro S hS
      have heS : e ∉ S := fun hs => notMem_erase e V (hS hs)
      have hc := hcont S (hS.trans (erase_subset e V))
      simpa only [run, queried, ite_eq_right heS, erase_eq_of_notMem heS,
        erase_sdiff_eq_sdiff_insert] using hc
    · intro S hS
      have heS : e ∉ S := fun hs => notMem_erase e V (hS hs)
      have hc := hcont (insert e S) (insert_subset he (hS.trans (erase_subset e V)))
      simpa only [run, queried, mem_insert_self, ite_true, erase_insert heS,
        erase_eq_of_notMem heS, erase_sdiff_eq_sdiff_insert] using hc

lemma RevealTree.transaction_run (tree : RevealTree α β) (cont : β → QueryProgram α)
    (V : Finset α) (fresh : tree.Fresh V) (cert : Finset α → Prop) [DecidablePred cert]
    (S I : Finset α) :
    (tree.transaction cont).run cert S I =
      (if cert (I ∪ (S ∩ tree.queried S)) then 1 else 0) + (cont (tree.run S)).run cert S ∅ := by
  induction tree generalizing V I with
  | leaf b => simp [transaction, QueryProgram.run, run, queried]
  | query e no yes ihn ihy =>
    rcases fresh with ⟨he, hn, hy⟩
    have ha : ∀ x ∈ V.erase e, (x ∈ S ↔ x ∈ S.erase e) := by
      intro x hx
      simp only [mem_erase]
      exact ⟨fun hs => ⟨(mem_erase.mp hx).1, hs⟩, fun hs => hs.2⟩
    by_cases heS : e ∈ S
    · rw [transaction, QueryProgram.run, ite_eq_left heS, ihy _ hy]
      rw [yes.run_congr _ S (S.erase e) hy ha, yes.queried_congr _ S (S.erase e) hy ha]
      simp only [run, queried, ite_eq_left heS]
      have heq : insert e I ∪ S ∩ yes.queried (S.erase e) =
          I ∪ S ∩ insert e (yes.queried (S.erase e)) := by
        ext x
        by_cases hxe : x = e
        · subst x; simp [heS]
        · simp [hxe]
      rw [heq]
    · rw [transaction, QueryProgram.run, ite_eq_right heS, ihn _ hn]
      rw [no.run_congr _ S (S.erase e) hn ha, no.queried_congr _ S (S.erase e) hn ha]
      simp only [run, queried, ite_eq_right heS]
      have heq : I ∪ S ∩ no.queried (S.erase e) =
          I ∪ S ∩ insert e (no.queried (S.erase e)) := by
        ext x
        by_cases hxe : x = e
        · subst x; simp [heS]
        · simp [hxe]
      rw [heq]

end MatroidProphet

end OAI