Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit bfadf05

Browse files
eric-wieserurkud
andcommitted
feat(algebra, logic): Pi instances for nontrivial and monoid_with_zero (#4766)
Co-authored-by: Yury G. Kudryashov <urkud@urkud.name>
1 parent 58f8817 commit bfadf05

File tree

5 files changed

+97
-55
lines changed

5 files changed

+97
-55
lines changed

src/algebra/group/pi.lean

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ Copyright (c) 2018 Simon Hudon. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Simon Hudon, Patrick Massot
55
-/
6+
import data.pi
67
import algebra.ordered_group
8+
import algebra.group_with_zero
79
import tactic.pi_instances
810
/-!
911
# Pi instances for groups and monoids
@@ -82,6 +84,15 @@ instance ordered_comm_group [∀ i, ordered_comm_group $ f i] :
8284
..pi.comm_group,
8385
..pi.partial_order }
8486

87+
instance mul_zero_class [∀ i, mul_zero_class $ f i] :
88+
mul_zero_class (Π i : I, f i) :=
89+
by refine_struct { zero := (0 : Π i, f i), mul := (*), .. }; tactic.pi_instance_derive_field
90+
91+
instance comm_monoid_with_zero [∀ i, comm_monoid_with_zero $ f i] :
92+
comm_monoid_with_zero (Π i : I, f i) :=
93+
by refine_struct { zero := (0 : Π i, f i), one := (1 : Π i, f i), mul := (*), .. };
94+
tactic.pi_instance_derive_field
95+
8596
section instance_lemmas
8697
open function
8798

@@ -95,31 +106,6 @@ variables {α β γ : Type*}
95106

96107
end instance_lemmas
97108

