src/HOL/Library/Option_ord.thy
author wenzelm
Tue May 15 13:57:39 2018 +0200 (16 months ago)
changeset 68189 6163c90694ef
parent 67951 655aa11359dc
child 68980 5717fbc55521
permissions -rw-r--r--
tuned headers;
haftmann@26241
     1
(*  Title:      HOL/Library/Option_ord.thy
haftmann@26241
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@26241
     3
*)
haftmann@26241
     4
wenzelm@60500
     5
section \<open>Canonical order on option type\<close>
haftmann@26241
     6
haftmann@26241
     7
theory Option_ord
wenzelm@67006
     8
imports Main
haftmann@26241
     9
begin
haftmann@26241
    10
haftmann@49190
    11
notation
haftmann@49190
    12
  bot ("\<bottom>") and
haftmann@49190
    13
  top ("\<top>") and
haftmann@49190
    14
  inf  (infixl "\<sqinter>" 70) and
haftmann@49190
    15
  sup  (infixl "\<squnion>" 65) and
haftmann@49190
    16
  Inf  ("\<Sqinter>_" [900] 900) and
haftmann@49190
    17
  Sup  ("\<Squnion>_" [900] 900)
haftmann@49190
    18
wenzelm@61955
    19
syntax
haftmann@49190
    20
  "_INF1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Sqinter>_./ _)" [0, 10] 10)
haftmann@49190
    21
  "_INF"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sqinter>_\<in>_./ _)" [0, 0, 10] 10)
haftmann@49190
    22
  "_SUP1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Squnion>_./ _)" [0, 10] 10)
haftmann@49190
    23
  "_SUP"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Squnion>_\<in>_./ _)" [0, 0, 10] 10)
haftmann@49190
    24
haftmann@49190
    25
haftmann@30662
    26
instantiation option :: (preorder) preorder
haftmann@26241
    27
begin
haftmann@26241
    28
haftmann@26241
    29
definition less_eq_option where
haftmann@37765
    30
  "x \<le> y \<longleftrightarrow> (case x of None \<Rightarrow> True | Some x \<Rightarrow> (case y of None \<Rightarrow> False | Some y \<Rightarrow> x \<le> y))"
haftmann@26241
    31
haftmann@26241
    32
definition less_option where
haftmann@37765
    33
  "x < y \<longleftrightarrow> (case y of None \<Rightarrow> False | Some y \<Rightarrow> (case x of None \<Rightarrow> True | Some x \<Rightarrow> x < y))"
haftmann@26241
    34
haftmann@26258
    35
lemma less_eq_option_None [simp]: "None \<le> x"
haftmann@26241
    36
  by (simp add: less_eq_option_def)
haftmann@26241
    37
haftmann@26258
    38
lemma less_eq_option_None_code [code]: "None \<le> x \<longleftrightarrow> True"
haftmann@26241
    39
  by simp
haftmann@26241
    40
haftmann@26258
    41
lemma less_eq_option_None_is_None: "x \<le> None \<Longrightarrow> x = None"
haftmann@26241
    42
  by (cases x) (simp_all add: less_eq_option_def)
haftmann@26241
    43
haftmann@26258
    44
lemma less_eq_option_Some_None [simp, code]: "Some x \<le> None \<longleftrightarrow> False"
haftmann@26241
    45
  by (simp add: less_eq_option_def)
haftmann@26241
    46
haftmann@26258
    47
lemma less_eq_option_Some [simp, code]: "Some x \<le> Some y \<longleftrightarrow> x \<le> y"
haftmann@26241
    48
  by (simp add: less_eq_option_def)
haftmann@26241
    49
haftmann@26258
    50
lemma less_option_None [simp, code]: "x < None \<longleftrightarrow> False"
haftmann@26241
    51
  by (simp add: less_option_def)
haftmann@26241
    52
haftmann@26258
    53
lemma less_option_None_is_Some: "None < x \<Longrightarrow> \<exists>z. x = Some z"
haftmann@26241
    54
  by (cases x) (simp_all add: less_option_def)
haftmann@26241
    55
haftmann@26258
    56
lemma less_option_None_Some [simp]: "None < Some x"
haftmann@26241
    57
  by (simp add: less_option_def)
haftmann@26241
    58
haftmann@26258
    59
lemma less_option_None_Some_code [code]: "None < Some x \<longleftrightarrow> True"
haftmann@26241
    60
  by simp
haftmann@26241
    61
haftmann@26258
    62
lemma less_option_Some [simp, code]: "Some x < Some y \<longleftrightarrow> x < y"
haftmann@26241
    63
  by (simp add: less_option_def)
haftmann@26241
    64
wenzelm@60679
    65
instance
wenzelm@60679
    66
  by standard
wenzelm@60679
    67
    (auto simp add: less_eq_option_def less_option_def less_le_not_le
wenzelm@60679
    68
      elim: order_trans split: option.splits)
haftmann@26241
    69
wenzelm@60679
    70
end
haftmann@30662
    71
wenzelm@60679
    72
instance option :: (order) order
wenzelm@60679
    73
  by standard (auto simp add: less_eq_option_def less_option_def split: option.splits)
wenzelm@60679
    74
wenzelm@60679
    75
instance option :: (linorder) linorder
wenzelm@60679
    76
  by standard (auto simp add: less_eq_option_def less_option_def split: option.splits)
haftmann@30662
    77
haftmann@52729
    78
instantiation option :: (order) order_bot
haftmann@30662
    79
begin
haftmann@30662
    80
wenzelm@60679
    81
definition bot_option where "\<bottom> = None"
haftmann@30662
    82
wenzelm@60679
    83
instance
wenzelm@60679
    84
  by standard (simp add: bot_option_def)
haftmann@30662
    85
haftmann@30662
    86
end
haftmann@30662
    87
haftmann@52729
    88
instantiation option :: (order_top) order_top
haftmann@30662
    89
begin
haftmann@30662
    90
wenzelm@60679
    91
definition top_option where "\<top> = Some \<top>"
haftmann@30662
    92
wenzelm@60679
    93
instance
wenzelm@60679
    94
  by standard (simp add: top_option_def less_eq_option_def split: option.split)
haftmann@26241
    95
haftmann@26241
    96
end
haftmann@30662
    97
wenzelm@60679
    98
instance option :: (wellorder) wellorder
wenzelm@60679
    99
proof
wenzelm@60679
   100
  fix P :: "'a option \<Rightarrow> bool"
wenzelm@60679
   101
  fix z :: "'a option"
haftmann@30662
   102
  assume H: "\<And>x. (\<And>y. y < x \<Longrightarrow> P y) \<Longrightarrow> P x"
haftmann@30662
   103
  have "P None" by (rule H) simp
wenzelm@67091
   104
  then have P_Some [case_names Some]: "P z" if "\<And>x. z = Some x \<Longrightarrow> (P \<circ> Some) x" for z
wenzelm@60679
   105
    using \<open>P None\<close> that by (cases z) simp_all
wenzelm@60679
   106
  show "P z"
wenzelm@60679
   107
  proof (cases z rule: P_Some)
haftmann@30662
   108
    case (Some w)
wenzelm@67091
   109
    show "(P \<circ> Some) w"
wenzelm@60679
   110
    proof (induct rule: less_induct)
haftmann@30662
   111
      case (less x)
wenzelm@60679
   112
      have "P (Some x)"
wenzelm@60679
   113
      proof (rule H)
haftmann@30662
   114
        fix y :: "'a option"
haftmann@30662
   115
        assume "y < Some x"
wenzelm@60679
   116
        show "P y"
wenzelm@60679
   117
        proof (cases y rule: P_Some)
wenzelm@60679
   118
          case (Some v)
wenzelm@60679
   119
          with \<open>y < Some x\<close> have "v < x" by simp
wenzelm@67091
   120
          with less show "(P \<circ> Some) v" .
haftmann@30662
   121
        qed
haftmann@30662
   122
      qed
haftmann@30662
   123
      then show ?case by simp
haftmann@30662
   124
    qed
haftmann@30662
   125
  qed
haftmann@30662
   126
qed
haftmann@30662
   127
haftmann@49190
   128
instantiation option :: (inf) inf
haftmann@49190
   129
begin
haftmann@49190
   130
haftmann@49190
   131
definition inf_option where
haftmann@49190
   132
  "x \<sqinter> y = (case x of None \<Rightarrow> None | Some x \<Rightarrow> (case y of None \<Rightarrow> None | Some y \<Rightarrow> Some (x \<sqinter> y)))"
haftmann@49190
   133
wenzelm@60679
   134
lemma inf_None_1 [simp, code]: "None \<sqinter> y = None"
haftmann@49190
   135
  by (simp add: inf_option_def)
haftmann@49190
   136
wenzelm@60679
   137
lemma inf_None_2 [simp, code]: "x \<sqinter> None = None"
haftmann@49190
   138
  by (cases x) (simp_all add: inf_option_def)
haftmann@49190
   139
wenzelm@60679
   140
lemma inf_Some [simp, code]: "Some x \<sqinter> Some y = Some (x \<sqinter> y)"
haftmann@49190
   141
  by (simp add: inf_option_def)
haftmann@49190
   142
haftmann@49190
   143
instance ..
haftmann@49190
   144
haftmann@30662
   145
end
haftmann@49190
   146
haftmann@49190
   147
instantiation option :: (sup) sup
haftmann@49190
   148
