author | wenzelm |
Mon, 24 Apr 2017 23:10:01 +0200 | |
changeset 65576 | 8376f83f9094 |
parent 65449 | c82e63b11b8b |
child 68233 | 5e0e9376b2b0 |
permissions | -rw-r--r-- |
12201 | 1 |
(* Title: ZF/Induct/Tree_Forest.thy |
2 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
3 |
Copyright 1994 University of Cambridge |
|
4 |
*) |
|
5 |
||
60770 | 6 |
section \<open>Trees and forests, a mutually recursive type definition\<close> |
12201 | 7 |
|
65449
c82e63b11b8b
clarified main ZF.thy / ZFC.thy, and avoid name clash with global HOL/Main.thy;
wenzelm
parents:
61798
diff
changeset
|
8 |
theory Tree_Forest imports ZF begin |
12201 | 9 |
|
60770 | 10 |
subsection \<open>Datatype definition\<close> |
12201 | 11 |
|
12 |
consts |
|
13 |
tree :: "i => i" |
|
14 |
forest :: "i => i" |
|
15 |
tree_forest :: "i => i" |
|
16 |
||
17 |
datatype "tree(A)" = Tcons ("a \<in> A", "f \<in> forest(A)") |
|
18 |
and "forest(A)" = Fnil | Fcons ("t \<in> tree(A)", "f \<in> forest(A)") |
|
19 |
||
18460 | 20 |
(* FIXME *) |
21 |
lemmas tree'induct = |
|
45602 | 22 |
tree_forest.mutual_induct [THEN conjunct1, THEN spec, THEN [2] rev_mp, of concl: _ t, consumes 1] |
18460 | 23 |
and forest'induct = |
45602 | 24 |
tree_forest.mutual_induct [THEN conjunct2, THEN spec, THEN [2] rev_mp, of concl: _ f, consumes 1] |
25 |
for t |
|
18460 | 26 |
|
12201 | 27 |
declare tree_forest.intros [simp, TC] |
28 |
||
12216 | 29 |
lemma tree_def: "tree(A) == Part(tree_forest(A), Inl)" |
12201 | 30 |
by (simp only: tree_forest.defs) |
31 |
||
12216 | 32 |
lemma forest_def: "forest(A) == Part(tree_forest(A), Inr)" |
12201 | 33 |
by (simp only: tree_forest.defs) |
34 |
||
35 |
||
60770 | 36 |
text \<open> |
12205 | 37 |
\medskip @{term "tree_forest(A)"} as the union of @{term "tree(A)"} |
12201 | 38 |
and @{term "forest(A)"}. |
60770 | 39 |
\<close> |
12201 | 40 |
|
41 |
lemma tree_subset_TF: "tree(A) \<subseteq> tree_forest(A)" |
|
42 |
apply (unfold tree_forest.defs) |
|
43 |
apply (rule Part_subset) |
|
44 |
done |
|
45 |
||
12243 | 46 |
lemma treeI [TC]: "x \<in> tree(A) ==> x \<in> tree_forest(A)" |
12201 | 47 |
by (rule tree_subset_TF [THEN subsetD]) |
48 |
||
49 |
lemma forest_subset_TF: "forest(A) \<subseteq> tree_forest(A)" |
|
50 |
apply (unfold tree_forest.defs) |
|
51 |
apply (rule Part_subset) |
|
52 |
done |
|
53 |
||
13535 | 54 |
lemma treeI' [TC]: "x \<in> forest(A) ==> x \<in> tree_forest(A)" |
12201 | 55 |
by (rule forest_subset_TF [THEN subsetD]) |
56 |
||
57 |
lemma TF_equals_Un: "tree(A) \<union> forest(A) = tree_forest(A)" |
|
58 |
apply (insert tree_subset_TF forest_subset_TF) |
|
59 |
apply (auto intro!: equalityI tree_forest.intros elim: tree_forest.cases) |
|
60 |
done |
|
61 |
||
60375 | 62 |
lemma tree_forest_unfold: |
63 |
"tree_forest(A) = (A \<times> forest(A)) + ({0} + tree(A) \<times> forest(A))" |
|
61798 | 64 |
\<comment> \<open>NOT useful, but interesting \dots\<close> |
60375 | 65 |
supply rews = tree_forest.con_defs tree_def forest_def |
12201 | 66 |
apply (unfold tree_def forest_def) |
67 |
apply (fast intro!: tree_forest.intros [unfolded rews, THEN PartD1] |
|
68 |
elim: tree_forest.cases [unfolded rews]) |
|
69 |
done |
|
70 |
||
71 |
lemma tree_forest_unfold': |
|
72 |
"tree_forest(A) = |
|
73 |
A \<times> Part(tree_forest(A), \<lambda>w. Inr(w)) + |
|
74 |
{0} + Part(tree_forest(A), \<lambda>w. Inl(w)) * Part(tree_forest(A), \<lambda>w. Inr(w))" |
|
75 |
by (rule tree_forest_unfold [unfolded tree_def forest_def]) |
|
76 |
||
77 |
lemma tree_unfold: "tree(A) = {Inl(x). x \<in> A \<times> forest(A)}" |
|
78 |
apply (unfold tree_def forest_def) |
|
79 |
apply (rule Part_Inl [THEN subst]) |
|
80 |
apply (rule tree_forest_unfold' [THEN subst_context]) |
|
81 |
done |
|
82 |
||
83 |
lemma forest_unfold: "forest(A) = {Inr(x). x \<in> {0} + tree(A)*forest(A)}" |
|
84 |
apply (unfold tree_def forest_def) |
|
85 |
apply (rule Part_Inr [THEN subst]) |
|
86 |
apply (rule tree_forest_unfold' [THEN subst_context]) |
|
87 |
done |
|
88 |
||
60770 | 89 |
text \<open> |
12201 | 90 |
\medskip Type checking for recursor: Not needed; possibly interesting? |
60770 | 91 |
\<close> |
12201 | 92 |
|
93 |
lemma TF_rec_type: |
|
94 |
"[| z \<in> tree_forest(A); |
|
95 |
!!x f r. [| x \<in> A; f \<in> forest(A); r \<in> C(f) |
|
12243 | 96 |
|] ==> b(x,f,r) \<in> C(Tcons(x,f)); |
12201 | 97 |
c \<in> C(Fnil); |
12243 | 98 |
!!t f r1 r2. [| t \<in> tree(A); f \<in> forest(A); r1 \<in> C(t); r2 \<in> C(f) |
99 |
|] ==> d(t,f,r1,r2) \<in> C(Fcons(t,f)) |
|
12201 | 100 |
|] ==> tree_forest_rec(b,c,d,z) \<in> C(z)" |
101 |
by (induct_tac z) simp_all |
|
102 |
||
103 |
lemma tree_forest_rec_type: |
|
104 |
"[| !!x f r. [| x \<in> A; f \<in> forest(A); r \<in> D(f) |
|
12243 | 105 |
|] ==> b(x,f,r) \<in> C(Tcons(x,f)); |
12201 | 106 |
c \<in> D(Fnil); |
12243 | 107 |
!!t f r1 r2. [| t \<in> tree(A); f \<in> forest(A); r1 \<in> C(t); r2 \<in> D(f) |
108 |
|] ==> d(t,f,r1,r2) \<in> D(Fcons(t,f)) |
|
109 |
|] ==> (\<forall>t \<in> tree(A). tree_forest_rec(b,c,d,t) \<in> C(t)) \<and> |
|
12201 | 110 |
(\<forall>f \<in> forest(A). tree_forest_rec(b,c,d,f) \<in> D(f))" |
61798 | 111 |
\<comment> \<open>Mutually recursive version.\<close> |
12243 | 112 |
apply (unfold Ball_def) |
12201 | 113 |
apply (rule tree_forest.mutual_induct) |
114 |
apply simp_all |
|
115 |
done |
|
116 |
||
117 |
||
60770 | 118 |
subsection \<open>Operations\<close> |
12201 | 119 |
|
120 |
consts |
|
121 |
map :: "[i => i, i] => i" |
|
122 |
size :: "i => i" |
|
123 |
preorder :: "i => i" |
|
124 |
list_of_TF :: "i => i" |
|
125 |
of_list :: "i => i" |
|
126 |
reflect :: "i => i" |
|
127 |
||
128 |
primrec |
|
129 |
"list_of_TF (Tcons(x,f)) = [Tcons(x,f)]" |
|
130 |
"list_of_TF (Fnil) = []" |
|
131 |
"list_of_TF (Fcons(t,tf)) = Cons (t, list_of_TF(tf))" |
|
132 |
||
133 |
primrec |
|
134 |
"of_list([]) = Fnil" |
|
135 |
"of_list(Cons(t,l)) = Fcons(t, of_list(l))" |
|
136 |
||
137 |
primrec |
|
138 |
"map (h, Tcons(x,f)) = Tcons(h(x), map(h,f))" |
|
139 |
"map (h, Fnil) = Fnil" |
|
140 |
"map (h, Fcons(t,tf)) = Fcons (map(h, t), map(h, tf))" |
|
141 |
||
142 |
primrec |
|
143 |
"size (Tcons(x,f)) = succ(size(f))" |
|
144 |
"size (Fnil) = 0" |
|
145 |
"size (Fcons(t,tf)) = size(t) #+ size(tf)" |
|
146 |
||
147 |
primrec |
|
148 |
"preorder (Tcons(x,f)) = Cons(x, preorder(f))" |
|
149 |
"preorder (Fnil) = Nil" |
|
150 |
"preorder (Fcons(t,tf)) = preorder(t) @ preorder(tf)" |
|
151 |
||
152 |
primrec |
|
153 |
"reflect (Tcons(x,f)) = Tcons(x, reflect(f))" |
|
154 |
"reflect (Fnil) = Fnil" |
|
155 |
"reflect (Fcons(t,tf)) = |
|
156 |
of_list (list_of_TF (reflect(tf)) @ Cons(reflect(t), Nil))" |
|
157 |
||
158 |
||
60770 | 159 |
text \<open> |
61798 | 160 |
\medskip \<open>list_of_TF\<close> and \<open>of_list\<close>. |
60770 | 161 |
\<close> |
12201 | 162 |
|
163 |
lemma list_of_TF_type [TC]: |
|
164 |
"z \<in> tree_forest(A) ==> list_of_TF(z) \<in> list(tree(A))" |
|
18460 | 165 |
by (induct set: tree_forest) simp_all |
12201 | 166 |
|
12243 | 167 |
lemma of_list_type [TC]: "l \<in> list(tree(A)) ==> of_list(l) \<in> forest(A)" |
18460 | 168 |
by (induct set: list) simp_all |
12201 | 169 |
|
60770 | 170 |
text \<open> |
61798 | 171 |
\medskip \<open>map\<close>. |
60770 | 172 |
\<close> |
12201 | 173 |
|
12937
0c4fd7529467
clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents:
12610
diff
changeset
|
174 |
lemma |
18460 | 175 |
assumes "!!x. x \<in> A ==> h(x): B" |
12937
0c4fd7529467
clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents:
12610
diff
changeset
|
176 |
shows map_tree_type: "t \<in> tree(A) ==> map(h,t) \<in> tree(B)" |
0c4fd7529467
clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents:
12610
diff
changeset
|
177 |
and map_forest_type: "f \<in> forest(A) ==> map(h,f) \<in> forest(B)" |
41526 | 178 |
using assms |
18460 | 179 |
by (induct rule: tree'induct forest'induct) simp_all |
12201 | 180 |
|
60770 | 181 |
text \<open> |
61798 | 182 |
\medskip \<open>size\<close>. |
60770 | 183 |
\<close> |
12201 | 184 |
|
185 |
lemma size_type [TC]: "z \<in> tree_forest(A) ==> size(z) \<in> nat" |
|
18460 | 186 |
by (induct set: tree_forest) simp_all |
12201 | 187 |
|
188 |
||
60770 | 189 |
text \<open> |
61798 | 190 |
\medskip \<open>preorder\<close>. |
60770 | 191 |
\<close> |
12201 | 192 |
|
193 |
lemma preorder_type [TC]: "z \<in> tree_forest(A) ==> preorder(z) \<in> list(A)" |
|
18460 | 194 |
by (induct set: tree_forest) simp_all |
12201 | 195 |
|
196 |
||
60770 | 197 |
text \<open> |
61798 | 198 |
\medskip Theorems about \<open>list_of_TF\<close> and \<open>of_list\<close>. |
60770 | 199 |
\<close> |
12201 | 200 |
|
18415 | 201 |
lemma forest_induct [consumes 1, case_names Fnil Fcons]: |
12201 | 202 |
"[| f \<in> forest(A); |
203 |
R(Fnil); |
|
204 |
!!t f. [| t \<in> tree(A); f \<in> forest(A); R(f) |] ==> R(Fcons(t,f)) |
|
205 |
|] ==> R(f)" |
|
61798 | 206 |
\<comment> \<open>Essentially the same as list induction.\<close> |
12610 | 207 |
apply (erule tree_forest.mutual_induct |
208 |
[THEN conjunct2, THEN spec, THEN [2] rev_mp]) |
|
12201 | 209 |
apply (rule TrueI) |
210 |
apply simp |
|
211 |
apply simp |
|
212 |
done |
|
213 |
||
214 |
lemma forest_iso: "f \<in> forest(A) ==> of_list(list_of_TF(f)) = f" |
|
18415 | 215 |
by (induct rule: forest_induct) simp_all |
12201 | 216 |
|
217 |
lemma tree_list_iso: "ts: list(tree(A)) ==> list_of_TF(of_list(ts)) = ts" |
|
18415 | 218 |
by (induct set: list) simp_all |
12201 | 219 |
|
220 |
||
60770 | 221 |
text \<open> |
61798 | 222 |
\medskip Theorems about \<open>map\<close>. |
60770 | 223 |
\<close> |
12201 | 224 |
|
225 |
lemma map_ident: "z \<in> tree_forest(A) ==> map(\<lambda>u. u, z) = z" |
|
18460 | 226 |
by (induct set: tree_forest) simp_all |
12201 | 227 |
|
12216 | 228 |
lemma map_compose: |
229 |
"z \<in> tree_forest(A) ==> map(h, map(j,z)) = map(\<lambda>u. h(j(u)), z)" |
|
18460 | 230 |
by (induct set: tree_forest) simp_all |
12201 | 231 |
|
232 |
||
60770 | 233 |
text \<open> |
61798 | 234 |
\medskip Theorems about \<open>size\<close>. |
60770 | 235 |
\<close> |
12201 | 236 |
|
237 |
lemma size_map: "z \<in> tree_forest(A) ==> size(map(h,z)) = size(z)" |
|
18460 | 238 |
by (induct set: tree_forest) simp_all |
12201 | 239 |
|
240 |
lemma size_length: "z \<in> tree_forest(A) ==> size(z) = length(preorder(z))" |
|
18460 | 241 |
by (induct set: tree_forest) (simp_all add: length_app) |
12201 | 242 |
|
60770 | 243 |
text \<open> |
61798 | 244 |
\medskip Theorems about \<open>preorder\<close>. |
60770 | 245 |
\<close> |
12201 | 246 |
|
247 |
lemma preorder_map: |
|
26056
6a0801279f4c
Made theory names in ZF disjoint from HOL theory names to allow loading both developments
krauss
parents:
18460
diff
changeset
|
248 |
"z \<in> tree_forest(A) ==> preorder(map(h,z)) = List_ZF.map(h, preorder(z))" |
18460 | 249 |
by (induct set: tree_forest) (simp_all add: map_app_distrib) |
12201 | 250 |
|
251 |
end |