Theory UpperPD

theory UpperPD
imports Compact_Basis
(*  Title:      HOL/HOLCF/UpperPD.thy
Author: Brian Huffman
*)


header {* Upper powerdomain *}

theory UpperPD
imports Compact_Basis
begin

subsection {* Basis preorder *}

definition
upper_le :: "'a pd_basis => 'a pd_basis => bool" (infix "≤\<sharp>" 50) where
"upper_le = (λu v. ∀y∈Rep_pd_basis v. ∃x∈Rep_pd_basis u. x \<sqsubseteq> y)"

lemma upper_le_refl [simp]: "t ≤\<sharp> t"
unfolding upper_le_def by fast

lemma upper_le_trans: "[|t ≤\<sharp> u; u ≤\<sharp> v|] ==> t ≤\<sharp> v"
unfolding upper_le_def
apply (rule ballI)
apply (drule (1) bspec, erule bexE)
apply (drule (1) bspec, erule bexE)
apply (erule rev_bexI)
apply (erule (1) below_trans)
done

interpretation upper_le: preorder upper_le
by (rule preorder.intro, rule upper_le_refl, rule upper_le_trans)

lemma upper_le_minimal [simp]: "PDUnit compact_bot ≤\<sharp> t"
unfolding upper_le_def Rep_PDUnit by simp

lemma PDUnit_upper_mono: "x \<sqsubseteq> y ==> PDUnit x ≤\<sharp> PDUnit y"
unfolding upper_le_def Rep_PDUnit by simp

lemma PDPlus_upper_mono: "[|s ≤\<sharp> t; u ≤\<sharp> v|] ==> PDPlus s u ≤\<sharp> PDPlus t v"
unfolding upper_le_def Rep_PDPlus by fast

lemma PDPlus_upper_le: "PDPlus t u ≤\<sharp> t"
unfolding upper_le_def Rep_PDPlus by fast

lemma upper_le_PDUnit_PDUnit_iff [simp]:
"(PDUnit a ≤\<sharp> PDUnit b) = (a \<sqsubseteq> b)"
unfolding upper_le_def Rep_PDUnit by fast

lemma upper_le_PDPlus_PDUnit_iff:
"(PDPlus t u ≤\<sharp> PDUnit a) = (t ≤\<sharp> PDUnit a ∨ u ≤\<sharp> PDUnit a)"
unfolding upper_le_def Rep_PDPlus Rep_PDUnit by fast

lemma upper_le_PDPlus_iff: "(t ≤\<sharp> PDPlus u v) = (t ≤\<sharp> u ∧ t ≤\<sharp> v)"
unfolding upper_le_def Rep_PDPlus by fast

lemma upper_le_induct [induct set: upper_le]:
assumes le: "t ≤\<sharp> u"
assumes 1: "!!a b. a \<sqsubseteq> b ==> P (PDUnit a) (PDUnit b)"
assumes 2: "!!t u a. P t (PDUnit a) ==> P (PDPlus t u) (PDUnit a)"
assumes 3: "!!t u v. [|P t u; P t v|] ==> P t (PDPlus u v)"
shows "P t u"
using le apply (induct u arbitrary: t rule: pd_basis_induct)
apply (erule rev_mp)
apply (induct_tac t rule: pd_basis_induct)
apply (simp add: 1)
apply (simp add: upper_le_PDPlus_PDUnit_iff)
apply (simp add: 2)
apply (subst PDPlus_commute)
apply (simp add: 2)
apply (simp add: upper_le_PDPlus_iff 3)
done


subsection {* Type definition *}

typedef 'a upper_pd =
"{S::'a pd_basis set. upper_le.ideal S}"
by (rule upper_le.ex_ideal)

type_notation (xsymbols) upper_pd ("('(_')\<sharp>)")

instantiation upper_pd :: (bifinite) below
begin

definition
"x \<sqsubseteq> y <-> Rep_upper_pd x ⊆ Rep_upper_pd y"

instance ..
end

instance upper_pd :: (bifinite) po
using type_definition_upper_pd below_upper_pd_def
by (rule upper_le.typedef_ideal_po)

instance upper_pd :: (bifinite) cpo
using type_definition_upper_pd below_upper_pd_def
by (rule upper_le.typedef_ideal_cpo)

definition
upper_principal :: "'a pd_basis => 'a upper_pd" where
"upper_principal t = Abs_upper_pd {u. u ≤\<sharp> t}"

interpretation upper_pd:
ideal_completion upper_le upper_principal Rep_upper_pd
using type_definition_upper_pd below_upper_pd_def
using upper_principal_def pd_basis_countable
by (rule upper_le.typedef_ideal_completion)

