14500
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
|
3 |
\def\isabellecontext{a{\isadigit{2}}}%
|
|
4 |
\isamarkupfalse%
|
|
5 |
%
|
|
6 |
\isamarkupsubsection{Folding Lists and Trees%
|
|
7 |
}
|
|
8 |
\isamarkuptrue%
|
|
9 |
%
|
|
10 |
\isamarkupsubsubsection{Some more list functions%
|
|
11 |
}
|
|
12 |
\isamarkuptrue%
|
|
13 |
%
|
|
14 |
\begin{isamarkuptext}%
|
|
15 |
Recall the summation function%
|
|
16 |
\end{isamarkuptext}%
|
|
17 |
\isamarkuptrue%
|
|
18 |
\ \ sum\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat\ list\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
|
|
19 |
\isamarkupfalse%
|
|
20 |
\isacommand{primrec}\isanewline
|
|
21 |
\ \ {\isachardoublequote}sum\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequote}\isanewline
|
|
22 |
\ \ {\isachardoublequote}sum\ {\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}\ {\isacharequal}\ x\ {\isacharplus}\ sum\ xs{\isachardoublequote}\isamarkupfalse%
|
|
23 |
%
|
|
24 |
\begin{isamarkuptext}%
|
|
25 |
In the Isabelle library, you will find in theory
|
|
26 |
\texttt{List.thy} the functions \isa{foldr} and \isa{foldl}, which
|
|
27 |
allow you to define some list functions, among them \isa{sum} and
|
|
28 |
\isa{length}. Show the following:%
|
|
29 |
\end{isamarkuptext}%
|
|
30 |
\isamarkuptrue%
|
|
31 |
\isacommand{lemma}\ sum{\isacharunderscore}foldr{\isacharcolon}\ {\isachardoublequote}sum\ xs\ {\isacharequal}\ foldr\ {\isacharparenleft}op\ {\isacharplus}{\isacharparenright}\ xs\ {\isadigit{0}}{\isachardoublequote}\isamarkupfalse%
|
|
32 |
\isanewline
|
|
33 |
\isamarkupfalse%
|
|
34 |
\isacommand{lemma}\ length{\isacharunderscore}foldr{\isacharcolon}\ {\isachardoublequote}length\ xs\ {\isacharequal}\ foldr\ {\isacharparenleft}{\isasymlambda}\ x\ res{\isachardot}\ {\isadigit{1}}\ {\isacharplus}\ res{\isacharparenright}\ xs\ {\isadigit{0}}{\isachardoublequote}\isamarkupfalse%
|
|
35 |
\isamarkupfalse%
|
|
36 |
%
|
|
37 |
\begin{isamarkuptext}%
|
|
38 |
Repeated application of \isa{foldr} and \isa{map} has the
|
|
39 |
disadvantage that a list is traversed several times. A single traversal is sufficient, as
|
|
40 |
illustrated by the following example:%
|
|
41 |
\end{isamarkuptext}%
|
|
42 |
\isamarkuptrue%
|
|
43 |
\isacommand{lemma}\ {\isachardoublequote}sum\ {\isacharparenleft}map\ {\isacharparenleft}{\isasymlambda}\ x{\isachardot}\ x\ {\isacharplus}\ {\isadigit{3}}{\isacharparenright}\ xs{\isacharparenright}\ {\isacharequal}\ foldr\ h\ xs\ b{\isachardoublequote}\isamarkupfalse%
|
|
44 |
\isamarkupfalse%
|
|
45 |
%
|
|
46 |
\begin{isamarkuptext}%
|
|
47 |
Find terms \isa{h} and \isa{b} which solve this
|
|
48 |
equation. Generalize this result, i.e. show for appropriate \isa{h}
|
|
49 |
and \isa{b}:%
|
|
50 |
\end{isamarkuptext}%
|
|
51 |
\isamarkuptrue%
|
|
52 |
\isacommand{lemma}\ {\isachardoublequote}foldr\ g\ {\isacharparenleft}map\ f\ xs{\isacharparenright}\ a\ {\isacharequal}\ foldr\ h\ xs\ b{\isachardoublequote}\isamarkupfalse%
|
|
53 |
\isamarkupfalse%
|
|
54 |
%
|
|
55 |
\begin{isamarkuptext}%
|
|
56 |
Hint: Isabelle can help you find the solution if you use the
|
|
57 |
equalities arising during a proof attempt.%
|
|
58 |
\end{isamarkuptext}%
|
|
59 |
\isamarkuptrue%
|
|
60 |
%
|
|
61 |
\begin{isamarkuptext}%
|
|
62 |
The following function \isa{rev{\isacharunderscore}acc} reverses a list in linear time:%
|
|
63 |
\end{isamarkuptext}%
|
|
64 |
\isamarkuptrue%
|
|
65 |
\isacommand{consts}\isanewline
|
|
66 |
\ \ rev{\isacharunderscore}acc\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharbrackleft}{\isacharprime}a\ list{\isacharcomma}\ {\isacharprime}a\ list{\isacharbrackright}\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequote}\isanewline
|
|
67 |
\isamarkupfalse%
|
|
68 |
\isacommand{primrec}\isanewline
|
|
69 |
\ \ {\isachardoublequote}rev{\isacharunderscore}acc\ {\isacharbrackleft}{\isacharbrackright}\ ys\ {\isacharequal}\ ys{\isachardoublequote}\isanewline
|
|
70 |
\ \ {\isachardoublequote}rev{\isacharunderscore}acc\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ ys\ {\isacharequal}\ {\isacharparenleft}rev{\isacharunderscore}acc\ xs\ {\isacharparenleft}x{\isacharhash}ys{\isacharparenright}{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
|
|
71 |
%
|
|
72 |
\begin{isamarkuptext}%
|
|
73 |
Show that \isa{rev{\isacharunderscore}acc} can be defined by means of \isa{foldl}.%
|
|
74 |
\end{isamarkuptext}%
|
|
75 |
\isamarkuptrue%
|
|
76 |
\isacommand{lemma}\ rev{\isacharunderscore}acc{\isacharunderscore}foldl{\isacharcolon}\ {\isachardoublequote}rev{\isacharunderscore}acc\ xs\ a\ {\isacharequal}\ foldl\ {\isacharparenleft}{\isasymlambda}\ ys\ x{\isachardot}\ x\ {\isacharhash}\ ys{\isacharparenright}\ a\ xs{\isachardoublequote}\isamarkupfalse%
|
|
77 |
\isamarkupfalse%
|
|
78 |
%
|
|
79 |
\begin{isamarkuptext}%
|
|
80 |
On the first exercise sheet, we have shown:%
|
|
81 |
\end{isamarkuptext}%
|
|
82 |
\isamarkuptrue%
|
|
83 |
\isacommand{lemma}\ sum{\isacharunderscore}append\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}sum\ {\isacharparenleft}xs\ {\isacharat}\ ys{\isacharparenright}\ {\isacharequal}\ sum\ xs\ {\isacharplus}\ sum\ ys{\isachardoublequote}\isanewline
|
|
84 |
\ \ \isamarkupfalse%
|
|
85 |
\isacommand{by}\ {\isacharparenleft}induct\ xs{\isacharparenright}\ auto\isamarkupfalse%
|
|
86 |
%
|
|
87 |
\begin{isamarkuptext}%
|
|
88 |
Prove a similar distributivity property for \isa{foldr},
|
|
89 |
i.e. something like \isa{foldr\ f\ {\isacharparenleft}xs\ {\isacharat}\ ys{\isacharparenright}\ a\ {\isacharequal}\ f\ {\isacharparenleft}foldr\ f\ xs\ a{\isacharparenright}\ {\isacharparenleft}foldr\ f\ ys\ a{\isacharparenright}}. However, you will have to strengthen the premisses
|
|
90 |
by taking into account algebraic properties of \isa{f} and \isa{a}.%
|
|
91 |
\end{isamarkuptext}%
|
|
92 |
\isamarkuptrue%
|
|
93 |
\isacommand{lemma}\ foldr{\isacharunderscore}append{\isacharcolon}\ {\isachardoublequote}foldr\ f\ {\isacharparenleft}xs\ {\isacharat}\ ys{\isacharparenright}\ a\ {\isacharequal}\ f\ {\isacharparenleft}foldr\ f\ xs\ a{\isacharparenright}\ {\isacharparenleft}foldr\ f\ ys\ a{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
|
|
94 |
\isamarkupfalse%
|
|
95 |
%
|
|
96 |
\begin{isamarkuptext}%
|
|
97 |
Now, define the function \isa{prod}, which computes the product of all list elements:%
|
|
98 |
\end{isamarkuptext}%
|
|
99 |
\isamarkuptrue%
|
|
100 |
\ \ prod\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat\ list\ {\isasymRightarrow}\ nat{\isachardoublequote}\isamarkupfalse%
|
|
101 |
%
|
|
102 |
\begin{isamarkuptext}%
|
|
103 |
direcly with the aid of a fold and prove the following:%
|
|
104 |
\end{isamarkuptext}%
|
|
105 |
\isamarkuptrue%
|
|
106 |
\isacommand{lemma}\ {\isachardoublequote}prod\ {\isacharparenleft}xs\ {\isacharat}\ ys{\isacharparenright}\ {\isacharequal}\ prod\ xs\ {\isacharasterisk}\ prod\ ys{\isachardoublequote}\isamarkupfalse%
|
|
107 |
\isamarkupfalse%
|
|
108 |
%
|
|
109 |
\isamarkupsubsubsection{Functions on Trees%
|
|
110 |
}
|
|
111 |
\isamarkuptrue%
|
|
112 |
%
|
|
113 |
\begin{isamarkuptext}%
|
|
114 |
Consider the following type of binary trees:%
|
|
115 |
\end{isamarkuptext}%
|
|
116 |
\isamarkuptrue%
|
|
117 |
\isacommand{datatype}\ {\isacharprime}a\ tree\ {\isacharequal}\ Tip\ {\isacharbar}\ Node\ {\isachardoublequote}{\isacharprime}a\ tree{\isachardoublequote}\ {\isacharprime}a\ {\isachardoublequote}{\isacharprime}a\ tree{\isachardoublequote}\isamarkupfalse%
|
|
118 |
%
|
|
119 |
\begin{isamarkuptext}%
|
|
120 |
Define functions which convert a tree into a list by traversing it in pre- resp. postorder:%
|
|
121 |
\end{isamarkuptext}%
|
|
122 |
\isamarkuptrue%
|
|
123 |
\ \ preorder\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ tree\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequote}\isanewline
|
|
124 |
\ \ postorder\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ tree\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequote}\isamarkupfalse%
|
|
125 |
%
|
|
126 |
\begin{isamarkuptext}%
|
|
127 |
You have certainly realized that computation of postorder traversal can be efficiently realized with an accumulator, in analogy to \isa{rev{\isacharunderscore}acc}:%
|
|
128 |
\end{isamarkuptext}%
|
|
129 |
\isamarkuptrue%
|
|
130 |
\isacommand{consts}\isanewline
|
|
131 |
\ \ postorder{\isacharunderscore}acc\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharbrackleft}{\isacharprime}a\ tree{\isacharcomma}\ {\isacharprime}a\ list{\isacharbrackright}\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequote}\isamarkupfalse%
|
|
132 |
%
|
|
133 |
\begin{isamarkuptext}%
|
|
134 |
Define this function and show:%
|
|
135 |
\end{isamarkuptext}%
|
|
136 |
\isamarkuptrue%
|
|
137 |
\isacommand{lemma}\ {\isachardoublequote}postorder{\isacharunderscore}acc\ t\ xs\ {\isacharequal}\ {\isacharparenleft}postorder\ t{\isacharparenright}\ {\isacharat}\ xs{\isachardoublequote}\isamarkupfalse%
|
|
138 |
\isamarkupfalse%
|
|
139 |
%
|
|
140 |
\begin{isamarkuptext}%
|
|
141 |
\isa{postorder{\isacharunderscore}acc} is the instance of a function
|
|
142 |
\isa{foldl{\isacharunderscore}tree}, which is similar to \isa{foldl}.%
|
|
143 |
\end{isamarkuptext}%
|
|
144 |
\isamarkuptrue%
|
|
145 |
\isacommand{consts}\isanewline
|
|
146 |
\ \ foldl{\isacharunderscore}tree\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}b\ {\isacharequal}{\isachargreater}\ {\isacharprime}a\ {\isacharequal}{\isachargreater}\ {\isacharprime}b{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}b\ {\isasymRightarrow}\ {\isacharprime}a\ tree\ {\isasymRightarrow}\ {\isacharprime}b{\isachardoublequote}\isamarkupfalse%
|
|
147 |
%
|
|
148 |
\begin{isamarkuptext}%
|
|
149 |
Show the following:%
|
|
150 |
\end{isamarkuptext}%
|
|
151 |
\isamarkuptrue%
|
|
152 |
\isacommand{lemma}\ {\isachardoublequote}{\isasymforall}\ a{\isachardot}\ postorder{\isacharunderscore}acc\ t\ a\ {\isacharequal}\ foldl{\isacharunderscore}tree\ {\isacharparenleft}{\isasymlambda}\ xs\ x{\isachardot}\ Cons\ x\ xs{\isacharparenright}\ a\ t{\isachardoublequote}\isamarkupfalse%
|
|
153 |
\isamarkupfalse%
|
|
154 |
%
|
|
155 |
\begin{isamarkuptext}%
|
|
156 |
Define a function \isa{tree{\isacharunderscore}sum} that computes the sum of
|
|
157 |
the elements of a tree of natural numbers:%
|
|
158 |
\end{isamarkuptext}%
|
|
159 |
\isamarkuptrue%
|
|
160 |
\isacommand{consts}\isanewline
|
|
161 |
\ \ tree{\isacharunderscore}sum\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat\ tree\ {\isasymRightarrow}\ nat{\isachardoublequote}\isamarkupfalse%
|
|
162 |
%
|
|
163 |
\begin{isamarkuptext}%
|
|
164 |
and show that this function satisfies%
|
|
165 |
\end{isamarkuptext}%
|
|
166 |
\isamarkuptrue%
|
|
167 |
\isacommand{lemma}\ {\isachardoublequote}tree{\isacharunderscore}sum\ t\ {\isacharequal}\ sum\ {\isacharparenleft}preorder\ t{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
|
|
168 |
\isanewline
|
|
169 |
\isamarkupfalse%
|
|
170 |
\isanewline
|
|
171 |
\isamarkupfalse%
|
|
172 |
\end{isabellebody}%
|
|
173 |
%%% Local Variables:
|
|
174 |
%%% mode: latex
|
|
175 |
%%% TeX-master: "root"
|
|
176 |
%%% End:
|