introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
8.09 kB
import OAI.Computability.SpaceSimulation.Evaluation.Circuit
import OAI.Computability.SpaceSimulation.Register.Static
namespace OAI
/-! Uniform circuit presentation. In contrast to lookup-table evaluation
as a pure algebraic object, actual computation of a Boolean assignment's
function value is needed. -/
namespace Fifths
namespace Circuit
open Bin Modulus Fifths.Comp
variable {I X U : Type}
structure Ready (C : I β†’ Circuit) (x : I β†’ List X) (S cp : I β†’ β„•) : Prop where
widthOf : Comp x S cp (fun i => (C i).width)
countOf : Comp x S cp (fun i => (C i).count)
fitNodes : Fits S (fun i => (C i).nodes)
fitRegisters : Fits S (fun i => (C i).P.n ^ ((C i).width+1))
nav : βˆ€ {U : Type} (r : U β†’ I) {a} (v : βˆ€ i, (C (r i)).Node),
ReComp x S r a (fun i => cp (r i)) β†’
ReComp x S r a (fun i => (v i).val) β†’
ReComp x S r a (fun i => ((C (r i)).base (v i)).toNat) ∧
ReComp x S r a (fun i => (C (r i)).guessing (v i)) ∧
ReComp x S r a (fun i => ((C (r i)).head (v i)).val) ∧
ReComp x S r a (fun i => (C (r i)).first (v i))
children : βˆ€ {U : Type} (r : U β†’ I) {a} (v : βˆ€ i, (C (r i)).Node) k,
ReComp x S r a (fun i => cp (r i)) β†’
ReComp x S r a (fun i => (v i).val) β†’ ReComp x S r a k β†’
(βˆ€ i, k i ≀ (C (r i)).count) β†’
ReComp x S r a (fun i => ((C (r i)).child (v i) (k i)).val)
proposeOf : βˆ€ {U : Type} (r : U β†’ I) {a} (v : βˆ€ i, (C (r i)).Node) z y h,
ReComp x S r a (fun i => cp (r i)) β†’
ReComp x S r a (fun i => (v i).val) β†’
ReComp x S r a z β†’ ReComp x S r a y β†’ ReComp x S r a h β†’
(βˆ€ i, z i ≀ (C (r i)).width+1) β†’ (βˆ€ i, y i ≀ 2^(C (r i)).width) β†’
(βˆ€ i, h i < 2^(C (r i)).width) β†’
ReComp x S r a (fun i => (C (r i)).propose (v i) (z i) (y i) (h i))
allowOf : βˆ€ {U : Type} (r : U β†’ I) {a} (v : βˆ€ i, (C (r i)).Node) z y k h b,
ReComp x S r a (fun i => cp (r i)) β†’
ReComp x S r a (fun i => (v i).val) β†’
ReComp x S r a z β†’ ReComp x S r a y β†’ ReComp x S r a k β†’
ReComp x S r a h β†’ ReComp x S r a b β†’
(βˆ€ i, z i ≀ (C (r i)).width+1) β†’ (βˆ€ i, y i ≀ 2^(C (r i)).width) β†’
(βˆ€ i, k i ≀ (C (r i)).count) β†’
(βˆ€ i, h i < 2^(C (r i)).width) β†’ (βˆ€ i, b i < 2^(C (r i)).width) β†’
ReComp x S r a (fun i => (C (r i)).allows (v i) (z i) (y i) (k i) (h i) (b i))
section Independent
-- Small reusable constructions on represented values.
variable {C : I β†’ Circuit} {x : I β†’ List X} {S a : I β†’ β„•}
theorem slotMap (s : Slot β†’ Slot) {q : I β†’ Slot}
(h : Comp x S a (fun i => (q i).val)) :
Comp x S a (fun i => (s (q i)).val) :=
chooseCase Fin.val Fin.val_injective (fun _ j => (s j).val) h
(fun _j => Comp.constant h.source _)
theorem which {R : βˆ€ i, (C i).Pool}
(p : Comp x S a (fun i => (C i).packPool (R i)))
(s : Slot) : Comp x S a
(fun i => (C i).P.packed (C i).width (R i s)) := by
fin_cases s
Β· exact p.left
Β· exact p.right.left
Β· exact p.right.right
theorem chooseSlot {R : βˆ€ i, (C i).Pool}
(p : Comp x S a (fun i => (C i).packPool (R i)))
{q : I β†’ Slot}
(h : Comp x S a (fun i => (q i).val)) :
Comp x S a (fun i => (C i).P.packed (C i).width (R i (q i))) :=
chooseCase Fin.val Fin.val_injective (fun i k => (C i).P.packed (C i).width (R i k)) h (which p)
theorem combinePool {R : βˆ€ i, (C i).Pool}
(p : βˆ€ s : Slot, Comp x S a (fun i => (C i).P.packed (C i).width (R i s))) :
Comp x S a (fun i => (C i).packPool (R i)) :=
(p 0).pair ((p 1).pair (p 2))
variable (s : Fits S (fun i => (C i).P.n ^ ((C i).width+1)))
(p : Comp x S a (fun i => (C i).P.n))
(q : Comp x S a (fun i => (C i).width))
include s p q
theorem plusComp {R : βˆ€ i, (C i).Pool}
(w : Comp x S a (fun i => (C i).packPool (R i))) {t : I β†’ Slot}
(v : Comp x S a (fun i => (t i).val)) {d : βˆ€ i, (C i).Vec}
(h : Comp x S a (fun i => (C i).P.packed (C i).width (d i))) :
Comp x S a (fun i => (C i).packPool ((C i).P.plus (R i) (t i) (d i))) := by
apply combinePool
intro k
have e := which w k
have l : Comp x S a (fun i => (C i).P.packed (C i).width
(fun j => R i k j + d i j)) := by
apply vector s p q _
intro U r a j u c g
let f := fun i => (C (r i)).P
have x := u.trans (p.pull r)
refine fieldAdd (P := f) x ?_ ?_
Β· exact coord (s.pull r) x (u.trans (e.pull r)) c g
Β· exact coord (s.pull r) x (u.trans (h.pull r)) c g
refine ((v.eq (Comp.constant v.source k.val)).iteB l e).congr (fun i => ?_)
by_cases g : k = t i
Β· simp [← g,plus_at]
Β· have f := Fin.val_injective.ne (Ne.symm g)
simp [f,(C i).P.plus_other _ _ g]
theorem jump {b : βˆ€ i, (C i).F} (w : Comp x S a (fun i => (b i).val))
{g : I β†’ β„•} (h : Comp x S a g) :
Comp x S a (fun i => (C i).P.packed (C i).width
((C i).P.multiple (b i) ((C i).P.aug (C i).width (g i)))) := by
apply vector s p q _
intro U r a j e c m
have x := e.trans (p.pull r)
exact fieldMul x (e.trans (w.pull r)) (valCast x
(wordBit (s.pull r) x (e.trans (q.pull r)) (e.trans (h.pull r)) c m))
theorem jumpOne {b : βˆ€ i, (C i).F} (w : Comp x S a (fun i => (b i).val))
{g : I β†’ β„•} (h : Comp x S a g) :
Comp x S a (fun i => (C i).P.packed (C i).width
((C i).P.spike (b i) (g i))) := by
apply vector s p q _
intro U r a j e c m
have x := e.trans (p.pull r)
refine ((c.eq (e.trans (h.pull r))).iteB (e.trans (w.pull r)) (valZero x)).congr
(fun i => ?_)
by_cases f : j i = g (r i) <;> simp [spike,f]
theorem formComp (k : Pc) {X' : βˆ€ i, (C i).Request} (w : Comp x S a
(fun i => (C i).packX (X' i)))
(r : Comp x S a (fun i => (C i).count))
{s' z y j m : I β†’ β„•} {b g : βˆ€ i, (C i).F} (c₁ : Comp x S a s') (cβ‚‚ : Comp x S a z)
(c₃ : Comp x S a y) (cβ‚„ : Comp x S a j) (cβ‚… : Comp x S a m)
(c₆ : Comp x S a (fun i => (b i).val)) (c₇ : Comp x S a (fun i => (g i).val)) :
Comp x S a (fun i => (C i).packFrame ((C i).form (X' i) k (s' i) (z i) (y i)
(j i) (m i) (b i) (g i))) :=
w.pair ((Comp.constant w.source k.code).pair
((p.min c₁).pair ((q.plus1.min cβ‚‚).pair
((((Comp.constant p.source 2).pow q (fitBitsD s)).min c₃).pair
((r.min cβ‚„).pair ((p.min cβ‚…).pair (c₆.pair c₇)))))))
end Independent
namespace Ready
variable {C : I β†’ Circuit} {x : I β†’ List X} {S cp : I β†’ β„•}
theorem uniformDegree (c : Ready C x S cp) :
Comp x S cp (fun i => (C i).outerDegree) :=
c.countOf.plus1.mul c.widthOf
theorem modulusN (c : Ready C x S cp) :
Comp x S cp (fun i => (C i).P.n) :=
c.uniformDegree.chosenN
theorem frameSpace (c : Ready C x S cp) {r : U β†’ I} (f : βˆ€ i, (C (r i)).Frame) :
Fits (fun i => S (r i)) (fun i => (C (r i)).packFrame (f i)) := by
have h₁ := c.fitNodes.pull r
have hβ‚‚ := c.modulusN.target.pull r
have h₃ := c.widthOf.target.pull r
have hβ‚„ := c.countOf.target.pull r
have hβ‚… := (fitBitsD c.fitRegisters).pull r
have w (g : βˆ€ i, (C (r i)).F) := fitElement (P := fun i => (C (r i)).P) hβ‚‚ g
have q : Fits (fun i => S (r i)) (fun i => (C (r i)).packX (f i).x) :=
((Fits.const _ 3).mono (fun i => (f i).x.target.isLt.le)).pair
((h₁.mono (fun i => (f i).x.node.isLt.le)).pair (w _))
refine q.pair (((Fits.const _ 7).mono ?_).pair
((hβ‚‚.mono ?_).pair (((h₃.add (Fits.const _ 1)).mono ?_).pair
((hβ‚….mono ?_).pair ((hβ‚„.mono ?_).pair ((hβ‚‚.mono ?_).pair ((w _).pair (w _))))))))
Β· intro i; cases (f i).pc <;> decide
all_goals intro i
Β· exact Nat.le_of_lt_succ (f i).s.isLt
Β· exact Nat.le_of_lt_succ (f i).z.isLt
Β· exact Nat.le_of_lt_succ (f i).y.isLt
Β· exact Nat.le_of_lt_succ (f i).i.isLt
Β· exact Nat.le_of_lt_succ (f i).r.isLt
theorem poolSpace (c : Ready C x S cp) {r : U β†’ I} (R : βˆ€ i, (C (r i)).Pool) :
Fits (fun i => S (r i)) (fun i => (C (r i)).packPool (R i)) := by
have f (j : Slot) := fitPacked (P := fun i => (C (r i)).P)
(c.fitRegisters.pull r) (fun i => R i j)
exact (f 0).pair ((f 1).pair (f 2))
end Ready
end Circuit
end Fifths
end OAI