doc-src/Exercises/2001/a2/Aufgabe2.thy
author wenzelm
Wed, 09 Jun 2004 18:52:42 +0200
changeset 14898 a25550451b51
parent 13739 f5d0a66c8124
permissions -rw-r--r--
Url.File;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13739
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     1
(*<*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     2
theory Aufgabe2 = Main:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     3
(*>*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     4
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     5
subsection {* Trees *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     6
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     7
text{* In the sequel we work with skeletons of binary trees where
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     8
neither the leaves (``tip'') nor the nodes contain any information: *}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
     9
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    10
datatype tree = Tp | Nd tree tree
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    11
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    12
text{* Define a function @{term tips} that counts the tips of a
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    13
tree, and a function @{term height} that computes the height of a
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    14
tree.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    15
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    16
Complete binary trees of a given height are generated as follows:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    17
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    18
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    19
consts cbt :: "nat \<Rightarrow> tree"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    20
primrec
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    21
"cbt 0 = Tp"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    22
"cbt(Suc n) = Nd (cbt n) (cbt n)"
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    23
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    24
text{*
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    25
We will now focus on these complete binary trees.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    26
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    27
Instead of generating complete binary trees, we can also \emph{test}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    28
if a binary tree is complete. Define a function @{term "iscbt f"}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    29
(where @{term f} is a function on trees) that checks for completeness:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    30
@{term Tp} is complete and @{term"Nd l r"} ist complete iff @{term l} and
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    31
@{term r} are complete and @{prop"f l = f r"}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    32
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    33
We now have 3 functions on trees, namely @{term tips}, @{term height}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    34
und @{term size}. The latter is defined automatically --- look it up
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    35
in the tutorial.  Thus we also have 3 kinds of completeness: complete
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    36
wrt.\ @{term tips}, complete wrt.\ @{term height} and complete wrt.\
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    37
@{term size}. Show that
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    38
\begin{itemize}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    39
\item the 3 notions are the same (e.g.\ @{prop"iscbt tips t = iscbt size t"}),
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    40
      and
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    41
\item the 3 notions describe exactly the trees generated by @{term cbt}:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    42
the result of @{term cbt} is complete (in the sense of @{term iscbt},
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    43
wrt.\ any function on trees), and if a tree is complete in the sense of
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    44
@{term iscbt}, it is the result of @{term cbt} (applied to a suitable number
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    45
--- which one?)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    46
\end{itemize}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    47
Find a function @{term f} such that @{prop"iscbt f"} is different from
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    48
@{term"iscbt size"}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    49
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    50
Hints:
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    51
\begin{itemize}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    52
\item Work out and prove suitable relationships between @{term tips},
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    53
      @{term height} und @{term size}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    54
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    55
\item If you need lemmas dealing only with the basic arithmetic operations
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    56
(@{text"+"}, @{text"*"}, @{text"^"} etc), you can ``prove'' them
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    57
with the command @{text sorry}, if neither @{text arith} nor you can
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    58
find a proof. Not @{text"apply sorry"}, just @{text sorry}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    59
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    60
\item
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    61
You do not need to show that every notion is equal to every other
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    62
notion.  It suffices to show that $A = C$ und $B = C$ --- $A = B$ is a
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    63
trivial consequence. However, the difficulty of the proof will depend
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    64
on which of the equivalences you prove.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    65
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    66
\item There is @{text"\<and>"} and @{text"\<longrightarrow>"}.
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    67
\end{itemize}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    68
*}
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    69
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    70
(*<*)
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    71
end;
f5d0a66c8124 exercise collection
kleing
parents:
diff changeset
    72
(*>*)