src/HOL/Option.thy
author haftmann
Fri Mar 06 20:30:17 2009 +0100 (2009-03-06)
changeset 30327 4d1185c77f4a
parent 30246 8253519dfc90
child 31080 21ffc770ebc0
permissions -rw-r--r--
moved instance option :: finite to Option.thy
     1 (*  Title:      HOL/Option.thy
     2     Author:     Folklore
     3 *)
     4 
     5 header {* Datatype option *}
     6 
     7 theory Option
     8 imports Datatype Finite_Set
     9 begin
    10 
    11 datatype 'a option = None | Some 'a
    12 
    13 lemma not_None_eq [iff]: "(x ~= None) = (EX y. x = Some y)"
    14   by (induct x) auto
    15 
    16 lemma not_Some_eq [iff]: "(ALL y. x ~= Some y) = (x = None)"
    17   by (induct x) auto
    18 
    19 text{*Although it may appear that both of these equalities are helpful
    20 only when applied to assumptions, in practice it seems better to give
    21 them the uniform iff attribute. *}
    22 
    23 lemma option_caseE:
    24   assumes c: "(case x of None => P | Some y => Q y)"
    25   obtains
    26     (None) "x = None" and P
    27   | (Some) y where "x = Some y" and "Q y"
    28   using c by (cases x) simp_all
    29 
    30 lemma insert_None_conv_UNIV: "insert None (range Some) = UNIV"
    31   by (rule set_ext, case_tac x) auto
    32 
    33 instance option :: (finite) finite proof
    34 qed (simp add: insert_None_conv_UNIV [symmetric])
    35 
    36 lemma inj_Some [simp]: "inj_on Some A"
    37   by (rule inj_onI) simp
    38 
    39 
    40 subsubsection {* Operations *}
    41 
    42 primrec the :: "'a option => 'a" where
    43 "the (Some x) = x"
    44 
    45 primrec set :: "'a option => 'a set" where
    46 "set None = {}" |
    47 "set (Some x) = {x}"
    48 
    49 lemma ospec [dest]: "(ALL x:set A. P x) ==> A = Some x ==> P x"
    50   by simp
    51 
    52 declaration {* fn _ =>
    53   Classical.map_cs (fn cs => cs addSD2 ("ospec", thm "ospec"))
    54 *}
    55 
    56 lemma elem_set [iff]: "(x : set xo) = (xo = Some x)"
    57   by (cases xo) auto
    58 
    59 lemma set_empty_eq [simp]: "(set xo = {}) = (xo = None)"
    60   by (cases xo) auto
    61 
    62 definition
    63   map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a option \<Rightarrow> 'b option"
    64 where
    65   [code del]: "map = (%f y. case y of None => None | Some x => Some (f x))"
    66 
    67 lemma option_map_None [simp, code]: "map f None = None"
    68   by (simp add: map_def)
    69 
    70 lemma option_map_Some [simp, code]: "map f (Some x) = Some (f x)"
    71   by (simp add: map_def)
    72 
    73 lemma option_map_is_None [iff]:
    74     "(map f opt = None) = (opt = None)"
    75   by (simp add: map_def split add: option.split)
    76 
    77 lemma option_map_eq_Some [iff]:
    78     "(map f xo = Some y) = (EX z. xo = Some z & f z = y)"
    79   by (simp add: map_def split add: option.split)
    80 
    81 lemma option_map_comp:
    82     "map f (map g opt) = map (f o g) opt"
    83   by (simp add: map_def split add: option.split)
    84 
    85 lemma option_map_o_sum_case [simp]:
    86     "map f o sum_case g h = sum_case (map f o g) (map f o h)"
    87   by (rule ext) (simp split: sum.split)
    88 
    89 
    90 hide (open) const set map
    91 
    92 subsubsection {* Code generator setup *}
    93 
    94 definition
    95   is_none :: "'a option \<Rightarrow> bool" where
    96   is_none_none [code post, symmetric, code inline]: "is_none x \<longleftrightarrow> x = None"
    97 
    98 lemma is_none_code [code]:
    99   shows "is_none None \<longleftrightarrow> True"
   100     and "is_none (Some x) \<longleftrightarrow> False"
   101   unfolding is_none_none [symmetric] by simp_all
   102 
   103 hide (open) const is_none
   104 
   105 code_type option
   106   (SML "_ option")
   107   (OCaml "_ option")
   108   (Haskell "Maybe _")
   109 
   110 code_const None and Some
   111   (SML "NONE" and "SOME")
   112   (OCaml "None" and "Some _")
   113   (Haskell "Nothing" and "Just")
   114 
   115 code_instance option :: eq
   116   (Haskell -)
   117 
   118 code_const "eq_class.eq \<Colon> 'a\<Colon>eq option \<Rightarrow> 'a option \<Rightarrow> bool"
   119   (Haskell infixl 4 "==")
   120 
   121 code_reserved SML
   122   option NONE SOME
   123 
   124 code_reserved OCaml
   125   option None Some
   126 
   127 end