src/HOL/Option.thy
author haftmann
Thu, 14 May 2009 15:09:47 +0200
changeset 31154 f919b8e67413
parent 31080 21ffc770ebc0
child 31998 2c7a24f74db9
permissions -rw-r--r--
preprocessing must consider eq
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
31080
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 30327
diff changeset
    23
lemma inj_Some [simp]: "inj_on Some A"
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 30327
diff changeset
    24
by (rule inj_onI) simp
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 30327
diff changeset
    25
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    26
lemma option_caseE:
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    27
  assumes c: "(case x of None => P | Some y => Q y)"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    28
  obtains
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    29
    (None) "x = None" and P
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    30
  | (Some) y where "x = Some y" and "Q y"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    31
  using c by (cases x) simp_all
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    32
31080
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 30327
diff changeset
    33
lemma UNIV_option_conv: "UNIV = insert None (range Some)"
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 30327
diff changeset
    34
by(auto intro: classical)
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 30327
diff changeset
    35
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 30327
diff changeset
    36
lemma finite_option_UNIV[simp]:
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 30327
diff changeset
    37
  "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 30327
diff changeset
    38
by(auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    39
30327
4d1185c77f4a moved instance option :: finite to Option.thy
haftmann
parents: 30246
diff changeset
    40
instance option :: (finite) finite proof
31080
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 30327
diff changeset
    41
qed (simp add: UNIV_option_conv)
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    42
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    43
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    44
subsubsection {* Operations *}
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    45
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    46
primrec the :: "'a option => 'a" where
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    47
"the (Some x) = x"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    48
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    49
primrec set :: "'a option => 'a set" where
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    50
"set None = {}" |
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    51
"set (Some x) = {x}"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    52
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    53
lemma ospec [dest]: "(ALL x:set A. P x) ==> A = Some x ==> P x"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    54
  by simp
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    55
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    56
declaration {* fn _ =>
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    57
  Classical.map_cs (fn cs => cs addSD2 ("ospec", thm "ospec"))
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    58
*}
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    59
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    60
lemma elem_set [iff]: "(x : set xo) = (xo = Some x)"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    61
  by (cases xo) auto
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    62
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    63
lemma set_empty_eq [simp]: "(set xo = {}) = (xo = None)"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    64
  by (cases xo) auto
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    65
31154
f919b8e67413 preprocessing must consider eq
haftmann
parents: 31080
diff changeset
    66
definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a option \<Rightarrow> 'b option" where
f919b8e67413 preprocessing must consider eq
haftmann
parents: 31080
diff changeset
    67
  "map = (%f y. case y of None => None | Some x => Some (f x))"
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    68
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    69
lemma option_map_None [simp, code]: "map f None = None"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    70
  by (simp add: map_def)
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    71
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    72
lemma option_map_Some [simp, code]: "map f (Some x) = Some (f x)"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    73
  by (simp add: map_def)
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    74
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    75
lemma option_map_is_None [iff]:
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    76
    "(map f opt = None) = (opt = None)"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    77
  by (simp add: map_def split add: option.split)
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    78
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    79
lemma option_map_eq_Some [iff]:
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    80
    "(map f xo = Some y) = (EX z. xo = Some z & f z = y)"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    81
  by (simp add: map_def split add: option.split)
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    82
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    83
lemma option_map_comp:
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    84
    "map f (map g opt) = map (f o g) opt"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    85
  by (simp add: map_def split add: option.split)
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    86
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    87
lemma option_map_o_sum_case [simp]:
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    88
    "map f o sum_case g h = sum_case (map f o g) (map f o h)"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    89
  by (rule ext) (simp split: sum.split)
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    90
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    91
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    92
hide (open) const set map
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    93
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    94
subsubsection {* Code generator setup *}
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    95
31154
f919b8e67413 preprocessing must consider eq
haftmann
parents: 31080
diff changeset
    96
definition is_none :: "'a option \<Rightarrow> bool" where
f919b8e67413 preprocessing must consider eq
haftmann
parents: 31080
diff changeset
    97
  [code post]: "is_none x \<longleftrightarrow> x = None"
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    98
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    99
lemma is_none_code [code]:
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   100
  shows "is_none None \<longleftrightarrow> True"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   101
    and "is_none (Some x) \<longleftrightarrow> False"
31154
f919b8e67413 preprocessing must consider eq
haftmann
parents: 31080
diff changeset
   102
  unfolding is_none_def by simp_all
f919b8e67413 preprocessing must consider eq
haftmann
parents: 31080
diff changeset
   103
f919b8e67413 preprocessing must consider eq
haftmann
parents: 31080
diff changeset
   104
lemma is_none_none:
f919b8e67413 preprocessing must consider eq
haftmann
parents: 31080
diff changeset
   105
  "is_none x \<longleftrightarrow> x = None"
f919b8e67413 preprocessing must consider eq
haftmann
parents: 31080
diff changeset
   106
  by (simp add: is_none_def)
f919b8e67413 preprocessing must consider eq
haftmann
parents: 31080
diff changeset
   107
f919b8e67413 preprocessing must consider eq
haftmann
parents: 31080
diff changeset
   108
lemma [code inline]:
f919b8e67413 preprocessing must consider eq
haftmann
parents: 31080
diff changeset
   109
  "eq_class.eq x None \<longleftrightarrow> is_none x"
f919b8e67413 preprocessing must consider eq
haftmann
parents: 31080
diff changeset
   110
  by (simp add: eq is_none_none)
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   111
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   112
hide (open) const is_none
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   113
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   114
code_type option
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   115
  (SML "_ option")
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   116
  (OCaml "_ option")
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   117
  (Haskell "Maybe _")
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   118
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   119
code_const None and Some
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   120
  (SML "NONE" and "SOME")
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   121
  (OCaml "None" and "Some _")
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   122
  (Haskell "Nothing" and "Just")
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   123
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   124
code_instance option :: eq
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   125
  (Haskell -)
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   126
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   127
code_const "eq_class.eq \<Colon> 'a\<Colon>eq option \<Rightarrow> 'a option \<Rightarrow> bool"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   128
  (Haskell infixl 4 "==")
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   129
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   130
code_reserved SML
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   131
  option NONE SOME
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   132
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   133
code_reserved OCaml
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   134
  option None Some
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   135
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   136
end