REVISED: Saturday, November 16, 2024
Coq is a powerful Proof Assistant that allows you to formally verify mathematical proofs. To effectively use Coq, you'll need to master a few fundamental proof tactics. Let's delve into three of the most common ones: reflexivity, rewrite, and destruct.
1. REFLEXIVITY
The reflexivity tactic is used to prove equalities where both sides of the equation are identical. In essence, it asserts that any term is equal to itself. If you are having problems understanding Coq, preceed reflexivity with simpl. simpl is not necessary; however, it might help with human readability.
Example:
Goal forall x: nat, x = x.
Proof.
reflexivity.
Qed.
In this example, the goal is to prove that for any natural number x, x is equal to itself. The reflexivity tactic directly proves this equality.
Compiling:
Goal forall x: nat,
x = x.
The Goal window of CoqIDE shows:
1 goal
______________________________________(1/1)
forall x : nat, x = x
Compiling:
Proof.
reflexivity.
The Goal window of CoqIDE shows:
No more goals.
Qed.
2. REWRITE
The rewrite tactic is used to replace one side of an equation with another equivalent expression. This is particularly useful when you have a known equality that can be applied to simplify a proof.
Example:
Theorem plus_id_example :
forall n m : nat,
n = m ->
n + n = m + m.
Proof.
intros n m.
intros H.
rewrite -> H.
reflexivity.
Qed.
Compiling:
Theorem plus_id_example :
forall n m : nat,
n = m ->
n + n = m + m.
The Goal window of CoqIDE shows:
1 goal
______________________________________(1/1)
forall n m : nat, n = m -> n + n = m + m
Compiling:
Proof.
intros n m.
The Goal window of CoqIDE shows:
1 goal
n, m : nat
______________________________________(1/1)
n = m -> n + n = m + m
forall n, m : nat are moved above the line and become assumptions to improve human readability. Now we more clearly see we have one goal, and we are assuming n and m are both natural numbers.
Compiling:
intros H.
The Goal window of CoqIDE shows:
1 goal
n, m : nat
H : n = m
______________________________________(1/1)
n + n = m + m
Compiling:
rewrite -> H.
The Coq default is left to right; therefore, we could have written rewrite H.
The Goal window of CoqIDE shows:
1 goal
n, m : nat
H : n = m
______________________________________(1/1)
m + m = m + m
Compiling:
reflexivity.
The Goal window of CoqIDE shows:
No more goals.
Qed.
3. DESTRUCT
The destruct tactic is used to break down inductive data types into their constituent cases. This is essential for reasoning about recursive definitions and proofs by induction.
Example:
Theorem andb_commutative : forall b c, andb b c = andb c b.
Proof.
intros b c.
destruct b, c.
- reflexivity.
- reflexivity.
- reflexivity.
- reflexivity.
Qed.
Compiling:
Theorem andb_commutative : forall b c, andb b c = andb c b.
The Goal window of CoqIDE shows:
1 goal
______________________________________(1/1)
forall b c : bool,
(b && c)%bool = (c && b)%bool
Compiling:
Proof.
intros b c.
The Goal window of CoqIDE shows:
1 goal
b, c : bool
______________________________________(1/1)
(b && c)%bool = (c && b)%bool
Compiling:
destruct b, c.
The Goal window of CoqIDE shows:
4 goals
______________________________________(1/4)
(true && true)%bool = (true && true)%bool
______________________________________(2/4)
(true && false)%bool = (false && true)%bool
______________________________________(3/4)
(false && true)%bool = (true && false)%bool
______________________________________(4/4)
(false && false)%bool = (false && false)%bool
Compiling:
- reflexivity.
The Goal window of CoqIDE shows:
This subproof is complete, but there are some unfocused goals:
______________________________________(1/3)
(true && false)%bool = (false && true)%bool
______________________________________(2/3)
(false && true)%bool = (true && false)%bool
______________________________________(3/3)
(false && false)%bool = (false && false)%bool
Compiling:
- reflexivity.
The Goal window of CoqIDE shows:
This subproof is complete, but there are some unfocused goals:
______________________________________(1/2)
(false && true)%bool = (true && false)%bool
______________________________________(2/2)
(false && false)%bool = (false && false)%bool
Compiling:
- reflexivity.
The Goal window of CoqIDE shows:
This subproof is complete, but there are some unfocused goals:
______________________________________(1/1)
(false && false)%bool = (false && false)%bool
Compiling:
- reflexivity.
The Goal window of CoqIDE shows:
No more goals.
Qed.
Additional information.
Combined Destruct:
Instead of performing two nested destructs, they can be combined into a single destruct b, c. This simultaneously breaks down both b and c into their possible cases, leading to a more concise proof.
Implicit Reflexivity:
In each case, the equality is trivial. Coq's automated tactics can often infer the necessary steps to prove such simple equalities. By simply using reflexivity without explicit argument, we let Coq fill in the details.
Key Points to Remember.
Efficient Destruct:
Use destruct judiciously to break down complex terms into simpler cases.
Leverage Automation:
Utilize Coq's automated tactics to simplify proofs, especially for trivial equalities.
Readability:
Write clear and concise proofs. Avoid unnecessary steps and redundant explanations.
4. CONCLUSION
By mastering these three techniques, you can tackle a wide range of proofs in Coq.
Remember that practice is key, so don't hesitate to experiment and explore more complex examples.
5. REFERENCES
Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.
Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.
Goerzen, J. & O'Sullivan, B. & Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.
Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.
Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.
Pierce, Benjamin C., et al., editors. Logical Foundations. Vol. 1 of Software Foundations. Electronic Textbook, 2024. Version 6.7, http://softwarefoundations.cis.upenn.edu.
Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.