math / data /lean /OAI /Computability /WLIdentification /ConstraintSchemas.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
14.7 kB
import OAI.Computability.WLIdentification.RowOffsets
namespace OAI
namespace WLIdentification
structure ComparisonSchemas (r : β„•) (P E : Type) where
pointIndex : P β†ͺ β„•
edgeIndex : E β†ͺ β„•
dimension : P β†’ β„•
dimension_bound : βˆ€ p, dimension p = 1 ∨ dimension p = 2
source : E β†’ P
target : E β†’ P
domain : E β†’ Fin r β†’ Finset Bool
domain_nonempty : βˆ€ e i, (domain e i).Nonempty
mapping : (e : E) β†’ (i : Fin r) β†’ {a // a ∈ domain e i} β†’ Bool
mapping_injective : βˆ€ e i, Function.Injective (mapping e i)
left : (e : E) β†’ (Fin (dimension (source e)) β†’ Bit) β†’β‚—[Bit] Bit
right : (e : E) β†’ (Fin (dimension (target e)) β†’ Bit) β†’β‚—[Bit] Bit
namespace ComparisonSchemas
variable {r : β„•} {P E : Type} (S : ComparisonSchemas r P E)
abbrev InternalType (_S : ComparisonSchemas r P E) (n : β„•) := P βŠ• (E Γ— Fin (n + 1))
abbrev Link (_S : ComparisonSchemas r P E) (n : β„•) := E βŠ• ((E Γ— Fin n) βŠ• E)
def first (n : β„•) : S.Link n β†’ S.InternalType n
| .inl e => .inl (S.source e)
| .inr (.inl (e, i)) => .inr (e, i.castSucc)
| .inr (.inr e) => .inr (e, Fin.last n)
def second (n : β„•) : S.Link n β†’ S.InternalType n
| .inl e => .inr (e, 0)
| .inr (.inl (e, i)) => .inr (e, i.succ)
| .inr (.inr e) => .inl (S.target e)
def wire (n : β„•) : S.Link n β†’ E
| .inl e => e
| .inr (.inl (e, _)) => e
| .inr (.inr e) => e
def layoutDimension (n : β„•) : S.InternalType n β†’ β„•
| .inl p => S.dimension p
| .inr _ => 1
def layoutLabels (n : β„•) : S.InternalType n β†’ Fin r β†’ Finset Bool
| .inl _, _ => Finset.univ
| .inr (e, _), i => S.domain e i
def firstLabel (n : β„•) (l : S.Link n) (i : Fin r) :
{a // a ∈ S.domain (S.wire n l) i} β†’ {a // a ∈ S.layoutLabels n (S.first n l) i} := by
cases l with
| inl e => exact fun a => ⟨a.val, Finset.mem_univ _⟩
| inr l => cases l with
| inl p => exact id
| inr e => exact id
def secondLabel (n : β„•) (l : S.Link n) (i : Fin r) :
{a // a ∈ S.domain (S.wire n l) i} β†’ {a // a ∈ S.layoutLabels n (S.second n l) i} := by
cases l with
| inl e => exact id
| inr l => cases l with
| inl p => exact id
| inr e => exact fun a => ⟨S.mapping e i a, Finset.mem_univ _⟩
def firstForm (n : β„•) (l : S.Link n) :
(Fin (S.layoutDimension n (S.first n l)) β†’ Bit) β†’β‚—[Bit] Bit := by
cases l with
| inl e => exact S.left e
| inr l => cases l with
| inl p => exact (LinearMap.proj (0 : Fin 1))
| inr e => exact (LinearMap.proj (0 : Fin 1))
def secondForm (n : β„•) (l : S.Link n) :
(Fin (S.layoutDimension n (S.second n l)) β†’ Bit) β†’β‚—[Bit] Bit := by
cases l with
| inl e => exact (LinearMap.proj (0 : Fin 1))
| inr l => cases l with
| inl p => exact (LinearMap.proj (0 : Fin 1))
| inr e => exact S.right e
def layout (n : β„•) : RingLayout r (S.InternalType n) (S.Link n) where
typeIndex := NaturalCode.sum S.pointIndex (NaturalCode.prod S.edgeIndex (NaturalCode.fin _))
linkIndex := NaturalCode.sum S.edgeIndex (NaturalCode.sum (NaturalCode.prod S.edgeIndex (NaturalCode.fin _)) S.edgeIndex)
dimension := S.layoutDimension n
dimension_bound t := by cases t with
| inl p => exact S.dimension_bound p
| inr p => exact Or.inl rfl
labels := S.layoutLabels n
labels_nonempty t i := by cases t with
| inl p => exact ⟨false, Finset.mem_univ _⟩
| inr p => exact S.domain_nonempty p.1 i
first := S.first n
second := S.second n
endpoints_ne l := by
cases l with
| inl e => intro h; cases h
| inr l => cases l with
| inl p =>
intro h
have hi := congrArg (fun t : S.InternalType n =>
match t with | .inl _ => 0 | .inr p => p.2.val) h
change p.2.val = p.2.val + 1 at hi
omega
| inr e => intro h; cases h
internal_endpoint l := by
cases l with
| inl e => exact Or.inr rfl
| inr l => cases l <;> exact Or.inl rfl
matchLabels l i := S.domain (S.wire n l) i
matchLabels_nonempty l i := S.domain_nonempty (S.wire n l) i
firstLabel := S.firstLabel n
secondLabel := S.secondLabel n
firstLabel_injective l i := by
cases l with
| inl e =>
intro a b h
apply Subtype.ext
exact congrArg (fun x : {a // a ∈ S.layoutLabels n (S.first n (.inl e)) i} => x.val) h
| inr l => cases l <;> exact Function.injective_id
secondLabel_injective l i := by
cases l with
| inl e => exact Function.injective_id
| inr l => cases l with
| inl p => exact Function.injective_id
| inr e => exact fun a b h => S.mapping_injective e i (congrArg Subtype.val h)
firstForm := S.firstForm n
secondForm := S.secondForm n
end ComparisonSchemas
namespace CycleRows.RowSum
variable {r : β„•} {A : Fin r β†’ Type} {V : Type} [AddCommMonoid V]
protected theorem sum {I : Type} (s : Finset I)
(h : I β†’ ((i : Fin r) β†’ A i) β†’ V) (hh : βˆ€ j ∈ s, RowSum A V (h j)) :
RowSum A V (fun a => βˆ‘ j ∈ s, h j a) := by
classical
induction s using Finset.induction_on with
| empty => simpa only [Finset.sum_empty] using (RowSum.zero (A := A) (V := V))
| @insert j s hj ih =>
simpa only [Finset.sum_insert hj] using
(hh j (Finset.mem_insert_self _ _)).add
(ih (fun i hi => hh i (Finset.mem_insert_of_mem hi)))
protected theorem pi {J : Type} {W : J β†’ Type} [βˆ€ j, AddCommMonoid (W j)]
(h : ((i : Fin r) β†’ A i) β†’ (j : J) β†’ W j)
(hh : βˆ€ j, RowSum A (W j) (fun a => h a j)) : RowSum A ((j : J) β†’ W j) h := by
choose f hf using hh
refine ⟨fun i u v j => f j i u v, ?_⟩
intro a
funext j
simpa only [Finset.sum_apply] using hf j a
end CycleRows.RowSum
namespace WireAlgebra
def prefixSum {n : β„•} (c : Fin n β†’ Bit) (j : Fin (n + 1)) : Bit :=
βˆ‘ i : Fin n, if i.val < j.val then c i else 0
@[simp]
theorem prefix_zero {n : β„•} (c : Fin n β†’ Bit) : prefixSum c 0 = 0 := by
simp [prefixSum]
@[simp]
theorem prefix_last {n : β„•} (c : Fin n β†’ Bit) : prefixSum c (Fin.last n) = βˆ‘ i, c i := by
simp [prefixSum]
theorem prefix_succ {n : β„•} (c : Fin n β†’ Bit) (j : Fin n) :
prefixSum c j.succ = prefixSum c j.castSucc + c j := by
have hi (i : Fin n) : (if i.val < j.val + 1 then c i else 0) =
(if i.val < j.val then c i else 0) + (if i = j then c i else 0) := by
by_cases h : i.val < j.val
Β· have hne : i β‰  j := fun hh => by subst i; omega
simp [h, hne, show i.val < j.val + 1 by omega]
Β· by_cases he : i = j
Β· subst i; simp
Β· have hn : Β¬i.val < j.val + 1 := by
intro hh
exact he (Fin.ext (by omega))
simp [h, he, hn]
simp only [prefixSum, Fin.val_succ, Fin.val_castSucc, hi, Finset.sum_add_distrib]
simp
theorem sum_adjacent {n : β„•} (h : Fin (n + 1) β†’ Bit) :
βˆ‘ i : Fin n, (h i.castSucc + h i.succ) = h 0 + h (Fin.last n) := by
have ha := Fin.sum_univ_castSucc h
have hb := Fin.sum_univ_succ h
have hab : (βˆ‘ i : Fin n, h i.castSucc) + h (Fin.last n) =
h 0 + βˆ‘ i : Fin n, h i.succ := ha.symm.trans hb
rw [Finset.sum_add_distrib]
calc
_ = ((βˆ‘ i : Fin n, h i.castSucc) + (βˆ‘ i : Fin n, h i.succ)) +
(h (Fin.last n) + h (Fin.last n)) := by
rw [CharTwo.add_self_eq_zero, add_zero]
_ = ((βˆ‘ i : Fin n, h i.castSucc) + h (Fin.last n)) +
(βˆ‘ i : Fin n, h i.succ) + h (Fin.last n) := by abel
_ = _ := by
rw [hab]
simp only [add_assoc, CharTwo.add_self_eq_zero, zero_add]
/-- A wire's internal scalars cancel; the converse uses explicit prefixSum sums. -/
theorem chain_lifting {n : β„•} (left right first last : Bit) (middle : Fin n β†’ Bit) :
(βˆƒ h : Fin (n + 1) β†’ Bit,
left + h 0 = first ∧
(βˆ€ i, h i.castSucc + h i.succ = middle i) ∧
h (Fin.last n) + right = last) ↔
left + right = first + (βˆ‘ i, middle i) + last := by
constructor
· rintro ⟨h, hfirst, hmiddle, hlast⟩
rw [← hfirst, ← hlast, ← funext hmiddle, sum_adjacent]
symm
calc
_ = left + right + (h 0 + h 0) + (h (Fin.last n) + h (Fin.last n)) := by abel
_ = _ := by simp only [CharTwo.add_self_eq_zero, add_zero]
Β· intro he
refine ⟨fun j => left + first + prefixSum middle j, ?_, ?_, ?_⟩
Β· simp only [prefix_zero, add_zero]
rw [← add_assoc, CharTwo.add_self_eq_zero, zero_add]
Β· intro i
dsimp only
rw [prefix_succ]
calc
_ = ((left + first + prefixSum middle i.castSucc) +
(left + first + prefixSum middle i.castSucc)) + middle i := by abel
_ = middle i := by rw [CharTwo.add_self_eq_zero, zero_add]
Β· dsimp only
rw [prefix_last]
calc
_ = (left + right) + first + (βˆ‘ i, middle i) := by abel
_ = last := by
rw [he]
calc
_ = (first + first) + ((βˆ‘ i, middle i) + (βˆ‘ i, middle i)) + last := by abel
_ = last := by simp only [CharTwo.add_self_eq_zero, zero_add]
end WireAlgebra
namespace WireAlgebra
theorem prefix_equations {n : β„•} (left right first last : Bit) (middle : Fin n β†’ Bit)
(he : left + right = first + (βˆ‘ i, middle i) + last) :
let h := fun j => left + first + prefixSum middle j
(left + h 0 = first) ∧ (βˆ€ i, h i.castSucc + h i.succ = middle i) ∧
(h (Fin.last n) + right = last) := by
dsimp only
constructor
Β· simp only [prefix_zero, add_zero]
rw [← add_assoc, CharTwo.add_self_eq_zero, zero_add]
constructor
Β· intro i
rw [prefix_succ]
calc
_ = ((left + first + prefixSum middle i.castSucc) +
(left + first + prefixSum middle i.castSucc)) + middle i := by abel
_ = middle i := by rw [CharTwo.add_self_eq_zero, zero_add]
Β· rw [prefix_last]
calc
_ = (left + right) + first + (βˆ‘ i, middle i) := by abel
_ = last := by
rw [he]
calc
_ = (first + first) + ((βˆ‘ i, middle i) + (βˆ‘ i, middle i)) + last := by abel
_ = last := by simp only [CharTwo.add_self_eq_zero, zero_add]
end WireAlgebra
namespace ComparisonSchemas
variable {r : β„•} {P E : Type} (S : ComparisonSchemas r P E)
abbrev Address (_S : ComparisonSchemas r P E) :=
Fin r β†’ {a : Bool // a ∈ (Finset.univ : Finset Bool)}
abbrev DomainAddress (e : E) := (i : Fin r) β†’ {a // a ∈ S.domain e i}
def sourceAddress (e : E) (a : S.DomainAddress e) : S.Address :=
fun i => ⟨(a i).val, Finset.mem_univ _⟩
def targetAddress (e : E) (a : S.DomainAddress e) : S.Address :=
fun i => ⟨S.mapping e i (a i), Finset.mem_univ _⟩
def firstShift (n : β„•) (beta : (S.layout n).Block β†’ Bit) (e : E)
(a : S.DomainAddress e) : Bit := (S.layout n).linkShift beta (.inl e) a
def middleShift (n : β„•) (beta : (S.layout n).Block β†’ Bit) (e : E) (j : Fin n)
(a : S.DomainAddress e) : Bit := (S.layout n).linkShift beta (.inr (.inl (e, j))) a
def lastShift (n : β„•) (beta : (S.layout n).Block β†’ Bit) (e : E)
(a : S.DomainAddress e) : Bit := (S.layout n).linkShift beta (.inr (.inr e)) a
def wireShift (n : β„•) (beta : (S.layout n).Block β†’ Bit) (e : E)
(a : S.DomainAddress e) : Bit :=
S.firstShift n beta e a + (βˆ‘ j, S.middleShift n beta e j a) + S.lastShift n beta e a
theorem wireShift_rowSum (n : β„•) (beta : (S.layout n).Block β†’ Bit) (e : E) :
CycleRows.RowSum (fun i => {a // a ∈ S.domain e i}) Bit (S.wireShift n beta e) := by
exact ((S.layout n).linkShift_rowSum beta (.inl e)).add
(CycleRows.RowSum.sum Finset.univ (S.middleShift n beta e)
(fun j _ => (S.layout n).linkShift_rowSum beta (.inr (.inl (e, j))))) |>.add
((S.layout n).linkShift_rowSum beta (.inr (.inr e)))
variable [Fintype P] [Fintype E]
theorem offsets_iff_real_rowSums (hr : 3 ≀ r) (n : β„•)
(beta : (S.layout n).Block β†’ Bit) :
(βˆƒ z, ((S.layout n).affine (by omega)).Solves beta z) ↔
βˆƒ h : (p : P) β†’ S.Address β†’ Fin (S.dimension p) β†’ Bit,
(βˆ€ p, CycleRows.RowSum (fun _ : Fin r => {a : Bool // a ∈ (Finset.univ : Finset Bool)})
(Fin (S.dimension p) β†’ Bit) (h p)) ∧
βˆ€ e (a : S.DomainAddress e),
S.left e (h (S.source e) (S.sourceAddress e a)) +
S.right e (h (S.target e) (S.targetAddress e a)) = S.wireShift n beta e a := by
rw [(S.layout n).offsets_iff_rowSums hr beta]
constructor
· rintro ⟨h, hs, he⟩
refine ⟨fun p => h (.inl p), fun p => hs (.inl p), ?_⟩
intro e a
apply (WireAlgebra.chain_lifting _ _ _ _ _).mp
refine ⟨fun j => h (.inr (e, j)) a (0 : Fin 1), ?_, ?_, ?_⟩
Β· exact he (.inl e) a
Β· intro j
exact he (.inr (.inl (e, j))) a
Β· exact he (.inr (.inr e)) a
· rintro ⟨h, hs, he⟩
let g (e : E) (j : Fin (n + 1)) (a : S.DomainAddress e) : Bit :=
S.left e (h (S.source e) (S.sourceAddress e a)) + S.firstShift n beta e a +
WireAlgebra.prefixSum (fun i => S.middleShift n beta e i a) j
let q : (t : S.InternalType n) β†’ (S.layout n).Address t β†’
Fin ((S.layout n).dimension t) β†’ Bit := fun t => match t with
| .inl p => h p
| .inr (e, j) => fun a _ => g e j a
have hg (e : E) (a : S.DomainAddress e) :
(S.left e (h (S.source e) (S.sourceAddress e a)) + g e 0 a = S.firstShift n beta e a) ∧
(βˆ€ j, g e j.castSucc a + g e j.succ a = S.middleShift n beta e j a) ∧
(g e (Fin.last n) a + S.right e (h (S.target e) (S.targetAddress e a)) = S.lastShift n beta e a) :=
WireAlgebra.prefix_equations _ _ _ _ _ (he e a)
refine ⟨q, ?_, ?_⟩
Β· intro t
cases t with
| inl p => exact hs p
| inr p =>
apply CycleRows.RowSum.pi
intro j
have hf := ((hs (S.source p.1)).map (S.left p.1).toAddMonoidHom).pullback
(fun i (a : {a // a ∈ S.domain p.1 i}) => (⟨a.val, Finset.mem_univ _⟩ : {b : Bool // b ∈ (Finset.univ : Finset Bool)}))
have hb := (S.layout n).linkShift_rowSum beta (.inl p.1)
have hc : CycleRows.RowSum (fun i => {a // a ∈ S.domain p.1 i}) Bit
(fun a => WireAlgebra.prefixSum (fun i => S.middleShift n beta p.1 i a) p.2) := by
apply CycleRows.RowSum.sum
intro i _
by_cases hi : i.val < p.2.val
Β· simpa only [hi, ite_true, middleShift, RingLayout.Match, layout, wire] using
(S.layout n).linkShift_rowSum beta (.inr (.inl (p.1, i)))
Β· simpa only [hi, ite_false] using
(CycleRows.RowSum.zero (A := fun i => {a // a ∈ S.domain p.1 i}) (V := Bit))
exact (hf.add hb).add hc
Β· intro l a
cases l with
| inl e => exact (hg e a).1
| inr l => cases l with
| inl p => exact (hg p.1 a).2.1 p.2
| inr e => exact (hg e a).2.2
end ComparisonSchemas
end WLIdentification
end OAI