hyperkitty-constraint-dsl / lean /HyperKitty /SLAComposition.lean
SNAPKITTYWEST's picture
push from SNAPKITTYWEST/hyperkitty-constraint-dsl
224e773 verified
Raw History Blame Contribute Delete
10.3 kB
27
def Ledger.evolve (λ δλ : Ledger) : Option Ledger :=
body · 7 lines
  if h : δλ.balance ∧ δλ.ω = 0 then
    some { s := λ.s + δλ.s
           δ := λ.δ + δλ.δ
           ι := λ.ι + δλ.ι
           ω := λ.ω }
  else
    none
41
def Ledger.reconcile (λ : Ledger) : ℤ := λ.δ + λ.ι
48
def Ledger.identity : Ledger := Ledger.mkBalanced 0 0 0
56
theorem compose_associative (λ₁ λ₂ λ₃ : Ledger)
    (h12 : λ₁.ω = λ₂.ω) (h23 : λ₂.ω = λ₃.ω)
    (hb1 : λ₁.balance) (hb2 : λ₂.balance) (hb3 : λ₃.balance) :
    let left_comp := (λ₁.comp λ₂) >>= fun x => x.comp λ₃
proof · 26 lines
    let right_comp := λ₁.comp (λ₂.comp λ₃)
    (left_comp.isSome ∧ left_comp.get (by simp [Ledger.comp, h12, h23])).balance ∧
    (right_comp.isSome ∧ right_comp.get (by simp [Ledger.comp, h12, h23])).balance := by
  constructor
  · -- Left associativity case: (λ₁ ∘ λ₂) ∘ λ₃
    have step1 : (λ₁.comp λ₂).isSome := by simp [Ledger.comp, h12]
    have comp12 := (λ₁.comp λ₂).get (by simp [Ledger.comp, h12])
    have step2 : (comp12.comp λ₃).isSome := by
      simp [Ledger.comp, h23]
      have : comp12.ω = λ₃.ω := by simp [Ledger.comp, h12, h23]
      exact this
    constructor
    · exact step2
    · simp [Ledger.comp, h12, h23, Ledger.balance]
      omega
  · -- Right associativity case: λ₁ ∘ (λ₂ ∘ λ₃)
    have step1 : (λ₂.comp λ₃).isSome := by simp [Ledger.comp, h23]
    have comp23 := (λ₂.comp λ₃).get (by simp [Ledger.comp, h23])
    have step2 : (λ₁.comp comp23).isSome := by
      simp [Ledger.comp, h12]
      have : λ₁.ω = comp23.ω := by simp [Ledger.comp, h12, h23]
      exact this
    constructor
    · exact step2
    · simp [Ledger.comp, h12, h23, Ledger.balance]
      omega
92
theorem compose_commutative (λ₁ λ₂ : Ledger)
    (h : λ₁.ω = λ₂.ω)
    (hb1 : λ₁.balance) (hb2 : λ₂.balance) :
    let result_12 := (λ₁.comp λ₂).get (by simp [Ledger.comp, h])
proof · 5 lines
    let result_21 := (λ₂.comp λ₁).get (by simp [Ledger.comp, h.symm])
    result_12.balance ∧ result_21.balance ∧
    result_12.reconcile = result_21.reconcile := by
  simp [Ledger.comp, h, Ledger.balance, Ledger.reconcile]
  omega
108
theorem evolution_chain_balanced (λ : Ledger) (deltas : List Ledger)
    (hb : λ.balance)
    (h_deltas : ∀ d ∈ deltas, d.balance ∧ d.ω = 0) :
    let final := List.foldl (fun acc d => acc >>= fun a => a.evolve d) (some λ) deltas
proof · 18 lines
    final.isSome ∧ (final.get (by simp)).balance := by
  induction deltas generalizing λ with
  | nil =>
    simp [Ledger.evolve, hb]
  | cons d ds ih =>
    simp at h_deltas ⊢
    have hd : d.balance ∧ d.ω = 0 := h_deltas d (List.mem_cons_self d ds)
    have hds : ∀ d' ∈ ds, d'.balance ∧ d'.ω = 0 := fun d' hd' =>
      h_deltas d' (List.mem_cons_of_mem d hd')
    simp [Ledger.evolve, hd.1, hd.2]
    have evolved_balance : ((λ.evolve d).get (by simp [Ledger.evolve, hd.1, hd.2])).balance := by
      simp [Ledger.evolve, hd.1, hd.2, Ledger.balance]
      have rearrange : (λ.δ + d.δ) + (λ.ι + d.ι) = (λ.δ + λ.ι) + (d.δ + d.ι) := by ring
      rw [rearrange]
      simp [Ledger.balance] at hb hd
      rw [hb, hd.1]
      ring
    exact ih ((λ.evolve d).get (by simp [Ledger.evolve, hd.1, hd.2])) evolved_balance hds
137
theorem invariant_preserved_history (λ₀ : Ledger) (deltas : List Ledger)
    (h0 : λ₀.balance)
    (h_deltas : ∀ d ∈ deltas, d.balance ∧ d.ω = 0) :
    let final := List.foldl (fun acc d => acc >>= fun a => a.evolve d) (some λ₀) deltas
