Theory Linear_Unification

theory Linear_Unification
  imports
    First_Order_Terms.Term_More
begin


section ‹Linear unification›

text ‹This section defines a linear unification criteria, which is similar to standard unification
      under the assumption that terms do not feature repeated occurrences of variables. This
      criteria is used as an overapproximation in so-called imperfect filter indexing techniques.›

definition term_unification :: ‹('f, 'v) term ⇒ ('f, 'v) term ⇒ bool› where
  ‹term_unification t1 t2 = (∃σ :: ('f, 'v) subst. t1 ⋅ σ = t2 ⋅ σ)›

definition term_instantiation :: ‹('f, 'v) term ⇒ ('f, 'v) term ⇒ bool› where
  ‹term_instantiation t1 t2 = (∃σ :: ('f, 'v) subst. t1 = t2 ⋅ σ)›

definition term_generalization :: ‹('f, 'v) term ⇒ ('f, 'v) term ⇒ bool› where
  ‹term_generalization t1 t2 = term_instantiation t2 t1›

definition term_variant :: ‹('f, 'v) term ⇒ ('f, 'v) term ⇒ bool› where
  ‹term_variant t1 t2 = (∃σ :: ('f, 'v) subst. inj σ ∧ (∀t ∈ subst_range σ. is_Var t) ∧ t1 ⋅ σ = t2)›

inductive linear_instance :: ‹('f, 'v) term ⇒ ('f, 'v) term ⇒ bool› where
  li_var: ‹linear_instance t (Var x)›
| li_func: ‹length args1 = length args2 ⟹ (∀i. i < length args1 ⟶ linear_instance (args1 ! i) (args2 ! i)) ⟹ linear_instance (Fun f args1) (Fun f args2)›

definition linear_generalization :: ‹('f, 'v) term ⇒ ('f, 'v) term ⇒ bool› where
  ‹linear_generalization t1 t2 = linear_instance t2 t1›

inductive linear_unification :: ‹('f, 'v) term ⇒ ('f, 'v) term ⇒ bool› where
  lu_varl: ‹linear_unification (Var x) t›
| lu_varr: ‹linear_unification t (Var x)›
| lu_func: ‹length args1 = length args2 ⟹ (∀i. i < length args1 ⟶ linear_unification (args1 ! i) (args2 ! i)) ⟹ linear_unification (Fun f args1) (Fun f args2)›

inductive linear_variant :: ‹('f, 'v) term ⇒ ('f, 'v) term ⇒ bool› where
  lv_var: ‹linear_variant (Var x) (Var y)›
| lv_func: ‹length args1 = length args2 ⟹ (∀i. i < length args1 ⟶ linear_variant (args1 ! i) (args2 ! i)) ⟹ linear_variant (Fun f args1) (Fun f args2)›

lemma linear_unification_necessary_condition:
  ‹term_unification t1 t2 ⟹ linear_unification t1 t2›
proof (induction t1 arbitrary: t2)
  case (Var x)
  then show ?case using lu_varl by fast
next
  case (Fun f f_args)
  then obtain σ :: ‹('a, 'b) subst› where h: ‹(Fun f f_args) ⋅ σ = t2 ⋅ σ›
    unfolding term_unification_def by blast
  show ?case
  proof (cases t2)
    case (Var y)
    then show ?thesis using lu_varr by fast
  next
    case (Fun g g_args)
    then show ?thesis
      by (smt (verit, del_insts) Fun.IH eval_term.simps(2) h lu_func map_equality_iff nth_mem term.inject(2) term_unification_def)
  qed
qed

lemma linear_instance_necessary_condition:
  ‹term_instantiation t1 t2 ⟹ linear_instance t1 t2›
proof (induction t2 arbitrary: t1)
  case (Var x)
  then show ?case using li_var by fast
