Download lean/HyperKitty/SLAComposition.lean from Snapkitty/hyperkitty-constraint-dsl: direct link, hf CLI and curl.
- Browser
- Download file 10.3 kB
-
https://huggingface.co/Snapkitty/hyperkitty-constraint-dsl/resolve/main/lean/HyperKitty/SLAComposition.lean
- Command line
-
hf download hf://Snapkitty/hyperkitty-constraint-dsl/lean/HyperKitty/SLAComposition.lean
-
curl -L -o SLAComposition.lean https://huggingface.co/Snapkitty/hyperkitty-constraint-dsl/resolve/main/lean/HyperKitty/SLAComposition.lean
10.3 kB
Lean 4 · 15
declarations statementsfull
def Ledger.evolve (λ δλ : Ledger) : Option Ledger := body · 7 lines
if h : δλ.balance ∧ δλ.ω = 0 then
some { s := λ.s + δλ.s
δ := λ.δ + δλ.δ
ι := λ.ι + δλ.ι
ω := λ.ω }
else
nonedef Ledger.reconcile (λ : Ledger) : ℤ := λ.δ + λ.ι def Ledger.identity : Ledger := Ledger.mkBalanced 0 0 0 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]
omegatheorem 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]
omegatheorem 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 hdstheorem 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_finaltheorem 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]
omegatheorem 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]
omegatheorem 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]
omegatheorem 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
omegatheorem 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]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
· omegatheorem 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]
omegatheorem 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.symmOutline
- def
Ledger.evolve27 - def
Ledger.reconcile41 - def
Ledger.identity48 - theorem
compose_associative56 - theorem
compose_commutative92 - theorem
evolution_chain_balanced108 - theorem
invariant_preserved_history137 - theorem
reconcile_idempotent169 - theorem
compose_identity_right181 - theorem
compose_identity_left192 - theorem
composition_always_balanced203 - theorem
evolution_preserves_domain219 - theorem
sequential_evolution_monoid230 - theorem
composition_distributivity247 - theorem
identity_unique262