-- Capless/Type/Basic.lean

import Capless.Type.Core

import Capless.Type.Renaming

namespace Capless

end

end

end

-- Repository definitions used:
content: notation:max "{x=" x "}" => CaptureSet.singleton x

content: notation:max "{c=" c "}" => CaptureSet.csingleton c

content: inductive SType : Nat -> Nat -> Nat -> Type where
| top : SType n m k
| tvar : Fin m -> SType n m k
| forall : CType n m k -> EType (n+1) m k -> SType n m k
| tforall : SType n m k -> EType n (m+1) k -> SType n m k
| cforall : CBound n k -> EType n m (k+1) -> SType n m k
| box : CType n m k -> SType n m k
| label : SType n m k -> SType n m k

content: def FinFun.ext (f : FinFun n n') : FinFun (n+1) (n'+1) :=

content: def CType.crename : CType n m k -> FinFun k k' -> CType n m k'
| CType.capt C S, f => CType.capt (C.crename f) (S.crename f)

content: inductive CType : Nat -> Nat -> Nat -> Type where
| capt : CaptureSet n k -> SType n m k -> CType n m k

content: def FinFun (n n' : Nat) : Type :=
  Fin n -> Fin n'

content: def EType.crename : EType n m k -> FinFun k k' -> EType n m k'
| EType.ex T, f => EType.ex (T.crename f.ext)
| EType.type T, f => EType.type (T.crename f)

content: inductive EType : Nat -> Nat -> Nat -> Type where
| ex : CType n m (k+1) -> EType n m k
| type : CType n m k -> EType n m k

content: def CBound.crename (b : CBound n k) (f : FinFun k k') : CBound n k' :=
  match b with
  | upper C => upper (C.crename f)
  | star => star

content: def SType.crename : SType n m k -> FinFun k k' -> SType n m k'
| SType.top, _ => SType.top
| SType.tvar X, _ => SType.tvar X
| SType.forall E1 E2, f => SType.forall (E1.crename f) (E2.crename f)
| SType.tforall S E, f => SType.tforall (S.crename f) (E.crename f)
| SType.cforall B E, f => SType.cforall (B.crename f) (E.crename f.ext)
| SType.box T, f => SType.box (T.crename f)
| SType.label S, f => SType.label (S.crename f)

content: @[simp]
def CaptureSet.crename (C : CaptureSet n k) (f : FinFun k k') : CaptureSet n k' :=
  match C with
  | empty => empty
  | union C1 C2 => (C1.crename f) ∪ (C2.crename f)
  | singleton x => {x=x}
  | csingleton c => {c=f c}

content: inductive CaptureSet : Nat -> Nat -> Type where
| empty : CaptureSet n k
| union : CaptureSet n k -> CaptureSet n k -> CaptureSet n k
| singleton : Fin n -> CaptureSet n k
| csingleton : Fin k -> CaptureSet n k

content: notation:max "{x=" x "}" => CaptureSet.singleton x

content: notation:max "{c=" c "}" => CaptureSet.csingleton c

content: notation "⊤" => SType.top

content: notation:50 "∀(x:" T ")" U => SType.forall T U

content: notation:50 "∀[X<:" S "]" T => SType.tforall S T

content: notation:50 "∀[c<:" B "]" T => SType.cforall B T

content: notation:max S " ^ " C => CType.capt C S

content: notation:40 "∃c." T => EType.ex T

content: notation:40 "Label[" S "]" => SType.label S

content: notation:60 "□" T => SType.box T

-- Repository lemmas used:
content: theorem CaptureSet.crename_crename {C : CaptureSet n k} :
  (C.crename f).crename g = C.crename (g ∘ f)

content: theorem FinFun.ext_comp_ext {f : FinFun n n'} {g : FinFun n' n''} :
  g.ext ∘ f.ext = FinFun.ext (g ∘ f)

-- Local definitions used:

-- Local lemmas used:
content: theorem CBound.crename_crename {b : CBound n k} :
  (b.crename f).crename g = b.crename (g ∘ f)

content: theorem EType.crename_crename (E : EType n m k) (f : FinFun k k') (g : FinFun k' k'') :
  (E.crename f).crename g = E.crename (g ∘ f)

content: theorem CType.crename_crename (T : CType n m k) (f : FinFun k k') (g : FinFun k' k'') :
  (T.crename f).crename g = T.crename (g ∘ f)

-- Target theorem and proof:
theorem SType.crename_crename (S : SType n m k) (f : FinFun k k') (g : FinFun k' k'') :
  (S.crename f).crename g = S.crename (g ∘ f) :=
  match S with
  | SType.top => by simp [SType.crename]
  | SType.tvar X => by simp [SType.crename]
  | SType.forall E1 E2 => by
    have ih1 := CType.crename_crename E1 f g
    have ih2 := EType.crename_crename E2 f g
    simp [SType.crename, ih1, ih2]
  | SType.tforall S E => by
    have ih1 := SType.crename_crename S f g
    have ih2 := EType.crename_crename E f g
    simp [SType.crename, ih1, ih2]
  | SType.cforall B E => by
    have ih := EType.crename_crename E f.ext g.ext
    simp [SType.crename, ih, FinFun.ext_comp_ext, CBound.crename_crename]
  | SType.box T => by
    have ih := CType.crename_crename T f g
    simp [SType.crename, ih]
  | SType.label S => by
    have ih := SType.crename_crename S f g
    simp [SType.crename, ih]
:=
  match S with
  | SType.top => by simp [SType.crename]
  | SType.tvar X => by simp [SType.crename]
  | SType.forall E1 E2 => by
    have ih1 := CType.crename_crename E1 f g
    have ih2 := EType.crename_crename E2 f g
    simp [SType.crename, ih1, ih2]
  | SType.tforall S E => by
    have ih1 := SType.crename_crename S f g
    have ih2 := EType.crename_crename E f g
    simp [SType.crename, ih1, ih2]
  | SType.cforall B E => by
    have ih := EType.crename_crename E f.ext g.ext
    simp [SType.crename, ih, FinFun.ext_comp_ext, CBound.crename_crename]
  | SType.box T => by
    have ih := CType.crename_crename T f g
    simp [SType.crename, ih]
  | SType.label S => by
    have ih := SType.crename_crename S f g
    simp [SType.crename, ih]