section \<open>Lifting Lemma\<close>

theory Completeness imports Resolution begin

locale unification =
  assumes unification: "\<And>\<sigma> L. finite L \<Longrightarrow> unifier\<^sub>l\<^sub>s \<sigma> L \<Longrightarrow> \<exists>\<theta>. mgu\<^sub>l\<^sub>s \<theta> L"
begin
text \<open>
  A proof of this assumption is available in @{file \<open>Unification_Theorem.thy\<close>} and used in
  @{file \<open>Completeness_Instance.thy\<close>}.
\<close>

lemma lifting:
  assumes fin: "finite C\<^sub>1 \<and> finite C\<^sub>2"
  assumes apart: "vars\<^sub>l\<^sub>s C\<^sub>1 \<inter> vars\<^sub>l\<^sub>s C\<^sub>2 = {}"
  assumes inst: "instance_of\<^sub>l\<^sub>s C\<^sub>1' C\<^sub>1 \<and> instance_of\<^sub>l\<^sub>s C\<^sub>2' C\<^sub>2"
  assumes appl: "applicable C\<^sub>1' C\<^sub>2' L\<^sub>1' L\<^sub>2' \<sigma>"
  shows "\<exists>L\<^sub>1 L\<^sub>2 \<tau>. applicable C\<^sub>1 C\<^sub>2 L\<^sub>1 L\<^sub>2 \<tau> \<and>
                instance_of\<^sub>l\<^sub>s (resolution C\<^sub>1' C\<^sub>2' L\<^sub>1' L\<^sub>2' \<sigma>) (resolution C\<^sub>1 C\<^sub>2 L\<^sub>1 L\<^sub>2 \<tau>)"
proof -
  \<comment> \<open>Obtaining the subsets we resolve upon:\<close>
  let ?R\<^sub>1' = "C\<^sub>1' - L\<^sub>1'" and ?R\<^sub>2' = "C\<^sub>2' - L\<^sub>2'"

  from inst obtain \<gamma> \<mu> where "C\<^sub>1 \<cdot>\<^sub>l\<^sub>s \<gamma> = C\<^sub>1' \<and> C\<^sub>2 \<cdot>\<^sub>l\<^sub>s \<mu> = C\<^sub>2'"
    unfolding instance_of\<^sub>l\<^sub>s_def by auto
  then obtain \<eta> where \<eta>_p: "C\<^sub>1 \<cdot>\<^sub>l\<^sub>s \<eta> = C\<^sub>1' \<and> C\<^sub>2 \<cdot>\<^sub>l\<^sub>s \<eta> = C\<^sub>2'"
    using apart merge_sub by force

  from \<eta>_p obtain L\<^sub>1 where L\<^sub>1_p: "L\<^sub>1 \<subseteq> C\<^sub>1 \<and> L\<^sub>1 \<cdot>\<^sub>l\<^sub>s \<eta> = L\<^sub>1' \<and> (C\<^sub>1 - L\<^sub>1) \<cdot>\<^sub>l\<^sub>s \<eta> = ?R\<^sub>1'"
    using appl project_sub using applicable_def by metis
  let ?R\<^sub>1 = "C\<^sub>1 - L\<^sub>1"
  from \<eta>_p obtain L\<^sub>2 where L\<^sub>2_p: "L\<^sub>2 \<subseteq> C\<^sub>2 \<and> L\<^sub>2 \<cdot>\<^sub>l\<^sub>s \<eta> = L\<^sub>2' \<and> (C\<^sub>2 - L\<^sub>2) \<cdot>\<^sub>l\<^sub>s \<eta> = ?R\<^sub>2'"
    using appl project_sub using applicable_def by metis
  let ?R\<^sub>2 = "C\<^sub>2 - L\<^sub>2"

  \<comment> \<open>Obtaining substitutions:\<close>
  from appl have "mgu\<^sub>l\<^sub>s \<sigma> (L\<^sub>1' \<union> L\<^sub>2'\<^sup>C)" using applicable_def by auto
  then have "mgu\<^sub>l\<^sub>s \<sigma> ((L\<^sub>1 \<cdot>\<^sub>l\<^sub>s \<eta>) \<union> (L\<^sub>2 \<cdot>\<^sub>l\<^sub>s \<eta>)\<^sup>C)" using L\<^sub>1_p L\<^sub>2_p by auto
  then have "mgu\<^sub>l\<^sub>s \<sigma> ((L\<^sub>1  \<union> L\<^sub>2\<^sup>C) \<cdot>\<^sub>l\<^sub>s \<eta>)" using compls_subls subls_union by auto
  then have "unifier\<^sub>l\<^sub>s \<sigma> ((L\<^sub>1  \<union> L\<^sub>2\<^sup>C) \<cdot>\<^sub>l\<^sub>s \<eta>)" using mgu\<^sub>l\<^sub>s_def by auto
  then have \<eta>\<sigma>uni: "unifier\<^sub>l\<^sub>s (\<eta> \<cdot> \<sigma>) (L\<^sub>1  \<union> L\<^sub>2\<^sup>C)"
    using unifier\<^sub>l\<^sub>s_def composition_conseq2l by auto
  then obtain \<tau> where \<tau>_p: "mgu\<^sub>l\<^sub>s \<tau> (L\<^sub>1  \<union> L\<^sub>2\<^sup>C)"
    using unification fin L\<^sub>1_p L\<^sub>2_p by (meson finite_UnI finite_imageI rev_finite_subset)
  then obtain \<phi> where \<phi>_p: "\<tau> \<cdot> \<phi> = \<eta> \<cdot> \<sigma>" using \<eta>\<sigma>uni mgu\<^sub>l\<^sub>s_def by auto

  \<comment> \<open>Showing that we have the desired resolvent:\<close>
  let ?C = "((C\<^sub>1 - L\<^sub>1)  \<union> (C\<^sub>2 - L\<^sub>2)) \<cdot>\<^sub>l\<^sub>s \<tau>"
  have "?C \<cdot>\<^sub>l\<^sub>s \<phi>  = (?R\<^sub>1 \<union> ?R\<^sub>2 ) \<cdot>\<^sub>l\<^sub>s (\<tau> \<cdot> \<phi>)"
    using subls_union composition_conseq2ls by auto
  also have "... = (?R\<^sub>1 \<union> ?R\<^sub>2 ) \<cdot>\<^sub>l\<^sub>s (\<eta> \<cdot> \<sigma>)" using \<phi>_p by auto
  also have "... = ((?R\<^sub>1 \<cdot>\<^sub>l\<^sub>s \<eta>) \<union> (?R\<^sub>2 \<cdot>\<^sub>l\<^sub>s \<eta>)) \<cdot>\<^sub>l\<^sub>s \<sigma>"
    using subls_union composition_conseq2ls by auto
  also have "... = (?R\<^sub>1' \<union> ?R\<^sub>2') \<cdot>\<^sub>l\<^sub>s \<sigma>" using \<eta>_p L\<^sub>1_p L\<^sub>2_p by auto
  finally have "?C \<cdot>\<^sub>l\<^sub>s \<phi> = ((C\<^sub>1' - L\<^sub>1') \<union> (C\<^sub>2' - L\<^sub>2')) \<cdot>\<^sub>l\<^sub>s \<sigma>" by auto
  then have ins: "instance_of\<^sub>l\<^sub>s (resolution C\<^sub>1' C\<^sub>2' L\<^sub>1' L\<^sub>2' \<sigma>) (resolution C\<^sub>1 C\<^sub>2 L\<^sub>1 L\<^sub>2 \<tau>)"
    using resolution_def instance_of\<^sub>l\<^sub>s_def by metis

  \<comment> \<open>Showing that the resolution rule is applicable:\<close>
  have "C\<^sub>1' \<noteq> {} \<and> C\<^sub>2' \<noteq> {} \<and> L\<^sub>1' \<noteq> {} \<and> L\<^sub>2' \<noteq> {}"
    using appl applicable_def by auto
  then have "C\<^sub>1 \<noteq> {} \<and> C\<^sub>2 \<noteq> {} \<and> L\<^sub>1 \<noteq> {} \<and> L\<^sub>2 \<noteq> {}" using \<eta>_p L\<^sub>1_p L\<^sub>2_p by auto
  then have appli: "applicable C\<^sub>1 C\<^sub>2 L\<^sub>1 L\<^sub>2 \<tau>"
    using apart L\<^sub>1_p L\<^sub>2_p \<tau>_p applicable_def by auto

  from ins appli show ?thesis by auto
qed

section \<open>Completeness\<close>

lemma falsifies\<^sub>g_empty:
  assumes "falsifies\<^sub>g [] C"
  shows "C = {}"
proof -
  have "\<forall>l \<in> C. False"
    proof
      fix l
      assume "l\<in>C"
      then have "falsifies\<^sub>l [] l" using assms by auto
      then show False unfolding falsifies\<^sub>l_def by (cases l) auto
    qed
  then show ?thesis by auto
qed

lemma falsifies\<^sub>c\<^sub>s_empty:
  assumes "falsifies\<^sub>c [] C"
  shows "C = {}"
proof -
  from assms obtain C' where C'_p: "instance_of\<^sub>l\<^sub>s C' C \<and> falsifies\<^sub>g [] C'" by auto
  then have "C'= {}" using falsifies\<^sub>g_empty by auto
  then show "C = {}" using C'_p unfolding instance_of\<^sub>l\<^sub>s_def by auto
qed

lemma complements_do_not_falsify':
  assumes l1C1': "l\<^sub>1 \<in> C\<^sub>1'"
  assumes l\<^sub>2C1': "l\<^sub>2 \<in> C\<^sub>1'"
  assumes comp: "l\<^sub>1 = l\<^sub>2\<^sup>c"
  assumes falsif: "falsifies\<^sub>g G C\<^sub>1'"
  shows "False"
proof (cases l\<^sub>1)
  case (Pos p ts)
  let ?i1 = "nat_of_fatom (p, ts)"

  from assms have gr: "ground\<^sub>l l\<^sub>1" unfolding falsifies\<^sub>l_def by auto
  then have Neg: "l\<^sub>2 = Neg p ts" using comp Pos by (cases l\<^sub>2) auto

  from falsif have "falsifies\<^sub>l G l\<^sub>1" using l1C1' by auto
  then have "G ! ?i1 = False" using l1C1' Pos unfolding falsifies\<^sub>l_def by (induction "Pos p ts") auto
  moreover
  let ?i2 = "nat_of_fatom (get_atom l\<^sub>2)"
  from falsif have "falsifies\<^sub>l G l\<^sub>2" using l\<^sub>2C1' by auto
  then have "G ! ?i2 = (\<not>sign l\<^sub>2)" unfolding falsifies\<^sub>l_def by meson
  then have "G ! ?i1 = (\<not>sign l\<^sub>2)" using Pos Neg comp by simp
  then have "G ! ?i1 = True" using Neg by auto
  ultimately show ?thesis by auto
next
  case (Neg p ts)
  let ?i1 = "nat_of_fatom (p,ts)"

  from assms have gr: "ground\<^sub>l l\<^sub>1" unfolding falsifies\<^sub>l_def by auto
  then have Pos: "l\<^sub>2 = Pos p ts" using comp Neg by (cases l\<^sub>2) auto

  from falsif have "falsifies\<^sub>l G l\<^sub>1" using l1C1' by auto
  then have "G ! ?i1 = True" using l1C1' Neg unfolding falsifies\<^sub>l_def by (metis get_atom.simps(2) literal.disc(2))
  moreover
  let ?i2 = "nat_of_fatom (get_atom l\<^sub>2)"
  from falsif have "falsifies\<^sub>l G l\<^sub>2" using l\<^sub>2C1' by auto
  then have "G ! ?i2 = (\<not>sign l\<^sub>2)" unfolding falsifies\<^sub>l_def by meson
  then have "G ! ?i1 = (\<not>sign l\<^sub>2)" using Pos Neg comp by simp
  then have "G ! ?i1 = False" using Pos using literal.disc(1) by blast
  ultimately show ?thesis by auto
