Theory Examples

theory Examples
imports Resolution
theory Examples imports Resolution begin

value "Var ''x''"
value "Fun ''one'' []"
value "Fun ''mul'' [Var ''y'',Var ''y'']"
value "Fun ''add'' [Fun ''mul'' [Var ''y'',Var ''y''], Fun ''one'' []]"

value "Pos ''greater'' [Var ''x'', Var ''y'']"
value "Neg ''less'' [Var ''x'', Var ''y'']"
value "Pos ''less'' [Var ''x'', Var ''y'']"
value "Pos ''equals''
        [Fun ''add''[Fun ''mul''[Var ''y'',Var ''y''], Fun ''one''[]],Var ''x'']"

fun Fnat :: "nat fun_denot" where
  " Fnat f [n,m] = 
     (if f = ''add'' then n + m else 
      if f = ''mul'' then n * m else 0)"
| " Fnat f [] = 
     (if f = ''one''  then 1 else
      if f = ''zero'' then 0 else 0)"
| " Fnat f us = 0"

fun Gnat :: "nat pred_denot" where
  "Gnat p [x,y] =
     (if p = ''less'' ∧ x < y then True else
      if p = ''greater'' ∧ x > y then True else 
      if p = ''equals'' ∧ x = y then True else False)"
| "Gnat p us = False"

fun Enat :: "nat var_denot" where
  "Enat x =
     (if x = ''x'' then 26 else
      if x = ''y'' then 5 else 0)"

lemma "evalt Enat Fnat (Var ''x'') = 26" 
  by auto
lemma "evalt Enat Fnat (Fun ''one'' []) = 1" 
  by auto
lemma "evalt Enat Fnat (Fun ''mul'' [Var ''y'',Var ''y'']) = 25" 
  by auto
lemma 
  "evalt Enat Fnat (Fun ''add'' [Fun ''mul'' [Var ''y'',Var ''y''], Fun ''one'' []]) = 26" 
  by auto

lemma "evall Enat Fnat Gnat (Pos ''greater'' [Var ''x'', Var ''y'']) = True" 
  by auto
lemma "evall Enat Fnat Gnat (Neg ''less'' [Var ''x'', Var ''y'']) = True" 
  by auto
lemma "evall Enat Fnat Gnat (Pos ''less'' [Var ''x'', Var ''y'']) = False" 
  by auto
lemma "evall Enat Fnat Gnat 
       (Pos ''equals'' 
         [Fun ''add'' [Fun ''mul'' [Var ''y'',Var ''y''],Fun ''one'' []]
         ,Var ''x'']
       ) = True" 
  by auto

definition PP :: "fterm literal" where
  "PP = Pos ''P'' [Fun ''c'' []]"

definition PQ :: "fterm literal" where
  "PQ = Pos ''Q'' [Fun ''d'' []]"

definition NP :: "fterm literal" where
  "NP = Neg ''P'' [Fun ''c'' []]"

definition NQ :: "fterm literal" where
  "NQ = Neg ''Q'' [Fun ''d'' []]"

theorem empty_mgu: "unifierls ε L ⟹ mguls ε L"
unfolding unifierls_def mguls_def apply auto
apply (rule_tac x=u in exI)
using empty_comp1 empty_comp2 apply auto
done

theorem unifier_single: "unifierls σ {l}" 
unfolding unifierls_def by auto

theorem resolution_rule':
      "C1 ∈ Cs ⟹ C2 ∈ Cs ⟹ applicable C1 C2 L1 L2 σ 
   ⟹ C = {resolution C1 C2 L1 L2 σ} 
   ⟹ resolution_step Cs (Cs ∪ C)"
  using resolution_rule by auto

lemma resolution_example1: 
   "resolution_deriv {{NP,PQ},{NQ},{PP,PQ}} 
                              {{NP,PQ},{NQ},{PP,PQ},{NP},{PP},{}}"
