85 Define a function \isa{trev} of type \isa{{\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}b{\isacharparenright}\ term\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}b{\isacharparenright}\ term} |
85 Define a function \isa{trev} of type \isa{{\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}b{\isacharparenright}\ term\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}b{\isacharparenright}\ term} |
86 that recursively reverses the order of arguments of all function symbols in a |
86 that recursively reverses the order of arguments of all function symbols in a |
87 term. Prove that \isa{trev\ {\isacharparenleft}trev\ t{\isacharparenright}\ {\isacharequal}\ t}. |
87 term. Prove that \isa{trev\ {\isacharparenleft}trev\ t{\isacharparenright}\ {\isacharequal}\ t}. |
88 \end{exercise} |
88 \end{exercise} |
89 |
89 |
90 The experienced functional programmer may feel that our above definition of |
90 The experienced functional programmer may feel that our definition of |
91 \isa{subst} is unnecessarily complicated in that \isa{substs} is |
91 \isa{subst} is too complicated in that \isa{substs} is |
92 completely unnecessary. The \isa{App}-case can be defined directly as |
92 unnecessary. The \isa{App}-case can be defined directly as |
93 \begin{isabelle}% |
93 \begin{isabelle}% |
94 \ \ \ \ \ subst\ s\ {\isacharparenleft}App\ f\ ts{\isacharparenright}\ {\isacharequal}\ App\ f\ {\isacharparenleft}map\ {\isacharparenleft}subst\ s{\isacharparenright}\ ts{\isacharparenright}% |
94 \ \ \ \ \ subst\ s\ {\isacharparenleft}App\ f\ ts{\isacharparenright}\ {\isacharequal}\ App\ f\ {\isacharparenleft}map\ {\isacharparenleft}subst\ s{\isacharparenright}\ ts{\isacharparenright}% |
95 \end{isabelle} |
95 \end{isabelle} |
96 where \isa{map} is the standard list function such that |
96 where \isa{map} is the standard list function such that |
97 \isa{map\ f\ {\isacharbrackleft}x{\isadigit{1}}{\isacharcomma}{\isachardot}{\isachardot}{\isachardot}{\isacharcomma}xn{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}f\ x{\isadigit{1}}{\isacharcomma}{\isachardot}{\isachardot}{\isachardot}{\isacharcomma}f\ xn{\isacharbrackright}}. This is true, but Isabelle |
97 \isa{map\ f\ {\isacharbrackleft}x{\isadigit{1}}{\isacharcomma}{\isachardot}{\isachardot}{\isachardot}{\isacharcomma}xn{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}f\ x{\isadigit{1}}{\isacharcomma}{\isachardot}{\isachardot}{\isachardot}{\isacharcomma}f\ xn{\isacharbrackright}}. This is true, but Isabelle |
98 insists on the above fixed format. Fortunately, we can easily \emph{prove} |
98 insists on the conjunctive format. Fortunately, we can easily \emph{prove} |
99 that the suggested equation holds:% |
99 that the suggested equation holds:% |
100 \end{isamarkuptext}% |
100 \end{isamarkuptext}% |
101 \isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}subst\ s\ {\isacharparenleft}App\ f\ ts{\isacharparenright}\ {\isacharequal}\ App\ f\ {\isacharparenleft}map\ {\isacharparenleft}subst\ s{\isacharparenright}\ ts{\isacharparenright}{\isachardoublequote}\isanewline |
101 \isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}subst\ s\ {\isacharparenleft}App\ f\ ts{\isacharparenright}\ {\isacharequal}\ App\ f\ {\isacharparenleft}map\ {\isacharparenleft}subst\ s{\isacharparenright}\ ts{\isacharparenright}{\isachardoublequote}\isanewline |
102 \isacommand{apply}{\isacharparenleft}induct{\isacharunderscore}tac\ ts{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}\isanewline |
102 \isacommand{apply}{\isacharparenleft}induct{\isacharunderscore}tac\ ts{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}\isanewline |
103 \isacommand{done}% |
103 \isacommand{done}% |
112 The advantage is that now we have replaced \isa{substs} by |
112 The advantage is that now we have replaced \isa{substs} by |
113 \isa{map}, we can profit from the large number of pre-proved lemmas |
113 \isa{map}, we can profit from the large number of pre-proved lemmas |
114 about \isa{map}. Unfortunately inductive proofs about type |
114 about \isa{map}. Unfortunately inductive proofs about type |
115 \isa{term} are still awkward because they expect a conjunction. One |
115 \isa{term} are still awkward because they expect a conjunction. One |
116 could derive a new induction principle as well (see |
116 could derive a new induction principle as well (see |
117 \S\ref{sec:derive-ind}), but turns out to be simpler to define |
117 \S\ref{sec:derive-ind}), but simpler is to stop using \isacommand{primrec} |
118 functions by \isacommand{recdef} instead of \isacommand{primrec}. |
118 and to define functions with \isacommand{recdef} instead. |
119 The details are explained in \S\ref{sec:nested-recdef} below. |
119 The details are explained in \S\ref{sec:nested-recdef} below. |
120 |
120 |
121 Of course, you may also combine mutual and nested recursion. For example, |
121 Of course, you may also combine mutual and nested recursion. For example, |
122 constructor \isa{Sum} in \S\ref{sec:datatype-mut-rec} could take a list of |
122 constructor \isa{Sum} in \S\ref{sec:datatype-mut-rec} could take a list of |
123 expressions as its argument: \isa{Sum}~\isa{{\isachardoublequote}{\isacharprime}a\ aexp\ list{\isachardoublequote}}.% |
123 expressions as its argument: \isa{Sum}~\isa{{\isachardoublequote}{\isacharprime}a\ aexp\ list{\isachardoublequote}}.% |