| author | wenzelm | 
| Thu, 11 Jul 2013 14:56:58 +0200 | |
| changeset 52597 | a8a81453833d | 
| parent 52435 | 6646bb548c6b | 
| child 53010 | ec5e6f69bd65 | 
| permissions | -rw-r--r-- | 
| 30246 | 1 | (* Title: HOL/Option.thy | 
| 2 | Author: Folklore | |
| 3 | *) | |
| 4 | ||
| 5 | header {* Datatype option *}
 | |
| 6 | ||
| 7 | theory Option | |
| 35719 
99b6152aedf5
split off theory Big_Operators from theory Finite_Set
 haftmann parents: 
34886diff
changeset | 8 | imports Datatype | 
| 30246 | 9 | begin | 
| 10 | ||
| 11 | datatype 'a option = None | Some 'a | |
| 12 | ||
| 13 | lemma not_None_eq [iff]: "(x ~= None) = (EX y. x = Some y)" | |
| 14 | by (induct x) auto | |
| 15 | ||
| 16 | lemma not_Some_eq [iff]: "(ALL y. x ~= Some y) = (x = None)" | |
| 17 | by (induct x) auto | |
| 18 | ||
| 19 | text{*Although it may appear that both of these equalities are helpful
 | |
| 20 | only when applied to assumptions, in practice it seems better to give | |
| 21 | them the uniform iff attribute. *} | |
| 22 | ||
| 31080 | 23 | lemma inj_Some [simp]: "inj_on Some A" | 
| 24 | by (rule inj_onI) simp | |
| 25 | ||
| 30246 | 26 | lemma option_caseE: | 
| 27 | assumes c: "(case x of None => P | Some y => Q y)" | |
| 28 | obtains | |
| 29 | (None) "x = None" and P | |
| 30 | | (Some) y where "x = Some y" and "Q y" | |
| 31 | using c by (cases x) simp_all | |
| 32 | ||
| 31080 | 33 | lemma UNIV_option_conv: "UNIV = insert None (range Some)" | 
| 34 | by(auto intro: classical) | |
| 35 | ||
| 30246 | 36 | |
| 37 | subsubsection {* Operations *}
 | |
| 38 | ||
| 39 | primrec the :: "'a option => 'a" where | |
| 40 | "the (Some x) = x" | |
| 41 | ||
| 42 | primrec set :: "'a option => 'a set" where | |
| 43 | "set None = {}" |
 | |
| 44 | "set (Some x) = {x}"
 | |
| 45 | ||
| 46 | lemma ospec [dest]: "(ALL x:set A. P x) ==> A = Some x ==> P x" | |
| 47 | by simp | |
| 48 | ||
| 51703 
f2e92fc0c8aa
modifiers for classical wrappers operate on Proof.context instead of claset;
 wenzelm parents: 
51096diff
changeset | 49 | setup {* map_theory_claset (fn ctxt => ctxt addSD2 ("ospec", @{thm ospec})) *}
 | 
| 30246 | 50 | |
| 51 | lemma elem_set [iff]: "(x : set xo) = (xo = Some x)" | |
| 52 | by (cases xo) auto | |
| 53 | ||
| 54 | lemma set_empty_eq [simp]: "(set xo = {}) = (xo = None)"
 | |
| 55 | by (cases xo) auto | |
| 56 | ||
| 31154 | 57 | definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a option \<Rightarrow> 'b option" where
 | 
