src/HOL/Datatype.thy
 author haftmann Sun Aug 24 14:42:22 2008 +0200 (2008-08-24) changeset 27981 feb0c01cf0fb parent 27823 52971512d1a2 child 28346 b8390cd56b8f permissions -rw-r--r--
tuned import order
1 (*  Title:      HOL/Datatype.thy
2     ID:         \$Id\$
3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
4     Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
6 Could <*> be generalized to a general summation (Sigma)?
7 *)
9 header {* Analogues of the Cartesian Product and Disjoint Sum for Datatypes *}
11 theory Datatype
12 imports Nat Relation
13 begin
15 typedef (Node)
16   ('a,'b) node = "{p. EX f x k. p = (f::nat=>'b+nat, x::'a+nat) & f k = Inr 0}"
17     --{*it is a subtype of @{text "(nat=>'b+nat) * ('a+nat)"}*}
18   by auto
20 text{*Datatypes will be represented by sets of type @{text node}*}
22 types 'a item        = "('a, unit) node set"
23       ('a, 'b) dtree = "('a, 'b) node set"
25 consts
26   Push      :: "[('b + nat), nat => ('b + nat)] => (nat => ('b + nat))"
28   Push_Node :: "[('b + nat), ('a, 'b) node] => ('a, 'b) node"
29   ndepth    :: "('a, 'b) node => nat"
31   Atom      :: "('a + nat) => ('a, 'b) dtree"
32   Leaf      :: "'a => ('a, 'b) dtree"
33   Numb      :: "nat => ('a, 'b) dtree"
34   Scons     :: "[('a, 'b) dtree, ('a, 'b) dtree] => ('a, 'b) dtree"
35   In0       :: "('a, 'b) dtree => ('a, 'b) dtree"
36   In1       :: "('a, 'b) dtree => ('a, 'b) dtree"
37   Lim       :: "('b => ('a, 'b) dtree) => ('a, 'b) dtree"
39   ntrunc    :: "[nat, ('a, 'b) dtree] => ('a, 'b) dtree"
41   uprod     :: "[('a, 'b) dtree set, ('a, 'b) dtree set]=> ('a, 'b) dtree set"
42   usum      :: "[('a, 'b) dtree set, ('a, 'b) dtree set]=> ('a, 'b) dtree set"
44   Split     :: "[[('a, 'b) dtree, ('a, 'b) dtree]=>'c, ('a, 'b) dtree] => 'c"
45   Case      :: "[[('a, 'b) dtree]=>'c, [('a, 'b) dtree]=>'c, ('a, 'b) dtree] => 'c"
47   dprod     :: "[(('a, 'b) dtree * ('a, 'b) dtree)set, (('a, 'b) dtree * ('a, 'b) dtree)set]
48                 => (('a, 'b) dtree * ('a, 'b) dtree)set"
49   dsum      :: "[(('a, 'b) dtree * ('a, 'b) dtree)set, (('a, 'b) dtree * ('a, 'b) dtree)set]
50                 => (('a, 'b) dtree * ('a, 'b) dtree)set"
53 defs
55   Push_Node_def:  "Push_Node == (%n x. Abs_Node (apfst (Push n) (Rep_Node x)))"
57   (*crude "lists" of nats -- needed for the constructions*)
58   Push_def:   "Push == (%b h. nat_case b h)"
60   (** operations on S-expressions -- sets of nodes **)
62   (*S-expression constructors*)
63   Atom_def:   "Atom == (%x. {Abs_Node((%k. Inr 0, x))})"
64   Scons_def:  "Scons M N == (Push_Node (Inr 1) ` M) Un (Push_Node (Inr (Suc 1)) ` N)"
66   (*Leaf nodes, with arbitrary or nat labels*)
67   Leaf_def:   "Leaf == Atom o Inl"
68   Numb_def:   "Numb == Atom o Inr"
70   (*Injections of the "disjoint sum"*)
71   In0_def:    "In0(M) == Scons (Numb 0) M"
72   In1_def:    "In1(M) == Scons (Numb 1) M"
74   (*Function spaces*)
75   Lim_def: "Lim f == Union {z. ? x. z = Push_Node (Inl x) ` (f x)}"
77   (*the set of nodes with depth less than k*)
78   ndepth_def: "ndepth(n) == (%(f,x). LEAST k. f k = Inr 0) (Rep_Node n)"
79   ntrunc_def: "ntrunc k N == {n. n:N & ndepth(n)<k}"
81   (*products and sums for the "universe"*)
82   uprod_def:  "uprod A B == UN x:A. UN y:B. { Scons x y }"
83   usum_def:   "usum A B == In0`A Un In1`B"
85   (*the corresponding eliminators*)
86   Split_def:  "Split c M == THE u. EX x y. M = Scons x y & u = c x y"
88   Case_def:   "Case c d M == THE u.  (EX x . M = In0(x) & u = c(x))
89                                   | (EX y . M = In1(y) & u = d(y))"
92   (** equality for the "universe" **)
94   dprod_def:  "dprod r s == UN (x,x'):r. UN (y,y'):s. {(Scons x y, Scons x' y')}"
96   dsum_def:   "dsum r s == (UN (x,x'):r. {(In0(x),In0(x'))}) Un
97                           (UN (y,y'):s. {(In1(y),In1(y'))})"
101 lemma apfst_convE:
102     "[| q = apfst f p;  !!x y. [| p = (x,y);  q = (f(x),y) |] ==> R
103      |] ==> R"
104 by (force simp add: apfst_def)
106 (** Push -- an injection, analogous to Cons on lists **)
108 lemma Push_inject1: "Push i f = Push j g  ==> i=j"
109 apply (simp add: Push_def expand_fun_eq)
110 apply (drule_tac x=0 in spec, simp)
111 done
113 lemma Push_inject2: "Push i f = Push j g  ==> f=g"
114 apply (auto simp add: Push_def expand_fun_eq)
115 apply (drule_tac x="Suc x" in spec, simp)
116 done
118 lemma Push_inject:
119     "[| Push i f =Push j g;  [| i=j;  f=g |] ==> P |] ==> P"
120 by (blast dest: Push_inject1 Push_inject2)
122 lemma Push_neq_K0: "Push (Inr (Suc k)) f = (%z. Inr 0) ==> P"
123 by (auto simp add: Push_def expand_fun_eq split: nat.split_asm)
125 lemmas Abs_Node_inj = Abs_Node_inject [THEN [2] rev_iffD1, standard]
128 (*** Introduction rules for Node ***)
130 lemma Node_K0_I: "(%k. Inr 0, a) : Node"
131 by (simp add: Node_def)
133 lemma Node_Push_I: "p: Node ==> apfst (Push i) p : Node"
134 apply (simp add: Node_def Push_def)
135 apply (fast intro!: apfst_conv nat_case_Suc [THEN trans])
136 done
139 subsection{*Freeness: Distinctness of Constructors*}
141 (** Scons vs Atom **)
143 lemma Scons_not_Atom [iff]: "Scons M N \<noteq> Atom(a)"
144 apply (simp add: Atom_def Scons_def Push_Node_def One_nat_def)
145 apply (blast intro: Node_K0_I Rep_Node [THEN Node_Push_I]
146          dest!: Abs_Node_inj
147          elim!: apfst_convE sym [THEN Push_neq_K0])
148 done
150 lemmas Atom_not_Scons [iff] = Scons_not_Atom [THEN not_sym, standard]
153 (*** Injectiveness ***)
155 (** Atomic nodes **)
157 lemma inj_Atom: "inj(Atom)"
158 apply (simp add: Atom_def)
159 apply (blast intro!: inj_onI Node_K0_I dest!: Abs_Node_inj)
160 done
161 lemmas Atom_inject = inj_Atom [THEN injD, standard]
163 lemma Atom_Atom_eq [iff]: "(Atom(a)=Atom(b)) = (a=b)"
164 by (blast dest!: Atom_inject)
166 lemma inj_Leaf: "inj(Leaf)"
167 apply (simp add: Leaf_def o_def)
168 apply (rule inj_onI)
169 apply (erule Atom_inject [THEN Inl_inject])
170 done
172 lemmas Leaf_inject [dest!] = inj_Leaf [THEN injD, standard]
174 lemma inj_Numb: "inj(Numb)"
175 apply (simp add: Numb_def o_def)
176 apply (rule inj_onI)
177 apply (erule Atom_inject [THEN Inr_inject])
178 done
180 lemmas Numb_inject [dest!] = inj_Numb [THEN injD, standard]
183 (** Injectiveness of Push_Node **)
185 lemma Push_Node_inject:
186     "[| Push_Node i m =Push_Node j n;  [| i=j;  m=n |] ==> P
187      |] ==> P"
188 apply (simp add: Push_Node_def)
189 apply (erule Abs_Node_inj [THEN apfst_convE])
190 apply (rule Rep_Node [THEN Node_Push_I])+
191 apply (erule sym [THEN apfst_convE])
192 apply (blast intro: Rep_Node_inject [THEN iffD1] trans sym elim!: Push_inject)
193 done
196 (** Injectiveness of Scons **)
198 lemma Scons_inject_lemma1: "Scons M N <= Scons M' N' ==> M<=M'"
199 apply (simp add: Scons_def One_nat_def)
200 apply (blast dest!: Push_Node_inject)
201 done
203 lemma Scons_inject_lemma2: "Scons M N <= Scons M' N' ==> N<=N'"
204 apply (simp add: Scons_def One_nat_def)
205 apply (blast dest!: Push_Node_inject)
206 done
208 lemma Scons_inject1: "Scons M N = Scons M' N' ==> M=M'"
209 apply (erule equalityE)
210 apply (iprover intro: equalityI Scons_inject_lemma1)
211 done
213 lemma Scons_inject2: "Scons M N = Scons M' N' ==> N=N'"
214 apply (erule equalityE)
215 apply (iprover intro: equalityI Scons_inject_lemma2)
216 done
218 lemma Scons_inject:
219     "[| Scons M N = Scons M' N';  [| M=M';  N=N' |] ==> P |] ==> P"
220 by (iprover dest: Scons_inject1 Scons_inject2)
222 lemma Scons_Scons_eq [iff]: "(Scons M N = Scons M' N') = (M=M' & N=N')"
223 by (blast elim!: Scons_inject)
225 (*** Distinctness involving Leaf and Numb ***)
227 (** Scons vs Leaf **)
229 lemma Scons_not_Leaf [iff]: "Scons M N \<noteq> Leaf(a)"
230 by (simp add: Leaf_def o_def Scons_not_Atom)
232 lemmas Leaf_not_Scons  [iff] = Scons_not_Leaf [THEN not_sym, standard]
234 (** Scons vs Numb **)
236 lemma Scons_not_Numb [iff]: "Scons M N \<noteq> Numb(k)"
237 by (simp add: Numb_def o_def Scons_not_Atom)
239 lemmas Numb_not_Scons [iff] = Scons_not_Numb [THEN not_sym, standard]
242 (** Leaf vs Numb **)
244 lemma Leaf_not_Numb [iff]: "Leaf(a) \<noteq> Numb(k)"
245 by (simp add: Leaf_def Numb_def)
247 lemmas Numb_not_Leaf [iff] = Leaf_not_Numb [THEN not_sym, standard]
250 (*** ndepth -- the depth of a node ***)
252 lemma ndepth_K0: "ndepth (Abs_Node(%k. Inr 0, x)) = 0"
253 by (simp add: ndepth_def  Node_K0_I [THEN Abs_Node_inverse] Least_equality)
255 lemma ndepth_Push_Node_aux:
256      "nat_case (Inr (Suc i)) f k = Inr 0 --> Suc(LEAST x. f x = Inr 0) <= k"
257 apply (induct_tac "k", auto)
258 apply (erule Least_le)
259 done
261 lemma ndepth_Push_Node:
262     "ndepth (Push_Node (Inr (Suc i)) n) = Suc(ndepth(n))"
263 apply (insert Rep_Node [of n, unfolded Node_def])
264 apply (auto simp add: ndepth_def Push_Node_def
265                  Rep_Node [THEN Node_Push_I, THEN Abs_Node_inverse])
266 apply (rule Least_equality)
267 apply (auto simp add: Push_def ndepth_Push_Node_aux)
268 apply (erule LeastI)
269 done
272 (*** ntrunc applied to the various node sets ***)
274 lemma ntrunc_0 [simp]: "ntrunc 0 M = {}"
275 by (simp add: ntrunc_def)
277 lemma ntrunc_Atom [simp]: "ntrunc (Suc k) (Atom a) = Atom(a)"
278 by (auto simp add: Atom_def ntrunc_def ndepth_K0)
280 lemma ntrunc_Leaf [simp]: "ntrunc (Suc k) (Leaf a) = Leaf(a)"
281 by (simp add: Leaf_def o_def ntrunc_Atom)
283 lemma ntrunc_Numb [simp]: "ntrunc (Suc k) (Numb i) = Numb(i)"
284 by (simp add: Numb_def o_def ntrunc_Atom)
286 lemma ntrunc_Scons [simp]:
287     "ntrunc (Suc k) (Scons M N) = Scons (ntrunc k M) (ntrunc k N)"
288 by (auto simp add: Scons_def ntrunc_def One_nat_def ndepth_Push_Node)
292 (** Injection nodes **)
294 lemma ntrunc_one_In0 [simp]: "ntrunc (Suc 0) (In0 M) = {}"
295 apply (simp add: In0_def)
296 apply (simp add: Scons_def)
297 done
299 lemma ntrunc_In0 [simp]: "ntrunc (Suc(Suc k)) (In0 M) = In0 (ntrunc (Suc k) M)"
300 by (simp add: In0_def)
302 lemma ntrunc_one_In1 [simp]: "ntrunc (Suc 0) (In1 M) = {}"
303 apply (simp add: In1_def)
304 apply (simp add: Scons_def)
305 done
307 lemma ntrunc_In1 [simp]: "ntrunc (Suc(Suc k)) (In1 M) = In1 (ntrunc (Suc k) M)"
308 by (simp add: In1_def)
311 subsection{*Set Constructions*}
314 (*** Cartesian Product ***)
316 lemma uprodI [intro!]: "[| M:A;  N:B |] ==> Scons M N : uprod A B"
317 by (simp add: uprod_def)
319 (*The general elimination rule*)
320 lemma uprodE [elim!]:
321     "[| c : uprod A B;
322         !!x y. [| x:A;  y:B;  c = Scons x y |] ==> P
323      |] ==> P"
324 by (auto simp add: uprod_def)
327 (*Elimination of a pair -- introduces no eigenvariables*)
328 lemma uprodE2: "[| Scons M N : uprod A B;  [| M:A;  N:B |] ==> P |] ==> P"
329 by (auto simp add: uprod_def)
332 (*** Disjoint Sum ***)
334 lemma usum_In0I [intro]: "M:A ==> In0(M) : usum A B"
335 by (simp add: usum_def)
337 lemma usum_In1I [intro]: "N:B ==> In1(N) : usum A B"
338 by (simp add: usum_def)
340 lemma usumE [elim!]:
341     "[| u : usum A B;
342         !!x. [| x:A;  u=In0(x) |] ==> P;
343         !!y. [| y:B;  u=In1(y) |] ==> P
344      |] ==> P"
345 by (auto simp add: usum_def)
348 (** Injection **)
350 lemma In0_not_In1 [iff]: "In0(M) \<noteq> In1(N)"
351 by (auto simp add: In0_def In1_def One_nat_def)
353 lemmas In1_not_In0 [iff] = In0_not_In1 [THEN not_sym, standard]
355 lemma In0_inject: "In0(M) = In0(N) ==>  M=N"
356 by (simp add: In0_def)
358 lemma In1_inject: "In1(M) = In1(N) ==>  M=N"
359 by (simp add: In1_def)
361 lemma In0_eq [iff]: "(In0 M = In0 N) = (M=N)"
362 by (blast dest!: In0_inject)
364 lemma In1_eq [iff]: "(In1 M = In1 N) = (M=N)"
365 by (blast dest!: In1_inject)
367 lemma inj_In0: "inj In0"
368 by (blast intro!: inj_onI)
370 lemma inj_In1: "inj In1"
371 by (blast intro!: inj_onI)
374 (*** Function spaces ***)
376 lemma Lim_inject: "Lim f = Lim g ==> f = g"
377 apply (simp add: Lim_def)
378 apply (rule ext)
379 apply (blast elim!: Push_Node_inject)
380 done
383 (*** proving equality of sets and functions using ntrunc ***)
385 lemma ntrunc_subsetI: "ntrunc k M <= M"
386 by (auto simp add: ntrunc_def)
388 lemma ntrunc_subsetD: "(!!k. ntrunc k M <= N) ==> M<=N"
389 by (auto simp add: ntrunc_def)
391 (*A generalized form of the take-lemma*)
392 lemma ntrunc_equality: "(!!k. ntrunc k M = ntrunc k N) ==> M=N"
393 apply (rule equalityI)
394 apply (rule_tac [!] ntrunc_subsetD)
395 apply (rule_tac [!] ntrunc_subsetI [THEN [2] subset_trans], auto)
396 done
398 lemma ntrunc_o_equality:
399     "[| !!k. (ntrunc(k) o h1) = (ntrunc(k) o h2) |] ==> h1=h2"
400 apply (rule ntrunc_equality [THEN ext])
401 apply (simp add: expand_fun_eq)
402 done
405 (*** Monotonicity ***)
407 lemma uprod_mono: "[| A<=A';  B<=B' |] ==> uprod A B <= uprod A' B'"
408 by (simp add: uprod_def, blast)
410 lemma usum_mono: "[| A<=A';  B<=B' |] ==> usum A B <= usum A' B'"
411 by (simp add: usum_def, blast)
413 lemma Scons_mono: "[| M<=M';  N<=N' |] ==> Scons M N <= Scons M' N'"
414 by (simp add: Scons_def, blast)
416 lemma In0_mono: "M<=N ==> In0(M) <= In0(N)"
417 by (simp add: In0_def subset_refl Scons_mono)
419 lemma In1_mono: "M<=N ==> In1(M) <= In1(N)"
420 by (simp add: In1_def subset_refl Scons_mono)
423 (*** Split and Case ***)
425 lemma Split [simp]: "Split c (Scons M N) = c M N"
426 by (simp add: Split_def)
428 lemma Case_In0 [simp]: "Case c d (In0 M) = c(M)"
429 by (simp add: Case_def)
431 lemma Case_In1 [simp]: "Case c d (In1 N) = d(N)"
432 by (simp add: Case_def)
436 (**** UN x. B(x) rules ****)
438 lemma ntrunc_UN1: "ntrunc k (UN x. f(x)) = (UN x. ntrunc k (f x))"
439 by (simp add: ntrunc_def, blast)
441 lemma Scons_UN1_x: "Scons (UN x. f x) M = (UN x. Scons (f x) M)"
442 by (simp add: Scons_def, blast)
444 lemma Scons_UN1_y: "Scons M (UN x. f x) = (UN x. Scons M (f x))"
445 by (simp add: Scons_def, blast)
447 lemma In0_UN1: "In0(UN x. f(x)) = (UN x. In0(f(x)))"
448 by (simp add: In0_def Scons_UN1_y)
450 lemma In1_UN1: "In1(UN x. f(x)) = (UN x. In1(f(x)))"
451 by (simp add: In1_def Scons_UN1_y)
454 (*** Equality for Cartesian Product ***)
456 lemma dprodI [intro!]:
457     "[| (M,M'):r;  (N,N'):s |] ==> (Scons M N, Scons M' N') : dprod r s"
458 by (auto simp add: dprod_def)
460 (*The general elimination rule*)
461 lemma dprodE [elim!]:
462     "[| c : dprod r s;
463         !!x y x' y'. [| (x,x') : r;  (y,y') : s;
464                         c = (Scons x y, Scons x' y') |] ==> P
465      |] ==> P"
466 by (auto simp add: dprod_def)
469 (*** Equality for Disjoint Sum ***)
471 lemma dsum_In0I [intro]: "(M,M'):r ==> (In0(M), In0(M')) : dsum r s"
472 by (auto simp add: dsum_def)
474 lemma dsum_In1I [intro]: "(N,N'):s ==> (In1(N), In1(N')) : dsum r s"
475 by (auto simp add: dsum_def)
477 lemma dsumE [elim!]:
478     "[| w : dsum r s;
479         !!x x'. [| (x,x') : r;  w = (In0(x), In0(x')) |] ==> P;
480         !!y y'. [| (y,y') : s;  w = (In1(y), In1(y')) |] ==> P
481      |] ==> P"
482 by (auto simp add: dsum_def)
485 (*** Monotonicity ***)
487 lemma dprod_mono: "[| r<=r';  s<=s' |] ==> dprod r s <= dprod r' s'"
488 by blast
490 lemma dsum_mono: "[| r<=r';  s<=s' |] ==> dsum r s <= dsum r' s'"
491 by blast
494 (*** Bounding theorems ***)
496 lemma dprod_Sigma: "(dprod (A <*> B) (C <*> D)) <= (uprod A C) <*> (uprod B D)"
497 by blast
499 lemmas dprod_subset_Sigma = subset_trans [OF dprod_mono dprod_Sigma, standard]
501 (*Dependent version*)
502 lemma dprod_subset_Sigma2:
503      "(dprod (Sigma A B) (Sigma C D)) <=
504       Sigma (uprod A C) (Split (%x y. uprod (B x) (D y)))"
505 by auto
507 lemma dsum_Sigma: "(dsum (A <*> B) (C <*> D)) <= (usum A C) <*> (usum B D)"
508 by blast
510 lemmas dsum_subset_Sigma = subset_trans [OF dsum_mono dsum_Sigma, standard]
513 (*** Domain ***)
515 lemma Domain_dprod [simp]: "Domain (dprod r s) = uprod (Domain r) (Domain s)"
516 by auto
518 lemma Domain_dsum [simp]: "Domain (dsum r s) = usum (Domain r) (Domain s)"
519 by auto
522 text {* hides popular names *}
523 hide (open) type node item
524 hide (open) const Push Node Atom Leaf Numb Lim Split Case
527 section {* Datatypes *}
529 subsection {* Representing sums *}
531 rep_datatype (sum) Inl Inr
532 proof -
533   fix P
534   fix s :: "'a + 'b"
535   assume x: "\<And>x\<Colon>'a. P (Inl x)" and y: "\<And>y\<Colon>'b. P (Inr y)"
536   then show "P s" by (auto intro: sumE [of s])
537 qed simp_all
539 lemma sum_case_KK[simp]: "sum_case (%x. a) (%x. a) = (%x. a)"
540   by (rule ext) (simp split: sum.split)
542 lemma surjective_sum: "sum_case (%x::'a. f (Inl x)) (%y::'b. f (Inr y)) s = f(s)"
543   apply (rule_tac s = s in sumE)
544    apply (erule ssubst)
545    apply (rule sum.cases(1))
546   apply (erule ssubst)
547   apply (rule sum.cases(2))
548   done
550 lemma sum_case_weak_cong: "s = t ==> sum_case f g s = sum_case f g t"
551   -- {* Prevents simplification of @{text f} and @{text g}: much faster. *}
552   by simp
554 lemma sum_case_inject:
555   "sum_case f1 f2 = sum_case g1 g2 ==> (f1 = g1 ==> f2 = g2 ==> P) ==> P"
556 proof -
557   assume a: "sum_case f1 f2 = sum_case g1 g2"
558   assume r: "f1 = g1 ==> f2 = g2 ==> P"
559   show P
560     apply (rule r)
561      apply (rule ext)
562      apply (cut_tac x = "Inl x" in a [THEN fun_cong], simp)
563     apply (rule ext)
564     apply (cut_tac x = "Inr x" in a [THEN fun_cong], simp)
565     done
566 qed
568 constdefs
569   Suml :: "('a => 'c) => 'a + 'b => 'c"
570   "Suml == (%f. sum_case f arbitrary)"
572   Sumr :: "('b => 'c) => 'a + 'b => 'c"
573   "Sumr == sum_case arbitrary"
575 lemma Suml_inject: "Suml f = Suml g ==> f = g"
576   by (unfold Suml_def) (erule sum_case_inject)
578 lemma Sumr_inject: "Sumr f = Sumr g ==> f = g"
579   by (unfold Sumr_def) (erule sum_case_inject)
581 hide (open) const Suml Sumr
584 subsection {* The option datatype *}
586 datatype 'a option = None | Some 'a
588 lemma not_None_eq [iff]: "(x ~= None) = (EX y. x = Some y)"
589   by (induct x) auto
591 lemma not_Some_eq [iff]: "(ALL y. x ~= Some y) = (x = None)"
592   by (induct x) auto
594 text{*Although it may appear that both of these equalities are helpful
595 only when applied to assumptions, in practice it seems better to give
596 them the uniform iff attribute. *}
598 lemma option_caseE:
599   assumes c: "(case x of None => P | Some y => Q y)"
600   obtains
601     (None) "x = None" and P
602   | (Some) y where "x = Some y" and "Q y"
603   using c by (cases x) simp_all
605 lemma insert_None_conv_UNIV: "insert None (range Some) = UNIV"
606   by (rule set_ext, case_tac x) auto
609 subsubsection {* Operations *}
611 consts
612   the :: "'a option => 'a"
613 primrec
614   "the (Some x) = x"
616 consts
617   o2s :: "'a option => 'a set"
618 primrec
619   "o2s None = {}"
620   "o2s (Some x) = {x}"
622 lemma ospec [dest]: "(ALL x:o2s A. P x) ==> A = Some x ==> P x"
623   by simp
625 declaration {* fn _ =>
626   Classical.map_cs (fn cs => cs addSD2 ("ospec", thm "ospec"))
627 *}
629 lemma elem_o2s [iff]: "(x : o2s xo) = (xo = Some x)"
630   by (cases xo) auto
632 lemma o2s_empty_eq [simp]: "(o2s xo = {}) = (xo = None)"
633   by (cases xo) auto
635 definition
636   option_map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a option \<Rightarrow> 'b option"
637 where
638   [code func del]: "option_map = (%f y. case y of None => None | Some x => Some (f x))"
640 lemma option_map_None [simp, code]: "option_map f None = None"
641   by (simp add: option_map_def)
643 lemma option_map_Some [simp, code]: "option_map f (Some x) = Some (f x)"
644   by (simp add: option_map_def)
646 lemma option_map_is_None [iff]:
647     "(option_map f opt = None) = (opt = None)"
648   by (simp add: option_map_def split add: option.split)
650 lemma option_map_eq_Some [iff]:
651     "(option_map f xo = Some y) = (EX z. xo = Some z & f z = y)"
652   by (simp add: option_map_def split add: option.split)
654 lemma option_map_comp:
655     "option_map f (option_map g opt) = option_map (f o g) opt"
656   by (simp add: option_map_def split add: option.split)
658 lemma option_map_o_sum_case [simp]:
659     "option_map f o sum_case g h = sum_case (option_map f o g) (option_map f o h)"
660   by (rule ext) (simp split: sum.split)
663 subsubsection {* Code generator setup *}
665 definition
666   is_none :: "'a option \<Rightarrow> bool" where
667   is_none_none [code post, symmetric, code inline]: "is_none x \<longleftrightarrow> x = None"
669 lemma is_none_code [code]:
670   shows "is_none None \<longleftrightarrow> True"
671     and "is_none (Some x) \<longleftrightarrow> False"
672   unfolding is_none_none [symmetric] by simp_all
674 hide (open) const is_none
676 code_type option
677   (SML "_ option")
678   (OCaml "_ option")
679   (Haskell "Maybe _")
681 code_const None and Some
682   (SML "NONE" and "SOME")
683   (OCaml "None" and "Some _")
684   (Haskell "Nothing" and "Just")
686 code_instance option :: eq