src/HOL/Option.thy
author wenzelm
Sat Jul 18 22:58:50 2015 +0200 (2015-07-18)
changeset 60758 d8d85a8172b5
parent 59523 860fb1c65553
child 61066 00a169fe5de4
permissions -rw-r--r--
isabelle update_cartouches;
nipkow@30246
     1
(*  Title:      HOL/Option.thy
nipkow@30246
     2
    Author:     Folklore
nipkow@30246
     3
*)
nipkow@30246
     4
wenzelm@60758
     5
section \<open>Datatype option\<close>
nipkow@30246
     6
nipkow@30246
     7
theory Option
traytel@58916
     8
imports Lifting Finite_Set
nipkow@30246
     9
begin
nipkow@30246
    10
blanchet@58310
    11
datatype 'a option =
blanchet@57091
    12
    None
blanchet@55406
    13
  | Some (the: 'a)
blanchet@57123
    14
blanchet@55531
    15
datatype_compat option
blanchet@55404
    16
blanchet@55406
    17
lemma [case_names None Some, cases type: option]:
wenzelm@60758
    18
  -- \<open>for backward compatibility -- names of variables differ\<close>
blanchet@55417
    19
  "(y = None \<Longrightarrow> P) \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> P) \<Longrightarrow> P"
blanchet@55406
    20
by (rule option.exhaust)
blanchet@55406
    21
blanchet@55406
    22
lemma [case_names None Some, induct type: option]:
wenzelm@60758
    23
  -- \<open>for backward compatibility -- names of variables differ\<close>
blanchet@55406
    24
  "P None \<Longrightarrow> (\<And>option. P (Some option)) \<Longrightarrow> P option"
blanchet@55406
    25
by (rule option.induct)
blanchet@55406
    26
wenzelm@60758
    27
text \<open>Compatibility:\<close>
blanchet@55442
    28
wenzelm@60758
    29
setup \<open>Sign.mandatory_path "option"\<close>
blanchet@55404
    30
blanchet@55404
    31
lemmas inducts = option.induct
blanchet@55404
    32
lemmas cases = option.case
blanchet@55404
    33
wenzelm@60758
    34
setup \<open>Sign.parent_path\<close>
nipkow@30246
    35
nipkow@30246
    36
lemma not_None_eq [iff]: "(x ~= None) = (EX y. x = Some y)"
nipkow@30246
    37
  by (induct x) auto
nipkow@30246
    38
nipkow@30246
    39
lemma not_Some_eq [iff]: "(ALL y. x ~= Some y) = (x = None)"
nipkow@30246
    40
  by (induct x) auto
nipkow@30246
    41
wenzelm@60758
    42
text\<open>Although it may appear that both of these equalities are helpful
nipkow@30246
    43
only when applied to assumptions, in practice it seems better to give
wenzelm@60758
    44
them the uniform iff attribute.\<close>
nipkow@30246
    45
nipkow@31080
    46
lemma inj_Some [simp]: "inj_on Some A"
nipkow@31080
    47
by (rule inj_onI) simp
nipkow@31080
    48
blanchet@55404
    49
lemma case_optionE:
nipkow@30246
    50
  assumes c: "(case x of None => P | Some y => Q y)"
nipkow@30246
    51
  obtains
nipkow@30246
    52
    (None) "x = None" and P
nipkow@30246
    53
  | (Some) y where "x = Some y" and "Q y"
nipkow@30246
    54
  using c by (cases x) simp_all
nipkow@30246
    55
kuncar@53010
    56
lemma split_option_all: "(\<forall>x. P x) \<longleftrightarrow> P None \<and> (\<forall>x. P (Some x))"
kuncar@53010
    57
by (auto intro: option.induct)
kuncar@53010
    58
kuncar@53010
    59
lemma split_option_ex: "(\<exists>x. P x) \<longleftrightarrow> P None \<or> (\<exists>x. P (Some x))"
kuncar@53010
    60
using split_option_all[of "\<lambda>x. \<not>P x"] by blast
kuncar@53010
    61
nipkow@31080
    62
lemma UNIV_option_conv: "UNIV = insert None (range Some)"
nipkow@31080
    63
by(auto intro: classical)
nipkow@31080
    64
Andreas@59522
    65
lemma rel_option_None1 [simp]: "rel_option P None x \<longleftrightarrow> x = None"
Andreas@59522
    66
by(cases x) simp_all
Andreas@59522
    67
Andreas@59522
    68
lemma rel_option_None2 [simp]: "rel_option P x None \<longleftrightarrow> x = None"
Andreas@59522
    69
by(cases x) simp_all
Andreas@59522
    70
Andreas@59522
    71
lemma rel_option_inf: "inf (rel_option A) (rel_option B) = rel_option (inf A B)" (is "?lhs = ?rhs")
Andreas@59522
    72
proof(rule antisym)
Andreas@59522
    73
  show "?lhs \<le> ?rhs" by(auto elim!: option.rel_cases)
Andreas@59522
    74
qed(auto elim: option.rel_mono_strong)
Andreas@59522
    75
Andreas@59522
    76
lemma rel_option_reflI:
Andreas@59522
    77
  "(\<And>x. x \<in> set_option y \<Longrightarrow> P x x) \<Longrightarrow> rel_option P y y"
Andreas@59522
    78
by(cases y) auto
Andreas@59522
    79
Andreas@59523
    80
wenzelm@60758
    81
subsubsection \<open>Operations\<close>
nipkow@30246
    82
blanchet@55518
    83
lemma ospec [dest]: "(ALL x:set_option A. P x) ==> A = Some x ==> P x"
nipkow@30246
    84
  by simp
nipkow@30246
    85
wenzelm@60758
    86
setup \<open>map_theory_claset (fn ctxt => ctxt addSD2 ("ospec", @{thm ospec}))\<close>
nipkow@30246
    87
blanchet@55518
    88
lemma elem_set [iff]: "(x : set_option xo) = (xo = Some x)"
nipkow@30246
    89
  by (cases xo) auto
nipkow@30246
    90
blanchet@55518
    91
lemma set_empty_eq [simp]: "(set_option xo = {}) = (xo = None)"
nipkow@30246
    92
  by (cases xo) auto
nipkow@30246
    93
blanchet@55466
    94
lemma map_option_case: "map_option f y = (case y of None => None | Some x => Some (f x))"
blanchet@55466
    95
  by (auto split: option.split)
nipkow@30246
    96
blanchet@55466
    97
lemma map_option_is_None [iff]:
blanchet@55466
    98
    "(map_option f opt = None) = (opt = None)"
blanchet@55466
    99
  by (simp add: map_option_case split add: option.split)
nipkow@30246
   100
blanchet@55466
   101
lemma map_option_eq_Some [iff]:
blanchet@55466
   102
    "(map_option f xo = Some y) = (EX z. xo = Some z & f z = y)"
blanchet@55466
   103
  by (simp add: map_option_case split add: option.split)
nipkow@30246
   104
blanchet@55466
   105
lemma map_option_o_case_sum [simp]:
blanchet@55466
   106
    "map_option f o case_sum g h = case_sum (map_option f o g) (map_option f o h)"
blanchet@55466
   107
  by (rule o_case_sum)
nipkow@30246
   108
blanchet@55466
   109
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"
krauss@46526
   110
by (cases x) auto
krauss@46526
   111
Andreas@59521
   112
functor map_option: map_option
Andreas@59521
   113
by(simp_all add: option.map_comp fun_eq_iff option.map_id)
haftmann@40609
   114
blanchet@55466
   115
lemma case_map_option [simp]:
blanchet@55466
   116
  "case_option g h (map_option f x) = case_option g (h \<circ> f) x"
haftmann@51096
   117
  by (cases x) simp_all
haftmann@51096
   118
traytel@58916
   119
lemma rel_option_iff:
traytel@58916
   120
  "rel_option R x y = (case (x, y) of (None, None) \<Rightarrow> True
traytel@58916
   121
    | (Some x, Some y) \<Rightarrow> R x y
traytel@58916
   122
    | _ \<Rightarrow> False)"
