source: src/common/Errors.ma @ 1316

Last change on this file since 1316 was 1316, checked in by campbell, 9 years ago

Merge in id-lookup-branch to trunk.

File size: 10.2 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
95(*
96(** The [do] notation, inspired by Haskell's, keeps the code readable. *)
97
98Notation "'do' X <- A ; B" := (bind A (fun X => B))
99 (at level 200, X ident, A at level 100, B at level 200)
100 : error_monad_scope.
101
102Notation "'do' ( X , Y ) <- A ; B" := (bind2 A (fun X Y => B))
103 (at level 200, X ident, Y ident, A at level 100, B at level 200)
104 : error_monad_scope.
105*)
106lemma bind_inversion:
107  ∀A,B: Type[0]. ∀f: res A. ∀g: A → res B. ∀y: B.
108  bind ?? f g = OK ? y →
109  ∃x. f = OK ? x ∧ g x = OK ? y.
110#A #B #f #g #y cases f;
111[ #a #e %{a} whd in e:(??%?); /2/;
112| #m #H whd in H:(??%?); destruct (H);
113] qed.
114
115lemma bind2_inversion:
116  ∀A,B,C: Type[0]. ∀f: res (A×B). ∀g: A → B → res C. ∀z: C.
117  bind2 ??? f g = OK ? z →
118  ∃x. ∃y. f = OK ? 〈x, y〉 ∧ g x y = OK ? z.
119#A #B #C #f #g #z cases f;
120[ #ab cases ab; #a #b #e %{a} %{b} whd in e:(??%?); /2/;
121| #m #H whd in H:(??%?); destruct
122] qed.
123
124(*
125Open Local Scope error_monad_scope.
126
127(** This is the familiar monadic map iterator. *)
128*)
129
130let rec mmap (A, B: Type[0]) (f: A → res B) (l: list A) on l : res (list B) ≝
131  match l with
132  [ nil ⇒ OK ? []
133  | cons hd tl ⇒ do hd' ← f hd; do tl' ← mmap ?? f tl; OK ? (hd'::tl')
134  ].
135
136(* And mmap coupled with proofs. *)
137
138let 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') ≝
139match l with
140[ nil ⇒ OK ? «nil B, ?»
141| cons h t ⇒
142    do h' ← f h;
143    do t' ← mmap_sigma A B P f t;
144    OK ? «cons B h' t', ?»
145].
146whd // %
147[ @(use_sig B P)
148| cases t' #l' #p @p
149] qed.
150
151(*
152lemma mmap_inversion:
153  ∀A, B: Type[0]. ∀f: A -> res B. ∀l: list A. ∀l': list B.
154  mmap A B f l = OK ? l' →
155  list_forall2 (fun x y => f x = OK y) l l'.
156Proof.
157  induction l; simpl; intros.
158  inversion_clear H. constructor.
159  destruct (bind_inversion _ _ H) as [hd' [P Q]].
160  destruct (bind_inversion _ _ Q) as [tl' [R S]].
161  inversion_clear S.
162  constructor. auto. auto.
163Qed.
164*)
165
166(* A monadic fold_left2 *)
167
168axiom WrongLength: String.
169
170let rec mfold_left2
171  (A: Type[0]) (B: Type[0]) (C: Type[0]) (f: A → B → C → res A) (accu: res A)
172  (left: list B) (right: list C) on left: res A ≝
173  match left with
174  [ nil ⇒
175    match right with
176    [ nil ⇒ accu
177    | cons hd tl ⇒ Error ? (msg WrongLength)
178    ]
179  | cons hd tl ⇒
180    match right with
181    [ nil ⇒ Error ? (msg WrongLength)
182    | cons hd' tl' ⇒
183       do accu ← accu;
184       mfold_left2 … f (f accu hd hd') tl tl'
185    ]
186  ].
187
188(*
189(** * Reasoning over monadic computations *)
190
191(** The [monadInv H] tactic below simplifies hypotheses of the form
192<<
193        H: (do x <- a; b) = OK res
194>>
195    By definition of the bind operation, both computations [a] and
196    [b] must succeed for their composition to succeed.  The tactic
197    therefore generates the following hypotheses:
198
199         x: ...
200        H1: a = OK x
201        H2: b x = OK res
202*)
203
204Ltac monadInv1 H :=
205  match type of H with
206  | (OK _ = OK _) =>
207      inversion H; clear H; try subst
208  | (Error _ = OK _) =>
209      discriminate
210  | (bind ?F ?G = OK ?X) =>
211      let x := fresh "x" in (
212      let EQ1 := fresh "EQ" in (
213      let EQ2 := fresh "EQ" in (
214      destruct (bind_inversion F G H) as [x [EQ1 EQ2]];
215      clear H;
216      try (monadInv1 EQ2))))
217  | (bind2 ?F ?G = OK ?X) =>
218      let x1 := fresh "x" in (
219      let x2 := fresh "x" in (
220      let EQ1 := fresh "EQ" in (
221      let EQ2 := fresh "EQ" in (
222      destruct (bind2_inversion F G H) as [x1 [x2 [EQ1 EQ2]]];
223      clear H;
224      try (monadInv1 EQ2)))))
225  | (mmap ?F ?L = OK ?M) =>
226      generalize (mmap_inversion F L H); intro
227  end.
228
229Ltac monadInv H :=
230  match type of H with
231  | (OK _ = OK _) => monadInv1 H
232  | (Error _ = OK _) => monadInv1 H
233  | (bind ?F ?G = OK ?X) => monadInv1 H
234  | (bind2 ?F ?G = OK ?X) => monadInv1 H
235  | (?F _ _ _ _ _ _ _ _ = OK _) =>
236      ((progress simpl in H) || unfold F in H); monadInv1 H
237  | (?F _ _ _ _ _ _ _ = OK _) =>
238      ((progress simpl in H) || unfold F in H); monadInv1 H
239  | (?F _ _ _ _ _ _ = OK _) =>
240      ((progress simpl in H) || unfold F in H); monadInv1 H
241  | (?F _ _ _ _ _ = OK _) =>
242      ((progress simpl in H) || unfold F in H); monadInv1 H
243  | (?F _ _ _ _ = OK _) =>
244      ((progress simpl in H) || unfold F in H); monadInv1 H
245  | (?F _ _ _ = OK _) =>
246      ((progress simpl in H) || unfold F in H); monadInv1 H
247  | (?F _ _ = OK _) =>
248      ((progress simpl in H) || unfold F in H); monadInv1 H
249  | (?F _ = OK _) =>
250      ((progress simpl in H) || unfold F in H); monadInv1 H
251  end.
252*)
253
254
255definition opt_to_res ≝ λA.λmsg.λv:option A. match v with [ None ⇒ Error A msg | Some v ⇒ OK A v ].
256lemma opt_OK: ∀A,m,P,e.
257  (∀v. e = Some ? v → P v) →
258  match opt_to_res A m e with [ Error _ ⇒ True | OK v ⇒ P v ].
259#A #m #P #e elim e; /2/;
260qed.
261
262(* A variation of bind and its notation that provides an equality proof for
263   later use. *)
264
265definition bind_eq ≝ λA,B:Type[0]. λf: res A. λg: ∀a:A. f = OK ? a → res B.
266  match f return λx. f = x → ? with
267  [ OK x ⇒ g x
268  | Error msg ⇒ λ_. Error ? msg
269  ] (refl ? f).
270
271notation > "vbox('do' ident v 'as' ident E ← e; break e')" with precedence 40 for @{ bind_eq ?? ${e} (λ${ident v}.λ${ident E}.${e'})}.
272
273definition res_to_opt : ∀A:Type[0]. res A → option A ≝
274 λA.λv. match v with [ Error _ ⇒ None ? | OK v ⇒ Some … v].
275
Note: See TracBrowser for help on using the repository browser.