src/HOL/Option.thy
 author haftmann Mon Nov 29 13:44:54 2010 +0100 (2010-11-29) changeset 40815 6e2d17cc0d1d parent 40609 efb0d7878538 child 40968 a6fcd305f7dc permissions -rw-r--r--
equivI has replaced equiv.intro
```     1 (*  Title:      HOL/Option.thy
```
```     2     Author:     Folklore
```
```     3 *)
```
```     4
```
```     5 header {* Datatype option *}
```
```     6
```
```     7 theory Option
```
```     8 imports Datatype
```
```     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 inj_Some [simp]: "inj_on Some A"
```
```    24 by (rule inj_onI) simp
```
```    25
```
```    26 lemma option_caseE:
```
```    27   assumes c: "(case x of None => P | Some y => Q y)"
```
```    28   obtains
```
```    29     (None) "x = None" and P
```
```    30   | (Some) y where "x = Some y" and "Q y"
```
```    31   using c by (cases x) simp_all
```
```    32
```
```    33 lemma UNIV_option_conv: "UNIV = insert None (range Some)"
```
```    34 by(auto intro: classical)
```
```    35
```
```    36
```
```    37 subsubsection {* Operations *}
```
```    38
```
```    39 primrec the :: "'a option => 'a" where
```
```    40 "the (Some x) = x"
```
```    41
```
```    42 primrec set :: "'a option => 'a set" where
```
```    43 "set None = {}" |
```
```    44 "set (Some x) = {x}"
```
```    45
```
```    46 lemma ospec [dest]: "(ALL x:set A. P x) ==> A = Some x ==> P x"
```
```    47   by simp
```
```    48
```
```    49 declaration {* fn _ =>
```
```    50   Classical.map_cs (fn cs => cs addSD2 ("ospec", @{thm ospec}))
```
```    51 *}
```
```    52
```
```    53 lemma elem_set [iff]: "(x : set xo) = (xo = Some x)"
```
```    54   by (cases xo) auto
```
```    55
```
```    56 lemma set_empty_eq [simp]: "(set xo = {}) = (xo = None)"
```
```    57   by (cases xo) auto
```
```    58
```
```    59 definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a option \<Rightarrow> 'b option" where
```
```    60   "map = (%f y. case y of None => None | Some x => Some (f x))"
```
```    61
```
```    62 lemma option_map_None [simp, code]: "map f None = None"
```
```    63   by (simp add: map_def)
```
```    64
```
```    65 lemma option_map_Some [simp, code]: "map f (Some x) = Some (f x)"
```
```    66   by (simp add: map_def)
```
```    67
```
```    68 lemma option_map_is_None [iff]:
```
```    69     "(map f opt = None) = (opt = None)"
```
```    70   by (simp add: map_def split add: option.split)
```
```    71
```
```    72 lemma option_map_eq_Some [iff]:
```
```    73     "(map f xo = Some y) = (EX z. xo = Some z & f z = y)"
```
```    74   by (simp add: map_def split add: option.split)
```
```    75
```
```    76 lemma option_map_comp:
```
```    77     "map f (map g opt) = map (f o g) opt"
```
```    78   by (simp add: map_def split add: option.split)
```
```    79
```
```    80 lemma option_map_o_sum_case [simp]:
```
```    81     "map f o sum_case g h = sum_case (map f o g) (map f o h)"
```
```    82   by (rule ext) (simp split: sum.split)
```
```    83
```
```    84 type_mapper Option.map proof -
```
```    85   fix f g x
```
```    86   show "Option.map f (Option.map g x) = Option.map (\<lambda>x. f (g x)) x"
```
```    87     by (cases x) simp_all
```
```    88 next
```
```    89   fix x
```
```    90   show "Option.map (\<lambda>x. x) x = x"
```
```    91     by (cases x) simp_all
```
```    92 qed
```
```    93
```
```    94 primrec bind :: "'a option \<Rightarrow> ('a \<Rightarrow> 'b option) \<Rightarrow> 'b option" where
```
```    95 bind_lzero: "bind None f = None" |
```
```    96 bind_lunit: "bind (Some x) f = f x"
```
```    97
```
```    98 lemma bind_runit[simp]: "bind x Some = x"
```
```    99 by (cases x) auto
```
```   100
```
```   101 lemma bind_assoc[simp]: "bind (bind x f) g = bind x (\<lambda>y. bind (f y) g)"
```
```   102 by (cases x) auto
```
```   103
```
```   104 lemma bind_rzero[simp]: "bind x (\<lambda>x. None) = None"
```
```   105 by (cases x) auto
```
```   106
```
```   107 hide_const (open) set map bind
```
```   108
```
```   109 subsubsection {* Code generator setup *}
```
```   110
```
```   111 definition is_none :: "'a option \<Rightarrow> bool" where
```
```   112   [code_post]: "is_none x \<longleftrightarrow> x = None"
```
```   113
```
```   114 lemma is_none_code [code]:
```
```   115   shows "is_none None \<longleftrightarrow> True"
```
```   116     and "is_none (Some x) \<longleftrightarrow> False"
```
```   117   unfolding is_none_def by simp_all
```
```   118
```
```   119 lemma [code_unfold]:
```
```   120   "HOL.equal x None \<longleftrightarrow> is_none x"
```
```   121   by (simp add: equal is_none_def)
```
```   122
```
```   123 hide_const (open) is_none
```
```   124
```
```   125 code_type option
```
```   126   (SML "_ option")
```
```   127   (OCaml "_ option")
```
```   128   (Haskell "Maybe _")
```
```   129   (Scala "!Option[(_)]")
```
```   130
```
```   131 code_const None and Some
```
```   132   (SML "NONE" and "SOME")
```
```   133   (OCaml "None" and "Some _")
```
```   134   (Haskell "Nothing" and "Just")
```
```   135   (Scala "!None" and "Some")
```
```   136
```
```   137 code_instance option :: equal
```
```   138   (Haskell -)
```
```   139
```
```   140 code_const "HOL.equal \<Colon> 'a option \<Rightarrow> 'a option \<Rightarrow> bool"
```
```   141   (Haskell infix 4 "==")
```
```   142
```
```   143 code_reserved SML
```
```   144   option NONE SOME
```
```   145
```
```   146 code_reserved OCaml
```
```   147   option None Some
```
```   148
```
```   149 code_reserved Scala
```
```   150   Option None Some
```
```   151
```
```   152 end
```