
berghofe@7018: (*  Title:      HOL/Induct/Tree.thy
berghofe@7018:     Author:     Stefan Berghofer,  TU Muenchen
paulson@16078:     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
berghofe@7018: *)
berghofe@7018: 
wenzelm@11046: header {* Infinitely branching trees *}
wenzelm@11046: 
haftmann@31602: theory Tree
haftmann@31602: imports Main
haftmann@31602: begin
berghofe@7018: 
blanchet@58249: datatype_new 'a tree =
wenzelm@11046:     Atom 'a
wenzelm@11046:   | Branch "nat => 'a tree"
berghofe@7018: 
wenzelm@46914: primrec map_tree :: "('a => 'b) => 'a tree => 'b tree"
krauss@35419: where
berghofe@7018:   "map_tree f (Atom a) = Atom (f a)"
krauss@35419: | "map_tree f (Branch ts) = Branch (\<lambda>x. map_tree f (ts x))"
wenzelm@11046: 
wenzelm@11046: lemma tree_map_compose: "map_tree g (map_tree f t) = map_tree (g \<circ> f) t"
wenzelm@12171:   by (induct t) simp_all
berghofe@7018: 
wenzelm@46914: primrec exists_tree :: "('a => bool) => 'a tree => bool"
krauss@35419: where
berghofe@7018:   "exists_tree P (Atom a) = P a"
krauss@35419: | "exists_tree P (Branch ts) = (\<exists>x. exists_tree P (ts x))"
wenzelm@11046: 
wenzelm@11046: lemma exists_map:
wenzelm@11046:   "(!!x. P x ==> Q (f x)) ==>
wenzelm@11046:     exists_tree P ts ==> exists_tree Q (map_tree f ts)"
wenzelm@12171:   by (induct ts) auto
berghofe@7018: 
paulson@16078: 
paulson@16078: subsection{*The Brouwer ordinals, as in ZF/Induct/Brouwer.thy.*}
paulson@16078: 
blanchet@58249: datatype_new brouwer = Zero | Succ "brouwer" | Lim "nat => brouwer"
paulson@16078: 
paulson@16078: text{*Addition of ordinals*}
wenzelm@46914: primrec add :: "[brouwer,brouwer] => brouwer"
krauss@35419: where
paulson@16078:   "add i Zero = i"
krauss@35419: | "add i (Succ j) = Succ (add i j)"
krauss@35419: | "add i (Lim f) = Lim (%n. add i (f n))"
paulson@16078: 
paulson@16078: lemma add_assoc: "add (add i j) k = add i (add j k)"
wenzelm@18242:   by (induct k) auto
paulson@16078: 
paulson@16078: text{*Multiplication of ordinals*}
wenzelm@46914: primrec mult :: "[brouwer,brouwer] => brouwer"
krauss@35419: where
paulson@16078:   "mult i Zero = Zero"
krauss@35419: | "mult i (Succ j) = add (mult i j) i"
krauss@35419: | "mult i (Lim f) = Lim (%n. mult i (f n))"
paulson@16078: 
paulson@16078: lemma add_mult_distrib: "mult i (add j k) = add (mult i j) (mult i k)"
wenzelm@18242:   by (induct k) (auto simp add: add_assoc)
paulson@16078: 
paulson@16078: lemma mult_assoc: "mult (mult i j) k = mult i (mult j k)"
wenzelm@18242:   by (induct k) (auto simp add: add_mult_distrib)
paulson@16078: 
paulson@16078: text{*We could probably instantiate some axiomatic type classes and use
paulson@16078: the standard infix operators.*}
paulson@16078: 
paulson@16174: subsection{*A WF Ordering for The Brouwer ordinals (Michael Compton)*}
paulson@16174: 
krauss@35439: text{*To use the function package we need an ordering on the Brouwer
paulson@16174:   ordinals.  Start with a predecessor relation and form its transitive 
paulson@16174:   closure. *} 
paulson@16174: 
wenzelm@46914: definition brouwer_pred :: "(brouwer * brouwer) set"
wenzelm@46914:   where "brouwer_pred = (\<Union>i. {(m,n). n = Succ m \<or> (EX f. n = Lim f & m = f i)})"
paulson@16174: 
wenzelm@46914: definition brouwer_order :: "(brouwer * brouwer) set"
wenzelm@46914:   where "brouwer_order = brouwer_pred^+"
paulson@16174: 
paulson@16174: lemma wf_brouwer_pred: "wf brouwer_pred"
paulson@16174:   by(unfold wf_def brouwer_pred_def, clarify, induct_tac x, blast+)
paulson@16174: 
krauss@35419: lemma wf_brouwer_order[simp]: "wf brouwer_order"
paulson@16174:   by(unfold brouwer_order_def, rule wf_trancl[OF wf_brouwer_pred])
paulson@16174: 
paulson@16174: lemma [simp]: "(j, Succ j) : brouwer_order"
paulson@16174:   by(auto simp add: brouwer_order_def brouwer_pred_def)
paulson@16174: 
paulson@16174: lemma [simp]: "(f n, Lim f) : brouwer_order"
paulson@16174:   by(auto simp add: brouwer_order_def brouwer_pred_def)
paulson@16174: 
krauss@35419: text{*Example of a general function*}
krauss@35419: 
wenzelm@46914: function add2 :: "brouwer \<Rightarrow> brouwer \<Rightarrow> brouwer"
krauss@35419: where
haftmann@39246:   "add2 i Zero = i"
haftmann@39246: | "add2 i (Succ j) = Succ (add2 i j)"
haftmann@39246: | "add2 i (Lim f) = Lim (\<lambda>n. add2 i (f n))"
krauss@35419: by pat_completeness auto
krauss@35419: termination by (relation "inv_image brouwer_order snd") auto
paulson@16174: 
haftmann@39246: lemma add2_assoc: "add2 (add2 i j) k = add2 i (add2 j k)"
wenzelm@18242:   by (induct k) auto
paulson@16174: 
berghofe@7018: end




