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