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(data/finsupp/lex): linear order on finsupps #15984

Closed
wants to merge 92 commits into from

Conversation

adomani
Copy link
Collaborator

@adomani adomani commented Aug 10, 2022

EDIT: #16127 is an evolution of this PR. This PR has been closed.

This PR constructs a linear order on finsupps where both source and target are linearly ordered. This is useful for #15983, where these linear orders are used to prove that add_monoid_algebras have no non-trivial zero-divisors.

The PR also proves that the lexicographic linear order on finitely supported functions preserves (one) covariant class assumption.

This is a further step in proving that many add_monoid_algebras have no zero-divisors.

This PR is work in progress and comments or suggestions are as usual really really appreciated!

Zulip discussion


Open in Gitpod

@adomani adomani added the awaiting-review The author would like community review of the PR label Aug 10, 2022
src/data/finsupp/lex.lean Outdated Show resolved Hide resolved
src/data/finsupp/lex.lean Outdated Show resolved Hide resolved
src/data/finsupp/lex.lean Outdated Show resolved Hide resolved
src/data/finsupp/lex.lean Outdated Show resolved Hide resolved
src/data/finsupp/lex.lean Outdated Show resolved Hide resolved
src/data/finsupp/lex.lean Outdated Show resolved Hide resolved
src/data/finsupp/lex.lean Outdated Show resolved Hide resolved
src/data/finsupp/lex.lean Outdated Show resolved Hide resolved
src/data/finsupp/lex.lean Outdated Show resolved Hide resolved
src/data/finsupp/lex.lean Outdated Show resolved Hide resolved
adomani and others added 3 commits August 11, 2022 09:25
Co-authored-by: Violeta Hernández <vi.hdz.p@gmail.com>
@adomani adomani added the t-order Order hierarchy label Aug 11, 2022

variables {α N : Type*} [has_zero N]

open_locale classical
Copy link
Member

Choose a reason for hiding this comment

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

Does removing this prevent things being noncomputable below?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removing open_locale classical prevents union in finsupp from existing.

Copy link
Member

Choose a reason for hiding this comment

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

Then you're missing decidable_eq α on a lemma somewhere. If you find yourself addnig open_locale classical to make a lemma statement compile, you've made a wrong turn.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should I then also add decidable_pred (λ (a : α), ⇑f a ≠ ⇑g a), which now Lean asks?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

(Or maybe decidable_eq N?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@eric-wieser, I take it that you are happy with the decidable_eq assumption, instead of the ne version that is needed? Is there some rationale between preferring one over the other (besides simplicity of stating)?

@eric-wieser
Copy link
Member

Can you copy over as many of the pi lemmas about lex as possible?

adomani and others added 3 commits August 11, 2022 16:31
Co-authored-by: Violeta Hernández <vi.hdz.p@gmail.com>
@eric-wieser eric-wieser added awaiting-author A reviewer has asked the author a question or requested changes and removed awaiting-review The author would like community review of the PR labels Aug 11, 2022
adomani and others added 2 commits August 11, 2022 16:39
Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
@adomani
Copy link
Collaborator Author

adomani commented Aug 11, 2022

Can you copy over as many of the pi lemmas about lex as possible?

If I understand correctly, in the pi.lex file there is an underlying order on the Pi type (on line 66) and this instance is then used to prove monotonicity and various other lemmas. Should I port over this instance as well?

@vihdzp
Copy link
Collaborator

vihdzp commented Aug 11, 2022

I think the suggestion is to prove the finsupp analogues of these pi theorems.

@adomani
Copy link
Collaborator Author

adomani commented Aug 11, 2022

Sure, but those lemmas involve inequalities on the non-lex type, which does not have an order currently.

@eric-wieser
Copy link
Member

Sure, but those lemmas involve inequalities on the non-lex type, which does not have an order currently.

What about this one?

instance : has_le (ι →₀ α) := ⟨λ f g, ∀ i, f i ≤ g i⟩

@adomani
Copy link
Collaborator Author

adomani commented Aug 11, 2022

Eric, thanks! I was missing the import! I added lt/strict_order instances as well. I copied over some of the pi.lex lemmas. I omitted the bot ones, since the instances for bot on finsupp seem to be on canonically_ordered stuff, which seems not too relevant. Let me know what you think!

Copy link
Member

@eric-wieser eric-wieser left a comment

Choose a reason for hiding this comment

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

Don't forget the lemmas finsupp.to_lex_apply and finsupp.of_lex_apply!

adomani and others added 3 commits August 11, 2022 18:47
Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
@adomani
Copy link
Collaborator Author

adomani commented Aug 11, 2022

Don't forget the lemmas finsupp.to_lex_apply and finsupp.of_lex_apply!

These lemmas are in!

I tried to use the linear_order.lift, but failed. If I understand your suggestion correctly, I was trying to lift the order from the pi.lex instance. However, that instance has an extra instance of well-order that is not needed here, thanks to the finite support.

#check @pi.lex.linear_order
pi.lex.linear_order :
  Π {ι : Type u_3} {β : ι → Type u_4} [_inst_1 : linear_order ι]
  [_inst_2 : is_well_order ι has_lt.lt]   --  <-- here
  [_inst_3 : Π (a : ι), linear_order (β a)], linear_order (lex (Π (i : ι), β i))

Copy link
Member

@eric-wieser eric-wieser left a comment

Choose a reason for hiding this comment

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

Here's a constructive version that doesn't need wit:

private def lt_trichotomy_rec {P : lex (α →₀ N)→ lex (α →₀ N) → Sort*}
  (h_lt : Π {f g}, f < g → P f g) (h_eq : Π {f g}, f = g → P f g) (h_gt : Π {f g}, g < f → P f g)
  (f g) : P f g  :=
(λ f g : α →₀ N, show P (to_lex f) (to_lex g), from
match _, rfl : ∀ y, (f.diff g).min = y → _ with
| ⊤, h := h_eq (finsupp.diff_eq_empty.mp (finset.min_eq_top.mp h))
| (wit : α), h :=
  have hne : f wit ≠ g wit := mem_diff.mp (finset.mem_of_min h),
  -- have hne' : f ≠ g := finsupp.diff_eq_empty.not.mp (ne_empty_o_,
  if hwit : f wit < g wit then
    h_lt ⟨wit, by exact λ j hj, begin
      refine mem_diff.not_left.mp _,
      replace hj := (with_top.coe_lt_coe.mpr hj).trans_eq h.symm,
      contrapose! hj,
      exact finset.min_le_coe_of_mem hj,
    end, hwit⟩
  else
    have hwit' : g wit < f wit := hne.lt_or_lt.resolve_left hwit,
    h_gt ⟨wit, by exact λ j hj, begin
      rw diff_comm at h,
      refine mem_diff.not_left.mp _,
      replace hj := (with_top.coe_lt_coe.mpr hj).trans_eq h.symm,
      contrapose! hj,
      exact finset.min_le_coe_of_mem hj,
    end, hwit'⟩
end) (of_lex f) (of_lex g)

instance lex.decidable_le : @decidable_rel (lex (α →₀ N)) (≤) :=
lt_trichotomy_rec
  (λ f g h, is_true $ or.inr h)
  (λ f g h, is_true $ or.inl $ congr_arg _ h)
  (λ f g h, is_false $ λ h', (lt_irrefl _ (h.trans_le h')).elim)