| 58 | "map = (%f y. case y of None => None | Some x => Some (f x))" | |
| 30246 | 59 | |
| 60 | lemma option_map_None [simp, code]: "map f None = None" | |
| 61 | by (simp add: map_def) | |
| 62 | ||
| 63 | lemma option_map_Some [simp, code]: "map f (Some x) = Some (f x)" | |
| 64 | by (simp add: map_def) | |
| 65 | ||
| 66 | lemma option_map_is_None [iff]: | |
| 67 | "(map f opt = None) = (opt = None)" | |
| 68 | by (simp add: map_def split add: option.split) | |
| 69 | ||
| 70 | lemma option_map_eq_Some [iff]: | |
| 71 | "(map f xo = Some y) = (EX z. xo = Some z & f z = y)" | |
| 72 | by (simp add: map_def split add: option.split) | |
| 73 | ||
| 74 | lemma option_map_comp: | |
| 75 | "map f (map g opt) = map (f o g) opt" | |
| 76 | by (simp add: map_def split add: option.split) | |
| 77 | ||
| 78 | lemma option_map_o_sum_case [simp]: | |
| 79 | "map f o sum_case g h = sum_case (map f o g) (map f o h)" | |
| 80 | by (rule ext) (simp split: sum.split) | |
| 81 | ||
| 46526 | 82 | lemma map_cong: "x = y \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> f a = g a) \<Longrightarrow> map f x = map g y" | 
| 83 | by (cases x) auto | |
| 84 | ||
| 41505 
6d19301074cf
"enriched_type" replaces less specific "type_lifting"
 haftmann parents: 
41372diff
changeset | 85 | enriched_type map: Option.map proof - | 
| 41372 | 86 | fix f g | 
| 87 | show "Option.map f \<circ> Option.map g = Option.map (f \<circ> g)" | |
| 88 | proof | |
| 89 | fix x | |
| 90 | show "(Option.map f \<circ> Option.map g) x= Option.map (f \<circ> g) x" | |
| 91 | by (cases x) simp_all | |
| 92 | qed | |
| 40609 | 93 | next | 
| 41372 | 94 | show "Option.map id = id" | 
| 95 | proof | |
| 96 | fix x | |
| 97 | show "Option.map id x = id x" | |
| 98 | by (cases x) simp_all | |
| 99 | qed | |
| 40609 | 100 | qed | 
| 101 | ||
| 51096 | 102 | lemma option_case_map [simp]: | 
| 103 | "option_case g h (Option.map f x) = option_case g (h \<circ> f) x" | |
| 104 | by (cases x) simp_all | |
| 105 | ||
| 39149 | 106 | primrec bind :: "'a option \<Rightarrow> ('a \<Rightarrow> 'b option) \<Rightarrow> 'b option" where
 | 
| 107 | bind_lzero: "bind None f = None" | | |
| 108 | bind_lunit: "bind (Some x) f = f x" | |
| 30246 | 109 | |
| 39149 | 110 | lemma bind_runit[simp]: "bind x Some = x" | 
| 111 | by (cases x) auto | |
| 112 | ||
| 113 | lemma bind_assoc[simp]: "bind (bind x f) g = bind x (\<lambda>y. bind (f y) g)" | |
| 114 | by (cases x) auto | |
| 115 | ||
| 116 | lemma bind_rzero[simp]: "bind x (\<lambda>x. None) = None" | |
| 117 | by (cases x) auto | |
| 118 | ||
| 46526 | 119 | lemma bind_cong: "x = y \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> f a = g a) \<Longrightarrow> bind x f = bind y g" | 
| 120 | by (cases x) auto | |
| 121 | ||
| 49189 | 122 | definition these :: "'a option set \<Rightarrow> 'a set" | 
| 123 | where | |
| 124 |   "these A = the ` {x \<in> A. x \<noteq> None}"
 | |
| 125 | ||
| 126 | lemma these_empty [simp]: | |
| 127 |   "these {} = {}"
 | |
| 128 | by (simp add: these_def) | |
| 129 | ||
| 130 | lemma these_insert_None [simp]: | |
| 131 | "these (insert None A) = these A" | |
| 132 | by (auto simp add: these_def) | |
| 133 | ||
| 134 | lemma these_insert_Some [simp]: | |
| 135 | "these (insert (Some x) A) = insert x (these A)" | |
| 136 | proof - | |
| 137 |   have "{y \<in> insert (Some x) A. y \<noteq> None} = insert (Some x) {y \<in> A. y \<noteq> None}"
 | |
