Zulip Chat Archive

Stream: metaprogramming / tactics

Topic: Applying commutativity in "normal form" order


Geoffrey Irving (May 15 2025 at 12:40):

Say I have a term like a ∪ b ∪ c ∪ d ∪ a. I'd like to be able to automatically reassociate and commutate things so that the two as end up next to each other, so that I can then use a ∪ a = a. Is there a way to use simp, union_comm, and union_assoc to do this kind of sorting? I don't care what the order of the terms is, as long as there is some reasonable total order.

Eric Wieser (May 15 2025 at 12:43):

You'll need a simproc here I think

Eric Wieser (May 15 2025 at 12:44):

Where you check if Expr.lt says one is ordered before the other, and apply the comm lemma otherwise

Henrik Böving (May 15 2025 at 12:56):

You can use ac_nf for this assuming your operations implement the associativity and commutativity typeclaaa

Geoffrey Irving (May 15 2025 at 13:03):

Oh, neat! ac_nf is great. And then docs#union_right_idem can finish things off.

Robin Arnez (May 15 2025 at 14:58):

Oh it should do that itself using Std.IdempotentOp but it seems like that instance is missing...

Robin Arnez (May 15 2025 at 15:05):

Huh what

import Mathlib.Data.Set.Basic

instance : Std.IdempotentOp (Union.union : Set α  _  _) := Set.union_self

def test (a b c d : Set Nat) : a  b  c  d  a = a  b  c  d := by
  ac_nf
  ac_nf

Shouldn't ac_nf be an idempotent op?

Bhavik Mehta (May 16 2025 at 16:38):

It looks like the first one isn't changing the rhs, which is surprising to me


Last updated: Dec 20 2025 at 21:32 UTC