-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImpMonads.v
263 lines (185 loc) · 7.42 KB
/
ImpMonads.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
(** Impure monad for interface with impure code
*)
Module Type MayReturnMonad.
(** The type of impure computations *)
Axiom t: Type -> Type.
(** The may-return relation *)
Axiom mayRet: forall {A:Type}, t A -> A -> Prop.
(** Standard monad operator: lift pure computation *)
Axiom ret: forall {A}, A -> t A.
(** Standard monad operator: bind two impure computations *)
Axiom bind: forall {A B}, (t A) -> (A -> t B) -> t B.
(** Axioms of monad operators wrt to may-return monad *)
Axiom mayRet_ret: forall A (a b:A),
mayRet (ret a) b -> a=b.
Axiom mayRet_bind: forall A B k1 k2 (b:B),
mayRet (bind k1 k2) b -> exists a:A, mayRet k1 a /\ mayRet (k2 a) b.
(** Operator to annotate returned types with may-return relation.
This overcomes the non-dependent [bind] application within dependent types.
*)
Axiom mk_annot: forall {A} (k: t A), t { a: A | mayRet k a }.
(** Exiting the monad for observationally deterministic computations (within partial correctness) *)
Axiom det_coerce: forall {A} (k: t A) (v: unit -> A) (DET: forall r, mayRet k r -> r = v tt), A.
Axiom det_coerce_correct: forall A (k: t A) v (DET: forall r, mayRet k r -> r = v tt), (det_coerce k v DET)=(v tt).
(** Exiting the monad on termination *)
Axiom has_returned: forall {A}, t A -> bool.
Axiom has_returned_correct: forall A (k: t A),
has_returned k = true -> exists r, mayRet k r.
End MayReturnMonad.
(** Model of impure computation as predicate *)
Module PowerSetMonad<: MayReturnMonad.
Definition t (A:Type) := A -> Prop.
Definition mayRet {A:Type} (k: t A) a: Prop := k a.
Definition ret {A:Type} (a:A) := eq a.
Definition bind {A B:Type} (k1: t A) (k2: A -> t B) :=
fun b => exists a, k1 a /\ k2 a b.
Definition mk_annot {A} (k: t A) : t { a | mayRet k a } := fun _ => True.
Lemma mayRet_ret A (a b:A): mayRet (ret a) b -> a=b.
Proof.
unfold mayRet, ret. firstorder.
Qed.
Lemma mayRet_bind A B k1 k2 (b:B):
mayRet (bind k1 k2) b -> exists (a:A), mayRet k1 a /\ mayRet (k2 a) b.
Proof.
unfold mayRet, bind.
firstorder.
Qed.
(* exiting the monad: "logical" models *)
Definition det_coerce {A} (k: t A) (v: unit -> A) (DET: forall r, mayRet k r -> r = v tt) := v tt.
Lemma det_coerce_correct: forall A (k: t A) v (DET: forall r, mayRet k r -> r = v tt), det_coerce k v DET = v tt.
Proof.
unfold det_coerce; auto.
Qed.
Definition has_returned {A} (k:t A) := false.
Lemma has_returned_correct: forall A (k: t A),
has_returned k = true -> exists r, mayRet k r.
Proof.
unfold has_returned; congruence.
Qed.
End PowerSetMonad.
(** The identity interpretation *)
Module IdentityMonad<: MayReturnMonad.
Definition t (A:Type) := A.
(* may-return semantics of computations *)
Definition mayRet {A:Type} (a b:A): Prop := a=b.
Definition ret {A:Type} (a:A) := a.
Definition bind {A B:Type} (k1: A) (k2: A -> B) := k2 k1.
Definition mk_annot {A} (k: t A) : t { a: A | mayRet k a }
:= exist _ k (eq_refl k) .
Lemma mayRet_ret (A:Type) (a b:A): mayRet (ret a) b -> a=b.
Proof.
intuition.
Qed.
Lemma mayRet_bind (A B:Type) (k1:t A) k2 (b:B):
mayRet (bind k1 k2) b -> exists (a:A), mayRet k1 a /\ mayRet (k2 a) b.
Proof.
firstorder.
Qed.
(* exiting the monad: "computational" models *)
Definition det_coerce {A} (k: t A) (v: unit -> A) (DET: forall r, mayRet k r -> r = v tt) := k.
Lemma det_coerce_correct A (k: t A) v (DET: forall r, mayRet k r -> r = v tt): det_coerce k v DET = v tt.
Proof.
eapply (DET k); refine (eq_refl _).
(* Very strange: in coq 8.16.1, "apply eq_refl" instead of "refine ..." generates a too strong universe constraint
that may later come problematic !
*)
Qed.
Definition has_returned {A} (k:t A) := true.
Lemma has_returned_correct: forall A (k: t A),
has_returned k = true -> exists r, mayRet k r.
Proof.
unfold has_returned, mayRet; eauto.
Qed.
End IdentityMonad.
Require Program.
(** Model of impure computation as state-transformers (with error-raising) *)
Module StateOptionMonad<: MayReturnMonad.
Parameter St: Type. (* A global state *)
Definition t (A:Type) := St -> (option A) * St.
Definition mayRet {A:Type} (k: t A) a: Prop :=
exists s, fst (k s)=Some a.
Definition ret {A:Type} (a:A) := fun (s:St) => (Some a,s).
Definition bind {A B:Type} (k1: t A) (k2: A -> t B) :=
fun s0 =>
let r := k1 s0 in
match fst r with
| Some a => k2 a (snd r)
| None => (None, snd r)
end.
Program Definition mk_annot {A} (k: t A) : t { a | mayRet k a } :=
fun s0 =>
let r := k s0 in
match fst r with
| Some a => (Some (exist _ a _), snd r)
| None => (None, snd r)
end.
Obligation 1.
unfold mayRet; eauto.
Qed.
Lemma mayRet_ret {A:Type} (a b:A): mayRet (ret a) b -> a=b.
Proof.
unfold mayRet, ret; simpl. firstorder congruence.
Qed.
Lemma mayRet_bind {A B:Type} k1 k2 (b:B):
mayRet (bind k1 k2) b -> exists (a:A), mayRet k1 a /\ mayRet (k2 a) b.
Proof.
unfold mayRet, bind.
intros (s & H); destruct (fst (k1 s)) eqn: H1; firstorder eauto.
simpl in *; congruence.
Qed.
(* exiting the monad: "computational" models *)
Parameter st: St. (* the global state is not empty! *)
Definition det_coerce {A} (k: t A) (v: unit -> A) (DET: forall r, mayRet k r -> r = v tt): A
:= match fst (k st) with
| Some a => a
| None => v tt
end.
Lemma det_coerce_correct: forall A (k: t A) v (DET: forall r, mayRet k r -> r = v tt), det_coerce k v DET = v tt.
Proof.
unfold det_coerce, mayRet; intros A k v DET.
destruct (fst (k st)) eqn: H1; simpl; eauto.
Qed.
Definition has_returned {A} (k:t A)
:= match fst (k st) with
| Some _ => true
| None => false
end.
Lemma has_returned_correct: forall A (k: t A),
has_returned k = true -> exists r, mayRet k r.
Proof.
unfold has_returned, mayRet; intros A k H.
destruct (fst (k st)) eqn: H1; simpl; eauto.
congruence.
Qed.
End StateOptionMonad.
(** The deferred interpretation *)
Module DeferredMonad<: MayReturnMonad.
Definition t (A:Type) := unit -> A.
(* may-return semantics of computations *)
Definition mayRet {A:Type} (a: t A) (b:A): Prop := a tt=b.
Definition ret {A:Type} (a:A) : t A := fun _ => a.
Definition bind {A B:Type} (k1: t A) (k2: A -> t B) : t B := fun _ => k2 (k1 tt) tt.
Definition mk_annot {A} (k: t A) : t { a: A | mayRet k a }
:= fun _ => exist _ (k tt) (eq_refl (k tt)).
Lemma mayRet_ret (A:Type) (a b: A): mayRet (ret a) b -> a=b.
Proof.
intuition.
Qed.
Lemma mayRet_bind (A B:Type) (k1:t A) k2 (b:B):
mayRet (bind k1 k2) b -> exists (a:A), mayRet k1 a /\ mayRet (k2 a) b.
Proof.
firstorder.
Qed.
(* exiting the monad: "computational" models *)
Definition det_coerce {A} (k: t A) (v: unit -> A) (DET: forall r, mayRet k r -> r = v tt) := k tt.
Lemma det_coerce_correct: forall A (k: t A) v (DET: forall r, mayRet k r -> r = v tt), det_coerce k v DET = v tt.
Proof.
unfold det_coerce, mayRet; intros A k v DET; eapply DET. apply eq_refl.
Qed.
Definition has_returned {A} (k:t A) := true.
Lemma has_returned_correct: forall A (k: t A),
has_returned k = true -> exists r, mayRet k r.
Proof.
unfold has_returned, mayRet; eauto.
Qed.
End DeferredMonad.