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