traytel@58916
   123
by (auto split: prod.split option.split)
traytel@58916
   124
Andreas@59522
   125
definition is_none :: "'a option \<Rightarrow> bool"
Andreas@59522
   126
where [code_post]: "is_none x \<longleftrightarrow> x = None"
Andreas@59522
   127
Andreas@59522
   128
lemma is_none_simps [simp]:
Andreas@59522
   129
  "is_none None"
Andreas@59522
   130
  "\<not> is_none (Some x)"
Andreas@59522
   131
by(simp_all add: is_none_def)
Andreas@59522
   132
Andreas@59522
   133
lemma is_none_code [code]:
Andreas@59522
   134
  "is_none None = True"
Andreas@59522
   135
  "is_none (Some x) = False"
Andreas@59522
   136
by simp_all
Andreas@59522
   137
Andreas@59522
   138
lemma rel_option_unfold:
Andreas@59522
   139
  "rel_option R x y \<longleftrightarrow>
Andreas@59522
   140
   (is_none x \<longleftrightarrow> is_none y) \<and> (\<not> is_none x \<longrightarrow> \<not> is_none y \<longrightarrow> R (the x) (the y))"
Andreas@59522
   141
by(simp add: rel_option_iff split: option.split)
Andreas@59522
   142
Andreas@59522
   143
