src/HOL/Option.thy
author traytel
Fri Nov 07 11:28:37 2014 +0100 (2014-11-07)
changeset 58916 229765cc3414
parent 58895 de0a4a76d7aa
child 59521 ef8ac8d2315e
permissions -rw-r--r--
more complete fp_sugars for sum and prod;
tuned;
removed theorem duplicates;
removed obsolete Lifting_{Option,Product,Sum} theories
     1 (*  Title:      HOL/Option.thy
     2     Author:     Folklore
     3 *)
     4 
     5 section {* Datatype option *}
     6 
     7 theory Option
     8 imports Lifting Finite_Set
     9 begin
    10 
    11 datatype 'a option =
    12     None
    13   | Some (the: 'a)
    14 
    15 datatype_compat option
    16 
    17 lemma [case_names None Some, cases type: option]:
    18   -- {* for backward compatibility -- names of variables differ *}
    19   "(y = None \<Longrightarrow> P) \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> P) \<Longrightarrow> P"
    20 by (rule option.exhaust)
    21 
    22 lemma [case_names None Some, induct type: option]:
    23   -- {* for backward compatibility -- names of variables differ *}
    24   "P None \<Longrightarrow> (\<And>option. P (Some option)) \<Longrightarrow> P option"
    25 by (rule option.induct)
    26 
    27 text {* Compatibility: *}
    28 
    29 setup {* Sign.mandatory_path "option" *}
    30 
    31 lemmas inducts = option.induct
    32 lemmas cases = option.case
    33 
    34 setup {* Sign.parent_path *}
    35 
    36 lemma not_None_eq [iff]: "(x ~= None) = (EX y. x = Some y)"
    37   by (induct x) auto
    38 
    39 lemma not_Some_eq [iff]: "(ALL y. x ~= Some y) = (x = None)"
    40   by (induct x) auto
    41 
    42 text{*Although it may appear that both of these equalities are helpful
    43 only when applied to assumptions, in practice it seems better to give
    44 them the uniform iff attribute. *}
    45 
    46 lemma inj_Some [simp]: "inj_on Some A"
    47 by (rule inj_onI) simp
    48 
    49 lemma case_optionE:
    50   assumes c: "(case x of None => P | Some y => Q y)"
    51   obtains
    52     (None) "x = None" and P
    53   | (Some) y where "x = Some y" and "Q y"
    54   using c by (cases x) simp_all
    55 
    56 lemma split_option_all: "(\<forall>x. P x) \<longleftrightarrow> P None \<and> (\<forall>x. P (Some x))"
    57 by (auto intro: option.induct)
    58 
    59 lemma split_option_ex: "(\<exists>x. P x) \<longleftrightarrow> P None \<or> (\<exists>x. P (Some x))"
    60 using split_option_all[of "\<lambda>x. \<not>P x"] by blast
    61 
    62 lemma UNIV_option_conv: "UNIV = insert None (range Some)"
    63 by(auto intro: classical)
    64 
    65 subsubsection {* Operations *}
    66 
    67 lemma ospec [dest]: "(ALL x:set_option A. P x) ==> A = Some x ==> P x"
    68   by simp
    69 
    70 setup {* map_theory_claset (fn ctxt => ctxt addSD2 ("ospec", @{thm ospec})) *}
    71 
    72 lemma elem_set [iff]: "(x : set_option xo) = (xo = Some x)"
    73   by (cases xo) auto
    74 
    75 lemma set_empty_eq [simp]: "(set_option xo = {}) = (xo = None)"
    76   by (cases xo) auto
    77 
    78 lemma map_option_case: "map_option f y = (case y of None => None | Some x => Some (f x))"
    79   by (auto split: option.split)
    80 
    81 lemma map_option_is_None [iff]:
    82     "(map_option f opt = None) = (opt = None)"
    83   by (simp add: map_option_case split add: option.split)
    84 
    85 lemma map_option_eq_Some [iff]:
    86     "(map_option f xo = Some y) = (EX z. xo = Some z & f z = y)"
    87   by (simp add: map_option_case split add: option.split)
    88 
    89 lemma map_option_o_case_sum [simp]:
    90     "map_option f o case_sum g h = case_sum (map_option f o g) (map_option f o h)"
    91   by (rule o_case_sum)
    92 
    93 lemma map_option_cong: "x = y \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> f a = g a) \<Longrightarrow> map_option f x = map_option g y"
    94 by (cases x) auto
    95 
    96 functor map_option: map_option proof -
    97   fix f g
    98   show "map_option f \<circ> map_option g = map_option (f \<circ> g)"
    99   proof
   100     fix x
   101     show "(map_option f \<circ> map_option g) x= map_option (f \<circ> g) x"
   102       by (cases x) simp_all
   103   qed
   104 next
   105   show "map_option id = id"
   106   proof
   107     fix x
   108     show "map_option id x = id x"
   109       by (cases x) simp_all
   110   qed
   111 qed
   112 
   113 lemma case_map_option [simp]:
   114   "case_option g h (map_option f x) = case_option g (h \<circ> f) x"
   115   by (cases x) simp_all
   116 
   117 lemma rel_option_iff:
   118   "rel_option R x y = (case (x, y) of (None, None) \<Rightarrow> True
   119     | (Some x, Some y) \<Rightarrow> R x y
   120     | _ \<Rightarrow> False)"
   121 by (auto split: prod.split option.split)
   122 
   123 primrec bind :: "'a option \<Rightarrow> ('a \<Rightarrow> 'b option) \<Rightarrow> 'b option" where
   124 bind_lzero: "bind None f = None" |
   125 bind_lunit: "bind (Some x) f = f x"
   126 
   127 lemma bind_runit[simp]: "bind x Some = x"
   128 by (cases x) auto
   129 
   130 lemma bind_assoc[simp]: "bind (bind x f) g = bind x (\<lambda>y. bind (f y) g)"
   131 by (cases x) auto
   132 
   133 lemma bind_rzero[simp]: "bind x (\<lambda>x. None) = None"
   134 by (cases x) auto
   135 
   136 lemma bind_cong: "x = y \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> f a = g a) \<Longrightarrow> bind x f = bind y g"
   137 by (cases x) auto
   138 
   139 lemma bind_split: "P (bind m f) 
   140   \<longleftrightarrow> (m = None \<longrightarrow> P None) \<and> (\<forall>v. m=Some v \<longrightarrow> P (f v))"
   141     by (cases m) auto
   142 
   143 lemma bind_split_asm: "P (bind m f) = (\<not>(
   144     m=None \<and> \<not>P None 
   145   \<or> (\<exists>x. m=Some x \<and> \<not>P (f x))))"
   146   by (cases m) auto
   147 
   148 lemmas bind_splits = bind_split bind_split_asm
   149 
   150 definition these :: "'a option set \<Rightarrow> 'a set"
   151 where
   152   "these A = the ` {x \<in> A. x \<noteq> None}"
   153 
   154 lemma these_empty [simp]:
   155   "these {} = {}"
   156   by (simp add: these_def)
   157 
   158 lemma these_insert_None [simp]:
   159   "these (insert None A) = these A"
   160   by (auto simp add: these_def)
   161 
   162 lemma these_insert_Some [simp]:
   163   "these (insert (Some x) A) = insert x (these A)"
   164 proof -
   165   have "{y \<in> insert (Some x) A. y \<noteq> None} = insert (Some x) {y \<in> A. y \<noteq> None}"
   166     by auto
   167   then show ?thesis by (simp add: these_def)
   168 qed
   169 
   170 lemma in_these_eq:
   171   "x \<in> these A \<longleftrightarrow> Some x \<in> A"
   172 proof
   173   assume "Some x \<in> A"
   174   then obtain B where "A = insert (Some x) B" by auto
   175   then show "x \<in> these A" by (auto simp add: these_def intro!: image_eqI)
   176 next
   177   assume "x \<in> these A"
   178   then show "Some x \<in> A" by (auto simp add: these_def)
   179 qed
   180 
   181 lemma these_image_Some_eq [simp]:
   182   "these (Some ` A) = A"
   183   by (auto simp add: these_def intro!: image_eqI)
   184 
   185 lemma Some_image_these_eq:
   186   "Some ` these A = {x\<in>A. x \<noteq> None}"
   187   by (auto simp add: these_def image_image intro!: image_eqI)
   188 
   189 lemma these_empty_eq:
   190   "these B = {} \<longleftrightarrow> B = {} \<or> B = {None}"
   191   by (auto simp add: these_def)
   192 
   193 lemma these_not_empty_eq:
   194   "these B \<noteq> {} \<longleftrightarrow> B \<noteq> {} \<and> B \<noteq> {None}"
   195   by (auto simp add: these_empty_eq)
   196 
   197 hide_const (open) bind these
   198 hide_fact (open) bind_cong
   199 
   200 
   201 subsection {* Transfer rules for the Transfer package *}
   202 
   203 context
   204 begin
   205 interpretation lifting_syntax .
   206 
   207 lemma option_bind_transfer [transfer_rule]:
   208   "(rel_option A ===> (A ===> rel_option B) ===> rel_option B)
   209     Option.bind Option.bind"
   210   unfolding rel_fun_def split_option_all by simp
   211 
   212 end
   213 
   214 
   215 subsubsection {* Interaction with finite sets *}
   216 
   217 lemma finite_option_UNIV [simp]:
   218   "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
   219   by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
   220 
   221 instance option :: (finite) finite
   222   by default (simp add: UNIV_option_conv)
   223 
   224 
   225 subsubsection {* Code generator setup *}
   226 
   227 definition is_none :: "'a option \<Rightarrow> bool" where
   228   [code_post]: "is_none x \<longleftrightarrow> x = None"
   229 
   230 lemma is_none_code [code]:
   231   shows "is_none None \<longleftrightarrow> True"
   232     and "is_none (Some x) \<longleftrightarrow> False"
   233   unfolding is_none_def by simp_all
   234 
   235 lemma [code_unfold]:
   236   "HOL.equal x None \<longleftrightarrow> is_none x"
   237   "HOL.equal None = is_none"
   238   by (auto simp add: equal is_none_def)
   239 
   240 hide_const (open) is_none
   241 
   242 code_printing
   243   type_constructor option \<rightharpoonup>
   244     (SML) "_ option"
   245     and (OCaml) "_ option"
   246     and (Haskell) "Maybe _"
   247     and (Scala) "!Option[(_)]"
   248 | constant None \<rightharpoonup>
   249     (SML) "NONE"
   250     and (OCaml) "None"
   251     and (Haskell) "Nothing"
   252     and (Scala) "!None"
   253 | constant Some \<rightharpoonup>
   254     (SML) "SOME"
   255     and (OCaml) "Some _"
   256     and (Haskell) "Just"
   257     and (Scala) "Some"
   258 | class_instance option :: equal \<rightharpoonup>
   259     (Haskell) -
   260 | constant "HOL.equal :: 'a option \<Rightarrow> 'a option \<Rightarrow> bool" \<rightharpoonup>
   261     (Haskell) infix 4 "=="
   262 
   263 code_reserved SML
   264   option NONE SOME
   265 
   266 code_reserved OCaml
   267   option None Some
   268 
   269 code_reserved Scala
   270   Option None Some
   271 
   272 end