| author | blanchet | 
| Wed, 22 Jan 2014 09:45:30 +0100 | |
| changeset 55101 | 57c875e488bd | 
| parent 55089 | 181751ad852f | 
| child 55129 | 26bd1cba3ab5 | 
| permissions | -rw-r--r-- | 
| 30246 | 1  | 
(* Title: HOL/Option.thy  | 
2  | 
Author: Folklore  | 
|
3  | 
*)  | 
|
4  | 
||
5  | 
header {* Datatype option *}
 | 
|
6  | 
||
7  | 
theory Option  | 
|
| 
55089
 
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
 
blanchet 
parents: 
53940 
diff
changeset
 | 
8  | 
imports Datatype Finite_Set  | 
| 30246 | 9  | 
begin  | 
10  | 
||
11  | 
datatype 'a option = None | Some 'a  | 
|
12  | 
||
13  | 
lemma not_None_eq [iff]: "(x ~= None) = (EX y. x = Some y)"  | 
|
14  | 
by (induct x) auto  | 
|
15  | 
||
16  | 
lemma not_Some_eq [iff]: "(ALL y. x ~= Some y) = (x = None)"  | 
|
17  | 
by (induct x) auto  | 
|
18  | 
||
19  | 
text{*Although it may appear that both of these equalities are helpful
 | 
|
20  | 
only when applied to assumptions, in practice it seems better to give  | 
|
21  | 
them the uniform iff attribute. *}  | 
|
22  | 
||
| 31080 | 23  | 
lemma inj_Some [simp]: "inj_on Some A"  | 
24  | 
by (rule inj_onI) simp  | 
|
25  | 
||
| 30246 | 26  | 
lemma option_caseE:  | 
27  | 
assumes c: "(case x of None => P | Some y => Q y)"  | 
|
28  | 
obtains  | 
|
29  | 
(None) "x = None" and P  | 
|
30  | 
| (Some) y where "x = Some y" and "Q y"  | 
|
31  | 
using c by (cases x) simp_all  | 
|
32  | 
||
| 53010 | 33  | 
lemma split_option_all: "(\<forall>x. P x) \<longleftrightarrow> P None \<and> (\<forall>x. P (Some x))"  | 
34  | 
by (auto intro: option.induct)  | 
|
35  | 
||
36  | 
lemma split_option_ex: "(\<exists>x. P x) \<longleftrightarrow> P None \<or> (\<exists>x. P (Some x))"  | 
|
37  | 
using split_option_all[of "\<lambda>x. \<not>P x"] by blast  | 
|
38  | 
||
| 31080 | 39  | 
lemma UNIV_option_conv: "UNIV = insert None (range Some)"  | 
40  | 
by(auto intro: classical)  | 
|
41  | 
||
| 30246 | 42  | 
subsubsection {* Operations *}
 | 
43  | 
||
44  | 
primrec the :: "'a option => 'a" where  | 
|
45  | 
"the (Some x) = x"  | 
|
46  | 
||
47  | 
primrec set :: "'a option => 'a set" where  | 
|
48  | 
"set None = {}" |
 | 
|
49  | 
"set (Some x) = {x}"
 | 
|
50  | 
||
51  | 
lemma ospec [dest]: "(ALL x:set A. P x) ==> A = Some x ==> P x"  | 
|
52  | 
by simp  | 
|
53  | 
||
| 
51703
 
f2e92fc0c8aa
modifiers for classical wrappers operate on Proof.context instead of claset;
 
wenzelm 
parents: 
51096 
diff
changeset
 | 
54  | 
setup {* map_theory_claset (fn ctxt => ctxt addSD2 ("ospec", @{thm ospec})) *}
 | 
| 30246 | 55  | 
|
56  | 
lemma elem_set [iff]: "(x : set xo) = (xo = Some x)"  | 
|
57  | 
by (cases xo) auto  | 
|
58  | 
||
59  | 
lemma set_empty_eq [simp]: "(set xo = {}) = (xo = None)"
 | 
|
60  | 
by (cases xo) auto  | 
|
61  | 
||
| 31154 | 62  | 
definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a option \<Rightarrow> 'b option" where
 | 
63  | 
"map = (%f y. case y of None => None | Some x => Some (f x))"  | 
|
| 30246 | 64  | 
|
65  | 
lemma option_map_None [simp, code]: "map f None = None"  | 
|
66  | 
by (simp add: map_def)  | 
|
67  | 
||
68  | 
lemma option_map_Some [simp, code]: "map f (Some x) = Some (f x)"  | 
|
69  | 
by (simp add: map_def)  | 
|
70  | 
||
71  | 
lemma option_map_is_None [iff]:  | 
|
72  | 
"(map f opt = None) = (opt = None)"  | 
|
73  | 
by (simp add: map_def split add: option.split)  | 
|
74  | 
||
75  | 
lemma option_map_eq_Some [iff]:  | 
|
76  | 
"(map f xo = Some y) = (EX z. xo = Some z & f z = y)"  | 
|
77  | 
by (simp add: map_def split add: option.split)  | 
|
78  | 
||
79  | 
lemma option_map_comp:  | 
|
80  | 
"map f (map g opt) = map (f o g) opt"  | 
|
81  | 
by (simp add: map_def split add: option.split)  | 
|
82  | 
||
83  | 
lemma option_map_o_sum_case [simp]:  | 
|
84  | 
"map f o sum_case g h = sum_case (map f o g) (map f o h)"  | 
|
85  | 
by (rule ext) (simp split: sum.split)  | 
|
86  | 
||
| 46526 | 87  | 
lemma map_cong: "x = y \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> f a = g a) \<Longrightarrow> map f x = map g y"  | 
88  | 
by (cases x) auto  | 
|
89  | 
||
| 
41505
 
6d19301074cf
"enriched_type" replaces less specific "type_lifting"
 
haftmann 
parents: 
41372 
diff
changeset
 | 
90  | 
enriched_type map: Option.map proof -  | 
| 41372 | 91  | 
fix f g  | 
92  | 
show "Option.map f \<circ> Option.map g = Option.map (f \<circ> g)"  | 
|
93  | 
proof  | 
|
94  | 
fix x  | 
|
95  | 
show "(Option.map f \<circ> Option.map g) x= Option.map (f \<circ> g) x"  | 
|
96  | 
by (cases x) simp_all  | 
|
97  | 
qed  | 
|
| 40609 | 98  | 
next  | 
| 41372 | 99  | 
show "Option.map id = id"  | 
100  | 
proof  | 
|
101  | 
fix x  | 
|
102  | 
show "Option.map id x = id x"  | 
|
103  | 
by (cases x) simp_all  | 
|
104  | 
qed  | 
|
| 40609 | 105  | 
qed  | 
106  | 
||
| 51096 | 107  | 
lemma option_case_map [simp]:  | 
108  | 
"option_case g h (Option.map f x) = option_case g (h \<circ> f) x"  | 
|
109  | 
by (cases x) simp_all  | 
|
110  | 
||
| 39149 | 111  | 
primrec bind :: "'a option \<Rightarrow> ('a \<Rightarrow> 'b option) \<Rightarrow> 'b option" where
 | 
112  | 
bind_lzero: "bind None f = None" |  | 
|
113  | 
bind_lunit: "bind (Some x) f = f x"  | 
|
| 30246 | 114  | 
|
| 39149 | 115  | 
lemma bind_runit[simp]: "bind x Some = x"  | 
116  | 
by (cases x) auto  | 
|
117  | 
||
118  | 
lemma bind_assoc[simp]: "bind (bind x f) g = bind x (\<lambda>y. bind (f y) g)"  | 
|
119  | 
by (cases x) auto  | 
|
120  | 
||
121  | 
lemma bind_rzero[simp]: "bind x (\<lambda>x. None) = None"  | 
|
122  | 
by (cases x) auto  | 
|
123  | 
||
| 46526 | 124  | 
lemma bind_cong: "x = y \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> f a = g a) \<Longrightarrow> bind x f = bind y g"  | 
125  | 
by (cases x) auto  | 
|
126  | 
||
| 49189 | 127  | 
definition these :: "'a option set \<Rightarrow> 'a set"  | 
128  | 
where  | 
|
129  | 
  "these A = the ` {x \<in> A. x \<noteq> None}"
 | 
