File size: 13,906 Bytes
92416a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Computability.WLIdentification.DimensionDecider

namespace OAI

namespace WLIdentification
/- Polynomially presented finite types. Membership refers to actual natural
codes and all iteration bounds have literal unary word realizers. -/
namespace FinitePresentation
open WordExpressions

structure Presented (A : Word → Type) where
  code : (w : Word) → A w ↪ ℕ
  bound : Word → ℕ
  good : Word → ℕ → Bool
  code_lt : ∀ w x, code w x < bound w
  good_iff : ∀ w i, good w i = true ↔ ∃ x, code w x = i
  bound_poly : NatPoly (fun S : Fin 1 → Word => bound (S 0))
  good_poly : BoolPoly (fun S : Fin 2 → Word => good (S 0) (S 1).length)

namespace Presented
variable {A B : Word → Type}

@[simp] theorem good_code (P : Presented A) (w : Word) (x : A w) : P.good w (P.code w x)=true :=
  (P.good_iff w _).mpr ⟨x,rfl⟩

theorem good_lt (P : Presented A) {w : Word} {i : ℕ} (h : P.good w i=true) : i<P.bound w := by
  obtain ⟨x,rfl⟩ := (P.good_iff w i).mp h
  exact P.code_lt w x

theorem bound_poly_of (P : Presented A) {n} {w : (Fin n → Word) → Word} (hw : WordExpressions.Poly w) :
    NatPoly (fun S => P.bound (w S)) := by
  exact P.bound_poly.subst (g := fun _ => w) (fun _ => hw)

theorem good_poly_of (P : Presented A) {n} {w : (Fin n → Word) → Word} {i : (Fin n → Word) → ℕ}
    (hw : WordExpressions.Poly w) (hi : NatPoly i) : BoolPoly (fun S => P.good (w S) (i S)) := by
  have h := P.good_poly.subst (g := ![w, fun S => unary (i S)]) (by intro i; fin_cases i; exacts [hw,hi])
  simpa [unary] using h

noncomputable def fixed (A : Type) [Fintype A] : Presented (fun _ => A) where
  code _ := NaturalCode.fixed A
  bound _ := Fintype.card A
  good _ i := decide (i<Fintype.card A)
  code_lt _ x := (Fintype.equivFin A x).isLt
  good_iff w i := by
    simp only [decide_eq_true_eq]
    constructor
    · intro h
      exact ⟨(Fintype.equivFin A).symm ⟨i,h⟩, congrArg Fin.val ((Fintype.equivFin A).apply_symm_apply _)⟩
    · rintro ⟨x,rfl⟩; exact (Fintype.equivFin A x).isLt
  bound_poly := NatPoly.const _
  good_poly := NatPoly.lt (NatPoly.var _) (NatPoly.const _)

def fin (f : Word → ℕ) (hf : NatPoly (fun S : Fin 1 → Word => f (S 0))) :
    Presented (fun w => Fin (f w)) where
  code w := NaturalCode.fin (f w)
  bound := f
  good w i := decide (i<f w)
  code_lt _ x := x.isLt
  good_iff w i := by simp only [decide_eq_true_eq]; exact ⟨fun h => ⟨⟨i,h⟩,rfl⟩, fun ⟨x,h⟩ => h ▸ x.isLt⟩
  bound_poly := hf
  good_poly := NatPoly.lt (NatPoly.var _) (hf.rename (fun _ => 0))

