167 comes equipped with a \isa{size} function from $t$ into the natural numbers |
167 comes equipped with a \isa{size} function from $t$ into the natural numbers |
168 (see~{\S}\ref{sec:nat} below). For lists, \isa{size} is just the length, i.e.\ |
168 (see~{\S}\ref{sec:nat} below). For lists, \isa{size} is just the length, i.e.\ |
169 \isa{size [] = 0} and \isa{size(x \# xs) = size xs + 1}. In general, |
169 \isa{size [] = 0} and \isa{size(x \# xs) = size xs + 1}. In general, |
170 \cdx{size} returns |
170 \cdx{size} returns |
171 \begin{itemize} |
171 \begin{itemize} |
172 \item zero for all constructors |
172 \item zero for all constructors that do not have an argument of type $t$, |
173 that do not have an argument of type $t$\\ |
|
174 \item one plus the sum of the sizes of all arguments of type~$t$, |
173 \item one plus the sum of the sizes of all arguments of type~$t$, |
175 for all other constructors |
174 for all other constructors. |
176 \end{itemize} |
175 \end{itemize} |
177 Note that because |
176 Note that because |
178 \isa{size} is defined on every datatype, it is overloaded; on lists |
177 \isa{size} is defined on every datatype, it is overloaded; on lists |
179 \isa{size} is also called \sdx{length}, which is not overloaded. |
178 \isa{size} is also called \sdx{length}, which is not overloaded. |
180 Isabelle will always show \isa{size} on lists as \isa{length}. |
179 Isabelle will always show \isa{size} on lists as \isa{length}. |
182 |
181 |
183 \subsection{Primitive Recursion} |
182 \subsection{Primitive Recursion} |
184 |
183 |
185 \index{recursion!primitive}% |
184 \index{recursion!primitive}% |
186 Functions on datatypes are usually defined by recursion. In fact, most of the |
185 Functions on datatypes are usually defined by recursion. In fact, most of the |
187 time they are defined by what is called \textbf{primitive recursion}. |
186 time they are defined by what is called \textbf{primitive recursion} over some |
188 The keyword \commdx{primrec} is followed by a list of |
187 datatype $t$. This means that the recursion equations must be of the form |
189 equations |
|
190 \[ f \, x@1 \, \dots \, (C \, y@1 \, \dots \, y@k)\, \dots \, x@n = r \] |
188 \[ f \, x@1 \, \dots \, (C \, y@1 \, \dots \, y@k)\, \dots \, x@n = r \] |
191 such that $C$ is a constructor of the datatype $t$ and all recursive calls of |
189 such that $C$ is a constructor of $t$ and all recursive calls of |
192 $f$ in $r$ are of the form $f \, \dots \, y@i \, \dots$ for some $i$. Thus |
190 $f$ in $r$ are of the form $f \, \dots \, y@i \, \dots$ for some $i$. Thus |
193 Isabelle immediately sees that $f$ terminates because one (fixed!) argument |
191 Isabelle immediately sees that $f$ terminates because one (fixed!) argument |
194 becomes smaller with every recursive call. There must be at most one equation |
192 becomes smaller with every recursive call. There must be at most one equation |
195 for each constructor. Their order is immaterial. |
193 for each constructor. Their order is immaterial. |
196 A more general method for defining total recursive functions is introduced in |
194 A more general method for defining total recursive functions is introduced in |