Theory Syntactic_Description

theory Syntactic_Description
imports Place_Framework Proper_Venn_Regions "HereditarilyFinite.Rank"
begin

locale satisfiable_normalized_MLSS_clause = ATOM Isa +
  normalized_MLSS_clause π’ž +
  proper_Venn_regions V π’œ
  for π’ž :: "'a pset_fm set" and π’œ :: "'a β‡’ hf" +
  assumes π’œ_sat_π’ž: "βˆ€lt ∈ π’ž. I π’œ lt"
begin

―‹The collection of the non-empty proper regions of the Venn diagram of V under π’œβ€Ί

definition Ξ£ :: "hf set" where
  "Ξ£ ≑ proper_Venn_regions.proper_Venn_region V π’œ ` (P+ V) - {0}"
declare Ξ£_def [simp]

lemma mem_Ξ£_not_empty: "Οƒ ∈ Ξ£ ⟹ Οƒ β‰  0" by simp

fun associated_place :: "hf β‡’ ('a β‡’ bool)"  (β€ΉΟ€β‡˜_⇙›) where
  "Ο€β‡˜Οƒβ‡™ x ⟷ x ∈ V ∧ Οƒ ≀ π’œ x"

definition PI :: "('a β‡’ bool) set" where
  "PI ≑ associated_place ` Ξ£"
declare PI_def [simp]

fun containing_region :: "'a β‡’ hf" (β€ΉΟƒβ‡˜_⇙›) where
  "Οƒβ‡˜x⇙ = HF {π’œ x}"

lemma containing_region_in_Ξ£:
  assumes "x ∈ W"
    shows "Οƒβ‡˜x⇙ ∈ Ξ£"
proof -
  from β€Ήx ∈ Wβ€Ί obtain y where y: "AT (Var y =s Single (Var x)) ∈ π’ž"
    using memW_E by blast
  then have "y ∈ V" by fastforce

  from y π’œ_sat_π’ž have "π’œ y = HF {π’œ x}" by auto
  then have "π’œ y = Οƒβ‡˜x⇙" by simp
  moreover
  from variable_as_composition_of_proper_Venn_regions β€Ήy ∈ Vβ€Ί have "π’œ y = ⨆HF (proper_Venn_region ` β„’ V y)"
    by presburger
  ultimately
  have "Οƒβ‡˜x⇙ = ⨆HF (proper_Venn_region ` β„’ V y)" by argo
  then have "βˆƒΞ± ∈ P+ V. π’œ y = proper_Venn_region Ξ±"
  proof -
    let ?l = "{Ξ± ∈ β„’ V y. proper_Venn_region Ξ± β‰  0}"

    have "?l β‰  {}"
    proof (rule ccontr)
      assume "Β¬ ?l β‰  {}"
      then have "⨆HF (proper_Venn_region ` β„’ V y) = 0"
        using HUnion_empty[where ?f = proper_Venn_region and ?S = "β„’ V y"]
        by blast
      with β€Ήπ’œ y = HF {π’œ x}β€Ί β€Ήπ’œ y = ⨆HF (proper_Venn_region ` β„’ V y)β€Ί show False
        by (metis Int_lower1 equals0I finite_Int finite_V finite_imageI finite_insert hemptyE hmem_HF_iff insert_not_empty subset_empty) 
    qed
    then obtain α where "α ∈ ?l" by blast
    moreover
    have "β = α" if "β ∈ ?l" for β
    proof (rule ccontr)
      assume "Ξ² β‰  Ξ±"

      from β€ΉΞ± ∈ ?lβ€Ί β€Ήy ∈ Vβ€Ί
      have "proper_Venn_region Ξ± ≀ π’œ y"
        by (metis (mono_tags, lifting) β„’.elims mem_Collect_eq mem_P_plus_subset proper_Venn_region_subset_variable_iff)
      with β€ΉΞ± ∈ ?lβ€Ί have "proper_Venn_region Ξ± = π’œ y"
        using β€Ήπ’œ y = HF {π’œ x}β€Ί
        using singleton_nonempty_subset[where ?s = "π’œ y" and ?c = "π’œ x" and ?t = "proper_Venn_region Ξ±"]
        by fastforce

      from β€ΉΞ² ∈ ?lβ€Ί β€Ήy ∈ Vβ€Ί
      have "proper_Venn_region Ξ² ≀ π’œ y"
        by (metis (mono_tags, lifting) β„’.elims mem_Collect_eq mem_P_plus_subset proper_Venn_region_subset_variable_iff)
      with β€ΉΞ² ∈ ?lβ€Ί have "proper_Venn_region Ξ² = π’œ y"
        using β€Ήπ’œ y = HF {π’œ x}β€Ί
        using singleton_nonempty_subset[where ?s = "π’œ y" and ?c = "π’œ x" and ?t = "proper_Venn_region Ξ²"]
        by fastforce

      from β€ΉΞ² β‰  Ξ±β€Ί β€Ήproper_Venn_region Ξ± = π’œ yβ€Ί β€Ήproper_Venn_region Ξ² = π’œ yβ€Ί β€Ήπ’œ y = HF {π’œ x}β€Ί β€Ήx ∈ Wβ€Ί
      show False using proper_Venn_region_strongly_injective[where ?Ξ± = Ξ± and ?Ξ² = Ξ²]
        using calculation that by auto
    qed
    ultimately
    have "?l = {Ξ±}" by blast

    have Ly_minus_l: "β„’ V y - ?l = {Ξ± ∈ β„’ V y. proper_Venn_region Ξ± = 0}" by auto

    have "?l βŠ† β„’ V y" by blast
    then have "?l βˆͺ (β„’ V y - ?l) = β„’ V y" by blast
    then have "⨆HF (proper_Venn_region ` β„’ V y) = ⨆HF (proper_Venn_region ` (?l βˆͺ (β„’ V y - ?l)))" by argo
    also have "... = ⨆HF (proper_Venn_region ` (?l βˆͺ {Ξ± ∈ β„’ V y. proper_Venn_region Ξ± = 0}))"
      using Ly_minus_l by argo
    also have "... = ⨆HF (proper_Venn_region ` ?l) βŠ” ⨆HF (proper_Venn_region ` {Ξ± ∈ β„’ V y. proper_Venn_region Ξ± = 0})"
      using HUnion_proper_Venn_region_union[where ?l = ?l and ?m = "{Ξ± ∈ β„’ V y. proper_Venn_region Ξ± = 0}"]
      using finite_V β€Ήy ∈ Vβ€Ί
      by auto
    also have "... = ⨆HF (proper_Venn_region ` ?l)"
      using finite_V β€Ήy ∈ Vβ€Ί by auto
    also have "... = ⨆HF (proper_Venn_region ` {Ξ±})"
      using β€Ή?l = {Ξ±}β€Ί by argo
    also have "... = proper_Venn_region Ξ±" by fastforce
    finally have "⨆HF (proper_Venn_region ` β„’ V y) = proper_Venn_region Ξ±" .

    with β€Ήπ’œ y = ⨆HF (proper_Venn_region ` (β„’ V y))β€Ί β€ΉΞ± ∈ ?lβ€Ί show ?thesis by auto
  qed
  moreover
  from β€Ήπ’œ y = HF {π’œ x}β€Ί β€Ήπ’œ y = Οƒβ‡˜x⇙› have "Οƒβ‡˜x⇙ β‰  0"
    by (metis finite.emptyI finite_insert hfset_0 hfset_HF insert_not_empty)
  ultimately
  show ?thesis
    by (metis Diff_iff Ξ£_def β€Ήπ’œ y = Οƒβ‡˜x⇙› empty_iff imageI insert_iff)
qed

fun atp_f :: "'a β‡’ ('a β‡’ bool)" where
  "atp_f w = Ο€β‡˜Οƒβ‡˜w⇙⇙"

lemma range_atp_f: "w ∈ W ⟹ atp_f w ∈ PI"
  using atp_f.simps containing_region_in_Ξ£ PI_def by blast

lemma Ο€_Οƒ_Ξ±:
  assumes "Ο€ ∈ PI"
    shows "βˆƒΞ± ∈ P+ V. Ο€ = (Ξ»x. x ∈ Ξ±) ∧ Ο€ = Ο€β‡˜proper_Venn_region α⇙ ∧ proper_Venn_region Ξ± β‰  0"
proof -
  from β€ΉΟ€ ∈ PIβ€Ί obtain Ξ± where Ξ±:
    "Ο€ = Ο€β‡˜proper_Venn_region α⇙" "proper_Venn_region Ξ± β‰  0" "Ξ± ∈ P+ V"
    by auto
  have "Ο€ x ⟷ x ∈ Ξ±" for x
  proof (cases "x ∈ V")
    case True
    with Ξ± proper_Venn_region_subset_variable_iff
    have "x ∈ Ξ± ⟷ proper_Venn_region Ξ± ≀ π’œ x" by simp
    with β€ΉΟ€ = Ο€β‡˜proper_Venn_region α⇙› True
    show ?thesis by simp
  next
    case False
    with β€ΉΟ€ = Ο€β‡˜(proper_Venn_region Ξ±)⇙› have "Β¬ Ο€ x" by auto
    from False β€ΉΞ± ∈ P+ Vβ€Ί have "x βˆ‰ Ξ±" by auto
    from β€ΉΒ¬ Ο€ xβ€Ί β€Ήx βˆ‰ Ξ±β€Ί show ?thesis by blast
  qed
  with Ξ± show ?thesis by blast
qed

lemma Ο€_Οƒ_Ξ±':
  assumes "Οƒ ∈ Ξ£"
  shows "βˆƒΞ± ∈ P+ V. Ο€β‡˜Οƒβ‡™ = (Ξ»x. x ∈ Ξ±) ∧ Οƒ = proper_Venn_region Ξ±"
proof -
  from β€ΉΟƒ ∈ Ξ£β€Ί obtain Ξ± where Ξ±: "Ξ± ∈ P+ V" "Οƒ = proper_Venn_region Ξ±" "proper_Venn_region Ξ± β‰  0"
    by auto
  have "Ο€β‡˜Οƒβ‡™ x ⟷ x ∈ Ξ±" for x
  proof (cases "x ∈ V")
    case True
    with Ξ± proper_Venn_region_subset_variable_iff
    have "x ∈ Ξ± ⟷ proper_Venn_region Ξ± ≀ π’œ x" by simp
    with β€ΉΟƒ = proper_Venn_region Ξ±β€Ί True
    show ?thesis by simp
  next
    case False
    with β€ΉΟƒ = proper_Venn_region Ξ±β€Ί have "Β¬ Ο€β‡˜Οƒβ‡™ x" by auto
    from False β€ΉΞ± ∈ P+ Vβ€Ί have "x βˆ‰ Ξ±" by auto
    from β€ΉΒ¬ Ο€β‡˜Οƒβ‡™ xβ€Ί β€Ήx βˆ‰ Ξ±β€Ί show ?thesis by blast
  qed
  with Ξ± show ?thesis by blast
qed

lemma realise_same_implies_eq_under_all_Ο€:
  assumes "x ∈ V"
      and "y ∈ V"
      and "π’œ x = π’œ y"
      and "Ο€ ∈ PI"
    shows "Ο€ x ⟷ Ο€ y"
  by (metis Ο€_Οƒ_Ξ± assms associated_place.elims(2) associated_place.elims(3))

definition "rel_PI ≑ place_membership π’ž PI"
declare rel_PI_def [simp]

definition "G_PI ≑ place_mem_graph π’ž PI"
declare G_PI_def [simp]

lemma arcs_G_PI[simp]: "arcs G_PI = rel_PI" by simp
lemma arcs_ends_G_PI[simp]: "arcs_ends G_PI = rel_PI"
  unfolding arcs_ends_def arc_to_ends_def by auto
lemma verts_G_PI[simp]: "verts G_PI = PI" by simp

lemma rel_PI_head_tail:
  assumes "e ∈ rel_PI"
    shows "e = (tail G_PI e, head G_PI e)"
proof -
  from assms arcs_ends_G_PI have "e ∈ arcs_ends G_PI" 
    by blast
  then show ?thesis
    unfolding arcs_ends_def arc_to_ends_def
    by simp
qed

lemma place_membership_irreflexive: "(Ο€, Ο€) βˆ‰ rel_PI"
proof (rule ccontr)
  assume "Β¬ (Ο€, Ο€) βˆ‰ rel_PI"
  then have "(Ο€, Ο€) ∈ rel_PI" by blast
  then obtain x y where xy: "AT (Var x =s Single (Var y)) ∈ π’ž" "Ο€ x" "Ο€ y" by auto
  with π’œ_sat_π’ž have "π’œ x = HF {π’œ y}" by fastforce
  then have "π’œ x βŠ“ π’œ y = 0" by (simp add: hmem_not_refl)

  from β€Ή(Ο€, Ο€) ∈ rel_PIβ€Ί have "Ο€ ∈ PI" by auto
  then obtain Οƒ where Οƒ: "Ο€ = Ο€β‡˜Οƒβ‡™" "Οƒ ∈ Ξ£" by auto
  then obtain Ξ± where Ξ±: "Οƒ = proper_Venn_region Ξ±" "Ξ± ∈ P+ V" by auto
  with Οƒ xy β€Ήπ’œ x βŠ“ π’œ y = 0β€Ί
  show False
    by (metis Ξ£_def DiffD2 associated_place.elims(2) insertCI le_inf_iff less_eq_hempty)
qed

interpretation pre_digraph G_PI .

lemma rel_PI_implies_rank_lt:
  assumes Ο€_lt: "(Ο€β‡˜Οƒβ‡™, Ο€β‡˜Οƒ'⇙) ∈ rel_PI"
      and "Οƒ ∈ Ξ£"
      and "Οƒ' ∈ Ξ£"
    shows "rank Οƒ < rank Οƒ'"
proof -
  from β€ΉΟƒ' ∈ Ξ£β€Ί have "Οƒ' β‰  0" by auto

  from Ο€_lt obtain x y where xy: "AT (Var x =s Single (Var y)) ∈ π’ž"
                         and "Ο€β‡˜Οƒβ‡™ y" and "Ο€β‡˜Οƒ'⇙ x"
    by auto
  with π’œ_sat_π’ž have "π’œ x = HF {π’œ y}" by auto
  with β€ΉΟ€β‡˜Οƒ'⇙ xβ€Ί β€ΉΟƒ' β‰  0β€Ί have "Οƒ' = π’œ x"
    by (metis associated_place.simps singleton_nonempty_subset)
  then have "rank Οƒ' = rank (π’œ x)" by blast
  moreover
  from β€ΉΟ€β‡˜Οƒβ‡™ yβ€Ί have "Οƒ ≀ π’œ y" by simp
  then have "rank Οƒ ≀ rank (π’œ y)"
    by (simp add: rank_mono) 
  moreover
  from β€Ήπ’œ x = HF {π’œ y}β€Ί have "rank (π’œ y) < rank (π’œ x)"
    by (simp add: rank_lt)
  ultimately
  show ?thesis by order
qed

lemma awalk_in_G_PI_rank_lt:
  assumes "awalk Ο€β‡˜Οƒβ‡™ p Ο€β‡˜Οƒ'⇙"
      and "Οƒ β‰  Οƒ'"
      and "Οƒ ∈ Ξ£"
      and "Οƒ' ∈ Ξ£"
    shows "rank Οƒ < rank Οƒ'"
  using assms unfolding awalk_def
proof (induction "Ο€β‡˜Οƒβ‡™" p "Ο€β‡˜Οƒ'⇙" arbitrary: Οƒ Οƒ' rule: cas.induct)
  case 1
  then have "Ο€β‡˜Οƒβ‡™ = Ο€β‡˜Οƒ'⇙" using cas.simps(1) by blast
  then have "Οƒ = Οƒ'"
    by (smt (verit) "1.prems"(3) "1.prems"(4) Collect_mem_eq Ο€_Οƒ_Ξ±')
  with β€ΉΟƒ β‰  Οƒ'β€Ί have False by blast
  then show ?case ..
next
  case (2 e es)
  then have e_es_in_arcs: "set (e # es) βŠ† arcs G_PI"
                 and cas_e_es: "cas Ο€β‡˜Οƒβ‡™ (e # es) Ο€β‡˜Οƒ'⇙"
    by argo+
  from e_es_in_arcs have "e ∈ rel_PI"
    by (metis arcs_G_PI in_mono list.set_intros(1))
  then obtain Ο€1 Ο€2 where "e = (Ο€1, Ο€2)" "Ο€1 ∈ PI" "Ο€2 ∈ PI" by auto
  then have e_Ο€12: "Ο€1 = tail G_PI e" "Ο€2 = head G_PI e" by simp+
  with cas_e_es have "Ο€1 = Ο€β‡˜Οƒβ‡™" by (meson pre_digraph.cas.simps(2))
  from β€ΉΟ€2 ∈ PIβ€Ί obtain Οƒ'' where "Οƒ'' ∈ Ξ£" "Ο€2 = Ο€β‡˜Οƒ''⇙" by auto
  with cas_e_es e_Ο€12 have "cas Ο€β‡˜Οƒ''⇙ es Ο€β‡˜Οƒ'⇙"
    by (metis cas.simps(2))

  from β€ΉΟ€1 = Ο€β‡˜Οƒβ‡™β€Ί β€ΉΟ€2 = Ο€β‡˜Οƒ''⇙› β€Ήe = (Ο€1, Ο€2)β€Ί have "e = (Ο€β‡˜Οƒβ‡™, Ο€β‡˜Οƒ''⇙)"
    by simp
  with β€Ήe ∈ rel_PIβ€Ί rel_PI_implies_rank_lt have "rank Οƒ < rank Οƒ''"
    using β€ΉΟƒ ∈ Ξ£β€Ί β€ΉΟƒ'' ∈ Ξ£β€Ί by blast

  show ?case
  proof (cases "Οƒ'' = Οƒ'")
    case True
    with β€Ήrank Οƒ < rank Οƒ''β€Ί show ?thesis by blast
  next
    case False
    with "2.hyps" have "rank Οƒ'' < rank Οƒ'"
      using "2.prems"(1) "2.prems"(4) β€ΉΟ€2 = Ο€β‡˜Οƒ''⇙› β€ΉΟ€2 ∈ PIβ€Ί β€ΉΟƒ'' ∈ Ξ£β€Ί β€Ήcas Ο€β‡˜Οƒ''⇙ es Ο€β‡˜Οƒ'⇙› e_Ο€12(2)
      by (metis insert_subset list.simps(15) verts_G_PI)
    with β€Ήrank Οƒ < rank Οƒ''β€Ί show ?thesis by order
  qed
qed

lemma all_places_same_implies_assignment_equal:
  assumes "x ∈ V"
      and "y ∈ V"
      and Ο€_eq: "βˆ€Ο€ ∈ PI. Ο€ x ⟷ Ο€ y"
    shows "π’œ x = π’œ y"
proof -
  let ?β„’_x' = "{Ξ± ∈ β„’ V x. proper_Venn_region Ξ± β‰  0}"
  let ?β„’_y' = "{Ξ± ∈ β„’ V y. proper_Venn_region Ξ± β‰  0}"

  have proper_Venn_region_β„’_x': "proper_Venn_region ` ?β„’_x' = {s ∈ proper_Venn_region ` β„’ V x. s β‰  0}"
    by fast
  have proper_Venn_region_β„’_y': "proper_Venn_region ` ?β„’_y' = {s ∈ proper_Venn_region ` β„’ V y. s β‰  0}"
    by fast

  from finite_V have "finite (β„’ V x)" by blast
  then have "finite (proper_Venn_region ` β„’ V x)" by blast
  from finite_V have "finite (β„’ V y)" by blast
  then have "finite (proper_Venn_region ` β„’ V y)" by blast

  have Ο€_eq': "Ο€β‡˜proper_Venn_region α⇙ x = Ο€β‡˜proper_Venn_region α⇙ y"
    if "Ξ± ∈ ?β„’_x' ∨ Ξ± ∈ ?β„’_y'" for Ξ±
  proof -
    from that have "x ∈ α ∨ y ∈ α" by fastforce
    from that have "α ∈ P+ V" by fastforce
    with that have "Ο€β‡˜proper_Venn_region α⇙ ∈ PI" by fastforce
    with Ο€_eq show ?thesis by fast
  qed

  have "?β„’_x' = ?β„’_y'"
  proof (standard; standard)
    fix Ξ± assume "Ξ± ∈ ?β„’_x'"
    then have "x ∈ α" by fastforce
    with β€ΉΞ± ∈ ?β„’_x'β€Ί β€Ήx ∈ Vβ€Ί have "Ο€β‡˜proper_Venn_region α⇙ x"
      using proper_Venn_region_subset_variable_iff by auto
    with Ο€_eq' β€ΉΞ± ∈ ?β„’_x'β€Ί have "Ο€β‡˜proper_Venn_region α⇙ y" by blast
    with β€ΉΞ± ∈ ?β„’_x'β€Ί β€Ήy ∈ Vβ€Ί have "y ∈ Ξ±"
      using proper_Venn_region_subset_variable_iff by auto
    with β€ΉΞ± ∈ ?β„’_x'β€Ί show "Ξ± ∈ ?β„’_y'" by fastforce
  next
    fix Ξ± assume "Ξ± ∈ ?β„’_y'"
    then have "y ∈ α" by fastforce
    with β€ΉΞ± ∈ ?β„’_y'β€Ί β€Ήy ∈ Vβ€Ί have "Ο€β‡˜proper_Venn_region α⇙ y"
      using proper_Venn_region_subset_variable_iff by auto
    with Ο€_eq' β€ΉΞ± ∈ ?β„’_y'β€Ί have "Ο€β‡˜proper_Venn_region α⇙ x" by blast
    with β€ΉΞ± ∈ ?β„’_y'β€Ί β€Ήx ∈ Vβ€Ί have "x ∈ Ξ±"
      using proper_Venn_region_subset_variable_iff by auto
    with β€ΉΞ± ∈ ?β„’_y'β€Ί show "Ξ± ∈ ?β„’_x'" by fastforce
  qed

  from β€Ήx ∈ Vβ€Ί have "π’œ x = ⨆HF (proper_Venn_region ` β„’ V x)"
    using variable_as_composition_of_proper_Venn_regions by presburger
  also have "... = ⨆HF (proper_Venn_region ` ?β„’_x')"
    using Hunion_nonempty[where ?S = "proper_Venn_region ` β„’ V x"]
    using β€Ήfinite (proper_Venn_region ` β„’ V x)β€Ί proper_Venn_region_β„’_x'
    by argo
  also have "... = ⨆HF (proper_Venn_region ` ?β„’_y')"
    using β€Ή?β„’_x' = ?β„’_y'β€Ί by argo
  also have "... = ⨆HF (proper_Venn_region ` β„’ V y)"
    using Hunion_nonempty[where ?S = "proper_Venn_region ` β„’ V y"]
    using β€Ήfinite (proper_Venn_region ` β„’ V y)β€Ί proper_Venn_region_β„’_y'
    by argo
  also have "... = π’œ y"
    using β€Ήy ∈ Vβ€Ί variable_as_composition_of_proper_Venn_regions
    by presburger
  finally show "π’œ x = π’œ y" .
qed

definition "atp ≑ {(y, atp_f y)|y. y ∈ W}"
declare atp_def [simp]

(* Lemma 27 *)
theorem syntactic_description_is_adequate:
  "adequate_place_framework π’ž PI atp"
proof (unfold_locales, goal_cases)
  case 1
  then show ?case
  proof
    fix Ο€ assume "Ο€ ∈ PI"
    then obtain Οƒ where "Ο€ = Ο€β‡˜Οƒβ‡™" "Οƒ ∈ Ξ£" by auto
    then obtain Ξ± where "Οƒ = proper_Venn_region Ξ±" "Ξ± ∈ P+ V" by auto
    with β€ΉΟƒ ∈ Ξ£β€Ί have "Οƒ β‰  0" "proper_Venn_region Ξ± β‰  0" by auto
    from β€ΉΞ± ∈ P+ Vβ€Ί have "Ξ± βŠ† V" by simp

    from proper_Venn_region_subset_variable_iff
    have Ξ±_Οƒ: "x ∈ Ξ± ⟷ Οƒ ≀ π’œ x" if "x ∈ V" for x
      using β€ΉΞ± βŠ† Vβ€Ί β€Ήx ∈ Vβ€Ί β€Ήproper_Venn_region Ξ± β‰  0β€Ί β€ΉΟƒ = proper_Venn_region Ξ±β€Ί
      by presburger

    from β€ΉΟ€ = Ο€β‡˜Οƒβ‡™β€Ί β€ΉΟƒ = proper_Venn_region Ξ±β€Ί have "Ο€ = (Ξ»x. x ∈ V ∧ proper_Venn_region Ξ± ≀ π’œ x)"
      by (meson associated_place.elims(2) associated_place.elims(3))
    also have "... = (λx. x ∈ α)"
      using Ξ±_Οƒ β€ΉΞ± βŠ† Vβ€Ί β€ΉΟƒ = proper_Venn_region Ξ±β€Ί by (metis rev_contra_hsubsetD)
    also have "... ∈ places V"
      unfolding places_def using β€ΉΞ± βŠ† Vβ€Ί by blast
    finally show "Ο€ ∈ places V" .
  qed
next
  case 2
  then show ?case using atp_f.simps by auto
next
  case 3
  then show ?case using range_atp_f by fastforce
next
  case 4
  then show ?case by (simp add: single_valued_def)
next
  case (5 Ο€)
  from place_membership_irreflexive show ?case by auto
next
  case (6 e)
  obtain Ο€ Ο€' where "e = (Ο€, Ο€')" by fastforce
  with 6 have "Ο€ ∈ PI" by simp
  with β€Ήe = (Ο€, Ο€')β€Ί show ?case by simp
next
  case (7 e)
  obtain Ο€ Ο€' where "e = (Ο€, Ο€')" by fastforce
  with 7 have "Ο€' ∈ PI" by simp
  with β€Ήe = (Ο€, Ο€')β€Ί show ?case by simp
next
  case 8
  from finite_V have "finite Ξ£" by auto
  then show ?case by auto
next
  case 9
  from finite_V have "finite PI" by auto
  then have "finite (PI Γ— PI)" by auto
  moreover
  have "rel_PI βŠ† PI Γ— PI" by auto
  ultimately
  have "finite rel_PI" by (simp add: finite_subset) 
  then show ?case by auto
next
  case (10 e)
  then have "e ∈ rel_PI" by force
  with place_membership_irreflexive have "fst e β‰  snd e"
    by (metis prod.collapse)
  with rel_PI_head_tail show ?case by auto
next
  case (11 e1 e2)
  then show ?case
    unfolding arc_to_ends_def by simp
next
  case 12
  show ?case
  proof (rule ccontr)
    assume "Β¬(βˆ„c. pre_digraph.cycle (place_mem_graph π’ž PI) c)"
    then obtain c where "cycle c"
      by fastforce
    then obtain e es Ο€ where e_es: "c = e # es" "awalk Ο€ (e # es) Ο€"
      by (metis awalk_def cas.elims(2) cycle_def)
    then obtain Ο€' where "e = (Ο€, Ο€')"
      by (metis arcs_G_PI awalk_def cas.simps(2) insert_subset list.simps(15) rel_PI_head_tail) 
    then show False
    proof (cases "es = []")
      case True
      with e_es have "e = (Ο€, Ο€)"
        by (metis arcs_G_PI awalk_def cas.simps(1) cas.simps(2) list.set_intros(1) rel_PI_head_tail subset_code(1))
      then have "(Ο€, Ο€) ∈ rel_PI"
        by (metis arcs_G_PI e_es(2) insert_subset list.simps(15) pre_digraph.awalk_def)
      with place_membership_irreflexive show False by blast
    next
      case False
      with e_es β€Ήcycle cβ€Ί β€Ήe = (Ο€, Ο€')β€Ί have "Ο€' β‰  Ο€"
        by (metis arcs_G_PI awalk_def list.set_intros(1) place_membership_irreflexive subset_code(1))
      from e_es β€Ήe = (Ο€, Ο€')β€Ί have "Ο€ ∈ PI" "Ο€' ∈ PI"
        unfolding awalk_def by simp+
      then obtain Οƒ Οƒ' where Οƒ_Οƒ': "Ο€ = Ο€β‡˜Οƒβ‡™" "Ο€' = Ο€β‡˜Οƒ'⇙" "Οƒ ∈ Ξ£" "Οƒ' ∈ Ξ£" by auto
      from e_es β€Ήe = (Ο€, Ο€')β€Ί have "awalk Ο€' es Ο€"
        unfolding awalk_def
        by (metis Pair_inject β€ΉΟ€' ∈ PIβ€Ί arcs_G_PI cas.simps(2) insert_subset list.simps(15) rel_PI_head_tail verts_G_PI)
      with awalk_in_G_PI_rank_lt have "rank Οƒ' < rank Οƒ"
        using β€ΉΟ€' β‰  Ο€β€Ί Οƒ_Οƒ' by blast
      moreover
      from rel_PI_implies_rank_lt have "rank Οƒ < rank Οƒ'"
        using β€Ήe = (Ο€, Ο€')β€Ί Οƒ_Οƒ'
        by (metis e_es arcs_G_PI e_es(1) list.set_intros(1) pre_digraph.awalk_def subset_code(1))
      ultimately
      show False by simp
    qed
  qed
next
  case (13 x)
  with π’œ_sat_π’ž have "π’œ x = 0" by fastforce
  from 13 have "x ∈ V" by force
  have "Β¬ Ο€ x" if "Ο€ ∈ PI" for Ο€
  proof -
    from that obtain Οƒ where "Οƒ ∈ Ξ£" "Ο€ = Ο€β‡˜Οƒβ‡™" by auto
    then have "Ο€ x ⟷ x ∈ V ∧ Οƒ ≀ π’œ x" "Οƒ β‰  0" by simp+
    with β€Ήx ∈ Vβ€Ί have "Ο€ x ⟷ Οƒ ≀ π’œ x" by argo
    with β€ΉΟƒ β‰  0β€Ί β€Ήπ’œ x = 0β€Ί show "Β¬ Ο€ x" by simp
  qed
  then show ?case unfolding PI_def by blast
next
  case (14 x y)
  with π’œ_sat_π’ž have "π’œ x = π’œ y" by fastforce
  from β€ΉAT (Var x =s Var y) ∈ π’žβ€Ί have "x ∈ V" "y ∈ V" by force+
  have "Ο€ x = Ο€ y" if "Ο€ ∈ PI" for Ο€
  proof -
    from that obtain Οƒ where "Οƒ ∈ Ξ£" "Ο€ = Ο€β‡˜Οƒβ‡™" by auto
    then have "Ο€ x ⟷ x ∈ V ∧ Οƒ ≀ π’œ x" "Ο€ y ⟷ y ∈ V ∧ Οƒ ≀ π’œ y" by simp+
    with β€Ήx ∈ Vβ€Ί β€Ήy ∈ Vβ€Ί have "Ο€ x ⟷ Οƒ ≀ π’œ x" "Ο€ y ⟷ Οƒ ≀ π’œ y" by argo+
    with β€Ήπ’œ x = π’œ yβ€Ί show "Ο€ x = Ο€ y" by simp
  qed
  then show ?case unfolding PI_def by blast
next
  case (15 x y z)
  with π’œ_sat_π’ž have "π’œ x = π’œ y βŠ” π’œ z" by fastforce
  from 15 have "x ∈ V" "y ∈ V" "z ∈ V" by force+
  have "Ο€ x ⟷ Ο€ y ∨ Ο€ z" if "Ο€ ∈ PI" for Ο€
  proof -
    from that Ο€_Οƒ_Ξ± obtain Ξ± where Ξ±:
      "Ξ± ∈ P+ V" "Ο€ = Ο€β‡˜proper_Venn_region α⇙" "Ο€ = (Ξ»v. v ∈ Ξ±)" "proper_Venn_region Ξ± β‰  0"
      by blast
    have "Ο€ y ∨ Ο€ z" if "Ο€ x"
    proof -
      from β€ΉΟ€ xβ€Ί Ξ± have "x ∈ Ξ±" by metis
      from β€ΉΟ€ xβ€Ί Ξ± have "proper_Venn_region Ξ± ≀ π’œ x" by simp
      with β€Ήproper_Venn_region Ξ± β‰  0β€Ί have "π’œ x β‰  0" by fastforce
      {assume "y βˆ‰ Ξ±" "z βˆ‰ Ξ±"
        from β€Ήy βˆ‰ Ξ±β€Ί β€Ήy ∈ Vβ€Ί have "π’œ y βŠ“ proper_Venn_region Ξ± = 0"
          using finite_V by fastforce
        moreover
        from β€Ήz βˆ‰ Ξ±β€Ί β€Ήz ∈ Vβ€Ί have "π’œ z βŠ“ proper_Venn_region Ξ± = 0"
          using finite_V by fastforce
        ultimately
        have "π’œ x βŠ“ proper_Venn_region Ξ± = 0"
          using β€Ήπ’œ x = π’œ y βŠ” π’œ zβ€Ί by simp
        from β€Ήx ∈ Ξ±β€Ί have "proper_Venn_region Ξ± ≀ π’œ x"
          using Ξ± by (meson associated_place.elims(2))
        with β€Ήπ’œ x β‰  0β€Ί β€Ήπ’œ x βŠ“ proper_Venn_region Ξ± = 0β€Ί
        have "proper_Venn_region Ξ± = 0" by blast
        with β€Ήproper_Venn_region Ξ± β‰  0β€Ί have False by blast}
      then have "y ∈ α ∨ z ∈ α" by blast
      then show "Ο€ y ∨ Ο€ z" by (simp add: β€ΉΟ€ = (Ξ»v. v ∈ Ξ±)β€Ί) 
    qed
    have "Ο€ x" if "Ο€ y ∨ Ο€ z"
    proof -
      from β€ΉΟ€ y ∨ Ο€ zβ€Ί Ξ± have "proper_Venn_region Ξ± ≀ π’œ y ∨ proper_Venn_region Ξ± ≀ π’œ z"
        by auto
      with β€Ήπ’œ x = π’œ y βŠ” π’œ zβ€Ί have "proper_Venn_region Ξ± ≀ π’œ x" by auto
      with Ξ± β€Ήx ∈ Vβ€Ί show "Ο€ x" by simp
    qed
    from β€ΉΟ€ x ⟹ Ο€ y ∨ Ο€ zβ€Ί β€ΉΟ€ y ∨ Ο€ z ⟹ Ο€ xβ€Ί 
    show ?thesis by blast
  qed
  then show ?case unfolding PI_def by blast
next
  case (16 x y z)
  with π’œ_sat_π’ž have "π’œ x = π’œ y - π’œ z" by fastforce
  from β€ΉAT (Var x =s Var y -s Var z) ∈ π’žβ€Ί have "x ∈ V" "y ∈ V" "z ∈ V" by force+
  have "Ο€ x ⟷ Ο€ y ∧ Β¬ Ο€ z" if "Ο€ ∈ PI" for Ο€
  proof -
    from that Ο€_Οƒ_Ξ± obtain Ξ± where Ξ±:
      "Ξ± ∈ P+ V" "Ο€ = Ο€β‡˜proper_Venn_region α⇙" "Ο€ = (Ξ»v. v ∈ Ξ±)" "proper_Venn_region Ξ± β‰  0"
      by blast
    with finite_V have "finite Ξ±" by blast
    have "Ο€ x" if "Ο€ y" "Β¬ Ο€ z"
    proof -
      have "proper_Venn_region Ξ± ≀ π’œ y"
        by (metis Ξ±(2) associated_place.elims(2) β€ΉΟ€ yβ€Ί)
      moreover
      from β€ΉΒ¬ Ο€ zβ€Ί β€Ήz ∈ Vβ€Ί have "z ∈ V - Ξ±"
        using Ξ± by (metis DiffI) 
      then have "π’œ z ≀ ⨆HF (π’œ ` (V - Ξ±))"
        by (meson finite_Diff finite_V finite_imageI image_eqI mem_hsubset_HUnion)
      then have "π’œ z βŠ“ proper_Venn_region Ξ± = 0"
        by (simp add: hinter_hdiff_hempty)
      ultimately
      have "proper_Venn_region Ξ± ≀ π’œ y - π’œ z" by blast
      with β€Ήπ’œ x = π’œ y - π’œ zβ€Ί have "proper_Venn_region Ξ± ≀ π’œ x" by argo
      with Ξ± β€Ήx ∈ Vβ€Ί show ?thesis by auto
    qed
    have "Ο€ y ∧ Β¬ Ο€ z" if "Ο€ x"
    proof -
      from β€ΉΟ€ xβ€Ί Ξ± have "proper_Venn_region Ξ± ≀ π’œ x"
        by auto
      with β€Ήπ’œ x = π’œ y - π’œ zβ€Ί have "proper_Venn_region Ξ± ≀ π’œ y" "Β¬ proper_Venn_region Ξ± ≀ π’œ z"
        apply simp_all
         apply auto[1]
        by (metis (no_types, lifting) Ξ±(4) hdiff_iff hdisjoint_iff inf.orderE proper_Venn_region.elims rev_hsubsetD)
      with Ξ± β€Ήy ∈ Vβ€Ί β€Ήz ∈ Vβ€Ί show "Ο€ y ∧ Β¬ Ο€ z" by simp
    qed
    from β€ΉΟ€ x ⟹ Ο€ y ∧ Β¬ Ο€ zβ€Ί β€ΉΟ€ y ⟹ Β¬ Ο€ z ⟹ Ο€ xβ€Ί 
    show ?thesis by blast
  qed
  then show ?case unfolding PI_def by blast
next
  case (17 x y)
  with π’œ_sat_π’ž have "π’œ x β‰  π’œ y" by fastforce
  from 17 have "x ∈ V" "y ∈ V" by fastforce+
  with β€Ήπ’œ x β‰  π’œ yβ€Ί all_places_same_implies_assignment_equal
  show ?case by metis
next
  case (18 x y)
  then have "y ∈ W" by fastforce
  from 18 have "x ∈ V" "y ∈ V" by fastforce+
  from 18 π’œ_sat_π’ž have "π’œ x = HF{π’œ y}" by fastforce
  then have "Οƒβ‡˜y⇙ ≀ π’œ x" by simp
  with β€Ήx ∈ Vβ€Ί have "atp_f y x" by (simp only: atp_f.simps) simp
  moreover
  {fix Ο€ assume "Ο€ ∈ PI" "Ο€ x"
    then obtain Οƒ where "Ο€ = Ο€β‡˜Οƒβ‡™" "Οƒ ≀ π’œ x" "Οƒ β‰  0" by fastforce
    with β€Ήπ’œ x = HF{π’œ y}β€Ί have "Οƒ = HF{π’œ y}" by fastforce
    with β€ΉΟ€ = Ο€β‡˜Οƒβ‡™β€Ί have "Ο€ = atp_f y" by simp
  }
  then have "βˆ€Ο€'∈PI. Ο€' β‰  atp_f y ⟢ Β¬ Ο€' x"
    by blast
  ultimately
  show ?case using β€Ήy ∈ Wβ€Ί by simp
next
  case (19 y z)
  then obtain Ο€ where "Ο€ = atp_f z" "Ο€ = atp_f y" by simp
  then have "atp_f z = atp_f y" by blast
  from β€Ήy ∈ Wβ€Ί obtain x where x: "AT (Var x =s Single (Var y)) ∈ π’ž"
    using memW_E by blast
  with π’œ_sat_π’ž have "π’œ x = HF{π’œ y}" by fastforce
  from 19 x have "x ∈ V" "y ∈ V" by fastforce+
  from 19 have "z ∈ V" using W_subset_V by blast

  from β€Ήπ’œ x = HF{π’œ y}β€Ί have "Οƒβ‡˜y⇙ ≀ π’œ x" by simp
  with β€Ήx ∈ Vβ€Ί have "atp_f y x" by auto
  with β€Ήatp_f z = atp_f yβ€Ί have "atp_f z x" by argo
  then have "Οƒβ‡˜z⇙ ≀ π’œ x" by simp
  with β€Ήπ’œ x = HF{π’œ y}β€Ί have "Οƒβ‡˜z⇙ = HF{π’œ y}" by force
  then have "π’œ z = π’œ y"
    by (metis finite.emptyI finite_insert hfset_HF containing_region.elims singleton_insert_inj_eq)

  {fix Ο€ assume "Ο€ ∈ PI"
    then obtain Οƒ where "Ο€ = Ο€β‡˜Οƒβ‡™" "Οƒ β‰  0" by auto
    with β€Ήy ∈ Vβ€Ί have "Ο€ y ⟷ Οƒ ≀ π’œ y" by fastforce
    also have "... ⟷ Οƒ ≀ π’œ z"
      using β€Ήπ’œ z = π’œ yβ€Ί by auto
    also have "... ⟷ Ο€ z"
      using β€ΉΟ€ = Ο€β‡˜Οƒβ‡™β€Ί β€Ήz ∈ Vβ€Ί by simp
    finally have "Ο€ y = Ο€ z" .
  }
  then show ?case by blast
next
  case (20 y y')
  then have "y ∈ V" "y' ∈ V" by (metis W_subset_V subsetD)+
  with β€Ήβˆ€Ο€βˆˆPI. Ο€ y' = Ο€ yβ€Ί all_places_same_implies_assignment_equal
  have "π’œ y = π’œ y'" by presburger
  then have "atp_f y = atp_f y'" by simp
  with β€Ήy ∈ Wβ€Ί β€Ήy' ∈ Wβ€Ί show ?case by simp
next
  case (21 Ο€1 Ο€2)
  have "Ο€ = Ο€β‡˜HF {0}⇙" if "Ο€ ∈ Range atp - Range (place_membership π’ž PI)" for Ο€
  proof -
    from that obtain y where y: "y ∈ W" "atp_f y = Ο€"
    by (simp only: atp_def) blast
    with range_atp_f have "Ο€ ∈ PI" by blast
    from β€Ήy ∈ Wβ€Ί obtain x where lt_in_π’ž:
      "AT (Var x =s Single (Var y)) ∈ π’ž"
      using memW_E by presburger
    with π’œ_sat_π’ž have "π’œ x = HF {π’œ y}" by fastforce
    then have "Οƒβ‡˜y⇙ ≀ π’œ x" by simp
    with lt_in_π’ž have "atp_f y x" by force
    with β€Ήatp_f y = Ο€β€Ί have "Ο€ x" by blast
  
    have "βˆ€Ο€ ∈ PI. Β¬ Ο€ y"
    proof (rule ccontr)
      assume "Β¬ (βˆ€Ο€βˆˆPI. Β¬ Ο€ y)"
      then obtain Ο€' where "Ο€' ∈ PI" "Ο€' y" by blast
      with β€ΉAT (Var x =s Single (Var y)) ∈ π’žβ€Ί β€ΉΟ€ xβ€Ί
      have "(Ο€', Ο€) ∈ place_membership π’ž PI"
        using β€ΉΟ€ ∈ PIβ€Ί
        by (simp only: place_membership.simps) blast
      then have "Ο€ ∈ Range (place_membership π’ž PI)" by blast
      with that show False by blast
    qed
    have "βˆ€Ξ± ∈ β„’ V y. proper_Venn_region Ξ± = 0"
    proof (rule ccontr)
      assume "Β¬ (βˆ€Ξ± ∈ β„’ V y. proper_Venn_region Ξ± = 0)"
      then obtain Ξ± where Ξ±: "Ξ± ∈ β„’ V y" "proper_Venn_region Ξ± β‰  0" by blast
      then have "proper_Venn_region Ξ± ≀ π’œ y"
        using variable_as_composition_of_proper_Venn_regions β„’_finite finite_V
        by (smt (verit, ccfv_threshold) W_subset_V finite_imageI image_eqI mem_hsubset_HUnion subset_iff y(1))
      then have "Ο€β‡˜proper_Venn_region α⇙ y"
        using W_subset_V β€Ήy ∈ Wβ€Ί by auto
      with β€Ήβˆ€Ο€ ∈ PI. Β¬ Ο€ yβ€Ί show False
        using Ξ± by auto
    qed
    then have "⨆HF (proper_Venn_region ` β„’ V y) = 0"
      by fastforce
    with variable_as_composition_of_proper_Venn_regions[of y]
    have "π’œ y = 0"
      using β€Ήy ∈ Wβ€Ί W_subset_V by auto
    with β€Ήπ’œ x = HF {π’œ y}β€Ί have "π’œ x = HF {0}" by argo

    from β€ΉΟ€ ∈ PIβ€Ί obtain Οƒ where "Οƒ ∈ Ξ£" "Ο€ = Ο€β‡˜Οƒβ‡™" by auto
    with β€ΉΟ€ xβ€Ί have "Οƒ β‰  0" "Οƒ ≀ π’œ x" by simp+
    with β€Ήπ’œ x = HF {0}β€Ί have "Οƒ = HF {0}" by fastforce
    with β€ΉΟ€ = Ο€β‡˜Οƒβ‡™β€Ί show "Ο€ = Ο€β‡˜HF {0}⇙" by blast
  qed
  with 21 show ?case by blast
qed

end

end