lemma rel_optionI:
Andreas@59522
   144
  "\<lbrakk> is_none x \<longleftrightarrow> is_none y; \<lbrakk> \<not> is_none x; \<not> is_none y \<rbrakk> \<Longrightarrow> P (the x) (the y) \<rbrakk>
Andreas@59522
   145
  \<Longrightarrow> rel_option P x y"
Andreas@59522
   146
by(simp add: rel_option_unfold)
Andreas@59522
   147
Andreas@59522
   148
lemma is_none_map_option [simp]: "is_none (map_option f x) \<longleftrightarrow> is_none x"
Andreas@59522
   149
by(simp add: is_none_def)
Andreas@59522
   150
Andreas@59522
   151
lemma the_map_option: "\<not> is_none x \<Longrightarrow> the (map_option f x) = f (the x)"
Andreas@59522
   152
by(clarsimp simp add: is_none_def)
Andreas@59522
   153
Andreas@59522
   154
krauss@39149
   155
primrec bind :: "'a option \<Rightarrow> ('a \<Rightarrow> 'b option) \<Rightarrow> 'b option" where
krauss@39149
   156
bind_lzero: "bind None f = None" |
krauss@39149
   157
bind_lunit: "bind (Some x) f = f x"
nipkow@30246
   158
Andreas@59522
   159
lemma is_none_bind: "is_none (bind f g) \<longleftrightarrow> is_none f \<or> is_none (g (the f))"
Andreas@59522
   160
by(cases f) simp_all
Andreas@59522
   161
krauss@39149
   162
lemma bind_runit[simp]: "bind x Some = x"
krauss@39149
   163
by (cases x) auto
krauss@39149
   164
krauss@39149
   165
lemma bind_assoc[simp]: "bind (bind x f) g = bind x (\<lambda>y. bind (f y) g)"
krauss@39149
   166
by (cases x) auto
krauss@39149
   167
krauss@39149
   168
lemma bind_rzero[simp]: "bind x (\<lambda>x. None) = None"
krauss@39149
   169
by (cases x) auto
krauss@39149
   170
krauss@46526
   171
lemma bind_cong: "x = y \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> f a = g a) \<Longrightarrow> bind x f = bind y g"
krauss@46526
   172
by (cases x) auto
krauss@46526
   173
lammich@58895
   174
lemma bind_split: "P (bind m f) 
lammich@58895
   175
  \<longleftrightarrow> (m = None \<longrightarrow> P None) \<and> (\<forall>v. m=Some v \<longrightarrow> P (f v))"
lammich@58895
   176
    by (cases m) auto
lammich@58895
   177
lammich@58895
   178
lemma bind_split_asm: "P (bind m f) = (\<not>(
lammich@58895
   179
    m=None \<and> \<not>P None 
lammich@58895
   180
  \<or> (\<exists>x. m=Some x \<and> \<not>P (f x))))"
lammich@58895
   181
  by (cases m) auto
lammich@58895
   182
lammich@58895
   183
lemmas bind_splits = bind_split bind_split_asm
lammich@58895
   184
Andreas@59522
   185
