src/HOL/Library/Option_ord.thy
author wenzelm
Mon Dec 28 01:28:28 2015 +0100 (2015-12-28)
changeset 61945 1135b8de26c3
parent 60679 ade12ef2773c
child 61955 e96292f32c3c
permissions -rw-r--r--
more symbols;
     1 (*  Title:      HOL/Library/Option_ord.thy
     2     Author:     Florian Haftmann, TU Muenchen
     3 *)
     4 
     5 section \<open>Canonical order on option type\<close>
     6 
     7 theory Option_ord
     8 imports Option Main
     9 begin
    10 
    11 notation
    12   bot ("\<bottom>") and
    13   top ("\<top>") and
    14   inf  (infixl "\<sqinter>" 70) and
    15   sup  (infixl "\<squnion>" 65) and
    16   Inf  ("\<Sqinter>_" [900] 900) and
    17   Sup  ("\<Squnion>_" [900] 900)
    18 
    19 syntax (xsymbols)
    20   "_INF1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Sqinter>_./ _)" [0, 10] 10)
    21   "_INF"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sqinter>_\<in>_./ _)" [0, 0, 10] 10)
    22   "_SUP1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Squnion>_./ _)" [0, 10] 10)
    23   "_SUP"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Squnion>_\<in>_./ _)" [0, 0, 10] 10)
    24 
    25 
    26 instantiation option :: (preorder) preorder
    27 begin
    28 
    29 definition less_eq_option where
    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))"
    31 
    32 definition less_option where
    33   "x < y \<longleftrightarrow> (case y of None \<Rightarrow> False | Some y \<Rightarrow> (case x of None \<Rightarrow> True | Some x \<Rightarrow> x < y))"
    34 
    35 lemma less_eq_option_None [simp]: "None \<le> x"
    36   by (simp add: less_eq_option_def)
    37 
    38 lemma less_eq_option_None_code [code]: "None \<le> x \<longleftrightarrow> True"
    39   by simp
    40 
    41 lemma less_eq_option_None_is_None: "x \<le> None \<Longrightarrow> x = None"
    42   by (cases x) (simp_all add: less_eq_option_def)
    43 
    44 lemma less_eq_option_Some_None [simp, code]: "Some x \<le> None \<longleftrightarrow> False"
    45   by (simp add: less_eq_option_def)
    46 
    47 lemma less_eq_option_Some [simp, code]: "Some x \<le> Some y \<longleftrightarrow> x \<le> y"
    48   by (simp add: less_eq_option_def)
    49 
    50 lemma less_option_None [simp, code]: "x < None \<longleftrightarrow> False"
    51   by (simp add: less_option_def)
    52 
    53 lemma less_option_None_is_Some: "None < x \<Longrightarrow> \<exists>z. x = Some z"
    54   by (cases x) (simp_all add: less_option_def)
    55 
    56 lemma less_option_None_Some [simp]: "None < Some x"
    57   by (simp add: less_option_def)
    58 
    59 lemma less_option_None_Some_code [code]: "None < Some x \<longleftrightarrow> True"
    60   by simp
    61 
    62 lemma less_option_Some [simp, code]: "Some x < Some y \<longleftrightarrow> x < y"
    63   by (simp add: less_option_def)
    64 
    65 instance
    66   by standard
    67     (auto simp add: less_eq_option_def less_option_def less_le_not_le
    68       elim: order_trans split: option.splits)
    69 
    70 end
    71 
    72 instance option :: (order) order
    73   by standard (auto simp add: less_eq_option_def less_option_def split: option.splits)
    74 
    75 instance option :: (linorder) linorder
    76   by standard (auto simp add: less_eq_option_def less_option_def split: option.splits)
    77 
    78 instantiation option :: (order) order_bot
    79 begin
    80 
    81 definition bot_option where "\<bottom> = None"
    82 
    83 instance
    84   by standard (simp add: bot_option_def)
    85 
    86 end
    87 
    88 instantiation option :: (order_top) order_top
    89 begin
    90 
    91 definition top_option where "\<top> = Some \<top>"
    92 
    93 instance
    94   by standard (simp add: top_option_def less_eq_option_def split: option.split)
    95 
    96 end
    97 
    98 instance option :: (wellorder) wellorder
    99 proof
   100   fix P :: "'a option \<Rightarrow> bool"
   101   fix z :: "'a option"
   102   assume H: "\<And>x. (\<And>y. y < x \<Longrightarrow> P y) \<Longrightarrow> P x"
   103   have "P None" by (rule H) simp
   104   then have P_Some [case_names Some]: "P z" if "\<And>x. z = Some x \<Longrightarrow> (P o Some) x" for z
   105     using \<open>P None\<close> that by (cases z) simp_all
   106   show "P z"
   107   proof (cases z rule: P_Some)
   108     case (Some w)
   109     show "(P o Some) w"
   110     proof (induct rule: less_induct)
   111       case (less x)
   112       have "P (Some x)"
   113       proof (rule H)
   114         fix y :: "'a option"
   115         assume "y < Some x"
   116         show "P y"
   117         proof (cases y rule: P_Some)
   118           case (Some v)
   119           with \<open>y < Some x\<close> have "v < x" by simp
   120           with less show "(P o Some) v" .
   121         qed
   122       qed
   123       then show ?case by simp
   124     qed
   125   qed
   126 qed
   127 
   128 instantiation option :: (inf) inf
   129 begin
   130 
   131 definition inf_option where
   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)))"
   133 
   134 lemma inf_None_1 [simp, code]: "None \<sqinter> y = None"
   135   by (simp add: inf_option_def)
   136 
   137 lemma inf_None_2 [simp, code]: "x \<sqinter> None = None"
   138   by (cases x) (simp_all add: inf_option_def)
   139 
   140 lemma inf_Some [simp, code]: "Some x \<sqinter> Some y = Some (x \<sqinter> y)"
   141   by (simp add: inf_option_def)
   142 
   143 instance ..
   144 
   145 end
   146 
   147 instantiation option :: (sup) sup
   148 begin
   149 
   150 definition sup_option where
   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)))"
   152 
   153 lemma sup_None_1 [simp, code]: "None \<squnion> y = y"
   154   by (simp add: sup_option_def)
   155 
   156 lemma sup_None_2 [simp, code]: "x \<squnion> None = x"
   157   by (cases x) (simp_all add: sup_option_def)
   158 
   159 lemma sup_Some [simp, code]: "Some x \<squnion> Some y = Some (x \<squnion> y)"
   160   by (simp add: sup_option_def)
   161 
   162 instance ..
   163 
   164 end
   165 
   166 instance option :: (semilattice_inf) semilattice_inf
   167 proof
   168   fix x y z :: "'a option"
   169   show "x \<sqinter> y \<le> x"
   170     by (cases x, simp_all, cases y, simp_all)
   171   show "x \<sqinter> y \<le> y"
   172     by (cases x, simp_all, cases y, simp_all)
   173   show "x \<le> y \<Longrightarrow> x \<le> z \<Longrightarrow> x \<le> y \<sqinter> z"
   174     by (cases x, simp_all, cases y, simp_all, cases z, simp_all)
   175 qed
   176 
   177 instance option :: (semilattice_sup) semilattice_sup
   178 proof
   179   fix x y z :: "'a option"
   180   show "x \<le> x \<squnion> y"
   181     by (cases x, simp_all, cases y, simp_all)
   182   show "y \<le> x \<squnion> y"
   183     by (cases x, simp_all, cases y, simp_all)
   184   fix x y z :: "'a option"
   185   show "y \<le> x \<Longrightarrow> z \<le> x \<Longrightarrow> y \<squnion> z \<le> x"
   186     by (cases y, simp_all, cases z, simp_all, cases x, simp_all)
   187 qed
   188 
   189 instance option :: (lattice) lattice ..
   190 
   191 instance option :: (lattice) bounded_lattice_bot ..
   192 
   193 instance option :: (bounded_lattice_top) bounded_lattice_top ..
   194 
   195 instance option :: (bounded_lattice_top) bounded_lattice ..
   196 
   197 instance option :: (distrib_lattice) distrib_lattice
   198 proof
   199   fix x y z :: "'a option"
   200   show "x \<squnion> y \<sqinter> z = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
   201     by (cases x, simp_all, cases y, simp_all, cases z, simp_all add: sup_inf_distrib1 inf_commute)
   202 qed
   203 
   204 instantiation option :: (complete_lattice) complete_lattice
   205 begin
   206 
   207 definition Inf_option :: "'a option set \<Rightarrow> 'a option" where
   208   "\<Sqinter>A = (if None \<in> A then None else Some (\<Sqinter>Option.these A))"
   209 
   210 lemma None_in_Inf [simp]: "None \<in> A \<Longrightarrow> \<Sqinter>A = None"
   211   by (simp add: Inf_option_def)
   212 
   213 definition Sup_option :: "'a option set \<Rightarrow> 'a option" where
   214   "\<Squnion>A = (if A = {} \<or> A = {None} then None else Some (\<Squnion>Option.these A))"
   215 
   216 lemma empty_Sup [simp]: "\<Squnion>{} = None"
   217   by (simp add: Sup_option_def)
   218 
   219 lemma singleton_None_Sup [simp]: "\<Squnion>{None} = None"
   220   by (simp add: Sup_option_def)
   221 
   222 instance
   223 proof
   224   fix x :: "'a option" and A
   225   assume "x \<in> A"
   226   then show "\<Sqinter>A \<le> x"
   227     by (cases x) (auto simp add: Inf_option_def in_these_eq intro: Inf_lower)
   228 next
   229   fix z :: "'a option" and A
   230   assume *: "\<And>x. x \<in> A \<Longrightarrow> z \<le> x"
   231   show "z \<le> \<Sqinter>A"
   232   proof (cases z)
   233     case None then show ?thesis by simp
   234   next
   235     case (Some y)
   236     show ?thesis
   237       by (auto simp add: Inf_option_def in_these_eq Some intro!: Inf_greatest dest!: *)
   238   qed
   239 next
   240   fix x :: "'a option" and A
   241   assume "x \<in> A"
   242   then show "x \<le> \<Squnion>A"
   243     by (cases x) (auto simp add: Sup_option_def in_these_eq intro: Sup_upper)
   244 next
   245   fix z :: "'a option" and A
   246   assume *: "\<And>x. x \<in> A \<Longrightarrow> x \<le> z"
   247   show "\<Squnion>A \<le> z "
   248   proof (cases z)
   249     case None
   250     with * have "\<And>x. x \<in> A \<Longrightarrow> x = None" by (auto dest: less_eq_option_None_is_None)
   251     then have "A = {} \<or> A = {None}" by blast
   252     then show ?thesis by (simp add: Sup_option_def)
   253   next
   254     case (Some y)
   255     from * have "\<And>w. Some w \<in> A \<Longrightarrow> Some w \<le> z" .
   256     with Some have "\<And>w. w \<in> Option.these A \<Longrightarrow> w \<le> y"
   257       by (simp add: in_these_eq)
   258     then have "\<Squnion>Option.these A \<le> y" by (rule Sup_least)
   259     with Some show ?thesis by (simp add: Sup_option_def)
   260   qed
   261 next
   262   show "\<Squnion>{} = (\<bottom>::'a option)"
   263     by (auto simp: bot_option_def)
   264   show "\<Sqinter>{} = (\<top>::'a option)"
   265     by (auto simp: top_option_def Inf_option_def)
   266 qed
   267 
   268 end
   269 
   270 lemma Some_Inf:
   271   "Some (\<Sqinter>A) = \<Sqinter>(Some ` A)"
   272   by (auto simp add: Inf_option_def)
   273 
   274 lemma Some_Sup:
   275   "A \<noteq> {} \<Longrightarrow> Some (\<Squnion>A) = \<Squnion>(Some ` A)"
   276   by (auto simp add: Sup_option_def)
   277 
   278 lemma Some_INF:
   279   "Some (\<Sqinter>x\<in>A. f x) = (\<Sqinter>x\<in>A. Some (f x))"
   280   using Some_Inf [of "f ` A"] by (simp add: comp_def)
   281 
   282 lemma Some_SUP:
   283   "A \<noteq> {} \<Longrightarrow> Some (\<Squnion>x\<in>A. f x) = (\<Squnion>x\<in>A. Some (f x))"
   284   using Some_Sup [of "f ` A"] by (simp add: comp_def)
   285 
   286 instance option :: (complete_distrib_lattice) complete_distrib_lattice
   287 proof
   288   fix a :: "'a option" and B
   289   show "a \<squnion> \<Sqinter>B = (\<Sqinter>b\<in>B. a \<squnion> b)"
   290   proof (cases a)
   291     case None
   292     then show ?thesis by (simp add: INF_def)
   293   next
   294     case (Some c)
   295     show ?thesis
   296     proof (cases "None \<in> B")
   297       case True
   298       then have "Some c = (\<Sqinter>b\<in>B. Some c \<squnion> b)"
   299         by (auto intro!: antisym INF_lower2 INF_greatest)
   300       with True Some show ?thesis by simp
   301     next
   302       case False then have B: "{x \<in> B. \<exists>y. x = Some y} = B" by auto (metis not_Some_eq)
   303       from sup_Inf have "Some c \<squnion> Some (\<Sqinter>Option.these B) = Some (\<Sqinter>b\<in>Option.these B. c \<squnion> b)" by simp
   304       then have "Some c \<squnion> \<Sqinter>(Some ` Option.these B) = (\<Sqinter>x\<in>Some ` Option.these B. Some c \<squnion> x)"
   305         by (simp add: Some_INF Some_Inf comp_def)
   306       with Some B show ?thesis by (simp add: Some_image_these_eq)
   307     qed
   308   qed
   309   show "a \<sqinter> \<Squnion>B = (\<Squnion>b\<in>B. a \<sqinter> b)"
   310   proof (cases a)
   311     case None
   312     then show ?thesis by (simp add: SUP_def image_constant_conv bot_option_def)
   313   next
   314     case (Some c)
   315     show ?thesis
   316     proof (cases "B = {} \<or> B = {None}")
   317       case True
   318       then show ?thesis by auto
   319     next
   320       have B: "B = {x \<in> B. \<exists>y. x = Some y} \<union> {x \<in> B. x = None}"
   321         by auto
   322       then have Sup_B: "\<Squnion>B = \<Squnion>({x \<in> B. \<exists>y. x = Some y} \<union> {x \<in> B. x = None})"
   323         and SUP_B: "\<And>f. (\<Squnion>x \<in> B. f x) = (\<Squnion>x \<in> {x \<in> B. \<exists>y. x = Some y} \<union> {x \<in> B. x = None}. f x)"
   324         by simp_all
   325       have Sup_None: "\<Squnion>{x. x = None \<and> x \<in> B} = None"
   326         by (simp add: bot_option_def [symmetric])
   327       have SUP_None: "(\<Squnion>x\<in>{x. x = None \<and> x \<in> B}. Some c \<sqinter> x) = None"
   328         by (simp add: bot_option_def [symmetric])
   329       case False then have "Option.these B \<noteq> {}" by (simp add: these_not_empty_eq)
   330       moreover from inf_Sup have "Some c \<sqinter> Some (\<Squnion>Option.these B) = Some (\<Squnion>b\<in>Option.these B. c \<sqinter> b)"
   331         by simp
   332       ultimately have "Some c \<sqinter> \<Squnion>(Some ` Option.these B) = (\<Squnion>x\<in>Some ` Option.these B. Some c \<sqinter> x)"
   333         by (simp add: Some_SUP Some_Sup comp_def)
   334       with Some show ?thesis
   335         by (simp add: Some_image_these_eq Sup_B SUP_B Sup_None SUP_None SUP_union Sup_union_distrib)
   336     qed
   337   qed
   338 qed
   339 
   340 instance option :: (complete_linorder) complete_linorder ..
   341 
   342 
   343 no_notation
   344   bot ("\<bottom>") and
   345   top ("\<top>") and
   346   inf  (infixl "\<sqinter>" 70) and
   347   sup  (infixl "\<squnion>" 65) and
   348   Inf  ("\<Sqinter>_" [900] 900) and
   349   Sup  ("\<Squnion>_" [900] 900)
   350 
   351 no_syntax (xsymbols)
   352   "_INF1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Sqinter>_./ _)" [0, 10] 10)
   353   "_INF"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sqinter>_\<in>_./ _)" [0, 0, 10] 10)
   354   "_SUP1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Squnion>_./ _)" [0, 10] 10)
   355   "_SUP"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Squnion>_\<in>_./ _)" [0, 0, 10] 10)
   356 
   357 end