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(algebra/category/*): get rid of the local reducible hack #3354

Closed
wants to merge 25 commits into from

Conversation

semorrison
Copy link
Collaborator

I thought I did this back in April, but apparently never made the PR.

We currently use a strange hack when setting up concrete categories, making them locally reducible. There's a library note about this, which ends:

TODO: Probably @[derive] should be able to create instances of the	
required form (without `id`), and then we could use that instead of	
this obscure `local attribute [reducible]` method.

This PR makes the small change required to delta_instance to make this happen, and then stops using the hack in setting up concrete categories (and deletes the library note explaining this hack).


@semorrison semorrison closed this Jul 10, 2020
@gebner
Copy link
Member

gebner commented Jul 10, 2020

Looks good to me. This is definitely the way to go. Why is the PR closed?

@semorrison
Copy link
Collaborator Author

semorrison commented Jul 10, 2020

I ran into trouble in analysis/analytic/basic.lean, where the definition of the order on ennreal is unfolding weirdly and breaking a proof. I closed, hoping that I could fix it, but I'm struggling. Help appreciated!

@semorrison semorrison reopened this Jul 10, 2020
@gebner gebner added enhancement help-wanted The author needs attention to resolve issues WIP Work in progress labels Jul 10, 2020
@semorrison
Copy link
Collaborator Author

It used to be that the line by_cases h : p.radius ≤ nnnorm x, introduced the perfectly sensible hypothesis h : p.radius ≤ ↑(nnnorm x).

Now, however we get the sick h : ∀ (a : nnreal), a ∈ ↑(nnnorm x) → (λ (a : nnreal), ∃ (b : nnreal) (H : b ∈ p.radius), b ≤ a) a.

The problem is surely connected to where we use @[derive] handlers to build the order instances for ennreal. Somehow the change I've made to delta_instance messes this up, but so far I'm not understanding how or why.

@semorrison semorrison changed the title Local reducible feat(algebra/category/*): get rid of the local reducible hack Jul 10, 2020
@semorrison
Copy link
Collaborator Author

Okay, that's awesome, @gebner, thank you! I was beginning to suspect that it was by_cases at fault, rather than my change ...

@semorrison semorrison removed the WIP Work in progress label Jul 10, 2020
@semorrison semorrison removed the help-wanted The author needs attention to resolve issues label Jul 10, 2020
@gebner
Copy link
Member

gebner commented Jul 10, 2020

Somehow the change I've made to delta_instance messes this up, but so far I'm not understanding how or why.

No, you made exactly the right change to the derive handler. Before:

@[instance]
protected noncomputable def ennreal.complete_linear_order : complete_linear_order ennreal :=
id with_top.complete_linear_order

After:

@[instance]
protected noncomputable def ennreal.complete_linear_order : complete_linear_order ennreal :=
with_top.complete_linear_order

Notice the missing id? We've wanted to change this for a long time now.

The built-in by_cases is buggy here, it shouldn't unfold the proposition at all.

dec_e ← mk_app ``decidable [e] <|> fail "by_cases tactic failed, type is not a proposition",
inst ← mk_instance dec_e <|> pure `(classical.prop_decidable %%e),
let inst := `(id %%inst : decidable %%e),
cases inst [h, h],
Copy link
Member

Choose a reason for hiding this comment

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

Does anybody use by_cases in term-mode and expect the result to be a dite?

Copy link
Member

Choose a reason for hiding this comment

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

Apparently @b-mehta's fintype_hom instance did, so I've changed it (back) to produce a dite term.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this something I can fix? Was it one of the things in wide_pullbacks?

Copy link
Member

Choose a reason for hiding this comment

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

No, I've already changed by_cases so that it produces a dite. I don't have a strong opinion one way or another. The cases tactic (which produces decidable.rec) was just easier to use than to construct a dite term by hand.

And yes, it was wide_pullbacks.

The only thing missing on this PR now are the linting errors (inhabited instances & docstrings).

@gebner
Copy link
Member

gebner commented Jul 10, 2020

It is surprisingly hard to write a robust by_cases. For example you can't use refine if the goal contains metavariables. Why? Because to_expr sees the (existing) metavariables in the proof, and believes that they need to be turned into new goals...

- It only works if the proposition is decidable.
- It sometimes unfolds the proposition.

We override the `by_cases` tactic with a correct implementation here.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not fix this in core?

Copy link
Member

Choose a reason for hiding this comment

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

I'll move it to core once it works in mathlib. But seeing the problems (and unforeseen issues) that I encountered while "improving" the tactic, it is absolutely necessary to test this on mathlib first.

@b-mehta
Copy link
Collaborator

b-mehta commented Jul 10, 2020

Oh hah just the other day I saw this local reducible trick and thought it was clever and copied you! Now I know :)

@semorrison semorrison added the awaiting-review The author would like community review of the PR label Jul 11, 2020
@gebner
Copy link
Member

gebner commented Jul 11, 2020

bors r+

@github-actions github-actions bot added ready-to-merge All that is left is for bors to build and merge this PR. (Remember you need to say `bors r+`.) and removed awaiting-review The author would like community review of the PR labels Jul 11, 2020
bors bot pushed a commit that referenced this pull request Jul 11, 2020
I thought I did this back in April, but apparently never made the PR.

We currently use a strange hack when setting up concrete categories, making them locally reducible. There's a library note about this, which ends:

```
TODO: Probably @[derive] should be able to create instances of the	
required form (without `id`), and then we could use that instead of	
this obscure `local attribute [reducible]` method.
```

This PR makes the small change required to `delta_instance` to make this happen, and then stops using the hack in setting up concrete categories (and deletes the library note explaining this hack).



Co-authored-by: Scott Morrison <scott.morrison@gmail.com>
Co-authored-by: Gabriel Ebner <gebner@gebner.org>
@bors
Copy link

bors bot commented Jul 11, 2020

Pull request successfully merged into master.

Build succeeded:

@bors bors bot changed the title feat(algebra/category/*): get rid of the local reducible hack [Merged by Bors] - feat(algebra/category/*): get rid of the local reducible hack Jul 11, 2020
@bors bors bot closed this Jul 11, 2020
@bors bors bot deleted the local_reducible branch July 11, 2020 10:11
lambda-fairy added a commit that referenced this pull request Aug 7, 2020
`by_cases` was changed to use classical reasoning (#3354, leanprover-community/lean#409), but the documentation hasn't been updated yet.

I leave `by_contra` alone as it still uses `decidable`.
bors bot pushed a commit that referenced this pull request Aug 8, 2020
`by_cases` was changed to use classical reasoning (#3354, leanprover-community/lean#409), but the documentation hasn't been updated yet.

I leave `by_contra` alone as it still uses `decidable`.
cipher1024 pushed a commit to cipher1024/mathlib that referenced this pull request Mar 15, 2022
…over-community#3354)

I thought I did this back in April, but apparently never made the PR.

We currently use a strange hack when setting up concrete categories, making them locally reducible. There's a library note about this, which ends:

```
TODO: Probably @[derive] should be able to create instances of the	
required form (without `id`), and then we could use that instead of	
this obscure `local attribute [reducible]` method.
```

This PR makes the small change required to `delta_instance` to make this happen, and then stops using the hack in setting up concrete categories (and deletes the library note explaining this hack).



Co-authored-by: Scott Morrison <scott.morrison@gmail.com>
Co-authored-by: Gabriel Ebner <gebner@gebner.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement ready-to-merge All that is left is for bors to build and merge this PR. (Remember you need to say `bors r+`.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants