author | huffman |
Tue, 01 Jul 2008 01:09:03 +0200 | |
changeset 27406 | 3897988917a3 |
parent 27104 | 791607529f6d |
child 27823 | 52971512d1a2 |
permissions | -rw-r--r-- |
5181
4ba3787d9709
New theory Datatype. Needed as an ancestor when defining datatypes.
berghofe
parents:
diff
changeset
|
1 |
(* Title: HOL/Datatype.thy |
4ba3787d9709
New theory Datatype. Needed as an ancestor when defining datatypes.
berghofe
parents:
diff
changeset
|
2 |
ID: $Id$ |
20819 | 3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
11954 | 4 |
Author: Stefan Berghofer and Markus Wenzel, TU Muenchen |
20819 | 5 |
|
6 |
Could <*> be generalized to a general summation (Sigma)? |
|
5181
4ba3787d9709
New theory Datatype. Needed as an ancestor when defining datatypes.
berghofe
parents:
diff
changeset
|
7 |
*) |
4ba3787d9709
New theory Datatype. Needed as an ancestor when defining datatypes.
berghofe
parents:
diff
changeset
|
8 |
|
21669 | 9 |
header {* Analogues of the Cartesian Product and Disjoint Sum for Datatypes *} |
11954 | 10 |
|
15131 | 11 |
theory Datatype |
26748
4d51ddd6aa5c
Merged theories about wellfoundedness into one: Wellfounded.thy
krauss
parents:
26356
diff
changeset
|
12 |
imports Finite_Set Wellfounded |
15131 | 13 |
begin |
11954 | 14 |
|
26072
f65a7fa2da6c
<= and < on nat no longer depend on wellfounded relations
haftmann
parents:
25836
diff
changeset
|
15 |
lemma size_bool [code func]: |
f65a7fa2da6c
<= and < on nat no longer depend on wellfounded relations
haftmann
parents:
25836
diff
changeset
|
16 |
"size (b\<Colon>bool) = 0" by (cases b) auto |
f65a7fa2da6c
<= and < on nat no longer depend on wellfounded relations
haftmann
parents:
25836
diff
changeset
|
17 |
|
f65a7fa2da6c
<= and < on nat no longer depend on wellfounded relations
haftmann
parents:
25836
diff
changeset
|
18 |
declare "prod.size" [noatp] |
f65a7fa2da6c
<= and < on nat no longer depend on wellfounded relations
haftmann
parents:
25836
diff
changeset
|
19 |
|
20819 | 20 |
typedef (Node) |
21 |
('a,'b) node = "{p. EX f x k. p = (f::nat=>'b+nat, x::'a+nat) & f k = Inr 0}" |
|
22 |
--{*it is a subtype of @{text "(nat=>'b+nat) * ('a+nat)"}*} |
|
23 |
by auto |
|
24 |
||
25 |
text{*Datatypes will be represented by sets of type @{text node}*} |
|
26 |
||
27 |
types 'a item = "('a, unit) node set" |
|
28 |
('a, 'b) dtree = "('a, 'b) node set" |
|
29 |
||
30 |
consts |
|
31 |
Push :: "[('b + nat), nat => ('b + nat)] => (nat => ('b + nat))" |
|
32 |
||
33 |
Push_Node :: "[('b + nat), ('a, 'b) node] => ('a, 'b) node" |
|
34 |
ndepth :: "('a, 'b) node => nat" |
|
35 |
||
36 |
Atom :: "('a + nat) => ('a, 'b) dtree" |
|
37 |
Leaf :: "'a => ('a, 'b) dtree" |
|
38 |
Numb :: "nat => ('a, 'b) dtree" |
|
39 |
Scons :: "[('a, 'b) dtree, ('a, 'b) dtree] => ('a, 'b) dtree" |
|
40 |
In0 :: "('a, 'b) dtree => ('a, 'b) dtree" |
|
41 |
In1 :: "('a, 'b) dtree => ('a, 'b) dtree" |
|
42 |
Lim :: "('b => ('a, 'b) dtree) => ('a, 'b) dtree" |
|
43 |
||
44 |
ntrunc :: "[nat, ('a, 'b) dtree] => ('a, 'b) dtree" |
|
45 |
||
46 |
uprod :: "[('a, 'b) dtree set, ('a, 'b) dtree set]=> ('a, 'b) dtree set" |
|
47 |
usum :: "[('a, 'b) dtree set, ('a, 'b) dtree set]=> ('a, 'b) dtree set" |
|
48 |
||
49 |
Split :: "[[('a, 'b) dtree, ('a, 'b) dtree]=>'c, ('a, 'b) dtree] => 'c" |
|
50 |
Case :: "[[('a, 'b) dtree]=>'c, [('a, 'b) dtree]=>'c, ('a, 'b) dtree] => 'c" |
|
51 |
||
52 |
dprod :: "[(('a, 'b) dtree * ('a, 'b) dtree)set, (('a, 'b) dtree * ('a, 'b) dtree)set] |
|
53 |
=> (('a, 'b) dtree * ('a, 'b) dtree)set" |
|
54 |
dsum :: "[(('a, 'b) dtree * ('a, 'b) dtree)set, (('a, 'b) dtree * ('a, 'b) dtree)set] |
|
55 |
=> (('a, 'b) dtree * ('a, 'b) dtree)set" |
|
56 |
||
57 |
||
58 |
defs |
|
59 |
||
60 |
Push_Node_def: "Push_Node == (%n x. Abs_Node (apfst (Push n) (Rep_Node x)))" |
|
61 |
||
62 |
(*crude "lists" of nats -- needed for the constructions*) |
|
63 |
Push_def: "Push == (%b h. nat_case b h)" |
|
64 |
||
65 |
(** operations on S-expressions -- sets of nodes **) |
|
66 |
||
67 |
(*S-expression constructors*) |
|
68 |
Atom_def: "Atom == (%x. {Abs_Node((%k. Inr 0, x))})" |
|
69 |
Scons_def: "Scons M N == (Push_Node (Inr 1) ` M) Un (Push_Node (Inr (Suc 1)) ` N)" |
|
70 |
||
71 |
(*Leaf nodes, with arbitrary or nat labels*) |
|
72 |
Leaf_def: "Leaf == Atom o Inl" |
|
73 |
Numb_def: "Numb == Atom o Inr" |
|
74 |
||
75 |
(*Injections of the "disjoint sum"*) |
|
76 |
In0_def: "In0(M) == Scons (Numb 0) M" |
|
77 |
In1_def: "In1(M) == Scons (Numb 1) M" |
|
78 |
||
79 |
(*Function spaces*) |
|
80 |
Lim_def: "Lim f == Union {z. ? x. z = Push_Node (Inl x) ` (f x)}" |
|
81 |
||
82 |
(*the set of nodes with depth less than k*) |
|
83 |
ndepth_def: "ndepth(n) == (%(f,x). LEAST k. f k = Inr 0) (Rep_Node n)" |
|
84 |
ntrunc_def: "ntrunc k N == {n. n:N & ndepth(n)<k}" |
|
85 |
||
86 |
(*products and sums for the "universe"*) |
|
87 |
uprod_def: "uprod A B == UN x:A. UN y:B. { Scons x y }" |
|
88 |
usum_def: "usum A B == In0`A Un In1`B" |
|
89 |
||
90 |
(*the corresponding eliminators*) |
|
91 |
Split_def: "Split c M == THE u. EX x y. M = Scons x y & u = c x y" |
|
92 |
||
93 |
Case_def: "Case c d M == THE u. (EX x . M = In0(x) & u = c(x)) |
|
94 |
| (EX y . M = In1(y) & u = d(y))" |
|
95 |
||
96 |
||
97 |
(** equality for the "universe" **) |
|
98 |
||
99 |
dprod_def: "dprod r s == UN (x,x'):r. UN (y,y'):s. {(Scons x y, Scons x' y')}" |
|
100 |
||
101 |
dsum_def: "dsum r s == (UN (x,x'):r. {(In0(x),In0(x'))}) Un |
|
102 |
(UN (y,y'):s. {(In1(y),In1(y'))})" |
|
103 |
||
104 |
||
105 |
||
106 |
lemma apfst_convE: |
|
107 |
"[| q = apfst f p; !!x y. [| p = (x,y); q = (f(x),y) |] ==> R |
|
108 |
|] ==> R" |
|
109 |
by (force simp add: apfst_def) |
|
110 |
||
111 |
(** Push -- an injection, analogous to Cons on lists **) |
|
112 |
||
113 |
lemma Push_inject1: "Push i f = Push j g ==> i=j" |
|
114 |
apply (simp add: Push_def expand_fun_eq) |
|
115 |
apply (drule_tac x=0 in spec, simp) |
|
116 |
done |
|
117 |
||
118 |
lemma Push_inject2: "Push i f = Push j g ==> f=g" |
|
119 |
apply (auto simp add: Push_def expand_fun_eq) |
|
120 |
apply (drule_tac x="Suc x" in spec, simp) |
|
121 |
done |
|
122 |
||
123 |
lemma Push_inject: |
|
124 |
"[| Push i f =Push j g; [| i=j; f=g |] ==> P |] ==> P" |
|
125 |
by (blast dest: Push_inject1 Push_inject2) |
|
126 |
||
127 |
lemma Push_neq_K0: "Push (Inr (Suc k)) f = (%z. Inr 0) ==> P" |
|
128 |
by (auto simp add: Push_def expand_fun_eq split: nat.split_asm) |
|
129 |
||
130 |
lemmas Abs_Node_inj = Abs_Node_inject [THEN [2] rev_iffD1, standard] |
|
131 |
||
132 |
||
133 |
(*** Introduction rules for Node ***) |
|
134 |
||
135 |
lemma Node_K0_I: "(%k. Inr 0, a) : Node" |
|
136 |
by (simp add: Node_def) |
|
137 |
||
138 |
lemma Node_Push_I: "p: Node ==> apfst (Push i) p : Node" |
|
139 |
apply (simp add: Node_def Push_def) |
|
140 |
apply (fast intro!: apfst_conv nat_case_Suc [THEN trans]) |
|
141 |
done |
|
142 |
||
143 |
||
144 |
subsection{*Freeness: Distinctness of Constructors*} |
|
145 |
||
146 |
(** Scons vs Atom **) |
|
147 |
||
148 |
lemma Scons_not_Atom [iff]: "Scons M N \<noteq> Atom(a)" |
|
149 |
apply (simp add: Atom_def Scons_def Push_Node_def One_nat_def) |
|
150 |
apply (blast intro: Node_K0_I Rep_Node [THEN Node_Push_I] |
|
151 |
dest!: Abs_Node_inj |
|
152 |
elim!: apfst_convE sym [THEN Push_neq_K0]) |
|
153 |
done |
|
154 |
||
21407 | 155 |
lemmas Atom_not_Scons [iff] = Scons_not_Atom [THEN not_sym, standard] |
156 |
||
20819 | 157 |
|
158 |
(*** Injectiveness ***) |
|
159 |
||
160 |
(** Atomic nodes **) |
|
161 |
||
162 |
lemma inj_Atom: "inj(Atom)" |
|
163 |
apply (simp add: Atom_def) |
|
164 |
apply (blast intro!: inj_onI Node_K0_I dest!: Abs_Node_inj) |
|
165 |
done |
|
166 |
lemmas Atom_inject = inj_Atom [THEN injD, standard] |
|
167 |
||
168 |
lemma Atom_Atom_eq [iff]: "(Atom(a)=Atom(b)) = (a=b)" |
|
169 |
by (blast dest!: Atom_inject) |
|
170 |
||
171 |
lemma inj_Leaf: "inj(Leaf)" |
|
172 |
apply (simp add: Leaf_def o_def) |
|
173 |
apply (rule inj_onI) |
|
174 |
apply (erule Atom_inject [THEN Inl_inject]) |
|
175 |
done |
|
176 |
||
21407 | 177 |
lemmas Leaf_inject [dest!] = inj_Leaf [THEN injD, standard] |
20819 | 178 |
|
179 |
lemma inj_Numb: "inj(Numb)" |
|
180 |
apply (simp add: Numb_def o_def) |
|
181 |
apply (rule inj_onI) |
|
182 |
apply (erule Atom_inject [THEN Inr_inject]) |
|
183 |
done |
|
184 |
||
21407 | 185 |
lemmas Numb_inject [dest!] = inj_Numb [THEN injD, standard] |
20819 | 186 |
|
187 |
||
188 |
(** Injectiveness of Push_Node **) |
|
189 |
||
190 |
lemma Push_Node_inject: |
|
191 |
"[| Push_Node i m =Push_Node j n; [| i=j; m=n |] ==> P |
|
192 |
|] ==> P" |
|
193 |
apply (simp add: Push_Node_def) |
|
194 |
apply (erule Abs_Node_inj [THEN apfst_convE]) |
|
195 |
apply (rule Rep_Node [THEN Node_Push_I])+ |
|
196 |
apply (erule sym [THEN apfst_convE]) |
|
197 |
apply (blast intro: Rep_Node_inject [THEN iffD1] trans sym elim!: Push_inject) |
|
198 |
done |
|
199 |
||
200 |
||
201 |
(** Injectiveness of Scons **) |
|
202 |
||
203 |
lemma Scons_inject_lemma1: "Scons M N <= Scons M' N' ==> M<=M'" |
|
204 |
apply (simp add: Scons_def One_nat_def) |
|
205 |
apply (blast dest!: Push_Node_inject) |
|
206 |
done |
|
207 |
||
208 |
lemma Scons_inject_lemma2: "Scons M N <= Scons M' N' ==> N<=N'" |
|
209 |
apply (simp add: Scons_def One_nat_def) |
|
210 |
apply (blast dest!: Push_Node_inject) |
|
211 |
done |
|
212 |
||
213 |
lemma Scons_inject1: "Scons M N = Scons M' N' ==> M=M'" |
|
214 |
apply (erule equalityE) |
|
215 |
apply (iprover intro: equalityI Scons_inject_lemma1) |
|
216 |
done |
|
217 |
||
218 |
lemma Scons_inject2: "Scons M N = Scons M' N' ==> N=N'" |
|
219 |
apply (erule equalityE) |
|
220 |
apply (iprover intro: equalityI Scons_inject_lemma2) |
|
221 |
done |
|
222 |
||
223 |
lemma Scons_inject: |
|
224 |
"[| Scons M N = Scons M' N'; [| M=M'; N=N' |] ==> P |] ==> P" |
|
225 |
by (iprover dest: Scons_inject1 Scons_inject2) |
|
226 |
||
227 |
lemma Scons_Scons_eq [iff]: "(Scons M N = Scons M' N') = (M=M' & N=N')" |
|
228 |
by (blast elim!: Scons_inject) |
|
229 |
||
230 |
(*** Distinctness involving Leaf and Numb ***) |
|
231 |
||
232 |
(** Scons vs Leaf **) |
|
233 |
||
234 |
lemma Scons_not_Leaf [iff]: "Scons M N \<noteq> Leaf(a)" |
|
235 |
by (simp add: Leaf_def o_def Scons_not_Atom) |
|
236 |
||
21407 | 237 |
lemmas Leaf_not_Scons [iff] = Scons_not_Leaf [THEN not_sym, standard] |
20819 | 238 |
|
239 |
(** Scons vs Numb **) |
|
240 |
||
241 |
lemma Scons_not_Numb [iff]: "Scons M N \<noteq> Numb(k)" |
|
242 |
by (simp add: Numb_def o_def Scons_not_Atom) |
|
243 |
||
21407 | 244 |
lemmas Numb_not_Scons [iff] = Scons_not_Numb [THEN not_sym, standard] |
20819 | 245 |
|
246 |
||
247 |
(** Leaf vs Numb **) |
|
248 |
||
249 |
lemma Leaf_not_Numb [iff]: "Leaf(a) \<noteq> Numb(k)" |
|
250 |
by (simp add: Leaf_def Numb_def) |
|
251 |
||
21407 | 252 |
lemmas Numb_not_Leaf [iff] = Leaf_not_Numb [THEN not_sym, standard] |
20819 | 253 |
|
254 |
||
255 |
(*** ndepth -- the depth of a node ***) |
|
256 |
||
257 |
lemma ndepth_K0: "ndepth (Abs_Node(%k. Inr 0, x)) = 0" |
|
258 |
by (simp add: ndepth_def Node_K0_I [THEN Abs_Node_inverse] Least_equality) |
|
259 |
||
260 |
lemma ndepth_Push_Node_aux: |
|
261 |
"nat_case (Inr (Suc i)) f k = Inr 0 --> Suc(LEAST x. f x = Inr 0) <= k" |
|
262 |
apply (induct_tac "k", auto) |
|
263 |
apply (erule Least_le) |
|
264 |
done |
|
265 |
||
266 |
lemma ndepth_Push_Node: |
|
267 |
"ndepth (Push_Node (Inr (Suc i)) n) = Suc(ndepth(n))" |
|
268 |
apply (insert Rep_Node [of n, unfolded Node_def]) |
|
269 |
apply (auto simp add: ndepth_def Push_Node_def |
|
270 |
Rep_Node [THEN Node_Push_I, THEN Abs_Node_inverse]) |
|
271 |
apply (rule Least_equality) |
|
272 |
apply (auto simp add: Push_def ndepth_Push_Node_aux) |
|
273 |
apply (erule LeastI) |
|
274 |
done |
|
275 |
||
276 |
||
277 |
(*** ntrunc applied to the various node sets ***) |
|
278 |
||
279 |
lemma ntrunc_0 [simp]: "ntrunc 0 M = {}" |
|
280 |
by (simp add: ntrunc_def) |
|
281 |
||
282 |
lemma ntrunc_Atom [simp]: "ntrunc (Suc k) (Atom a) = Atom(a)" |
|
283 |
by (auto simp add: Atom_def ntrunc_def ndepth_K0) |
|
284 |
||
285 |
lemma ntrunc_Leaf [simp]: "ntrunc (Suc k) (Leaf a) = Leaf(a)" |
|
286 |
by (simp add: Leaf_def o_def ntrunc_Atom) |
|
287 |
||
288 |
lemma ntrunc_Numb [simp]: "ntrunc (Suc k) (Numb i) = Numb(i)" |
|
289 |
by (simp add: Numb_def o_def ntrunc_Atom) |
|
290 |
||
291 |
lemma ntrunc_Scons [simp]: |
|
292 |
"ntrunc (Suc k) (Scons M N) = Scons (ntrunc k M) (ntrunc k N)" |
|
293 |
by (auto simp add: Scons_def ntrunc_def One_nat_def ndepth_Push_Node) |
|
294 |
||
295 |
||
296 |
||
297 |
(** Injection nodes **) |
|
298 |
||
299 |
lemma ntrunc_one_In0 [simp]: "ntrunc (Suc 0) (In0 M) = {}" |
|
300 |
apply (simp add: In0_def) |
|
301 |
apply (simp add: Scons_def) |
|
302 |
done |
|
303 |
||
304 |
lemma ntrunc_In0 [simp]: "ntrunc (Suc(Suc k)) (In0 M) = In0 (ntrunc (Suc k) M)" |
|
305 |
by (simp add: In0_def) |
|
306 |
||
307 |
lemma ntrunc_one_In1 [simp]: "ntrunc (Suc 0) (In1 M) = {}" |
|
308 |
apply (simp add: In1_def) |
|
309 |
apply (simp add: Scons_def) |
|
310 |
done |
|
311 |
||
312 |
lemma ntrunc_In1 [simp]: "ntrunc (Suc(Suc k)) (In1 M) = In1 (ntrunc (Suc k) M)" |
|
313 |
by (simp add: In1_def) |
|
314 |
||
315 |
||
316 |
subsection{*Set Constructions*} |
|
317 |
||
318 |
||
319 |
(*** Cartesian Product ***) |
|
320 |
||
321 |
lemma uprodI [intro!]: "[| M:A; N:B |] ==> Scons M N : uprod A B" |
|
322 |
by (simp add: uprod_def) |
|
323 |
||
324 |
(*The general elimination rule*) |
|
325 |
lemma uprodE [elim!]: |
|
326 |
"[| c : uprod A B; |
|
327 |
!!x y. [| x:A; y:B; c = Scons x y |] ==> P |
|
328 |
|] ==> P" |
|
329 |
by (auto simp add: uprod_def) |
|
330 |
||
331 |
||
332 |
(*Elimination of a pair -- introduces no eigenvariables*) |
|
333 |
lemma uprodE2: "[| Scons M N : uprod A B; [| M:A; N:B |] ==> P |] ==> P" |
|
334 |
by (auto simp add: uprod_def) |
|
335 |
||
336 |
||
337 |
(*** Disjoint Sum ***) |
|
338 |
||
339 |
lemma usum_In0I [intro]: "M:A ==> In0(M) : usum A B" |
|
340 |
by (simp add: usum_def) |
|
341 |
||
342 |
lemma usum_In1I [intro]: "N:B ==> In1(N) : usum A B" |
|
343 |
by (simp add: usum_def) |
|
344 |
||
345 |
lemma usumE [elim!]: |
|
346 |
"[| u : usum A B; |
|
347 |
!!x. [| x:A; u=In0(x) |] ==> P; |
|
348 |
!!y. [| y:B; u=In1(y) |] ==> P |
|
349 |
|] ==> P" |
|
350 |
by (auto simp add: usum_def) |
|
351 |
||
352 |
||
353 |
(** Injection **) |
|
354 |
||
355 |
lemma In0_not_In1 [iff]: "In0(M) \<noteq> In1(N)" |
|
356 |
by (auto simp add: In0_def In1_def One_nat_def) |
|
357 |
||
21407 | 358 |
lemmas In1_not_In0 [iff] = In0_not_In1 [THEN not_sym, standard] |
20819 | 359 |
|
360 |
lemma In0_inject: "In0(M) = In0(N) ==> M=N" |
|
361 |
by (simp add: In0_def) |
|
362 |
||
363 |
lemma In1_inject: "In1(M) = In1(N) ==> M=N" |
|
364 |
by (simp add: In1_def) |
|
365 |
||
366 |
lemma In0_eq [iff]: "(In0 M = In0 N) = (M=N)" |
|
367 |
by (blast dest!: In0_inject) |
|
368 |
||
369 |
lemma In1_eq [iff]: "(In1 M = In1 N) = (M=N)" |
|
370 |
by (blast dest!: In1_inject) |
|
371 |
||
372 |
lemma inj_In0: "inj In0" |
|
373 |
by (blast intro!: inj_onI) |
|
374 |
||
375 |
lemma inj_In1: "inj In1" |
|
376 |
by (blast intro!: inj_onI) |
|
377 |
||
378 |
||
379 |
(*** Function spaces ***) |
|
380 |
||
381 |
lemma Lim_inject: "Lim f = Lim g ==> f = g" |
|
382 |
apply (simp add: Lim_def) |
|
383 |
apply (rule ext) |
|
384 |
apply (blast elim!: Push_Node_inject) |
|
385 |
done |
|
386 |
||
387 |
||
388 |
(*** proving equality of sets and functions using ntrunc ***) |
|
389 |
||
390 |
lemma ntrunc_subsetI: "ntrunc k M <= M" |
|
391 |
by (auto simp add: ntrunc_def) |
|
392 |
||
393 |
lemma ntrunc_subsetD: "(!!k. ntrunc k M <= N) ==> M<=N" |
|
394 |
by (auto simp add: ntrunc_def) |
|
395 |
||
396 |
(*A generalized form of the take-lemma*) |
|
397 |
lemma ntrunc_equality: "(!!k. ntrunc k M = ntrunc k N) ==> M=N" |
|
398 |
apply (rule equalityI) |
|
399 |
apply (rule_tac [!] ntrunc_subsetD) |
|
400 |
apply (rule_tac [!] ntrunc_subsetI [THEN [2] subset_trans], auto) |
|
401 |
done |
|
402 |
||
403 |
lemma ntrunc_o_equality: |
|
404 |
"[| !!k. (ntrunc(k) o h1) = (ntrunc(k) o h2) |] ==> h1=h2" |
|
405 |
apply (rule ntrunc_equality [THEN ext]) |
|
406 |
apply (simp add: expand_fun_eq) |
|
407 |
done |
|
408 |
||
409 |
||
410 |
(*** Monotonicity ***) |
|
411 |
||
412 |
lemma uprod_mono: "[| A<=A'; B<=B' |] ==> uprod A B <= uprod A' B'" |
|
413 |
by (simp add: uprod_def, blast) |
|
414 |
||
415 |
lemma usum_mono: "[| A<=A'; B<=B' |] ==> usum A B <= usum A' B'" |
|
416 |
by (simp add: usum_def, blast) |
|
417 |
||
418 |
lemma Scons_mono: "[| M<=M'; N<=N' |] ==> Scons M N <= Scons M' N'" |
|
419 |
by (simp add: Scons_def, blast) |
|
420 |
||
421 |
lemma In0_mono: "M<=N ==> In0(M) <= In0(N)" |
|
422 |
by (simp add: In0_def subset_refl Scons_mono) |
|
423 |
||
424 |
lemma In1_mono: "M<=N ==> In1(M) <= In1(N)" |
|
425 |
by (simp add: In1_def subset_refl Scons_mono) |
|
426 |
||
427 |
||
428 |
(*** Split and Case ***) |
|
429 |
||
430 |
lemma Split [simp]: "Split c (Scons M N) = c M N" |
|
431 |
by (simp add: Split_def) |
|
432 |
||
433 |
lemma Case_In0 [simp]: "Case c d (In0 M) = c(M)" |
|
434 |
by (simp add: Case_def) |
|
435 |
||
436 |
lemma Case_In1 [simp]: "Case c d (In1 N) = d(N)" |
|
437 |
by (simp add: Case_def) |
|
438 |
||
439 |
||
440 |
||
441 |
(**** UN x. B(x) rules ****) |
|
442 |
||
443 |
lemma ntrunc_UN1: "ntrunc k (UN x. f(x)) = (UN x. ntrunc k (f x))" |
|
444 |
by (simp add: ntrunc_def, blast) |
|
445 |
||
446 |
lemma Scons_UN1_x: "Scons (UN x. f x) M = (UN x. Scons (f x) M)" |
|
447 |
by (simp add: Scons_def, blast) |
|
448 |
||
449 |
lemma Scons_UN1_y: "Scons M (UN x. f x) = (UN x. Scons M (f x))" |
|
450 |
by (simp add: Scons_def, blast) |
|
451 |
||
452 |
lemma In0_UN1: "In0(UN x. f(x)) = (UN x. In0(f(x)))" |
|
453 |
by (simp add: In0_def Scons_UN1_y) |
|
454 |
||
455 |
lemma In1_UN1: "In1(UN x. f(x)) = (UN x. In1(f(x)))" |
|
456 |
by (simp add: In1_def Scons_UN1_y) |
|
457 |
||
458 |
||
459 |
(*** Equality for Cartesian Product ***) |
|
460 |
||
461 |
lemma dprodI [intro!]: |
|
462 |
"[| (M,M'):r; (N,N'):s |] ==> (Scons M N, Scons M' N') : dprod r s" |
|
463 |
by (auto simp add: dprod_def) |
|
464 |
||
465 |
(*The general elimination rule*) |
|
466 |
lemma dprodE [elim!]: |
|
467 |
"[| c : dprod r s; |
|
468 |
!!x y x' y'. [| (x,x') : r; (y,y') : s; |
|
469 |
c = (Scons x y, Scons x' y') |] ==> P |
|
470 |
|] ==> P" |
|
471 |
by (auto simp add: dprod_def) |
|
472 |
||
473 |
||
474 |
(*** Equality for Disjoint Sum ***) |
|
475 |
||
476 |
lemma dsum_In0I [intro]: "(M,M'):r ==> (In0(M), In0(M')) : dsum r s" |
|
477 |
by (auto simp add: dsum_def) |
|
478 |
||
479 |
lemma dsum_In1I [intro]: "(N,N'):s ==> (In1(N), In1(N')) : dsum r s" |
|
480 |
by (auto simp add: dsum_def) |
|
481 |
||
482 |
lemma dsumE [elim!]: |
|
483 |
"[| w : dsum r s; |
|
484 |
!!x x'. [| (x,x') : r; w = (In0(x), In0(x')) |] ==> P; |
|
485 |
!!y y'. [| (y,y') : s; w = (In1(y), In1(y')) |] ==> P |
|
486 |
|] ==> P" |
|
487 |
by (auto simp add: dsum_def) |
|
488 |
||
489 |
||
490 |
(*** Monotonicity ***) |
|
491 |
||
492 |
lemma dprod_mono: "[| r<=r'; s<=s' |] ==> dprod r s <= dprod r' s'" |
|
493 |
by blast |
|
494 |
||
495 |
lemma dsum_mono: "[| r<=r'; s<=s' |] ==> dsum r s <= dsum r' s'" |
|
496 |
by blast |
|
497 |
||
498 |
||
499 |
(*** Bounding theorems ***) |
|
500 |
||
501 |
lemma dprod_Sigma: "(dprod (A <*> B) (C <*> D)) <= (uprod A C) <*> (uprod B D)" |
|
502 |
by blast |
|
503 |
||
504 |
lemmas dprod_subset_Sigma = subset_trans [OF dprod_mono dprod_Sigma, standard] |
|
505 |
||
506 |
(*Dependent version*) |
|
507 |
lemma dprod_subset_Sigma2: |
|
508 |
"(dprod (Sigma A B) (Sigma C D)) <= |
|
509 |
Sigma (uprod A C) (Split (%x y. uprod (B x) (D y)))" |
|
510 |
by auto |
|
511 |
||
512 |
lemma dsum_Sigma: "(dsum (A <*> B) (C <*> D)) <= (usum A C) <*> (usum B D)" |
|
513 |
by blast |
|
514 |
||
515 |
lemmas dsum_subset_Sigma = subset_trans [OF dsum_mono dsum_Sigma, standard] |
|
516 |
||
517 |
||
518 |
(*** Domain ***) |
|
519 |
||
520 |
lemma Domain_dprod [simp]: "Domain (dprod r s) = uprod (Domain r) (Domain s)" |
|
521 |
by auto |
|
522 |
||
523 |
lemma Domain_dsum [simp]: "Domain (dsum r s) = usum (Domain r) (Domain s)" |
|
524 |
by auto |
|
525 |
||
526 |
||
24162
8dfd5dd65d82
split off theory Option for benefit of code generator
haftmann
parents:
22886
diff
changeset
|
527 |
text {* hides popular names *} |
8dfd5dd65d82
split off theory Option for benefit of code generator
haftmann
parents:
22886
diff
changeset
|
528 |
hide (open) type node item |
20819 | 529 |
hide (open) const Push Node Atom Leaf Numb Lim Split Case |
530 |
||
531 |
||
532 |
section {* Datatypes *} |
|
533 |
||
24699
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
24286
diff
changeset
|
534 |
subsection {* Representing sums *} |
12918 | 535 |
|
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
26748
diff
changeset
|
536 |
rep_datatype (sum) Inl Inr |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
26748
diff
changeset
|
537 |
proof - |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
26748
diff
changeset
|
538 |
fix P |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
26748
diff
changeset
|
539 |
fix s :: "'a + 'b" |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
26748
diff
changeset
|
540 |
assume x: "\<And>x\<Colon>'a. P (Inl x)" and y: "\<And>y\<Colon>'b. P (Inr y)" |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
26748
diff
changeset
|
541 |
then show "P s" by (auto intro: sumE [of s]) |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
26748
diff
changeset
|
542 |
qed simp_all |
24194 | 543 |
|
22230 | 544 |
lemma sum_case_KK[simp]: "sum_case (%x. a) (%x. a) = (%x. a)" |
545 |
by (rule ext) (simp split: sum.split) |
|
546 |
||
12918 | 547 |
lemma surjective_sum: "sum_case (%x::'a. f (Inl x)) (%y::'b. f (Inr y)) s = f(s)" |
548 |
apply (rule_tac s = s in sumE) |
|
549 |
apply (erule ssubst) |
|
20798 | 550 |
apply (rule sum.cases(1)) |
12918 | 551 |
apply (erule ssubst) |
20798 | 552 |
apply (rule sum.cases(2)) |
12918 | 553 |
done |
554 |
||
555 |
lemma sum_case_weak_cong: "s = t ==> sum_case f g s = sum_case f g t" |
|
556 |
-- {* Prevents simplification of @{text f} and @{text g}: much faster. *} |
|
20798 | 557 |
by simp |
12918 | 558 |
|
559 |
lemma sum_case_inject: |
|
560 |
"sum_case f1 f2 = sum_case g1 g2 ==> (f1 = g1 ==> f2 = g2 ==> P) ==> P" |
|
561 |
proof - |
|
562 |
assume a: "sum_case f1 f2 = sum_case g1 g2" |
|
563 |
assume r: "f1 = g1 ==> f2 = g2 ==> P" |
|
564 |
show P |
|
565 |
apply (rule r) |
|
566 |
apply (rule ext) |
|
14208 | 567 |
apply (cut_tac x = "Inl x" in a [THEN fun_cong], simp) |
12918 | 568 |
apply (rule ext) |
14208 | 569 |
apply (cut_tac x = "Inr x" in a [THEN fun_cong], simp) |
12918 | 570 |
done |
571 |
qed |
|
572 |
||
13635
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
573 |
constdefs |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
574 |
Suml :: "('a => 'c) => 'a + 'b => 'c" |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
575 |
"Suml == (%f. sum_case f arbitrary)" |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
576 |
|
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
577 |
Sumr :: "('b => 'c) => 'a + 'b => 'c" |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
578 |
"Sumr == sum_case arbitrary" |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
579 |
|
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
580 |
lemma Suml_inject: "Suml f = Suml g ==> f = g" |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
581 |
by (unfold Suml_def) (erule sum_case_inject) |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
582 |
|
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
583 |
lemma Sumr_inject: "Sumr f = Sumr g ==> f = g" |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
584 |
by (unfold Sumr_def) (erule sum_case_inject) |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
585 |
|
20798 | 586 |
hide (open) const Suml Sumr |
13635
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
587 |
|
12918 | 588 |
|
24194 | 589 |
subsection {* The option datatype *} |
590 |
||
591 |
datatype 'a option = None | Some 'a |
|
592 |
||
593 |
lemma not_None_eq [iff]: "(x ~= None) = (EX y. x = Some y)" |
|
594 |
by (induct x) auto |
|
595 |
||
596 |
lemma not_Some_eq [iff]: "(ALL y. x ~= Some y) = (x = None)" |
|
597 |
by (induct x) auto |
|
598 |
||
599 |
text{*Although it may appear that both of these equalities are helpful |
|
600 |
only when applied to assumptions, in practice it seems better to give |
|
601 |
them the uniform iff attribute. *} |
|
602 |
||
603 |
lemma option_caseE: |
|
604 |
assumes c: "(case x of None => P | Some y => Q y)" |
|
605 |
obtains |
|
606 |
(None) "x = None" and P |
|
607 |
| (Some) y where "x = Some y" and "Q y" |
|
608 |
using c by (cases x) simp_all |
|
609 |
||
24728 | 610 |
lemma insert_None_conv_UNIV: "insert None (range Some) = UNIV" |
611 |
by (rule set_ext, case_tac x) auto |
|
612 |
||
26146 | 613 |
instance option :: (finite) finite |
614 |
by default (simp add: insert_None_conv_UNIV [symmetric]) |
|
24728 | 615 |
|
24194 | 616 |
|
617 |
subsubsection {* Operations *} |
|
618 |
||
619 |
consts |
|
620 |
the :: "'a option => 'a" |
|
621 |
primrec |
|
622 |
"the (Some x) = x" |
|
623 |
||
624 |
consts |
|
625 |
o2s :: "'a option => 'a set" |
|
626 |
primrec |
|
627 |
"o2s None = {}" |
|
628 |
"o2s (Some x) = {x}" |
|
629 |
||
630 |
lemma ospec [dest]: "(ALL x:o2s A. P x) ==> A = Some x ==> P x" |
|
631 |
by simp |
|
632 |
||
26339 | 633 |
declaration {* fn _ => |
634 |
Classical.map_cs (fn cs => cs addSD2 ("ospec", thm "ospec")) |
|
635 |
*} |
|
24194 | 636 |
|
637 |
lemma elem_o2s [iff]: "(x : o2s xo) = (xo = Some x)" |
|
638 |
by (cases xo) auto |
|
639 |
||
640 |
lemma o2s_empty_eq [simp]: "(o2s xo = {}) = (xo = None)" |
|
641 |
by (cases xo) auto |
|
642 |
||
25511 | 643 |
definition |
644 |
option_map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a option \<Rightarrow> 'b option" |
|
645 |
where |
|
646 |
[code func del]: "option_map = (%f y. case y of None => None | Some x => Some (f x))" |
|
24194 | 647 |
|
648 |
lemma option_map_None [simp, code]: "option_map f None = None" |
|
649 |
by (simp add: option_map_def) |
|
650 |
||
651 |
lemma option_map_Some [simp, code]: "option_map f (Some x) = Some (f x)" |
|
652 |
by (simp add: option_map_def) |
|
653 |
||
654 |
lemma option_map_is_None [iff]: |
|
655 |
"(option_map f opt = None) = (opt = None)" |
|
656 |
by (simp add: option_map_def split add: option.split) |
|
657 |
||
658 |
lemma option_map_eq_Some [iff]: |
|
659 |
"(option_map f xo = Some y) = (EX z. xo = Some z & f z = y)" |
|
660 |
by (simp add: option_map_def split add: option.split) |
|
661 |
||
662 |
lemma option_map_comp: |
|
663 |
"option_map f (option_map g opt) = option_map (f o g) opt" |
|
664 |
by (simp add: option_map_def split add: option.split) |
|
665 |
||
666 |
lemma option_map_o_sum_case [simp]: |
|
667 |
"option_map f o sum_case g h = sum_case (option_map f o g) (option_map f o h)" |
|
668 |
by (rule ext) (simp split: sum.split) |
|
669 |
||
670 |
||
671 |
subsubsection {* Code generator setup *} |
|
672 |
||
673 |
definition |
|
674 |
is_none :: "'a option \<Rightarrow> bool" where |
|
675 |
is_none_none [code post, symmetric, code inline]: "is_none x \<longleftrightarrow> x = None" |
|
676 |
||
677 |
lemma is_none_code [code]: |
|
678 |
shows "is_none None \<longleftrightarrow> True" |
|
679 |
and "is_none (Some x) \<longleftrightarrow> False" |
|
680 |
unfolding is_none_none [symmetric] by simp_all |
|
681 |
||
682 |
hide (open) const is_none |
|
683 |
||
684 |
code_type option |
|
685 |
(SML "_ option") |
|
686 |
(OCaml "_ option") |
|
687 |
(Haskell "Maybe _") |
|
688 |
||
689 |
code_const None and Some |
|
690 |
(SML "NONE" and "SOME") |
|
691 |
(OCaml "None" and "Some _") |
|
692 |
(Haskell "Nothing" and "Just") |
|
693 |
||
694 |
code_instance option :: eq |
|
695 |
(Haskell -) |
|
696 |
||
697 |
code_const "op = \<Colon> 'a\<Colon>eq option \<Rightarrow> 'a option \<Rightarrow> bool" |
|
698 |
(Haskell infixl 4 "==") |
|
699 |
||
700 |
code_reserved SML |
|
701 |
option NONE SOME |
|
702 |
||
703 |
code_reserved OCaml |
|
704 |
option None Some |
|
705 |
||
706 |
code_modulename SML |
|
707 |
Datatype Nat |
|
708 |
||
709 |
code_modulename OCaml |
|
710 |
Datatype Nat |
|
711 |
||
712 |
code_modulename Haskell |
|
713 |
Datatype Nat |
|
714 |
||
5181
4ba3787d9709
New theory Datatype. Needed as an ancestor when defining datatypes.
berghofe
parents:
diff
changeset
|
715 |
end |