3. Modules
3.1 Opening library modules
When you start Coq without further requirements in the command line, you get a bare system with few libraries loaded. As we saw, a standard prelude module provides the standard logic connectives, and a few arithmetic notions. If you want to load and open other modules from the library, you have to use the
Require command, as we saw for
classical logic above. For instance, if you want more arithmetic
constructions, you should request:
Coq < Require Import Arith.
Such a command looks for a (compiled) module file
Arith.vo in
the libraries registered by Coq. Libraries inherit the structure of
the file system of the operating system and are registered with the
command Add LoadPath. Physical directories are mapped to
logical directories. Especially the standard library of Coq is
pre-registered as a library of name Coq. Modules have absolute
unique names denoting their place in Coq libraries. An absolute
name is a sequence of single identifiers separated by dots. E.g. the
module Arith has full name Coq.Arith.Arith and because
it resides in eponym subdirectory Arith of the standard
library, it can be as well required by the command
Coq < Require Import Coq.Arith.Arith.
This may be useful to avoid ambiguities if somewhere, in another branch of the libraries known by Coq, another module is also called
Arith. Notice that by default, when a library is registered,
all its contents, and all the contents of its subdirectories recursively are
visible and accessible by a short (relative) name as Arith.
Notice also that modules or definitions not explicitly registered in
a library are put in a default library called Top.The loading of a compiled file is quick, because the corresponding development is not type-checked again.
3.2 Creating your own modules
You may create your own modules, by writing Coq commands in a file, say
my_module.v. Such a module may be simply loaded in the current
context, with command Load my_module. It may also be compiled,
in “batch” mode, using the UNIX command
coqc. Compiling the module my_module.v creates a
file my_module.vo that can be reloaded with command
Require Import my_module. If a required module depends on other modules then the latters are automatically required beforehand. However their contents is not automatically visible. If you want a module
M required in a
module N to be automatically visible when N is required,
you should use Require Export M in your module N.3.3 Managing the context
It is often difficult to remember the names of all lemmas and definitions available in the current context, especially if large libraries have been loaded. A convenient
SearchAbout command
is available to lookup all known facts
concerning a given predicate. For instance, if you want to know all the
known lemmas about the less or equal relation, just ask:
Coq < SearchAbout le.
tricky: forall n : nat, le n 0 -> n = 0
Top.le_n_S: forall n m : nat, le n m -> le (S n) (S m)
le_ind:
forall (n : nat) (P : nat -> Prop),
P n ->
(forall m : nat, le n m -> P m -> P (S m)) ->
forall n0 : nat, le n n0 -> P n0
le_n: forall n : nat, le n n
le_S: forall n m : nat, le n m -> le n (S m)
Another command tricky: forall n : nat, le n 0 -> n = 0
Top.le_n_S: forall n m : nat, le n m -> le (S n) (S m)
le_ind:
forall (n : nat) (P : nat -> Prop),
P n ->
(forall m : nat, le n m -> P m -> P (S m)) ->
forall n0 : nat, le n n0 -> P n0
le_n: forall n : nat, le n n
le_S: forall n m : nat, le n m -> le n (S m)
Search displays only lemmas where the searched
predicate appears at the head position in the conclusion.
Coq < Search le.
Top.le_n_S: forall n m : nat, le n m -> le (S n) (S m)
le_n: forall n : nat, le n n
le_S: forall n m : nat, le n m -> le n (S m)
Top.le_n_S: forall n m : nat, le n m -> le (S n) (S m)
le_n: forall n : nat, le n n
le_S: forall n m : nat, le n m -> le n (S m)
A new and more convenient search tool is SearchPattern developed by Yves Bertot. It allows to find the theorems with a conclusion matching a given pattern, where
\_ can be used in
place of an arbitrary term. We remark in this example, that Coq
provides usual infix notations for arithmetic operators.
Coq < SearchPattern (_ + _ = _).
le_plus_minus_r: forall n m : nat, n <= m -> n + (m - n) = m
mult_acc_aux: forall n m p : nat, m + n * p = mult_acc m p n
plus_O_n: forall n : nat, 0 + n = n
plus_Sn_m: forall n m : nat, S n + m = S (n + m)
mult_n_Sm: forall n m : nat, n * m + n = n * S m
plus_0_l: forall n : nat, 0 + n = n
plus_0_r: forall n : nat, n + 0 = n
plus_comm: forall n m : nat, n + m = m + n
plus_Snm_nSm: forall n m : nat, S n + m = n + S m
plus_assoc: forall n m p : nat, n + (m + p) = n + m + p
plus_permute: forall n m p : nat, n + (m + p) = m + (n + p)
plus_assoc_reverse: forall n m p : nat, n + m + p = n + (m + p)
plus_permute_2_in_4: forall n m p q : nat, n + m + (p + q) = n + p + (m + q)
plus_tail_plus: forall n m : nat, n + m = tail_plus n m
plus_com: forall n m : nat, n + m = m + n
le_plus_minus_r: forall n m : nat, n <= m -> n + (m - n) = m
mult_acc_aux: forall n m p : nat, m + n * p = mult_acc m p n
plus_O_n: forall n : nat, 0 + n = n
plus_Sn_m: forall n m : nat, S n + m = S (n + m)
mult_n_Sm: forall n m : nat, n * m + n = n * S m
plus_0_l: forall n : nat, 0 + n = n
plus_0_r: forall n : nat, n + 0 = n
plus_comm: forall n m : nat, n + m = m + n
plus_Snm_nSm: forall n m : nat, S n + m = n + S m
plus_assoc: forall n m p : nat, n + (m + p) = n + m + p
plus_permute: forall n m p : nat, n + (m + p) = m + (n + p)
plus_assoc_reverse: forall n m p : nat, n + m + p = n + (m + p)
plus_permute_2_in_4: forall n m p q : nat, n + m + (p + q) = n + p + (m + q)
plus_tail_plus: forall n m : nat, n + m = tail_plus n m
plus_com: forall n m : nat, n + m = m + n
3.4 Now you are on your own
This tutorial is necessarily incomplete. If you wish to pursue serious proving in Coq, you should now get your hands on Coq's Reference Manual, which contains a complete description of all the tactics we saw, plus many more. You also should look in the library of developed theories which is distributed with Coq, in order to acquaint yourself with various proof techniques.