lemma bind_eq_Some_conv: "bind f g = Some x \<longleftrightarrow> (\<exists>y. f = Some y \<and> g y = Some x)"
Andreas@59522
   186
by(cases f) simp_all
Andreas@59522
   187
Andreas@59522
   188
lemma map_option_bind: "map_option f (bind x g) = bind x (map_option f \<circ> g)"
Andreas@59522
   189
by(cases x) simp_all
Andreas@59522
   190
Andreas@59522
   191
lemma bind_option_cong:
Andreas@59522
   192
  "\<lbrakk> x = y; \<And>z. z \<in> set_option y \<Longrightarrow> f z = g z \<rbrakk> \<Longrightarrow> bind x f = bind y g"
Andreas@59522
   193
by(cases y) simp_all
Andreas@59522
   194
Andreas@59522
   195
lemma bind_option_cong_simp:
Andreas@59522
   196
  "\<lbrakk> x = y; \<And>z. z \<in> set_option y =simp=> f z = g z \<rbrakk> \<Longrightarrow> bind x f = bind y g"
Andreas@59522
   197
unfolding simp_implies_def by(rule bind_option_cong)
Andreas@59522
   198
Andreas@59522
   199
lemma bind_option_cong_code: "x = y \<Longrightarrow> bind x f = bind y f" by simp
Andreas@59522
   200
setup \<open>Code_Simp.map_ss (Simplifier.add_cong @{thm bind_option_cong_code})\<close>
Andreas@59522
   201
Andreas@59522
   202
haftmann@49189
   203
definition these :: "'a option set \<Rightarrow> 'a set"
haftmann@49189
   204
where
haftmann@49189
   205
  "these A = the ` {x \<in> A. x \<noteq> None}"
haftmann@49189
   206
haftmann@49189
   207
lemma these_empty [simp]:
haftmann@49189
   208
  "these {} = {}"
haftmann@49189
   209
  by (simp add: these_def)
haftmann@49189
   210
haftmann@49189
   211
lemma these_insert_None [simp]:
haftmann@49189
   212
  "these (insert None A) = these A"
haftmann@49189
   213
  by (auto simp add: these_def)
haftmann@49189
   214
haftmann@49189
   215
lemma these_insert_Some [simp]:
haftmann@49189
   216
  "these (insert (Some x) A) = insert x (these A)"
haftmann@49189
   217
proof -
haftmann@49189
   218
  have "{y \<in> insert (Some x) A. y \<noteq> None} = insert (Some x) {y \<in> A. y \<noteq> None}"
haftmann@49189
   219
    by auto
haftmann@49189
   220
  then show ?thesis by (simp add: these_def)
haftmann@49189
   221
qed
haftmann@49189
   222
haftmann@49189
   223
lemma in_these_eq:
haftmann@49189
   224
  "x \<in> these A \<longleftrightarrow> Some x \<in> A"
haftmann@49189
   225
proof
haftmann@49189
   226
  assume "Some x \<in> A"
haftmann@49189
   227
  then obtain B where "A = insert (Some x) B" by auto
haftmann@49189
   228
  then show "x \<in> these A" by (auto simp add: these_def intro!: image_eqI)
haftmann@49189
   229
next
haftmann@49189
   230
  assume "x \<in> these A"
haftmann@49189
   231
  then show "Some x \<in> A" by (auto simp add: these_def)
haftmann@49189
   232
qed
haftmann@49189
   233
haftmann@49189
   234
lemma these_image_Some_eq [simp]:
haftmann@49189
   235
  "these (Some ` A) = A"
haftmann@49189
   236
  by (auto simp add: these_def intro!: image_eqI)
haftmann@49189
   237
haftmann@49189
   238
lemma Some_image_these_eq:
haftmann@49189
   239
  "Some ` these A = {x\<in>A. x \<noteq> None}"
haftmann@49189
   240
  by (auto simp add: these_def image_image intro!: image_eqI)
haftmann@49189
   241
haftmann@49189
   242
lemma these_empty_eq:
haftmann@49189
   243
  "these B = {} \<longleftrightarrow> B = {} \<or> B = {None}"
haftmann@49189
   244
  by (auto simp add: these_def)
haftmann@49189
   245
haftmann@49189
   246
