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