File size: 5,642 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
import Mathlib
import OAI.Computability.VertexCover.Fourier.SourceOccurrences
import OAI.Computability.VertexCover.Reduction.ConditionNested
import OAI.Computability.VertexCover.Machines.Primitives

namespace OAI

section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
                          
section

namespace VertexCover.Machine

namespace BoundedMemory
abbrev Memory (B : ℕ) := {s : List Bool // s.length ≤ B}

noncomputable instance (B : ℕ) : Fintype (Memory B) := by
  let f : (Σ n : Fin (B+1), List.Vector Bool n.val) → Memory B :=
    fun p => ⟨p.2.val, by have h := p.2.property; omega⟩
  have hf : Function.Surjective f := by
    intro ⟨s,hs⟩
    exact ⟨⟨⟨s.length, by omega⟩,⟨s,rfl⟩⟩,rfl⟩
  letI : Finite (Memory B) := Finite.of_surjective f hf
  exact Fintype.ofFinite _

noncomputable def next (B : ℕ) (q : Option (Memory B)) (b : Bool) :
    Option (Memory B) × List Bool :=
  (q.bind (fun p => if h : (p.val++[b]).length ≤ B then some ⟨p.val++[b],h⟩ else none), [])

theorem output (B : ℕ) (last : Option (Memory B) → List Bool) (s xs : List Bool)
    (h : (s++xs).length ≤ B) :
    Stream.output (next B) last (some ⟨s, by simp only [List.length_append] at h; omega⟩) xs =
      last (some ⟨s++xs,h⟩) := by
  induction xs generalizing s with
  | nil => simp only [Stream.output, List.append_nil]
  | cons b bs ih =>
    have hs : (s++[b]).length ≤ B := by simp only [List.length_append, List.length_cons, List.length_nil] at *; omega
    simp only [Stream.output, next, Option.bind_some, dite_eq_left hs, List.nil_append]
    rw [ih (s++[b]) (by simpa only [List.append_assoc, List.singleton_append] using h)]
    simp only [List.append_assoc, List.singleton_append]

end BoundedMemory

noncomputable def Poly.finite {α β : Type} [Fintype α]
    (ea : α → List Bool) (eb : β → List Bool) (hc : Function.Injective ea)
    (f : α → β) : Poly ea eb f := by
  classical
  let B := Finset.univ.sup (fun a : α => (ea a).length)
  let W := Finset.univ.sup (fun a : α => (eb (f a)).length)
  let last : Option (BoundedMemory.Memory B) → List Bool := fun q =>
    match q with
    | none => []
    | some s => if h : ∃ a, ea a = s.val then eb (f h.choose) else []
  have hl : ∀ q, (last q).length ≤ W := by
    intro q
    cases q with
    | none => exact Nat.zero_le _
    | some s =>
      dsimp [last]
      split
      · exact Finset.le_sup (f := fun a : α => (eb (f a)).length) (Finset.mem_univ _)
      · exact Nat.zero_le _
  let c := Stream.poly (some (⟨[],by simp⟩ : BoundedMemory.Memory B))
    (BoundedMemory.next B) last W (by intro q b; simp [BoundedMemory.next]) hl
  apply c.realizes
  intro a
  have ha : (ea a).length ≤ B := Finset.le_sup (f := fun a : α => (ea a).length) (Finset.mem_univ a)
  rw [BoundedMemory.output B last [] (ea a) (by simpa using ha)]
  dsimp [last]
  split
  · rename_i h
    rw [hc h.choose_spec]
  · rename_i h
    exact False.elim (h ⟨a,rfl⟩)

noncomputable def Poly.bool (f : Bool → Bool) : Poly boolBits boolBits f :=
  Poly.finite boolBits boolBits (by intro a b h; simpa [boolBits] using h) f

namespace Select

def trans (s : Fin 6 × Bool) (b : Bool) : (Fin 6 × Bool) × List Bool :=
  if s.1 = 0 then ((1,s.2),[])
  else if s.1 = 1 then ((2,b),[])
  else if s.1 = 2 then ((3,s.2),[])
  else if s.1 = 3 then ((if b then 4 else 5,s.2),[])
  else if s.1 = 4 then ((3,s.2),if s.2 then [b] else [])
  else ((5,s.2),if s.2 then [] else [b])

def emb (q : Fin 3) (b : Bool) : Fin 6 × Bool := (⟨q.val+3,by omega⟩,b)

theorem bound (s : Fin 6 × Bool) (b : Bool) : (trans s b).2.length ≤ 1 := by
  rcases s with ⟨q,f⟩
  fin_cases q <;> cases f <;> cases b <;> decide

theorem trans_emb (q : Fin 3) (flag b : Bool) :
    trans (emb q flag) b =
      (emb (Project.trans flag (!flag) q b).1 flag, (Project.trans flag (!flag) q b).2) := by
  fin_cases q <;> cases flag <;> cases b <;> rfl

theorem normal (q : Fin 3) (flag : Bool) (xs : List Bool) :
    Stream.output trans (fun _ => []) (emb q flag) xs =
      Stream.output (Project.trans flag (!flag)) (fun _ => []) q xs := by
  induction xs generalizing q with
  | nil => rfl
  | cons b bs ih => simp only [Stream.output, trans_emb, ih]

theorem encoded (flag : Bool) (a b : List Bool) :
    Stream.output trans (fun _ => []) (0,false) (pairBits [flag] (pairBits a b)) =
      if flag then a else b := by
  change Stream.output trans (fun _ => []) (emb 0 flag) (pairBits a b) = _
  rw [normal, Project.framed]
  cases flag <;> simp
end Select

noncomputable def Poly.select {α : Type} (e : α → List Bool) :
    Poly (prodBits boolBits (prodBits e e)) e
      (fun p : Bool × (α × α) => if p.1 then p.2.1 else p.2.2) :=
  (Stream.poly (0,false) Select.trans (fun _ => []) 1 Select.bound (by simp)).realizes
    (fun p => by simpa only [prodBits, boolBits, apply_ite] using
      Select.encoded p.1 (e p.2.1) (e p.2.2))

noncomputable def Poly.ite {α β : Type} {ea : α → List Bool} {eb : β → List Bool}
    {test : α → Bool} {yes no : α → β} (ct : Poly ea boolBits test)
    (cy : Poly ea eb yes) (cn : Poly ea eb no) :
    Poly ea eb (fun a => if test a then yes a else no a) :=
  ((ct.pair (cy.pair cn)).comp (Poly.select eb)).congr (fun _ => rfl)

end VertexCover.Machine
end


end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end

end OAI