src/HOL/Option.thy
author blanchet
Fri Feb 14 07:53:46 2014 +0100 (2014-02-14)
changeset 55466 786edc984c98
parent 55442 17fb554688f0
child 55467 a5c9002bc54d
permissions -rw-r--r--
merged 'Option.map' and 'Option.map_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
nipkow@30246
    68
primrec set :: "'a option => 'a set" where
nipkow@30246
    69
"set None = {}" |
nipkow@30246
    70
"set (Some x) = {x}"
nipkow@30246
    71
nipkow@30246
    72
lemma ospec [dest]: "(ALL x:set A. P x) ==> A = Some x ==> P x"
nipkow@30246
    73
  by simp
nipkow@30246
    74
wenzelm@51703
    75
setup {* map_theory_claset (fn ctxt => ctxt addSD2 ("ospec", @{thm ospec})) *}
nipkow@30246
    76
nipkow@30246
    77
lemma elem_set [iff]: "(x : set xo) = (xo = Some x)"
nipkow@30246
    78
  by (cases xo) auto
nipkow@30246
    79
nipkow@30246
    80
lemma set_empty_eq [simp]: "(set xo = {}) = (xo = None)"
nipkow@30246
    81
  by (cases xo) auto
nipkow@30246
    82
blanchet@55466
    83
lemma map_option_case: "map_option f y = (case y of None => None | Some x => Some (f x))"
blanchet@55466
    84
  by (auto split: option.split)
nipkow@30246
    85
blanchet@55466
    86
lemma map_option_is_None [iff]:
blanchet@55466
    87
    "(map_option f opt = None) = (opt = None)"
blanchet@55466
    88
  by (simp add: map_option_case split add: option.split)
nipkow@30246
    89
blanchet@55466
    90
lemma map_option_eq_Some [iff]:
blanchet@55466
    91
    "(map_option f xo = Some y) = (EX z. xo = Some z & f z = y)"
blanchet@55466
    92
  by (simp add: map_option_case split add: option.split)
nipkow@30246
    93
blanchet@55466
    94
lemma map_option_o_case_sum [simp]:
blanchet@55466
    95
    "map_option f o case_sum g h = case_sum (map_option f o g) (map_option f o h)"
blanchet@55466
    96
  by (rule o_case_sum)
nipkow@30246
    97
blanchet@55466
    98
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
    99
by (cases x) auto
krauss@46526
   100
blanchet@55466
   101
enriched_type map_option: map_option proof -
haftmann@41372
   102
  fix f g
blanchet@55466
   103
  show "map_option f \<circ> map_option g = map_option (f \<circ> g)"
haftmann@41372
   104
  proof
haftmann@41372
   105
    fix x
blanchet@55466
   106
    show "(map_option f \<circ> map_option g) x= map_option (f \<circ> g) x"
haftmann@41372
   107
      by (cases x) simp_all
haftmann@41372
   108
  qed
haftmann@40609
   109
next
blanchet@55466
   110
  show "map_option id = id"
haftmann@41372
   111
  proof
haftmann@41372
   112
    fix x
blanchet@55466
   113
    show "map_option id x = id x"
haftmann@41372
   114
      by (cases x) simp_all
haftmann@41372
   115
  qed
haftmann@40609
   116
qed
haftmann@40609
   117
blanchet@55466
   118
lemma case_map_option [simp]:
blanchet@55466
   119
  "case_option g h (map_option f x) = case_option g (h \<circ> f) x"
haftmann@51096
   120
  by (cases x) simp_all
haftmann@51096
   121
krauss@39149
   122
primrec bind :: "'a option \<Rightarrow> ('a \<Rightarrow> 'b option) \<Rightarrow> 'b option" where
krauss@39149
   123
bind_lzero: "bind None f = None" |
krauss@39149
   124
bind_lunit: "bind (Some x) f = f x"
nipkow@30246
   125
krauss@39149
   126
lemma bind_runit[simp]: "bind x Some = x"
krauss@39149
   127
by (cases x) auto
krauss@39149
   128
krauss@39149
   129
lemma bind_assoc[simp]: "bind (bind x f) g = bind x (\<lambda>y. bind (f y) g)"
krauss@39149
   130
by (cases x) auto
krauss@39149
   131
krauss@39149
   132
lemma bind_rzero[simp]: "bind x (\<lambda>x. None) = None"
krauss@39149
   133
by (cases x) auto
krauss@39149
   134
krauss@46526
   135
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
   136
by (cases x) auto
krauss@46526
   137
haftmann@49189
   138
definition these :: "'a option set \<Rightarrow> 'a set"
haftmann@49189
   139
where
haftmann@49189
   140
  "these A = the ` {x \<in> A. x \<noteq> None}"
haftmann@49189
   141
haftmann@49189
   142
lemma these_empty [simp]:
haftmann@49189
   143
  "these {} = {}"
haftmann@49189
   144
  by (simp add: these_def)
haftmann@49189
   145
haftmann@49189
   146
lemma these_insert_None [simp]:
haftmann@49189
   147
  "these (insert None A) = these A"
haftmann@49189
   148
  by (auto simp add: these_def)
haftmann@49189
   149
haftmann@49189
   150
lemma these_insert_Some [simp]:
haftmann@49189
   151
  "these (insert (Some x) A) = insert x (these A)"
haftmann@49189
   152
proof -
haftmann@49189
   153
  have "{y \<in> insert (Some x) A. y \<noteq> None} = insert (Some x) {y \<in> A. y \<noteq> None}"