def subtype (P : Presented A) (p : (w : Word) → A w → Prop) (q : Word → ℕ → Bool)
    (hq : ∀ w x, q w (P.code w x)=true ↔ p w x)
    (hp : BoolPoly (fun S : Fin 2 → Word => q (S 0) (S 1).length)) :
    Presented (fun w => {x : A w // p w x}) where
  code w := NaturalCode.subtype (P.code w) (p w)
  bound := P.bound
  good w i := P.good w i && q w i
  code_lt w x := P.code_lt w x.val
  good_iff w i := by
    simp only [Bool.and_eq_true]
    constructor
    · rintro ⟨h,hq'⟩
      obtain ⟨x,rfl⟩ := (P.good_iff w i).mp h
      exact ⟨⟨x,(hq w x).mp hq'⟩,rfl⟩
    · rintro ⟨x,rfl⟩
      exact ⟨P.good_code w x.val,(hq w x.val).mpr x.property⟩
  bound_poly := P.bound_poly
  good_poly := BoolPoly.and P.good_poly hp

def sigma (P : Presented A) (B : (w : Word) → A w → Type)
    (c : ∀ w x, B w x ↪ ℕ) (b : Word → ℕ) (q : Word → ℕ → ℕ → Bool)
    (hc : ∀ w x y, c w x y < b w)
    (hq : ∀ w x i, q w (P.code w x) i=true ↔ ∃ y, c w x y=i)
    (hb : NatPoly (fun S : Fin 1 → Word => b (S 0)))
    (hp : BoolPoly (fun S : Fin 3 → Word => q (S 0) (S 1).length (S 2).length)) :
    Presented (fun w => Σ x, B w x) where
  code w := NaturalCode.sigma (P.code w) (c w)
  bound w := Nat.pair (P.bound w) (b w)+1
  good w i := P.good w (Nat.unpair i).1 && q w (Nat.unpair i).1 (Nat.unpair i).2
  code_lt w x := by
    exact (Nat.pair_lt_pair_left _ (P.code_lt w x.1)).trans
      ((Nat.pair_lt_pair_right _ (hc w x.1 x.2)).trans_le (Nat.le_succ _))
  good_iff w i := by
    simp only [Bool.and_eq_true]
    constructor
    · rintro ⟨h,hq'⟩
      obtain ⟨x,hx⟩ := (P.good_iff w _).mp h
      rw [← hx] at hq'
      obtain ⟨y,hy⟩ := (hq w x _).mp hq'
      exact ⟨⟨x,y⟩, by change Nat.pair _ _ = i; rw [hx,hy,Nat.pair_unpair]⟩
    · rintro ⟨⟨x,y⟩,rfl⟩
      change P.good w (Nat.unpair (Nat.pair _ _)).1=true ∧ q w (Nat.unpair (Nat.pair _ _)).1 (Nat.unpair (Nat.pair _ _)).2=true
      rw [Nat.unpair_pair]
      exact ⟨P.good_code w x,(hq w x _).mpr ⟨y,rfl⟩⟩
  bound_poly := NatPoly.succ (NatPoly.pair P.bound_poly hb)
  good_poly := by
    apply BoolPoly.and
    · apply P.good_poly_of <;> word_poly
    · have h := hp.subst (g := ![fun S : Fin 2 → Word => S 0,
        fun S => unary (Nat.unpair (S 1).length).1, fun S => unary (Nat.unpair (S 1).length).2])
        (by intro i; fin_cases i <;> word_poly)
      simpa [unary] using h

end Presented
end FinitePresentation

namespace FinitePresentation.Presented
open WordExpressions
variable {A B : Word → Type}

def retype (P : Presented A) (e : ∀ w, B w ≃ A w) : Presented B where
  code w := (e w).toEmbedding.trans (P.code w)
  bound := P.bound
  good := P.good
  code_lt w x := P.code_lt w (e w x)
  good_iff w i := (P.good_iff w i).trans ⟨fun ⟨x,h⟩ => ⟨(e w).symm x, by simpa using h⟩,
    fun ⟨x,h⟩ => ⟨e w x,h⟩⟩
  bound_poly := P.bound_poly
  good_poly := P.good_poly

def prod (P : Presented A) (Q : Presented B) : Presented (fun w => A w × B w) :=
  (P.sigma (fun w _ => B w) (fun w _ => Q.code w) Q.bound (fun w _ => Q.good w)
    (fun w _ y => Q.code_lt w y) (fun w _ i => Q.good_iff w i) Q.bound_poly
    (Q.good_poly.rename ![0,2])).retype (fun _ => Equiv.sigmaEquivProd _ _ |>.symm)

def sum (P : Presented A) (Q : Presented B) : Presented (fun w => A w ⊕ B w) where
  code w := NaturalCode.sum (P.code w) (Q.code w)
  bound w := 2*(P.bound w+Q.bound w)+2
  good w i := if i%2=0 then P.good w (i/2) else Q.good w (i/2)
  code_lt w x := by cases x with
    | inl a => have := P.code_lt w a; change 2*P.code w a < _; omega
    | inr b => have := Q.code_lt w b; change 2*Q.code w b+1 < _; omega
  good_iff w i := by
    by_cases he : i%2=0
    · simp only [he, ↓reduceIte, P.good_iff]
      constructor
      · rintro ⟨a,ha⟩
        refine ⟨.inl a, ?_⟩
        change 2*P.code w a=i
        omega
      · rintro ⟨a,ha⟩
        cases a with
        | inl a => exact ⟨a, by change 2*P.code w a=i at ha; omega⟩
        | inr b => change 2*Q.code w b+1=i at ha; omega
    · simp only [he, ↓reduceIte, Q.good_iff]
      constructor
      · rintro ⟨b,hb⟩
        refine ⟨.inr b, ?_⟩
        change 2*Q.code w b+1=i
        omega
      · rintro ⟨a,ha⟩
        cases a with
        | inl a => change 2*P.code w a=i at ha; omega
        | inr b => exact ⟨b, by change 2*Q.code w b+1=i at ha; omega⟩
  bound_poly := by word_poly; exact P.bound_poly; exact Q.bound_poly
  good_poly := by
    apply BoolPoly.iteProp
    · word_poly
    · apply P.good_poly_of <;> word_poly
    · apply Q.good_poly_of <;> word_poly

def option (P : Presented A) : Presented (fun w => Option (A w)) where
  code w := NaturalCode.option (P.code w)
  bound w := P.bound w+1
  good w i := decide (i=0) || (decide (0 < i) && P.good w (i-1))
  code_lt w x := by cases x with
    | none => change 0 < P.bound w+1; omega
    | some a => exact Nat.succ_lt_succ (P.code_lt w a)
  good_iff w i := by
    simp only [Bool.or_eq_true, Bool.and_eq_true, decide_eq_true_eq]
    constructor
    · rintro (rfl | ⟨hi,hg⟩)
      · exact ⟨none,rfl⟩
      · obtain ⟨a,ha⟩ := (P.good_iff w _).mp hg
        exact ⟨some a, by change P.code w a+1=i; omega⟩
    · rintro ⟨x,rfl⟩
      cases x with
      | none => exact Or.inl rfl
      | some a => exact Or.inr ⟨by change 0 < P.code w a+1; omega, by change P.good w (P.code w a+1-1)=true; simp⟩
  bound_poly := NatPoly.succ P.bound_poly
  good_poly := BoolPoly.or (NatPoly.eqtest (NatPoly.var _) (NatPoly.const _))
    (BoolPoly.and (NatPoly.lt (NatPoly.const 0) (NatPoly.var _))
      (P.good_poly_of (Poly.var _) (NatPoly.sub (NatPoly.var _) (NatPoly.const 1))))

def bool : Presented (fun _ => Bool) where
  code _ := NaturalCode.bool
  bound _ := 2
  good _ i := decide (i<2)
  code_lt _ x := by cases x <;> decide
  good_iff _ i := by
    simp only [decide_eq_true_eq]
    constructor
    · intro hi
      interval_cases i
      · exact ⟨false,rfl⟩
      · exact ⟨true,rfl⟩
    · rintro ⟨x,rfl⟩; cases x <;> decide
  bound_poly := NatPoly.const _
  good_poly := NatPoly.lt (NatPoly.var _) (NatPoly.const 2)

def unit : Presented (fun _ => Unit) where
  code _ := NaturalCode.unit
  bound _ := 1
  good _ i := decide (i=0)
  code_lt _ x := by cases x; decide
  good_iff _ i := by simp only [decide_eq_true_eq]; exact ⟨fun h => ⟨(),h.symm⟩,fun ⟨_,h⟩ => h.symm⟩
  bound_poly := NatPoly.const _
  good_poly := NatPoly.eqtest (NatPoly.var _) (NatPoly.const 0)

end FinitePresentation.Presented

namespace FinitePresentation.Presented
open WordExpressions NumericFinite
variable {A : Word → Type}

abbrev card (P : Presented A) (w : Word) := count (P.bound w) (P.good w)
abbrev select (P : Presented A) (w : Word) (i : ℕ) := NumericFinite.select (P.bound w) (P.good w) i

theorem rank_lt (P : Presented A) (w : Word) (x : A w) : count (P.code w x) (P.good w) < P.card w :=
  count_strict (P.code_lt w x) (P.good_code w x)

theorem select_good (P : Presented A) (w : Word) {i : ℕ} (hi : i<P.card w) :
    P.good w (P.select w i)=true := by
  rw [select, select_correct _ _ _ hi]
  exact (mem_elements.mp (List.getElem_mem hi)).2

theorem rank_select (P : Presented A) (w : Word) {i : ℕ} (hi : i<P.card w) :
    count (P.select w i) (P.good w)=i := by
  rw [select, select_correct _ _ _ hi]
  exact NumericFinite.rank _ _ _ hi

theorem select_rank (P : Presented A) (w : Word) (x : A w) :
    P.select w (count (P.code w x) (P.good w))=P.code w x := by
  have hr := P.rank_select w (P.rank_lt w x)
  have hg := P.select_good w (P.rank_lt w x)
  rcases lt_trichotomy (P.select w (count (P.code w x) (P.good w))) (P.code w x) with hl | he | hl
  · have := count_strict hl hg; omega
  · exact he
  · have := count_strict hl (P.good_code w x); omega

noncomputable def decode (P : Presented A) (w : Word) (i : Fin (P.card w)) : A w :=
  Classical.choose ((P.good_iff w _).mp (P.select_good w i.isLt))

theorem code_decode (P : Presented A) (w : Word) (i : Fin (P.card w)) :
    P.code w (P.decode w i)=P.select w i :=
  Classical.choose_spec ((P.good_iff w _).mp (P.select_good w i.isLt))

noncomputable def rankEquiv (P : Presented A) (w : Word) : A w ≃ Fin (P.card w) where
  toFun x := ⟨count (P.code w x) (P.good w), P.rank_lt w x⟩
  invFun := P.decode w
  left_inv x := by apply (P.code w).injective; rw [P.code_decode, P.select_rank]
  right_inv i := by apply Fin.ext; exact (congrArg (fun j => count j (P.good w)) (P.code_decode w i)).trans (P.rank_select w i.isLt)

@[simp] theorem card_eq (P : Presented A) (w : Word) [Fintype (A w)] : P.card w=Fintype.card (A w) := by
  simpa only [Fintype.card_fin] using (Fintype.card_congr (P.rankEquiv w)).symm

theorem card_poly (P : Presented A) : NatPoly (fun S : Fin 1 → Word => P.card (S 0)) := by
  apply (NatPoly.filterCount P.bound_poly P.good_poly).congr
  intro S
  simp [Fin.snoc, unary, card]

theorem card_poly_of (P : Presented A) {n} {w : (Fin n → Word) → Word} (hw : Poly w) :
    NatPoly (fun S => P.card (w S)) :=
  P.card_poly.subst (g := fun _ => w) (fun _ => hw)

theorem select_poly (P : Presented A) : NatPoly (fun S : Fin 2 → Word => P.select (S 0) (S 1).length) := by
  apply (NatPoly.filterSelect (P.bound_poly.rename (fun _ => 0)) (NatPoly.var 1)
    (P.good_poly.rename ![0,2])).congr
  intro S
  simp [Fin.snoc, unary, select]

theorem select_poly_of (P : Presented A) {n} {w : (Fin n → Word) → Word} {i : (Fin n → Word) → ℕ}
    (hw : Poly w) (hi : NatPoly i) : NatPoly (fun S => P.select (w S) (i S)) := by
  have h := P.select_poly.subst (g := ![w, fun S => unary (i S)]) (by intro i; fin_cases i; exacts [hw,hi])
  simpa [unary] using h

end FinitePresentation.Presented

namespace FinitePresentation
open WordExpressions
variable {A : Word → Type}

structure PresentedGraph (P : Presented A) (G : ∀ w, SimpleGraph (A w)) where
  adjacent : Word → ℕ → ℕ → Bool
  adjacent_iff : ∀ w x y, adjacent w (P.code w x) (P.code w y)=true ↔ (G w).Adj x y
  adjacent_poly : BoolPoly (fun S : Fin 3 → Word => adjacent (S 0) (S 1).length (S 2).length)

namespace PresentedGraph
variable {P : Presented A} {G : ∀ w, SimpleGraph (A w)} (Q : PresentedGraph P G)

noncomputable def graph (w : Word) : Graph where
  order := P.card w
  edge i j := Q.adjacent w (P.select w i) (P.select w j)
  symmetric i j := by
    rw [← P.code_decode w i, ← P.code_decode w j]
    apply Bool.eq_iff_iff.mpr
    rw [Q.adjacent_iff, Q.adjacent_iff, (G w).adj_comm]
  loopless i := by
    rw [← P.code_decode w i]
    apply Bool.eq_false_iff.mpr
    intro h
    exact (G w).loopless.irrefl _ ((Q.adjacent_iff w _ _).mp h)

theorem graph_isomorphic (w : Word) [Fintype (A w)] : Isomorphic (Q.graph w) (graphOfSimple (G w)) := by
  classical
  refine ⟨(P.rankEquiv w).symm.trans (Fintype.equivFin (A w)), ?_⟩
  intro i j
  change Q.adjacent w (P.select w i) (P.select w j) = _
  rw [← P.code_decode w i, ← P.code_decode w j]
  change _ = (graphOfSimple (G w)).edge (Fintype.equivFin _ (P.decode w i)) (Fintype.equivFin _ (P.decode w j))
  rw [graphOfSimple_edge]
  apply Bool.eq_iff_iff.mpr
  simp only [Q.adjacent_iff, decide_eq_true_eq]

end PresentedGraph
end FinitePresentation

end WLIdentification

end OAI