-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateMonadDevel.v
461 lines (348 loc) · 11.9 KB
/
StateMonadDevel.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
458
459
460
461
(*
TODO:
+ add put and get
+ add >>
+ convert to use >>= and >> notations
+ create StateMonad class by extending monad class
*)
Require Import FunctionalExtensionality.
Require Import Arith.
Require Import Monad.
(** First attempt that simply copies the PVS implementation. The datatype
is not parameterized, but [A] and [S] are specified as a uninterpretted
types. *)
Module definition1.
Definition A := nat.
Definition S := nat.
(** [State] is an algebraic type that encapsulates [state] as done in Haskell
and PVS *)
Inductive State :=
| state : (S -> A * S) -> State.
(** [unit :: A -> M A] has the classical definition of lifting a state into
the monad and waiting for an input *)
Definition unit(x:A):State := state(fun (s:S) => (x,s)).
(** [bind :: M A -> (A -> M B) -> M B] has the classical definition of taking
a monad, running it, and feeding the result into the next monadic expression
in sequence. *)
Definition bind(m:State)(f:A -> State):State :=
(state (fun (s0:S) =>
match m with
| state m' =>
match (m' s0) with
| (a,s1) =>
match (f a) with
| state m'' => (m'' s1)
end
end
end)).
Example unit_ex1 : (unit 3) = (unit 3).
Proof.
reflexivity.
Qed.
End definition1.
(** Second attempt that removes the constructed type in favor of a simple
type definition. Coq supports general functions over types. Thus, we
redefine [State] directly as the type [S -> A * S] *)
Module definition2.
(** Still use uninterpretted types for [A] and [S] *)
Definition A := nat.
Definition S := nat.
(** [State] is a type that defines the state monad in the classical fashion
without using a constructed type *)
Definition State := S -> A * S.
(** Definitions of [unit] and [bind] follow directly from [State]. The
[sequence] operation is added in this definition *)
Definition unit(x:A):State := (fun (s:S) => (x,s)).
Definition bind(m:State)(f:A -> State):State :=
(fun (s0:S) =>
match (m s0) with
| (a,s1) => (f a) s1
end).
Definition sequence(m:State)(n:State):State :=
(fun (s0:S) =>
match (m s0) with
| (a,s1) => n s1
end).
Example unit_ex1 : unit(0)(1) = (0,1).
Proof.
unfold unit.
reflexivity.
Qed.
Example bind_ex1 : ((bind (unit 0) (fun (a:A) => (fun (s:S) => (a,(s+1))))) 0) = (0,1).
Proof.
unfold bind. reflexivity.
Qed.
Example bind_ex2 : ((bind
(bind
(unit 0) (fun (a:A) => (fun (s:S) => (0,(s+1)))))
(fun (a:A) => (fun (s:S) => (0,(s+1))))) 0) = (0,2).
Proof.
unfold bind. reflexivity.
Qed.
(** Add the standard notations for [bind] and [sequence]. These definitions
will clash with those in the [Monad] module, but it's okay. Easier to see
what's going on if I leave these notation definitions here.*)
Notation "m >>= f" :=
(bind m f) (left associativity, at level 49).
Notation "m >> f" :=
(sequence m f) (left associativity, at level 49).
Example bind_ex1' : ((unit 0) >>= (fun (a:A) => (fun (s:S) => (a,(s+1))))) 0 = (0,1).
Proof.
unfold bind. reflexivity.
Qed.
Example bind_ex2' : (((unit 0) >>= (fun (a:A) => (fun (s:S) => (0,(s+1)))))
>>= (fun (a:A) => (fun (s:S) => (0,(s+1))))) 0
= (0,2).
Proof.
unfold bind. reflexivity.
Qed.
(** Not at all this notation is useful given that it appears to sequence only
two operations and does not support sequence. *)
Notation "'do' a <- e ; c" :=
(e >>= (fun a => c)) (at level 59, right associativity).
Example bind_ex1'' : (do a <- (unit 0) ; (fun (s:S) => (0,(s+1)))) 0 = (0,1).
Proof.
unfold bind. reflexivity.
Qed.
End definition2.
(** Third attempt that parameterizes [State] over types. Instead of using
uninterpreted types for [S] and [A] the are now parameters to the [State]
type. *)
Module definition3.
Definition State (S A:Type) := S -> A * S.
(** [unit] and [bind] change only in that [A] and [S] are now inferred types
rather than explicitly defined. *)
Definition unit{S A:Type}(x:A):(State S A) := (fun (s:S) => (x,s)).
Definition bind{S A:Type}(m:(State S A))(f:A -> (State S A)):(State S A) :=
(fun (s0:S) =>
match (m s0) with
| (a,s1) => (f a) s1
end).
Definition sequence{S A:Type}(m:(State S A))(n:(State S A)):(State S A) :=
(fun (s0:S) =>
match (m s0) with
| (a,s1) => n s1
end).
(** [put] and [get] are defined for all state monads in addition to [unit]
and [bind]. Still need to find the laws for these operations. *)
Definition put{S A:Type}(a:A)(s1:S):(State S A) := (fun (s0:S) => (a,s1)).
Eval compute in unit 0 1.
Example unit_ex1 : unit(0)(1) = (0,1).
Proof.
unfold unit.
reflexivity.
Qed.
Eval compute in ((bind (unit 0) (fun a => (fun s => (a,(s+1))))) 0).
Example bind_ex1: ((bind (unit 0) (fun a => (fun s => (a,(s+1))))) 0) = (0,1).
Proof.
unfold bind. reflexivity.
Qed.
Eval compute in ((bind
(bind
(unit 0) (fun a => (fun s => (0,(s+1)))))
(fun a => (fun s => (0,(s+1))))) 0).
Example bind_ex2 : ((bind
(bind
(unit 0) (fun a => (fun s => (0,(s+1)))))
(fun a => (fun s => (0,(s+1))))) 0) = (0,2).
Proof.
unfold bind. reflexivity.
Qed.
Eval compute in (put 0 1).
Eval compute in ((bind (unit 0) (fun a => (put 0 1))) 50).
Example bind_put : ((bind (unit 0) (fun a => (put 0 1))) 50) = (0,1).
Proof.
unfold bind, unit, put. reflexivity.
Qed.
Print bind_put.
(** Prove the three basic monad laws - left unit, right unit and associativity.
None of these proofs is particularly challenging.
Proofs should be parameterized over both [A] and [B] allowing
[f:A->State S B] to change the output type.
Something is fouling that up. Will come back to it later. Right now
[State] and its functions are a monoid and not a monad. Specifically, the
associative operator, [bind] is closed.
*)
Theorem left_unit :
forall {S A} (a:A) (f:A -> (State S A)), bind (unit a) f = f a.
Proof.
intros. unfold bind. reflexivity.
Qed.
Theorem right_unit : forall {S A} (ma:(State S A)), bind ma unit = ma.
Proof.
intros. unfold bind. extensionality x.
unfold unit.
destruct (ma x) as (a,s1).
reflexivity.
Qed.
Theorem assoc :
forall {S A} (ma:(State S A)) (f:A -> (State S A)) (g:A -> (State S A)),
bind (bind ma f) g = bind ma (fun a => bind (f a) g).
Proof.
intros. unfold bind. extensionality x.
destruct (ma x) as (a,s1).
reflexivity.
Qed.
Eval compute in unit 0 1.
End definition3.
Module definition4.
(** Define the [StateMonad] as a typeclass rather than a single type. This
will enable defining things to be state monads rather than defining a single
monad.*)
Definition State (S A:Type) := S -> A * S.
(** Define a [StateMonad] as an instance of [Monad]. Note that this includes
only the definitions of [unit] and [bind]. [put] and [get] are defined later
outside the typeclass. This is not optimal, but we'll fix it later *)
Instance StateMonad (S:Type) : Monad (State S) :=
{
unit A x := (fun s => (x,s))
; bind A B m f := (fun s0 =>
match (m s0) with
| (a,s1) => (f a) s1
end)
; sequence A B m1 m2 := (fun s0 =>
match (m1 s0) with
| (a,s1) => m2 s1
end)
}.
Proof.
intros. extensionality x. reflexivity.
intros. extensionality x. destruct (ma x) as (a,s1). reflexivity.
intros. extensionality x. destruct (ma x) as (a,s1). reflexivity.
Defined.
Example unit_ex1 : ((unit 0) 1) = (0,1).
Proof.
unfold unit.
simpl.
reflexivity.
Qed.
Definition incState:(State nat nat) := (fun s => (0, (s+1))).
Eval compute in ((unit 2) >>= (fun a => incState)) 0.
Eval compute in ((unit 2) >> incState) 0.
Example bind_ex1: ((unit 0) >>= (fun a => incState)) 0 = (0,1).
Proof.
unfold bind. reflexivity.
Qed.
Example sequence_ex1: ((unit 0) >> incState) 0 = (0,1).
Proof.
unfold sequence. reflexivity.
Qed.
Example bind_ex2 :
((unit 0) >>= (fun a => incState) >>= (fun a => incState)) 0 = (0,2).
Proof.
unfold bind. reflexivity.
Qed.
Example sequence_ex2 : ((unit 0) >> incState >> incState) 0 = (0,2).
Proof.
unfold bind. reflexivity.
Qed.
Definition addInput:(nat -> (State nat nat)) :=
(fun a => (fun s => (a,(a+s)))).
Example bind_ex3 :
((unit 1) >>= addInput >>= addInput) 0 = (1,2).
Proof.
unfold bind. reflexivity.
Qed.
Definition put{S A:Type}(s:S)(a:A):(State S A) := (fun (_:S) => (a,s)).
Example put_ex1 : ((unit 1) >>= (put 10)) 0 = (1,10).
Proof.
unfold bind. simpl. unfold put. reflexivity.
Qed.
Definition get{S:Type}(a:S):(State S S) := (fun (s:S) => (s,s)).
Example get_ex1 : ((unit 0) >>= get) 10 = (10,10).
Proof.
unfold bind. simpl. unfold get. reflexivity.
Qed.
End definition4.
Module definition5.
(** Try the same approach extending the [Monad] typeclass to implement a
[StateMonad] typeclass. [M] is the monad constructor. *)
Definition State (S A:Type) := S -> A * S.
Class StateMonad {S A:Type} (State: Type -> Type -> Type) `(Monad (State S)) :Type :=
{
get: A -> State S S
; put: S -> A -> State S A
}.
Print StateMonad.
Instance StateMonadI {S:Type} : Monad (State S) :=
{
unit A x := (fun s => (x,s))
; bind A B m f := (fun s0 =>
match (m s0) with
| (a,s1) => (f a) s1
end)
; sequence A B m1 m2 := (fun s0 =>
match (m1 s0) with
| (a,s1) => m2 s1
end)
}.
Proof.
intros. extensionality x. reflexivity.
intros. extensionality x. destruct (ma x) as (a,s1). reflexivity.
intros. extensionality x. destruct (ma x) as (a,s1). reflexivity.
Defined.
Print StateMonadI.
Definition putState{S A:Type}(s:S)(a:A):(State S A) := (fun (_:S) => (a,s)).
Instance StateMonadEx {S A:Type} : StateMonad State StateMonadI :=
{
(* put := (fun (s:S) => (fun (a:A) => (putState s a))) *)
put := (fun (s:S) => (fun (a:A) => (fun (_:S) => (a,s))))
; get := (fun (a:A) => (fun (s:S) => (s,s)))
}.
Print StateMonadEx.
Example unit_ex1 : ((unit 0) 1) = (0,1).
Proof.
unfold unit.
simpl.
reflexivity.
Qed.
Definition incState:(State nat nat) := (fun s => (0, (s+1))).
Eval compute in ((unit 2) >>= (fun a => incState)) 0.
Eval compute in ((unit 2) >> incState) 0.
Example bind_ex1: ((unit 0) >>= (fun a => incState)) 0 = (0,1).
Proof.
unfold bind. reflexivity.
Qed.
Example sequence_ex1: ((unit 0) >> incState) 0 = (0,1).
Proof.
unfold sequence. reflexivity.
Qed.
Example bind_ex2 :
((unit 0) >>= (fun a => incState) >>= (fun a => incState)) 0 = (0,2).
Proof.
unfold bind. reflexivity.
Qed.
Example sequence_ex2 : ((unit 0) >> incState >> incState) 0 = (0,2).
Proof.
unfold bind. reflexivity.
Qed.
Definition addInput:(nat -> (State nat nat)) :=
(fun a => (fun s => (a,(a+s)))).
Example bind_ex3 :
((unit 1) >>= addInput >>= addInput) 0 = (1,2).
Proof.
unfold bind. reflexivity.
Qed.
(* Definition put{S A:Type}(s:S)(a:A):(State S A) := (fun (_:S) => (a,s)). *)
Check (put 0 10).
Eval compute in (fun (a:nat) => (fun (s:nat) => (0,10)):(State nat nat)).
Eval compute in (((unit 1):(State nat nat)) >>= ((fun (a:nat) => (put 0 10):(State nat nat)))) 10.
Example put_ex2: (((unit 1):(State nat nat)) >>= ((fun (a:nat) => (fun (s:nat) => (0,10)):(State nat nat)))) 10 = (0,10).
Proof.
unfold unit, bind.
trivial.
Qed.
Eval compute in ((((unit 1) >>= (put 10))) 8).
Example put_ex1 : ((((unit 1) >>= (put 10)):(State nat nat)) 8) = (1,10).
Proof.
unfold bind. simpl. unfold put. reflexivity.
Qed.
(* Definition get{S:Type}(a:S):(State S S) := (fun (s:S) => (s,s)). *)
Eval compute in ((unit 0) >>= get).
Check ((unit 0) >>= get).
Eval compute in ((unit 0) >>= get) 10.
Example get_ex1 : ((unit 0) >>= get) 10 = (10,10).
Proof.
unfold bind. simpl. unfold get. reflexivity.
Qed.
End definition5.