Theory Typed_Substitution_Example

theory Typed_Substitution_Example
  imports
    Typed_Substitution
    Abstract_Substitution.Substitution_First_Order_Term
begin                                 

type_synonym ('f, 'ty) fun_types = "'f ⇒ 'ty list × 'ty"

text ‹Inductive predicate defining well-typed terms.›

inductive welltyped :: "('f, 'ty) fun_types ⇒ ('v, 'ty) var_types ⇒ ('f,'v) term ⇒ 'ty ⇒ bool"
  for ℱ 𝒱 where
    Var: "𝒱 x = τ ⟹ welltyped ℱ 𝒱 (Var x) τ"
  | Fun: "ℱ f = (τs, τ) ⟹ list_all2 (welltyped ℱ 𝒱) ts τs ⟹ welltyped ℱ 𝒱 (Fun f ts) τ"

global_interpretation "term": base_typing "welltyped ℱ 𝒱"
proof unfold_locales
  show "right_unique (welltyped ℱ 𝒱)"
  proof (rule right_uniqueI)
    fix t τ1 τ2
    assume "welltyped ℱ 𝒱 t τ1" and "welltyped ℱ 𝒱 t τ2"
    thus "τ1 = τ2"
      by (auto elim!: welltyped.cases)
  qed
qed

global_interpretation base_typed_substitution where
  welltyped = "welltyped ℱ" and subst = subst_apply_term and id_subst = Var and
  comp_subst = subst_compose and vars = "vars_term :: ('f, 'v) term ⇒ 'v set" and
  apply_subst = apply_subst and is_ground = is_ground and subst_update = fun_upd
  for ℱ :: "('f, 'ty) fun_types"
  by unfold_locales (simp_all add: welltyped.Var)

text ‹A selection of substitution properties for typed terms.›
locale typed_term_subst_properties =
  base_typed_subst_stability where welltyped = "welltyped ℱ"
  for ℱ :: "('f, 'ty) fun_types"

global_interpretation "term": typed_term_subst_properties where
  subst = subst_apply_term and id_subst = Var and
  comp_subst = subst_compose and vars = "vars_term :: ('f, 'v) term ⇒ 'v set" and
  apply_subst = apply_subst and is_ground = is_ground and subst_update = fun_upd
  for ℱ :: "'f ⇒ 'ty list × 'ty"
proof unfold_locales
  fix 𝒱 :: "('v, 'ty) var_types" and t :: "('f, 'v) term" and σ τ
  assume is_welltyped_on: "type_preserving_on ℱ (vars_term t) 𝒱 σ"

  show "welltyped ℱ 𝒱 (t ⋅ σ) τ ⟷ welltyped ℱ 𝒱 t τ"
  proof(rule iffI)
    assume "welltyped ℱ 𝒱 t τ"
    then show "welltyped ℱ 𝒱 (t ⋅ σ) τ"
      using is_welltyped_on
      by(induction rule: welltyped.induct)
        (auto simp: list.rel_mono_strong list_all2_map1 welltyped.simps)
  next
    assume "welltyped ℱ 𝒱 (t ⋅ σ) τ"
    then show "welltyped ℱ 𝒱 t τ"
      using is_welltyped_on
    proof(induction "t ⋅ σ" τ arbitrary: t rule: welltyped.induct)
      case (Var x τ)

      then obtain x' where t: "t = Var x'"
        by (metis subst_apply_eq_Var)

      moreover have "welltyped ℱ 𝒱 t (𝒱 x)"
        using Var
        unfolding t
        by (simp add: welltyped.simps)

      ultimately have 𝒱_x': "τ = 𝒱 x'"
        using Var.hyps
        by (simp add: t welltyped.simps)

      show ?case
        unfolding t 𝒱_x'
        by (simp add: welltyped.Var)
    next
      case (Fun f τs τ ts)

      then show ?case
        by (cases t) (simp_all add: list.rel_mono_strong list_all2_map1 welltyped.simps)
    qed
  qed
qed

text ‹Examples of generated lemmas and definitions›
thm
  term.right_unique
  term.welltyped_subst_stability

  type_preserving_on_subset
  type_preserving_on_id_subst

term term.is_welltyped
term type_preserving_on
term type_preserving

end