File size: 10,957 Bytes
4a56cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Algorithms.SubexponentialSAT.LeanCompat
import OAI.Algorithms.SubexponentialSAT.SubexpLayoutSpec
import OAI.Algorithms.SubexponentialSAT.ComputablePackingSplit
import OAI.Algorithms.SubexponentialSAT.Subexp.ConstructivePacking

namespace OAI.Algorithms

/-!
# Explicit enumeration by copying local block coordinates

The input is an explicit local decomposition.  Global coordinates are obtained
by copying this decomposition into each packed block and retaining the values
on leftover coordinates.  Finite tuples are encoded by ordinary mixed-radix
equivalences.  No basis of the global vector space is chosen or searched for.
-/

namespace SubexponentialSAT.Subexp.BlockwiseEnumeration

open scoped BigOperators

/-- Finite local coordinate data, supplied by the searches on one universal
block.  The inverses are data, not existentially chosen coordinate maps. -/
structure LocalData (K : Type*) [Semiring K] (U : Type*) [Fintype U]
    (r : β„•) (W : Fin r β†’ Submodule K (U β†’ K)) where
  outerCount : β„•
  innerCount : Fin r β†’ β„•
  outerBase : Fin outerCount β†’ U β†’ K
  incrementEquiv : βˆ€ g, Fin (innerCount g) ≃ W g
  assignmentEquiv : (U β†’ K) ≃ (Fin outerCount Γ— βˆ€ g, Fin (innerCount g))
  reconstruction : βˆ€ o x, assignmentEquiv.symm (o, x) =
    outerBase o + βˆ‘ g, (incrementEquiv g (x g)).val

/-- Explicit base-`p` encoding of a vector over the prime residue ring. -/
def zmodVectorEquiv (p n : β„•) [NeZero p] :
    (Fin n β†’ ZMod p) ≃ Fin (p ^ n) :=
  (Equiv.piCongrRight fun _ => (ZMod.finEquiv p).symm.toEquiv).trans
    finFunctionFinEquiv

/-- Reindexing a function along supplied finite coordinate data. -/
def reindexFunction {A B K : Type*} (e : A ≃ B) : (B β†’ K) ≃ (A β†’ K) where
  toFun f a := f (e a)
  invFun f b := f (e.symm b)
  left_inv f := by funext b; simp
  right_inv f := by funext a; simp

section Packed

variable {K I U V : Type*} [Field K] [Fintype I] [Fintype U]
  [DecidableEq V] {r : β„•} {W : Fin r β†’ Submodule K (U β†’ K)}

open ComputablePackingSplit

/-- The outer coordinates are the untouched values and one local outer index
for every block. -/
abbrev PackedOuter (e : I Γ— U β†ͺ V) (D : LocalData K U r W) :=
  (Leftover e β†’ K) Γ— (I β†’ Fin D.outerCount)

/-- One local inner index in every block. -/
abbrev PackedInner (D : LocalData K U r W) (g : Fin r) :=
  I β†’ Fin (D.innerCount g)

/-- Restrict to each block, apply its supplied local coordinate map, and keep
the leftover values. The inverse uses only local reconstruction and scatter. -/
def packedAssignmentEquiv (e : I Γ— U β†ͺ V) (D : LocalData K U r W) :
    (V β†’ K) ≃ (PackedOuter e D Γ— βˆ€ g, PackedInner (I := I) D g) where
  toFun v :=
    (((split e v).2, fun i => (D.assignmentEquiv ((split e v).1 i)).1),
      fun g i => (D.assignmentEquiv ((split e v).1 i)).2 g)
  invFun z := (split e).symm
    (fun i => D.assignmentEquiv.symm (z.1.2 i, fun g => z.2 g i), z.1.1)
  left_inv v := by
    apply (split e).injective
    rw [LinearEquiv.apply_symm_apply]
    apply Prod.ext
    Β· funext i
      exact D.assignmentEquiv.symm_apply_apply _
    Β· rfl
  right_inv z := by
    dsimp only
    rw [LinearEquiv.apply_symm_apply]
    apply Prod.ext
    Β· apply Prod.ext
      Β· rfl
      Β· funext i
        exact congrArg Prod.fst (D.assignmentEquiv.apply_symm_apply _)
    Β· funext g i
      exact congrFun (congrArg Prod.snd (D.assignmentEquiv.apply_symm_apply _)) g

