Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

418 lines (295 sloc) 11.633 kB
\section{Theorem Proving}
\Idris{} allows propositional equalities to be declared, allowing theorems about
programs to be stated and proved. Equality is built in, but conceptually has
the following definition:
data (=) : a -> b -> Set where
refl : x = x
Equalities can be proposed between any values of any types, but the only way to
construct a proof of equality is if values actually are equal. For example:
fiveIsFive : 5 = 5
fiveIsFive = refl
twoPlusTwo : 2 + 2 = 4
twoPlusTwo = refl
\subsection{The Empty Type}
There is an empty type, \texttt{\_|\_}, which has no constructors. It is
therefore impossible to construct an element of the empty type, at least
without using a partially defined or general recursive function (see Section
\ref{sect:totality} for more details). We can therefore use the empty type
to prove that something is impossible, for example zero is never equal
to a successor:
disjoint : (n : Nat) -> O = S n -> _|_
disjoint n p = replace {P = disjointTy} p ()
disjointTy : Nat -> Set
disjointTy O = ()
disjointTy (S k) = _|_
There is no need to worry too much about how this function works --- essentially,
it applies the library function \texttt{replace}, which uses an equality proof to
transform a predicate. Here we use it to transform a value of a type which can exist,
the empty tuple, to a value of a type which can't, by using a proof of something
which can't exist.
Once we have an element of the empty type, we can prove anything. \texttt{FalseElim}
is defined in the library, to assist with proofs by contradiction.
FalseElim : _|_ -> a
\subsection{Simple Theorems}
When type checking dependent types, the type itself gets \emph{normalised}. So imagine
we want to prove the following theorem about the reduction behaviour of \texttt{plus}:
plusReduces : (n:Nat) -> plus O n = n
We've written down the statement of the theorem as a type, in just the same way
as we would write the type of a program. In fact there is no real distinction
between proofs and programs. A proof, as far as we are concerned here, is
merely a program with a precise enough type to guarantee a particular property
of interest.
We won't go into details here, but the Curry-Howard
correspondence~\cite{howard} explains this relationship.
The proof itself is trivial, because \texttt{plus O n} normalises to \texttt{n}
by the definition of \texttt{plus}:
plusReduces n = refl
It is slightly harder if we try the arguments the other way, because plus is
defined by recursion on its first argument. The proof also works by recursion
on the first argument to \texttt{plus}, namely \texttt{n}.
plusReducesO : (n:Nat) -> n = plus n O
plusReducesO O = refl
plusReducesO (S k) = cong (plusReducesO k)
\texttt{cong} is a function defined in the library which states that
equality respects function application:
cong : {f : t -> u} -> a = b -> f a = f b
We can do the same for the reduction behaviour of plus on successors:
plusReducesS : (n:Nat) -> (m:Nat) -> S (plus n m) = plus n (S m)
plusReducesS O m = refl
plusReducesS (S k) m = cong (plusReducesS k m)
Even for trival theorems like these, the proofs are a little tricky to
construct in one go. When things get even slightly more complicated, it becomes
too much to think about to construct proofs in this 'batch mode'. \Idris{}
therefore provides an interactive proof mode.
\subsection{Interactive theorem proving}
Instead of writing the proof in one go, we can use \Idris{}'s interactive
proof mode. To do this, we write the general \emph{structure} of the proof,
and use the interactive mode to complete the details. We'll be constructing
the proof by \emph{induction}, so we write the cases for \texttt{O} and
\texttt{S}, with a recursive call in the \texttt{S} case giving the inductive
hypothesis, and insert \emph{metavariables} for the rest of the definition:
plusReducesO' : (n:Nat) -> n = plus n O
plusReducesO' O = ?plusredO_O
plusReducesO' (S k) = let ih = plusReducesO' k in
On running \Idris{}, two global names are created, \texttt{plusredO\_O} and
\texttt{plusredO\_S}, with no definition. We can use the \texttt{:m} command
at the prompt to find out which metavariables are still to be solved (or, more
precisely, which functions exist but have no definitions), then the
\texttt{:t} command to see their types:
*theorems> :m
Global metavariables:
*theorems> :t plusredO_O
plusredO_O : O = plus O O
*theorems> :t plusredO_S
plusredO_S : (k : Nat) -> (k = plus k O) -> S k = S (plus k O)
The \texttt{:p} command enters interactive proof mode, which can be used to complete
the missing definitions.
*theorems> :p plusredO_O
---------------------------------- (plusredO_O) --------
{hole0} : O = plus O O
This gives us a list of premisses
(above the line; there are none here) and the current goal (below the line;
named \texttt{\{hole0\}} here).
At the prompt we can enter tactics to direct the construction of the proof. In this case,
we can normalise the goal with the \texttt{compute} tactic:
-plusredO_O> compute
---------------------------------- (plusredO_O) --------
{hole0} : O = O
Now we have to prove that \texttt{O} equals \texttt{O}, which is easy to prove by
\texttt{refl}. To apply a function, such as \texttt{refl}, we use \texttt{refine}
which introduces subgoals for each of the function's explicit arguments (\texttt{refl}
has none):
-plusredO_O> refine refl
plusredO_O: no more goals
Here, we could also have used the \texttt{trivial} tactic, which tries to refine by
\texttt{refl}, and if that fails, tries to refine by each name in the local context.
When a proof is complete, we use the \texttt{qed} tactic to add the proof to the
global context, and remove the metavariable from the unsolved metavariables list.
This also outputs a trace of the proof:
-plusredO_O> qed
plusredO_O = proof {
refine refl;
*theorems> :m
Global metavariables:
The \texttt{:addproof} command, at the interactive prompt, will add the proof to
the source file (effectively in an appendix).
Let us now prove the other required lemma, \texttt{plusredO\_S}:
*theorems> :p plusredO_S
---------------------------------- (plusredO_S) --------
{hole0} : (k : Nat) -> (k = plus k O) -> S k = S (plus k O)
In this case, the goal is a function type, using \texttt{k} (the argument accessible by
pattern matching) and \texttt{ih} (the local variable containing the result of
the recursive call). We can introduce these as premisses using the \texttt{intro} tactic
twice (or \texttt{intros}, which introduces all arguments as premisses). This gives:
k : Nat
ih : k = plus k O
---------------------------------- (plusredO_S) --------
{hole2} : S k = S (plus k O)
We know, from the type of \texttt{ih}, that \texttt{k = plus k O}, so we would like to
use this knowledge to replace \texttt{plus k O} in the goal with \texttt{k}. We can
achieve this with the \texttt{rewrite} tactic:
-plusredO_S> rewrite ih
k : Nat
ih : k = plus k O
---------------------------------- (plusredO_S) --------
{hole3} : S k = S k
The \texttt{rewrite} tactic takes an equality proof as an argument, and tries to rewrite
the goal using that proof. Here, it results in an equality which is trivially provable:
-plusredO_S> trivial
plusredO_S: no more goals
-plusredO_S> qed
plusredO_S = proof {
rewrite ih;
Again, we can add this proof to the end of our source file using the \texttt{:addproof}
command at the interactive prompt.
\subsection{Totality Checking}
If we really want to trust our proofs, it is important that they are defined by
\emph{total} functions --- that is, a function which is defined for all possible inputs
and is guaranteed to terminate. Otherwise we could construct an element of the empty type,
from which we could prove anything:
-- making use of 'hd' being partially defined
empty1 : _|_
empty1 = hd [] where
hd : List a -> a
hd (x :: xs) = x
-- not terminating
empty2 : _|_
empty2 = empty2
Internally, \Idris{} checks every definition for totality, and we can check at the prompt
with the \texttt{:total} command. We see that neither of the above definitions is total:
*theorems> :total empty1
possibly not total due to: empty1#hd
not total as there are missing cases
*theorems> :total empty2
possibly not total as it is not well founded
Note the use of the word ``possibly'' --- a totality check can, of course, never be certain
due to the undecidability of the halting problem. The check is, therefore, conservative.
It is also possible (and indeed advisable, in the case of proofs) to mark functions as
total so that it will be a compile time error for the totality check to fail:
total empty2 : _|_
empty2 = empty2
Type checking ./theorems.idr
theorems.idr:25:empty2 is possibly not total as it is not well founded
Reassuringly, our proof in Section \ref{sect:empty} that the zero and successor constructors
are disjoint is total:
*theorems> :total disjoint
The totality check is currently very conservative. To be recorded as total, a function must:
\item Cover all possible inputs
\item Be \emph{well-founded} --- i.e. each recursive call must have a decreasing argument
\item Not call any non-total functions
\item Not use any data types which are not \emph{strictly positive}
\item (In version 0.9.2) Not be mutually recursive
The last of these conditions may be relaxed in the future.
Jump to Line
Something went wrong with that request. Please try again.