
haftmann@26241: (*  Title:      HOL/Library/Option_ord.thy
haftmann@26241:     Author:     Florian Haftmann, TU Muenchen
haftmann@26241: *)
haftmann@26241: 
haftmann@26263: header {* Canonical order on option type *}
haftmann@26241: 
haftmann@26241: theory Option_ord
haftmann@30662: imports Option Main
haftmann@26241: begin
haftmann@26241: 
haftmann@49190: notation
haftmann@49190:   bot ("\<bottom>") and
haftmann@49190:   top ("\<top>") and
haftmann@49190:   inf  (infixl "\<sqinter>" 70) and
haftmann@49190:   sup  (infixl "\<squnion>" 65) and
haftmann@49190:   Inf  ("\<Sqinter>_" [900] 900) and
haftmann@49190:   Sup  ("\<Squnion>_" [900] 900)
haftmann@49190: 
haftmann@49190: syntax (xsymbols)
haftmann@49190:   "_INF1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Sqinter>_./ _)" [0, 10] 10)
haftmann@49190:   "_INF"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sqinter>_\<in>_./ _)" [0, 0, 10] 10)
haftmann@49190:   "_SUP1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Squnion>_./ _)" [0, 10] 10)
haftmann@49190:   "_SUP"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Squnion>_\<in>_./ _)" [0, 0, 10] 10)
haftmann@49190: 
haftmann@49190: 
haftmann@30662: instantiation option :: (preorder) preorder
haftmann@26241: begin
haftmann@26241: 
haftmann@26241: definition less_eq_option where
haftmann@37765:   "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: 
haftmann@26241: definition less_option where
haftmann@37765:   "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: 
haftmann@26258: lemma less_eq_option_None [simp]: "None \<le> x"
haftmann@26241:   by (simp add: less_eq_option_def)
haftmann@26241: 
haftmann@26258: lemma less_eq_option_None_code [code]: "None \<le> x \<longleftrightarrow> True"
haftmann@26241:   by simp
haftmann@26241: 
haftmann@26258: lemma less_eq_option_None_is_None: "x \<le> None \<Longrightarrow> x = None"
haftmann@26241:   by (cases x) (simp_all add: less_eq_option_def)
haftmann@26241: 
haftmann@26258: lemma less_eq_option_Some_None [simp, code]: "Some x \<le> None \<longleftrightarrow> False"
haftmann@26241:   by (simp add: less_eq_option_def)
haftmann@26241: 
haftmann@26258: lemma less_eq_option_Some [simp, code]: "Some x \<le> Some y \<longleftrightarrow> x \<le> y"
haftmann@26241:   by (simp add: less_eq_option_def)
haftmann@26241: 
haftmann@26258: lemma less_option_None [simp, code]: "x < None \<longleftrightarrow> False"
haftmann@26241:   by (simp add: less_option_def)
haftmann@26241: 
haftmann@26258: lemma less_option_None_is_Some: "None < x \<Longrightarrow> \<exists>z. x = Some z"
haftmann@26241:   by (cases x) (simp_all add: less_option_def)
haftmann@26241: 
haftmann@26258: lemma less_option_None_Some [simp]: "None < Some x"
haftmann@26241:   by (simp add: less_option_def)
haftmann@26241: 
haftmann@26258: lemma less_option_None_Some_code [code]: "None < Some x \<longleftrightarrow> True"
haftmann@26241:   by simp
haftmann@26241: 
haftmann@26258: lemma less_option_Some [simp, code]: "Some x < Some y \<longleftrightarrow> x < y"
haftmann@26241:   by (simp add: less_option_def)
haftmann@26241: 
haftmann@30662: instance proof
haftmann@30662: qed (auto simp add: less_eq_option_def less_option_def less_le_not_le elim: order_trans split: option.splits)
haftmann@26241: 
haftmann@26241: end 
haftmann@26241: 
haftmann@30662: instance option :: (order) order proof
haftmann@30662: qed (auto simp add: less_eq_option_def less_option_def split: option.splits)
haftmann@30662: 
haftmann@30662: instance option :: (linorder) linorder proof
haftmann@30662: qed (auto simp add: less_eq_option_def less_option_def split: option.splits)
haftmann@30662: 
haftmann@43815: instantiation option :: (order) bot
haftmann@30662: begin
haftmann@30662: 
haftmann@49190: definition bot_option where
haftmann@49190:   "\<bottom> = None"
haftmann@30662: 
haftmann@30662: instance proof
haftmann@30662: qed (simp add: bot_option_def)
haftmann@30662: 
haftmann@30662: end
haftmann@30662: 
haftmann@30662: instantiation option :: (top) top
haftmann@30662: begin
haftmann@30662: 
haftmann@49190: definition top_option where
haftmann@49190:   "\<top> = Some \<top>"
haftmann@30662: 
haftmann@30662: instance proof
haftmann@30662: qed (simp add: top_option_def less_eq_option_def split: option.split)
haftmann@26241: 
haftmann@26241: end
haftmann@30662: 
haftmann@30662: instance option :: (wellorder) wellorder proof
haftmann@30662:   fix P :: "'a option \<Rightarrow> bool" and z :: "'a option"
haftmann@30662:   assume H: "\<And>x. (\<And>y. y < x \<Longrightarrow> P y) \<Longrightarrow> P x"
haftmann@30662:   have "P None" by (rule H) simp
haftmann@30662:   then have P_Some [case_names Some]:
haftmann@30662:     "\<And>z. (\<And>x. z = Some x \<Longrightarrow> (P o Some) x) \<Longrightarrow> P z"
haftmann@30662:   proof -
haftmann@30662:     fix z
haftmann@30662:     assume "\<And>x. z = Some x \<Longrightarrow> (P o Some) x"
haftmann@30662:     with `P None` show "P z" by (cases z) simp_all
haftmann@30662:   qed
haftmann@30662:   show "P z" proof (cases z rule: P_Some)
haftmann@30662:     case (Some w)
haftmann@30662:     show "(P o Some) w" proof (induct rule: less_induct)
haftmann@30662:       case (less x)
haftmann@30662:       have "P (Some x)" proof (rule H)
haftmann@30662:         fix y :: "'a option"
haftmann@30662:         assume "y < Some x"
haftmann@30662:         show "P y" proof (cases y rule: P_Some)
haftmann@30662:           case (Some v) with `y < Some x` have "v < x" by simp
haftmann@30662:           with less show "(P o Some) v" .
haftmann@30662:         qed
haftmann@30662:       qed
haftmann@30662:       then show ?case by simp
haftmann@30662:     qed
haftmann@30662:   qed
haftmann@30662: qed
haftmann@30662: 
haftmann@49190: instantiation option :: (inf) inf
haftmann@49190: begin
haftmann@49190: 
haftmann@49190: definition inf_option where
haftmann@49190:   "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: 
haftmann@49190: lemma inf_None_1 [simp, code]:
haftmann@49190:   "None \<sqinter> y = None"
haftmann@49190:   by (simp add: inf_option_def)
haftmann@49190: 
haftmann@49190: lemma inf_None_2 [simp, code]:
haftmann@49190:   "x \<sqinter> None = None"
haftmann@49190:   by (cases x) (simp_all add: inf_option_def)
haftmann@49190: 
haftmann@49190: lemma inf_Some [simp, code]:
haftmann@49190:   "Some x \<sqinter> Some y = Some (x \<sqinter> y)"
haftmann@49190:   by (simp add: inf_option_def)
haftmann@49190: 
haftmann@49190: instance ..
haftmann@49190: 
haftmann@30662: end
haftmann@49190: 
haftmann@49190: instantiation option :: (sup) sup
haftmann@49190: begin
haftmann@49190: 
haftmann@49190: definition sup_option where
haftmann@49190:   "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: 
haftmann@49190: lemma sup_None_1 [simp, code]:
haftmann@49190:   "None \<squnion> y = y"
haftmann@49190:   by (simp add: sup_option_def)
haftmann@49190: 
haftmann@49190: lemma sup_None_2 [simp, code]:
haftmann@49190:   "x \<squnion> None = x"
haftmann@49190:   by (cases x) (simp_all add: sup_option_def)
haftmann@49190: 
haftmann@49190: lemma sup_Some [simp, code]:
haftmann@49190:   "Some x \<squnion> Some y = Some (x \<squnion> y)"
haftmann@49190:   by (simp add: sup_option_def)
haftmann@49190: 
haftmann@49190: instance ..
haftmann@49190: 
haftmann@49190: end
haftmann@49190: 
haftmann@49190: instantiation option :: (semilattice_inf) semilattice_inf
haftmann@49190: begin
haftmann@49190: 
haftmann@49190: instance proof
haftmann@49190:   fix x y z :: "'a option"
haftmann@49190:   show "x \<sqinter> y \<le> x"
haftmann@49190:     by - (cases x, simp_all, cases y, simp_all)
haftmann@49190:   show "x \<sqinter> y \<le> y"
haftmann@49190:     by - (cases x, simp_all, cases y, simp_all)
haftmann@49190:   show "x \<le> y \<Longrightarrow> x \<le> z \<Longrightarrow> x \<le> y \<sqinter> z"
haftmann@49190:     by - (cases x, simp_all, cases y, simp_all, cases z, simp_all)
haftmann@49190: qed
haftmann@49190:   
haftmann@49190: end
haftmann@49190: 
haftmann@49190: instantiation option :: (semilattice_sup) semilattice_sup
haftmann@49190: begin
haftmann@49190: 
haftmann@49190: instance proof
haftmann@49190:   fix x y z :: "'a option"
haftmann@49190:   show "x \<le> x \<squnion> y"
haftmann@49190:     by - (cases x, simp_all, cases y, simp_all)
haftmann@49190:   show "y \<le> x \<squnion> y"
haftmann@49190:     by - (cases x, simp_all, cases y, simp_all)
haftmann@49190:   fix x y z :: "'a option"
haftmann@49190:   show "y \<le> x \<Longrightarrow> z \<le> x \<Longrightarrow> y \<squnion> z \<le> x"
haftmann@49190:     by - (cases y, simp_all, cases z, simp_all, cases x, simp_all)
haftmann@49190: qed
haftmann@49190: 
haftmann@49190: end
haftmann@49190: 
haftmann@49190: instance option :: (lattice) lattice ..
haftmann@49190: 
haftmann@49190: instance option :: (lattice) bounded_lattice_bot ..
haftmann@49190: 
haftmann@49190: instance option :: (bounded_lattice_top) bounded_lattice_top ..
haftmann@49190: 
haftmann@49190: instance option :: (bounded_lattice_top) bounded_lattice ..
haftmann@49190: 
haftmann@49190: instance option :: (distrib_lattice) distrib_lattice
haftmann@49190: proof
haftmann@49190:   fix x y z :: "'a option"
haftmann@49190:   show "x \<squnion> y \<sqinter> z = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
haftmann@49190:     by - (cases x, simp_all, cases y, simp_all, cases z, simp_all add: sup_inf_distrib1 inf_commute)
haftmann@49190: qed 
haftmann@49190: 
haftmann@49190: instantiation option :: (complete_lattice) complete_lattice
haftmann@49190: begin
haftmann@49190: 
haftmann@49190: definition Inf_option :: "'a option set \<Rightarrow> 'a option" where
haftmann@49190:   "\<Sqinter>A = (if None \<in> A then None else Some (\<Sqinter>Option.these A))"
haftmann@49190: 
haftmann@49190: lemma None_in_Inf [simp]:
haftmann@49190:   "None \<in> A \<Longrightarrow> \<Sqinter>A = None"
haftmann@49190:   by (simp add: Inf_option_def)
haftmann@49190: 
haftmann@49190: definition Sup_option :: "'a option set \<Rightarrow> 'a option" where
haftmann@49190:   "\<Squnion>A = (if A = {} \<or> A = {None} then None else Some (\<Squnion>Option.these A))"
haftmann@49190: 
haftmann@49190: lemma empty_Sup [simp]:
haftmann@49190:   "\<Squnion>{} = None"
haftmann@49190:   by (simp add: Sup_option_def)
haftmann@49190: 
haftmann@49190: lemma singleton_None_Sup [simp]:
haftmann@49190:   "\<Squnion>{None} = None"
haftmann@49190:   by (simp add: Sup_option_def)
haftmann@49190: 
haftmann@49190: instance proof
haftmann@49190:   fix x :: "'a option" and A
haftmann@49190:   assume "x \<in> A"
haftmann@49190:   then show "\<Sqinter>A \<le> x"
haftmann@49190:     by (cases x) (auto simp add: Inf_option_def in_these_eq intro: Inf_lower)
haftmann@49190: next
haftmann@49190:   fix z :: "'a option" and A
haftmann@49190:   assume *: "\<And>x. x \<in> A \<Longrightarrow> z \<le> x"
haftmann@49190:   show "z \<le> \<Sqinter>A"
haftmann@49190:   proof (cases z)
haftmann@49190:     case None then show ?thesis by simp
haftmann@49190:   next
haftmann@49190:     case (Some y)
haftmann@49190:     show ?thesis
haftmann@49190:       by (auto simp add: Inf_option_def in_these_eq Some intro!: Inf_greatest dest!: *)
haftmann@49190:   qed
haftmann@49190: next
haftmann@49190:   fix x :: "'a option" and A
haftmann@49190:   assume "x \<in> A"
haftmann@49190:   then show "x \<le> \<Squnion>A"
haftmann@49190:     by (cases x) (auto simp add: Sup_option_def in_these_eq intro: Sup_upper)
haftmann@49190: next
haftmann@49190:   fix z :: "'a option" and A
haftmann@49190:   assume *: "\<And>x. x \<in> A \<Longrightarrow> x \<le> z"
haftmann@49190:   show "\<Squnion>A \<le> z "
haftmann@49190:   proof (cases z)
haftmann@49190:     case None
haftmann@49190:     with * have "\<And>x. x \<in> A \<Longrightarrow> x = None" by (auto dest: less_eq_option_None_is_None)
haftmann@49190:     then have "A = {} \<or> A = {None}" by blast
haftmann@49190:     then show ?thesis by (simp add: Sup_option_def)
haftmann@49190:   next
haftmann@49190:     case (Some y)
haftmann@49190:     from * have "\<And>w. Some w \<in> A \<Longrightarrow> Some w \<le> z" .
haftmann@49190:     with Some have "\<And>w. w \<in> Option.these A \<Longrightarrow> w \<le> y"
haftmann@49190:       by (simp add: in_these_eq)
haftmann@49190:     then have "\<Squnion>Option.these A \<le> y" by (rule Sup_least)
haftmann@49190:     with Some show ?thesis by (simp add: Sup_option_def)
haftmann@49190:   qed
haftmann@49190: qed
haftmann@49190: 
haftmann@49190: end
haftmann@49190: 
haftmann@49190: lemma Some_Inf:
haftmann@49190:   "Some (\<Sqinter>A) = \<Sqinter>(Some ` A)"
haftmann@49190:   by (auto simp add: Inf_option_def)
haftmann@49190: 
haftmann@49190: lemma Some_Sup:
haftmann@49190:   "A \<noteq> {} \<Longrightarrow> Some (\<Squnion>A) = \<Squnion>(Some ` A)"
haftmann@49190:   by (auto simp add: Sup_option_def)
haftmann@49190: 
haftmann@49190: lemma Some_INF:
haftmann@49190:   "Some (\<Sqinter>x\<in>A. f x) = (\<Sqinter>x\<in>A. Some (f x))"
haftmann@49190:   by (simp add: INF_def Some_Inf image_image)
haftmann@49190: 
haftmann@49190: lemma Some_SUP:
haftmann@49190:   "A \<noteq> {} \<Longrightarrow> Some (\<Squnion>x\<in>A. f x) = (\<Squnion>x\<in>A. Some (f x))"
haftmann@49190:   by (simp add: SUP_def Some_Sup image_image)
haftmann@49190: 
haftmann@49190: instantiation option :: (complete_distrib_lattice) complete_distrib_lattice
haftmann@49190: begin
haftmann@49190: 
haftmann@49190: instance proof
haftmann@49190:   fix a :: "'a option" and B
haftmann@49190:   show "a \<squnion> \<Sqinter>B = (\<Sqinter>b\<in>B. a \<squnion> b)"
haftmann@49190:   proof (cases a)
haftmann@49190:     case None
haftmann@49190:     then show ?thesis by (simp add: INF_def)
haftmann@49190:   next
haftmann@49190:     case (Some c)
haftmann@49190:     show ?thesis
haftmann@49190:     proof (cases "None \<in> B")
haftmann@49190:       case True
haftmann@49190:       then have "Some c = (\<Sqinter>b\<in>B. Some c \<squnion> b)"
haftmann@49190:         by (auto intro!: antisym INF_lower2 INF_greatest)
haftmann@49190:       with True Some show ?thesis by simp
haftmann@49190:     next
haftmann@49190:       case False then have B: "{x \<in> B. \<exists>y. x = Some y} = B" by auto (metis not_Some_eq)
haftmann@49190:       from sup_Inf have "Some c \<squnion> Some (\<Sqinter>Option.these B) = Some (\<Sqinter>b\<in>Option.these B. c \<squnion> b)" by simp
haftmann@49190:       then have "Some c \<squnion> \<Sqinter>(Some ` Option.these B) = (\<Sqinter>x\<in>Some ` Option.these B. Some c \<squnion> x)"
haftmann@49190:         by (simp add: Some_INF Some_Inf)
haftmann@49190:       with Some B show ?thesis by (simp add: Some_image_these_eq)
haftmann@49190:     qed
haftmann@49190:   qed
haftmann@49190:   show "a \<sqinter> \<Squnion>B = (\<Squnion>b\<in>B. a \<sqinter> b)"
haftmann@49190:   proof (cases a)
haftmann@49190:     case None
haftmann@49190:     then show ?thesis by (simp add: SUP_def image_constant_conv bot_option_def)
haftmann@49190:   next
haftmann@49190:     case (Some c)
haftmann@49190:     show ?thesis
haftmann@49190:     proof (cases "B = {} \<or> B = {None}")
haftmann@49190:       case True
haftmann@49190:       then show ?thesis by (auto simp add: SUP_def)
haftmann@49190:     next
haftmann@49190:       have B: "B = {x \<in> B. \<exists>y. x = Some y} \<union> {x \<in> B. x = None}"
haftmann@49190:         by auto
haftmann@49190:       then have Sup_B: "\<Squnion>B = \<Squnion>({x \<in> B. \<exists>y. x = Some y} \<union> {x \<in> B. x = None})"
haftmann@49190:         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)"
haftmann@49190:         by simp_all
haftmann@49190:       have Sup_None: "\<Squnion>{x. x = None \<and> x \<in> B} = None"
haftmann@49190:         by (simp add: bot_option_def [symmetric])
haftmann@49190:       have SUP_None: "(\<Squnion>x\<in>{x. x = None \<and> x \<in> B}. Some c \<sqinter> x) = None"
haftmann@49190:         by (simp add: bot_option_def [symmetric])
haftmann@49190:       case False then have "Option.these B \<noteq> {}" by (simp add: these_not_empty_eq)
haftmann@49190:       moreover from inf_Sup have "Some c \<sqinter> Some (\<Squnion>Option.these B) = Some (\<Squnion>b\<in>Option.these B. c \<sqinter> b)"
haftmann@49190:         by simp
haftmann@49190:       ultimately have "Some c \<sqinter> \<Squnion>(Some ` Option.these B) = (\<Squnion>x\<in>Some ` Option.these B. Some c \<sqinter> x)"
haftmann@49190:         by (simp add: Some_SUP Some_Sup)
haftmann@49190:       with Some show ?thesis
haftmann@49190:         by (simp add: Some_image_these_eq Sup_B SUP_B Sup_None SUP_None SUP_union Sup_union_distrib)
haftmann@49190:     qed
haftmann@49190:   qed
haftmann@49190: qed
haftmann@49190: 
haftmann@49190: end
haftmann@49190: 
haftmann@49190: instantiation option :: (complete_linorder) complete_linorder
haftmann@49190: begin
haftmann@49190: 
haftmann@49190: instance ..
haftmann@49190: 
haftmann@49190: end
haftmann@49190: 
haftmann@49190: 
haftmann@49190: no_notation
haftmann@49190:   bot ("\<bottom>") and
haftmann@49190:   top ("\<top>") and
haftmann@49190:   inf  (infixl "\<sqinter>" 70) and
haftmann@49190:   sup  (infixl "\<squnion>" 65) and
haftmann@49190:   Inf  ("\<Sqinter>_" [900] 900) and
haftmann@49190:   Sup  ("\<Squnion>_" [900] 900)
haftmann@49190: 
haftmann@49190: no_syntax (xsymbols)
haftmann@49190:   "_INF1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Sqinter>_./ _)" [0, 10] 10)
haftmann@49190:   "_INF"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sqinter>_\<in>_./ _)" [0, 0, 10] 10)
haftmann@49190:   "_SUP1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Squnion>_./ _)" [0, 10] 10)
haftmann@49190:   "_SUP"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Squnion>_\<in>_./ _)" [0, 0, 10] 10)
haftmann@49190: 
haftmann@49190: end
haftmann@49190: 




