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