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