File size: 11,469 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
import OAI.Computability.PerfectCompleteness.Foundations.CanonicalPartialTable
import OAI.Computability.PerfectCompleteness.Machines.CanonicalCoarseningEncoding
import OAI.Computability.PerfectCompleteness.Machines.MetadataFreeTapeLemmas

namespace OAI


namespace PerfectCompleteness.FixedQueryCodec

open HierarchicalArrays (Nodes Block)
open MixedSupport
open scoped Classical

noncomputable section

abbrev LeftOutput (branch : Nat → Nat) (n : Nat) (rows : Nat → Nat) :=
  HierarchicalArrays.Output branch n rows

def leftCount (branch : Nat → Nat) (n : Nat) (rows : Nat → Nat) : Nat :=
  Fintype.card (LeftOutput branch n rows)

def leftEquiv (branch : Nat → Nat) (n : Nat) (rows : Nat → Nat) :
    LeftOutput branch n rows ≃ Fin (leftCount branch n rows) :=
  MetadataFreeSampler.outputEquiv branch n rows

def leftEncode (branch : Nat → Nat) (n : Nat) (rows : Nat → Nat) :
    LeftOutput branch n rows → Fin (leftCount branch n rows) :=
  leftEquiv branch n rows

theorem leftEncode_injective (branch : Nat → Nat) (n : Nat) (rows : Nat → Nat) :
    Function.Injective (leftEncode branch n rows) :=
  (leftEquiv branch n rows).injective

abbrev TestIndex (branch : Nat → Nat) (n : Nat) (rows : Nat → Nat) :=
  Σ node : Nodes branch n, BucketSampler.Direction (rows (Nodes.height node))

instance testIndexFintype (branch : Nat → Nat) (n : Nat) (rows : Nat → Nat) :
    Fintype (TestIndex branch n rows) := by
  unfold TestIndex
  infer_instance

variable {branch : Nat → Nat} {n t v m : Nat} {rows repeats : Nat → Nat}

abbrev RightOutput (θ : TestIndex branch n rows) : Type :=
  DirectionQuotient.Space θ.2.val ×
    (BlockQuotient.OtherBlocks (fun node : Nodes branch n => Block rows node) θ.1 × PUnit.{1})

instance rightOutputFintype (θ : TestIndex branch n rows) : Fintype (RightOutput θ) :=
  Fintype.ofFinite _

def project (θ : TestIndex branch n rows) : LeftOutput branch n rows → RightOutput θ :=
  BlockQuotient.projectBlock (I := Nodes branch n)
    (V := fun node : Nodes branch n => Block rows node) (B := PUnit.{1}) θ.1 θ.2.val

abbrev TaggedRight (branch : Nat → Nat) (n : Nat) (rows : Nat → Nat) : Type :=
  Σ θ : TestIndex branch n rows, RightOutput θ

instance taggedRightFintype (branch : Nat → Nat) (n : Nat) (rows : Nat → Nat) :
    Fintype (TaggedRight branch n rows) := by
  letI : (θ : TestIndex branch n rows) → Fintype (RightOutput θ) :=
    fun θ => rightOutputFintype θ
  unfold TaggedRight
  infer_instance

def rightCount (branch : Nat → Nat) (n : Nat) (rows : Nat → Nat) : Nat :=
  Fintype.card (TaggedRight branch n rows)

def rightEquiv (branch : Nat → Nat) (n : Nat) (rows : Nat → Nat) :
    TaggedRight branch n rows ≃ Fin (rightCount branch n rows) :=
  Fintype.equivFin _

def rightEncode (θ : TestIndex branch n rows) :
    RightOutput θ → Fin (rightCount branch n rows) :=
  fun z => rightEquiv branch n rows ⟨θ, z⟩

theorem rightEncode_injective (θ : TestIndex branch n rows) :
    Function.Injective (rightEncode θ) :=
  (rightEquiv branch n rows).injective.comp (sigma_mk_injective (i := θ))

def outputMap (θ : TestIndex branch n rows) :
    Fin (leftCount branch n rows) → Fin (rightCount branch n rows) :=
  fun a => rightEncode θ (project θ ((leftEquiv branch n rows).symm a))

@[simp] theorem outputMap_leftEncode (θ : TestIndex branch n rows)
    (y : LeftOutput branch n rows) :
    outputMap θ (leftEncode branch n rows y) = rightEncode θ (project θ y) := by
  change rightEncode θ (project θ
    ((leftEquiv branch n rows).symm (leftEquiv branch n rows y))) = _
  rw [(leftEquiv branch n rows).symm_apply_apply]