proof -
  have "resolution_step 
          {{NP,PQ},{NQ},{PP,PQ}}
         ({{NP,PQ},{NQ},{PP,PQ}} ∪ {{NP}})"
    apply (rule resolution_rule'[of "{NP,PQ}" _ "{NQ}" "{PQ}" "{NQ}" ε])
    unfolding applicable_def varsls_def  varsl_def 
              NQ_def NP_def PQ_def PP_def resolution_def
    using unifier_single empty_mgu using empty_subls by auto 
  then have "resolution_step 
          {{NP,PQ},{NQ},{PP,PQ}}
         ({{NP,PQ},{NQ},{PP,PQ},{NP}})" 
    by (simp add: insert_commute) 
  moreover
  have "resolution_step
         {{NP,PQ},{NQ},{PP,PQ},{NP}}
        ({{NP,PQ},{NQ},{PP,PQ},{NP}} ∪ {{PP}})"
    apply (rule resolution_rule'[of "{NQ}" _ "{PP,PQ}" "{NQ}" "{PQ}" ε])
    unfolding applicable_def varsls_def  varsl_def 
              NQ_def NP_def PQ_def PP_def resolution_def
    using unifier_single empty_mgu empty_subls apply auto
    done
  then have "resolution_step
         {{NP,PQ},{NQ},{PP,PQ},{NP}}
        ({{NP,PQ},{NQ},{PP,PQ},{NP},{PP}})" 
    by (simp add: insert_commute)
  moreover
  have "resolution_step
         {{NP,PQ},{NQ},{PP,PQ},{NP},{PP}}
        ({{NP,PQ},{NQ},{PP,PQ},{NP},{PP}} ∪ {{}})"
    apply (rule resolution_rule'[of "{NP}" _ "{PP}" "{NP}" "{PP}" ε])
    unfolding applicable_def varsls_def  varsl_def 
              NQ_def NP_def PQ_def PP_def resolution_def
    using unifier_single empty_mgu apply auto
    done
  then have "resolution_step
         {{NP,PQ},{NQ},{PP,PQ},{NP},{PP}}
        ({{NP,PQ},{NQ},{PP,PQ},{NP},{PP},{}})" 
    by (simp add: insert_commute)
  ultimately
  have "resolution_deriv {{NP,PQ},{NQ},{PP,PQ}} 
                         {{NP,PQ},{NQ},{PP,PQ},{NP},{PP},{}}"
    unfolding resolution_deriv_def by auto 
  then show ?thesis by auto
qed

definition Pa :: "fterm literal" where
  "Pa = Pos ''a'' []"

definition Na :: "fterm literal" where
  "Na = Neg ''a'' []"

definition Pb :: "fterm literal" where
  "Pb = Pos ''b'' []"

definition Nb :: "fterm literal" where
  "Nb = Neg ''b'' []"

definition Paa :: "fterm literal" where
  "Paa = Pos ''a'' [Fun ''a'' []]"

definition Naa :: "fterm literal" where
  "Naa = Neg ''a'' [Fun ''a'' []]"

definition Pax :: "fterm literal" where
  "Pax = Pos ''a'' [Var ''x'']"

definition Nax :: "fterm literal" where
  "Nax = Neg ''a'' [Var ''x'']"

definition mguPaaPax :: substitution where
  "mguPaaPax = (λx. if x = ''x'' then Fun ''a'' [] else Var x)"

lemma mguPaaPax_mgu: "mguls mguPaaPax {Paa,Pax}"
proof -
  let  = "λx. if x = ''x'' then Fun ''a'' [] else Var x"
  have a: "unifierls (λx. if x = ''x'' then Fun ''a'' [] else Var x) {Paa,Pax}" unfolding Paa_def Pax_def unifierls_def by auto
  have b: "∀u. unifierls u {Paa,Pax} ⟶ (∃i. u = ?σ ⋅ i)"
    proof (rule;rule)
      fix u
      assume "unifierls u {Paa,Pax}"
      then have uuu: "u ''x'' = Fun ''a'' []" unfolding unifierls_def Paa_def Pax_def by auto
      have "?σ ⋅ u = u"
        proof
          fix x
          {
            assume "x=''x''"
            moreover
            have "(?σ ⋅ u) ''x'' =  Fun ''a'' []" unfolding composition_def by auto
            ultimately have "(?σ ⋅ u) x = u x" using uuu by auto
          }
          moreover
          {
            assume "x≠''x''"
            then have "(?σ ⋅ u) x = (ε x) ⋅t u" unfolding composition_def by auto
            then have "(?σ ⋅ u) x = u x" by auto
          }
          ultimately show "(?σ ⋅ u) x = u x" by auto
        qed
      then have "∃i. ?σ ⋅ i = u" by auto
      then show "∃i. u = ?σ ⋅ i" by auto
     qed
   from a b show ?thesis unfolding mguls_def unfolding mguPaaPax_def by auto
qed

lemma resolution_example2: 
   "resolution_deriv {{Nb,Na},{Pax},{Pa},{Na,Pb,Naa}}
                              {{Nb,Na},{Pax},{Pa},{Na,Pb,Naa},{Na,Pb},{Na},{}}"
proof -
  have "resolution_step 
          {{Nb,Na},{Pax},{Pa},{Na,Pb,Naa}}
         ({{Nb,Na},{Pax},{Pa},{Na,Pb,Naa}} ∪ {{Na,Pb}})"
    apply (rule resolution_rule'[of "{Pax}" _ "{Na,Pb,Naa}" "{Pax}" "{Naa}" mguPaaPax ])
    using mguPaaPax_mgu unfolding applicable_def varsls_def  varsl_def 
          Nb_def Na_def Pax_def Pa_def Pb_def Naa_def Paa_def mguPaaPax_def resolution_def
    apply auto
    apply (rule_tac x=Na in image_eqI)
    unfolding Na_def apply auto
    apply (rule_tac x=Pb in image_eqI)
    unfolding Pb_def apply auto
    done
  then have "resolution_step 
          {{Nb,Na},{Pax},{Pa},{Na,Pb,Naa}}
         ({{Nb,Na},{Pax},{Pa},{Na,Pb,Naa},{Na,Pb}})" 
    by (simp add: insert_commute)
  moreover
  have "resolution_step 
          {{Nb,Na},{Pax},{Pa},{Na,Pb,Naa},{Na,Pb}}
         ({{Nb,Na},{Pax},{Pa},{Na,Pb,Naa},{Na,Pb}} ∪ {{Na}})"
    apply (rule resolution_rule'[of "{Nb,Na}" _ "{Na,Pb}" "{Nb}" "{Pb}" ε])
    unfolding applicable_def varsls_def  varsl_def 
              Pb_def Nb_def Na_def PP_def resolution_def
    using unifier_single empty_mgu apply auto
    done
  then have "resolution_step 
          {{Nb,Na},{Pax},{Pa},{Na,Pb,Naa},{Na,Pb}}
         ({{Nb,Na},{Pax},{Pa},{Na,Pb,Naa},{Na,Pb},{Na}})" 
    by (simp add: insert_commute)
  moreover
  have "resolution_step 
          {{Nb,Na},{Pax},{Pa},{Na,Pb,Naa},{Na,Pb},{Na}}
         ({{Nb,Na},{Pax},{Pa},{Na,Pb,Naa},{Na,Pb},{Na}} ∪ {{}})"
    apply (rule resolution_rule'[of "{Na}" _ "{Pa}" "{Na}" "{Pa}" ε])
    unfolding applicable_def varsls_def  varsl_def 
              Pa_def Nb_def Na_def PP_def resolution_def
    using unifier_single empty_mgu apply auto
    done
  then have "resolution_step 
          {{Nb,Na},{Pax},{Pa},{Na,Pb,Naa},{Na,Pb},{Na}}
         ({{Nb,Na},{Pax},{Pa},{Na,Pb,Naa},{Na,Pb},{Na},{}})" 
    by (simp add: insert_commute)
  ultimately
  have "resolution_deriv {{Nb,Na},{Pax},{Pa},{Na,Pb,Naa}} 
          {{Nb,Na},{Pax},{Pa},{Na,Pb,Naa},{Na,Pb},{Na},{}}"
    unfolding resolution_deriv_def by auto 
  then show ?thesis by auto
qed

lemma ref_sound: 
  assumes deriv: "resolution_deriv Cs Cs' ∧ {} ∈ Cs'"
  shows "¬evalcs F G Cs"
proof -
  from deriv have "evalcs F G Cs ⟹ evalcs F G Cs'" using lsound_derivation by auto
  moreover
  from deriv have "evalcs F G Cs' ⟹ evalc F G {}" unfolding evalcs_def by auto
  moreover
  then have "evalc F G {} ⟹ False" unfolding evalc_def by auto
  ultimately show ?thesis by auto
qed

lemma resolution_example1_sem: "¬evalcs F G {{NP, PQ}, {NQ}, {PP, PQ}}"
  using resolution_example1 ref_sound by auto

lemma resolution_example2_sem: "¬evalcs F G {{Nb,Na},{Pax},{Pa},{Na,Pb,Naa}}"
  using resolution_example2 ref_sound by auto

end