qed

lemma complements_do_not_falsify:
  assumes l1C1': "l\<^sub>1 \<in> C\<^sub>1'"
  assumes l\<^sub>2C1': "l\<^sub>2 \<in> C\<^sub>1'"
  assumes fals: "falsifies\<^sub>g G C\<^sub>1'"
  shows "l\<^sub>1 \<noteq> l\<^sub>2\<^sup>c"
using assms complements_do_not_falsify' by blast

lemma other_falsified:
  assumes C1'_p: "ground\<^sub>l\<^sub>s C\<^sub>1' \<and> falsifies\<^sub>g (B@[d]) C\<^sub>1'"
  assumes l_p: "l \<in> C\<^sub>1'" "nat_of_fatom (get_atom l) = length B"
  assumes other: "lo \<in> C\<^sub>1'" "lo \<noteq> l"
  shows "falsifies\<^sub>l B lo"
proof -
  let ?i = "nat_of_fatom (get_atom lo)"
  have ground_l\<^sub>2: "ground\<^sub>l l" using l_p C1'_p by auto
  \<comment> \<open>They are, of course, also ground:\<close>
  have ground_lo: "ground\<^sub>l lo" using C1'_p other by auto
  from C1'_p have "falsifies\<^sub>g (B@[d]) (C\<^sub>1' - {l})" by auto
  \<comment> \<open>And indeed, falsified by @{term "B@[d]"}:\<close>
  then have loB\<^sub>2: "falsifies\<^sub>l (B@[d]) lo" using other by auto
  then have "?i < length (B @ [d])" unfolding falsifies\<^sub>l_def by meson
  \<comment> \<open>And they have numbers in the range of @{term "B@[d]"}, i.e. less than @{term "length B + 1"}:\<close>
  then have "nat_of_fatom (get_atom lo) < length B + 1" using undiag_diag_fatom by (cases lo) auto
  moreover
  have l_lo: "l\<noteq>lo" using other by auto
  \<comment> \<open>The are not the complement of @{term l }, since then the clause could not be falsified:\<close>
  have lc_lo: "lo \<noteq> l\<^sup>c" using C1'_p l_p other complements_do_not_falsify[of lo C\<^sub>1' l "(B@[d])"] by auto
  from l_lo lc_lo have "get_atom l \<noteq> get_atom lo" using sign_comp_atom by metis
  then have "nat_of_fatom (get_atom lo) \<noteq> nat_of_fatom (get_atom l)"
    using nat_of_fatom_bij ground_lo ground_l\<^sub>2 ground\<^sub>l_ground_fatom
    unfolding bij_betw_def inj_on_def by metis
  \<comment> \<open>Therefore they have different numbers:\<close>
  then have "nat_of_fatom (get_atom lo) \<noteq> length B" using l_p by auto
  ultimately
  \<comment> \<open>So their numbers are in the range of @{term B}:\<close>
  have "nat_of_fatom (get_atom lo) < length B" by auto
  \<comment> \<open>So we did not need the last index of @{term "B@[d]"} to falsify them, i.e. @{term B} suffices:\<close>
  then show "falsifies\<^sub>l B lo" using loB\<^sub>2 shorter_falsifies\<^sub>l by blast
qed

theorem completeness':
  assumes "closed_tree T Cs"
  assumes "\<forall>C\<in>Cs. finite C"
  shows "\<exists>Cs'. resolution_deriv Cs Cs' \<and> {} \<in> Cs'"
using assms proof (induction T arbitrary: Cs rule: measure_induct_rule[of treesize])
  fix T :: tree
  fix Cs :: "fterm clause set"
  assume ih: "\<And>T' Cs. treesize T' < treesize T \<Longrightarrow> closed_tree T' Cs \<Longrightarrow>
                            \<forall>C\<in>Cs. finite C \<Longrightarrow> \<exists>Cs'. resolution_deriv Cs Cs' \<and> {} \<in> Cs'"
  assume clo: "closed_tree T Cs"
  assume finite_Cs: "\<forall>C\<in>Cs. finite C"
  { \<comment> \<open>Base case:\<close>
    assume "treesize T = 0"
    then have "T=Leaf" using treesize_Leaf by auto
    then have "closed_branch [] Leaf Cs" using branch_inv_Leaf clo unfolding closed_tree_def by auto
    then have "falsifies\<^sub>c\<^sub>s [] Cs" by auto
    then have "{} \<in> Cs" using falsifies\<^sub>c\<^sub>s_empty by auto
    then have "\<exists>Cs'. resolution_deriv Cs Cs' \<and> {} \<in> Cs'"
      unfolding resolution_deriv_def by auto
  }
  moreover
  { \<comment> \<open>Induction case:\<close>
    assume "treesize T > 0"
    then have "\<exists>l r. T=Branching l r" by (cases T) auto

    \<comment> \<open>Finding sibling branches and their corresponding clauses:\<close>
    then obtain B where b_p: "internal B T \<and> branch (B@[True]) T \<and> branch (B@[False]) T"
      using internal_branch[of _ "[]" _ T] Branching_Leaf_Leaf_Tree by fastforce
    let ?B\<^sub>1 = "B@[True]"
    let ?B\<^sub>2 = "B@[False]"

    obtain C\<^sub>1o where C\<^sub>1o_p: "C\<^sub>1o \<in> Cs \<and> falsifies\<^sub>c ?B\<^sub>1 C\<^sub>1o" using b_p clo unfolding closed_tree_def by metis
    obtain C\<^sub>2o where C\<^sub>2o_p: "C\<^sub>2o \<in> Cs \<and> falsifies\<^sub>c ?B\<^sub>2 C\<^sub>2o" using b_p clo unfolding closed_tree_def by metis

    \<comment> \<open>Standardizing the clauses apart:\<close>
    let ?C\<^sub>1 = "std\<^sub>1 C\<^sub>1o"
    let ?C\<^sub>2 = "std\<^sub>2 C\<^sub>2o"
    have C\<^sub>1_p: "falsifies\<^sub>c ?B\<^sub>1 ?C\<^sub>1" using std\<^sub>1_falsifies C\<^sub>1o_p by auto
    have C\<^sub>2_p: "falsifies\<^sub>c ?B\<^sub>2 ?C\<^sub>2" using std\<^sub>2_falsifies C\<^sub>2o_p by auto

    have fin: "finite ?C\<^sub>1 \<and> finite ?C\<^sub>2" using C\<^sub>1o_p C\<^sub>2o_p finite_Cs by auto

    \<comment> \<open>We go down to the ground world.\<close>
    \<comment> \<open>Finding the falsifying ground instance @{term C\<^sub>1'} of @{term ?C\<^sub>1}, and proving properties about it:\<close>

    \<comment> \<open>@{term C\<^sub>1'} is falsified by @{term ?B\<^sub>1}:\<close>
    from C\<^sub>1_p  obtain C\<^sub>1' where C\<^sub>1'_p: "ground\<^sub>l\<^sub>s C\<^sub>1' \<and> instance_of\<^sub>l\<^sub>s C\<^sub>1' ?C\<^sub>1 \<and> falsifies\<^sub>g ?B\<^sub>1 C\<^sub>1'" by metis

    have "\<not>falsifies\<^sub>c B C\<^sub>1o" using C\<^sub>1o_p b_p clo unfolding closed_tree_def by metis
    then have "\<not>falsifies\<^sub>c B ?C\<^sub>1" using std\<^sub>1_falsifies using prod.exhaust_sel by blast
    \<comment> \<open>@{term C\<^sub>1'} is not falsified by @{term B}:\<close>
    then have l_B: "\<not>falsifies\<^sub>g B C\<^sub>1'" using C\<^sub>1'_p by auto

    \<comment> \<open>@{term C\<^sub>1'} contains a literal @{term l\<^sub>1} that is falsified by @{term ?B\<^sub>1}, but not @{term B}:\<close>
    from C\<^sub>1'_p l_B obtain l\<^sub>1 where l\<^sub>1_p: "l\<^sub>1 \<in> C\<^sub>1' \<and> falsifies\<^sub>l (B@[True]) l\<^sub>1 \<and> \<not>(falsifies\<^sub>l B l\<^sub>1)" by auto
    let ?i = "nat_of_fatom (get_atom l\<^sub>1)"

    \<comment> \<open>@{term l\<^sub>1} is of course ground:\<close>
    have ground_l\<^sub>1: "ground\<^sub>l l\<^sub>1" using C\<^sub>1'_p l\<^sub>1_p by auto

    from l\<^sub>1_p have "\<not>(?i < length B \<and> B ! ?i = (\<not>sign l\<^sub>1))" using ground_l\<^sub>1 unfolding falsifies\<^sub>l_def by meson
    then have "\<not>(?i < length B \<and> (B@[True]) ! ?i = (\<not>sign l\<^sub>1))" by (metis nth_append) \<comment> \<open>Not falsified by @{term B}.\<close>
    moreover
    from l\<^sub>1_p have "?i < length (B @ [True]) \<and> (B @ [True]) ! ?i = (\<not>sign l\<^sub>1)" unfolding falsifies\<^sub>l_def by meson
    ultimately
    have l\<^sub>1_sign_no: "?i = length B \<and> (B @ [True]) ! ?i = (\<not>sign l\<^sub>1)" by auto

    \<comment> \<open>@{term l\<^sub>1} is negative:\<close>
    from l\<^sub>1_sign_no have l\<^sub>1_sign: "sign l\<^sub>1 = False" by auto
    from l\<^sub>1_sign_no have l\<^sub>1_no: "nat_of_fatom (get_atom l\<^sub>1) = length B" by auto

    \<comment> \<open>All the other literals in @{term C\<^sub>1'} must be falsified by B, since they are falsified by @{term ?B\<^sub>1}, but not @{term l\<^sub>1}.\<close>
    from C\<^sub>1'_p l\<^sub>1_no l\<^sub>1_p have B_C\<^sub>1'l\<^sub>1: "falsifies\<^sub>g B (C\<^sub>1' - {l\<^sub>1})" (* This should be a lemma *)
      using other_falsified by blast

    \<comment> \<open>We do the same exercise for @{term ?C\<^sub>2}, @{term C\<^sub>2'}, @{term ?B\<^sub>2}, @{term l\<^sub>2}:\<close>
    from C\<^sub>2_p obtain C\<^sub>2' where C\<^sub>2'_p: "ground\<^sub>l\<^sub>s C\<^sub>2' \<and> instance_of\<^sub>l\<^sub>s C\<^sub>2' ?C\<^sub>2 \<and> falsifies\<^sub>g ?B\<^sub>2 C\<^sub>2'" by metis

    have "\<not>falsifies\<^sub>c B C\<^sub>2o" using C\<^sub>2o_p b_p clo unfolding closed_tree_def by metis
    then have "\<not>falsifies\<^sub>c B ?C\<^sub>2" using std\<^sub>2_falsifies using prod.exhaust_sel by blast
    then have l_B: "\<not>falsifies\<^sub>g B C\<^sub>2'" using C\<^sub>2'_p by auto (* I already had something called l_B... I could give it a new name *)

    \<comment> \<open>@{term C\<^sub>2'} contains a literal @{term l\<^sub>2} that is falsified by @{term ?B\<^sub>2}, but not B:\<close>
    from C\<^sub>2'_p l_B obtain l\<^sub>2 where l\<^sub>2_p: "l\<^sub>2 \<in> C\<^sub>2' \<and> falsifies\<^sub>l (B@[False]) l\<^sub>2 \<and> \<not>falsifies\<^sub>l B l\<^sub>2" by auto
    let ?i = "nat_of_fatom (get_atom l\<^sub>2)"

    have ground_l\<^sub>2: "ground\<^sub>l l\<^sub>2" using C\<^sub>2'_p l\<^sub>2_p by auto

    from l\<^sub>2_p have "\<not>(?i < length B \<and> B ! ?i = (\<not>sign l\<^sub>2))" using ground_l\<^sub>2 unfolding falsifies\<^sub>l_def by meson
    then have "\<not>(?i < length B \<and> (B@[False]) ! ?i = (\<not>sign l\<^sub>2))" by (metis nth_append) \<comment> \<open>Not falsified by @{term B}.\<close>
    moreover
    from l\<^sub>2_p have "?i < length (B @ [False]) \<and> (B @ [False]) ! ?i = (\<not>sign l\<^sub>2)" unfolding falsifies\<^sub>l_def by meson
    ultimately
    have l\<^sub>2_sign_no: "?i = length B \<and> (B @ [False]) ! ?i = (\<not>sign l\<^sub>2)" by auto

    \<comment> \<open>@{term l\<^sub>2} is negative:\<close>
    from l\<^sub>2_sign_no have l\<^sub>2_sign: "sign l\<^sub>2 = True" by auto
    from l\<^sub>2_sign_no have l\<^sub>2_no: "nat_of_fatom (get_atom l\<^sub>2) = length B" by auto

    \<comment> \<open>All the other literals in @{term C\<^sub>2'} must be falsified by B, since they are falsified by
          @{term ?B\<^sub>2}, but not @{term l\<^sub>2}.\<close>
    from C\<^sub>2'_p l\<^sub>2_no l\<^sub>2_p have B_C\<^sub>2'l\<^sub>2: "falsifies\<^sub>g B (C\<^sub>2' - {l\<^sub>2})"
      using other_falsified by blast

    \<comment> \<open>Proving some properties about @{term C\<^sub>1'} and @{term C\<^sub>2'}, @{term l\<^sub>1} and @{term l\<^sub>2}, as well as
          the resolvent of @{term C\<^sub>1'} and @{term C\<^sub>2'}:\<close>
    have l\<^sub>2cisl\<^sub>1: "l\<^sub>2\<^sup>c = l\<^sub>1" (* Could perhaps be a lemma *)
      proof -
        from l\<^sub>1_no l\<^sub>2_no ground_l\<^sub>1 ground_l\<^sub>2 have "get_atom l\<^sub>1 = get_atom l\<^sub>2"
              using nat_of_fatom_bij ground\<^sub>l_ground_fatom
              unfolding bij_betw_def inj_on_def by metis
        then show "l\<^sub>2\<^sup>c = l\<^sub>1" using l\<^sub>1_sign l\<^sub>2_sign using sign_comp_atom by metis
      qed

    have "applicable C\<^sub>1' C\<^sub>2' {l\<^sub>1} {l\<^sub>2} Resolution.\<epsilon>" unfolding applicable_def
      using l\<^sub>1_p l\<^sub>2_p C\<^sub>1'_p ground\<^sub>l\<^sub>s_vars\<^sub>l\<^sub>s l\<^sub>2cisl\<^sub>1 empty_comp2 unfolding mgu\<^sub>l\<^sub>s_def unifier\<^sub>l\<^sub>s_def by auto
    \<comment> \<open>Lifting to get a resolvent of @{term ?C\<^sub>1} and @{term ?C\<^sub>2}:\<close>
    then obtain L\<^sub>1 L\<^sub>2 \<tau> where L\<^sub>1L\<^sub>2\<tau>_p: "applicable ?C\<^sub>1 ?C\<^sub>2 L\<^sub>1 L\<^sub>2 \<tau>  \<and> instance_of\<^sub>l\<^sub>s (resolution C\<^sub>1' C\<^sub>2' {l\<^sub>1} {l\<^sub>2} Resolution.\<epsilon>) (resolution ?C\<^sub>1 ?C\<^sub>2 L\<^sub>1 L\<^sub>2 \<tau>)"
      using std_apart_apart C\<^sub>1'_p C\<^sub>2'_p lifting[of ?C\<^sub>1 ?C\<^sub>2 C\<^sub>1' C\<^sub>2' "{l\<^sub>1}" "{l\<^sub>2}" Resolution.\<epsilon>] fin by auto

    \<comment> \<open>Defining the clause to be derived, the new clausal form and the new tree:\<close>
    \<comment> \<open>We name the resolvent @{term C}.\<close>
    obtain C where C_p: "C = resolution ?C\<^sub>1 ?C\<^sub>2 L\<^sub>1 L\<^sub>2 \<tau>" by auto
    obtain CsNext where CsNext_p: "CsNext = Cs \<union> {?C\<^sub>1, ?C\<^sub>2, C}" by auto
    obtain T'' where T''_p: "T'' = delete B T" by auto
        \<comment> \<open>Here we delete the two branch children @{term ?B\<^sub>1} and @{term ?B\<^sub>2} of @{term B}.\<close>

    \<comment> \<open>Our new clause is falsified by the branch @{term B} of our new tree:\<close>
    have "falsifies\<^sub>g B ((C\<^sub>1' - {l\<^sub>1}) \<union> (C\<^sub>2' - {l\<^sub>2}))" using B_C\<^sub>1'l\<^sub>1 B_C\<^sub>2'l\<^sub>2 by cases auto
    then have "falsifies\<^sub>g B (resolution C\<^sub>1' C\<^sub>2' {l\<^sub>1} {l\<^sub>2} Resolution.\<epsilon>)" unfolding resolution_def empty_subls by auto
    then have falsifies_C: "falsifies\<^sub>c B C" using C_p L\<^sub>1L\<^sub>2\<tau>_p by auto

    have T''_smaller: "treesize T'' < treesize T" using treezise_delete T''_p b_p by auto
    have T''_bran: "anybranch T'' (\<lambda>b. closed_branch b T'' CsNext)"
      proof (rule allI; rule impI)
        fix b
        assume br: "branch b T''"
        from br have "b = B \<or> branch b T" using branch_delete T''_p by auto
        then show "closed_branch b T'' CsNext"
          proof
            assume "b=B"
            then show "closed_branch b T'' CsNext" using falsifies_C br CsNext_p by auto
          next
            assume "branch b T"
            then show "closed_branch b T'' CsNext" using clo br T''_p CsNext_p unfolding closed_tree_def by auto
          qed
      qed
    then have T''_bran2: "anybranch T'' (\<lambda>b. falsifies\<^sub>c\<^sub>s b CsNext)" by auto (* replace T''_bran with this maybe? *)

    \<comment> \<open>We cut the tree even smaller to ensure only the branches are falsified, i.e. it is a closed tree:\<close>
    obtain T' where T'_p: "T' = cutoff (\<lambda>G. falsifies\<^sub>c\<^sub>s G CsNext) [] T''" by auto
    have T'_smaller: "treesize T' < treesize T" using treesize_cutoff[of "\<lambda>G. falsifies\<^sub>c\<^sub>s G CsNext" "[]" T''] T''_smaller unfolding T'_p by auto

    from T''_bran2 have "anybranch T' (\<lambda>b. falsifies\<^sub>c\<^sub>s b CsNext)" using cutoff_branch[of T'' "\<lambda>b. falsifies\<^sub>c\<^sub>s b CsNext"] T'_p by auto
    then have T'_bran: "anybranch T' (\<lambda>b. closed_branch b T' CsNext)" by auto
    have T'_intr: "anyinternal T' (\<lambda>p. \<not>falsifies\<^sub>c\<^sub>s p CsNext)" using T'_p cutoff_internal[of T'' "\<lambda>b. falsifies\<^sub>c\<^sub>s b CsNext"] T''_bran2 by blast
    have T'_closed: "closed_tree T' CsNext" using T'_bran T'_intr unfolding closed_tree_def by auto
    have finite_CsNext: "\<forall>C\<in>CsNext. finite C" unfolding CsNext_p C_p resolution_def using finite_Cs fin by auto

    \<comment> \<open>By induction hypothesis we get a resolution derivation of @{term "{}"} from our new clausal form:\<close>
    from T'_smaller T'_closed have "\<exists>Cs''. resolution_deriv CsNext Cs'' \<and> {} \<in> Cs''" using ih[of T' CsNext] finite_CsNext by blast
    then obtain Cs'' where Cs''_p: "resolution_deriv CsNext Cs'' \<and> {} \<in> Cs''" by auto
    moreover
    { \<comment> \<open>Proving that we can actually derive the new clausal form:\<close>
      have "resolution_step Cs (Cs \<union> {?C\<^sub>1})" using std\<^sub>1_renames standardize_apart C\<^sub>1o_p by (metis Un_insert_right)
      moreover
      have "resolution_step (Cs \<union> {?C\<^sub>1}) (Cs \<union> {?C\<^sub>1} \<union> {?C\<^sub>2})" using std\<^sub>2_renames[of C\<^sub>2o] standardize_apart[of C\<^sub>2o _ ?C\<^sub>2] C\<^sub>2o_p by auto
      then have "resolution_step (Cs \<union> {?C\<^sub>1}) (Cs \<union> {?C\<^sub>1,?C\<^sub>2})" by (simp add: insert_commute)
      moreover
      then have "resolution_step (Cs \<union> {?C\<^sub>1,?C\<^sub>2}) (Cs \<union> {?C\<^sub>1,?C\<^sub>2} \<union> {C})"
        using L\<^sub>1L\<^sub>2\<tau>_p resolution_rule[of ?C\<^sub>1 "Cs \<union> {?C\<^sub>1,?C\<^sub>2}" ?C\<^sub>2 L\<^sub>1 L\<^sub>2 \<tau> ] using C_p by auto
      then have "resolution_step (Cs \<union> {?C\<^sub>1,?C\<^sub>2}) CsNext" using CsNext_p by (simp add:  Un_commute)
      ultimately
      have "resolution_deriv Cs CsNext"  unfolding resolution_deriv_def by auto
    }
    \<comment> \<open>Combining the two derivations, we get the desired derivation from @{term Cs} of @{term "{}"}:\<close>
    ultimately have "resolution_deriv Cs Cs''"  unfolding resolution_deriv_def by auto
    then have "\<exists>Cs'. resolution_deriv Cs Cs' \<and> {} \<in> Cs'" using Cs''_p by auto
  }
  ultimately show "\<exists>Cs'. resolution_deriv Cs Cs' \<and> {} \<in> Cs'" by auto
qed

theorem completeness:
  assumes finite_cs: "finite Cs" "\<forall>C\<in>Cs. finite C"
  assumes unsat: "\<forall>(F::hterm fun_denot) (G::hterm pred_denot) . \<not>eval\<^sub>c\<^sub>s F G Cs"
  shows "\<exists>Cs'. resolution_deriv Cs Cs' \<and> {} \<in> Cs'"
proof -
  from unsat have "\<forall>(G::hterm pred_denot) . \<not>eval\<^sub>c\<^sub>s HFun G Cs" by auto
  then obtain T where "closed_tree T Cs" using herbrand assms by blast
  then show "\<exists>Cs'. resolution_deriv Cs Cs' \<and> {} \<in> Cs'" using completeness' assms by auto
qed

definition E_conv :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a var_denot \<Rightarrow> 'b var_denot" where
  "E_conv b_of_a E \<equiv> \<lambda>x. (b_of_a (E x))"

definition F_conv :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a fun_denot \<Rightarrow> 'b fun_denot" where
  "F_conv b_of_a F \<equiv> \<lambda>f bs. b_of_a (F f (map (inv b_of_a) bs))"

definition G_conv :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a pred_denot \<Rightarrow> 'b pred_denot" where
  "G_conv b_of_a G \<equiv> \<lambda>p bs. (G p (map (inv b_of_a) bs))"

lemma eval\<^sub>t_bij:
  assumes "bij (b_of_a::'a \<Rightarrow> 'b)"
  shows"eval\<^sub>t (E_conv b_of_a E) (F_conv b_of_a F) t = b_of_a (eval\<^sub>t E F t)"
proof (induction t)
  case (Fun f ts)
  then have "map (inv b_of_a \<circ> eval\<^sub>t (E_conv b_of_a E) (F_conv b_of_a F)) ts = eval\<^sub>t\<^sub>s E F ts"
    unfolding E_conv_def F_conv_def
    using assms bij_is_inj by fastforce
  then have "b_of_a (F f (map (inv b_of_a \<circ> eval\<^sub>t (E_conv b_of_a E) ((F_conv b_of_a F))) ts)) = b_of_a (F f (eval\<^sub>t\<^sub>s E F ts))" by metis
  then show ?case using assms unfolding E_conv_def F_conv_def by auto
next
  case (Var x)
  then show ?case using assms unfolding E_conv_def by auto
qed

lemma eval\<^sub>t\<^sub>s_bij:
  assumes "bij (b_of_a::'a \<Rightarrow> 'b)"
  shows "G_conv b_of_a G p (eval\<^sub>t\<^sub>s (E_conv b_of_a E) (F_conv b_of_a F) ts) = G p (eval\<^sub>t\<^sub>s E F ts)"
  using assms using eval\<^sub>t_bij
proof -
  have "map (inv b_of_a \<circ> eval\<^sub>t (E_conv b_of_a E) (F_conv b_of_a F)) ts = eval\<^sub>t\<^sub>s E F ts"
    using eval\<^sub>t_bij assms bij_is_inj by fastforce
  then show ?thesis
    by (metis (no_types) G_conv_def map_map)
qed

lemma eval\<^sub>l_bij:
  assumes "bij (b_of_a::'a \<Rightarrow> 'b)"
  shows "eval\<^sub>l (E_conv b_of_a E) (F_conv b_of_a F) (G_conv b_of_a G) l = eval\<^sub>l E F G l"
  using assms eval\<^sub>t\<^sub>s_bij
proof (cases l)
  case (Pos p ts)
  then show ?thesis
    by (simp add: eval\<^sub>t\<^sub>s_bij assms)
next
  case (Neg p ts)
  then show ?thesis
    by (simp add: eval\<^sub>t\<^sub>s_bij assms)
qed

lemma eval\<^sub>c_bij:
  assumes "bij (b_of_a::'a \<Rightarrow> 'b)"
  shows "eval\<^sub>c (F_conv b_of_a F) (G_conv b_of_a G) C = eval\<^sub>c F G C"
proof -
  {
    fix E :: "char list \<Rightarrow> 'b"
    assume bij_b_of_a: "bij b_of_a"
    assume C_sat: "\<forall>E :: char list \<Rightarrow> 'a. \<exists>l\<in>C. eval\<^sub>l E F G l"
    have E_p: "E = E_conv b_of_a (E_conv (inv b_of_a) E)"
      unfolding E_conv_def using bij_b_of_a
      using bij_betw_inv_into_right by fastforce
    have "\<exists>l\<in>C. eval\<^sub>l (E_conv b_of_a (E_conv (inv b_of_a) E)) (F_conv b_of_a F) (G_conv b_of_a G) l"
      using eval\<^sub>l_bij bij_b_of_a C_sat by blast
    then have "\<exists>l\<in>C. eval\<^sub>l E (F_conv b_of_a F) (G_conv b_of_a G) l" using E_p by auto
  }
  then show ?thesis
    by (meson eval\<^sub>l_bij assms eval\<^sub>c_def)
qed

lemma eval\<^sub>c\<^sub>s_bij:
  assumes "bij (b_of_a::'a \<Rightarrow> 'b)"
  shows "eval\<^sub>c\<^sub>s (F_conv b_of_a F) (G_conv b_of_a G) Cs \<longleftrightarrow> eval\<^sub>c\<^sub>s F G Cs"
    by (meson eval\<^sub>c_bij assms eval\<^sub>c\<^sub>s_def)

lemma countably_inf_bij:
  assumes inf_a_uni: "infinite (UNIV :: ('a ::countable) set)"
  assumes inf_b_uni: "infinite (UNIV :: ('b ::countable) set)"
  shows "\<exists>b_of_a :: 'a \<Rightarrow> 'b. bij b_of_a"
proof -
  let ?S = "UNIV :: (('a::countable)) set"
  have "countable ?S" by auto
  moreover
  have "infinite ?S" using inf_a_uni by auto
  ultimately
  obtain nat_of_a where QWER: "bij (nat_of_a :: 'a \<Rightarrow> nat)" using countableE_infinite[of ?S] by blast

  let ?T = "UNIV :: (('b::countable)) set"
  have "countable ?T" by auto
  moreover
  have "infinite ?T" using inf_b_uni by auto
  ultimately
  obtain nat_of_b where TYUI: "bij (nat_of_b :: 'b \<Rightarrow> nat)" using countableE_infinite[of ?T] by blast

  let ?b_of_a = "\<lambda>a. (inv nat_of_b) (nat_of_a a)"

  have bij_nat_of_b: "\<forall>n. nat_of_b (inv nat_of_b n) = n"
    using TYUI bij_betw_inv_into_right by fastforce
  have "\<forall>a. inv nat_of_a (nat_of_a a) = a"
    by (meson QWER UNIV_I bij_betw_inv_into_left)
  then have "inj (\<lambda>a. inv nat_of_b (nat_of_a a))"
    using bij_nat_of_b injI by (metis (no_types))
  moreover
  have "range (\<lambda>a. inv nat_of_b (nat_of_a a)) = UNIV"
    by (metis QWER TYUI bij_def image_image inj_imp_surj_inv)
  ultimately
  have "bij ?b_of_a"
    unfolding bij_def by auto

  then show ?thesis by auto
qed

lemma infinite_hterms: "infinite (UNIV :: hterm set)"
proof -
  let ?diago = "\<lambda>n. HFun (string_of_nat n) []"
  let ?undiago = "\<lambda>a. nat_of_string (case a of HFun f ts \<Rightarrow> f)"
  have "\<forall>n. ?undiago (?diago n) = n" using nat_of_string_string_of_nat by auto
  moreover
  have "\<forall>n. ?diago n \<in> UNIV" by auto
  ultimately show "infinite (UNIV :: hterm set)" using infinity[of ?undiago ?diago UNIV] by simp
qed

theorem completeness_countable:
  assumes inf_uni: "infinite (UNIV :: ('u :: countable) set)"
  assumes finite_cs: "finite Cs" "\<forall>C\<in>Cs. finite C"
  assumes unsat: "\<forall>(F::'u fun_denot) (G::'u pred_denot). \<not>eval\<^sub>c\<^sub>s F G Cs"
  shows "\<exists>Cs'. resolution_deriv Cs Cs' \<and> {} \<in> Cs'"
proof -
  have "\<forall>(F::hterm fun_denot) (G::hterm pred_denot) . \<not>eval\<^sub>c\<^sub>s F G Cs"
  proof (rule; rule)
    fix F :: "hterm fun_denot"
    fix G :: "hterm pred_denot"

    obtain u_of_hterm :: "hterm \<Rightarrow> 'u" where p_u_of_hterm: "bij u_of_hterm"
      using countably_inf_bij inf_uni infinite_hterms by auto

    let ?F = "F_conv u_of_hterm F"
    let ?G = "G_conv u_of_hterm G"

    have "\<not> eval\<^sub>c\<^sub>s ?F ?G Cs" using unsat by auto
    then show "\<not> eval\<^sub>c\<^sub>s F G Cs" using eval\<^sub>c\<^sub>s_bij using p_u_of_hterm by auto
  qed
  then show "\<exists>Cs'. resolution_deriv Cs Cs' \<and> {} \<in> Cs'" using finite_cs completeness by auto
qed

theorem completeness_nat:
  assumes finite_cs: "finite Cs" "\<forall>C\<in>Cs. finite C"
  assumes unsat: "\<forall>(F::nat fun_denot) (G::nat pred_denot) . \<not>eval\<^sub>c\<^sub>s F G Cs"
  shows "\<exists>Cs'. resolution_deriv Cs Cs' \<and> {} \<in> Cs'"
  using assms completeness_countable by blast

end \<comment> \<open>unification locale\<close>

end


section \<open>Big-step semantics\<close>

theory Big_Step_Sterm
imports
  Rewriting_Sterm
  "../Terms/Term_as_Value"
begin

subsection \<open>Big-step semantics evaluating to irreducible @{typ sterm}s\<close>

inductive (in constructors) seval :: "srule list \<Rightarrow> (name, sterm) fmap \<Rightarrow> sterm \<Rightarrow> sterm \<Rightarrow> bool"  ("_, _/ \<turnstile>\<^sub>s/ _ \<down>/ _" [50,0,50] 50) for rs where
const: "(name, rhs) \<in> set rs \<Longrightarrow> rs, \<Gamma> \<turnstile>\<^sub>s Sconst name \<down> rhs" |
var: "fmlookup \<Gamma> name = Some val \<Longrightarrow> rs, \<Gamma> \<turnstile>\<^sub>s Svar name \<down> val" |
abs: "rs, \<Gamma> \<turnstile>\<^sub>s Sabs cs \<down> Sabs (map (\<lambda>(pat, t). (pat, subst t (fmdrop_fset (frees pat) \<Gamma>))) cs)" |
comb: "
  rs, \<Gamma> \<turnstile>\<^sub>s t \<down> Sabs cs \<Longrightarrow> rs, \<Gamma> \<turnstile>\<^sub>s u \<down> u' \<Longrightarrow>
  find_match cs u' = Some (env, _, rhs) \<Longrightarrow>
  rs, \<Gamma> ++\<^sub>f env \<turnstile>\<^sub>s rhs \<down> val \<Longrightarrow>
  rs, \<Gamma> \<turnstile>\<^sub>s t $\<^sub>s u \<down> val" |
constr: "
  name |\<in>| C \<Longrightarrow>
  list_all2 (seval rs \<Gamma>) ts us \<Longrightarrow>
  rs, \<Gamma> \<turnstile>\<^sub>s name $$ ts \<down> name $$ us"

lemma (in constructors) seval_closed:
  assumes "rs, \<Gamma> \<turnstile>\<^sub>s t \<down> u" "closed_srules rs" "closed_env \<Gamma>" "closed_except t (fmdom \<Gamma>)"
  shows "closed u"
using assms proof induction
  case (const name rhs \<Gamma>)
  thus ?case
    by (auto simp: list_all_iff)
next
  case (comb \<Gamma> t cs u u' env pat rhs val)
  hence "closed (Sabs cs)" "closed u'"
    by (auto simp: closed_except_def)
  moreover have "(pat, rhs) \<in> set cs" "match pat u' = Some env"
    using comb by (auto simp: find_match_elem)
  ultimately have "closed_except rhs (frees pat)"
    by (auto dest: closed_except_sabs)

  show ?case
    proof (rule comb)
      have "closed_env env"
        by (rule closed.match) fact+
      thus "closed_env (\<Gamma> ++\<^sub>f env)"
        using \<open>closed_env \<Gamma>\<close> by auto
    next
      have "closed_except rhs (fmdom \<Gamma> |\<union>| frees pat)"
        using \<open>closed_except rhs _\<close>
        unfolding closed_except_def by auto
      hence "closed_except rhs (fmdom \<Gamma> |\<union>| fmdom env)"
        using \<open>match pat u' = Some env\<close> by (metis match_dom)
      thus "closed_except rhs (fmdom (\<Gamma> ++\<^sub>f env))"
        using comb by simp
    qed fact
next
  case (abs \<Gamma> cs)
  show ?case
    apply (subst subst_sterm.simps[symmetric])
    apply (subst closed_except_def)
    apply (subst subst_frees)
    apply fact+
    apply (subst fminus_fsubset_conv)
    apply (subst closed_except_def[symmetric])
    apply (subst funion_fempty_right)
    apply fact
    done
next
  case (constr name \<Gamma> ts us)
  have "list_all closed us"
    using \<open>list_all2 _ _ _\<close> \<open>closed_except (list_comb _ _) _\<close>
    proof (induction ts us rule: list.rel_induct)
      case (Cons v vs u us)
      thus ?case
        using constr unfolding closed.list_comb
        by auto
    qed simp
  thus ?case
    unfolding closed.list_comb
    by (simp add: closed_except_def)
qed auto

lemma (in srules) seval_wellformed:
  assumes "rs, \<Gamma> \<turnstile>\<^sub>s t \<down> u" "wellformed t" "wellformed_env \<Gamma>"
  shows "wellformed u"
using assms proof induction
  case (const name rhs \<Gamma>)
  thus ?case
    using all_rules
    by (auto simp: list_all_iff)
next
  case (comb \<Gamma> t cs u u' env pat rhs val)
  hence "(pat, rhs) \<in> set cs" "match pat u' = Some env"
    by (auto simp: find_match_elem)

  show ?case
    proof (rule comb)
      show "wellformed rhs"
        using \<open>(pat, rhs) \<in> set cs\<close> comb
        by (auto simp: list_all_iff)
    next
      have "wellformed_env env"
        apply (rule wellformed.match)
         apply fact
        apply (rule comb)
        using comb apply simp
        apply fact+
        done
      thus "wellformed_env (\<Gamma> ++\<^sub>f env)"
        using comb by auto
    qed
next
  case (abs \<Gamma> cs)
  thus ?case
    by (metis subst_sterm.simps subst_wellformed)
next
  case (constr name \<Gamma> ts us)
  have "list_all wellformed us"
    using \<open>list_all2 _ _ _\<close> \<open>wellformed (list_comb _ _)\<close>
    proof (induction ts us rule: list.rel_induct)
      case (Cons v vs u us)
      thus ?case
        using constr by (auto simp: wellformed.list_comb app_sterm_def)
    qed simp
  thus ?case
    by (simp add: wellformed.list_comb const_sterm_def)
qed auto

lemma (in constants) seval_shadows:
  assumes "rs, \<Gamma> \<turnstile>\<^sub>s t \<down> u" "\<not> shadows_consts t"
  assumes "list_all (\<lambda>(_, rhs). \<not> shadows_consts rhs) rs"
  assumes "not_shadows_consts_env \<Gamma>"
  shows "\<not> shadows_consts u"
using assms proof induction
  case (const name rhs \<Gamma>)
  thus ?case
    unfolding srules_def
    by (auto simp: list_all_iff)
next
  case (comb \<Gamma> t cs u u' env pat rhs val)
  hence "\<not> shadows_consts (Sabs cs)" "\<not> shadows_consts u'"
    by auto
  moreover from comb have "(pat, rhs) \<in> set cs" "match pat u' = Some env"
    by (auto simp: find_match_elem)
  ultimately have "\<not> shadows_consts rhs"
    by (auto simp: list_ex_iff)

  moreover have "not_shadows_consts_env env"
    using comb \<open>match pat u' = _\<close> by (auto intro: shadows.match)

  ultimately show ?case
    using comb by blast
next
  case (abs \<Gamma> cs)
  show ?case
    apply (subst subst_sterm.simps[symmetric])
    apply (rule subst_shadows)
     apply fact+
    done
next
  case (constr name \<Gamma> ts us)
  have "list_all (Not \<circ> shadows_consts) us"
    using \<open>list_all2 _ _ _\<close> \<open>\<not> shadows_consts (name $$ ts)\<close>
    proof (induction ts us rule: list.rel_induct)
      case (Cons v vs u us)
      thus ?case
        using constr by (auto simp: shadows.list_comb const_sterm_def app_sterm_def)
    qed simp
  thus ?case
    by (auto simp: shadows.list_comb list_ex_iff list_all_iff const_sterm_def)
qed auto

lemma (in constructors) seval_list_comb_abs:
  assumes "rs, \<Gamma> \<turnstile>\<^sub>s name $$ args \<down> Sabs cs"
  shows "name \<in> dom (map_of rs)"
using assms
proof (induction \<Gamma> "name $$ args" "Sabs cs" arbitrary: args cs)
  case (constr name' _ _ us)
  hence "Sabs cs = name' $$ us" by simp
  hence False
    by (cases rule: list_comb_cases) (auto simp: const_sterm_def app_sterm_def)
  thus ?case ..
next
  case (comb \<Gamma> t cs' u u' env pat rhs)

  hence "strip_comb (t $\<^sub>s u) = strip_comb (name $$ args)"
    by simp
  hence "strip_comb t = (Sconst name, butlast args)" "u = last args"
     apply -
    subgoal
      apply (simp add: strip_list_comb_const)
      apply (fold app_sterm_def const_sterm_def)
      by (auto split: prod.splits)
    subgoal
      apply (simp add: strip_list_comb_const)
      apply (fold app_sterm_def const_sterm_def)
      by (auto split: prod.splits)
    done
  hence "t = name $$ butlast args"
    apply (fold const_sterm_def)
    by (metis list_strip_comb fst_conv snd_conv)

  thus ?case
    using comb by auto
qed (auto elim: list_comb_cases simp: const_sterm_def app_sterm_def intro: weak_map_of_SomeI)

lemma (in constructors) is_value_eval_id:
  assumes "is_value t" "closed t"
  shows "rs, \<Gamma> \<turnstile>\<^sub>s t \<down> t"
using assms proof induction
  case (abs cs)

  have "rs, \<Gamma> \<turnstile>\<^sub>s Sabs cs \<down> Sabs (map (\<lambda>(pat, t). (pat, subst t (fmdrop_fset (frees pat) \<Gamma>))) cs)"
    by (rule seval.abs)
  moreover have "subst (Sabs cs) \<Gamma> = Sabs cs"
    using abs by (metis subst_closed_id)
  ultimately show ?case
    by simp
next
  case (constr vs name)
  have "list_all2 (seval rs \<Gamma>) vs vs"
    proof (rule list.rel_refl_strong)
      fix v
      assume "v \<in> set vs"
      moreover hence "closed v"
        using constr
        unfolding closed.list_comb
        by (auto simp: list_all_iff)
      ultimately show "rs, \<Gamma> \<turnstile>\<^sub>s v \<down> v"
        using \<open>list_all _ _\<close>
        by (force simp: list_all_iff)
    qed
    with \<open>name |\<in>| C\<close> show ?case
      by (rule seval.constr)
qed

lemma (in constructors) ssubst_eval:
  assumes "rs, \<Gamma> \<turnstile>\<^sub>s t \<down> t'" "\<Gamma>' \<subseteq>\<^sub>f \<Gamma>" "closed_env \<Gamma>" "value_env \<Gamma>"
  shows "rs, \<Gamma> \<turnstile>\<^sub>s subst t \<Gamma>' \<down> t'"
using assms proof induction
  case (var \<Gamma> name val)
  show ?case
    proof (cases "fmlookup \<Gamma>' name")
      case None
      thus ?thesis
        using var by (auto intro: seval.intros)
    next
      case (Some val')
      with var have "val' = val"
        unfolding fmsubset_alt_def by force
      show ?thesis
        apply simp
        apply (subst Some)
        apply (subst \<open>val' = _\<close>)
        apply simp
        apply (rule is_value_eval_id)
        using var by auto
    qed
next
  case (abs \<Gamma> cs)
  hence "subst (subst (Sabs cs) \<Gamma>') \<Gamma> = subst (Sabs cs) \<Gamma>"
    by (metis subst_twice fmsubset_pred)
  moreover have "rs, \<Gamma> \<turnstile>\<^sub>s subst (Sabs cs) \<Gamma>' \<down> subst (subst (Sabs cs) \<Gamma>') \<Gamma>"
    apply simp
    apply (subst map_map[symmetric])
    apply (rule seval.abs)
    done
  ultimately have "rs, \<Gamma> \<turnstile>\<^sub>s subst (Sabs cs) \<Gamma>' \<down> subst (Sabs cs) \<Gamma>"
    by metis
  thus ?case by simp
next
  case (constr name \<Gamma> ts us)
  hence "list_all2 (\<lambda>t. seval rs \<Gamma> (subst t \<Gamma>')) ts us"
    by (blast intro: list.rel_mono_strong)
  with constr show ?case
    by (auto simp: subst_list_comb list_all2_map1 intro: seval.constr)
qed (auto intro: seval.intros)

lemma (in constructors) seval_agree_eq:
  assumes "rs, \<Gamma> \<turnstile>\<^sub>s t \<down> u" "fmrestrict_fset S \<Gamma> = fmrestrict_fset S \<Gamma>'" "closed_except t S"
  assumes "S |\<subseteq>| fmdom \<Gamma>" "closed_srules rs" "closed_env \<Gamma>"
  shows "rs, \<Gamma>' \<turnstile>\<^sub>s t \<down> u"
using assms proof (induction arbitrary: \<Gamma>' S)
  case (var \<Gamma> name val)
  hence "name |\<in>| S"
    by (simp add: closed_except_def)
  hence "fmlookup \<Gamma> name = fmlookup \<Gamma>' name"
    using \<open>fmrestrict_fset S \<Gamma> = _\<close>
    unfolding fmfilter_alt_defs
    including fmap.lifting
    by transfer' (auto simp: map_filter_def fun_eq_iff split: if_splits)
  with var show ?case
    by (auto intro: seval.var)
next
  case (abs \<Gamma> cs)

  \<comment> \<open>Intentionally local: not really a useful lemma outside of its scope\<close>
  have *: "fmdrop_fset S (fmrestrict_fset T m) = fmrestrict_fset (T |\<union>| S) (fmdrop_fset S m)" for S T m
    unfolding fmfilter_alt_defs fmfilter_comp
    by (rule fmfilter_cong) auto

  {
    fix pat t
    assume "(pat, t) \<in> set cs"
    with abs have "closed_except t (S |\<union>| frees pat)"
      by (auto simp: Sterm.closed_except_simps list_all_iff)

    have
      "subst t (fmdrop_fset (frees pat) (fmrestrict_fset S \<Gamma>)) = subst t (fmdrop_fset (frees pat) \<Gamma>)"
      apply (subst *)
      apply (rule subst_restrict_closed)
      apply fact
      done

    moreover have
      "subst t (fmdrop_fset (frees pat) (fmrestrict_fset S \<Gamma>')) = subst t (fmdrop_fset (frees pat) \<Gamma>')"
      apply (subst *)
      apply (rule subst_restrict_closed)
      apply fact
      done

    ultimately have "subst t (fmdrop_fset (frees pat) \<Gamma>) = subst t (fmdrop_fset (frees pat) \<Gamma>')"
      using abs by metis
  }

  hence "map (\<lambda>(pat, t). (pat, subst t (fmdrop_fset (frees pat) \<Gamma>))) cs =
         map (\<lambda>(pat, t). (pat, subst t (fmdrop_fset (frees pat) \<Gamma>'))) cs"
    by auto

  thus ?case
    by (metis seval.abs)
next
  case (comb \<Gamma> t cs u u' env pat rhs val)
  have "fmdom env = frees pat"
    apply (rule match_dom)
    apply (rule find_match_elem)
    apply fact
    done

  show ?case
    proof (rule seval.comb)
      show "rs, \<Gamma>' \<turnstile>\<^sub>s t \<down> Sabs cs" "rs, \<Gamma>' \<turnstile>\<^sub>s u \<down> u'"
        using comb by (auto simp: Sterm.closed_except_simps)
    next
      show "rs, \<Gamma>' ++\<^sub>f env \<turnstile>\<^sub>s rhs \<down> val"
        proof (rule comb)
          have "fmrestrict_fset (S |\<union>| fmdom env) (\<Gamma> ++\<^sub>f env) = fmrestrict_fset (S |\<union>| fmdom env) (\<Gamma>' ++\<^sub>f env)"
            using comb(8)
            unfolding fmfilter_alt_defs
            including fmap.lifting fset.lifting
            by transfer' (auto simp: map_filter_def fun_eq_iff map_add_def split: option.splits if_splits)

          thus "fmrestrict_fset (S |\<union>| frees pat) (\<Gamma> ++\<^sub>f env) = fmrestrict_fset (S |\<union>| frees pat) (\<Gamma>' ++\<^sub>f env)"
            unfolding \<open>fmdom env = _\<close> .
        next
          have "closed_except t S"
            using comb by (simp add: Sterm.closed_except_simps)

          have "closed (Sabs cs)"
            apply (rule seval_closed)
            apply fact+
            using \<open>closed_except t S\<close> \<open>S |\<subseteq>| fmdom \<Gamma>\<close>
            unfolding closed_except_def apply simp
            done

          have "(pat, rhs) \<in> set cs"
            using \<open>find_match _ _ = _\<close> by (rule find_match_elem)
          hence "closed_except rhs (frees pat)"
            using \<open>closed (Sabs cs)\<close> by (auto dest: closed_except_sabs)
          thus "closed_except rhs (S |\<union>| frees pat)"
            unfolding closed_except_def by auto
        next
          show "S |\<union>| frees pat |\<subseteq>| fmdom (\<Gamma> ++\<^sub>f env)"
            apply simp
            apply (intro conjI)
            using comb(10) apply blast
            unfolding \<open>fmdom env = _\<close> by blast
        next
          have "closed_except u S"
            using comb by (auto simp: closed_except_def)

          show "closed_env (\<Gamma> ++\<^sub>f env)"
            apply rule
             apply fact
            apply (rule closed.match[where t = u' and pat = pat])
            subgoal
              by (rule find_match_elem) fact
            subgoal
              apply (rule seval_closed)
                 apply fact+
              using \<open>closed_except u S\<close> \<open>S |\<subseteq>| fmdom \<Gamma>\<close> unfolding closed_except_def by blast
            done
        qed fact
    qed fact
next
  case (constr name \<Gamma> ts us)
  show ?case
    apply (rule seval.constr)
     apply fact
    apply (rule list.rel_mono_strong)
     apply fact
    using constr
    unfolding closed.list_comb list_all_iff
    by auto
qed (auto intro: seval.intros)

subsubsection \<open>Correctness wrt @{const srewrite}\<close>

context srules begin context begin

private lemma seval_correct0:
  assumes "rs, \<Gamma> \<turnstile>\<^sub>s t \<down> u" "closed_except t (fmdom \<Gamma>)" "closed_env \<Gamma>"
  shows "rs \<turnstile>\<^sub>s subst t \<Gamma> \<longrightarrow>* u"
using assms proof induction
  case (const name rhs \<Gamma>)

  have "srewrite_step rs name rhs"
    by (rule srewrite_stepI) fact
  thus ?case
    by (auto intro: srewrite.intros)
next
  case (comb \<Gamma> t cs u u' env pat rhs val)
  hence "closed_except t (fmdom \<Gamma>)" "closed_except u (fmdom \<Gamma>)"
    by (simp add: Sterm.closed_except_simps)+
  moreover have "closed_srules rs"
    using all_rules
    unfolding list_all_iff by fastforce
  ultimately have "closed (Sabs cs)" "closed u'"
    using comb by (metis seval_closed)+

  from comb have "(pat, rhs) \<in> set cs" "match pat u' = Some env"
    by (auto simp: find_match_elem)
  hence "closed_except rhs (frees pat)"
    using \<open>closed (Sabs cs)\<close> by (auto dest: closed_except_sabs)
  hence "frees rhs |\<subseteq>| frees pat"
    by (simp add: closed_except_def)
  moreover have "fmdom env = frees pat"
    using \<open>match pat u' = _\<close> by (auto simp: match_dom)
  ultimately have "frees rhs |\<subseteq>| fmdom env"
    by simp
  hence "subst rhs (\<Gamma> ++\<^sub>f env) = subst rhs env"
    by (rule subst_add_shadowed_env)

  have "rs \<turnstile>\<^sub>s subst t \<Gamma> $\<^sub>s subst u \<Gamma> \<longrightarrow>* Sabs cs $\<^sub>s u'"
    using comb by (force intro: srewrite.rt_comb[unfolded app_sterm_def] simp: Sterm.closed_except_simps)
  also have "rs \<turnstile>\<^sub>s Sabs cs $\<^sub>s u' \<longrightarrow>* subst rhs env"
    using comb \<open>closed u'\<close> by (force intro: srewrite.beta find_match_rewrite_first)
  also have "rs \<turnstile>\<^sub>s subst rhs env \<longrightarrow>* subst rhs (\<Gamma> ++\<^sub>f env)"
    unfolding \<open>subst rhs (\<Gamma> ++\<^sub>f env) = _\<close> by simp
  also have "rs \<turnstile>\<^sub>s subst rhs (\<Gamma> ++\<^sub>f env) \<longrightarrow>* val"
    proof (rule comb)
      show "closed_except rhs (fmdom (\<Gamma> ++\<^sub>f env))"
        using comb \<open>match pat u' = Some env\<close> \<open>fmdom env = _\<close> \<open>frees rhs |\<subseteq>| frees pat\<close>
        by (auto simp: closed_except_def)
    next
      show "closed_env (\<Gamma> ++\<^sub>f env)"
        using comb \<open>match pat u' = Some env\<close> \<open>closed u'\<close>
        by (blast intro: closed.match)
    qed

  finally show ?case by simp
next
  case (constr name \<Gamma> ts us)
  show ?case
    apply (simp add: subst_list_comb)
    apply (rule srewrite.rt_list_comb)
    subgoal
      apply (simp add: list.rel_map)
      apply (rule list.rel_mono_strong[OF constr(2)])
      apply clarify
      apply (elim impE)
      using constr(3) apply (erule closed.list_combE)
       apply (rule constr)+
      apply (auto simp: const_sterm_def)
      done
    subgoal by auto
    done
qed auto

corollary seval_correct:
  assumes "rs, fmempty \<turnstile>\<^sub>s t \<down> u" "closed t"
  shows "rs \<turnstile>\<^sub>s t \<longrightarrow>* u"
proof -
  have "closed_except t (fmdom fmempty)"
    using assms by simp
  with assms have "rs \<turnstile>\<^sub>s subst t fmempty \<longrightarrow>* u"
    by (fastforce intro!: seval_correct0)
  thus ?thesis
    by simp
qed

end end

end


(*  Title:      HOL/Statespace/StateSpaceEx.thy
    Author:     Norbert Schirmer, TU Muenchen
*)

section \<open>Examples \label{sec:Examples}\<close>
theory StateSpaceEx
imports StateSpaceLocale StateSpaceSyntax
begin

(*<*)
syntax
 "_statespace_updates" :: "('a \<Rightarrow> 'b) \<Rightarrow> updbinds \<Rightarrow> ('a \<Rightarrow> 'b)" ("_\<langle>_\<rangle>" [900,0] 900)
(*>*)

text \<open>Did you ever dream about records with multiple inheritance?
Then you should definitely have a look at statespaces. They may be
what you are dreaming of. Or at least almost \dots\<close>

text \<open>Isabelle allows to add new top-level commands to the
system. Building on the locale infrastructure, we provide a command
\<^theory_text>\<open>statespace\<close> like this:\<close>

statespace vars =
  n::nat
  b::bool

print_locale vars_namespace
print_locale vars_valuetypes
print_locale vars

text \<open>\noindent This resembles a \<^theory_text>\<open>record\<close> definition,
but introduces sophisticated locale
infrastructure instead of HOL type schemes.  The resulting context
postulates two distinct names @{term "n"} and @{term "b"} and
projection~/ injection functions that convert from abstract values to
@{typ "nat"} and \<open>bool\<close>. The logical content of the locale is:\<close>

locale vars' =
  fixes n::'name and b::'name
  assumes "distinct [n, b]"

  fixes project_nat::"'value \<Rightarrow> nat" and inject_nat::"nat \<Rightarrow> 'value"
  assumes "\<And>n. project_nat (inject_nat n) = n"

  fixes project_bool::"'value \<Rightarrow> bool" and inject_bool::"bool \<Rightarrow> 'value"
  assumes "\<And>b. project_bool (inject_bool b) = b"

text \<open>\noindent The HOL predicate @{const "distinct"} describes
distinctness of all names in the context.  Locale \<open>vars'\<close>
defines the raw logical content that is defined in the state space
locale. We also maintain non-logical context information to support
the user:

\begin{itemize}

\item Syntax for state lookup and updates that automatically inserts
the corresponding projection and injection functions.

\item Setup for the proof tools that exploit the distinctness
information and the cancellation of projections and injections in
deductions and simplifications.

\end{itemize}

This extra-logical information is added to the locale in form of
declarations, which associate the name of a variable to the
corresponding projection and injection functions to handle the syntax
transformations, and a link from the variable name to the
corresponding distinctness theorem. As state spaces are merged or
extended there are multiple distinctness theorems in the context. Our
declarations take care that the link always points to the strongest
distinctness assumption.  With these declarations in place, a lookup
can be written as \<open>s\<cdot>n\<close>, which is translated to \<open>project_nat (s n)\<close>, and an update as \<open>s\<langle>n := 2\<rangle>\<close>, which is
translated to \<open>s(n := inject_nat 2)\<close>. We can now establish the
following lemma:\<close>

lemma (in vars) foo: "s<n := 2>\<cdot>b = s\<cdot>b" by simp

text \<open>\noindent Here the simplifier was able to refer to
distinctness of @{term "b"} and @{term "n"} to solve the equation.
The resulting lemma is also recorded in locale \<open>vars\<close> for
later use and is automatically propagated to all its interpretations.
Here is another example:\<close>

statespace 'a varsX = NB: vars [n=N, b=B] + vars + x::'a

text \<open>\noindent The state space \<open>varsX\<close> imports two copies
of the state space \<open>vars\<close>, where one has the variables renamed
to upper-case letters, and adds another variable @{term "x"} of type
@{typ "'a"}. This type is fixed inside the state space but may get
instantiated later on, analogous to type parameters of an ML-functor.
The distinctness assumption is now \<open>distinct [N, B, n, b, x]\<close>,
from this we can derive both @{term "distinct [N,B]"} and @{term
"distinct [n,b]"}, the distinction assumptions for the two versions of
locale \<open>vars\<close> above.  Moreover we have all necessary
projection and injection assumptions available. These assumptions
together allow us to establish state space @{term "varsX"} as an
interpretation of both instances of locale @{term "vars"}. Hence we
inherit both variants of theorem \<open>foo\<close>: \<open>s\<langle>N := 2\<rangle>\<cdot>B =
s\<cdot>B\<close> as well as \<open>s\<langle>n := 2\<rangle>\<cdot>b = s\<cdot>b\<close>. These are immediate
consequences of the locale interpretation action.

The declarations for syntax and the distinctness theorems also observe
the morphisms generated by the locale package due to the renaming
@{term "n = N"}:\<close>

lemma (in varsX) foo: "s\<langle>N := 2\<rangle>\<cdot>x = s\<cdot>x" by simp

text \<open>To assure scalability towards many distinct names, the
distinctness predicate is refined to operate on balanced trees. Thus
we get logarithmic certificates for the distinctness of two names by
the distinctness of the paths in the tree. Asked for the distinctness
of two names, our tool produces the paths of the variables in the tree
(this is implemented in SML, outside the logic) and returns a
certificate corresponding to the different paths.  Merging state
spaces requires to prove that the combined distinctness assumption
implies the distinctness assumptions of the components.  Such a proof
is of the order $m \cdot \log n$, where $n$ and $m$ are the number of
nodes in the larger and smaller tree, respectively.\<close>

text \<open>We continue with more examples.\<close>

statespace 'a foo =
  f::"nat\<Rightarrow>nat"
  a::int
  b::nat
  c::'a

lemma (in foo) foo1:
  shows "s\<langle>a := i\<rangle>\<cdot>a = i"
  by simp

lemma (in foo) foo2:
  shows "(s\<langle>a:=i\<rangle>)\<cdot>a = i"
  by simp

lemma (in foo) foo3:
  shows "(s\<langle>a:=i\<rangle>)\<cdot>b = s\<cdot>b"
  by simp

lemma (in foo) foo4:
  shows "(s\<langle>a:=i,b:=j,c:=k,a:=x\<rangle>) = (s\<langle>b:=j,c:=k,a:=x\<rangle>)"
  by simp

statespace bar =
  b::bool
  c::string

lemma (in bar) bar1:
  shows "(s\<langle>b:=True\<rangle>)\<cdot>c = s\<cdot>c"
  by simp

text \<open>You can define a derived state space by inheriting existing state spaces, renaming
of components if you like, and by declaring new components.
\<close>

statespace ('a,'b) loo = 'a foo + bar [b=B,c=C] +
  X::'b

lemma (in loo) loo1:
  shows "s\<langle>a:=i\<rangle>\<cdot>B = s\<cdot>B"
proof -
  thm foo1
  txt \<open>The Lemma @{thm [source] foo1} from the parent state space
         is also available here: \begin{center}@{thm foo1}\end{center}\<close>
  have "s<a:=i>\<cdot>a = i"
    by (rule foo1)
  thm bar1
  txt \<open>Note the renaming of the parameters in Lemma @{thm [source] bar1}:
         \begin{center}@{thm bar1}\end{center}\<close>
  have "s<B:=True>\<cdot>C = s\<cdot>C"
    by (rule bar1)
  show ?thesis
    by simp
qed

statespace 'a dup = FA: 'a foo [f=F, a=A] + 'a foo +
  x::int

lemma (in dup)
 shows "s<a := i>\<cdot>x = s\<cdot>x"
  by simp

lemma (in dup)
 shows "s<A := i>\<cdot>a = s\<cdot>a"
  by simp

lemma (in dup)
 shows "s<A := i>\<cdot>x = s\<cdot>x"
  by simp

(*
text "Hmm, I hoped this would work now..."

locale fooX = foo +
 assumes "s<a:=i>\<cdot>b = k"
*)

(* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *)
text \<open>There are known problems with syntax-declarations. They currently
only work, when the context is already built. Hopefully this will be
implemented correctly in future Isabelle versions.\<close>

(*
lemma
  assumes "foo f a b c p1 i1 p2 i2 p3 i3 p4 i4"
  shows True
proof
  interpret foo [f a b c p1 i1 p2 i2 p3 i3 p4 i4] by fact
  term "s<a := i>\<cdot>a = i"
qed
*)
(*
lemma
  includes foo
  shows "s<a := i>\<cdot>a = i"
*)

text \<open>It would be nice to have nested state spaces. This is
logically no problem. From the locale-implementation side this may be
something like an 'includes' into a locale. When there is a more
elaborate locale infrastructure in place this may be an easy exercise.
\<close>

subsection \<open>Benchmarks\<close>

text \<open>Here are some bigger examples for benchmarking.\<close>

ML \<open>
  fun make_benchmark n =
    writeln (Active.sendback_markup_command
      ("statespace benchmark" ^ string_of_int n ^ " =\n" ^
        (cat_lines (map (fn i => "A" ^ string_of_int i ^ "::nat") (1 upto n)))));
\<close>

text "0.2s"
statespace benchmark100 = A1::nat A2::nat A3::nat A4::nat A5::nat
A6::nat A7::nat A8::nat A9::nat A10::nat A11::nat A12::nat A13::nat
A14::nat A15::nat A16::nat A17::nat A18::nat A19::nat A20::nat
A21::nat A22::nat A23::nat A24::nat A25::nat A26::nat A27::nat
A28::nat A29::nat A30::nat A31::nat A32::nat A33::nat A34::nat
A35::nat A36::nat A37::nat A38::nat A39::nat A40::nat A41::nat
A42::nat A43::nat A44::nat A45::nat A46::nat A47::nat A48::nat
A49::nat A50::nat A51::nat A52::nat A53::nat A54::nat A55::nat
A56::nat A57::nat A58::nat A59::nat A60::nat A61::nat A62::nat
A63::nat A64::nat A65::nat A66::nat A67::nat A68::nat A69::nat
A70::nat A71::nat A72::nat A73::nat A74::nat A75::nat A76::nat
A77::nat A78::nat A79::nat A80::nat A81::nat A82::nat A83::nat
A84::nat A85::nat A86::nat A87::nat A88::nat A89::nat A90::nat
A91::nat A92::nat A93::nat A94::nat A95::nat A96::nat A97::nat
A98::nat A99::nat A100::nat

text "2.4s"
statespace benchmark500 = A1::nat A2::nat A3::nat A4::nat A5::nat
A6::nat A7::nat A8::nat A9::nat A10::nat A11::nat A12::nat A13::nat
A14::nat A15::nat A16::nat A17::nat A18::nat A19::nat A20::nat
A21::nat A22::nat A23::nat A24::nat A25::nat A26::nat A27::nat
A28::nat A29::nat A30::nat A31::nat A32::nat A33::nat A34::nat
A35::nat A36::nat A37::nat A38::nat A39::nat A40::nat A41::nat
A42::nat A43::nat A44::nat A45::nat A46::nat A47::nat A48::nat
A49::nat A50::nat A51::nat A52::nat A53::nat A54::nat A55::nat
A56::nat A57::nat A58::nat A59::nat A60::nat A61::nat A62::nat
A63::nat A64::nat A65::nat A66::nat A67::nat A68::nat A69::nat
A70::nat A71::nat A72::nat A73::nat A74::nat A75::nat A76::nat
A77::nat A78::nat A79::nat A80::nat A81::nat A82::nat A83::nat
A84::nat A85::nat A86::nat A87::nat A88::nat A89::nat A90::nat
A91::nat A92::nat A93::nat A94::nat A95::nat A96::nat A97::nat
A98::nat A99::nat A100::nat A101::nat A102::nat A103::nat A104::nat
A105::nat A106::nat A107::nat A108::nat A109::nat A110::nat A111::nat
A112::nat A113::nat A114::nat A115::nat A116::nat A117::nat A118::nat
A119::nat A120::nat A121::nat A122::nat A123::nat A124::nat A125::nat
A126::nat A127::nat A128::nat A129::nat A130::nat A131::nat A132::nat
A133::nat A134::nat A135::nat A136::nat A137::nat A138::nat A139::nat
A140::nat A141::nat A142::nat A143::nat A144::nat A145::nat A146::nat
A147::nat A148::nat A149::nat A150::nat A151::nat A152::nat A153::nat
A154::nat A155::nat A156::nat A157::nat A158::nat A159::nat A160::nat
A161::nat A162::nat A163::nat A164::nat A165::nat A166::nat A167::nat
A168::nat A169::nat A170::nat A171::nat A172::nat A173::nat A174::nat
A175::nat A176::nat A177::nat A178::nat A179::nat A180::nat A181::nat
A182::nat A183::nat A184::nat A185::nat A186::nat A187::nat A188::nat
A189::nat A190::nat A191::nat A192::nat A193::nat A194::nat A195::nat
A196::nat A197::nat A198::nat A199::nat A200::nat A201::nat A202::nat
A203::nat A204::nat A205::nat A206::nat A207::nat A208::nat A209::nat
A210::nat A211::nat A212::nat A213::nat A214::nat A215::nat A216::nat
A217::nat A218::nat A219::nat A220::nat A221::nat A222::nat A223::nat
A224::nat A225::nat A226::nat A227::nat A228::nat A229::nat A230::nat
A231::nat A232::nat A233::nat A234::nat A235::nat A236::nat A237::nat
A238::nat A239::nat A240::nat A241::nat A242::nat A243::nat A244::nat
A245::nat A246::nat A247::nat A248::nat A249::nat A250::nat A251::nat
A252::nat A253::nat A254::nat A255::nat A256::nat A257::nat A258::nat
A259::nat A260::nat A261::nat A262::nat A263::nat A264::nat A265::nat
A266::nat A267::nat A268::nat A269::nat A270::nat A271::nat A272::nat
A273::nat A274::nat A275::nat A276::nat A277::nat A278::nat A279::nat
A280::nat A281::nat A282::nat A283::nat A284::nat A285::nat A286::nat
A287::nat A288::nat A289::nat A290::nat A291::nat A292::nat A293::nat
A294::nat A295::nat A296::nat A297::nat A298::nat A299::nat A300::nat
A301::nat A302::nat A303::nat A304::nat A305::nat A306::nat A307::nat
A308::nat A309::nat A310::nat A311::nat A312::nat A313::nat A314::nat
A315::nat A316::nat A317::nat A318::nat A319::nat A320::nat A321::nat
A322::nat A323::nat A324::nat A325::nat A326::nat A327::nat A328::nat
A329::nat A330::nat A331::nat A332::nat A333::nat A334::nat A335::nat
A336::nat A337::nat A338::nat A339::nat A340::nat A341::nat A342::nat
A343::nat A344::nat A345::nat A346::nat A347::nat A348::nat A349::nat
A350::nat A351::nat A352::nat A353::nat A354::nat A355::nat A356::nat
A357::nat A358::nat A359::nat A360::nat A361::nat A362::nat A363::nat
A364::nat A365::nat A366::nat A367::nat A368::nat A369::nat A370::nat
A371::nat A372::nat A373::nat A374::nat A375::nat A376::nat A377::nat
A378::nat A379::nat A380::nat A381::nat A382::nat A383::nat A384::nat
A385::nat A386::nat A387::nat A388::nat A389::nat A390::nat A391::nat
A392::nat A393::nat A394::nat A395::nat A396::nat A397::nat A398::nat
A399::nat A400::nat A401::nat A402::nat A403::nat A404::nat A405::nat
A406::nat A407::nat A408::nat A409::nat A410::nat A411::nat A412::nat
A413::nat A414::nat A415::nat A416::nat A417::nat A418::nat A419::nat
A420::nat A421::nat A422::nat A423::nat A424::nat A425::nat A426::nat
A427::nat A428::nat A429::nat A430::nat A431::nat A432::nat A433::nat
A434::nat A435::nat A436::nat A437::nat A438::nat A439::nat A440::nat
A441::nat A442::nat A443::nat A444::nat A445::nat A446::nat A447::nat
A448::nat A449::nat A450::nat A451::nat A452::nat A453::nat A454::nat
A455::nat A456::nat A457::nat A458::nat A459::nat A460::nat A461::nat
A462::nat A463::nat A464::nat A465::nat A466::nat A467::nat A468::nat
A469::nat A470::nat A471::nat A472::nat A473::nat A474::nat A475::nat
A476::nat A477::nat A478::nat A479::nat A480::nat A481::nat A482::nat
A483::nat A484::nat A485::nat A486::nat A487::nat A488::nat A489::nat
A490::nat A491::nat A492::nat A493::nat A494::nat A495::nat A496::nat
A497::nat A498::nat A499::nat A500::nat

text "9.0s"
statespace benchmark1000 = A1::nat A2::nat A3::nat A4::nat A5::nat
A6::nat A7::nat A8::nat A9::nat A10::nat A11::nat A12::nat A13::nat
A14::nat A15::nat A16::nat A17::nat A18::nat A19::nat A20::nat
A21::nat A22::nat A23::nat A24::nat A25::nat A26::nat A27::nat
A28::nat A29::nat A30::nat A31::nat A32::nat A33::nat A34::nat
A35::nat A36::nat A37::nat A38::nat A39::nat A40::nat A41::nat
A42::nat A43::nat A44::nat A45::nat A46::nat A47::nat A48::nat
A49::nat A50::nat A51::nat A52::nat A53::nat A54::nat A55::nat
A56::nat A57::nat A58::nat A59::nat A60::nat A61::nat A62::nat
A63::nat A64::nat A65::nat A66::nat A67::nat A68::nat A69::nat
A70::nat A71::nat A72::nat A73::nat A74::nat A75::nat A76::nat
A77::nat A78::nat A79::nat A80::nat A81::nat A82::nat A83::nat
A84::nat A85::nat A86::nat A87::nat A88::nat A89::nat A90::nat
A91::nat A92::nat A93::nat A94::nat A95::nat A96::nat A97::nat
A98::nat A99::nat A100::nat A101::nat A102::nat A103::nat A104::nat
A105::nat A106::nat A107::nat A108::nat A109::nat A110::nat A111::nat
A112::nat A113::nat A114::nat A115::nat A116::nat A117::nat A118::nat
A119::nat A120::nat A121::nat A122::nat A123::nat A124::nat A125::nat
A126::nat A127::nat A128::nat A129::nat A130::nat A131::nat A132::nat
A133::nat A134::nat A135::nat A136::nat A137::nat A138::nat A139::nat
A140::nat A141::nat A142::nat A143::nat A144::nat A145::nat A146::nat
A147::nat A148::nat A149::nat A150::nat A151::nat A152::nat A153::nat
A154::nat A155::nat A156::nat A157::nat A158::nat A159::nat A160::nat
A161::nat A162::nat A163::nat A164::nat A165::nat A166::nat A167::nat
A168::nat A169::nat A170::nat A171::nat A172::nat A173::nat A174::nat
A175::nat A176::nat A177::nat A178::nat A179::nat A180::nat A181::nat
A182::nat A183::nat A184::nat A185::nat A186::nat A187::nat A188::nat
A189::nat A190::nat A191::nat A192::nat A193::nat A194::nat A195::nat
A196::nat A197::nat A198::nat A199::nat A200::nat A201::nat A202::nat
A203::nat A204::nat A205::nat A206::nat A207::nat A208::nat A209::nat
A210::nat A211::nat A212::nat A213::nat A214::nat A215::nat A216::nat
A217::nat A218::nat A219::nat A220::nat A221::nat A222::nat A223::nat
A224::nat A225::nat A226::nat A227::nat A228::nat A229::nat A230::nat
A231::nat A232::nat A233::nat A234::nat A235::nat A236::nat A237::nat
A238::nat A239::nat A240::nat A241::nat A242::nat A243::nat A244::nat
A245::nat A246::nat A247::nat A248::nat A249::nat A250::nat A251::nat
A252::nat A253::nat A254::nat A255::nat A256::nat A257::nat A258::nat
A259::nat A260::nat A261::nat A262::nat A263::nat A264::nat A265::nat
A266::nat A267::nat A268::nat A269::nat A270::nat A271::nat A272::nat
A273::nat A274::nat A275::nat A276::nat A277::nat A278::nat A279::nat
A280::nat A281::nat A282::nat A283::nat A284::nat A285::nat A286::nat
A287::nat A288::nat A289::nat A290::nat A291::nat A292::nat A293::nat
A294::nat A295::nat A296::nat A297::nat A298::nat A299::nat A300::nat
A301::nat A302::nat A303::nat A304::nat A305::nat A306::nat A307::nat
A308::nat A309::nat A310::nat A311::nat A312::nat A313::nat A314::nat
A315::nat A316::nat A317::nat A318::nat A319::nat A320::nat A321::nat
A322::nat A323::nat A324::nat A325::nat A326::nat A327::nat A328::nat
A329::nat A330::nat A331::nat A332::nat A333::nat A334::nat A335::nat
A336::nat A337::nat A338::nat A339::nat A340::nat A341::nat A342::nat
A343::nat A344::nat A345::nat A346::nat A347::nat A348::nat A349::nat
A350::nat A351::nat A352::nat A353::nat A354::nat A355::nat A356::nat
A357::nat A358::nat A359::nat A360::nat A361::nat A362::nat A363::nat
A364::nat A365::nat A366::nat A367::nat A368::nat A369::nat A370::nat
A371::nat A372::nat A373::nat A374::nat A375::nat A376::nat A377::nat
A378::nat A379::nat A380::nat A381::nat A382::nat A383::nat A384::nat
A385::nat A386::nat A387::nat A388::nat A389::nat A390::nat A391::nat
A392::nat A393::nat A394::nat A395::nat A396::nat A397::nat A398::nat
A399::nat A400::nat A401::nat A402::nat A403::nat A404::nat A405::nat
A406::nat A407::nat A408::nat A409::nat A410::nat A411::nat A412::nat
A413::nat A414::nat A415::nat A416::nat A417::nat A418::nat A419::nat
A420::nat A421::nat A422::nat A423::nat A424::nat A425::nat A426::nat
A427::nat A428::nat A429::nat A430::nat A431::nat A432::nat A433::nat
A434::nat A435::nat A436::nat A437::nat A438::nat A439::nat A440::nat
A441::nat A442::nat A443::nat A444::nat A445::nat A446::nat A447::nat
A448::nat A449::nat A450::nat A451::nat A452::nat A453::nat A454::nat
A455::nat A456::nat A457::nat A458::nat A459::nat A460::nat A461::nat
A462::nat A463::nat A464::nat A465::nat A466::nat A467::nat A468::nat
A469::nat A470::nat A471::nat A472::nat A473::nat A474::nat A475::nat
A476::nat A477::nat A478::nat A479::nat A480::nat A481::nat A482::nat
A483::nat A484::nat A485::nat A486::nat A487::nat A488::nat A489::nat
A490::nat A491::nat A492::nat A493::nat A494::nat A495::nat A496::nat
A497::nat A498::nat A499::nat A500::nat A501::nat A502::nat A503::nat
A504::nat A505::nat A506::nat A507::nat A508::nat A509::nat A510::nat
A511::nat A512::nat A513::nat A514::nat A515::nat A516::nat A517::nat
A518::nat A519::nat A520::nat A521::nat A522::nat A523::nat A524::nat
A525::nat A526::nat A527::nat A528::nat A529::nat A530::nat A531::nat
A532::nat A533::nat A534::nat A535::nat A536::nat A537::nat A538::nat
A539::nat A540::nat A541::nat A542::nat A543::nat A544::nat A545::nat
A546::nat A547::nat A548::nat A549::nat A550::nat A551::nat A552::nat
A553::nat A554::nat A555::nat A556::nat A557::nat A558::nat A559::nat
A560::nat A561::nat A562::nat A563::nat A564::nat A565::nat A566::nat
A567::nat A568::nat A569::nat A570::nat A571::nat A572::nat A573::nat
A574::nat A575::nat A576::nat A577::nat A578::nat A579::nat A580::nat
A581::nat A582::nat A583::nat A584::nat A585::nat A586::nat A587::nat
A588::nat A589::nat A590::nat A591::nat A592::nat A593::nat A594::nat
A595::nat A596::nat A597::nat A598::nat A599::nat A600::nat A601::nat
A602::nat A603::nat A604::nat A605::nat A606::nat A607::nat A608::nat
A609::nat A610::nat A611::nat A612::nat A613::nat A614::nat A615::nat
A616::nat A617::nat A618::nat A619::nat A620::nat A621::nat A622::nat
A623::nat A624::nat A625::nat A626::nat A627::nat A628::nat A629::nat
A630::nat A631::nat A632::nat A633::nat A634::nat A635::nat A636::nat
A637::nat A638::nat A639::nat A640::nat A641::nat A642::nat A643::nat
A644::nat A645::nat A646::nat A647::nat A648::nat A649::nat A650::nat
A651::nat A652::nat A653::nat A654::nat A655::nat A656::nat A657::nat
A658::nat A659::nat A660::nat A661::nat A662::nat A663::nat A664::nat
A665::nat A666::nat A667::nat A668::nat A669::nat A670::nat A671::nat
A672::nat A673::nat A674::nat A675::nat A676::nat A677::nat A678::nat
A679::nat A680::nat A681::nat A682::nat A683::nat A684::nat A685::nat
A686::nat A687::nat A688::nat A689::nat A690::nat A691::nat A692::nat
A693::nat A694::nat A695::nat A696::nat A697::nat A698::nat A699::nat
A700::nat A701::nat A702::nat A703::nat A704::nat A705::nat A706::nat
A707::nat A708::nat A709::nat A710::nat A711::nat A712::nat A713::nat
A714::nat A715::nat A716::nat A717::nat A718::nat A719::nat A720::nat
A721::nat A722::nat A723::nat A724::nat A725::nat A726::nat A727::nat
A728::nat A729::nat A730::nat A731::nat A732::nat A733::nat A734::nat
A735::nat A736::nat A737::nat A738::nat A739::nat A740::nat A741::nat
A742::nat A743::nat A744::nat A745::nat A746::nat A747::nat A748::nat
A749::nat A750::nat A751::nat A752::nat A753::nat A754::nat A755::nat
A756::nat A757::nat A758::nat A759::nat A760::nat A761::nat A762::nat
A763::nat A764::nat A765::nat A766::nat A767::nat A768::nat A769::nat
A770::nat A771::nat A772::nat A773::nat A774::nat A775::nat A776::nat
A777::nat A778::nat A779::nat A780::nat A781::nat A782::nat A783::nat
A784::nat A785::nat A786::nat A787::nat A788::nat A789::nat A790::nat
A791::nat A792::nat A793::nat A794::nat A795::nat A796::nat A797::nat
A798::nat A799::nat A800::nat A801::nat A802::nat A803::nat A804::nat
A805::nat A806::nat A807::nat A808::nat A809::nat A810::nat A811::nat
A812::nat A813::nat A814::nat A815::nat A816::nat A817::nat A818::nat
A819::nat A820::nat A821::nat A822::nat A823::nat A824::nat A825::nat
A826::nat A827::nat A828::nat A829::nat A830::nat A831::nat A832::nat
A833::nat A834::nat A835::nat A836::nat A837::nat A838::nat A839::nat
A840::nat A841::nat A842::nat A843::nat A844::nat A845::nat A846::nat
A847::nat A848::nat A849::nat A850::nat A851::nat A852::nat A853::nat
A854::nat A855::nat A856::nat A857::nat A858::nat A859::nat A860::nat
A861::nat A862::nat A863::nat A864::nat A865::nat A866::nat A867::nat
A868::nat A869::nat A870::nat A871::nat A872::nat A873::nat A874::nat
A875::nat A876::nat A877::nat A878::nat A879::nat A880::nat A881::nat
A882::nat A883::nat A884::nat A885::nat A886::nat A887::nat A888::nat
A889::nat A890::nat A891::nat A892::nat A893::nat A894::nat A895::nat
A896::nat A897::nat A898::nat A899::nat A900::nat A901::nat A902::nat
A903::nat A904::nat A905::nat A906::nat A907::nat A908::nat A909::nat
A910::nat A911::nat A912::nat A913::nat A914::nat A915::nat A916::nat
A917::nat A918::nat A919::nat A920::nat A921::nat A922::nat A923::nat
A924::nat A925::nat A926::nat A927::nat A928::nat A929::nat A930::nat
A931::nat A932::nat A933::nat A934::nat A935::nat A936::nat A937::nat
A938::nat A939::nat A940::nat A941::nat A942::nat A943::nat A944::nat
A945::nat A946::nat A947::nat A948::nat A949::nat A950::nat A951::nat
A952::nat A953::nat A954::nat A955::nat A956::nat A957::nat A958::nat
A959::nat A960::nat A961::nat A962::nat A963::nat A964::nat A965::nat
A966::nat A967::nat A968::nat A969::nat A970::nat A971::nat A972::nat
A973::nat A974::nat A975::nat A976::nat A977::nat A978::nat A979::nat
A980::nat A981::nat A982::nat A983::nat A984::nat A985::nat A986::nat
A987::nat A988::nat A989::nat A990::nat A991::nat A992::nat A993::nat
A994::nat A995::nat A996::nat A997::nat A998::nat A999::nat A1000::nat

lemma (in benchmark100) test: "s<A1 := a>\<cdot>A100 = s\<cdot>A100" by simp
lemma (in benchmark500) test: "s<A1 := a>\<cdot>A100 = s\<cdot>A100" by simp
lemma (in benchmark1000) test: "s<A1 := a>\<cdot>A100 = s\<cdot>A100" by simp

end