File size: 18,852 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
450
import OAI.Computability.PerfectCompleteness.Foundations.PartitionsLemmas
import OAI.Computability.PerfectCompleteness.Foundations.SourceClause

namespace OAI

section

namespace PerfectCompleteness.ClauseSupport

open SourceClause

variable {C C' D E Y Z : Type*}

abbrev Answer (signs : Triple) := {u : Triple // u ∈ satisfyingTriples signs}

def coordinate {signs : Triple} (i : Fin 3) (u : Answer signs) : Bool :=
  u.val.at i

theorem coordinate_surjective (signs : Triple) (i : Fin 3) :
    Function.Surjective (coordinate (signs := signs) i) := by
  intro b
  obtain ⟨u, hu, hb⟩ := SourceClause.coordinate_surjective signs i b
  exact ⟨⟨u, hu⟩, hb⟩

theorem pair_surjective (signs : Triple) (i j : Fin 3) (hij : i ≠ j)
    (a b : Bool) : ∃ u : Answer signs, coordinate i u = a ∧ coordinate j u = b := by
  obtain ⟨u, hu, hi, hj⟩ := SourceClause.pair_surjective signs i j hij a b
  exact ⟨⟨u, hu⟩, hi, hj⟩

instance (signs : Triple) : Nonempty (Answer signs) := by
  obtain ⟨u, _⟩ := coordinate_surjective signs 0 false
  exact ⟨u⟩

def Unused (f : C → D → Y) : Prop := ∀ c u v, f c u = f c v

def Factors (f : C → D → Y) (p : D → E) : Prop :=
  ∀ c u v, p u = p v → f c u = f c v

abbrev Reducible {signs : Triple} (f : C → Answer signs → Y) (i : Fin 3) :=
  Factors f (coordinate i)

theorem unused_of_two_reductions {signs : Triple} (f : C → Answer signs → Y)
    {i j : Fin 3} (hij : i ≠ j) (hi : Reducible f i) (hj : Reducible f j) :
    Unused f := by
  intro c u v
  obtain ⟨w, hw₁, hw₂⟩ := pair_surjective signs i j hij (coordinate i u) (coordinate j v)
  exact (hi c u w hw₁.symm).trans (hj c w v hw₂)

theorem used_reduction_unique {signs : Triple} (f : C → Answer signs → Y)
    (hused : ¬ Unused f) {i j : Fin 3} (hi : Reducible f i) (hj : Reducible f j) :
    i = j := by
  by_contra hij
  exact hused (unused_of_two_reductions f hij hi hj)

def pull (f : C → D → Y) (context : C' → C) (localMap : E → D) : C' → E → Y :=
  fun c u => f (context c) (localMap u)

theorem unused_pull_iff (f : C → D → Y) (context : C' → C) (localMap : E → D)
    (hcontext : Function.Surjective context) (hlocal : Function.Surjective localMap) :
    Unused (pull f context localMap) ↔ Unused f := by
  constructor
  · intro h c u v
    obtain ⟨c', rfl⟩ := hcontext c
    obtain ⟨u', rfl⟩ := hlocal u
    obtain ⟨v', rfl⟩ := hlocal v
    exact h c' u' v'
  · intro h c u v
    exact h (context c) (localMap u) (localMap v)

theorem factors_pull_iff (f : C → D → Y) (context : C' → C) (localMap : E → D)
    (p : D → Z) (hcontext : Function.Surjective context)
    (hlocal : Function.Surjective localMap) :
    Factors (pull f context localMap) (p ∘ localMap) ↔ Factors f p := by
  constructor
  · intro h c u v huv
    obtain ⟨c', rfl⟩ := hcontext c
    obtain ⟨u', rfl⟩ := hlocal u
    obtain ⟨v', rfl⟩ := hlocal v
    exact h c' u' v' huv
  · intro h c u v huv
    exact h (context c) (localMap u) (localMap v) huv

theorem unused_kernel_invariant (f : C → D → Y) (g : C → D → Z)
    (hkernel : ∀ c u v, f c u = f c v ↔ g c u = g c v) :
    Unused f ↔ Unused g := by
  exact ⟨fun h c u v => (hkernel c u v).1 (h c u v),
    fun h c u v => (hkernel c u v).2 (h c u v)⟩

theorem factors_kernel_invariant (f : C → D → Y) (g : C → D → Z) (p : D → E)
    (hkernel : ∀ c u v, f c u = f c v ↔ g c u = g c v) :
    Factors f p ↔ Factors g p := by
  exact ⟨fun h c u v hp => (hkernel c u v).1 (h c u v hp),
    fun h c u v hp => (hkernel c u v).2 (h c u v hp)⟩

inductive Mode where
  | dropped
  | bit (position : Fin 3)
  | full
  deriving DecidableEq

noncomputable def clauseMode {signs : Triple} (f : C → Answer signs → Y) : Mode := by
  classical
  exact if Unused f then .dropped else
    if h : ∃ i, Reducible f i then .bit h.choose else .full

theorem clauseMode_unused {signs : Triple} (f : C → Answer signs → Y)
    (h : Unused f) : clauseMode f = .dropped := by
  simp [clauseMode, h]

theorem clauseMode_bit {signs : Triple} (f : C → Answer signs → Y)
    (hused : ¬ Unused f) (i : Fin 3) (hi : Reducible f i) : clauseMode f = .bit i := by
  classical
  have hex : ∃ j, Reducible f j := ⟨i, hi⟩
  simp only [clauseMode, ite_eq_right hused, dite_eq_left hex]
  exact congrArg Mode.bit (used_reduction_unique f hused hex.choose_spec hi)

theorem clauseMode_full {signs : Triple} (f : C → Answer signs → Y)
    (hused : ¬ Unused f) (hfull : ¬ ∃ i, Reducible f i) : clauseMode f = .full := by
  simp [clauseMode, hused, hfull]

theorem clauseMode_eq_of_predicates {signs : Triple}
    (f : C → Answer signs → Y) (g : C' → Answer signs → Z)
    (hu : Unused f ↔ Unused g) (hr : ∀ i, Reducible f i ↔ Reducible g i) :
    clauseMode f = clauseMode g := by
  classical
  by_cases hf : Unused f
  · rw [clauseMode_unused f hf, clauseMode_unused g (hu.1 hf)]
  · have hg : ¬ Unused g := fun h => hf (hu.2 h)
    by_cases hred : ∃ i, Reducible f i
    · obtain ⟨i, hi⟩ := hred
      rw [clauseMode_bit f hf i hi, clauseMode_bit g hg i ((hr i).1 hi)]
    · have hn : ¬ ∃ i, Reducible g i := by
        rintro ⟨i, hi⟩
        exact hred ⟨i, (hr i).2 hi⟩
      rw [clauseMode_full f hf hred, clauseMode_full g hg hn]

theorem clauseMode_kernel_invariant {signs : Triple}
    (f : C → Answer signs → Y) (g : C → Answer signs → Z)
    (hkernel : ∀ c u v, f c u = f c v ↔ g c u = g c v) :
    clauseMode f = clauseMode g :=
  clauseMode_eq_of_predicates f g (unused_kernel_invariant f g hkernel)
    (fun i => factors_kernel_invariant f g (coordinate i) hkernel)

theorem unprojected_mode_eq {signs : Triple} (f : C → Answer signs → Y)
    (context : C' → C) (hcontext : Function.Surjective context) :
    clauseMode (pull f context id) = clauseMode f := by
  apply clauseMode_eq_of_predicates
  · exact unused_pull_iff f context id hcontext Function.surjective_id
  · intro i
    exact factors_pull_iff f context id (coordinate i) hcontext Function.surjective_id

inductive ReducedValue where
  | dropped
  | bit (value : Bool)
  | full (value : Triple)
  deriving DecidableEq

def reduceByMode {signs : Triple} (mode : Mode) (u : Answer signs) : ReducedValue :=
  match mode with
  | .dropped => .dropped
  | .bit i => .bit (coordinate i u)
  | .full => .full u.val

noncomputable def clauseReduction {signs : Triple} (f : C → Answer signs → Y) :
    Answer signs → ReducedValue := reduceByMode (clauseMode f)

noncomputable def bitReduction (f : C → Bool → Y) (b : Bool) : ReducedValue := by
  classical
  exact if Unused f then .dropped else .bit b

theorem clauseReduction_sound {signs : Triple} (f : C → Answer signs → Y)
    (c : C) (u v : Answer signs) (h : clauseReduction f u = clauseReduction f v) :
    f c u = f c v := by
  classical
  by_cases hu : Unused f
  · exact hu c u v
  · by_cases hr : ∃ i, Reducible f i
    · obtain ⟨i, hi⟩ := hr
      have hm := clauseMode_bit f hu i hi
      have hc : coordinate i u = coordinate i v := by
        simpa only [clauseReduction, hm, reduceByMode, ReducedValue.bit.injEq] using h
      exact hi c u v hc
    · have hm := clauseMode_full f hu hr
      have hv : u.val = v.val := by
        simpa only [clauseReduction, hm, reduceByMode, ReducedValue.full.injEq] using h
      exact congrArg (f c) (Subtype.ext hv)

theorem bitReduction_sound (f : C → Bool → Y) (c : C) (u v : Bool)
    (h : bitReduction f u = bitReduction f v) : f c u = f c v := by
  classical
  by_cases hu : Unused f
  · exact hu c u v
  · have huv : u = v := by simpa [bitReduction, hu] using h
    exact congrArg (f c) huv

inductive SlotKey where
  | dropped
  | bit (variableID : Nat)
  | full (clauseOccurrence : Nat) (signs : Triple)
  deriving DecidableEq

noncomputable def clauseKey {signs : Triple} (occurrence : Nat) (variableIDs : Fin 3 → Nat)
    (f : C → Answer signs → Y) : SlotKey :=
  match clauseMode f with
  | .dropped => .dropped
  | .bit i => .bit (variableIDs i)
  | .full => .full occurrence signs

noncomputable def bitKey (variableID : Nat) (f : C → Bool → Y) : SlotKey := by
  classical
  exact if Unused f then .dropped else .bit variableID

theorem unprojected_clause_key_eq {signs : Triple} (f : C → Answer signs → Y)
    (context : C' → C) (hcontext : Function.Surjective context)
    (occurrence : Nat) (variableIDs : Fin 3 → Nat) :
    clauseKey occurrence variableIDs (pull f context id) = clauseKey occurrence variableIDs f := by
  simp only [clauseKey, unprojected_mode_eq f context hcontext]

theorem unprojected_clause_reduction_eq {signs : Triple} (f : C → Answer signs → Y)
    (context : C' → C) (hcontext : Function.Surjective context) (u : Answer signs) :
    clauseReduction (pull f context id) u = clauseReduction f u := by
  simp only [clauseReduction, unprojected_mode_eq f context hcontext]

theorem unprojected_bit_key_eq (f : C → Bool → Y)
    (context : C' → C) (hcontext : Function.Surjective context) (variableID : Nat) :
    bitKey variableID (pull f context id) = bitKey variableID f := by
  classical
  simp only [bitKey, unused_pull_iff f context id hcontext Function.surjective_id]

theorem unprojected_bit_reduction_eq (f : C → Bool → Y)
    (context : C' → C) (hcontext : Function.Surjective context) (b : Bool) :
    bitReduction (pull f context id) b = bitReduction f b := by
  classical
  simp only [bitReduction, unused_pull_iff f context id hcontext Function.surjective_id]

theorem projected_reducible {signs : Triple} (f : C → Bool → Y)
    (context : C' → C) (i : Fin 3) :
    Reducible (pull f context (coordinate (signs := signs) i)) i := by
  intro c u v h
  exact congrArg (f (context c)) h

theorem projected_unused_iff {signs : Triple} (f : C → Bool → Y)
    (context : C' → C) (hcontext : Function.Surjective context) (i : Fin 3) :
    Unused (pull f context (coordinate (signs := signs) i)) ↔ Unused f :=
  unused_pull_iff f context (coordinate i) hcontext (coordinate_surjective signs i)

theorem projected_key_eq {signs : Triple} (f : C → Bool → Y)
    (context : C' → C) (hcontext : Function.Surjective context)
    (occurrence : Nat) (variableIDs : Fin 3 → Nat) (i : Fin 3) :
    clauseKey occurrence variableIDs (pull f context (coordinate (signs := signs) i)) =
      bitKey (variableIDs i) f := by
  classical
  have hu := projected_unused_iff (signs := signs) f context hcontext i
  by_cases hf : Unused f
  · simp [clauseKey, clauseMode_unused _ (hu.2 hf), bitKey, hf]
  · have hp : ¬ Unused (pull f context (coordinate (signs := signs) i)) :=
      fun h => hf (hu.1 h)
    rw [clauseKey, clauseMode_bit _ hp i (projected_reducible f context i)]
    simp [bitKey, hf]

theorem projected_reduction_eq {signs : Triple} (f : C → Bool → Y)
    (context : C' → C) (hcontext : Function.Surjective context)
    (i : Fin 3) (u : Answer signs) :
    clauseReduction (pull f context (coordinate i)) u = bitReduction f (coordinate i u) := by
  classical
  have hu := projected_unused_iff (signs := signs) f context hcontext i
  by_cases hf : Unused f
  · simp [clauseReduction, clauseMode_unused _ (hu.2 hf), reduceByMode, bitReduction, hf]
  · have hp : ¬ Unused (pull f context (coordinate (signs := signs) i)) :=
      fun h => hf (hu.1 h)
    rw [clauseReduction, clauseMode_bit _ hp i (projected_reducible f context i)]
    simp [reduceByMode, bitReduction, hf]

end PerfectCompleteness.ClauseSupport

end

section

namespace PerfectCompleteness.MixedSupport

open ClauseSupport

variable {I C C' Y : Type*}

inductive Slot where
  | clause (occurrence : Nat) (variableIDs : Fin 3 → Nat) (signs : SourceClause.Triple)
  | bit (variableID : Nat)

def Slot.Domain : Slot → Type
  | .clause _ _ signs => Answer signs
  | .bit _ => Bool

instance (s : Slot) : Nonempty s.Domain := by
  cases s <;> dsimp [Slot.Domain] <;> infer_instance

abbrev Assignment (slots : I → Slot) := ∀ i, (slots i).Domain

inductive Projection : Slot → Slot → Type
  | keep (s : Slot) : Projection s s
  | select (occurrence : Nat) (variableIDs : Fin 3 → Nat)
      (signs : SourceClause.Triple) (position : Fin 3) :
      Projection (.clause occurrence variableIDs signs) (.bit (variableIDs position))

def Projection.map {s t : Slot} : Projection s t → s.Domain → t.Domain
  | .keep _ => id
  | .select _ _ _ i => coordinate i

theorem Projection.surjective {s t : Slot} (p : Projection s t) :
    Function.Surjective p.map := by
  cases p with
  | keep s => exact Function.surjective_id
  | select occurrence variableIDs signs i => exact coordinate_surjective signs i

def projectionMap {slots projected : I → Slot}
    (p : ∀ i, Projection (slots i) (projected i)) :
    Assignment slots → Assignment projected :=
  ProductReduction.productMap (fun i => (p i).map)

theorem projectionMap_surjective {slots projected : I → Slot}
    (p : ∀ i, Projection (slots i) (projected i)) :
    Function.Surjective (projectionMap p) :=
  ProductReduction.productMap_surjective _ (fun i => (p i).surjective)

variable [DecidableEq I]

theorem projectionMap_update {slots projected : I → Slot}
    (p : ∀ i, Projection (slots i) (projected i))
    (x : Assignment slots) (i : I) (u : (slots i).Domain) :
    projectionMap p (Function.update x i u) =
      Function.update (projectionMap p x) i ((p i).map u) := by
  funext j
  by_cases hji : j = i
  · subst j
    simp [projectionMap, ProductReduction.productMap]
  · simp [projectionMap, ProductReduction.productMap, Function.update_of_ne hji]

def sectionFunction (slots : I → Slot) (f : Assignment slots → Y) (i : I) :
    Assignment slots → (slots i).Domain → Y :=
  fun context u => f (Function.update context i u)

theorem sectionFunction_pullback {slots projected : I → Slot}
    (p : ∀ i, Projection (slots i) (projected i))
    (f : Assignment projected → Y) (i : I) :
    sectionFunction slots (f ∘ projectionMap p) i =
      pull (sectionFunction projected f i) (projectionMap p) (p i).map := by
  funext context u
  exact congrArg f (projectionMap_update p context i u)

noncomputable def localKey (s : Slot) : (C → s.Domain → Y) → SlotKey :=
  match s with
  | .clause occurrence variableIDs _ => clauseKey occurrence variableIDs
  | .bit variableID => bitKey variableID

noncomputable def localReduction (s : Slot) : (C → s.Domain → Y) → s.Domain → ReducedValue :=
  match s with
  | .clause _ _ _ => clauseReduction
  | .bit _ => bitReduction

theorem localReduction_sound (s : Slot) (f : C → s.Domain → Y)
    (c : C) (u v : s.Domain) (h : localReduction s f u = localReduction s f v) :
    f c u = f c v := by
  cases s with
  | clause occurrence variableIDs signs => exact clauseReduction_sound f c u v h
  | bit variableID => exact bitReduction_sound f c u v h

theorem localKey_projection {s t : Slot} (p : Projection s t)
    (f : C → t.Domain → Y) (context : C' → C)
    (hcontext : Function.Surjective context) :
    localKey s (pull f context p.map) = localKey t f := by
  cases p with
  | keep s =>
    cases s with
    | clause occurrence variableIDs signs =>
      exact unprojected_clause_key_eq f context hcontext occurrence variableIDs
    | bit variableID => exact unprojected_bit_key_eq f context hcontext variableID
  | select occurrence variableIDs signs i =>
    exact projected_key_eq f context hcontext occurrence variableIDs i

theorem localReduction_projection {s t : Slot} (p : Projection s t)
    (f : C → t.Domain → Y) (context : C' → C)
    (hcontext : Function.Surjective context) (u : s.Domain) :
    localReduction s (pull f context p.map) u = localReduction t f (p.map u) := by
  cases p with
  | keep s =>
    cases s with
    | clause occurrence variableIDs signs =>
      exact unprojected_clause_reduction_eq f context hcontext u
    | bit variableID => exact unprojected_bit_reduction_eq f context hcontext u
  | select occurrence variableIDs signs i =>
    exact projected_reduction_eq f context hcontext i u

noncomputable def keyFields (slots : I → Slot) (f : Assignment slots → Y) : I → SlotKey :=
  fun i => localKey (slots i) (sectionFunction slots f i)

noncomputable def coordinateReduction (slots : I → Slot) (f : Assignment slots → Y)
    (i : I) : (slots i).Domain → ReducedValue :=
  localReduction (slots i) (sectionFunction slots f i)

noncomputable def reduction (slots : I → Slot) (f : Assignment slots → Y) :
    Assignment slots → I → ReducedValue :=
  ProductReduction.productMap (coordinateReduction slots f)

theorem keyFields_projection {slots projected : I → Slot}
    (p : ∀ i, Projection (slots i) (projected i)) (f : Assignment projected → Y) :
    keyFields slots (f ∘ projectionMap p) = keyFields projected f := by
  funext i
  unfold keyFields
  rw [sectionFunction_pullback]
  exact localKey_projection (p i) (sectionFunction projected f i)
    (projectionMap p) (projectionMap_surjective p)

theorem reduction_projection {slots projected : I → Slot}
    (p : ∀ i, Projection (slots i) (projected i))
    (f : Assignment projected → Y) (x : Assignment slots) :
    reduction slots (f ∘ projectionMap p) x = reduction projected f (projectionMap p x) := by
  funext i
  change localReduction (slots i) (sectionFunction slots (f ∘ projectionMap p) i) (x i) = _
  rw [sectionFunction_pullback]
  exact localReduction_projection (p i) (sectionFunction projected f i)
    (projectionMap p) (projectionMap_surjective p) (x i)

theorem coordinateInvariant (slots : I → Slot) (f : Assignment slots → Y) :
    ProductReduction.CoordinateInvariant (coordinateReduction slots f) f := by
  apply ProductReduction.coordinateInvariant_of_update
  intro i x u h
  have hs := localReduction_sound (slots i) (sectionFunction slots f i) x (x i) u h
  simpa only [sectionFunction, Function.update_eq_self] using hs

theorem constant_on_reduction_fibers [Finite I]
    (slots : I → Slot) (f : Assignment slots → Y) {x x' : Assignment slots}
    (h : reduction slots f x = reduction slots f x') : f x = f x' :=
  ProductReduction.constant_on_product_fibers _ f (coordinateInvariant slots f) h

noncomputable def retainedKeys {n : Nat} (slots : Fin n → Slot)
    (f : Assignment slots → Y) : List (Fin n × SlotKey) :=
  ((List.finRange n).map (fun i => (i, keyFields slots f i))).filter
    (fun entry => decide (entry.2 ≠ .dropped))

theorem retainedKeys_projection {n : Nat} {slots projected : Fin n → Slot}
    (p : ∀ i, Projection (slots i) (projected i)) (f : Assignment projected → Y) :
    retainedKeys slots (f ∘ projectionMap p) = retainedKeys projected f := by
  simp only [retainedKeys, keyFields_projection]

end PerfectCompleteness.MixedSupport

end

end OAI