Mercurial > cpdt > repo
comparison src/Subset.v @ 335:1f57a8d0ed3d
Pass over Subset
author | Adam Chlipala <adam@chlipala.net> |
---|---|
date | Wed, 05 Oct 2011 11:32:13 -0400 |
parents | d5787b70cf48 |
children | 4186722d329b |
comparison
equal
deleted
inserted
replaced
334:386b7ad8849b | 335:1f57a8d0ed3d |
---|---|
18 | 18 |
19 (** %\part{Programming with Dependent Types} | 19 (** %\part{Programming with Dependent Types} |
20 | 20 |
21 \chapter{Subset Types and Variations}% *) | 21 \chapter{Subset Types and Variations}% *) |
22 | 22 |
23 (** 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 %\textit{%#<i>#dependent types#</i>#%}% to integrate programming, specification, and proving into a single phase. *) | 23 (** 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}\textit{%#<i>#dependent types#</i>#%}% 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. *) |
24 | 24 |
25 | 25 |
26 (** * Introducing Subset Types *) | 26 (** * Introducing Subset Types *) |
27 | 27 |
28 (** Let us consider several ways of implementing the natural number predecessor function. We start by displaying the definition from the standard library: *) | 28 (** Let us consider several ways of implementing the natural number predecessor function. We start by displaying the definition from the standard library: *) |
35 end | 35 end |
36 : nat -> nat | 36 : nat -> nat |
37 | 37 |
38 ]] | 38 ]] |
39 | 39 |
40 We can use a new command, [Extraction], to produce an OCaml version of this function. *) | 40 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. *) |
41 | 41 |
42 Extraction pred. | 42 Extraction pred. |
43 | 43 |
44 (** %\begin{verbatim} | 44 (** %\begin{verbatim} |
45 (** val pred : nat -> nat **) | 45 (** val pred : nat -> nat **) |
90 Definition pred_strong1' (n : nat) (pf : n > 0) : nat := | 90 Definition pred_strong1' (n : nat) (pf : n > 0) : nat := |
91 match n with | 91 match n with |
92 | O => match zgtz pf with end | 92 | O => match zgtz pf with end |
93 | S n' => n' | 93 | S n' => n' |
94 end. | 94 end. |
95 | 95 ]] |
96 | |
97 << | |
96 Error: In environment | 98 Error: In environment |
97 n : nat | 99 n : nat |
98 pf : n > 0 | 100 pf : n > 0 |
99 The term "pf" has type "n > 0" while it is expected to have type | 101 The term "pf" has type "n > 0" while it is expected to have type |
100 "0 > 0" | 102 "0 > 0" |
101 | 103 >> |
102 ]] | |
103 | 104 |
104 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. | 105 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. |
105 | 106 |
106 In this case, we must use a [return] annotation to declare the relationship between the %\textit{%#<i>#value#</i>#%}% of the [match] discriminee and the %\textit{%#<i>#type#</i>#%}% 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. | 107 In this case, we must use a [return] annotation to declare the relationship between the %\textit{%#<i>#value#</i>#%}% of the [match] discriminee and the %\textit{%#<i>#type#</i>#%}% 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. |
107 | 108 |
108 We are lucky that Coq's heuristics infer the [return] clause (specifically, [return n > 0 -> nat]) for us in this case. In general, however, the inference problem is undecidable. The known undecidable problem of %\textit{%#<i>#higher-order unification#</i>#%}% 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. | 109 We are lucky that Coq's heuristics infer the [return] clause (specifically, [return n > 0 -> nat]) for us in this case. *) |
110 | |
111 Definition pred_strong1' (n : nat) : n > 0 -> nat := | |
112 match n return n > 0 -> nat with | |
113 | O => fun pf : 0 > 0 => match zgtz pf with end | |
114 | S n' => fun _ => n' | |
115 end. | |
116 | |
117 (** 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}\textit{%#<i>#higher-order unification#</i>#%}~\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. | |
109 | 118 |
110 Let us now take a look at the OCaml code Coq generates for [pred_strong1]. *) | 119 Let us now take a look at the OCaml code Coq generates for [pred_strong1]. *) |
111 | 120 |
112 Extraction pred_strong1. | 121 Extraction pred_strong1. |
113 | 122 |
127 | S n' -> n' | 136 | S n' -> n' |
128 </pre># *) | 137 </pre># *) |
129 | 138 |
130 (** 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. | 139 (** 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. |
131 | 140 |
132 We can reimplement our dependently-typed [pred] based on %\textit{%#<i>#subset types#</i>#%}%, defined in the standard library with the type family [sig]. *) | 141 We can reimplement our dependently typed [pred] based on %\index{subset types}\textit{%#<i>#subset types#</i>#%}%, defined in the standard library with the type family %\index{Gallina terms!sig}%[sig]. *) |
133 | 142 |
134 Print sig. | 143 Print sig. |
135 (** %\vspace{-.15in}% [[ | 144 (** %\vspace{-.15in}% [[ |
136 Inductive sig (A : Type) (P : A -> Prop) : Type := | 145 Inductive sig (A : Type) (P : A -> Prop) : Type := |
137 exist : forall x : A, P x -> sig P | 146 exist : forall x : A, P x -> sig P |
138 For sig: Argument A is implicit | |
139 For exist: Argument A is implicit | |
140 | 147 |
141 ]] | 148 ]] |
142 | 149 |
143 [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. | 150 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. |
144 | 151 |
145 We rewrite [pred_strong1], using some syntactic sugar for subset types. *) | 152 We rewrite [pred_strong1], using some syntactic sugar for subset types. *) |
146 | 153 |
147 Locate "{ _ : _ | _ }". | 154 Locate "{ _ : _ | _ }". |
148 (** %\vspace{-.15in}% [[ | 155 (** %\vspace{-.15in}% [[ |
149 Notation Scope | 156 Notation |
150 "{ x : A | P }" := sig (fun x : A => P) | 157 "{ x : A | P }" := sig (fun x : A => P) |
151 : type_scope | |
152 (default interpretation) | |
153 ]] | 158 ]] |
154 *) | 159 *) |
155 | 160 |
156 Definition pred_strong2 (s : {n : nat | n > 0}) : nat := | 161 Definition pred_strong2 (s : {n : nat | n > 0}) : nat := |
157 match s with | 162 match s with |
158 | exist O pf => match zgtz pf with end | 163 | exist O pf => match zgtz pf with end |
159 | exist (S n') _ => n' | 164 | exist (S n') _ => n' |
160 end. | 165 end. |
161 | 166 |
162 (** 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. *) | 167 (** 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). *) |
163 | 168 |
164 Eval compute in pred_strong2 (exist _ 2 two_gt0). | 169 Eval compute in pred_strong2 (exist _ 2 two_gt0). |
165 (** %\vspace{-.15in}% [[ | 170 (** %\vspace{-.15in}% [[ |
166 = 1 | 171 = 1 |
167 : nat | 172 : nat |
168 | |
169 ]] | 173 ]] |
170 *) | 174 *) |
171 | 175 |
172 Extraction pred_strong2. | 176 Extraction pred_strong2. |
173 | 177 |
199 | 203 |
200 Eval compute in pred_strong3 (exist _ 2 two_gt0). | 204 Eval compute in pred_strong3 (exist _ 2 two_gt0). |
201 (** %\vspace{-.15in}% [[ | 205 (** %\vspace{-.15in}% [[ |
202 = exist (fun m : nat => 2 = S m) 1 (refl_equal 2) | 206 = exist (fun m : nat => 2 = S m) 1 (refl_equal 2) |
203 : {m : nat | proj1_sig (exist (lt 0) 2 two_gt0) = S m} | 207 : {m : nat | proj1_sig (exist (lt 0) 2 two_gt0) = S m} |
204 | 208 ]] |
205 ]] | 209 *) |
206 *) | 210 |
207 | 211 (** 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. |
208 (** The function [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. | |
209 | 212 |
210 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. *) | 213 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. *) |
211 | 214 |
212 Extraction pred_strong3. | 215 Extraction pred_strong3. |
213 | 216 |
225 let pred_strong3 = function | 228 let pred_strong3 = function |
226 | O -> assert false (* absurd case *) | 229 | O -> assert false (* absurd case *) |
227 | S n' -> n' | 230 | S n' -> n' |
228 </pre># | 231 </pre># |
229 | 232 |
230 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. *) | 233 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].) *) |
231 | 234 |
232 Definition pred_strong4 : forall n : nat, n > 0 -> {m : nat | n = S m}. | 235 Definition pred_strong4 : forall n : nat, n > 0 -> {m : nat | n = S m}. |
233 refine (fun n => | 236 refine (fun n => |
234 match n with | 237 match n with |
235 | O => fun _ => False_rec _ _ | 238 | O => fun _ => False_rec _ _ |
236 | S n' => fun _ => exist _ n' _ | 239 | S n' => fun _ => exist _ n' _ |
237 end). | 240 end). |
238 | 241 |
239 (* begin thide *) | 242 (* begin thide *) |
240 (** 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. We do most of the work with the [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: | 243 (** 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. |
241 | 244 |
242 [[ | 245 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: |
243 2 subgoals | 246 |
247 %\vspace{.1in} \noindent 2 \coqdockw{subgoals}\vspace{-.1in}%#<tt>2 subgoals</tt># | |
248 [[ | |
244 | 249 |
245 n : nat | 250 n : nat |
246 _ : 0 > 0 | 251 _ : 0 > 0 |
247 ============================ | 252 ============================ |
248 False | 253 False |
249 | 254 ]] |
250 subgoal 2 is: | 255 %\noindent \coqdockw{subgoal} 2 \coqdockw{is}:%#<tt>subgoal 2 is</tt># |
256 [[ | |
251 S n' = S n' | 257 S n' = S n' |
252 | |
253 ]] | 258 ]] |
254 | 259 |
255 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. *) | 260 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. *) |
256 | 261 |
257 Undo. | 262 Undo. |
261 | S n' => fun _ => exist _ n' _ | 266 | S n' => fun _ => exist _ n' _ |
262 end); crush. | 267 end); crush. |
263 (* end thide *) | 268 (* end thide *) |
264 Defined. | 269 Defined. |
265 | 270 |
266 (** We end the %``%#"#proof#"#%''% with [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. Let us see what our proof script constructed. *) | 271 (** 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}\emph{%#<i>#transparent#</i>#%}%, allowing it to be unfolded; while [Qed] marks an identifier as %\index{opaque}\emph{%#<i>#opaque#</i>#%}%, preventing unfolding.) Let us see what our proof script constructed. *) |
267 | 272 |
268 Print pred_strong4. | 273 Print pred_strong4. |
269 (** %\vspace{-.15in}% [[ | 274 (** %\vspace{-.15in}% [[ |
270 pred_strong4 = | 275 pred_strong4 = |
271 fun n : nat => | 276 fun n : nat => |
289 | 294 |
290 Eval compute in pred_strong4 two_gt0. | 295 Eval compute in pred_strong4 two_gt0. |
291 (** %\vspace{-.15in}% [[ | 296 (** %\vspace{-.15in}% [[ |
292 = exist (fun m : nat => 2 = S m) 1 (refl_equal 2) | 297 = exist (fun m : nat => 2 = S m) 1 (refl_equal 2) |
293 : {m : nat | 2 = S m} | 298 : {m : nat | 2 = S m} |
294 | 299 ]] |
295 ]] | 300 |
296 | 301 A tactic modifier called %\index{tactics!abstract}%[abstract] can be helpful for producing shorter terms, by automatically abstracting subgoals into named lemmas. *) |
297 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. *) | 302 |
303 (* begin thide *) | |
304 Definition pred_strong4' : forall n : nat, n > 0 -> {m : nat | n = S m}. | |
305 refine (fun n => | |
306 match n with | |
307 | O => fun _ => False_rec _ _ | |
308 | S n' => fun _ => exist _ n' _ | |
309 end); abstract crush. | |
310 Defined. | |
311 | |
312 Print pred_strong4'. | |
313 (* end thide *) | |
314 | |
315 (** %\vspace{-.15in}% [[ | |
316 pred_strong4' = | |
317 fun n : nat => | |
318 match n as n0 return (n0 > 0 -> {m : nat | n0 = S m}) with | |
319 | 0 => | |
320 fun _H : 0 > 0 => | |
321 False_rec {m : nat | 0 = S m} (pred_strong4'_subproof n _H) | |
322 | S n' => | |
323 fun _H : S n' > 0 => | |
324 exist (fun m : nat => S n' = S m) n' (pred_strong4'_subproof0 n _H) | |
325 end | |
326 : forall n : nat, n > 0 -> {m : nat | n = S m} | |
327 ]] | |
328 | |
329 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. *) | |
298 | 330 |
299 Notation "!" := (False_rec _ _). | 331 Notation "!" := (False_rec _ _). |
300 Notation "[ e ]" := (exist _ e _). | 332 Notation "[ e ]" := (exist _ e _). |
301 | 333 |
302 Definition pred_strong5 : forall n : nat, n > 0 -> {m : nat | n = S m}. | 334 Definition pred_strong5 : forall n : nat, n > 0 -> {m : nat | n = S m}. |
311 | 343 |
312 Eval compute in pred_strong5 two_gt0. | 344 Eval compute in pred_strong5 two_gt0. |
313 (** %\vspace{-.15in}% [[ | 345 (** %\vspace{-.15in}% [[ |
314 = [1] | 346 = [1] |
315 : {m : nat | 2 = S m} | 347 : {m : nat | 2 = S m} |
316 | 348 ]] |
317 ]] | 349 |
318 | 350 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}% *) |
319 One other alternative is worth demonstrating. Recent Coq versions include a facility called [Program] that streamlines this style of definition. Here is a complete implementation using [Program]. *) | |
320 | 351 |
321 Obligation Tactic := crush. | 352 Obligation Tactic := crush. |
322 | 353 |
323 Program Definition pred_strong6 (n : nat) (_ : n > 0) : {m : nat | n = S m} := | 354 Program Definition pred_strong6 (n : nat) (_ : n > 0) : {m : nat | n = S m} := |
324 match n with | 355 match n with |
325 | O => _ | 356 | O => _ |
326 | S n' => n' | 357 | S n' => n' |
327 end. | 358 end. |
328 | 359 |
329 (** 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. *) | 360 (** 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. *) |
330 | 361 |
331 Eval compute in pred_strong6 two_gt0. | 362 Eval compute in pred_strong6 two_gt0. |
332 (** %\vspace{-.15in}% [[ | 363 (** %\vspace{-.15in}% [[ |
333 = [1] | 364 = [1] |
334 : {m : nat | 2 = S m} | 365 : {m : nat | 2 = S m} |
335 | 366 ]] |
336 ]] | 367 |
337 *) | 368 In this case, we see that the new definition yields the same computational behavior as before. *) |
338 | 369 |
339 | 370 |
340 (** * Decidable Proposition Types *) | 371 (** * Decidable Proposition Types *) |
341 | 372 |
342 (** There is another type in the standard library which captures the idea of program values that indicate which of two propositions is true. *) | 373 (** 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}% *) |
343 | 374 |
344 Print sumbool. | 375 Print sumbool. |
345 (** %\vspace{-.15in}% [[ | 376 (** %\vspace{-.15in}% [[ |
346 Inductive sumbool (A : Prop) (B : Prop) : Set := | 377 Inductive sumbool (A : Prop) (B : Prop) : Set := |
347 left : A -> {A} + {B} | right : B -> {A} + {B} | 378 left : A -> {A} + {B} | right : B -> {A} + {B} |
348 For left: Argument A is implicit | |
349 For right: Argument B is implicit | |
350 | |
351 ]] | 379 ]] |
352 | 380 |
353 We can define some notations to make working with [sumbool] more convenient. *) | 381 We can define some notations to make working with [sumbool] more convenient. *) |
354 | 382 |
355 Notation "'Yes'" := (left _ _). | 383 Notation "'Yes'" := (left _ _). |
371 | 399 |
372 Eval compute in eq_nat_dec 2 2. | 400 Eval compute in eq_nat_dec 2 2. |
373 (** %\vspace{-.15in}% [[ | 401 (** %\vspace{-.15in}% [[ |
374 = Yes | 402 = Yes |
375 : {2 = 2} + {2 <> 2} | 403 : {2 = 2} + {2 <> 2} |
376 | |
377 ]] | 404 ]] |
378 *) | 405 *) |
379 | 406 |
380 Eval compute in eq_nat_dec 2 3. | 407 Eval compute in eq_nat_dec 2 3. |
381 (** %\vspace{-.15in}% [[ | 408 (** %\vspace{-.15in}% [[ |
382 = No | 409 = No |
383 : {2 = 2} + {2 <> 2} | 410 : {2 = 2} + {2 <> 2} |
384 | 411 ]] |
385 ]] | 412 *) |
386 *) | 413 |
387 | 414 (** Note that the [Yes] and [No] notations are hiding proofs establishing the correctness of the outputs. |
388 (** Our definition extracts to reasonable OCaml code. *) | 415 |
416 Our definition extracts to reasonable OCaml code. *) | |
389 | 417 |
390 Extraction eq_nat_dec. | 418 Extraction eq_nat_dec. |
391 | 419 |
392 (** %\begin{verbatim} | 420 (** %\begin{verbatim} |
393 (** val eq_nat_dec : nat -> nat -> sumbool **) | 421 (** val eq_nat_dec : nat -> nat -> sumbool **) |
413 | S n' -> (match m with | 441 | S n' -> (match m with |
414 | O -> Right | 442 | O -> Right |
415 | S m' -> eq_nat_dec n' m') | 443 | S m' -> eq_nat_dec n' m') |
416 </pre># | 444 </pre># |
417 | 445 |
418 Proving this kind of decidable equality result is so common that Coq comes with a tactic for automating it. *) | 446 Proving this kind of decidable equality result is so common that Coq comes with a tactic for automating it.%\index{tactics!decide equality}% *) |
419 | 447 |
420 Definition eq_nat_dec' (n m : nat) : {n = m} + {n <> m}. | 448 Definition eq_nat_dec' (n m : nat) : {n = m} + {n <> m}. |
421 decide equality. | 449 decide equality. |
422 Defined. | 450 Defined. |
423 | 451 |
424 (** 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 %\texttt{%#<tt>#Left#</tt>#%}% and %\texttt{%#<tt>#Right#</tt>#%}% 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. *) | 452 (** 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 %\texttt{%#<tt>#Left#</tt>#%}% and %\texttt{%#<tt>#Right#</tt>#%}% 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}% *) |
425 | 453 |
426 Extract Inductive sumbool => "bool" ["true" "false"]. | 454 Extract Inductive sumbool => "bool" ["true" "false"]. |
427 Extraction eq_nat_dec'. | 455 Extraction eq_nat_dec'. |
428 | 456 |
429 (** %\begin{verbatim} | 457 (** %\begin{verbatim} |
478 | 506 |
479 Eval compute in In_dec eq_nat_dec 2 (1 :: 2 :: nil). | 507 Eval compute in In_dec eq_nat_dec 2 (1 :: 2 :: nil). |
480 (** %\vspace{-.15in}% [[ | 508 (** %\vspace{-.15in}% [[ |
481 = Yes | 509 = Yes |
482 : {In 2 (1 :: 2 :: nil)} + {~ In 2 (1 :: 2 :: nil)} | 510 : {In 2 (1 :: 2 :: nil)} + {~ In 2 (1 :: 2 :: nil)} |
483 | |
484 ]] | 511 ]] |
485 *) | 512 *) |
486 | 513 |
487 Eval compute in In_dec eq_nat_dec 3 (1 :: 2 :: nil). | 514 Eval compute in In_dec eq_nat_dec 3 (1 :: 2 :: nil). |
488 (** %\vspace{-.15in}% [[ | 515 (** %\vspace{-.15in}% [[ |
489 = No | 516 = No |
490 : {In 3 (1 :: 2 :: nil)} + {~ In 3 (1 :: 2 :: nil)} | 517 : {In 3 (1 :: 2 :: nil)} + {~ In 3 (1 :: 2 :: nil)} |
491 | |
492 ]] | 518 ]] |
493 *) | 519 *) |
494 | 520 |
495 (** [In_dec] has a reasonable extraction to OCaml. *) | 521 (** [In_dec] has a reasonable extraction to OCaml. *) |
496 | 522 |
520 </pre># *) | 546 </pre># *) |
521 | 547 |
522 | 548 |
523 (** * Partial Subset Types *) | 549 (** * Partial Subset Types *) |
524 | 550 |
525 (** 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 that than returning a default value, as [pred] does for [0]. One approach is to define this type family [maybe], which is a version of [sig] that allows obligation-free failure. *) | 551 (** 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. *) |
526 | 552 |
527 Inductive maybe (A : Set) (P : A -> Prop) : Set := | 553 Inductive maybe (A : Set) (P : A -> Prop) : Set := |
528 | Unknown : maybe P | 554 | Unknown : maybe P |
529 | Found : forall x : A, P x -> maybe P. | 555 | Found : forall x : A, P x -> maybe P. |
530 | 556 |
531 (** We can define some new notations, analogous to those we defined for subset types. *) | 557 (** We can define some new notations, analogous to those we defined for subset types. *) |
532 | 558 |
533 Notation "{{ x | P }}" := (maybe (fun x => P)). | 559 Notation "{{ x | P }}" := (maybe (fun x => P)). |
534 Notation "??" := (Unknown _). | 560 Notation "??" := (Unknown _). |
535 Notation "[[ x ]]" := (Found _ x _). | 561 Notation "[| x |]" := (Found _ x _). |
536 | 562 |
537 (** Now our next version of [pred] is trivial to write. *) | 563 (** Now our next version of [pred] is trivial to write. *) |
538 | 564 |
539 Definition pred_strong7 : forall n : nat, {{m | n = S m}}. | 565 Definition pred_strong7 : forall n : nat, {{m | n = S m}}. |
540 refine (fun n => | 566 refine (fun n => |
541 match n with | 567 match n with |
542 | O => ?? | 568 | O => ?? |
543 | S n' => [[n']] | 569 | S n' => [|n'|] |
544 end); trivial. | 570 end); trivial. |
545 Defined. | 571 Defined. |
546 | 572 |
547 Eval compute in pred_strong7 2. | 573 Eval compute in pred_strong7 2. |
548 (** %\vspace{-.15in}% [[ | 574 (** %\vspace{-.15in}% [[ |
549 = [[1]] | 575 = [|1|] |
550 : {{m | 2 = S m}} | 576 : {{m | 2 = S m}} |
551 | 577 ]] |
552 ]] | |
553 *) | 578 *) |
554 | 579 |
555 Eval compute in pred_strong7 0. | 580 Eval compute in pred_strong7 0. |
556 (** %\vspace{-.15in}% [[ | 581 (** %\vspace{-.15in}% [[ |
557 = ?? | 582 = ?? |
558 : {{m | 0 = S m}} | 583 : {{m | 0 = S m}} |
559 | 584 ]] |
560 ]] | 585 |
561 | 586 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]. *) |
562 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 [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]. *) | |
563 | 587 |
564 Print sumor. | 588 Print sumor. |
565 (** %\vspace{-.15in}% [[ | 589 (** %\vspace{-.15in}% [[ |
566 Inductive sumor (A : Type) (B : Prop) : Type := | 590 Inductive sumor (A : Type) (B : Prop) : Type := |
567 inleft : A -> A + {B} | inright : B -> A + {B} | 591 inleft : A -> A + {B} | inright : B -> A + {B} |
568 For inleft: Argument A is implicit | |
569 For inright: Argument B is implicit | |
570 ]] | 592 ]] |
571 *) | 593 *) |
572 | 594 |
573 (** 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. *) | 595 (** 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. *) |
574 | 596 |
575 Notation "!!" := (inright _ _). | 597 Notation "!!" := (inright _ _). |
576 Notation "[[[ x ]]]" := (inleft _ [x]). | 598 Notation "[|| x ||]" := (inleft _ [x]). |
577 | 599 |
578 (** 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. *) | 600 (** 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. *) |
579 | 601 |
580 Definition pred_strong8 : forall n : nat, {m : nat | n = S m} + {n = 0}. | 602 Definition pred_strong8 : forall n : nat, {m : nat | n = S m} + {n = 0}. |
581 refine (fun n => | 603 refine (fun n => |
582 match n with | 604 match n with |
583 | O => !! | 605 | O => !! |
584 | S n' => [[[n']]] | 606 | S n' => [||n'||] |
585 end); trivial. | 607 end); trivial. |
586 Defined. | 608 Defined. |
587 | 609 |
588 Eval compute in pred_strong8 2. | 610 Eval compute in pred_strong8 2. |
589 (** %\vspace{-.15in}% [[ | 611 (** %\vspace{-.15in}% [[ |
590 = [[[1]]] | 612 = [||1||] |
591 : {m : nat | 2 = S m} + {2 = 0} | 613 : {m : nat | 2 = S m} + {2 = 0} |
592 | |
593 ]] | 614 ]] |
594 *) | 615 *) |
595 | 616 |
596 Eval compute in pred_strong8 0. | 617 Eval compute in pred_strong8 0. |
597 (** %\vspace{-.15in}% [[ | 618 (** %\vspace{-.15in}% [[ |
598 = !! | 619 = !! |
599 : {m : nat | 0 = S m} + {0 = 0} | 620 : {m : nat | 0 = S m} + {0 = 0} |
600 | 621 ]] |
601 ]] | 622 *) |
602 *) | 623 |
624 (** As with our other maximally expressive [pred] function, we arrive at quite simple output values, thanks to notations. *) | |
603 | 625 |
604 | 626 |
605 (** * Monadic Notations *) | 627 (** * Monadic Notations *) |
606 | 628 |
607 (** We can treat [maybe] like a 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. *) | 629 (** 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. *) |
608 | 630 |
609 Notation "x <- e1 ; e2" := (match e1 with | 631 Notation "x <- e1 ; e2" := (match e1 with |
610 | Unknown => ?? | 632 | Unknown => ?? |
611 | Found x _ => e2 | 633 | Found x _ => e2 |
612 end) | 634 end) |
613 (right associativity, at level 60). | 635 (right associativity, at level 60). |
614 | 636 |
615 (** 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] %\textit{%#<i>#does#</i>#%}% find an answer, pass that answer on to [e2] to find the final result. The variable [x] can be considered bound in [e2]. | 637 (** 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] %\textit{%#<i>#does#</i>#%}% find an answer, pass that answer on to [e2] to find the final result. The variable [x] can be considered bound in [e2]. |
616 | 638 |
617 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. *) | 639 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. *) |
618 | 640 |
619 Definition doublePred : forall n1 n2 : nat, {{p | n1 = S (fst p) /\ n2 = S (snd p)}}. | 641 Definition doublePred : forall n1 n2 : nat, {{p | n1 = S (fst p) /\ n2 = S (snd p)}}. |
620 refine (fun n1 n2 => | 642 refine (fun n1 n2 => |
621 m1 <- pred_strong7 n1; | 643 m1 <- pred_strong7 n1; |
622 m2 <- pred_strong7 n2; | 644 m2 <- pred_strong7 n2; |
623 [[(m1, m2)]]); tauto. | 645 [|(m1, m2)|]); tauto. |
624 Defined. | 646 Defined. |
625 | 647 |
626 (** We can build a [sumor] version of the %``%#"#bind#"#%''% notation and use it to write a similarly straightforward version of this function. *) | 648 (** We can build a [sumor] version of the %``%#"#bind#"#%''% notation and use it to write a similarly straightforward version of this function. *) |
627 | 649 |
628 (** printing <-- $\longleftarrow$ *) | 650 (** printing <-- $\longleftarrow$ *) |
639 {p : nat * nat | n1 = S (fst p) /\ n2 = S (snd p)} | 661 {p : nat * nat | n1 = S (fst p) /\ n2 = S (snd p)} |
640 + {n1 = 0 \/ n2 = 0}. | 662 + {n1 = 0 \/ n2 = 0}. |
641 refine (fun n1 n2 => | 663 refine (fun n1 n2 => |
642 m1 <-- pred_strong8 n1; | 664 m1 <-- pred_strong8 n1; |
643 m2 <-- pred_strong8 n2; | 665 m2 <-- pred_strong8 n2; |
644 [[[(m1, m2)]]]); tauto. | 666 [||(m1, m2)||]); tauto. |
645 Defined. | 667 Defined. |
646 | 668 |
647 | 669 |
648 (** * A Type-Checking Example *) | 670 (** * A Type-Checking Example *) |
649 | 671 |
650 (** We can apply these specification types to build a certified type-checker for a simple expression language. *) | 672 (** We can apply these specification types to build a certified type checker for a simple expression language. *) |
651 | 673 |
652 Inductive exp : Set := | 674 Inductive exp : Set := |
653 | Nat : nat -> exp | 675 | Nat : nat -> exp |
654 | Plus : exp -> exp -> exp | 676 | Plus : exp -> exp -> exp |
655 | Bool : bool -> exp | 677 | Bool : bool -> exp |
683 (** 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. *) | 705 (** 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. *) |
684 | 706 |
685 Notation "e1 ;; e2" := (if e1 then e2 else ??) | 707 Notation "e1 ;; e2" := (if e1 then e2 else ??) |
686 (right associativity, at level 60). | 708 (right associativity, at level 60). |
687 | 709 |
688 (** 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. *) | 710 (** 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. *) |
689 (* end thide *) | 711 (* end thide *) |
690 | 712 |
691 Definition typeCheck : forall e : exp, {{t | hasType e t}}. | 713 Definition typeCheck : forall e : exp, {{t | hasType e t}}. |
692 (* begin thide *) | 714 (* begin thide *) |
693 Hint Constructors hasType. | 715 Hint Constructors hasType. |
694 | 716 |
695 refine (fix F (e : exp) : {{t | hasType e t}} := | 717 refine (fix F (e : exp) : {{t | hasType e t}} := |
696 match e with | 718 match e with |
697 | Nat _ => [[TNat]] | 719 | Nat _ => [|TNat|] |
698 | Plus e1 e2 => | 720 | Plus e1 e2 => |
699 t1 <- F e1; | 721 t1 <- F e1; |
700 t2 <- F e2; | 722 t2 <- F e2; |
701 eq_type_dec t1 TNat;; | 723 eq_type_dec t1 TNat;; |
702 eq_type_dec t2 TNat;; | 724 eq_type_dec t2 TNat;; |
703 [[TNat]] | 725 [|TNat|] |
704 | Bool _ => [[TBool]] | 726 | Bool _ => [|TBool|] |
705 | And e1 e2 => | 727 | And e1 e2 => |
706 t1 <- F e1; | 728 t1 <- F e1; |
707 t2 <- F e2; | 729 t2 <- F e2; |
708 eq_type_dec t1 TBool;; | 730 eq_type_dec t1 TBool;; |
709 eq_type_dec t2 TBool;; | 731 eq_type_dec t2 TBool;; |
710 [[TBool]] | 732 [|TBool|] |
711 end); crush. | 733 end); crush. |
712 (* end thide *) | 734 (* end thide *) |
713 Defined. | 735 Defined. |
714 | 736 |
715 (** Despite manipulating proofs, our type checker is easy to run. *) | 737 (** Despite manipulating proofs, our type checker is easy to run. *) |
716 | 738 |
717 Eval simpl in typeCheck (Nat 0). | 739 Eval simpl in typeCheck (Nat 0). |
718 (** %\vspace{-.15in}% [[ | 740 (** %\vspace{-.15in}% [[ |
719 = [[TNat]] | 741 = [|TNat|] |
720 : {{t | hasType (Nat 0) t}} | 742 : {{t | hasType (Nat 0) t}} |
721 ]] | 743 ]] |
722 *) | 744 *) |
723 | 745 |
724 Eval simpl in typeCheck (Plus (Nat 1) (Nat 2)). | 746 Eval simpl in typeCheck (Plus (Nat 1) (Nat 2)). |
725 (** %\vspace{-.15in}% [[ | 747 (** %\vspace{-.15in}% [[ |
726 = [[TNat]] | 748 = [|TNat|] |
727 : {{t | hasType (Plus (Nat 1) (Nat 2)) t}} | 749 : {{t | hasType (Plus (Nat 1) (Nat 2)) t}} |
728 ]] | 750 ]] |
729 *) | 751 *) |
730 | 752 |
731 Eval simpl in typeCheck (Plus (Nat 1) (Bool false)). | 753 Eval simpl in typeCheck (Plus (Nat 1) (Bool false)). |
733 = ?? | 755 = ?? |
734 : {{t | hasType (Plus (Nat 1) (Bool false)) t}} | 756 : {{t | hasType (Plus (Nat 1) (Bool false)) t}} |
735 ]] | 757 ]] |
736 *) | 758 *) |
737 | 759 |
738 (** The type-checker also extracts to some reasonable OCaml code. *) | 760 (** The type checker also extracts to some reasonable OCaml code. *) |
739 | 761 |
740 Extraction typeCheck. | 762 Extraction typeCheck. |
741 | 763 |
742 (** %\begin{verbatim} | 764 (** %\begin{verbatim} |
743 (** val typeCheck : exp -> type0 maybe **) | 765 (** val typeCheck : exp -> type0 maybe **) |
817 | 839 |
818 (** Next, we prove a helpful lemma, which states that a given expression can have at most one type. *) | 840 (** Next, we prove a helpful lemma, which states that a given expression can have at most one type. *) |
819 | 841 |
820 Lemma hasType_det : forall e t1, | 842 Lemma hasType_det : forall e t1, |
821 hasType e t1 | 843 hasType e t1 |
822 -> forall t2, | 844 -> forall t2, hasType e t2 |
823 hasType e t2 | |
824 -> t1 = t2. | 845 -> t1 = t2. |
825 induction 1; inversion 1; crush. | 846 induction 1; inversion 1; crush. |
826 Qed. | 847 Qed. |
827 | 848 |
828 (** Now we can define the type-checker. Its type expresses that it only fails on untypable expressions. *) | 849 (** Now we can define the type-checker. Its type expresses that it only fails on untypable expressions. *) |
850 | |
851 (** printing <-- $\longleftarrow$ *) | |
829 | 852 |
830 (* end thide *) | 853 (* end thide *) |
831 Definition typeCheck' : forall e : exp, {t : type | hasType e t} + {forall t, ~ hasType e t}. | 854 Definition typeCheck' : forall e : exp, {t : type | hasType e t} + {forall t, ~ hasType e t}. |
832 (* begin thide *) | 855 (* begin thide *) |
833 Hint Constructors hasType. | 856 Hint Constructors hasType. |
834 (** We register all of the typing rules as hints. *) | 857 (** We register all of the typing rules as hints. *) |
835 | 858 |
836 Hint Resolve hasType_det. | 859 Hint Resolve hasType_det. |
837 (** [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. *) | 860 (** 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. *) |
838 | 861 |
839 (** Finally, the implementation of [typeCheck] can be transcribed literally, simply switching notations as needed. *) | 862 (** Finally, the implementation of [typeCheck] can be transcribed literally, simply switching notations as needed. *) |
840 | 863 |
841 refine (fix F (e : exp) : {t : type | hasType e t} + {forall t, ~ hasType e t} := | 864 refine (fix F (e : exp) : {t : type | hasType e t} + {forall t, ~ hasType e t} := |
842 match e with | 865 match e with |
843 | Nat _ => [[[TNat]]] | 866 | Nat _ => [||TNat||] |
844 | Plus e1 e2 => | 867 | Plus e1 e2 => |
845 t1 <-- F e1; | 868 t1 <-- F e1; |
846 t2 <-- F e2; | 869 t2 <-- F e2; |
847 eq_type_dec t1 TNat;;; | 870 eq_type_dec t1 TNat;;; |
848 eq_type_dec t2 TNat;;; | 871 eq_type_dec t2 TNat;;; |
849 [[[TNat]]] | 872 [||TNat||] |
850 | Bool _ => [[[TBool]]] | 873 | Bool _ => [||TBool||] |
851 | And e1 e2 => | 874 | And e1 e2 => |
852 t1 <-- F e1; | 875 t1 <-- F e1; |
853 t2 <-- F e2; | 876 t2 <-- F e2; |
854 eq_type_dec t1 TBool;;; | 877 eq_type_dec t1 TBool;;; |
855 eq_type_dec t2 TBool;;; | 878 eq_type_dec t2 TBool;;; |
856 [[[TBool]]] | 879 [||TBool||] |
857 end); clear F; crush' tt hasType; eauto. | 880 end); clear F; crush' tt hasType; eauto. |
858 | 881 |
859 (** 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 [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. *) | 882 (** 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. *) |
860 (* end thide *) | 883 (* end thide *) |
861 | 884 |
862 | 885 |
863 Defined. | 886 Defined. |
864 | 887 |
865 (** 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. | 888 (** 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. |
866 | 889 |
867 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. *) | 890 Our new function remains easy to test: *) |
868 | 891 |
869 Eval simpl in typeCheck' (Nat 0). | 892 Eval simpl in typeCheck' (Nat 0). |
870 (** %\vspace{-.15in}% [[ | 893 (** %\vspace{-.15in}% [[ |
871 = [[[TNat]]] | 894 = [||TNat||] |
872 : {t : type | hasType (Nat 0) t} + | 895 : {t : type | hasType (Nat 0) t} + |
873 {(forall t : type, ~ hasType (Nat 0) t)} | 896 {(forall t : type, ~ hasType (Nat 0) t)} |
874 ]] | 897 ]] |
875 *) | 898 *) |
876 | 899 |
877 Eval simpl in typeCheck' (Plus (Nat 1) (Nat 2)). | 900 Eval simpl in typeCheck' (Plus (Nat 1) (Nat 2)). |
878 (** %\vspace{-.15in}% [[ | 901 (** %\vspace{-.15in}% [[ |
879 = [[[TNat]]] | 902 = [||TNat||] |
880 : {t : type | hasType (Plus (Nat 1) (Nat 2)) t} + | 903 : {t : type | hasType (Plus (Nat 1) (Nat 2)) t} + |
881 {(forall t : type, ~ hasType (Plus (Nat 1) (Nat 2)) t)} | 904 {(forall t : type, ~ hasType (Plus (Nat 1) (Nat 2)) t)} |
882 ]] | 905 ]] |
883 *) | 906 *) |
884 | 907 |
886 (** %\vspace{-.15in}% [[ | 909 (** %\vspace{-.15in}% [[ |
887 = !! | 910 = !! |
888 : {t : type | hasType (Plus (Nat 1) (Bool false)) t} + | 911 : {t : type | hasType (Plus (Nat 1) (Bool false)) t} + |
889 {(forall t : type, ~ hasType (Plus (Nat 1) (Bool false)) t)} | 912 {(forall t : type, ~ hasType (Plus (Nat 1) (Bool false)) t)} |
890 ]] | 913 ]] |
891 *) | 914 |
915 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. *) | |
892 | 916 |
893 | 917 |
894 (** * Exercises *) | 918 (** * Exercises *) |
895 | 919 |
896 (** All of the notations defined in this chapter, plus some extras, are available for import from the module [MoreSpecif] of the book source. | 920 (** All of the notations defined in this chapter, plus some extras, are available for import from the module [MoreSpecif] of the book source. |
901 %\item%#<li># %\begin{enumerate}%#<ol># | 925 %\item%#<li># %\begin{enumerate}%#<ol># |
902 %\item%#<li># Define [var], a type of propositional variables, as a synonym for [nat].#</li># | 926 %\item%#<li># Define [var], a type of propositional variables, as a synonym for [nat].#</li># |
903 %\item%#<li># Define an inductive type [prop] of propositional logic formulas, consisting of variables, negation, and binary conjunction and disjunction.#</li># | 927 %\item%#<li># Define an inductive type [prop] of propositional logic formulas, consisting of variables, negation, and binary conjunction and disjunction.#</li># |
904 %\item%#<li># Define a function [propDenote] from variable truth assignments and [prop]s to [Prop], based on the usual meanings of the connectives. Represent truth assignments as functions from [var] to [bool].#</li># | 928 %\item%#<li># Define a function [propDenote] from variable truth assignments and [prop]s to [Prop], based on the usual meanings of the connectives. Represent truth assignments as functions from [var] to [bool].#</li># |
905 %\item%#<li># Define a function [bool_true_dec] that checks whether a boolean is true, with a maximally expressive dependent type. That is, the function should have type [forall b, {b = true} + {b = true -> False}]. #</li># | 929 %\item%#<li># Define a function [bool_true_dec] that checks whether a boolean is true, with a maximally expressive dependent type. That is, the function should have type [forall b, {b = true} + {b = true -> False}]. #</li># |
906 %\item%#<li># Define a function [decide] that determines whether a particular [prop] is true under a particular truth assignment. That is, the function should have type [forall (truth : var -> bool) (p : prop), {propDenote truth p} + {~ propDenote truth p}]. This function is probably easiest to write in the usual tactical style, instead of programming with [refine]. [bool_true_dec] may come in handy as a hint.#</li># | 930 %\item%#<li># Define a function [decide] that determines whether a particular [prop] is true under a particular truth assignment. That is, the function should have type [forall (truth : var -> bool) (p : prop), {propDenote truth p} + {~ propDenote truth p}]. This function is probably easiest to write in the usual tactical style, instead of programming with [refine]. The function [bool_true_dec] may come in handy as a hint.#</li># |
907 %\item%#<li># Define a function [negate] that returns a simplified version of the negation of a [prop]. That is, the function should have type [forall p : prop, {p' : prop | forall truth, propDenote truth p <-> ~ propDenote truth p'}]. To simplify a variable, just negate it. Simplify a negation by returning its argument. Simplify conjunctions and disjunctions using De Morgan's laws, negating the arguments recursively and switching the kind of connective. [decide] may be useful in some of the proof obligations, even if you do not use it in the computational part of [negate]'s definition. Lemmas like [decide] allow us to compensate for the lack of a general Law of the Excluded Middle in CIC.#</li># | 931 %\item%#<li># Define a function [negate] that returns a simplified version of the negation of a [prop]. That is, the function should have type [forall p : prop, {p' : prop | forall truth, propDenote truth p <-> ~ propDenote truth p'}]. To simplify a variable, just negate it. Simplify a negation by returning its argument. Simplify conjunctions and disjunctions using De Morgan's laws, negating the arguments recursively and switching the kind of connective. Your [decide] function may be useful in some of the proof obligations, even if you do not use it in the computational part of [negate]'s definition. Lemmas like [decide] allow us to compensate for the lack of a general Law of the Excluded Middle in CIC.#</li># |
908 #</ol>#%\end{enumerate}% #</li># | 932 #</ol>#%\end{enumerate}% #</li># |
909 | 933 |
910 %\item%#<li># Implement the DPLL satisfiability decision procedure for boolean formulas in conjunctive normal form, with a dependent type that guarantees its correctness. An example of a reasonable type for this function would be [forall f : formula, {truth : tvals | formulaTrue truth f} + {forall truth, ~ formulaTrue truth f}]. Implement at least %``%#"#the basic backtracking algorithm#"#%''% as defined here: | 934 %\item%#<li># Implement the DPLL satisfiability decision procedure for boolean formulas in conjunctive normal form, with a dependent type that guarantees its correctness. An example of a reasonable type for this function would be [forall f : formula, {truth : tvals | formulaTrue truth f} + {forall truth, ~ formulaTrue truth f}]. Implement at least %``%#"#the basic backtracking algorithm#"#%''% as defined here: |
911 %\begin{center}\url{http://en.wikipedia.org/wiki/DPLL_algorithm}\end{center}% | 935 %\begin{center}\url{http://en.wikipedia.org/wiki/DPLL_algorithm}\end{center}% |
912 #<blockquote><a href="http://en.wikipedia.org/wiki/DPLL_algorithm">http://en.wikipedia.org/wiki/DPLL_algorithm</a></blockquote># | 936 #<blockquote><a href="http://en.wikipedia.org/wiki/DPLL_algorithm">http://en.wikipedia.org/wiki/DPLL_algorithm</a></blockquote># |