source: src/common/Errors.ma @ 1500

Last change on this file since 1500 was 1355, checked in by sacerdot, 8 years ago

monadic fold_lefti added

File size: 11.1 KB
Line 
1(* *********************************************************************)
2(*                                                                     *)
3(*              The Compcert verified compiler                         *)
4(*                                                                     *)
5(*          Xavier Leroy, INRIA Paris-Rocquencourt                     *)
6(*                                                                     *)
7(*  Copyright Institut National de Recherche en Informatique et en     *)
8(*  Automatique.  All rights reserved.  This file is distributed       *)
9(*  under the terms of the GNU General Public License as published by  *)
10(*  the Free Software Foundation, either version 2 of the License, or  *)
11(*  (at your option) any later version.  This file is also distributed *)
12(*  under the terms of the INRIA Non-Commercial License Agreement.     *)
13(*                                                                     *)
14(* *********************************************************************)
15
16include "basics/types.ma".
17include "basics/logic.ma".
18include "basics/list.ma".
19include "common/PreIdentifiers.ma".
20include "utilities/lists.ma".
21include "utilities/deppair.ma".
22
23(* * Error reporting and the error monad. *)
24
25(* * * Representation of error messages. *)
26
27(* * Compile-time errors produce an error message, represented in Coq
28  as a list of either substrings or positive numbers encoding
29  a source-level identifier (see module AST). *)
30
31inductive errcode: Type[0] :=
32  | MSG: String → errcode
33  | CTX: ∀tag:String. identifier tag → errcode.
34
35definition errmsg: Type[0] ≝ list errcode.
36
37definition msg : String → errmsg ≝ λs. [MSG s].
38
39(* * * The error monad *)
40
41(* * Compilation functions that can fail have return type [res A].
42  The return value is either [OK res] to indicate success,
43  or [Error msg] to indicate failure. *)
44
45inductive res (A: Type[0]) : Type[0] ≝
46| OK: A → res A
47| Error: errmsg → res A.
48
49(*Implicit Arguments Error [A].*)
50
51(* * To automate the propagation of errors, we use a monadic style
52  with the following [bind] operation. *)
53
54definition bind ≝ λA,B:Type[0]. λf: res A. λg: A → res B.
55  match f with
56  [ OK x ⇒ g x
57  | Error msg ⇒ Error ? msg
58  ].
59
60definition bind2 ≝ λA,B,C: Type[0]. λf: res (A × B). λg: A → B → res C.
61  match f with
62  [ OK v ⇒ match v with [ pair x y ⇒ g x y ]
63  | Error msg => Error ? msg
64  ].
65
66definition bind3 ≝ λA,B,C,D: Type[0]. λf: res (A × B × C). λg: A → B → C → res D.
67  match f with
68  [ OK v ⇒ match v with [ pair xy z ⇒ match xy with [ pair x y ⇒ g x y z ] ]
69  | Error msg => Error ? msg
70  ].
71 
72(* Not sure what level to use *)
73notation > "vbox('do' ident v ← e; break e')" with precedence 40 for @{'bind ${e} (λ${ident v}.${e'})}.
74notation > "vbox('do' ident v : ty ← e; break e')" with precedence 40 for @{'bind ${e} (λ${ident v} : ${ty}.${e'})}.
75notation < "vbox('do' \nbsp ident v ← e; break e')" with precedence 40 for @{'bind ${e} (λ${ident v}.${e'})}.
76notation < "vbox('do' \nbsp ident v : ty ← e; break e')" with precedence 40 for @{'bind ${e} (λ${ident v} : ${ty}.${e'})}.
77interpretation "error monad bind" 'bind e f = (bind ?? e f).
78notation > "vbox('do' 〈ident v1, ident v2〉 ← e; break e')" with precedence 40 for @{'bind2 ${e} (λ${ident v1}.λ${ident v2}.${e'})}.
79notation > "vbox('do' 〈ident v1 : ty1, ident v2 : ty2〉 ← e; break e')" with precedence 40 for @{'bind2 ${e} (λ${ident v1} : ${ty1}.λ${ident v2} : ${ty2}.${e'})}.
80notation < "vbox('do' \nbsp 〈ident v1, ident v2〉 ← e; break e')" with precedence 40 for @{'bind2 ${e} (λ${ident v1}.λ${ident v2}.${e'})}.
81notation < "vbox('do' \nbsp 〈ident v1 : ty1, ident v2 : ty2〉 ← e; break e')" with precedence 40 for @{'bind2 ${e} (λ${ident v1} : ${ty1}.λ${ident v2} : ${ty2}.${e'})}.
82interpretation "error monad Prod bind" 'bind2 e f = (bind2 ??? e f).
83notation > "vbox('do' 〈ident v1, ident v2, ident v3〉 ← e; break e')" with precedence 40 for @{'bind3 ${e} (λ${ident v1}.λ${ident v2}.λ${ident v3}.${e'})}.
84notation > "vbox('do' 〈ident v1 : ty1, ident v2 : ty2, ident v3 : ty3〉 ← e; break e')" with precedence 40 for @{'bind3 ${e} (λ${ident v1} : ${ty1}.λ${ident v2} : ${ty2}.λ${ident v3} : ${ty3}.${e'})}.
85notation < "vbox('do' \nbsp 〈ident v1, ident v2, ident v3〉 ← e; break e')" with precedence 40 for @{'bind3 ${e} (λ${ident v1}.λ${ident v2}.λ${ident v3}.${e'})}.
86notation < "vbox('do' \nbsp 〈ident v1 : ty1, ident v2 : ty2, ident v3 : ty3〉 ← e; break e')" with precedence 40 for @{'bind3 ${e} (λ${ident v1} : ${ty1}.λ${ident v2} : ${ty2}.λ${ident v3} : ${ty3}.${e'})}.
87interpretation "error monad triple bind" 'bind3 e f = (bind3 ???? e f).
88(*interpretation "error monad ret" 'ret e = (ret ? e).
89notation "'ret' e" non associative with precedence 45 for @{'ret ${e}}.*)
90
91(* Dependent pair version. *)
92notation > "vbox('do' « ident v , ident p » ← e; break e')" with precedence 40
93  for @{ bind ?? ${e} (λ${fresh x}.match ${fresh x} with [ dp ${ident v} ${ident p} ⇒ ${e'} ]) }.
94
95definition sigbind2 : ∀A,B,C:Type[0]. ∀P:A×B → Prop. res (Σx:A×B.P x) → (∀a,b. P 〈a,b〉 → res C) → res C ≝
96λA,B,C,P,e,f.
97  match e with
98  [ OK v ⇒ match v with [ dp v' p ⇒ match v' return λv'. P v' → res C with [ pair a b ⇒ f a b ] p ]
99  | Error msg ⇒ Error ? msg
100  ].
101
102notation > "vbox('do' «ident v1, ident v2, ident H» ← e; break e')" with precedence 40 for @{'sigbind2 ${e} (λ${ident v1}.λ${ident v2}.λ${ident H}.${e'})}.
103interpretation "error monad sig Prod bind" 'sigbind2 e f = (sigbind2 ???? e f).
104
105(*
106(** The [do] notation, inspired by Haskell's, keeps the code readable. *)
107
108Notation "'do' X <- A ; B" := (bind A (fun X => B))
109 (at level 200, X ident, A at level 100, B at level 200)
110 : error_monad_scope.
111
112Notation "'do' ( X , Y ) <- A ; B" := (bind2 A (fun X Y => B))
113 (at level 200, X ident, Y ident, A at level 100, B at level 200)
114 : error_monad_scope.
115*)
116lemma bind_inversion:
117  ∀A,B: Type[0]. ∀f: res A. ∀g: A → res B. ∀y: B.
118  bind ?? f g = OK ? y →
119  ∃x. f = OK ? x ∧ g x = OK ? y.
120#A #B #f #g #y cases f;
121[ #a #e %{a} whd in e:(??%?); /2/;
122| #m #H whd in H:(??%?); destruct (H);
123] qed.
124
125lemma bind2_inversion:
126  ∀A,B,C: Type[0]. ∀f: res (A×B). ∀g: A → B → res C. ∀z: C.
127  bind2 ??? f g = OK ? z →
128  ∃x. ∃y. f = OK ? 〈x, y〉 ∧ g x y = OK ? z.
129#A #B #C #f #g #z cases f;
130[ #ab cases ab; #a #b #e %{a} %{b} whd in e:(??%?); /2/;
131| #m #H whd in H:(??%?); destruct
132] qed.
133
134(*
135Open Local Scope error_monad_scope.
136
137(** This is the familiar monadic map iterator. *)
138*)
139
140let rec mmap (A, B: Type[0]) (f: A → res B) (l: list A) on l : res (list B) ≝
141  match l with
142  [ nil ⇒ OK ? []
143  | cons hd tl ⇒ do hd' ← f hd; do tl' ← mmap ?? f tl; OK ? (hd'::tl')
144  ].
145
146(* And mmap coupled with proofs. *)
147
148let rec mmap_sigma (A,B:Type[0]) (P:B → Prop) (f:A → res (Σx:B.P x)) (l:list A) on l : res (Σl':list B.All B P l') ≝
149match l with
150[ nil ⇒ OK ? «nil B, ?»
151| cons h t ⇒
152    do h' ← f h;
153    do t' ← mmap_sigma A B P f t;
154    OK ? «cons B h' t', ?»
155].
156whd // %
157[ @(use_sig B P)
158| cases t' #l' #p @p
159] qed.
160
161(*
162lemma mmap_inversion:
163  ∀A, B: Type[0]. ∀f: A -> res B. ∀l: list A. ∀l': list B.
164  mmap A B f l = OK ? l' →
165  list_forall2 (fun x y => f x = OK y) l l'.
166Proof.
167  induction l; simpl; intros.
168  inversion_clear H. constructor.
169  destruct (bind_inversion _ _ H) as [hd' [P Q]].
170  destruct (bind_inversion _ _ Q) as [tl' [R S]].
171  inversion_clear S.
172  constructor. auto. auto.
173Qed.
174*)
175
176(* A monadic fold_lefti *)
177let rec mfold_left_i_aux (A: Type[0]) (B: Type[0])
178                        (f: nat → A → B → res A) (x: res A) (i: nat) (l: list B) on l ≝
179  match l with
180    [ nil ⇒ x
181    | cons hd tl ⇒
182       do x ← x ;
183       mfold_left_i_aux A B f (f i x hd) (S i) tl
184    ].
185
186definition mfold_left_i ≝ λA,B,f,x. mfold_left_i_aux A B f x O.
187
188
189(* A monadic fold_left2 *)
190
191axiom WrongLength: String.
192
193let rec mfold_left2
194  (A: Type[0]) (B: Type[0]) (C: Type[0]) (f: A → B → C → res A) (accu: res A)
195  (left: list B) (right: list C) on left: res A ≝
196  match left with
197  [ nil ⇒
198    match right with
199    [ nil ⇒ accu
200    | cons hd tl ⇒ Error ? (msg WrongLength)
201    ]
202  | cons hd tl ⇒
203    match right with
204    [ nil ⇒ Error ? (msg WrongLength)
205    | cons hd' tl' ⇒
206       do accu ← accu;
207       mfold_left2 … f (f accu hd hd') tl tl'
208    ]
209  ].
210
211(*
212(** * Reasoning over monadic computations *)
213
214(** The [monadInv H] tactic below simplifies hypotheses of the form
215<<
216        H: (do x <- a; b) = OK res
217>>
218    By definition of the bind operation, both computations [a] and
219    [b] must succeed for their composition to succeed.  The tactic
220    therefore generates the following hypotheses:
221
222         x: ...
223        H1: a = OK x
224        H2: b x = OK res
225*)
226
227Ltac monadInv1 H :=
228  match type of H with
229  | (OK _ = OK _) =>
230      inversion H; clear H; try subst
231  | (Error _ = OK _) =>
232      discriminate
233  | (bind ?F ?G = OK ?X) =>
234      let x := fresh "x" in (
235      let EQ1 := fresh "EQ" in (
236      let EQ2 := fresh "EQ" in (
237      destruct (bind_inversion F G H) as [x [EQ1 EQ2]];
238      clear H;
239      try (monadInv1 EQ2))))
240  | (bind2 ?F ?G = OK ?X) =>
241      let x1 := fresh "x" in (
242      let x2 := fresh "x" in (
243      let EQ1 := fresh "EQ" in (
244      let EQ2 := fresh "EQ" in (
245      destruct (bind2_inversion F G H) as [x1 [x2 [EQ1 EQ2]]];
246      clear H;
247      try (monadInv1 EQ2)))))
248  | (mmap ?F ?L = OK ?M) =>
249      generalize (mmap_inversion F L H); intro
250  end.
251
252Ltac monadInv H :=
253  match type of H with
254  | (OK _ = OK _) => monadInv1 H
255  | (Error _ = OK _) => monadInv1 H
256  | (bind ?F ?G = OK ?X) => monadInv1 H
257  | (bind2 ?F ?G = OK ?X) => monadInv1 H
258  | (?F _ _ _ _ _ _ _ _ = OK _) =>
259      ((progress simpl in H) || unfold F in H); monadInv1 H
260  | (?F _ _ _ _ _ _ _ = OK _) =>
261      ((progress simpl in H) || unfold F in H); monadInv1 H
262  | (?F _ _ _ _ _ _ = OK _) =>
263      ((progress simpl in H) || unfold F in H); monadInv1 H
264  | (?F _ _ _ _ _ = OK _) =>
265      ((progress simpl in H) || unfold F in H); monadInv1 H
266  | (?F _ _ _ _ = OK _) =>
267      ((progress simpl in H) || unfold F in H); monadInv1 H
268  | (?F _ _ _ = OK _) =>
269      ((progress simpl in H) || unfold F in H); monadInv1 H
270  | (?F _ _ = OK _) =>
271      ((progress simpl in H) || unfold F in H); monadInv1 H
272  | (?F _ = OK _) =>
273      ((progress simpl in H) || unfold F in H); monadInv1 H
274  end.
275*)
276
277
278definition opt_to_res ≝ λA.λmsg.λv:option A. match v with [ None ⇒ Error A msg | Some v ⇒ OK A v ].
279lemma opt_OK: ∀A,m,P,e.
280  (∀v. e = Some ? v → P v) →
281  match opt_to_res A m e with [ Error _ ⇒ True | OK v ⇒ P v ].
282#A #m #P #e elim e; /2/;
283qed.
284
285(* A variation of bind and its notation that provides an equality proof for
286   later use. *)
287
288definition bind_eq ≝ λA,B:Type[0]. λf: res A. λg: ∀a:A. f = OK ? a → res B.
289  match f return λx. f = x → ? with
290  [ OK x ⇒ g x
291  | Error msg ⇒ λ_. Error ? msg
292  ] (refl ? f).
293
294notation > "vbox('do' ident v 'as' ident E ← e; break e')" with precedence 40 for @{ bind_eq ?? ${e} (λ${ident v}.λ${ident E}.${e'})}.
295
296definition res_to_opt : ∀A:Type[0]. res A → option A ≝
297 λA.λv. match v with [ Error _ ⇒ None ? | OK v ⇒ Some … v].
298
Note: See TracBrowser for help on using the repository browser.