src/HOL/Option.thy
author haftmann
Fri Oct 10 19:55:32 2014 +0200 (2014-10-10)
changeset 58646 cd63a4b12a33
parent 58310 91ea607a34d8
child 58889 5b7a9633cfa8
permissions -rw-r--r--
specialized specification: avoid trivial instances
     1 (*  Title:      HOL/Option.thy
     2     Author:     Folklore
     3 *)
     4 
     5 header {* Datatype option *}
     6 
     7 theory Option
     8 imports BNF_Least_Fixpoint 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 primrec bind :: "'a option \<Rightarrow> ('a \<Rightarrow> 'b option) \<Rightarrow> 'b option" where
   118 bind_lzero: "bind None f = None" |
   119 bind_lunit: "bind (Some x) f = f x"
   120 
   121 lemma bind_runit[simp]: "bind x Some = x"
   122 by (cases x) auto
   123 
   124 lemma bind_assoc[simp]: "bind (bind x f) g = bind x (\<lambda>y. bind (f y) g)"
   125 by (cases x) auto
   126 
   127 lemma bind_rzero[simp]: "bind x (\<lambda>x. None) = None"
   128 by (cases x) auto
   129 
   130 lemma bind_cong: "x = y \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> f a = g a) \<Longrightarrow> bind x f = bind y g"
   131 by (cases x) auto
   132 
   133 definition these :: "'a option set \<Rightarrow> 'a set"
   134 where
   135   "these A = the ` {x \<in> A. x \<noteq> None}"
   136 
   137 lemma these_empty [simp]:
   138   "these {} = {}"
   139   by (simp add: these_def)
   140 
   141 lemma these_insert_None [simp]:
   142   "these (insert None A) = these A"
   143   by (auto simp add: these_def)
   144 
   145 lemma these_insert_Some [simp]:
   146   "these (insert (Some x) A) = insert x (these A)"
   147 proof -
   148   have "{y \<in> insert (Some x) A. y \<noteq> None} = insert (Some x) {y \<in> A. y \<noteq> None}"
   149     by auto
   150   then show ?thesis by (simp add: these_def)
   151 qed
   152 
   153 lemma in_these_eq:
   154   "x \<in> these A \<longleftrightarrow> Some x \<in> A"
   155 proof
   156   assume "Some x \<in> A"
   157   then obtain B where "A = insert (Some x) B" by auto
   158   then show "x \<in> these A" by (auto simp add: these_def intro!: image_eqI)
   159 next
   160   assume "x \<in> these A"
   161   then show "Some x \<in> A" by (auto simp add: these_def)
   162 qed
   163 
   164 lemma these_image_Some_eq [simp]:
   165   "these (Some ` A) = A"
   166   by (auto simp add: these_def intro!: image_eqI)
   167 
   168 lemma Some_image_these_eq:
   169   "Some ` these A = {x\<in>A. x \<noteq> None}"
   170   by (auto simp add: these_def image_image intro!: image_eqI)
   171 
   172 lemma these_empty_eq:
   173   "these B = {} \<longleftrightarrow> B = {} \<or> B = {None}"
   174   by (auto simp add: these_def)
   175 
   176 lemma these_not_empty_eq:
   177   "these B \<noteq> {} \<longleftrightarrow> B \<noteq> {} \<and> B \<noteq> {None}"
   178   by (auto simp add: these_empty_eq)
   179 
   180 hide_const (open) bind these
   181 hide_fact (open) bind_cong
   182 
   183 
   184 subsubsection {* Interaction with finite sets *}
   185 
   186 lemma finite_option_UNIV [simp]:
   187   "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
   188   by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
   189 
   190 instance option :: (finite) finite
   191   by default (simp add: UNIV_option_conv)
   192 
   193 
   194 subsubsection {* Code generator setup *}
   195 
   196 definition is_none :: "'a option \<Rightarrow> bool" where
   197   [code_post]: "is_none x \<longleftrightarrow> x = None"
   198 
   199 lemma is_none_code [code]:
   200   shows "is_none None \<longleftrightarrow> True"
   201     and "is_none (Some x) \<longleftrightarrow> False"
   202   unfolding is_none_def by simp_all
   203 
   204 lemma [code_unfold]:
   205   "HOL.equal x None \<longleftrightarrow> is_none x"
   206   "HOL.equal None = is_none"
   207   by (auto simp add: equal is_none_def)
   208 
   209 hide_const (open) is_none
   210 
   211 code_printing
   212   type_constructor option \<rightharpoonup>
   213     (SML) "_ option"
   214     and (OCaml) "_ option"
   215     and (Haskell) "Maybe _"
   216     and (Scala) "!Option[(_)]"
   217 | constant None \<rightharpoonup>
   218     (SML) "NONE"
   219     and (OCaml) "None"
   220     and (Haskell) "Nothing"
   221     and (Scala) "!None"
   222 | constant Some \<rightharpoonup>
   223     (SML) "SOME"
   224     and (OCaml) "Some _"
   225     and (Haskell) "Just"
   226     and (Scala) "Some"
   227 | class_instance option :: equal \<rightharpoonup>
   228     (Haskell) -
   229 | constant "HOL.equal :: 'a option \<Rightarrow> 'a option \<Rightarrow> bool" \<rightharpoonup>
   230     (Haskell) infix 4 "=="
   231 
   232 code_reserved SML
   233   option NONE SOME
   234 
   235 code_reserved OCaml
   236   option None Some
   237 
   238 code_reserved Scala
   239   Option None Some
   240 
   241 end