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