Theory Conditional_Critical_Pairs

theory Conditional_Critical_Pairs
imports Conditional_Rewriting
(*
Author:  Christian Sternagel <c.sternagel@gmail.com> (2016)
Author:  Julian Nagele <julian.nagele@uibk.ac.at> (2017)
License: LGPL (see file COPYING.LESSER)
*)

section ‹Conditional Critical Pairs›

theory Conditional_Critical_Pairs
imports
  Conditional_Rewriting
  QTRS.More_Abstract_Rewriting
begin

(*TODO: move*)
lemma greaterThanLessThan_all_conv:
  "(∀i∈{m <..< n}. P i) ⟷ (∀i. m < i ∧ i < n ⟶ P i)"
by auto

subsection ‹Conditional Overlaps and Conditional Critical Pairs›

definition "overlap R r r' p ⟷
  (∃p. p ∙ r ∈ R) ∧ (∃p. p ∙ r' ∈ R) ∧ vars_crule r ∩ vars_crule r' = {} ∧
  p ∈ funposs (clhs r) ∧ (∃σ. mgu (clhs r |_ p) (clhs r') = Some σ)"

definition
  CCP :: "('f, 'v :: infinite) ctrs ⇒ ('f, 'v) crule set"
where
  "CCP R =
    {((crhs ρ1 ⋅ σ, replace_at (clhs ρ1) p (crhs ρ2) ⋅ σ), subst_list σ (snd ρ1 @ snd ρ2)) | ρ1 ρ2 σ p.
      overlap R ρ1 ρ2 p ∧ mgu (clhs ρ1 |_ p) (clhs ρ2) = Some σ ∧ (p = ε ⟶ ¬ (∃π. π ∙ ρ1 = ρ2))}"

lemma overlapI [intro]:
  assumes "∃π. π ∙ ρ1 ∈ R" and "∃π. π ∙ ρ2 ∈ R"
    and "vars_crule ρ1 ∩ vars_crule ρ2 = {}"
    and "p ∈ funposs (clhs ρ1)"
    and "mgu (clhs ρ1 |_ p) (clhs ρ2) = Some σ"
  shows "overlap R ρ1 ρ2 p"
using assms unfolding overlap_def by blast

lemma overlap_mguD:
  assumes "overlap R ρ1 ρ2 p"
  obtains σ where "mgu (clhs ρ1 |_ p) (clhs ρ2) = Some σ"
using assms by (auto simp: overlap_def)

lemma CCP_I:
  assumes "∃π. π ∙ ρ1 ∈ R" and "∃π. π ∙ ρ2 ∈ R"
    and "vars_crule ρ1 ∩ vars_crule ρ2 = {}"
    and "p ∈ funposs (clhs ρ1)"
    and "mgu (clhs ρ1 |_ p) (clhs ρ2) = Some σ"
    and "p = ε ⟹ ¬ (∃π. π ∙ ρ1 = ρ2)"
  shows "((crhs ρ1 ⋅ σ, replace_at (clhs ρ1) p (crhs ρ2) ⋅ σ), subst_list σ (snd ρ1 @ snd ρ2)) ∈ CCP R"
using assms unfolding CCP_def by blast

text ‹
  Almost orthogonality modulo infeasibility (that is, all overlaps are either trivial,
  we have a root-overlap of two variants of the same rule,
  or the corresponding conditions are not all satisfiable at the same time).
›
definition almost_orthogonal :: "('f, 'v :: infinite) ctrs ⇒ bool"
where
  "almost_orthogonal R ⟷
    left_linear_trs (Ru R) ∧ (∀ρ1 ρ2 p. overlap R ρ1 ρ2 p ⟶
      (let μ = the (mgu (clhs ρ1 |_ p) (clhs ρ2)) in
      p = ε ∧ crhs ρ1 ⋅ μ = crhs ρ2 ⋅ μ ∨
      p = ε ∧ (∃p. p ∙ ρ1 = ρ2) ∨
      (∀m n. comm ((cstep_n R m)*) ((cstep_n R n)*) ⟶
        ¬ (∃σ. conds_n_sat R m (subst_list μ (snd ρ1)) σ ∧
               conds_n_sat R n (subst_list μ (snd ρ2)) σ))))"

lemma almost_orthogonal_imp_linear [simp]:
  "almost_orthogonal R ⟹ ((l, r), cs) ∈ R ⟹ linear_term l"
  by (auto simp: almost_orthogonal_def left_linear_trs_def Ru_def)

lemma almost_orthogonal_overlap_cases [consumes 2, cases pred: almost_orthogonal]:
  assumes "almost_orthogonal R" and "overlap R ρ1 ρ2 p"
  obtains (trivial) σ where "p = ε" and "mgu (clhs ρ1) (clhs ρ2) = Some σ"
    and "crhs ρ1 ⋅ σ = crhs ρ2 ⋅ σ"
  | (variants) π where "p = ε" and "π ∙ ρ1 = ρ2"
  | (infeasible) σ where "mgu (clhs ρ1 |_ p) (clhs ρ2) = Some σ"
    and "⋀m n. comm ((cstep_n R m)*) ((cstep_n R n)*) ⟹
      ¬ (∃τ. conds_n_sat R m (subst_list σ (snd ρ1)) τ ∧
             conds_n_sat R n (subst_list σ (snd ρ2)) τ)"
apply (rule overlap_mguD [OF ‹overlap R ρ1 ρ2 p›])
apply (insert assms)
apply (simp only: almost_orthogonal_def Let_def)
  by (metis option.sel subt_at.simps(1))

lemma CCP_E:
  fixes s :: "('f, 'v :: infinite) term"
  assumes "((s, t), cs) ∈ CCP R"
  shows "∃ρ1 ρ2 p σ. s = crhs ρ1 ⋅ σ ∧ t = replace_at (clhs ρ1) p (crhs ρ2) ⋅ σ ∧
    cs = subst_list σ (snd ρ1 @ snd ρ2) ∧
    overlap R ρ1 ρ2 p ∧ mgu (clhs ρ1 |_ p) (clhs ρ2) = Some σ ∧ (p = ε ⟶ ¬ (∃π. π ∙ ρ1 = ρ2))"
using assms unfolding CCP_def by blast

end