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

feat: add Simp.Config.index #4202

Merged
merged 2 commits into from
May 17, 2024
Merged

feat: add Simp.Config.index #4202

merged 2 commits into from
May 17, 2024

Conversation

leodemoura
Copy link
Member

The simp tactic uses a discrimination tree to select candidate theorems that will be used to rewrite an expression. This indexing data structure minimizes the number of theorems that need to be tried and improves performance. However, indexing modulo reducibility is challenging, and a theorem that could be applied, when taking reduction into account, may be missed. For example, suppose we have a simp theorem foo : forall x y, f x (x, y).2 = y, and we are trying to simplify the expression f a b <= b. foo will not be tried by simp because the second argument of f a b is not a projection of a pair. However, f a b is definitionally equal to f a (a, b).2 since we can reduce (a, b).2.

In Lean 3, we had a much simpler indexing data structure where only the head symbol was taken into account. For the theorem foo, the head symbol is f. Thus, the theorem would be considered by simp.

This commit adds the option Simp.Config.index. When simp (config := { index := false }), only the head symbol is considered when retrieving theorems, as in Lean 3. Moreover, if set_option diagnostics true, simp will check whether every applied theorem would also have been applied if index := true, and report them. This feature can help users diagnose tricky issues in code that has been ported from libraries developed using Lean 3 and then ported to Lean 4. In the following example, it will report that foo is a problematic theorem.

opaque f : Nat → Nat → Nat

@[simp] theorem foo : f x (x, y).2 = y := by sorry

example : f a b ≤ b := by
  set_option diagnostics true in
  simp (config := { index := false })

In the example above, the following diagnostic message is produced.

[simp] theorems with bad keys
    foo, key: [f, *, Prod.1, Prod.mk, Nat, Nat, *, *]

With the information above, users can annotate theorems such as foo using no_index for problematic subterms.
Example:

opaque f : Nat → Nat → Nat

@[simp] theorem foo : f x (no_index (x, y).2) = y := by sorry

example : f a b ≤ b := by
  simp -- `foo` is still applied

cc @semorrison
cc @PatrickMassot

When `Simp.Config.index := false`, the discrimination tree index is
mostly ignored. Only the head symbol is taken into account.
This option approximates the Lean 3 behavior better.
When `Simp.Config.index := false` and `set_option diagnostics true`,
for every `simp` theorem that is used to rewrite an expression,
`simp` checks whether the theorem would have been applied if
`Simp.Config.index := true`. If it would not, we record it,
and display it at diagnostics message.
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc May 17, 2024 18:06 Inactive
@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label May 17, 2024
@leanprover-community-mathlib4-bot
Copy link
Collaborator

Mathlib CI status (docs):

  • ❗ Mathlib CI can not be attempted yet, as the nightly-testing-2024-05-17 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-mathlib, Mathlib CI should run now. (2024-05-17 18:09:30)

@leodemoura leodemoura added this pull request to the merge queue May 17, 2024
Merged via the queue into master with commit ee0bcc8 May 17, 2024
12 checks passed
nomeata added a commit that referenced this pull request Aug 16, 2024
makes the option introduced in #4202 also available when using `dsimp`
github-merge-queue bot pushed a commit that referenced this pull request Aug 19, 2024
makes the option introduced in #4202 also available when using `dsimp`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants