File size: 5,294 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
import Mathlib
import OAI.Computability.VertexCover.Machines.ListFilter
import OAI.Computability.VertexCover.Machines.Generic

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 BinaryOutput
abbrev State := ℕ × List Bool
abbrev enc : State → List Bool := prodBits natBits id
def body (p : State) : Bool × State :=
  if p.1=0 then (false,p) else (true,(p.1/2,decide (p.1%2=1)::p.2))
def result (p : State) : State := (0,p.1.bits.reverse++p.2)
noncomputable def bodyPoly : Poly enc (flagBits enc) body := by
  let n := Poly.fst natBits (id : List Bool → List Bool)
  let bs := Poly.snd natBits (id : List Bool → List Bool)
  let test := n.comp Poly.natZero
  let bit := (((n.pair (Poly.const enc natBits 2)).comp Poly.natMod).pair
    (Poly.const enc natBits 1)).comp Poly.natEq
  let out := bit.ite (bs.comp (Poly.cons true)) (bs.comp (Poly.cons false))
  let next := ((n.pair (Poly.const enc natBits 2)).comp Poly.natDiv).pair out
  exact ((test.comp (Poly.bool Bool.not)).flagPair (test.ite (Poly.identity enc) next)).congr
    (fun p => by
      by_cases h : p.1=0
      · simp [Function.comp_apply,body,h]
      · simp only [Function.comp_apply,body,h,ite_false,decide_false,Bool.not_false]
        congr 2
        by_cases hb : p.1%2=1 <;> simp [hb])
 theorem bits_step (n : ℕ) (hn : n≠0) : n.bits=decide (n%2=1)::(n/2).bits := by
  have he : Nat.bit (decide (n%2=1)) (n/2)=n := by
    have hm := Nat.mod_lt n (by decide : 0<2)
    have hv := Nat.mod_add_div n 2
    by_cases h : n%2=1 <;> simp [Nat.bit,h] <;> omega
  conv_lhs => rw [← he]
  rw [Nat.bits_append_bit _ _ (by
    intro hz
    by_contra hb
    have hh : decide (n%2=1)=false := by cases h : decide (n%2=1) <;> simp_all
    rw [hh,hz] at he
    exact hn he.symm)]
 theorem run (n : ℕ) (xs : List Bool) (B : ℕ) (hsize : (enc (n,xs)).length ≤ B) :
    ∃ k, k≤n+1 ∧ BoundedRun enc body B (n,xs) (result (n,xs)) k := by
  induction n using Nat.strong_induction_on generalizing xs with
  | h n ih =>
    by_cases hn : n=0
    · subst n
      refine ⟨1,le_rfl,?_⟩
      simpa [body,result] using
        (BoundedRun.stop (e := enc) (body := body) (0,xs) hsize (by simp [body]))
    · have hlt := Nat.div_lt_self (Nat.pos_of_ne_zero hn) (by decide : 1<2)
      have hs : (enc (n/2,decide (n%2=1)::xs)).length ≤ B := by
        simp only [enc,prodBits,pairBits_length,natBits_length,id_eq,List.length_cons] at hsize ⊢
        omega
      obtain ⟨k,hk,hr⟩ := ih (n/2) hlt _ hs
      have he : result (n/2,decide (n%2=1)::xs)=result (n,xs) := by
        simp only [result,bits_step n hn,List.reverse_cons,List.append_assoc,List.singleton_append]
      rw [he] at hr
      refine ⟨k+1,by omega,.next _ _ k hsize (by simp [body,hn]) ?_⟩
      simpa only [body,hn,ite_false] using hr
noncomputable def poly : Poly enc enc result :=
  bodyPoly.loop result Polynomial.X (Polynomial.X+1) (by
    intro ⟨n,xs⟩
    obtain ⟨k,hk,hr⟩ := run n xs (enc (n,xs)).length le_rfl
    refine ⟨k,?_,by simpa only [Polynomial.eval_X] using hr⟩
    simp only [Polynomial.eval_add,Polynomial.eval_X,Polynomial.eval_one,
      enc,prodBits,pairBits_length,natBits_length,id_eq]
    omega)
end BinaryOutput
noncomputable def Poly.rawReverse : Poly id id (List.reverse : List Bool → List Bool) :=
  ((Poly.rawToList.comp (Poly.listReverse boolBits false)).comp Poly.rawBooleans).congr (fun _ => rfl)
noncomputable def Poly.binaryDigits : Poly natBits id Nat.bits :=
  (((((Poly.identity natBits).pair (Poly.const natBits id ([] : List Bool))).comp BinaryOutput.poly).comp
    (Poly.snd natBits (id : List Bool → List Bool))).comp Poly.rawReverse).congr
      (fun n => by simp only [Function.comp_apply,BinaryOutput.result,List.append_nil,List.reverse_reverse,id_eq])
noncomputable def Poly.binaryFrame : Poly id id UniqueGames.BinaryEncoding.frame := by
  let δ : Unit → Bool → Unit × List Bool := fun _ b => ((),[true,b])
  have h (q : Unit) (bs : List Bool) : Stream.output δ (fun _ => [false]) q bs = UniqueGames.BinaryEncoding.frame bs := by
    induction bs generalizing q with
    | nil => rfl
    | cons b bs ih => simp only [Stream.output,δ,List.cons_append,List.nil_append,ih,UniqueGames.BinaryEncoding.frame]
  exact (Stream.poly () δ (fun _ => [false]) 2 (by simp [δ]) (by simp)).congr (fun bs => h () bs)
noncomputable def Poly.nameBits : Poly natBits id UniqueGames.BinaryEncoding.nameBits :=
  Poly.binaryDigits.comp Poly.binaryFrame
noncomputable def Poly.nameList : Poly (listBits natBits) id
    (fun xs => xs.flatMap UniqueGames.BinaryEncoding.nameBits) :=
  ((Poly.listMap natBits id 0 [] Poly.nameBits).comp Poly.rawFlatten).congr (fun xs => by
    simp only [Function.comp_apply]
    induction xs with
    | nil => rfl
    | cons a xs ih => simp only [List.map_cons,List.flatten_cons,List.flatMap_cons,ih])
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