begin
haftmann@49190
   149
haftmann@49190
   150
definition sup_option where
haftmann@49190
   151
  "x \<squnion> y = (case x of None \<Rightarrow> y | Some x' \<Rightarrow> (case y of None \<Rightarrow> x | Some y \<Rightarrow> Some (x' \<squnion> y)))"
haftmann@49190
   152
wenzelm@60679
   153
lemma sup_None_1 [simp, code]: "None \<squnion> y = y"
haftmann@49190
   154
  by (simp add: sup_option_def)
haftmann@49190
   155
wenzelm@60679
   156
lemma sup_None_2 [simp, code]: "x \<squnion> None = x"
haftmann@49190
   157
  by (cases x) (simp_all add: sup_option_def)
haftmann@49190
   158
wenzelm@60679
   159
lemma sup_Some [simp, code]: "Some x \<squnion> Some y = Some (x \<squnion> y)"
haftmann@49190
   160
  by (simp add: sup_option_def)
haftmann@49190
   161
haftmann@49190
   162
instance ..
haftmann@49190
   163
haftmann@49190
   164
end
haftmann@49190
   165
wenzelm@60679
   166
instance option :: (semilattice_inf) semilattice_inf
wenzelm@60679
   167
proof
haftmann@49190
   168
  fix x y z :: "'a option"
haftmann@49190
   169
  show "x \<sqinter> y \<le> x"
wenzelm@60679
   170
    by (cases x, simp_all, cases y, simp_all)
haftmann@49190
   171
  show "x \<sqinter> y \<le> y"
wenzelm@60679
   172
    by (cases x, simp_all, cases y, simp_all)
haftmann@49190
   173
  show "x \<le> y \<Longrightarrow> x \<le> z \<Longrightarrow> x \<le> y \<sqinter> z"
wenzelm@60679
   174
    by (cases x, simp_all, cases y, simp_all, cases z, simp_all)
haftmann@49190
   175
qed
haftmann@49190
   176
wenzelm@60679
   177
instance option :: (semilattice_sup) semilattice_sup
wenzelm@60679
   178
proof
haftmann@49190
   179
  fix x y z :: "'a option"
haftmann@49190
   180
  show "x \<le> x \<squnion> y"
wenzelm@60679
   181
    by (cases x, simp_all, cases y, simp_all)
haftmann@49190
   182
  show "y \<le> x \<squnion> y"
wenzelm@60679
   183
    by (cases x, simp_all, cases y, simp_all)
haftmann@49190
   184
  fix x y z :: "'a option"
haftmann@49190
   185
  show "y \<le> x \<Longrightarrow> z \<le> x \<Longrightarrow> y \<squnion> z \<le> x"
wenzelm@60679
   186
    by (cases y, simp_all, cases z, simp_all, cases x, simp_all)
haftmann@49190
   187
qed
haftmann@49190
   188
haftmann@49190
   189
instance option :: (lattice) lattice ..
haftmann@49190
   190
haftmann@49190
   191
instance option :: (lattice) bounded_lattice_bot ..
haftmann@49190
   192
haftmann@49190
   193
instance option :: (bounded_lattice_top) bounded_lattice_top ..
haftmann@49190
   194
haftmann@49190
   195
instance option :: (bounded_lattice_top) bounded_lattice ..
haftmann@49190
   196
haftmann@49190
   197
instance option :: (distrib_lattice) distrib_lattice
haftmann@49190
   198
proof
haftmann@49190
   199
  fix x y z :: "'a option"
haftmann@49190
   200
  show "x \<squnion> y \<sqinter> z = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
wenzelm@60679
   201
    by (cases x, simp_all, cases y, simp_all, cases z, simp_all add: sup_inf_distrib1 inf_commute)
wenzelm@60679
   202
qed
haftmann@49190
   203
haftmann@49190
   204
instantiation option :: (complete_lattice) complete_lattice
haftmann@49190
   205
begin
haftmann@49190
   206
haftmann@49190
   207
definition Inf_option :: "'a option set \<Rightarrow> 'a option" where
haftmann@49190
   208
  "\<Sqinter>A = (if None \<in> A then None else Some (\<Sqinter>Option.these A))"
haftmann@49190
   209
wenzelm@60679
   210
lemma None_in_Inf [simp]: "None \<in> A \<Longrightarrow> \<Sqinter>A = None"
haftmann@49190
   211
  by (simp add: Inf_option_def)
haftmann@49190
   212
haftmann@49190
   213
definition Sup_option :: "'a option set \<Rightarrow> 'a option" where
haftmann@49190
   214
  "\<Squnion>A = (if A = {} \<or> A = {None} then None else Some (\<Squnion>Option.these A))"
haftmann@49190
   215
wenzelm@60679
   216
lemma empty_Sup [simp]: "\<Squnion>{} = None"
haftmann@49190
   217
  by (simp add: Sup_option_def)
haftmann@49190
   218
wenzelm@60679
   219
lemma singleton_None_Sup [simp]: "\<Squnion>{None} = None"
haftmann@49190
   220
  by (simp add: Sup_option_def)
haftmann@49190
   221
wenzelm@60679
   222
instance
wenzelm@60679
   223
proof
haftmann@49190
   224
  fix x :: "'a option" and A
haftmann@49190
   225
  assume "x \<in> A"
haftmann@49190
   226
  then show "\<Sqinter>A \<le> x"
haftmann@49190
   227
    by (cases x) (auto simp add: Inf_option_def in_these_eq intro: Inf_lower)
haftmann@49190
   228
next
haftmann@49190
   229
  fix z :: "'a option" and A
haftmann@49190
   230
  assume *: "\<And>x. x \<in> A \<Longrightarrow> z \<le> x"
haftmann@49190
   231
  show "z \<le> \<Sqinter>A"
haftmann@49190
   232
  proof (cases z)
haftmann@49190
   233
    case None then show ?thesis by simp
haftmann@49190
   234
  next
haftmann@49190
   235
    case (Some y)
haftmann@49190
   236
    show ?thesis
haftmann@49190
   237
      by (auto simp add: Inf_option_def in_these_eq Some intro!: Inf_greatest dest!: *)
haftmann@49190
   238
  qed
haftmann@49190
   239
next
haftmann@49190
   240
  fix x :: "'a option" and A
haftmann@49190
   241
  assume "x \<in> A"
haftmann@49190
   242
  then show "x \<le> \<Squnion>A"
haftmann@49190
   243
    by (cases x) (auto simp add: Sup_option_def in_these_eq intro: Sup_upper)
haftmann@49190
   244
next
haftmann@49190
   245
  fix z :: "'a option" and A
haftmann@49190
   246
  assume *: "\<And>x. x \<in> A \<Longrightarrow> x \<le> z"
haftmann@49190
   247
  show "\<Squnion>A \<le> z "
haftmann@49190
   248
  proof (cases z)
haftmann@49190
   249
    case None
haftmann@49190
   250
    with * have "\<And>x. x \<in> A \<Longrightarrow> x = None" by (auto dest: less_eq_option_None_is_None)
haftmann@49190
   251
    then have "A = {} \<or> A = {None}" by blast
haftmann@49190
   252
    then show ?thesis by (simp add: Sup_option_def)
haftmann@49190
   253
  next
haftmann@49190
   254
    case (Some y)
haftmann@49190
   255
    from * have "\<And>w. Some w \<in> A \<Longrightarrow> Some w \<le> z" .
haftmann@49190
   256
    with Some have "\<And>w. w \<in> Option.these A \<Longrightarrow> w \<le> y"
haftmann@49190
   257
      by (simp add: in_these_eq)
haftmann@49190
   258
    then have "\<Squnion>Option.these A \<le> y" by (rule Sup_least)
haftmann@49190
   259
    with Some show ?thesis by (simp add: Sup_option_def)
haftmann@49190
   260
  qed
haftmann@52729
   261
next
haftmann@52729
   262
  show "\<Squnion>{} = (\<bottom>::'a option)"
wenzelm@60679
   263
    by (auto simp: bot_option_def)
haftmann@52729
   264
  show "\<Sqinter>{} = (\<top>::'a option)"
wenzelm@60679
   265
    by (auto simp: top_option_def Inf_option_def)
haftmann@49190
   266
qed
haftmann@49190
   267
haftmann@49190
   268
end
haftmann@49190
   269
haftmann@49190
   270
lemma Some_Inf:
haftmann@49190
   271
  "Some (\<Sqinter>A) = \<Sqinter>(Some ` A)"
haftmann@49190
   272
  by (auto simp add: Inf_option_def)
haftmann@49190
   273
haftmann@49190
   274
lemma Some_Sup:
haftmann@49190
   275
  "A \<noteq> {} \<Longrightarrow> Some (\<Squnion>A) = \<Squnion>(Some ` A)"
haftmann@49190
   276
  by (auto simp add: Sup_option_def)
haftmann@49190
   277
haftmann@49190
   278
lemma Some_INF:
haftmann@49190
   279
  "Some (\<Sqinter>x\<in>A. f x) = (\<Sqinter>x\<in>A. Some (f x))"
haftmann@56166
   280
  using Some_Inf [of "f ` A"] by (simp add: comp_def)
haftmann@49190
   281
haftmann@49190
   282
lemma Some_SUP:
haftmann@49190
   283
  "A \<noteq> {} \<Longrightarrow> Some (\<Squnion>x\<in>A. f x) = (\<Squnion>x\<in>A. Some (f x))"
haftmann@56166
   284
  using Some_Sup [of "f ` A"] by (simp add: comp_def)
haftmann@49190
   285
eberlm@67829
   286
lemma option_Inf_Sup: "INFIMUM (A::('a::complete_distrib_lattice option) set set) Sup \<le> SUPREMUM {f ` A |f. \<forall>Y\<in>A. f Y \<in> Y} Inf"
eberlm@67829
   287
proof (cases "{} \<in> A")
eberlm@67829
   288
  case True
eberlm@67829
   289
  then show ?thesis
eberlm@67951
   290
    by (rule INF_lower2, simp_all)
eberlm@67829
   291
next
eberlm@67829
   292
  case False
eberlm@67829
   293
  from this have X: "{} \<notin> A"
eberlm@67829
   294
    by simp
eberlm@67829
   295
  then show ?thesis
eberlm@67829
   296
  proof (cases "{None} \<in> A")
eberlm@67829
   297
    case True
eberlm@67829
   298
    then show ?thesis
eberlm@67951
   299
      by (rule INF_lower2, simp_all)
haftmann@49190
   300
  next
eberlm@67829
   301
    case False
eberlm@67951
   302
eberlm@67951
   303
    {fix y
eberlm@67951
   304
      assume A: "y \<in> A"
eberlm@67951
   305
      have "Sup (y - {None}) = Sup y"
eberlm@67951
   306
        by (metis (no_types, lifting) Sup_option_def insert_Diff_single these_insert_None these_not_empty_eq)
eberlm@67951
   307
      from A and this have "(\<exists>z. y - {None} = z - {None} \<and> z \<in> A) \<and> \<Squnion>y = \<Squnion>(y - {None})"
eberlm@67951
   308
        by auto
eberlm@67951
   309
    }
eberlm@67829
   310
    from this have A: "Sup ` A = (Sup ` {y - {None} | y. y\<in>A})"
eberlm@67951
   311
      by (auto simp add: image_def)
eberlm@67829
   312
eberlm@67829
   313
    have [simp]: "\<And>y. y \<in> A \<Longrightarrow> \<exists>ya. {ya. \<exists>x. x \<in> y \<and> (\<exists>y. x = Some y) \<and> ya = the x} 
eberlm@67829
   314
          = {y. \<exists>x\<in>ya - {None}. y = the x} \<and> ya \<in> A"
eberlm@67951
   315
      by (rule exI, auto)
eberlm@67829
   316
eberlm@67829
   317
    have [simp]: "\<And>y. y \<in> A \<Longrightarrow>
eberlm@67829
   318
         (\<exists>ya. y - {None} = ya - {None} \<and> ya \<in> A) \<and> \<Squnion>{ya. \<exists>x\<in>y - {None}. ya = the x} 
eberlm@67829
   319
          = \<Squnion>{ya. \<exists>x. x \<in> y \<and> (\<exists>y. x = Some y) \<and> ya = the x}"
eberlm@67951
   320
      apply (safe, blast)
eberlm@67951
   321
      by (rule arg_cong [of _ _ Sup], auto)
eberlm@67951
   322
    {fix y
eberlm@67951
   323
      assume [simp]: "y \<in> A"
eberlm@67951
   324
      have "\<exists>x. (\<exists>y. x = {ya. \<exists>x\<in>y - {None}. ya = the x} \<and> y \<in> A) \<and> \<Squnion>{ya. \<exists>x. x \<in> y \<and> (\<exists>y. x = Some y) \<and> ya = the x} = \<Squnion>x"
eberlm@67951
   325
      and "\<exists>x. (\<exists>y. x = y - {None} \<and> y \<in> A) \<and> \<Squnion>{ya. \<exists>x\<in>y - {None}. ya = the x} = \<Squnion>{y. \<exists>xa. xa \<in> x \<and> (\<exists>y. xa = Some y) \<and> y = the xa}"
eberlm@67951
   326
         apply (rule exI [of _ "{ya. \<exists>x. x \<in> y \<and> (\<exists>y. x = Some y) \<and> ya = the x}"], simp)
eberlm@67951
   327
        by (rule exI [of _ "y - {None}"], simp)
eberlm@67951
   328
    }
eberlm@67951
   329
    from this have C: "(\<lambda>x.  (\<Squnion>Option.these x)) ` {y - {None} |y. y \<in> A} =  (Sup ` {the ` (y - {None}) |y. y \<in> A})"
eberlm@67951
   330
      by (simp add: image_def Option.these_def, safe, simp_all)
eberlm@67829
   331
  
eberlm@67829
   332
    have D: "\<forall> f . \<exists>Y\<in>A. f Y \<notin> Y \<Longrightarrow> False"
