3

Suppose I've a sum like a + (b + (c + d)), which I wish to transform into a + b + c + d to apply a lemma.

Doing this manually with Nat.add_assoc is extremly tedious. Is there a smarter way?

Anton Trunov
  • 15,074
  • 2
  • 23
  • 43
Shuzheng
  • 11,288
  • 20
  • 88
  • 186

2 Answers2

4

The 'easy but not nice' way I would use is replace (a + (b + (c + d)))) with (a + b + c + d) by now omega

Vinz
  • 5,997
  • 1
  • 31
  • 52
  • 1
    Why is it not nice? Additionally, you can also do `by auto with *`. – Tiago Cogumbreiro Nov 18 '16 at 19:47
  • 1
    If you ever looked at the output term created by `omega`, you'll understand. Most of the time it is overly complicated, and a small proof using rewrite will be way smaller. But that's a matter of personal opinion ;) – Vinz Nov 19 '16 at 09:05
  • `by omega` would work as well; and `by ring` also solves the problem. The former needs `Omega` module to be imported; the latter -- `Arith`. – Anton Trunov Nov 19 '16 at 13:20
  • @Vinz why is the output term important? Is the problem of `omega` that it _might_ slow down the compilation/check stage? – Tiago Cogumbreiro Nov 19 '16 at 20:27
  • Yep, that's the idea. In this particular case I don't think it will really matter. But in bigger development, this might change the execution time of a `Qed` significantly. – Vinz Nov 20 '16 at 08:14
4

You can use the repeat tactical, which repeats some tactic until it cannot be applied anymore:

repeat rewrite Nat.add_assoc.

or a more concise version:

rewrite !Nat.add_assoc.

It works just the same as the variant with repeat.

The downside of this approach is that it rewrites everywhere in the goal. So, you might want to select some part of your formula to do the rewrites on it only.

Anton Trunov
  • 15,074
  • 2
  • 23
  • 43