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.

def Nat.minus (m : ) (n : ) (hyp : n m) :

Subtract m and n in the presence of a proof that n ≤ m.

Instances For

    An example using minus (using decide for the proof).

    #eval minus 5 3 (by decide) -- 2
    theorem Nat.minus_add_cancel (m : ) (n : ) (hyp : n m) :
    Nat.minus m n hyp + n = m

    Subtraction (when valid) and addition are inverses

    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.

    #check Nat.sub_add_cancel -- Nat.sub_add_cancel {n m : ℕ} (h : m ≤ n) : n - m + m = n