|
130  | 
||
131  | 
lemma these_empty [simp]:  | 
|
132  | 
  "these {} = {}"
 | 
|
133  | 
by (simp add: these_def)  | 
|
134  | 
||
135  | 
lemma these_insert_None [simp]:  | 
|
136  | 
"these (insert None A) = these A"  | 
|
137  | 
by (auto simp add: these_def)  | 
|
138  | 
||
139  | 
lemma these_insert_Some [simp]:  | 
|
140  | 
"these (insert (Some x) A) = insert x (these A)"  | 
|
141  | 
proof -  | 
|
142  | 
  have "{y \<in> insert (Some x) A. y \<noteq> None} = insert (Some x) {y \<in> A. y \<noteq> None}"
 | 
|
143  | 
by auto  | 
|
144  | 
then show ?thesis by (simp add: these_def)  | 
|
145  | 
qed  | 
|
146  | 
||
147  | 
lemma in_these_eq:  | 
|
148  | 
"x \<in> these A \<longleftrightarrow> Some x \<in> A"  | 
|
149  | 
proof  | 
|
150  | 
assume "Some x \<in> A"  | 
|
151  | 
then obtain B where "A = insert (Some x) B" by auto  | 
|
152  | 
then show "x \<in> these A" by (auto simp add: these_def intro!: image_eqI)  | 
|
153  | 
next  | 
|
154  | 
assume "x \<in> these A"  | 
|
155  | 
then show "Some x \<in> A" by (auto simp add: these_def)  | 
|
156  | 
qed  | 
|
157  | 
||
158  | 
lemma these_image_Some_eq [simp]:  | 
|
159  | 
"these (Some ` A) = A"  | 
|
160  | 
by (auto simp add: these_def intro!: image_eqI)  | 
|
161  | 
||
162  | 
lemma Some_image_these_eq:  | 
|
163  | 
  "Some ` these A = {x\<in>A. x \<noteq> None}"
 | 
|
164  | 
by (auto simp add: these_def image_image intro!: image_eqI)  | 
|
165  | 
||
166  | 
lemma these_empty_eq:  | 
|
167  | 
  "these B = {} \<longleftrightarrow> B = {} \<or> B = {None}"
 | 
|
168  | 
by (auto simp add: these_def)  | 
|
169  | 
||
170  | 
lemma these_not_empty_eq:  | 
|
171  | 
  "these B \<noteq> {} \<longleftrightarrow> B \<noteq> {} \<and> B \<noteq> {None}"
 | 
|
172  | 
by (auto simp add: these_empty_eq)  | 
|
173  | 
||
174  | 
hide_const (open) set map bind these  | 
|
| 46526 | 175  | 
hide_fact (open) map_cong bind_cong  | 
| 30246 | 176  | 
|
| 49189 | 177  | 
|
| 
55089
 
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
 
blanchet 
parents: 
53940 
diff
changeset
 | 
178  | 
subsubsection {* Interaction with finite sets *}
 | 
| 
 
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
 
blanchet 
parents: 
53940 
diff
changeset
 | 
179  | 
|
| 
 
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
 
blanchet 
parents: 
53940 
diff
changeset
 | 
180  | 
lemma finite_option_UNIV [simp]:  | 
| 
 
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
 
blanchet 
parents: 
53940 
diff
changeset
 | 
181  | 
"finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"  | 
| 
 
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
 
blanchet 
parents: 
53940 
diff
changeset
 | 
182  | 
by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)  | 
| 
 
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
 
blanchet 
parents: 
53940 
diff
changeset
 | 
183  | 
|
| 
 
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
 
