| .input =>20
| .const n =>2 * n.bits.length
| .add a b | .mul a b => timeCoeff a + timeCoeff b +
100 * (widthCoeff a + widthCoeff b + 1)^2 + 100 * (widthCoeff a + widthCoeff b + 1)
| .pred a => timeCoeff a + 100 * (widthCoeff a + 1)
defbinaryPorts (a b : ℕ) (ha : 2≤ a) (hb : 2≤ b) : ArithmeticCalls.Ports (16 + a + b) where
body · 13
lines
left := leftBlock a b (outputPort ha)
right := rightBlock a b (outputPort hb)
out := lowPort a b 1
work := workBlock a b
out_ne_left := (left_ne_low a b (outputPort ha) 1).symm
out_ne_right := (right_ne_low a b (outputPort hb) 1).symm
work_ne_left :=fun i => (left_ne_low a b (outputPort ha) _).symm
work_ne_right :=fun i => (right_ne_low a b (outputPort hb) _).symm
work_ne_out :=by
intro i h
have:= congrArg Fin.val h
change i.val + 2 = 1 at this
omega
abbrevbinaryCmd {a b : ℕ} (ha : 2≤ a) (hb : 2≤ b)
(isMul : Bool) (ca : Cmd8 a) (cb : Cmd8 b) : Cmd8 (16 + a + b) :=
body · 10
lines
.seq (copyCmd (lowPort a b 0) (leftBlock a b (inputPort ha)) (lowPort a b 7))
(.seq (blockCall ca ha (leftBlock a b))
(.seq (copyCmd (lowPort a b 0) (rightBlock a b (inputPort hb)) (lowPort a b 7))
(.seq (blockCall cb hb (rightBlock a b))
(.seq (if isMul thenArithmeticCalls.multiplyAt (binaryPorts a b ha hb)
elseArithmeticCalls.addAt (binaryPorts a b ha hb))
(.seq (clearCmd (rightBlock a b (outputPort hb)))
(.seq (clearCmd (rightBlock a b (inputPort hb)))
(.seq (clearCmd (leftBlock a b (outputPort ha)))
(clearCmd (leftBlock a b (inputPort ha))))))))))
defpredPorts (a : ℕ) : CounterForPrograms.Ports8 (16 + a) where
body · 11
lines
register := ⟨Fin.castAdd 5, by
intro i j h
apply Fin.ext
exact congrArg (funx : Fin8=> x.val) h⟩
counter := lowPort a 01
scratch := lowPort a 02
counter_ne_scratch :=by
intro h
have:= congrArg Fin.val h
change (1 : ℕ) = 2 at this
omega
abbrevunaryCmd {a : ℕ} (ha : 2≤ a) (ca : Cmd8 a) : Cmd8 (16 + a) :=
body · 6
lines
.seq (copyCmd (lowPort a 00) (leftBlock a 0 (inputPort ha)) (lowPort a 07))
(.seq (blockCall ca ha (leftBlock a 0))
(.seq (copyCmd (leftBlock a 0 (outputPort ha)) (lowPort a 01) (lowPort a 07))
(.seq (CounterPredProgram.predCmd (predPorts a))
(.seq (clearCmd (leftBlock a 0 (outputPort ha)))
(clearCmd (leftBlock a 0 (inputPort ha)))))))
Compiling a call depends only on the fixed child syntax and port layout.
∀ (xs : ListBool) (regs : Fin8→Bool), ∃ n word rf,
Eval c n (state h regs xs []) (state h rf xs word) ∧BinaryArithmetic.toNat word = eval (BinaryArithmetic.toNat xs) e ∧
word.length ≤ widthCoeff e * (xs.length + 1) ∧
n ≤ timeCoeff e * (xs.length + 1)^2
theorembinary_spec {ea eb : Expr} {a b : ℕ} (ha : 2≤ a) (hb : 2≤ b)
(isMul : Bool) (ca : Cmd8 a) (cb : Cmd8 b)
(hca : Spec ea ha ca) (hcb : Spec eb hb cb) :
Spec (if isMul then .mul ea eb else .add ea eb) (by omega)
(binaryCmd ha hb isMul ca cb) :=by
proof · 152
lines
intro xs regs
lethp : 2≤16 + a + b :=by omega
let d0 := state hp regs xs []
letd1 : State8 (16 + a + b) :=
⟨readRegs d0.regs 01 none, Function.update d0.stacks (leftBlock a b (inputPort ha)) xs⟩
have hleft0 (i : Fin a) : leftBlock a b i ≠ (0 : Fin (16 + a + b)) :=
left_ne_low a b i 0have hright0 (i : Fin b) : rightBlock a b i ≠ (0 : Fin (16 + a + b)) :=
right_ne_low a b i 0havehtmp0 : lowPort a b 7≠ (0 : Fin (16 + a + b)) :=by
intro h
have:= congrArg Fin.val h
simp [lowPort] at this
havehd0source : d0.stacks (lowPort a b 0) = xs :=by
simpa only [d0, ← parent_input a b hp] using state_input hp regs xs []
havehcopyA : Eval
(copyCmd (lowPort a b 0) (leftBlock a b (inputPort ha)) (lowPort a b 7))
(7 * xs.length + 5) d0 d1 :=byhave h :=
copy_eval (lowPort a b 0) (leftBlock a b (inputPort ha)) (lowPort a b 7) d0
(by simp) (by simp) (by simp)
(by simp [d0, initial_stacks ])
(by simp [d0, initial_stacks, inputPort, htmp0])
rw [hd0source] at h
simpa only [d1] using h
havehcleanA : d1.stacks ∘ leftBlock a b = (state ha d1.regs xs []).stacks :=by
funext i
simp [d1, d0, initial_stacks, Function.comp_apply, Function.update_apply,
(leftBlock a b).injective.eq_iff ]
obtain ⟨na, wa, ra, heA, hvA, hwA, htA⟩ := block_eval hca (leftBlock a b) d1 xs hcleanA
letd2 : State8 (16 + a + b) :=
⟨ra, Function.update d1.stacks (leftBlock a b (outputPort ha)) wa⟩
letd3 : State8 (16 + a + b) :=
⟨readRegs d2.regs 01 none, Function.update d2.stacks (rightBlock a b (inputPort hb)) xs⟩
havehd2source : d2.stacks (lowPort a b 0) = xs :=by
simp [d2, d1, hd0source ]
havehcopyB : Eval
(copyCmd (lowPort a b 0) (rightBlock a b (inputPort hb)) (lowPort a b 7))
(7 * xs.length + 5) d2 d3 :=byhave h :=
copy_eval (lowPort a b 0) (rightBlock a b (inputPort hb)) (lowPort a b 7) d2
(by simp) (by simp) (by simp)
(by simp [d2, d1, d0, initial_stacks ])
(by simp [d2, d1, d0, initial_stacks, inputPort, htmp0 ])
rw [hd2source] at h
simpa only [d3] using h
havehcleanB : d3.stacks ∘ rightBlock a b = (state hb d3.regs xs []).stacks :=by
funext i
simp [d3, d2, d1, d0, initial_stacks, Function.comp_apply,
Function.update_apply, (rightBlock a b).injective.eq_iff ]
obtain ⟨nb, wb, rb, heB, hvB, hwB, htB⟩ := block_eval hcb (rightBlock a b) d3 xs hcleanB
letd4 : State8 (16 + a + b) :=
⟨rb, Function.update d3.stacks (rightBlock a b (outputPort hb)) wb⟩
havehlow10 : lowPort a b 1≠ (0 : Fin (16 + a + b)) :=by
intro h
have:= congrArg Fin.val h
simp [lowPort] at this
havehvalid : ArithmeticCalls.Valid (binaryPorts a b ha hb) d4 :=by
constructor
· simp [d4, d3, d2, d1, d0, initial_stacks, inputPort, binaryPorts,
hlow10 ]
· intro i
havehwli : workBlock a b i ≠ leftBlock a b (inputPort ha) :=
(left_ne_low a b (inputPort ha) ⟨i.val + 2, by omega⟩).symm
havehwlo : workBlock a b i ≠ leftBlock a b (outputPort ha) :=
(left_ne_low a b (outputPort ha) ⟨i.val + 2, by omega⟩).symm
havehwri : workBlock a b i ≠ rightBlock a b (inputPort hb) :=
(right_ne_low a b (inputPort hb) ⟨i.val + 2, by omega⟩).symm
havehwro : workBlock a b i ≠ rightBlock a b (outputPort hb) :=
(right_ne_low a b (outputPort hb) ⟨i.val + 2, by omega⟩).symm
havehwparent : workBlock a b i ≠ lowPort a b 0:=by
intro h
have hv := congrArg Fin.val h
change i.val + 2 = 0 at hv
omega
havehwzero : workBlock a b i ≠ (0 : Fin (16 + a + b)) := hwparent
simp [d4, d3, d2, d1, d0, initial_stacks, binaryPorts,
hwli, hwlo, hwri, hwro ]
havehleft : d4.stacks (binaryPorts a b ha hb).left = wa :=by
simp [d4, d3, d2, d1, binaryPorts]
havehright : d4.stacks (binaryPorts a b ha hb).right = wb :=by
simp [d4, binaryPorts]
haveharith : ∃ nt rf word,
Eval (if isMul thenArithmeticCalls.multiplyAt (binaryPorts a b ha hb)
elseArithmeticCalls.addAt (binaryPorts a b ha hb)) nt d4
⟨rf, Function.update d4.stacks (lowPort a b 1) word⟩ ∧BinaryArithmetic.toNat word = (if isMul thenBinaryArithmetic.toNat wa * BinaryArithmetic.toNat wb elseBinaryArithmetic.toNat wa + BinaryArithmetic.toNat wb) ∧
word.length ≤ wa.length + wb.length + 1∧
nt ≤100 * (wa.length + wb.length + 1)^2:=by
cases isMul with
| false=>
obtain ⟨t, r, w, he, hv, hw, ht⟩ :=ArithmeticCalls.addAt_eval (binaryPorts a b ha hb) d4 hvalid
rw [hleft, hright] at hv hw ht
exact ⟨t, r, w, he, hv, hw, ht⟩
| true=>
obtain ⟨t, r, w, he, hv, hw, ht⟩ :=ArithmeticCalls.multiplyAt_eval (binaryPorts a b ha hb) d4 hvalid
rw [hleft, hright] at hv hw ht
exact ⟨t, r, w, he, hv, hw, ht⟩
obtain ⟨nt, rf, word, heT, hvT, hwT, htT⟩ := harith
letd5 : State8 (16 + a + b) := ⟨rf, Function.update d4.stacks (lowPort a b 1) word⟩
letd6 : State8 (16 + a + b) :=
⟨readRegs d5.regs 01 none, Function.update d5.stacks (rightBlock a b (outputPort hb)) []⟩
letd7 : State8 (16 + a + b) :=
⟨readRegs d6.regs 01 none, Function.update d6.stacks (rightBlock a b (inputPort hb)) []⟩
letd8 : State8 (16 + a + b) :=
⟨readRegs d7.regs 01 none, Function.update d7.stacks (leftBlock a b (outputPort ha)) []⟩
letd9 : State8 (16 + a + b) :=
⟨readRegs d8.regs 01 none, Function.update d8.stacks (leftBlock a b (inputPort ha)) []⟩
havehcB1 : Eval (clearCmd (rightBlock a b (outputPort hb))) (2 * wb.length + 3) d5 d6 :=by
simpa [d6, d5, d4] using clear_eval (rightBlock a b (outputPort hb)) d5
havehcB0 : Eval (clearCmd (rightBlock a b (inputPort hb))) (2 * xs.length + 3) d6 d7 :=by
simpa [d7, d6, d5, d4, d3, (rightBlock a b).injective.eq_iff] using
clear_eval (rightBlock a b (inputPort hb)) d6
havehcA1 : Eval (clearCmd (leftBlock a b (outputPort ha))) (2 * wa.length + 3) d7 d8 :=by
simpa [d8, d7, d6, d5, d4, d3, d2] using
clear_eval (leftBlock a b (outputPort ha)) d7
havehcA0 : Eval (clearCmd (leftBlock a b (inputPort ha))) (2 * xs.length + 3) d8 d9 :=by
simpa [d9, d8, d7, d6, d5, d4, d3, d2, d1, (leftBlock a b).injective.eq_iff] using
clear_eval (leftBlock a b (inputPort ha)) d8
havehend : d9 = state hp d9.regs xs word :=by
apply congrArg₂ StructuredProgram.State.mk
· rfl
· funext i
simp only [d8, d7, d6, d5, d4, d3, d2, d1, d0,
parent_input, state, parent_output, Function.update_apply]
split_ifs <;> simp_all
let n := na + nb + 18 * xs.length + 2 * wa.length + 2 * wb.length + 24 + nt
havehrun : Eval (binaryCmd ha hb isMul ca cb) n d0 d9 :=byhave hseq :=Eval.seq hcopyA (Eval.seq heA (Eval.seq hcopyB (Eval.seq heB
(Eval.seq heT (Eval.seq hcB1 (Eval.seq hcB0 (Eval.seq hcA1 hcA0)))))))
convert hseq using1
dsimp [binaryCmd, n]
omega
refine ⟨n, word, d9.regs, ?_, ?_, ?_, ?_⟩
· rw [← hend]
exact hrun
· rw [hvT, hvA, hvB]
cases isMul <;> rfl
· have hw := hwT.trans (ArithmeticExpressionBounds.width_bound hwA hwB)
cases isMul <;> simpa only [Bool.false_eq_true, ↓reduceIte, widthCoeff] using hw
· have htime :=ArithmeticExpressionBounds.binary_cost_bound
(K:=100) hwA hwB htA htB
havehtotal : n ≤ (timeCoeff ea + timeCoeff eb +
100 * (widthCoeff ea + widthCoeff eb + 1)^2 +
100 * (widthCoeff ea + widthCoeff eb + 1)) * (xs.length + 1)^2:=by
dsimp [n]
omega
cases isMul <;> simpa only [Bool.false_eq_true, ↓reduceIte, timeCoeff] using htotal
theoremcommand_spec (e : Expr) :
Spec e (byhave:= slots_ge e; omega) (command e) :=by
proof · 13
lines
induction e with
| input => exact input_spec
| const n => exact const_spec n
| add a b ha hb =>
simpa only [Bool.false_eq_true, ↓reduceIte, command, slots] using
binary_spec (byhave:= slots_ge a; omega) (byhave:= slots_ge b; omega)
false (command a) (command b) ha hb
| mul a b ha hb =>
simpa only [↓reduceIte, command, slots] using
binary_spec (byhave:= slots_ge a; omega) (byhave:= slots_ge b; omega)
true (command a) (command b) ha hb
| pred a ha =>
simpa only [command, slots] using unary_spec (byhave:= slots_ge a; omega) (command a) ha