pith. machine review for the scientific record. sign in

IndisputableMonolith.RRF.Foundation.Ledger

IndisputableMonolith/RRF/Foundation/Ledger.lean · 218 lines · 24 declarations

show as:
view math explainer →

open module explainer GitHub source

Explainer status: ready · generated 2026-05-13 03:33:40.379664+00:00

   1import Mathlib.Data.Int.Basic
   2import Mathlib.Data.List.Basic
   3import Mathlib.Tactic.Ring
   4import Mathlib.Tactic.Linarith
   5import Mathlib.Data.Real.Basic
   6
   7/-!
   8# RRF Foundation: Ledger Algebra
   9
  10The ledger is the core accounting structure that enforces conservation.
  11
  12## Double-Entry Bookkeeping
  13
  14Every transaction has two sides: debit and credit.
  15The fundamental constraint: debit + credit = 0.
  16
  17## Conservation Laws
  18
  19Each conservation law (energy, charge, momentum, etc.) is an instance
  20of ledger closure.
  21-/
  22
  23namespace IndisputableMonolith
  24namespace RRF.Foundation
  25
  26/-! ## Basic Transaction -/
  27
  28/-- A single transaction: a debit and credit that must balance. -/
  29structure Transaction where
  30  /-- The debit amount (positive = outflow). -/
  31  debit : ℤ
  32  /-- The credit amount (positive = inflow). -/
  33  credit : ℤ
  34  /-- The balance constraint: debit + credit = 0. -/
  35  balanced : debit + credit = 0
  36
  37namespace Transaction
  38
  39/-- Create a balanced transaction from a single amount. -/
  40def fromAmount (amount : ℤ) : Transaction := {
  41  debit := amount,
  42  credit := -amount,
  43  balanced := by omega
  44}
  45
  46/-- The trivial (zero) transaction. -/
  47def zero : Transaction := fromAmount 0
  48
  49/-- Transactions form an additive structure. -/
  50def add (t₁ t₂ : Transaction) : Transaction := {
  51  debit := t₁.debit + t₂.debit,
  52  credit := t₁.credit + t₂.credit,
  53  balanced := by
  54    have h₁ := t₁.balanced
  55    have h₂ := t₂.balanced
  56    omega
  57}
  58
  59theorem add_balanced (t₁ t₂ : Transaction) :
  60    (add t₁ t₂).debit + (add t₁ t₂).credit = 0 := (add t₁ t₂).balanced
  61
  62end Transaction
  63
  64/-! ## Full Ledger -/
  65
  66/-- A ledger: a sequence of transactions that globally balance. -/
  67structure Ledger where
  68  /-- The transactions in the ledger. -/
  69  transactions : List Transaction
  70  /-- Total debit. -/
  71  total_debit : ℤ := (transactions.map (·.debit)).sum
  72  /-- Total credit. -/
  73  total_credit : ℤ := (transactions.map (·.credit)).sum
  74  /-- Global balance: total debit + total credit = 0. -/
  75  global_balance : total_debit + total_credit = 0
  76
  77namespace Ledger
  78
  79/-- The empty ledger. -/
  80def empty : Ledger := {
  81  transactions := [],
  82  total_debit := 0,
  83  total_credit := 0,
  84  global_balance := by omega
  85}
  86
  87/-- A singleton ledger. -/
  88def singleton (t : Transaction) : Ledger := {
  89  transactions := [t],
  90  total_debit := t.debit,
  91  total_credit := t.credit,
  92  global_balance := t.balanced
  93}
  94
  95/-- Append a transaction to a ledger. -/
  96def append (L : Ledger) (t : Transaction) : Ledger := {
  97  transactions := L.transactions ++ [t],
  98  total_debit := L.total_debit + t.debit,
  99  total_credit := L.total_credit + t.credit,
 100  global_balance := by
 101    have hL := L.global_balance
 102    have ht := t.balanced
 103    omega
 104}
 105
 106/-- The net balance of a ledger (should always be 0). -/
 107def net (L : Ledger) : ℤ := L.total_debit + L.total_credit
 108
 109/-- Ledger net is always zero. -/
 110theorem net_zero (L : Ledger) : L.net = 0 := L.global_balance
 111
 112/-- Concatenate two ledgers. -/
 113def concat (L₁ L₂ : Ledger) : Ledger := {
 114  transactions := L₁.transactions ++ L₂.transactions,
 115  total_debit := L₁.total_debit + L₂.total_debit,
 116  total_credit := L₁.total_credit + L₂.total_credit,
 117  global_balance := by
 118    have h₁ := L₁.global_balance
 119    have h₂ := L₂.global_balance
 120    omega
 121}
 122
 123theorem concat_preserves_balance (L₁ L₂ : Ledger) :
 124    (concat L₁ L₂).net = 0 := (concat L₁ L₂).global_balance
 125
 126end Ledger
 127
 128/-! ## Conservation Laws as Ledger Instances -/
 129
 130/-- A conservation law: a named charge assignment with closure. -/
 131structure ConservationLaw (α : Type*) where
 132  /-- Name of the conserved quantity. -/
 133  name : String
 134  /-- Charge assignment to elements. -/
 135  charge : α → ℤ
 136  /-- Any interaction (list of elements) has net zero charge. -/
 137  closed : ∀ (interaction : List α), (interaction.map charge).sum = 0
 138
 139namespace ConservationLaw
 140
 141/-- Electric charge conservation (trivial example). -/
 142def trivial : ConservationLaw Unit := {
 143  name := "trivial",
 144  charge := fun _ => 0,
 145  closed := fun l => by
 146    have h : l.map (fun _ => (0 : ℤ)) = List.replicate l.length 0 := by
 147      induction l with
 148      | nil => rfl
 149      | cons _ _ ih => simp [ih, List.replicate_succ]
 150    rw [h]
 151    clear h
 152    induction l.length with
 153    | zero => rfl
 154    | succ n ih => simp [List.replicate_succ, ih]
 155}
 156
 157/-- Conservation law from particle-antiparticle pairs. -/
 158inductive ParticlePair | particle | antiparticle
 159
 160def particleCharge : ParticlePair → ℤ
 161  | .particle => 1
 162  | .antiparticle => -1
 163
 164end ConservationLaw
 165
 166/-! ## Ledger Density and Mass -/
 167
 168/-- Ledger density at a point (abstract). -/
 169structure LedgerDensity where
 170  /-- Number of transactions per unit volume. -/
 171  density : ℝ
 172  /-- Density is non-negative. -/
 173  nonneg : 0 ≤ density
 174
 175/-- Mass from ledger density (the claim). -/
 176noncomputable def massFromLedger (L : LedgerDensity) (volume : ℝ) : ℝ :=
 177  L.density * volume
 178
 179/-- Curvature from ledger density (the gravity mapping). -/
 180noncomputable def curvatureFromLedger (L : LedgerDensity) : ℝ :=
 181  L.density  -- Placeholder: actual mapping is more complex
 182
 183/-! ## Double-Entry Principle -/
 184
 185/-- The double-entry principle: every credit has a matching debit. -/
 186structure DoubleEntry where
 187  /-- For every credit, there's a corresponding debit. -/
 188  credit_has_debit : ∀ (c : ℤ), c < 0 → ∃ (d : ℤ), d = -c
 189  /-- For every debit, there's a corresponding credit. -/
 190  debit_has_credit : ∀ (d : ℤ), d > 0 → ∃ (c : ℤ), c = -d
 191
 192/-- Double-entry is always satisfied (by construction). -/
 193theorem double_entry_exists : DoubleEntry := {
 194  credit_has_debit := fun c _ => ⟨-c, rfl⟩,
 195  debit_has_credit := fun d _ => ⟨-d, rfl⟩
 196}
 197
 198/-! ## Ledger Algebra Summary -/
 199
 200/-- The complete ledger algebra bundle. -/
 201structure LedgerAlgebra where
 202  /-- The transaction type. -/
 203  transaction : Type := Transaction
 204  /-- The ledger type. -/
 205  ledger : Type := Ledger
 206  /-- Transactions are balanced. -/
 207  transactions_balanced : ∀ t : Transaction, t.debit + t.credit = 0 := fun t => t.balanced
 208  /-- Ledgers are balanced. -/
 209  ledgers_balanced : ∀ L : Ledger, L.net = 0 := fun L => L.global_balance
 210  /-- Double-entry holds. -/
 211  double_entry : DoubleEntry := double_entry_exists
 212
 213/-- The ledger algebra is consistent. -/
 214theorem ledger_algebra_consistent : Nonempty LedgerAlgebra := ⟨{}⟩
 215
 216end RRF.Foundation
 217end IndisputableMonolith
 218

source mirrored from github.com/jonwashburn/shape-of-logic