doc-src/TutorialI/fp.tex
changeset 11428 332347b9b942
parent 11419 9577530e8a5a
child 11450 1b02a6c4032f
equal deleted inserted replaced
11427:3ed58bbcf4bd 11428:332347b9b942
   124   automatically loads all the required parent theories from their respective
   124   automatically loads all the required parent theories from their respective
   125   files (which may take a moment, unless the theories are already loaded and
   125   files (which may take a moment, unless the theories are already loaded and
   126   the files have not been modified).
   126   the files have not been modified).
   127   
   127   
   128   If you suddenly discover that you need to modify a parent theory of your
   128   If you suddenly discover that you need to modify a parent theory of your
   129   current theory, you must first abandon your current theory\indexbold{abandon
   129   current theory, you must first abandon your current theory%
   130   theory}\indexbold{theory!abandon} (at the shell
   130   \indexbold{abandoning a theory}\indexbold{theories!abandoning} 
       
   131   (at the shell
   131   level type \commdx{kill}). After the parent theory has
   132   level type \commdx{kill}). After the parent theory has
   132   been modified, you go back to your original theory. When its first line
   133   been modified, you go back to your original theory. When its first line
   133   \isa{\isacommand{theory}~T~=~\dots~:} is processed, the
   134   \isa{\isacommand{theory}~T~=~\dots~:} is processed, the
   134   modified parent is reloaded automatically.
   135   modified parent is reloaded automatically.
   135   
   136   
   152 lists, before we turn to datatypes in general. The section closes with a
   153 lists, before we turn to datatypes in general. The section closes with a
   153 case study.
   154 case study.
   154 
   155 
   155 
   156 
   156 \subsection{Lists}
   157 \subsection{Lists}
   157 \indexbold{*list}
   158 
   158 
   159 \index{*list (type)}%
   159 Lists are one of the essential datatypes in computing. Readers of this
   160 Lists are one of the essential datatypes in computing. Readers of this
   160 tutorial and users of HOL need to be familiar with their basic operations.
   161 tutorial and users of HOL need to be familiar with their basic operations.
   161 Theory \isa{ToyList} is only a small fragment of HOL's predefined theory
   162 Theory \isa{ToyList} is only a small fragment of HOL's predefined theory
   162 \isa{List}\footnote{\url{http://isabelle.in.tum.de/library/HOL/List.html}}.
   163 \thydx{List}\footnote{\url{http://isabelle.in.tum.de/library/HOL/List.html}}.
   163 The latter contains many further operations. For example, the functions
   164 The latter contains many further operations. For example, the functions
   164 \cdx{hd} (``head'') and \cdx{tl} (``tail'') return the first
   165 \cdx{hd} (``head'') and \cdx{tl} (``tail'') return the first
   165 element and the remainder of a list. (However, pattern-matching is usually
   166 element and the remainder of a list. (However, pattern-matching is usually
   166 preferable to \isa{hd} and \isa{tl}.)  
   167 preferable to \isa{hd} and \isa{tl}.)  
   167 Also available are higher-order functions like \isa{map} and \isa{filter}.
   168 Also available are higher-order functions like \isa{map} and \isa{filter}.
   203 
   204 
   204 
   205 
   205 \subsection{Primitive Recursion}
   206 \subsection{Primitive Recursion}
   206 
   207 
   207 Functions on datatypes are usually defined by recursion. In fact, most of the
   208 Functions on datatypes are usually defined by recursion. In fact, most of the
   208 time they are defined by what is called \bfindex{primitive recursion}.
   209 time they are defined by what is called \textbf{primitive recursion}.
   209 The keyword \isacommand{primrec}\indexbold{*primrec} is followed by a list of
   210 The keyword \commdx{primrec} is followed by a list of
   210 equations
   211 equations
   211 \[ f \, x@1 \, \dots \, (C \, y@1 \, \dots \, y@k)\, \dots \, x@n = r \]
   212 \[ f \, x@1 \, \dots \, (C \, y@1 \, \dots \, y@k)\, \dots \, x@n = r \]
   212 such that $C$ is a constructor of the datatype $t$ and all recursive calls of
   213 such that $C$ is a constructor of the datatype $t$ and all recursive calls of
   213 $f$ in $r$ are of the form $f \, \dots \, y@i \, \dots$ for some $i$. Thus
   214 $f$ in $r$ are of the form $f \, \dots \, y@i \, \dots$ for some $i$. Thus
   214 Isabelle immediately sees that $f$ terminates because one (fixed!) argument
   215 Isabelle immediately sees that $f$ terminates because one (fixed!) argument
   228 \section{Some Basic Types}
   229 \section{Some Basic Types}
   229 
   230 
   230 
   231 
   231 \subsection{Natural Numbers}
   232 \subsection{Natural Numbers}
   232 \label{sec:nat}
   233 \label{sec:nat}
   233 \index{arithmetic|(}
   234 \index{linear arithmetic|(}
   234 
   235 
   235 \input{Misc/document/fakenat.tex}
   236 \input{Misc/document/fakenat.tex}
   236 \input{Misc/document/natsum.tex}
   237 \input{Misc/document/natsum.tex}
   237 
   238 
   238 \index{arithmetic|)}
   239 \index{linear arithmetic|)}
   239 
   240 
   240 
   241 
   241 \subsection{Pairs}
   242 \subsection{Pairs}
   242 \input{Misc/document/pairs.tex}
   243 \input{Misc/document/pairs.tex}
   243 
   244 
   254 called type synonyms, those on the term level are called (constant)
   255 called type synonyms, those on the term level are called (constant)
   255 definitions.
   256 definitions.
   256 
   257 
   257 
   258 
   258 \subsection{Type Synonyms}
   259 \subsection{Type Synonyms}
   259 \indexbold{type synonym}
   260 
   260 
   261 \indexbold{type synonyms|(}%
   261 Type synonyms are similar to those found in ML\@. Their syntax is fairly self
   262 Type synonyms are similar to those found in ML\@. They are issued by a 
   262 explanatory:
   263 \commdx{types} command:
   263 
   264 
   264 \input{Misc/document/types.tex}%
   265 \input{Misc/document/types.tex}%
   265 
   266 
   266 Note that pattern-matching is not allowed, i.e.\ each definition must be of
   267 Note that pattern-matching is not allowed, i.e.\ each definition must be of
   267 the form $f\,x@1\,\dots\,x@n~\isasymequiv~t$.
   268 the form $f\,x@1\,\dots\,x@n~\isasymequiv~t$.
   268 Section~{\S}\ref{sec:Simp-with-Defs} explains how definitions are used
   269 Section~{\S}\ref{sec:Simp-with-Defs} explains how definitions are used
   269 in proofs.
   270 in proofs.%
       
   271 \indexbold{type synonyms|)}
   270 
   272 
   271 \input{Misc/document/prime_def.tex}
   273 \input{Misc/document/prime_def.tex}
   272 
   274 
   273 \input{Misc/document/Translations.tex}
   275 \input{Misc/document/Translations.tex}
   274 
   276 
   357 \input{CodeGen/document/CodeGen.tex}
   359 \input{CodeGen/document/CodeGen.tex}
   358 
   360 
   359 
   361 
   360 \section{Advanced Datatypes}
   362 \section{Advanced Datatypes}
   361 \label{sec:advanced-datatypes}
   363 \label{sec:advanced-datatypes}
   362 \index{*datatype|(}
   364 \index{datatype@\isacommand {datatype} (command)|(}
   363 \index{*primrec|(}
   365 \index{primrec@\isacommand {primrec} (command)|(}
   364 %|)
   366 %|)
   365 
   367 
   366 This section presents advanced forms of \isacommand{datatype}s.
   368 This section presents advanced forms of datatypes: mutual and nested
       
   369 recursion.  A series of examples will culminate in a treatment of the trie
       
   370 data structure.
       
   371 
   367 
   372 
   368 \subsection{Mutual Recursion}
   373 \subsection{Mutual Recursion}
   369 \label{sec:datatype-mut-rec}
   374 \label{sec:datatype-mut-rec}
   370 
   375 
   371 \input{Datatype/document/ABexpr.tex}
   376 \input{Datatype/document/ABexpr.tex}
   410 do indeed make sense~\cite{paulson87}.  Note the different arrow,
   415 do indeed make sense~\cite{paulson87}.  Note the different arrow,
   411 \isa{\isasymrightarrow} instead of \isa{\isasymRightarrow},
   416 \isa{\isasymrightarrow} instead of \isa{\isasymRightarrow},
   412 expressing the type of \emph{continuous} functions. 
   417 expressing the type of \emph{continuous} functions. 
   413 There is even a version of LCF on top of HOL,
   418 There is even a version of LCF on top of HOL,
   414 called HOLCF~\cite{MuellerNvOS99}.
   419 called HOLCF~\cite{MuellerNvOS99}.
   415 
   420 \index{datatype@\isacommand {datatype} (command)|)}
   416 \index{*primrec|)}
   421 \index{primrec@\isacommand {primrec} (command)|)}
   417 \index{*datatype|)}
   422 
   418 
   423 
   419 \subsection{Case Study: Tries}
   424 \subsection{Case Study: Tries}
   420 \label{sec:Trie}
   425 \label{sec:Trie}
   421 
   426 
   422 Tries are a classic search tree data structure~\cite{Knuth3-75} for fast
   427 Tries are a classic search tree data structure~\cite{Knuth3-75} for fast
   470 of the predefined datatype \isa{option} (see {\S}\ref{sec:option}).
   475 of the predefined datatype \isa{option} (see {\S}\ref{sec:option}).
   471 \input{Trie/document/Trie.tex}
   476 \input{Trie/document/Trie.tex}
   472 
   477 
   473 \section{Total Recursive Functions}
   478 \section{Total Recursive Functions}
   474 \label{sec:recdef}
   479 \label{sec:recdef}
   475 \index{*recdef|(}
   480 \index{recdef@\isacommand {recdef} (command)|(}\index{functions!total|(}
   476 
   481 
   477 Although many total functions have a natural primitive recursive definition,
   482 Although many total functions have a natural primitive recursive definition,
   478 this is not always the case. Arbitrary total recursive functions can be
   483 this is not always the case. Arbitrary total recursive functions can be
   479 defined by means of \isacommand{recdef}: you can use full pattern-matching,
   484 defined by means of \isacommand{recdef}: you can use full pattern-matching,
   480 recursion need not involve datatypes, and termination is proved by showing
   485 recursion need not involve datatypes, and termination is proved by showing
   502 \input{Recdef/document/Induction.tex}
   507 \input{Recdef/document/Induction.tex}
   503 \label{sec:recdef-induction}
   508 \label{sec:recdef-induction}
   504 
   509 
   505 \index{induction!recursion|)}
   510 \index{induction!recursion|)}
   506 \index{recursion induction|)}
   511 \index{recursion induction|)}
   507 \index{*recdef|)}
   512 \index{recdef@\isacommand {recdef} (command)|)}\index{functions!total|)}