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 | |
---|
16 | include "basics/types.ma". |
---|
17 | include "basics/logic.ma". |
---|
18 | include "basics/list.ma". |
---|
19 | |
---|
20 | (* * Error reporting and the error monad. *) |
---|
21 | (* |
---|
22 | (** * Representation of error messages. *) |
---|
23 | |
---|
24 | (** Compile-time errors produce an error message, represented in Coq |
---|
25 | as a list of either substrings or positive numbers encoding |
---|
26 | a source-level identifier (see module AST). *) |
---|
27 | |
---|
28 | Inductive errcode: Type := |
---|
29 | | MSG: string -> errcode |
---|
30 | | CTX: positive -> errcode. |
---|
31 | |
---|
32 | Definition errmsg: Type := list errcode. |
---|
33 | |
---|
34 | Definition msg (s: string) : errmsg := MSG s :: nil. |
---|
35 | *) |
---|
36 | (* * * The error monad *) |
---|
37 | |
---|
38 | (* * Compilation functions that can fail have return type [res A]. |
---|
39 | The return value is either [OK res] to indicate success, |
---|
40 | or [Error msg] to indicate failure. *) |
---|
41 | |
---|
42 | inductive res (A: Type[0]) : Type[0] ≝ |
---|
43 | | OK: A → res A |
---|
44 | | Error: (* FIXME errmsg →*) res A. |
---|
45 | |
---|
46 | (*Implicit Arguments Error [A].*) |
---|
47 | |
---|
48 | (* * To automate the propagation of errors, we use a monadic style |
---|
49 | with the following [bind] operation. *) |
---|
50 | |
---|
51 | definition bind ≝ λA,B:Type[0]. λf: res A. λg: A → res B. |
---|
52 | match f with |
---|
53 | [ OK x ⇒ g x |
---|
54 | | Error (*msg*) ⇒ Error ? (*msg*) |
---|
55 | ]. |
---|
56 | |
---|
57 | definition bind2 ≝ λA,B,C: Type[0]. λf: res (A × B). λg: A → B → res C. |
---|
58 | match f with |
---|
59 | [ OK v ⇒ match v with [ pair x y ⇒ g x y ] |
---|
60 | | Error (*msg*) => Error ? (*msg*) |
---|
61 | ]. |
---|
62 | |
---|
63 | (* Not sure what level to use *) |
---|
64 | notation > "vbox('do' ident v ← e; break e')" with precedence 40 for @{'bind ${e} (λ${ident v}.${e'})}. |
---|
65 | notation > "vbox('do' ident v : ty ← e; break e')" with precedence 40 for @{'bind ${e} (λ${ident v} : ${ty}.${e'})}. |
---|
66 | notation < "vbox('do' \nbsp ident v ← e; break e')" with precedence 40 for @{'bind ${e} (λ${ident v}.${e'})}. |
---|
67 | notation < "vbox('do' \nbsp ident v : ty ← e; break e')" with precedence 40 for @{'bind ${e} (λ${ident v} : ${ty}.${e'})}. |
---|
68 | interpretation "error monad bind" 'bind e f = (bind ?? e f). |
---|
69 | notation > "vbox('do' 〈ident v1, ident v2〉 ← e; break e')" with precedence 40 for @{'bind2 ${e} (λ${ident v1}.λ${ident v2}.${e'})}. |
---|
70 | notation > "vbox('do' 〈ident v1 : ty1, ident v2 : ty2〉 ← e; break e')" with precedence 40 for @{'bind2 ${e} (λ${ident v1} : ${ty1}.λ${ident v2} : ${ty2}.${e'})}. |
---|
71 | notation < "vbox('do' \nbsp 〈ident v1, ident v2〉 ← e; break e')" with precedence 40 for @{'bind2 ${e} (λ${ident v1}.λ${ident v2}.${e'})}. |
---|
72 | notation < "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'})}. |
---|
73 | interpretation "error monad Prod bind" 'bind2 e f = (bind2 ??? e f). |
---|
74 | (*interpretation "error monad ret" 'ret e = (ret ? e). |
---|
75 | notation "'ret' e" non associative with precedence 45 for @{'ret ${e}}.*) |
---|
76 | |
---|
77 | (* |
---|
78 | (** The [do] notation, inspired by Haskell's, keeps the code readable. *) |
---|
79 | |
---|
80 | Notation "'do' X <- A ; B" := (bind A (fun X => B)) |
---|
81 | (at level 200, X ident, A at level 100, B at level 200) |
---|
82 | : error_monad_scope. |
---|
83 | |
---|
84 | Notation "'do' ( X , Y ) <- A ; B" := (bind2 A (fun X Y => B)) |
---|
85 | (at level 200, X ident, Y ident, A at level 100, B at level 200) |
---|
86 | : error_monad_scope. |
---|
87 | *) |
---|
88 | lemma bind_inversion: |
---|
89 | ∀A,B: Type[0]. ∀f: res A. ∀g: A → res B. ∀y: B. |
---|
90 | bind ?? f g = OK ? y → |
---|
91 | ∃x. f = OK ? x ∧ g x = OK ? y. |
---|
92 | #A #B #f #g #y cases f; |
---|
93 | [ #a #e %{a} whd in e:(??%?); /2/; |
---|
94 | | #H whd in H:(??%?); destruct (H); |
---|
95 | ] qed. |
---|
96 | |
---|
97 | lemma bind2_inversion: |
---|
98 | ∀A,B,C: Type[0]. ∀f: res (A×B). ∀g: A → B → res C. ∀z: C. |
---|
99 | bind2 ??? f g = OK ? z → |
---|
100 | ∃x. ∃y. f = OK ? 〈x, y〉 ∧ g x y = OK ? z. |
---|
101 | #A #B #C #f #g #z cases f; |
---|
102 | [ #ab cases ab; #a #b #e %{a} %{b} whd in e:(??%?); /2/; |
---|
103 | | #H whd in H:(??%?); destruct |
---|
104 | ] qed. |
---|
105 | |
---|
106 | (* |
---|
107 | Open Local Scope error_monad_scope. |
---|
108 | |
---|
109 | (** This is the familiar monadic map iterator. *) |
---|
110 | *) |
---|
111 | |
---|
112 | let rec mmap (A, B: Type[0]) (f: A → res B) (l: list A) on l : res (list B) ≝ |
---|
113 | match l with |
---|
114 | [ nil ⇒ OK ? [] |
---|
115 | | cons hd tl ⇒ do hd' ← f hd; do tl' ← mmap ?? f tl; OK ? (hd'::tl') |
---|
116 | ]. |
---|
117 | |
---|
118 | (* |
---|
119 | lemma mmap_inversion: |
---|
120 | ∀A, B: Type[0]. ∀f: A -> res B. ∀l: list A. ∀l': list B. |
---|
121 | mmap A B f l = OK ? l' → |
---|
122 | list_forall2 (fun x y => f x = OK y) l l'. |
---|
123 | Proof. |
---|
124 | induction l; simpl; intros. |
---|
125 | inversion_clear H. constructor. |
---|
126 | destruct (bind_inversion _ _ H) as [hd' [P Q]]. |
---|
127 | destruct (bind_inversion _ _ Q) as [tl' [R S]]. |
---|
128 | inversion_clear S. |
---|
129 | constructor. auto. auto. |
---|
130 | Qed. |
---|
131 | |
---|
132 | (** * Reasoning over monadic computations *) |
---|
133 | |
---|
134 | (** The [monadInv H] tactic below simplifies hypotheses of the form |
---|
135 | << |
---|
136 | H: (do x <- a; b) = OK res |
---|
137 | >> |
---|
138 | By definition of the bind operation, both computations [a] and |
---|
139 | [b] must succeed for their composition to succeed. The tactic |
---|
140 | therefore generates the following hypotheses: |
---|
141 | |
---|
142 | x: ... |
---|
143 | H1: a = OK x |
---|
144 | H2: b x = OK res |
---|
145 | *) |
---|
146 | |
---|
147 | Ltac monadInv1 H := |
---|
148 | match type of H with |
---|
149 | | (OK _ = OK _) => |
---|
150 | inversion H; clear H; try subst |
---|
151 | | (Error _ = OK _) => |
---|
152 | discriminate |
---|
153 | | (bind ?F ?G = OK ?X) => |
---|
154 | let x := fresh "x" in ( |
---|
155 | let EQ1 := fresh "EQ" in ( |
---|
156 | let EQ2 := fresh "EQ" in ( |
---|
157 | destruct (bind_inversion F G H) as [x [EQ1 EQ2]]; |
---|
158 | clear H; |
---|
159 | try (monadInv1 EQ2)))) |
---|
160 | | (bind2 ?F ?G = OK ?X) => |
---|
161 | let x1 := fresh "x" in ( |
---|
162 | let x2 := fresh "x" in ( |
---|
163 | let EQ1 := fresh "EQ" in ( |
---|
164 | let EQ2 := fresh "EQ" in ( |
---|
165 | destruct (bind2_inversion F G H) as [x1 [x2 [EQ1 EQ2]]]; |
---|
166 | clear H; |
---|
167 | try (monadInv1 EQ2))))) |
---|
168 | | (mmap ?F ?L = OK ?M) => |
---|
169 | generalize (mmap_inversion F L H); intro |
---|
170 | end. |
---|
171 | |
---|
172 | Ltac monadInv H := |
---|
173 | match type of H with |
---|
174 | | (OK _ = OK _) => monadInv1 H |
---|
175 | | (Error _ = OK _) => monadInv1 H |
---|
176 | | (bind ?F ?G = OK ?X) => monadInv1 H |
---|
177 | | (bind2 ?F ?G = OK ?X) => monadInv1 H |
---|
178 | | (?F _ _ _ _ _ _ _ _ = OK _) => |
---|
179 | ((progress simpl in H) || unfold F in H); monadInv1 H |
---|
180 | | (?F _ _ _ _ _ _ _ = OK _) => |
---|
181 | ((progress simpl in H) || unfold F in H); monadInv1 H |
---|
182 | | (?F _ _ _ _ _ _ = OK _) => |
---|
183 | ((progress simpl in H) || unfold F in H); monadInv1 H |
---|
184 | | (?F _ _ _ _ _ = OK _) => |
---|
185 | ((progress simpl in H) || unfold F in H); monadInv1 H |
---|
186 | | (?F _ _ _ _ = OK _) => |
---|
187 | ((progress simpl in H) || unfold F in H); monadInv1 H |
---|
188 | | (?F _ _ _ = OK _) => |
---|
189 | ((progress simpl in H) || unfold F in H); monadInv1 H |
---|
190 | | (?F _ _ = OK _) => |
---|
191 | ((progress simpl in H) || unfold F in H); monadInv1 H |
---|
192 | | (?F _ = OK _) => |
---|
193 | ((progress simpl in H) || unfold F in H); monadInv1 H |
---|
194 | end. |
---|
195 | *) |
---|
196 | |
---|
197 | |
---|
198 | definition opt_to_res ≝ λA.λv:option A. match v with [ None ⇒ Error A | Some v ⇒ OK A v ]. |
---|
199 | lemma opt_OK: ∀A,P,e. |
---|
200 | (∀v. e = Some ? v → P v) → |
---|
201 | match opt_to_res A e with [ Error ⇒ True | OK v ⇒ P v ]. |
---|
202 | #A #P #e elim e; /2/; |
---|
203 | qed. |
---|