15 Functional Programming in Rocq
Borrowed from https://www.cs.cornell.edu/courses/cs3110/2018sp/l/19-coq-fp/notes.html
Super-duper important tip: You absolutely must step through this file interactively in Rocq to understand what’s going on. Observe what happens to the proof state and in the output window. Just reading this file in your browser does not suffice!
15.1 Types and functions
Rocq
Inductive day : Type := | sun : day | mon : day | tue : day | wed : day | thu : day | fri : day | sat : day.
The definition starts with the keyword Inductive instead of the keyword type.
day has a type itself, Type. Essentially this is saying that day is a type.
The definition uses := instead of =.
Every constructor is explicitly given the type day. Later we’ll see that constructors can have more complicated types.
The definition must end with a period.
Here’s a function that returns the next day after its input.
Definition next_day d :=
match d with
| sun => mon
| mon => tue
| tue => wed
| wed => thu
| thu => fri
| fri => sat
| sat => sun
end.The Definition keyword is capitalized.
The pattern match must end with the end keyword.
The branches use => instead of -> to separate the pattern from the value to be returned.
Here is another function.
Definition prev_day d :=
match d with
| sun => sat
| mon => sun
| tue => mon
| wed => tue
| thu => wed
| fri => thu
| sat => fri
end.15.2 Theorems and proofs
Now let’s prove some small theorems about programs that use days.
First, let’s prove that applying next_day to [tue] evaluates to wed. If you were going to do that with a more informal proof in natural language, your first instinct might be to say "it’s obvious." That’s kind of what the proof below does.
Rocq
Theorem wed_after_tue : next_day tue = wed. Proof. auto. Qed.
Print wed_after_tue.The output of that is
wed_after_tue = eq_refl : next_day tue = wedRarely, though, is a theorem so simple that it can immediately be proved by searching for a proof. Usually we need to provide Rocq with more explicit guidance. A more careful informal proof of the theorem might read "next_day tue evaluates to wed. So the equality to be proved reduces to wed = wed. That trivially holds." That is essentially the proof that strategy that is followed below:
Theorem wed_after_tue' : next_day tue = wed.
Proof.
simpl. trivial.
Qed.auto.
Print wed_after_tue'.The output of that is
wed_after_tue' = eq_refl : next_day tue = wed
Theorem day_never_repeats : forall d : day, next_day d <> d.
Proof. auto. next_day sun <> sun
next_day mon <> mon
etc.
mon <> sun
tue <> mon
etc.
destruct is a tactic that considers all the possible ways to construct an inductive type.
discriminate is a tactic that proves different constructors cannot be equal.
intros d. destruct d.
simpl. discriminate.
simpl. discriminate.
simpl. discriminate.
simpl. discriminate.
simpl. discriminate.
simpl. discriminate.
simpl. discriminate.
Qed.That repetition of the same tactic is rather ugly. First, we don’t actually need to use simpl. In fact, discriminate will do simplification itself, as will many other tactics. Second, instead of writing discriminate 7 times, we could instead write all: discriminate. That means to use discriminate on all the remaining subgoals in the proof.
Theorem day_never_repeats' : forall d : day, next_day d <> d.
Proof.
intros d. destruct d.
all: discriminate.
Qed.
Theorem day_never_repeats'' : forall d : day, next_day d <> d.
Proof.
intros d. destruct d; discriminate.
Qed.Now let’s prove that if the day after d is tue, then d must be mon. To express that implication in a theorem we use the symbol ->, where A -> B means that if A is true, then so is B.
Theorem mon_preceds_tues : forall d : day,
next_day d = tue -> d = mon.Rocq
Proof. intros d next_day_is_tue. destruct d. all: discriminate || trivial. Qed.
The first line introduces two assumptions into the proof: that d is a day, and that next_day d = tue. The latter is the antecedent of the implication that is being proved, i.e., the P in P -> Q; Q is called the consequent. We chose the name next_day_is_tue to record that assumption. Idiomatically, Rocq programmers will often use short names like H for these kinds of hypotheses.
The second line does the case analysis on d as we did before.
The third line says to use discriminate || trivial on all the subgoals. That works like a short-circuit OR in OCaml: if discriminate fails to find a proof of the subgoal, then trivial will be used to find the proof instead.
15.3 Lists
The Rocq standard library includes lists. Some parts of the list library are already included by default, but not all are. To load the full list library, we need to issue the following commands:
Require Import List.
Import ListNotations.
Module MyList.
Inductive list (A : Type) : Type :=
| nil : list A
| cons : A -> list A -> list A.
End MyList.Let’s compare that to a similar OCaml definition of lists:
type 'a list = nil | cons of 'a * 'a list
Check list.
list : Type -> TypeAlso, notice the branches:
| nil : list A
| cons : A -> list A -> list A.These say that nil already has type list A, for any A, and that cons is a function that takes an A and a list A and returns a list A. So Rocq’s cons is truly a function, unlike OCaml’s. (In OCaml you can’t, for example, pass a constructor name to a function.)
We can use pattern matching to code functions that take lists as inputs, just like in OCaml.
Definition is_empty (A : Type) (lst : list A) :=
match lst with
| nil => true
| cons _ _ => false
end.Like in OCaml, we can use syntactic sugar for lists, writing [] for nil and :: for cons:
Definition is_empty_sugar (A : Type) (lst : list A) :=
match lst with
| [] => true
| _::_ => false
end.
Compute is_empty nat [1].
Compute is_empty nat [].In the second computation above, it wouldn’t matter what type we pass in because the list is empty, but we do have to provide _some_ type as an argument. There are various ways in Rocq of making this type argument something that the compiler infers, instead of the programmer having to provide. These are called _implicit arguments_. Here’s one way to make an argument implicit:
Definition is_empty' {A : Type} (lst : list A) :=
match lst with
| [] => true
| _::_ => false
end.
Compute is_empty' [1].We put the A:Type parameter in braces instead of parentheses. This tells Rocq that the argument should be inferred. As you can see, we didn’t provide it in the example usage. In fact, Rocq would reject is_empty’ nat [1], because once we’ve made the argument implicit, we’re actually not even allowed to provide it.
If for some reason we really did need to explicitly provide an implicit argument, we could do that by prefixing the name of the function with @. For example:
Compute @is_empty' nat [1].To define recursive functions on lists, instead of OCaml’s let rec, Rocq uses Fixpoint as a keyword. That perhaps-cryptic name comes from programming languages theory, where recursion can be defined as a so-called fixed point of a function.
Here is a definition of length for lists, and an example usage. Again, we do this in its own module to avoid clashing with the standard library’s definition of length.
Module MyLength.
Fixpoint length {A : Type} (lst : list A) :=
match lst with
| nil => 0
| _::t => 1 + length t
end.
Compute length [1;2].
End MyLength.15.4 Options
In the Rocq standard library, options are defined as follows. (Once more we wrap the definition in its own module to avoid clashing with the standard library’s own definition.)
Module MyOption.
Inductive option (A:Type) : Type :=
| Some : A -> option A
| None : option A.
End MyOption.Here is a function [hd_opt]. It returns the head of a list, wrapped in [Some]. If the list is empty, it returns None.
Definition hd_opt {A : Type} (lst : list A) : option A :=
match lst with
| nil => None
| x :: _ => Some x
end.
Compute hd_opt [1].
Compute @hd_opt nat [].Now let’s prove something about hd_opt: when applied to a list of length 0, it returns None. Informally, that holds because there are two possibilities of how a list is constructed:
- if the list is [nil], then its length is certainly 0, and evaluation of hd_opt’s pattern match will choose the branch that returns None.
- if the list is constructed with [cons], then its length is at least 1. That contradicts the assumption that its length is 0.
The proof below uses exactly that reasoning.
Theorem length0_implies_hdopt_is_none :
forall A : Type, forall lst : list A,
length lst = 0 -> hd_opt lst = None.
Proof.
intros A lst length_lst_is_0.
destruct lst.
trivial.
discriminate.
Qed. The first case, where the list is nil, is handled by trivial, which finds a proof that hd_opt nil = None. It succeeds in doing that by first doing some computation, which reduces hd_opt nil to None, then showing that None = None because equality is reflexive.
The second case, where the list is cons, is handled by discriminate, which finds a contradiction while trying to prove that [hd_opt (a :: lst) = None]. Of course, that equality is untrue: hd_opt (a :: lst) is actually Some a. So discriminate looks in the hypotheses of the proof and finds one that claims length (a :: lst) = 0. Of course, that’s untrue, because length (a :: lst) is 1 + length lst, which is at least 1, and 1 <> 0. The discriminate tactic detects that contradiction. You might recall that from an assumption of false, you are allowed to conclude anything. That’s the reasoning being used here.
When there are a few cases in a proof, as there were (two) in the previous proof, sometimes Rocq programmers use bullets to make the structure of their proofs apparent to readers. The bullets also help with focusing the programmer’s attention on the proof state during the process of constructing the proof. The bullets in the proof below, written -, cause all but one subgoal to disappear. After a subgoal has been proved, the proof state reads "There are unfocused goals." The next bullet causes the next unfocused goal to become focused. This is probably best understood by stepping through the following proof in Rocq and observing what happens to the proof state when each bullet is processed.
Theorem length0_implies_hdopt_is_none' :
forall A : Type, forall lst : list A,
length lst = 0 -> hd_opt lst = None.
Proof.
intros A lst length_lst_is_0.
destruct lst.
- trivial.
- discriminate.
Qed. 15.5 Summary
Rocq is a functional programming language. It contains many of the same features as OCaml. In fact, Rocq is implemented in OCaml. Rocq is also a proof assistant. We can write programs and prove theorems about those programs. Rocq searches for those proofs, and we guide its search with tactics.
15.5.1 Terms and concepts
antecedent
case analysis
consequent
constructor
definition
fixpoint
function
goal
hypothesis
implicit argument
inductive type
proof search
proof state
reflexivity of equality
tactic
tactical
theorem
15.5.2 Tactics
auto
destruct
discriminate
intros
simpl
trivial
tacticals: all, ||, ;, -, *, +