author | blanchet |
Tue, 10 Jun 2014 19:15:15 +0200 | |
changeset 57204 | 7c36ce8e45f6 |
parent 57123 | b5324647e0f1 |
child 58112 | 8081087096ad |
permissions | -rw-r--r-- |
30246 | 1 |
(* Title: HOL/Option.thy |
2 |
Author: Folklore |
|
3 |
*) |
|
4 |
||
5 |
header {* Datatype option *} |
|
6 |
||
7 |
theory Option |
|
55404
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55129
diff
changeset
|
8 |
imports BNF_LFP Datatype Finite_Set |
30246 | 9 |
begin |
10 |
||
55406 | 11 |
datatype_new 'a option = |
57091 | 12 |
None |
55406 | 13 |
| Some (the: 'a) |
57123
b5324647e0f1
tuned whitespace, to make datatype definitions slightly less intimidating
blanchet
parents:
57091
diff
changeset
|
14 |
|
55531
601ca8efa000
renamed 'datatype_new_compat' to 'datatype_compat'
blanchet
parents:
55518
diff
changeset
|
15 |
datatype_compat option |
55404
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55129
diff
changeset
|
16 |
|
55406 | 17 |
lemma [case_names None Some, cases type: option]: |
18 |
-- {* for backward compatibility -- names of variables differ *} |
|
55417
01fbfb60c33e
adapted to 'xxx_{case,rec}' renaming, to new theorem names, and to new variable names in theorems
blanchet
parents:
55414
diff
changeset
|
19 |
"(y = None \<Longrightarrow> P) \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> P) \<Longrightarrow> P" |
55406 | 20 |
by (rule option.exhaust) |
21 |
||
22 |
lemma [case_names None Some, induct type: option]: |
|
23 |
-- {* for backward compatibility -- names of variables differ *} |
|
24 |
"P None \<Longrightarrow> (\<And>option. P (Some option)) \<Longrightarrow> P option" |
|
25 |
by (rule option.induct) |
|
26 |
||
55442 | 27 |
text {* Compatibility: *} |
28 |
||
55404
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55129
diff
changeset
|
29 |
setup {* Sign.mandatory_path "option" *} |
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55129
diff
changeset
|
30 |
|
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55129
diff
changeset
|
31 |
lemmas inducts = option.induct |
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55129
diff
changeset
|
32 |
lemmas cases = option.case |
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55129
diff
changeset
|
33 |
|
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55129
diff
changeset
|
34 |
setup {* Sign.parent_path *} |
30246 | 35 |
|
36 |
lemma not_None_eq [iff]: "(x ~= None) = (EX y. x = Some y)" |
|
37 |
by (induct x) auto |
|
38 |
||
39 |
lemma not_Some_eq [iff]: "(ALL y. x ~= Some y) = (x = None)" |
|
40 |
by (induct x) auto |
|
41 |
||
42 |
text{*Although it may appear that both of these equalities are helpful |
|
43 |
only when applied to assumptions, in practice it seems better to give |
|
44 |
them the uniform iff attribute. *} |
|
45 |
||
31080 | 46 |
lemma inj_Some [simp]: "inj_on Some A" |
47 |
by (rule inj_onI) simp |
|
48 |
||
55404
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55129
diff
changeset
|
49 |
lemma case_optionE: |
30246 | 50 |
assumes c: "(case x of None => P | Some y => Q y)" |
51 |
obtains |
|
52 |
(None) "x = None" and P |
|
53 |
| (Some) y where "x = Some y" and "Q y" |
|
54 |
using c by (cases x) simp_all |
|
55 |
||
53010 | 56 |
lemma split_option_all: "(\<forall>x. P x) \<longleftrightarrow> P None \<and> (\<forall>x. P (Some x))" |
57 |
by (auto intro: option.induct) |
|
58 |
||
59 |
lemma split_option_ex: "(\<exists>x. P x) \<longleftrightarrow> P None \<or> (\<exists>x. P (Some x))" |
|
60 |
using split_option_all[of "\<lambda>x. \<not>P x"] by blast |
|
61 |
||
31080 | 62 |
lemma UNIV_option_conv: "UNIV = insert None (range Some)" |
63 |
by(auto intro: classical) |
|
64 |
||
30246 | 65 |
subsubsection {* Operations *} |
66 |
||
55518
1ddb2edf5ceb
folded 'Option.set' into BNF-generated 'set_option'
blanchet
parents:
55467
diff
changeset
|
67 |
lemma ospec [dest]: "(ALL x:set_option A. P x) ==> A = Some x ==> P x" |
30246 | 68 |
by simp |
69 |
||
51703
f2e92fc0c8aa
modifiers for classical wrappers operate on Proof.context instead of claset;
wenzelm
parents:
51096
diff
changeset
|
70 |
setup {* map_theory_claset (fn ctxt => ctxt addSD2 ("ospec", @{thm ospec})) *} |
30246 | 71 |
|
55518
1ddb2edf5ceb
folded 'Option.set' into BNF-generated 'set_option'
blanchet
parents:
55467
diff
changeset
|
72 |
lemma elem_set [iff]: "(x : set_option xo) = (xo = Some x)" |
30246 | 73 |
by (cases xo) auto |
74 |
||
55518
1ddb2edf5ceb
folded 'Option.set' into BNF-generated 'set_option'
blanchet
parents:
55467
diff
changeset
|
75 |
lemma set_empty_eq [simp]: "(set_option xo = {}) = (xo = None)" |
30246 | 76 |
by (cases xo) auto |
77 |
||
55466 | 78 |
lemma map_option_case: "map_option f y = (case y of None => None | Some x => Some (f x))" |
79 |
by (auto split: option.split) |
|
30246 | 80 |
|
55466 | 81 |
lemma map_option_is_None [iff]: |
82 |
"(map_option f opt = None) = (opt = None)" |
|
83 |
by (simp add: map_option_case split add: option.split) |
|
30246 | 84 |
|
55466 | 85 |
lemma map_option_eq_Some [iff]: |
86 |
"(map_option f xo = Some y) = (EX z. xo = Some z & f z = y)" |
|
87 |
by (simp add: map_option_case split add: option.split) |
|
30246 | 88 |
|
55466 | 89 |
lemma map_option_o_case_sum [simp]: |
90 |
"map_option f o case_sum g h = case_sum (map_option f o g) (map_option f o h)" |
|
91 |
by (rule o_case_sum) |
|
30246 | 92 |
|
55466 | 93 |
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" |
46526 | 94 |
by (cases x) auto |
95 |
||
55467
a5c9002bc54d
renamed 'enriched_type' to more informative 'functor' (following the renaming of enriched type constructors to bounded natural functors)
blanchet
parents:
55466
diff
changeset
|
96 |
functor map_option: map_option proof - |
41372 | 97 |
fix f g |
55466 | 98 |
show "map_option f \<circ> map_option g = map_option (f \<circ> g)" |
41372 | 99 |
proof |
100 |
fix x |
|
55466 | 101 |
show "(map_option f \<circ> map_option g) x= map_option (f \<circ> g) x" |
41372 | 102 |
by (cases x) simp_all |
103 |
qed |
|
40609 | 104 |
next |
55466 | 105 |
show "map_option id = id" |
41372 | 106 |
proof |
107 |
fix x |
|
55466 | 108 |
show "map_option id x = id x" |
41372 | 109 |
by (cases x) simp_all |
110 |
qed |
|
40609 | 111 |
qed |
112 |
||
55466 | 113 |
lemma case_map_option [simp]: |
114 |
"case_option g h (map_option f x) = case_option g (h \<circ> f) x" |
|
51096 | 115 |
by (cases x) simp_all |
116 |
||
39149 | 117 |
primrec bind :: "'a option \<Rightarrow> ('a \<Rightarrow> 'b option) \<Rightarrow> 'b option" where |
118 |
bind_lzero: "bind None f = None" | |
|
119 |
bind_lunit: "bind (Some x) f = f x" |
|
30246 | 120 |
|
39149 | 121 |
lemma bind_runit[simp]: "bind x Some = x" |
122 |
by (cases x) auto |
|
123 |
||
124 |
lemma bind_assoc[simp]: "bind (bind x f) g = bind x (\<lambda>y. bind (f y) g)" |
|
125 |
by (cases x) auto |
|
126 |
||
127 |
lemma bind_rzero[simp]: "bind x (\<lambda>x. None) = None" |
|
128 |
by (cases x) auto |
|
129 |
||
46526 | 130 |
lemma bind_cong: "x = y \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> f a = g a) \<Longrightarrow> bind x f = bind y g" |
131 |
by (cases x) auto |
|
132 |
||
49189 | 133 |
definition these :: "'a option set \<Rightarrow> 'a set" |
134 |
where |
|
135 |
"these A = the ` {x \<in> A. x \<noteq> None}" |
|
136 |
||
137 |
lemma these_empty [simp]: |
|
138 |
"these {} = {}" |
|
139 |
by (simp add: these_def) |
|
140 |
||
141 |
lemma these_insert_None [simp]: |
|
142 |
"these (insert None A) = these A" |
|
143 |
by (auto simp add: these_def) |
|
144 |
||
145 |
lemma these_insert_Some [simp]: |
|
146 |
"these (insert (Some x) A) = insert x (these A)" |
|
147 |
proof - |
|
148 |
have "{y \<in> insert (Some x) A. y \<noteq> None} = insert (Some x) {y \<in> A. y \<noteq> None}" |
|
149 |
by auto |
|
150 |
then show ?thesis by (simp add: these_def) |
|
151 |
qed |
|
152 |
||
153 |
lemma in_these_eq: |
|
154 |
"x \<in> these A \<longleftrightarrow> Some x \<in> A" |
|
155 |
proof |
|
156 |
assume "Some x \<in> A" |
|
157 |
then obtain B where "A = insert (Some x) B" by auto |
|
158 |
then show "x \<in> these A" by (auto simp add: these_def intro!: image_eqI) |
|
159 |
next |
|
160 |
assume "x \<in> these A" |
|
161 |
then show "Some x \<in> A" by (auto simp add: these_def) |
|
162 |
qed |
|
163 |
||
164 |
lemma these_image_Some_eq [simp]: |
|
165 |
"these (Some ` A) = A" |
|
166 |
by (auto simp add: these_def intro!: image_eqI) |
|
167 |
||
168 |
lemma Some_image_these_eq: |
|
169 |
"Some ` these A = {x\<in>A. x \<noteq> None}" |
|
170 |
by (auto simp add: these_def image_image intro!: image_eqI) |
|
171 |
||
172 |
lemma these_empty_eq: |
|
173 |
"these B = {} \<longleftrightarrow> B = {} \<or> B = {None}" |
|
174 |
by (auto simp add: these_def) |
|
175 |
||
176 |
lemma these_not_empty_eq: |
|
177 |
"these B \<noteq> {} \<longleftrightarrow> B \<noteq> {} \<and> B \<noteq> {None}" |
|
178 |
by (auto simp add: these_empty_eq) |
|
179 |
||
55518
1ddb2edf5ceb
folded 'Option.set' into BNF-generated 'set_option'
blanchet
parents:
55467
diff
changeset
|
180 |
hide_const (open) bind these |
55466 | 181 |
hide_fact (open) bind_cong |
30246 | 182 |
|
49189 | 183 |
|
55089
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents:
53940
diff
changeset
|
184 |
subsubsection {* Interaction with finite sets *} |
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents:
53940
diff
changeset
|
185 |
|
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents:
53940
diff
changeset
|
186 |
lemma finite_option_UNIV [simp]: |
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents:
53940
diff
changeset
|
187 |
"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
|
188 |
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
|
189 |
|
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents:
53940
diff
changeset
|
190 |
instance option :: (finite) finite |
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents:
53940
diff
changeset
|
191 |
by default (simp add: UNIV_option_conv) |
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents:
53940
diff
changeset
|
192 |
|
181751ad852f
swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents:
53940
diff
changeset
|
193 |
|
30246 | 194 |
subsubsection {* Code generator setup *} |
195 |
||
31154 | 196 |
definition is_none :: "'a option \<Rightarrow> bool" where |
31998
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31154
diff
changeset
|
197 |
[code_post]: "is_none x \<longleftrightarrow> x = None" |
30246 | 198 |
|
199 |
lemma is_none_code [code]: |
|
200 |
shows "is_none None \<longleftrightarrow> True" |
|
201 |
and "is_none (Some x) \<longleftrightarrow> False" |
|
31154 | 202 |
unfolding is_none_def by simp_all |
203 |
||
32069
6d28bbd33e2c
prefer code_inline over code_unfold; use code_unfold_post where appropriate
haftmann
parents:
31998
diff
changeset
|
204 |
lemma [code_unfold]: |
38857
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
37880
diff
changeset
|
205 |
"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
|
206 |
"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
|
207 |
by (auto simp add: equal is_none_def) |
30246 | 208 |
|
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
|
209 |
hide_const (open) is_none |
30246 | 210 |
|
52435
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
51703
diff
changeset
|
211 |
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
|
212 |
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
|
213 |
(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
|
214 |
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
|
215 |
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
|
216 |
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
|
217 |
| 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
|
218 |
(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
|
219 |
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
|
220 |
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
|
221 |
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
|
222 |
| 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
|
223 |
(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
|
224 |
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
|
225 |
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
|
226 |
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
|
227 |
| 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
|
228 |
(Haskell) - |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
51703
diff
changeset
|
229 |
| 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
|
230 |
(Haskell) infix 4 "==" |
30246 | 231 |
|
232 |
code_reserved SML |
|
233 |
option NONE SOME |
|
234 |
||
235 |
code_reserved OCaml |
|
236 |
option None Some |
|
237 |
||
34886 | 238 |
code_reserved Scala |
239 |
Option None Some |
|
240 |
||
30246 | 241 |
end |