How can I apply rewrite -> targetting only a sub-expression? For example, consider this theorem:
Parameter add : nat -> nat -> nat.
Axiom comm : forall a b, add a b = add b a.
Theorem t1 : forall a b : nat,
(add (add a b) (add a (add a b))) =
(add (add a b) (add a (add b a))).
Intuitively, it requires commuting only one (add a b) sub-expression, but if I do rewrite -> (comm a b), it rewrites all the occurrences. How can I target a particular sub-expression?
You can target a specific occurrence with the
rewritetactic using the suffixat N. Occurrences are numbered from 1 in left-to-right order. You can rewrite multiple occurrencess by separating their indices with spaces. You needRequire Import Setoid. Theatsuffix is also available with some other tactics that target occurrences of a term, including many tactics that perform conversions (change,unfold,fold, etc.),set,destruct, etc.There are other possible approaches, especially if all you need is to apply equalities. The
congruencetactic can find what to rewrite and apply symmetry and transitivity on its own, but you need to prime it by adding all equivalences to the context (in the form of universally-quantified equalities), it won't query hint databases.To get more automation,
Hint Rewritecreates a database of theorems which the tacticautorewritewill try applying. For more advanced automation, look up generalized rewriting with setoids, which I'm not sufficiently familiar with to expound on.