comparison src/InductiveTypes.v @ 439:393b8ed99c2f

A pass of improvements to vertical spacing, up through end of InductiveTypes
author Adam Chlipala <adam@chlipala.net>
date Mon, 30 Jul 2012 13:21:36 -0400
parents 8077352044b2
children 89c67796754e
comparison
equal deleted inserted replaced
438:f1f779c6a232 439:393b8ed99c2f
36 A short demonstration should explain how this can be. The identity function over the natural numbers is certainly not a controversial program. *) 36 A short demonstration should explain how this can be. The identity function over the natural numbers is certainly not a controversial program. *)
37 37
38 Check (fun x : nat => x). 38 Check (fun x : nat => x).
39 (** [: nat -> nat] *) 39 (** [: nat -> nat] *)
40 40
41 (** Consider this alternate program, which is almost identical to the last one. *) 41 (** %\smallskip{}%Consider this alternate program, which is almost identical to the last one. *)
42 42
43 Check (fun x : True => x). 43 Check (fun x : True => x).
44 (** [: True -> True] *) 44 (** [: True -> True] *)
45 45
46 (** The identity program is interpreted as a proof that %\index{Gallina terms!True}%[True], the always-true proposition, implies itself! What we see is that Curry-Howard interprets implications as functions, where an input is a proposition being assumed and an output is a proposition being deduced. This intuition is not too far from a common one for informal theorem proving, where we might already think of an implication proof as a process for transforming a hypothesis into a conclusion. 46 (** %\smallskip{}%The identity program is interpreted as a proof that %\index{Gallina terms!True}%[True], the always-true proposition, implies itself! What we see is that Curry-Howard interprets implications as functions, where an input is a proposition being assumed and an output is a proposition being deduced. This intuition is not too far from a common one for informal theorem proving, where we might already think of an implication proof as a process for transforming a hypothesis into a conclusion.
47 47
48 There are also more primitive proof forms available. For instance, the term %\index{Gallina terms!I}%[I] is the single proof of [True], applicable in any context. *) 48 There are also more primitive proof forms available. For instance, the term %\index{Gallina terms!I}%[I] is the single proof of [True], applicable in any context. *)
49 49
50 Check I. 50 Check I.
51 (** [: True] *) 51 (** [: True] *)
52 52
53 (** With [I], we can prove another simple propositional theorem. *) 53 (** %\smallskip{}%With [I], we can prove another simple propositional theorem. *)
54 54
55 Check (fun _ : False => I). 55 Check (fun _ : False => I).
56 (** [: False -> True] *) 56 (** [: False -> True] *)
57 57
58 (** No proofs of %\index{Gallina terms!False}%[False] exist in the top-level context, but the implication-as-function analogy gives us an easy way to, for example, show that [False] implies itself. *) 58 (** %\smallskip{}%No proofs of %\index{Gallina terms!False}%[False] exist in the top-level context, but the implication-as-function analogy gives us an easy way to, for example, show that [False] implies itself. *)
59 59
60 Check (fun x : False => x). 60 Check (fun x : False => x).
61 (** [: False -> False] *) 61 (** [: False -> False] *)
62 62
63 (** In fact, [False] implies anything, and we can take advantage of this fact with an odd looking [match] expression that has no branches. Since there are no rules for deducing [False], there are no cases to consider! *) 63 (** %\smallskip{}%In fact, [False] implies anything, and we can take advantage of this fact with an odd looking [match] expression that has no branches. Since there are no rules for deducing [False], there are no cases to consider! *)
64 64
65 Check (fun x : False => match x with end : True). 65 Check (fun x : False => match x with end : True).
66 (** [: False -> True] *) 66 (** [: False -> True] *)
67 67
68 (** Every one of these example programs whose type looks like a logical formula is a%\index{proof term}% _proof term_. We use that name for any Gallina term of a logical type, and we will elaborate shortly on what makes a type logical. 68 (** %\smallskip{}%Every one of these example programs whose type looks like a logical formula is a%\index{proof term}% _proof term_. We use that name for any Gallina term of a logical type, and we will elaborate shortly on what makes a type logical.
69 69
70 In the rest of this chapter, we will introduce different ways of defining types. Every example type can be interpreted alternatively as a type of programs or %\index{proposition}%propositions (i.e., formulas or theorem statements). 70 In the rest of this chapter, we will introduce different ways of defining types. Every example type can be interpreted alternatively as a type of programs or %\index{proposition}%propositions (i.e., formulas or theorem statements).
71 71
72 One of the first types we introduce will be [bool], with constructors [true] and [false]. Newcomers to Coq often wonder about the distinction between [True] and [true] and the distinction between [False] and [false]. One glib answer is that [True] and [False] are types, but [true] and [false] are not. A more useful answer is that Coq's metatheory guarantees that any term of type [bool] _evaluates_ to either [true] or [false]. This means that we have an _algorithm_ for answering any question phrased as an expression of type [bool]. Conversely, most propositions do not evaluate to [True] or [False]; the language of inductively defined propositions is much richer than that. We ought to be glad that we have no algorithm for deciding mathematical truth, since otherwise it would be clear that we could not formalize undecidable properties, like most any properties of general-purpose programs. *) 72 One of the first types we introduce will be [bool], with constructors [true] and [false]. Newcomers to Coq often wonder about the distinction between [True] and [true] and the distinction between [False] and [false]. One glib answer is that [True] and [False] are types, but [true] and [false] are not. A more useful answer is that Coq's metatheory guarantees that any term of type [bool] _evaluates_ to either [true] or [false]. This means that we have an _algorithm_ for answering any question phrased as an expression of type [bool]. Conversely, most propositions do not evaluate to [True] or [False]; the language of inductively defined propositions is much richer than that. We ought to be glad that we have no algorithm for deciding mathematical truth, since otherwise it would be clear that we could not formalize undecidable properties, like most any properties of general-purpose programs. *)
73 73
87 (** [unit : Set] *) 87 (** [unit : Set] *)
88 88
89 Check tt. 89 Check tt.
90 (** [tt : unit] *) 90 (** [tt : unit] *)
91 91
92 (** We can prove that [unit] is a genuine singleton type. *) 92 (** %\smallskip{}%We can prove that [unit] is a genuine singleton type. *)
93 93
94 Theorem unit_singleton : forall x : unit, x = tt. 94 Theorem unit_singleton : forall x : unit, x = tt.
95 95
96 (** The important thing about an inductive type is, unsurprisingly, that you can do induction over its values, and induction is the key to proving this theorem. We ask to proceed by induction on the variable [x].%\index{tactics!induction}% *) 96 (** The important thing about an inductive type is, unsurprisingly, that you can do induction over its values, and induction is the key to proving this theorem. We ask to proceed by induction on the variable [x].%\index{tactics!induction}% *)
97 97
109 reflexivity. 109 reflexivity.
110 Qed. 110 Qed.
111 (* end thide *) 111 (* end thide *)
112 112
113 (** It seems kind of odd to write a proof by induction with no inductive hypotheses. We could have arrived at the same result by beginning the proof with:%\index{tactics!destruct}% [[ 113 (** It seems kind of odd to write a proof by induction with no inductive hypotheses. We could have arrived at the same result by beginning the proof with:%\index{tactics!destruct}% [[
114
115 destruct x. 114 destruct x.
116
117 ]] 115 ]]
118 116
119 %\noindent%...which corresponds to "proof by case analysis" in classical math. For non-recursive inductive types, the two tactics will always have identical behavior. Often case analysis is sufficient, even in proofs about recursive types, and it is nice to avoid introducing unneeded induction hypotheses. 117 %\noindent%...which corresponds to "proof by case analysis" in classical math. For non-recursive inductive types, the two tactics will always have identical behavior. Often case analysis is sufficient, even in proofs about recursive types, and it is nice to avoid introducing unneeded induction hypotheses.
120 118
121 What exactly _is_ the %\index{induction principles}%induction principle for [unit]? We can ask Coq: *) 119 What exactly _is_ the %\index{induction principles}%induction principle for [unit]? We can ask Coq: *)
122 120
123 Check unit_ind. 121 Check unit_ind.
124 (** [unit_ind : forall P : unit -> Prop, P tt -> forall u : unit, P u] *) 122 (** [unit_ind : forall P : unit -> Prop, P tt -> forall u : unit, P u] *)
125 123
126 (** Every [Inductive] command defining a type [T] also defines an induction principle named [T_ind]. Recall from the last section that our type, operations over it, and principles for reasoning about it all live in the same language and are described by the same type system. The key to telling what is a program and what is a proof lies in the distinction between the type %\index{Gallina terms!Prop}%[Prop], which appears in our induction principle; and the type %\index{Gallina terms!Set}%[Set], which we have seen a few times already. 124 (** %\smallskip{}%Every [Inductive] command defining a type [T] also defines an induction principle named [T_ind]. Recall from the last section that our type, operations over it, and principles for reasoning about it all live in the same language and are described by the same type system. The key to telling what is a program and what is a proof lies in the distinction between the type %\index{Gallina terms!Prop}%[Prop], which appears in our induction principle; and the type %\index{Gallina terms!Set}%[Set], which we have seen a few times already.
127 125
128 The convention goes like this: [Set] is the type of normal types used in programming, and the values of such types are programs. [Prop] is the type of logical propositions, and the values of such types are proofs. Thus, an induction principle has a type that shows us that it is a function for building proofs. 126 The convention goes like this: [Set] is the type of normal types used in programming, and the values of such types are programs. [Prop] is the type of logical propositions, and the values of such types are proofs. Thus, an induction principle has a type that shows us that it is a function for building proofs.
129 127
130 Specifically, [unit_ind] quantifies over a predicate [P] over [unit] values. If we can present a proof that [P] holds of [tt], then we are rewarded with a proof that [P] holds for any value [u] of type [unit]. In our last proof, the predicate was [(fun u : unit => u = tt)]. 128 Specifically, [unit_ind] quantifies over a predicate [P] over [unit] values. If we can present a proof that [P] holds of [tt], then we are rewarded with a proof that [P] holds for any value [u] of type [unit]. In our last proof, the predicate was [(fun u : unit => u = tt)].
131 129
150 We can see the induction principle that made this proof so easy: *) 148 We can see the induction principle that made this proof so easy: *)
151 149
152 Check Empty_set_ind. 150 Check Empty_set_ind.
153 (** [Empty_set_ind : forall (P : Empty_set -> Prop) (e : Empty_set), P e] *) 151 (** [Empty_set_ind : forall (P : Empty_set -> Prop) (e : Empty_set), P e] *)
154 152
155 (** In other words, any predicate over values from the empty set holds vacuously of every such element. In the last proof, we chose the predicate [(fun _ : Empty_set => 2 + 2 = 5)]. 153 (** %\smallskip{}%In other words, any predicate over values from the empty set holds vacuously of every such element. In the last proof, we chose the predicate [(fun _ : Empty_set => 2 + 2 = 5)].
156 154
157 We can also apply this get-out-of-jail-free card programmatically. Here is a lazy way of converting values of [Empty_set] to values of [unit]: *) 155 We can also apply this get-out-of-jail-free card programmatically. Here is a lazy way of converting values of [Empty_set] to values of [unit]: *)
158 156
159 Definition e2u (e : Empty_set) : unit := match e with end. 157 Definition e2u (e : Empty_set) : unit := match e with end.
160 158
186 Theorem negb_inverse : forall b : bool, negb (negb b) = b. 184 Theorem negb_inverse : forall b : bool, negb (negb b) = b.
187 (* begin thide *) 185 (* begin thide *)
188 destruct b. 186 destruct b.
189 187
190 (** After we case-analyze on [b], we are left with one subgoal for each constructor of [bool]. 188 (** After we case-analyze on [b], we are left with one subgoal for each constructor of [bool].
191
192 %\vspace{.1in} \noindent 2 \coqdockw{subgoals}\vspace{-.1in}%#<tt>2 subgoals</tt>#
193
194 [[ 189 [[
190 2 subgoals
191
195 ============================ 192 ============================
196 negb (negb true) = true 193 negb (negb true) = true
197 ]] 194
198 %\noindent \coqdockw{subgoal} 2 \coqdockw{is}:%#<tt>subgoal 2 is</tt># 195 subgoal 2 is
199 [[ 196
200 negb (negb false) = false 197 negb (negb false) = false
201
202 ]] 198 ]]
203 199
204 The first subgoal follows by Coq's rules of computation, so we can dispatch it easily: *) 200 The first subgoal follows by Coq's rules of computation, so we can dispatch it easily: *)
205 201
206 reflexivity. 202 reflexivity.
226 At this point, it is probably not hard to guess what the underlying induction principle for [bool] is. *) 222 At this point, it is probably not hard to guess what the underlying induction principle for [bool] is. *)
227 223
228 Check bool_ind. 224 Check bool_ind.
229 (** [bool_ind : forall P : bool -> Prop, P true -> P false -> forall b : bool, P b] *) 225 (** [bool_ind : forall P : bool -> Prop, P true -> P false -> forall b : bool, P b] *)
230 226
231 (** That is, to prove that a property describes all [bool]s, prove that it describes both [true] and [false]. 227 (** %\smallskip{}%That is, to prove that a property describes all [bool]s, prove that it describes both [true] and [false].
232 228
233 There is no interesting Curry-Howard analogue of [bool]. Of course, we can define such a type by replacing [Set] by [Prop] above, but the proposition we arrive at is not very useful. It is logically equivalent to [True], but it provides two indistinguishable primitive proofs, [true] and [false]. In the rest of the chapter, we will skip commenting on Curry-Howard versions of inductive definitions where such versions are not interesting. *) 229 There is no interesting Curry-Howard analogue of [bool]. Of course, we can define such a type by replacing [Set] by [Prop] above, but the proposition we arrive at is not very useful. It is logically equivalent to [True], but it provides two indistinguishable primitive proofs, [true] and [false]. In the rest of the chapter, we will skip commenting on Curry-Howard versions of inductive definitions where such versions are not interesting. *)
234 230
235 231
236 (** * Simple Recursive Types *) 232 (** * Simple Recursive Types *)
239 235
240 Inductive nat : Set := 236 Inductive nat : Set :=
241 | O : nat 237 | O : nat
242 | S : nat -> nat. 238 | S : nat -> nat.
243 239
244 (** [O] is zero, and [S] is the successor function, so that [0] is syntactic sugar for [O], [1] for [S O], [2] for [S (S O)], and so on. 240 (** The constructor [O] is zero, and [S] is the successor function, so that [0] is syntactic sugar for [O], [1] for [S O], [2] for [S (S O)], and so on.
245 241
246 Pattern matching works as we demonstrated in the last chapter:%\index{Gallina terms!pred}% *) 242 Pattern matching works as we demonstrated in the last chapter:%\index{Gallina terms!pred}% *)
247 243
248 Definition isZero (n : nat) : bool := 244 Definition isZero (n : nat) : bool :=
249 match n with 245 match n with
325 321
326 Check nat_ind. 322 Check nat_ind.
327 (** %\vspace{-.15in}% [[ 323 (** %\vspace{-.15in}% [[
328 nat_ind : forall P : nat -> Prop, 324 nat_ind : forall P : nat -> Prop,
329 P O -> (forall n : nat, P n -> P (S n)) -> forall n : nat, P n 325 P O -> (forall n : nat, P n -> P (S n)) -> forall n : nat, P n
330
331 ]] 326 ]]
332 327
333 Each of the two cases of our last proof came from the type of one of the arguments to [nat_ind]. We chose [P] to be [(fun n : nat => plus n O = n)]. The first proof case corresponded to [P O] and the second case to [(forall n : nat, P n -> P (S n))]. The free variable [n] and inductive hypothesis [IHn] came from the argument types given here. 328 %\smallskip{}%Each of the two cases of our last proof came from the type of one of the arguments to [nat_ind]. We chose [P] to be [(fun n : nat => plus n O = n)]. The first proof case corresponded to [P O] and the second case to [(forall n : nat, P n -> P (S n))]. The free variable [n] and inductive hypothesis [IHn] came from the argument types given here.
334 329
335 Since [nat] has a constructor that takes an argument, we may sometimes need to know that that constructor is injective.%\index{tactics!injection}\index{tactics!trivial}% *) 330 Since [nat] has a constructor that takes an argument, we may sometimes need to know that that constructor is injective.%\index{tactics!injection}\index{tactics!trivial}% *)
336 331
337 Theorem S_inj : forall n m : nat, S n = S m -> n = m. 332 Theorem S_inj : forall n m : nat, S n = S m -> n = m.
338 (* begin thide *) 333 (* begin thide *)
502 497
503 Print list. 498 Print list.
504 (** %\vspace{-.15in}% [[ 499 (** %\vspace{-.15in}% [[
505 Inductive list (T : Set) : Set := 500 Inductive list (T : Set) : Set :=
506 Nil : list T | Cons : T -> list T -> list T 501 Nil : list T | Cons : T -> list T -> list T
507 502 ]]
508 ]] 503
509 504 %\smallskip{}%The final definition is the same as what we wrote manually before. The other elements of the section are altered similarly, turning out exactly as they were before, though we managed to write their definitions more succinctly. *)
510 The final definition is the same as what we wrote manually before. The other elements of the section are altered similarly, turning out exactly as they were before, though we managed to write their definitions more succinctly. *)
511 505
512 Check length. 506 Check length.
513 (** %\vspace{-.15in}% [[ 507 (** %\vspace{-.15in}% [[
514 length 508 length
515 : forall T : Set, list T -> nat 509 : forall T : Set, list T -> nat
516 ]] 510 ]]
517 511
518 The parameter [T] is treated as a new argument to the induction principle, too. *) 512 %\smallskip{}%The parameter [T] is treated as a new argument to the induction principle, too. *)
519 513
520 Check list_ind. 514 Check list_ind.
521 (** %\vspace{-.15in}% [[ 515 (** %\vspace{-.15in}% [[
522 list_ind 516 list_ind
523 : forall (T : Set) (P : list T -> Prop), 517 : forall (T : Set) (P : list T -> Prop),
524 P (Nil T) -> 518 P (Nil T) ->
525 (forall (t : T) (l : list T), P l -> P (Cons t l)) -> 519 (forall (t : T) (l : list T), P l -> P (Cons t l)) ->
526 forall l : list T, P l 520 forall l : list T, P l
527 ]] 521 ]]
528 522
529 Thus, even though we just saw that [T] is added as an extra argument to the constructor [Cons], there is no quantifier for [T] in the type of the inductive case like there is for each of the other arguments. *) 523 %\smallskip{}%Thus, even though we just saw that [T] is added as an extra argument to the constructor [Cons], there is no quantifier for [T] in the type of the inductive case like there is for each of the other arguments. *)
530 524
531 525
532 (** * Mutually Inductive Types *) 526 (** * Mutually Inductive Types *)
533 527
534 (** We can define inductive types that refer to each other: *) 528 (** We can define inductive types that refer to each other: *)
586 even_list_ind 580 even_list_ind
587 : forall P : even_list -> Prop, 581 : forall P : even_list -> Prop,
588 P ENil -> 582 P ENil ->
589 (forall (n : nat) (o : odd_list), P (ECons n o)) -> 583 (forall (n : nat) (o : odd_list), P (ECons n o)) ->
590 forall e : even_list, P e 584 forall e : even_list, P e
591 585 ]]
592 ]] 586
593 587 %\smallskip{}%We see that no inductive hypotheses are included anywhere in the type. To get them, we must ask for mutual principles as we need them, using the %\index{Vernacular commands!Scheme}%[Scheme] command. *)
594 We see that no inductive hypotheses are included anywhere in the type. To get them, we must ask for mutual principles as we need them, using the %\index{Vernacular commands!Scheme}%[Scheme] command. *)
595 588
596 Scheme even_list_mut := Induction for even_list Sort Prop 589 Scheme even_list_mut := Induction for even_list Sort Prop
597 with odd_list_mut := Induction for odd_list Sort Prop. 590 with odd_list_mut := Induction for odd_list Sort Prop.
598 591
599 (** This invocation of [Scheme] asks for the creation of induction principles [even_list_mut] for the type [even_list] and [odd_list_mut] for the type [odd_list]. The [Induction] keyword says we want standard induction schemes, since [Scheme] supports more exotic choices. Finally, [Sort Prop] establishes that we really want induction schemes, not recursion schemes, which are the same according to Curry-Howard, save for the [Prop]/[Set] distinction. *) 592 (** This invocation of [Scheme] asks for the creation of induction principles [even_list_mut] for the type [even_list] and [odd_list_mut] for the type [odd_list]. The [Induction] keyword says we want standard induction schemes, since [Scheme] supports more exotic choices. Finally, [Sort Prop] establishes that we really want induction schemes, not recursion schemes, which are the same according to Curry-Howard, save for the [Prop]/[Set] distinction. *)
698 (forall f0 : formula, 691 (forall f0 : formula,
699 P f0 -> forall f1 : formula, P f1 -> P (And f0 f1)) -> 692 P f0 -> forall f1 : formula, P f1 -> P (And f0 f1)) ->
700 (forall f1 : nat -> formula, 693 (forall f1 : nat -> formula,
701 (forall n : nat, P (f1 n)) -> P (Forall f1)) -> 694 (forall n : nat, P (f1 n)) -> P (Forall f1)) ->
702 forall f2 : formula, P f2 695 forall f2 : formula, P f2
703 696 ]]
704 ]] 697
705 698 %\smallskip{}%Focusing on the [Forall] case, which comes third, we see that we are allowed to assume that the theorem holds _for any application of the argument function [f1]_. That is, Coq induction principles do not follow a simple rule that the textual representations of induction variables must get shorter in appeals to induction hypotheses. Luckily for us, the people behind the metatheory of Coq have verified that this flexibility does not introduce unsoundness.
706 Focusing on the [Forall] case, which comes third, we see that we are allowed to assume that the theorem holds _for any application of the argument function [f1]_. That is, Coq induction principles do not follow a simple rule that the textual representations of induction variables must get shorter in appeals to induction hypotheses. Luckily for us, the people behind the metatheory of Coq have verified that this flexibility does not introduce unsoundness.
707 699
708 %\medskip% 700 %\medskip%
709 701
710 Up to this point, we have seen how to encode in Coq more and more of what is possible with algebraic datatypes in %\index{Haskell}%Haskell and %\index{ML}%ML. This may have given the inaccurate impression that inductive types are a strict extension of algebraic datatypes. In fact, Coq must rule out some types allowed by Haskell and ML, for reasons of soundness. Reflexive types provide our first good example of such a case. 702 Up to this point, we have seen how to encode in Coq more and more of what is possible with algebraic datatypes in %\index{Haskell}%Haskell and %\index{ML}%ML. This may have given the inaccurate impression that inductive types are a strict extension of algebraic datatypes. In fact, Coq must rule out some types allowed by Haskell and ML, for reasons of soundness. Reflexive types provide our first good example of such a case.
711 703
729 721
730 We have run afoul of the%\index{strict positivity requirement}\index{positivity requirement}% _strict positivity requirement_ for inductive definitions, which says that the type being defined may not occur to the left of an arrow in the type of a constructor argument. It is important that the type of a constructor is viewed in terms of a series of arguments and a result, since obviously we need recursive occurrences to the lefts of the outermost arrows if we are to have recursive occurrences at all. Our candidate definition above violates the positivity requirement because it involves an argument of type [term -> term], where the type [term] that we are defining appears to the left of an arrow. The candidate type of [App] is fine, however, since every occurrence of [term] is either a constructor argument or the final result type. 722 We have run afoul of the%\index{strict positivity requirement}\index{positivity requirement}% _strict positivity requirement_ for inductive definitions, which says that the type being defined may not occur to the left of an arrow in the type of a constructor argument. It is important that the type of a constructor is viewed in terms of a series of arguments and a result, since obviously we need recursive occurrences to the lefts of the outermost arrows if we are to have recursive occurrences at all. Our candidate definition above violates the positivity requirement because it involves an argument of type [term -> term], where the type [term] that we are defining appears to the left of an arrow. The candidate type of [App] is fine, however, since every occurrence of [term] is either a constructor argument or the final result type.
731 723
732 Why must Coq enforce this restriction? Imagine that our last definition had been accepted, allowing us to write this function: 724 Why must Coq enforce this restriction? Imagine that our last definition had been accepted, allowing us to write this function:
733 725
734 [[ 726 %\vspace{-.15in}%[[
735 Definition uhoh (t : term) : term := 727 Definition uhoh (t : term) : term :=
736 match t with 728 match t with
737 | Abs f => f t 729 | Abs f => f t
738 | _ => t 730 | _ => t
739 end. 731 end.
740
741 ]] 732 ]]
742 733
743 Using an informal idea of Coq's semantics, it is easy to verify that the application [uhoh (Abs uhoh)] will run forever. This would be a mere curiosity in OCaml and Haskell, where non-termination is commonplace, though the fact that we have a non-terminating program without explicit recursive function definitions is unusual. 734 Using an informal idea of Coq's semantics, it is easy to verify that the application [uhoh (Abs uhoh)] will run forever. This would be a mere curiosity in OCaml and Haskell, where non-termination is commonplace, though the fact that we have a non-terminating program without explicit recursive function definitions is unusual.
744 735
745 %\index{termination checking}%For Coq, however, this would be a disaster. The possibility of writing such a function would destroy all our confidence that proving a theorem means anything. Since Coq combines programs and proofs in one language, we would be able to prove every theorem with an infinite loop. 736 %\index{termination checking}%For Coq, however, this would be a disaster. The possibility of writing such a function would destroy all our confidence that proving a theorem means anything. Since Coq combines programs and proofs in one language, we would be able to prove every theorem with an infinite loop.
746 737
747 Nonetheless, the basic insight of HOAS is a very useful one, and there are ways to realize most benefits of HOAS in Coq. We will study a particular technique of this kind in the later chapters on programming language syntax and semantics. *) 738 Nonetheless, the basic insight of HOAS is a very useful one, and there are ways to realize most benefits of HOAS in Coq. We will study a particular technique of this kind in the final chapter, on programming language syntax and semantics. *)
748 739
749 740
750 (** * An Interlude on Induction Principles *) 741 (** * An Interlude on Induction Principles *)
751 742
752 (** As we have emphasized a few times already, Coq proofs are actually programs, written in the same language we have been using in our examples all along. We can get a first sense of what this means by taking a look at the definitions of some of the %\index{induction principles}%induction principles we have used. A close look at the details here will help us construct induction principles manually, which we will see is necessary for some more advanced inductive definitions. *) 743 (** As we have emphasized a few times already, Coq proofs are actually programs, written in the same language we have been using in our examples all along. We can get a first sense of what this means by taking a look at the definitions of some of the %\index{induction principles}%induction principles we have used. A close look at the details here will help us construct induction principles manually, which we will see is necessary for some more advanced inductive definitions. *)
754 Print unit_ind. 745 Print unit_ind.
755 (** %\vspace{-.15in}%[[ 746 (** %\vspace{-.15in}%[[
756 unit_ind = 747 unit_ind =
757 fun P : unit -> Prop => unit_rect P 748 fun P : unit -> Prop => unit_rect P
758 : forall P : unit -> Prop, P tt -> forall u : unit, P u 749 : forall P : unit -> Prop, P tt -> forall u : unit, P u
759 750 ]]
760 ]] 751
761 752 %\smallskip{}%We see that this induction principle is defined in terms of a more general principle, [unit_rect]. The <<rec>> stands for "recursion principle," and the <<t>> at the end stands for [Type]. *)
762 We see that this induction principle is defined in terms of a more general principle, [unit_rect]. The <<rec>> stands for "recursion principle," and the <<t>> at the end stands for [Type]. *)
763 753
764 Check unit_rect. 754 Check unit_rect.
765 (** %\vspace{-.15in}% [[ 755 (** %\vspace{-.15in}% [[
766 unit_rect 756 unit_rect
767 : forall P : unit -> Type, P tt -> forall u : unit, P u 757 : forall P : unit -> Type, P tt -> forall u : unit, P u
768 758 ]]
769 ]] 759
770 760 %\smallskip{}%The principle [unit_rect] gives [P] type [unit -> Type] instead of [unit -> Prop]. [Type] is another universe, like [Set] and [Prop]. In fact, it is a common supertype of both. Later on, we will discuss exactly what the significances of the different universes are. For now, it is just important that we can use [Type] as a sort of meta-universe that may turn out to be either [Set] or [Prop]. We can see the symmetry inherent in the subtyping relationship by printing the definition of another principle that was generated for [unit] automatically: *)
771 The principle [unit_rect] gives [P] type [unit -> Type] instead of [unit -> Prop]. [Type] is another universe, like [Set] and [Prop]. In fact, it is a common supertype of both. Later on, we will discuss exactly what the significances of the different universes are. For now, it is just important that we can use [Type] as a sort of meta-universe that may turn out to be either [Set] or [Prop]. We can see the symmetry inherent in the subtyping relationship by printing the definition of another principle that was generated for [unit] automatically: *)
772 761
773 Print unit_rec. 762 Print unit_rec.
774 (** %\vspace{-.15in}%[[ 763 (** %\vspace{-.15in}%[[
775 unit_rec = 764 unit_rec =
776 fun P : unit -> Set => unit_rect P 765 fun P : unit -> Set => unit_rect P
777 : forall P : unit -> Set, P tt -> forall u : unit, P u 766 : forall P : unit -> Set, P tt -> forall u : unit, P u
778 767 ]]
779 ]] 768
780 769 %\smallskip{}%This is identical to the definition for [unit_ind], except that we have substituted [Set] for [Prop]. For most inductive types [T], then, we get not just induction principles [T_ind], but also %\index{recursion principles}%recursion principles [T_rec]. We can use [T_rec] to write recursive definitions without explicit [Fixpoint] recursion. For instance, the following two definitions are equivalent: *)
781 This is identical to the definition for [unit_ind], except that we have substituted [Set] for [Prop]. For most inductive types [T], then, we get not just induction principles [T_ind], but also %\index{recursion principles}%recursion principles [T_rec]. We can use [T_rec] to write recursive definitions without explicit [Fixpoint] recursion. For instance, the following two definitions are equivalent: *)
782 770
783 Definition always_O (u : unit) : nat := 771 Definition always_O (u : unit) : nat :=
784 match u with 772 match u with
785 | tt => O 773 | tt => O
786 end. 774 end.
796 fun (P : unit -> Type) (f : P tt) (u : unit) => 784 fun (P : unit -> Type) (f : P tt) (u : unit) =>
797 match u as u0 return (P u0) with 785 match u as u0 return (P u0) with
798 | tt => f 786 | tt => f
799 end 787 end
800 : forall P : unit -> Type, P tt -> forall u : unit, P u 788 : forall P : unit -> Type, P tt -> forall u : unit, P u
801 789 ]]
802 ]] 790
803 791 %\smallskip{}%The only new wrinkle here is the annotations on the [match] expression. This is a%\index{dependent pattern matching}% _dependently typed_ pattern match, because the _type_ of the expression depends on the _value_ being matched on. Of course, for this example, the dependency is degenerate; the value being matched on has type [unit], so it may only take on a single known value, [tt]. We will meet more involved examples later, especially in Part II of the book.
804 The only new wrinkle here is the annotations on the [match] expression. This is a%\index{dependent pattern matching}% _dependently typed_ pattern match, because the _type_ of the expression depends on the _value_ being matched on. Of course, for this example, the dependency is degenerate; the value being matched on has type [unit], so it may only take on a single known value, [tt]. We will meet more involved examples later, especially in Part II of the book.
805 792
806 %\index{type inference}%Type inference for dependent pattern matching is undecidable, which can be proved by reduction from %\index{higher-order unification}%higher-order unification%~\cite{HOU}%. Thus, we often find ourselves needing to annotate our programs in a way that explains dependencies to the type checker. In the example of [unit_rect], we have an %\index{Gallina terms!as}%[as] clause, which binds a name for the discriminee; and a %\index{Gallina terms!return}%[return] clause, which gives a way to compute the [match] result type as a function of the discriminee. 793 %\index{type inference}%Type inference for dependent pattern matching is undecidable, which can be proved by reduction from %\index{higher-order unification}%higher-order unification%~\cite{HOU}%. Thus, we often find ourselves needing to annotate our programs in a way that explains dependencies to the type checker. In the example of [unit_rect], we have an %\index{Gallina terms!as}%[as] clause, which binds a name for the discriminee; and a %\index{Gallina terms!return}%[return] clause, which gives a way to compute the [match] result type as a function of the discriminee.
807 794
808 To prove that [unit_rect] is nothing special, we can reimplement it manually. *) 795 To prove that [unit_rect] is nothing special, we can reimplement it manually. *)
809 796
821 (** We rely on Coq's heuristics for inferring [match] annotations, which are not consulted in the pretty-printing of terms. 808 (** We rely on Coq's heuristics for inferring [match] annotations, which are not consulted in the pretty-printing of terms.
822 809
823 We can check the implementation [nat_rect] as well: *) 810 We can check the implementation [nat_rect] as well: *)
824 811
825 Print nat_rect. 812 Print nat_rect.
826 813 (** %\vspace{-.15in}% [[
827 (** %\vspace{-.05in}% [[
828 nat_rect = 814 nat_rect =
829 fun (P : nat -> Type) (f : P O) (f0 : forall n : nat, P n -> P (S n)) => 815 fun (P : nat -> Type) (f : P O) (f0 : forall n : nat, P n -> P (S n)) =>
830 fix F (n : nat) : P n := 816 fix F (n : nat) : P n :=
831 match n as n0 return (P n0) with 817 match n as n0 return (P n0) with
832 | O => f 818 | O => f
834 end 820 end
835 : forall P : nat -> Type, 821 : forall P : nat -> Type,
836 P O -> (forall n : nat, P n -> P (S n)) -> forall n : nat, P n 822 P O -> (forall n : nat, P n -> P (S n)) -> forall n : nat, P n
837 ]] 823 ]]
838 824
839 Now we have an actual recursive definition. Expressions starting with %\index{Gallina terms!fix}%[fix] are anonymous forms of [Fixpoint], just as [fun] expressions stand for anonymous non-recursive functions. Beyond that, the syntax of [fix] mirrors that of [Fixpoint]. We can understand the definition of [nat_rect] better by reimplementing [nat_ind] using sections. *) 825 %\smallskip{}%Now we have an actual recursive definition. Expressions starting with %\index{Gallina terms!fix}%[fix] are anonymous forms of [Fixpoint], just as [fun] expressions stand for anonymous non-recursive functions. Beyond that, the syntax of [fix] mirrors that of [Fixpoint]. We can understand the definition of [nat_rect] better by reimplementing [nat_ind] using sections. *)
840 826
841 Section nat_ind'. 827 Section nat_ind'.
842 (** First, we have the property of natural numbers that we aim to prove. *) 828 (** First, we have the property of natural numbers that we aim to prove. *)
843 829
844 Variable P : nat -> Prop. 830 Variable P : nat -> Prop.
865 %\medskip% 851 %\medskip%
866 852
867 We can also examine the definition of [even_list_mut], which we generated with [Scheme] for a mutually recursive type. *) 853 We can also examine the definition of [even_list_mut], which we generated with [Scheme] for a mutually recursive type. *)
868 854
869 Print even_list_mut. 855 Print even_list_mut.
870 (** [[ 856 (** %\vspace{-.15in}%[[
871 even_list_mut = 857 even_list_mut =
872 fun (P : even_list -> Prop) (P0 : odd_list -> Prop) 858 fun (P : even_list -> Prop) (P0 : odd_list -> Prop)
873 (f : P ENil) (f0 : forall (n : nat) (o : odd_list), P0 o -> P (ECons n o)) 859 (f : P ENil) (f0 : forall (n : nat) (o : odd_list), P0 o -> P (ECons n o))
874 (f1 : forall (n : nat) (e : even_list), P e -> P0 (OCons n e)) => 860 (f1 : forall (n : nat) (e : even_list), P e -> P0 (OCons n e)) =>
875 fix F (e : even_list) : P e := 861 fix F (e : even_list) : P e :=
885 : forall (P : even_list -> Prop) (P0 : odd_list -> Prop), 871 : forall (P : even_list -> Prop) (P0 : odd_list -> Prop),
886 P ENil -> 872 P ENil ->
887 (forall (n : nat) (o : odd_list), P0 o -> P (ECons n o)) -> 873 (forall (n : nat) (o : odd_list), P0 o -> P (ECons n o)) ->
888 (forall (n : nat) (e : even_list), P e -> P0 (OCons n e)) -> 874 (forall (n : nat) (e : even_list), P e -> P0 (OCons n e)) ->
889 forall e : even_list, P e 875 forall e : even_list, P e
890 876 ]]
891 ]] 877
892 878 %\smallskip{}%We see a mutually recursive [fix], with the different functions separated by %\index{Gallina terms!with}%[with] in the same way that they would be separated by <<and>> in ML. A final %\index{Gallina terms!for}%[for] clause identifies which of the mutually recursive functions should be the final value of the [fix] expression. Using this definition as a template, we can reimplement [even_list_mut] directly. *)
893 We see a mutually recursive [fix], with the different functions separated by %\index{Gallina terms!with}%[with] in the same way that they would be separated by <<and>> in ML. A final %\index{Gallina terms!for}%[for] clause identifies which of the mutually recursive functions should be the final value of the [fix] expression. Using this definition as a template, we can reimplement [even_list_mut] directly. *)
894 879
895 Section even_list_mut'. 880 Section even_list_mut'.
896 (** First, we need the properties that we are proving. *) 881 (** First, we need the properties that we are proving. *)
897 882
898 Variable Peven : even_list -> Prop. 883 Variable Peven : even_list -> Prop.
955 nat_tree_ind 940 nat_tree_ind
956 : forall P : nat_tree -> Prop, 941 : forall P : nat_tree -> Prop,
957 P NLeaf' -> 942 P NLeaf' ->
958 (forall (n : nat) (l : list nat_tree), P (NNode' n l)) -> 943 (forall (n : nat) (l : list nat_tree), P (NNode' n l)) ->
959 forall n : nat_tree, P n 944 forall n : nat_tree, P n
960 945 ]]
961 ]] 946
962 947 %\smallskip{}%There is no command like [Scheme] that will implement an improved principle for us. In general, it takes creativity to figure out how to incorporate nested uses of different type families. This is roughly the same creativity employed in the traditional task of strengthening an induction hypothesis. Now that we know how to implement induction principles manually, we are in a position to apply just such creativity to this problem.
963 There is no command like [Scheme] that will implement an improved principle for us. In general, it takes creativity to figure out how to incorporate nested uses of different type families. This is roughly the same creativity employed in the traditional task of strengthening an induction hypothesis. Now that we know how to implement induction principles manually, we are in a position to apply just such creativity to this problem.
964 948
965 First, we will need an auxiliary definition, characterizing what it means for a property to hold of every element of a list. *) 949 First, we will need an auxiliary definition, characterizing what it means for a property to hold of every element of a list. *)
966 950
967 Section All. 951 Section All.
968 Variable T : Set. 952 Variable T : Set.
976 End All. 960 End All.
977 961
978 (** It will be useful to review the definitions of [True] and [/\], since we will want to write manual proofs of them below. *) 962 (** It will be useful to review the definitions of [True] and [/\], since we will want to write manual proofs of them below. *)
979 963
980 Print True. 964 Print True.
981 (** [[ 965 (** %\vspace{-.15in}%[[
982 Inductive True : Prop := I : True 966 Inductive True : Prop := I : True
983 ]] 967 ]]
984 968
985 That is, [True] is a proposition with exactly one proof, [I], which we may always supply trivially. 969 %\smallskip{}%That is, [True] is a proposition with exactly one proof, [I], which we may always supply trivially.
986 970
987 Finding the definition of [/\] takes a little more work. Coq supports user registration of arbitrary parsing rules, and it is such a rule that is letting us write [/\] instead of an application of some inductive type family. We can find the underlying inductive type with the %\index{Vernacular commands!Locate}%[Locate] command, whose argument may be a parsing token.%\index{Gallina terms!and}% *) 971 Finding the definition of [/\] takes a little more work. Coq supports user registration of arbitrary parsing rules, and it is such a rule that is letting us write [/\] instead of an application of some inductive type family. We can find the underlying inductive type with the %\index{Vernacular commands!Locate}%[Locate] command, whose argument may be a parsing token.%\index{Gallina terms!and}% *)
988 972
989 Locate "/\". 973 Locate "/\".
990 (** [[ 974 (** %\vspace{-.15in}%[[
991 "A /\ B" := and A B : type_scope (default interpretation) 975 "A /\ B" := and A B : type_scope (default interpretation)
992 ]] 976 ]]
993 *) 977 *)
994 978
995 Print and. 979 Print and.
996 (** [[ 980 (** %\vspace{-.15in}%[[
997 Inductive and (A : Prop) (B : Prop) : Prop := conj : A -> B -> A /\ B 981 Inductive and (A : Prop) (B : Prop) : Prop := conj : A -> B -> A /\ B
998 ]] 982 ]]
999 %\vspace{-.1in}% 983 %\vspace{-.1in}%
1000 << 984 <<
1001 For conj: Arguments A, B are implicit 985 For conj: Arguments A, B are implicit
1020 (* end thide *) 1004 (* end thide *)
1021 (* end hide *) 1005 (* end hide *)
1022 1006
1023 (** A first attempt at writing the induction principle itself follows the intuition that nested inductive type definitions are expanded into mutual inductive definitions. 1007 (** A first attempt at writing the induction principle itself follows the intuition that nested inductive type definitions are expanded into mutual inductive definitions.
1024 1008
1025 [[ 1009 %\vspace{-.15in}%[[
1026 Fixpoint nat_tree_ind' (tr : nat_tree) : P tr := 1010 Fixpoint nat_tree_ind' (tr : nat_tree) : P tr :=
1027 match tr with 1011 match tr with
1028 | NLeaf' => NLeaf'_case 1012 | NLeaf' => NLeaf'_case
1029 | NNode' n ls => NNode'_case n ls (list_nat_tree_ind ls) 1013 | NNode' n ls => NNode'_case n ls (list_nat_tree_ind ls)
1030 end 1014 end
1032 with list_nat_tree_ind (ls : list nat_tree) : All P ls := 1016 with list_nat_tree_ind (ls : list nat_tree) : All P ls :=
1033 match ls with 1017 match ls with
1034 | Nil => I 1018 | Nil => I
1035 | Cons tr rest => conj (nat_tree_ind' tr) (list_nat_tree_ind rest) 1019 | Cons tr rest => conj (nat_tree_ind' tr) (list_nat_tree_ind rest)
1036 end. 1020 end.
1037
1038 ]] 1021 ]]
1039 1022
1040 Coq rejects this definition, saying 1023 %\smallskip{}%Coq rejects this definition, saying
1041 << 1024 <<
1042 Recursive call to nat_tree_ind' has principal argument equal to "tr" 1025 Recursive call to nat_tree_ind' has principal argument equal to "tr"
1043 instead of rest. 1026 instead of rest.
1044 >> 1027 >>
1045 1028
1160 1143
1161 (* begin thide *) 1144 (* begin thide *)
1162 (** We begin with the tactic %\index{tactics!red}%[red], which is short for "one step of reduction," to unfold the definition of logical negation. *) 1145 (** We begin with the tactic %\index{tactics!red}%[red], which is short for "one step of reduction," to unfold the definition of logical negation. *)
1163 1146
1164 red. 1147 red.
1165 (** [[ 1148 (** %\vspace{-.15in}%[[
1166 ============================ 1149 ============================
1167 true = false -> False 1150 true = false -> False
1168 1151 ]]
1169 ]] 1152
1170 1153 %\smallskip{}%The negation is replaced with an implication of falsehood. We use the tactic %\index{tactics!intro}%[intro H] to change the assumption of the implication into a hypothesis named [H]. *)
1171 The negation is replaced with an implication of falsehood. We use the tactic %\index{tactics!intro}%[intro H] to change the assumption of the implication into a hypothesis named [H]. *)
1172 1154
1173 intro H. 1155 intro H.
1174 (** [[ 1156 (** %\vspace{-.15in}%[[
1175 H : true = false 1157 H : true = false
1176 ============================ 1158 ============================
1177 False 1159 False
1178 1160 ]]
1179 ]] 1161
1180 1162 %\smallskip{}%This is the point in the proof where we apply some creativity. We define a function whose utility will become clear soon. *)
1181 This is the point in the proof where we apply some creativity. We define a function whose utility will become clear soon. *)
1182 1163
1183 Definition toProp (b : bool) := if b then True else False. 1164 Definition toProp (b : bool) := if b then True else False.
1184 1165
1185 (** It is worth recalling the difference between the lowercase and uppercase versions of truth and falsehood: [True] and [False] are logical propositions, while [true] and [false] are boolean values that we can case-analyze. We have defined [toProp] such that our conclusion of [False] is computationally equivalent to [toProp false]. Thus, the %\index{tactics!change}%[change] tactic will let us change the conclusion to [toProp false]. The general form [change e] replaces the conclusion with [e], whenever Coq's built-in computation rules suffice to establish the equivalence of [e] with the original conclusion. *) 1166 (** It is worth recalling the difference between the lowercase and uppercase versions of truth and falsehood: [True] and [False] are logical propositions, while [true] and [false] are boolean values that we can case-analyze. We have defined [toProp] such that our conclusion of [False] is computationally equivalent to [toProp false]. Thus, the %\index{tactics!change}%[change] tactic will let us change the conclusion to [toProp false]. The general form [change e] replaces the conclusion with [e], whenever Coq's built-in computation rules suffice to establish the equivalence of [e] with the original conclusion. *)
1186 1167
1187 change (toProp false). 1168 change (toProp false).
1188 (** [[ 1169 (** %\vspace{-.15in}%[[
1189 H : true = false 1170 H : true = false
1190 ============================ 1171 ============================
1191 toProp false 1172 toProp false
1192 1173 ]]
1193 ]] 1174
1194 1175 %\smallskip{}%Now the righthand side of [H]'s equality appears in the conclusion, so we can rewrite, using the notation [<-] to request to replace the righthand side the equality with the lefthand side.%\index{tactics!rewrite}% *)
1195 Now the righthand side of [H]'s equality appears in the conclusion, so we can rewrite, using the notation [<-] to request to replace the righthand side the equality with the lefthand side.%\index{tactics!rewrite}% *)
1196 1176
1197 rewrite <- H. 1177 rewrite <- H.
1198 (** [[ 1178 (** %\vspace{-.15in}%[[
1199 H : true = false 1179 H : true = false
1200 ============================ 1180 ============================
1201 toProp true 1181 toProp true
1202 1182 ]]
1203 ]] 1183
1204 1184 %\smallskip{}%We are almost done. Just how close we are to done is revealed by computational simplification. *)
1205 We are almost done. Just how close we are to done is revealed by computational simplification. *)
1206 1185
1207 simpl. 1186 simpl.
1208 (** [[ 1187 (** %\vspace{-.15in}%[[
1209 H : true = false 1188 H : true = false
1210 ============================ 1189 ============================
1211 True 1190 True
1212
1213 ]] 1191 ]]
1214 *) 1192 *)
1215 1193
1216 trivial. 1194 trivial.
1217 Qed. 1195 Qed.