src/HOL/Library/Option_ord.thy
changeset 49190 e1e1d427747d
parent 43815 4f6e2965d821
child 52729 412c9e0381a1
     1.1 --- a/src/HOL/Library/Option_ord.thy	Fri Sep 07 08:20:18 2012 +0200
     1.2 +++ b/src/HOL/Library/Option_ord.thy	Fri Sep 07 08:20:18 2012 +0200
     1.3 @@ -8,6 +8,21 @@
     1.4  imports Option Main
     1.5  begin
     1.6  
     1.7 +notation
     1.8 +  bot ("\<bottom>") and
     1.9 +  top ("\<top>") and
    1.10 +  inf  (infixl "\<sqinter>" 70) and
    1.11 +  sup  (infixl "\<squnion>" 65) and
    1.12 +  Inf  ("\<Sqinter>_" [900] 900) and
    1.13 +  Sup  ("\<Squnion>_" [900] 900)
    1.14 +
    1.15 +syntax (xsymbols)
    1.16 +  "_INF1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Sqinter>_./ _)" [0, 10] 10)
    1.17 +  "_INF"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sqinter>_\<in>_./ _)" [0, 0, 10] 10)
    1.18 +  "_SUP1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Squnion>_./ _)" [0, 10] 10)
    1.19 +  "_SUP"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Squnion>_\<in>_./ _)" [0, 0, 10] 10)
    1.20 +
    1.21 +
    1.22  instantiation option :: (preorder) preorder
    1.23  begin
    1.24  
    1.25 @@ -61,7 +76,8 @@
    1.26  instantiation option :: (order) bot
    1.27  begin
    1.28  
    1.29 -definition "bot = None"
    1.30 +definition bot_option where
    1.31 +  "\<bottom> = None"
    1.32  
    1.33  instance proof
    1.34  qed (simp add: bot_option_def)
    1.35 @@ -71,7 +87,8 @@
    1.36  instantiation option :: (top) top
    1.37  begin
    1.38  
    1.39 -definition "top = Some top"
    1.40 +definition top_option where
    1.41 +  "\<top> = Some \<top>"
    1.42  
    1.43  instance proof
    1.44  qed (simp add: top_option_def less_eq_option_def split: option.split)
    1.45 @@ -106,4 +123,254 @@
    1.46    qed
    1.47  qed
    1.48  
    1.49 +instantiation option :: (inf) inf
    1.50 +begin
    1.51 +
    1.52 +definition inf_option where
    1.53 +  "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)))"
    1.54 +
    1.55 +lemma inf_None_1 [simp, code]:
    1.56 +  "None \<sqinter> y = None"
    1.57 +  by (simp add: inf_option_def)
    1.58 +
    1.59 +lemma inf_None_2 [simp, code]:
    1.60 +  "x \<sqinter> None = None"
    1.61 +  by (cases x) (simp_all add: inf_option_def)
    1.62 +
    1.63 +lemma inf_Some [simp, code]:
    1.64 +  "Some x \<sqinter> Some y = Some (x \<sqinter> y)"
    1.65 +  by (simp add: inf_option_def)
    1.66 +
    1.67 +instance ..
    1.68 +
    1.69  end
    1.70 +
    1.71 +instantiation option :: (sup) sup
    1.72 +begin
    1.73 +
    1.74 +definition sup_option where
    1.75 +  "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)))"
    1.76 +
    1.77 +lemma sup_None_1 [simp, code]:
    1.78 +  "None \<squnion> y = y"
    1.79 +  by (simp add: sup_option_def)
    1.80 +
    1.81 +lemma sup_None_2 [simp, code]:
    1.82 +  "x \<squnion> None = x"
    1.83 +  by (cases x) (simp_all add: sup_option_def)
    1.84 +
    1.85 +lemma sup_Some [simp, code]:
    1.86 +  "Some x \<squnion> Some y = Some (x \<squnion> y)"
    1.87 +  by (simp add: sup_option_def)
    1.88 +
    1.89 +instance ..
    1.90 +
    1.91 +end
    1.92 +
    1.93 +instantiation option :: (semilattice_inf) semilattice_inf
    1.94 +begin
    1.95 +
    1.96 +instance proof
    1.97 +  fix x y z :: "'a option"
    1.98 +  show "x \<sqinter> y \<le> x"
    1.99 +    by - (cases x, simp_all, cases y, simp_all)
   1.100 +  show "x \<sqinter> y \<le> y"
   1.101 +    by - (cases x, simp_all, cases y, simp_all)
   1.102 +  show "x \<le> y \<Longrightarrow> x \<le> z \<Longrightarrow> x \<le> y \<sqinter> z"
   1.103 +    by - (cases x, simp_all, cases y, simp_all, cases z, simp_all)
   1.104 +qed
   1.105 +  
   1.106 +end
   1.107 +
   1.108 +instantiation option :: (semilattice_sup) semilattice_sup
   1.109 +begin
   1.110 +
   1.111 +instance proof
   1.112 +  fix x y z :: "'a option"
   1.113 +  show "x \<le> x \<squnion> y"
   1.114 +    by - (cases x, simp_all, cases y, simp_all)
   1.115 +  show "y \<le> x \<squnion> y"
   1.116 +    by - (cases x, simp_all, cases y, simp_all)
   1.117 +  fix x y z :: "'a option"
   1.118 +  show "y \<le> x \<Longrightarrow> z \<le> x \<Longrightarrow> y \<squnion> z \<le> x"
   1.119 +    by - (cases y, simp_all, cases z, simp_all, cases x, simp_all)
   1.120 +qed
   1.121 +
   1.122 +end
   1.123 +
   1.124 +instance option :: (lattice) lattice ..
   1.125 +
   1.126 +instance option :: (lattice) bounded_lattice_bot ..
   1.127 +
   1.128 +instance option :: (bounded_lattice_top) bounded_lattice_top ..
   1.129 +
   1.130 +instance option :: (bounded_lattice_top) bounded_lattice ..
   1.131 +
   1.132 +instance option :: (distrib_lattice) distrib_lattice
   1.133 +proof
   1.134 +  fix x y z :: "'a option"
   1.135 +  show "x \<squnion> y \<sqinter> z = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
   1.136 +    by - (cases x, simp_all, cases y, simp_all, cases z, simp_all add: sup_inf_distrib1 inf_commute)
   1.137 +qed 
   1.138 +
   1.139 +instantiation option :: (complete_lattice) complete_lattice
   1.140 +begin
   1.141 +
   1.142 +definition Inf_option :: "'a option set \<Rightarrow> 'a option" where
   1.143 +  "\<Sqinter>A = (if None \<in> A then None else Some (\<Sqinter>Option.these A))"
   1.144 +
   1.145 +lemma None_in_Inf [simp]:
   1.146 +  "None \<in> A \<Longrightarrow> \<Sqinter>A = None"
   1.147 +  by (simp add: Inf_option_def)
   1.148 +
   1.149 +definition Sup_option :: "'a option set \<Rightarrow> 'a option" where
   1.150 +  "\<Squnion>A = (if A = {} \<or> A = {None} then None else Some (\<Squnion>Option.these A))"
   1.151 +
   1.152 +lemma empty_Sup [simp]:
   1.153 +  "\<Squnion>{} = None"
   1.154 +  by (simp add: Sup_option_def)
   1.155 +
   1.156 +lemma singleton_None_Sup [simp]:
   1.157 +  "\<Squnion>{None} = None"
   1.158 +  by (simp add: Sup_option_def)
   1.159 +
   1.160 +instance proof
   1.161 +  fix x :: "'a option" and A
   1.162 +  assume "x \<in> A"
   1.163 +  then show "\<Sqinter>A \<le> x"
   1.164 +    by (cases x) (auto simp add: Inf_option_def in_these_eq intro: Inf_lower)
   1.165 +next
   1.166 +  fix z :: "'a option" and A
   1.167 +  assume *: "\<And>x. x \<in> A \<Longrightarrow> z \<le> x"
   1.168 +  show "z \<le> \<Sqinter>A"
   1.169 +  proof (cases z)
   1.170 +    case None then show ?thesis by simp
   1.171 +  next
   1.172 +    case (Some y)
   1.173 +    show ?thesis
   1.174 +      by (auto simp add: Inf_option_def in_these_eq Some intro!: Inf_greatest dest!: *)
   1.175 +  qed
   1.176 +next
   1.177 +  fix x :: "'a option" and A
   1.178 +  assume "x \<in> A"
   1.179 +  then show "x \<le> \<Squnion>A"
   1.180 +    by (cases x) (auto simp add: Sup_option_def in_these_eq intro: Sup_upper)
   1.181 +next
   1.182 +  fix z :: "'a option" and A
   1.183 +  assume *: "\<And>x. x \<in> A \<Longrightarrow> x \<le> z"
   1.184 +  show "\<Squnion>A \<le> z "
   1.185 +  proof (cases z)
   1.186 +    case None
   1.187 +    with * have "\<And>x. x \<in> A \<Longrightarrow> x = None" by (auto dest: less_eq_option_None_is_None)
   1.188 +    then have "A = {} \<or> A = {None}" by blast
   1.189 +    then show ?thesis by (simp add: Sup_option_def)
   1.190 +  next
   1.191 +    case (Some y)
   1.192 +    from * have "\<And>w. Some w \<in> A \<Longrightarrow> Some w \<le> z" .
   1.193 +    with Some have "\<And>w. w \<in> Option.these A \<Longrightarrow> w \<le> y"
   1.194 +      by (simp add: in_these_eq)
   1.195 +    then have "\<Squnion>Option.these A \<le> y" by (rule Sup_least)
   1.196 +    with Some show ?thesis by (simp add: Sup_option_def)
   1.197 +  qed
   1.198 +qed
   1.199 +
   1.200 +end
   1.201 +
   1.202 +lemma Some_Inf:
   1.203 +  "Some (\<Sqinter>A) = \<Sqinter>(Some ` A)"
   1.204 +  by (auto simp add: Inf_option_def)
   1.205 +
   1.206 +lemma Some_Sup:
   1.207 +  "A \<noteq> {} \<Longrightarrow> Some (\<Squnion>A) = \<Squnion>(Some ` A)"
   1.208 +  by (auto simp add: Sup_option_def)
   1.209 +
   1.210 +lemma Some_INF:
   1.211 +  "Some (\<Sqinter>x\<in>A. f x) = (\<Sqinter>x\<in>A. Some (f x))"
   1.212 +  by (simp add: INF_def Some_Inf image_image)
   1.213 +
   1.214 +lemma Some_SUP:
   1.215 +  "A \<noteq> {} \<Longrightarrow> Some (\<Squnion>x\<in>A. f x) = (\<Squnion>x\<in>A. Some (f x))"
   1.216 +  by (simp add: SUP_def Some_Sup image_image)
   1.217 +
   1.218 +instantiation option :: (complete_distrib_lattice) complete_distrib_lattice
   1.219 +begin
   1.220 +
   1.221 +instance proof
   1.222 +  fix a :: "'a option" and B
   1.223 +  show "a \<squnion> \<Sqinter>B = (\<Sqinter>b\<in>B. a \<squnion> b)"
   1.224 +  proof (cases a)
   1.225 +    case None
   1.226 +    then show ?thesis by (simp add: INF_def)
   1.227 +  next
   1.228 +    case (Some c)
   1.229 +    show ?thesis
   1.230 +    proof (cases "None \<in> B")
   1.231 +      case True
   1.232 +      then have "Some c = (\<Sqinter>b\<in>B. Some c \<squnion> b)"
   1.233 +        by (auto intro!: antisym INF_lower2 INF_greatest)
   1.234 +      with True Some show ?thesis by simp
   1.235 +    next
   1.236 +      case False then have B: "{x \<in> B. \<exists>y. x = Some y} = B" by auto (metis not_Some_eq)
   1.237 +      from sup_Inf have "Some c \<squnion> Some (\<Sqinter>Option.these B) = Some (\<Sqinter>b\<in>Option.these B. c \<squnion> b)" by simp
   1.238 +      then have "Some c \<squnion> \<Sqinter>(Some ` Option.these B) = (\<Sqinter>x\<in>Some ` Option.these B. Some c \<squnion> x)"
   1.239 +        by (simp add: Some_INF Some_Inf)
   1.240 +      with Some B show ?thesis by (simp add: Some_image_these_eq)
   1.241 +    qed
   1.242 +  qed
   1.243 +  show "a \<sqinter> \<Squnion>B = (\<Squnion>b\<in>B. a \<sqinter> b)"
   1.244 +  proof (cases a)
   1.245 +    case None
   1.246 +    then show ?thesis by (simp add: SUP_def image_constant_conv bot_option_def)
   1.247 +  next
   1.248 +    case (Some c)
   1.249 +    show ?thesis
   1.250 +    proof (cases "B = {} \<or> B = {None}")
   1.251 +      case True
   1.252 +      then show ?thesis by (auto simp add: SUP_def)
   1.253 +    next
   1.254 +      have B: "B = {x \<in> B. \<exists>y. x = Some y} \<union> {x \<in> B. x = None}"
   1.255 +        by auto
   1.256 +      then have Sup_B: "\<Squnion>B = \<Squnion>({x \<in> B. \<exists>y. x = Some y} \<union> {x \<in> B. x = None})"
   1.257 +        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)"
   1.258 +        by simp_all
   1.259 +      have Sup_None: "\<Squnion>{x. x = None \<and> x \<in> B} = None"
   1.260 +        by (simp add: bot_option_def [symmetric])
   1.261 +      have SUP_None: "(\<Squnion>x\<in>{x. x = None \<and> x \<in> B}. Some c \<sqinter> x) = None"
   1.262 +        by (simp add: bot_option_def [symmetric])
   1.263 +      case False then have "Option.these B \<noteq> {}" by (simp add: these_not_empty_eq)
   1.264 +      moreover from inf_Sup have "Some c \<sqinter> Some (\<Squnion>Option.these B) = Some (\<Squnion>b\<in>Option.these B. c \<sqinter> b)"
   1.265 +        by simp
   1.266 +      ultimately have "Some c \<sqinter> \<Squnion>(Some ` Option.these B) = (\<Squnion>x\<in>Some ` Option.these B. Some c \<sqinter> x)"
   1.267 +        by (simp add: Some_SUP Some_Sup)
   1.268 +      with Some show ?thesis
   1.269 +        by (simp add: Some_image_these_eq Sup_B SUP_B Sup_None SUP_None SUP_union Sup_union_distrib)
   1.270 +    qed
   1.271 +  qed
   1.272 +qed
   1.273 +
   1.274 +end
   1.275 +
   1.276 +instantiation option :: (complete_linorder) complete_linorder
   1.277 +begin
   1.278 +
   1.279 +instance ..
   1.280 +
   1.281 +end
   1.282 +
   1.283 +
   1.284 +no_notation
   1.285 +  bot ("\<bottom>") and
   1.286 +  top ("\<top>") and
   1.287 +  inf  (infixl "\<sqinter>" 70) and
   1.288 +  sup  (infixl "\<squnion>" 65) and
   1.289 +  Inf  ("\<Sqinter>_" [900] 900) and
   1.290 +  Sup  ("\<Squnion>_" [900] 900)
   1.291 +
   1.292 +no_syntax (xsymbols)
   1.293 +  "_INF1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Sqinter>_./ _)" [0, 10] 10)
   1.294 +  "_INF"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sqinter>_\<in>_./ _)" [0, 0, 10] 10)
   1.295 +  "_SUP1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Squnion>_./ _)" [0, 10] 10)
   1.296 +  "_SUP"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Squnion>_\<in>_./ _)" [0, 0, 10] 10)
   1.297 +
   1.298 +end
   1.299 +