Zulip Chat Archive

Stream: mathlib4

Topic: Even and odd functions


David Loeffler (Sep 02 2024 at 15:55):

I believe Mathlib doesn't currently have the notion of "even" and "odd" functions (between types with negation), i.e. functions satisfying f (-x) = f x or f (-x) = -(f x) respectively. Cf. https://en.wikipedia.org/wiki/Even_and_odd_functions.

These are pretty trivial to code up:

/-- A function `f` is _even_ if it satisfies `f (-x) = f x` for all `x`. -/
protected def Function.Even {R R' : Type*} [Neg R] (f : R  R') : Prop :=  (x : R), f (-x) = f x

/-- A function `f` is _odd_ if it satisfies `f (-x) = -f x` for all `x`. -/
protected def Function.Odd {R R' : Type*} [Neg R] [Neg R'] (f : R  R') : Prop :=
   (x : R), f (-x) = -(f x)

My question is: where should they go in Mathlib, given that they have virtually no prerequisites at all? Would a new file under Data, perhaps Data/EvenOddFunction.lean, be a good solution?

Yaël Dillies (Sep 02 2024 at 15:56):

Definitely not under Data since they import algebra if you try to prove anything non-trivial. Algebra could work

David Loeffler (Sep 02 2024 at 16:00):

Good point, I guess you will very soon need InvolutiveNeg and that lives in Algebra/Group. How about Algebra.Group.EvenOddFunction then?

Yaël Dillies (Sep 02 2024 at 16:11):

Happy with it

Yury G. Kudryashov (Sep 02 2024 at 17:52):

Should it come with a linter warning against _root_.Even (f : α → β)?

Yury G. Kudryashov (Sep 02 2024 at 17:53):

This is a valid term that means (after some simplifications) ∀ a, Even (f a).

David Loeffler (Sep 02 2024 at 20:11):

Yury G. Kudryashov said:

Should it come with a linter warning against _root_.Even (f : α → β)?

This sounds like a nice idea (I made that mistake several times myself) but I have no idea how to write linters myself...

Johan Commelin (Sep 03 2024 at 04:52):

I think such a linter can be useful (not for mathlib CI, but just as an authoring tool in VScode). But it can definitely be a separate PR.

Damiano Testa (Sep 03 2024 at 06:23):

I'm not sure that I understand what the linter should do: it is supposed to prevent using _root_.Even with a function? Due to the protected this means Even f where f is a function, right?

Yaël Dillies (Sep 03 2024 at 06:44):

Why don't you just write f.Even?

Yaël Dillies (Sep 03 2024 at 06:44):

Or, let's be crazy, why not call the predicates EvenFun/OddFun?

Eric Wieser (Sep 03 2024 at 08:50):

Johan Commelin said:

I think such a linter can be useful (not for mathlib CI, but just as an authoring tool in VScode). But it can definitely be a separate PR.

A similar linter would be good for IsCoprime on Nat

Damiano Testa (Sep 03 2024 at 09:01):

This linter would be a "reverse" protected, or am I misunderstanding? It should flag a selected few declarations X for which Y.X is preferred (and possibly protected)?

Johan Commelin (Sep 03 2024 at 09:03):

Well, I think it should also look at the t that comes after X. So for certain t it should warn about X t and suggest Y.X t.

Johan Commelin (Sep 03 2024 at 09:03):

And I think it should only run locally.

Damiano Testa (Sep 03 2024 at 09:06):

Ok, yes, I did not mention further necessary checks to trigger the linter, but I think that I have a clearer idea of what it should do!

David Loeffler (Sep 03 2024 at 11:10):

PR up at #16446.


Last updated: May 02 2025 at 03:31 UTC