adam@380
|
1 (* Copyright (c) 2008-2012, Adam Chlipala
|
adamc@70
|
2 *
|
adamc@70
|
3 * This work is licensed under a
|
adamc@70
|
4 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
|
adamc@70
|
5 * Unported License.
|
adamc@70
|
6 * The license text is available at:
|
adamc@70
|
7 * http://creativecommons.org/licenses/by-nc-nd/3.0/
|
adamc@70
|
8 *)
|
adamc@70
|
9
|
adamc@70
|
10 (* begin hide *)
|
adamc@70
|
11 Require Import List.
|
adamc@70
|
12
|
adam@314
|
13 Require Import CpdtTactics.
|
adamc@70
|
14
|
adamc@70
|
15 Set Implicit Arguments.
|
adamc@70
|
16 (* end hide *)
|
adamc@70
|
17
|
adam@403
|
18 (** printing <-- $\longleftarrow$ *)
|
adam@403
|
19
|
adamc@70
|
20
|
adamc@74
|
21 (** %\part{Programming with Dependent Types}
|
adamc@74
|
22
|
adamc@74
|
23 \chapter{Subset Types and Variations}% *)
|
adamc@70
|
24
|
adam@423
|
25 (** So far, we have seen many examples of what we might call "classical program verification." We write programs, write their specifications, and then prove that the programs satisfy their specifications. The programs that we have written in Coq have been normal functional programs that we could just as well have written in Haskell or ML. In this chapter, we start investigating uses of%\index{dependent types}% _dependent types_ to integrate programming, specification, and proving into a single phase. The techniques we will learn make it possible to reduce the cost of program verification dramatically. *)
|
adamc@70
|
26
|
adamc@70
|
27
|
adamc@70
|
28 (** * Introducing Subset Types *)
|
adamc@70
|
29
|
adamc@70
|
30 (** Let us consider several ways of implementing the natural number predecessor function. We start by displaying the definition from the standard library: *)
|
adamc@70
|
31
|
adam@423
|
32 (* begin hide *)
|
adam@423
|
33 Definition foo := pred.
|
adam@423
|
34 (* end hide *)
|
adam@423
|
35
|
adamc@70
|
36 Print pred.
|
adamc@212
|
37 (** %\vspace{-.15in}% [[
|
adamc@70
|
38 pred = fun n : nat => match n with
|
adamc@70
|
39 | 0 => 0
|
adamc@70
|
40 | S u => u
|
adamc@70
|
41 end
|
adamc@70
|
42 : nat -> nat
|
adamc@212
|
43
|
adamc@212
|
44 ]]
|
adamc@70
|
45
|
adam@335
|
46 We can use a new command, %\index{Vernacular commands!Extraction}\index{program extraction}\index{extraction|see{program extraction}}%[Extraction], to produce an %\index{OCaml}%OCaml version of this function. *)
|
adamc@70
|
47
|
adamc@70
|
48 Extraction pred.
|
adamc@70
|
49
|
adamc@70
|
50 (** %\begin{verbatim}
|
adamc@70
|
51 (** val pred : nat -> nat **)
|
adamc@70
|
52
|
adamc@70
|
53 let pred = function
|
adamc@70
|
54 | O -> O
|
adamc@70
|
55 | S u -> u
|
adamc@70
|
56 \end{verbatim}%
|
adamc@70
|
57
|
adamc@70
|
58 #<pre>
|
adamc@70
|
59 (** val pred : nat -> nat **)
|
adamc@70
|
60
|
adamc@70
|
61 let pred = function
|
adamc@70
|
62 | O -> O
|
adamc@70
|
63 | S u -> u
|
adamc@70
|
64 </pre># *)
|
adamc@70
|
65
|
adamc@70
|
66 (** Returning 0 as the predecessor of 0 can come across as somewhat of a hack. In some situations, we might like to be sure that we never try to take the predecessor of 0. We can enforce this by giving [pred] a stronger, dependent type. *)
|
adamc@70
|
67
|
adamc@70
|
68 Lemma zgtz : 0 > 0 -> False.
|
adamc@70
|
69 crush.
|
adamc@70
|
70 Qed.
|
adamc@70
|
71
|
adamc@70
|
72 Definition pred_strong1 (n : nat) : n > 0 -> nat :=
|
adamc@212
|
73 match n with
|
adamc@70
|
74 | O => fun pf : 0 > 0 => match zgtz pf with end
|
adamc@70
|
75 | S n' => fun _ => n'
|
adamc@70
|
76 end.
|
adamc@70
|
77
|
adam@398
|
78 (** We expand the type of [pred] to include a _proof_ that its argument [n] is greater than 0. When [n] is 0, we use the proof to derive a contradiction, which we can use to build a value of any type via a vacuous pattern match. When [n] is a successor, we have no need for the proof and just return the answer. The proof argument can be said to have a _dependent_ type, because its type depends on the _value_ of the argument [n].
|
adamc@70
|
79
|
adam@398
|
80 Coq's [Eval] command can execute particular invocations of [pred_strong1] just as easily as it can execute more traditional functional programs. Note that Coq has decided that argument [n] of [pred_strong1] can be made _implicit_, since it can be deduced from the type of the second argument, so we need not write [n] in function calls. *)
|
adam@282
|
81
|
adam@282
|
82 Theorem two_gt0 : 2 > 0.
|
adam@282
|
83 crush.
|
adam@282
|
84 Qed.
|
adam@282
|
85
|
adam@282
|
86 Eval compute in pred_strong1 two_gt0.
|
adam@282
|
87 (** %\vspace{-.15in}% [[
|
adam@282
|
88 = 1
|
adam@282
|
89 : nat
|
adam@282
|
90
|
adam@282
|
91 ]]
|
adam@282
|
92
|
adam@294
|
93 One aspect in particular of the definition of [pred_strong1] may be surprising. We took advantage of [Definition]'s syntactic sugar for defining function arguments in the case of [n], but we bound the proofs later with explicit [fun] expressions. Let us see what happens if we write this function in the way that at first seems most natural.
|
adamc@70
|
94
|
adamc@212
|
95 [[
|
adamc@70
|
96 Definition pred_strong1' (n : nat) (pf : n > 0) : nat :=
|
adamc@70
|
97 match n with
|
adamc@70
|
98 | O => match zgtz pf with end
|
adamc@70
|
99 | S n' => n'
|
adamc@70
|
100 end.
|
adam@335
|
101 ]]
|
adamc@70
|
102
|
adam@335
|
103 <<
|
adamc@70
|
104 Error: In environment
|
adamc@70
|
105 n : nat
|
adamc@70
|
106 pf : n > 0
|
adamc@70
|
107 The term "pf" has type "n > 0" while it is expected to have type
|
adamc@70
|
108 "0 > 0"
|
adam@335
|
109 >>
|
adamc@70
|
110
|
adamc@212
|
111 The term [zgtz pf] fails to type-check. Somehow the type checker has failed to take into account information that follows from which [match] branch that term appears in. The problem is that, by default, [match] does not let us use such implied information. To get refined typing, we must always rely on [match] annotations, either written explicitly or inferred.
|
adamc@70
|
112
|
adam@398
|
113 In this case, we must use a [return] annotation to declare the relationship between the _value_ of the [match] discriminee and the _type_ of the result. There is no annotation that lets us declare a relationship between the discriminee and the type of a variable that is already in scope; hence, we delay the binding of [pf], so that we can use the [return] annotation to express the needed relationship.
|
adamc@70
|
114
|
adam@335
|
115 We are lucky that Coq's heuristics infer the [return] clause (specifically, [return n > 0 -> nat]) for us in this case. *)
|
adam@335
|
116
|
adam@335
|
117 Definition pred_strong1' (n : nat) : n > 0 -> nat :=
|
adam@335
|
118 match n return n > 0 -> nat with
|
adam@335
|
119 | O => fun pf : 0 > 0 => match zgtz pf with end
|
adam@335
|
120 | S n' => fun _ => n'
|
adam@335
|
121 end.
|
adam@335
|
122
|
adam@403
|
123 (** By making explicit the functional relationship between value [n] and the result type of the [match], we guide Coq toward proper type checking. The clause for this example follows by simple copying of the original annotation on the definition. In general, however, the [match] annotation inference problem is undecidable. The known undecidable problem of%\index{higher-order unification}% _higher-order unification_ %\cite{HOU}% reduces to the [match] type inference problem. Over time, Coq is enhanced with more and more heuristics to get around this problem, but there must always exist [match]es whose types Coq cannot infer without annotations.
|
adamc@70
|
124
|
adamc@70
|
125 Let us now take a look at the OCaml code Coq generates for [pred_strong1]. *)
|
adamc@70
|
126
|
adamc@70
|
127 Extraction pred_strong1.
|
adamc@70
|
128
|
adamc@70
|
129 (** %\begin{verbatim}
|
adamc@70
|
130 (** val pred_strong1 : nat -> nat **)
|
adamc@70
|
131
|
adamc@70
|
132 let pred_strong1 = function
|
adamc@70
|
133 | O -> assert false (* absurd case *)
|
adamc@70
|
134 | S n' -> n'
|
adamc@70
|
135 \end{verbatim}%
|
adamc@70
|
136
|
adamc@70
|
137 #<pre>
|
adamc@70
|
138 (** val pred_strong1 : nat -> nat **)
|
adamc@70
|
139
|
adamc@70
|
140 let pred_strong1 = function
|
adamc@70
|
141 | O -> assert false (* absurd case *)
|
adamc@70
|
142 | S n' -> n'
|
adamc@70
|
143 </pre># *)
|
adamc@70
|
144
|
adamc@70
|
145 (** The proof argument has disappeared! We get exactly the OCaml code we would have written manually. This is our first demonstration of the main technically interesting feature of Coq program extraction: program components of type [Prop] are erased systematically.
|
adamc@70
|
146
|
adam@403
|
147 We can reimplement our dependently typed [pred] based on%\index{subset types}% _subset types_, defined in the standard library with the type family %\index{Gallina terms!sig}%[sig]. *)
|
adamc@70
|
148
|
adam@423
|
149 (* begin hide *)
|
adam@423
|
150 Definition bar := (sig, ex).
|
adam@423
|
151 (* end hide *)
|
adam@423
|
152
|
adamc@70
|
153 Print sig.
|
adamc@212
|
154 (** %\vspace{-.15in}% [[
|
adamc@70
|
155 Inductive sig (A : Type) (P : A -> Prop) : Type :=
|
adamc@70
|
156 exist : forall x : A, P x -> sig P
|
adamc@212
|
157
|
adamc@70
|
158 ]]
|
adamc@70
|
159
|
adam@335
|
160 The family [sig] is a Curry-Howard twin of [ex], except that [sig] is in [Type], while [ex] is in [Prop]. That means that [sig] values can survive extraction, while [ex] proofs will always be erased. The actual details of extraction of [sig]s are more subtle, as we will see shortly.
|
adamc@70
|
161
|
adamc@70
|
162 We rewrite [pred_strong1], using some syntactic sugar for subset types. *)
|
adamc@70
|
163
|
adamc@70
|
164 Locate "{ _ : _ | _ }".
|
adamc@212
|
165 (** %\vspace{-.15in}% [[
|
adam@335
|
166 Notation
|
adamc@70
|
167 "{ x : A | P }" := sig (fun x : A => P)
|
adam@302
|
168 ]]
|
adam@302
|
169 *)
|
adamc@70
|
170
|
adamc@70
|
171 Definition pred_strong2 (s : {n : nat | n > 0}) : nat :=
|
adamc@70
|
172 match s with
|
adamc@70
|
173 | exist O pf => match zgtz pf with end
|
adamc@70
|
174 | exist (S n') _ => n'
|
adamc@70
|
175 end.
|
adamc@70
|
176
|
adam@335
|
177 (** To build a value of a subset type, we use the [exist] constructor, and the details of how to do that follow from the output of our earlier [Print sig] command (where we elided the extra information that parameter [A] is implicit). *)
|
adam@282
|
178
|
adam@282
|
179 Eval compute in pred_strong2 (exist _ 2 two_gt0).
|
adam@282
|
180 (** %\vspace{-.15in}% [[
|
adam@282
|
181 = 1
|
adam@282
|
182 : nat
|
adam@302
|
183 ]]
|
adam@302
|
184 *)
|
adam@282
|
185
|
adamc@70
|
186 Extraction pred_strong2.
|
adamc@70
|
187
|
adamc@70
|
188 (** %\begin{verbatim}
|
adamc@70
|
189 (** val pred_strong2 : nat -> nat **)
|
adamc@70
|
190
|
adamc@70
|
191 let pred_strong2 = function
|
adamc@70
|
192 | O -> assert false (* absurd case *)
|
adamc@70
|
193 | S n' -> n'
|
adamc@70
|
194 \end{verbatim}%
|
adamc@70
|
195
|
adamc@70
|
196 #<pre>
|
adamc@70
|
197 (** val pred_strong2 : nat -> nat **)
|
adamc@70
|
198
|
adamc@70
|
199 let pred_strong2 = function
|
adamc@70
|
200 | O -> assert false (* absurd case *)
|
adamc@70
|
201 | S n' -> n'
|
adamc@70
|
202 </pre>#
|
adamc@70
|
203
|
adamc@70
|
204 We arrive at the same OCaml code as was extracted from [pred_strong1], which may seem surprising at first. The reason is that a value of [sig] is a pair of two pieces, a value and a proof about it. Extraction erases the proof, which reduces the constructor [exist] of [sig] to taking just a single argument. An optimization eliminates uses of datatypes with single constructors taking single arguments, and we arrive back where we started.
|
adamc@70
|
205
|
adamc@70
|
206 We can continue on in the process of refining [pred]'s type. Let us change its result type to capture that the output is really the predecessor of the input. *)
|
adamc@70
|
207
|
adamc@70
|
208 Definition pred_strong3 (s : {n : nat | n > 0}) : {m : nat | proj1_sig s = S m} :=
|
adamc@70
|
209 match s return {m : nat | proj1_sig s = S m} with
|
adamc@70
|
210 | exist 0 pf => match zgtz pf with end
|
adam@426
|
211 | exist (S n') pf => exist _ n' (eq_refl _)
|
adamc@70
|
212 end.
|
adamc@70
|
213
|
adam@282
|
214 Eval compute in pred_strong3 (exist _ 2 two_gt0).
|
adam@282
|
215 (** %\vspace{-.15in}% [[
|
adam@426
|
216 = exist (fun m : nat => 2 = S m) 1 (eq_refl 2)
|
adam@282
|
217 : {m : nat | proj1_sig (exist (lt 0) 2 two_gt0) = S m}
|
adam@335
|
218 ]]
|
adam@302
|
219 *)
|
adam@282
|
220
|
adam@423
|
221 (* begin hide *)
|
adam@423
|
222 Definition pred_strong := 0.
|
adam@423
|
223 (* end hide *)
|
adam@423
|
224
|
adam@335
|
225 (** The function %\index{Gallina terms!proj1\_sig}%[proj1_sig] extracts the base value from a subset type. It turns out that we need to include an explicit [return] clause here, since Coq's heuristics are not smart enough to propagate the result type that we wrote earlier.
|
adamc@70
|
226
|
adamc@70
|
227 By now, the reader is probably ready to believe that the new [pred_strong] leads to the same OCaml code as we have seen several times so far, and Coq does not disappoint. *)
|
adamc@70
|
228
|
adamc@70
|
229 Extraction pred_strong3.
|
adamc@70
|
230
|
adamc@70
|
231 (** %\begin{verbatim}
|
adamc@70
|
232 (** val pred_strong3 : nat -> nat **)
|
adamc@70
|
233
|
adamc@70
|
234 let pred_strong3 = function
|
adamc@70
|
235 | O -> assert false (* absurd case *)
|
adamc@70
|
236 | S n' -> n'
|
adamc@70
|
237 \end{verbatim}%
|
adamc@70
|
238
|
adamc@70
|
239 #<pre>
|
adamc@70
|
240 (** val pred_strong3 : nat -> nat **)
|
adamc@70
|
241
|
adamc@70
|
242 let pred_strong3 = function
|
adamc@70
|
243 | O -> assert false (* absurd case *)
|
adamc@70
|
244 | S n' -> n'
|
adamc@70
|
245 </pre>#
|
adamc@70
|
246
|
adam@335
|
247 We have managed to reach a type that is, in a formal sense, the most expressive possible for [pred]. Any other implementation of the same type must have the same input-output behavior. However, there is still room for improvement in making this kind of code easier to write. Here is a version that takes advantage of tactic-based theorem proving. We switch back to passing a separate proof argument instead of using a subset type for the function's input, because this leads to cleaner code. (Recall that [False_rec] is the [Set]-level induction principle for [False], which can be used to produce a value in any [Set] given a proof of [False].) *)
|
adamc@70
|
248
|
adam@297
|
249 Definition pred_strong4 : forall n : nat, n > 0 -> {m : nat | n = S m}.
|
adamc@70
|
250 refine (fun n =>
|
adamc@212
|
251 match n with
|
adamc@70
|
252 | O => fun _ => False_rec _ _
|
adamc@70
|
253 | S n' => fun _ => exist _ n' _
|
adamc@70
|
254 end).
|
adamc@212
|
255
|
adamc@77
|
256 (* begin thide *)
|
adam@335
|
257 (** We build [pred_strong4] using tactic-based proving, beginning with a [Definition] command that ends in a period before a definition is given. Such a command enters the interactive proving mode, with the type given for the new identifier as our proof goal. It may seem strange to change perspective so implicitly between programming and proving, but recall that programs and proofs are two sides of the same coin in Coq, thanks to the Curry-Howard correspondence.
|
adamc@70
|
258
|
adam@423
|
259 We do most of the work with the %\index{tactics!refine}%[refine] tactic, to which we pass a partial "proof" of the type we are trying to prove. There may be some pieces left to fill in, indicated by underscores. Any underscore that Coq cannot reconstruct with type inference is added as a proof subgoal. In this case, we have two subgoals:
|
adam@335
|
260
|
adam@335
|
261 %\vspace{.1in} \noindent 2 \coqdockw{subgoals}\vspace{-.1in}%#<tt>2 subgoals</tt>#
|
adam@335
|
262 [[
|
adamc@70
|
263
|
adamc@70
|
264 n : nat
|
adamc@70
|
265 _ : 0 > 0
|
adamc@70
|
266 ============================
|
adamc@70
|
267 False
|
adam@335
|
268 ]]
|
adam@335
|
269 %\noindent \coqdockw{subgoal} 2 \coqdockw{is}:%#<tt>subgoal 2 is</tt>#
|
adam@335
|
270 [[
|
adamc@70
|
271 S n' = S n'
|
adamc@70
|
272 ]]
|
adamc@70
|
273
|
adamc@70
|
274 We can see that the first subgoal comes from the second underscore passed to [False_rec], and the second subgoal comes from the second underscore passed to [exist]. In the first case, we see that, though we bound the proof variable with an underscore, it is still available in our proof context. It is hard to refer to underscore-named variables in manual proofs, but automation makes short work of them. Both subgoals are easy to discharge that way, so let us back up and ask to prove all subgoals automatically. *)
|
adamc@70
|
275
|
adamc@70
|
276 Undo.
|
adamc@70
|
277 refine (fun n =>
|
adamc@212
|
278 match n with
|
adamc@70
|
279 | O => fun _ => False_rec _ _
|
adamc@70
|
280 | S n' => fun _ => exist _ n' _
|
adamc@70
|
281 end); crush.
|
adamc@77
|
282 (* end thide *)
|
adamc@70
|
283 Defined.
|
adamc@70
|
284
|
adam@423
|
285 (** We end the "proof" with %\index{Vernacular commands!Defined}%[Defined] instead of [Qed], so that the definition we constructed remains visible. This contrasts to the case of ending a proof with [Qed], where the details of the proof are hidden afterward. (More formally, [Defined] marks an identifier as%\index{transparent}% _transparent_, allowing it to be unfolded; while [Qed] marks an identifier as%\index{opaque}% _opaque_, preventing unfolding.) Let us see what our proof script constructed. *)
|
adamc@70
|
286
|
adamc@70
|
287 Print pred_strong4.
|
adamc@212
|
288 (** %\vspace{-.15in}% [[
|
adamc@70
|
289 pred_strong4 =
|
adamc@70
|
290 fun n : nat =>
|
adamc@70
|
291 match n as n0 return (n0 > 0 -> {m : nat | n0 = S m}) with
|
adamc@70
|
292 | 0 =>
|
adamc@70
|
293 fun _ : 0 > 0 =>
|
adamc@70
|
294 False_rec {m : nat | 0 = S m}
|
adamc@70
|
295 (Bool.diff_false_true
|
adamc@70
|
296 (Bool.absurd_eq_true false
|
adamc@70
|
297 (Bool.diff_false_true
|
adamc@70
|
298 (Bool.absurd_eq_true false (pred_strong4_subproof n _)))))
|
adamc@70
|
299 | S n' =>
|
adamc@70
|
300 fun _ : S n' > 0 =>
|
adam@426
|
301 exist (fun m : nat => S n' = S m) n' (eq_refl (S n'))
|
adamc@70
|
302 end
|
adamc@70
|
303 : forall n : nat, n > 0 -> {m : nat | n = S m}
|
adamc@212
|
304
|
adamc@70
|
305 ]]
|
adamc@70
|
306
|
adam@282
|
307 We see the code we entered, with some proofs filled in. The first proof obligation, the second argument to [False_rec], is filled in with a nasty-looking proof term that we can be glad we did not enter by hand. The second proof obligation is a simple reflexivity proof. *)
|
adamc@70
|
308
|
adam@282
|
309 Eval compute in pred_strong4 two_gt0.
|
adam@282
|
310 (** %\vspace{-.15in}% [[
|
adam@426
|
311 = exist (fun m : nat => 2 = S m) 1 (eq_refl 2)
|
adam@282
|
312 : {m : nat | 2 = S m}
|
adam@282
|
313 ]]
|
adam@282
|
314
|
adam@335
|
315 A tactic modifier called %\index{tactics!abstract}%[abstract] can be helpful for producing shorter terms, by automatically abstracting subgoals into named lemmas. *)
|
adam@335
|
316
|
adam@335
|
317 (* begin thide *)
|
adam@335
|
318 Definition pred_strong4' : forall n : nat, n > 0 -> {m : nat | n = S m}.
|
adam@335
|
319 refine (fun n =>
|
adam@335
|
320 match n with
|
adam@335
|
321 | O => fun _ => False_rec _ _
|
adam@335
|
322 | S n' => fun _ => exist _ n' _
|
adam@335
|
323 end); abstract crush.
|
adam@335
|
324 Defined.
|
adam@335
|
325
|
adam@335
|
326 Print pred_strong4'.
|
adam@335
|
327 (* end thide *)
|
adam@335
|
328
|
adam@335
|
329 (** %\vspace{-.15in}% [[
|
adam@335
|
330 pred_strong4' =
|
adam@335
|
331 fun n : nat =>
|
adam@335
|
332 match n as n0 return (n0 > 0 -> {m : nat | n0 = S m}) with
|
adam@335
|
333 | 0 =>
|
adam@335
|
334 fun _H : 0 > 0 =>
|
adam@335
|
335 False_rec {m : nat | 0 = S m} (pred_strong4'_subproof n _H)
|
adam@335
|
336 | S n' =>
|
adam@335
|
337 fun _H : S n' > 0 =>
|
adam@335
|
338 exist (fun m : nat => S n' = S m) n' (pred_strong4'_subproof0 n _H)
|
adam@335
|
339 end
|
adam@335
|
340 : forall n : nat, n > 0 -> {m : nat | n = S m}
|
adam@335
|
341 ]]
|
adam@335
|
342
|
adam@338
|
343 We are almost done with the ideal implementation of dependent predecessor. We can use Coq's syntax extension facility to arrive at code with almost no complexity beyond a Haskell or ML program with a complete specification in a comment. In this book, we will not dwell on the details of syntax extensions; the Coq manual gives a straightforward introduction to them. *)
|
adamc@70
|
344
|
adamc@70
|
345 Notation "!" := (False_rec _ _).
|
adamc@70
|
346 Notation "[ e ]" := (exist _ e _).
|
adamc@70
|
347
|
adam@297
|
348 Definition pred_strong5 : forall n : nat, n > 0 -> {m : nat | n = S m}.
|
adamc@70
|
349 refine (fun n =>
|
adamc@212
|
350 match n with
|
adamc@70
|
351 | O => fun _ => !
|
adamc@70
|
352 | S n' => fun _ => [n']
|
adamc@70
|
353 end); crush.
|
adamc@70
|
354 Defined.
|
adamc@71
|
355
|
adam@282
|
356 (** By default, notations are also used in pretty-printing terms, including results of evaluation. *)
|
adam@282
|
357
|
adam@282
|
358 Eval compute in pred_strong5 two_gt0.
|
adam@282
|
359 (** %\vspace{-.15in}% [[
|
adam@282
|
360 = [1]
|
adam@282
|
361 : {m : nat | 2 = S m}
|
adam@282
|
362 ]]
|
adam@282
|
363
|
adam@335
|
364 One other alternative is worth demonstrating. Recent Coq versions include a facility called %\index{Program}%[Program] that streamlines this style of definition. Here is a complete implementation using [Program].%\index{Vernacular commands!Obligation Tactic}\index{Vernacular commands!Program Definition}% *)
|
adamc@212
|
365
|
adamc@212
|
366 Obligation Tactic := crush.
|
adamc@212
|
367
|
adamc@212
|
368 Program Definition pred_strong6 (n : nat) (_ : n > 0) : {m : nat | n = S m} :=
|
adamc@212
|
369 match n with
|
adamc@212
|
370 | O => _
|
adamc@212
|
371 | S n' => n'
|
adamc@212
|
372 end.
|
adamc@212
|
373
|
adam@335
|
374 (** Printing the resulting definition of [pred_strong6] yields a term very similar to what we built with [refine]. [Program] can save time in writing programs that use subset types. Nonetheless, [refine] is often just as effective, and [refine] gives you more control over the form the final term takes, which can be useful when you want to prove additional theorems about your definition. [Program] will sometimes insert type casts that can complicate theorem proving. *)
|
adamc@212
|
375
|
adam@282
|
376 Eval compute in pred_strong6 two_gt0.
|
adam@282
|
377 (** %\vspace{-.15in}% [[
|
adam@282
|
378 = [1]
|
adam@282
|
379 : {m : nat | 2 = S m}
|
adam@302
|
380 ]]
|
adam@335
|
381
|
adam@335
|
382 In this case, we see that the new definition yields the same computational behavior as before. *)
|
adam@282
|
383
|
adamc@71
|
384
|
adamc@71
|
385 (** * Decidable Proposition Types *)
|
adamc@71
|
386
|
adam@335
|
387 (** There is another type in the standard library which captures the idea of program values that indicate which of two propositions is true.%\index{Gallina terms!sumbool}% *)
|
adamc@71
|
388
|
adam@423
|
389 (* begin hide *)
|
adam@423
|
390 Definition baz := sumbool.
|
adam@423
|
391 (* end hide *)
|
adam@423
|
392
|
adamc@71
|
393 Print sumbool.
|
adamc@212
|
394 (** %\vspace{-.15in}% [[
|
adamc@71
|
395 Inductive sumbool (A : Prop) (B : Prop) : Set :=
|
adamc@71
|
396 left : A -> {A} + {B} | right : B -> {A} + {B}
|
adamc@212
|
397 ]]
|
adamc@71
|
398
|
adamc@212
|
399 We can define some notations to make working with [sumbool] more convenient. *)
|
adamc@71
|
400
|
adamc@71
|
401 Notation "'Yes'" := (left _ _).
|
adamc@71
|
402 Notation "'No'" := (right _ _).
|
adamc@71
|
403 Notation "'Reduce' x" := (if x then Yes else No) (at level 50).
|
adamc@71
|
404
|
adamc@71
|
405 (** The [Reduce] notation is notable because it demonstrates how [if] is overloaded in Coq. The [if] form actually works when the test expression has any two-constructor inductive type. Moreover, in the [then] and [else] branches, the appropriate constructor arguments are bound. This is important when working with [sumbool]s, when we want to have the proof stored in the test expression available when proving the proof obligations generated in the appropriate branch.
|
adamc@71
|
406
|
adamc@71
|
407 Now we can write [eq_nat_dec], which compares two natural numbers, returning either a proof of their equality or a proof of their inequality. *)
|
adamc@71
|
408
|
adam@297
|
409 Definition eq_nat_dec : forall n m : nat, {n = m} + {n <> m}.
|
adamc@212
|
410 refine (fix f (n m : nat) : {n = m} + {n <> m} :=
|
adamc@212
|
411 match n, m with
|
adamc@71
|
412 | O, O => Yes
|
adamc@71
|
413 | S n', S m' => Reduce (f n' m')
|
adamc@71
|
414 | _, _ => No
|
adamc@71
|
415 end); congruence.
|
adamc@71
|
416 Defined.
|
adamc@71
|
417
|
adam@282
|
418 Eval compute in eq_nat_dec 2 2.
|
adam@282
|
419 (** %\vspace{-.15in}% [[
|
adam@282
|
420 = Yes
|
adam@282
|
421 : {2 = 2} + {2 <> 2}
|
adam@302
|
422 ]]
|
adam@302
|
423 *)
|
adam@282
|
424
|
adam@282
|
425 Eval compute in eq_nat_dec 2 3.
|
adam@282
|
426 (** %\vspace{-.15in}% [[
|
adam@282
|
427 = No
|
adam@341
|
428 : {2 = 3} + {2 <> 3}
|
adam@302
|
429 ]]
|
adam@302
|
430 *)
|
adam@282
|
431
|
adam@335
|
432 (** Note that the [Yes] and [No] notations are hiding proofs establishing the correctness of the outputs.
|
adam@335
|
433
|
adam@335
|
434 Our definition extracts to reasonable OCaml code. *)
|
adamc@71
|
435
|
adamc@71
|
436 Extraction eq_nat_dec.
|
adamc@71
|
437
|
adamc@71
|
438 (** %\begin{verbatim}
|
adamc@71
|
439 (** val eq_nat_dec : nat -> nat -> sumbool **)
|
adamc@71
|
440
|
adamc@71
|
441 let rec eq_nat_dec n m =
|
adamc@71
|
442 match n with
|
adamc@71
|
443 | O -> (match m with
|
adamc@71
|
444 | O -> Left
|
adamc@71
|
445 | S n0 -> Right)
|
adamc@71
|
446 | S n' -> (match m with
|
adamc@71
|
447 | O -> Right
|
adamc@71
|
448 | S m' -> eq_nat_dec n' m')
|
adamc@71
|
449 \end{verbatim}%
|
adamc@71
|
450
|
adamc@71
|
451 #<pre>
|
adamc@71
|
452 (** val eq_nat_dec : nat -> nat -> sumbool **)
|
adamc@71
|
453
|
adamc@71
|
454 let rec eq_nat_dec n m =
|
adamc@71
|
455 match n with
|
adamc@71
|
456 | O -> (match m with
|
adamc@71
|
457 | O -> Left
|
adamc@71
|
458 | S n0 -> Right)
|
adamc@71
|
459 | S n' -> (match m with
|
adamc@71
|
460 | O -> Right
|
adamc@71
|
461 | S m' -> eq_nat_dec n' m')
|
adamc@71
|
462 </pre>#
|
adamc@71
|
463
|
adam@335
|
464 Proving this kind of decidable equality result is so common that Coq comes with a tactic for automating it.%\index{tactics!decide equality}% *)
|
adamc@71
|
465
|
adamc@71
|
466 Definition eq_nat_dec' (n m : nat) : {n = m} + {n <> m}.
|
adamc@71
|
467 decide equality.
|
adamc@71
|
468 Defined.
|
adamc@71
|
469
|
adam@423
|
470 (** Curious readers can verify that the [decide equality] version extracts to the same OCaml code as our more manual version does. That OCaml code had one undesirable property, which is that it uses <<Left>> and <<Right>> constructors instead of the boolean values built into OCaml. We can fix this, by using Coq's facility for mapping Coq inductive types to OCaml variant types.%\index{Vernacular commands!Extract Inductive}% *)
|
adamc@71
|
471
|
adamc@71
|
472 Extract Inductive sumbool => "bool" ["true" "false"].
|
adamc@71
|
473 Extraction eq_nat_dec'.
|
adamc@71
|
474
|
adamc@71
|
475 (** %\begin{verbatim}
|
adamc@71
|
476 (** val eq_nat_dec' : nat -> nat -> bool **)
|
adamc@71
|
477
|
adamc@71
|
478 let rec eq_nat_dec' n m0 =
|
adamc@71
|
479 match n with
|
adamc@71
|
480 | O -> (match m0 with
|
adamc@71
|
481 | O -> true
|
adamc@71
|
482 | S n0 -> false)
|
adamc@71
|
483 | S n0 -> (match m0 with
|
adamc@71
|
484 | O -> false
|
adamc@71
|
485 | S n1 -> eq_nat_dec' n0 n1)
|
adamc@71
|
486 \end{verbatim}%
|
adamc@71
|
487
|
adamc@71
|
488 #<pre>
|
adamc@71
|
489 (** val eq_nat_dec' : nat -> nat -> bool **)
|
adamc@71
|
490
|
adamc@71
|
491 let rec eq_nat_dec' n m0 =
|
adamc@71
|
492 match n with
|
adamc@71
|
493 | O -> (match m0 with
|
adamc@71
|
494 | O -> true
|
adamc@71
|
495 | S n0 -> false)
|
adamc@71
|
496 | S n0 -> (match m0 with
|
adamc@71
|
497 | O -> false
|
adamc@71
|
498 | S n1 -> eq_nat_dec' n0 n1)
|
adamc@71
|
499 </pre># *)
|
adamc@72
|
500
|
adamc@72
|
501 (** %\smallskip%
|
adamc@72
|
502
|
adam@423
|
503 We can build "smart" versions of the usual boolean operators and put them to good use in certified programming. For instance, here is a [sumbool] version of boolean "or." *)
|
adamc@72
|
504
|
adam@337
|
505 (* EX: Write a function that decides if an element belongs to a list. *)
|
adam@337
|
506
|
adamc@77
|
507 (* begin thide *)
|
adamc@204
|
508 Notation "x || y" := (if x then Yes else Reduce y).
|
adamc@72
|
509
|
adamc@72
|
510 (** Let us use it for building a function that decides list membership. We need to assume the existence of an equality decision procedure for the type of list elements. *)
|
adamc@72
|
511
|
adamc@72
|
512 Section In_dec.
|
adamc@72
|
513 Variable A : Set.
|
adamc@72
|
514 Variable A_eq_dec : forall x y : A, {x = y} + {x <> y}.
|
adamc@72
|
515
|
adamc@72
|
516 (** The final function is easy to write using the techniques we have developed so far. *)
|
adamc@72
|
517
|
adamc@212
|
518 Definition In_dec : forall (x : A) (ls : list A), {In x ls} + {~ In x ls}.
|
adamc@212
|
519 refine (fix f (x : A) (ls : list A) : {In x ls} + {~ In x ls} :=
|
adamc@212
|
520 match ls with
|
adamc@72
|
521 | nil => No
|
adamc@72
|
522 | x' :: ls' => A_eq_dec x x' || f x ls'
|
adamc@72
|
523 end); crush.
|
adam@282
|
524 Defined.
|
adamc@72
|
525 End In_dec.
|
adamc@72
|
526
|
adam@282
|
527 Eval compute in In_dec eq_nat_dec 2 (1 :: 2 :: nil).
|
adam@282
|
528 (** %\vspace{-.15in}% [[
|
adam@282
|
529 = Yes
|
adam@282
|
530 : {In 2 (1 :: 2 :: nil)} + {~ In 2 (1 :: 2 :: nil)}
|
adam@302
|
531 ]]
|
adam@302
|
532 *)
|
adam@282
|
533
|
adam@282
|
534 Eval compute in In_dec eq_nat_dec 3 (1 :: 2 :: nil).
|
adam@282
|
535 (** %\vspace{-.15in}% [[
|
adam@282
|
536 = No
|
adam@282
|
537 : {In 3 (1 :: 2 :: nil)} + {~ In 3 (1 :: 2 :: nil)}
|
adam@302
|
538 ]]
|
adam@302
|
539 *)
|
adam@282
|
540
|
adamc@72
|
541 (** [In_dec] has a reasonable extraction to OCaml. *)
|
adamc@72
|
542
|
adamc@72
|
543 Extraction In_dec.
|
adamc@77
|
544 (* end thide *)
|
adamc@72
|
545
|
adamc@72
|
546 (** %\begin{verbatim}
|
adamc@72
|
547 (** val in_dec : ('a1 -> 'a1 -> bool) -> 'a1 -> 'a1 list -> bool **)
|
adamc@72
|
548
|
adamc@72
|
549 let rec in_dec a_eq_dec x = function
|
adamc@72
|
550 | Nil -> false
|
adamc@72
|
551 | Cons (x', ls') ->
|
adamc@72
|
552 (match a_eq_dec x x' with
|
adamc@72
|
553 | true -> true
|
adamc@72
|
554 | false -> in_dec a_eq_dec x ls')
|
adamc@72
|
555 \end{verbatim}%
|
adamc@72
|
556
|
adamc@72
|
557 #<pre>
|
adamc@72
|
558 (** val in_dec : ('a1 -> 'a1 -> bool) -> 'a1 -> 'a1 list -> bool **)
|
adamc@72
|
559
|
adamc@72
|
560 let rec in_dec a_eq_dec x = function
|
adamc@72
|
561 | Nil -> false
|
adamc@72
|
562 | Cons (x', ls') ->
|
adamc@72
|
563 (match a_eq_dec x x' with
|
adamc@72
|
564 | true -> true
|
adamc@72
|
565 | false -> in_dec a_eq_dec x ls')
|
adam@403
|
566 </pre>#
|
adam@403
|
567
|
adam@403
|
568 This is more or the less code for the corresponding function from the OCaml standard library. *)
|
adamc@72
|
569
|
adamc@72
|
570
|
adamc@72
|
571 (** * Partial Subset Types *)
|
adamc@72
|
572
|
adam@335
|
573 (** Our final implementation of dependent predecessor used a very specific argument type to ensure that execution could always complete normally. Sometimes we want to allow execution to fail, and we want a more principled way of signaling failure than returning a default value, as [pred] does for [0]. One approach is to define this type family %\index{Gallina terms!maybe}%[maybe], which is a version of [sig] that allows obligation-free failure. *)
|
adamc@73
|
574
|
adamc@89
|
575 Inductive maybe (A : Set) (P : A -> Prop) : Set :=
|
adamc@72
|
576 | Unknown : maybe P
|
adamc@72
|
577 | Found : forall x : A, P x -> maybe P.
|
adamc@72
|
578
|
adamc@73
|
579 (** We can define some new notations, analogous to those we defined for subset types. *)
|
adamc@73
|
580
|
adamc@72
|
581 Notation "{{ x | P }}" := (maybe (fun x => P)).
|
adamc@72
|
582 Notation "??" := (Unknown _).
|
adam@335
|
583 Notation "[| x |]" := (Found _ x _).
|
adamc@72
|
584
|
adamc@73
|
585 (** Now our next version of [pred] is trivial to write. *)
|
adamc@73
|
586
|
adam@297
|
587 Definition pred_strong7 : forall n : nat, {{m | n = S m}}.
|
adamc@73
|
588 refine (fun n =>
|
adam@380
|
589 match n return {{m | n = S m}} with
|
adamc@73
|
590 | O => ??
|
adam@335
|
591 | S n' => [|n'|]
|
adamc@73
|
592 end); trivial.
|
adamc@73
|
593 Defined.
|
adamc@73
|
594
|
adam@282
|
595 Eval compute in pred_strong7 2.
|
adam@282
|
596 (** %\vspace{-.15in}% [[
|
adam@335
|
597 = [|1|]
|
adam@282
|
598 : {{m | 2 = S m}}
|
adam@335
|
599 ]]
|
adam@302
|
600 *)
|
adam@282
|
601
|
adam@282
|
602 Eval compute in pred_strong7 0.
|
adam@282
|
603 (** %\vspace{-.15in}% [[
|
adam@282
|
604 = ??
|
adam@282
|
605 : {{m | 0 = S m}}
|
adam@282
|
606 ]]
|
adam@282
|
607
|
adam@335
|
608 Because we used [maybe], one valid implementation of the type we gave [pred_strong7] would return [??] in every case. We can strengthen the type to rule out such vacuous implementations, and the type family %\index{Gallina terms!sumor}%[sumor] from the standard library provides the easiest starting point. For type [A] and proposition [B], [A + {B}] desugars to [sumor A B], whose values are either values of [A] or proofs of [B]. *)
|
adamc@73
|
609
|
adam@423
|
610 (* begin hide *)
|
adam@423
|
611 Definition sumor' := sumor.
|
adam@423
|
612 (* end hide *)
|
adam@423
|
613
|
adamc@73
|
614 Print sumor.
|
adamc@212
|
615 (** %\vspace{-.15in}% [[
|
adamc@73
|
616 Inductive sumor (A : Type) (B : Prop) : Type :=
|
adamc@73
|
617 inleft : A -> A + {B} | inright : B -> A + {B}
|
adam@302
|
618 ]]
|
adam@302
|
619 *)
|
adamc@73
|
620
|
adamc@73
|
621 (** We add notations for easy use of the [sumor] constructors. The second notation is specialized to [sumor]s whose [A] parameters are instantiated with regular subset types, since this is how we will use [sumor] below. *)
|
adamc@73
|
622
|
adamc@73
|
623 Notation "!!" := (inright _ _).
|
adam@335
|
624 Notation "[|| x ||]" := (inleft _ [x]).
|
adamc@73
|
625
|
adam@335
|
626 (** Now we are ready to give the final version of possibly failing predecessor. The [sumor]-based type that we use is maximally expressive; any implementation of the type has the same input-output behavior. *)
|
adamc@73
|
627
|
adam@297
|
628 Definition pred_strong8 : forall n : nat, {m : nat | n = S m} + {n = 0}.
|
adamc@73
|
629 refine (fun n =>
|
adamc@212
|
630 match n with
|
adamc@73
|
631 | O => !!
|
adam@335
|
632 | S n' => [||n'||]
|
adamc@73
|
633 end); trivial.
|
adamc@73
|
634 Defined.
|
adamc@73
|
635
|
adam@282
|
636 Eval compute in pred_strong8 2.
|
adam@282
|
637 (** %\vspace{-.15in}% [[
|
adam@335
|
638 = [||1||]
|
adam@282
|
639 : {m : nat | 2 = S m} + {2 = 0}
|
adam@302
|
640 ]]
|
adam@302
|
641 *)
|
adam@282
|
642
|
adam@282
|
643 Eval compute in pred_strong8 0.
|
adam@282
|
644 (** %\vspace{-.15in}% [[
|
adam@282
|
645 = !!
|
adam@282
|
646 : {m : nat | 0 = S m} + {0 = 0}
|
adam@302
|
647 ]]
|
adam@302
|
648 *)
|
adam@282
|
649
|
adam@335
|
650 (** As with our other maximally expressive [pred] function, we arrive at quite simple output values, thanks to notations. *)
|
adam@335
|
651
|
adamc@73
|
652
|
adamc@73
|
653 (** * Monadic Notations *)
|
adamc@73
|
654
|
adam@423
|
655 (** We can treat [maybe] like a monad%~\cite{Monads}\index{monad}\index{failure monad}%, in the same way that the Haskell <<Maybe>> type is interpreted as a failure monad. Our [maybe] has the wrong type to be a literal monad, but a "bind"-like notation will still be helpful. *)
|
adamc@73
|
656
|
adamc@72
|
657 Notation "x <- e1 ; e2" := (match e1 with
|
adamc@72
|
658 | Unknown => ??
|
adamc@72
|
659 | Found x _ => e2
|
adamc@72
|
660 end)
|
adamc@72
|
661 (right associativity, at level 60).
|
adamc@72
|
662
|
adam@398
|
663 (** The meaning of [x <- e1; e2] is: First run [e1]. If it fails to find an answer, then announce failure for our derived computation, too. If [e1] _does_ find an answer, pass that answer on to [e2] to find the final result. The variable [x] can be considered bound in [e2].
|
adamc@73
|
664
|
adam@335
|
665 This notation is very helpful for composing richly typed procedures. For instance, here is a very simple implementation of a function to take the predecessors of two naturals at once. *)
|
adamc@73
|
666
|
adam@337
|
667 (* EX: Write a function that tries to compute predecessors of two [nat]s at once. *)
|
adam@337
|
668
|
adam@337
|
669 (* begin thide *)
|
adam@297
|
670 Definition doublePred : forall n1 n2 : nat, {{p | n1 = S (fst p) /\ n2 = S (snd p)}}.
|
adamc@73
|
671 refine (fun n1 n2 =>
|
adamc@212
|
672 m1 <- pred_strong7 n1;
|
adamc@212
|
673 m2 <- pred_strong7 n2;
|
adam@335
|
674 [|(m1, m2)|]); tauto.
|
adamc@73
|
675 Defined.
|
adam@337
|
676 (* end thide *)
|
adamc@73
|
677
|
adam@423
|
678 (** We can build a [sumor] version of the "bind" notation and use it to write a similarly straightforward version of this function. %The operator rendered here as $\longleftarrow$ is noted in the source as a less-than character followed by two hyphens.% *)
|
adamc@73
|
679
|
adamc@73
|
680 Notation "x <-- e1 ; e2" := (match e1 with
|
adamc@73
|
681 | inright _ => !!
|
adamc@73
|
682 | inleft (exist x _) => e2
|
adamc@73
|
683 end)
|
adamc@73
|
684 (right associativity, at level 60).
|
adamc@73
|
685
|
adamc@73
|
686 (** printing * $\times$ *)
|
adamc@73
|
687
|
adam@337
|
688 (* EX: Write a more expressively typed version of the last exercise. *)
|
adam@337
|
689
|
adam@337
|
690 (* begin thide *)
|
adam@297
|
691 Definition doublePred' : forall n1 n2 : nat,
|
adam@297
|
692 {p : nat * nat | n1 = S (fst p) /\ n2 = S (snd p)}
|
adamc@73
|
693 + {n1 = 0 \/ n2 = 0}.
|
adamc@73
|
694 refine (fun n1 n2 =>
|
adamc@212
|
695 m1 <-- pred_strong8 n1;
|
adamc@212
|
696 m2 <-- pred_strong8 n2;
|
adam@335
|
697 [||(m1, m2)||]); tauto.
|
adamc@73
|
698 Defined.
|
adam@337
|
699 (* end thide *)
|
adamc@72
|
700
|
adam@392
|
701 (** This example demonstrates how judicious selection of notations can hide complexities in the rich types of programs. *)
|
adam@392
|
702
|
adamc@72
|
703
|
adamc@72
|
704 (** * A Type-Checking Example *)
|
adamc@72
|
705
|
adam@335
|
706 (** We can apply these specification types to build a certified type checker for a simple expression language. *)
|
adamc@75
|
707
|
adamc@72
|
708 Inductive exp : Set :=
|
adamc@72
|
709 | Nat : nat -> exp
|
adamc@72
|
710 | Plus : exp -> exp -> exp
|
adamc@72
|
711 | Bool : bool -> exp
|
adamc@72
|
712 | And : exp -> exp -> exp.
|
adamc@72
|
713
|
adamc@75
|
714 (** We define a simple language of types and its typing rules, in the style introduced in Chapter 4. *)
|
adamc@75
|
715
|
adamc@72
|
716 Inductive type : Set := TNat | TBool.
|
adamc@72
|
717
|
adamc@72
|
718 Inductive hasType : exp -> type -> Prop :=
|
adamc@72
|
719 | HtNat : forall n,
|
adamc@72
|
720 hasType (Nat n) TNat
|
adamc@72
|
721 | HtPlus : forall e1 e2,
|
adamc@72
|
722 hasType e1 TNat
|
adamc@72
|
723 -> hasType e2 TNat
|
adamc@72
|
724 -> hasType (Plus e1 e2) TNat
|
adamc@72
|
725 | HtBool : forall b,
|
adamc@72
|
726 hasType (Bool b) TBool
|
adamc@72
|
727 | HtAnd : forall e1 e2,
|
adamc@72
|
728 hasType e1 TBool
|
adamc@72
|
729 -> hasType e2 TBool
|
adamc@72
|
730 -> hasType (And e1 e2) TBool.
|
adamc@72
|
731
|
adamc@75
|
732 (** It will be helpful to have a function for comparing two types. We build one using [decide equality]. *)
|
adamc@75
|
733
|
adamc@77
|
734 (* begin thide *)
|
adamc@75
|
735 Definition eq_type_dec : forall t1 t2 : type, {t1 = t2} + {t1 <> t2}.
|
adamc@72
|
736 decide equality.
|
adamc@72
|
737 Defined.
|
adamc@72
|
738
|
adam@423
|
739 (** Another notation complements the monadic notation for [maybe] that we defined earlier. Sometimes we want to include "assertions" in our procedures. That is, we want to run a decision procedure and fail if it fails; otherwise, we want to continue, with the proof that it produced made available to us. This infix notation captures that idea, for a procedure that returns an arbitrary two-constructor type. *)
|
adamc@75
|
740
|
adamc@73
|
741 Notation "e1 ;; e2" := (if e1 then e2 else ??)
|
adamc@73
|
742 (right associativity, at level 60).
|
adamc@73
|
743
|
adam@335
|
744 (** With that notation defined, we can implement a [typeCheck] function, whose code is only more complex than what we would write in ML because it needs to include some extra type annotations. Every [[|e|]] expression adds a [hasType] proof obligation, and [crush] makes short work of them when we add [hasType]'s constructors as hints. *)
|
adamc@77
|
745 (* end thide *)
|
adamc@75
|
746
|
adam@297
|
747 Definition typeCheck : forall e : exp, {{t | hasType e t}}.
|
adamc@77
|
748 (* begin thide *)
|
adamc@72
|
749 Hint Constructors hasType.
|
adamc@72
|
750
|
adamc@72
|
751 refine (fix F (e : exp) : {{t | hasType e t}} :=
|
adam@380
|
752 match e return {{t | hasType e t}} with
|
adam@335
|
753 | Nat _ => [|TNat|]
|
adamc@72
|
754 | Plus e1 e2 =>
|
adamc@72
|
755 t1 <- F e1;
|
adamc@72
|
756 t2 <- F e2;
|
adamc@72
|
757 eq_type_dec t1 TNat;;
|
adamc@72
|
758 eq_type_dec t2 TNat;;
|
adam@335
|
759 [|TNat|]
|
adam@335
|
760 | Bool _ => [|TBool|]
|
adamc@72
|
761 | And e1 e2 =>
|
adamc@72
|
762 t1 <- F e1;
|
adamc@72
|
763 t2 <- F e2;
|
adamc@72
|
764 eq_type_dec t1 TBool;;
|
adamc@72
|
765 eq_type_dec t2 TBool;;
|
adam@335
|
766 [|TBool|]
|
adamc@72
|
767 end); crush.
|
adamc@77
|
768 (* end thide *)
|
adamc@72
|
769 Defined.
|
adamc@72
|
770
|
adamc@75
|
771 (** Despite manipulating proofs, our type checker is easy to run. *)
|
adamc@75
|
772
|
adamc@72
|
773 Eval simpl in typeCheck (Nat 0).
|
adamc@212
|
774 (** %\vspace{-.15in}% [[
|
adam@335
|
775 = [|TNat|]
|
adamc@75
|
776 : {{t | hasType (Nat 0) t}}
|
adam@302
|
777 ]]
|
adam@302
|
778 *)
|
adamc@75
|
779
|
adamc@72
|
780 Eval simpl in typeCheck (Plus (Nat 1) (Nat 2)).
|
adamc@212
|
781 (** %\vspace{-.15in}% [[
|
adam@335
|
782 = [|TNat|]
|
adamc@75
|
783 : {{t | hasType (Plus (Nat 1) (Nat 2)) t}}
|
adam@302
|
784 ]]
|
adam@302
|
785 *)
|
adamc@75
|
786
|
adamc@72
|
787 Eval simpl in typeCheck (Plus (Nat 1) (Bool false)).
|
adamc@212
|
788 (** %\vspace{-.15in}% [[
|
adamc@75
|
789 = ??
|
adamc@75
|
790 : {{t | hasType (Plus (Nat 1) (Bool false)) t}}
|
adam@302
|
791 ]]
|
adam@302
|
792 *)
|
adamc@75
|
793
|
adam@335
|
794 (** The type checker also extracts to some reasonable OCaml code. *)
|
adamc@75
|
795
|
adamc@75
|
796 Extraction typeCheck.
|
adamc@75
|
797
|
adamc@75
|
798 (** %\begin{verbatim}
|
adamc@75
|
799 (** val typeCheck : exp -> type0 maybe **)
|
adamc@75
|
800
|
adamc@75
|
801 let rec typeCheck = function
|
adamc@75
|
802 | Nat n -> Found TNat
|
adamc@75
|
803 | Plus (e1, e2) ->
|
adamc@75
|
804 (match typeCheck e1 with
|
adamc@75
|
805 | Unknown -> Unknown
|
adamc@75
|
806 | Found t1 ->
|
adamc@75
|
807 (match typeCheck e2 with
|
adamc@75
|
808 | Unknown -> Unknown
|
adamc@75
|
809 | Found t2 ->
|
adamc@75
|
810 (match eq_type_dec t1 TNat with
|
adamc@75
|
811 | true ->
|
adamc@75
|
812 (match eq_type_dec t2 TNat with
|
adamc@75
|
813 | true -> Found TNat
|
adamc@75
|
814 | false -> Unknown)
|
adamc@75
|
815 | false -> Unknown)))
|
adamc@75
|
816 | Bool b -> Found TBool
|
adamc@75
|
817 | And (e1, e2) ->
|
adamc@75
|
818 (match typeCheck e1 with
|
adamc@75
|
819 | Unknown -> Unknown
|
adamc@75
|
820 | Found t1 ->
|
adamc@75
|
821 (match typeCheck e2 with
|
adamc@75
|
822 | Unknown -> Unknown
|
adamc@75
|
823 | Found t2 ->
|
adamc@75
|
824 (match eq_type_dec t1 TBool with
|
adamc@75
|
825 | true ->
|
adamc@75
|
826 (match eq_type_dec t2 TBool with
|
adamc@75
|
827 | true -> Found TBool
|
adamc@75
|
828 | false -> Unknown)
|
adamc@75
|
829 | false -> Unknown)))
|
adamc@75
|
830 \end{verbatim}%
|
adamc@75
|
831
|
adamc@75
|
832 #<pre>
|
adamc@75
|
833 (** val typeCheck : exp -> type0 maybe **)
|
adamc@75
|
834
|
adamc@75
|
835 let rec typeCheck = function
|
adamc@75
|
836 | Nat n -> Found TNat
|
adamc@75
|
837 | Plus (e1, e2) ->
|
adamc@75
|
838 (match typeCheck e1 with
|
adamc@75
|
839 | Unknown -> Unknown
|
adamc@75
|
840 | Found t1 ->
|
adamc@75
|
841 (match typeCheck e2 with
|
adamc@75
|
842 | Unknown -> Unknown
|
adamc@75
|
843 | Found t2 ->
|
adamc@75
|
844 (match eq_type_dec t1 TNat with
|
adamc@75
|
845 | true ->
|
adamc@75
|
846 (match eq_type_dec t2 TNat with
|
adamc@75
|
847 | true -> Found TNat
|
adamc@75
|
848 | false -> Unknown)
|
adamc@75
|
849 | false -> Unknown)))
|
adamc@75
|
850 | Bool b -> Found TBool
|
adamc@75
|
851 | And (e1, e2) ->
|
adamc@75
|
852 (match typeCheck e1 with
|
adamc@75
|
853 | Unknown -> Unknown
|
adamc@75
|
854 | Found t1 ->
|
adamc@75
|
855 (match typeCheck e2 with
|
adamc@75
|
856 | Unknown -> Unknown
|
adamc@75
|
857 | Found t2 ->
|
adamc@75
|
858 (match eq_type_dec t1 TBool with
|
adamc@75
|
859 | true ->
|
adamc@75
|
860 (match eq_type_dec t2 TBool with
|
adamc@75
|
861 | true -> Found TBool
|
adamc@75
|
862 | false -> Unknown)
|
adamc@75
|
863 | false -> Unknown)))
|
adamc@75
|
864 </pre># *)
|
adamc@75
|
865
|
adamc@75
|
866 (** %\smallskip%
|
adamc@75
|
867
|
adam@423
|
868 We can adapt this implementation to use [sumor], so that we know our type-checker only fails on ill-typed inputs. First, we define an analogue to the "assertion" notation. *)
|
adamc@73
|
869
|
adamc@77
|
870 (* begin thide *)
|
adamc@73
|
871 Notation "e1 ;;; e2" := (if e1 then e2 else !!)
|
adamc@73
|
872 (right associativity, at level 60).
|
adamc@73
|
873
|
adamc@75
|
874 (** Next, we prove a helpful lemma, which states that a given expression can have at most one type. *)
|
adamc@75
|
875
|
adamc@75
|
876 Lemma hasType_det : forall e t1,
|
adamc@73
|
877 hasType e t1
|
adam@335
|
878 -> forall t2, hasType e t2
|
adamc@73
|
879 -> t1 = t2.
|
adamc@73
|
880 induction 1; inversion 1; crush.
|
adamc@73
|
881 Qed.
|
adamc@73
|
882
|
adamc@75
|
883 (** Now we can define the type-checker. Its type expresses that it only fails on untypable expressions. *)
|
adamc@75
|
884
|
adamc@77
|
885 (* end thide *)
|
adam@297
|
886 Definition typeCheck' : forall e : exp, {t : type | hasType e t} + {forall t, ~ hasType e t}.
|
adamc@77
|
887 (* begin thide *)
|
adamc@73
|
888 Hint Constructors hasType.
|
adamc@75
|
889 (** We register all of the typing rules as hints. *)
|
adamc@75
|
890
|
adamc@73
|
891 Hint Resolve hasType_det.
|
adam@335
|
892 (** The lemma [hasType_det] will also be useful for proving proof obligations with contradictory contexts. Since its statement includes [forall]-bound variables that do not appear in its conclusion, only [eauto] will apply this hint. *)
|
adamc@73
|
893
|
adamc@75
|
894 (** Finally, the implementation of [typeCheck] can be transcribed literally, simply switching notations as needed. *)
|
adamc@212
|
895
|
adamc@212
|
896 refine (fix F (e : exp) : {t : type | hasType e t} + {forall t, ~ hasType e t} :=
|
adam@380
|
897 match e return {t : type | hasType e t} + {forall t, ~ hasType e t} with
|
adam@335
|
898 | Nat _ => [||TNat||]
|
adamc@73
|
899 | Plus e1 e2 =>
|
adamc@73
|
900 t1 <-- F e1;
|
adamc@73
|
901 t2 <-- F e2;
|
adamc@73
|
902 eq_type_dec t1 TNat;;;
|
adamc@73
|
903 eq_type_dec t2 TNat;;;
|
adam@335
|
904 [||TNat||]
|
adam@335
|
905 | Bool _ => [||TBool||]
|
adamc@73
|
906 | And e1 e2 =>
|
adamc@73
|
907 t1 <-- F e1;
|
adamc@73
|
908 t2 <-- F e2;
|
adamc@73
|
909 eq_type_dec t1 TBool;;;
|
adamc@73
|
910 eq_type_dec t2 TBool;;;
|
adam@335
|
911 [||TBool||]
|
adamc@73
|
912 end); clear F; crush' tt hasType; eauto.
|
adamc@75
|
913
|
adam@335
|
914 (** We clear [F], the local name for the recursive function, to avoid strange proofs that refer to recursive calls that we never make. The [crush] variant %\index{tactics!crush'}%[crush'] helps us by performing automatic inversion on instances of the predicates specified in its second argument. Once we throw in [eauto] to apply [hasType_det] for us, we have discharged all the subgoals. *)
|
adamc@77
|
915 (* end thide *)
|
adamc@212
|
916
|
adamc@212
|
917
|
adamc@73
|
918 Defined.
|
adamc@73
|
919
|
adamc@75
|
920 (** The short implementation here hides just how time-saving automation is. Every use of one of the notations adds a proof obligation, giving us 12 in total. Most of these obligations require multiple inversions and either uses of [hasType_det] or applications of [hasType] rules.
|
adamc@75
|
921
|
adam@335
|
922 Our new function remains easy to test: *)
|
adamc@75
|
923
|
adamc@73
|
924 Eval simpl in typeCheck' (Nat 0).
|
adamc@212
|
925 (** %\vspace{-.15in}% [[
|
adam@335
|
926 = [||TNat||]
|
adamc@75
|
927 : {t : type | hasType (Nat 0) t} +
|
adamc@75
|
928 {(forall t : type, ~ hasType (Nat 0) t)}
|
adam@302
|
929 ]]
|
adam@302
|
930 *)
|
adamc@75
|
931
|
adamc@73
|
932 Eval simpl in typeCheck' (Plus (Nat 1) (Nat 2)).
|
adamc@212
|
933 (** %\vspace{-.15in}% [[
|
adam@335
|
934 = [||TNat||]
|
adamc@75
|
935 : {t : type | hasType (Plus (Nat 1) (Nat 2)) t} +
|
adamc@75
|
936 {(forall t : type, ~ hasType (Plus (Nat 1) (Nat 2)) t)}
|
adam@302
|
937 ]]
|
adam@302
|
938 *)
|
adamc@75
|
939
|
adamc@73
|
940 Eval simpl in typeCheck' (Plus (Nat 1) (Bool false)).
|
adamc@212
|
941 (** %\vspace{-.15in}% [[
|
adamc@75
|
942 = !!
|
adamc@75
|
943 : {t : type | hasType (Plus (Nat 1) (Bool false)) t} +
|
adamc@75
|
944 {(forall t : type, ~ hasType (Plus (Nat 1) (Bool false)) t)}
|
adam@302
|
945 ]]
|
adam@335
|
946
|
adam@335
|
947 The results of simplifying calls to [typeCheck'] look deceptively similar to the results for [typeCheck], but now the types of the results provide more information. *)
|