adam@297: (* Copyright (c) 2008-2011, Adam Chlipala adamc@70: * adamc@70: * This work is licensed under a adamc@70: * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 adamc@70: * Unported License. adamc@70: * The license text is available at: adamc@70: * http://creativecommons.org/licenses/by-nc-nd/3.0/ adamc@70: *) adamc@70: adamc@70: (* begin hide *) adamc@70: Require Import List. adamc@70: adam@314: Require Import CpdtTactics. adamc@70: adamc@70: Set Implicit Arguments. adamc@70: (* end hide *) adamc@70: adamc@70: adamc@74: (** %\part{Programming with Dependent Types} adamc@74: adamc@74: \chapter{Subset Types and Variations}% *) adamc@70: adam@335: (** 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{%##dependent types##%}% to integrate programming, specification, and proving into a single phase. The techniques we will learn make it possible to reduce the cost of program verification dramatically. *) adamc@70: adamc@70: adamc@70: (** * Introducing Subset Types *) adamc@70: adamc@70: (** Let us consider several ways of implementing the natural number predecessor function. We start by displaying the definition from the standard library: *) adamc@70: adamc@70: Print pred. adamc@212: (** %\vspace{-.15in}% [[ adamc@70: pred = fun n : nat => match n with adamc@70: | 0 => 0 adamc@70: | S u => u adamc@70: end adamc@70: : nat -> nat adamc@212: adamc@212: ]] adamc@70: adam@335: We can use a new command, %\index{Vernacular commands!Extraction}\index{program extraction}\index{extraction|see{program extraction}}%[Extraction], to produce an %\index{OCaml}%OCaml version of this function. *) adamc@70: adamc@70: Extraction pred. adamc@70: adamc@70: (** %\begin{verbatim} adamc@70: (** val pred : nat -> nat **) adamc@70: adamc@70: let pred = function adamc@70: | O -> O adamc@70: | S u -> u adamc@70: \end{verbatim}% adamc@70: adamc@70: #
adamc@70: (** val pred : nat -> nat **)
adamc@70: 
adamc@70: let pred = function
adamc@70:   | O -> O
adamc@70:   | S u -> u
adamc@70: 
# *) adamc@70: adamc@70: (** Returning 0 as the predecessor of 0 can come across as somewhat of a hack. In some situations, we might like to be sure that we never try to take the predecessor of 0. We can enforce this by giving [pred] a stronger, dependent type. *) adamc@70: adamc@70: Lemma zgtz : 0 > 0 -> False. adamc@70: crush. adamc@70: Qed. adamc@70: adamc@70: Definition pred_strong1 (n : nat) : n > 0 -> nat := adamc@212: match n with adamc@70: | O => fun pf : 0 > 0 => match zgtz pf with end adamc@70: | S n' => fun _ => n' adamc@70: end. adamc@70: adamc@70: (** We expand the type of [pred] to include a %\textit{%##proof##%}% that its argument [n] is greater than 0. When [n] is 0, we use the proof to derive a contradiction, which we can use to build a value of any type via a vacuous pattern match. When [n] is a successor, we have no need for the proof and just return the answer. The proof argument can be said to have a %\textit{%##dependent##%}% type, because its type depends on the %\textit{%##value##%}% of the argument [n]. adamc@70: adam@282: Coq's [Eval] command can execute particular invocations of [pred_strong1] just as easily as it can execute more traditional functional programs. Note that Coq has decided that argument [n] of [pred_strong1] can be made %\textit{%##implicit##%}%, since it can be deduced from the type of the second argument, so we need not write [n] in function calls. *) adam@282: adam@282: Theorem two_gt0 : 2 > 0. adam@282: crush. adam@282: Qed. adam@282: adam@282: Eval compute in pred_strong1 two_gt0. adam@282: (** %\vspace{-.15in}% [[ adam@282: = 1 adam@282: : nat adam@282: adam@282: ]] adam@282: adam@294: One aspect in particular of the definition of [pred_strong1] may be surprising. We took advantage of [Definition]'s syntactic sugar for defining function arguments in the case of [n], but we bound the proofs later with explicit [fun] expressions. Let us see what happens if we write this function in the way that at first seems most natural. adamc@70: adamc@212: [[ adamc@70: Definition pred_strong1' (n : nat) (pf : n > 0) : nat := adamc@70: match n with adamc@70: | O => match zgtz pf with end adamc@70: | S n' => n' adamc@70: end. adam@335: ]] adamc@70: adam@335: << adamc@70: Error: In environment adamc@70: n : nat adamc@70: pf : n > 0 adamc@70: The term "pf" has type "n > 0" while it is expected to have type adamc@70: "0 > 0" adam@335: >> adamc@70: adamc@212: The term [zgtz pf] fails to type-check. Somehow the type checker has failed to take into account information that follows from which [match] branch that term appears in. The problem is that, by default, [match] does not let us use such implied information. To get refined typing, we must always rely on [match] annotations, either written explicitly or inferred. adamc@70: adamc@70: In this case, we must use a [return] annotation to declare the relationship between the %\textit{%##value##%}% of the [match] discriminee and the %\textit{%##type##%}% of the result. There is no annotation that lets us declare a relationship between the discriminee and the type of a variable that is already in scope; hence, we delay the binding of [pf], so that we can use the [return] annotation to express the needed relationship. adamc@70: adam@335: We are lucky that Coq's heuristics infer the [return] clause (specifically, [return n > 0 -> nat]) for us in this case. *) adam@335: adam@335: Definition pred_strong1' (n : nat) : n > 0 -> nat := adam@335: match n return n > 0 -> nat with adam@335: | O => fun pf : 0 > 0 => match zgtz pf with end adam@335: | S n' => fun _ => n' adam@335: end. adam@335: adam@335: (** 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{%##higher-order unification##%}~\cite{HOU}% reduces to the [match] type inference problem. Over time, Coq is enhanced with more and more heuristics to get around this problem, but there must always exist [match]es whose types Coq cannot infer without annotations. adamc@70: adamc@70: Let us now take a look at the OCaml code Coq generates for [pred_strong1]. *) adamc@70: adamc@70: Extraction pred_strong1. adamc@70: adamc@70: (** %\begin{verbatim} adamc@70: (** val pred_strong1 : nat -> nat **) adamc@70: adamc@70: let pred_strong1 = function adamc@70: | O -> assert false (* absurd case *) adamc@70: | S n' -> n' adamc@70: \end{verbatim}% adamc@70: adamc@70: #
adamc@70: (** val pred_strong1 : nat -> nat **)
adamc@70: 
adamc@70: let pred_strong1 = function
adamc@70:   | O -> assert false (* absurd case *)
adamc@70:   | S n' -> n'
adamc@70: 
# *) adamc@70: adamc@70: (** The proof argument has disappeared! We get exactly the OCaml code we would have written manually. This is our first demonstration of the main technically interesting feature of Coq program extraction: program components of type [Prop] are erased systematically. adamc@70: adam@335: We can reimplement our dependently typed [pred] based on %\index{subset types}\textit{%##subset types##%}%, defined in the standard library with the type family %\index{Gallina terms!sig}%[sig]. *) adamc@70: adamc@70: Print sig. adamc@212: (** %\vspace{-.15in}% [[ adamc@70: Inductive sig (A : Type) (P : A -> Prop) : Type := adamc@70: exist : forall x : A, P x -> sig P adamc@212: adamc@70: ]] adamc@70: adam@335: The family [sig] is a Curry-Howard twin of [ex], except that [sig] is in [Type], while [ex] is in [Prop]. That means that [sig] values can survive extraction, while [ex] proofs will always be erased. The actual details of extraction of [sig]s are more subtle, as we will see shortly. adamc@70: adamc@70: We rewrite [pred_strong1], using some syntactic sugar for subset types. *) adamc@70: adamc@70: Locate "{ _ : _ | _ }". adamc@212: (** %\vspace{-.15in}% [[ adam@335: Notation adamc@70: "{ x : A | P }" := sig (fun x : A => P) adam@302: ]] adam@302: *) adamc@70: adamc@70: Definition pred_strong2 (s : {n : nat | n > 0}) : nat := adamc@70: match s with adamc@70: | exist O pf => match zgtz pf with end adamc@70: | exist (S n') _ => n' adamc@70: end. adamc@70: adam@335: (** To build a value of a subset type, we use the [exist] constructor, and the details of how to do that follow from the output of our earlier [Print sig] command (where we elided the extra information that parameter [A] is implicit). *) adam@282: adam@282: Eval compute in pred_strong2 (exist _ 2 two_gt0). adam@282: (** %\vspace{-.15in}% [[ adam@282: = 1 adam@282: : nat adam@302: ]] adam@302: *) adam@282: adamc@70: Extraction pred_strong2. adamc@70: adamc@70: (** %\begin{verbatim} adamc@70: (** val pred_strong2 : nat -> nat **) adamc@70: adamc@70: let pred_strong2 = function adamc@70: | O -> assert false (* absurd case *) adamc@70: | S n' -> n' adamc@70: \end{verbatim}% adamc@70: adamc@70: #
adamc@70: (** val pred_strong2 : nat -> nat **)
adamc@70: 
adamc@70: let pred_strong2 = function
adamc@70:   | O -> assert false (* absurd case *)
adamc@70:   | S n' -> n'
adamc@70: 
# adamc@70: adamc@70: We arrive at the same OCaml code as was extracted from [pred_strong1], which may seem surprising at first. The reason is that a value of [sig] is a pair of two pieces, a value and a proof about it. Extraction erases the proof, which reduces the constructor [exist] of [sig] to taking just a single argument. An optimization eliminates uses of datatypes with single constructors taking single arguments, and we arrive back where we started. adamc@70: adamc@70: We can continue on in the process of refining [pred]'s type. Let us change its result type to capture that the output is really the predecessor of the input. *) adamc@70: adamc@70: Definition pred_strong3 (s : {n : nat | n > 0}) : {m : nat | proj1_sig s = S m} := adamc@70: match s return {m : nat | proj1_sig s = S m} with adamc@70: | exist 0 pf => match zgtz pf with end adamc@212: | exist (S n') pf => exist _ n' (refl_equal _) adamc@70: end. adamc@70: adam@282: Eval compute in pred_strong3 (exist _ 2 two_gt0). adam@282: (** %\vspace{-.15in}% [[ adam@282: = exist (fun m : nat => 2 = S m) 1 (refl_equal 2) adam@282: : {m : nat | proj1_sig (exist (lt 0) 2 two_gt0) = S m} adam@335: ]] adam@302: *) adam@282: adam@335: (** The function %\index{Gallina terms!proj1\_sig}%[proj1_sig] extracts the base value from a subset type. It turns out that we need to include an explicit [return] clause here, since Coq's heuristics are not smart enough to propagate the result type that we wrote earlier. adamc@70: adamc@70: By now, the reader is probably ready to believe that the new [pred_strong] leads to the same OCaml code as we have seen several times so far, and Coq does not disappoint. *) adamc@70: adamc@70: Extraction pred_strong3. adamc@70: adamc@70: (** %\begin{verbatim} adamc@70: (** val pred_strong3 : nat -> nat **) adamc@70: adamc@70: let pred_strong3 = function adamc@70: | O -> assert false (* absurd case *) adamc@70: | S n' -> n' adamc@70: \end{verbatim}% adamc@70: adamc@70: #
adamc@70: (** val pred_strong3 : nat -> nat **)
adamc@70: 
adamc@70: let pred_strong3 = function
adamc@70:   | O -> assert false (* absurd case *)
adamc@70:   | S n' -> n'
adamc@70: 
# adamc@70: adam@335: We have managed to reach a type that is, in a formal sense, the most expressive possible for [pred]. Any other implementation of the same type must have the same input-output behavior. However, there is still room for improvement in making this kind of code easier to write. Here is a version that takes advantage of tactic-based theorem proving. We switch back to passing a separate proof argument instead of using a subset type for the function's input, because this leads to cleaner code. (Recall that [False_rec] is the [Set]-level induction principle for [False], which can be used to produce a value in any [Set] given a proof of [False].) *) adamc@70: adam@297: Definition pred_strong4 : forall n : nat, n > 0 -> {m : nat | n = S m}. adamc@70: refine (fun n => adamc@212: match n with adamc@70: | O => fun _ => False_rec _ _ adamc@70: | S n' => fun _ => exist _ n' _ adamc@70: end). adamc@212: adamc@77: (* begin thide *) adam@335: (** We build [pred_strong4] using tactic-based proving, beginning with a [Definition] command that ends in a period before a definition is given. Such a command enters the interactive proving mode, with the type given for the new identifier as our proof goal. It may seem strange to change perspective so implicitly between programming and proving, but recall that programs and proofs are two sides of the same coin in Coq, thanks to the Curry-Howard correspondence. adamc@70: adam@335: We do most of the work with the %\index{tactics!refine}%[refine] tactic, to which we pass a partial %``%#"#proof#"#%''% of the type we are trying to prove. There may be some pieces left to fill in, indicated by underscores. Any underscore that Coq cannot reconstruct with type inference is added as a proof subgoal. In this case, we have two subgoals: adam@335: adam@335: %\vspace{.1in} \noindent 2 \coqdockw{subgoals}\vspace{-.1in}%#2 subgoals# adam@335: [[ adamc@70: adamc@70: n : nat adamc@70: _ : 0 > 0 adamc@70: ============================ adamc@70: False adam@335: ]] adam@335: %\noindent \coqdockw{subgoal} 2 \coqdockw{is}:%#subgoal 2 is# adam@335: [[ adamc@70: S n' = S n' adamc@70: ]] adamc@70: adamc@70: We can see that the first subgoal comes from the second underscore passed to [False_rec], and the second subgoal comes from the second underscore passed to [exist]. In the first case, we see that, though we bound the proof variable with an underscore, it is still available in our proof context. It is hard to refer to underscore-named variables in manual proofs, but automation makes short work of them. Both subgoals are easy to discharge that way, so let us back up and ask to prove all subgoals automatically. *) adamc@70: adamc@70: Undo. adamc@70: refine (fun n => adamc@212: match n with adamc@70: | O => fun _ => False_rec _ _ adamc@70: | S n' => fun _ => exist _ n' _ adamc@70: end); crush. adamc@77: (* end thide *) adamc@70: Defined. adamc@70: adam@335: (** 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{%##transparent##%}%, allowing it to be unfolded; while [Qed] marks an identifier as %\index{opaque}\emph{%##opaque##%}%, preventing unfolding.) Let us see what our proof script constructed. *) adamc@70: adamc@70: Print pred_strong4. adamc@212: (** %\vspace{-.15in}% [[ adamc@70: pred_strong4 = adamc@70: fun n : nat => adamc@70: match n as n0 return (n0 > 0 -> {m : nat | n0 = S m}) with adamc@70: | 0 => adamc@70: fun _ : 0 > 0 => adamc@70: False_rec {m : nat | 0 = S m} adamc@70: (Bool.diff_false_true adamc@70: (Bool.absurd_eq_true false adamc@70: (Bool.diff_false_true adamc@70: (Bool.absurd_eq_true false (pred_strong4_subproof n _))))) adamc@70: | S n' => adamc@70: fun _ : S n' > 0 => adamc@70: exist (fun m : nat => S n' = S m) n' (refl_equal (S n')) adamc@70: end adamc@70: : forall n : nat, n > 0 -> {m : nat | n = S m} adamc@212: adamc@70: ]] adamc@70: adam@282: We see the code we entered, with some proofs filled in. The first proof obligation, the second argument to [False_rec], is filled in with a nasty-looking proof term that we can be glad we did not enter by hand. The second proof obligation is a simple reflexivity proof. *) adamc@70: adam@282: Eval compute in pred_strong4 two_gt0. adam@282: (** %\vspace{-.15in}% [[ adam@282: = exist (fun m : nat => 2 = S m) 1 (refl_equal 2) adam@282: : {m : nat | 2 = S m} adam@282: ]] adam@282: adam@335: A tactic modifier called %\index{tactics!abstract}%[abstract] can be helpful for producing shorter terms, by automatically abstracting subgoals into named lemmas. *) adam@335: adam@335: (* begin thide *) adam@335: Definition pred_strong4' : forall n : nat, n > 0 -> {m : nat | n = S m}. adam@335: refine (fun n => adam@335: match n with adam@335: | O => fun _ => False_rec _ _ adam@335: | S n' => fun _ => exist _ n' _ adam@335: end); abstract crush. adam@335: Defined. adam@335: adam@335: Print pred_strong4'. adam@335: (* end thide *) adam@335: adam@335: (** %\vspace{-.15in}% [[ adam@335: pred_strong4' = adam@335: fun n : nat => adam@335: match n as n0 return (n0 > 0 -> {m : nat | n0 = S m}) with adam@335: | 0 => adam@335: fun _H : 0 > 0 => adam@335: False_rec {m : nat | 0 = S m} (pred_strong4'_subproof n _H) adam@335: | S n' => adam@335: fun _H : S n' > 0 => adam@335: exist (fun m : nat => S n' = S m) n' (pred_strong4'_subproof0 n _H) adam@335: end adam@335: : forall n : nat, n > 0 -> {m : nat | n = S m} adam@335: ]] adam@335: adam@335: 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. *) adamc@70: adamc@70: Notation "!" := (False_rec _ _). adamc@70: Notation "[ e ]" := (exist _ e _). adamc@70: adam@297: Definition pred_strong5 : forall n : nat, n > 0 -> {m : nat | n = S m}. adamc@70: refine (fun n => adamc@212: match n with adamc@70: | O => fun _ => ! adamc@70: | S n' => fun _ => [n'] adamc@70: end); crush. adamc@70: Defined. adamc@71: adam@282: (** By default, notations are also used in pretty-printing terms, including results of evaluation. *) adam@282: adam@282: Eval compute in pred_strong5 two_gt0. adam@282: (** %\vspace{-.15in}% [[ adam@282: = [1] adam@282: : {m : nat | 2 = S m} adam@282: ]] adam@282: adam@335: One other alternative is worth demonstrating. Recent Coq versions include a facility called %\index{Program}%[Program] that streamlines this style of definition. Here is a complete implementation using [Program].%\index{Vernacular commands!Obligation Tactic}\index{Vernacular commands!Program Definition}% *) adamc@212: adamc@212: Obligation Tactic := crush. adamc@212: adamc@212: Program Definition pred_strong6 (n : nat) (_ : n > 0) : {m : nat | n = S m} := adamc@212: match n with adamc@212: | O => _ adamc@212: | S n' => n' adamc@212: end. adamc@212: adam@335: (** Printing the resulting definition of [pred_strong6] yields a term very similar to what we built with [refine]. [Program] can save time in writing programs that use subset types. Nonetheless, [refine] is often just as effective, and [refine] gives you more control over the form the final term takes, which can be useful when you want to prove additional theorems about your definition. [Program] will sometimes insert type casts that can complicate theorem proving. *) adamc@212: adam@282: Eval compute in pred_strong6 two_gt0. adam@282: (** %\vspace{-.15in}% [[ adam@282: = [1] adam@282: : {m : nat | 2 = S m} adam@302: ]] adam@335: adam@335: In this case, we see that the new definition yields the same computational behavior as before. *) adam@282: adamc@71: adamc@71: (** * Decidable Proposition Types *) adamc@71: adam@335: (** There is another type in the standard library which captures the idea of program values that indicate which of two propositions is true.%\index{Gallina terms!sumbool}% *) adamc@71: adamc@71: Print sumbool. adamc@212: (** %\vspace{-.15in}% [[ adamc@71: Inductive sumbool (A : Prop) (B : Prop) : Set := adamc@71: left : A -> {A} + {B} | right : B -> {A} + {B} adamc@212: ]] adamc@71: adamc@212: We can define some notations to make working with [sumbool] more convenient. *) adamc@71: adamc@71: Notation "'Yes'" := (left _ _). adamc@71: Notation "'No'" := (right _ _). adamc@71: Notation "'Reduce' x" := (if x then Yes else No) (at level 50). adamc@71: adamc@71: (** The [Reduce] notation is notable because it demonstrates how [if] is overloaded in Coq. The [if] form actually works when the test expression has any two-constructor inductive type. Moreover, in the [then] and [else] branches, the appropriate constructor arguments are bound. This is important when working with [sumbool]s, when we want to have the proof stored in the test expression available when proving the proof obligations generated in the appropriate branch. adamc@71: adamc@71: Now we can write [eq_nat_dec], which compares two natural numbers, returning either a proof of their equality or a proof of their inequality. *) adamc@71: adam@297: Definition eq_nat_dec : forall n m : nat, {n = m} + {n <> m}. adamc@212: refine (fix f (n m : nat) : {n = m} + {n <> m} := adamc@212: match n, m with adamc@71: | O, O => Yes adamc@71: | S n', S m' => Reduce (f n' m') adamc@71: | _, _ => No adamc@71: end); congruence. adamc@71: Defined. adamc@71: adam@282: Eval compute in eq_nat_dec 2 2. adam@282: (** %\vspace{-.15in}% [[ adam@282: = Yes adam@282: : {2 = 2} + {2 <> 2} adam@302: ]] adam@302: *) adam@282: adam@282: Eval compute in eq_nat_dec 2 3. adam@282: (** %\vspace{-.15in}% [[ adam@282: = No adam@282: : {2 = 2} + {2 <> 2} adam@302: ]] adam@302: *) adam@282: adam@335: (** Note that the [Yes] and [No] notations are hiding proofs establishing the correctness of the outputs. adam@335: adam@335: Our definition extracts to reasonable OCaml code. *) adamc@71: adamc@71: Extraction eq_nat_dec. adamc@71: adamc@71: (** %\begin{verbatim} adamc@71: (** val eq_nat_dec : nat -> nat -> sumbool **) adamc@71: adamc@71: let rec eq_nat_dec n m = adamc@71: match n with adamc@71: | O -> (match m with adamc@71: | O -> Left adamc@71: | S n0 -> Right) adamc@71: | S n' -> (match m with adamc@71: | O -> Right adamc@71: | S m' -> eq_nat_dec n' m') adamc@71: \end{verbatim}% adamc@71: adamc@71: #
adamc@71: (** val eq_nat_dec : nat -> nat -> sumbool **)
adamc@71: 
adamc@71: let rec eq_nat_dec n m =
adamc@71:   match n with
adamc@71:     | O -> (match m with
adamc@71:               | O -> Left
adamc@71:               | S n0 -> Right)
adamc@71:     | S n' -> (match m with
adamc@71:                  | O -> Right
adamc@71:                  | S m' -> eq_nat_dec n' m')
adamc@71: 
# adamc@71: adam@335: Proving this kind of decidable equality result is so common that Coq comes with a tactic for automating it.%\index{tactics!decide equality}% *) adamc@71: adamc@71: Definition eq_nat_dec' (n m : nat) : {n = m} + {n <> m}. adamc@71: decide equality. adamc@71: Defined. adamc@71: adam@335: (** 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{%##Left##%}% and %\texttt{%##Right##%}% constructors instead of the boolean values built into OCaml. We can fix this, by using Coq's facility for mapping Coq inductive types to OCaml variant types.%\index{Vernacular commands!Extract Inductive}% *) adamc@71: adamc@71: Extract Inductive sumbool => "bool" ["true" "false"]. adamc@71: Extraction eq_nat_dec'. adamc@71: adamc@71: (** %\begin{verbatim} adamc@71: (** val eq_nat_dec' : nat -> nat -> bool **) adamc@71: adamc@71: let rec eq_nat_dec' n m0 = adamc@71: match n with adamc@71: | O -> (match m0 with adamc@71: | O -> true adamc@71: | S n0 -> false) adamc@71: | S n0 -> (match m0 with adamc@71: | O -> false adamc@71: | S n1 -> eq_nat_dec' n0 n1) adamc@71: \end{verbatim}% adamc@71: adamc@71: #
adamc@71: (** val eq_nat_dec' : nat -> nat -> bool **)
adamc@71: 
adamc@71: let rec eq_nat_dec' n m0 =
adamc@71:   match n with
adamc@71:     | O -> (match m0 with
adamc@71:               | O -> true
adamc@71:               | S n0 -> false)
adamc@71:     | S n0 -> (match m0 with
adamc@71:                  | O -> false
adamc@71:                  | S n1 -> eq_nat_dec' n0 n1)
adamc@71: 
# *) adamc@72: adamc@72: (** %\smallskip% adamc@72: adam@292: We can build %``%#"#smart#"#%''% versions of the usual boolean operators and put them to good use in certified programming. For instance, here is a [sumbool] version of boolean %``%#"#or.#"#%''% *) adamc@72: adam@337: (* EX: Write a function that decides if an element belongs to a list. *) adam@337: adamc@77: (* begin thide *) adamc@204: Notation "x || y" := (if x then Yes else Reduce y). adamc@72: adamc@72: (** Let us use it for building a function that decides list membership. We need to assume the existence of an equality decision procedure for the type of list elements. *) adamc@72: adamc@72: Section In_dec. adamc@72: Variable A : Set. adamc@72: Variable A_eq_dec : forall x y : A, {x = y} + {x <> y}. adamc@72: adamc@72: (** The final function is easy to write using the techniques we have developed so far. *) adamc@72: adamc@212: Definition In_dec : forall (x : A) (ls : list A), {In x ls} + {~ In x ls}. adamc@212: refine (fix f (x : A) (ls : list A) : {In x ls} + {~ In x ls} := adamc@212: match ls with adamc@72: | nil => No adamc@72: | x' :: ls' => A_eq_dec x x' || f x ls' adamc@72: end); crush. adam@282: Defined. adamc@72: End In_dec. adamc@72: adam@282: Eval compute in In_dec eq_nat_dec 2 (1 :: 2 :: nil). adam@282: (** %\vspace{-.15in}% [[ adam@282: = Yes adam@282: : {In 2 (1 :: 2 :: nil)} + {~ In 2 (1 :: 2 :: nil)} adam@302: ]] adam@302: *) adam@282: adam@282: Eval compute in In_dec eq_nat_dec 3 (1 :: 2 :: nil). adam@282: (** %\vspace{-.15in}% [[ adam@282: = No adam@282: : {In 3 (1 :: 2 :: nil)} + {~ In 3 (1 :: 2 :: nil)} adam@302: ]] adam@302: *) adam@282: adamc@72: (** [In_dec] has a reasonable extraction to OCaml. *) adamc@72: adamc@72: Extraction In_dec. adamc@77: (* end thide *) adamc@72: adamc@72: (** %\begin{verbatim} adamc@72: (** val in_dec : ('a1 -> 'a1 -> bool) -> 'a1 -> 'a1 list -> bool **) adamc@72: adamc@72: let rec in_dec a_eq_dec x = function adamc@72: | Nil -> false adamc@72: | Cons (x', ls') -> adamc@72: (match a_eq_dec x x' with adamc@72: | true -> true adamc@72: | false -> in_dec a_eq_dec x ls') adamc@72: \end{verbatim}% adamc@72: adamc@72: #
adamc@72: (** val in_dec : ('a1 -> 'a1 -> bool) -> 'a1 -> 'a1 list -> bool **)
adamc@72: 
adamc@72: let rec in_dec a_eq_dec x = function
adamc@72:   | Nil -> false
adamc@72:   | Cons (x', ls') ->
adamc@72:       (match a_eq_dec x x' with
adamc@72:          | true -> true
adamc@72:          | false -> in_dec a_eq_dec x ls')
adamc@72: 
# *) adamc@72: adamc@72: adamc@72: (** * Partial Subset Types *) adamc@72: adam@335: (** Our final implementation of dependent predecessor used a very specific argument type to ensure that execution could always complete normally. Sometimes we want to allow execution to fail, and we want a more principled way of signaling failure than returning a default value, as [pred] does for [0]. One approach is to define this type family %\index{Gallina terms!maybe}%[maybe], which is a version of [sig] that allows obligation-free failure. *) adamc@73: adamc@89: Inductive maybe (A : Set) (P : A -> Prop) : Set := adamc@72: | Unknown : maybe P adamc@72: | Found : forall x : A, P x -> maybe P. adamc@72: adamc@73: (** We can define some new notations, analogous to those we defined for subset types. *) adamc@73: adamc@72: Notation "{{ x | P }}" := (maybe (fun x => P)). adamc@72: Notation "??" := (Unknown _). adam@335: Notation "[| x |]" := (Found _ x _). adamc@72: adamc@73: (** Now our next version of [pred] is trivial to write. *) adamc@73: adam@297: Definition pred_strong7 : forall n : nat, {{m | n = S m}}. adamc@73: refine (fun n => adamc@212: match n with adamc@73: | O => ?? adam@335: | S n' => [|n'|] adamc@73: end); trivial. adamc@73: Defined. adamc@73: adam@282: Eval compute in pred_strong7 2. adam@282: (** %\vspace{-.15in}% [[ adam@335: = [|1|] adam@282: : {{m | 2 = S m}} adam@335: ]] adam@302: *) adam@282: adam@282: Eval compute in pred_strong7 0. adam@282: (** %\vspace{-.15in}% [[ adam@282: = ?? adam@282: : {{m | 0 = S m}} adam@282: ]] adam@282: adam@335: Because we used [maybe], one valid implementation of the type we gave [pred_strong7] would return [??] in every case. We can strengthen the type to rule out such vacuous implementations, and the type family %\index{Gallina terms!sumor}%[sumor] from the standard library provides the easiest starting point. For type [A] and proposition [B], [A + {B}] desugars to [sumor A B], whose values are either values of [A] or proofs of [B]. *) adamc@73: adamc@73: Print sumor. adamc@212: (** %\vspace{-.15in}% [[ adamc@73: Inductive sumor (A : Type) (B : Prop) : Type := adamc@73: inleft : A -> A + {B} | inright : B -> A + {B} adam@302: ]] adam@302: *) adamc@73: adamc@73: (** We add notations for easy use of the [sumor] constructors. The second notation is specialized to [sumor]s whose [A] parameters are instantiated with regular subset types, since this is how we will use [sumor] below. *) adamc@73: adamc@73: Notation "!!" := (inright _ _). adam@335: Notation "[|| x ||]" := (inleft _ [x]). adamc@73: adam@335: (** Now we are ready to give the final version of possibly failing predecessor. The [sumor]-based type that we use is maximally expressive; any implementation of the type has the same input-output behavior. *) adamc@73: adam@297: Definition pred_strong8 : forall n : nat, {m : nat | n = S m} + {n = 0}. adamc@73: refine (fun n => adamc@212: match n with adamc@73: | O => !! adam@335: | S n' => [||n'||] adamc@73: end); trivial. adamc@73: Defined. adamc@73: adam@282: Eval compute in pred_strong8 2. adam@282: (** %\vspace{-.15in}% [[ adam@335: = [||1||] adam@282: : {m : nat | 2 = S m} + {2 = 0} adam@302: ]] adam@302: *) adam@282: adam@282: Eval compute in pred_strong8 0. adam@282: (** %\vspace{-.15in}% [[ adam@282: = !! adam@282: : {m : nat | 0 = S m} + {0 = 0} adam@302: ]] adam@302: *) adam@282: adam@335: (** As with our other maximally expressive [pred] function, we arrive at quite simple output values, thanks to notations. *) adam@335: adamc@73: adamc@73: (** * Monadic Notations *) adamc@73: adam@335: (** We can treat [maybe] like a monad%~\cite{Monads}\index{monad}\index{failure monad}%, in the same way that the Haskell [Maybe] type is interpreted as a failure monad. Our [maybe] has the wrong type to be a literal monad, but a %``%#"#bind#"#%''%-like notation will still be helpful. *) adamc@73: adamc@72: Notation "x <- e1 ; e2" := (match e1 with adamc@72: | Unknown => ?? adamc@72: | Found x _ => e2 adamc@72: end) adamc@72: (right associativity, at level 60). adamc@72: adamc@73: (** 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{%##does##%}% find an answer, pass that answer on to [e2] to find the final result. The variable [x] can be considered bound in [e2]. adamc@73: adam@335: This notation is very helpful for composing richly typed procedures. For instance, here is a very simple implementation of a function to take the predecessors of two naturals at once. *) adamc@73: adam@337: (* EX: Write a function that tries to compute predecessors of two [nat]s at once. *) adam@337: adam@337: (* begin thide *) adam@297: Definition doublePred : forall n1 n2 : nat, {{p | n1 = S (fst p) /\ n2 = S (snd p)}}. adamc@73: refine (fun n1 n2 => adamc@212: m1 <- pred_strong7 n1; adamc@212: m2 <- pred_strong7 n2; adam@335: [|(m1, m2)|]); tauto. adamc@73: Defined. adam@337: (* end thide *) adamc@73: adam@292: (** We can build a [sumor] version of the %``%#"#bind#"#%''% notation and use it to write a similarly straightforward version of this function. *) adamc@73: adamc@73: (** printing <-- $\longleftarrow$ *) adamc@73: adamc@73: Notation "x <-- e1 ; e2" := (match e1 with adamc@73: | inright _ => !! adamc@73: | inleft (exist x _) => e2 adamc@73: end) adamc@73: (right associativity, at level 60). adamc@73: adamc@73: (** printing * $\times$ *) adamc@73: adam@337: (* EX: Write a more expressively typed version of the last exercise. *) adam@337: adam@337: (* begin thide *) adam@297: Definition doublePred' : forall n1 n2 : nat, adam@297: {p : nat * nat | n1 = S (fst p) /\ n2 = S (snd p)} adamc@73: + {n1 = 0 \/ n2 = 0}. adamc@73: refine (fun n1 n2 => adamc@212: m1 <-- pred_strong8 n1; adamc@212: m2 <-- pred_strong8 n2; adam@335: [||(m1, m2)||]); tauto. adamc@73: Defined. adam@337: (* end thide *) adamc@72: adamc@72: adamc@72: (** * A Type-Checking Example *) adamc@72: adam@335: (** We can apply these specification types to build a certified type checker for a simple expression language. *) adamc@75: adamc@72: Inductive exp : Set := adamc@72: | Nat : nat -> exp adamc@72: | Plus : exp -> exp -> exp adamc@72: | Bool : bool -> exp adamc@72: | And : exp -> exp -> exp. adamc@72: adamc@75: (** We define a simple language of types and its typing rules, in the style introduced in Chapter 4. *) adamc@75: adamc@72: Inductive type : Set := TNat | TBool. adamc@72: adamc@72: Inductive hasType : exp -> type -> Prop := adamc@72: | HtNat : forall n, adamc@72: hasType (Nat n) TNat adamc@72: | HtPlus : forall e1 e2, adamc@72: hasType e1 TNat adamc@72: -> hasType e2 TNat adamc@72: -> hasType (Plus e1 e2) TNat adamc@72: | HtBool : forall b, adamc@72: hasType (Bool b) TBool adamc@72: | HtAnd : forall e1 e2, adamc@72: hasType e1 TBool adamc@72: -> hasType e2 TBool adamc@72: -> hasType (And e1 e2) TBool. adamc@72: adamc@75: (** It will be helpful to have a function for comparing two types. We build one using [decide equality]. *) adamc@75: adamc@77: (* begin thide *) adamc@75: Definition eq_type_dec : forall t1 t2 : type, {t1 = t2} + {t1 <> t2}. adamc@72: decide equality. adamc@72: Defined. adamc@72: adam@292: (** Another notation complements the monadic notation for [maybe] that we defined earlier. Sometimes we want to include %``%#"#assertions#"#%''% in our procedures. That is, we want to run a decision procedure and fail if it fails; otherwise, we want to continue, with the proof that it produced made available to us. This infix notation captures that idea, for a procedure that returns an arbitrary two-constructor type. *) adamc@75: adamc@73: Notation "e1 ;; e2" := (if e1 then e2 else ??) adamc@73: (right associativity, at level 60). adamc@73: adam@335: (** With that notation defined, we can implement a [typeCheck] function, whose code is only more complex than what we would write in ML because it needs to include some extra type annotations. Every [[|e|]] expression adds a [hasType] proof obligation, and [crush] makes short work of them when we add [hasType]'s constructors as hints. *) adamc@77: (* end thide *) adamc@75: adam@297: Definition typeCheck : forall e : exp, {{t | hasType e t}}. adamc@77: (* begin thide *) adamc@72: Hint Constructors hasType. adamc@72: adamc@72: refine (fix F (e : exp) : {{t | hasType e t}} := adamc@212: match e with adam@335: | Nat _ => [|TNat|] adamc@72: | Plus e1 e2 => adamc@72: t1 <- F e1; adamc@72: t2 <- F e2; adamc@72: eq_type_dec t1 TNat;; adamc@72: eq_type_dec t2 TNat;; adam@335: [|TNat|] adam@335: | Bool _ => [|TBool|] adamc@72: | And e1 e2 => adamc@72: t1 <- F e1; adamc@72: t2 <- F e2; adamc@72: eq_type_dec t1 TBool;; adamc@72: eq_type_dec t2 TBool;; adam@335: [|TBool|] adamc@72: end); crush. adamc@77: (* end thide *) adamc@72: Defined. adamc@72: adamc@75: (** Despite manipulating proofs, our type checker is easy to run. *) adamc@75: adamc@72: Eval simpl in typeCheck (Nat 0). adamc@212: (** %\vspace{-.15in}% [[ adam@335: = [|TNat|] adamc@75: : {{t | hasType (Nat 0) t}} adam@302: ]] adam@302: *) adamc@75: adamc@72: Eval simpl in typeCheck (Plus (Nat 1) (Nat 2)). adamc@212: (** %\vspace{-.15in}% [[ adam@335: = [|TNat|] adamc@75: : {{t | hasType (Plus (Nat 1) (Nat 2)) t}} adam@302: ]] adam@302: *) adamc@75: adamc@72: Eval simpl in typeCheck (Plus (Nat 1) (Bool false)). adamc@212: (** %\vspace{-.15in}% [[ adamc@75: = ?? adamc@75: : {{t | hasType (Plus (Nat 1) (Bool false)) t}} adam@302: ]] adam@302: *) adamc@75: adam@335: (** The type checker also extracts to some reasonable OCaml code. *) adamc@75: adamc@75: Extraction typeCheck. adamc@75: adamc@75: (** %\begin{verbatim} adamc@75: (** val typeCheck : exp -> type0 maybe **) adamc@75: adamc@75: let rec typeCheck = function adamc@75: | Nat n -> Found TNat adamc@75: | Plus (e1, e2) -> adamc@75: (match typeCheck e1 with adamc@75: | Unknown -> Unknown adamc@75: | Found t1 -> adamc@75: (match typeCheck e2 with adamc@75: | Unknown -> Unknown adamc@75: | Found t2 -> adamc@75: (match eq_type_dec t1 TNat with adamc@75: | true -> adamc@75: (match eq_type_dec t2 TNat with adamc@75: | true -> Found TNat adamc@75: | false -> Unknown) adamc@75: | false -> Unknown))) adamc@75: | Bool b -> Found TBool adamc@75: | And (e1, e2) -> adamc@75: (match typeCheck e1 with adamc@75: | Unknown -> Unknown adamc@75: | Found t1 -> adamc@75: (match typeCheck e2 with adamc@75: | Unknown -> Unknown adamc@75: | Found t2 -> adamc@75: (match eq_type_dec t1 TBool with adamc@75: | true -> adamc@75: (match eq_type_dec t2 TBool with adamc@75: | true -> Found TBool adamc@75: | false -> Unknown) adamc@75: | false -> Unknown))) adamc@75: \end{verbatim}% adamc@75: adamc@75: #
adamc@75: (** val typeCheck : exp -> type0 maybe **)
adamc@75: 
adamc@75: let rec typeCheck = function
adamc@75:   | Nat n -> Found TNat
adamc@75:   | Plus (e1, e2) ->
adamc@75:       (match typeCheck e1 with
adamc@75:          | Unknown -> Unknown
adamc@75:          | Found t1 ->
adamc@75:              (match typeCheck e2 with
adamc@75:                 | Unknown -> Unknown
adamc@75:                 | Found t2 ->
adamc@75:                     (match eq_type_dec t1 TNat with
adamc@75:                        | true ->
adamc@75:                            (match eq_type_dec t2 TNat with
adamc@75:                               | true -> Found TNat
adamc@75:                               | false -> Unknown)
adamc@75:                        | false -> Unknown)))
adamc@75:   | Bool b -> Found TBool
adamc@75:   | And (e1, e2) ->
adamc@75:       (match typeCheck e1 with
adamc@75:          | Unknown -> Unknown
adamc@75:          | Found t1 ->
adamc@75:              (match typeCheck e2 with
adamc@75:                 | Unknown -> Unknown
adamc@75:                 | Found t2 ->
adamc@75:                     (match eq_type_dec t1 TBool with
adamc@75:                        | true ->
adamc@75:                            (match eq_type_dec t2 TBool with
adamc@75:                               | true -> Found TBool
adamc@75:                               | false -> Unknown)
adamc@75:                        | false -> Unknown)))
adamc@75: 
# *) adamc@75: adamc@75: (** %\smallskip% adamc@75: adam@292: We can adapt this implementation to use [sumor], so that we know our type-checker only fails on ill-typed inputs. First, we define an analogue to the %``%#"#assertion#"#%''% notation. *) adamc@73: adamc@77: (* begin thide *) adamc@73: Notation "e1 ;;; e2" := (if e1 then e2 else !!) adamc@73: (right associativity, at level 60). adamc@73: adamc@75: (** Next, we prove a helpful lemma, which states that a given expression can have at most one type. *) adamc@75: adamc@75: Lemma hasType_det : forall e t1, adamc@73: hasType e t1 adam@335: -> forall t2, hasType e t2 adamc@73: -> t1 = t2. adamc@73: induction 1; inversion 1; crush. adamc@73: Qed. adamc@73: adamc@75: (** Now we can define the type-checker. Its type expresses that it only fails on untypable expressions. *) adamc@75: adam@335: (** printing <-- $\longleftarrow$ *) adam@335: adamc@77: (* end thide *) adam@297: Definition typeCheck' : forall e : exp, {t : type | hasType e t} + {forall t, ~ hasType e t}. adamc@77: (* begin thide *) adamc@73: Hint Constructors hasType. adamc@75: (** We register all of the typing rules as hints. *) adamc@75: adamc@73: Hint Resolve hasType_det. adam@335: (** The lemma [hasType_det] will also be useful for proving proof obligations with contradictory contexts. Since its statement includes [forall]-bound variables that do not appear in its conclusion, only [eauto] will apply this hint. *) adamc@73: adamc@75: (** Finally, the implementation of [typeCheck] can be transcribed literally, simply switching notations as needed. *) adamc@212: adamc@212: refine (fix F (e : exp) : {t : type | hasType e t} + {forall t, ~ hasType e t} := adamc@212: match e with adam@335: | Nat _ => [||TNat||] adamc@73: | Plus e1 e2 => adamc@73: t1 <-- F e1; adamc@73: t2 <-- F e2; adamc@73: eq_type_dec t1 TNat;;; adamc@73: eq_type_dec t2 TNat;;; adam@335: [||TNat||] adam@335: | Bool _ => [||TBool||] adamc@73: | And e1 e2 => adamc@73: t1 <-- F e1; adamc@73: t2 <-- F e2; adamc@73: eq_type_dec t1 TBool;;; adamc@73: eq_type_dec t2 TBool;;; adam@335: [||TBool||] adamc@73: end); clear F; crush' tt hasType; eauto. adamc@75: adam@335: (** We clear [F], the local name for the recursive function, to avoid strange proofs that refer to recursive calls that we never make. The [crush] variant %\index{tactics!crush'}%[crush'] helps us by performing automatic inversion on instances of the predicates specified in its second argument. Once we throw in [eauto] to apply [hasType_det] for us, we have discharged all the subgoals. *) adamc@77: (* end thide *) adamc@212: adamc@212: adamc@73: Defined. adamc@73: adamc@75: (** The short implementation here hides just how time-saving automation is. Every use of one of the notations adds a proof obligation, giving us 12 in total. Most of these obligations require multiple inversions and either uses of [hasType_det] or applications of [hasType] rules. adamc@75: adam@335: Our new function remains easy to test: *) adamc@75: adamc@73: Eval simpl in typeCheck' (Nat 0). adamc@212: (** %\vspace{-.15in}% [[ adam@335: = [||TNat||] adamc@75: : {t : type | hasType (Nat 0) t} + adamc@75: {(forall t : type, ~ hasType (Nat 0) t)} adam@302: ]] adam@302: *) adamc@75: adamc@73: Eval simpl in typeCheck' (Plus (Nat 1) (Nat 2)). adamc@212: (** %\vspace{-.15in}% [[ adam@335: = [||TNat||] adamc@75: : {t : type | hasType (Plus (Nat 1) (Nat 2)) t} + adamc@75: {(forall t : type, ~ hasType (Plus (Nat 1) (Nat 2)) t)} adam@302: ]] adam@302: *) adamc@75: adamc@73: Eval simpl in typeCheck' (Plus (Nat 1) (Bool false)). adamc@212: (** %\vspace{-.15in}% [[ adamc@75: = !! adamc@75: : {t : type | hasType (Plus (Nat 1) (Bool false)) t} + adamc@75: {(forall t : type, ~ hasType (Plus (Nat 1) (Bool false)) t)} adam@302: ]] adam@335: adam@335: 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. *) adamc@82: adamc@82: adamc@82: (** * Exercises *) adamc@82: adamc@82: (** All of the notations defined in this chapter, plus some extras, are available for import from the module [MoreSpecif] of the book source. adamc@82: adamc@82: %\begin{enumerate}%#
    # adamc@82: %\item%#
  1. # Write a function of type [forall n m : nat, {n <= m} + {n > m}]. That is, this function decides whether one natural is less than another, and its dependent type guarantees that its results are accurate.#
  2. # adamc@82: adamc@82: %\item%#
  3. # %\begin{enumerate}%#
      # adamc@82: %\item%#
    1. # Define [var], a type of propositional variables, as a synonym for [nat].#
    2. # adamc@82: %\item%#
    3. # Define an inductive type [prop] of propositional logic formulas, consisting of variables, negation, and binary conjunction and disjunction.#
    4. # adamc@82: %\item%#
    5. # 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].#
    6. # adamc@82: %\item%#
    7. # 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}]. #
    8. # adam@335: %\item%#
    9. # 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.#
    10. # adam@335: %\item%#
    11. # 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.#
    12. # adamc@82: #
    #%\end{enumerate}% #
  4. # adamc@82: adam@292: %\item%#
  5. # 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: adamc@82: %\begin{center}\url{http://en.wikipedia.org/wiki/DPLL_algorithm}\end{center}% adamc@82: #
    http://en.wikipedia.org/wiki/DPLL_algorithm
    # adamc@82: It might also be instructive to implement the unit propagation and pure literal elimination optimizations described there or some other optimizations that have been used in modern SAT solvers.#
  6. # adamc@82: adamc@82: #
#%\end{enumerate}% *)