1 (* Title: HOL/Option.thy
5 header {* Datatype option *}
8 imports BNF_LFP Datatype Finite_Set
11 datatype_new 'a option =
14 datatype_compat option
16 lemma [case_names None Some, cases type: option]:
17 -- {* for backward compatibility -- names of variables differ *}
18 "(y = None \<Longrightarrow> P) \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> P) \<Longrightarrow> P"
19 by (rule option.exhaust)
21 lemma [case_names None Some, induct type: option]:
22 -- {* for backward compatibility -- names of variables differ *}
23 "P None \<Longrightarrow> (\<And>option. P (Some option)) \<Longrightarrow> P option"
24 by (rule option.induct)
26 text {* Compatibility: *}
28 setup {* Sign.mandatory_path "option" *}
30 lemmas inducts = option.induct
31 lemmas cases = option.case
33 setup {* Sign.parent_path *}
35 lemma not_None_eq [iff]: "(x ~= None) = (EX y. x = Some y)"
38 lemma not_Some_eq [iff]: "(ALL y. x ~= Some y) = (x = None)"
41 text{*Although it may appear that both of these equalities are helpful
42 only when applied to assumptions, in practice it seems better to give
43 them the uniform iff attribute. *}
45 lemma inj_Some [simp]: "inj_on Some A"
46 by (rule inj_onI) simp
49 assumes c: "(case x of None => P | Some y => Q y)"
51 (None) "x = None" and P
52 | (Some) y where "x = Some y" and "Q y"
53 using c by (cases x) simp_all
55 lemma split_option_all: "(\<forall>x. P x) \<longleftrightarrow> P None \<and> (\<forall>x. P (Some x))"
56 by (auto intro: option.induct)
58 lemma split_option_ex: "(\<exists>x. P x) \<longleftrightarrow> P None \<or> (\<exists>x. P (Some x))"
59 using split_option_all[of "\<lambda>x. \<not>P x"] by blast
61 lemma UNIV_option_conv: "UNIV = insert None (range Some)"
62 by(auto intro: classical)
64 subsubsection {* Operations *}
66 lemma ospec [dest]: "(ALL x:set_option A. P x) ==> A = Some x ==> P x"
69 setup {* map_theory_claset (fn ctxt => ctxt addSD2 ("ospec", @{thm ospec})) *}
71 lemma elem_set [iff]: "(x : set_option xo) = (xo = Some x)"
74 lemma set_empty_eq [simp]: "(set_option xo = {}) = (xo = None)"
77 lemma map_option_case: "map_option f y = (case y of None => None | Some x => Some (f x))"
78 by (auto split: option.split)
80 lemma map_option_is_None [iff]:
81 "(map_option f opt = None) = (opt = None)"
82 by (simp add: map_option_case split add: option.split)
84 lemma map_option_eq_Some [iff]:
85 "(map_option f xo = Some y) = (EX z. xo = Some z & f z = y)"
86 by (simp add: map_option_case split add: option.split)
88 lemma map_option_o_case_sum [simp]:
89 "map_option f o case_sum g h = case_sum (map_option f o g) (map_option f o h)"
92 lemma map_option_cong: "x = y \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> f a = g a) \<Longrightarrow> map_option f x = map_option g y"
95 functor map_option: map_option proof -
97 show "map_option f \<circ> map_option g = map_option (f \<circ> g)"
100 show "(map_option f \<circ> map_option g) x= map_option (f \<circ> g) x"
101 by (cases x) simp_all
104 show "map_option id = id"
107 show "map_option id x = id x"
108 by (cases x) simp_all
112 lemma case_map_option [simp]:
113 "case_option g h (map_option f x) = case_option g (h \<circ> f) x"
114 by (cases x) simp_all
116 primrec bind :: "'a option \<Rightarrow> ('a \<Rightarrow> 'b option) \<Rightarrow> 'b option" where
117 bind_lzero: "bind None f = None" |
118 bind_lunit: "bind (Some x) f = f x"
120 lemma bind_runit[simp]: "bind x Some = x"
123 lemma bind_assoc[simp]: "bind (bind x f) g = bind x (\<lambda>y. bind (f y) g)"
126 lemma bind_rzero[simp]: "bind x (\<lambda>x. None) = None"
129 lemma bind_cong: "x = y \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> f a = g a) \<Longrightarrow> bind x f = bind y g"
132 definition these :: "'a option set \<Rightarrow> 'a set"
134 "these A = the ` {x \<in> A. x \<noteq> None}"
136 lemma these_empty [simp]:
138 by (simp add: these_def)
140 lemma these_insert_None [simp]:
141 "these (insert None A) = these A"
142 by (auto simp add: these_def)
144 lemma these_insert_Some [simp]:
145 "these (insert (Some x) A) = insert x (these A)"
147 have "{y \<in> insert (Some x) A. y \<noteq> None} = insert (Some x) {y \<in> A. y \<noteq> None}"
149 then show ?thesis by (simp add: these_def)
153 "x \<in> these A \<longleftrightarrow> Some x \<in> A"
155 assume "Some x \<in> A"
156 then obtain B where "A = insert (Some x) B" by auto
157 then show "x \<in> these A" by (auto simp add: these_def intro!: image_eqI)
159 assume "x \<in> these A"
160 then show "Some x \<in> A" by (auto simp add: these_def)
163 lemma these_image_Some_eq [simp]:
164 "these (Some ` A) = A"
165 by (auto simp add: these_def intro!: image_eqI)
167 lemma Some_image_these_eq:
168 "Some ` these A = {x\<in>A. x \<noteq> None}"
169 by (auto simp add: these_def image_image intro!: image_eqI)
171 lemma these_empty_eq:
172 "these B = {} \<longleftrightarrow> B = {} \<or> B = {None}"
173 by (auto simp add: these_def)
175 lemma these_not_empty_eq:
176 "these B \<noteq> {} \<longleftrightarrow> B \<noteq> {} \<and> B \<noteq> {None}"
177 by (auto simp add: these_empty_eq)
179 hide_const (open) bind these
180 hide_fact (open) bind_cong
183 subsubsection {* Interaction with finite sets *}
185 lemma finite_option_UNIV [simp]:
186 "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
187 by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
189 instance option :: (finite) finite
190 by default (simp add: UNIV_option_conv)
193 subsubsection {* Code generator setup *}
195 definition is_none :: "'a option \<Rightarrow> bool" where
196 [code_post]: "is_none x \<longleftrightarrow> x = None"
198 lemma is_none_code [code]:
199 shows "is_none None \<longleftrightarrow> True"
200 and "is_none (Some x) \<longleftrightarrow> False"
201 unfolding is_none_def by simp_all
204 "HOL.equal x None \<longleftrightarrow> is_none x"
205 "HOL.equal None = is_none"
206 by (auto simp add: equal is_none_def)
208 hide_const (open) is_none
211 type_constructor option \<rightharpoonup>
213 and (OCaml) "_ option"
214 and (Haskell) "Maybe _"
215 and (Scala) "!Option[(_)]"
216 | constant None \<rightharpoonup>
219 and (Haskell) "Nothing"
221 | constant Some \<rightharpoonup>
226 | class_instance option :: equal \<rightharpoonup>
228 | constant "HOL.equal :: 'a option \<Rightarrow> 'a option \<Rightarrow> bool" \<rightharpoonup>
229 (Haskell) infix 4 "=="