src/HOL/Option.thy
author wenzelm
Fri Mar 07 22:30:58 2014 +0100 (2014-03-07)
changeset 55990 41c6b99c5fb7
parent 55867 79b915f26533
child 57091 1fa9c19ba2c9
permissions -rw-r--r--
more antiquotations;
nipkow@30246
     1
(*  Title:      HOL/Option.thy
nipkow@30246
     2
    Author:     Folklore
nipkow@30246
     3
*)
nipkow@30246
     4
nipkow@30246
     5
header {* Datatype option *}
nipkow@30246
     6
nipkow@30246
     7
theory Option
blanchet@55404
     8
imports BNF_LFP Datatype Finite_Set
nipkow@30246
     9
begin
nipkow@30246
    10
blanchet@55406
    11
datatype_new 'a option =
blanchet@55406
    12
    =: None
blanchet@55406
    13
  | Some (the: 'a)
blanchet@55531
    14
datatype_compat option
blanchet@55404
    15
blanchet@55406
    16
lemma [case_names None Some, cases type: option]:
blanchet@55406
    17
  -- {* for backward compatibility -- names of variables differ *}
blanchet@55417
    18
  "(y = None \<Longrightarrow> P) \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> P) \<Longrightarrow> P"
blanchet@55406
    19
by (rule option.exhaust)
blanchet@55406
    20
blanchet@55406
    21
lemma [case_names None Some, induct type: option]:
blanchet@55406
    22
  -- {* for backward compatibility -- names of variables differ *}
blanchet@55406
    23
  "P None \<Longrightarrow> (\<And>option. P (Some option)) \<Longrightarrow> P option"
blanchet@55406
    24
by (rule option.induct)
blanchet@55406
    25
blanchet@55442
    26
text {* Compatibility: *}
blanchet@55442
    27
blanchet@55404
    28
setup {* Sign.mandatory_path "option" *}
blanchet@55404
    29
blanchet@55404
    30
lemmas inducts = option.induct
blanchet@55404
    31
lemmas cases = option.case
blanchet@55404
    32
blanchet@55404
    33
setup {* Sign.parent_path *}
nipkow@30246
    34
nipkow@30246
    35
lemma not_None_eq [iff]: "(x ~= None) = (EX y. x = Some y)"
nipkow@30246
    36
  by (induct x) auto
nipkow@30246
    37
nipkow@30246
    38
lemma not_Some_eq [iff]: "(ALL y. x ~= Some y) = (x = None)"
nipkow@30246
    39
  by (induct x) auto
nipkow@30246
    40
nipkow@30246
    41
text{*Although it may appear that both of these equalities are helpful
nipkow@30246
    42
only when applied to assumptions, in practice it seems better to give
nipkow@30246
    43
them the uniform iff attribute. *}
nipkow@30246
    44
nipkow@31080
    45
lemma inj_Some [simp]: "inj_on Some A"
nipkow@31080
    46
by (rule inj_onI) simp
nipkow@31080
    47
blanchet@55404
    48
lemma case_optionE:
nipkow@30246
    49
  assumes c: "(case x of None => P | Some y => Q y)"
nipkow@30246
    50
  obtains
nipkow@30246
    51
    (None) "x = None" and P
nipkow@30246
    52
  | (Some) y where "x = Some y" and "Q y"
nipkow@30246
    53
  using c by (cases x) simp_all
nipkow@30246
    54
kuncar@53010
    55
lemma split_option_all: "(\<forall>x. P x) \<longleftrightarrow> P None \<and> (\<forall>x. P (Some x))"
kuncar@53010
    56
by (auto intro: option.induct)
kuncar@53010
    57
kuncar@53010
    58
lemma split_option_ex: "(\<exists>x. P x) \<longleftrightarrow> P None \<or> (\<exists>x. P (Some x))"
kuncar@53010
    59
using split_option_all[of "\<lambda>x. \<not>P x"] by blast
kuncar@53010
    60
nipkow@31080
    61
lemma UNIV_option_conv: "UNIV = insert None (range Some)"
nipkow@31080
    62
by(auto intro: classical)
nipkow@31080
    63
nipkow@30246
    64
subsubsection {* Operations *}
nipkow@30246
    65
blanchet@55518
    66
lemma ospec [dest]: "(ALL x:set_option A. P x) ==> A = Some x ==> P x"
nipkow@30246
    67
  by simp
nipkow@30246
    68
wenzelm@51703
    69
setup {* map_theory_claset (fn ctxt => ctxt addSD2 ("ospec", @{thm ospec})) *}
nipkow@30246
    70
blanchet@55518
    71
lemma elem_set [iff]: "(x : set_option xo) = (xo = Some x)"
nipkow@30246
    72
  by (cases xo) auto
nipkow@30246
    73
blanchet@55518
    74
lemma set_empty_eq [simp]: "(set_option xo = {}) = (xo = None)"
nipkow@30246
    75
  by (cases xo) auto
nipkow@30246
    76
blanchet@55466
    77
lemma map_option_case: "map_option f y = (case y of None => None | Some x => Some (f x))"
blanchet@55466
    78
  by (auto split: option.split)
nipkow@30246
    79
blanchet@55466
    80
lemma map_option_is_None [iff]:
blanchet@55466
    81
    "(map_option f opt = None) = (opt = None)"
blanchet@55466
    82
  by (simp add: map_option_case split add: option.split)
nipkow@30246
    83
blanchet@55466
    84
lemma map_option_eq_Some [iff]:
blanchet@55466
    85
    "(map_option f xo = Some y) = (EX z. xo = Some z & f z = y)"
blanchet@55466
    86
  by (simp add: map_option_case split add: option.split)
nipkow@30246
    87
blanchet@55466
    88
lemma map_option_o_case_sum [simp]:
blanchet@55466
    89
    "map_option f o case_sum g h = case_sum (map_option f o g) (map_option f o h)"
blanchet@55466
    90
  by (rule o_case_sum)
nipkow@30246
    91
blanchet@55466
    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"
krauss@46526
    93
by (cases x) auto
krauss@46526
    94
blanchet@55467
    95
functor map_option: map_option proof -
haftmann@41372
    96
  fix f g
blanchet@55466
    97
  show "map_option f \<circ> map_option g = map_option (f \<circ> g)"
haftmann@41372
    98
  proof
haftmann@41372
    99
    fix x
blanchet@55466
   100
    show "(map_option f \<circ> map_option g) x= map_option (f \<circ> g) x"
haftmann@41372
   101
      by (cases x) simp_all
haftmann@41372
   102
  qed
haftmann@40609
   103
next
blanchet@55466
   104
  show "map_option id = id"
haftmann@41372
   105
  proof
haftmann@41372
   106
    fix x
blanchet@55466
   107
    show "map_option id x = id x"
haftmann@41372
   108
      by (cases x) simp_all
haftmann@41372
   109
  qed
haftmann@40609
   110
qed
haftmann@40609
   111
blanchet@55466
   112
lemma case_map_option [simp]:
blanchet@55466
   113
  "case_option g h (map_option f x) = case_option g (h \<circ> f) x"
haftmann@51096
   114
  by (cases x) simp_all
haftmann@51096
   115
krauss@39149
   116
primrec bind :: "'a option \<Rightarrow> ('a \<Rightarrow> 'b option) \<Rightarrow> 'b option" where
krauss@39149
   117
bind_lzero: "bind None f = None" |
krauss@39149
   118
bind_lunit: "bind (Some x) f = f x"
nipkow@30246
   119
krauss@39149
   120
lemma bind_runit[simp]: "bind x Some = x"
krauss@39149
   121
by (cases x) auto
krauss@39149
   122
krauss@39149
   123
lemma bind_assoc[simp]: "bind (bind x f) g = bind x (\<lambda>y. bind (f y) g)"
krauss@39149
   124
by (cases x) auto
krauss@39149
   125
krauss@39149
   126
lemma bind_rzero[simp]: "bind x (\<lambda>x. None) = None"
krauss@39149
   127
by (cases x) auto
krauss@39149
   128
krauss@46526
   129
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
   130
by (cases x) auto
krauss@46526
   131
haftmann@49189
   132
definition these :: "'a option set \<Rightarrow> 'a set"
haftmann@49189
   133
where
haftmann@49189
   134
  "these A = the ` {x \<in> A. x \<noteq> None}"
haftmann@49189
   135
haftmann@49189
   136
lemma these_empty [simp]:
haftmann@49189
   137
  "these {} = {}"
haftmann@49189
   138
  by (simp add: these_def)
haftmann@49189
   139
haftmann@49189
   140
lemma these_insert_None [simp]:
haftmann@49189
   141
  "these (insert None A) = these A"
haftmann@49189
   142
  by (auto simp add: these_def)
haftmann@49189
   143
haftmann@49189
   144
lemma these_insert_Some [simp]:
haftmann@49189
   145
  "these (insert (Some x) A) = insert x (these A)"
haftmann@49189
   146
proof -
haftmann@49189
   147
  have "{y \<in> insert (Some x) A. y \<noteq> None} = insert (Some x) {y \<in> A. y \<noteq> None}"
haftmann@49189
   148
    by auto
haftmann@49189
   149
  then show ?thesis by (simp add: these_def)
haftmann@49189
   150
qed
haftmann@49189
   151
haftmann@49189
   152
lemma in_these_eq:
haftmann@49189
   153
  "x \<in> these A \<longleftrightarrow> Some x \<in> A"
haftmann@49189
   154
proof
haftmann@49189
   155
  assume "Some x \<in> A"
haftmann@49189
   156
  then obtain B where "A = insert (Some x) B" by auto
haftmann@49189
   157
  then show "x \<in> these A" by (auto simp add: these_def intro!: image_eqI)
haftmann@49189
   158
next
haftmann@49189
   159
  assume "x \<in> these A"
haftmann@49189
   160
  then show "Some x \<in> A" by (auto simp add: these_def)
haftmann@49189
   161
qed
haftmann@49189
   162
haftmann@49189
   163
lemma these_image_Some_eq [simp]:
haftmann@49189
   164
  "these (Some ` A) = A"
haftmann@49189
   165
  by (auto simp add: these_def intro!: image_eqI)
haftmann@49189
   166
haftmann@49189
   167
lemma Some_image_these_eq:
haftmann@49189
   168
  "Some ` these A = {x\<in>A. x \<noteq> None}"
haftmann@49189
   169
  by (auto simp add: these_def image_image intro!: image_eqI)
haftmann@49189
   170
haftmann@49189
   171
lemma these_empty_eq:
haftmann@49189
   172
  "these B = {} \<longleftrightarrow> B = {} \<or> B = {None}"
haftmann@49189
   173
  by (auto simp add: these_def)
haftmann@49189
   174
haftmann@49189
   175
lemma these_not_empty_eq:
haftmann@49189
   176
  "these B \<noteq> {} \<longleftrightarrow> B \<noteq> {} \<and> B \<noteq> {None}"
haftmann@49189
   177
  by (auto simp add: these_empty_eq)
haftmann@49189
   178
blanchet@55518
   179
hide_const (open) bind these
blanchet@55466
   180
hide_fact (open) bind_cong
nipkow@30246
   181
haftmann@49189
   182
blanchet@55089
   183
subsubsection {* Interaction with finite sets *}
blanchet@55089
   184
blanchet@55089
   185
lemma finite_option_UNIV [simp]:
blanchet@55089
   186
  "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
blanchet@55089
   187
  by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
blanchet@55089
   188
blanchet@55089
   189
instance option :: (finite) finite
blanchet@55089
   190
  by default (simp add: UNIV_option_conv)
blanchet@55089
   191
blanchet@55089
   192
nipkow@30246
   193
subsubsection {* Code generator setup *}
nipkow@30246
   194
haftmann@31154
   195
definition is_none :: "'a option \<Rightarrow> bool" where
haftmann@31998
   196
  [code_post]: "is_none x \<longleftrightarrow> x = None"
nipkow@30246
   197
nipkow@30246
   198
lemma is_none_code [code]:
nipkow@30246
   199
  shows "is_none None \<longleftrightarrow> True"
nipkow@30246
   200
    and "is_none (Some x) \<longleftrightarrow> False"
haftmann@31154
   201
  unfolding is_none_def by simp_all
haftmann@31154
   202
haftmann@32069
   203
lemma [code_unfold]:
haftmann@38857
   204
  "HOL.equal x None \<longleftrightarrow> is_none x"
lammich@53940
   205
  "HOL.equal None = is_none"
lammich@53940
   206
  by (auto simp add: equal is_none_def)
nipkow@30246
   207
wenzelm@36176
   208
hide_const (open) is_none
nipkow@30246
   209
haftmann@52435
   210
code_printing
haftmann@52435
   211
  type_constructor option \<rightharpoonup>
haftmann@52435
   212
    (SML) "_ option"
haftmann@52435
   213
    and (OCaml) "_ option"
haftmann@52435
   214
    and (Haskell) "Maybe _"
haftmann@52435
   215
    and (Scala) "!Option[(_)]"
haftmann@52435
   216
| constant None \<rightharpoonup>
haftmann@52435
   217
    (SML) "NONE"
haftmann@52435
   218
    and (OCaml) "None"
haftmann@52435
   219
    and (Haskell) "Nothing"
haftmann@52435
   220
    and (Scala) "!None"
haftmann@52435
   221
| constant Some \<rightharpoonup>
haftmann@52435
   222
    (SML) "SOME"
haftmann@52435
   223
    and (OCaml) "Some _"
haftmann@52435
   224
    and (Haskell) "Just"
haftmann@52435
   225
    and (Scala) "Some"
haftmann@52435
   226
| class_instance option :: equal \<rightharpoonup>
haftmann@52435
   227
    (Haskell) -
haftmann@52435
   228
| constant "HOL.equal :: 'a option \<Rightarrow> 'a option \<Rightarrow> bool" \<rightharpoonup>
haftmann@52435
   229
    (Haskell) infix 4 "=="
nipkow@30246
   230
nipkow@30246
   231
code_reserved SML
nipkow@30246
   232
  option NONE SOME
nipkow@30246
   233
nipkow@30246
   234
code_reserved OCaml
nipkow@30246
   235
  option None Some
nipkow@30246
   236
haftmann@34886
   237
code_reserved Scala
haftmann@34886
   238
  Option None Some
haftmann@34886
   239
nipkow@30246
   240
end