src/HOL/Option.thy
author kuncar
Tue Aug 13 15:59:22 2013 +0200 (2013-08-13)
changeset 53010 ec5e6f69bd65
parent 52435 6646bb548c6b
child 53940 36cf426cb1c6
permissions -rw-r--r--
move useful lemmas to Main
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
haftmann@35719
     8
imports Datatype
nipkow@30246
     9
begin
nipkow@30246
    10
nipkow@30246
    11
datatype 'a option = None | Some 'a
nipkow@30246
    12
nipkow@30246
    13
lemma not_None_eq [iff]: "(x ~= None) = (EX y. x = Some y)"
nipkow@30246
    14
  by (induct x) auto
nipkow@30246
    15
nipkow@30246
    16
lemma not_Some_eq [iff]: "(ALL y. x ~= Some y) = (x = None)"
nipkow@30246
    17
  by (induct x) auto
nipkow@30246
    18
nipkow@30246
    19
text{*Although it may appear that both of these equalities are helpful
nipkow@30246
    20
only when applied to assumptions, in practice it seems better to give
nipkow@30246
    21
them the uniform iff attribute. *}
nipkow@30246
    22
nipkow@31080
    23
lemma inj_Some [simp]: "inj_on Some A"
nipkow@31080
    24
by (rule inj_onI) simp
nipkow@31080
    25
nipkow@30246
    26
lemma option_caseE:
nipkow@30246
    27
  assumes c: "(case x of None => P | Some y => Q y)"
nipkow@30246
    28
  obtains
nipkow@30246
    29
    (None) "x = None" and P
nipkow@30246
    30
  | (Some) y where "x = Some y" and "Q y"
nipkow@30246
    31
  using c by (cases x) simp_all
nipkow@30246
    32
kuncar@53010
    33
lemma split_option_all: "(\<forall>x. P x) \<longleftrightarrow> P None \<and> (\<forall>x. P (Some x))"
kuncar@53010
    34
by (auto intro: option.induct)
kuncar@53010
    35
kuncar@53010
    36
lemma split_option_ex: "(\<exists>x. P x) \<longleftrightarrow> P None \<or> (\<exists>x. P (Some x))"
kuncar@53010
    37
using split_option_all[of "\<lambda>x. \<not>P x"] by blast
kuncar@53010
    38
nipkow@31080
    39
lemma UNIV_option_conv: "UNIV = insert None (range Some)"
nipkow@31080
    40
by(auto intro: classical)
nipkow@31080
    41
nipkow@30246
    42
subsubsection {* Operations *}
nipkow@30246
    43
nipkow@30246
    44
primrec the :: "'a option => 'a" where
nipkow@30246
    45
"the (Some x) = x"
nipkow@30246
    46
nipkow@30246
    47
primrec set :: "'a option => 'a set" where
nipkow@30246
    48
"set None = {}" |
nipkow@30246
    49
"set (Some x) = {x}"
nipkow@30246
    50
nipkow@30246
    51
lemma ospec [dest]: "(ALL x:set A. P x) ==> A = Some x ==> P x"
nipkow@30246
    52
  by simp
nipkow@30246
    53
wenzelm@51703
    54
setup {* map_theory_claset (fn ctxt => ctxt addSD2 ("ospec", @{thm ospec})) *}
nipkow@30246
    55
nipkow@30246
    56
lemma elem_set [iff]: "(x : set xo) = (xo = Some x)"
nipkow@30246
    57
  by (cases xo) auto
nipkow@30246
    58
nipkow@30246
    59
lemma set_empty_eq [simp]: "(set xo = {}) = (xo = None)"
nipkow@30246
    60
  by (cases xo) auto
nipkow@30246
    61
haftmann@31154
    62
definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a option \<Rightarrow> 'b option" where
haftmann@31154
    63
  "map = (%f y. case y of None => None | Some x => Some (f x))"
nipkow@30246
    64
nipkow@30246
    65
lemma option_map_None [simp, code]: "map f None = None"
nipkow@30246
    66
  by (simp add: map_def)
nipkow@30246
    67
nipkow@30246
    68
lemma option_map_Some [simp, code]: "map f (Some x) = Some (f x)"
nipkow@30246
    69
  by (simp add: map_def)
nipkow@30246
    70
nipkow@30246
    71
lemma option_map_is_None [iff]:
nipkow@30246
    72
    "(map f opt = None) = (opt = None)"
nipkow@30246
    73
  by (simp add: map_def split add: option.split)
nipkow@30246
    74
nipkow@30246
    75
lemma option_map_eq_Some [iff]:
nipkow@30246
    76
    "(map f xo = Some y) = (EX z. xo = Some z & f z = y)"
nipkow@30246
    77
  by (simp add: map_def split add: option.split)
nipkow@30246
    78
nipkow@30246
    79
lemma option_map_comp:
nipkow@30246
    80
    "map f (map g opt) = map (f o g) opt"
nipkow@30246
    81
  by (simp add: map_def split add: option.split)
nipkow@30246
    82
nipkow@30246
    83
lemma option_map_o_sum_case [simp]:
nipkow@30246
    84
    "map f o sum_case g h = sum_case (map f o g) (map f o h)"
nipkow@30246
    85
  by (rule ext) (simp split: sum.split)
nipkow@30246
    86
krauss@46526
    87
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
    88
by (cases x) auto
krauss@46526
    89
haftmann@41505
    90
enriched_type map: Option.map proof -
haftmann@41372
    91
  fix f g
haftmann@41372
    92
  show "Option.map f \<circ> Option.map g = Option.map (f \<circ> g)"
haftmann@41372
    93
  proof
haftmann@41372
    94
    fix x
haftmann@41372
    95
    show "(Option.map f \<circ> Option.map g) x= Option.map (f \<circ> g) x"
haftmann@41372
    96
      by (cases x) simp_all
haftmann@41372
    97
  qed
haftmann@40609
    98
next
haftmann@41372
    99
  show "Option.map id = id"
haftmann@41372
   100
  proof
haftmann@41372
   101
    fix x
haftmann@41372
   102
    show "Option.map id x = id x"
haftmann@41372
   103
      by (cases x) simp_all
haftmann@41372
   104
  qed
haftmann@40609
   105
qed
haftmann@40609
   106
haftmann@51096
   107
lemma option_case_map [simp]:
haftmann@51096
   108
  "option_case g h (Option.map f x) = option_case g (h \<circ> f) x"
haftmann@51096
   109
  by (cases x) simp_all
haftmann@51096
   110
krauss@39149
   111
primrec bind :: "'a option \<Rightarrow> ('a \<Rightarrow> 'b option) \<Rightarrow> 'b option" where
krauss@39149
   112
bind_lzero: "bind None f = None" |
krauss@39149
   113
bind_lunit: "bind (Some x) f = f x"
nipkow@30246
   114
krauss@39149
   115
lemma bind_runit[simp]: "bind x Some = x"
krauss@39149
   116
by (cases x) auto
krauss@39149
   117
krauss@39149
   118
lemma bind_assoc[simp]: "bind (bind x f) g = bind x (\<lambda>y. bind (f y) g)"
krauss@39149
   119
by (cases x) auto
krauss@39149
   120
krauss@39149
   121
lemma bind_rzero[simp]: "bind x (\<lambda>x. None) = None"
krauss@39149
   122
by (cases x) auto
krauss@39149
   123
krauss@46526
   124
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
   125
by (cases x) auto
krauss@46526
   126
haftmann@49189
   127
definition these :: "'a option set \<Rightarrow> 'a set"
haftmann@49189
   128
where
haftmann@49189
   129
  "these A = the ` {x \<in> A. x \<noteq> None}"
haftmann@49189
   130
haftmann@49189
   131
lemma these_empty [simp]:
haftmann@49189
   132
  "these {} = {}"
haftmann@49189
   133
  by (simp add: these_def)
haftmann@49189
   134
haftmann@49189
   135
lemma these_insert_None [simp]:
haftmann@49189
   136
  "these (insert None A) = these A"
haftmann@49189
   137
  by (auto simp add: these_def)
haftmann@49189
   138
haftmann@49189
   139
lemma these_insert_Some [simp]:
haftmann@49189
   140
  "these (insert (Some x) A) = insert x (these A)"
haftmann@49189
   141
proof -
haftmann@49189
   142
  have "{y \<in> insert (Some x) A. y \<noteq> None} = insert (Some x) {y \<in> A. y \<noteq> None}"
haftmann@49189
   143
    by auto
haftmann@49189
   144
  then show ?thesis by (simp add: these_def)
haftmann@49189
   145
qed
haftmann@49189
   146
haftmann@49189
   147
lemma in_these_eq:
haftmann@49189
   148
  "x \<in> these A \<longleftrightarrow> Some x \<in> A"
haftmann@49189
   149
proof
haftmann@49189
   150
  assume "Some x \<in> A"