| 138 | by auto | |
| 139 | then show ?thesis by (simp add: these_def) | |
| 140 | qed | |
| 141 | ||
| 142 | lemma in_these_eq: | |
| 143 | "x \<in> these A \<longleftrightarrow> Some x \<in> A" | |
| 144 | proof | |
| 145 | assume "Some x \<in> A" | |
| 146 | then obtain B where "A = insert (Some x) B" by auto | |
| 147 | then show "x \<in> these A" by (auto simp add: these_def intro!: image_eqI) | |
| 148 | next | |
| 149 | assume "x \<in> these A" | |
| 150 | then show "Some x \<in> A" by (auto simp add: these_def) | |
| 151 | qed | |
| 152 | ||
| 153 | lemma these_image_Some_eq [simp]: | |
| 154 | "these (Some ` A) = A" | |
| 155 | by (auto simp add: these_def intro!: image_eqI) | |
| 156 | ||
| 157 | lemma Some_image_these_eq: | |
| 158 |   "Some ` these A = {x\<in>A. x \<noteq> None}"
 | |
| 159 | by (auto simp add: these_def image_image intro!: image_eqI) | |
| 160 | ||
| 161 | lemma these_empty_eq: | |
| 162 |   "these B = {} \<longleftrightarrow> B = {} \<or> B = {None}"
 | |
| 163 | by (auto simp add: these_def) | |
| 164 | ||
| 165 | lemma these_not_empty_eq: | |
| 166 |   "these B \<noteq> {} \<longleftrightarrow> B \<noteq> {} \<and> B \<noteq> {None}"
 | |
| 167 | by (auto simp add: these_empty_eq) | |
| 168 | ||
| 169 | hide_const (open) set map bind these | |
| 46526 | 170 | hide_fact (open) map_cong bind_cong | 
| 30246 | 171 | |
| 49189 | 172 | |
| 30246 | 173 | subsubsection {* Code generator setup *}
 | 
| 174 | ||
| 31154 | 175 | definition is_none :: "'a option \<Rightarrow> bool" where | 
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31154diff
changeset | 176 | [code_post]: "is_none x \<longleftrightarrow> x = None" | 
| 30246 | 177 | |
| 178 | lemma is_none_code [code]: | |
| 179 | shows "is_none None \<longleftrightarrow> True" | |
| 180 | and "is_none (Some x) \<longleftrightarrow> False" | |
| 31154 | 181 | unfolding is_none_def by simp_all | 
| 182 | ||
| 32069 
6d28bbd33e2c
prefer code_inline over code_unfold; use code_unfold_post where appropriate
 haftmann parents: 
31998diff
changeset | 183 | lemma [code_unfold]: | 
| 38857 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
37880diff
changeset | 184 | "HOL.equal x None \<longleftrightarrow> is_none x" | 
| 39150 | 185 | by (simp add: equal is_none_def) | 
| 30246 | 186 | |
| 36176 
3fe7e97ccca8
replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
 wenzelm parents: 
35719diff
changeset | 187 | hide_const (open) is_none | 
| 30246 | 188 | |
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 189 | code_printing | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 190 | type_constructor option \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 191 | (SML) "_ option" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 192 | and (OCaml) "_ option" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 193 | and (Haskell) "Maybe _" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 194 | and (Scala) "!Option[(_)]" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 195 | | constant None \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 196 | (SML) "NONE" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 197 | and (OCaml) "None" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 198 | and (Haskell) "Nothing" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 199 | and (Scala) "!None" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 200 | | constant Some \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 201 | (SML) "SOME" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 202 | and (OCaml) "Some _" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 203 | and (Haskell) "Just" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 204 | and (Scala) "Some" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 205 | | class_instance option :: equal \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 206 | (Haskell) - | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 207 | | constant "HOL.equal :: 'a option \<Rightarrow> 'a option \<Rightarrow> bool" \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51703diff
changeset | 208 | (Haskell) infix 4 "==" | 
| 30246 | 209 | |
| 210 | code_reserved SML | |
| 211 | option NONE SOME | |
| 212 | ||
| 213 | code_reserved OCaml | |
| 214 | option None Some | |
| 215 | ||
| 34886 | 216 | code_reserved Scala | 
| 217 | Option None Some | |
| 218 | ||
| 30246 | 219 | end | 
| 49189 | 220 |