doc-src/TutorialI/Misc/Tree.thy
changeset 9493 494f8cd34df7
parent 9458 c613cd06d5cf
child 9792 bbefb6ce5cb2
equal deleted inserted replaced
9492:72e429c66608 9493:494f8cd34df7
     9 datatype 'a tree = Tip | Node "'a tree" 'a "'a tree";(*<*)
     9 datatype 'a tree = Tip | Node "'a tree" 'a "'a tree";(*<*)
    10 
    10 
    11 consts mirror :: "'a tree \\<Rightarrow> 'a tree";
    11 consts mirror :: "'a tree \\<Rightarrow> 'a tree";
    12 primrec
    12 primrec
    13 "mirror Tip = Tip"
    13 "mirror Tip = Tip"
    14 "mirror (Node l x r) = Node (mirror l) x (mirror r)";(*>*)
    14 "mirror (Node l x r) = Node (mirror r) x (mirror l)";(*>*)
    15 
    15 
    16 text{*\noindent
    16 text{*\noindent
    17 and a function \isa{mirror} that mirrors a binary tree
    17 and a function \isa{mirror} that mirrors a binary tree
    18 by swapping subtrees (recursively). Prove
    18 by swapping subtrees (recursively). Prove
    19 *}
    19 *}
    21 lemma mirror_mirror: "mirror(mirror t) = t";
    21 lemma mirror_mirror: "mirror(mirror t) = t";
    22 (*<*)
    22 (*<*)
    23 apply(induct_tac t);
    23 apply(induct_tac t);
    24 by(auto);
    24 by(auto);
    25 
    25 
       
    26 consts flatten :: "'a tree => 'a list"
       
    27 primrec
       
    28 "flatten Tip = []"
       
    29 "flatten (Node l x r) = flatten l @ [x] @ flatten r";
       
    30 (*>*)
       
    31 
       
    32 text{*\noindent
       
    33 Define a function \isa{flatten} that flattens a tree into a list
       
    34 by traversing it in infix order. Prove
       
    35 *}
       
    36 
       
    37 lemma "flatten(mirror t) = rev(flatten t)";
       
    38 (*<*)
       
    39 apply(induct_tac t);
       
    40 by(auto);
       
    41 
    26 end
    42 end
    27 (*>*)
    43 (*>*)