haftmann@49189
   151
  then obtain B where "A = insert (Some x) B" by auto
haftmann@49189
   152
  then show "x \<in> these A" by (auto simp add: these_def intro!: image_eqI)
haftmann@49189
   153
next
haftmann@49189
   154
  assume "x \<in> these A"
haftmann@49189
   155
  then show "Some x \<in> A" by (auto simp add: these_def)
haftmann@49189
   156
qed
haftmann@49189
   157
haftmann@49189
   158
lemma these_image_Some_eq [simp]:
haftmann@49189
   159
  "these (Some ` A) = A"
haftmann@49189
   160
  by (auto simp add: these_def intro!: image_eqI)
haftmann@49189
   161
haftmann@49189
   162
lemma Some_image_these_eq:
haftmann@49189
   163
  "Some ` these A = {x\<in>A. x \<noteq> None}"
haftmann@49189
   164
  by (auto simp add: these_def image_image intro!: image_eqI)
haftmann@49189
   165
haftmann@49189
   166
lemma these_empty_eq:
haftmann@49189
   167
  "these B = {} \<longleftrightarrow> B = {} \<or> B = {None}"
haftmann@49189
   168
  by (auto simp add: these_def)
haftmann@49189
   169
haftmann@49189
   170
lemma these_not_empty_eq:
haftmann@49189
   171
  "these B \<noteq> {} \<longleftrightarrow> B \<noteq> {} \<and> B \<noteq> {None}"
haftmann@49189
   172
  by (auto simp add: these_empty_eq)
haftmann@49189
   173
haftmann@49189
   174
hide_const (open) set map bind these
krauss@46526
   175
hide_fact (open) map_cong bind_cong
nipkow@30246
   176
haftmann@49189
   177
nipkow@30246
   178
subsubsection {* Code generator setup *}
nipkow@30246
   179
haftmann@31154
   180
definition is_none :: "'a option \<Rightarrow> bool" where
haftmann@31998
   181
  [code_post]: "is_none x \<longleftrightarrow> x = None"
nipkow@30246
   182
nipkow@30246
   183
lemma is_none_code [code]:
nipkow@30246
   184
  shows "is_none None \<longleftrightarrow> True"
nipkow@30246
   185
    and "is_none (Some x) \<longleftrightarrow> False"
haftmann@31154
   186
  unfolding is_none_def by simp_all
haftmann@31154
   187
haftmann@32069
   188
lemma [code_unfold]:
haftmann@38857
   189
  "HOL.equal x None \<longleftrightarrow> is_none x"
krauss@39150
   190
  by (simp add: equal is_none_def)
nipkow@30246
   191
wenzelm@36176
   192
hide_const (open) is_none
nipkow@30246
   193
haftmann@52435
   194
code_printing
haftmann@52435
   195
  type_constructor option \<rightharpoonup>
haftmann@52435
   196
    (SML) "_ option"
haftmann@52435
   197
    and (OCaml) "_ option"
haftmann@52435
   198
    and (Haskell) "Maybe _"
haftmann@52435
   199
    and (Scala) "!Option[(_)]"
haftmann@52435
   200
| constant None \<rightharpoonup>
haftmann@52435
   201
    (SML) "NONE"
haftmann@52435
   202
    and (OCaml) "None"
haftmann@52435
   203
    and (Haskell) "Nothing"
haftmann@52435
   204
    and (Scala) "!None"
haftmann@52435
   205
| constant Some \<rightharpoonup>
haftmann@52435
   206
    (SML) "SOME"
haftmann@52435
   207
    and (OCaml) "Some _"
haftmann@52435
   208
    and (Haskell) "Just"
haftmann@52435
   209
    and (Scala) "Some"
haftmann@52435
   210
| class_instance option :: equal \<rightharpoonup>
haftmann@52435
   211
    (Haskell) -
haftmann@52435
   212
| constant "HOL.equal :: 'a option \<Rightarrow> 'a option \<Rightarrow> bool" \<rightharpoonup>
haftmann@52435
   213
    (Haskell) infix 4 "=="
nipkow@30246
   214
nipkow@30246
   215
code_reserved SML
nipkow@30246
   216
  option NONE SOME
nipkow@30246
   217
nipkow@30246
   218
code_reserved OCaml
nipkow@30246
   219
  option None Some
nipkow@30246
   220
haftmann@34886
   221
code_reserved Scala
haftmann@34886
   222
  Option None Some
haftmann@34886
   223
nipkow@30246
   224
end
haftmann@49189
   225