1 (* Title: ZF/Induct/Binary_Trees.thy |
1 (* Title: ZF/Induct/Binary_Trees.thy |
2 Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
2 Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
3 Copyright 1992 University of Cambridge |
3 Copyright 1992 University of Cambridge |
4 *) |
4 *) |
5 |
5 |
6 section {* Binary trees *} |
6 section \<open>Binary trees\<close> |
7 |
7 |
8 theory Binary_Trees imports Main begin |
8 theory Binary_Trees imports Main begin |
9 |
9 |
10 subsection {* Datatype definition *} |
10 subsection \<open>Datatype definition\<close> |
11 |
11 |
12 consts |
12 consts |
13 bt :: "i => i" |
13 bt :: "i => i" |
14 |
14 |
15 datatype "bt(A)" = |
15 datatype "bt(A)" = |
25 by (fast elim!: bt.free_elims) |
25 by (fast elim!: bt.free_elims) |
26 |
26 |
27 inductive_cases BrE: "Br(a, l, r) \<in> bt(A)" |
27 inductive_cases BrE: "Br(a, l, r) \<in> bt(A)" |
28 -- "An elimination rule, for type-checking." |
28 -- "An elimination rule, for type-checking." |
29 |
29 |
30 text {* |
30 text \<open> |
31 \medskip Lemmas to justify using @{term bt} in other recursive type |
31 \medskip Lemmas to justify using @{term bt} in other recursive type |
32 definitions. |
32 definitions. |
33 *} |
33 \<close> |
34 |
34 |
35 lemma bt_mono: "A \<subseteq> B ==> bt(A) \<subseteq> bt(B)" |
35 lemma bt_mono: "A \<subseteq> B ==> bt(A) \<subseteq> bt(B)" |
36 apply (unfold bt.defs) |
36 apply (unfold bt.defs) |
37 apply (rule lfp_mono) |
37 apply (rule lfp_mono) |
38 apply (rule bt.bnd_mono)+ |
38 apply (rule bt.bnd_mono)+ |
56 "[| t \<in> bt(A); |
56 "[| t \<in> bt(A); |
57 c \<in> C(Lf); |
57 c \<in> C(Lf); |
58 !!x y z r s. [| x \<in> A; y \<in> bt(A); z \<in> bt(A); r \<in> C(y); s \<in> C(z) |] ==> |
58 !!x y z r s. [| x \<in> A; y \<in> bt(A); z \<in> bt(A); r \<in> C(y); s \<in> C(z) |] ==> |
59 h(x, y, z, r, s) \<in> C(Br(x, y, z)) |
59 h(x, y, z, r, s) \<in> C(Br(x, y, z)) |
60 |] ==> bt_rec(c, h, t) \<in> C(t)" |
60 |] ==> bt_rec(c, h, t) \<in> C(t)" |
61 -- {* Type checking for recursor -- example only; not really needed. *} |
61 -- \<open>Type checking for recursor -- example only; not really needed.\<close> |
62 apply (induct_tac t) |
62 apply (induct_tac t) |
63 apply simp_all |
63 apply simp_all |
64 done |
64 done |
65 |
65 |
66 |
66 |
67 subsection {* Number of nodes, with an example of tail-recursion *} |
67 subsection \<open>Number of nodes, with an example of tail-recursion\<close> |
68 |
68 |
69 consts n_nodes :: "i => i" |
69 consts n_nodes :: "i => i" |
70 primrec |
70 primrec |
71 "n_nodes(Lf) = 0" |
71 "n_nodes(Lf) = 0" |
72 "n_nodes(Br(a, l, r)) = succ(n_nodes(l) #+ n_nodes(r))" |
72 "n_nodes(Br(a, l, r)) = succ(n_nodes(l) #+ n_nodes(r))" |
105 |
105 |
106 lemma n_leaves_type [simp]: "t \<in> bt(A) ==> n_leaves(t) \<in> nat" |
106 lemma n_leaves_type [simp]: "t \<in> bt(A) ==> n_leaves(t) \<in> nat" |
107 by (induct set: bt) auto |
107 by (induct set: bt) auto |
108 |
108 |
109 |
109 |
110 subsection {* Reflecting trees *} |
110 subsection \<open>Reflecting trees\<close> |
111 |
111 |
112 consts |
112 consts |
113 bt_reflect :: "i => i" |
113 bt_reflect :: "i => i" |
114 primrec |
114 primrec |
115 "bt_reflect(Lf) = Lf" |
115 "bt_reflect(Lf) = Lf" |
116 "bt_reflect(Br(a, l, r)) = Br(a, bt_reflect(r), bt_reflect(l))" |
116 "bt_reflect(Br(a, l, r)) = Br(a, bt_reflect(r), bt_reflect(l))" |
117 |
117 |
118 lemma bt_reflect_type [simp]: "t \<in> bt(A) ==> bt_reflect(t) \<in> bt(A)" |
118 lemma bt_reflect_type [simp]: "t \<in> bt(A) ==> bt_reflect(t) \<in> bt(A)" |
119 by (induct set: bt) auto |
119 by (induct set: bt) auto |
120 |
120 |
121 text {* |
121 text \<open> |
122 \medskip Theorems about @{term n_leaves}. |
122 \medskip Theorems about @{term n_leaves}. |
123 *} |
123 \<close> |
124 |
124 |
125 lemma n_leaves_reflect: "t \<in> bt(A) ==> n_leaves(bt_reflect(t)) = n_leaves(t)" |
125 lemma n_leaves_reflect: "t \<in> bt(A) ==> n_leaves(bt_reflect(t)) = n_leaves(t)" |
126 by (induct set: bt) (simp_all add: add_commute) |
126 by (induct set: bt) (simp_all add: add_commute) |
127 |
127 |
128 lemma n_leaves_nodes: "t \<in> bt(A) ==> n_leaves(t) = succ(n_nodes(t))" |
128 lemma n_leaves_nodes: "t \<in> bt(A) ==> n_leaves(t) = succ(n_nodes(t))" |
129 by (induct set: bt) simp_all |
129 by (induct set: bt) simp_all |
130 |
130 |
131 text {* |
131 text \<open> |
132 Theorems about @{term bt_reflect}. |
132 Theorems about @{term bt_reflect}. |
133 *} |
133 \<close> |
134 |
134 |
135 lemma bt_reflect_bt_reflect_ident: "t \<in> bt(A) ==> bt_reflect(bt_reflect(t)) = t" |
135 lemma bt_reflect_bt_reflect_ident: "t \<in> bt(A) ==> bt_reflect(bt_reflect(t)) = t" |
136 by (induct set: bt) simp_all |
136 by (induct set: bt) simp_all |
137 |
137 |
138 end |
138 end |