Theory MLSSmf_to_MLSS_Soundness

theory MLSSmf_to_MLSS_Soundness
  imports MLSSmf_to_MLSS MLSSmf_Semantics Proper_Venn_Regions MLSSmf_HF_Extras
begin

locale satisfiable_normalized_MLSSmf_clause =
  normalized_MLSSmf_clause š’ž for š’ž :: "('v, 'f) MLSSmf_clause" +
    fixes Mv :: "'v ⇒ hf"
      and Mf :: "'f ⇒ hf ⇒ hf"
  assumes model_for_š’ž: "Icl Mv Mf š’ž"
begin

interpretation proper_Venn_regions V Mv
  using finite_V by unfold_locales

function ℳ :: "('v, 'f) Composite ⇒ hf" where
  "ℳ (Solo x)  = Mv x"
| "ℳ (vā‡˜Ī±ā‡™)    = proper_Venn_region α"
| "ℳ (UnionOfVennRegions xss) = ⨆HF ((ℳ ∘ VennRegion) ` set xss)"
| "ℳ (wā‡˜fā‡™ā‡˜l⇙) = (Mf f) (ℳ (UnionOfVennRegions (var_set_set_to_var_set_list l)))"
| "ℳ (UnionOfVars xs) = ⨆HF (Mv ` set xs)"
| "ℳ (InterOfVars xs) = ⨅HF (Mv ` set xs)"
| "ℳ (MemAux x) = HF {Mv x}"
| "ℳ (InterOfWAux f l m) = ℳ wā‡˜fā‡™ā‡˜l⇙ - ℳ wā‡˜fā‡™ā‡˜m⇙"
| "ℳ (InterOfVarsAux xs) = Mv (hd xs) - ℳ (InterOfVars (tl xs))"
  by pat_completeness auto
termination
  apply (relation "measure (λcomp. case comp of
                    InterOfVarsAux _ ⇒ Suc 0
                  | UnionOfVennRegions _ ⇒ Suc 0
                  | wā‡˜_ā‡™ā‡˜_⇙ ⇒ Suc (Suc 0)
                  | InterOfWAux _ _ _ ⇒ Suc (Suc (Suc 0))
                  | _ ⇒ 0)")
       apply auto
  done

lemma soundness_restriction_on_InterOfVars:
  assumes "set xs ∈ P+ V"
    shows "āˆ€a ∈ restriction_on_InterOfVars xs. Isa ℳ a"
proof (induction xs rule: restriction_on_InterOfVars.induct)
  case (2 x)
  {fix a assume "a ∈ restriction_on_InterOfVars [x]"
    then have "a = Var (InterOfVars [x]) =s Var (Solo x)" by simp
    then have "Isa ℳ a" by (simp add: HInter_singleton)
  }
  then show ?case by blast
