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

Equation lemmas are longer generated by a simple match #2042

Closed
1 task done
eric-wieser opened this issue Jan 18, 2023 · 1 comment · Fixed by #2783
Closed
1 task done

Equation lemmas are longer generated by a simple match #2042

eric-wieser opened this issue Jan 18, 2023 · 1 comment · Fixed by #2783
Labels
Mathlib4 high prio Mathlib4 high priority issue

Comments

@eric-wieser
Copy link
Contributor

eric-wieser commented Jan 18, 2023

Prerequisites

  • Put an X between the brackets on this line if you have done all of the following:
    • Checked that your issue isn't already filed.
    • Reduced the issue to a self-contained, reproducible test case.

Description

Compare this Lean 3 code

@[simp]
def foo : ℕ → ℕ
| a := 2 * a

example : foo = λ a, a + a :=
begin
  success_if_fail { simp },  -- only `foo x` is matched by the equation lemma
  funext,
  simp, -- now matches
  sorry
end

vs the same code in Lean 4

@[simp]
def foo : Nat → Nat
| a => 2 * a

example : foo = fun a => a + a :=
by
  simp -- unfolds foo into a lambda
  sorry

The Lean3 behavior was very useful in mathlib; for instance, in the definition of

/-- The transpose of a matrix. -/
def transpose (M : matrix m n α) : matrix n m α
| x y := M y x

it means that simp [transpose] only simplifies terms of the form transpose M i j and not transpose M.

Steps to Reproduce

  1. Run the code above in Lean 3 and Lean 4
  2. Observe the behavior difference

Expected behavior: Lean 4 behaves like Lean 3 and generates an equation lemma foo a = 2 * a

Actual behavior: Lean 4 generates an equation lemma foo = fun a => 2 * a

Reproduces how often: 100%

Versions

Lean 4.0.0-nightly-2023-01-16

Additional Information

Zulip thread, and an older Zulip message

@eric-wieser eric-wieser changed the title Equation lemmas no longer generated by simple match Equation lemmas are longer generated by a simple match Jan 18, 2023
@eric-wieser
Copy link
Contributor Author

It seems that match statements are a distraction here, as

@[simp]
def foo (a : Nat) : Nat := 2 * a

results in simp unfolding unapplied foo to fun a => 2 * a just as it did above.

@digama0 digama0 added the Mathlib4 high prio Mathlib4 high priority issue label Mar 5, 2023
eric-wieser added a commit to leanprover-community/mathlib that referenced this issue Mar 29, 2023
This adjust definitions such that everything is well-behaved in the case that things are unfolded.
For each such definition, a lemma is added that replaces the equation lemma.

While this just adds `_apply` noise in Lean 3, it is necessary when porting to Lean 4 as there the equation lemma is not generated in the way that we want.
bors bot pushed a commit to leanprover-community/mathlib that referenced this issue Mar 30, 2023
This adjust definitions such that everything is well-behaved in the case that things are unfolded. For each such definition, a lemma is added that replaces the equation lemma. Before this PR, we used
```lean
def transpose (M : matrix m n α) : matrix n m α
| x y := M y x
```
which has the nice behavior (in Lean 3 only) of `rw transpose` only unfolding the definition when it is of the applied form `transpose M i j`. If `dunfold transpose` is used then it becomes the undesirable `λ x y, M y x` in both Lean versions. After this PR, we use
```lean
def transpose (M : matrix m n α) : matrix n m α :=
of $ λ x y, M y x

-- TODO: set as an equation lemma for `transpose`, see mathlib4#3024
@[simp] lemma transpose_apply (M : matrix m n α) (i j) :
  transpose M i j = M j i := rfl
```
This no longer has the nice `rw` behavior, but we can't have that in Lean4 anyway (leanprover/lean4#2042). It also makes `dunfold` insert the `of`, which is better for type-safety.

This affects
* `matrix.transpose`
* `matrix.row`
* `matrix.col`
* `matrix.diagonal`
* `matrix.vec_mul_vec`
* `matrix.block_diagonal`
* `matrix.block_diagonal'`
* `matrix.hadamard`
* `matrix.kronecker_map`
* `pequiv.to_matrix`
* `matrix.circulant`
* `matrix.mv_polynomial_X`
* `algebra.trace_matrix`
* `algebra.embeddings_matrix`

While this just adds `_apply` noise in Lean 3, it is necessary when porting to Lean 4 as there the equation lemma is not generated in the way that we want.

This is hopefully exhaustive; it was found by looking for lines ending in `matrix .*` followed by a `|` line
bors bot pushed a commit to leanprover-community/mathlib4 that referenced this issue Apr 11, 2023
This is to help with #2960 and work around leanprover/lean4#2042.

Ideally we would inspect the function to find that it was declared as `| i, j => A j i` and generate `transpose_apply`, but that's not something I know how to do.



Co-authored-by: Scott Morrison <scott.morrison@gmail.com>
Co-authored-by: Jeremy Tan Jie Rui <reddeloostw@gmail.com>
eric-wieser added a commit to leanprover-community/mathlib4 that referenced this issue Apr 12, 2023
This is to help with #2960 and work around leanprover/lean4#2042.

Ideally we would inspect the function to find that it was declared as `| i, j => A j i` and generate `transpose_apply`, but that's not something I know how to do.



Co-authored-by: Scott Morrison <scott.morrison@gmail.com>
Co-authored-by: Jeremy Tan Jie Rui <reddeloostw@gmail.com>
leodemoura added a commit that referenced this issue Oct 29, 2023
It is not being used yet, but we need to add it before solving issue #2042.
Reason: bootstrapping.
leodemoura added a commit that referenced this issue Oct 29, 2023
semorrison pushed a commit that referenced this issue Oct 30, 2023
semorrison pushed a commit that referenced this issue Oct 30, 2023
Komyyy pushed a commit to Komyyy/lean4 that referenced this issue Nov 2, 2023
It is not being used yet, but we need to add it before solving issue leanprover#2042.
Reason: bootstrapping.
semorrison pushed a commit that referenced this issue Nov 4, 2023
semorrison pushed a commit that referenced this issue Nov 6, 2023
leodemoura added a commit that referenced this issue Nov 9, 2023
arthur-adjedj pushed a commit to arthur-adjedj/lean4 that referenced this issue Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Mathlib4 high prio Mathlib4 high priority issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants