src/HOL/Option.thy
changeset 30246 8253519dfc90
child 30327 4d1185c77f4a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Option.thy	Wed Mar 04 11:48:52 2009 +0100
     1.3 @@ -0,0 +1,124 @@
     1.4 +(*  Title:      HOL/Option.thy
     1.5 +    Author:     Folklore
     1.6 +*)
     1.7 +
     1.8 +header {* Datatype option *}
     1.9 +
    1.10 +theory Option
    1.11 +imports Datatype
    1.12 +begin
    1.13 +
    1.14 +datatype 'a option = None | Some 'a
    1.15 +
    1.16 +lemma not_None_eq [iff]: "(x ~= None) = (EX y. x = Some y)"
    1.17 +  by (induct x) auto
    1.18 +
    1.19 +lemma not_Some_eq [iff]: "(ALL y. x ~= Some y) = (x = None)"
    1.20 +  by (induct x) auto
    1.21 +
    1.22 +text{*Although it may appear that both of these equalities are helpful
    1.23 +only when applied to assumptions, in practice it seems better to give
    1.24 +them the uniform iff attribute. *}
    1.25 +
    1.26 +lemma option_caseE:
    1.27 +  assumes c: "(case x of None => P | Some y => Q y)"
    1.28 +  obtains
    1.29 +    (None) "x = None" and P
    1.30 +  | (Some) y where "x = Some y" and "Q y"
    1.31 +  using c by (cases x) simp_all
    1.32 +
    1.33 +lemma insert_None_conv_UNIV: "insert None (range Some) = UNIV"
    1.34 +  by (rule set_ext, case_tac x) auto
    1.35 +
    1.36 +lemma inj_Some [simp]: "inj_on Some A"
    1.37 +  by (rule inj_onI) simp
    1.38 +
    1.39 +
    1.40 +subsubsection {* Operations *}
    1.41 +
    1.42 +primrec the :: "'a option => 'a" where
    1.43 +"the (Some x) = x"
    1.44 +
    1.45 +primrec set :: "'a option => 'a set" where
    1.46 +"set None = {}" |
    1.47 +"set (Some x) = {x}"
    1.48 +
    1.49 +lemma ospec [dest]: "(ALL x:set A. P x) ==> A = Some x ==> P x"
    1.50 +  by simp
    1.51 +
    1.52 +declaration {* fn _ =>
    1.53 +  Classical.map_cs (fn cs => cs addSD2 ("ospec", thm "ospec"))
    1.54 +*}
    1.55 +
    1.56 +lemma elem_set [iff]: "(x : set xo) = (xo = Some x)"
    1.57 +  by (cases xo) auto
    1.58 +
    1.59 +lemma set_empty_eq [simp]: "(set xo = {}) = (xo = None)"
    1.60 +  by (cases xo) auto
    1.61 +
    1.62 +definition
    1.63 +  map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a option \<Rightarrow> 'b option"
    1.64 +where
    1.65 +  [code del]: "map = (%f y. case y of None => None | Some x => Some (f x))"
    1.66 +
    1.67 +lemma option_map_None [simp, code]: "map f None = None"
    1.68 +  by (simp add: map_def)
    1.69 +
    1.70 +lemma option_map_Some [simp, code]: "map f (Some x) = Some (f x)"
    1.71 +  by (simp add: map_def)
    1.72 +
    1.73 +lemma option_map_is_None [iff]:
    1.74 +    "(map f opt = None) = (opt = None)"
    1.75 +  by (simp add: map_def split add: option.split)
    1.76 +
    1.77 +lemma option_map_eq_Some [iff]:
    1.78 +    "(map f xo = Some y) = (EX z. xo = Some z & f z = y)"
    1.79 +  by (simp add: map_def split add: option.split)
    1.80 +
    1.81 +lemma option_map_comp:
    1.82 +    "map f (map g opt) = map (f o g) opt"
    1.83 +  by (simp add: map_def split add: option.split)
    1.84 +
    1.85 +lemma option_map_o_sum_case [simp]:
    1.86 +    "map f o sum_case g h = sum_case (map f o g) (map f o h)"
    1.87 +  by (rule ext) (simp split: sum.split)
    1.88 +
    1.89 +
    1.90 +hide (open) const set map
    1.91 +
    1.92 +subsubsection {* Code generator setup *}
    1.93 +
    1.94 +definition
    1.95 +  is_none :: "'a option \<Rightarrow> bool" where
    1.96 +  is_none_none [code post, symmetric, code inline]: "is_none x \<longleftrightarrow> x = None"
    1.97 +
    1.98 +lemma is_none_code [code]:
    1.99 +  shows "is_none None \<longleftrightarrow> True"
   1.100 +    and "is_none (Some x) \<longleftrightarrow> False"
   1.101 +  unfolding is_none_none [symmetric] by simp_all
   1.102 +
   1.103 +hide (open) const is_none
   1.104 +
   1.105 +code_type option
   1.106 +  (SML "_ option")
   1.107 +  (OCaml "_ option")
   1.108 +  (Haskell "Maybe _")
   1.109 +
   1.110 +code_const None and Some
   1.111 +  (SML "NONE" and "SOME")
   1.112 +  (OCaml "None" and "Some _")
   1.113 +  (Haskell "Nothing" and "Just")
   1.114 +
   1.115 +code_instance option :: eq
   1.116 +  (Haskell -)
   1.117 +
   1.118 +code_const "eq_class.eq \<Colon> 'a\<Colon>eq option \<Rightarrow> 'a option \<Rightarrow> bool"
   1.119 +  (Haskell infixl 4 "==")
   1.120 +
   1.121 +code_reserved SML
   1.122 +  option NONE SOME
   1.123 +
   1.124 +code_reserved OCaml
   1.125 +  option None Some
   1.126 +
   1.127 +end