Im trying to prove commutativity in Isabelle/HOL for a self-defined add
function. I managed to prove associativity but I'm stuck on this.
The definition of add
:
fun add :: "nat ⇒ nat ⇒ nat" where
"add 0 n = n" |
"add (Suc m) n = Suc(add m n)"
The proof of associativity:
lemma add_Associative: "add(add k m) z = add k (add m z)"
apply(induction k)
apply(auto)
done
The proof of commutativity:
theorem add_commutativity: "add k m = add m k"
apply(induction k)
apply(induction m)
apply(auto)
I get the following goals:
goal (3 subgoals):
1. add 0 0 = add 0 0
2. ⋀m. add 0 m = add m 0 ⟹
add 0 (Suc m) = add (Suc m) 0
3. ⋀k. add k m = add m k ⟹
add (Suc k) m = add m (Suc k)
After applying auto I'm left with just subgoal 3:
3. ⋀k. add k m = add m k ⟹
add (Suc k) m = add m (Suc k)
EDIT: Im not so much looking for an answer, as a push in the right direction. These are exercises from a book called Concrete Sementics.
I answer the RainyCats's question in a comment of the chris's answer: "How Isabelle proves". I give a detailed proof of the associativity of
add
both manually and step by step in Isar.Manually for associativity, by induction on k:
for
k
=0
we have to proveadd (add 0 m) z = add 0 (add m z)
.We rewrite with definition of
add
:add (add 0 m) z
⇢add m z
add 0 (add m z)
⇢add m z
Then the goal is proven by reflexivity of
=
.for
k
=Suc k'
add (add k' m) z = add k' (add m z)
.add (add (Suc k') m) z = add (Suc k') (add m z)
.We rewrite with definition of
add
:add (add (Suc k') m) z
⇢add (Suc (add k' m)) z
⇢Suc (add (add k' m) z)
add (Suc k') (add m z)
⇢Suc (add k' (add m z))
By induction hypothesis:
Suc (add (add k' m) z)
⇢Suc (add k' (add m z))
And then then goal is proven by reflexivity of
=
.In Isar with this level of detail, this would give:
where I have made the smallest steps possible and where all the
by ...
could be replaced byby simp
.I would suggest to make the proof as modular as possible (i.e., prove intermediate lemmas that will later help to solve the commutativity proof). To this end it is often more informative to meditate on the subgoals introduced by
induct
, before applyng full automation (like yourapply (auto)
).At this point the subgoals are:
Lets look at them separately.
Using the definition of
add
we will only be able to simplify the left-hand side, i.e.,add 0 m = m
. Then the question remains how to proveadd m 0 = m
. You did this as part of your main proof. I would argue that it increases readability to proof the following separate lemmaand add it to the automated tools (like
simp
andauto
) using[simp]
. At this point the first subgoal can be solved bysimp
and only the second subgoal remains.After applying the definition of
add
as well as the induction hypothesis (add k m = add m k
) we will have to proveSuc (add m k) = add m (Suc k)
. This looks very similar to the second equation of the original definition ofadd
, only with swapped arguments. (From that perspective, what we had to prove for the first subgoal corresponded to the first equation in the definition ofadd
with swapped arguments.) Now, I would suggest to try to prove the general lemmaadd m (Suc n) = Suc (add m n)
in order to proceed.