Theory Typed_Substitution_Lifting

theory Typed_Substitution_Lifting
  imports
    Typed_Substitution
    Abstract_Substitution.Based_Substitution_Lifting
begin

locale typed_substitution_lifting =
  sub: typed_substitution where
  vars = sub_vars and subst = sub_subst and is_ground = sub_is_ground and 
  welltyped = sub_welltyped and base_vars = base_vars +

  based_substitution_lifting where 
  to_set = to_set and base_vars = base_vars
for
  sub_welltyped :: "('v, 'ty) var_types ⇒ 'sub ⇒ 'ty' ⇒ bool" and
  to_set :: "'expr ⇒ 'sub set" and
  base_vars :: "'base ⇒ 'v set"
begin

sublocale typing_lifting where sub_welltyped = "sub_welltyped 𝒱"
  by unfold_locales

sublocale typed_substitution where
  vars = vars and subst = subst and is_ground = is_ground and welltyped = welltyped
  by unfold_locales

end

locale witnessed_typed_substitution_lifting =
  typed_substitution_lifting +
  subst_update_lifting +
  finite_variables_lifting +
  vars_grounded_iff_is_grounding_lifting +
  sub: witnessed_typed_substitution where
  vars = sub_vars and subst = sub_subst and is_ground = sub_is_ground and welltyped = sub_welltyped
begin

sublocale witnessed_typed_substitution where
  vars = vars and subst = subst and is_ground = is_ground and welltyped = welltyped
  by unfold_locales (simp add: sub.types_witnessed)

end

locale typed_subst_stability_lifting =
  typed_substitution_lifting +
  sub: typed_subst_stability where
  welltyped = sub_welltyped and vars = sub_vars and subst = sub_subst and is_ground = sub_is_ground
begin

sublocale typed_subst_stability where
  welltyped = welltyped and subst = subst and vars = vars and is_ground = is_ground
proof unfold_locales
  fix 𝒱 σ expr
  assume type_preserving_σ: "sub.base.type_preserving_on (vars expr) 𝒱 σ"
  
  show "is_welltyped 𝒱 (expr ⋅ σ) ⟷ is_welltyped 𝒱 expr"
    using type_preserving_σ sub.welltyped_subst_stability
    unfolding vars_def is_welltyped_def subst_def
    by auto
qed

end

locale replaceable_𝒱_lifting =
  typed_substitution_lifting +
  sub: replaceable_𝒱 where
  welltyped = sub_welltyped and vars = sub_vars and subst = sub_subst and is_ground = sub_is_ground
begin

sublocale replaceable_𝒱 where
  subst = subst and vars = vars and is_ground = is_ground and welltyped = welltyped
  by unfold_locales (metis SUP_upper2 sub.replace_𝒱 subset_eq vars_def is_welltyped_def)

end

locale typed_renaming_lifting =
  typed_substitution_lifting where
  base_welltyped = "base_welltyped :: ('v, 'ty) var_types ⇒ 'base ⇒ 'ty ⇒ bool" +
  renaming_variables_lifting +
  (* TODO: *)
  subst_updates_compat_lifting +
  sub: based_typed_renaming where
  subst = sub_subst and vars = sub_vars and is_ground = sub_is_ground and welltyped = sub_welltyped
begin

sublocale based_typed_renaming where
  subst = subst and vars = vars and is_ground = is_ground and welltyped = welltyped
  by unfold_locales (force simp: vars_def subst_def is_welltyped_def)

end

end