File size: 19,893 Bytes
2f887db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import OAI.Computability.PerfectCompleteness.Construction.FactoredFunctionsLemmas
import OAI.Computability.PerfectCompleteness.Foundations.CanonicalKeys
import OAI.Computability.PerfectCompleteness.Foundations.ClauseSupportLemmas
import OAI.Computability.PerfectCompleteness.Foundations.QuotientTableAgreementLemmas
import OAI.Computability.PerfectCompleteness.Repetition.CleanConditioning

namespace OAI


namespace PerfectCompleteness.CleanCounting

open scoped BigOperators
open UniqueGamesTheorem.Foundations.Games
open FiniteProduct CleanConditioning

noncomputable section

theorem expectation_event_factor {A : Type*} [Fintype A]
    (μ : FiniteDistribution A) (event : A → Bool) (a : ℝ) :
    μ.expectation (fun x => if event x then a else 1) =
      1 - (1 - a) * μ.probability event := by
  have hpoint (x : A) : μ.weight x * (if event x then a else 1) =
      μ.weight x - (1 - a) * (if event x then μ.weight x else 0) := by
    cases event x <;> simp
    ring
  change (∑ x, μ.weight x * (if event x then a else 1)) = _
  simp_rw [hpoint]
  rw [Finset.sum_sub_distrib, ← Finset.mul_sum, μ.normalized]
  rfl

variable {I : Type*} [Fintype I] [DecidableEq I]
  {Ω : I → Type*} [∀ i, Fintype (Ω i)]

def cleanCount (clean : (i : I) → Ω i → Bool) (x : (i : I) → Ω i) : Nat :=
  (Finset.univ.filter fun i => clean i (x i) = true).card

omit [DecidableEq I] [∀ i, Fintype (Ω i)] in
theorem power_cleanCount (clean : (i : I) → Ω i → Bool)
    (x : (i : I) → Ω i) (a : ℝ) :
    a ^ cleanCount clean x = ∏ i, if clean i (x i) = true then a else 1 := by
  rw [← Finset.prod_filter]
  simp [cleanCount]

theorem cleanCount_moment (P : (i : I) → FiniteDistribution (Ω i))
    (clean : (i : I) → Ω i → Bool) (a : ℝ) :
    (law P).expectation (fun x => a ^ cleanCount clean x) =
      ∏ i : I, (1 - (1 - a) * (P i).probability (clean i)) := by
  calc
    _ = (law P).expectation (fun x => ∏ i : I, if clean i (x i) then a else 1) :=
      FiniteDistribution.expectation_congr _
        (fun x => power_cleanCount (I := I) (Ω := Ω) clean x a)
    _ = ∏ i : I, (P i).expectation (fun x => if clean i x then a else 1) :=
      expectation_product (I := I) (Ω := Ω) P
        (fun (i : I) (x : Ω i) => if clean i x then a else 1)
    _ = _ := by
      apply Finset.prod_congr rfl
      intro i _
      exact expectation_event_factor (P i) (clean i) a

