Built with doc-gen4, running Lean4. Bubbles () indicate interactive fragments: hover for details, tap to reveal contents. Use Ctrl+↑Ctrl+↓to navigate, Ctrl+🖱️to focus. On Mac, use Cmdinstead of Ctrl.
import Mathlib

/-!
# Subtraction: example of intertwined proofs and definitions. 

The difference of natural numbers is not a natural number. We see three ways of overcoming this problem, which illustrate various important concepts in Lean. 

The first two are in an earlier module while the third is in this one. 

Here we subtract natural numbers only when given a proof that the second is less than or equal to the first. Note that as any two terms whose type is the same proposition are equal by definition, the value will not depend on the specific proof.
-/

namespace Nat

/-- Subtract `m` and `n` in the presence of a proof that `n ≤ m`. -/
def 
minus: (m n : ) → n m
minus
(
m:
m
n:
n
:
: Type
)(
hyp: n m
hyp
:
n:
n
m:
m
) :
: Type
:= match
m:
m
,
n:
n
,
hyp: n m
hyp
with |
m:
m
,
0:
0
, _ =>
m:
m
|
0:
0
, _ +1,
pf: n✝ + 1 0
pf
=> nomatch
pf: n✝ + 1 0
pf
|
m:
m
+ 1,
n:
n
+ 1 ,
pf: n + 1 m + 1
pf
=>
minus: (m n : ) → n m
minus
m:
m
n:
n
(
le_of_succ_le_succ: ∀ {n m : }, succ n succ mn m
le_of_succ_le_succ
pf: n + 1 m + 1
pf
) /-! An example using `minus` (using `decide` for the proof). ```lean #eval minus 5 3 (by decide) -- 2 ``` -/
2
minus: (m n : ) → n m
minus
5: ?m.1097
5
3: ?m.1107
3
(

Goals accomplished! 🐙

3 5

Goals accomplished! 🐙
) -- 2 /-- Subtraction (when valid) and addition are inverses -/ theorem
minus_add_cancel: ∀ (m n : ) (hyp : n m), minus m n hyp + n = m
minus_add_cancel
(
m:
m
n:
n
:
: Type
)(
hyp: n m
hyp
:
n:
n
m:
m
) :
minus: (m n : ) → n m
minus
m:
m
n:
n
hyp: n m
hyp
+
n:
n
=
m:
m
:=

Goals accomplished! 🐙
m, n:

hyp: n m


minus m n hyp + n = m
m, n:

hyp: n m


minus m n hyp + n = m
m✝, n:

hyp: n m✝

m:

x✝: 0 m


minus m 0 x✝ + 0 = m
m, n:

hyp: n m


minus m n hyp + n = m

Goals accomplished! 🐙
m, n:

hyp: n m


minus m n hyp + n = m
m, n:

hyp: n m

n✝:

pf: n✝ + 1 0


minus 0 (n✝ + 1) pf + (n✝ + 1) = 0
m, n:

hyp: n m


minus m n hyp + n = m

Goals accomplished! 🐙
m, n:

hyp: n m


minus m n hyp + n = m
m✝, n✝:

hyp: n✝ m✝

m, n:

pf: n + 1 m + 1


minus (m + 1) (n + 1) pf + (n + 1) = m + 1
m, n:

hyp: n m


minus m n hyp + n = m
m✝, n✝:

hyp: n✝ m✝

m, n:

pf: n + 1 m + 1


minus m n (_ : n m) + (n + 1) = m + 1
m✝, n✝:

hyp: n✝ m✝

m, n:

pf: n + 1 m + 1


minus (m + 1) (n + 1) pf + (n + 1) = m + 1
m✝, n✝:

hyp: n✝ m✝

m, n:

pf: n + 1 m + 1


minus m n (_ : n m) + (n + 1) = m + 1
m✝, n✝:

hyp: n✝ m✝

m, n:

pf: n + 1 m + 1


minus m n (_ : n m) + n + 1 = m + 1
m✝, n✝:

hyp: n✝ m✝

m, n:

pf: n + 1 m + 1


minus m n (_ : n m) + n + 1 = m + 1
m✝, n✝:

hyp: n✝ m✝

m, n:

pf: n + 1 m + 1


minus (m + 1) (n + 1) pf + (n + 1) = m + 1

Goals accomplished! 🐙
Nat.sub_add_cancel {n m : ℕ} (h : m ≤ n) : n - m + m = n
Nat.sub_add_cancel: ∀ {n m : }, m nn - m + m = n
Nat.sub_add_cancel
-- Nat.sub_add_cancel {n m : ℕ} (h : m ≤ n) : n - m + m = n /-! Note that in `Lean 4` there is a result `Nat.sub_add_cancel` which is analogous to `minus_add_cancel` above. However the statement of the result requires that the subtraction is valid. ```lean #check Nat.sub_add_cancel -- Nat.sub_add_cancel {n m : ℕ} (h : m ≤ n) : n - m + m = n ``` -/ end Nat