src/HOL/Option.thy
author haftmann
Mon Apr 27 10:11:44 2009 +0200 (2009-04-27)
changeset 31001 7e6ffd8f51a9
parent 30327 4d1185c77f4a
child 31080 21ffc770ebc0
permissions -rw-r--r--
cleaned up theory power further
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@30246
    23
lemma option_caseE:
nipkow@30246
    24
  assumes c: "(case x of None => P | Some y => Q y)"
nipkow@30246
    25
  obtains
nipkow@30246
    26
    (None) "x = None" and P
nipkow@30246
    27
  | (Some) y where "x = Some y" and "Q y"
nipkow@30246
    28
  using c by (cases x) simp_all
nipkow@30246
    29
nipkow@30246
    30
lemma insert_None_conv_UNIV: "insert None (range Some) = UNIV"
nipkow@30246
    31
  by (rule set_ext, case_tac x) auto
nipkow@30246
    32
haftmann@30327
    33
instance option :: (finite) finite proof
haftmann@30327
    34
qed (simp add: insert_None_conv_UNIV [symmetric])
haftmann@30327
    35
nipkow@30246
    36
lemma inj_Some [simp]: "inj_on Some A"
nipkow@30246
    37
  by (rule inj_onI) simp
nipkow@30246
    38
nipkow@30246
    39
nipkow@30246
    40
subsubsection {* Operations *}
nipkow@30246
    41
nipkow@30246
    42
primrec the :: "'a option => 'a" where
nipkow@30246
    43
"the (Some x) = x"
nipkow@30246
    44
nipkow@30246
    45
primrec set :: "'a option => 'a set" where
nipkow@30246
    46
"set None = {}" |
nipkow@30246
    47
"set (Some x) = {x}"
nipkow@30246
    48
nipkow@30246
    49
lemma ospec [dest]: "(ALL x:set A. P x) ==> A = Some x ==> P x"
nipkow@30246
    50
  by simp
nipkow@30246
    51
nipkow@30246
    52
declaration {* fn _ =>
nipkow@30246
    53
  Classical.map_cs (fn cs => cs addSD2 ("ospec", thm "ospec"))
nipkow@30246
    54
*}
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
nipkow@30246
    62
definition
nipkow@30246
    63
  map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a option \<Rightarrow> 'b option"
nipkow@30246
    64
where
nipkow@30246
    65
  [code del]: "map = (%f y. case y of None => None | Some x => Some (f x))"
nipkow@30246
    66
nipkow@30246
    67
lemma option_map_None [simp, code]: "map f None = None"
nipkow@30246
    68
  by (simp add: map_def)
nipkow@30246
    69
nipkow@30246
    70
lemma option_map_Some [simp, code]: "map f (Some x) = Some (f x)"
nipkow@30246
    71
  by (simp add: map_def)
nipkow@30246
    72
nipkow@30246
    73
lemma option_map_is_None [iff]:
nipkow@30246
    74
    "(map f opt = None) = (opt = None)"
nipkow@30246
    75
  by (simp add: map_def split add: option.split)
nipkow@30246
    76
nipkow@30246
    77
lemma option_map_eq_Some [iff]:
nipkow@30246
    78
    "(map f xo = Some y) = (EX z. xo = Some z & f z = y)"
nipkow@30246
    79
  by (simp add: map_def split add: option.split)
nipkow@30246
    80
nipkow@30246
    81
lemma option_map_comp:
nipkow@30246
    82
    "map f (map g opt) = map (f o g) opt"
nipkow@30246
    83
  by (simp add: map_def split add: option.split)
nipkow@30246
    84
nipkow@30246
    85
lemma option_map_o_sum_case [simp]:
nipkow@30246
    86
    "map f o sum_case g h = sum_case (map f o g) (map f o h)"
nipkow@30246
    87
  by (rule ext) (simp split: sum.split)
nipkow@30246
    88
nipkow@30246
    89
nipkow@30246
    90
hide (open) const set map
nipkow@30246
    91
nipkow@30246
    92
subsubsection {* Code generator setup *}
nipkow@30246
    93
nipkow@30246
    94
definition
nipkow@30246
    95
  is_none :: "'a option \<Rightarrow> bool" where
nipkow@30246
    96
  is_none_none [code post, symmetric, code inline]: "is_none x \<longleftrightarrow> x = None"
nipkow@30246
    97
nipkow@30246
    98
lemma is_none_code [code]:
nipkow@30246
    99
  shows "is_none None \<longleftrightarrow> True"
nipkow@30246
   100
    and "is_none (Some x) \<longleftrightarrow> False"
nipkow@30246
   101
  unfolding is_none_none [symmetric] by simp_all
nipkow@30246
   102
nipkow@30246
   103
hide (open) const is_none
nipkow@30246
   104
nipkow@30246
   105
code_type option
nipkow@30246
   106
  (SML "_ option")
nipkow@30246
   107
  (OCaml "_ option")
nipkow@30246
   108
  (Haskell "Maybe _")
nipkow@30246
   109
nipkow@30246
   110
code_const None and Some
nipkow@30246
   111
  (SML "NONE" and "SOME")
nipkow@30246
   112
  (OCaml "None" and "Some _")
nipkow@30246
   113
  (Haskell "Nothing" and "Just")
nipkow@30246
   114
nipkow@30246
   115
code_instance option :: eq
nipkow@30246
   116
  (Haskell -)
nipkow@30246
   117
nipkow@30246
   118
code_const "eq_class.eq \<Colon> 'a\<Colon>eq option \<Rightarrow> 'a option \<Rightarrow> bool"
nipkow@30246
   119
  (Haskell infixl 4 "==")
nipkow@30246
   120
nipkow@30246
   121
code_reserved SML
nipkow@30246
   122
  option NONE SOME
nipkow@30246
   123
nipkow@30246
   124
code_reserved OCaml
nipkow@30246
   125
  option None Some
nipkow@30246
   126
nipkow@30246
   127
end