/-- Scatter one local vector from the selected color into each block. -/
def packedIncrementEquiv (e : I Γ— U β†ͺ V) (D : LocalData K U r W) (g : Fin r) :
    PackedInner (I := I) D g ≃ DirectSums.packedSubspace e (W g) where
  toFun x := ⟨assemble e (fun i => (D.incrementEquiv g (x i)).val, 0), by
    apply (DirectSums.mem_packedSubspace_iff e (W g) _).mpr
    constructor
    Β· intro i
      simpa only [assemble_on_block] using (D.incrementEquiv g (x i)).property
    Β· intro v hv
      exact assemble_outside e _ v hv⟩
  invFun v i := (D.incrementEquiv g).symm
    ⟨fun u => v.val (e (i, u)), DirectSums.packed_restriction_mem e (W g) v.property i⟩
  left_inv x := by
    funext i
    apply (D.incrementEquiv g).symm_apply_eq.mpr
    apply Subtype.ext
    funext u
    exact assemble_on_block e _ i u
  right_inv v := by
    apply Subtype.ext
    funext a
    change assemble e _ a = v.val a
    by_cases ha : a ∈ Set.range e
    · obtain ⟨⟨i, u⟩, rfl⟩ := ha
      rw [assemble_on_block]
      exact congrFun (congrArg Subtype.val
        ((D.incrementEquiv g).apply_symm_apply _)) u
    Β· rw [assemble_outside e _ a ha]
      exact (DirectSums.packed_zero_outside e (W g) v.property a ha).symm

/-- Copy the local complement values and the untouched coordinates. -/
def packedOuterBase (e : I Γ— U β†ͺ V) (D : LocalData K U r W)
    (o : PackedOuter e D) : V β†’ K :=
  assemble e (fun i => D.outerBase (o.2 i), o.1)

theorem packed_reconstruction (e : I Γ— U β†ͺ V) (D : LocalData K U r W)
    (o : PackedOuter e D) (x : βˆ€ g, PackedInner (I := I) D g) :
    (packedAssignmentEquiv e D).symm (o, x) =
      packedOuterBase e D o + βˆ‘ g, (packedIncrementEquiv e D g (x g)).val := by
  funext a
  by_cases ha : a ∈ Set.range e
  · obtain ⟨⟨i, u⟩, rfl⟩ := ha
    change assemble e _ (e (i, u)) = _
    simp only [assemble_on_block, packedOuterBase, packedIncrementEquiv,
      Equiv.coe_fn_mk, Pi.add_apply, Finset.sum_apply]
    simpa only [Pi.add_apply, Finset.sum_apply] using
      congrFun (D.reconstruction (o.2 i) (fun g => x g i)) u
  Β· change assemble e _ a = _
    simp only [assemble_outside e _ a ha, packedOuterBase, packedIncrementEquiv,
      Equiv.coe_fn_mk, Pi.add_apply, Finset.sum_apply, Pi.zero_apply,
      Finset.sum_const_zero, add_zero]

end Packed

section FiniteIndices

variable {p b l r : β„•} [Fact p.Prime]
  {U V : Type*} [Fintype U] [Fintype V] [DecidableEq V]
  {W : Fin r β†’ Submodule (ZMod p) (U β†’ ZMod p)}

/-- A numerical index for the untouched values and copied local complements.
The supplied leftover enumeration is coordinate data, not a chosen basis. -/
def outerIndexEquiv (e : Fin b Γ— U β†ͺ V) (D : LocalData (ZMod p) U r W)
    (leftoverEnum : Fin l ≃ ComputablePackingSplit.Leftover e) :
    PackedOuter e D ≃ Fin (p ^ l * D.outerCount ^ b) :=
  (Equiv.prodCongr
    ((reindexFunction leftoverEnum).trans (zmodVectorEquiv p l))
    finFunctionFinEquiv).trans finProdFinEquiv

/-- Encode the independent local inner indices in base `innerCount g`. -/
def innerIndexEquiv (D : LocalData (ZMod p) U r W) (g : Fin r) :
    PackedInner (I := Fin b) D g ≃ Fin (D.innerCount g ^ b) :=
  finFunctionFinEquiv