proof · 21 lines
    final.isSome → (final.get (by simp)).balance := by
  intro h_final
  induction deltas generalizing λ₀ with
  | nil =>
    simp [Ledger.evolve] at h_final ⊢
    exact h0
  | cons d ds ih =>
    simp at h_deltas
    have hd : d.balance ∧ d.ω = 0 := h_deltas d (List.mem_cons_self d ds)
    have hds : ∀ d' ∈ ds, d'.balance ∧ d'.ω = 0 := fun d' hd' =>
      h_deltas d' (List.mem_cons_of_mem d hd')
    simp [Ledger.evolve, hd.1, hd.2] at h_final ⊢
    let λ_evolved := (λ₀.evolve d).get (by simp [Ledger.evolve, hd.1, hd.2])
    have h_evolved : λ_evolved.balance := by
      simp [Ledger.evolve, hd.1, hd.2, Ledger.balance]
      have : (λ₀.δ + d.δ) + (λ₀.ι + d.ι) = (λ₀.δ + λ₀.ι) + (d.δ + d.ι) := by ring
      rw [this]
      simp [Ledger.balance] at h0 hd
      rw [h0, hd.1]
      ring
    exact ih λ_evolved h_evolved hds h_final
169
theorem reconcile_idempotent (λ δλ : Ledger)
    (h_balance : δλ.balance) (h_inv : δλ.ω = 0) :
    let evolved := (λ.evolve δλ).get (by simp [Ledger.evolve, h_balance, h_inv])
proof · 3 lines
    evolved.reconcile = 0 := by
  simp [Ledger.evolve, h_balance, h_inv, Ledger.reconcile]
  omega
181
theorem compose_identity_right (λ : Ledger) (hb : λ.balance) :
    let id_result := (λ.comp Ledger.identity).get (by simp [Ledger.comp, Ledger.identity, Ledger.mkBalanced])
proof · 3 lines
    id_result.s = λ.s ∧ id_result.δ = λ.δ ∧ id_result.ι = λ.ι ∧ id_result.ω = λ.ω := by
  simp [Ledger.comp, Ledger.identity, Ledger.mkBalanced]
  omega
192
theorem compose_identity_left (λ : Ledger) (hb : λ.balance) :
    let id_result := (Ledger.identity.comp λ).get (by simp [Ledger.comp, Ledger.identity, Ledger.mkBalanced])
proof · 3 lines
    id_result.s = λ.s ∧ id_result.δ = λ.δ ∧ id_result.ι = λ.ι ∧ id_result.ω = λ.ω := by
  simp [Ledger.comp, Ledger.identity, Ledger.mkBalanced]
  omega
203
theorem composition_always_balanced (λ₁ λ₂ : Ledger)
    (h : λ₁.ω = λ₂.ω)
    (hb1 : λ₁.balance) (hb2 : λ₂.balance) :
    (λ₁.comp λ₂).isSome ∧ ((λ₁.comp λ₂).get (by simp [Ledger.comp, h])).balance := by
proof · 6 lines
  constructor
  · simp [Ledger.comp, h]
  · simp [Ledger.comp, h, Ledger.balance]
    have eq1 : λ₁.δ + λ₁.ι = 0 := hb1
    have eq2 : λ₂.δ + λ₂.ι = 0 := hb2
    omega
219
theorem evolution_preserves_domain (λ δλ : Ledger)
    (h_balance : δλ.balance) (h_inv : δλ.ω = 0) :
    let evolved := (λ.evolve δλ).get (by simp [Ledger.evolve, h_balance, h_inv])
proof · 2 lines
    evolved.ω = λ.ω := by
  simp [Ledger.evolve, h_balance, h_inv]
230
theorem sequential_evolution_monoid (λ : Ledger) (δ₁ δ₂ : Ledger)
    (hb0 : λ.balance)
    (hb1 : δ₁.balance) (hω1 : δ₁.ω = 0)
    (hb2 : δ₂.balance) (hω2 : δ₂.ω = 0) :
    let step1 := (λ.evolve δ₁).get (by simp [Ledger.evolve, hb1, hω1])
proof · 6 lines
    let step2 := (step1.evolve δ₂).get (by simp [Ledger.evolve, hb2, hω2])
    step1.balance ∧ step2.balance := by
  simp [Ledger.evolve, hb1, hω1, hb2, hω2, Ledger.balance]
  constructor
  · omega
  · omega
247
theorem composition_distributivity (λ₁ λ₂ λ₃ : Ledger)
    (h12 : λ₁.ω = λ₂.ω) (h13 : λ₁.ω = λ₃.ω)
    (hb1 : λ₁.balance) (hb2 : λ₂.balance) (hb3 : λ₃.balance) :
    let comp12 := (λ₁.comp λ₂).get (by simp [Ledger.comp, h12])
proof · 5 lines
    let comp13 := (λ₁.comp λ₃).get (by simp [Ledger.comp, h13])
    let comp_both := (comp12.comp λ₃).get (by simp [Ledger.comp, h13])
    comp_both.s = λ₁.s + λ₂.s + λ₃.s := by
  simp [Ledger.comp, h12, h13]
  omega
262
theorem identity_unique (λ : Ledger)
    (h : (λ.comp Ledger.identity).get (by simp [Ledger.comp, Ledger.identity, Ledger.mkBalanced]) = λ ∧
         (Ledger.identity.comp λ).get (by simp [Ledger.comp, Ledger.identity, Ledger.mkBalanced]) = λ) :
    λ = (λ.comp Ledger.identity).get (by simp [Ledger.comp, Ledger.identity, Ledger.mkBalanced]) := by
proof · 1 line
  exact h.1.symm