eberlm@67951
   333
      by (drule spec [of _ "\<lambda> Y . SOME x . x \<in> Y"], simp add: X some_in_eq)
eberlm@67829
   334
  
eberlm@67829
   335
    define F where "F = (\<lambda> Y . SOME x::'a option . x \<in> (Y - {None}))"
eberlm@67829
   336
  
eberlm@67829
   337
    have G: "\<And> Y . Y \<in> A \<Longrightarrow> \<exists> x . x \<in> Y - {None}"
eberlm@67829
   338
      by (metis False X all_not_in_conv insert_Diff_single these_insert_None these_not_empty_eq)
eberlm@67829
   339
  
eberlm@67829
   340
    have F: "\<And> Y . Y \<in> A \<Longrightarrow> F Y \<in> (Y - {None})"
eberlm@67829
   341
      by (metis F_def G empty_iff some_in_eq)
eberlm@67829
   342
  
eberlm@67829
   343
    have "Some \<bottom> \<le> Inf (F ` A)"
eberlm@67829
   344
      by (metis (no_types, lifting) Diff_iff F Inf_option_def bot.extremum image_iff 
eberlm@67829
   345
          less_eq_option_Some singletonI)
eberlm@67829
   346
      
eberlm@67829
   347
    from this have "Inf (F ` A) \<noteq> None"
eberlm@67951
   348
      by (cases "\<Sqinter>x\<in>A. F x", simp_all)
eberlm@67951
   349
eberlm@67951
   350
    from this have "Inf (F ` A) \<noteq> None \<and> Inf (F ` A) \<in> Inf ` {f ` A |f. \<forall>Y\<in>A. f Y \<in> Y}"
eberlm@67951
   351
      using F by auto
eberlm@67951
   352
eberlm@67829
   353
    from this have "\<exists> x . x \<noteq> None \<and> x \<in> Inf ` {f ` A |f. \<forall>Y\<in>A. f Y \<in> Y}"
eberlm@67951
   354
      by blast
eberlm@67829
   355
  
eberlm@67829
   356
    from this have E:" Inf ` {f ` A |f. \<forall>Y\<in>A. f Y \<in> Y} = {None} \<Longrightarrow> False"
eberlm@67829
   357
      by blast
eberlm@67829
   358
eberlm@67829
   359
    have [simp]: "((\<Squnion>x\<in>{f ` A |f. \<forall>Y\<in>A. f Y \<in> Y}. \<Sqinter>x) = None) = False"
eberlm@67829
   360
      by (metis (no_types, lifting) E Sup_option_def \<open>\<exists>x. x \<noteq> None \<and> x \<in> Inf ` {f ` A |f. \<forall>Y\<in>A. f Y \<in> Y}\<close> 
eberlm@67829
   361
          ex_in_conv option.simps(3))
eberlm@67829
   362
  
eberlm@67829
   363
    have B: "Option.these ((\<lambda>x. Some (\<Squnion>Option.these x)) ` {y - {None} |y. y \<in> A}) 
eberlm@67829
   364
        = ((\<lambda>x. (\<Squnion> Option.these x)) ` {y - {None} |y. y \<in> A})"
eberlm@67829
   365
      by (metis image_image these_image_Some_eq)
eberlm@67951
   366
    {
eberlm@67951
   367
      fix f
eberlm@67951
   368
      assume A: "\<And> Y . (\<exists>y. Y = the ` (y - {None}) \<and> y \<in> A) \<Longrightarrow> f Y \<in> Y"
eberlm@67829
   369
eberlm@67951
   370
      have "\<And>xa. xa \<in> A \<Longrightarrow> f {y. \<exists>a\<in>xa - {None}. y = the a} = f (the ` (xa - {None}))"
eberlm@67951
   371
        by  (simp add: image_def)
eberlm@67951
   372
      from this have [simp]: "\<And>xa. xa \<in> A \<Longrightarrow> \<exists>x\<in>A. f {y. \<exists>a\<in>xa - {None}. y = the a} = f (the ` (x - {None}))"
eberlm@67951
   373
        by blast
eberlm@67951
   374
      have "\<And>xa. xa \<in> A \<Longrightarrow> f (the ` (xa - {None})) = f {y. \<exists>a \<in> xa - {None}. y = the a} \<and> xa \<in> A"
eberlm@67951
   375
        by (simp add: image_def)
eberlm@67951
   376
      from this have [simp]: "\<And>xa. xa \<in> A \<Longrightarrow> \<exists>x. f (the ` (xa - {None})) = f {y. \<exists>a\<in>x - {None}. y = the a} \<and> x \<in> A"
eberlm@67951
   377
        by blast
eberlm@67829
   378
eberlm@67951
   379
      {
eberlm@67951
   380
        fix Y
eberlm@67951
   381
        have "Y \<in> A \<Longrightarrow> Some (f (the ` (Y - {None}))) \<in> Y"
eberlm@67951
   382
          using A [of "the ` (Y - {None})"] apply (simp add: image_def)
eberlm@67951
   383
          using option.collapse by fastforce
eberlm@67951
   384
      }
eberlm@67951
   385
      from this have [simp]: "\<And> Y . Y \<in> A \<Longrightarrow> Some (f (the ` (Y - {None}))) \<in> Y"
eberlm@67951
   386
        by blast
eberlm@67951
   387
      have [simp]: "(\<Sqinter>x\<in>A. Some (f {y. \<exists>x\<in>x - {None}. y = the x})) = \<Sqinter>{Some (f {y. \<exists>a\<in>x - {None}. y = the a}) |x. x \<in> A}"
eberlm@67951
   388
        by (simp add: Setcompr_eq_image)
eberlm@67951
   389
      
eberlm@67951
   390
      have [simp]: "\<exists>x. (\<exists>f. x = {y. \<exists>x\<in>A. y = f x} \<and> (\<forall>Y\<in>A. f Y \<in> Y)) \<and> \<Sqinter>{Some (f {y. \<exists>a\<in>x - {None}. y = the a}) |x. x \<in> A} = \<Sqinter>x"
eberlm@67951
   391
        apply (rule exI [of _ "{Some (f {y. \<exists>a\<in>x - {None}. y = the a}) | x . x\<in> A}"], safe)
eberlm@67951
   392
        by (rule exI [of _ "(\<lambda> Y . Some (f (the ` (Y - {None})))) "], safe, simp_all)
eberlm@67829
   393
eberlm@67951
   394
      {
eberlm@67951
   395
        fix xb
eberlm@67951
   396
        have "xb \<in> A \<Longrightarrow> (\<Sqinter>x\<in>{{ya. \<exists>x\<in>y - {None}. ya = the x} |y. y \<in> A}. f x) \<le> f {y. \<exists>x\<in>xb - {None}. y = the x}"
eberlm@67951
   397
          apply (rule INF_lower2 [of "{y. \<exists>x\<in>xb - {None}. y = the x}"])
eberlm@67951
   398
          by blast+
eberlm@67951
   399
      }
eberlm@67951
   400
      from this have [simp]: "(\<Sqinter>x\<in>{the ` (y - {None}) |y. y \<in> A}. f x) \<le> the (\<Sqinter>Y\<in>A. Some (f (the ` (Y - {None}))))"
eberlm@67951
   401
        apply (simp add: Inf_option_def image_def Option.these_def)
eberlm@67951
   402
        by (rule Inf_greatest, clarsimp)
eberlm@67951
   403
eberlm@67951
   404
      have [simp]: "the (\<Sqinter>Y\<in>A. Some (f (the ` (Y - {None})))) \<in> Option.these (Inf ` {f ` A |f. \<forall>Y\<in>A. f Y \<in> Y})"
eberlm@67951
   405
        apply (simp add:  Option.these_def image_def)
eberlm@67951
   406
        apply (rule exI [of _ "(\<Sqinter>x\<in>A. Some (f {y. \<exists>x\<in>x - {None}. y = the x}))"], simp)
eberlm@67951
   407
        by (simp add: Inf_option_def)
eberlm@67951
   408
eberlm@67951
   409
      have "(\<Sqinter>x\<in>{the ` (y - {None}) |y. y \<in> A}. f x) \<le> \<Squnion>Option.these (Inf ` {f ` A |f. \<forall>Y\<in>A. f Y \<in> Y})"
eberlm@67951
   410
        by (rule Sup_upper2 [of "the (Inf ((\<lambda> Y . Some (f (the ` (Y - {None})) )) ` A))"], simp_all)
eberlm@67951
   411
    }
eberlm@67951
   412
    from this have X: "\<And> f . \<forall>Y. (\<exists>y. Y = the ` (y - {None}) \<and> y \<in> A) \<longrightarrow> f Y \<in> Y \<Longrightarrow>
