Repository navigation
Expand file tree
/
Copy pathFunctional.lean
More file actions
289 lines (254 loc) · 13.2 KB
/
Copy pathFunctional.lean
File metadata and controls
289 lines (254 loc) · 13.2 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
module
public import RandomSystems.Typed.Interface
public import RandomSystems.PDS.Function
/-!
# Functional typed systems
Typed systems given by reply functions, by conditional reply laws, and by sampling once and
retaining the sample. Each is a random system on the declared alphabets and domain: reply
laws use `RandomSystem.ofConditional`, sampling uses `RandomSystem.mix`.
## Main definitions
* `TypedSystem.ofConditional`: conditional reply distributions on the domain
* `TypedSystem.ofFunction`: a reply function at each interface
* `TypedSystem.sample`: sample once, then keep the sample
## Main results
* `TypedSystem.ofFunction_mass`: the behavior of a function is the indicator of its
admitted transcripts
* `TypedSystem.eq_ofFunction`: probability one on the functional transcripts
determines the typed system
* `TypedSystem.sample_ofFunction_mass_eq`: event mass of draws supplying the transcript replies
* `TypedSystem.sample_ofFunction_mass`: sampled functions are the finite PDS of the
function systems
* `TypedSystem.sample_fTransform`, `TypedConverter.smul_sample`: a retained sample commutes with
pushforward of its law and with converter attachment
-/
@[expose] public section
namespace SystemAlgebra
open Probability
open scoped BigOperators
namespace TypedSystem
variable {A : Interface}
open Classical in
/-- Conditional reply distributions at the queried interface, on the declared domain. -/
noncomputable def ofConditional
(q : ∀ (h : List ((Σ i, A.X i) × (Σ i, A.Y i))) (i : A.I) (x : A.X i),
A.domain (h.map Prod.fst ++ [⟨i, x⟩]) → Distribution.ProbDist (A.Y i)) :
TypedSystem A := by
let reply : ∀ h (x : Σ i, A.X i), A.inputDomain h x → Distribution.ProbDist (Σ i, A.Y i) :=
fun h x hd => Distribution.PMF (q h x.1 x.2 hd) (fun y => (⟨x.1, y⟩ : Σ i, A.Y i))
refine ⟨RandomSystem.ofConditional reply, ?_⟩
intro h x y hne
rw [RandomSystem.ofConditional_snoc]
split_ifs with hd
· have hz : reply h x hd y = 0 := by
apply Distribution.fTransform_apply_of_forall_ne
intro v he
exact hne (congrArg Sigma.fst he).symm
rw [hz, mul_zero]
· rfl
/-- A deterministic function at each interface, on the declared input domain. -/
noncomputable def ofFunction (f : ∀ i, A.X i → A.Y i) : TypedSystem A :=
ofConditional fun _ i x _ => ⟨Finsupp.single (f i x) 1, Distribution.isProbDist_single _⟩
open Classical in
lemma ofFunction_snoc (f : ∀ i, A.X i → A.Y i)
(h : List ((Σ i, A.X i) × (Σ i, A.Y i))) (x : Σ i, A.X i) (y : Σ i, A.Y i) :
(ofFunction f).1 (h ++ [(x, y)]) =
if A.domain (h.map Prod.fst ++ [x]) then
(ofFunction f).1 h * (if y = ⟨x.1, f x.1 x.2⟩ then 1 else 0) else 0 := by
unfold ofFunction ofConditional
rw [RandomSystem.ofConditional_snoc]
by_cases hd : A.domain (h.map Prod.fst ++ [x])
· rw [dite_eq_left (show A.inputDomain h x from hd), ite_eq_left hd]
congr 1
simp [Distribution.PMF, Distribution.fTransform, Finsupp.single_apply, eq_comm]
· rw [dite_eq_right (show ¬ A.inputDomain h x from hd), ite_eq_right hd]
/-- A functional typed system gives probability one to its prescribed answers on
each admitted history. -/
lemma ofFunction_mass_eq_one (f : ∀ i, A.X i → A.Y i)
(h : List ((Σ i, A.X i) × (Σ i, A.Y i)))
(hd : h = [] ∨ A.domain (h.map Prod.fst))
(he : ∀ z ∈ h, z.2 = ⟨z.1.1, f z.1.1 z.1.2⟩) :
(ofFunction f).1 h = 1 := by
classical
induction h using List.reverseRecOn with
| nil => exact RandomSystem.mass_nil _
| append_singleton h z ih =>
have hp : A.domain (h.map Prod.fst ++ [z.1]) := by
rcases hd with hd | hd
· simp at hd
· simpa only [List.map_append, List.map_singleton] using hd
have hh : h = [] ∨ A.domain (h.map Prod.fst) := by
by_cases hn : h = []
· exact Or.inl hn
· exact Or.inr (A.nonempty_prefix.2 (List.prefix_append _ _) (by simpa using hn) hp)
rw [ofFunction_snoc, ite_eq_left hp, ite_eq_left (he z (by simp)),
ih hh (fun w hw => he w (List.mem_append_left _ hw)), mul_one]
lemma ofFunction_answers (f : ∀ i, A.X i → A.Y i)
(h : List ((Σ i, A.X i) × (Σ i, A.Y i))) (hm : (ofFunction f).1 h ≠ 0) :
∀ z ∈ h, z.2 = ⟨z.1.1, f z.1.1 z.1.2⟩ := by
induction h using List.reverseRecOn with
| nil => simp
| append_singleton h z ih =>
rw [ofFunction_snoc] at hm
split_ifs at hm with hd hy
· intro w hw
rcases List.mem_append.mp hw with hw | hw
· exact ih (by simpa only [mul_one] using hm) w hw
· exact (List.mem_singleton.mp hw) ▸ hy
· simp at hm
· exact (hm rfl).elim
open Classical in
/-- The cumulative law of a function is the indicator of its admitted
query/reply histories. -/
lemma ofFunction_mass (f : ∀ i, A.X i → A.Y i)
(h : List ((Σ i, A.X i) × (Σ i, A.Y i))) :
(ofFunction f).1 h =
if (h = [] ∨ A.domain (h.map Prod.fst)) ∧
(∀ z ∈ h, z.2 = ⟨z.1.1, f z.1.1 z.1.2⟩) then 1 else 0 := by
by_cases hd : h = [] ∨ A.domain (h.map Prod.fst)
· by_cases he : ∀ z ∈ h, z.2 = ⟨z.1.1, f z.1.1 z.1.2⟩
· rw [ite_eq_left ⟨hd, he⟩]; exact ofFunction_mass_eq_one f h hd he
· rw [ite_eq_right (fun hc => he hc.2)]
by_contra hn
exact he (ofFunction_answers f h hn)
· rw [ite_eq_right (fun hc => hd hc.1)]
obtain ⟨h, z, rfl⟩ := List.eq_nil_or_concat h |>.resolve_left (fun hn => hd (Or.inl hn))
simp only [List.concat_eq_append, List.map_append, List.map_singleton] at hd ⊢
rw [ofFunction_snoc, ite_eq_right (fun hc => hd (Or.inr hc))]
/-- Probability one on the functional transcripts determines the entire
behavior, including probability zero for every different answer. -/
lemma eq_ofFunction (R : TypedSystem A) (f : ∀ i, A.X i → A.Y i)
(certain : ∀ h, (h = [] ∨ A.domain (h.map Prod.fst)) →
(∀ z ∈ h, z.2 = ⟨z.1.1, f z.1.1 z.1.2⟩) → R.1 h = 1) :
R = ofFunction f := by
have mass (h : List ((Σ i, A.X i) × (Σ i, A.Y i))) :
R.1 h = (ofFunction f).1 h := by
induction h using List.reverseRecOn with
| nil => rw [RandomSystem.mass_nil, RandomSystem.mass_nil]
| append_singleton h z ih =>
by_cases hd : A.domain (h.map Prod.fst ++ [z.1])
· by_cases he : ∀ w ∈ h, w.2 = ⟨w.1.1, f w.1.1 w.1.2⟩
· have hh : h = [] ∨ A.domain (h.map Prod.fst) := by
by_cases hn : h = []
· exact Or.inl hn
· exact Or.inr (A.nonempty_prefix.2 (List.prefix_append _ _)
(by simpa using hn) hd)
have hp := certain h hh he
let v : Σ i, A.Y i := ⟨z.1.1, f z.1.1 z.1.2⟩
have hv : R.1 (h ++ [(z.1, v)]) = 1 := by
apply certain _ (Or.inr (by simpa only [List.map_append, List.map_singleton] using hd))
intro w hw
rcases List.mem_append.mp hw with hw | hw
· exact he w hw
· obtain rfl := List.mem_singleton.mp hw
rfl
rw [ofFunction_snoc, ite_eq_left hd, ← ih, hp, one_mul]
by_cases hy : z.2 = v
· rw [ite_eq_left hy]
have hz : z = (z.1, v) := Prod.ext rfl hy
rw [hz]
exact hv
· rw [ite_eq_right hy]
have hc := Distribution.mass_add_compl (R.1.extensionLaw h z.1) (fun y => y = v)
rw [Distribution.mass_singleton, RandomSystem.extensionLaw_apply, hv,
RandomSystem.extensionLaw_weight,
ite_eq_left (show A.inputDomain h z.1 from hd), hp] at hc
have hz := Distribution.apply_le_mass (R.1.extensionLaw_nonneg h z.1)
(P := fun y => y ≠ v) (a := z.2) hy
rw [RandomSystem.extensionLaw_apply] at hz
exact le_antisymm (by linarith) (R.1.mass_nonneg _)
· have zero : (ofFunction f).1 h = 0 := by
by_contra hn
exact he (ofFunction_answers f h hn)
have hz := R.1.mass_eq_zero_of_prefix (List.prefix_append h [z]) (ih.trans zero)
rw [hz, ofFunction_snoc, ite_eq_left hd, zero, zero_mul]
· rw [R.1.mass_eq_zero_of_not_admitted hd, ofFunction_snoc, ite_eq_right hd]
exact Subtype.ext (RandomSystem.ext mass)
/-- Sample once and retain that choice throughout the interaction. -/
noncomputable def sample {K : Type} (P : Distribution K) [Distribution.IsProbability P]
(R : K → TypedSystem A) : TypedSystem A :=
⟨RandomSystem.mix ⟨P, Distribution.IsProbability.isProbDist P⟩ (fun k => (R k).1), by
intro h x y hne
rw [RandomSystem.mix_mass]
exact Finset.sum_eq_zero fun k _ => by rw [(R k).2 h x y hne, mul_zero]⟩
lemma sample_mass {K : Type} (P : Distribution K) [Distribution.IsProbability P]
(R : K → TypedSystem A) h :
(sample P R).1 h = ∑ k ∈ P.support, P k * (R k).1 h := rfl
/-- **Sampling a pushforward**: drawing `l ← f(P)` once equals drawing `k ← P` once and
retaining `f k`, `sample (f(P)) R = sample P (R ∘ f)`; at every transcript `h`,
`∑ₗ f(P)(l) · R l (h) = ∑ₖ P(k) · R (f k) (h)`. The map `f` need not be injective. -/
lemma sample_fTransform {K L : Type} (f : K → L) (P : Distribution K)
[Distribution.IsProbability P] (R : L → TypedSystem A) :
sample (Distribution.fTransform f P) R = sample P (fun k => R (f k)) := by
-- Equal cumulative masses at every transcript, by the change of variables
-- `∑ₗ f(P)(l) · g l = ∑ₖ P(k) · g (f k)` (`Distribution.sum_fTransform_mul`).
apply Subtype.ext
apply RandomSystem.ext
intro h
exact Distribution.sum_fTransform_mul f P (fun k => (R k).1 h)
/-- The mass of sampled functions is the probability that the draw supplies every reply. -/
lemma sample_ofFunction_mass_eq
-- `A` fixes the query/reply alphabets and domain; `P` is one retained initial draw.
{A : Interface} {K : Type} (P : Distribution K) [Distribution.IsProbability P]
(f : K → ∀ i, A.X i → A.Y i)
-- `h` is the full outside transcript, including repeated inputs.
(h : List ((Σ i, A.X i) × (Σ i, A.Y i))) :
(TypedSystem.sample P (fun k => TypedSystem.ofFunction (f k))).1 h =
P.mass (fun k => (h = [] ∨ A.domain (h.map Prod.fst)) ∧
∀ z ∈ h, z.2 = ⟨z.1.1, f k z.1.1 z.1.2⟩) := by
classical
rw [TypedSystem.sample_mass, Distribution.mass, Finsupp.sum]
apply Finset.sum_congr rfl
intro k _
rw [TypedSystem.ofFunction_mass]
split_ifs <;> simp only [mul_one, mul_zero]
/-- Initial sampling of functions is their existing finite DDS presentation. -/
lemma sample_ofFunction_mass {K : Type} (P : Distribution K)
[Distribution.IsProbability P] (f : K → ∀ i, A.X i → A.Y i)
(h : List ((Σ i, A.X i) × (Σ i, A.Y i))) :
(sample P (fun k => ofFunction (f k))).1 h =
behaviorMass (Distribution.fTransform
(fun k => DDS.ofFunction A.domain A.nonempty_prefix
(fun x => ⟨x.1, f k x.1 x.2⟩)) P) (h.map Prod.fst) (h.map Prod.snd) := by
rw [sample_ofFunction_mass_eq, behaviorMass, Distribution.mass_fTransform]
apply Distribution.mass_congr
intro k
simp only [DDS.replies_ofFunction_iff]
have answers : h.map Prod.snd = (h.map Prod.fst).map (fun x => ⟨x.1, f k x.1 x.2⟩) ↔
∀ z ∈ h, z.2 = ⟨z.1.1, f k z.1.1 z.1.2⟩ := by
rw [List.map_map, List.map_eq_map_iff]
rfl
simp only [answers, List.map_eq_nil_iff]
end TypedSystem
namespace TypedConverter
/-- **Attachment to a retained sample**: `α • sample P R = sample P (fun k => α • R k)`. One
`k ← P` is drawn before the interaction, and every inside query of `α` reaches the same `R k`;
nothing is redrawn per query. This is `PDCBehavior.attach_mix` on typed systems.
```text
A ┌───┐ B ┌─────────────┐ A ┌─────────────┐
────┤ α ├───┤ k ← P, once │ = ────┤ k ← P, once │
└───┘ │ R k │ │ α • R k │
└─────────────┘ └─────────────┘
``` -/
lemma smul_sample {A B : Interface} (α : TypedConverter A B) {K : Type}
(P : Distribution K) [Distribution.IsProbability P] (R : K → TypedSystem B) :
α • TypedSystem.sample P R = TypedSystem.sample P (fun k => α • R k) :=
Subtype.ext (PDCBehavior.attach_mix B.nonempty_prefix A.nonempty_prefix α
⟨P, Distribution.IsProbability.isProbDist P⟩ (fun k => (R k).1)
(fun k => (R k).2) (TypedSystem.sample P R).2)
end TypedConverter
open Lean.Parser.Term in
/-- A function at each named interface. -/
scoped macro "system " i:funBinder x:funBinder " => " f:term : term =>
`(TypedSystem.ofFunction (fun $i $x => $f))
open Lean.Parser.Term in
/-- Annotate a functional system with its interface. -/
scoped macro "system " "[" A:term "] " i:funBinder x:funBinder " => " f:term : term =>
`(TypedSystem.ofFunction (A := $A) (fun $i $x => $f))
/-- Initial sampling scopes over the complete system expression that follows. -/
scoped macro "sample " k:ident " ← " law:term "; " body:term : term =>
`(TypedSystem.sample $law (fun $k => $body))
/-- Initial sampling, with the same sampling symbol as component declarations. -/
scoped macro "sample " k:ident " ←$ " law:term "; " body:term : term =>
`(TypedSystem.sample (show Probability.Distribution.ProbDist _ from $law).1
(fun $k => $body))
end SystemAlgebra