Skip to content

Instantly share code, notes, and snippets.

@jtpaasch
Created July 21, 2018 03:19
Show Gist options
  • Select an option

  • Save jtpaasch/ba9c62459ee9850dabdbe6966b9ba9b5 to your computer and use it in GitHub Desktop.

Select an option

Save jtpaasch/ba9c62459ee9850dabdbe6966b9ba9b5 to your computer and use it in GitHub Desktop.
Software Foundations, Logical Foundations, Basics
(** * Basics: Functional Programming in Coq *)
(* REMINDER:
#####################################################
### PLEASE DO NOT DISTRIBUTE SOLUTIONS PUBLICLY ###
#####################################################
(See the [Preface] for why.)
*)
(* ################################################################# *)
(** * Introduction *)
(** The functional programming style is founded on simple, everyday
mathematical intuition: If a procedure or method has no side
effects, then (ignoring efficiency) all we need to understand
about it is how it maps inputs to outputs -- that is, we can think
of it as just a concrete method for computing a mathematical
function. This is one sense of the word "functional" in
"functional programming." The direct connection between programs
and simple mathematical objects supports both formal correctness
proofs and sound informal reasoning about program behavior.
The other sense in which functional programming is "functional" is
that it emphasizes the use of functions (or methods) as
_first-class_ values -- i.e., values that can be passed as
arguments to other functions, returned as results, included in
data structures, etc. The recognition that functions can be
treated as data gives rise to a host of useful and powerful
programming idioms.
Other common features of functional languages include _algebraic
data types_ and _pattern matching_, which make it easy to
construct and manipulate rich data structures, and sophisticated
_polymorphic type systems_ supporting abstraction and code reuse.
Coq offers all of these features.
The first half of this chapter introduces the most essential
elements of Coq's functional programming language, called
_Gallina_. The second half introduces some basic _tactics_ that
can be used to prove properties of Coq programs. *)
(* ################################################################# *)
(** * Data and Functions *)
(* ================================================================= *)
(** ** Enumerated Types *)
(** One notable aspect of Coq is that its set of built-in
features is _extremely_ small. For example, instead of providing
the usual palette of atomic data types (booleans, integers,
strings, etc.), Coq offers a powerful mechanism for defining new
data types from scratch, with all these familiar types as
instances.
Naturally, the Coq distribution comes preloaded with an extensive
standard library providing definitions of booleans, numbers, and
many common data structures like lists and hash tables. But there
is nothing magic or primitive about these library definitions. To
illustrate this, we will explicitly recapitulate all the
definitions we need in this course, rather than just getting them
implicitly from the library. *)
(* ================================================================= *)
(** ** Days of the Week *)
(** To see how this definition mechanism works, let's start with
a very simple example. The following declaration tells Coq that
we are defining a new set of data values -- a _type_. *)
Inductive day : Type :=
| monday : day
| tuesday : day
| wednesday : day
| thursday : day
| friday : day
| saturday : day
| sunday : day.
(** The type is called [day], and its members are [monday],
[tuesday], etc. The second and following lines of the definition
can be read "[monday] is a [day], [tuesday] is a [day], etc."
Having defined [day], we can write functions that operate on
days. *)
Definition next_weekday (d:day) : day :=
match d with
| monday => tuesday
| tuesday => wednesday
| wednesday => thursday
| thursday => friday
| friday => monday
| saturday => monday
| sunday => monday
end.
(** One thing to note is that the argument and return types of
this function are explicitly declared. Like most functional
programming languages, Coq can often figure out these types for
itself when they are not given explicitly -- i.e., it can do _type
inference_ -- but we'll generally include them to make reading
easier. *)
(** Having defined a function, we should check that it works on
some examples. There are actually three different ways to do this
in Coq. First, we can use the command [Compute] to evaluate a
compound expression involving [next_weekday]. *)
Compute (next_weekday friday).
(* ==> monday : day *)
Compute (next_weekday (next_weekday saturday)).
(* ==> tuesday : day *)
(** (We show Coq's responses in comments, but, if you have a
computer handy, this would be an excellent moment to fire up the
Coq interpreter under your favorite IDE -- either CoqIde or Proof
General -- and try this for yourself. Load this file, [Basics.v],
from the book's Coq sources, find the above example, submit it to
Coq, and observe the result.) *)
(** Second, we can record what we _expect_ the result to be in the
form of a Coq example: *)
Example test_next_weekday:
(next_weekday (next_weekday saturday)) = tuesday.
(** This declaration does two things: it makes an
assertion (that the second weekday after [saturday] is [tuesday]),
and it gives the assertion a name that can be used to refer to it
later. Having made the assertion, we can also ask Coq to verify
it, like this: *)
Proof.
simpl. (* [simpl] can reduce the left hand side, by using the constructors. *)
reflexivity. (* [reflexivity] will check that both sides of [=] are the same value. *)
Qed.
(* Informal proof:
Theorem: [next_weekday (next_weekday saturday)) = tuesday].
Proof: by the definition of [next_weekday].
By definition, teh left hand side of the equation evaluates to [tuesday],
which is the same as the right hand side. []
*)
(* Print the proof term. *)
Print test_next_weekday.
(* The proof term is: [eq_refl : next_weekday (next_weekday saturday) = tuesday].
What is this [eq_refl]? *)
Check eq_refl.
(* It prints out [?x = ?x] where [?A] is a [Type] and [?x] is an [?A].
I think this means that [eq_refl] has the form [x : A = x : A],
with holes for the [x]s and [A]s, that can be filled in by actual instances. *)
(* Let's see what it's definition is. *)
Print eq_refl.
(* The name [eq_refl] refers to equality being reflexive.
That is: anything is equal to itself: [x = x].
I think this should be interpreted as follows:
This is an inductive proposition [eq], which takes an [A : Type] and an [x : A],
and returns a function from [A] to [Prop].
It has one constructor, which is [eq_refl].
So if you call [eq_refl] and give it an [A] and an [x], [eq_refl] will give
you back a proposition of the form [x = x]. *)
Check eq_refl : 1 = 1.
(* I think this says: [give eq_refl the proposition [1 = 1], and it will
give you back the proposition [1 = 1]. Maybe that's right? *)
(* Well, let's just assume that it says: the following are reflexivily equal. *)
(** The details are not important for now (we'll come back to
them in a bit), but essentially this can be read as "The assertion
we've just made can be proved by observing that both sides of the
equality evaluate to the same thing, after some simplification."
Third, we can ask Coq to _extract_, from our [Definition], a
program in some other, more conventional, programming
language (OCaml, Scheme, or Haskell) with a high-performance
compiler. This facility is very interesting, since it gives us a
way to go from proved-correct algorithms written in Gallina to
efficient machine code. (Of course, we are trusting the
correctness of the OCaml/Haskell/Scheme compiler, and of Coq's
extraction facility itself, but this is still a big step forward
from the way most software is developed today.) Indeed, this is
one of the main uses for which Coq was developed. We'll come back
to this topic in later chapters. *)
(* ================================================================= *)
(** ** Homework Submission Guidelines *)
(** If you are using Software Foundations in a course, your instructor
may use automatic scripts to help grade your homework assignments.
In order for these scripts to work correctly (so that you get full
credit for your work!), please be careful to follow these rules:
- The grading scripts work by extracting marked regions of the
[.v] files that you submit. It is therefore important that
you do not alter the "markup" that delimits exercises: the
Exercise header, the name of the exercise, the "empty square
bracket" marker at the end, etc. Please leave this markup
exactly as you find it.
- Do not delete exercises. If you skip an exercise (e.g.,
because it is marked Optional, or because you can't solve it),
it is OK to leave a partial proof in your [.v] file, but in
this case please make sure it ends with [Admitted] (not, for
example [Abort]).
- It is fine to use additional definitions (of helper functions,
useful lemmas, etc.) in your solutions. You can put these
between the exercise header and the theorem you are asked to
prove. *)
(* ================================================================= *)
(** ** Booleans *)
(** In a similar way, we can define the standard type [bool] of
booleans, with members [true] and [false]. *)
Inductive bool : Type :=
| true : bool
| false : bool.
(** Although we are rolling our own booleans here for the sake
of building up everything from scratch, Coq does, of course,
provide a default implementation of the booleans, together with a
multitude of useful functions and lemmas. (Take a look at
[Coq.Init.Datatypes] in the Coq library documentation if you're
interested.) Whenever possible, we'll name our own definitions
and theorems so that they exactly coincide with the ones in the
standard library.
Functions over booleans can be defined in the same way as
above: *)
Definition negb (b:bool) : bool :=
match b with
| true => false
| false => true
end.
Definition andb (b1:bool) (b2:bool) : bool :=
match b1 with
| true => b2
| false => false
end.
Definition orb (b1:bool) (b2:bool) : bool :=
match b1 with
| true => true
| false => b2
end.
(** The last two of these illustrate Coq's syntax for
multi-argument function definitions. The corresponding
multi-argument application syntax is illustrated by the following
"unit tests," which constitute a complete specification -- a truth
table -- for the [orb] function: *)
(* These [simpl. reflexivity.] sequences work the same as above.
[simpl] can reduce the expressions on the left using the constructors,
and [reflexivity] can check that both sides of [=] hold the same value. *)
Example test_orb1: (orb true false) = true.
Proof. simpl. reflexivity. Qed. Print test_orb1.
Example test_orb2: (orb false false) = false.
Proof. simpl. reflexivity. Qed. Print test_orb2.
Example test_orb3: (orb false true) = true.
Proof. simpl. reflexivity. Qed. Print test_orb3.
Example test_orb4: (orb true true) = true.
Proof. simpl. reflexivity. Qed. Print test_orb4.
(** We can also introduce some familiar syntax for the boolean
operations we have just defined. The [Notation] command defines a new
symbolic notation for an existing definition. *)
Notation "x && y" := (andb x y).
Notation "x || y" := (orb x y).
Example test_orb5: false || false || true = true.
Proof.
Print "||". (* This is really orb x y. So the equation actually is:
[orb false (orb false true) = true]. *)
simpl. (* Again, [simpl] will reduce by using constructors. *)
reflexivity.
Qed.
(* Informal proof:
Theorem: [false || false || true = true].
Proof: by definition of [||] ([orb]).
The left hand side of the equation is [false || (false || true)].
[(false || true)] evaluates to [true], so that leaves [false || (true)].
That, in turn, evaluates to [true], which is the same as the
right hand side. []
*)
Print test_orb5.
(** _A note on notation_: In [.v] files, we use square brackets
to delimit fragments of Coq code within comments; this convention,
also used by the [coqdoc] documentation tool, keeps them visually
separate from the surrounding text. In the HTML version of the
files, these pieces of text appear in a [different font].
The command [Admitted] can be used as a placeholder for an
incomplete proof. We'll use it in exercises, to indicate the
parts that we're leaving for you -- i.e., your job is to replace
[Admitted]s with real proofs. *)
(** **** Exercise: 1 star (nandb) *)
(** Remove "[Admitted.]" and complete the definition of the following
function; then make sure that the [Example] assertions below can
each be verified by Coq. (Remove "[Admitted.]" and fill in each
proof, following the model of the [orb] tests above.) The function
should return [true] if either or both of its inputs are
[false]. *)
(** Every chapter comes with a [[*Test.v]] file, containing scripts
that check some of the exercises. After you finish an exercise in
this file, you can run [[make BasicsTest.vo]] and check its
output. If you did correctly, it will show "Type: ok", based on no
"Assumptions". *)
Definition nandb (b1:bool) (b2:bool) : bool :=
match b1 with
| false => true (* If [b1] is [false], then one of [b1] or [b2] is [false],
so the whole [nandb b1 b2] is [true]. *)
| true => negb b2 (* If [b1] is true, then we have to move on and check [b2].
The answer will be the opposite value of [b2].
If [b2] is [false], then one of [b1] or [b2] is [false], so the whole
of [nandb b1 b2] is [true].
If [b2] is [true], then neither of [b1] or [b2] is [false], so the whole
of [nandb b1 b2] is [false]. *)
end.
Example test_nandb1: (nandb true false) = true.
Proof.
simpl. (* [simpl] can reduce the left hand side by using the constructors. *)
reflexivity.
Qed.
Example test_nandb2: (nandb false false) = true.
Proof. simpl. reflexivity. Qed.
Example test_nandb3: (nandb false true) = true.
Proof. simpl. reflexivity. Qed.
Example test_nandb4: (nandb true true) = false.
Proof. simpl. reflexivity. Qed.
(** [] *)
(** **** Exercise: 1 star (andb3) *)
(** Do the same for the [andb3] function below. This function should
return [true] when all of its inputs are [true], and [false]
otherwise. *)
Definition andb3 (b1:bool) (b2:bool) (b3:bool) : bool :=
match b1 with
| false => false (* If [b1] is false, then we know the whole of [andb3 b1 b2 b3] is [false],
because all three must be [true] for the whole thing to be [true]. *)
| true => andb b2 b3 (* If [b1] is true, then we can check [andb b2 b3]. That will be [true]
only if both [b2] and [b3] are [true]. *)
end.
Example test_andb31: (andb3 true true true) = true.
Proof. simpl. reflexivity. Qed.
Example test_andb32: (andb3 false true true) = false.
Proof. simpl. reflexivity. Qed.
Example test_andb33: (andb3 true false true) = false.
Proof. simpl. reflexivity. Qed.
Example test_andb34: (andb3 true true false) = false.
Proof. simpl. reflexivity. Qed.
(** [] *)
(* ================================================================= *)
(** ** Function Types *)
(** Every expression in Coq has a type, describing what sort of
thing it computes. The [Check] command asks Coq to print the type
of an expression. *)
Check true.
(* ===> true : bool *)
Check (negb true).
(* ===> negb true : bool *)
(** Functions like [negb] itself are also data values, just like
[true] and [false]. Their types are called _function types_, and
they are written with arrows. *)
Check negb.
(* ===> negb : bool -> bool *)
(** The type of [negb], written [bool -> bool] and pronounced
"[bool] arrow [bool]," can be read, "Given an input of type
[bool], this function produces an output of type [bool]."
Similarly, the type of [andb], written [bool -> bool -> bool], can
be read, "Given two inputs, both of type [bool], this function
produces an output of type [bool]." *)
(* ================================================================= *)
(** ** Compound Types *)
(** The types we have defined so far are examples of "enumerated
types": their definitions explicitly enumerate a finite set of
elements, each of which is just a bare constructor. Here is a
more interesting type definition, where one of the constructors
takes an argument: *)
Inductive rgb : Type :=
| red : rgb
| green : rgb
| blue : rgb.
Inductive color : Type :=
| black : color
| white : color
| primary : rgb -> color.
(** Let's look at this in a little more detail.
Every inductively defined type ([day], [bool], [rgb], [color],
etc.) contains a set of _constructor expressions_ built from
_constructors_ like [red], [primary], [true], [false], [monday],
etc. The definitions of [rgb] and [color] say how expressions in
the sets [rgb] and [color] can be built:
- [red], [green], and [blue] are the constructors of [rgb];
- [black], [white], and [primary] are the constructors of [color];
- the expression [red] belongs to the set [rgb], as do the
expressions [green] and [blue];
- the expressions [black] and [white] belong to the set [color];
- if [p] is an expression belonging to the set [rgb], then
[primary p] (pronounced "the constructor [primary] applied to
the argument [p]") is an expression belonging to the set
[color]; and
- expressions formed in these ways are the _only_ ones belonging
to the sets [rgb] and [color]. *)
(** We can define functions on colors using pattern matching just as
we have done for [day] and [bool]. *)
Definition monochrome (c : color) : bool :=
match c with
| black => true
| white => true
| primary p => false
end.
(** Since the [primary] constructor takes an argument, a pattern
matching [primary] should include either a variable (as above) or
a constant of appropriate type (as below). *)
Definition isred (c : color) : bool :=
match c with
| black => false
| white => false
| primary red => true
| primary _ => false
end.
(** The pattern [primary _] here is shorthand for "[primary] applied
to any [rgb] constructor except [red]." (The wildcard pattern [_]
has the same effect as the dummy pattern variable [p] in the
definition of [monochrome].) *)
(* ================================================================= *)
(** ** Modules *)
(** Coq provides a _module system_, to aid in organizing large
developments. In this course we won't need most of its features,
but one is useful: If we enclose a collection of declarations
between [Module X] and [End X] markers, then, in the remainder of
the file after the [End], these definitions are referred to by
names like [X.foo] instead of just [foo]. We will use this
feature to introduce the definition of the type [nat] in an inner
module so that it does not interfere with the one from the
standard library (which we want to use in the rest because it
comes with a tiny bit of convenient special notation). *)
Module NatPlayground.
(* ================================================================= *)
(** ** Numbers *)
(** An even more interesting way of defining a type is to allow its
constructors to take arguments from the very same type -- that is,
to allow the rules describing its elements to be _inductive_.
For example, we can define (a unary representation of) natural
numbers as follows: *)
Inductive nat : Type :=
| O : nat
| S : nat -> nat.
(** The clauses of this definition can be read:
- [O] is a natural number (note that this is the letter "[O],"
not the numeral "[0]").
- [S] can be put in front of a natural number to yield another
one -- if [n] is a natural number, then [S n] is too. *)
(** Again, let's look at this in a little more detail. The definition
of [nat] says how expressions in the set [nat] can be built:
- [O] and [S] are constructors;
- the expression [O] belongs to the set [nat];
- if [n] is an expression belonging to the set [nat], then [S n]
is also an expression belonging to the set [nat]; and
- expressions formed in these two ways are the only ones belonging
to the set [nat]. *)
(** The same rules apply for our definitions of [day], [bool],
[color], etc.
The above conditions are the precise force of the [Inductive]
declaration. They imply that the expression [O], the expression
[S O], the expression [S (S O)], the expression [S (S (S O))], and
so on all belong to the set [nat], while other expressions built
from data constructors, like [true], [andb true false], [S (S
false)], and [O (O (O S))] do not.
A critical point here is that what we've done so far is just to
define a _representation_ of numbers: a way of writing them down.
The names [O] and [S] are arbitrary, and at this point they have
no special meaning -- they are just two different marks that we
can use to write down numbers (together with a rule that says any
[nat] will be written as some string of [S] marks followed by an
[O]). If we like, we can write essentially the same definition
this way: *)
Inductive nat' : Type :=
| stop : nat'
| tick : nat' -> nat'.
(** The _interpretation_ of these marks comes from how we use them to
compute. *)
(** We can do this by writing functions that pattern match on
representations of natural numbers just as we did above with
booleans and days -- for example, here is the predecessor
function: *)
Definition pred (n : nat) : nat :=
match n with
| O => O
| S n' => n'
end.
(** The second branch can be read: "if [n] has the form [S n']
for some [n'], then return [n']." *)
End NatPlayground.
(** Because natural numbers are such a pervasive form of data,
Coq provides a tiny bit of built-in magic for parsing and printing
them: ordinary arabic numerals can be used as an alternative to
the "unary" notation defined by the constructors [S] and [O]. Coq
prints numbers in arabic form by default: *)
Check (S (S (S (S O)))).
(* ===> 4 : nat *)
Definition minustwo (n : nat) : nat :=
match n with
| O => O (* 0 - 2 = 0 in nats. *)
| S O => O (* 1 - 2 = 0 in nats. *)
| S (S n') => n' (* Any n + 2 - 2 = n. *)
end.
Compute (minustwo 4).
(* ===> 2 : nat *)
(** The constructor [S] has the type [nat -> nat], just like
[pred] and functions like [minustwo]: *)
Check S.
Check pred.
Check minustwo.
(** These are all things that can be applied to a number to yield a
number. However, there is a fundamental difference between the
first one and the other two: functions like [pred] and [minustwo]
come with _computation rules_ -- e.g., the definition of [pred]
says that [pred 2] can be simplified to [1] -- while the
definition of [S] has no such behavior attached. Although it is
like a function in the sense that it can be applied to an
argument, it does not _do_ anything at all! It is just a way of
writing down numbers. (Think about standard arabic numerals: the
numeral [1] is not a computation; it's a piece of data. When we
write [111] to mean the number one hundred and eleven, we are
using [1], three times, to write down a concrete representation of
a number.)
For most function definitions over numbers, just pattern matching
is not enough: we also need recursion. For example, to check that
a number [n] is even, we may need to recursively check whether
[n-2] is even. To write such functions, we use the keyword
[Fixpoint]. *)
Fixpoint evenb (n:nat) : bool :=
match n with
| O => true (* 0 is even in nats. *)
| S O => false (* 1 is not even in nats. *)
| S (S n') => evenb n' (* Any n + 2 is even if n is even. *)
end.
(** We can define [oddb] by a similar [Fixpoint] declaration, but here
is a simpler definition: *)
Definition oddb (n:nat) : bool := negb (evenb n). (* Just find if it's even, then negate that. *)
Example test_oddb1: oddb 1 = true.
Proof.
simpl. (* Note that [simpl] does nothing here.
Why? An explanation is promised below. *)
reflexivity. (* But [reflexivity] solves this. *)
Qed.
Example test_oddb2: oddb 4 = false.
Proof. simpl. reflexivity. Qed.
(** (You will notice if you step through these proofs that
[simpl] actually has no effect on the goal -- all of the work is
done by [reflexivity]. We'll see more about why that is shortly.)
Naturally, we can also define multi-argument functions by
recursion. *)
Module NatPlayground2.
Fixpoint plus (n : nat) (m : nat) : nat :=
match n with
| O => m (* 0 + m = m in nats. *)
| S n' => S (plus n' m) (* Any n + m is the same as 1 + ((n - 1) + m)). *)
end.
(** Adding three to two now gives us five, as we'd expect. *)
Compute (plus 3 2).
(** The simplification that Coq performs to reach this conclusion can
be visualized as follows: *)
(* [plus (S (S (S O))) (S (S O))]
==> [S (plus (S (S O)) (S (S O)))]
by the second clause of the [match]
==> [S (S (plus (S O) (S (S O))))]
by the second clause of the [match]
==> [S (S (S (plus O (S (S O)))))]
by the second clause of the [match]
==> [S (S (S (S (S O))))]
by the first clause of the [match]
*)
(** As a notational convenience, if two or more arguments have
the same type, they can be written together. In the following
definition, [(n m : nat)] means just the same as if we had written
[(n : nat) (m : nat)]. *)
Fixpoint mult (n m : nat) : nat :=
match n with
| O => O (* 0 * m = 0 in nats. *)
| S n' => plus m (mult n' m) (* Any n * m is the same as m + ((n - 1) * m)). *)
end.
Example test_mult1: (mult 3 3) = 9.
(*
mult 3 3 ==>
mult (S S S O) (S S S O) ==>
plus (S S S O) (mult (S S O) (S S S O)) ==>
plus (S S S O) (plus (S S S O) (mult (S O) (S S S O)) ===>
plus (S S S O) (plus (S S S O) (plus (O (S S S O)))
*)
Proof.
simpl. (* In this case, [simpl] can reduce the left hand side to 9.
That's: S S S S S S S S S O. *)
reflexivity. (* Both sides of [=] have the same value. *)
Qed.
(** You can match two expressions at once by putting a comma
between them: *)
Fixpoint minus (n m:nat) : nat :=
match n, m with
| O , _ => O (* 0 - m = 0 in nats. *)
| S _ , O => n (* n - 0 = n in nats. *)
| S n', S m' => minus n' m' (* n - m is the same as (n - 1) - (m - 1). *)
end.
Example test_minus1 : (minus 4 2) = 2.
(*
minus 4 3 ==>
minus (S S S S O) (S S O) ==>
minus (S S S O) (S O) ==>
minus (S S O) (O) ==>
match S S O ==> S O O ==> 2
*)
Proof.
simpl. (* [simpl] can reduce this. *)
reflexivity.
Qed.
(** Again, the [_] in the first line is a _wildcard pattern_. Writing
[_] in a pattern is the same as writing some variable that doesn't
get used on the right-hand side. This avoids the need to invent a
variable name. *)
End NatPlayground2.
Fixpoint exp (base power : nat) : nat :=
match power with
| O => S O (* Anything to the power of 0 is 1 in nats. *)
| S p => mult base (exp base p) (* b^n is mult b (exp b (n - 1)). *)
end.
(** **** Exercise: 1 star (factorial) *)
(** Recall the standard mathematical factorial function:
factorial(0) = 1
factorial(n) = n * factorial(n-1) (if n>0)
Translate this into Coq. *)
Fixpoint factorial (n:nat) : nat :=
match n with
| O => S O
| S n' => mult n (factorial n')
end.
Example test_factorial1: (factorial 3) = 6.
Proof.
simpl. (* [simpl] can reduce the left hand side to 6. *)
reflexivity.
Qed.
Example test_factorial2: (factorial 5) = (mult 10 12).
Proof. simpl. reflexivity. Qed.
(** [] *)
(** We can make numerical expressions a little easier to read and
write by introducing _notations_ for addition, multiplication, and
subtraction. *)
Notation "x + y" := (plus x y)
(at level 50, left associativity)
: nat_scope.
Notation "x - y" := (minus x y)
(at level 50, left associativity)
: nat_scope.
Notation "x * y" := (mult x y)
(at level 40, left associativity)
: nat_scope.
Check ((0 + 1) + 1).
(** (The [level], [associativity], and [nat_scope] annotations
control how these notations are treated by Coq's parser. The
details are not important for our purposes, but interested readers
can refer to the optional "More on Notation" section at the end of
this chapter.)
Note that these do not change the definitions we've already made:
they are simply instructions to the Coq parser to accept [x + y]
in place of [plus x y] and, conversely, to the Coq pretty-printer
to display [plus x y] as [x + y]. *)
(** When we say that Coq comes with almost nothing built-in, we really
mean it: even equality testing for numbers is a user-defined
operation! We now define a function [beq_nat], which tests
[nat]ural numbers for [eq]uality, yielding a [b]oolean. Note the
use of nested [match]es (we could also have used a simultaneous
match, as we did in [minus].) *)
Fixpoint beq_nat (n m : nat) : bool :=
match n with
| O => match m with (* The case where [n] is [O]. *)
| O => true (* If [m] is also [O], then n = m. So return [true]. *)
| S m' => false (* If [m] is not [O] (i.e., if it has the pattern [S _]), then n != m. *)
end
| S n' => match m with (* The case where [n] has the pattern [S n']. *)
| O => false (* If [m] is [O], then n != m, since [n] is some successor of [O]. *)
| S m' => beq_nat n' m' (* If [m] is a successor of some [m'], then check if [n' = m']. *)
end
end.
(** The [leb] function tests whether its first argument is less than or
equal to its second argument, yielding a boolean. *)
Fixpoint leb (n m : nat) : bool :=
match n with
| O => true (* If [n] is [O], then n is the smallest it could be, so [n <= m] in nats. *)
| S n' => (* If [n] is a successor of some [n'], then: *)
match m with
| O => false (* If [m] is [O], then [n] is not smaller. *)
| S m' => leb n' m' (* If [m] is a successor of some [m'], check if [n' <= m']. *)
end
end.
Example test_leb1: (leb 2 2) = true.
Proof.
simpl. (* [simpl] can reduce the left-hand-side. *)
reflexivity.
Qed.
Example test_leb2: (leb 2 4) = true.
Proof. simpl. reflexivity. Qed.
Example test_leb3: (leb 4 2) = false.
Proof. simpl. reflexivity. Qed.
(** **** Exercise: 1 star (blt_nat) *)
(** The [blt_nat] function tests [nat]ural numbers for [l]ess-[t]han,
yielding a [b]oolean. Instead of making up a new [Fixpoint] for
this one, define it in terms of a previously defined function. *)
Definition blt_nat (n m : nat) : bool :=
match leb n m with (* Check if [n <= m]. *)
| false => false (* If [n] is not [<=] to [m], then it's strictly greater. So [n !< m]. *)
| true => match beq_nat n m with (* But if [n] is [<=] to [m], we need to check if [n = m]. *)
| true => false (* If [n = m], then [n] is not less than [m]. *)
| false => true (* If [n] is not the same as [m], then it must be less than [m]. *)
end
end.
Example test_blt_nat1: (blt_nat 2 2) = false.
Proof.
simpl. (* [simpl] cannot reduce the left hand side. *)
reflexivity. (* But this can. Why? *)
Qed.
Example test_blt_nat2: (blt_nat 2 4) = true.
Proof. simpl. reflexivity. Qed.
Example test_blt_nat3: (blt_nat 4 2) = false.
Proof. simpl. reflexivity. Qed.
(* Print the proof object: *)
Print test_blt_nat3.
(* It says:
[eq_refl : blt_nat 4 2 = false].
I think this means that the [eq_refl] function will return
a proof of [blt_nat 4 2 = false]. *)
(** [] *)
(* ################################################################# *)
(** * Proof by Simplification *)
(** Now that we've defined a few datatypes and functions, let's
turn to stating and proving properties of their behavior.
Actually, we've already started doing this: each [Example] in the
previous sections makes a precise claim about the behavior of some
function on some particular inputs. The proofs of these claims
were always the same: use [simpl] to simplify both sides of the
equation, then use [reflexivity] to check that both sides contain
identical values.
The same sort of "proof by simplification" can be used to prove
more interesting properties as well. For example, the fact that
[0] is a "neutral element" for [+] on the left can be proved just
by observing that [0 + n] reduces to [n] no matter what [n] is, a
fact that can be read directly off the definition of [plus].*)
Theorem plus_O_n : forall n : nat, 0 + n = n.
Proof.
intros n. (* Take [n] out of the [forall] and pick some arbitrary value, call it [n]. *)
simpl. (* [simpl] can read that [O + n] in the definition of [plus] returns [n]. So it
doesn't have to do any more computation. It can just reduce the left hand side to [n]. *)
reflexivity.
Qed.
(* Informal proof:
Theorem: [forall n : nat, 0 + n = n].
Proof. By definition of [+].
- Suppose [n] is a fixed number.
- By the definition of [+], [0 + n] is [n], which is the
same as the right hand side of the equation.
- Since we've proved this for an arbitrary [n],
it holds for all [n]. []
*)
(** (You may notice that the above statement looks different in
the [.v] file in your IDE than it does in the HTML rendition in
your browser, if you are viewing both. In [.v] files, we write the
[forall] universal quantifier using the reserved identifier
"forall." When the [.v] files are converted to HTML, this gets
transformed into an upside-down-A symbol.)
This is a good place to mention that [reflexivity] is a bit
more powerful than we have admitted. In the examples we have seen,
the calls to [simpl] were actually not needed, because
[reflexivity] can perform some simplification automatically when
checking that two sides are equal; [simpl] was just added so that
we could see the intermediate state -- after simplification but
before finishing the proof. Here is a shorter proof of the
theorem: *)
Theorem plus_O_n' : forall n : nat, 0 + n = n.
Proof.
intros n. reflexivity. Qed.
(** Moreover, it will be useful later to know that [reflexivity]
does somewhat _more_ simplification than [simpl] does -- for
example, it tries "unfolding" defined terms, replacing them with
their right-hand sides. The reason for this difference is that,
if reflexivity succeeds, the whole goal is finished and we don't
need to look at whatever expanded expressions [reflexivity] has
created by all this simplification and unfolding; by contrast,
[simpl] is used in situations where we may have to read and
understand the new goal that it creates, so we would not want it
blindly expanding definitions and leaving the goal in a messy
state.
The form of the theorem we just stated and its proof are almost
exactly the same as the simpler examples we saw earlier; there are
just a few differences.
First, we've used the keyword [Theorem] instead of [Example].
This difference is mostly a matter of style; the keywords
[Example] and [Theorem] (and a few others, including [Lemma],
[Fact], and [Remark]) mean pretty much the same thing to Coq.
Second, we've added the quantifier [forall n:nat], so that our
theorem talks about _all_ natural numbers [n]. Informally, to
prove theorems of this form, we generally start by saying "Suppose
[n] is some number..." Formally, this is achieved in the proof by
[intros n], which moves [n] from the quantifier in the goal to a
_context_ of current assumptions.
The keywords [intros], [simpl], and [reflexivity] are examples of
_tactics_. A tactic is a command that is used between [Proof] and
[Qed] to guide the process of checking some claim we are making.
We will see several more tactics in the rest of this chapter and
yet more in future chapters. *)
(* Print the proof term. *)
Print plus_O_n'.
(* This prints out a function:
[fun n : nat => eq_refl : forall n : nat, 0 + n = n].
I think this means the following:
if you give me a nat, I will give you a proof that
forall n : nat, 0 + n = n.
I'm not sure why the [forall] is quantified in there. *)
(** Other similar theorems can be proved with the same pattern. *)
Theorem plus_1_l : forall n:nat, 1 + n = S n.
Proof.
intros n. (* Suppose [n] is a fixed number. *)
simpl. (* By definition of [+], [1 + n] is [S n]. *)
reflexivity. (* That's the same as the right side of the equation. *)
Qed.
Theorem mult_0_l : forall n:nat, 0 * n = 0.
Proof.
intros n. simpl. reflexivity. Qed.
(** The [_l] suffix in the names of these theorems is
pronounced "on the left." *)
(** It is worth stepping through these proofs to observe how the
context and the goal change. You may want to add calls to [simpl]
before [reflexivity] to see the simplifications that Coq performs
on the terms before checking that they are equal. *)
(* ################################################################# *)
(** * Proof by Rewriting *)
(** This theorem is a bit more interesting than the others we've
seen: *)
Theorem plus_id_example : forall n m:nat,
n = m ->
n + n = m + m.
(** Instead of making a universal claim about all numbers [n] and [m],
it talks about a more specialized property that only holds when [n
= m]. The arrow symbol is pronounced "implies."
As before, we need to be able to reason by assuming we are given such
numbers [n] and [m]. We also need to assume the hypothesis
[n = m]. The [intros] tactic will serve to move all three of these
from the goal into assumptions in the current context.
Since [n] and [m] are arbitrary numbers, we can't just use
simplification to prove this theorem. Instead, we prove it by
observing that, if we are assuming [n = m], then we can replace
[n] with [m] in the goal statement and obtain an equality with the
same expression on both sides. The tactic that tells Coq to
perform this replacement is called [rewrite]. *)
Proof.
(* move both quantifiers into the context: *)
intros n m. (* Suppose that [n] and [m] are some fixed numbers. So declare [n, m : nat]
as hypotheses in the proof context.
Now your goal is: if you can prove the goal about these fixed number,
then you've proven it about the universal case, i.e., where [forall n m : nat].
This is because [n] and [m] are arbitrary. *)
(* move the hypothesis into the context: *)
intros H. (* Assume the antecedent. So put [H : n = m] into the proof context.
Now the goal is the consequent [n + n = m + m].
If you can show that the goal follows from [H], then you've shown [H -> goal].
In other words, you've established (and hence introduced) [H -> goal]. *)
(* rewrite the goal using the hypothesis: *)
rewrite -> H. (* Replace every occurrence of [n] with [m]. *)
reflexivity. Qed.
(* Informal proof:
Theorem: [forall n m : nat, (n = m) -> ((n + n) = (m + m))].
Proof: by rewriting [n] with [m].
- Let [A] be the hypothesis [n = m], and
let [B] be the consequent [(n + n) = (m + m)].
- Suppose [A] is true. We must show that [B] follows.
- Since [n = m] (in [A]), we can replace every occurrence of
[n] in [B] with [m].
- That yields: [(m + m) = (m + m)], which is true by the
reflexivity of equality.
- Therefore, [A -> B] holds, since [B] follows from [A]. []
*)
(** The first line of the proof moves the universally quantified
variables [n] and [m] into the context. The second moves the
hypothesis [n = m] into the context and gives it the name [H].
The third tells Coq to rewrite the current goal ([n + n = m + m])
by replacing the left side of the equality hypothesis [H] with the
right side.
(The arrow symbol in the [rewrite] has nothing to do with
implication: it tells Coq to apply the rewrite from left to right.
To rewrite from right to left, you can use [rewrite <-]. Try
making this change in the above proof and see what difference it
makes.) *)
(** **** Exercise: 1 star (plus_id_exercise) *)
(** Remove "[Admitted.]" and fill in the proof. *)
Theorem plus_id_exercise : forall n m o : nat,
n = m -> m = o -> n + m = m + o.
Proof.
intros n m o. (* Suppose [n], [m], and [o] are some fixed numbers.
Let's try to prove the goal for these numbers.
If we prove it holds for these arbitrary numbers,
then we've proved it for the universal case. *)
intros H1. (* Assume the antecedent as a hypothesis [n = m]. Call it [H1].
Our goal is now to prove the consequent [m = 0 -> n + m = m + o].
If we can show that the consequent follows by assuming [H1],
then we've shown that the implication [H1 -> goal] holds. *)
intros H2. (* Now assume the next antecedent [m = o]. Call it [H2]. Again, the goal
now is to show the new antecedent [n + m = m + o]. If we can show
that this follows from the assumed hypothesis [m = o], then we've shown
that [(m = o) -> (n + m = m + o)]. *)
rewrite -> H1. (* Replace every occurrence of [n] with [m], since by hypothesis [H1], [n = m]. *)
rewrite <- H2. (* Replace every occurrence of [o] with [m], via hypothesis [H2]. *)
reflexivity. (* Now the goal says m + m = m + m. We can use [reflexivity] to complete this. *)
(* After [reflexivity] runs, Coq does these three things:
- it completes the proof of [H2 -> goal]. That is, it says: "you've shown that
[m = o -> n + m = m + o] (under the assumption that [n = m])".
- Then it completes the proof of [n = m -> (m = o -> n + m = m + o)]. That is, it says,
"you've shown that if [n = m], then [m = o -> n + m = m + o]."
- Finally, it completes the proof of [forall n m o : nat]. That is, it says:
"you've shown that [n = m -> m = o -> n + m = m + o] for the arbitrary numbers
[n], [m], and [o], so you've shown that it holds for all [n], [m], and [o]." *)
Qed.
(** [] *)
(* Informal proof:
Theorem:
[forall n m o : nat, (n = m) -> ((m = o) -> ((n + m) = (m + o)))].
Proof: by rewriting [n] with [m], and [o] with [m].
- Let [A] be [n = m], let [B] be [m = o] , and
let [C] be [(n + m) = (m + o)].
- Suppose [A] is true.
We must show that [B -> C] follows.
- Suppose [B] is true.
We must show that [C] follows.
- Since [n = m] (in [A]), we can rewrite
every occurrence of [n] in [C] with [m].
- Since [m = o] (in [A]), we can rewrite
every occurrence of [o] in [C] with [m].
- That yields [(m + m) = (m + m)], which is true
by the reflexivity of equality.
- So [B -> C] holds, since [C] follows from [B].
- So [A -> (B -> C)] holds too, since [B -> C] follows from [A]. []
*)
(* Print the proof term. *)
Print plus_id_exercise.
(* This declares the types up front,
but the meat of the proof is this:
eq_ind_r (fun n0 : nat => n0 + m = m + o)
(eq_ind m (fun oO : nat => m + m = m + o0)
eq_refl o H2) H1. *)
(** The [Admitted] command tells Coq that we want to skip trying
to prove this theorem and just accept it as a given. This can be
useful for developing longer proofs, since we can state subsidiary
lemmas that we believe will be useful for making some larger
argument, use [Admitted] to accept them on faith for the moment,
and continue working on the main argument until we are sure it
makes sense; then we can go back and fill in the proofs we
skipped. Be careful, though: every time you say [Admitted] you
are leaving a door open for total nonsense to enter Coq's nice,
rigorous, formally checked world! *)
(** We can also use the [rewrite] tactic with a previously proved
theorem instead of a hypothesis from the context. If the statement
of the previously proved theorem involves quantified variables,
as in the example below, Coq tries to instantiate them
by matching with the current goal. *)
Theorem mult_0_plus : forall n m : nat,
(0 + n) * m = n * m.
Proof.
intros n m.
Print plus_O_n. (* [forall n : nat, 0 + n = n] *)
rewrite -> plus_O_n. (* Replace every occurrence of [0 + n] with [n]. *)
reflexivity. Qed.
(** **** Exercise: 2 stars (mult_S_1) *)
Theorem mult_S_1 : forall n m : nat,
m = S n ->
m * (1 + n) = m * m.
Proof.
intros n m. (* Suppose [n] and [m] are fixed numbers. If we can show this for
an arbitrary [n] and [m], then we'll have shown that it's true for all [n] [m]. *)
intro H. (* Suppose the antecedent [m = S n] is true. Call it [H]. *)
rewrite -> H. (* Replace every occurrence of [m] with [S n]. *)
simpl. (* Unfold the multiplication symbols into the plus variants.
Somehow [simpl] can do this. *)
reflexivity.
Qed.
(* (N.b. This proof can actually be completed with tactics other than
[rewrite], but please do use [rewrite] for the sake of the exercise.) *)
(** [] *)
(* Look at the proof term. *)
Print mult_S_1.
(* Again, lots of [eq_*] sorts of terms. *)
(* ################################################################# *)
(** * Proof by Case Analysis *)
(** Of course, not everything can be proved by simple
calculation and rewriting: In general, unknown, hypothetical
values (arbitrary numbers, booleans, lists, etc.) can block
simplification. For example, if we try to prove the following
fact using the [simpl] tactic as above, we get stuck. (We then
use the [Abort] command to give up on it for the moment.)*)
Theorem plus_1_neq_0_firsttry : forall n : nat,
beq_nat (n + 1) 0 = false.
Proof.
intros n.
simpl. (* does nothing! *)
Abort.
(** The reason for this is that the definitions of both
[beq_nat] and [+] begin by performing a [match] on their first
argument. But here, the first argument to [+] is the unknown
number [n] and the argument to [beq_nat] is the compound
expression [n + 1]; neither can be simplified.
To make progress, we need to consider the possible forms of [n]
separately. If [n] is [O], then we can calculate the final result
of [beq_nat (n + 1) 0] and check that it is, indeed, [false]. And
if [n = S n'] for some [n'], then, although we don't know exactly
what number [n + 1] yields, we can calculate that, at least, it
will begin with one [S], and this is enough to calculate that,
again, [beq_nat (n + 1) 0] will yield [false].
The tactic that tells Coq to consider, separately, the cases where
[n = O] and where [n = S n'] is called [destruct]. *)
Theorem plus_1_neq_0 : forall n : nat,
beq_nat (n + 1) 0 = false.
Proof.
intros n. (* Suppose that [n] is a fixed number. *)
destruct n as [| n']. (* [n] can be [0] or [S n']. This removes the current goal
and generates two new ones, one for each case. *)
- (* Case: [n = 0]. *)
reflexivity. (* [beq_nat (0 + 1) 0] is [false], which matches the right hand side. *)
- (* Case: [n = S n']. *)
reflexivity. (* [beq_nat ((S n') + 1) 0] is [false] too. *)
Qed.
(* Informal proof:
Theorem: [forall n : nat, beq_nat (n + 1) 0 = false].
Proof: by case analysis on [n].
- Let [n] be a fixed number.
- First, suppose [n] is [0]. We must show that [beq_nat (0 + 1) 0 = false],
which follows from the definition of [+] and [beq_nat].
- Second, suppose [n] is a succesor [S] of some [n'].
We must show that [beq_nat ((S n') + 1) 0 = false],
which follows from the definition of [+], [S], and [beq_nat].
- So the theorem holds for all cases. []
*)
(** The [destruct] generates _two_ subgoals, which we must then
prove, separately, in order to get Coq to accept the theorem. The
annotation "[as [| n']]" is called an _intro pattern_. It tells
Coq what variable names to introduce in each subgoal. In general,
what goes between the square brackets is a _list of lists_ of
names, separated by [|]. In this case, the first component is
empty, since the [O] constructor is nullary (it doesn't have any
arguments). The second component gives a single name, [n'], since
[S] is a unary constructor.
The [-] signs on the second and third lines are called _bullets_,
and they mark the parts of the proof that correspond to each
generated subgoal. The proof script that comes after a bullet is
the entire proof for a subgoal. In this example, each of the
subgoals is easily proved by a single use of [reflexivity], which
itself performs some simplification -- e.g., the first one
simplifies [beq_nat (S n' + 1) 0] to [false] by first rewriting
[(S n' + 1)] to [S (n' + 1)], then unfolding [beq_nat], and then
simplifying the [match].
Marking cases with bullets is entirely optional: if bullets are
not present, Coq simply asks you to prove each subgoal in
sequence, one at a time. But it is a good idea to use bullets.
For one thing, they make the structure of a proof apparent, making
it more readable. Also, bullets instruct Coq to ensure that a
subgoal is complete before trying to verify the next one,
preventing proofs for different subgoals from getting mixed
up. These issues become especially important in large
developments, where fragile proofs lead to long debugging
sessions.
There are no hard and fast rules for how proofs should be
formatted in Coq -- in particular, where lines should be broken
and how sections of the proof should be indented to indicate their
nested structure. However, if the places where multiple subgoals
are generated are marked with explicit bullets at the beginning of
lines, then the proof will be readable almost no matter what
choices are made about other aspects of layout.
This is also a good place to mention one other piece of somewhat
obvious advice about line lengths. Beginning Coq users sometimes
tend to the extremes, either writing each tactic on its own line
or writing entire proofs on one line. Good style lies somewhere
in the middle. One reasonable convention is to limit yourself to
80-character lines.
The [destruct] tactic can be used with any inductively defined
datatype. For example, we use it next to prove that boolean
negation is involutive -- i.e., that negation is its own
inverse. *)
Theorem negb_involutive : forall b : bool,
negb (negb b) = b.
Proof.
intros b. (* Suppose [b] is a fixed boolean value. *)
destruct b. (* Let's show that the theorem holds for all cases of [b]. *)
- (* Case: b = true. *)
reflexivity. (* [negb (negb b)] flips the [bool] around, then back,
in which case the value of [b] is still the same. *)
- (* Case: b = false. *)
reflexivity. (* ditto. *)
Qed.
(* Informal proof:
Theorem: [forall b : bool, negb (negb b) = b].
Proof: by case analysis on [b].
- Let [b] be a fixed boolean value.
- First, suppose [b] is true. We need to show that
[negb (negb true) = true], which follows since
reversing the value of [true] twice is still [true].
- Second, suppose [b] is false. we need to show that
[negb (negb false) = false], which follows since
reversing the value of [false] twice is still [false].
- So the theorem holds in all cases. []
*)
(** Note that the [destruct] here has no [as] clause because
none of the subcases of the [destruct] need to bind any variables,
so there is no need to specify any names. (We could also have
written [as [|]], or [as []].) In fact, we can omit the [as]
clause from _any_ [destruct] and Coq will fill in variable names
automatically. This is generally considered bad style, since Coq
often makes confusing choices of names when left to its own
devices.
It is sometimes useful to invoke [destruct] inside a subgoal,
generating yet more proof obligations. In this case, we use
different kinds of bullets to mark goals on different "levels."
For example: *)
Theorem andb_commutative : forall b c, andb b c = andb c b.
Proof.
intros b c. (* Suppose [b] and [c] are fixed boolean values. *)
destruct b. (* Let's consider both cases of [b]: when it's [true], or [false]. *)
- (* Case: b = true. *)
(* We need to show that [andb true c = andb c true]. *)
destruct c. (* Let's consider both cases of [c]: when it's [true], or [false]. *)
+ (* Case: c = true. *)
(* We need to show that [andb true true = andb true true]. *)
reflexivity. (* Both sides of the equation are the same, so it holds. *)
+ (* Case: c = false. *)
(* We need to show that [andb true false = andb false true]. *)
reflexivity. (* Both sides of the equation evaluate to [false], so they're the same. *)
- (* Case: b = false. *)
(* We need to show that [andb false c = andb c false]. *)
destruct c. (* Let's consider both cases of [c]: when it's [true], or [false]. *)
+ (* Case: c = true. *)
(* We need to show that [andb false true = andb true false]. *)
reflexivity. (* Both sides of the equation evaluate to [false], so they're the same. *)
+ (* Case: c = false. *)
(* We need to show that [andb false false = andb false false]. *)
reflexivity. (* Both sides of the equation are the same. *)
Qed.
(* Informal proof:
Theorem: [forall b c, andb b c = andb c b].
Proof: by case analysis on [b] and [c].
- Let [b] and [c] be fixed [bool] values.
- First, suppose [b] is [true]. We need to show [andb true c = andb c true].
- We proceed by case analysis on [c].
- Suppose [c] is [true]. Then [andb true true = andb true true], because
both sides of the equation are the same.
- Suppose [c] is [false]. Then [andb true false = andb false true],
which follows because both sides of the equation evaluate to [false].
- Second, suppose [b] is [false]. We need to show [andb false c = andb c false].
- We proceed by case analysis on [c].
- Suppose [c] is [true]. Then [andb false true = andb true false], which
follows because both sides of the equation evaluate to [false].
- Suppose [c] is [false]. Then [andb false false = andb false false],
which holds because both sides of the equation are the same.
- So the theorem holds for all cases. []
*)
(** Each pair of calls to [reflexivity] corresponds to the
subgoals that were generated after the execution of the [destruct c]
line right above it. *)
(** Besides [-] and [+], we can use [*] (asterisk) as a third kind of
bullet. We can also enclose sub-proofs in curly braces, which is
useful in case we ever encounter a proof that generates more than
three levels of subgoals: *)
Theorem andb_commutative' : forall b c, andb b c = andb c b.
Proof.
intros b c. destruct b.
{ destruct c.
{ reflexivity. }
{ reflexivity. } }
{ destruct c.
{ reflexivity. }
{ reflexivity. } }
Qed.
(** Since curly braces mark both the beginning and the end of a
proof, they can be used for multiple subgoal levels, as this
example shows. Furthermore, curly braces allow us to reuse the
same bullet shapes at multiple levels in a proof: *)
Theorem andb3_exchange :
forall b c d, andb (andb b c) d = andb (andb b d) c.
Proof.
intros b c d.
destruct b.
- destruct c.
{ destruct d.
- reflexivity.
- reflexivity. }
{ destruct d.
- reflexivity.
- reflexivity. }
- destruct c.
{ destruct d.
- reflexivity.
- reflexivity. }
{ destruct d.
- reflexivity.
- reflexivity. }
Qed.
(** Before closing the chapter, let's mention one final convenience.
As you may have noticed, many proofs perform case analysis on a variable
right after introducing it:
intros x y. destruct y as [|y].
This pattern is so common that Coq provides a shorthand for it: we
can perform case analysis on a variable when introducing it by
using an intro pattern instead of a variable name. For instance,
here is a shorter proof of the [plus_1_neq_0] theorem above. *)
Theorem plus_1_neq_0' : forall n : nat,
beq_nat (n + 1) 0 = false.
Proof.
intros [|n]. (* Suppose [n] is a fixed number. Let's consider all cases. *)
- (* Case: n = 0. *)
(* We need to show that [beq_nat (0 + 1) 0 = false]. *)
reflexivity. (* The left side of the equation evaluates to [false],
which is the same as the right side of the equation. *)
- (* Case: S n' = 0. *)
(* We need to show that [be_nat ((S n') + 1) 0 = false]. *)
reflexivity. (* The left side of the equation evaluates to [false],
which is the same as the right side of the equation. *)
Qed.
(** If there are no arguments to name, we can just write [[]]. *)
Theorem andb_commutative'' :
forall b c, andb b c = andb c b.
Proof.
intros [] []. (* Suppose [b] is a fixed [bool] value, and let's consider both of the cases.
Also, suppose [c] is a fixed number, and let's consider it's cases too. *)
- (* Case: [b = true], and [c = true]. *)
(* We need to show that [andb true true = andb true true]. *)
reflexivity. (* Both sides of the equation evaluate to the same value. *)
- (* Case: [b = true] and [c = false]. *)
(* We need to show that [andb true false = andb false true]. *)
reflexivity. (* Both sides evaluate to the same value. *)
- (* Case: [b = false] and [c = true]. *)
(* We need to show that [andb false true = andb true false]. *)
reflexivity. (* Both sides evaluate to the same value. *)
- (* Case: [b = false] and [c = false]. *)
(* We need to show that [andb false false = andb false false]. *)
reflexivity. (* Both sides evaluate to the same value. *)
Qed.
(** **** Exercise: 2 stars (andb_true_elim2) *)
(** Prove the following claim, marking cases (and subcases) with
bullets when you use [destruct]. *)
Theorem andb_true_elim2 : forall b c : bool,
andb b c = true -> c = true.
Proof.
intros b c. (* Suppose [b] and [c] are fixed boolean values. *)
intros H. (* Assume that the antecedent [b && c = true] is true. Call it [H].
If we want to show that [b && c = true] implies [c = true], then
We need to show that [c = true] follows. *)
destruct b. (* Let's proceed by case analysis. Let's consider [b] both as [true], and [false]. *)
- (* Case: [b = true]. *)
(* We need to show that [H: true && c = true] implies [c = true]. *)
destruct c. (* Let's proceed by case analysis. Let's consider [c] both as [true], and [false]. *)
+ (* Case: [c = true]. *)
(* We need to show that [true && true = true] implies [true = true]. *)
reflexivity. (* both sides of the equation [true = true] are the same, so we have shown
that [true = true] follows from [true && true = true]. So, we've shown
[true && true = true -> true = true]. *)
+ (* Case: [c = false]. *)
(* We need to show that [H: true && false = true] implies [false = true]. *)
rewrite <- H. (* Since [true && false = true], we can rewrite every occurrence of [true]
in the goal with [true && false]. That yields [false = true && false]. *)
reflexivity. (* The right side of the equation evaluates to [false], which is the same
as the left side of the equation, so they're the same. *)
- (* Case [b = false]. *)
(* We need to show that [H: false && c = true] implies [c = true]. *)
destruct c. (* Let's proceed by case analysis. Let's consider [c] both as [true], and [false]. *)
+ (* Case [c = true]. *)
(* We need to show that [H: false && true = true] implies [true = true]. *)
reflexivity. (* Both sides of the equation are the same. *)
+ (* Case [c = false]. *)
(* We need to show that [H: false && false = true] implies [false = true]. *)
rewrite <- H. (* Since [false && false = true], we can replace every occurrence of [true]
in the goal with [false && false]. That fields [false = false && false]. *)
reflexivity. (* The right hand side of the equation evaluates to [false], which
is the same as the right hand side of the equation. *)
Qed.
(** [] *)
(* Informal proof:
Theorem: [forall b c : bool, andb b c = true -> c = true.]
Proof: by case analysis on [b] and [c].
- Suppose [b] and [c] are fixed boolean values.
- Let [A] be the antecedent [andb b c = true], and
let [B] be the consequent [c = true].
- Suppose that [A] is true. We need to show that [B] follows from [A].
- Let's proceed by case analysis on [b].
- First, suppose [b] is [true]. Then [A] is [andb true c = true]
and [B] is [c = true]. We need to show that [B] follows from [A].
- Let's proceed by case analysis on [c].
- Suppose [c] is [true]. Then [A] is [andb true true = true],
and [B] is [true = true].
- Since [true = true], then [B] does follow from [A].
- Suppose [c] is [false]. Then [A] is [andb true false = true],
and [B] is [false = true].
- Since [A] says that [andb true false] and [true] are equal,
we can rewrite every occurrence of [true] in [B] with [andb true false].
That yields [false = andb true false].
- The right side of the equation evaluates to [false],
which is the same as the left hand side of the equation.
So we've shown that [B] follows from [A] in this case too.
- Second, suppose [b] is [false]. Then [A] is [andb false c = true]
and [B] is [c = true]. We need to show that [B] follows from [A] again.
- Let's proceed by case analysis on [c].
- Suppose [c] is [true]. Then [A] is [andb false true = true],
and [B] is [false = true].
- Since [A] says that [andb false false] and [true] are equal,
we can rewrite every occurrence of [true] in [B] with [andb false false].
That yields [false = andb false true].
- The right side of the equation evaluates to [false],
which is the same as the left hand side of the equation.
So we've shown that [B] follows from [A] in this case too.
- Suppose [c] is [false]. Then [A] is [andb false false = true],
and [B] is [false = true].
- Since [A] says that [andb false false] and [true] are equal,
we can rewrite every occurrence of [true] in [B] with [andb false false].
That yields [false = andb false false].
- The right side of the equation evaluates to [false],
which is the same as the left hand side of the equation.
So we've shown that [B] follows from [A] in this case too.
- So we've shown that [B] follows from [A] in all cases. []
*)
(** **** Exercise: 1 star (zero_nbeq_plus_1) *)
Theorem zero_nbeq_plus_1 : forall n : nat,
beq_nat 0 (n + 1) = false.
Proof.
intros n. (* Suppose [n] is a fixed number. *)
destruct n as [| n']. (* Let's proceed by case analysis.
That is, let's prove that the theorem holds when [n] is [0],
and when [n] is a successor [S] of [n']. *)
- (* Case: [n = 0]. *)
(* We need to show that [beq_nat 0 (0 + 1) = false]. *)
simpl. (* The left side of the equation reduces to [false]. *)
reflexivity. (* Now both sides of the equation are the same. *)
- (* Case: [n = S n']. *)
(* We need to show that [beq_nat 0 ((S n') + 1) = false]. *)
simpl. (* The left hand side of the equation reduces to [false]. *)
reflexivity. (* Now both sides of the equation are the same. *)
Qed.
(** [] *)
(* ================================================================= *)
(** ** More on Notation (Optional) *)
(** (In general, sections marked Optional are not needed to follow the
rest of the book, except possibly other Optional sections. On a
first reading, you might want to skim these sections so that you
know what's there for future reference.)
Recall the notation definitions for infix plus and times: *)
Notation "x + y" := (plus x y)
(at level 50, left associativity)
: nat_scope.
Notation "x * y" := (mult x y)
(at level 40, left associativity)
: nat_scope.
(** For each notation symbol in Coq, we can specify its _precedence
level_ and its _associativity_. The precedence level [n] is
specified by writing [at level n]; this helps Coq parse compound
expressions. The associativity setting helps to disambiguate
expressions containing multiple occurrences of the same
symbol. For example, the parameters specified above for [+] and
[*] say that the expression [1+2*3*4] is shorthand for
[(1+((2*3)*4))]. Coq uses precedence levels from 0 to 100, and
_left_, _right_, or _no_ associativity. We will see more examples
of this later, e.g., in the [Lists]
chapter.
Each notation symbol is also associated with a _notation scope_.
Coq tries to guess what scope is meant from context, so when it
sees [S(O*O)] it guesses [nat_scope], but when it sees the
cartesian product (tuple) type [bool*bool] (which we'll see in
later chapters) it guesses [type_scope]. Occasionally, it is
necessary to help it out with percent-notation by writing
[(x*y)%nat], and sometimes in what Coq prints it will use [%nat]
to indicate what scope a notation is in.
Notation scopes also apply to numeral notation ([3], [4], [5],
etc.), so you may sometimes see [0%nat], which means [O] (the
natural number [0] that we're using in this chapter), or [0%Z],
which means the Integer zero (which comes from a different part of
the standard library).
Pro tip: Coq's notation mechanism is not especially powerful.
Don't expect too much from it! *)
(* ================================================================= *)
(** ** Fixpoints and Structural Recursion (Optional) *)
(** Here is a copy of the definition of addition: *)
Fixpoint plus' (n : nat) (m : nat) : nat :=
match n with
| O => m
| S n' => S (plus' n' m)
end.
(** When Coq checks this definition, it notes that [plus'] is
"decreasing on 1st argument." What this means is that we are
performing a _structural recursion_ over the argument [n] -- i.e.,
that we make recursive calls only on strictly smaller values of
[n]. This implies that all calls to [plus'] will eventually
terminate. Coq demands that some argument of _every_ [Fixpoint]
definition is "decreasing."
This requirement is a fundamental feature of Coq's design: In
particular, it guarantees that every function that can be defined
in Coq will terminate on all inputs. However, because Coq's
"decreasing analysis" is not very sophisticated, it is sometimes
necessary to write functions in slightly unnatural ways. *)
(** **** Exercise: 2 stars, optional (decreasing) *)
(** To get a concrete sense of this, find a way to write a sensible
[Fixpoint] definition (of a simple function on numbers, say) that
_does_ terminate on all inputs, but that Coq will reject because
of this restriction. *)
(* FILL IN HERE *)
(* Note: the trick is to recursively call the function with
some argument that's not one of the ones matched in the pattern. *)
(** [] *)
(* ################################################################# *)
(** * More Exercises *)
(** **** Exercise: 2 stars (boolean_functions) *)
(** Use the tactics you have learned so far to prove the following
theorem about boolean functions. *)
Theorem identity_fn_applied_twice :
forall (f : bool -> bool),
(forall (x : bool), f x = x) ->
forall (b : bool), f (f b) = b.
Proof.
intros f. (* Suppose [f] is a fixed function from [bool] to [bool]. *)
intros H. (* Suppose the antecedent [forall x : bool, f x = x] is true. Call it [H].
We need to show that [forall b : bool, f (f b) = b] follows. *)
intros b. (* Suppose [b] is a fixed boolen value. *)
destruct b. (* Let's proceed by cases. That is, let's consider the case where
[b] is [true], and the case where [b] is [false].
We need to show that the goal holds in both those cases. *)
- (* Case [b = true]. *)
(* We need to show that [H] implies [f (f true) = true]. *)
rewrite -> H. (* [rewrite] is universally quantified, but it can match alpha-equivalent
variable names, apparently. And it says that [f x = x], i.e., it says
that [f x] is the same as [x] for any [x].
Our goal says: [f (f true) = true]. Look at the left hand side of
the equation: [f (f true)]. If we let [x] be [f true], then we
have something of the form [f x]. So, let's rewrite every occurrence
of [f x] (in this case, [f (f true)]) with just [x]. That yiels
[f true] on the left side of the equation. So we have [f true = true]. *)
rewrite -> H. (* On the left side of the equation, we still have [f true]. Again, that's
of the form [f x]. So let's rewrite [f x] again with [x]. That yields:
[true = true]. *)
reflexivity. (* Both sides of the equation are the same.
So we've shown that the goal follows from [H]. *)
- (* Case [b = false]. *)
(* We need to show that [H] implies [f (f false) = false]. *)
rewrite -> H. (* We follow the same procedure. Rewrite [f x] with [x]. In this case,
[f x] is [f (f false)], so if we just leave [x] we get [f false]. *)
rewrite -> H. (* Do it again, which yields [false = false]. *)
reflexivity. (* Both sides of the equation are now the same.
So we've shown that the goal follows from [H] in this case too. *)
Qed.
(** Now state and prove a theorem [negation_fn_applied_twice] similar
to the previous one but where the second hypothesis says that the
function [f] has the property that [f x = negb x].*)
Theorem negation_fn_applied_twice :
forall (f : bool -> bool),
(forall (x : bool), f x = negb x) ->
forall (b : bool), f (f b) = b.
Proof.
intros f.
intros H.
intros b.
destruct b.
- (* Case: [b = true]. *)
rewrite -> H. rewrite -> H. (* We do the same: rewrite twice. *)
simpl. (* The left side of the equation reduces to [true]. *)
reflexivity. (* Now both sides of the equation are the same. *)
- (* Case: [b = false]. *)
rewrite -> H. rewrite -> H. (* Ditto. *)
simpl. reflexivity.
Qed.
(* The [Import] statement on the next line tells Coq to use the
standard library String module. We'll use strings more in later
chapters, but for the moment we just need syntax for literal
strings for the grader comments. *)
From Coq Require Export String.
(* Do not modify the following line: *)
Definition manual_grade_for_negation_fn_applied_twice : option (prod nat string) := None.
(** [] *)
(** **** Exercise: 3 stars, optional (andb_eq_orb) *)
(** Prove the following theorem. (Hint: This one can be a bit tricky,
depending on how you approach it. You will probably need both
[destruct] and [rewrite], but destructing everything in sight is
not the best way.) *)
Theorem andb_eq_orb :
forall (b c : bool),
(andb b c = orb b c) ->
b = c.
Proof.
intros b c. (* Assume [b] and [c] are fixed boolean values. *)
(* The trick here is to NOT intros the antecedent of the implication at the start. *)
destruct b. (* Let's proceed by case analysis on [b]. That is, let's show that
the goal holds when [b] is [true], and when it's [false]. *)
- (* Case: [b = true]. *)
(* We need to show that [true && c = true || c -> true = c]. *)
destruct c. (* Let's proceed by case analysis on [c]. That is, let's show that
the goal holds when [c] is [true], and when it's [false]. *)
+ (* Case: [c = true]. *)
(* We need to show that [true && true = true || true -> true = true]. *)
reflexivity. (* [reflexivity can handle this case, where everything is [true]. *)
+ (* Case: [c = false]. *)
(* We need to show that [true && false = true || false -> true = false]. *)
simpl. (* [simpl] can reduce [true && false] to [false], and [true || false] to [true].
That yields [false = true -> true = false]. *)
intros H. (* Now let's suppose that the antecedent [false = true] is true. Call it [H].
Our goal is to show that the consequent [true = false] follows. *)
rewrite -> H. (* Since [H] says [false] and [true] are the same, we can replace
every occurrence of [false] in the goal with [true].
That yields [true = true]. *)
reflexivity. (* Both sides of the equation are the same, so we've shown that the goal
follows from [H]. *)
- (* Case [b = false]. *)
(* We need to show that [false && c = false || c -> false = c]. *)
simpl. (* [simpl] can reduce [false && c] to [false]. That yields [false = c -> false = c]. *)
intros H. (* Now let's suppose that the antecedent [false = c] is true. Call it [H].
Our goal now is to show that the consequent [false = c] follows. *)
destruct c. (* Let's proceed by case analysis on [c]. That is, let's show that the
theorem holds when [c] is [true], and when it's [false]. *)
+ (* Case: [c = true]. *)
(* We need to show that [false = true] implies [false = true]. *)
rewrite -> H. (* Since [H] says that [false] and [true] are the same, we can replace
every occurrence of [false] in the goal with [true]. That yields
[true = true]. *)
reflexivity. (* Both sides of the equation are the same. *)
+ (* Case: [c = false]. *)
(* We need to show that [false = false] implies [false = false]. *)
reflexivity. (* Reflexivity can solve that. *)
Qed.
(** [] *)
(** **** Exercise: 3 stars (binary) *)
(** Consider a different, more efficient representation of natural
numbers using a binary rather than unary system. That is, instead
of saying that each natural number is either zero or the successor
of a natural number, we can say that each binary number is either
- zero,
- twice a binary number, or
- one more than twice a binary number.
(a) First, write an inductive definition of the type [bin]
corresponding to this description of binary numbers.
(Hint: Recall that the definition of [nat] above,
Inductive nat : Type :=
| O : nat
| S : nat -> nat.
says nothing about what [O] and [S] "mean." It just says "[O]
is in the set called [nat], and if [n] is in the set then so
is [S n]." The interpretation of [O] as zero and [S] as
successor/plus one comes from the way that we _use_ [nat]
values, by writing functions to do things with them, proving
things about them, and so on. Your definition of [bin] should
be correspondingly simple; it is the functions you will write
next that will give it mathematical meaning.)
One caveat: If you use [O] or [S] as constructor names in your
definition, it will confuse the auto-grader script. Please choose
different names.
(b) Next, write an increment function [incr] for binary numbers,
and a function [bin_to_nat] to convert binary numbers to unary
numbers.
(c) Write five unit tests [test_bin_incr1], [test_bin_incr2], etc.
for your increment and binary-to-unary functions. (A "unit
test" in Coq is a specific [Example] that can be proved with
just [reflexivity], as we've done for several of our
definitions.) Notice that incrementing a binary number and
then converting it to unary should yield the same result as
first converting it to unary and then incrementing. *)
(* FILL IN HERE *)
Inductive bin : Type :=
| Zero : bin
| Twice : bin -> bin
| Twice_plus_one : bin -> bin.
Fixpoint incr (b : bin) : bin :=
match b with
| Zero => Twice_plus_one Zero
| Twice b' => Twice_plus_one b'
| Twice_plus_one b' => Twice (incr b')
end.
Fixpoint bin_to_nat (b : bin) : nat :=
match b with
| Zero => 0
| Twice b' => mult 2 (bin_to_nat b')
| Twice_plus_one b' => plus 1 (mult 2 (bin_to_nat b'))
end.
Example bin_example_1 :
bin_to_nat (incr Zero) = (bin_to_nat Zero) + 1.
Proof. simpl. reflexivity. Qed.
Example bin_example_2 :
bin_to_nat (incr (Twice Zero)) = plus (bin_to_nat (Twice Zero)) 1.
Proof. simpl. reflexivity. Qed.
Example bin_example_3 :
bin_to_nat (incr (Twice_plus_one Zero)) =
plus (bin_to_nat (Twice_plus_one Zero)) 1.
Proof. simpl. reflexivity. Qed.
Example bin_example_4 :
bin_to_nat (incr (Twice (Twice Zero))) =
plus (bin_to_nat (Twice (Twice Zero))) 1.
Proof. simpl. reflexivity. Qed.
Example bin_example_5 :
bin_to_nat (incr (Twice (Twice_plus_one Zero))) =
plus (bin_to_nat (Twice (Twice_plus_one Zero))) 1.
Proof. simpl. reflexivity. Qed.
(* Do not modify the following line: *)
Definition manual_grade_for_binary : option (prod nat string) := None.
(** [] *)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment