src/HOL/Option.thy
author haftmann
Tue Jul 14 10:54:04 2009 +0200 (2009-07-14)
changeset 31998 2c7a24f74db9
parent 31154 f919b8e67413
child 32069 6d28bbd33e2c
permissions -rw-r--r--
code attributes use common underscore convention
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@30327
     8
imports Datatype Finite_Set
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@31080
    36
lemma finite_option_UNIV[simp]:
nipkow@31080
    37
  "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
nipkow@31080
    38
by(auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
nipkow@30246
    39
haftmann@30327
    40
instance option :: (finite) finite proof
nipkow@31080
    41
qed (simp add: UNIV_option_conv)
nipkow@30246
    42
nipkow@30246
    43
nipkow@30246
    44
subsubsection {* Operations *}
nipkow@30246
    45
nipkow@30246
    46
primrec the :: "'a option => 'a" where
nipkow@30246
    47
"the (Some x) = x"
nipkow@30246
    48
nipkow@30246
    49
primrec set :: "'a option => 'a set" where
nipkow@30246
    50
"set None = {}" |
nipkow@30246
    51
"set (Some x) = {x}"
nipkow@30246
    52
nipkow@30246
    53
lemma ospec [dest]: "(ALL x:set A. P x) ==> A = Some x ==> P x"
nipkow@30246
    54
  by simp
nipkow@30246
    55
nipkow@30246
    56
declaration {* fn _ =>
nipkow@30246
    57
  Classical.map_cs (fn cs => cs addSD2 ("ospec", thm "ospec"))
nipkow@30246
    58
*}
nipkow@30246
    59
nipkow@30246
    60
lemma elem_set [iff]: "(x : set xo) = (xo = Some x)"
nipkow@30246
    61
  by (cases xo) auto
nipkow@30246
    62
nipkow@30246
    63
lemma set_empty_eq [simp]: "(set xo = {}) = (xo = None)"
nipkow@30246
    64
  by (cases xo) auto
nipkow@30246
    65
haftmann@31154
    66
definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a option \<Rightarrow> 'b option" where
haftmann@31154
    67
  "map = (%f y. case y of None => None | Some x => Some (f x))"
nipkow@30246
    68
nipkow@30246
    69
lemma option_map_None [simp, code]: "map f None = None"
nipkow@30246
    70
  by (simp add: map_def)
nipkow@30246
    71
nipkow@30246
    72
lemma option_map_Some [simp, code]: "map f (Some x) = Some (f x)"
nipkow@30246
    73
  by (simp add: map_def)
nipkow@30246
    74
nipkow@30246
    75
lemma option_map_is_None [iff]:
nipkow@30246
    76
    "(map f opt = None) = (opt = None)"
nipkow@30246
    77
  by (simp add: map_def split add: option.split)
nipkow@30246
    78
nipkow@30246
    79
lemma option_map_eq_Some [iff]:
nipkow@30246
    80
    "(map f xo = Some y) = (EX z. xo = Some z & f z = y)"
nipkow@30246
    81
  by (simp add: map_def split add: option.split)
nipkow@30246
    82
nipkow@30246
    83
lemma option_map_comp:
nipkow@30246
    84
    "map f (map g opt) = map (f o g) opt"
nipkow@30246
    85
  by (simp add: map_def split add: option.split)
nipkow@30246
    86
nipkow@30246
    87
lemma option_map_o_sum_case [simp]:
nipkow@30246
    88
    "map f o sum_case g h = sum_case (map f o g) (map f o h)"
nipkow@30246
    89
  by (rule ext) (simp split: sum.split)
nipkow@30246
    90
nipkow@30246
    91
nipkow@30246
    92
hide (open) const set map
nipkow@30246
    93
nipkow@30246
    94
subsubsection {* Code generator setup *}
nipkow@30246
    95
haftmann@31154
    96
definition is_none :: "'a option \<Rightarrow> bool" where
haftmann@31998
    97
  [code_post]: "is_none x \<longleftrightarrow> x = None"
nipkow@30246
    98
nipkow@30246
    99
lemma is_none_code [code]:
nipkow@30246
   100
  shows "is_none None \<longleftrightarrow> True"
nipkow@30246
   101
    and "is_none (Some x) \<longleftrightarrow> False"
haftmann@31154
   102
  unfolding is_none_def by simp_all
haftmann@31154
   103
haftmann@31154
   104
lemma is_none_none:
haftmann@31154
   105
  "is_none x \<longleftrightarrow> x = None"
haftmann@31154
   106
  by (simp add: is_none_def)
haftmann@31154
   107
haftmann@31998
   108
lemma [code_inline]:
haftmann@31154
   109
  "eq_class.eq x None \<longleftrightarrow> is_none x"
haftmann@31154
   110
  by (simp add: eq is_none_none)
nipkow@30246
   111
nipkow@30246
   112
hide (open) const is_none
nipkow@30246
   113
nipkow@30246
   114
code_type option
nipkow@30246
   115
  (SML "_ option")
nipkow@30246
   116
  (OCaml "_ option")
nipkow@30246
   117
  (Haskell "Maybe _")
nipkow@30246
   118
nipkow@30246
   119
code_const None and Some
nipkow@30246
   120
  (SML "NONE" and "SOME")
nipkow@30246
   121
  (OCaml "None" and "Some _")
nipkow@30246
   122
  (Haskell "Nothing" and "Just")
nipkow@30246
   123
nipkow@30246
   124
code_instance option :: eq
nipkow@30246
   125
  (Haskell -)
nipkow@30246
   126
nipkow@30246
   127
code_const "eq_class.eq \<Colon> 'a\<Colon>eq option \<Rightarrow> 'a option \<Rightarrow> bool"
nipkow@30246
   128
  (Haskell infixl 4 "==")
nipkow@30246
   129
nipkow@30246
   130
code_reserved SML
nipkow@30246
   131
  option NONE SOME
nipkow@30246
   132
nipkow@30246
   133
code_reserved OCaml
nipkow@30246
   134
  option None Some
nipkow@30246
   135
nipkow@30246
   136
end