forked from leanprover-community/mathlib4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic.lean
More file actions
221 lines (168 loc) · 6.89 KB
/
Basic.lean
File metadata and controls
221 lines (168 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
module
public import Mathlib.Control.Combinators
public import Mathlib.Tactic.CasesM
public import Mathlib.Tactic.Attr.Core
import Mathlib.Tactic.Attr.Register
/-!
Extends the theory on functors, applicatives and monads.
-/
@[expose] public section
universe u v w
variable {α β γ : Type u}
section Functor
attribute [functor_norm] Functor.map_map
end Functor
section Applicative
variable {F : Type u → Type v} [Applicative F]
/-- A generalization of `List.zipWith` which combines list elements with an `Applicative`. -/
def zipWithM {α₁ α₂ φ : Type u} (f : α₁ → α₂ → F φ) : ∀ (_ : List α₁) (_ : List α₂), F (List φ)
| x :: xs, y :: ys => (· :: ·) <$> f x y <*> zipWithM f xs ys
| _, _ => pure []
/-- Like `zipWithM` but evaluates the result as it traverses the lists using `*>`. -/
def zipWithM' (f : α → β → F γ) : List α → List β → F PUnit
| x :: xs, y :: ys => f x y *> zipWithM' f xs ys
| [], _ => pure PUnit.unit
| _, [] => pure PUnit.unit
variable [LawfulApplicative F]
@[simp]
theorem pure_id'_seq (x : F α) : (pure fun x => x) <*> x = x :=
pure_id_seq x
@[functor_norm]
theorem seq_map_assoc (x : F (α → β)) (f : γ → α) (y : F γ) :
x <*> f <$> y = (· ∘ f) <$> x <*> y := by
simp only [← pure_seq]
simp only [seq_assoc, seq_pure, ← comp_map]
simp [pure_seq]
rfl
@[functor_norm]
theorem map_seq (f : β → γ) (x : F (α → β)) (y : F α) :
f <$> (x <*> y) = (f ∘ ·) <$> x <*> y := by
simp only [← pure_seq]; simp [seq_assoc]
end Applicative
section Monad
variable {m : Type u → Type v} [Monad m] [LawfulMonad m]
theorem seq_bind_eq (x : m α) {g : β → m γ} {f : α → β} :
f <$> x >>= g = x >>= g ∘ f :=
show bind (f <$> x) g = bind x (g ∘ f) by
simp [Function.comp_def]
-- order of implicits and `Seq.seq` has a lazily evaluated second argument using `Unit`
@[functor_norm]
theorem fish_pure {α β} (f : α → m β) : f >=> pure = f := by
simp +unfoldPartialApp only [(· >=> ·), functor_norm]
@[functor_norm]
theorem fish_pipe {α β} (f : α → m β) : pure >=> f = f := by
simp +unfoldPartialApp only [(· >=> ·), functor_norm]
-- note: in Lean 3 `>=>` is left-associative, but in Lean 4 it is right-associative.
@[functor_norm]
theorem fish_assoc {α β γ φ} (f : α → m β) (g : β → m γ) (h : γ → m φ) :
(f >=> g) >=> h = f >=> g >=> h := by
simp +unfoldPartialApp only [(· >=> ·), functor_norm]
variable {β' γ' : Type v}
variable {m' : Type v → Type w} [Monad m']
/-- Takes a value `β` and `List α` and accumulates pairs according to a monadic function `f`.
Accumulation occurs from the right (i.e., starting from the tail of the list). -/
def List.mapAccumRM (f : α → β' → m' (β' × γ')) : β' → List α → m' (β' × List γ')
| a, [] => pure (a, [])
| a, x :: xs => do
let (a', ys) ← List.mapAccumRM f a xs
let (a'', y) ← f x a'
pure (a'', y :: ys)
/-- Takes a value `β` and `List α` and accumulates pairs according to a monadic function `f`.
Accumulation occurs from the left (i.e., starting from the head of the list). -/
def List.mapAccumLM (f : β' → α → m' (β' × γ')) : β' → List α → m' (β' × List γ')
| a, [] => pure (a, [])
| a, x :: xs => do
let (a', y) ← f a x
let (a'', ys) ← List.mapAccumLM f a' xs
pure (a'', y :: ys)
end Monad
section
variable {m : Type u → Type u} [Monad m] [LawfulMonad m]
theorem joinM_map_map {α β : Type u} (f : α → β) (a : m (m α)) :
joinM (Functor.map f <$> a) = f <$> joinM a := by
simp only [joinM, id, ← bind_pure_comp, bind_assoc, pure_bind]
theorem joinM_map_joinM {α : Type u} (a : m (m (m α))) : joinM (joinM <$> a) = joinM (joinM a) := by
simp only [joinM, id, ← bind_pure_comp, bind_assoc, pure_bind]
@[simp]
theorem joinM_map_pure {α : Type u} (a : m α) : joinM (pure <$> a) = a := by
simp only [joinM, id, ← bind_pure_comp, bind_assoc, pure_bind, bind_pure]
@[simp]
theorem joinM_pure {α : Type u} (a : m α) : joinM (pure a) = a :=
LawfulMonad.pure_bind a id
end
section Alternative
variable {F : Type → Type v} [Alternative F]
-- [todo] add notation for `Functor.mapConst` and port `Functor.mapConstRev`
/-- Returns `pure true` if the computation succeeds and `pure false` otherwise. -/
def succeeds {α} (x : F α) : F Bool :=
Functor.mapConst true x <|> pure false
/-- Attempts to perform the computation, but fails silently if it doesn't succeed. -/
def tryM {α} (x : F α) : F Unit :=
Functor.mapConst () x <|> pure ()
/-- Attempts to perform the computation, and returns `none` if it doesn't succeed. -/
def try? {α} (x : F α) : F (Option α) :=
some <$> x <|> pure none
@[simp]
theorem guard_true {h : Decidable True} : @guard F _ True h = pure () := by simp [guard]
@[simp]
theorem guard_false {h : Decidable False} : @guard F _ False h = failure := by
simp [guard]
end Alternative
namespace Sum
variable {e : Type v}
/-- The monadic `bind` operation for `Sum`. -/
protected def bind {α β} : e ⊕ α → (α → e ⊕ β) → e ⊕ β
| inl x, _ => inl x
| inr x, f => f x
-- incorrectly marked as a bad translation by mathport, so we do not mark with `ₓ`.
instance : Monad (Sum.{v, u} e) where
pure := @Sum.inr e
bind := @Sum.bind e
instance : LawfulFunctor (Sum.{v, u} e) := by
constructor <;> intros <;> (try casesm Sum _ _) <;> rfl
instance : LawfulMonad (Sum.{v, u} e) where
seqRight_eq := by
intros
casesm Sum _ _ <;> casesm Sum _ _ <;> rfl
seqLeft_eq := by
intros
casesm Sum _ _ <;> rfl
pure_seq := by
intros
rfl
bind_assoc := by
intros
casesm Sum _ _ <;> rfl
pure_bind := by
intros
rfl
bind_pure_comp := by
intros
casesm Sum _ _ <;> rfl
bind_map := by
intros
casesm Sum _ _ <;> rfl
end Sum
/-- A `CommApplicative` functor `m` is a (lawful) applicative functor which behaves identically on
`α × β` and `β × α`, so computations can occur in either order. -/
class CommApplicative (m : Type u → Type v) [Applicative m] : Prop extends LawfulApplicative m where
/-- Computations performed first on `a : α` and then on `b : β` are equal to those performed in
the reverse order. -/
commutative_prod : ∀ {α β} (a : m α) (b : m β),
Prod.mk <$> a <*> b = (fun (b : β) a => (a, b)) <$> b <*> a
open Functor
theorem CommApplicative.commutative_map {m : Type u → Type v} [h : Applicative m]
[CommApplicative m] {α β γ} (a : m α) (b : m β) {f : α → β → γ} :
f <$> a <*> b = flip f <$> b <*> a :=
calc
f <$> a <*> b = (fun p : α × β => f p.1 p.2) <$> (Prod.mk <$> a <*> b) := by
simp only [map_seq, map_map, Function.comp_def]
_ = (fun b a => f a b) <$> b <*> a := by
rw [@CommApplicative.commutative_prod m h]
simp [map_seq, map_map]
rfl