# public idris-lang /Idris-dev

### Subversion checkout URL

You can clone with HTTPS or Subversion.

Fetching contributors…

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 aboutprograms to be stated and proved. Equality is built in, but conceptually hasthe following definition:\begin{SaveVerbatim}{}data (=) : a -> b -> Set where   refl : x = x\end{SaveVerbatim}\useverb{}\noindentEqualities can be proposed between any values of any types, but the only way toconstruct a proof of equality is if values actually are equal. For example:\begin{SaveVerbatim}{eqprf}fiveIsFive : 5 = 5fiveIsFive = refltwoPlusTwo : 2 + 2 = 4twoPlusTwo = refl\end{SaveVerbatim}\useverb{eqprf}\subsection{The Empty Type}\label{sect:empty}There is an empty type, \texttt{\_|\_}, which has no constructors. It istherefore impossible to construct an element of the empty type, at leastwithout using a partially defined or general recursive function (see Section\ref{sect:totality} for more details). We can therefore use the empty typeto prove that something is impossible, for example zero is never equalto 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} \noindentThere 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 somethingwhich 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 imaginewe 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}\noindentWe've written down the statement of the theorem as a type, in just the same wayas we would write the type of a program. In fact there is no real distinctionbetween proofs and programs. A proof, as far as we are concerned here, ismerely a program with a precise enough type to guarantee a particular propertyof interest.We won't go into details here, but the Curry-Howardcorrespondence~\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}\noindentIt is slightly harder if we try the arguments the other way, because plus isdefined by recursion on its first argument. The proof also works by recursionon the first argument to \texttt{plus}, namely \texttt{n}.\begin{SaveVerbatim}{plusRedO}plusReducesO : (n:Nat) -> n = plus n OplusReducesO O = reflplusReducesO (S k) = cong (plusReducesO k)\end{SaveVerbatim}\useverb{plusRedO}\noindent\texttt{cong} is a function defined in the library which states thatequality respects function application:\begin{SaveVerbatim}{resps}cong : {f : t -> u} -> a = b -> f a = f b\end{SaveVerbatim}\useverb{resps}\noindentWe 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 = reflplusReducesS (S k) m = cong (plusReducesS k m)\end{SaveVerbatim}\useverb{plusRedS}\noindentEven for trival theorems like these, the proofs are a little tricky toconstruct in one go. When things get even slightly more complicated, it becomestoo 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 interactiveproof 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 constructingthe 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 inductivehypothesis, and insert \emph{metavariables} for the rest of the definition:\begin{SaveVerbatim}{prOstruct}plusReducesO' : (n:Nat) -> n = plus n OplusReducesO' O = ?plusredO_OplusReducesO' (S k) = let ih = plusReducesO' k in                      ?plusredO_S\end{SaveVerbatim}\useverb{prOstruct}\noindentOn running \Idris{}, two global names are created, \texttt{plusredO\_O} and\texttt{plusredO\_S}, with no definition. We can use the \texttt{:m} commandat the prompt to find out which metavariables are still to be solved (or, moreprecisely, 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}\noindentThe \texttt{:p} command enters interactive proof mode, which can be used to completethe missing definitions.\begin{SaveVerbatim}{proveO}*theorems> :p plusredO_O---------------------------------- (plusredO_O) --------{hole0} : O = plus O O\end{SaveVerbatim}\useverb{proveO}\noindentThis 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}\noindentNow 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}\noindentHere, 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 theglobal 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} \noindentThe \texttt{:addproof} command, at the interactive prompt, will add the proof tothe 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}\noindentIn this case, the goal is a function type, using \texttt{k} (the argument accessible bypattern matching) and \texttt{ih} (the local variable containing the result ofthe recursive call). We can introduce these as premisses using the \texttt{intro} tactictwice (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}\noindentWe know, from the type of \texttt{ih}, that \texttt{k = plus k O}, so we would like touse this knowledge to replace \texttt{plus k O} in the goal with \texttt{k}. We canachieve 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{}\noindentThe \texttt{rewrite} tactic takes an equality proof as an argument, and tries to rewritethe 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}\noindentAgain, 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 inputsand 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 definedempty1 : _|_empty1 = hd [] where    hd : List a -> a    hd (x :: xs) = x-- not terminatingempty2 : _|_empty2 = empty2\end{SaveVerbatim}\useverb{empties} \noindentInternally, \Idris{} checks every definition for totality, and we can check at the promptwith the \texttt{:total} command. We see that neither of the above definitions is total:\begin{SaveVerbatim}{totalcheck}*theorems> :total empty1possibly not total due to: empty1#hd not total as there are missing cases*theorems> :total empty2possibly not total as it is not well founded\end{SaveVerbatim}\useverb{totalcheck} \noindentNote the use of the word possibly'' --- a totality check can, of course, never be certaindue 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 astotal so that it will be a compile time error for the totality check to fail:\begin{SaveVerbatim}{emptyfail}total empty2 : _|_empty2 = empty2Type checking ./theorems.idrtheorems.idr:25:empty2 is possibly not total as it is not well founded\end{SaveVerbatim}\useverb{emptyfail} \noindentReassuringly, our proof in Section \ref{sect:empty} that the zero and successor constructorsare disjoint is total:\begin{SaveVerbatim}{totdisjoint}*theorems> :total disjointTotal\end{SaveVerbatim}\useverb{totdisjoint} \noindentThe 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}\noindentThe last of these conditions may be relaxed in the future.
Something went wrong with that request. Please try again.