/-- The explicit global coordinate data. Only restriction, local coordinate
conversion, scattering, and mixed-radix arithmetic appear in the definitions. -/
def packedData (e : Fin b Γ— U β†ͺ V) (D : LocalData (ZMod p) U r W)
    (leftoverEnum : Fin l ≃ ComputablePackingSplit.Leftover e)
    (G : Fin r β†’ Submodule (ZMod p) (V β†’ ZMod p))
    (hG : βˆ€ g, DirectSums.packedSubspace e (W g) = G g) :
    LocalData (ZMod p) V r G where
  outerCount := p ^ l * D.outerCount ^ b
  innerCount g := D.innerCount g ^ b
  outerBase o := packedOuterBase e D ((outerIndexEquiv e D leftoverEnum).symm o)
  incrementEquiv g := (innerIndexEquiv D g).symm.trans
    { toFun := fun x => ⟨(packedIncrementEquiv e D g x).val, by
        rw [← hG g]
        exact (packedIncrementEquiv e D g x).property⟩
      invFun := fun x => (packedIncrementEquiv e D g).symm ⟨x.val, by
        rw [hG g]
        exact x.property⟩
      left_inv := fun x => (packedIncrementEquiv e D g).symm_apply_apply x
      right_inv := by
        intro x
        apply Subtype.ext
        change ((packedIncrementEquiv e D g)
          ((packedIncrementEquiv e D g).symm ⟨x.val, _⟩)).val = x.val
        exact congrArg Subtype.val ((packedIncrementEquiv e D g).apply_symm_apply _) }
  assignmentEquiv := (packedAssignmentEquiv e D).trans
    (Equiv.prodCongr (outerIndexEquiv e D leftoverEnum)
      (Equiv.piCongrRight fun g => innerIndexEquiv D g))
  reconstruction o x := by
    exact packed_reconstruction e D ((outerIndexEquiv e D leftoverEnum).symm o)
      (fun g => (innerIndexEquiv D g).symm (x g))

@[simp] theorem packedData_outerCount (e : Fin b Γ— U β†ͺ V)
    (D : LocalData (ZMod p) U r W)
    (leftoverEnum : Fin l ≃ ComputablePackingSplit.Leftover e)
    (G : Fin r β†’ Submodule (ZMod p) (V β†’ ZMod p))
    (hG : βˆ€ g, DirectSums.packedSubspace e (W g) = G g) :
    (packedData e D leftoverEnum G hG).outerCount = p ^ l * D.outerCount ^ b := rfl

@[simp] theorem packedData_innerCount (e : Fin b Γ— U β†ͺ V)
    (D : LocalData (ZMod p) U r W)
    (leftoverEnum : Fin l ≃ ComputablePackingSplit.Leftover e)
    (G : Fin r β†’ Submodule (ZMod p) (V β†’ ZMod p))
    (hG : βˆ€ g, DirectSums.packedSubspace e (W g) = G g) (g : Fin r) :
    (packedData e D leftoverEnum G hG).innerCount g = D.innerCount g ^ b := rfl

end FiniteIndices

section Layout

variable {p k Ξ” r b l : β„•} {tau : β„š} [Fact p.Prime]
  {V F E U : Type*} [Fintype V] [DecidableEq V] [Fintype F] [Fintype U]
  [Field E] [Algebra (ZMod p) E]
  {P : Factor.Pattern V F} {c : SubexpLayoutSpec.Parameters p k Ξ” r tau}

/-- Directly package explicit coordinate data into the enumeration interface
used by the counting algorithm. -/
def toEnumerationData (L : SubexpLayoutSpec.Layout (E := E) P c) (t : Fin c.Q)
    (D : LocalData (ZMod p) V r (L.K t)) : SubexpLayoutSpec.EnumerationData L t where
  outerCount := D.outerCount
  innerCount := D.innerCount
  outerBase := D.outerBase
  incrementEquiv := D.incrementEquiv
  assignmentEquiv := D.assignmentEquiv
  reconstruction := D.reconstruction

/-- The actual layout consumer. Equality with the packed local spaces is the
only bridge required; for the layout assembled from complete blocks it is
definitional equality. -/
def ofPackedSpaces (L : SubexpLayoutSpec.Layout (E := E) P c) (t : Fin c.Q)
    (e : Fin b Γ— U β†ͺ V) {W : Fin r β†’ Submodule (ZMod p) (U β†’ ZMod p)}
    (D : LocalData (ZMod p) U r W)
    (leftoverEnum : Fin l ≃ ComputablePackingSplit.Leftover e)
    (hK : L.K t = fun g => DirectSums.packedSubspace e (W g)) :
    SubexpLayoutSpec.EnumerationData L t :=
  toEnumerationData L t
    (packedData e D leftoverEnum (L.K t) (fun g => congrFun hK.symm g))

/-- Use the executable bucket packing and its explicit leftover list directly. -/
def ofPackingData [DecidableEq U]
    (L : SubexpLayoutSpec.Layout (E := E) P c) (t : Fin c.Q)
    (packing : ConstructivePacking.PackingData V U)
    {W : Fin r β†’ Submodule (ZMod p) (U β†’ ZMod p)}
    (D : LocalData (ZMod p) U r W)
    (hK : L.K t = fun g =>
      DirectSums.packedSubspace packing.completeBlocks.embed (W g)) :
    SubexpLayoutSpec.EnumerationData L t :=
  ofPackedSpaces L t packing.completeBlocks.embed D packing.leftoverEquiv hK

end Layout

end SubexponentialSAT.Subexp.BlockwiseEnumeration

end OAI.Algorithms