src/HOL/Option.thy
author haftmann
Wed Feb 13 13:38:52 2013 +0100 (2013-02-13)
changeset 51096 60e4b75fefe1
parent 49189 3f85cd15a0cc
child 51703 f2e92fc0c8aa
permissions -rw-r--r--
combinator List.those;
simprule for case distinction on Option.map expression
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
nipkow@31080
    33
lemma UNIV_option_conv: "UNIV = insert None (range Some)"
nipkow@31080
    34
by(auto intro: classical)
nipkow@31080
    35
nipkow@30246
    36
nipkow@30246
    37
subsubsection {* Operations *}
nipkow@30246
    38
nipkow@30246
    39
primrec the :: "'a option => 'a" where
nipkow@30246
    40
"the (Some x) = x"
nipkow@30246
    41
nipkow@30246
    42
primrec set :: "'a option => 'a set" where
nipkow@30246
    43
"set None = {}" |
nipkow@30246
    44
"set (Some x) = {x}"
nipkow@30246
    45
nipkow@30246
    46
lemma ospec [dest]: "(ALL x:set A. P x) ==> A = Some x ==> P x"
nipkow@30246
    47
  by simp
nipkow@30246
    48
nipkow@30246
    49
declaration {* fn _ =>
wenzelm@39159
    50
  Classical.map_cs (fn cs => cs addSD2 ("ospec", @{thm ospec}))
nipkow@30246
    51
*}
nipkow@30246
    52
nipkow@30246
    53
lemma elem_set [iff]: "(x : set xo) = (xo = Some x)"
nipkow@30246
    54
  by (cases xo) auto
nipkow@30246
    55
nipkow@30246
    56
lemma set_empty_eq [simp]: "(set xo = {}) = (xo = None)"
nipkow@30246
    57
  by (cases xo) auto
nipkow@30246
    58
haftmann@31154
    59
definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a option \<Rightarrow> 'b option" where
haftmann@31154
    60
  "map = (%f y. case y of None => None | Some x => Some (f x))"
nipkow@30246
    61
nipkow@30246
    62
lemma option_map_None [simp, code]: "map f None = None"
nipkow@30246
    63
  by (simp add: map_def)
nipkow@30246
    64
nipkow@30246
    65
lemma option_map_Some [simp, code]: "map f (Some x) = Some (f x)"
nipkow@30246
    66
  by (simp add: map_def)
nipkow@30246
    67
nipkow@30246
    68
lemma option_map_is_None [iff]:
nipkow@30246
    69
    "(map f opt = None) = (opt = None)"
nipkow@30246
    70
  by (simp add: map_def split add: option.split)
nipkow@30246
    71
nipkow@30246
    72
lemma option_map_eq_Some [iff]:
nipkow@30246
    73
    "(map f xo = Some y) = (EX z. xo = Some z & f z = y)"
nipkow@30246
    74
  by (simp add: map_def split add: option.split)
nipkow@30246
    75
nipkow@30246
    76
lemma option_map_comp:
nipkow@30246
    77
    "map f (map g opt) = map (f o g) opt"
nipkow@30246
    78
  by (simp add: map_def split add: option.split)
nipkow@30246
    79
nipkow@30246
    80
lemma option_map_o_sum_case [simp]:
nipkow@30246
    81
    "map f o sum_case g h = sum_case (map f o g) (map f o h)"
nipkow@30246
    82
  by (rule ext) (simp split: sum.split)
nipkow@30246
    83
krauss@46526
    84
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
    85
by (cases x) auto
krauss@46526
    86
haftmann@41505
    87
enriched_type map: Option.map proof -
haftmann@41372
    88
  fix f g
haftmann@41372
    89
  show "Option.map f \<circ> Option.map g = Option.map (f \<circ> g)"
haftmann@41372
    90
  proof
haftmann@41372
    91
    fix x
haftmann@41372
    92
    show "(Option.map f \<circ> Option.map g) x= Option.map (f \<circ> g) x"
haftmann@41372
    93
      by (cases x) simp_all
haftmann@41372
    94
  qed
haftmann@40609
    95
next
haftmann@41372
    96
  show "Option.map id = id"
haftmann@41372
    97
  proof
haftmann@41372
    98
    fix x
haftmann@41372
    99
    show "Option.map id x = id x"
haftmann@41372
   100
      by (cases x) simp_all
haftmann@41372
   101
  qed
haftmann@40609
   102
qed
haftmann@40609
   103
haftmann@51096
   104
lemma option_case_map [simp]:
haftmann@51096
   105
  "option_case g h (Option.map f x) = option_case g (h \<circ> f) x"
haftmann@51096
   106
  by (cases x) simp_all
haftmann@51096
   107
krauss@39149
   108
primrec bind :: "'a option \<Rightarrow> ('a \<Rightarrow> 'b option) \<Rightarrow> 'b option" where
krauss@39149
   109
bind_lzero: "bind None f = None" |
krauss@39149
   110
bind_lunit: "bind (Some x) f = f x"
nipkow@30246
   111
krauss@39149
   112
lemma bind_runit[simp]: "bind x Some = x"
krauss@39149
   113
by (cases x) auto
krauss@39149
   114
krauss@39149
   115
lemma bind_assoc[simp]: "bind (bind x f) g = bind x (\<lambda>y. bind (f y) g)"
krauss@39149
   116
by (cases x) auto
krauss@39149
   117
krauss@39149
   118
lemma bind_rzero[simp]: "bind x (\<lambda>x. None) = None"
krauss@39149
   119
by (cases x) auto
krauss@39149
   120
krauss@46526
   121
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
   122