theorem cleanCount_moment_le (P : (i : I) → FiniteDistribution (Ω i))
    (clean : (i : I) → Ω i → Bool) (a p₀ : ℝ)
    (ha : 0 ≤ a) (ha' : a ≤ 1)
    (lower : ∀ i, p₀ ≤ (P i).probability (clean i)) :
    (law P).expectation (fun x => a ^ cleanCount clean x) ≤
      (1 - (1 - a) * p₀) ^ Fintype.card I := by
  rw [cleanCount_moment]
  calc
    _ ≤ ∏ _i : I, (1 - (1 - a) * p₀) := by
      apply Finset.prod_le_prod₀
      · intro i _
        have h := mul_le_mul_of_nonneg_left ((P i).probability_le_one (clean i))
          (sub_nonneg.mpr ha')
        nlinarith
      · intro i _
        have h := mul_le_mul_of_nonneg_left (lower i) (sub_nonneg.mpr ha')
        linarith
    _ = _ := by simp


variable {E R T : I → Type*}
  [∀ i, Fintype (E i)] [∀ i, Fintype (R i)] [∀ i, Fintype (T i)]

omit [Fintype I] [DecidableEq I] in
theorem kernel_clean_probability
    (μ : (i : I) → FiniteDistribution (E i))
    (ν : (i : I) → FiniteDistribution (R i))
    (kernel : (i : I) → E i → R i → FiniteDistribution (T i))
    (clean : (i : I) → E i → R i → T i → Bool) (p : I → ℝ)
    (constant : ∀ i e r, (kernel i e r).probability (clean i e r) = p i) (i : I) :
    (kernelJoint (μ i) (fun e => kernelJoint (ν i) (kernel i e))).probability
      (fun x => clean i x.1 x.2.1 x.2.2) = p i := by
  apply probability_kernelJoint_constant (μ i) _ _ (p i)
  intro e
  apply probability_kernelJoint_constant (ν i) _ _ (p i)
  intro r
  exact constant i e r

theorem kernel_cleanCount_moment
    (μ : (i : I) → FiniteDistribution (E i))
    (ν : (i : I) → FiniteDistribution (R i))
    (kernel : (i : I) → E i → R i → FiniteDistribution (T i))
    (clean : (i : I) → E i → R i → T i → Bool) (p : I → ℝ)
    (constant : ∀ i e r, (kernel i e r).probability (clean i e r) = p i) (a : ℝ) :
    (law (fun i => kernelJoint (μ i) (fun e => kernelJoint (ν i) (kernel i e)))).expectation
      (fun x => a ^ cleanCount (I := I) (Ω := fun i => E i × (R i × T i))
        (fun i y => clean i y.1 y.2.1 y.2.2) x) =
        ∏ i : I, (1 - (1 - a) * p i) := by
  rw [cleanCount_moment]
  simp only [kernel_clean_probability μ ν kernel clean p constant]


end
end PerfectCompleteness.CleanCounting


section

namespace PerfectCompleteness.MixedSupport

open ClauseSupport

variable {C Y : Type*}

theorem localKey_unused (s : Slot) (f : C → s.Domain → Y) (unused : Unused f) :
    localKey s f = .dropped := by
  classical
  cases s with
  | clause occurrence variableIDs signs =>
      change clauseKey occurrence variableIDs f = .dropped
      simp only [clauseKey, clauseMode_unused f unused]
  | bit variableID =>
      change (if Unused f then SlotKey.dropped else SlotKey.bit variableID) = SlotKey.dropped
      exact ite_eq_left unused

theorem localReduction_unused (s : Slot) (f : C → s.Domain → Y)
    (unused : Unused f) (u : s.Domain) : localReduction s f u = .dropped := by
  classical
  cases s with
  | clause occurrence variableIDs signs =>
      change clauseReduction f u = .dropped
      simp only [clauseReduction, clauseMode_unused f unused, reduceByMode]
  | bit variableID =>
      change (if Unused f then ReducedValue.dropped else ReducedValue.bit u) = ReducedValue.dropped
      exact ite_eq_left unused

end PerfectCompleteness.MixedSupport

namespace PerfectCompleteness.CleanKeyErasure

open ClauseSupport MixedSupport FactoredFunctions

variable {I Y : Type*} [DecidableEq I]

abbrev Retained (slots : I → Slot) (keep : I → Prop) :=
  (i : {i : I // keep i}) → (slots i.val).Domain

def retain (slots : I → Slot) (keep : I → Prop) :
    Assignment slots → Retained slots keep := fun x i => x i.val

theorem retain_update_of_not_kept (slots : I → Slot) (keep : I → Prop)
    (x : Assignment slots) (i : I) (not_kept : ¬ keep i) (u : (slots i).Domain) :
    retain slots keep (Function.update x i u) = retain slots keep x := by
  funext j
  have hji : j.val ≠ i := by
    intro h
    exact not_kept (h ▸ j.property)
  simp only [retain, Function.update_of_ne hji]

theorem section_unused_of_retained (slots : I → Slot) (keep : I → Prop)
    (f : Assignment slots → Y)
    (constant : ∀ x y, retain slots keep x = retain slots keep y → f x = f y)
    (i : I) (not_kept : ¬ keep i) : Unused (sectionFunction slots f i) := by
  intro context u v
  apply constant
  exact (retain_update_of_not_kept slots keep context i not_kept u).trans
    (retain_update_of_not_kept slots keep context i not_kept v).symm

theorem section_unused_of_factor (slots : I → Slot) (keep : I → Prop)
    (f : Retained slots keep → Y) (i : I) (not_kept : ¬ keep i) :
    Unused (sectionFunction slots (f ∘ retain slots keep) i) := by
  apply section_unused_of_retained slots keep (f ∘ retain slots keep)
    (fun x y h => congrArg f h) i not_kept

theorem keyFields_dropped_of_retained (slots : I → Slot) (keep : I → Prop)
    (f : Assignment slots → Y)
    (constant : ∀ x y, retain slots keep x = retain slots keep y → f x = f y)
    (i : I) (not_kept : ¬ keep i) : keyFields slots f i = .dropped :=
  localKey_unused (slots i) (sectionFunction slots f i)
    (section_unused_of_retained slots keep f constant i not_kept)

theorem reduction_dropped_of_retained (slots : I → Slot) (keep : I → Prop)
    (f : Assignment slots → Y)
    (constant : ∀ x y, retain slots keep x = retain slots keep y → f x = f y)
    (i : I) (not_kept : ¬ keep i) (x : Assignment slots) :
    reduction slots f x i = .dropped :=
  localReduction_unused (slots i) (sectionFunction slots f i)
    (section_unused_of_retained slots keep f constant i not_kept) (x i)

section Scalar

variable {𝕜 : Type*} [Field 𝕜]

theorem section_unused_of_factoring (slots : I → Slot) (keep : I → Prop)
    (f : Assignment slots → 𝕜) (factors : f ∈ factoringSpace (retain slots keep))
    (i : I) (not_kept : ¬ keep i) : Unused (sectionFunction slots f i) :=
  section_unused_of_retained slots keep f factors i not_kept

theorem keyFields_dropped_of_factoring (slots : I → Slot) (keep : I → Prop)
    (f : Assignment slots → 𝕜) (factors : f ∈ factoringSpace (retain slots keep))
    (i : I) (not_kept : ¬ keep i) : keyFields slots f i = .dropped :=
  keyFields_dropped_of_retained slots keep f factors i not_kept

theorem reduction_dropped_of_factoring (slots : I → Slot) (keep : I → Prop)
    (f : Assignment slots → 𝕜) (factors : f ∈ factoringSpace (retain slots keep))
    (i : I) (not_kept : ¬ keep i) (x : Assignment slots) :
    reduction slots f x i = .dropped :=
  reduction_dropped_of_retained slots keep f factors i not_kept x

end Scalar


variable {n : Nat}

theorem retainedKeys_only_kept (slots : Fin n → Slot) (keep : Fin n → Prop)
    (f : Assignment slots → Y)
    (constant : ∀ x y, retain slots keep x = retain slots keep y → f x = f y)
    (entry : Fin n × SlotKey) (present : entry ∈ retainedKeys slots f) : keep entry.1 := by
  classical
  unfold retainedKeys at present
  obtain ⟨in_map, not_dropped⟩ := List.mem_filter.mp present
  obtain ⟨i, _, rfl⟩ := List.mem_map.mp in_map
  by_contra not_kept
  exact (of_decide_eq_true not_dropped)
    (keyFields_dropped_of_retained slots keep f constant i not_kept)

theorem not_mem_retainedKeys_of_not_kept (slots : Fin n → Slot) (keep : Fin n → Prop)
    (f : Assignment slots → Y)
    (constant : ∀ x y, retain slots keep x = retain slots keep y → f x = f y)
    (i : Fin n) (not_kept : ¬ keep i) (metadata : SlotKey) :
    (i, metadata) ∉ retainedKeys slots f := by
  intro present
  exact not_kept (retainedKeys_only_kept slots keep f constant (i, metadata) present)

theorem keyFields_const (slots : Fin n → Slot) (value : Y) (i : Fin n) :
    keyFields slots (fun _ => value) i = .dropped :=
  localKey_unused (slots i) (sectionFunction slots (fun _ => value) i)
    (fun _ _ _ => rfl)

theorem retainedKeys_const (slots : Fin n → Slot) (value : Y) :
    retainedKeys slots (fun _ => value) = [] := by
  apply List.eq_nil_iff_forall_not_mem.mpr
  intro entry present
  exact retainedKeys_only_kept slots (fun _ => False) (fun _ => value)
    (fun _ _ _ => rfl) entry present

end PerfectCompleteness.CleanKeyErasure

end


namespace PerfectCompleteness.CleanKeyFactorization

open ClauseSupport MixedSupport CanonicalKeys CleanKeyErasure
open scoped Classical

noncomputable section

variable {I Y : Type*} [DecidableEq I]

abbrev retainedSlots (slots : I → Slot) (keep : I → Prop) : {i : I // keep i} → Slot :=
  fun i => slots i.val

def fill (slots : I → Slot) (keep : I → Prop)
    (x : Assignment (retainedSlots slots keep)) : Assignment slots :=
  fun i => if h : keep i then x ⟨i, h⟩ else Classical.choice (inferInstance : Nonempty (slots i).Domain)

omit [DecidableEq I] in
theorem retain_fill (slots : I → Slot) (keep : I → Prop)
    (x : Assignment (retainedSlots slots keep)) : retain slots keep (fill slots keep x) = x := by
  funext i
  simp only [retain, fill, dite_eq_left i.property]

omit [DecidableEq I] in
theorem retain_surjective (slots : I → Slot) (keep : I → Prop) :
    Function.Surjective (retain slots keep) :=
  fun x => ⟨fill slots keep x, retain_fill slots keep x⟩

theorem retain_update (slots : I → Slot) (keep : I → Prop)
    (x : Assignment slots) (i : {i : I // keep i}) (u : (slots i.val).Domain) :
    retain slots keep (Function.update x i.val u) =
      Function.update (retain slots keep x) i u :=
  Function.update_comp_eq_of_injective' x Subtype.val_injective i u

theorem sectionFunction_factor (slots : I → Slot) (keep : I → Prop)
    (g : Assignment (retainedSlots slots keep) → Y) (i : {i : I // keep i}) :
    sectionFunction slots (g ∘ retain slots keep) i.val =
      pull (sectionFunction (retainedSlots slots keep) g i) (retain slots keep) id := by
  funext context u
  change g (retain slots keep (Function.update context i.val u)) =
    g (Function.update (retain slots keep context) i u)
  rw [retain_update]

theorem keyFields_kept (slots : I → Slot) (keep : I → Prop)
    (g : Assignment (retainedSlots slots keep) → Y) (i : {i : I // keep i}) :
    keyFields slots (g ∘ retain slots keep) i.val =
      keyFields (retainedSlots slots keep) g i := by
  change localKey (slots i.val) (sectionFunction slots (g ∘ retain slots keep) i.val) = _
  rw [sectionFunction_factor]
  exact localKey_projection (Projection.keep (slots i.val))
    (sectionFunction (retainedSlots slots keep) g i)
    (retain slots keep) (retain_surjective slots keep)

theorem reduction_kept (slots : I → Slot) (keep : I → Prop)
    (g : Assignment (retainedSlots slots keep) → Y) (i : {i : I // keep i})
    (x : Assignment slots) :
    reduction slots (g ∘ retain slots keep) x i.val =
      reduction (retainedSlots slots keep) g (retain slots keep x) i := by
  change localReduction (slots i.val)
    (sectionFunction slots (g ∘ retain slots keep) i.val) (x i.val) = _
  rw [sectionFunction_factor]
  exact localReduction_projection (Projection.keep (slots i.val))
    (sectionFunction (retainedSlots slots keep) g i)
    (retain slots keep) (retain_surjective slots keep) (x i.val)

def embeddedFields (keep : I → Prop) (slots : {i : I // keep i} → Slot)
    (g : Assignment slots → Y) (i : I) : SlotKey :=
  if h : keep i then keyFields slots g ⟨i, h⟩ else .dropped

def embeddedReduction (keep : I → Prop) (slots : {i : I // keep i} → Slot)
    (g : Assignment slots → Y) (x : Assignment slots) (i : I) : ReducedValue :=
  if h : keep i then reduction slots g x ⟨i, h⟩ else .dropped

theorem keyFields_factor (slots : I → Slot) (keep : I → Prop)
    (g : Assignment (retainedSlots slots keep) → Y) :
    keyFields slots (g ∘ retain slots keep) = embeddedFields keep (retainedSlots slots keep) g := by
  funext i
  by_cases hi : keep i
  · simpa only [embeddedFields, dite_eq_left hi] using keyFields_kept slots keep g ⟨i, hi⟩
  · rw [embeddedFields, dite_eq_right hi]
    exact keyFields_dropped_of_retained slots keep (g ∘ retain slots keep)
      (fun x y h => congrArg g h) i hi

theorem reduction_factor (slots : I → Slot) (keep : I → Prop)
    (g : Assignment (retainedSlots slots keep) → Y) (x : Assignment slots) :
    reduction slots (g ∘ retain slots keep) x =
      embeddedReduction keep (retainedSlots slots keep) g (retain slots keep x) := by
  funext i
  by_cases hi : keep i
  · simpa only [embeddedReduction, dite_eq_left hi] using reduction_kept slots keep g ⟨i, hi⟩ x
  · rw [embeddedReduction, dite_eq_right hi]
    exact reduction_dropped_of_retained slots keep (g ∘ retain slots keep)
      (fun x y h => congrArg g h) i hi x

theorem embeddedReduction_constant [Finite I]
    (keep : I → Prop) (slots : {i : I // keep i} → Slot) (g : Assignment slots → Y) :
    ReducedPartition.ConstantOnFibers (embeddedReduction keep slots g) g := by
  intro x y h
  apply constant_on_reduction_fibers slots g
  funext i
  have hi := congrFun h i.val
  simpa only [embeddedReduction, dite_eq_left i.property] using hi


variable {n : Nat}

def retainedMetadata (keep : Fin n → Prop) (slots : {i : Fin n // keep i} → Slot)
    (g : Assignment slots → Y) : List (Fin n × SlotKey) :=
  ((List.finRange n).map (fun i => (i, embeddedFields keep slots g i))).filter
    (fun entry => decide (entry.2 ≠ .dropped))

def retainedKey (side : Side) (keep : Fin n → Prop)
    (slots : {i : Fin n // keep i} → Slot) (g : Assignment slots → Y) : Key n where
  side := side
  retained := retainedMetadata keep slots g
  partition := ReducedPartition.parts (embeddedReduction keep slots g) g

theorem retainedKeys_factor (slots : Fin n → Slot) (keep : Fin n → Prop)
    (g : Assignment (retainedSlots slots keep) → Y) :
    retainedKeys slots (g ∘ retain slots keep) =
      retainedMetadata keep (retainedSlots slots keep) g := by
  simp only [retainedKeys, retainedMetadata, keyFields_factor]

theorem partition_factor (slots : Fin n → Slot) (keep : Fin n → Prop)
    (g : Assignment (retainedSlots slots keep) → Y) :
    CanonicalKeys.partition slots (g ∘ retain slots keep) =
      ReducedPartition.parts (embeddedReduction keep (retainedSlots slots keep) g) g :=
  ReducedPartition.parts_eq_of_pullback (retain slots keep) (retain_surjective slots keep)
    (reduction slots (g ∘ retain slots keep))
    (embeddedReduction keep (retainedSlots slots keep) g)
    (g ∘ retain slots keep) g (reduction_factor slots keep g) (fun _ => rfl)

theorem key_factor (side : Side) (slots : Fin n → Slot) (keep : Fin n → Prop)
    (g : Assignment (retainedSlots slots keep) → Y) :
    key side slots (g ∘ retain slots keep) =
      retainedKey side keep (retainedSlots slots keep) g := by
  simp only [key, retainedKey, retainedKeys_factor, partition_factor]

theorem retainedKey_nonempty (side : Side) (keep : Fin n → Prop)
    (slots : {i : Fin n // keep i} → Slot) (g : Assignment slots → Y) :
    (retainedKey side keep slots g).partition.Nonempty := by
  obtain ⟨x⟩ := (inferInstance : Nonempty (Assignment slots))
  exact ⟨ReducedPartition.fiber (embeddedReduction keep slots g) g x, ⟨x, rfl⟩⟩

def retainedQueryKey (side : Side) (keep : Fin n → Prop)
    (slots : {i : Fin n // keep i} → Slot) (g : Assignment slots → Y) :
    KeyStrategy.NonemptyKey n :=
  ⟨retainedKey side keep slots g, retainedKey_nonempty side keep slots g⟩

theorem queryKey_factor (side : Side) (slots : Fin n → Slot) (keep : Fin n → Prop)
    (g : Assignment (retainedSlots slots keep) → Y) :
    KeyStrategy.queryKey side slots (g ∘ retain slots keep) =
      retainedQueryKey side keep (retainedSlots slots keep) g := by
  apply Subtype.ext
  exact key_factor side slots keep g

theorem label_factor (labeling : KeyStrategy.Strategy n) (side : Side)
    (slots : Fin n → Slot) (keep : Fin n → Prop)
    (g : Assignment (retainedSlots slots keep) → Y) :
    (KeyStrategy.label labeling side slots (g ∘ retain slots keep)).val =
      (labeling (retainedQueryKey side keep (retainedSlots slots keep) g)).val :=
  congrArg (fun k : KeyStrategy.NonemptyKey n => (labeling k).val)
    (queryKey_factor side slots keep g)

def retainedResponse (labeling : KeyStrategy.Strategy n) (side : Side)
    (keep : Fin n → Prop) (slots : {i : Fin n // keep i} → Slot)
    (g : Assignment slots → Y) : Y :=
  ReducedPartition.output (embeddedReduction keep slots g) g
    (labeling (retainedQueryKey side keep slots g))

theorem response_factor (labeling : KeyStrategy.Strategy n) (side : Side)
    (slots : Fin n → Slot) (keep : Fin n → Prop)
    (g : Assignment (retainedSlots slots keep) → Y) :
    KeyStrategy.response labeling side slots (g ∘ retain slots keep) =
      retainedResponse labeling side keep (retainedSlots slots keep) g :=
  ReducedPartition.output_eq_of_shared_part (retain slots keep)
    (reduction slots (g ∘ retain slots keep))
    (embeddedReduction keep (retainedSlots slots keep) g)
    (g ∘ retain slots keep) g (reduction_factor slots keep g) (fun _ => rfl)
    (embeddedReduction_constant keep (retainedSlots slots keep) g)
    (KeyStrategy.label labeling side slots (g ∘ retain slots keep))
    (labeling (retainedQueryKey side keep (retainedSlots slots keep) g))
    (label_factor labeling side slots keep g)


end
end PerfectCompleteness.CleanKeyFactorization

end OAI