coq - How to apply rewrite inside a specific subexpression? -
i'm using online book "software foundations" learn coq.
in second chapter, asked prove "plus_assoc" theorem:
theorem plus_assoc : forall n m p : nat, n + (m + p) = (n + m) + p.
i make use of 2 proven theorems:
theorem plus_comm : forall n m : nat, n + m = m + n. theorem plus_n_sm : forall n m : nat, s (n + m) = n + (s m).
i prove plus_assoc theorem using induction on n:
proof. intros n m p. induction n [ | n' ]. reflexivity. rewrite plus_comm. rewrite <- plus_n_sm. rewrite plus_comm. rewrite ihn'. symmetry. rewrite plus_comm.
at point, context (*) is:
1 subgoals case := "n = s n'" : string.string n' : nat m : nat p : nat ihn' : n' + (m + p) = n' + m + p ______________________________________(1/1) p + (s n' + m) = s (n' + m + p)
i use plus_comm get
p + (m + s n') = s (n' + m + p)
then plus_n_sm
p + s (m + n') = s (n' + m + p)
then again plus_n_sm
s (p + (m + n')) = s (n' + m + p)
and finish proof using plus_comm twice reflexivity
s (p + (n' + m)) = s (n' + m + p) s (n' + m + p) = s (n' + m + p)
the small question don't know how apply plus_comm (s n' + m).
the great question is: why issuing
apply plus_comm.
finishes proof instantly (in given context (*)) ?
thank in advance clarification !
fabian pijcke
you can apply plus_comm (s n' + m) instanciating (s n') , m.
check plus_comm. check plus_comm (s n'). check plus_comm (s n') m. rewrite (plus_comm (s n') m). rewrite <- plus_n_sm. rewrite <- plus_n_sm. rewrite (plus_comm m n'). rewrite plus_comm. reflexivity. qed.
i think using require import coq.setoids.setoid.
, using rewrite plus_comm @ 2.
supposed have same effect, doesn't work.
the reason apply plus_comm
finishes goal because apply
performs unification modulo conversion. say, p + (s n' + m) = s (n' + m + p)
convertible p + (s n' + m) = s n' + m + p
, , p + (s n' + m) = s n' + m + p
unifiable ?1 + ?2 = ?2 + ?1
.
in fact, if perform reduction using simpl
tactic proof becomes shorter.
theorem plus_assoc : forall n m p : nat, n + (m + p) = (n + m) + p. proof. induction n. reflexivity. intros. simpl. apply f_equal. apply ihn. qed.
Comments
Post a Comment