(** lecture note 2/5/2014 **) Fixpoint plus (n : nat) (m : nat) : nat := match n with | O => m | S n' => S (plus n' m) end. (** Adding three to two now gives us five, as we'd expect. *) Eval simpl in (plus (S (S (S O))) (S (S O))). (** 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 | S n' => plus m (mult n' m) end. Example test_mult1: (mult 3 3) = 9. Proof. reflexivity. 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 | S _ , O => n | S n', S m' => minus n' m' end. (** 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 bogus variable name. *) Fixpoint exp (base power : nat) : nat := match power with | O => S O | S p => mult base (exp base p) end. (** **** Exercise: 1 star (factorial) *) (** Recall the standard factorial function: << factorial(0) = 1 factorial(n) = n * factorial(n-1) (if n>0) >> Translate this into Coq. *) (** Fixpoint factorial (n:nat) : nat := (* FILL IN HERE *) admit. Example test_factorial1: (factorial 3) = 6. (* FILL IN HERE *) Admitted. Example test_factorial2: (factorial 5) = (mult 10 12). (* FILL IN HERE *) Admitted. (** [] *) **) 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. 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, but interested readers can refer to the "More on Notation" subsection in the "Optional Material" 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 nothing built-in, we really mean it: even equality testing for numbers is a user-defined operation! *) (** The [beq_nat] function 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 | O => true | S m' => false end | S n' => match m with | O => false | S m' => beq_nat n' m' end end. (** Similarly, the [ble_nat] function tests [nat]ural numbers for [l]ess-or-[e]qual, yielding a [b]oolean. *) Fixpoint ble_nat (n m : nat) : bool := match n with | O => true | S n' => match m with | O => false | S m' => ble_nat n' m' end end. Example test_ble_nat1: (ble_nat 2 2) = true. Proof. reflexivity. Qed. Example test_ble_nat2: (ble_nat 2 4) = true. Proof. reflexivity. Qed. Example test_ble_nat3: (ble_nat 4 2) = false. Proof. reflexivity. Qed. Theorem plus_O_n : forall n : nat, 0 + n = n. Proof. intros n. reflexivity. Qed.