-- Bank fact: guardian-act-a-rescue-moves-only-what-the-fix-needs-within-the-mandate-and-only-into-the-persons-own-loan -- Primary implementation: agent/contracts/HarmonicGuardian.sol (planOf, repayNeed, addNeed, execute), mirrored by -- agent/src/guardianact.js (rescueOf) and sized by agent/src/guardianmath.js (fixOf). -- Machine checked with Lean 4.22.0, core only, no Mathlib (bank/proofs/lean-toolchain). -- Check it: lean GuardianAct.lean (the #print axioms lines list only Lean's standard axioms) /- THE GUARDIAN ACTING FOR A PERSON: a rescue moves no more than the fix needs, no more than the person allowed, and only while the loan is past their line. Stated exactly and proved. A person sets a mandate once from their own wallet: their line, how the Guardian may rescue the loan, and the most that may ever leave their wallet for it. When the loan is at or past the line, the contract sizes the rescue with the Guardian's own arithmetic (the repayment: the excess over the room the line leaves, up to the next whole cent, the whole debt when that would pass it), then moves no more than the budget left, the wallet's balance and its allowance, with its fee, if any, inside them and rounded down. Proved over that rule, for every debt, limit, line, budget, balance, allowance and fee: * a loan inside its line is never acted on, * a rescue never moves more than the fix needs, and never more than the debt, * what it moves and its fee together never pass the budget left, the balance or the allowance, * the fee is never more than its share of what moved, * with no fee, everything available may move, * a rescue that is not cut short by the person's limits brings the loan back inside the line, a tenth to spare, * a repayment that moves anything leaves less owed, and collateral added never lowers the venue's limit. -/ namespace GuardianAct /-- The venue's limit on a loan, in the loan token's smallest unit, each division floored as the venue floors it. -/ def limit (c p l : Nat) : Nat := c * p / 1000000000000000000000000000000000000 * l / 1000000000000000000 /-- The room a line of `F` percent leaves on a limit `L`: nine tenths of the line's share, floored. -/ def room (L F : Nat) : Nat := L * (100 - F) * 9 / 1000 /-- Up to the next whole cent: ten thousand of a six-decimal dollar's smallest unit. -/ def ceilCent (n : Nat) : Nat := (n + 9999) / 10000 * 10000 /-- AT OR PAST THE LINE: owing something, with the debt using (100 - F)% of the limit or more. The only state in which the contract acts (it refuses every other with InsideTheLine or NothingOwed). -/ def past (d L F : Nat) : Prop := d ≠ 0 ∧ L * (100 - F) ≤ d * 100 /-- THE REPAYMENT A RESCUE SIZES (the contract's repayNeed): the excess over the room up to the next cent, and the whole debt when that would pass it. -/ def need (d L F : Nat) : Nat := if d ≤ ceilCent (d - room L F) then d else ceilCent (d - room L F) /-- What may move at all: the least of the budget left, the wallet's balance and its allowance. -/ def avail (left bal alw : Nat) : Nat := min left (min bal alw) /-- The most that may move with a fee of `f` basis points inside what is available. -/ def most (a f : Nat) : Nat := a * 10000 / (10000 + f) /-- WHAT A RESCUE MOVES: the need, or the most available when that is less. -/ def moved (n a f : Nat) : Nat := min n (most a f) /-- The fee on what moved, in basis points, rounded down. -/ def fee (m f : Nat) : Nat := m * f / 10000 /-- Where a loan stands against a line of `F` percent: 0 safe, 1 at the line, 2 close to a sale, 3 past the limit. -/ def level (d L F : Nat) : Nat := if d = 0 then 0 else if L < d then 3 else if L * (100 - min F 10) ≤ d * 100 then 2 else if L * (100 - F) ≤ d * 100 then 1 else 0 /-- A loan inside its line is never acted on. -/ theorem inside_is_never_acted_on (d L F : Nat) (h : d * 100 < L * (100 - F)) : ¬ past d L F := by intro ⟨_, hp⟩ omega /-- A rescue never moves more than the fix needs. -/ theorem moved_le_need (n a f : Nat) : moved n a f ≤ n := Nat.min_le_left _ _ /-- The fix is never more than the debt. -/ theorem need_le_debt (d L F : Nat) : need d L F ≤ d := by unfold need split <;> omega /-- So a repayment never moves more than the debt. -/ theorem moved_le_debt (d L F a f : Nat) : moved (need d L F) a f ≤ d := Nat.le_trans (moved_le_need _ a f) (need_le_debt d L F) /-- THE KEY: what a rescue moves and its fee together never pass what is available. -/ theorem moved_and_fee_within_avail (n a f : Nat) : moved n a f + fee (moved n a f) f ≤ a := by unfold fee generalize hm : moved n a f = m have hmq : m ≤ a * 10000 / (10000 + f) := by rw [← hm]; exact Nat.min_le_right _ _ have hq : a * 10000 / (10000 + f) * (10000 + f) ≤ a * 10000 := Nat.div_mul_le_self _ _ have h1 : m * (10000 + f) ≤ a * 10000 := Nat.le_trans (Nat.mul_le_mul_right _ hmq) hq have h2 : m * (10000 + f) = m * 10000 + m * f := Nat.mul_add m 10000 f generalize hx : m * f = x at * generalize hy : m * (10000 + f) = y at * omega /-- So it never passes the budget left. -/ theorem within_budget (n left bal alw f : Nat) : moved n (avail left bal alw) f + fee (moved n (avail left bal alw) f) f ≤ left := Nat.le_trans (moved_and_fee_within_avail n _ f) (Nat.min_le_left _ _) /-- Nor the wallet's balance. -/ theorem within_balance (n left bal alw f : Nat) : moved n (avail left bal alw) f + fee (moved n (avail left bal alw) f) f ≤ bal := Nat.le_trans (moved_and_fee_within_avail n _ f) (Nat.le_trans (Nat.min_le_right _ _) (Nat.min_le_left _ _)) /-- Nor what the wallet allowed. -/ theorem within_allowance (n left bal alw f : Nat) : moved n (avail left bal alw) f + fee (moved n (avail left bal alw) f) f ≤ alw := Nat.le_trans (moved_and_fee_within_avail n _ f) (Nat.le_trans (Nat.min_le_right _ _) (Nat.min_le_right _ _)) /-- The fee is never more than its share of what moved. -/ theorem fee_within_its_share (m f : Nat) : fee m f * 10000 ≤ m * f := Nat.div_mul_le_self _ _ /-- With no fee, everything available may move. -/ theorem no_fee_moves_all (a : Nat) : most a 0 = a := by unfold most simp /-- Past the line the debt is over the room, so the fix is the Guardian's repayment and not nothing. -/ theorem past_is_over_the_room (d L F : Nat) (h : past d L F) : room L F < d := by obtain ⟨hd, hp⟩ := h unfold room omega /-- A RESCUE NOT CUT SHORT BRINGS THE LOAN BACK INSIDE THE LINE, a tenth to spare: what is left owed is at most the room. -/ theorem full_rescue_leaves_the_room (d L F : Nat) (h : past d L F) : d - need d L F ≤ room L F := by have hr := past_is_over_the_room d L F h unfold need ceilCent split <;> omega /-- So after it the loan reads safe against the person's line: not at the line, not close to a sale, not past the limit. -/ theorem full_rescue_is_safe (d L F : Nat) (h : past d L F) : level (d - need d L F) L F = 0 := by have h1 := full_rescue_leaves_the_room d L F h have h2 : (d - need d L F) * 1000 ≤ L * (100 - F) * 9 := by unfold room at h1; omega have hmin : L * (100 - F) ≤ L * (100 - min F 10) := Nat.mul_le_mul_left L (by omega) have hle : L * (100 - F) ≤ L * 100 := Nat.mul_le_mul_left L (by omega) unfold level split · rfl · split · omega · split · omega · split · omega · rfl /-- A repayment that moves anything leaves less owed. -/ theorem a_repayment_lowers_the_debt (d m : Nat) (h0 : 0 < m) (hm : m ≤ d) : d - m < d := by omega /-- Collateral added never lowers the venue's limit, so adding is never less safe. -/ theorem adding_never_lowers_the_limit (c c' p l : Nat) (h : c ≤ c') : limit c p l ≤ limit c' p l := by unfold limit apply Nat.div_le_div_right apply Nat.mul_le_mul_right apply Nat.div_le_div_right exact Nat.mul_le_mul_right p h /-- A worked example, decided: four tokens at 164 dollars with a 62.5 percent limit carry 410 dollars; 350 owed on them is past a 25 percent line; the rescue repays 73.25 and the loan reads safe; with a 1 percent fee and only 50 dollars available it moves 49.504950 and a fee of 0.495049, together under the 50. -/ theorem the_worked_example : limit 4000000000000000000 164000000000000000000000000 625000000000000000 = 410000000 ∧ need 350000000 410000000 25 = 73250000 ∧ level (350000000 - 73250000) 410000000 25 = 0 ∧ moved 73250000 50000000 100 = 49504950 ∧ fee 49504950 100 = 495049 ∧ 49504950 + 495049 ≤ 50000000 := by decide end GuardianAct #print axioms GuardianAct.inside_is_never_acted_on #print axioms GuardianAct.moved_le_need #print axioms GuardianAct.need_le_debt #print axioms GuardianAct.moved_le_debt #print axioms GuardianAct.moved_and_fee_within_avail #print axioms GuardianAct.within_budget #print axioms GuardianAct.within_balance #print axioms GuardianAct.within_allowance #print axioms GuardianAct.fee_within_its_share #print axioms GuardianAct.no_fee_moves_all #print axioms GuardianAct.past_is_over_the_room #print axioms GuardianAct.full_rescue_leaves_the_room #print axioms GuardianAct.full_rescue_is_safe #print axioms GuardianAct.a_repayment_lowers_the_debt #print axioms GuardianAct.adding_never_lowers_the_limit #print axioms GuardianAct.the_worked_example