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