source: src/common/Errors.ma @ 1351

Last change on this file since 1351 was 1351, checked in by campbell, 8 years ago

Tidy up some loose ends from the invariants branch merge.

File size: 10.8 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_left2 *)
177
178axiom WrongLength: String.
179
180let rec mfold_left2
181  (A: Type[0]) (B: Type[0]) (C: Type[0]) (f: A → B → C → res A) (accu: res A)
182  (left: list B) (right: list C) on left: res A ≝
183  match left with
184  [ nil ⇒
185    match right with
186    [ nil ⇒ accu
187    | cons hd tl ⇒ Error ? (msg WrongLength)
188    ]
189  | cons hd tl ⇒
190    match right with
191    [ nil ⇒ Error ? (msg WrongLength)
192    | cons hd' tl' ⇒
193       do accu ← accu;
194       mfold_left2 … f (f accu hd hd') tl tl'
195    ]
196  ].
197
198(*
199(** * Reasoning over monadic computations *)
200
201(** The [monadInv H] tactic below simplifies hypotheses of the form
202<<
203        H: (do x <- a; b) = OK res
204>>
205    By definition of the bind operation, both computations [a] and
206    [b] must succeed for their composition to succeed.  The tactic
207    therefore generates the following hypotheses:
208
209         x: ...
210        H1: a = OK x
211        H2: b x = OK res
212*)
213
214Ltac monadInv1 H :=
215  match type of H with
216  | (OK _ = OK _) =>
217      inversion H; clear H; try subst
218  | (Error _ = OK _) =>
219      discriminate
220  | (bind ?F ?G = OK ?X) =>
221      let x := fresh "x" in (
222      let EQ1 := fresh "EQ" in (
223      let EQ2 := fresh "EQ" in (
224      destruct (bind_inversion F G H) as [x [EQ1 EQ2]];
225      clear H;
226      try (monadInv1 EQ2))))
227  | (bind2 ?F ?G = OK ?X) =>
228      let x1 := fresh "x" in (
229      let x2 := fresh "x" in (
230      let EQ1 := fresh "EQ" in (
231      let EQ2 := fresh "EQ" in (
232      destruct (bind2_inversion F G H) as [x1 [x2 [EQ1 EQ2]]];
233      clear H;
234      try (monadInv1 EQ2)))))
235  | (mmap ?F ?L = OK ?M) =>
236      generalize (mmap_inversion F L H); intro
237  end.
238
239Ltac monadInv H :=
240  match type of H with
241  | (OK _ = OK _) => monadInv1 H
242  | (Error _ = OK _) => monadInv1 H
243  | (bind ?F ?G = OK ?X) => monadInv1 H
244  | (bind2 ?F ?G = OK ?X) => 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  | (?F _ _ _ _ _ = OK _) =>
252      ((progress simpl in H) || unfold F in H); monadInv1 H
253  | (?F _ _ _ _ = OK _) =>
254      ((progress simpl in H) || unfold F in H); monadInv1 H
255  | (?F _ _ _ = OK _) =>
256      ((progress simpl in H) || unfold F in H); monadInv1 H
257  | (?F _ _ = OK _) =>
258      ((progress simpl in H) || unfold F in H); monadInv1 H
259  | (?F _ = OK _) =>
260      ((progress simpl in H) || unfold F in H); monadInv1 H
261  end.
262*)
263
264
265definition opt_to_res ≝ λA.λmsg.λv:option A. match v with [ None ⇒ Error A msg | Some v ⇒ OK A v ].
266lemma opt_OK: ∀A,m,P,e.
267  (∀v. e = Some ? v → P v) →
268  match opt_to_res A m e with [ Error _ ⇒ True | OK v ⇒ P v ].
269#A #m #P #e elim e; /2/;
270qed.
271
272(* A variation of bind and its notation that provides an equality proof for
273   later use. *)
274
275definition bind_eq ≝ λA,B:Type[0]. λf: res A. λg: ∀a:A. f = OK ? a → res B.
276  match f return λx. f = x → ? with
277  [ OK x ⇒ g x
278  | Error msg ⇒ λ_. Error ? msg
279  ] (refl ? f).
280
281notation > "vbox('do' ident v 'as' ident E ← e; break e')" with precedence 40 for @{ bind_eq ?? ${e} (λ${ident v}.λ${ident E}.${e'})}.
282
283definition res_to_opt : ∀A:Type[0]. res A → option A ≝
284 λA.λv. match v with [ Error _ ⇒ None ? | OK v ⇒ Some … v].
285
Note: See TracBrowser for help on using the repository browser.