lemma these_not_empty_eq:
haftmann@49189
   247
  "these B \<noteq> {} \<longleftrightarrow> B \<noteq> {} \<and> B \<noteq> {None}"
haftmann@49189
   248
  by (auto simp add: these_empty_eq)
haftmann@49189
   249
blanchet@55518
   250
hide_const (open) bind these
blanchet@55466
   251
hide_fact (open) bind_cong
nipkow@30246
   252
haftmann@49189
   253
wenzelm@60758
   254
subsection \<open>Transfer rules for the Transfer package\<close>
traytel@58916
   255
traytel@58916
   256
context
traytel@58916
   257
begin
traytel@58916
   258
interpretation lifting_syntax .
traytel@58916
   259
traytel@58916
   260
lemma option_bind_transfer [transfer_rule]:
traytel@58916
   261
  "(rel_option A ===> (A ===> rel_option B) ===> rel_option B)
traytel@58916
   262
    Option.bind Option.bind"
traytel@58916
   263
  unfolding rel_fun_def split_option_all by simp
traytel@58916
   264
Andreas@59523
   265
lemma pred_option_parametric [transfer_rule]:
Andreas@59523
   266
  "((A ===> op =) ===> rel_option A ===> op =) pred_option pred_option"
Andreas@59523
   267
by(rule rel_funI)+(auto simp add: rel_option_unfold is_none_def dest: rel_funD)
Andreas@59523
   268
traytel@58916
   269
end
traytel@58916
   270
traytel@58916
   271
wenzelm@60758
   272
subsubsection \<open>Interaction with finite sets\<close>
blanchet@55089
   273
blanchet@55089
   274
lemma finite_option_UNIV [simp]:
blanchet@55089
   275
  "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
blanchet@55089
   276
  by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
blanchet@55089
   277
blanchet@55089
   278
instance option :: (finite) finite
blanchet@55089
   279
  by default (simp add: UNIV_option_conv)
blanchet@55089
   280
blanchet@55089
   281
wenzelm@60758
   282
subsubsection \<open>Code generator setup\<close>
nipkow@30246
   283
Andreas@59522
   284
lemma equal_None_code_unfold [code_unfold]:
haftmann@38857
   285
  "HOL.equal x None \<longleftrightarrow> is_none x"
lammich@53940
   286
  "HOL.equal None = is_none"
lammich@53940
   287
  by (auto simp add: equal is_none_def)
nipkow@30246
   288
wenzelm@36176
   289
hide_const (open) is_none
nipkow@30246
   290
haftmann@52435
   291
code_printing
haftmann@52435
   292
  type_constructor option \<rightharpoonup>
haftmann@52435
   293
    (SML) "_ option"
haftmann@52435
   294
    and (OCaml) "_ option"
haftmann@52435
   295
    and (Haskell) "Maybe _"
haftmann@52435
   296
    and (Scala) "!Option[(_)]"
haftmann@52435
   297
| constant None \<rightharpoonup>
haftmann@52435
   298
    (SML) "NONE"
haftmann@52435
   299
    and (OCaml) "None"
haftmann@52435
   300
    and (Haskell) "Nothing"
haftmann@52435
   301
    and (Scala) "!None"
haftmann@52435
   302
| constant Some \<rightharpoonup>
haftmann@52435
   303
    (SML) "SOME"
haftmann@52435
   304
    and (OCaml) "Some _"
haftmann@52435
   305
    and (Haskell) "Just"
haftmann@52435
   306
    and (Scala) "Some"
haftmann@52435
   307
| class_instance option :: equal \<rightharpoonup>
haftmann@52435
   308
    (Haskell) -
haftmann@52435
   309
| constant "HOL.equal :: 'a option \<Rightarrow> 'a option \<Rightarrow> bool" \<rightharpoonup>
haftmann@52435
   310
    (Haskell) infix 4 "=="
nipkow@30246
   311
nipkow@30246
   312
code_reserved SML
nipkow@30246
   313
  option NONE SOME
nipkow@30246
   314
nipkow@30246
   315
code_reserved OCaml
nipkow@30246
   316
  option None Some
nipkow@30246
   317
haftmann@34886
   318
code_reserved Scala
haftmann@34886
   319
  Option None Some
haftmann@34886
   320
nipkow@30246
   321
end