doc-src/TutorialI/fp.tex
changeset 9644 6b0b6b471855
parent 9541 d17c0b34d5c8
child 9673 1b2d4f995b13
equal deleted inserted replaced
9643:c94db1a96f4e 9644:6b0b6b471855
   183 Laws about datatypes, such as \isa{[] \isasymnoteq~x\#xs} and
   183 Laws about datatypes, such as \isa{[] \isasymnoteq~x\#xs} and
   184 \isa{(x\#xs = y\#ys) = (x=y \isasymand~xs=ys)}, are used automatically
   184 \isa{(x\#xs = y\#ys) = (x=y \isasymand~xs=ys)}, are used automatically
   185 during proofs by simplification.  The same is true for the equations in
   185 during proofs by simplification.  The same is true for the equations in
   186 primitive recursive function definitions.
   186 primitive recursive function definitions.
   187 
   187 
       
   188 Every datatype $t$ comes equipped with a \isa{size} function from $t$ into
       
   189 the natural numbers (see~\S\ref{sec:nat} below). For lists, \isa{size} is
       
   190 just the length, i.e.\ \isa{size [] = 0} and \isa{size(x \# xs) = size xs +
       
   191   1}.  In general, \isa{size} returns \isa{0} for all constructors that do
       
   192 not have an argument of type $t$, and for all other constructors \isa{1 +}
       
   193 the sum of the sizes of all arguments of type $t$. Note that because
       
   194 \isa{size} is defined on every datatype, it is overloaded; on lists
       
   195 \isa{size} is also called \isa{length}, which is not overloaded.
       
   196 
       
   197 
   188 \subsection{Primitive recursion}
   198 \subsection{Primitive recursion}
   189 
   199 
   190 Functions on datatypes are usually defined by recursion. In fact, most of the
   200 Functions on datatypes are usually defined by recursion. In fact, most of the
   191 time they are defined by what is called \bfindex{primitive recursion}.
   201 time they are defined by what is called \bfindex{primitive recursion}.
   192 The keyword \isacommand{primrec}\indexbold{*primrec} is followed by a list of
   202 The keyword \isacommand{primrec}\indexbold{*primrec} is followed by a list of
   265 Note that we do not need to give a lemma a name if we do not intend to refer
   275 Note that we do not need to give a lemma a name if we do not intend to refer
   266 to it explicitly in the future.
   276 to it explicitly in the future.
   267 
   277 
   268 \begin{warn}
   278 \begin{warn}
   269   Induction is only allowed on free (or \isasymAnd-bound) variables that
   279   Induction is only allowed on free (or \isasymAnd-bound) variables that
   270   should not occur among the assumptions of the subgoal.  Case distinction
   280   should not occur among the assumptions of the subgoal; see
       
   281   \S\ref{sec:ind-var-in-prems} for details. Case distinction
   271   (\isa{case_tac}) works for arbitrary terms, which need to be
   282   (\isa{case_tac}) works for arbitrary terms, which need to be
   272   quoted if they are non-atomic.
   283   quoted if they are non-atomic.
   273 \end{warn}
   284 \end{warn}
   274 
   285 
   275 
   286 
   299 
   310 
   300 \section{Some basic types}
   311 \section{Some basic types}
   301 
   312 
   302 
   313 
   303 \subsection{Natural numbers}
   314 \subsection{Natural numbers}
       
   315 \label{sec:nat}
   304 \index{arithmetic|(}
   316 \index{arithmetic|(}
   305 
   317 
   306 \input{Misc/document/fakenat.tex}
   318 \input{Misc/document/fakenat.tex}
   307 \input{Misc/document/natsum.tex}
   319 \input{Misc/document/natsum.tex}
   308 
   320 
   692 \label{sec:datatype-mut-rec}
   704 \label{sec:datatype-mut-rec}
   693 
   705 
   694 \input{Datatype/document/ABexpr.tex}
   706 \input{Datatype/document/ABexpr.tex}
   695 
   707 
   696 \subsection{Nested recursion}
   708 \subsection{Nested recursion}
   697 
   709 \label{sec:nested-datatype}
   698 \input{Datatype/document/Nested.tex}
   710 
       
   711 {\makeatother\input{Datatype/document/Nested.tex}}
   699 
   712 
   700 
   713 
   701 \subsection{The limits of nested recursion}
   714 \subsection{The limits of nested recursion}
   702 
   715 
   703 How far can we push nested recursion? By the unfolding argument above, we can
   716 How far can we push nested recursion? By the unfolding argument above, we can
   826 
   839 
   827 \subsection{Simplification with recdef}
   840 \subsection{Simplification with recdef}
   828 
   841 
   829 \input{Recdef/document/simplification.tex}
   842 \input{Recdef/document/simplification.tex}
   830 
   843 
   831 
       
   832 \subsection{Induction}
   844 \subsection{Induction}
   833 \index{induction!recursion|(}
   845 \index{induction!recursion|(}
   834 \index{recursion induction|(}
   846 \index{recursion induction|(}
   835 
   847 
   836 \input{Recdef/document/Induction.tex}
   848 \input{Recdef/document/Induction.tex}
       
   849 \label{sec:recdef-induction}
   837 
   850 
   838 \index{induction!recursion|)}
   851 \index{induction!recursion|)}
   839 \index{recursion induction|)}
   852 \index{recursion induction|)}
       
   853 
       
   854 %\subsection{Advanced forms of recursion}
       
   855 
       
   856 %\input{Recdef/document/Nested0.tex}
       
   857 %\input{Recdef/document/Nested1.tex}
       
   858 
   840 \index{*recdef|)}
   859 \index{*recdef|)}
       
   860 
       
   861 \section{Advanced induction techniques}
       
   862 \label{sec:advanced-ind}
       
   863 \input{Misc/document/AdvancedInd.tex}
       
   864 
       
   865 \input{Datatype/document/Nested2.tex}