next
  case (Fun f f_args)
  then obtain σ :: ‹('a, 'b) subst› where h: ‹t1 = (Fun f f_args) ⋅ σ›
    unfolding term_instantiation_def by auto
  show ?case
    by (smt (verit, del_insts) Fun.IH eval_term.simps(2) h term_instantiation_def length_map li_func nth_map nth_mem)
qed

lemma linear_generalization_necessary_condition:
  ‹term_generalization t1 t2 ⟹ linear_generalization t1 t2›
  by (simp add: term_generalization_def linear_generalization_def linear_instance_necessary_condition)

lemma linear_variant_necessary_condition:
  ‹term_variant t1 t2 ⟹ linear_variant t1 t2›
proof (induction t2 arbitrary: t1)
  case (Var x)
  then show ?case
  proof (cases t1)
    case (Var y)
    then show ?thesis using lv_var by auto
  next
    case (Fun g g_args)
    then show ?thesis
      using Var term_variant_def by force
  qed
next
  case (Fun f f_args)
  show ?case
  proof (cases t1)
    case (Var y)
    then obtain σ :: ‹('a, 'b) subst› where ‹(Var y) ⋅ σ = Fun f f_args›
                                        and ‹is_Var (σ y)›
      using Fun unfolding term_variant_def
      by (metis (mono_tags, lifting) image_iff mem_Collect_eq subst_domain_def subst_range.simps term.disc(1))
    then show ?thesis by force
  next
    case (Fun g g_args)
    then show ?thesis
      by (smt (verit, best) Fun.IH Fun.prems eval_term.simps(2) length_map lv_func nth_map nth_mem term_variant_def term.inject(2))
  qed
qed

lemma linear_variant_refl: ‹linear_variant t t›
  by (induction t; simp add: lv_var lv_func)

lemma linear_variant_trans: ‹linear_variant t1 t2 ⟹ linear_variant t2 t3 ⟹ linear_variant t1 t3›
  by (induction t2 arbitrary: t1 t3; smt (verit, ccfv_threshold) linear_variant.simps nth_mem term.distinct(1) term.inject(2))

lemma linear_instance_refl: ‹linear_instance t t›
  by (induction t; simp add: li_var li_func)

lemma linear_instance_trans: ‹linear_instance t1 t2 ⟹ linear_instance t2 t3 ⟹ linear_instance t1 t3›
  by (induction t2 arbitrary: t1 t3; smt (verit, ccfv_threshold) linear_instance.simps nth_mem term.distinct(1) term.inject(2))

lemma linear_unification_refl: ‹linear_unification t t›
  by (induction t; simp add: lu_varl lu_func)

subsection ‹Term symbols›

text ‹This subsection defines the symbols used for indexing, with a single symbol ‹Star›
      corresponding to any occurrence of a variable in a term.›

datatype 'f symbol =
	Star
| F 'f nat

fun arity :: ‹('f, 'v) term ⇒ nat› where
  ‹arity (Var x) = 0›
| ‹arity (Fun f largs) = length largs›

fun symbol_arity :: ‹'f symbol ⇒ nat› where
  ‹symbol_arity Star = 0›
| ‹symbol_arity (F f n) = n›

fun root_symbol :: ‹('f, 'v) term ⇒ 'f symbol› where
  ‹root_symbol (Var x) = Star›
| ‹root_symbol (Fun f f_args) = F f (length f_args)›

lemma symbol_arity_root [simp]: ‹symbol_arity (root_symbol t) = num_args t›
  by (cases t; auto)

lemma term_induct2: ‹(⋀x. P (Var x)) ⟹(⋀f f_args. (⋀x. x < (length f_args) ⟹ P (f_args !x)) ⟹ P (Fun f f_args)) ⟹ P a›
  by (rule term.induct) (auto intro: nth_mem)

subsection ‹Preorder strings›

text ‹This subsection defines preorder string representations of terms, which use the previously
      defined symbols.›

