author | traytel |
Tue, 03 Mar 2015 19:08:04 +0100 | |
changeset 59580 | cbc38731d42f |
parent 58871 | c399ae4b836f |
child 60375 | b35b08a143b2 |
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 |
||
58871 | 6 |
section {* Trees and forests, a mutually recursive type definition *} |
12201 | 7 |
|
16417 | 8 |
theory Tree_Forest imports Main begin |
12201 | 9 |
|
10 |
subsection {* Datatype definition *} |
|
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 |
||
36 |
text {* |
|
12205 | 37 |
\medskip @{term "tree_forest(A)"} as the union of @{term "tree(A)"} |
12201 | 38 |
and @{term "forest(A)"}. |
39 |
*} |
|
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 |
||
12937
0c4fd7529467
clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents:
12610
diff
changeset
|
62 |
lemma |
0c4fd7529467
clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents:
12610
diff
changeset
|
63 |
notes rews = tree_forest.con_defs tree_def forest_def |
0c4fd7529467
clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents:
12610
diff
changeset
|
64 |
shows |
0c4fd7529467
clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents:
12610
diff
changeset
|
65 |
tree_forest_unfold: "tree_forest(A) = |
0c4fd7529467
clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents:
12610
diff
changeset
|
66 |
(A \<times> forest(A)) + ({0} + tree(A) \<times> forest(A))" |
12201 | 67 |
-- {* NOT useful, but interesting \dots *} |
68 |
apply (unfold tree_def forest_def) |
|
69 |
apply (fast intro!: tree_forest.intros [unfolded rews, THEN PartD1] |
|
70 |
elim: tree_forest.cases [unfolded rews]) |
|
71 |
done |
|
72 |
||
73 |
lemma tree_forest_unfold': |
|
74 |
"tree_forest(A) = |
|
75 |
A \<times> Part(tree_forest(A), \<lambda>w. Inr(w)) + |
|
76 |
{0} + Part(tree_forest(A), \<lambda>w. Inl(w)) * Part(tree_forest(A), \<lambda>w. Inr(w))" |
|
77 |
by (rule tree_forest_unfold [unfolded tree_def forest_def]) |
|
78 |
||
79 |
lemma tree_unfold: "tree(A) = {Inl(x). x \<in> A \<times> forest(A)}" |
|
80 |
apply (unfold tree_def forest_def) |
|
81 |
apply (rule Part_Inl [THEN subst]) |
|
82 |
apply (rule tree_forest_unfold' [THEN subst_context]) |
|
83 |
done |
|
84 |
||
85 |
lemma forest_unfold: "forest(A) = {Inr(x). x \<in> {0} + tree(A)*forest(A)}" |
|
86 |
apply (unfold tree_def forest_def) |
|
87 |
apply (rule Part_Inr [THEN subst]) |
|
88 |
apply (rule tree_forest_unfold' [THEN subst_context]) |
|
89 |
done |
|
90 |
||
91 |
text {* |
|
92 |
\medskip Type checking for recursor: Not needed; possibly interesting? |
|
93 |
*} |
|
94 |
||
95 |
lemma TF_rec_type: |
|
96 |
"[| z \<in> tree_forest(A); |
|
97 |
!!x f r. [| x \<in> A; f \<in> forest(A); r \<in> C(f) |
|
12243 | 98 |
|] ==> b(x,f,r) \<in> C(Tcons(x,f)); |
12201 | 99 |
c \<in> C(Fnil); |
12243 | 100 |
!!t f r1 r2. [| t \<in> tree(A); f \<in> forest(A); r1 \<in> C(t); r2 \<in> C(f) |
101 |
|] ==> d(t,f,r1,r2) \<in> C(Fcons(t,f)) |
|
12201 | 102 |
|] ==> tree_forest_rec(b,c,d,z) \<in> C(z)" |
103 |
by (induct_tac z) simp_all |
|
104 |
||
105 |
lemma tree_forest_rec_type: |
|
106 |
"[| !!x f r. [| x \<in> A; f \<in> forest(A); r \<in> D(f) |
|
12243 | 107 |
|] ==> b(x,f,r) \<in> C(Tcons(x,f)); |
12201 | 108 |
c \<in> D(Fnil); |
12243 | 109 |
!!t f r1 r2. [| t \<in> tree(A); f \<in> forest(A); r1 \<in> C(t); r2 \<in> D(f) |
110 |
|] ==> d(t,f,r1,r2) \<in> D(Fcons(t,f)) |
|
111 |
|] ==> (\<forall>t \<in> tree(A). tree_forest_rec(b,c,d,t) \<in> C(t)) \<and> |
|
12201 | 112 |
(\<forall>f \<in> forest(A). tree_forest_rec(b,c,d,f) \<in> D(f))" |
113 |
-- {* Mutually recursive version. *} |
|
12243 | 114 |
apply (unfold Ball_def) |
12201 | 115 |
apply (rule tree_forest.mutual_induct) |
116 |
apply simp_all |
|
117 |
done |
|
118 |
||
119 |
||
120 |
subsection {* Operations *} |
|
121 |
||
122 |
consts |
|
123 |
map :: "[i => i, i] => i" |
|
124 |
size :: "i => i" |
|
125 |
preorder :: "i => i" |
|
126 |
list_of_TF :: "i => i" |
|
127 |
of_list :: "i => i" |
|
128 |
reflect :: "i => i" |
|
129 |
||
130 |
primrec |
|
131 |
"list_of_TF (Tcons(x,f)) = [Tcons(x,f)]" |
|
132 |
"list_of_TF (Fnil) = []" |
|
133 |
"list_of_TF (Fcons(t,tf)) = Cons (t, list_of_TF(tf))" |
|
134 |
||
135 |
primrec |
|
136 |
"of_list([]) = Fnil" |
|
137 |
"of_list(Cons(t,l)) = Fcons(t, of_list(l))" |
|
138 |
||
139 |
primrec |
|
140 |
"map (h, Tcons(x,f)) = Tcons(h(x), map(h,f))" |
|
141 |
"map (h, Fnil) = Fnil" |
|
142 |
"map (h, Fcons(t,tf)) = Fcons (map(h, t), map(h, tf))" |
|
143 |
||
144 |
primrec |
|
145 |
"size (Tcons(x,f)) = succ(size(f))" |
|
146 |
"size (Fnil) = 0" |
|
147 |
"size (Fcons(t,tf)) = size(t) #+ size(tf)" |
|
148 |
||
149 |
primrec |
|
150 |
"preorder (Tcons(x,f)) = Cons(x, preorder(f))" |
|
151 |
"preorder (Fnil) = Nil" |
|
152 |
"preorder (Fcons(t,tf)) = preorder(t) @ preorder(tf)" |
|
153 |
||
154 |
primrec |
|
155 |
"reflect (Tcons(x,f)) = Tcons(x, reflect(f))" |
|
156 |
"reflect (Fnil) = Fnil" |
|
157 |
"reflect (Fcons(t,tf)) = |
|
158 |
of_list (list_of_TF (reflect(tf)) @ Cons(reflect(t), Nil))" |
|
159 |
||
160 |
||
161 |
text {* |
|
162 |
\medskip @{text list_of_TF} and @{text of_list}. |
|
163 |
*} |
|
164 |
||
165 |
lemma list_of_TF_type [TC]: |
|
166 |
"z \<in> tree_forest(A) ==> list_of_TF(z) \<in> list(tree(A))" |
|
18460 | 167 |
by (induct set: tree_forest) simp_all |
12201 | 168 |
|
12243 | 169 |
lemma of_list_type [TC]: "l \<in> list(tree(A)) ==> of_list(l) \<in> forest(A)" |
18460 | 170 |
by (induct set: list) simp_all |
12201 | 171 |
|
172 |
text {* |
|
173 |
\medskip @{text map}. |
|
174 |
*} |
|
175 |
||
12937
0c4fd7529467
clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents:
12610
diff
changeset
|
176 |
lemma |
18460 | 177 |
assumes "!!x. x \<in> A ==> h(x): B" |
12937
0c4fd7529467
clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents:
12610
diff
changeset
|
178 |
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
|
179 |
and map_forest_type: "f \<in> forest(A) ==> map(h,f) \<in> forest(B)" |
41526 | 180 |
using assms |
18460 | 181 |
by (induct rule: tree'induct forest'induct) simp_all |
12201 | 182 |
|
183 |
text {* |
|
184 |
\medskip @{text size}. |
|
185 |
*} |
|
186 |
||
187 |
lemma size_type [TC]: "z \<in> tree_forest(A) ==> size(z) \<in> nat" |
|
18460 | 188 |
by (induct set: tree_forest) simp_all |
12201 | 189 |
|
190 |
||
191 |
text {* |
|
192 |
\medskip @{text preorder}. |
|
193 |
*} |
|
194 |
||
195 |
lemma preorder_type [TC]: "z \<in> tree_forest(A) ==> preorder(z) \<in> list(A)" |
|
18460 | 196 |
by (induct set: tree_forest) simp_all |
12201 | 197 |
|
198 |
||
199 |
text {* |
|
200 |
\medskip Theorems about @{text list_of_TF} and @{text of_list}. |
|
201 |
*} |
|
202 |
||
18415 | 203 |
lemma forest_induct [consumes 1, case_names Fnil Fcons]: |
12201 | 204 |
"[| f \<in> forest(A); |
205 |
R(Fnil); |
|
206 |
!!t f. [| t \<in> tree(A); f \<in> forest(A); R(f) |] ==> R(Fcons(t,f)) |
|
207 |
|] ==> R(f)" |
|
208 |
-- {* Essentially the same as list induction. *} |
|
12610 | 209 |
apply (erule tree_forest.mutual_induct |
210 |
[THEN conjunct2, THEN spec, THEN [2] rev_mp]) |
|
12201 | 211 |
apply (rule TrueI) |
212 |
apply simp |
|
213 |
apply simp |
|
214 |
done |
|
215 |
||
216 |
lemma forest_iso: "f \<in> forest(A) ==> of_list(list_of_TF(f)) = f" |
|
18415 | 217 |
by (induct rule: forest_induct) simp_all |
12201 | 218 |
|
219 |
lemma tree_list_iso: "ts: list(tree(A)) ==> list_of_TF(of_list(ts)) = ts" |
|
18415 | 220 |
by (induct set: list) simp_all |
12201 | 221 |
|
222 |
||
223 |
text {* |
|
224 |
\medskip Theorems about @{text map}. |
|
225 |
*} |
|
226 |
||
227 |
lemma map_ident: "z \<in> tree_forest(A) ==> map(\<lambda>u. u, z) = z" |
|
18460 | 228 |
by (induct set: tree_forest) simp_all |
12201 | 229 |
|
12216 | 230 |
lemma map_compose: |
231 |
"z \<in> tree_forest(A) ==> map(h, map(j,z)) = map(\<lambda>u. h(j(u)), z)" |
|
18460 | 232 |
by (induct set: tree_forest) simp_all |
12201 | 233 |
|
234 |
||
235 |
text {* |
|
236 |
\medskip Theorems about @{text size}. |
|
237 |
*} |
|
238 |
||
239 |
lemma size_map: "z \<in> tree_forest(A) ==> size(map(h,z)) = size(z)" |
|
18460 | 240 |
by (induct set: tree_forest) simp_all |
12201 | 241 |
|
242 |
lemma size_length: "z \<in> tree_forest(A) ==> size(z) = length(preorder(z))" |
|
18460 | 243 |
by (induct set: tree_forest) (simp_all add: length_app) |
12201 | 244 |
|
245 |
text {* |
|
246 |
\medskip Theorems about @{text preorder}. |
|
247 |
*} |
|
248 |
||
249 |
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
|
250 |
"z \<in> tree_forest(A) ==> preorder(map(h,z)) = List_ZF.map(h, preorder(z))" |
18460 | 251 |
by (induct set: tree_forest) (simp_all add: map_app_distrib) |
12201 | 252 |
|
253 |
end |