-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add Function.prod and Function.diag
#37631
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
Open
linesthatinterlace
wants to merge
15
commits into
leanprover-community:master
Choose a base branch
from
linesthatinterlace:function_prod
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1720cfd
Simply add the new functions
linesthatinterlace 81d11ca
Remove old Pi.prod
linesthatinterlace 262a3d4
Improve lemmas
linesthatinterlace 584c821
Add dependency
linesthatinterlace 66defff
Fix proofs
linesthatinterlace b893bfd
Fix issues
linesthatinterlace 6e4be8d
Add fixes
linesthatinterlace 0869be6
Final fixes?
linesthatinterlace 3af101f
Final fixes
linesthatinterlace 5dd45ba
Fiddle with notation and lemmas
linesthatinterlace afffd93
Merge branch 'master' into function_prod
linesthatinterlace d0c883b
Merge branch 'master' into function_prod
linesthatinterlace 789eb4e
Merge branch 'master' into function_prod
linesthatinterlace b7f483b
Merge branch 'master' into function_prod
linesthatinterlace 0d618f2
Restore diagrams
linesthatinterlace File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| /- | ||
| Copyright (c) 2026 Wrenna Robson. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Wrenna Robson | ||
| -/ | ||
| module | ||
|
|
||
| public import Mathlib.Init | ||
|
|
||
| /-! | ||
|
|
||
| This file defines `(f ▽ g)`, the operation that pairs two functions `f : ι → α` and | ||
| `g : ι → β` into a function `ι → α × β`. | ||
|
|
||
| It also defines the special case when `f = g = id`, `Function.diag`. This is the canonical injection | ||
| of a type into its prouduct with itself onto its diagonal. | ||
|
|
||
|
|
||
| This file should not depend on anything defined in Mathlib (except for notation), so that it can be | ||
| upstreamed to Batteries or the Lean standard library easily. | ||
|
|
||
| -/ | ||
|
|
||
| @[expose] public section | ||
|
|
||
| namespace Pi | ||
|
|
||
| /-- The dependent mapping into a product type built from dependent maps into each component. -/ | ||
| protected def prod {ι} {α β : ι → Type*} (f : ∀ i, α i) (g : ∀ i, β i) (i : ι) : α i × β i := | ||
| Prod.mk (f i) (g i) | ||
|
|
||
| @[inherit_doc] infixr:95 " ▽' " => Pi.prod | ||
|
|
||
| section | ||
|
|
||
| variable {ι} {α β : ι → Type*} (f f' : ∀ i, α i) (g g' : ∀ i, β i) {c} | ||
|
|
||
| @[simp, grind =] theorem prod_apply : (f ▽' g) c = (f c, g c) := rfl | ||
|
|
||
| theorem fst_prod : ((f ▽' g) c).fst = f c := rfl | ||
| theorem snd_prod : ((f ▽' g) c).snd = g c := rfl | ||
|
|
||
| @[simp] theorem prod_fst_snd {α β} : (Prod.fst : _ → α) ▽' (Prod.snd : _ → β) = id := rfl | ||
| @[simp] theorem prod_snd_fst {α β} : (Prod.snd : _ → β) ▽' (Prod.fst : _ → α) = .swap := rfl | ||
|
|
||
| theorem prod_fst_snd_comp {h : ∀ i, α i × β i} : | ||
| (Prod.fst <| h ·) ▽' (Prod.snd <| h ·) = h := rfl | ||
|
|
||
| theorem fst_comp_prod {f : ∀ i, α i} {g : ∀ i, β i} : (Prod.fst <| (f ▽' g) ·) = f := rfl | ||
| theorem snd_comp_prod {f : ∀ i, α i} {g : ∀ i, β i} : (Prod.snd <| (f ▽' g) ·) = g := rfl | ||
|
|
||
| @[simp] | ||
| theorem prod_eq_iff {f : ∀ i, α i} {g : ∀ i, β i} : | ||
| f ▽' g = f' ▽' g' ↔ f = f' ∧ g = g' := by simp [funext_iff, Prod.ext_iff, forall_and] | ||
|
|
||
| theorem prod_ext_iff {h h' : ∀ i, α i × β i} : h = h' ↔ | ||
| (Prod.fst <| h ·) = (Prod.fst <| h' ·) ∧ (Prod.snd <| h ·) = (Prod.snd <| h' ·) := by | ||
| simp [funext_iff, Prod.ext_iff, forall_and] | ||
|
|
||
| theorem prod_ext {h h' : ∀ i, α i × β i} (h₁ : (Prod.fst <| h ·) = (Prod.fst <| h' ·)) | ||
| (h₂ : (Prod.snd <| h ·) = (Prod.snd <| h' ·)) : h = h' := prod_ext_iff.mpr ⟨h₁, h₂⟩ | ||
|
|
||
| theorem exists_prod_apply_eq (h : ∀ i, α i × β i) : ∃ f g, (f ▽' g) = h := | ||
| ⟨(Prod.fst <| h ·), (Prod.snd <| h ·), prod_fst_snd_comp⟩ | ||
|
|
||
| theorem exists_fst_comp (f : ∀ i, α i) (g : ∀ i, β i) : | ||
| ∃ h : ∀ i, α i × β i, (Prod.fst <| h ·) = f := ⟨(f ▽' g), fst_comp_prod⟩ | ||
| theorem exists_snd_comp (f : ∀ i, α i) (g : ∀ i, β i) : | ||
| ∃ h : ∀ i, α i × β i, (Prod.snd <| h ·) = g := ⟨(f ▽' g), snd_comp_prod⟩ | ||
|
|
||
| @[grind =] | ||
| theorem prod_const_const {ι} {α β} {a : α} {b : β} : | ||
| (Function.const ι a) ▽' (Function.const ι b) = Function.const ι (a, b) := rfl | ||
|
|
||
| theorem eq_prod_iff_fst_comp_snd_comp {f g} {h : ∀ i, α i × β i} : | ||
| h = f ▽' g ↔ (Prod.fst <| h ·) = f ∧ (Prod.snd <| h ·) = g := by simp [prod_ext_iff] | ||
|
|
||
| theorem eq_prod_of_fst_comp_snd_comp {f g} {h : ∀ i, α i × β i} (h₁ : (Prod.fst <| h ·) = f) | ||
| (h₂ : (Prod.snd <| h ·) = g) : h = f ▽' g := eq_prod_iff_fst_comp_snd_comp.mpr ⟨h₁, h₂⟩ | ||
|
|
||
| end | ||
|
|
||
| end Pi | ||
|
|
||
| namespace Function | ||
|
|
||
| variable {α β γ δ : Type*} {ι : Sort*} | ||
|
|
||
| /-- The map into a product type built from maps into each component. -/ | ||
| protected def prod : (ι → α) → (ι → β) → ι → α × β := (· ▽' ·) | ||
|
|
||
| @[inherit_doc] infixr:95 " ▽ " => Function.prod | ||
|
|
||
| section | ||
|
|
||
| variable (f : ι → α) (g : ι → β) | ||
|
|
||
| @[simp, grind =] theorem prod_apply (c : ι) : (f.prod g) c = (f c, g c) := rfl | ||
|
|
||
| theorem prod_comp {γ} {h : γ → ι} : (f ▽ g) ∘ h = (f ∘ h) ▽ (g ∘ h) := rfl | ||
|
|
||
| theorem fst_prod {c} : ((f ▽ g) c).fst = f c := by simp | ||
| theorem snd_prod {c} : ((f ▽ g) c).snd = g c := by simp | ||
|
|
||
| @[simp] theorem prod_fst_snd : Prod.fst (α := α) ▽ Prod.snd (β := β) = id := rfl | ||
| @[simp] theorem prod_snd_fst : Prod.snd (β := β) ▽ Prod.fst (α := α) = .swap := rfl | ||
|
|
||
| @[simp] theorem prod_fst_snd_comp {f : ι → α × β} : (Prod.fst ∘ f) ▽ (Prod.snd ∘ f) = f := rfl | ||
|
|
||
| @[simp] theorem fst_comp_prod {f : ι → α} {g : ι → β} : Prod.fst ∘ (f ▽ g) = f := rfl | ||
| @[simp] theorem snd_comp_prod {f : ι → α} {g : ι → β} : Prod.snd ∘ (f ▽ g) = g := rfl | ||
|
|
||
| theorem prod_comp_prod {f : ι → α} {g : ι → β} {h : α × β → γ} {k : α × β → δ} : | ||
| (h ▽ k) ∘ (f ▽ g) = (h ∘ (f ▽ g)) ▽ (k ∘ (f ▽ g)) := rfl | ||
|
|
||
| theorem comp_prod_comp {f : ι → α} {g : ι → β} {h : α → γ} {k : β → δ} : | ||
| (h ∘ f) ▽ (k ∘ g) = (h ∘ Prod.fst) ▽ (k ∘ Prod.snd) ∘ f ▽ g := rfl | ||
|
|
||
| theorem map_comp_prod {f : ι → α} {g : ι → β} {h : α → γ} {k : β → δ} : | ||
| Prod.map h k ∘ f ▽ g = (h ∘ f) ▽ (k ∘ g) := rfl | ||
|
|
||
| theorem prod_eq_iff {f f' : ι → α} {g g' : ι → β} : f ▽ g = f' ▽ g' ↔ | ||
| f = f' ∧ g = g' := by simp [funext_iff, Prod.ext_iff, forall_and] | ||
|
|
||
| theorem prod_ext_iff {h h' : ι → α × β} : h = h' ↔ | ||
| Prod.fst ∘ h = Prod.fst ∘ h' ∧ Prod.snd ∘ h = (Prod.snd ∘ h') := by | ||
| simp [funext_iff, Prod.ext_iff, forall_and] | ||
|
|
||
| theorem exists_prod_apply_eq (h : ι → α × β) : ∃ f g, f ▽ g = h := | ||
| ⟨Prod.fst ∘ h, Prod.snd ∘ h, prod_fst_snd_comp⟩ | ||
|
|
||
| theorem exists_fst_comp (f : ι → α) (g : ι → β) : | ||
| ∃ h : ι → α × β, Prod.fst ∘ h = f := ⟨f ▽ g, fst_comp_prod⟩ | ||
| theorem exists_snd_comp (f : ι → α) (g : ι → β) : | ||
| ∃ h : ι → α × β, Prod.snd ∘ h = g := ⟨f ▽ g, snd_comp_prod⟩ | ||
|
|
||
| theorem leftInverse_uncurry_prod_prod_fst_comp_snd_comp : Function.LeftInverse | ||
| (Function.prod (ι := γ)).uncurry ((Prod.fst (α := α) ∘ ·) ▽ (Prod.snd (β := β) ∘ ·)) := | ||
| fun _ => rfl | ||
|
|
||
| theorem rightInverse_uncurry_prod_prod_fst_comp_snd_comp : Function.RightInverse | ||
| (Function.prod (ι := γ)).uncurry ((Prod.fst (α := α) ∘ ·) ▽ (Prod.snd (β := β) ∘ ·)) := | ||
| fun _ => rfl | ||
|
|
||
| @[simp, grind =] | ||
| theorem prod_const_const (a : α) (b : β) : | ||
| (Function.const ι a) ▽ (Function.const ι b) = Function.const ι (a, b) := rfl | ||
|
|
||
| theorem const_prod {ι} {α β} {p : α × β} : | ||
| Function.const ι p = (Function.const ι p.1) ▽ (Function.const ι p.2) := rfl | ||
|
|
||
| theorem eq_prod_iff_fst_comp_snd_comp {f g} {h : ι → α × β} : | ||
| h = f ▽ g ↔ Prod.fst ∘ h = f ∧ Prod.snd ∘ h = g := by simp [prod_ext_iff] | ||
|
|
||
| theorem eq_prod_of_fst_comp_snd_comp {f g} {h : ι → α × β} (h₁ : Prod.fst ∘ h = f) | ||
| (h₂ : Prod.snd ∘ h = g) : h = f ▽ g := eq_prod_iff_fst_comp_snd_comp.mpr ⟨h₁, h₂⟩ | ||
|
|
||
| end | ||
|
|
||
| /-- The diagonal map into `Prod`. -/ | ||
| protected def diag : α → α × α := id ▽ id | ||
|
|
||
| @[inherit_doc] prefix:max "⟋" => Function.diag | ||
|
|
||
| section | ||
|
|
||
| variable {a b : α} | ||
|
|
||
| @[grind =] theorem diag_apply : ⟋a = (a, a) := rfl | ||
|
|
||
| @[simp] theorem fst_diag : (⟋a).1 = a := rfl | ||
| @[simp] theorem snd_diag : (⟋a).2 = a := rfl | ||
|
|
||
| theorem map_diag {f : α → β} {g : α → γ} : Prod.map f g ⟋a = (f ▽ g) a := rfl | ||
|
|
||
| @[simp] theorem map_comp_diag {f : α → β} {g : α → γ} : | ||
| Prod.map f g ∘ Function.diag = f ▽ g := rfl | ||
|
|
||
| theorem injective_diag : Function.Injective (α := α) Function.diag := fun _ _ => congrArg Prod.fst | ||
|
|
||
| theorem exists_diag_apply_iff (p : α × α) : (∃ a, ⟋a = p) ↔ p.1 = p.2 := by | ||
| simp [Prod.ext_iff, eq_comm] | ||
|
|
||
| theorem diag_eq_iff : ⟋a = ⟋b ↔ a = b := injective_diag.eq_iff | ||
|
|
||
| @[simp] theorem diag_prod_diag : Function.diag ▽ Function.diag (α := α) = | ||
| Function.diag ∘ Function.diag := rfl | ||
|
|
||
| end | ||
|
|
||
| /-- `Function.prodMap` is `Prod.map` in the `Function` namespace. -/ | ||
| def prodMap (f : α → β) (g : γ → δ) := (f ∘ Prod.fst) ▽ (g ∘ Prod.snd) | ||
|
|
||
| section | ||
|
|
||
| @[simp, grind =] | ||
| theorem prodMap_eq_prod_map {f : α → β} {g : γ → δ} : f.prodMap g = Prod.map f g := rfl | ||
|
|
||
| end | ||
|
|
||
| end Function | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question:
I don't understand why we need both
Pi.prodandFunction.prod. Can't we have just the general version (Pi.prod)? (Related: #36902)If the issue is dot notation, then it's worth mentioning that leanprover/lean4#1629 had some progress recently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that is exactly the issue I was concerned about (I also thought it was quite nice to have the dependent and non-dependent versions, in the same way that we have
Function.dcompandFunction.comp). I also note that if this does get upstreamed (which I am hopeful for), Batteries and Core seem to make extremely minimal use of the "Pi" namespace. So if we do have the general version and we can make the dot notation work, I would rather call the general oneFunction.prod- it is more consistent with, sayLinearMap.prod.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that is the other reason it is quite nice to explictly have a non-dependent version - because
Function.componly works for non-dependent functions, I wanted to avoid future frustration of someone trying to use the non-dependent theorems for the dependent case. Arguably though you could just have the definition bePi.prodand only separate the theorems by namespace, though.It's weird because we obviously do differentiate
SigmaandProddespite the fact that in theory we could just define Prod as "Sigma in the non-dependent case". And actually as the links you post say, we also do differentiateFunction.swapandflip. It feels more consistent to differentiate these.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general - why DO we have the Pi namespace?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the difference between this situation and
SigmavsProd(orDMatrixvsMatrix, orDFinsuppvsFinsupp) is that they are type constructors.This means that if we were to define
Prod A B := Sigma fun _ : A ↦ Bthen both Lean and humans would have a hard time type-checking, since it now involves comparing functions.Here's an example of Lean getting confused because of using
DMatrixwith constant functions instead ofMatrix.But for
Function.{flip,swap,prod,diag}I don't see any benefit in restricting when the function applies to only allow for non-dependent functions. In #36902 I also support getting rid of the duplication and only keeping the dependent version (but it requires a core PR).I'm not sure I've articulated it well, but these feel like different situations to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense to me. Do you see a need to keep the Pi namespace in any instance? That I think for me is the thing I don't quite understand the need for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dot-notation will only work once leanprover/lean4#13244 is merged, so if you want to use dot-notation for now I think you have to have the two separate versions and keep the non-dependent version under
Function. Then thePinamespace is a nice place to keep the dependent version.But I thought that since you defined notation for everything here you don't use dot-notation, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't rule out that people might try and use it in the future. I think it's good to have the option which is why I have the two versions.