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(data/sign): the sign function #12835

Closed
wants to merge 16 commits into from
Closed

Conversation

ericrbg
Copy link
Collaborator

@ericrbg ericrbg commented Mar 19, 2022


Open in Gitpod

Mostly for discussion about how this should be done - what should the output type be? What should we require? What basic theorems would make a good part of the API?

@ericrbg ericrbg added awaiting-review The author would like community review of the PR RFC Request for comment labels Mar 19, 2022
@vihdzp
Copy link
Collaborator

vihdzp commented Mar 20, 2022

Given that there's no clear canonical type for the sign function's output, maybe a new sign type should be made?

inductive sign : Type
| pos  : sign
| zero : sign
| neg  : sign

We could then define casts from sign into or into other rings, which would all be homomorphisms under multiplication.

As for theorems, I think the basic things should be enough, i.e. sign (a * b) = sign a * sign b and variants thereof.

@ericrbg
Copy link
Collaborator Author

ericrbg commented Mar 20, 2022

this field would be isomorphic to zmod 3, whilst being a more suitable output type. interesting, I think that's a nice idea

Copy link
Collaborator

@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 agree with @vihdzp's suggestion of creating a new type. If we add a coercion of the form

instance [has_zero α] [has_one α] [has_neg α] : sign → α

then we can recover the original return value quite easily.


variables [preorder α] [decidable_rel ((<) : α → α → Prop)] {a b : α}

@[simp] lemma sign_zero : sign (0 : α) = 0 := by simp [sign]
Copy link
Collaborator

Choose a reason for hiding this comment

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

For completeness, could you add the sign of positive and negative values?

@Vierkantor Vierkantor 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 Mar 28, 2022
@ericrbg
Copy link
Collaborator Author

ericrbg commented Mar 28, 2022

hmm, it could be a field but I feel like mathematically it's more of a group_with_zero; which instance should I add?

@Vierkantor
Copy link
Collaborator

Let's go for group_with_zero with a remark that it could be a field (but then we wouldn't get a nice order).

src/data/sign.lean Outdated Show resolved Hide resolved
@ericrbg
Copy link
Collaborator Author

ericrbg commented Mar 28, 2022

I'm also not sure how to bundle this; I want to bundle sign as a monoid_with_zero_hom, but there's no real ordered_comm_monoid_with_zero, just linear_ordered_comm_monoid_with_zero. should I do that? or ordered_semiring?

src/data/sign.lean Outdated Show resolved Hide resolved
@eric-wieser
Copy link
Member

eric-wieser commented Mar 28, 2022

I think I remember on zulip seeing some of the following suggested:

  • sign : R → with_zero (unitary R), which gives complex direction
  • sign : R → with_zero (units ℤ)

Were these ruled out as annoying, or just forgotten?

@ericrbg ericrbg added awaiting-review The author would like community review of the PR and removed awaiting-author A reviewer has asked the author a question or requested changes labels Mar 28, 2022
Co-authored-by: damiano <adomani@gmail.com>
@ericrbg
Copy link
Collaborator Author

ericrbg commented Mar 28, 2022

the first one I thought may be a bit annoying. the second could work, too; I do quite like this custom type solution as we get to have a coercion to anything without needing to mess with smul and such like

@ericrbg
Copy link
Collaborator Author

ericrbg commented Mar 30, 2022

I think this should be good for review again, I'm not sure what other obvious things are missing. There is a couple comments in the code - if people could figure out whether we really need ring for sign_hom that'd be nice!

src/data/sign.lean Outdated Show resolved Hide resolved
src/data/sign.lean Outdated Show resolved Hide resolved
Copy link
Collaborator

@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 think this is starting to look very good, thanks for the hard work!

src/data/sign.lean Outdated Show resolved Hide resolved
src/data/sign.lean Outdated Show resolved Hide resolved
src/data/sign.lean Outdated Show resolved Hide resolved
src/data/sign.lean Outdated Show resolved Hide resolved
@Vierkantor Vierkantor 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 Apr 8, 2022
ericrbg and others added 2 commits April 8, 2022 15:04
Co-authored-by: Anne Baanen <Vierkantor@users.noreply.github.com>
Co-authored-by: damiano <adomani@gmail.com>
@ericrbg ericrbg added awaiting-review The author would like community review of the PR and removed awaiting-author A reviewer has asked the author a question or requested changes labels Apr 8, 2022

@[simp] lemma zero_eq_zero : zero = 0 := rfl
@[simp] lemma neg_eq_neg_one : neg = -1 := rfl
@[simp] lemma pos_eq_one : pos = 1 := rfl
Copy link
Member

Choose a reason for hiding this comment

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

Do we get -0 = 0 and --1 = 1 via some other instance 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.

I just added neg_zero' in another PR, and so it's fine because of the distrib neg. We have neg_neg as we have an involutive negation

Comment on lines 151 to 153
{ cases lt_irrefl 0 (h_1.trans $ h.trans_lt h_3) },
{ cases h_2 (h_1.trans_le h) },
{ cases h_2 (h.trans_lt h_4) }
Copy link
Member

Choose a reason for hiding this comment

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

This is referring to auto-generated names. Can you please rewrite?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done, but I think it made it a bit more messy

src/data/sign.lean Outdated Show resolved Hide resolved
@jcommelin jcommelin 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 Apr 11, 2022
ericrbg and others added 2 commits April 11, 2022 09:24
Co-authored-by: Johan Commelin <johan@commelin.net>
@ericrbg ericrbg added awaiting-review The author would like community review of the PR and removed awaiting-author A reviewer has asked the author a question or requested changes labels Apr 11, 2022
Copy link
Member

@jcommelin jcommelin left a comment

Choose a reason for hiding this comment

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

Thanks 🎉

bors merge

@leanprover-community-bot-assistant leanprover-community-bot-assistant 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 Apr 11, 2022
bors bot pushed a commit that referenced this pull request Apr 11, 2022
Co-authored-by: Eric Rodriguez <37984851+ericrbg@users.noreply.github.com>
@bors
Copy link

bors bot commented Apr 11, 2022

Pull request successfully merged into master.

Build succeeded:

@bors bors bot changed the title feat(data/sign): the sign function [Merged by Bors] - feat(data/sign): the sign function Apr 11, 2022
@bors bors bot closed this Apr 11, 2022
@bors bors bot deleted the sign branch April 11, 2022 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-to-merge All that is left is for bors to build and merge this PR. (Remember you need to say `bors r+`.) RFC Request for comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants