Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 418 lines (295 sloc) 11.633 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
\section{Theorem Proving}

\subsection{Equality}

\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:

\begin{SaveVerbatim}{}

data (=) : a -> b -> Set where
   refl : x = x

\end{SaveVerbatim}
\useverb{}

\noindent
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:

\begin{SaveVerbatim}{eqprf}

fiveIsFive : 5 = 5
fiveIsFive = refl

twoPlusTwo : 2 + 2 = 4
twoPlusTwo = refl

\end{SaveVerbatim}
\useverb{eqprf}

\subsection{The Empty Type}

\label{sect:empty}

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:

\begin{SaveVerbatim}{natdisjoint}

disjoint : (n : Nat) -> O = S n -> _|_
disjoint n p = replace {P = disjointTy} p ()
  where
    disjointTy : Nat -> Set
    disjointTy O = ()
    disjointTy (S k) = _|_

\end{SaveVerbatim}
\useverb{natdisjoint}

\noindent
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.

\begin{SaveVerbatim}{falseelim}

FalseElim : _|_ -> a

\end{SaveVerbatim}
\useverb{falseelim}

\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}:

\begin{SaveVerbatim}{plusred}

plusReduces : (n:Nat) -> plus O n = n

\end{SaveVerbatim}
\useverb{plusred}

\noindent
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}:

\begin{SaveVerbatim}{plusredp}

plusReduces n = refl

\end{SaveVerbatim}
\useverb{plusredp}

\noindent
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}.

\begin{SaveVerbatim}{plusRedO}

plusReducesO : (n:Nat) -> n = plus n O
plusReducesO O = refl
plusReducesO (S k) = cong (plusReducesO k)

\end{SaveVerbatim}
\useverb{plusRedO}

\noindent
\texttt{cong} is a function defined in the library which states that
equality respects function application:

\begin{SaveVerbatim}{resps}

cong : {f : t -> u} -> a = b -> f a = f b

\end{SaveVerbatim}
\useverb{resps}

\noindent
We can do the same for the reduction behaviour of plus on successors:

\begin{SaveVerbatim}{plusRedS}

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)

\end{SaveVerbatim}
\useverb{plusRedS}

\noindent
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:

\begin{SaveVerbatim}{prOstruct}

plusReducesO' : (n:Nat) -> n = plus n O
plusReducesO' O = ?plusredO_O
plusReducesO' (S k) = let ih = plusReducesO' k in
                      ?plusredO_S

\end{SaveVerbatim}
\useverb{prOstruct}

\noindent
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:

\begin{SaveVerbatim}{showmetas}

*theorems> :m
Global metavariables:
        [plusredO_S,plusredO_O]

\end{SaveVerbatim}

\begin{SaveVerbatim}{metatypes}

*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)

\end{SaveVerbatim}
\useverb{showmetas}

\useverb{metatypes}

\noindent
The \texttt{:p} command enters interactive proof mode, which can be used to complete
the missing definitions.

\begin{SaveVerbatim}{proveO}

*theorems> :p plusredO_O

---------------------------------- (plusredO_O) --------
{hole0} : O = plus O O

\end{SaveVerbatim}
\useverb{proveO}

\noindent
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:

\begin{SaveVerbatim}{compute}

-plusredO_O> compute

---------------------------------- (plusredO_O) --------
{hole0} : O = O

\end{SaveVerbatim}
\useverb{compute}

\noindent
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):

\begin{SaveVerbatim}{refrefl}

-plusredO_O> refine refl
plusredO_O: no more goals

\end{SaveVerbatim}
\useverb{refrefl}

\noindent
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:

\begin{SaveVerbatim}{prOprooftrace}

-plusredO_O> qed
plusredO_O = proof {
    compute;
    refine refl;
}

\end{SaveVerbatim}
\useverb{prOprooftrace}

\begin{SaveVerbatim}{showmetasO}

*theorems> :m
Global metavariables:
        [plusredO_S]

\end{SaveVerbatim}
\useverb{showmetasO}

\noindent
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}:

\begin{SaveVerbatim}{plusredOSprf}

*theorems> :p plusredO_S

---------------------------------- (plusredO_S) --------
{hole0} : (k : Nat) -> (k = plus k O) -> S k = S (plus k O)

\end{SaveVerbatim}
\useverb{plusredOSprf}

\noindent
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:

\begin{SaveVerbatim}{prSintros}

  k : Nat
  ih : k = plus k O
---------------------------------- (plusredO_S) --------
{hole2} : S k = S (plus k O)

\end{SaveVerbatim}
\useverb{prSintros}

\noindent
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:

\begin{SaveVerbatim}{}

-plusredO_S> rewrite ih

  k : Nat
  ih : k = plus k O
---------------------------------- (plusredO_S) --------
{hole3} : S k = S k

-plusredO_S>

\end{SaveVerbatim}
\useverb{}

\noindent
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:

\begin{SaveVerbatim}{prOStrace}

-plusredO_S> trivial
plusredO_S: no more goals
-plusredO_S> qed
plusredO_S = proof {
    intros;
    rewrite ih;
    trivial;
}

\end{SaveVerbatim}
\useverb{prOStrace}

\noindent
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}

\label{sect:totality}

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:

\begin{SaveVerbatim}{empties}

-- making use of 'hd' being partially defined
empty1 : _|_
empty1 = hd [] where
    hd : List a -> a
    hd (x :: xs) = x

-- not terminating
empty2 : _|_
empty2 = empty2

\end{SaveVerbatim}
\useverb{empties}

\noindent
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:

\begin{SaveVerbatim}{totalcheck}

*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

\end{SaveVerbatim}
\useverb{totalcheck}

\noindent
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:

\begin{SaveVerbatim}{emptyfail}

total empty2 : _|_
empty2 = empty2

Type checking ./theorems.idr
theorems.idr:25:empty2 is possibly not total as it is not well founded

\end{SaveVerbatim}
\useverb{emptyfail}

\noindent
Reassuringly, our proof in Section \ref{sect:empty} that the zero and successor constructors
are disjoint is total:

\begin{SaveVerbatim}{totdisjoint}

*theorems> :total disjoint
Total

\end{SaveVerbatim}
\useverb{totdisjoint}

\noindent
The totality check is currently very conservative. To be recorded as total, a function must:

\begin{itemize}
\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
\end{itemize}

\noindent
The last of these conditions may be relaxed in the future.
Something went wrong with that request. Please try again.