fun preorder_string :: ‹('f, 'v) term ⇒ ('f symbol) list› where
  ‹preorder_string (Var x) = [Star]›
| ‹preorder_string (Fun f f_args) = F f (length f_args) # concat (map preorder_string f_args)›

lemma preorder_string_alt_def: ‹preorder_string t = root_symbol t # concat (map preorder_string (args t))›
  by (cases t; simp)

lemma non_empty_preorder_string [simp]: ‹preorder_string t ≠ []›
  using preorder_string.elims by blast

lemma linear_variant_same_preorder_string: ‹linear_variant t1 t2 ⟹ preorder_string t1 = preorder_string t2›
proof (induction rule: linear_variant.induct)
  case (lv_var x y)
  then show ?case by simp
next
  case (lv_func args1 args2 f)
  then show ?case by (simp_all; metis nth_map_conv)
qed

lemma root_symbol_arity: ‹root_symbol t1 = root_symbol t2 ⟹ arity t1 = arity t2›
  by (metis arity.simps(1) arity.simps(2) root_symbol.elims symbol.distinct(1) symbol.inject)

lemma preorder_string_root_symbol: ‹preorder_string t1 = preorder_string t2 ⟹ root_symbol t1 = root_symbol t2›
  by (simp add: preorder_string_alt_def)

lemma preorder_string_arity: ‹preorder_string s = preorder_string t ⟹ arity s = arity t›
  using root_symbol_arity preorder_string_root_symbol by blast

lemma linear_variant_length: ‹linear_variant t1 t2 ⟹ length (preorder_string t1) = length (preorder_string t2)›
proof (induction rule: linear_variant.induct)
  case (lv_var x y)
  then show ?case by auto
next
  case (lv_func args1 args2 f)
  then have ‹sum_list (map length (map preorder_string args1)) = sum_list (map length (map preorder_string args2))›
    by (metis (no_types, lifting) length_map map_nth_eq_conv)
  then have ‹length (concat (map preorder_string args1)) = length (concat (map preorder_string args2))›
    using length_concat by metis
  then show ?case by auto
qed

lemma linear_instance_length: ‹linear_instance t1 t2 ⟹ length (preorder_string t2) ≤ length (preorder_string t1)›
proof (induction rule: linear_instance.induct)
  case (li_var t x)
  then show ?case
    using le_Suc_eq by fastforce
next
  case (li_func args1 args2 f)
  then have ‹sum_list (map length (map preorder_string args2)) ≤ sum_list (map length (map preorder_string args1))›
    by (simp add: sum_list_mono2)
  then have ‹length (concat (map preorder_string args2)) ≤ length (concat (map preorder_string args1))›
    using length_concat by metis
  then show ?case by auto
qed

lemma length_preorder_string_sum_arities:
  ‹length (preorder_string t) = Suc (sum_list (map symbol_arity (preorder_string t)))›
proof (induction t)
  case (Var x)
  then show ?case by auto
next
  case (Fun f f_args)
  have ‹length (concat (map preorder_string f_args)) = sum_list (map length (map preorder_string f_args))›
    using length_concat by blast
  also have ‹... = sum_list (map (length ∘ preorder_string) f_args)› by auto
  also have ‹... = (∑t←f_args. Suc (sum_list (map symbol_arity (preorder_string t))))›
    by (metis (mono_tags, lifting) Fun comp_apply map_eq_conv)
  also have ‹... = length f_args + (∑t←f_args. sum_list (map symbol_arity (preorder_string t)))›
    by (simp add: sum_list_Suc)
  also have ‹... = length f_args + sum_list (map symbol_arity (concat (map preorder_string f_args)))›
    by (induction f_args) auto
  finally show ?case by auto
qed

lemma length_concat_map_preorder_string: ‹length (concat (map preorder_string ts)) = sum_list (map symbol_arity (concat (map preorder_string ts))) + length ts›
proof (induction ts)
  case Nil
  then show ?case by auto
next
  case (Cons a ts)
  then show ?case
    using length_preorder_string_sum_arities by auto
qed

lemma arity_last_symbol: ‹symbol_arity (last (preorder_string t)) = 0›
proof (induction t)
  case (Var x)
  then show ?case by auto
next
  case (Fun f f_args)
  consider ‹f_args = []› | ‹∃xs x. f_args = xs @ [x]›
    by (meson rev_exhaust)
  then show ?case
  proof cases
    case 1
    then show ?thesis by auto
  next
    case 2
    then obtain xs x where ‹f_args = xs @ [x]› by auto
    then show ?thesis using Fun.IH by auto
  qed
qed

lemma concat_decompose: ‹l1 @ s # l2 = concat (Cons x xs) ⟹
                         (∃l3. l1 = x @ l3 ∧ l3 @ s # l2 = concat xs)
                         ∨ (∃l3. l2 = l3 @ concat xs ∧ l1 @ s # l3 = x)›
proof -
  assume ‹l1 @ s # l2 = concat (Cons x xs)›
  then have h: ‹l1 @ s # l2 = x @ concat xs› by auto
  show ?thesis
  proof (cases ‹length x ≤ length l1›)
    case True
    then show ?thesis using h
      by (smt (verit, ccfv_SIG) append_eq_append_conv append_eq_append_conv2 nle_le prefixI prefix_length_le)
  next
    case False
    then have ‹length x > length l1› by auto
    then show ?thesis using h
      by (smt (z3) append_Cons append_eq_append_conv2 append_eq_conv_conj id_take_nth_drop list.inject)
  qed
qed

lemma concat_map_preorder_string_decompose: ‹l1 @ s # l2 = concat (map preorder_string f_args) ⟹
                                        (∃f_args1 t f_args2 ss1 ss2. f_args = f_args1 @ t # f_args2
                                                                   ∧ preorder_string t = ss1 @ s # ss2
                                                                   ∧ l2 = ss2 @ concat (map preorder_string f_args2))›
proof (induction f_args arbitrary: l1 l2)
  case Nil
  then show ?case by auto
next
  case (Cons t ts)
  then consider ‹∃l3. l1 = preorder_string t @ l3 ∧ l3 @ s # l2 = concat (map preorder_string ts)›
              | ‹∃l3. l2 = l3 @ concat (map preorder_string ts) ∧ l1 @ s # l3 = preorder_string t›
    using concat_decompose by (smt (verit, ccfv_threshold) list.simps(9))
  then show ?case
  proof cases
    case 1
    then show ?thesis
      by (metis Cons.IH Cons_eq_appendI)
  next
    case 2
    then show ?thesis
      by (metis append_self_conv2)
  qed
qed

lemma preorder_string_prefix_bound: ‹preorder_string s = t @ u # v ⟹ symbol_arity u + sum_list (map symbol_arity v) < Suc (length v)›
proof (induction s arbitrary: t v)
  case (Var x)
  then have ‹v = [] ∧ u = Star›
    by (simp add: Cons_eq_append_conv)
  then show ?case by auto
next
  case (Fun f f_args)
  show ?case
  proof (cases t)
    case Nil
    then have ‹u = F f (length f_args) ∧ v = concat (map preorder_string f_args)›
      using Fun.prems by auto
    then show ?thesis
      by (metis length_Cons length_preorder_string_sum_arities less_Suc_eq list.simps(9) preorder_string.simps(2) sum_list.Cons)
  next
    case (Cons x t1)
    then have ‹t1 @ u # v = concat (map preorder_string f_args)›
      using Fun.prems by auto
    then obtain s1 f_args1 us1 us2 f_args2
      where h: ‹f_args = f_args1 @ s1 # f_args2
               ∧ preorder_string s1 = us1 @ u # us2
               ∧ v = us2 @ concat (map preorder_string f_args2)›
      using concat_map_preorder_string_decompose [of t1 u v f_args] by blast
    have ‹symbol_arity u + sum_list (map symbol_arity v) = symbol_arity u + sum_list (map symbol_arity us2) + sum_list (map symbol_arity (concat (map preorder_string f_args2)))›
      using h by auto
    also have ‹... ≤ symbol_arity u + sum_list (map symbol_arity us2) + sum_list (map symbol_arity (concat (map preorder_string f_args2))) + length f_args2›
      by auto
    also have ‹... = symbol_arity u + sum_list (map symbol_arity us2) + length (concat (map preorder_string f_args2))›
      using length_concat_map_preorder_string [of f_args2] by auto
    also have ‹... < Suc (length us2) + length (concat (map preorder_string f_args2))›
      using Fun.IH h by auto
    also have ‹... = Suc (length v)›
      using h by auto
    finally show ?thesis by auto
  qed
qed

lemma preorder_string_prefix: ‹preorder_string s @ l = preorder_string t ⟹ l = []›
proof -
  assume h: ‹preorder_string s @ l = preorder_string t›
  then have h0: ‹sum_list (map symbol_arity l) = length l›
    by (smt (verit, best) ab_semigroup_add_class.add_ac(1) add_left_imp_eq length_append length_preorder_string_sum_arities map_append plus_1_eq_Suc sum_list_append)
  show ?thesis
  proof (rule ccontr)
    assume ‹l ≠ []›
    then obtain u us where ‹l = Cons u us›
      by (metis list.exhaust)
    then have ‹sum_list (map symbol_arity l) < length l›
      using preorder_string_prefix_bound h
      by (metis length_Cons list.simps(9) sum_list.Cons)
    then show False using h0 by auto
  qed
qed

lemma preorder_string_prefix_take: ‹preorder_string s = take n (preorder_string t) ⟹ n ≥ length (preorder_string t)›
  by (metis take_all_iff preorder_string_prefix append_take_drop_id eq_imp_le)

lemma take_concat: ‹i < length (concat xss) ⟹ ∃j k. j < length xss ∧ k ≤ length (xss ! j) ∧ take i (concat xss) = concat (take j xss) @ take k (xss ! j)›
proof (induction xss arbitrary: i)
  case Nil
  then show ?case by auto
next
  case (Cons a xss)
  show ?case
  proof (cases ‹i ≤ length a›)
    case True
    then have ‹take i (concat (a # xss)) = concat (take 0 (a # xss)) @ take i ((a # xss) ! 0)› by auto
    then show ?thesis using True by fastforce
  next
    case False
    then obtain j k where ‹j < length xss ∧ k ≤ length (xss ! j) ∧ take i (concat (a # xss)) = a @ concat (take j xss) @ take k (xss ! j)› using Cons by force
    then show ?thesis by force
  qed
qed

lemma string_sum_arities_prefix: ‹n < length (preorder_string t) ⟹ n < Suc (sum_list (map symbol_arity (take n (preorder_string t))))›
proof (induction t arbitrary: n)
  case (Var x)
  then show ?case by auto
next
  case (Fun f f_args)
  show ?case
  proof (cases n)
    case 0
    then show ?thesis by auto
  next
    case (Suc m)
    then have ‹m < length (concat (map preorder_string f_args))› using Fun by auto
    then obtain j k where ‹take m (concat (map preorder_string f_args)) = concat (take j (map preorder_string f_args)) @ take k ((map preorder_string f_args) ! j)›
                      and ‹j < length f_args›
                      and ‹k ≤ length ((map preorder_string f_args) ! j)›
      using take_concat length_map by metis
    then have h0: ‹take m (concat (map preorder_string f_args)) = concat (map preorder_string (take j f_args)) @ take k (preorder_string (f_args ! j))›
      by (metis take_map nth_map)
    then have h1: ‹take n (preorder_string (Fun f f_args)) = F f (length f_args) # concat (map preorder_string (take j f_args)) @ take k (preorder_string (f_args ! j))› using Suc by auto
    then have ‹n = 1 + length (concat (map preorder_string (take j f_args))) + k›
      using ‹j < length f_args› ‹k ≤ length ((map preorder_string f_args) ! j)› Fun.prems length_take [of n ‹preorder_string (Fun f f_args)›] by auto
    then have h2: ‹n = 1 + sum_list (map symbol_arity (concat (map preorder_string (take j f_args)))) + j + k›
      using length_concat_map_preorder_string [of ‹take j f_args›] ‹j < length f_args› by auto
    then have h3: ‹sum_list (map symbol_arity (take n (preorder_string (Fun f f_args)))) = length f_args + sum_list (map symbol_arity (concat (map preorder_string (take j f_args)))) + sum_list (map symbol_arity (take k (preorder_string (f_args ! j))))›
      using h1 by auto
    then show ?thesis
    proof (cases ‹k = length (preorder_string (f_args ! j))›)
      case True
      then have ‹take m (concat (map preorder_string f_args)) = concat (map preorder_string (take (Suc j) f_args))›
        using h0 ‹j < length f_args› take_Suc_conv_app_nth [of j f_args] by auto
      then show ?thesis
        by (smt (verit) h2 h3 True ‹m < length (concat (map preorder_string f_args))› add.commute add_Suc_right add_less_mono1 dual_order.refl
            length_preorder_string_sum_arities linorder_not_le plus_1_eq_Suc take_all_iff)
    next
      case False
      then have ‹k < Suc (sum_list (map symbol_arity (take k (preorder_string (f_args ! j)))))›
        using Fun.IH ‹j < length f_args› ‹k ≤ length (map preorder_string f_args ! j)› by auto
      then show ?thesis using h2 h3 ‹j < length f_args› by linarith
    qed
  qed
qed

lemma preorder_string_prefix_sum_arities: ‹n < length (preorder_string t) ⟹ sum_list (map symbol_arity (take n (preorder_string t))) ≥ n›
proof (induction n)
  case 0
  then show ?case by auto
next
  case (Suc m)
  then consider ‹m < sum_list (map symbol_arity (take m (preorder_string t)))› | ‹m = sum_list (map symbol_arity (take m (preorder_string t)))› by linarith
  then show ?case
  proof cases
    case 1
    have ‹take (Suc m) (preorder_string t) = take m (preorder_string t) @ [preorder_string t ! m]›
      using ‹Suc m < length (preorder_string t)› take_Suc_conv_app_nth [of m ‹preorder_string t›] by linarith
    then show ?thesis using 1 by auto
  next
    case 2
    have ‹length (take m (preorder_string t)) = m› using Suc.prems by auto
    then have ‹length (preorder_string t) = m + length (drop m (preorder_string t))› by auto
    then have ‹Suc (sum_list (map symbol_arity (preorder_string t))) = m + length (drop m (preorder_string t))›
      by (simp add: length_preorder_string_sum_arities)
    show ?thesis
    proof (cases ‹symbol_arity (preorder_string t ! m)›)
      case 0
      then have ‹sum_list (map symbol_arity (take (Suc m) (preorder_string t))) = sum_list ((map symbol_arity (take m (preorder_string t))))›
        by (simp add: Suc.prems Suc_lessD take_Suc_conv_app_nth)
      then have ‹sum_list (map symbol_arity (take (Suc m) (preorder_string t))) = length (take m (preorder_string t))›
        using 2 Suc.prems by fastforce
      then have ‹Suc (sum_list (map symbol_arity (take (Suc m) (preorder_string t)))) = length (take (Suc m) (preorder_string t))›
        using Suc.prems by auto
      then show ?thesis
        using Suc.prems string_sum_arities_prefix by fastforce
    next
      case (Suc m')
      then have ‹sum_list (map symbol_arity (take (Suc m) (preorder_string t))) = sum_list ((map symbol_arity (take m (preorder_string t)) @ [Suc m']))›
        by (simp add: Suc.prems Suc_lessD take_Suc_conv_app_nth)
      then show ?thesis using 2 by auto
    qed
  qed
qed

lemma preorder_string_suffix: ‹preorder_string s = l @ take n (preorder_string t) ⟹ n = 0 ∨ n ≥ length (preorder_string t)›
proof -
  assume h: ‹preorder_string s = l @ take n (preorder_string t)›
  then have ‹length l + length (take n (preorder_string t)) = Suc (sum_list (map symbol_arity (preorder_string s)))›
    using length_preorder_string_sum_arities [of s] by auto
  show ?thesis
  proof (rule ccontr)
    assume h0: ‹¬ (n = 0 ∨ length (preorder_string t) ≤ n)›
    then have ‹take n (preorder_string t) ≠ [] ∧ drop n (preorder_string t) ≠ []› by auto
    then obtain u us v vs where ‹take n (preorder_string t) = Cons u us ∧ drop n (preorder_string t) = Cons v vs›
      by (metis list.exhaust)
    then show False using h h0 append_take_drop_id preorder_string_prefix_sum_arities [of n t]
      by (metis add_right_cancel diff_add_inverse length_Cons length_append length_drop less_imp_add_positive linorder_not_le list.simps(9) preorder_string_prefix_bound sum_list.Cons)
  qed
qed

lemma concat_map_preorder_string_congruence: ‹concat (map preorder_string l1) = concat (map preorder_string l2) ⟹ (∀(s, t) ∈ set (zip l1 l2). preorder_string s = preorder_string t)›
proof (induction l1 arbitrary: l2)
  case Nil
  then show ?case by auto
next
  case (Cons s ss)
  then show ?case
  proof (cases l2)
    case Nil
    then show ?thesis by auto
  next
    case (Cons t ts)
    then have h1: ‹preorder_string s @ concat (map preorder_string ss) = preorder_string t @ concat (map preorder_string ts)›
      using Cons.prems by auto
    then have h2: ‹preorder_string s = preorder_string t› using preorder_string_prefix
      by (smt (verit, best) append_eq_append_conv2)
    from h1 h2 show ?thesis using Cons.IH
      using local.Cons by auto
  qed
qed

lemma preorder_string_congruence: ‹preorder_string s = preorder_string t ⟹
                                  (∀i < arity s. preorder_string (args s ! i) = preorder_string (args t ! i))›
proof -
  assume h: ‹preorder_string s = preorder_string t›
  show ?thesis
  proof (cases s)
    case (Var x)
    then show ?thesis by auto
  next
    case f: (Fun f f_args)
    then show ?thesis
    proof (cases t)
      case (Var y)
      then show ?thesis using f h by auto
    next
      case (Fun g g_args)
      then show ?thesis using h f concat_map_preorder_string_congruence
        by (metis (mono_tags, lifting) arity.simps(2) list.inject list_all2_iff list_all2_nthD2 preorder_string_alt_def preorder_string_arity term.sel(4))
    qed
  qed
qed

lemma same_preorder_string_linear_variant: ‹preorder_string s = preorder_string t ⟹ linear_variant s t›
proof (induction s arbitrary: t rule: term_induct2)
  case (1 x)
  then show ?case using 1  
    by (metis lv_var preorder_string.elims preorder_string_root_symbol root_symbol.simps(1) root_symbol.simps(2) symbol.distinct(1))
next
  case (2 f f_args)
  have h: ‹∀i<arity(Fun f f_args). preorder_string (f_args ! i) = preorder_string (args t ! i)›
    by (metis 2(2) preorder_string_congruence term.sel(4))
  then show ?case
  proof(cases t)
    case (Var y)
    then show ?thesis
      using 2 by force 
  next
    case (Fun g g_args)
    then show ?thesis using Fun 2   
      by (simp add: h lv_func)
  qed
qed

end