-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTErrorMonad.v
269 lines (212 loc) · 7.9 KB
/
TErrorMonad.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
Require Import Ascii String Decidable HoTT PartialOrder.
Require Import ExtrOcamlString.
Set Universe Polymorphism.
Set Implicit Arguments.
(** * The [TError] monad *)
(** ** Datatype *)
(* =_Cast= *)
Inductive TError A (info: HProp) :=
| Some: A -> TError A info
| Fail: info -> TError A info.
(* =end= *)
Arguments Some {_ _} _.
Arguments Fail {_ _} _.
(** We need to assume that the string message is irrelevant. We would
not need prositional truncation as an axiom if we were using the
simpler version with [None] (and no [info]). *)
(* =Cast= *)
Definition info_str := hprop (Trunc string).
(* =end= *)
Unset Implicit Arguments.
Notation "A ⇀ B" := (A -> TError B info_str) (at level 50).
(** ** Transport maps *)
Definition transport_TError (A : Type) (B : A -> Type) {info} a a' (e:a=a') x:
transport (fun a => TError (B a) info) e (Some x) = Some (e # x).
destruct e. reflexivity.
Defined.
Definition transport_TError_Fail (A : Type) (B : A -> Type) a a' (e:a=a') {info} i:
transport (fun a => TError (B a) info) e (Fail i) = Fail i.
destruct e. reflexivity.
Defined.
Definition path_TError {A} {info} {x y : TError A info}
(xy : match x,y with
Some x0, Some y0 => x0 = y0
| Fail _ , Fail _ => True
| _, _ => False end) : x = y.
Proof.
destruct x, y. all: try apply ap, xy.
all: try elim xy. apply ap.
apply is_hprop.
Defined.
Definition path_TError_inv {A} {info} {x y : TError A info} :
x = y ->
match x,y with
Some x0, Some y0 => x0 = y0
| Fail _ , Fail _ => True
| _, _ => False end.
Proof.
destruct 1, x; reflexivity.
Defined.
Definition Some_inj {A} {info} {x y : A} : @Some _ info x = @Some _ info y -> x = y :=
fun e => path_TError_inv e.
Definition Some_inj_sect {A} {info} {x y :A} (e:x=y) : Some_inj (ap (@Some _ info) e) = e.
Proof.
destruct e; reflexivity.
Defined.
Definition Some_inj_retr {A} info {x y : TError A info} (e: x = y) :
path_TError (path_TError_inv e) = e.
Proof.
destruct e, x as [x|i]. reflexivity.
simpl. assert (is_hprop i i = eq_refl).
etransitivity. symmetry. apply contr. apply contr.
refine (transport (fun X => ap _ X =_ ) H^ _). reflexivity.
Defined.
Definition Some_inj_eq {A} {x y :A} info (e:Some x= Some y) : ap (@Some _ _) (Some_inj e) = e :=
Some_inj_retr info e.
(** ** Monadic operations *)
(* =creturn= *)
Definition creturn {A} : A ⇀ A := Some.
(* =end= *)
(* =clift= *)
Definition clift A B: (A -> B) -> (A ⇀ B) :=
fun f a => creturn (f a).
(* =end= *)
Arguments clift {_ _} _ _.
(* =cbind= *)
Definition cbind A B {info}:
TError A info -> (A -> TError B info) -> TError B info :=
fun a f => match a with
Some a => f a
| Fail i => Fail i
end.
Notation "x <- e1 ; e2" := (cbind _ _ e1 (fun x => e2))
(* =end= *)
(right associativity, at level 60).
Notation "x >>= f" := (cbind _ _ x f) (at level 50).
(* =kleisliComp= *)
Definition kleisliComp {A B C: Type}: (A ⇀ B) -> (B ⇀ C) -> (A ⇀ C) :=
fun f g a => b <- f a ; g b.
Notation "g °° f" := (kleisliComp f g)
(* =end= *)
(at level 1).
Definition cbind_Some {A B} x (f:A ⇀ B) b :
x >>= f = Some b -> {a:A & ((x = Some a) * (f a = Some b))%type}.
intro e. destruct x.
- exists a. split. reflexivity. exact e.
- simpl in e. inversion e.
Defined.
Definition cbind_Some' {A B} x (f:A ⇀ B) b :
{a:A & ((x = Some a) * (f a = Some b))%type} ->
x >>= f = Some b.
intros [a (Hx, Hfa)]. unfold cbind. destruct x.
- inversion Hx. exact Hfa.
- inversion Hx.
Defined.
Lemma cbind_right_id {A} (x : TError A _):
(x >>= creturn) = x.
destruct x; reflexivity.
Defined.
Lemma cbind_assoc {A B C} x (f: A ⇀ B) (g: B ⇀ C):
(x >>= f) >>= g = x >>= (fun x => f x >>= g).
destruct x; reflexivity.
Defined.
(** ** Extraction *)
Extraction Language Ocaml.
(* =extract_cast= *)
Extract Inductive TError =>
(* Transparent extraction of TError:
- if Some t, then extract plain t
- if Fail, then fail with a runtime coercion exception *)
"" [ "" "(let f s = failwith
(String.concat """" ([""Coercion failure: ""]@
(List.map (String.make 1) s))) in f)" ]
"(let new_pattern some none = some in new_pattern)".
(* =end= *)
(** ** Example *)
Example coercion_some : unit ⇀ bool := fun _ => Some true.
Definition _with (s:string) : info_str := (tr s).
(* =cast_bool= *)
Example err: TError bool info_str := Fail (_with "coercion to bool").
(* =end= *)
Example use_bool (cb: TError bool info_str) := cb >>= (fun b => creturn (andb b b)).
Example use_some := fun x => use_bool (coercion_some x).
Example use_fail := fun x:unit => use_bool err.
Extraction "test_coercion_extr.ml" use_some use_fail.
(* Expected result: it should compile (first..) and then trigger a [failwith] *)
(** ** Decidable instances *)
Definition Decidable_eq_TError : forall A (HA: DecidablePaths A)
(x y: TError A info_str), (x = y) + not (x = y).
intros. destruct x as [a |]; destruct y as [a0 |].
- case (dec_paths a a0); intro H.
+ left. exact (ap _ H).
+ right. intro Hc. exact (H (Some_inj Hc)).
- right. unfold not. intro Hc. inversion Hc.
- right. unfold not. intro Hc. inversion Hc.
- left. apply ap. apply is_hprop.
Defined.
Instance IsHSet_TError (A:HSet) HA : IsHSet (TError A info_str) := Hedberg (@Decidable_eq_TError A HA).
Instance DecidablePaths_TError :
forall A (HA: DecidablePaths A), DecidablePaths (hset (TError A info_str)) :=
{ dec_paths := Decidable_eq_TError A HA }.
(** ** Preorder instance *)
(** We define a notion of preorder on [TError A], for any [A]. The least
element is bottom while successful computations are compared by
equality. *)
(* =preordercast= *)
Instance IsPartialOrderTError (A: HSet): IsPartialOrder_pp (TError A info_str) :=
{| rel := fun a a' => @hprop (match a with
| Some _ => a = a'
| Fail _ => True
end) _;
bot := Fail (_with "bot") |}.
(* =end= *)
Proof.
- intros e e'. cbn in *. destruct a as [a|], a' as [a'|]; cbn in *.
+ assert (path_TError_inv e = path_TError_inv e') by apply is_hprop.
apply (ap (@path_TError A info_str (Some a) (Some a'))) in H.
repeat rewrite Some_inj_retr in H. assumption.
+ inversion e.
+ destruct e, e'. reflexivity.
+ destruct e, e'. reflexivity.
- destruct x. reflexivity. exact I.
- intros x y z. destruct x, y; intros e e'; auto.
exact (e @ e').
discriminate.
- intros x y H H'. destruct x, y; eauto.
apply ap. apply is_hprop.
- intro; exact I.
Defined.
(** To avoid failure of evaluation of bot in ocaml code *)
Extract Constant IsPartialOrderTError => "0".
Instance TError_HSet A `{IsHSet A} : IsHSet (TError A info_str).
intros.
intros a b e e'.
destruct a as [a|]; destruct b as [b|];
try now inversion e.
+ pose proof (Some_inj_eq info_str e)^.
pose proof (Some_inj_eq info_str e').
etransitivity; eauto.
etransitivity; [| eauto].
destruct (is_hset a b (Some_inj e) (Some_inj e')).
reflexivity.
+ pose proof (Some_inj_retr info_str e).
pose proof (Some_inj_retr info_str e').
etransitivity; [symmetry; eauto|].
etransitivity; [|eauto].
assert (e'': path_TError_inv e = path_TError_inv e') by apply is_hprop.
destruct e''. reflexivity.
Defined.
Definition TError_HSet_preorder (A:HSet) : forall (x y : TError A info_str) (e e' : x ≼ y), e = e'.
Proof.
intros x y e e'; destruct x as [x|], y as [y|]; simpl in *.
apply TError_HSet; typeclasses eauto.
inversion e.
destruct e, e'; reflexivity.
destruct e, e'; reflexivity.
Defined.
Definition transport_bind A (x y : A) (e:y=x) B (f : A -> B) :
transport (λ X : TError A info_str, Some (f x) = b <- X; clift f b)
(ap creturn e)^ eq_refl
= ap creturn (ap f e^).
destruct e. reflexivity.
Defined.