Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - feat: @[eqns] attribute to override equation lemmas #3024

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Mathlib.lean
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,7 @@ import Mathlib.Tactic.Core
import Mathlib.Tactic.Elementwise
import Mathlib.Tactic.Existsi
import Mathlib.Tactic.Expect
import Mathlib.Tactic.Eqns
import Mathlib.Tactic.FailIfNoProgress
import Mathlib.Tactic.FieldSimp
import Mathlib.Tactic.FinCases
Expand Down
45 changes: 45 additions & 0 deletions Mathlib/Tactic/Eqns.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/-
Copyright (c) 2023 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser
-/
import Lean.Meta.Eqns
import Mathlib.Lean.Expr
import Std.Lean.NameMapAttribute

/-! # The `@[eqns]` attribute

This file provide the `eqns` attribute as a way of overring the default equation lemmas. For
Parcly-Taxel marked this conversation as resolved.
Show resolved Hide resolved
example

```lean4
def transpose {m n} (A : m → n → ℕ) : n → m → ℕ
| i, j => A j i

theorem transpose_apply {m n} (A : m → n → ℕ) (i j) :
transpose A i j = A j i := rfl

attribute [eqns transpose_apply] transpose

theorem transpose_const {m n} (c : ℕ) :
transpose (fun (i : m) (j : n) => c) = fun j i => c :=
by
eric-wieser marked this conversation as resolved.
Show resolved Hide resolved
funext i j -- the rw below does not work without this line
rw [transpose]
```
-/
open Lean

syntax (name := eqns) "eqns" ident* : attr

initialize notationClassAttr : NameMapExtension (Array Name) ←
registerNameMapAttribute {
name := `eqns
descr := "Overrides the equation lemmas for a declation to the provided list"
add := fun
| _, `(attr| eqns $[$names]*) =>
pure <| names.map (fun n => n.getId)
| _, _ => Lean.Elab.throwUnsupportedSyntax }

initialize Lean.Meta.registerGetEqnsFn (fun name => do
pure (notationClassAttr.find? (← getEnv) name))
17 changes: 17 additions & 0 deletions test/eqns.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Mathlib.Tactic.Eqns

def transpose {m n} (A : m → n → ℕ) : n → m → ℕ
| i, j => A j i

theorem transpose_apply {m n} (A : m → n → ℕ) (i j) :
transpose A i j = A j i := rfl

attribute [eqns transpose_apply] transpose

theorem transpose_const {m n} (c : ℕ) :
transpose (fun (_i : m) (_j : n) => c) = fun _j _i => c :=
by
fail_if_success {rw [transpose]}
fail_if_success {simp [transpose]}
funext i j -- the rw below does not work without this line
rw [transpose]