text {* Upper powerdomain is pointed *}

lemma upper_pd_minimal: "upper_principal (PDUnit compact_bot) \<sqsubseteq> ys"
by (induct ys rule: upper_pd.principal_induct, simp, simp)

instance upper_pd :: (bifinite) pcpo
by intro_classes (fast intro: upper_pd_minimal)

lemma inst_upper_pd_pcpo: "⊥ = upper_principal (PDUnit compact_bot)"
by (rule upper_pd_minimal [THEN bottomI, symmetric])


subsection {* Monadic unit and plus *}

definition
upper_unit :: "'a -> 'a upper_pd" where
"upper_unit = compact_basis.extension (λa. upper_principal (PDUnit a))"

definition
upper_plus :: "'a upper_pd -> 'a upper_pd -> 'a upper_pd" where
"upper_plus = upper_pd.extension (λt. upper_pd.extension (λu.
upper_principal (PDPlus t u)))"


abbreviation
upper_add :: "'a upper_pd => 'a upper_pd => 'a upper_pd"
(infixl "∪\<sharp>" 65) where
"xs ∪\<sharp> ys == upper_plus·xs·ys"

syntax
"_upper_pd" :: "args => logic" ("{_}\<sharp>")

translations
"{x,xs}\<sharp>" == "{x}\<sharp> ∪\<sharp> {xs}\<sharp>"
"{x}\<sharp>" == "CONST upper_unit·x"

lemma upper_unit_Rep_compact_basis [simp]:
"{Rep_compact_basis a}\<sharp> = upper_principal (PDUnit a)"
unfolding upper_unit_def
by (simp add: compact_basis.extension_principal PDUnit_upper_mono)

lemma upper_plus_principal [simp]:
"upper_principal t ∪\<sharp> upper_principal u = upper_principal (PDPlus t u)"
unfolding upper_plus_def
by (simp add: upper_pd.extension_principal
upper_pd.extension_mono PDPlus_upper_mono)

interpretation upper_add: semilattice upper_add proof
fix xs ys zs :: "'a upper_pd"
show "(xs ∪\<sharp> ys) ∪\<sharp> zs = xs ∪\<sharp> (ys ∪\<sharp> zs)"
apply (induct xs rule: upper_pd.principal_induct, simp)
apply (induct ys rule: upper_pd.principal_induct, simp)
apply (induct zs rule: upper_pd.principal_induct, simp)
apply (simp add: PDPlus_assoc)
done
show "xs ∪\<sharp> ys = ys ∪\<sharp> xs"
apply (induct xs rule: upper_pd.principal_induct, simp)
apply (induct ys rule: upper_pd.principal_induct, simp)
apply (simp add: PDPlus_commute)
done
show "xs ∪\<sharp> xs = xs"
apply (induct xs rule: upper_pd.principal_induct, simp)
apply (simp add: PDPlus_absorb)
done
qed

lemmas upper_plus_assoc = upper_add.assoc
lemmas upper_plus_commute = upper_add.commute
lemmas upper_plus_absorb = upper_add.idem
lemmas upper_plus_left_commute = upper_add.left_commute
lemmas upper_plus_left_absorb = upper_add.left_idem

text {* Useful for @{text "simp add: upper_plus_ac"} *}
lemmas upper_plus_ac =
upper_plus_assoc upper_plus_commute upper_plus_left_commute

text {* Useful for @{text "simp only: upper_plus_aci"} *}
lemmas upper_plus_aci =
upper_plus_ac upper_plus_absorb upper_plus_left_absorb

lemma upper_plus_below1: "xs ∪\<sharp> ys \<sqsubseteq> xs"
apply (induct xs rule: upper_pd.principal_induct, simp)
apply (induct ys rule: upper_pd.principal_induct, simp)
apply (simp add: PDPlus_upper_le)
done

lemma upper_plus_below2: "xs ∪\<sharp> ys \<sqsubseteq> ys"
by (subst upper_plus_commute, rule upper_plus_below1)

lemma upper_plus_greatest: "[|xs \<sqsubseteq> ys; xs \<sqsubseteq> zs|] ==> xs \<sqsubseteq> ys ∪\<sharp> zs"
apply (subst upper_plus_absorb [of xs, symmetric])
apply (erule (1) monofun_cfun [OF monofun_cfun_arg])
done

lemma upper_below_plus_iff [simp]:
"xs \<sqsubseteq> ys ∪\<sharp> zs <-> xs \<sqsubseteq> ys ∧ xs \<sqsubseteq> zs"
apply safe
apply (erule below_trans [OF _ upper_plus_below1])
apply (erule below_trans [OF _ upper_plus_below2])
apply (erule (1) upper_plus_greatest)
done

lemma upper_plus_below_unit_iff [simp]:
"xs ∪\<sharp> ys \<sqsubseteq> {z}\<sharp> <-> xs \<sqsubseteq> {z}\<sharp> ∨ ys \<sqsubseteq> {z}\<sharp>"
apply (induct xs rule: upper_pd.principal_induct, simp)
apply (induct ys rule: upper_pd.principal_induct, simp)
apply (induct z rule: compact_basis.principal_induct, simp)
apply (simp add: upper_le_PDPlus_PDUnit_iff)
done

lemma upper_unit_below_iff [simp]: "{x}\<sharp> \<sqsubseteq> {y}\<sharp> <-> x \<sqsubseteq> y"
apply (induct x rule: compact_basis.principal_induct, simp)
apply (induct y rule: compact_basis.principal_induct, simp)
apply simp
done

lemmas upper_pd_below_simps =
upper_unit_below_iff
upper_below_plus_iff
upper_plus_below_unit_iff

lemma upper_unit_eq_iff [simp]: "{x}\<sharp> = {y}\<sharp> <-> x = y"
unfolding po_eq_conv by simp

lemma upper_unit_strict [simp]: "{⊥}\<sharp> = ⊥"
using upper_unit_Rep_compact_basis [of compact_bot]
by (simp add: inst_upper_pd_pcpo)

lemma upper_plus_strict1 [simp]: "⊥ ∪\<sharp> ys = ⊥"
by (rule bottomI, rule upper_plus_below1)

lemma upper_plus_strict2 [simp]: "xs ∪\<sharp> ⊥ = ⊥"
by (rule bottomI, rule upper_plus_below2)

lemma upper_unit_bottom_iff [simp]: "{x}\<sharp> = ⊥ <-> x = ⊥"
unfolding upper_unit_strict [symmetric] by (rule upper_unit_eq_iff)

lemma upper_plus_bottom_iff [simp]:
"xs ∪\<sharp> ys = ⊥ <-> xs = ⊥ ∨ ys = ⊥"
apply (induct xs rule: upper_pd.principal_induct, simp)
apply (induct ys rule: upper_pd.principal_induct, simp)
apply (simp add: inst_upper_pd_pcpo upper_pd.principal_eq_iff
upper_le_PDPlus_PDUnit_iff)
done

lemma compact_upper_unit: "compact x ==> compact {x}\<sharp>"
by (auto dest!: compact_basis.compact_imp_principal)

lemma compact_upper_unit_iff [simp]: "compact {x}\<sharp> <-> compact x"
apply (safe elim!: compact_upper_unit)
apply (simp only: compact_def upper_unit_below_iff [symmetric])
apply (erule adm_subst [OF cont_Rep_cfun2])
done

lemma compact_upper_plus [simp]:
"[|compact xs; compact ys|] ==> compact (xs ∪\<sharp> ys)"
by (auto dest!: upper_pd.compact_imp_principal)


subsection {* Induction rules *}

lemma upper_pd_induct1:
assumes P: "adm P"
assumes unit: "!!x. P {x}\<sharp>"
assumes insert: "!!x ys. [|P {x}\<sharp>; P ys|] ==> P ({x}\<sharp> ∪\<sharp> ys)"
shows "P (xs::'a upper_pd)"
apply (induct xs rule: upper_pd.principal_induct, rule P)
apply (induct_tac a rule: pd_basis_induct1)
apply (simp only: upper_unit_Rep_compact_basis [symmetric])
apply (rule unit)
apply (simp only: upper_unit_Rep_compact_basis [symmetric]
upper_plus_principal [symmetric])
apply (erule insert [OF unit])
done

lemma upper_pd_induct
[case_names adm upper_unit upper_plus, induct type: upper_pd]:
assumes P: "adm P"
assumes unit: "!!x. P {x}\<sharp>"
assumes plus: "!!xs ys. [|P xs; P ys|] ==> P (xs ∪\<sharp> ys)"
shows "P (xs::'a upper_pd)"
apply (induct xs rule: upper_pd.principal_induct, rule P)
apply (induct_tac a rule: pd_basis_induct)
apply (simp only: upper_unit_Rep_compact_basis [symmetric] unit)
apply (simp only: upper_plus_principal [symmetric] plus)
done


subsection {* Monadic bind *}

definition
upper_bind_basis ::
"'a pd_basis => ('a -> 'b upper_pd) -> 'b upper_pd" where
"upper_bind_basis = fold_pd
(λa. Λ f. f·(Rep_compact_basis a))
(λx y. Λ f. x·f ∪\<sharp> y·f)"


lemma ACI_upper_bind:
"semilattice (λx y. Λ f. x·f ∪\<sharp> y·f)"
apply unfold_locales
apply (simp add: upper_plus_assoc)
apply (simp add: upper_plus_commute)
apply (simp add: eta_cfun)
done

lemma upper_bind_basis_simps [simp]:
"upper_bind_basis (PDUnit a) =
(Λ f. f·(Rep_compact_basis a))"

"upper_bind_basis (PDPlus t u) =
(Λ f. upper_bind_basis t·f ∪\<sharp> upper_bind_basis u·f)"

unfolding upper_bind_basis_def
apply -
apply (rule fold_pd_PDUnit [OF ACI_upper_bind])
apply (rule fold_pd_PDPlus [OF ACI_upper_bind])
done

lemma upper_bind_basis_mono:
"t ≤\<sharp> u ==> upper_bind_basis t \<sqsubseteq> upper_bind_basis u"
unfolding cfun_below_iff
apply (erule upper_le_induct, safe)
apply (simp add: monofun_cfun)
apply (simp add: below_trans [OF upper_plus_below1])
apply simp
done

definition
upper_bind :: "'a upper_pd -> ('a -> 'b upper_pd) -> 'b upper_pd" where
"upper_bind = upper_pd.extension upper_bind_basis"

syntax
"_upper_bind" :: "[logic, logic, logic] => logic"
("(3\<Union>\<sharp>_∈_./ _)" [0, 0, 10] 10)

translations
"\<Union>\<sharp>x∈xs. e" == "CONST upper_bind·xs·(Λ x. e)"

lemma upper_bind_principal [simp]:
"upper_bind·(upper_principal t) = upper_bind_basis t"
unfolding upper_bind_def
apply (rule upper_pd.extension_principal)
apply (erule upper_bind_basis_mono)
done

lemma upper_bind_unit [simp]:
"upper_bind·{x}\<sharp>·f = f·x"
by (induct x rule: compact_basis.principal_induct, simp, simp)

lemma upper_bind_plus [simp]:
"upper_bind·(xs ∪\<sharp> ys)·f = upper_bind·xs·f ∪\<sharp> upper_bind·ys·f"
by (induct xs rule: upper_pd.principal_induct, simp,
induct ys rule: upper_pd.principal_induct, simp, simp)

lemma upper_bind_strict [simp]: "upper_bind·⊥·f = f·⊥"
unfolding upper_unit_strict [symmetric] by (rule upper_bind_unit)

lemma upper_bind_bind:
"upper_bind·(upper_bind·xs·f)·g = upper_bind·xs·(Λ x. upper_bind·(f·x)·g)"
by (induct xs, simp_all)


subsection {* Map *}

definition
upper_map :: "('a -> 'b) -> 'a upper_pd -> 'b upper_pd" where
"upper_map = (Λ f xs. upper_bind·xs·(Λ x. {f·x}\<sharp>))"

lemma upper_map_unit [simp]:
"upper_map·f·{x}\<sharp> = {f·x}\<sharp>"
unfolding upper_map_def by simp

lemma upper_map_plus [simp]:
"upper_map·f·(xs ∪\<sharp> ys) = upper_map·f·xs ∪\<sharp> upper_map·f·ys"
unfolding upper_map_def by simp

lemma upper_map_bottom [simp]: "upper_map·f·⊥ = {f·⊥}\<sharp>"
unfolding upper_map_def by simp

lemma upper_map_ident: "upper_map·(Λ x. x)·xs = xs"
by (induct xs rule: upper_pd_induct, simp_all)

lemma upper_map_ID: "upper_map·ID = ID"
by (simp add: cfun_eq_iff ID_def upper_map_ident)

lemma upper_map_map:
"upper_map·f·(upper_map·g·xs) = upper_map·(Λ x. f·(g·x))·xs"
by (induct xs rule: upper_pd_induct, simp_all)

lemma upper_bind_map:
"upper_bind·(upper_map·f·xs)·g = upper_bind·xs·(Λ x. g·(f·x))"
by (simp add: upper_map_def upper_bind_bind)

lemma upper_map_bind:
"upper_map·f·(upper_bind·xs·g) = upper_bind·xs·(Λ x. upper_map·f·(g·x))"
by (simp add: upper_map_def upper_bind_bind)

