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