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 | include "common/PreIdentifiers.ma". |
---|
20 | include "utilities/lists.ma". |
---|
21 | include "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 | |
---|
31 | inductive errcode: Type[0] := |
---|
32 | | MSG: String → errcode |
---|
33 | | CTX: ∀tag:String. identifier tag → errcode. |
---|
34 | |
---|
35 | definition errmsg: Type[0] ≝ list errcode. |
---|
36 | |
---|
37 | definition 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 | |
---|
45 | inductive 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 | |
---|
54 | definition 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 | |
---|
60 | definition 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 | |
---|
66 | definition 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 *) |
---|
73 | notation > "vbox('do' ident v ← e; break e')" with precedence 40 for @{'bind ${e} (λ${ident v}.${e'})}. |
---|
74 | notation > "vbox('do' ident v : ty ← e; break e')" with precedence 40 for @{'bind ${e} (λ${ident v} : ${ty}.${e'})}. |
---|
75 | notation < "vbox('do' \nbsp ident v ← e; break e')" with precedence 40 for @{'bind ${e} (λ${ident v}.${e'})}. |
---|
76 | notation < "vbox('do' \nbsp ident v : ty ← e; break e')" with precedence 40 for @{'bind ${e} (λ${ident v} : ${ty}.${e'})}. |
---|
77 | interpretation "error monad bind" 'bind e f = (bind ?? e f). |
---|
78 | notation > "vbox('do' 〈ident v1, ident v2〉 ← e; break e')" with precedence 40 for @{'bind2 ${e} (λ${ident v1}.λ${ident v2}.${e'})}. |
---|
79 | 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'})}. |
---|
80 | notation < "vbox('do' \nbsp 〈ident v1, ident v2〉 ← e; break e')" with precedence 40 for @{'bind2 ${e} (λ${ident v1}.λ${ident v2}.${e'})}. |
---|
81 | 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'})}. |
---|
82 | interpretation "error monad Prod bind" 'bind2 e f = (bind2 ??? e f). |
---|
83 | notation > "vbox('do' 〈ident v1, ident v2, ident v3〉 ← e; break e')" with precedence 40 for @{'bind3 ${e} (λ${ident v1}.λ${ident v2}.λ${ident v3}.${e'})}. |
---|
84 | notation > "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'})}. |
---|
85 | notation < "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'})}. |
---|
86 | notation < "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'})}. |
---|
87 | interpretation "error monad triple bind" 'bind3 e f = (bind3 ???? e f). |
---|
88 | (*interpretation "error monad ret" 'ret e = (ret ? e). |
---|
89 | notation "'ret' e" non associative with precedence 45 for @{'ret ${e}}.*) |
---|
90 | |
---|
91 | (* Dependent pair version. *) |
---|
92 | notation > "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 | definition 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 | |
---|
102 | notation > "vbox('do' «ident v1, ident v2, ident H» ← e; break e')" with precedence 40 for @{'sigbind2 ${e} (λ${ident v1}.λ${ident v2}.λ${ident H}.${e'})}. |
---|
103 | interpretation "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 | |
---|
108 | Notation "'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 | |
---|
112 | Notation "'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 | *) |
---|
116 | lemma 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 | |
---|
125 | lemma 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 | (* |
---|
135 | Open Local Scope error_monad_scope. |
---|
136 | |
---|
137 | (** This is the familiar monadic map iterator. *) |
---|
138 | *) |
---|
139 | |
---|
140 | let 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 | |
---|
148 | let 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') ≝ |
---|
149 | match 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 | ]. |
---|
156 | whd // % |
---|
157 | [ @(use_sig B P) |
---|
158 | | cases t' #l' #p @p |
---|
159 | ] qed. |
---|
160 | |
---|
161 | (* |
---|
162 | lemma 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'. |
---|
166 | Proof. |
---|
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. |
---|
173 | Qed. |
---|
174 | *) |
---|
175 | |
---|
176 | (* A monadic fold_lefti *) |
---|
177 | let 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 | |
---|
186 | definition mfold_left_i ≝ λA,B,f,x. mfold_left_i_aux A B f x O. |
---|
187 | |
---|
188 | |
---|
189 | (* A monadic fold_left2 *) |
---|
190 | |
---|
191 | axiom WrongLength: String. |
---|
192 | |
---|
193 | let 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 | |
---|
227 | Ltac 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 | |
---|
252 | Ltac 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 | |
---|
278 | definition opt_to_res ≝ λA.λmsg.λv:option A. match v with [ None ⇒ Error A msg | Some v ⇒ OK A v ]. |
---|
279 | lemma 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/; |
---|
283 | qed. |
---|
284 | |
---|
285 | (* A variation of bind and its notation that provides an equality proof for |
---|
286 | later use. *) |
---|
287 | |
---|
288 | definition 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 | |
---|
294 | notation > "vbox('do' ident v 'as' ident E ← e; break e')" with precedence 40 for @{ bind_eq ?? ${e} (λ${ident v}.λ${ident E}.${e'})}. |
---|
295 | |
---|
296 | definition res_to_opt : ∀A:Type[0]. res A → option A ≝ |
---|
297 | λA.λv. match v with [ Error _ ⇒ None ? | OK v ⇒ Some … v]. |
---|
298 | |
---|