haftmann@49189
   154
    by auto
haftmann@49189
   155
  then show ?thesis by (simp add: these_def)
haftmann@49189
   156
qed
haftmann@49189
   157
haftmann@49189
   158
lemma in_these_eq:
haftmann@49189
   159
  "x \<in> these A \<longleftrightarrow> Some x \<in> A"
haftmann@49189
   160
proof
haftmann@49189
   161
  assume "Some x \<in> A"
haftmann@49189
   162
  then obtain B where "A = insert (Some x) B" by auto
haftmann@49189
   163
  then show "x \<in> these A" by (auto simp add: these_def intro!: image_eqI)
haftmann@49189
   164
next
haftmann@49189
   165
  assume "x \<in> these A"
haftmann@49189
   166
  then show "Some x \<in> A" by (auto simp add: these_def)
haftmann@49189
   167
qed
haftmann@49189
   168
haftmann@49189
   169
lemma these_image_Some_eq [simp]:
haftmann@49189
   170
  "these (Some ` A) = A"
haftmann@49189
   171
  by (auto simp add: these_def intro!: image_eqI)
haftmann@49189
   172
haftmann@49189
   173
lemma Some_image_these_eq:
haftmann@49189
   174
  "Some ` these A = {x\<in>A. x \<noteq> None}"
haftmann@49189
   175
  by (auto simp add: these_def image_image intro!: image_eqI)
haftmann@49189
   176
haftmann@49189
   177
lemma these_empty_eq:
haftmann@49189
   178
  "these B = {} \<longleftrightarrow> B = {} \<or> B = {None}"
haftmann@49189
   179
  by (auto simp add: these_def)
haftmann@49189
   180
haftmann@49189
   181
lemma these_not_empty_eq:
haftmann@49189
   182
  "these B \<noteq> {} \<longleftrightarrow> B \<noteq> {} \<and> B \<noteq> {None}"
haftmann@49189
   183
  by (auto simp add: these_empty_eq)
haftmann@49189
   184
blanchet@55466
   185
hide_const (open) set bind these
blanchet@55466
   186
hide_fact (open) bind_cong
nipkow@30246
   187
haftmann@49189
   188
blanchet@55089
   189
subsubsection {* Interaction with finite sets *}
blanchet@55089
   190
blanchet@55089
   191
lemma finite_option_UNIV [simp]:
blanchet@55089
   192
  "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
blanchet@55089
   193
  by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
blanchet@55089
   194
blanchet@55089
   195
instance option :: (finite) finite
blanchet@55089
   196
  by default (simp add: UNIV_option_conv)
blanchet@55089
   197
blanchet@55089
   198
nipkow@30246
   199
subsubsection {* Code generator setup *}
nipkow@30246
   200
haftmann@31154
   201
definition is_none :: "'a option \<Rightarrow> bool" where
haftmann@31998
   202
  [code_post]: "is_none x \<longleftrightarrow> x = None"
nipkow@30246
   203
nipkow@30246
   204
lemma is_none_code [code]:
nipkow@30246
   205
  shows "is_none None \<longleftrightarrow> True"
nipkow@30246
   206
    and "is_none (Some x) \<longleftrightarrow> False"
haftmann@31154
   207
  unfolding is_none_def by simp_all
haftmann@31154
   208
haftmann@32069
   209
lemma [code_unfold]:
haftmann@38857
   210
  "HOL.equal x None \<longleftrightarrow> is_none x"
lammich@53940
   211
  "HOL.equal None = is_none"
lammich@53940
   212
  by (auto simp add: equal is_none_def)
nipkow@30246
   213
wenzelm@36176
   214
hide_const (open) is_none
nipkow@30246
   215
haftmann@52435
   216
code_printing
haftmann@52435
   217
  type_constructor option \<rightharpoonup>
haftmann@52435
   218
    (SML) "_ option"
haftmann@52435
   219
    and (OCaml) "_ option"
haftmann@52435
   220
    and (Haskell) "Maybe _"
haftmann@52435
   221
    and (Scala) "!Option[(_)]"
haftmann@52435
   222
| constant None \<rightharpoonup>
haftmann@52435
   223
    (SML) "NONE"
haftmann@52435
   224
    and (OCaml) "None"
haftmann@52435
   225
    and (Haskell) "Nothing"
haftmann@52435
   226
    and (Scala) "!None"
haftmann@52435
   227
| constant Some \<rightharpoonup>
haftmann@52435
   228
    (SML) "SOME"
haftmann@52435
   229
    and (OCaml) "Some _"
haftmann@52435
   230
    and (Haskell) "Just"
haftmann@52435
   231
    and (Scala) "Some"
haftmann@52435
   232
| class_instance option :: equal \<rightharpoonup>
haftmann@52435
   233
    (Haskell) -
haftmann@52435
   234
| constant "HOL.equal :: 'a option \<Rightarrow> 'a option \<Rightarrow> bool" \<rightharpoonup>
haftmann@52435
   235
    (Haskell) infix 4 "=="
nipkow@30246
   236
nipkow@30246
   237
code_reserved SML
nipkow@30246
   238
  option NONE SOME
nipkow@30246
   239
nipkow@30246
   240
code_reserved OCaml
nipkow@30246
   241
  option None Some
nipkow@30246
   242
haftmann@34886
   243
code_reserved Scala
haftmann@34886
   244
  Option None Some
haftmann@34886
   245
nipkow@30246
   246
end