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