src/HOL/Option.thy
author krauss
Sun Sep 05 21:39:16 2010 +0200 (2010-09-05)
changeset 39149 aabd6d4a5c3a
parent 38857 97775f3e8722
child 39150 c4ff5fd8db99
permissions -rw-r--r--
added Option.bind
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 _ =>
nipkow@30246
    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@39149
    84
primrec bind :: "'a option \<Rightarrow> ('a \<Rightarrow> 'b option) \<Rightarrow> 'b option" where
krauss@39149
    85
bind_lzero: "bind None f = None" |
krauss@39149
    86
bind_lunit: "bind (Some x) f = f x"
nipkow@30246
    87
krauss@39149
    88
lemma bind_runit[simp]: "bind x Some = x"
krauss@39149
    89
by (cases x) auto
krauss@39149
    90
krauss@39149
    91
lemma bind_assoc[simp]: "bind (bind x f) g = bind x (\<lambda>y. bind (f y) g)"
krauss@39149
    92
by (cases x) auto
krauss@39149
    93
krauss@39149
    94
lemma bind_rzero[simp]: "bind x (\<lambda>x. None) = None"
krauss@39149
    95
by (cases x) auto
krauss@39149
    96
krauss@39149
    97
hide_const (open) set map bind
nipkow@30246
    98
nipkow@30246
    99
subsubsection {* Code generator setup *}
nipkow@30246
   100
haftmann@31154
   101
definition is_none :: "'a option \<Rightarrow> bool" where
haftmann@31998
   102
  [code_post]: "is_none x \<longleftrightarrow> x = None"
nipkow@30246
   103
nipkow@30246
   104
lemma is_none_code [code]:
nipkow@30246
   105
  shows "is_none None \<longleftrightarrow> True"
nipkow@30246
   106
    and "is_none (Some x) \<longleftrightarrow> False"
haftmann@31154
   107
  unfolding is_none_def by simp_all
haftmann@31154
   108
haftmann@31154
   109
lemma is_none_none:
haftmann@31154
   110
  "is_none x \<longleftrightarrow> x = None"
haftmann@31154
   111
  by (simp add: is_none_def)
haftmann@31154
   112
haftmann@32069
   113
lemma [code_unfold]:
haftmann@38857
   114
  "HOL.equal x None \<longleftrightarrow> is_none x"
haftmann@38857
   115
  by (simp add: equal is_none_none)
nipkow@30246
   116
wenzelm@36176
   117
hide_const (open) is_none
nipkow@30246
   118
nipkow@30246
   119
code_type option
nipkow@30246
   120
  (SML "_ option")
nipkow@30246
   121
  (OCaml "_ option")
nipkow@30246
   122
  (Haskell "Maybe _")
haftmann@34886
   123
  (Scala "!Option[(_)]")
nipkow@30246
   124
nipkow@30246
   125
code_const None and Some
nipkow@30246
   126
  (SML "NONE" and "SOME")
nipkow@30246
   127
  (OCaml "None" and "Some _")
nipkow@30246
   128
  (Haskell "Nothing" and "Just")
haftmann@37880
   129
  (Scala "!None" and "Some")
nipkow@30246
   130
haftmann@38857
   131
code_instance option :: equal
nipkow@30246
   132
  (Haskell -)
nipkow@30246
   133
haftmann@38857
   134
code_const "HOL.equal \<Colon> 'a option \<Rightarrow> 'a option \<Rightarrow> bool"
nipkow@30246
   135
  (Haskell infixl 4 "==")
nipkow@30246
   136
nipkow@30246
   137
code_reserved SML
nipkow@30246
   138
  option NONE SOME
nipkow@30246
   139
nipkow@30246
   140
code_reserved OCaml
nipkow@30246
   141
  option None Some
nipkow@30246
   142
haftmann@34886
   143
code_reserved Scala
haftmann@34886
   144
  Option None Some
haftmann@34886
   145
nipkow@30246
   146
end