File size: 13,292 Bytes
760f90e | 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 | import OAI.Computability.RationalHitting.Compiler
namespace OAI
namespace RationalHitting.StackLanguage
open Function
variable {K : Type} [DecidableEq K]
def clear (a : K) : Program K := .loop a (.drop a)
def reverse (a b : K) : Program K := .loop a (.transfer a b)
lemma eval_clear (a : K) (s : Store K) :
Eval (clear a) s (update s a []) (2*(s a).length+1) := by
generalize he : s a = l
induction l generalizing s with
| nil =>
have hh : update s a [] = s := by ext k; by_cases hk : k=a <;> simp_all
simpa [clear,he,hh] using (Eval.whileNil (p:=Program.drop a) he)
| cons b l ih =>
let s₁ := update s a l
have h₁ : Eval (Program.drop a) s s₁ 1 := by simpa [s₁,he] using Eval.drop a s
have h₂ := ih s₁ (by simp [s₁])
have hh : update s₁ a [] = update s a [] := by simp [s₁]
have h := Eval.whileCons (by simp [he]) h₁ h₂
convert h using 1 <;> simp_all [clear,s₁,Nat.mul_add,Nat.add_assoc] ; omega
lemma transfer_cons {a b : K} (hab : a ≠ b) (s : Store K) (x : Bool) (l : List Bool)
(hs : s a = x::l) : transfer a b s = update (update s a l) b (x::s b) := by
simp [transfer,hs,Ne.symm hab]
lemma eval_reverse {a b : K} (hab : a ≠ b) (s : Store K) :
Eval (reverse a b) s (update (update s a []) b ((s a).reverse ++ s b))
(2*(s a).length+1) := by
generalize he : s a = l
induction l generalizing s with
| nil =>
have hh : update (update s a []) b (s b) = s := by
ext k
by_cases hka : k=a <;> by_cases hkb : k=b <;> simp_all
simpa [reverse,he,hh] using (Eval.whileNil (p:=Program.transfer a b) he)
| cons x l ih =>
let s₁ := update (update s a l) b (x::s b)
have ha : s₁ a = l := by simp [s₁,hab]
have hb : s₁ b = x::s b := by simp [s₁]
have h₁ : Eval (Program.transfer a b) s s₁ 1 := by
simpa [transfer_cons hab s x l he,s₁] using Eval.transfer a b s
have h₂ := ih s₁ ha
have hh : update (update s₁ a []) b (l.reverse++s₁ b) =
update (update s a []) b ((x::l).reverse++s b) := by
ext k
by_cases hka : k=a <;> by_cases hkb : k=b <;>
simp_all [s₁,List.reverse_cons,List.append_assoc]
have h := Eval.whileCons (by simp [he]) h₁ h₂
convert h using 1 <;> simp_all [reverse,Nat.mul_add,Nat.add_assoc] ; omega
/-- Copy a nonempty head to another register without consuming it. -/
def copyHead (a b : K) : Program K := .branch a .skip (.push b false) (.push b true)
lemma eval_copyHead {a b : K} (s : Store K) (x : Bool) (l : List Bool) (hs : s a = x::l) :
Eval (copyHead a b) s (update s b (x::s b)) 2 := by
cases x with
| false => exact Eval.branchFalse hs (Eval.push b false s)
| true => exact Eval.branchTrue hs (Eval.push b true s)
/-- Two simultaneous reversed copies of a word, consuming the operand. -/
def duplicateReverse (a b c : K) : Program K :=
.loop a (.seq (.transfer a b) (copyHead b c))
lemma eval_duplicateReverse {a b c : K} (hab : a ≠ b) (hac : a ≠ c) (hbc : b ≠ c)
(s : Store K) :
Eval (duplicateReverse a b c) s
(update (update (update s a []) b ((s a).reverse++s b)) c ((s a).reverse++s c))
(5*(s a).length+1) := by
generalize he : s a = l
induction l generalizing s with
| nil =>
have hh : update (update (update s a []) b (s b)) c (s c) = s := by
ext k
by_cases hka : k=a <;> by_cases hkb : k=b <;> by_cases hkc : k=c <;> simp_all
simpa [duplicateReverse,he,hh] using
(Eval.whileNil (p:=Program.seq (.transfer a b) (copyHead b c)) he)
| cons x l ih =>
let s₁ := update (update s a l) b (x::s b)
let s₂ := update s₁ c (x::s c)
have h₁ : Eval (Program.transfer a b) s s₁ 1 := by
simpa [transfer_cons hab s x l he,s₁] using Eval.transfer a b s
have h₂ : Eval (copyHead b c) s₁ s₂ 2 := by
simpa [s₂,s₁,hac,hbc,Ne.symm hab,Ne.symm hac,Ne.symm hbc] using
eval_copyHead (a:=b) (b:=c) s₁ x (s b) (by simp [s₁])
have ha : s₂ a = l := by simp [s₂,s₁,hab,hac,]
have ht := ih s₂ ha
have hh : update (update (update s₂ a []) b (l.reverse++s₂ b)) c (l.reverse++s₂ c) =
update (update (update s a []) b ((x::l).reverse++s b)) c ((x::l).reverse++s c) := by
ext k
by_cases hka : k=a <;> by_cases hkb : k=b <;> by_cases hkc : k=c <;>
simp_all [s₂,s₁,List.reverse_cons,List.append_assoc]
have h := Eval.whileCons (by simp [he]) (Eval.seq h₁ h₂) ht
convert h using 1 <;> simp_all [duplicateReverse,Nat.mul_add,Nat.add_assoc] ; omega
/-- Copy an arbitrary binary word, restoring the operand and clearing
temporary storage; all registers have fixed indices. -/
def copy (a b tmp : K) : Program K :=
.seq (clear b) (.seq (clear tmp) (.seq (reverse a tmp) (duplicateReverse tmp a b)))
lemma eval_copy {a b tmp : K} (hab : a ≠ b) (hat : a ≠ tmp) (hbt : b ≠ tmp)
(s : Store K) :
Eval (copy a b tmp) s (update (update s b (s a)) tmp [])
(7*(s a).length+2*(s b).length+2*(s tmp).length+7) := by
let s₁ := update s b []
let s₂ := update s₁ tmp []
let s₃ := update (update s₂ a []) tmp ((s₂ a).reverse++s₂ tmp)
have h₁ := eval_clear b s
have h₂ := eval_clear tmp s₁
have h₃ := eval_reverse hat s₂
have h₄ := eval_duplicateReverse (Ne.symm hat) (Ne.symm hbt) hab s₃
have h := Eval.seq h₁ (Eval.seq h₂ (Eval.seq h₃ h₄))
have hh : update (update (update s₃ tmp []) a ((s₃ tmp).reverse++s₃ a)) b
((s₃ tmp).reverse++s₃ b) = update (update s b (s a)) tmp [] := by
ext k
by_cases hka : k=a <;> by_cases hkb : k=b <;> by_cases hkt : k=tmp <;>
simp_all [s₃,s₂,s₁]
change Eval (copy a b tmp) s
(update (update (update s₃ tmp []) a ((s₃ tmp).reverse++s₃ a)) b
((s₃ tmp).reverse++s₃ b)) _ at h
rw [hh] at h
convert h using 1
simp [s₃,s₂,s₁,hab,hat,Ne.symm hbt]
omega
end RationalHitting.StackLanguage
namespace RationalHitting.StackLanguage
open Function
variable {K : Type} [DecidableEq K]
/-- Little-endian binary words; zero has the empty word. -/
def wordValue : List Bool → ℕ
| [] => 0
| b::l => (if b then 1 else 0)+2*wordValue l
def incrementWord : List Bool → List Bool
| [] => [true]
| false::l => true::l
| true::l => false::incrementWord l
@[simp] theorem incrementWord_value (l : List Bool) : wordValue (incrementWord l) = wordValue l+1 := by
induction l with
| nil => simp [incrementWord,wordValue]
| cons b l ih => cases b <;> simp [incrementWord,wordValue,ih] <;> omega
def carrySplit : List Bool → ℕ × List Bool
| [] => (0,[true])
| false::l => (0,true::l)
| true::l => let r := carrySplit l; (r.1+1,r.2)
theorem carrySplit_spec (l : List Bool) :
incrementWord l = List.replicate (carrySplit l).1 false ++ (carrySplit l).2 := by
induction l with
| nil => rfl
| cons b l ih =>
cases b with
| false => rfl
| true => simp [incrementWord,carrySplit,List.replicate_succ,ih]
theorem carrySplit_length (l : List Bool) : (carrySplit l).1 ≤ l.length := by
induction l with
| nil => simp [carrySplit]
| cons b l ih => cases b <;> simp [carrySplit] ; omega
theorem incrementWord_length (l : List Bool) : (incrementWord l).length ≤ l.length+1 := by
induction l with
| nil => simp [incrementWord]
| cons b l ih => cases b <;> simp [incrementWord] ; omega
def incrementBody (a tmp flag : K) : Program K := .branch a
(.seq (.push a true) (clear flag))
(.seq (.drop a) (.seq (.push a true) (clear flag)))
(.seq (.drop a) (.push tmp false))
def incrementLoop (a tmp flag : K) : Program K := .loop flag (incrementBody a tmp flag)
lemma eval_incrementLoop {a tmp flag : K} (hat : a ≠ tmp) (haf : a ≠ flag) (htf : tmp ≠ flag)
(s : Store K) (hf : s flag = [true]) :
∃ t ≤ 5*(s a).length+10, Eval (incrementLoop a tmp flag) s
(update (update (update s a (carrySplit (s a)).2) tmp
(List.replicate (carrySplit (s a)).1 false ++ s tmp)) flag []) t := by
generalize he : s a = l
induction l generalizing s with
| nil =>
let s₁ := update s a [true]
have hf₁ : s₁ flag = [true] := by simp [s₁,Ne.symm haf,hf]
have hclear : Eval (clear flag) s₁ (update s₁ flag []) 3 := by
simpa [hf₁] using eval_clear flag s₁
have hpush : Eval (Program.push a true) s s₁ 1 := by
simpa [s₁,he] using Eval.push a true s
have hb : Eval (incrementBody a tmp flag) s (update s₁ flag []) 6 :=
Eval.branchNone he (Eval.seq hpush hclear)
have halt : Eval (incrementLoop a tmp flag) (update s₁ flag []) (update s₁ flag []) 1 :=
Eval.whileNil (by simp)
have h := Eval.whileCons (by simp [hf]) hb halt
have hh : update (update (update s a [true]) tmp (s tmp)) flag [] = update s₁ flag [] := by
ext k
by_cases hka : k=a <;> by_cases hkt : k=tmp <;> by_cases hkf : k=flag <;> simp_all [s₁]
refine ⟨8,by simp,?_⟩
simpa [incrementLoop,he,carrySplit,hh] using h
| cons b l ih =>
cases b with
| false =>
let s₁ := update s a l
let s₂ := update s₁ a (true::l)
have hf₂ : s₂ flag = [true] := by simp [s₂,s₁,Ne.symm haf,hf]
have hclear : Eval (clear flag) s₂ (update s₂ flag []) 3 := by
simpa [hf₂] using eval_clear flag s₂
have hdrop : Eval (Program.drop a) s s₁ 1 := by simpa [s₁,he] using Eval.drop a s
have hpush : Eval (Program.push a true) s₁ s₂ 1 := by simpa [s₂,s₁] using Eval.push a true s₁
have hb : Eval (incrementBody a tmp flag) s (update s₂ flag []) 8 :=
Eval.branchFalse he (Eval.seq hdrop (Eval.seq hpush hclear))
have halt : Eval (incrementLoop a tmp flag) (update s₂ flag []) (update s₂ flag []) 1 :=
Eval.whileNil (by simp)
have h := Eval.whileCons (by simp [hf]) hb halt
have hh : update (update (update s a (true::l)) tmp (s tmp)) flag [] = update s₂ flag [] := by
ext k
by_cases hka : k=a <;> by_cases hkt : k=tmp <;> by_cases hkf : k=flag <;> simp_all [s₂,s₁]
refine ⟨10,by simp,?_⟩
simpa [incrementLoop,he,carrySplit,hh] using h
| true =>
let s₁ := update s a l
let s₂ := update s₁ tmp (false::s tmp)
have ha₂ : s₂ a = l := by simp [s₂,s₁,hat]
have hf₂ : s₂ flag = [true] := by simp [s₂,s₁,Ne.symm haf,Ne.symm htf,hf]
have hdrop : Eval (Program.drop a) s s₁ 1 := by simpa [s₁,he] using Eval.drop a s
have hpush : Eval (Program.push tmp false) s₁ s₂ 1 := by simpa [s₂,s₁,Ne.symm hat] using Eval.push tmp false s₁
have hb : Eval (incrementBody a tmp flag) s s₂ 4 :=
Eval.branchTrue he (Eval.seq hdrop hpush)
obtain ⟨t,ht,hev⟩ := ih s₂ hf₂ ha₂
have h := Eval.whileCons (by simp [hf]) hb hev
have hh :
update (update (update s₂ a (carrySplit l).2) tmp
(List.replicate (carrySplit l).1 false++s₂ tmp)) flag [] =
update (update (update s a (carrySplit l).2) tmp
(List.replicate ((carrySplit l).1+1) false++s tmp)) flag [] := by
ext k
by_cases hka : k=a <;> by_cases hkt : k=tmp <;> by_cases hkf : k=flag <;>
simp_all [s₂,s₁,List.replicate_add,List.append_assoc]
refine ⟨5+t,by simp only [List.length_cons] at *; omega,?_⟩
simpa only [incrementLoop,ha₂,carrySplit,hh] using h
def increment (a tmp flag : K) : Program K :=
.seq (clear tmp) (.seq (clear flag) (.seq (.push flag true)
(.seq (incrementLoop a tmp flag) (reverse tmp a))))
lemma eval_increment {a tmp flag : K} (hat : a ≠ tmp) (haf : a ≠ flag) (htf : tmp ≠ flag)
(s : Store K) :
∃ t ≤ 7*(s a).length+2*(s tmp).length+2*(s flag).length+18,
Eval (increment a tmp flag) s
(update (update (update s a (incrementWord (s a))) tmp []) flag []) t := by
let s₁ := update s tmp []
let s₂ := update s₁ flag []
let s₃ := update s₂ flag [true]
have ha₃ : s₃ a = s a := by simp [s₃,s₂,s₁,hat,haf]
have ht₃ : s₃ tmp = [] := by simp [s₃,s₂,s₁,htf]
obtain ⟨t,ht,hloop⟩ := eval_incrementLoop hat haf htf s₃ (by simp [s₃])
let s₄ := update (update (update s₃ a (carrySplit (s a)).2) tmp
(List.replicate (carrySplit (s a)).1 false)) flag []
change Eval (incrementLoop a tmp flag) s₃ _ t at hloop
have hloop' : Eval (incrementLoop a tmp flag) s₃ s₄ t := by
simpa [ha₃,ht₃,s₄] using hloop
have h₁ := eval_clear tmp s
have h₂ := eval_clear flag s₁
have hp : Eval (Program.push flag true) s₂ s₃ 1 := by simpa [s₂,s₃] using Eval.push flag true s₂
have hrev := eval_reverse (Ne.symm hat) s₄
have h := Eval.seq h₁ (Eval.seq h₂ (Eval.seq hp (Eval.seq hloop' hrev)))
have hfinal : update (update s₄ tmp []) a ((s₄ tmp).reverse++s₄ a) =
update (update (update s a (incrementWord (s a))) tmp []) flag [] := by
ext k
by_cases hka : k=a <;> by_cases hkt : k=tmp <;> by_cases hkf : k=flag <;>
simp_all [s₄,s₃,s₂,s₁,carrySplit_spec]
rw [hfinal] at h
refine ⟨_,?_,h⟩
have hc := carrySplit_length (s a)
simp only [ha₃] at ht
simp [s₄,s₃,s₂,s₁,htf,Ne.symm htf]
omega
end RationalHitting.StackLanguage
end OAI
|