98-
variables [decidable_eq I]
99-
variables [Π i, has_zero (f i)]
100-
101-
/-- The function supported at `i`, with value `x` there. -/
102-
def single (i : I) (x : f i) : Π i, f i :=
103-
λ i', if h : i' = i then (by { subst h, exact x }) else 0
104-
105-
@[simp]
106-
lemma single_eq_same (i : I) (x : f i) : single i x i = x :=
107-
begin
108-
dsimp [single],
109-
split_ifs,
110-
{ refl, },
111-
{ exfalso, exact h rfl, }
112-
end
113-
114-
@[simp]
115-
lemma single_eq_of_ne {i i' : I} (h : i' ≠ i) (x : f i) : single i x i' = 0 :=
116-
begin
117-
dsimp [single],
118-
split_ifs with h',
119-
{ exfalso, exact h h', },
120-
{ refl, }
121-
end
122-
123109
end pi
124110

125111
section monoid_hom

src/algebra/ring/pi.lean

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ variable {I : Type u} -- The indexing type
1919
variable {f : I → Type v} -- The family of types already equipped with instances
2020
variables (x y : Π i, f i) (i : I)
2121

22-
instance mul_zero_class [Π i, mul_zero_class $ f i] : mul_zero_class (Π i : I, f i) :=
23-
by refine_struct { zero := (0 : Π i, f i), mul := (*), .. }; tactic.pi_instance_derive_field
24-
2522
instance distrib [Π i, distrib $ f i] : distrib (Π i : I, f i) :=
2623
by refine_struct { add := (+), mul := (*), .. }; tactic.pi_instance_derive_field
2724

src/data/pi.lean

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/-
2+
Copyright (c) 2020 Eric Wieser. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Simon Hudon, Patrick Massot, Eric Wieser
5+
-/
6+
import tactic.split_ifs
7+
import tactic.simpa
8+
/-!
9+
# Theorems on pi types
10+
11+
This file defines basic structures on Pi Types
12+
-/
13+
14+
universes u v w
15+
variable {I : Type u} -- The indexing type
16+
variable {f : I → Type v} -- The family of types already equipped with instances
17+
variables (x y : Π i, f i) (i : I)
18+
19+
namespace pi
20+
21+
variables [decidable_eq I]
22+
variables [Π i, has_zero (f i)]
23+
24+
/-- The function supported at `i`, with value `x` there. -/
25+
def single (i : I) (x : f i) : Π i, f i :=
26+
λ i', if h : i' = i then (by { subst h, exact x }) else 0
27+
28+
@[simp]
29+
lemma single_eq_same (i : I) (x : f i) : single i x i = x :=
30+
begin
31+
dsimp [single],
32+
split_ifs,
33+
{ refl, },
34+
{ exfalso, exact h rfl, }
35+
end
36+
37+
@[simp]
38+
lemma single_eq_of_ne {i i' : I} (h : i' ≠ i) (x : f i) : single i x i' = 0 :=
39+
begin
40+
dsimp [single],
41+
split_ifs with h',
42+
{ exfalso, exact h h', },
43+
{ refl, }
44+
end
45+
46+
variables (f)
47+
48+
lemma single_injective (i : I) : function.injective (single i : f i → Π i, f i) :=
49+
λ x y h, by simpa only [single, dif_pos] using congr_fun h i
50+
51+
end pi

src/logic/function/basic.lean

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ if h : a = a' then eq.rec v h.symm else f a
306306
@[simp] lemma update_same (a : α) (v : β a) (f : Πa, β a) : update f a v a = v :=
307307
dif_pos rfl
308308

309+
lemma update_injective (f : Πa, β a) (a' : α) : injective (update f a') :=
310+
λ v v' h, have _ := congr_fun h a', by rwa [update_same, update_same] at this
311+
309312
@[simp] lemma update_noteq {a a' : α} (h : a ≠ a') (v : β a') (f : Πa, β a) : update f a' v a = f a :=
310313
dif_neg h
311314

src/logic/nontrivial.lean

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Sébastien Gouëzel
55
-/
6+
import data.pi
7+
import data.prod
68
import logic.unique
79
import logic.function.basic
810

@@ -80,36 +82,9 @@ by { rw [← not_nontrivial_iff_subsingleton, or_comm], exact classical.em _ }
8082
lemma false_of_nontrivial_of_subsingleton (α : Type*) [nontrivial α] [subsingleton α] : false :=
8183
let ⟨x, y, h⟩ := exists_pair_ne α in h $ subsingleton.elim x y
8284

83-
instance nontrivial_prod_left [nontrivial α] [nonempty β] : nontrivial (α × β) :=
84-
begin
85-
inhabit β,
86-
rcases exists_pair_ne α with ⟨x, y, h⟩,
87-
use [(x, default β), (y, default β)],
88-
contrapose! h,
89-
exact congr_arg prod.fst h
90-
end
91-
92-
instance nontrivial_prod_right [nontrivial α] [nonempty β] : nontrivial (β × α) :=
93-
begin
94-
inhabit β,
95-
rcases exists_pair_ne α with ⟨x, y, h⟩,
96-
use [(default β, x), (default β, y)],
97-
contrapose! h,
98-
exact congr_arg prod.snd h
99-
end
100-
10185
instance option.nontrivial [nonempty α] : nontrivial (option α) :=
10286
by { inhabit α, use [none, some (default α)] }
10387

104-
instance function.nontrivial [nonempty α] [nontrivial β] : nontrivial (α → β) :=
105-
begin
106-
rcases exists_pair_ne β with ⟨x, y, h⟩,
107-
use [λ _, x, λ _, y],
108-
contrapose! h,
109-
inhabit α,
110-
exact congr_fun h (default α)
111-
end
112-
11388
/-- Pushforward a `nontrivial` instance along an injective function. -/
11489
protected lemma function.injective.nontrivial [nontrivial α]
11590
{f : α → β} (hf : function.injective f) : nontrivial β :=
@@ -137,6 +112,36 @@ begin
137112
{ exact ⟨x₂, h⟩ }
138113
end
139114

115+
instance nontrivial_prod_right [nonempty α] [nontrivial β] : nontrivial (α × β) :=
116+
prod.snd_surjective.nontrivial
117+
118+
instance nontrivial_prod_left [nontrivial α] [nonempty β] : nontrivial (α × β) :=
119+
prod.fst_surjective.nontrivial
120+
121+
namespace pi
122+
123+
variables {I : Type*} {f : I → Type*}
124+
125+
/-- A pi type is nontrivial if it's nonempty everywhere and nontrivial somewhere. -/
126+
lemma nontrivial_at (i' : I) [inst : Π i, nonempty (f i)] [nontrivial (f i')] :
127+
nontrivial (Π i : I, f i) :=
128+
by classical; exact
129+
(function.update_injective (λ i, classical.choice (inst i)) i').nontrivial
130+
131+
/--
132+
As a convenience, provide an instance automatically if `(f (default I))` is nontrivial.
133+
134+
If a different index has the non-trivial type, then use `haveI := nontrivial_at that_index`.
135+
-/
136+
instance nontrivial [inhabited I] [inst : Π i, nonempty (f i)] [nontrivial (f (default I))] :
137+
nontrivial (Π i : I, f i) :=
138+
nontrivial_at (default I)
139+
140+
end pi
141+
142+
instance function.nontrivial [h : nonempty α] [nontrivial β] : nontrivial (α → β) :=
143+
h.elim $ λ a, pi.nontrivial_at a
144+
140145
mk_simp_attribute nontriviality "Simp lemmas for `nontriviality` tactic"
141146

142147
protected lemma subsingleton.le [preorder α] [subsingleton α] (x y : α) : x ≤ y :=

0 commit comments

Comments
 (0)