Zulip Chat Archive
Stream: Is there code for X?
Topic: Creating rw/simp sets "on the fly"
Miguel Marco (Oct 10 2025 at 17:18):
In certain contexts, it may be useful to simplify/rewrite using a specific set of rules. Is there a way to define such a set "on the fly"?
Something like
def my_rule_set : RuleSet := [mul_add, mul_sub, mul_neg]
example (a b c : Nat) : a * (b + c - a) = 0 := by
simp only [my_rule_set]
Aaron Liu (Oct 10 2025 at 17:20):
I don't see how this is "on the fly"
Miguel Marco (Oct 10 2025 at 17:21):
I mean you can do it in your own file, without having to go through the code of each lemma you want to use to mark them.
Aaron Liu (Oct 10 2025 at 17:23):
oh is that how it usually works (I don't really make simp sets myself so I wouldn't know)
Miguel Marco (Oct 10 2025 at 17:24):
I think so. Am I wrong?
Aaron Liu (Oct 10 2025 at 17:24):
what's the syntax to declare a simp set?
Aaron Liu (Oct 10 2025 at 17:27):
found it
Kenny Lau (Oct 10 2025 at 17:28):
@Miguel Marco What about this?
import Mathlib
local macro "my_rule_set" : tactic => `(tactic|
repeat first | rw [mul_add] | rw [mul_sub] | rw [mul_neg])
example (a b c : Int) : a * (b + c - a) = 0 := by
my_rule_set
(I changed Nat to Int because mul_sub doesn't apply for Nat)
Aaron Liu (Oct 10 2025 at 17:28):
rw isnt the same as simp
Miguel Marco (Oct 10 2025 at 17:29):
So, that is defining a new tactic, rather than a simpset, right?
Aaron Liu (Oct 10 2025 at 17:30):
this should definitely be possible to declare a simp extension with all its theorems at the declaring site
Kenny Lau (Oct 10 2025 at 17:31):
Miguel Marco said:
So, that is defining a new tactic, rather than a simpset, right?
yes
Kenny Lau (Oct 10 2025 at 17:31):
this happens in mathlib all the time
Aaron Liu (Oct 10 2025 at 17:40):
I think the simplest to do it (if you know metaprogramming) would be
- obtain a simp extension with docs#Lean.Meta.mkSimpExt (unfortunately this starts you out with an empty extension)
- add all the theorems with docs#Lean.Meta.addSimpTheorem
- register it to the simp extension map by using docs#ST.Ref.modify to docs#Std.HashMap.insert it into docs#Lean.Meta.simpExtensionMapRef
Robin Arnez (Oct 10 2025 at 17:47):
Well, the usual thing is
/-- My attribute doc-string -/
initialize mySimpExtension : SimpExtension ← registerSimpAttr `special_simps "Special simps"
and then use @[special_simps] or attribute [special_simps] thm1 thm2 thm3 for tagging theorems and finally use simp only [special_simps] (note that you can't use the attribute in the file with the initialize directly though but only in a file that imports it)
Kenny Lau (Oct 10 2025 at 17:48):
Robin Arnez said:
note that you can't use the attribute in the file with the
initializedirectly
yeah, I find this a very annoying "feature"
Kenny Lau (Oct 10 2025 at 17:49):
can I declare inside a file that I want Lean to treat the code afterwards as part of a new file?
Kim Morrison (Oct 14 2025 at 23:52):
No.
Kevin Buzzard (Oct 15 2025 at 18:12):
This is of course an unsurprising answer! But it is on the face of it a bit weird that you can't use the attributes in the file where you define them.
Last updated: Dec 20 2025 at 21:32 UTC