-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonad.v
458 lines (386 loc) · 13.5 KB
/
Monad.v
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
Require Import List.
Require Import Program.
Require Import Coq.Sets.Ensembles.
(** * Type classes for functors, applicatives, and monads *)
Local Notation "f ∘ g" := (fun x => f (g x)) (at level 40, left associativity).
Class Functor (f : Type -> Type) : Type :=
{ fmap : forall {A B}, (A -> B) -> f A -> f B }.
Class Applicative (f : Type -> Type) `{F : Functor f} : Type :=
{ pure : forall {A}, A -> f A;
liftA : forall {A B}, f (A -> B) -> f A -> f B
}.
Class Monad (m: Type -> Type) `{M : Applicative m} : Type :=
{ bind: forall {A}, m A -> forall {B}, (A -> m B) -> m B
}.
Definition return_ {m : Type -> Type} `{M : Monad m} {A : Type} : A -> m A := pure.
Class MonadTrans (t : (Type -> Type) -> (Type -> Type)) :=
{ liftT : forall {m} `{Monad m} {A}, m A -> t m A }.
(** ** Notations *)
Module MonadNotations.
Notation "f <*> a" := (liftA f a) (left associativity, at level 64) : monad_scope.
Notation "a >>= f" := (bind a f) (left associativity, at level 66) : monad_scope.
Notation "a >> b" := (bind a (fun _ => b)) (at level 66, left associativity) : monad_scope.
Notation "'do' a ← e ; c" := (bind e (fun a => c)) (at level 80, right associativity) : monad_scope.
Notation "'do' ( a , b ) ← e ; c" := (bind e (fun x => let (a,b) := x in c)) (at level 80, right associativity) : monad_scope.
End MonadNotations.
Import MonadNotations.
Open Scope monad_scope.
(** ** Functor, Applicative, and Monad Laws *)
Class Functor_Correct (f : Type -> Type) `{F : Functor f} :=
{ fmap_id : forall {A}, fmap (fun (x:A)=> x) = (fun x => x);
fmap_compose : forall {A B C} (g : A -> B) (f : B -> C),
fmap (f ∘ g) = fmap f ∘ fmap g
}.
Class Applicative_Correct (f : Type -> Type) `{Applicative f} :=
{ applicative_id : forall A, liftA (pure (fun (x:A) => x)) = (fun x => x);
applicative_composition : forall {A B C} (u : f (B -> C)) (v : f (A -> B)) (w : f A),
pure (fun x => fun y => x ∘ y) <*> u <*> v <*> w = u <*> (v <*> w);
applicative_homomorphism : forall {A B} (f : A -> B) (x : A),
pure f <*> pure x = pure (f x);
applicative_interchange : forall {A B} (u : f (A -> B)) (y : A),
u <*> pure y = pure (fun x => x y) <*> u
}.
Class Monad_Correct (m : Type -> Type) `{M : Monad m} := {
bind_right_unit: forall A (a: m A), a = a >>= return_;
bind_left_unit: forall A (a: A) B (f: A -> m B),
f a = return_ a >>= f;
bind_associativity: forall A (ma: m A) B f C (g: B -> m C),
bind ma (fun x=> f x >>= g) = (ma >>= f) >>= g
}.
Arguments Functor f : assert.
Arguments Functor_Correct f {F}.
Arguments Applicative f [F].
Arguments Applicative_Correct f {F} {A} : rename.
Arguments Monad m [F] [M].
Arguments Monad_Correct m [F] [A] [M] : rename.
(** ** Helper functions *)
Section monadic_functions.
Context (m : Type -> Type)
`{Functor m}
`{Applicative m}
`{Monad m}.
Definition wbind {A: Type} (ma: m A) {B: Type} (mb: m B) :=
ma >>= fun _=>mb.
Definition liftM {A B: Type} (f: A->B) (ma: m A): m B :=
ma >>= (fun a => return_ (f a)).
Definition join {A: Type} (mma: m (m A)): m A :=
mma >>= (fun ma => ma).
Fixpoint foldM {A B}
(f : B -> A -> m B) (b : B) (ls : list A) : m B :=
match ls with
| nil => return_ b
| x :: ls' => do y ← f b x;
foldM f y ls'
end.
End monadic_functions.
Section monadic_proofs.
Lemma fmap_compose' {f} (F : Functor f) `{Functor_Correct f} :
forall {A B C} (g : A -> B) (h : B -> C) (a : f A),
fmap h (fmap g a) = fmap (h ∘ g) a.
Proof.
intros.
rewrite (fmap_compose g h).
reflexivity.
Qed.
Lemma bind_eq : forall {A B m} `{Monad m} (a a' : m A) (f f' : A -> m B),
a = a' ->
(forall x, f x = f' x) ->
bind a f = bind a' f'.
Proof.
intros. subst.
f_equal.
apply functional_extensionality.
auto.
Qed.
End monadic_proofs.
(** ** Tactics *)
Module Tactics.
Hint Transparent return_.
(** Reduce by monad laws inside of a goal *)
Ltac simplify_monad :=
repeat match goal with
| [ |- context[ Some ?e ] ] =>
let tp := type of e in
replace (Some e) with (return_ e : option tp)
by reflexivity
| [ |- context[ bind (return_ _) _ ] ] => rewrite <- bind_left_unit
| [ |- context[ bind (bind _ _) _ ] ] => rewrite <- bind_associativity
| [ |- _ = _ ] => reflexivity
| [ |- bind ?a ?f = _ ] => erewrite (bind_eq a);
[ reflexivity | intro; simplify_monad | ]
end.
Ltac simplify_under_monad :=
repeat (try match goal with
[ |- bind ?a _ = bind ?a _ ] => apply bind_eq; [ reflexivity | intros ]
end; simplify_monad).
End Tactics.
(** * Some classic monads *)
Module Instances.
(** ** The list monad *)
Open Scope list_scope.
Definition list_fmap {A B : Type} := @map A B.
Definition list_liftA {A B} (fs : list (A -> B)) (xs : list A) : list B :=
let g := fun a => list_fmap (fun f => f a) fs
in concat (list_fmap g xs).
Fixpoint list_bind {A} (xs : list A) {B} (f : A -> list B) : list B :=
match xs with
| nil => nil
| a :: xs' => f a ++ list_bind xs' f
end.
Instance listF : Functor list := { fmap := @list_fmap }.
Instance listA : Applicative list := { pure := fun _ x => x :: nil
; liftA := @list_liftA }.
Instance listM : Monad list :=
{ bind := @list_bind }.
Instance listF_correct : Functor_Correct list.
Proof.
constructor.
* intros. simpl. apply functional_extensionality; intros x.
induction x; simpl; auto.
rewrite IHx; auto.
* intros. simpl. apply functional_extensionality; intros x.
induction x; simpl; auto.
rewrite IHx.
auto.
Qed.
Instance listA_correct : Applicative_Correct list.
Proof.
constructor.
* intros. simpl. apply functional_extensionality; intros l.
induction l; simpl; auto.
unfold list_liftA in *. simpl in *.
rewrite IHl; easy.
Abort.
Instance listM_correct : Monad_Correct list.
Abort.
Lemma fmap_app : forall {A B} (f : A -> B) ls1 ls2,
fmap f (ls1 ++ ls2) = fmap f ls1 ++ fmap f ls2.
Proof.
induction ls1; intros; simpl; auto.
rewrite IHls1. auto.
Qed.
(** ** The option monad *)
Definition option_fmap {A B} (f : A -> B) (x : option A) : option B :=
match x with
| None => None
| Some a => Some (f a)
end.
Definition option_liftA {A B} (f : option (A -> B)) (x : option A) : option B :=
match f, x with
| Some f', Some a => Some (f' a)
| _, _ => None
end.
Instance optionF : Functor option := { fmap := @option_fmap}.
Instance optionA : Applicative option := { pure := @Some;
liftA := @option_liftA}.
Instance optionM : Monad option :=
{ bind := fun A m B f => match m with None => None | Some a => f a end
}.
Instance optionF_Laws : Functor_Correct option.
Proof. split.
- intros A.
apply functional_extensionality; intros op.
destruct op; reflexivity.
- intros A B C g f.
apply functional_extensionality; intros op.
destruct op; reflexivity.
Defined.
Instance optionA_Laws : Applicative_Correct option.
Proof. split.
- intros A.
apply functional_extensionality; intros op.
destruct op; reflexivity.
- intros A B C [f|] [g|] [a|]; reflexivity.
- intros A B f x. reflexivity.
- intros A B [f|] a; reflexivity.
Defined.
Instance optionM_Laws : Monad_Correct option.
Proof. split.
- destruct a; auto.
- intros; auto.
- destruct ma; intros; auto.
Defined.
(** ** Option monad transformer *)
Definition optionT m (A : Type) : Type := m (option A).
Definition optionT_liftT {m} `{Monad m} {A} (x : m A) : optionT m A.
Proof.
unfold optionT.
refine (do a ← x; return_ (Some a)).
Defined.
Instance optionT_T : MonadTrans optionT := {liftT := @optionT_liftT}.
Definition optionT_fmap {f} `{Functor f}
{A B} (g : A -> B) (x : optionT f A) : optionT f B :=
@fmap f _ _ _ (fmap g) x.
Definition optionT_liftA {f} `{Applicative f}
{A B} (g : optionT f (A -> B)) (x : optionT f A)
: optionT f B.
(* @liftA f _ _ _ _ (fmap liftA g) x.*)
Proof.
unfold optionT in *.
exact (fmap liftA g <*> x).
Defined.
Definition optionT_pure {f} `{Applicative f}
{A} (a : A) : optionT f A := @pure f _ _ _ (pure a).
Definition optionT_bind {m} `{Monad m}
{A} (ma : optionT m A) {B} (f : A -> optionT m B)
: optionT m B.
unfold optionT in *.
exact (do oa ← ma;
match oa with
| None => pure None
| Some a => f a
end
).
Defined.
Instance optionT_F {f} `{Functor f} : Functor (optionT f) :=
{fmap := @optionT_fmap f _}.
Instance optionT_A {f} `{Applicative f} : Applicative (optionT f) :=
{ pure := @optionT_pure f _ _;
liftA := @optionT_liftA f _ _ }.
Instance optionT_M {m} `{Monad m} : Monad (optionT m) :=
{ bind := @optionT_bind m _ _ _ }.
(** ** The reader monad *)
Axiom Eta: forall A (B: A -> Type) (f: forall a, B a), f = fun a=>f a.
Definition Reader (E : Type) := fun X => E -> X.
Definition reader_fmap E A B (f : A -> B) (r : Reader E A) : Reader E B :=
fun x => f (r x).
Definition reader_liftA E A B (f : Reader E (A -> B)) (r : Reader E A) :=
fun x => (f x) (r x).
Definition reader_bind E A (r : Reader E A) B (f : A -> Reader E B) : Reader E B :=
fun x => f (r x) x.
Instance readerF E : Functor (Reader E) :=
{ fmap := @reader_fmap E }.
Instance readerA E : Applicative (Reader E) :=
{ pure := fun A (a:A) e=> a;
liftA := @reader_liftA E }.
Instance readerM (E : Type): Monad (Reader E) :=
{ bind := @reader_bind E }.
(** ** The state monad *)
Section State.
Variable S : Type.
Definition State (A : Type) := S -> A * S.
Definition state_fmap A B (f : A -> B) (st : State A) : State B :=
fun s => let (a,s) := st s in (f a,s).
Definition state_liftA A B (st_f : State (A -> B)) (st_a : State A) :=
fun s => let (f,s) := st_f s in
let (a,s) := st_a s in
(f a,s).
Definition state_bind A (st_a : State A) B (f : A -> State B) :=
fun s => let (a,s) := st_a s in
f a s.
Definition put (x : S) : State () :=
fun _ => (tt,x).
Definition get : State S :=
fun x => (x,x).
Definition runState {A} (op : State A) : S -> A * S := op.
Definition evalState {A} (op : State A) : S -> A := fst ∘ op.
Definition execState {A} (op : State A) : S -> S := snd ∘ op.
End State.
Ltac fold_evalState :=
match goal with
| [ |- context[fst (?c ?v)] ] => replace (fst (c v)) with (evalState c v)
by reflexivity
end.
Arguments get {S}.
Arguments put {S}.
Instance stateF {A} : Functor (State A) :=
{ fmap := @state_fmap A }.
Instance stateA {A} : Applicative (State A) :=
{ pure := fun A a s=> (a,s);
liftA := @state_liftA A }.
Instance stateM {A} : Monad (State A) :=
{ bind := @state_bind A }.
Instance stateF_correct {A} : Functor_Correct (State A).
Proof.
split; intros;
apply functional_extensionality; intros op;
apply functional_extensionality; intros x;
simpl; unfold state_fmap.
- destruct (op x); reflexivity.
- destruct (op x); reflexivity.
Qed.
Instance stateA_correct {A} : Applicative_Correct (State A).
Proof.
split; intros;
apply functional_extensionality; intros op;
simpl; unfold state_liftA.
- apply functional_extensionality; intros x.
destruct (op x); reflexivity.
- destruct (u op).
destruct (v a).
destruct (w a0).
reflexivity.
- reflexivity.
- destruct (u op).
reflexivity.
Qed.
Instance stateM_correct {A} : Monad_Correct (State A).
Proof.
split; intros; simpl; unfold state_bind.
- apply functional_extensionality; intros x.
destruct (a x); reflexivity.
- reflexivity.
- apply functional_extensionality; intros x.
destruct (ma x).
reflexivity.
Qed.
(** ** The Ensemble monad *)
Instance ensemble_F : Functor Ensemble.
Proof.
split; intros A B f X.
intros b.
exact (exists (a : A), In _ X a /\ f a = b).
Defined.
Instance ensemble_A : Applicative Ensemble.
Proof.
split.
* intros A a. exact (Singleton _ a).
* intros A B F X b.
exact (exists f, F f /\ exists a, X a /\ f a = b).
Defined.
Instance ensemble_M : Monad Ensemble.
Proof.
split.
intros A X B f.
intros b.
exact (exists a, X a /\ f a b).
Defined.
(*
Ltac solve_ensemble_law :=
repeat match goal with
| [ |- forall x, _] => intros
| [ |- ?A -> ?B ] => intros
| [ |- ?A <-> ?B ] => split; simpl
| [ H : In _ _ _ |- _ ] => unfold In in H
| [ H : exists x, _ |- _] => destruct H; subst
| [ H : _ /\ _ |- _] => destruct H; subst
| [ H : Singleton _ _ _ |- _ ] => inversion H; subst; clear H
| [ |- _ = _] => apply functional_extensionality
(* irreversible *)
(* this admit here is because the monad laws assert the results should be equal, but we only know that the propositions are equivalent *)
| [ |- ?P = ?Q ] => assert (P <-> Q); [ | admit]
end;
repeat (try eexists; try split); eauto.
Instance ensemble_F_correct : Functor_Correct Ensemble.
Proof.
split.
* solve_ensemble_law.
* solve_ensemble_law.
Admitted.
Instance ensemble_A_correct : Applicative_Correct Ensemble.
Proof.
split.
* solve_ensemble_law.
* solve_ensemble_law.
* solve_ensemble_law.
* solve_ensemble_law.
Admitted.
Instance ensemble_M_correct : Monad_Correct Ensemble.
Proof.
split.
* solve_ensemble_law.
* solve_ensemble_law.
* solve_ensemble_law.
Admitted.
*)
End Instances.