lemma ep_pair_upper_map: "ep_pair e p ==> ep_pair (upper_map·e) (upper_map·p)"
apply default
apply (induct_tac x rule: upper_pd_induct, simp_all add: ep_pair.e_inverse)
apply (induct_tac y rule: upper_pd_induct)
apply (simp_all add: ep_pair.e_p_below monofun_cfun del: upper_below_plus_iff)
done

lemma deflation_upper_map: "deflation d ==> deflation (upper_map·d)"
apply default
apply (induct_tac x rule: upper_pd_induct, simp_all add: deflation.idem)
apply (induct_tac x rule: upper_pd_induct)
apply (simp_all add: deflation.below monofun_cfun del: upper_below_plus_iff)
done

(* FIXME: long proof! *)
lemma finite_deflation_upper_map:
assumes "finite_deflation d" shows "finite_deflation (upper_map·d)"
proof (rule finite_deflation_intro)
interpret d: finite_deflation d by fact
have "deflation d" by fact
thus "deflation (upper_map·d)" by (rule deflation_upper_map)
have "finite (range (λx. d·x))" by (rule d.finite_range)
hence "finite (Rep_compact_basis -` range (λx. d·x))"
by (rule finite_vimageI, simp add: inj_on_def Rep_compact_basis_inject)
hence "finite (Pow (Rep_compact_basis -` range (λx. d·x)))" by simp
hence "finite (Rep_pd_basis -` (Pow (Rep_compact_basis -` range (λx. d·x))))"
by (rule finite_vimageI, simp add: inj_on_def Rep_pd_basis_inject)
hence *: "finite (upper_principal ` Rep_pd_basis -` (Pow (Rep_compact_basis -` range (λx. d·x))))" by simp
hence "finite (range (λxs. upper_map·d·xs))"
apply (rule rev_finite_subset)
apply clarsimp
apply (induct_tac xs rule: upper_pd.principal_induct)
apply (simp add: adm_mem_finite *)
apply (rename_tac t, induct_tac t rule: pd_basis_induct)
apply (simp only: upper_unit_Rep_compact_basis [symmetric] upper_map_unit)
apply simp
apply (subgoal_tac "∃b. d·(Rep_compact_basis a) = Rep_compact_basis b")
apply clarsimp
apply (rule imageI)
apply (rule vimageI2)
apply (simp add: Rep_PDUnit)
apply (rule range_eqI)
apply (erule sym)
apply (rule exI)
apply (rule Abs_compact_basis_inverse [symmetric])
apply (simp add: d.compact)
apply (simp only: upper_plus_principal [symmetric] upper_map_plus)
apply clarsimp
apply (rule imageI)
apply (rule vimageI2)
apply (simp add: Rep_PDPlus)
done
thus "finite {xs. upper_map·d·xs = xs}"
by (rule finite_range_imp_finite_fixes)
qed

subsection {* Upper powerdomain is bifinite *}

lemma approx_chain_upper_map:
assumes "approx_chain a"
shows "approx_chain (λi. upper_map·(a i))"
using assms unfolding approx_chain_def
by (simp add: lub_APP upper_map_ID finite_deflation_upper_map)

instance upper_pd :: (bifinite) bifinite
proof
show "∃(a::nat => 'a upper_pd -> 'a upper_pd). approx_chain a"
using bifinite [where 'a='a]
by (fast intro!: approx_chain_upper_map)
qed

subsection {* Join *}

definition
upper_join :: "'a upper_pd upper_pd -> 'a upper_pd" where
"upper_join = (Λ xss. upper_bind·xss·(Λ xs. xs))"

lemma upper_join_unit [simp]:
"upper_join·{xs}\<sharp> = xs"
unfolding upper_join_def by simp

lemma upper_join_plus [simp]:
"upper_join·(xss ∪\<sharp> yss) = upper_join·xss ∪\<sharp> upper_join·yss"
unfolding upper_join_def by simp

lemma upper_join_bottom [simp]: "upper_join·⊥ = ⊥"
unfolding upper_join_def by simp

lemma upper_join_map_unit:
"upper_join·(upper_map·upper_unit·xs) = xs"
by (induct xs rule: upper_pd_induct, simp_all)

lemma upper_join_map_join:
"upper_join·(upper_map·upper_join·xsss) = upper_join·(upper_join·xsss)"
by (induct xsss rule: upper_pd_induct, simp_all)

lemma upper_join_map_map:
"upper_join·(upper_map·(upper_map·f)·xss) =
upper_map·f·(upper_join·xss)"

by (induct xss rule: upper_pd_induct, simp_all)

end