author | Walther Neuper <neuper@ist.tugraz.at> |
Thu, 12 Aug 2010 15:03:34 +0200 | |
branch | isac-from-Isabelle2009-2 |
changeset 37913 | 20e3616b2d9c |
parent 27015 | f8537d69f514 |
permissions | -rw-r--r-- |
nipkow@8745 | 1 |
(*<*) |
haftmann@16417 | 2 |
theory Tree imports Main begin |
nipkow@8745 | 3 |
(*>*) |
nipkow@8745 | 4 |
|
nipkow@8745 | 5 |
text{*\noindent |
paulson@11456 | 6 |
Define the datatype of \rmindex{binary trees}: |
nipkow@8745 | 7 |
*} |
nipkow@8745 | 8 |
|
nipkow@8745 | 9 |
datatype 'a tree = Tip | Node "'a tree" 'a "'a tree";(*<*) |
nipkow@8745 | 10 |
|
nipkow@27015 | 11 |
primrec mirror :: "'a tree \<Rightarrow> 'a tree" where |
nipkow@27015 | 12 |
"mirror Tip = Tip" | |
nipkow@9493 | 13 |
"mirror (Node l x r) = Node (mirror r) x (mirror l)";(*>*) |
nipkow@8745 | 14 |
|
nipkow@8745 | 15 |
text{*\noindent |
paulson@11456 | 16 |
Define a function @{term"mirror"} that mirrors a binary tree |
paulson@10795 | 17 |
by swapping subtrees recursively. Prove |
nipkow@8745 | 18 |
*} |
nipkow@8745 | 19 |
|
nipkow@8745 | 20 |
lemma mirror_mirror: "mirror(mirror t) = t"; |
nipkow@8745 | 21 |
(*<*) |
nipkow@8745 | 22 |
apply(induct_tac t); |
nipkow@9458 | 23 |
by(auto); |
nipkow@8745 | 24 |
|
nipkow@27015 | 25 |
primrec flatten :: "'a tree => 'a list" where |
nipkow@27015 | 26 |
"flatten Tip = []" | |
nipkow@9493 | 27 |
"flatten (Node l x r) = flatten l @ [x] @ flatten r"; |
nipkow@9493 | 28 |
(*>*) |
nipkow@9493 | 29 |
|
nipkow@9493 | 30 |
text{*\noindent |
nipkow@9792 | 31 |
Define a function @{term"flatten"} that flattens a tree into a list |
nipkow@9493 | 32 |
by traversing it in infix order. Prove |
nipkow@9493 | 33 |
*} |
nipkow@9493 | 34 |
|
nipkow@9493 | 35 |
lemma "flatten(mirror t) = rev(flatten t)"; |
nipkow@9493 | 36 |
(*<*) |
nipkow@9493 | 37 |
apply(induct_tac t); |
nipkow@9493 | 38 |
by(auto); |
nipkow@9493 | 39 |
|
nipkow@8745 | 40 |
end |
nipkow@8745 | 41 |
(*>*) |