by (cases x) auto
krauss@46526
   123
haftmann@49189
   124
definition these :: "'a option set \<Rightarrow> 'a set"
haftmann@49189
   125
where
haftmann@49189
   126
  "these A = the ` {x \<in> A. x \<noteq> None}"
haftmann@49189
   127
haftmann@49189
   128
lemma these_empty [simp]:
haftmann@49189
   129
  "these {} = {}"
haftmann@49189
   130
  by (simp add: these_def)
haftmann@49189
   131
haftmann@49189
   132
lemma these_insert_None [simp]:
haftmann@49189
   133
  "these (insert None A) = these A"
haftmann@49189
   134
  by (auto simp add: these_def)
haftmann@49189
   135
haftmann@49189
   136
lemma these_insert_Some [simp]:
haftmann@49189
   137
  "these (insert (Some x) A) = insert x (these A)"
haftmann@49189
   138
proof -
haftmann@49189
   139
  have "{y \<in> insert (Some x) A. y \<noteq> None} = insert (Some x) {y \<in> A. y \<noteq> None}"
haftmann@49189
   140
    by auto
haftmann@49189
   141
  then show ?thesis by (simp add: these_def)
haftmann@49189
   142
qed
haftmann@49189
   143
haftmann@49189
   144
lemma in_these_eq:
haftmann@49189
   145
  "x \<in> these A \<longleftrightarrow> Some x \<in> A"
haftmann@49189
   146
proof
haftmann@49189
   147
  assume "Some x \<in> A"
haftmann@49189
   148
  then obtain B where "A = insert (Some x) B" by auto
haftmann@49189
   149
  then show "x \<in> these A" by (auto simp add: these_def intro!: image_eqI)
haftmann@49189
   150
next
haftmann@49189
   151
  assume "x \<in> these A"
haftmann@49189
   152
  then show "Some x \<in> A" by (auto simp add: these_def)
haftmann@49189
   153
qed
haftmann@49189
   154
haftmann@49189
   155
lemma these_image_Some_eq [simp]:
haftmann@49189
   156
  "these (Some ` A) = A"
haftmann@49189
   157
  by (auto simp add: these_def intro!: image_eqI)
haftmann@49189
   158
haftmann@49189
   159
lemma Some_image_these_eq:
haftmann@49189
   160
  "Some ` these A = {x\<in>A. x \<noteq> None}"
haftmann@49189
   161
  by (auto simp add: these_def image_image intro!: image_eqI)
haftmann@49189
   162
haftmann@49189
   163
lemma these_empty_eq:
haftmann@49189
   164
  "these B = {} \<longleftrightarrow> B = {} \<or> B = {None}"
haftmann@49189
   165
  by (auto simp add: these_def)
haftmann@49189
   166
haftmann@49189
   167
lemma these_not_empty_eq:
haftmann@49189
   168
  "these B \<noteq> {} \<longleftrightarrow> B \<noteq> {} \<and> B \<noteq> {None}"
haftmann@49189
   169
  by (auto simp add: these_empty_eq)
haftmann@49189
   170
haftmann@49189
   171
hide_const (open) set map bind these
krauss@46526
   172
hide_fact (open) map_cong bind_cong
nipkow@30246
   173
haftmann@49189
   174
nipkow@30246
   175
subsubsection {* Code generator setup *}
nipkow@30246
   176
haftmann@31154
   177
definition is_none :: "'a option \<Rightarrow> bool" where
haftmann@31998
   178
  [code_post]: "is_none x \<longleftrightarrow> x = None"
nipkow@30246
   179
nipkow@30246
   180
lemma is_none_code [code]:
nipkow@30246
   181
  shows "is_none None \<longleftrightarrow> True"
nipkow@30246
   182
    and "is_none (Some x) \<longleftrightarrow> False"
haftmann@31154
   183
  unfolding is_none_def by simp_all
haftmann@31154
   184
haftmann@32069
   185
lemma [code_unfold]:
haftmann@38857
   186
  "HOL.equal x None \<longleftrightarrow> is_none x"
krauss@39150
   187
  by (simp add: equal is_none_def)
nipkow@30246
   188
wenzelm@36176
   189
hide_const (open) is_none
nipkow@30246
   190
nipkow@30246
   191
code_type option
nipkow@30246
   192
  (SML "_ option")
nipkow@30246
   193
  (OCaml "_ option")
nipkow@30246
   194
  (Haskell "Maybe _")
haftmann@34886
   195
  (Scala "!Option[(_)]")
nipkow@30246
   196
nipkow@30246
   197
code_const None and Some
nipkow@30246
   198
  (SML "NONE" and "SOME")
nipkow@30246
   199
  (OCaml "None" and "Some _")
nipkow@30246
   200
  (Haskell "Nothing" and "Just")
haftmann@37880
   201
  (Scala "!None" and "Some")
nipkow@30246
   202
haftmann@38857
   203
code_instance option :: equal
nipkow@30246
   204
  (Haskell -)
nipkow@30246
   205
haftmann@38857
   206
code_const "HOL.equal \<Colon> 'a option \<Rightarrow> 'a option \<Rightarrow> bool"
haftmann@39272
   207
  (Haskell infix 4 "==")
nipkow@30246
   208
nipkow@30246
   209
code_reserved SML
nipkow@30246
   210
  option NONE SOME
nipkow@30246
   211
nipkow@30246
   212
code_reserved OCaml
nipkow@30246
   213
  option None Some
nipkow@30246
   214
haftmann@34886
   215
code_reserved Scala
haftmann@34886
   216
  Option None Some
haftmann@34886
   217
nipkow@30246
   218
end
haftmann@49189
   219