src/HOL/Option.thy
author blanchet
Wed Feb 12 17:35:59 2014 +0100 (2014-02-12)
changeset 55442 17fb554688f0
parent 55417 01fbfb60c33e
child 55466 786edc984c98
permissions -rw-r--r--
tuning
     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 primrec set :: "'a option => 'a set" where
    69 "set None = {}" |
    70 "set (Some x) = {x}"
    71 
    72 lemma ospec [dest]: "(ALL x:set A. P x) ==> A = Some x ==> P x"
    73   by simp
    74 
    75 setup {* map_theory_claset (fn ctxt => ctxt addSD2 ("ospec", @{thm ospec})) *}
    76 
    77 lemma elem_set [iff]: "(x : set xo) = (xo = Some x)"
    78   by (cases xo) auto
    79 
    80 lemma set_empty_eq [simp]: "(set xo = {}) = (xo = None)"
    81   by (cases xo) auto
    82 
    83 definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a option \<Rightarrow> 'b option" where
    84   "map = (%f y. case y of None => None | Some x => Some (f x))"
    85 
    86 lemma option_map_None [simp, code]: "map f None = None"
    87   by (simp add: map_def)
    88 
    89 lemma option_map_Some [simp, code]: "map f (Some x) = Some (f x)"
    90   by (simp add: map_def)
    91 
    92 lemma option_map_is_None [iff]:
    93     "(map f opt = None) = (opt = None)"
    94   by (simp add: map_def split add: option.split)
    95 
    96 lemma option_map_eq_Some [iff]:
    97     "(map f xo = Some y) = (EX z. xo = Some z & f z = y)"
    98   by (simp add: map_def split add: option.split)
    99 
   100 lemma option_map_comp:
   101     "map f (map g opt) = map (f o g) opt"
   102   by (simp add: map_def split add: option.split)
   103 
   104 lemma option_map_o_case_sum [simp]:
   105     "map f o case_sum g h = case_sum (map f o g) (map f o h)"
   106   by (rule ext) (simp split: sum.split)
   107 
   108 lemma map_cong: "x = y \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> f a = g a) \<Longrightarrow> map f x = map g y"
   109 by (cases x) auto
   110 
   111 enriched_type map: Option.map proof -
   112   fix f g
   113   show "Option.map f \<circ> Option.map g = Option.map (f \<circ> g)"
   114   proof
   115     fix x
   116     show "(Option.map f \<circ> Option.map g) x= Option.map (f \<circ> g) x"
   117       by (cases x) simp_all
   118   qed
   119 next
   120   show "Option.map id = id"
   121   proof
   122     fix x
   123     show "Option.map id x = id x"
   124       by (cases x) simp_all
   125   qed
   126 qed
   127 
   128 lemma case_option_map [simp]:
   129   "case_option g h (Option.map f x) = case_option g (h \<circ> f) x"
   130   by (cases x) simp_all
   131 
   132 primrec bind :: "'a option \<Rightarrow> ('a \<Rightarrow> 'b option) \<Rightarrow> 'b option" where
   133 bind_lzero: "bind None f = None" |
   134 bind_lunit: "bind (Some x) f = f x"
   135 
   136 lemma bind_runit[simp]: "bind x Some = x"
   137 by (cases x) auto
   138 
   139 lemma bind_assoc[simp]: "bind (bind x f) g = bind x (\<lambda>y. bind (f y) g)"
   140 by (cases x) auto
   141 
   142 lemma bind_rzero[simp]: "bind x (\<lambda>x. None) = None"
   143 by (cases x) auto
   144 
   145 lemma bind_cong: "x = y \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> f a = g a) \<Longrightarrow> bind x f = bind y g"
   146 by (cases x) auto
   147 
   148 definition these :: "'a option set \<Rightarrow> 'a set"
   149 where
   150   "these A = the ` {x \<in> A. x \<noteq> None}"
   151 
   152 lemma these_empty [simp]:
   153   "these {} = {}"
   154   by (simp add: these_def)
   155 
   156 lemma these_insert_None [simp]:
   157   "these (insert None A) = these A"
   158   by (auto simp add: these_def)
   159 
   160 lemma these_insert_Some [simp]:
   161   "these (insert (Some x) A) = insert x (these A)"
   162 proof -
   163   have "{y \<in> insert (Some x) A. y \<noteq> None} = insert (Some x) {y \<in> A. y \<noteq> None}"
   164     by auto
   165   then show ?thesis by (simp add: these_def)
   166 qed
   167 
   168 lemma in_these_eq:
   169   "x \<in> these A \<longleftrightarrow> Some x \<in> A"
   170 proof
   171   assume "Some x \<in> A"
   172   then obtain B where "A = insert (Some x) B" by auto
   173   then show "x \<in> these A" by (auto simp add: these_def intro!: image_eqI)
   174 next
   175   assume "x \<in> these A"
   176   then show "Some x \<in> A" by (auto simp add: these_def)
   177 qed
   178 
   179 lemma these_image_Some_eq [simp]:
   180   "these (Some ` A) = A"
   181   by (auto simp add: these_def intro!: image_eqI)
   182 
   183 lemma Some_image_these_eq:
   184   "Some ` these A = {x\<in>A. x \<noteq> None}"
   185   by (auto simp add: these_def image_image intro!: image_eqI)
   186 
   187 lemma these_empty_eq:
   188   "these B = {} \<longleftrightarrow> B = {} \<or> B = {None}"
   189   by (auto simp add: these_def)
   190 
   191 lemma these_not_empty_eq:
   192   "these B \<noteq> {} \<longleftrightarrow> B \<noteq> {} \<and> B \<noteq> {None}"
   193   by (auto simp add: these_empty_eq)
   194 
   195 hide_const (open) set map bind these
   196 hide_fact (open) map_cong bind_cong
   197 
   198 
   199 subsubsection {* Interaction with finite sets *}
   200 
   201 lemma finite_option_UNIV [simp]:
   202   "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
   203   by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
   204 
   205 instance option :: (finite) finite
   206   by default (simp add: UNIV_option_conv)
   207 
   208 
   209 subsubsection {* Code generator setup *}
   210 
   211 definition is_none :: "'a option \<Rightarrow> bool" where
   212   [code_post]: "is_none x \<longleftrightarrow> x = None"
   213 
   214 lemma is_none_code [code]:
   215   shows "is_none None \<longleftrightarrow> True"
   216     and "is_none (Some x) \<longleftrightarrow> False"
   217   unfolding is_none_def by simp_all
   218 
   219 lemma [code_unfold]:
   220   "HOL.equal x None \<longleftrightarrow> is_none x"
   221   "HOL.equal None = is_none"
   222   by (auto simp add: equal is_none_def)
   223 
   224 hide_const (open) is_none
   225 
   226 code_printing
   227   type_constructor option \<rightharpoonup>
   228     (SML) "_ option"
   229     and (OCaml) "_ option"
   230     and (Haskell) "Maybe _"
   231     and (Scala) "!Option[(_)]"
   232 | constant None \<rightharpoonup>
   233     (SML) "NONE"
   234     and (OCaml) "None"
   235     and (Haskell) "Nothing"
   236     and (Scala) "!None"
   237 | constant Some \<rightharpoonup>
   238     (SML) "SOME"
   239     and (OCaml) "Some _"
   240     and (Haskell) "Just"
   241     and (Scala) "Some"
   242 | class_instance option :: equal \<rightharpoonup>
   243     (Haskell) -
   244 | constant "HOL.equal :: 'a option \<Rightarrow> 'a option \<Rightarrow> bool" \<rightharpoonup>
   245     (Haskell) infix 4 "=="
   246 
   247 code_reserved SML
   248   option NONE SOME
   249 
   250 code_reserved OCaml
   251   option None Some
   252 
   253 code_reserved Scala
   254   Option None Some
   255 
   256 end