blanchet 
parents: 
53940 
diff
changeset
 | 
184  | 
instance option :: (finite) finite  | 
| 
 
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
 
blanchet 
parents: 
53940 
diff
changeset
 | 
185  | 
by default (simp add: UNIV_option_conv)  | 
| 
 
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
 
blanchet 
parents: 
53940 
diff
changeset
 | 
186  | 
|
| 
 
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
 
blanchet 
parents: 
53940 
diff
changeset
 | 
187  | 
|
| 30246 | 188  | 
subsubsection {* Code generator setup *}
 | 
189  | 
||
| 31154 | 190  | 
definition is_none :: "'a option \<Rightarrow> bool" where  | 
| 
31998
 
2c7a24f74db9
code attributes use common underscore convention
 
haftmann 
parents: 
31154 
diff
changeset
 | 
191  | 
[code_post]: "is_none x \<longleftrightarrow> x = None"  | 
| 30246 | 192  | 
|
193  | 
lemma is_none_code [code]:  | 
|
194  | 
shows "is_none None \<longleftrightarrow> True"  | 
|
195  | 
and "is_none (Some x) \<longleftrightarrow> False"  | 
|
| 31154 | 196  | 
unfolding is_none_def by simp_all  | 
197  | 
||
| 
32069
 
6d28bbd33e2c
prefer code_inline over code_unfold; use code_unfold_post where appropriate
 
haftmann 
parents: 
31998 
diff
changeset
 | 
198  | 
lemma [code_unfold]:  | 
| 
38857
 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 
haftmann 
parents: 
37880 
diff
changeset
 | 
199  | 
"HOL.equal x None \<longleftrightarrow> is_none x"  | 
| 
53940
 
36cf426cb1c6
Added symmetric code_unfold-lemmas for null and is_none
 
lammich <lammich@in.tum.de> 
parents: 
53010 
diff
changeset
 | 
200  | 
"HOL.equal None = is_none"  | 
| 
 
36cf426cb1c6
Added symmetric code_unfold-lemmas for null and is_none
 
lammich <lammich@in.tum.de> 
parents: 
53010 
diff
changeset
 | 
201  | 
by (auto simp add: equal is_none_def)  | 
| 30246 | 202  | 
|
| 
36176
 
3fe7e97ccca8
replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
 
wenzelm 
parents: 
35719 
diff
changeset
 | 
203  | 
hide_const (open) is_none  | 
| 30246 | 204  | 
|
| 
52435
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
205  | 
code_printing  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
206  | 
type_constructor option \<rightharpoonup>  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
207  | 
(SML) "_ option"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
208  | 
and (OCaml) "_ option"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
209  | 
and (Haskell) "Maybe _"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
210  | 
and (Scala) "!Option[(_)]"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
211  | 
| constant None \<rightharpoonup>  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
212  | 
(SML) "NONE"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
213  | 
and (OCaml) "None"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
214  | 
and (Haskell) "Nothing"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
215  | 
and (Scala) "!None"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
216  | 
| constant Some \<rightharpoonup>  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
217  | 
(SML) "SOME"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
218  | 
and (OCaml) "Some _"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
219  | 
and (Haskell) "Just"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
220  | 
and (Scala) "Some"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
221  | 
| class_instance option :: equal \<rightharpoonup>  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
222  | 
(Haskell) -  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
223  | 
| constant "HOL.equal :: 'a option \<Rightarrow> 'a option \<Rightarrow> bool" \<rightharpoonup>  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
51703 
diff
changeset
 | 
224  | 
(Haskell) infix 4 "=="  | 
| 30246 | 225  | 
|
226  | 
code_reserved SML  | 
|
227  | 
option NONE SOME  | 
|
228  | 
||
229  | 
code_reserved OCaml  | 
|
230  | 
option None Some  | 
|
231  | 
||
| 34886 | 232  | 
code_reserved Scala  | 
233  | 
Option None Some  | 
|
234  | 
||
| 30246 | 235  | 
end  | 
| 49189 | 236  |