1-- Truncated subtraction on the natural numbers: sub(m, n) is m − n when2-- n ≤ m and zero otherwise3-- Part of Cumulus, the Grace17 mathematics library.4-- Copyright (c) 2026 Grant Passmore.5-- Released under the Apache License 2.0. See LICENSE in the repository root.6-- Authors: Grant Passmore78module Grace.Cumulus.Nat.Subtraction9import OrderLemmas1011-- Truncated subtraction: m - n is 0 whenever n exceeds m.12def sub (m n : Nat) →Nat13| m, zero => m14| m, succ(n) => predecessor(sub(m, n))15descends n1617notationinfixl60"-"=> sub forNat1819theorem sub.one (n : Nat)20shows n - 1= predecessor(n)21proof22simp [sub]2324theorem sub.zero_right (m : Nat)25shows m - 0= m26proof27simp [sub]2829theorem sub.succ_right (m n : Nat)30shows m - succ(n) = predecessor(m - n)31proof32simp [sub]3334theorem predecessor.zero35shows predecessor(0) =036proof37compute3839theorem predecessor.succ (n : Nat)40shows predecessor(succ(n)) = n41proof42simp [predecessor]4344theorem predecessor.closed (n : Nat)45showsNat(predecessor(n))46proof47induction n48| zero =>49rw predecessor.zero50typecheck51| succ(k) using ih =>52rw predecessor.succ53typecheck5455theorem sub.closed (m n : Nat)56showsNat(m - n)57proof58induction n59| zero =>60rw sub.zero_right61typecheck62| succ(k) using ih =>63rw sub.succ_right64exact predecessor.closed at sub(m, k) with ih6566theorem sub.zero_left (n : Nat)67shows0 - n =068proof69induction n70| zero =>compute71| succ(k) using ih =>72rw [sub.succ_right, ih]73exact predecessor.zero7475theorem sub.succ_succ (m n : Nat)76shows succ(m) - succ(n) = m - n77proof78induction n79| zero =>80rw [sub.succ_right, sub.zero_right, sub.zero_right]81exact predecessor.succ at m82| succ(k) using ih =>83rw [sub.succ_right, ih]84rw sub.succ_right85rfl8687theorem sub.add_right (m n : Nat)88shows (m + n) - n = m89proof90induction n91| zero =>92have shape : m +0= m byring93rw shape94exact sub.zero_right at m95| succ(k) using ih =>96have shape : m + succ(k) = succ(m + k) byring97rw [shape, sub.succ_succ]98exact ih99100-- Peeling one unit off the subtrahend below the minuend.101theorem sub.succ_of_Less (k : Nat)102shows∀ n, Nat(n) → k < n → n - k = succ(n - succ(k))103proof104induction k105| zero =>106intros n nNat below107obtain⟨d, dNat, shape⟩:= below108have form : succ(0) + d = succ(d) byring109rw form at shape110rw← shape111rw [sub.zero_right, sub.succ_succ, sub.zero_right]112rfl113| succ(j) using ih =>114intros n nNat below115obtain⟨d, dNat, shape⟩:= below116have form : succ(succ(j)) + d = succ(j + succ(d)) byring117rw form at shape118have innerNat : Nat(j + succ(d)) bytypecheck119have innerBelow : j < j + succ(d)120witness d121ring122have inner := ih with j + succ(d), innerNat, innerBelow123rw← shape124rw [sub.succ_succ, sub.succ_succ]125exact inner126