source: src/common/Errors.ma @ 1599

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

Start of merging of stuff into the standard library of Matita.

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