next
  case (3 y x xs)
  {fix a assume "a ∈ restriction_on_InterOfVars (y # x # xs) - restriction_on_InterOfVars (x # xs)"
    then consider "a = Var (InterOfVarsAux (y # x # xs)) =s Var (Solo y) -s Var (InterOfVars (x # xs))"
                | "a = Var (InterOfVars (y # x # xs)) =s Var (Solo y) -s Var (InterOfVarsAux (y # x # xs))"
      by fastforce
    then have "Isa ℳ a"
    proof (cases)
      case 1
      then show ?thesis by simp
    next
      case 2
      have "⨅HF (insert (Mv y) (insert (Mv x) (Mv ` set xs))) =
            ⨅ (HF ((insert (Mv x) (Mv ` set xs))) ā—ƒ Mv y)"
        using HF_insert_hinsert by auto
      also have "... = Mv y āŠ“ ⨅HF (insert (Mv x) (Mv ` set xs))"
        by (simp add: HF_nonempty)
      also have "... = Mv y - (Mv y - ⨅HF (insert (Mv x) (Mv ` set xs)))"
        by blast
      finally show ?thesis using 2 by simp
    qed
  }
  with "3.IH" show ?case by blast
qed simp

lemma soundness_restriction_on_UnionOfVars:
  assumes "set xs ∈ Pow V"
    shows "āˆ€a ∈ restriction_on_UnionOfVars xs. Isa ℳ a"
proof (induction xs rule: restriction_on_UnionOfVars.induct)
  case 1
  then show ?case by auto
next
  case (2 x xs)
  {fix a assume "a ∈ restriction_on_UnionOfVars (x # xs) - restriction_on_UnionOfVars xs"
    then have a: "a = Var (UnionOfVars (x # xs)) =s Var (Solo x) āŠ”s Var (UnionOfVars xs)"
      by fastforce
    have "⨆HF (insert (Mv x) (Mv ` set xs)) = ⨆ (HF (Mv ` set xs) ā—ƒ Mv x)"
      by (simp add: HF_insert_hinsert)
    also have "... = Mv x āŠ” ⨆HF (Mv ` set xs)" by auto
    finally have "Isa ℳ a"
      using a by simp
  }
  with "2.IH" show ?case by blast
qed

lemma soundness_introduce_v:
  "āˆ€fml ∈ introduce_v. interp Isa ℳ fml"
proof -
  {fix α assume "α ∈ P+ V"
    have "ℳ vā‡˜Ī±ā‡™ = ⨅HF (Mv ` α) - ⨆HF (Mv ` (V - α))"
      by simp
    also have "... = ⨅HF ((ℳ ∘ Solo) ` α) - ⨆HF ((ℳ ∘ Solo) ` (V - α))"
      by simp
    finally have "Isa ℳ (restriction_on_v α)"
      apply (simp add: set_V_list)
      using ‹α ∈ P+ V›
      by (metis Int_def inf.absorb2 mem_P_plus_subset set_diff_eq)
  }
  then have "āˆ€Ī± ∈ P+ V. interp Isa ℳ (AT (restriction_on_v α))"
    by simp
  moreover
  from soundness_restriction_on_InterOfVars
  have "āˆ€a ∈ (restriction_on_InterOfVars ∘ var_set_to_list) α. Isa ℳ a" if "α ∈ P+ V" for α
    by (metis comp_apply mem_P_plus_subset set_var_set_to_list that)
  then have "āˆ€lt ∈ AT ` ā‹ƒ ((restriction_on_InterOfVars ∘ var_set_to_list) ` P+ V). interp Isa ℳ lt"
    by fastforce
  moreover
  from soundness_restriction_on_UnionOfVars
  have "āˆ€a ∈ (restriction_on_UnionOfVars ∘ var_set_to_list) α. Isa ℳ a" if "α ∈ Pow V" for α
    by (metis Pow_iff comp_apply set_var_set_to_list that)
  then have "āˆ€lt ∈ AT ` ā‹ƒ ((restriction_on_UnionOfVars ∘ var_set_to_list) ` Pow V). interp Isa ℳ lt"
    by fastforce
  ultimately
  show ?thesis
    unfolding introduce_v_def by blast
qed

lemma soundness_restriction_on_UnionOfVennRegions:
  assumes "set αs ∈ Pow (Pow V)"
    shows "āˆ€a ∈ restriction_on_UnionOfVennRegions αs. Isa ℳ a"
proof (induction αs rule: restriction_on_UnionOfVennRegions.induct)
  case 1
  then show ?case by auto
next
  case (2 α αs)
  {fix a assume "a ∈ restriction_on_UnionOfVennRegions (α # αs) - restriction_on_UnionOfVennRegions αs"
    then have a: "a = Var (UnionOfVennRegions (α # αs)) =s Var vā‡˜Ī±ā‡™ āŠ”s Var (UnionOfVennRegions αs)"
      by fastforce
    have "⨆HF ((ℳ ∘ VennRegion) ` set (α # αs)) = ⨆HF (insert (ℳ vā‡˜Ī±ā‡™) ((ℳ ∘ VennRegion) ` set αs))"
      by simp
    also have "... = ⨆ (HF ((ℳ ∘ VennRegion) ` set αs) ā—ƒ ℳ vā‡˜Ī±ā‡™)"
      by (simp add: HF_insert_hinsert)
    also have "... = ℳ vā‡˜Ī±ā‡™ āŠ” ⨆HF ((ℳ ∘ VennRegion) ` set αs)"
      by blast
    finally have "Isa ℳ a" using a by simp
  }
  with "2.IH" show ?case by blast
qed

lemma soundness_introduce_UnionOfVennRegions:
  "āˆ€lt ∈ introduce_UnionOfVennRegions. interp Isa ℳ lt"
proof
  fix lt assume "lt ∈ introduce_UnionOfVennRegions"
  then obtain αs where "αs ∈ set all_V_set_lists" "lt ∈ AT ` restriction_on_UnionOfVennRegions αs"
    unfolding introduce_UnionOfVennRegions_def by blast
  with soundness_restriction_on_UnionOfVennRegions
  show "interp Isa ℳ lt"
    using set_all_V_set_lists by fastforce
qed

lemma soundness_restriction_on_FunOfUnionOfVennRegions:
  assumes l'_l: "l' = var_set_set_to_var_set_list l"
      and m'_m: "m' = var_set_set_to_var_set_list m"
    shows "∃lt ∈ set (restriction_on_FunOfUnionOfVennRegions l' m' f). interp Isa ℳ lt"
proof (cases "ℳ (UnionOfVennRegions l') = ℳ (UnionOfVennRegions m')")
  case True
  then have "ℳ wā‡˜fā‡™ā‡˜l⇙ = ℳ wā‡˜fā‡™ā‡˜m⇙"
    using l'_l m'_m by auto
  then have "interp Isa ℳ (AT (Var wā‡˜fā‡™ā‡˜set l'⇙ =s Var wā‡˜fā‡™ā‡˜set m'⇙))"
    using l'_l m'_m by auto
  then show ?thesis by simp
next
  case False
  then have "interp Isa ℳ (AF (Var (UnionOfVennRegions l') =s Var (UnionOfVennRegions m')))"
    by fastforce
  then show ?thesis by simp
qed

lemma soundness_introduce_w:
  "∃clause ∈ introduce_w. āˆ€lt ∈ clause. interp Isa ℳ lt"
proof -
  let ?f = "Ī»lts. if interp Isa ℳ (lts ! 0) then lts ! 0 else lts ! 1"
  let ?g = "Ī»(l, m, f). restriction_on_FunOfUnionOfVennRegions l m f"
  let ?xs = "List.product all_V_set_lists (List.product all_V_set_lists F_list)"
  have "āˆ€(l', m', f) ∈ set ?xs. ?f (?g (l', m', f)) ∈ set (?g (l', m', f))"
    by fastforce
  with valid_choice[where ?f = ?f and ?g = ?g and ?xs = ?xs]
  have "map ?f (map ?g ?xs) ∈ set (choices_from_lists (map ?g ?xs))"
    by fast
  then have "set (map ?f (map ?g ?xs)) ∈ introduce_w"
    unfolding introduce_w_def
    using mem_set_map[where ?x = "map ?f (map ?g ?xs)" and ?f = set]
    by blast
  moreover
  have "{x ∈ set V_set_list. x ∈ set l'} = set l'" if "l' ∈ set all_V_set_lists" for l'
    using that set_V_set_list set_all_V_set_lists by auto
  then have "interp Isa ℳ (?f (restriction_on_FunOfUnionOfVennRegions l' m' f))"
    if "l' ∈ set all_V_set_lists" "m' ∈ set all_V_set_lists" for l' m' f
    using that by auto
  then have "āˆ€lt ∈ set (map ?f (map ?g ?xs)). interp Isa ℳ lt"
    by force
  ultimately
  show ?thesis by blast
qed

lemma soundness_reduce_literal:
  assumes "lt ∈ set š’ž"
    shows "āˆ€fml ∈ reduce_literal lt. interp Isa ℳ fml"
proof -
  from norm_š’ž ‹lt ∈ set š’žā€ŗ have "norm_literal lt" by auto
  then show ?thesis
  proof (cases rule: norm_literal.cases)
    case (inc f)
    show ?thesis
    proof
      fix fml assume "fml ∈ reduce_literal lt"
      then have "fml ∈ reduce_literal (ATm (inc(f)))"
        using inc by blast
      then obtain l m where lm: "l āŠ† P+ V" "m āŠ† P+ V" "l āŠ† m"
                        and fml: "fml = AT (Var wā‡˜fā‡™ā‡˜m⇙ =s Var wā‡˜fā‡™ā‡˜m⇙ āŠ”s Var wā‡˜fā‡™ā‡˜l⇙)"
        by auto
      from model_for_š’ž ‹lt ∈ set š’žā€ŗ inc have "Ia Mv Mf (inc(f))" by fastforce
      then have "āˆ€s t. s ≤ t ⟶ (Mf f) s ≤ (Mf f) t" by simp
      moreover
      from lm have "⨆HF ((ℳ ∘ VennRegion) ` l) ≤ ⨆HF ((ℳ ∘ VennRegion) ` m)"
        by (metis HUnion_proper_Venn_region_inter ℳ.simps(2) comp_apply image_cong inf.absorb_iff2)
      ultimately
      have "Mf f (⨆HF ((ℳ ∘ VennRegion) ` l)) ≤ Mf f (⨆HF ((ℳ ∘ VennRegion) ` m))"
        by blast
      then have "Mf f (⨆HF ((ℳ ∘ VennRegion) ` m)) =
                 Mf f (⨆HF ((ℳ ∘ VennRegion) ` m)) āŠ” Mf f (⨆HF ((ℳ ∘ VennRegion) ` l))"
        by blast
      with fml lm show "interp Isa ℳ fml"
        by (auto simp only: interp.simps Isa.simps Ist.simps ℳ.simps set_var_set_set_to_var_set_list)
    qed
  next
    case (dec f)
    show ?thesis
    proof
      fix fml assume "fml ∈ reduce_literal lt"
      then have "fml ∈ reduce_literal (ATm (dec(f)))"
        using dec by blast
      then obtain l m where lm: "l āŠ† P+ V" "m āŠ† P+ V" "l āŠ† m"
                        and fml: "fml = AT (Var wā‡˜fā‡™ā‡˜l⇙ =s Var wā‡˜fā‡™ā‡˜l⇙ āŠ”s Var wā‡˜fā‡™ā‡˜m⇙)"
        by auto
      from model_for_š’ž ‹lt ∈ set š’žā€ŗ dec have "Ia Mv Mf (dec(f))" by fastforce
      then have "āˆ€s t. s ≤ t ⟶ (Mf f) t ≤ (Mf f) s" by simp
      moreover
      from lm have "⨆HF ((ℳ ∘ VennRegion) ` l) ≤ ⨆HF ((ℳ ∘ VennRegion) ` m)"
        by (metis HUnion_proper_Venn_region_inter ℳ.simps(2) comp_apply image_cong inf.absorb_iff2)
      ultimately
      have "Mf f (⨆HF ((ℳ ∘ VennRegion) ` m)) ≤ Mf f (⨆HF ((ℳ ∘ VennRegion) ` l))"
        by blast
      then have "Mf f (⨆HF ((ℳ ∘ VennRegion) ` l)) =
                 Mf f (⨆HF ((ℳ ∘ VennRegion) ` l)) āŠ” Mf f (⨆HF ((ℳ ∘ VennRegion) ` m))"
        by blast
      with fml lm show "interp Isa ℳ fml"
        by (auto simp only: interp.simps Isa.simps Ist.simps ℳ.simps set_var_set_set_to_var_set_list)
    qed
  next
    case (add f)
    show ?thesis
    proof
      fix fml assume "fml ∈ reduce_literal lt"
      then have "fml ∈ reduce_literal (ATm (add(f)))"
        using add by blast
      then obtain l m where lm: "l āŠ† P+ V" "m āŠ† P+ V"
                        and fml: "fml = AT (Var wā‡˜fā‡™ā‡˜l∪m⇙ =s Var wā‡˜fā‡™ā‡˜l⇙ āŠ”s Var wā‡˜fā‡™ā‡˜m⇙)"
        by auto
      from model_for_š’ž ‹lt ∈ set š’žā€ŗ add have "Ia Mv Mf (add(f))" by fastforce
      then have "āˆ€s t. (Mf f) (s āŠ” t) = (Mf f) s āŠ” (Mf f) t" by simp
      moreover
      have "⨆HF ((ℳ ∘ VennRegion) ` (l ∪ m)) = ⨆HF ((ℳ ∘ VennRegion) ` l) āŠ” ⨆HF ((ℳ ∘ VennRegion) ` m)"
        using HUnion_proper_Venn_region_union ℳ.simps(2) lm(1) lm(2) by auto
      ultimately
      have "Mf f (⨆HF ((ℳ ∘ VennRegion) ` (l ∪ m))) =
            Mf f (⨆HF ((ℳ ∘ VennRegion) ` l)) āŠ” Mf f (⨆HF ((ℳ ∘ VennRegion) ` m))"
        by auto
      with fml lm show "interp Isa ℳ fml"
        using set_var_set_set_to_var_set_list
        apply (simp only: interp.simps Isa.simps Ist.simps ℳ.simps)
        by (metis le_sup_iff)
    qed
  next
    case (mul f)
    with model_for_š’ž ‹lt ∈ set š’žā€ŗ have "Ia Mv Mf (mul(f))" by fastforce
    then have f_mul: "āˆ€s t. (Mf f) (s āŠ“ t) = (Mf f) s āŠ“ (Mf f) t" by simp
    have InterOfWAux: "Isa ℳ (Var (InterOfWAux f l m) =s Var wā‡˜fā‡™ā‡˜l⇙ -s Var wā‡˜fā‡™ā‡˜m⇙)" for l m
      by auto
    {fix l m assume "l āŠ† P+ V" "m āŠ† P+ V"
      then have "⨆HF ((ℳ ∘ VennRegion) ` (l ∩ m)) = ⨆HF ((ℳ ∘ VennRegion) ` l) āŠ“ ⨆HF ((ℳ ∘ VennRegion) ` m)"
        using HUnion_proper_Venn_region_inter by force
      then have "ℳ (UnionOfVennRegions (var_set_set_to_var_set_list (l ∩ m))) =
                 ℳ (UnionOfVennRegions (var_set_set_to_var_set_list l)) āŠ“
                 ℳ (UnionOfVennRegions (var_set_set_to_var_set_list m))"
        using set_var_set_set_to_var_set_list ‹l āŠ† P+ V› ‹m āŠ† P+ V›
        by (metis ℳ.simps(3) inf.absorb_iff2 inf_left_commute)
      with f_mul have "ℳ wā‡˜fā‡™ā‡˜l∩m⇙ = ℳ wā‡˜fā‡™ā‡˜l⇙ āŠ“ ℳ wā‡˜fā‡™ā‡˜m⇙"
        by auto
      moreover
      from InterOfWAux have "ℳ (InterOfWAux f l m) = ℳ wā‡˜fā‡™ā‡˜l⇙ - ℳ wā‡˜fā‡™ā‡˜m⇙"
        by simp
      ultimately
      have "ℳ wā‡˜fā‡™ā‡˜l∩m⇙ = ℳ wā‡˜fā‡™ā‡˜l⇙ - ℳ (InterOfWAux f l m)"
        by auto
      then have "Isa ℳ (Var wā‡˜fā‡™ā‡˜l∩m⇙ =s Var wā‡˜fā‡™ā‡˜l⇙ -s Var (InterOfWAux f l m))"
        by auto
    }
    with InterOfWAux show ?thesis
      using mul by auto
  next
    case (le f g)
    show ?thesis
    proof
      fix fml assume "fml ∈ reduce_literal lt"
      then have "fml ∈ reduce_literal (ATm (f ≼m g))"
        using le by blast
      then obtain l where l: "l āŠ† P+ V"
                      and fml: "fml = AT (Var wā‡˜gā‡™ā‡˜l⇙ =s Var wā‡˜gā‡™ā‡˜l⇙ āŠ”s Var wā‡˜fā‡™ā‡˜l⇙)"
        by auto
      from model_for_š’ž ‹lt ∈ set š’žā€ŗ le have "Ia Mv Mf (f ≼m g)" by fastforce
      then have "āˆ€s. (Mf f) s ≤ (Mf g) s" by simp
      then have "Mf f (⨆HF ((ℳ ∘ VennRegion) ` l)) ≤ Mf g (⨆HF ((ℳ ∘ VennRegion) ` l))"
        by auto
      with fml l show "interp Isa ℳ fml"
        using set_var_set_set_to_var_set_list
        by (auto simp only: interp.simps Isa.simps Ist.simps ℳ.simps)
    qed
  next
    case (eq_empty x n)
    with ‹lt ∈ set š’žā€ŗ model_for_š’ž have "Mv x = 0" by auto
    show ?thesis
    proof
      fix fml assume "fml ∈ reduce_literal lt"
      with eq_empty have "fml = AT (Var (Solo x) =s āˆ… n)"
        by simp
      with ‹Mv x = 0› show "interp Isa ℳ fml" by auto
    qed
  next
    case (eq x y)
    with ‹lt ∈ set š’žā€ŗ model_for_š’ž have "Mv x = Mv y" by auto
    show ?thesis
    proof
      fix fml assume "fml ∈ reduce_literal lt"
      with eq have "fml = AT (Var (Solo x) =s Var (Solo y))"
        by simp
      with ‹Mv x = Mv y› show "interp Isa ℳ fml" by auto
    qed
  next
    case (neq x y)
    with ‹lt ∈ set š’žā€ŗ model_for_š’ž have "Mv x ≠ Mv y" by auto
    show ?thesis
    proof
      fix fml assume "fml ∈ reduce_literal lt"
      with neq have "fml = AF (Var (Solo x) =s Var (Solo y))"
        by simp
      with ‹Mv x ≠ Mv y› show "interp Isa ℳ fml" by auto
    qed
  next
    case (union x y z)
    with ‹lt ∈ set š’žā€ŗ model_for_š’ž have "Mv x = Mv y āŠ” Mv z" by fastforce
    then have "interp Isa ℳ (AT (Var (Solo x) =s Var (Solo y) āŠ”s Var (Solo z)))" by simp
    with union show ?thesis by auto
  next
    case (diff x y z)
    with ‹lt ∈ set š’žā€ŗ model_for_š’ž have "Mv x = Mv y - Mv z" by fastforce
    then have "interp Isa ℳ (AT (Var (Solo x) =s Var (Solo y) -s Var (Solo z)))" by simp
    with diff show ?thesis by auto
  next
    case (single x y)
    with ‹lt ∈ set š’žā€ŗ model_for_š’ž have "Mv x = HF {Mv y}" by fastforce
    then have "interp Isa ℳ (AT (Var (Solo x) =s Single (Var (Solo y))))" by simp
    with single show ?thesis by auto
  next
    case (app x f y)
    with ‹lt ∈ set š’žā€ŗ model_for_š’ž
    have "Mv x = (Mf f) (Mv y)" by fastforce
    moreover
    from app ‹lt ∈ set š’žā€ŗ have "y ∈ V"
      unfolding V_def by force
    with variable_as_composition_of_proper_Venn_regions
    have "Mv y = ⨆HF (proper_Venn_region ` ā„’ V y)"
      by presburger
    then have "Mv y = ⨆HF ((ℳ ∘ VennRegion) ` ā„’ V y)"
      by simp
    ultimately
    have "ℳ (Solo x) = ℳ wā‡˜fā‡™ā‡˜ā„’ V y⇙"
      using ℳ.simps set_var_set_set_to_var_set_list ā„’_subset_P_plus
      by metis
    with app show ?thesis by simp
  qed
qed

lemma soundness_reduce_cl:
  "āˆ€fml ∈ reduce_clause. interp Isa ℳ fml"
  unfolding reduce_clause_def
  using soundness_reduce_literal
  by fastforce

lemma ℳ_is_model_for_reduced_dnf: "is_model_for_reduced_dnf ℳ"
  unfolding is_model_for_reduced_dnf_def
  unfolding reduced_dnf_def
  using soundness_introduce_v soundness_introduce_w soundness_introduce_UnionOfVennRegions soundness_reduce_cl
  by (metis (no_types, lifting) Un_iff imageI)

end

lemma MLSSmf_to_MLSS_soundness:
  assumes š’ž_norm: "norm_clause š’ž"
      and š’ž_has_model: "∃Mv Mf. Icl Mv Mf š’ž"
    shows "∃M. normalized_MLSSmf_clause.is_model_for_reduced_dnf š’ž M"
proof -
  from š’ž_has_model obtain Mv Mf where "Icl Mv Mf š’ž" by blast
  with š’ž_norm
  interpret satisfiable_normalized_MLSSmf_clause š’ž Mv Mf
    by unfold_locales
  from ℳ_is_model_for_reduced_dnf show ?thesis by auto
qed

end