3 \def\isabellecontext{Nested{\isadigit{2}}}%
6 \isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}t\ {\isasymin}\ set\ ts\ {\isasymlongrightarrow}\ size\ t\ {\isacharless}\ Suc{\isacharparenleft}term{\isacharunderscore}list{\isacharunderscore}size\ ts{\isacharparenright}{\isachardoublequote}\isanewline
8 \isacommand{by}{\isacharparenleft}induct{\isacharunderscore}tac\ ts{\isacharcomma}\ auto{\isacharparenright}\isamarkupfalse%
11 \begin{isamarkuptext}%
13 By making this theorem a simplification rule, \isacommand{recdef}
14 applies it automatically and the definition of \isa{trev}
15 succeeds now. As a reward for our effort, we can now prove the desired
16 lemma directly. We no longer need the verbose
17 induction schema for type \isa{term} and can use the simpler one arising from
21 \isacommand{lemma}\ {\isachardoublequote}trev{\isacharparenleft}trev\ t{\isacharparenright}\ {\isacharequal}\ t{\isachardoublequote}\isanewline
23 \isacommand{apply}{\isacharparenleft}induct{\isacharunderscore}tac\ t\ rule{\isacharcolon}\ trev{\isachardot}induct{\isacharparenright}\isamarkupfalse%
27 \ {\isadigit{1}}{\isachardot}\ {\isasymAnd}x{\isachardot}\ trev\ {\isacharparenleft}trev\ {\isacharparenleft}Var\ x{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ Var\ x\isanewline
28 \ {\isadigit{2}}{\isachardot}\ {\isasymAnd}f\ ts{\isachardot}\isanewline
29 \isaindent{\ {\isadigit{2}}{\isachardot}\ \ \ \ }{\isasymforall}x{\isachardot}\ x\ {\isasymin}\ set\ ts\ {\isasymlongrightarrow}\ trev\ {\isacharparenleft}trev\ x{\isacharparenright}\ {\isacharequal}\ x\ {\isasymLongrightarrow}\isanewline
30 \isaindent{\ {\isadigit{2}}{\isachardot}\ \ \ \ }trev\ {\isacharparenleft}trev\ {\isacharparenleft}App\ f\ ts{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ App\ f\ ts%
32 Both the base case and the induction step fall to simplification:%
35 \isacommand{by}{\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ rev{\isacharunderscore}map\ sym{\isacharbrackleft}OF\ map{\isacharunderscore}compose{\isacharbrackright}\ cong{\isacharcolon}\ map{\isacharunderscore}cong{\isacharparenright}\isamarkupfalse%
37 \begin{isamarkuptext}%
39 If the proof of the induction step mystifies you, we recommend that you go through
40 the chain of simplification steps in detail; you will probably need the help of
41 \isa{trace{\isacharunderscore}simp}. Theorem \isa{map{\isacharunderscore}cong} is discussed below.
43 %{term[display]"trev(trev(App f ts))"}\\
44 %{term[display]"App f (rev(map trev (rev(map trev ts))))"}\\
45 %{term[display]"App f (map trev (rev(rev(map trev ts))))"}\\
46 %{term[display]"App f (map trev (map trev ts))"}\\
47 %{term[display]"App f (map (trev o trev) ts)"}\\
48 %{term[display]"App f (map (%x. x) ts)"}\\
49 %{term[display]"App f ts"}
52 The definition of \isa{trev} above is superior to the one in
53 \S\ref{sec:nested-datatype} because it uses \isa{rev}
54 and lets us use existing facts such as \hbox{\isa{rev\ {\isacharparenleft}rev\ xs{\isacharparenright}\ {\isacharequal}\ xs}}.
55 Thus this proof is a good example of an important principle:
57 \emph{Chose your definitions carefully\\
58 because they determine the complexity of your proofs.}
61 Let us now return to the question of how \isacommand{recdef} can come up with
62 sensible termination conditions in the presence of higher-order functions
63 like \isa{map}. For a start, if nothing were known about \isa{map}, then
64 \isa{map\ trev\ ts} might apply \isa{trev} to arbitrary terms, and thus
65 \isacommand{recdef} would try to prove the unprovable \isa{size\ t\ {\isacharless}\ Suc\ {\isacharparenleft}term{\isacharunderscore}list{\isacharunderscore}size\ ts{\isacharparenright}}, without any assumption about \isa{t}. Therefore
66 \isacommand{recdef} has been supplied with the congruence theorem
67 \isa{map{\isacharunderscore}cong}:
69 \ \ \ \ \ {\isasymlbrakk}xs\ {\isacharequal}\ ys{\isacharsemicolon}\ {\isasymAnd}x{\isachardot}\ x\ {\isasymin}\ set\ ys\ {\isasymLongrightarrow}\ f\ x\ {\isacharequal}\ g\ x{\isasymrbrakk}\isanewline
70 \isaindent{\ \ \ \ \ }{\isasymLongrightarrow}\ map\ f\ xs\ {\isacharequal}\ map\ g\ ys%
72 Its second premise expresses that in \isa{map\ f\ xs},
73 function \isa{f} is only applied to elements of list \isa{xs}. Congruence
74 rules for other higher-order functions on lists are similar. If you get
75 into a situation where you need to supply \isacommand{recdef} with new
76 congruence rules, you can append a hint after the end of
77 the recursion equations:\cmmdx{hints}%
81 {\isacharparenleft}\isakeyword{hints}\ recdef{\isacharunderscore}cong{\isacharcolon}\ map{\isacharunderscore}cong{\isacharparenright}\isamarkupfalse%
83 \begin{isamarkuptext}%
85 Or you can declare them globally
86 by giving them the \attrdx{recdef_cong} attribute:%
89 \isacommand{declare}\ map{\isacharunderscore}cong{\isacharbrackleft}recdef{\isacharunderscore}cong{\isacharbrackright}\isamarkupfalse%
91 \begin{isamarkuptext}%
92 The \isa{cong} and \isa{recdef{\isacharunderscore}cong} attributes are
93 intentionally kept apart because they control different activities, namely
94 simplification and making recursive definitions.
95 %The simplifier's congruence rules cannot be used by recdef.
96 %For example the weak congruence rules for if and case would prevent
97 %recdef from generating sensible termination conditions.%
104 %%% TeX-master: "root"