def decodedFiberEquiv (θ : TestIndex branch n rows) (z : RightOutput θ) :
    {a : Fin (leftCount branch n rows) //
      project θ ((leftEquiv branch n rows).symm a) = z} ≃
    {y : LeftOutput branch n rows // project θ y = z} :=
  Equiv.subtypeEquivOfSubtype (p := fun y : LeftOutput branch n rows => project θ y = z)
    (leftEquiv branch n rows).symm

theorem outputMap_fiber_le_two (θ : TestIndex branch n rows)
    (b : Fin (rightCount branch n rows)) :
    Nat.card {a : Fin (leftCount branch n rows) // outputMap θ a = b} ≤ 2 := by
  have hsmall : ∀ z : RightOutput θ,
      Nat.card {a : Fin (leftCount branch n rows) //
        project θ ((leftEquiv branch n rows).symm a) = z} ≤ 2 := by
    intro z
    have hc := (Nat.card_congr (decodedFiberEquiv θ z)).trans
      (BlockQuotient.card_projectBlock_fiber (I := Nodes branch n)
        (V := fun node : Nodes branch n => Block rows node) (B := PUnit.{1})
        θ.1 θ.2.val θ.2.property z)
    exact hc.le
  by_cases hb : ∃ z : RightOutput θ, rightEncode θ z = b
  · obtain ⟨z, rfl⟩ := hb
    let e :
        {a : Fin (leftCount branch n rows) // outputMap θ a = rightEncode θ z} ≃
        {a : Fin (leftCount branch n rows) //
          project θ ((leftEquiv branch n rows).symm a) = z} :=
      Equiv.subtypeEquivRight (fun a => by
        change (rightEncode θ (project θ ((leftEquiv branch n rows).symm a)) =
          rightEncode θ z) ↔ _
        exact ⟨fun h => (rightEncode_injective θ) h,
          fun h => congrArg (rightEncode θ) h⟩)
    exact (Nat.card_congr e).le.trans (hsmall z)
  · let : IsEmpty {a : Fin (leftCount branch n rows) // outputMap θ a = b} :=
      ⟨fun a => hb
        ⟨project θ ((leftEquiv branch n rows).symm a.val), a.property⟩⟩
    rw [Nat.card_of_isEmpty]
    exact Nat.zero_le 2

section Keys

variable {k : Nat}

theorem left_key (side : CanonicalKeys.Side) (slots : Fin k → Slot)
    (f : Assignment slots → LeftOutput branch n rows) :
    CanonicalKeys.key side slots (leftEncode branch n rows ∘ f) =
      CanonicalKeys.key side slots f :=
  CanonicalCoarseningEncoding.key_fine side slots f
    (leftEncode branch n rows) (leftEncode_injective branch n rows)

theorem right_key (side : CanonicalKeys.Side) (slots : Fin k → Slot)
    (f : Assignment slots → LeftOutput branch n rows) (θ : TestIndex branch n rows) :
    CanonicalKeys.key side slots (outputMap θ ∘ (leftEncode branch n rows ∘ f)) =
      CanonicalKeys.key side slots (project θ ∘ f) :=
  CanonicalCoarseningEncoding.key_coarse side slots f (project θ)
    (leftEncode branch n rows) (rightEncode θ) (outputMap θ)
    (rightEncode_injective θ) (outputMap_leftEncode θ)

theorem right_kernel (slots : Fin k → Slot)
    (f : Assignment slots → LeftOutput branch n rows) (θ : TestIndex branch n rows) :
    ∀ x x', (project θ ∘ f) x = (project θ ∘ f) x' ↔
      (outputMap θ ∘ (leftEncode branch n rows ∘ f)) x =
        (outputMap θ ∘ (leftEncode branch n rows ∘ f)) x' :=
  CanonicalCoarseningEncoding.coarseKernel slots f (project θ)
    (leftEncode branch n rows) (rightEncode θ) (outputMap θ)
    (rightEncode_injective θ) (outputMap_leftEncode θ)

end Keys

def leftTable {slots : RecursiveSpaces.Slots branch n → Fin t → Slot}
    (arrays : HierarchicalArrays.Arrays slots rows) :
    Assignment (TreeCanonical.numberedSlots slots) → Fin (leftCount branch n rows) :=
  leftEncode branch n rows ∘
    TreeCanonical.numberedFunction slots (HierarchicalArrays.fullJoint arrays)

theorem leftTable_key (side : CanonicalKeys.Side)
    {slots : RecursiveSpaces.Slots branch n → Fin t → Slot}
    (arrays : HierarchicalArrays.Arrays slots rows) :
    CanonicalKeys.key side (TreeCanonical.numberedSlots slots) (leftTable arrays) =
      TreeCanonical.key side slots (HierarchicalArrays.fullJoint arrays) :=
  left_key side (TreeCanonical.numberedSlots slots)
    (TreeCanonical.numberedFunction slots (HierarchicalArrays.fullJoint arrays))

theorem rightTable_key (side : CanonicalKeys.Side)
    {slots : RecursiveSpaces.Slots branch n → Fin t → Slot}
    (arrays : HierarchicalArrays.Arrays slots rows) (θ : TestIndex branch n rows) :
    CanonicalKeys.key side (TreeCanonical.numberedSlots slots) (outputMap θ ∘ leftTable arrays) =
      TreeCanonical.key side slots (project θ ∘ HierarchicalArrays.fullJoint arrays) :=
  right_key side (TreeCanonical.numberedSlots slots)
    (TreeCanonical.numberedFunction slots (HierarchicalArrays.fullJoint arrays)) θ

def testIndex (data : MetadataFreeTape.Input branch n t rows repeats) :
    TestIndex branch n rows :=
  ⟨GeometricPath.nodeAtLevel data.2.1 data.2.2.2.1, data.2.2.2.2⟩

def rawTestIndex (clauses : Fin m → SourceClause.NormalizedClause v)
    (e : PreliminarySampler.Raw clauses branch n t rows repeats) : TestIndex branch n rows :=
  ⟨GeometricPath.nodeAtLevel e.1.2 e.2.2.1, e.2.2.2⟩

@[simp] theorem testIndex_encodeRaw (clauses : Fin m → SourceClause.NormalizedClause v)
    (e : PreliminarySampler.Raw clauses branch n t rows repeats) :
    testIndex (MetadataFreeTape.encodeRaw clauses rows repeats e) = rawTestIndex clauses e := rfl

def withProjection {a k l : Nat} (fine : CanonicalKeyShape.Input a k)
    (p : Fin k → Fin l) : CanonicalPartialTable.Input a k l :=
  ⟨fine.1, fine.2, p⟩

def partialInput (data : MetadataFreeTape.Input branch n t rows repeats) :
    CanonicalPartialTable.Input (TreeCanonical.locationCount branch n t)
      (leftCount branch n rows) (rightCount branch n rows) :=
  withProjection (MetadataFreeTape.keyInput data) (outputMap (testIndex data))

theorem partialInput_encodeRaw (clauses : Fin m → SourceClause.NormalizedClause v)
    (e : PreliminarySampler.Raw clauses branch n t rows repeats) :
    partialInput (MetadataFreeTape.encodeRaw clauses rows repeats e) =
      CanonicalPartialTable.input
        (TreeCanonical.numberedSlots (MetadataFreeSampler.sourceSlots clauses e.1.1))
        (leftTable (WholeArraySampler.evaluate rows repeats (GeometricPath.leafPath e.1.2)
          (MetadataFreeSampler.sourceSlots clauses e.1.1) e.2.1))
        (outputMap (rawTestIndex clauses e)) := by
  unfold partialInput
  rw [MetadataFreeTape.keyInput_encodeRaw, testIndex_encodeRaw]
  rfl

def rightInputOfPartial {a k l : Nat} (data : CanonicalPartialTable.Input a k l) :
    CanonicalKeyShape.Input a l :=
  ⟨data.1, data.2.2 ∘ data.2.1⟩

def rightKeyInput (data : MetadataFreeTape.Input branch n t rows repeats) :
    CanonicalKeyShape.Input (TreeCanonical.locationCount branch n t)
      (rightCount branch n rows) :=
  rightInputOfPartial (partialInput data)

theorem rightKeyInput_encodeRaw (clauses : Fin m → SourceClause.NormalizedClause v)
    (e : PreliminarySampler.Raw clauses branch n t rows repeats) :
    rightKeyInput (MetadataFreeTape.encodeRaw clauses rows repeats e) =
      CanonicalKeyShape.input
        (TreeCanonical.numberedSlots (MetadataFreeSampler.sourceSlots clauses e.1.1))
        (outputMap (rawTestIndex clauses e) ∘
          leftTable (WholeArraySampler.evaluate rows repeats (GeometricPath.leafPath e.1.2)
            (MetadataFreeSampler.sourceSlots clauses e.1.1) e.2.1)) := by
  rw [rightKeyInput, partialInput_encodeRaw]
  rfl

theorem partialInput_admissible {q : Nat} (hq : 0 < q)
    (large : CanonicalKeyEncoding.partitionWidth (TreeCanonical.locationCount branch n t) ≤ q)
    (data : MetadataFreeTape.Input branch n t rows repeats) :
    LocalCompletionFamily.Admissible
      (CanonicalPartialTable.lookup hq large (partialInput data)) :=
  CanonicalPartialTable.admissible_of_output_fibers hq large (partialInput data)
    (outputMap_fiber_le_two (testIndex data))

end
end PerfectCompleteness.FixedQueryCodec

end OAI