src/HOL/Option.thy
author blanchet
Mon, 26 May 2014 16:32:55 +0200
changeset 57091 1fa9c19ba2c9
parent 55867 79b915f26533
child 57123 b5324647e0f1
permissions -rw-r--r--
got rid of '=:' squiggly
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
55404
5cb95b79a51f transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents: 55129
diff changeset
     8
imports BNF_LFP Datatype Finite_Set
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
     9
begin
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    10
55406
186076d100d3 compatibility names
blanchet
parents: 55405
diff changeset
    11
datatype_new 'a option =
57091
1fa9c19ba2c9 got rid of '=:' squiggly
blanchet
parents: 55867
diff changeset
    12
    None
55406
186076d100d3 compatibility names
blanchet
parents: 55405
diff changeset
    13
  | Some (the: 'a)
55531
601ca8efa000 renamed 'datatype_new_compat' to 'datatype_compat'
blanchet
parents: 55518
diff changeset
    14
datatype_compat option
55404
5cb95b79a51f transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents: 55129
diff changeset
    15
55406
186076d100d3 compatibility names
blanchet
parents: 55405
diff changeset
    16
lemma [case_names None Some, cases type: option]:
186076d100d3 compatibility names
blanchet
parents: 55405
diff changeset
    17
  -- {* for backward compatibility -- names of variables differ *}
55417
01fbfb60c33e adapted to 'xxx_{case,rec}' renaming, to new theorem names, and to new variable names in theorems
blanchet
parents: 55414
diff changeset
    18
  "(y = None \<Longrightarrow> P) \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> P) \<Longrightarrow> P"
55406
186076d100d3 compatibility names
blanchet
parents: 55405
diff changeset
    19
by (rule option.exhaust)
186076d100d3 compatibility names
blanchet
parents: 55405
diff changeset
    20
186076d100d3 compatibility names
blanchet
parents: 55405
diff changeset
    21
lemma [case_names None Some, induct type: option]:
186076d100d3 compatibility names
blanchet
parents: 55405
diff changeset
    22
  -- {* for backward compatibility -- names of variables differ *}
186076d100d3 compatibility names
blanchet
parents: 55405
diff changeset
    23
  "P None \<Longrightarrow> (\<And>option. P (Some option)) \<Longrightarrow> P option"
186076d100d3 compatibility names
blanchet
parents: 55405
diff changeset
    24
by (rule option.induct)
186076d100d3 compatibility names
blanchet
parents: 55405
diff changeset
    25
55442
blanchet
parents: 55417
diff changeset
    26
text {* Compatibility: *}
blanchet
parents: 55417
diff changeset
    27
55404
5cb95b79a51f transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents: 55129
diff changeset
    28
setup {* Sign.mandatory_path "option" *}
5cb95b79a51f transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents: 55129
diff changeset
    29
5cb95b79a51f transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents: 55129
diff changeset
    30
lemmas inducts = option.induct
5cb95b79a51f transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents: 55129
diff changeset
    31
lemmas cases = option.case
5cb95b79a51f transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents: 55129
diff changeset
    32
5cb95b79a51f transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents: 55129
diff changeset
    33
setup {* Sign.parent_path *}
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    34
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    35
lemma not_None_eq [iff]: "(x ~= None) = (EX y. x = Some y)"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    36
  by (induct x) auto
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    37
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    38
lemma not_Some_eq [iff]: "(ALL y. x ~= Some y) = (x = None)"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    39
  by (induct x) auto
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    40
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    41
text{*Although it may appear that both of these equalities are helpful
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    42
only when applied to assumptions, in practice it seems better to give
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    43
them the uniform iff attribute. *}
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    44
31080
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 30327
diff changeset
    45
lemma inj_Some [simp]: "inj_on Some A"
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 30327
diff changeset
    46
by (rule inj_onI) simp
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 30327
diff changeset
    47
55404
5cb95b79a51f transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents: 55129
diff changeset
    48
lemma case_optionE:
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    49
  assumes c: "(case x of None => P | Some y => Q y)"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    50
  obtains
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    51
    (None) "x = None" and P
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    52
  | (Some) y where "x = Some y" and "Q y"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    53
  using c by (cases x) simp_all
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    54
53010
ec5e6f69bd65 move useful lemmas to Main
kuncar
parents: 52435
diff changeset
    55
lemma split_option_all: "(\<forall>x. P x) \<longleftrightarrow> P None \<and> (\<forall>x. P (Some x))"
ec5e6f69bd65 move useful lemmas to Main
kuncar
parents: 52435
diff changeset
    56
by (auto intro: option.induct)
ec5e6f69bd65 move useful lemmas to Main
kuncar
parents: 52435
diff changeset
    57
ec5e6f69bd65 move useful lemmas to Main
kuncar
parents: 52435
diff changeset
    58
lemma split_option_ex: "(\<exists>x. P x) \<longleftrightarrow> P None \<or> (\<exists>x. P (Some x))"
ec5e6f69bd65 move useful lemmas to Main
kuncar
parents: 52435
diff changeset
    59
using split_option_all[of "\<lambda>x. \<not>P x"] by blast
ec5e6f69bd65 move useful lemmas to Main
kuncar
parents: 52435
diff changeset
    60
31080
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 30327
diff changeset
    61
lemma UNIV_option_conv: "UNIV = insert None (range Some)"
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 30327
diff changeset
    62
by(auto intro: classical)
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 30327
diff changeset
    63
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    64
subsubsection {* Operations *}
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    65
55518
1ddb2edf5ceb folded 'Option.set' into BNF-generated 'set_option'
blanchet
parents: 55467
diff changeset
    66
lemma ospec [dest]: "(ALL x:set_option A. P x) ==> A = Some x ==> P x"
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    67
  by simp
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    68
51703
f2e92fc0c8aa modifiers for classical wrappers operate on Proof.context instead of claset;
wenzelm
parents: 51096
diff changeset
    69
setup {* map_theory_claset (fn ctxt => ctxt addSD2 ("ospec", @{thm ospec})) *}
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    70
55518
1ddb2edf5ceb folded 'Option.set' into BNF-generated 'set_option'
blanchet
parents: 55467
diff changeset
    71
lemma elem_set [iff]: "(x : set_option xo) = (xo = Some x)"
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    72
  by (cases xo) auto
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    73
55518
1ddb2edf5ceb folded 'Option.set' into BNF-generated 'set_option'
blanchet
parents: 55467
diff changeset
    74
lemma set_empty_eq [simp]: "(set_option xo = {}) = (xo = None)"
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    75
  by (cases xo) auto
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    76
55466
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
    77
lemma map_option_case: "map_option f y = (case y of None => None | Some x => Some (f x))"
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
    78
  by (auto split: option.split)
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    79
55466
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
    80
lemma map_option_is_None [iff]:
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
    81
    "(map_option f opt = None) = (opt = None)"
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
    82
  by (simp add: map_option_case split add: option.split)
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    83
55466
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
    84
lemma map_option_eq_Some [iff]:
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
    85
    "(map_option f xo = Some y) = (EX z. xo = Some z & f z = y)"
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
    86
  by (simp add: map_option_case split add: option.split)
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    87
55466
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
    88
lemma map_option_o_case_sum [simp]:
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
    89
    "map_option f o case_sum g h = case_sum (map_option f o g) (map_option f o h)"
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
    90
  by (rule o_case_sum)
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
    91
55466
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
    92
lemma map_option_cong: "x = y \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> f a = g a) \<Longrightarrow> map_option f x = map_option g y"
46526
c4cf9d03c352 added congruence rules for Option.{map|bind}
krauss
parents: 41505
diff changeset
    93
by (cases x) auto
c4cf9d03c352 added congruence rules for Option.{map|bind}
krauss
parents: 41505
diff changeset
    94
55467
a5c9002bc54d renamed 'enriched_type' to more informative 'functor' (following the renaming of enriched type constructors to bounded natural functors)
blanchet
parents: 55466
diff changeset
    95
functor map_option: map_option proof -
41372
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 40968
diff changeset
    96
  fix f g
55466
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
    97
  show "map_option f \<circ> map_option g = map_option (f \<circ> g)"
41372
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 40968
diff changeset
    98
  proof
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 40968
diff changeset
    99
    fix x
55466
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
   100
    show "(map_option f \<circ> map_option g) x= map_option (f \<circ> g) x"
41372
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 40968
diff changeset
   101
      by (cases x) simp_all
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 40968
diff changeset
   102
  qed
40609
efb0d7878538 mapper for option type
haftmann
parents: 39272
diff changeset
   103
next
55466
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
   104
  show "map_option id = id"
41372
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 40968
diff changeset
   105
  proof
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 40968
diff changeset
   106
    fix x
55466
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
   107
    show "map_option id x = id x"
41372
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 40968
diff changeset
   108
      by (cases x) simp_all
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 40968
diff changeset
   109
  qed
40609
efb0d7878538 mapper for option type
haftmann
parents: 39272
diff changeset
   110
qed
efb0d7878538 mapper for option type
haftmann
parents: 39272
diff changeset
   111
55466
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
   112
lemma case_map_option [simp]:
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
   113
  "case_option g h (map_option f x) = case_option g (h \<circ> f) x"
51096
60e4b75fefe1 combinator List.those;
haftmann
parents: 49189
diff changeset
   114
  by (cases x) simp_all
60e4b75fefe1 combinator List.those;
haftmann
parents: 49189
diff changeset
   115
39149
aabd6d4a5c3a added Option.bind
krauss
parents: 38857
diff changeset
   116
primrec bind :: "'a option \<Rightarrow> ('a \<Rightarrow> 'b option) \<Rightarrow> 'b option" where
aabd6d4a5c3a added Option.bind
krauss
parents: 38857
diff changeset
   117
bind_lzero: "bind None f = None" |
aabd6d4a5c3a added Option.bind
krauss
parents: 38857
diff changeset
   118
bind_lunit: "bind (Some x) f = f x"
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   119
39149
aabd6d4a5c3a added Option.bind
krauss
parents: 38857
diff changeset
   120
lemma bind_runit[simp]: "bind x Some = x"
aabd6d4a5c3a added Option.bind
krauss
parents: 38857
diff changeset
   121
by (cases x) auto
aabd6d4a5c3a added Option.bind
krauss
parents: 38857
diff changeset
   122
aabd6d4a5c3a added Option.bind
krauss
parents: 38857
diff changeset
   123
lemma bind_assoc[simp]: "bind (bind x f) g = bind x (\<lambda>y. bind (f y) g)"
aabd6d4a5c3a added Option.bind
krauss
parents: 38857
diff changeset
   124
by (cases x) auto
aabd6d4a5c3a added Option.bind
krauss
parents: 38857
diff changeset
   125
aabd6d4a5c3a added Option.bind
krauss
parents: 38857
diff changeset
   126
lemma bind_rzero[simp]: "bind x (\<lambda>x. None) = None"
aabd6d4a5c3a added Option.bind
krauss
parents: 38857
diff changeset
   127
by (cases x) auto
aabd6d4a5c3a added Option.bind
krauss
parents: 38857
diff changeset
   128
46526
c4cf9d03c352 added congruence rules for Option.{map|bind}
krauss
parents: 41505
diff changeset
   129
lemma bind_cong: "x = y \<Longrightarrow> (\<And>a. y = Some a \<Longrightarrow> f a = g a) \<Longrightarrow> bind x f = bind y g"
c4cf9d03c352 added congruence rules for Option.{map|bind}
krauss
parents: 41505
diff changeset
   130
by (cases x) auto
c4cf9d03c352 added congruence rules for Option.{map|bind}
krauss
parents: 41505
diff changeset
   131
49189
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   132
definition these :: "'a option set \<Rightarrow> 'a set"
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   133
where
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   134
  "these A = the ` {x \<in> A. x \<noteq> None}"
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   135
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   136
lemma these_empty [simp]:
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   137
  "these {} = {}"
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   138
  by (simp add: these_def)
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   139
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   140
lemma these_insert_None [simp]:
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   141
  "these (insert None A) = these A"
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   142
  by (auto simp add: these_def)
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   143
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   144
lemma these_insert_Some [simp]:
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   145
  "these (insert (Some x) A) = insert x (these A)"
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   146
proof -
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   147
  have "{y \<in> insert (Some x) A. y \<noteq> None} = insert (Some x) {y \<in> A. y \<noteq> None}"
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   148
    by auto
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   149
  then show ?thesis by (simp add: these_def)
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   150
qed
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   151
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   152
lemma in_these_eq:
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   153
  "x \<in> these A \<longleftrightarrow> Some x \<in> A"
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   154
proof
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   155
  assume "Some x \<in> A"
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   156
  then obtain B where "A = insert (Some x) B" by auto
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   157
  then show "x \<in> these A" by (auto simp add: these_def intro!: image_eqI)
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   158
next
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   159
  assume "x \<in> these A"
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   160
  then show "Some x \<in> A" by (auto simp add: these_def)
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   161
qed
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   162
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   163
lemma these_image_Some_eq [simp]:
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   164
  "these (Some ` A) = A"
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   165
  by (auto simp add: these_def intro!: image_eqI)
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   166
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   167
lemma Some_image_these_eq:
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   168
  "Some ` these A = {x\<in>A. x \<noteq> None}"
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   169
  by (auto simp add: these_def image_image intro!: image_eqI)
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   170
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   171
lemma these_empty_eq:
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   172
  "these B = {} \<longleftrightarrow> B = {} \<or> B = {None}"
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   173
  by (auto simp add: these_def)
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   174
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   175
lemma these_not_empty_eq:
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   176
  "these B \<noteq> {} \<longleftrightarrow> B \<noteq> {} \<and> B \<noteq> {None}"
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   177
  by (auto simp add: these_empty_eq)
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   178
55518
1ddb2edf5ceb folded 'Option.set' into BNF-generated 'set_option'
blanchet
parents: 55467
diff changeset
   179
hide_const (open) bind these
55466
786edc984c98 merged 'Option.map' and 'Option.map_option'
blanchet
parents: 55442
diff changeset
   180
hide_fact (open) bind_cong
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   181
49189
3f85cd15a0cc combinator Option.these
haftmann
parents: 46526
diff changeset
   182
55089
181751ad852f swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents: 53940
diff changeset
   183
subsubsection {* Interaction with finite sets *}
181751ad852f swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents: 53940
diff changeset
   184
181751ad852f swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents: 53940
diff changeset
   185
lemma finite_option_UNIV [simp]:
181751ad852f swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents: 53940
diff changeset
   186
  "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
181751ad852f swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents: 53940
diff changeset
   187
  by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
181751ad852f swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents: 53940
diff changeset
   188
181751ad852f swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents: 53940
diff changeset
   189
instance option :: (finite) finite
181751ad852f swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents: 53940
diff changeset
   190
  by default (simp add: UNIV_option_conv)
181751ad852f swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents: 53940
diff changeset
   191
181751ad852f swapped dependencies of 'Finite_Set' and 'Option' (to move BNF up)
blanchet
parents: 53940
diff changeset
   192
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   193
subsubsection {* Code generator setup *}
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   194
31154
f919b8e67413 preprocessing must consider eq
haftmann
parents: 31080
diff changeset
   195
definition is_none :: "'a option \<Rightarrow> bool" where
31998
2c7a24f74db9 code attributes use common underscore convention
haftmann
parents: 31154
diff changeset
   196
  [code_post]: "is_none x \<longleftrightarrow> x = None"
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   197
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   198
lemma is_none_code [code]:
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   199
  shows "is_none None \<longleftrightarrow> True"
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   200
    and "is_none (Some x) \<longleftrightarrow> False"
31154
f919b8e67413 preprocessing must consider eq
haftmann
parents: 31080
diff changeset
   201
  unfolding is_none_def by simp_all
f919b8e67413 preprocessing must consider eq
haftmann
parents: 31080
diff changeset
   202
32069
6d28bbd33e2c prefer code_inline over code_unfold; use code_unfold_post where appropriate
haftmann
parents: 31998
diff changeset
   203
lemma [code_unfold]:
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37880
diff changeset
   204
  "HOL.equal x None \<longleftrightarrow> is_none x"
53940
36cf426cb1c6 Added symmetric code_unfold-lemmas for null and is_none
lammich <lammich@in.tum.de>
parents: 53010
diff changeset
   205
  "HOL.equal None = is_none"
36cf426cb1c6 Added symmetric code_unfold-lemmas for null and is_none
lammich <lammich@in.tum.de>
parents: 53010
diff changeset
   206
  by (auto simp add: equal is_none_def)
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   207
36176
3fe7e97ccca8 replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents: 35719
diff changeset
   208
hide_const (open) is_none
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   209
52435
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   210
code_printing
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   211
  type_constructor option \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   212
    (SML) "_ option"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   213
    and (OCaml) "_ option"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   214
    and (Haskell) "Maybe _"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   215
    and (Scala) "!Option[(_)]"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   216
| constant None \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   217
    (SML) "NONE"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   218
    and (OCaml) "None"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   219
    and (Haskell) "Nothing"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   220
    and (Scala) "!None"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   221
| constant Some \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   222
    (SML) "SOME"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   223
    and (OCaml) "Some _"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   224
    and (Haskell) "Just"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   225
    and (Scala) "Some"
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   226
| class_instance option :: equal \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   227
    (Haskell) -
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   228
| constant "HOL.equal :: 'a option \<Rightarrow> 'a option \<Rightarrow> bool" \<rightharpoonup>
6646bb548c6b migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents: 51703
diff changeset
   229
    (Haskell) infix 4 "=="
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   230
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   231
code_reserved SML
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   232
  option NONE SOME
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   233
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   234
code_reserved OCaml
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   235
  option None Some
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   236
34886
873c31d9f10d some syntax setup for Scala
haftmann
parents: 32069
diff changeset
   237
code_reserved Scala
873c31d9f10d some syntax setup for Scala
haftmann
parents: 32069
diff changeset
   238
  Option None Some
873c31d9f10d some syntax setup for Scala
haftmann
parents: 32069
diff changeset
   239
30246
8253519dfc90 Option.thy
nipkow
parents:
diff changeset
   240
end