2 theory Tree2 imports Tree begin
5 text{*\noindent In Exercise~\ref{ex:Tree} we defined a function
6 @{term"flatten"} from trees to lists. The straightforward version of
7 @{term"flatten"} is based on @{text"@"} and is thus, like @{term"rev"},
8 quadratic. A linear time version of @{term"flatten"} again reqires an extra
9 argument, the accumulator: *}
11 consts flatten2 :: "'a tree \<Rightarrow> 'a list \<Rightarrow> 'a list"
14 "flatten2 Tip xs = xs"
15 "flatten2 (Node l x r) xs = flatten2 l (x#(flatten2 r xs))"
18 text{*\noindent Define @{term"flatten2"} and prove
21 lemma [simp]: "!xs. flatten2 t xs = flatten t @ xs";
25 lemma "flatten2 t [] = flatten t";