eberlm@67951
   413
      (\<Sqinter>x\<in>{the ` (y - {None}) |y. y \<in> A}. f x) \<le> \<Squnion>Option.these (Inf ` {f ` A |f. \<forall>Y\<in>A. f Y \<in> Y})"
eberlm@67829
   414
      by blast
eberlm@67951
   415
    
eberlm@67829
   416
eberlm@67951
   417
    have [simp]: "\<And> x . x\<in>{y - {None} |y. y \<in> A} \<Longrightarrow>  x \<noteq> {} \<and> x \<noteq> {None}"
eberlm@67951
   418
      using F by fastforce
eberlm@67829
   419
eberlm@67829
   420
    have "(Inf (Sup `A)) = (Inf (Sup ` {y - {None} | y. y\<in>A}))"
eberlm@67829
   421
      by (subst A, simp)
eberlm@67829
   422
eberlm@67829
   423
    also have "... = (\<Sqinter>x\<in>{y - {None} |y. y \<in> A}. if x = {} \<or> x = {None} then None else Some (\<Squnion>Option.these x))"
eberlm@67829
   424
      by (simp add: Sup_option_def)
eberlm@67829
   425
eberlm@67829
   426
    also have "... = (\<Sqinter>x\<in>{y - {None} |y. y \<in> A}. Some (\<Squnion>Option.these x))"
eberlm@67829
   427
      using G by fastforce
eberlm@67829
   428
  
eberlm@67829
   429
    also have "... = Some (\<Sqinter>Option.these ((\<lambda>x. Some (\<Squnion>Option.these x)) ` {y - {None} |y. y \<in> A}))"
eberlm@67829
   430
      by (simp add: Inf_option_def, safe)
eberlm@67829
   431
  
eberlm@67829
   432
    also have "... =  Some (\<Sqinter> ((\<lambda>x.  (\<Squnion>Option.these x)) ` {y - {None} |y. y \<in> A}))"
eberlm@67829
   433
      by (simp add: B)
eberlm@67829
   434
  
eberlm@67829
   435
    also have "... = Some (Inf (Sup ` {the ` (y - {None}) |y. y \<in> A}))"
eberlm@67829
   436
      by (unfold C, simp)
eberlm@67829
   437
    thm Inf_Sup
eberlm@67829
   438
    also have "... = Some (\<Squnion>x\<in>{f ` {the ` (y - {None}) |y. y \<in> A} |f. \<forall>Y. (\<exists>y. Y = the ` (y - {None}) \<and> y \<in> A) \<longrightarrow> f Y \<in> Y}. \<Sqinter>x) "
eberlm@67829
   439
      by (simp add: Inf_Sup)
eberlm@67829
   440
  
eberlm@67829
   441
    also have "... \<le> SUPREMUM {f ` A |f. \<forall>Y\<in>A. f Y \<in> Y} Inf"
eberlm@67951
   442
    proof (cases "SUPREMUM {f ` A |f. \<forall>Y\<in>A. f Y \<in> Y} Inf")
eberlm@67951
   443
      case None
eberlm@67951
   444
      then show ?thesis by (simp add: less_eq_option_def)
eberlm@67951
   445
    next
eberlm@67951
   446
      case (Some a)
eberlm@67951
   447
      then show ?thesis
eberlm@67951
   448
        apply simp
eberlm@67951
   449
        apply (rule Sup_least, safe)
eberlm@67951
   450
        apply (simp add: Sup_option_def)
eberlm@67951
   451
        apply (cases "(\<forall>f. \<exists>Y\<in>A. f Y \<notin> Y) \<or> Inf ` {f ` A |f. \<forall>Y\<in>A. f Y \<in> Y} = {None}", simp_all)
eberlm@67951
   452
        by (drule X, simp)
eberlm@67951
   453
    qed
eberlm@67829
   454
    finally show ?thesis by simp
haftmann@49190
   455
  qed
haftmann@49190
   456
qed
haftmann@49190
   457
eberlm@67829
   458
instance option :: (complete_distrib_lattice) complete_distrib_lattice
eberlm@67829
   459
  by (standard, simp add: option_Inf_Sup)
eberlm@67829
   460
wenzelm@60679
   461
instance option :: (complete_linorder) complete_linorder ..
haftmann@49190
   462
haftmann@49190
   463
haftmann@49190
   464
no_notation
haftmann@49190
   465
  bot ("\<bottom>") and
haftmann@49190
   466
  top ("\<top>") and
haftmann@49190
   467
  inf  (infixl "\<sqinter>" 70) and
haftmann@49190
   468
  sup  (infixl "\<squnion>" 65) and
haftmann@49190
   469
  Inf  ("\<Sqinter>_" [900] 900) and
haftmann@49190
   470
  Sup  ("\<Squnion>_" [900] 900)
haftmann@49190
   471
wenzelm@61955
   472
no_syntax
haftmann@49190
   473
  "_INF1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Sqinter>_./ _)" [0, 10] 10)
haftmann@49190
   474
  "_INF"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sqinter>_\<in>_./ _)" [0, 0, 10] 10)
haftmann@49190
   475
  "_SUP1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Squnion>_./ _)" [0, 10] 10)
haftmann@49190
   476
  "_SUP"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Squnion>_\<in>_./ _)" [0, 0, 10] 10)
haftmann@49190
   477
haftmann@49190
   478
end