Skip to content

[Merged by Bors] - feat: port cc tactic (3/3)#5938

Closed
Komyyy wants to merge 178 commits intomasterfrom
port/Tactic.CC
Closed

[Merged by Bors] - feat: port cc tactic (3/3)#5938
Komyyy wants to merge 178 commits intomasterfrom
port/Tactic.CC

Conversation

@Komyyy
Copy link
Copy Markdown
Contributor

@Komyyy Komyyy commented Jul 16, 2023

@Komyyy Komyyy added help-wanted The author needs attention to resolve issues WIP Work in progress mathlib-port This is a port of a theory file from mathlib. t-meta Tactics, attributes or user commands labels Jul 16, 2023
@kim-em kim-em added the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Jul 16, 2023
@ghost ghost removed the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Jul 17, 2023
@ghost ghost deleted a comment from kim-em Jul 17, 2023
@Komyyy Komyyy force-pushed the port/Tactic.CC branch from 1789653 to 870bbf4 Compare May 8, 2024 10:04
@Komyyy Komyyy removed the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label May 15, 2024
@Komyyy Komyyy added the awaiting-CI This PR does not pass CI yet. This label is automatically removed once it does. label May 15, 2024
@Komyyy Komyyy requested a review from Vierkantor May 15, 2024 13:41
@ghost
Copy link
Copy Markdown

ghost commented May 15, 2024

This PR/issue depends on:

@github-actions github-actions bot removed the awaiting-CI This PR does not pass CI yet. This label is automatically removed once it does. label May 15, 2024
Copy link
Copy Markdown
Contributor

@Vierkantor Vierkantor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very impressed by all the work! Just a few more comments and (assuming the extra tests pass) I think the tactic is totally ready to go!

Comment on lines +206 to +245
/--
The congruence closure tactic `cc` tries to solve the goal by chaining
equalities from context and applying congruence (i.e. if `a = b`, then `f a = f b`).
It is a finishing tactic, i.e. it is meant to close
the current goal, not to make some inconclusive progress.
A mostly trivial example would be:

```lean
example (a b c : ℕ) (f : ℕ → ℕ) (h: a = b) (h' : b = c) : f a = f c := by
cc
```

As an example requiring some thinking to do by hand, consider:

```lean
example (f : ℕ → ℕ) (x : ℕ)
(H1 : f (f (f x)) = x) (H2 : f (f (f (f (f x)))) = x) :
f x = x := by
cc
```

The tactic works by building an equality matching graph. It's a graph where
the vertices are terms and they are linked by edges if they are known to
be equal. Once you've added all the equalities in your context, you take
the transitive closure of the graph and, for each connected component
(i.e. equivalence class) you can elect a term that will represent the
whole class and store proofs that the other elements are equal to it.
You then take the transitive closure of these equalities under the
congruence lemmas.

The `cc` implementation in Lean does a few more tricks: for example it
derives `a = b` from `Nat.succ a = Nat.succ b`, and `Nat.succ a != Nat.zero` for any `a`.

* The starting reference point is Nelson, Oppen, [Fast decision procedures based on congruence
closure](http://www.cs.colorado.edu/~bec/courses/csci5535-s09/reading/nelson-oppen-congruence.pdf),
Journal of the ACM (1980)

* The congruence lemmas for dependent type theory as used in Lean are described in
[Congruence closure in intensional type theory](https://leanprover.github.io/papers/congr.pdf)
(de Moura, Selsam IJCAR 2016). -/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this docstring should be used only on the cc tactic elaborator itself (and then perhaps copied via @[inherit_doc cc]). Otherwise someone will modify one but not the other.

Suggested change
/--
The congruence closure tactic `cc` tries to solve the goal by chaining
equalities from context and applying congruence (i.e. if `a = b`, then `f a = f b`).
It is a finishing tactic, i.e. it is meant to close
the current goal, not to make some inconclusive progress.
A mostly trivial example would be:
```lean
example (a b c : ℕ) (f : ℕ → ℕ) (h: a = b) (h' : b = c) : f a = f c := by
cc
```
As an example requiring some thinking to do by hand, consider:
```lean
example (f : ℕ → ℕ) (x : ℕ)
(H1 : f (f (f x)) = x) (H2 : f (f (f (f (f x)))) = x) :
f x = x := by
cc
```
The tactic works by building an equality matching graph. It's a graph where
the vertices are terms and they are linked by edges if they are known to
be equal. Once you've added all the equalities in your context, you take
the transitive closure of the graph and, for each connected component
(i.e. equivalence class) you can elect a term that will represent the
whole class and store proofs that the other elements are equal to it.
You then take the transitive closure of these equalities under the
congruence lemmas.
The `cc` implementation in Lean does a few more tricks: for example it
derives `a = b` from `Nat.succ a = Nat.succ b`, and `Nat.succ a != Nat.zero` for any `a`.
* The starting reference point is Nelson, Oppen, [Fast decision procedures based on congruence
closure](http://www.cs.colorado.edu/~bec/courses/csci5535-s09/reading/nelson-oppen-congruence.pdf),
Journal of the ACM (1980)
* The congruence lemmas for dependent type theory as used in Lean are described in
[Congruence closure in intensional type theory](https://leanprover.github.io/papers/congr.pdf)
(de Moura, Selsam IJCAR 2016). -/
/-- Solve the metavariable using the congruence closure tactic.
See the docstring on the `cc` tactic for a full explanation of how the tactic works.
-/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative is to keep the explanation of the algorithm (everything from "The tactic works by ..." onwards) here, removing it from the cc tactic docstring. The tactic user doesn't need to know too much about the implementation, after all.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use the inherit_doc attribute on the elaborator, tactic docs can't be displayed.

@Vierkantor Vierkantor added awaiting-author A reviewer has asked the author a question or requested changes. and removed awaiting-review labels May 21, 2024
@Komyyy Komyyy added awaiting-review awaiting-CI This PR does not pass CI yet. This label is automatically removed once it does. and removed awaiting-author A reviewer has asked the author a question or requested changes. labels May 21, 2024
@Komyyy Komyyy requested a review from Vierkantor May 21, 2024 09:52
@github-actions github-actions bot removed the awaiting-CI This PR does not pass CI yet. This label is automatically removed once it does. label May 21, 2024
Co-authored-by: Anne Baanen <Vierkantor@users.noreply.github.com>
@Komyyy Komyyy requested a review from Vierkantor May 21, 2024 12:03
Copy link
Copy Markdown
Contributor

@Vierkantor Vierkantor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, thank you very much! Let's Get This Merged 🎉

bors r+

@mathlib-bors
Copy link
Copy Markdown
Contributor

mathlib-bors bot commented May 21, 2024

Pull request successfully merged into master.

Build succeeded:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mathlib-port This is a port of a theory file from mathlib. modifies-tactic-syntax This PR adds a new interactive tactic or modifies the syntax of an existing tactic. ready-to-merge This PR has been sent to bors. t-meta Tactics, attributes or user commands

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants