-- Bank fact: guardian-a-fix-brings-the-loan-back-inside-the-line-and-never-asks-more-than-the-debt -- Primary implementation: agent/src/guardianmath.js (limitWei, levelOf, fallBps, targetOf, fixOf), used by -- agent/src/guardian.js (standingOf, stepOf, fixTx). -- Machine checked with Lean 4.22.0, core only, no Mathlib (bank/proofs/lean-toolchain). -- Check it: lean Guardian.lean (the #print axioms lines list only Lean's standard axioms) /- THE GUARDIAN'S FIX: the way back it hands a person is enough, and never more than they owe. Stated exactly and proved. The Guardian watches a loan against a Stock Token and, when the price can fall less than the person's line before the venue sells the collateral, hands them the dollars to repay that bring the loan back inside the line. A person told "repay this and you are back inside" who is still past the line has been lied to by arithmetic, so the figure is sized in the venue's own integers, each division floored where the venue floors it, and then rounded up to the next whole cent. Proved over that rule, for every debt, every limit and every line a person may hold: * the repayment is never more than the debt, * after it the debt is at or under the room the line leaves, a tenth inside the line, * so the loan then reads safe: not at the line, not close to a sale, not past the limit, * the figure is whole cents (or the whole debt), and the rounding adds less than one cent, * a loan at or past its line is never called safe, and one past the venue's limit is always the worst level, * the fall a loan is said to survive is never more than the fall it survives, and * more collateral never lowers the venue's limit, so collateral that carries a debt still carries it when more is added. -/ namespace Guardian /-- The venue's limit on a loan, in the loan token's smallest unit: collateral times price over 10^36, then times the liquidation limit over 10^18, each division floored, exactly as the venue computes what a position may carry. -/ def limit (c p l : Nat) : Nat := c * p / 1000000000000000000000000000000000000 * l / 1000000000000000000 /-- The room a line leaves: the debt a limit `L` may carry a tenth inside a line of `F` percent, floored. -/ def room (L F : Nat) : Nat := L * (100 - F) * 9 / 1000 /-- Up to the next whole cent: ten thousand of the loan token's smallest unit. -/ def ceilCent (n : Nat) : Nat := (n + 9999) / 10000 * 10000 /-- THE REPAYMENT: nothing when the debt is at or under the room; else the excess, up to the next whole cent, and the whole debt when that would pass it. -/ def repay (d L F : Nat) : Nat := if d ≤ room L F then 0 else if d ≤ ceilCent (d - room L F) then d else ceilCent (d - room L F) /-- Where a loan stands against a line of `F` percent: 0 safe, 1 at the line, 2 within ten percent of a sale (or at the line, when that is nearer), 3 past the venue's limit. Owing nothing is safe at any price. -/ 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 /-- The fall a loan still survives, in basis points of the price, floored. -/ def fallBps (d L : Nat) : Nat := if d = 0 then 10000 else if L ≤ d then 0 else (L - d) * 10000 / L /-- The repayment is never more than the debt. -/ theorem repay_le_debt (d L F : Nat) : repay d L F ≤ d := by unfold repay split · omega · split <;> omega /-- After the repayment the debt is at or under the room the line leaves. -/ theorem after_le_room (d L F : Nat) : d - repay d L F ≤ room L F := by unfold repay ceilCent split · omega · split <;> omega /-- So the debt left is at most nine tenths of the line's share of the limit. -/ theorem after_inside_target (d L F : Nat) : (d - repay d L F) * 1000 ≤ L * (100 - F) * 9 := by have h := after_le_room d L F unfold room at h omega /-- AFTER THE FIX THE LOAN READS SAFE: not at the line, not close to a sale, not past the limit, for every line. -/ theorem safe_after_repay (d L F : Nat) : level (d - repay d L F) L F = 0 := by have h := after_inside_target d L F 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 /-- The figure is the whole debt or whole cents. -/ theorem repay_is_whole_cents (d L F : Nat) : repay d L F = d ∨ repay d L F % 10000 = 0 := by unfold repay ceilCent split · omega · split · exact Or.inl rfl · exact Or.inr (by omega) /-- The rounding adds less than one cent: a cent less would not have been enough. -/ theorem repay_adds_less_than_a_cent (d L F : Nat) (h : room L F < d) : room L F + repay d L F < d + 10000 := by unfold repay ceilCent split · omega · split <;> omega /-- A loan at or past its line is never called safe. -/ theorem past_the_line_is_never_safe (d L F : Nat) (hd : d ≠ 0) (h : L * (100 - F) ≤ d * 100) : 1 ≤ level d L F := by unfold level repeat' split all_goals first | omega | contradiction /-- A loan past the venue's limit is the worst level, whatever the line. -/ theorem past_the_limit_is_a_sale (d L F : Nat) (h : L < d) : level d L F = 3 := by unfold level repeat' split all_goals first | rfl | omega | contradiction /-- The fall said is never past the whole price. -/ theorem fall_le_10000 (d L : Nat) : fallBps d L ≤ 10000 := by unfold fallBps split · omega · split · omega · apply Nat.div_le_of_le_mul have : L - d ≤ L := Nat.sub_le L d calc (L - d) * 10000 ≤ L * 10000 := Nat.mul_le_mul_right 10000 this _ = L * 10000 := rfl /-- The fall said is never more than the fall the loan survives: it is floored, never rounded up. -/ theorem fall_never_overstated (d L : Nat) (hd : d ≠ 0) : fallBps d L * L ≤ (L - d) * 10000 := by unfold fallBps split · contradiction · split · simp · exact Nat.div_mul_le_self _ _ /-- More collateral never lowers the venue's limit. -/ theorem limit_mono (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 larger limit never leaves less room. -/ theorem room_mono (L L' F : Nat) (h : L ≤ L') : room L F ≤ room L' F := by unfold room apply Nat.div_le_div_right apply Nat.mul_le_mul_right exact Nat.mul_le_mul_right (100 - F) h /-- Collateral that carries a debt inside the line still carries it when more is added. -/ theorem more_collateral_keeps_a_fit (d c c' p l F : Nat) (h : c ≤ c') (fits : d ≤ room (limit c p l) F) : d ≤ room (limit c' p l) F := Nat.le_trans fits (room_mono _ _ F (limit_mono c c' p l h)) /-- The desk's own worked example, decided: two tokens at 170 dollars with a 77 percent limit carry 261.80; 200 owed on them is at a 25 percent line; the fix is 23.29, and after it the loan reads safe. -/ theorem the_worked_example : limit 2000000000000000000 170000000000000000000000000 770000000000000000 = 261800000 ∧ level 200000000 261800000 25 = 1 ∧ repay 200000000 261800000 25 = 23290000 ∧ level (200000000 - 23290000) 261800000 25 = 0 ∧ fallBps 200000000 261800000 = 2360 := by decide end Guardian #print axioms Guardian.repay_le_debt #print axioms Guardian.after_le_room #print axioms Guardian.after_inside_target #print axioms Guardian.safe_after_repay #print axioms Guardian.repay_is_whole_cents #print axioms Guardian.repay_adds_less_than_a_cent #print axioms Guardian.past_the_line_is_never_safe #print axioms Guardian.past_the_limit_is_a_sale #print axioms Guardian.fall_le_10000 #print axioms Guardian.fall_never_overstated #print axioms Guardian.limit_mono #print axioms Guardian.room_mono #print axioms Guardian.more_collateral_keeps_a_fit #print axioms Guardian.the_worked_example