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

Commit b0d1d17

Browse files
committed
feat(data/ulift): add monad ulift and monad plift (#3588)
We add `functor`/`applicative`/`monad` instances for `ulift` and `plift`.
1 parent 80f2762 commit b0d1d17

File tree

1 file changed

+95
-5
lines changed

1 file changed

+95
-5
lines changed

src/data/ulift.lean

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,107 @@
11
/-
22
Copyright (c) 2018 Scott Morrison. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
4-
Authors: Scott Morrison
4+
Authors: Scott Morrison, Jannis Limperg
55
6-
Transport through constant families.
6+
Facts about `ulift` and `plift`.
77
-/
88

9-
universes u₁ u₂
9+
universes u v
1010

11-
@[simp] lemma plift.rec.constant {α : Sort u₁} {β : Sort u₂} (b : β) :
11+
namespace plift
12+
13+
variables {α : Sort u} {β : Sort v}
14+
15+
/-- Functorial action. -/
16+
@[simp] protected def map (f : α → β) : plift α → plift β
17+
| (up a) := up (f a)
18+
19+
/-- Embedding of pure values. -/
20+
@[simp] protected def pure : α → plift α := up
21+
22+
/-- Applicative sequencing. -/
23+
@[simp] protected def seq : plift (α → β) → plift α → plift β
24+
| (up f) (up a) := up (f a)
25+
26+
/-- Monadic bind. -/
27+
@[simp] protected def bind : plift α → (α → plift β) → plift β
28+
| (up a) f := f a
29+
30+
instance : monad plift :=
31+
{ map := @plift.map,
32+
pure := @plift.pure,
33+
seq := @plift.seq,
34+
bind := @plift.bind }
35+
36+
instance : is_lawful_functor plift :=
37+
{ id_map := λ α ⟨x⟩, rfl,
38+
comp_map := λ α β γ g h ⟨x⟩, rfl }
39+
40+
instance : is_lawful_applicative plift :=
41+
{ pure_seq_eq_map := λ α β g ⟨x⟩, rfl,
42+
map_pure := λ α β g x, rfl,
43+
seq_pure := λ α β ⟨g⟩ x, rfl,
44+
seq_assoc := λ α β γ ⟨x⟩ ⟨g⟩ ⟨h⟩, rfl }
45+
46+
instance : is_lawful_monad plift :=
47+
{ bind_pure_comp_eq_map := λ α β f ⟨x⟩, rfl,
48+
bind_map_eq_seq := λ α β ⟨a⟩ ⟨b⟩, rfl,
49+
pure_bind := λ α β x f, rfl,
50+
bind_assoc := λ α β γ ⟨x⟩ f g, rfl }
51+
52+
@[simp] lemma rec.constant {α : Sort u} {β : Type v} (b : β) :
1253
@plift.rec α (λ _, β) (λ _, b) = λ _, b :=
1354
funext (λ x, plift.cases_on x (λ a, eq.refl (plift.rec (λ a', b) {down := a})))
1455

15-
@[simp] lemma ulift.rec.constant {α : Type u₁} {β : Sort u₂} (b : β) :
56+
end plift
57+
58+
59+
namespace ulift
60+
61+
variables {α : Type u} {β : Type v}
62+
63+
/-- Functorial action. -/
64+
@[simp] protected def map (f : α → β) : ulift α → ulift β
65+
| (up a) := up (f a)
66+
67+
/-- Embedding of pure values. -/
68+
@[simp] protected def pure : α → ulift α := up
69+
70+
/-- Applicative sequencing. -/
71+
@[simp] protected def seq : ulift (α → β) → ulift α → ulift β
72+
| (up f) (up a) := up (f a)
73+
74+
/-- Monadic bind. -/
75+
@[simp] protected def bind : ulift α → (α → ulift β) → ulift β
76+
| (up a) f := up (down (f a))
77+
-- The `up ∘ down` gives us more universe polymorphism than simply `f a`.
78+
79+
instance : monad ulift :=
80+
{ map := @ulift.map,
81+
pure := @ulift.pure,
82+
seq := @ulift.seq,
83+
bind := @ulift.bind }
84+
85+
instance : is_lawful_functor ulift :=
86+
{ id_map := λ α ⟨x⟩, rfl,
87+
comp_map := λ α β γ g h ⟨x⟩, rfl }
88+
89+
instance : is_lawful_applicative ulift :=
90+
{ pure_seq_eq_map := λ α β g ⟨x⟩, rfl,
91+
map_pure := λ α β g x, rfl,
92+
seq_pure := λ α β ⟨g⟩ x, rfl,
93+
seq_assoc := λ α β γ ⟨x⟩ ⟨g⟩ ⟨h⟩, rfl }
94+
95+
instance : is_lawful_monad ulift :=
96+
{ bind_pure_comp_eq_map := λ α β f ⟨x⟩, rfl,
97+
bind_map_eq_seq := λ α β ⟨a⟩ ⟨b⟩, rfl,
98+
pure_bind := λ α β x f,
99+
by { dsimp only [bind, pure, ulift.pure, ulift.bind], cases (f x), refl },
100+
bind_assoc := λ α β γ ⟨x⟩ f g,
101+
by { dsimp only [bind, pure, ulift.pure, ulift.bind], cases (f x), refl } }
102+
103+
@[simp] lemma rec.constant {α : Type u} {β : Sort v} (b : β) :
16104
@ulift.rec α (λ _, β) (λ _, b) = λ _, b :=
17105
funext (λ x, ulift.cases_on x (λ a, eq.refl (ulift.rec (λ a', b) {down := a})))
106+
107+
end ulift

0 commit comments

Comments
 (0)