instance lex.decidable_lt : @decidable_rel (lex (α →₀ N)) (<) :=
lt_trichotomy_rec
  (λ f g h, is_true h)
  (λ f g h, is_false h.not_lt)
  (λ f g h, is_false h.asymm)

/--  The linear order on `finsupp`s obtained by the lexicographic ordering. -/
instance lex.linear_order : linear_order (lex (α →₀ N)) :=
{ le_total := lt_trichotomy_rec
    (λ f g h, or.inl h.le)
    (λ f g h, or.inl h.le)
    (λ f g h, or.inr h.le),
  decidable_lt := by apply_instance,
  decidable_le := by apply_instance,
  decidable_eq := by apply_instance,
  ..lex.partial_order }

There's one obvious duplication there that would be solved either by wit or more API for diff

@adomani
Copy link
Collaborator Author

adomani commented Aug 17, 2022

@eric-wieser, if you are happy with this version feel free to push it!

I am partly in favour of wit since it is a common way of arguing: you want to prove something, you find a minimal representative where you can test what happens and you decide your statement there. I agree that wit probably does not achieve this in a very general way.

Also, my eventual goal is to be able to prove that several add_monoid_algebras have no zero-divisors and the linear order is just a convenient tool that will actually self-eliminate from the final statement: the order on the source of the finsupp is completely arbitrary, while the order on the target is tied in with covariant assumptions. Since the target is nat in cases of interest, this is ok. For the source, I simply plan to take any linear order on the source, as this is all that is needed.

@eric-wieser
Copy link
Member

I've pushed a slightly cleaner version.

I think the problem with your wit is that it has to repeatedly deal with a junk value that doesn't make anything easier. It's not really much more work to write (f.diff g).min or (f.diff g).min' or whatever variant of junk-handling the user actually wants, and the spelling is arguably still very readable.

@adomani
Copy link
Collaborator Author

adomani commented Aug 18, 2022

@eric-wieser, do you mind if I PR the finset.lattice lemmas separately? I am about to open a PR with a proof of covariance that does not use wit (and is much simpler than I thought), so those lemmas would have to be part of the other PR as well.

@adomani adomani added WIP Work in progress and removed awaiting-review The author would like community review of the PR labels Aug 18, 2022
@adomani
Copy link
Collaborator Author

adomani commented Aug 19, 2022

This PR has been essentially replaced by #16127.

@adomani adomani closed this Aug 19, 2022
bors bot pushed a commit that referenced this pull request Aug 23, 2022
…in (#16192)

These four lemmas were proved and used by Eric in his decidable version of `finsupp.lex`, replacing my previous undecidable one.

They are extracted from #15984 (now closed) and used in #16127 (which replaces the closed PR).

Authored-by: Eric Wieser <[wieser.eric@gmail.com](mailto:wieser.eric@gmail.com)>
Submitted by me 😄
bors bot pushed a commit that referenced this pull request Sep 1, 2022
…t classes (#16127)

This PR constructs a linear order on `finsupp`s where both source and target are linearly ordered. This is useful for #15983, where these linear orders help prove that (some) `add_monoid_algebra`s have no non-trivial zero-divisors.

The PR also proves that the lexicographic linear order on finitely supported functions preserves a few covariant class assumptions.

As always, comments and suggestions are really really appreciated!

[Zulip discussion](https://leanprover.zulipchat.com/#narrow/stream/217875-Is-there-code-for-X.3F/topic/linear.20order.20on.20finsupps)

This PR supersedes #15984.
@YaelDillies YaelDillies deleted the adomani_finsupp_lex branch February 27, 2023 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked-by-other-PR This PR depends on another PR which is still in the queue. A bot manages this label via PR comment. t-order Order hierarchy WIP Work in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants