-1

The following is from a homework that I already did, and did wrong. I fail to see why the solution is sufficient. (After one week of reading and googling I turn to asking.)

The example is similar to an example used in the Hutton book on Haskell.

Base case:

add Zero m
=   { applying add }
m
=   { property of add }
add m Zero

Inductive case:

add (Succ n) m
=   { applying add }
Succ (add n m)
=   { induction hypothesis }
Succ (add m n)
=   { property of add }
add m (Succ n)



-- Hypothesis:
add n m = add m n

-- Method:
by induction on n

-- Using:
add n (Succ m) = Succ (add n m)
add n Zero = n

data Nat = Zero
         | Succ Nat


add :: Nat -> Nat -> Nat
add Zero m = m
add (Succ n) m = Succ (add n m) 

What I fail to see is why the induction case is allowed to stop where it stops. It uses the hypothesis, and then un-applies add to generate something that is the version of the hypothesis of the induction case.

add n m = add m n -- hypothesis
add m (Succ n)  -- hypthesis applied to the induction case add (Succ n) m

In other words, another version of the hypothesis was generated by already assuming that the hypothesis holds.

That is enough? Can I always treat the hypothesis as valid when I find one single case in which it works? And for the inductive case it is always sufficient if I can generate a version of the hypothesis applied to the inductive case by assuming that the hypothesis itself is already proven?

I have troubles to transfer induction from numbers to function. Please don't let me die stupid. Thanks.

enahel
  • 355
  • 3
  • 15

3 Answers3

3

That is enough? Can I always treat the hypothesis as valid when I find one single case in which it works?

No. You need to:

  1. Show it works for all base (non-recursive) cases. Here you have only one: Zero. So you need to show

add Zero m = add m Zero holds.

  1. Show that for all recursive cases, assuming the hypothesis for arguments is sufficient to show it for the constructed result. Again, here the only recursive case is Succ; so you need to show that

given add n m = add m n, add (Succ n) m = add m (Succ n) holds.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
2

Maybe it helps if you spell out the problem more formally, including forall quantifiers:

forall (n m : Nat), add n m = add m n

Induction over n

Apply the induction principle:

forall m, add 0 m = add m 0 
          -> (forall n m, add n m = add m n -> add (S n) m = add m (S n)) 
          -> forall n m, add n m = add m n

Now we need to prove the base case add Z m = add m Z and the inductive case forall n m, add n m = add m n -> add (S n) m = add m (S n)

Base case: forall m, add Z m = add m Z

Let m be any Nat (this is called introduction).

  • Apply add to LHS and you get m = add m Z.
  • Apply forall m, add m Z = m (You can prove this if you want) to rewrite RHS and you get m = m.

This holds by reflexivity of equality.

Inductive case forall n m, add n m = add m n -> add (S n) m = add m (S n)

Let n, m be any Nat and introduce the hypothesis add n m = add m n (i.e. assume that it holds)

Goal: add (S n) m = add m (S n)

  • Apply add to LHS: S (add n m) = add m (S n)
  • Rewrite using hypothesis: S (add m n) = add m (S n)
  • Apply forall x y, S (add x y) = add (S x) y (again, you can prove this if you want to) to LHS to get add m (S n) = add m (S n).

This holds by reflexivity.

And we're done.

-1

Try an example.

3 = 1 + 1 + 1 + 0

so in Nat, 3 is represented as Succ (Succ (Succ Zero)). Now for any m :: Nat, (consider the two cases: Zero and Succ (Succ (... (Succ Zero) ...))

we have

add (Succ (Succ (Succ Zero))) m = 
Succ (add (Succ (Succ Zero)) m) = -- we took 1 from 3 and added it to the overall answer
Succ (Succ (add (Succ Zero) m)) = -- ditto
Succ (Succ (Succ (add Zero m))) = -- ditto
Succ (Succ (Succ m)) -- by applying the base case for `add`

So we see the result is "whatever m was (0 or otherwise), plus three more", or m+3 as you would expect.

crockeea
  • 21,651
  • 10
  • 48
  • 101