11open import Scope
22
3- open import Haskell.Prelude hiding (All; c; t)
3+ open import Haskell.Prelude hiding (All; coerce; a; b; c; t)
44open import Haskell.Extra.Dec
5+ open import Haskell.Law.Equality using (subst0; sym; trans)
6+ open import Haskell.Law.Monoid.Def using (leftIdentity; rightIdentity)
7+ open import Haskell.Law.Semigroup.Def using (associativity)
58open import Haskell.Extra.Erase
69
710open import Utils.Either
@@ -18,16 +21,118 @@ module Agda.Core.Substitute
1821private variable
1922 @0 x c : Name
2023 @0 α β γ cs : Scope Name
24+ @0 rγ : RScope Name
2125 t : @0 Scope Name → Set
2226
27+ data Subst : (@0 α β : Scope Name) → Set where
28+ SNil : Subst mempty β
29+ SCons : Term β → Subst α β → Subst (x ◃ α) β
30+
31+ {-# COMPILE AGDA2HS Subst deriving Show #-}
32+ infix 5 Subst
33+ syntax Subst α β = α ⇒ β
34+
35+ pattern ⌈⌉ = SNil
36+ infix 6 ⌈_◃_↦_⌉
37+ pattern ⌈_◃_↦_⌉ σ x u = SCons {x = x} u σ
38+ infix 4 ⌈◃_↦_⌉
39+ pattern ⌈◃_↦_⌉ x u = ⌈ ⌈⌉ ◃ x ↦ u ⌉
40+
41+ rezzSubst : α ⇒ β → Rezz α
42+ rezzSubst ⌈⌉ = rezz mempty
43+ rezzSubst ⌈ σ ◃ x ↦ u ⌉ = rezzBind (rezzSubst σ)
44+
45+ weakenSubst : α ⊆ β → γ ⇒ α → γ ⇒ β
46+ weakenSubst p ⌈⌉ = ⌈⌉
47+ weakenSubst p ⌈ s ◃ x ↦ t ⌉ = ⌈ weakenSubst p s ◃ x ↦ weaken p t ⌉
48+
49+ instance
50+ iWeakenSubst : Weaken (Subst γ)
51+ iWeakenSubst .weaken = weakenSubst
52+ {-# COMPILE AGDA2HS iWeakenSubst #-}
53+
54+ lookupSubst : α ⇒ β
55+ → (@0 x : Name)
56+ → x ∈ α
57+ → Term β
58+ lookupSubst ⌈⌉ x q = inEmptyCase q
59+ lookupSubst ⌈ f ◃ _ ↦ u ⌉ x q = inBindCase q (λ _ → u) (lookupSubst f x)
60+
61+ {-# COMPILE AGDA2HS lookupSubst #-}
62+
63+ concatSubst : α ⇒ γ → β ⇒ γ → (α <> β) ⇒ γ
64+ concatSubst ⌈⌉ q =
65+ subst0 (λ α → α ⇒ _) (sym (leftIdentity _)) q
66+ concatSubst ⌈ p ◃ _ ↦ v ⌉ q =
67+ subst0 (λ α → α ⇒ _) (associativity _ _ _) ⌈ concatSubst p q ◃ _ ↦ v ⌉
68+
69+ {-# COMPILE AGDA2HS concatSubst #-}
70+
71+ opaque
72+ unfolding Scope Sub
73+
74+ subToSubst : Rezz α → α ⊆ β → α ⇒ β
75+ subToSubst (rezz []) p = ⌈⌉
76+ subToSubst (rezz (Erased x ∷ α)) p =
77+ ⌈ (subToSubst (rezz α) (joinSubRight (rezz _) p)) ◃ x ↦ (TVar (⟨ x ⟩ coerce p inHere)) ⌉
78+
79+ {-# COMPILE AGDA2HS subToSubst #-}
80+
81+
82+ opaque
83+ unfolding Scope revScope
84+
85+ revSubstAcc : {@0 α β γ : Scope Name} → α ⇒ γ → β ⇒ γ → (revScopeAcc α β) ⇒ γ
86+ revSubstAcc ⌈⌉ p = p
87+ revSubstAcc ⌈ s ◃ y ↦ x ⌉ p = revSubstAcc s ⌈ p ◃ y ↦ x ⌉
88+ {-# COMPILE AGDA2HS revSubstAcc #-}
89+
90+ revSubst : {@0 α β : Scope Name} → α ⇒ β → ~ α ⇒ β
91+ revSubst = flip revSubstAcc ⌈⌉
92+ {-# COMPILE AGDA2HS revSubst #-}
93+
94+ liftSubst : {@0 α β γ : Scope Name} → Rezz α → β ⇒ γ → (α <> β) ⇒ (α <> γ)
95+ liftSubst r f =
96+ concatSubst (subToSubst r (subJoinHere r subRefl))
97+ (weaken (subJoinDrop r subRefl) f)
98+ {-# COMPILE AGDA2HS liftSubst #-}
99+
100+ {-# COMPILE AGDA2HS liftSubst #-}
101+ idSubst : {@0 β : Scope Name} → Rezz β → β ⇒ β
102+ idSubst r = subst0 (λ β → β ⇒ β) (rightIdentity _) (liftSubst r ⌈⌉)
103+ {-# COMPILE AGDA2HS idSubst #-}
104+
105+ liftBindSubst : {@0 α β : Scope Name} {@0 x y : Name} → α ⇒ β → (bind x α) ⇒ (bind y β)
106+ liftBindSubst {y = y} e = ⌈ (weaken (subBindDrop subRefl) e) ◃ _ ↦ (TVar (⟨ y ⟩ inHere)) ⌉
107+ {-# COMPILE AGDA2HS liftBindSubst #-}
108+
109+ raiseSubst : {@0 α β : Scope Name} → Rezz β → α ⇒ β → (α <> β) ⇒ β
110+ raiseSubst {β = β} r ⌈⌉ = subst0 (λ α → α ⇒ β) (sym (leftIdentity β)) (idSubst r)
111+ raiseSubst {β = β} r (SCons {α = α} u e) =
112+ subst0 (λ α → α ⇒ β)
113+ (associativity (singleton _) α β)
114+ ⌈ raiseSubst r e ◃ _ ↦ u ⌉
115+ {-# COMPILE AGDA2HS raiseSubst #-}
116+
117+ revIdSubst : {@0 α : Scope Name} → Rezz α → α ⇒ ~ α
118+ revIdSubst {α} r = subst0 (λ s → s ⇒ (~ α)) (revsInvolution α) (revSubst (idSubst (rezz~ r)))
119+ {-# COMPILE AGDA2HS revIdSubst #-}
120+
121+ revIdSubst' : {@0 α : Scope Name} → Rezz α → ~ α ⇒ α
122+ revIdSubst' {α} r = subst0 (λ s → (~ α) ⇒ s) (revsInvolution α) (revIdSubst (rezz~ r))
123+ {-# COMPILE AGDA2HS revIdSubst' #-}
124+
125+ substExtScope : α ⇒ β → (Rezz rγ) → (extScope α rγ) ⇒ (extScope β rγ)
126+ substExtScope p (rezz Nil) = p
127+ substExtScope p (rezz (x ◂ rγ)) = substExtScope (liftBindSubst p) (rezz rγ)
23128
24129substTerm : α ⇒ β → Term α → Term β
25130substSort : α ⇒ β → Sort α → Sort β
26131substType : α ⇒ β → Type α → Type β
27132substBranch : α ⇒ β → Branch α c → Branch β c
28133substBranches : α ⇒ β → Branches α cs → Branches β cs
29- substSubst : α ⇒ β → γ ⇒ α → γ ⇒ β
30- substTypeS : α ⇒ β → γ ⇛ α → γ ⇛ β
134+ substTermS : α ⇒ β → TermS α rγ → TermS β rγ
135+ substTypeS : α ⇒ β → TypeS α rγ → TypeS β rγ
31136
32137substSort f (STyp x) = STyp x
33138{-# COMPILE AGDA2HS substSort #-}
@@ -37,35 +142,35 @@ substType f (El st t) = El (substSort f st) (substTerm f t)
37142
38143substTerm f (TVar (⟨ x ⟩ k)) = lookupSubst f x k
39144substTerm f (TDef d) = TDef d
40- substTerm f (TData d ps is) = TData d (substSubst f ps) (substSubst f is)
41- substTerm f (TCon c vs) = TCon c (substSubst f vs)
145+ substTerm f (TData d ps is) = TData d (substTermS f ps) (substTermS f is)
146+ substTerm f (TCon c vs) = TCon c (substTermS f vs)
42147substTerm f (TLam x v) = TLam x (substTerm (liftBindSubst f) v)
43148substTerm f (TApp u v) = TApp (substTerm f u) (substTerm f v)
44149substTerm f (TProj u p) = TProj (substTerm f u) p
45150substTerm f (TCase {x = x} d r u bs m) =
46151 TCase {x = x} d r
47152 (substTerm f u)
48153 (substBranches f bs)
49- (substType (liftBindSubst (liftSubst r f )) m)
154+ (substType (liftBindSubst (substExtScope f r )) m)
50155substTerm f (TPi x a b) = TPi x (substType f a) (substType (liftBindSubst f) b)
51156substTerm f (TSort s) = TSort (substSort f s)
52157substTerm f (TLet x u v) = TLet x (substTerm f u) (substTerm (liftBindSubst f) v)
53158substTerm f (TAnn u t) = TAnn (substTerm f u) (substType f t)
54159{-# COMPILE AGDA2HS substTerm #-}
55160
56- substBranch f (BBranch c r u) = BBranch c r (substTerm (liftSubst r f ) u)
161+ substBranch f (BBranch c r u) = BBranch c r (substTerm (substExtScope f r ) u)
57162{-# COMPILE AGDA2HS substBranch #-}
58163
59164substBranches f BsNil = BsNil
60165substBranches f (BsCons b bs) = BsCons (substBranch f b) (substBranches f bs)
61166{-# COMPILE AGDA2HS substBranches #-}
62167
63- substSubst f SNil = SNil
64- substSubst f (SCons x e) = SCons (substTerm f x) (substSubst f e)
65- {-# COMPILE AGDA2HS substSubst #-}
168+ substTermS f ⌈⌉ = ⌈⌉
169+ substTermS f (_ ↦ x ◂ e) = TSCons (substTerm f x) (substTermS f e)
170+ {-# COMPILE AGDA2HS substTermS #-}
66171
67172substTypeS f ⌈⌉ = ⌈⌉
68- substTypeS f ⌈ e ◃ _ ∶ x ⌉ = YCons (substType f x ) (substTypeS f e)
173+ substTypeS {rγ = x ◂ rγ} f ( _ ∶ u ◂ e) = YCons (substType (substExtScope {rγ = rγ} f (rezzTypeS e)) u ) (substTypeS f e)
69174{-# COMPILE AGDA2HS substTypeS #-}
70175
71176record Substitute (t : @0 Scope Name → Set ) : Set where
@@ -84,16 +189,16 @@ instance
84189 iSubstBranch .subst = substBranch
85190 iSubstBranches : Substitute (λ α → Branches α cs)
86191 iSubstBranches .subst = substBranches
87- iSubstSubst : Substitute (Subst α )
88- iSubstSubst .subst = substSubst
89- iSubstTypeS : Substitute (TypeS α)
192+ iSubstTermS : Substitute (λ α → TermS α rγ )
193+ iSubstTermS .subst = substTermS
194+ iSubstTypeS : Substitute (λ α → TypeS α rγ )
90195 iSubstTypeS .subst = substTypeS
91196{-# COMPILE AGDA2HS iSubstTerm #-}
92197{-# COMPILE AGDA2HS iSubstType #-}
93198{-# COMPILE AGDA2HS iSubstSort #-}
94199{-# COMPILE AGDA2HS iSubstBranch #-}
95200{-# COMPILE AGDA2HS iSubstBranches #-}
96- {-# COMPILE AGDA2HS iSubstSubst #-}
201+ {-# COMPILE AGDA2HS iSubstTermS #-}
97202{-# COMPILE AGDA2HS iSubstTypeS #-}
98203
99204substTop : {{Substitute t}} → Rezz α → Term α → t (x ◃ α) → t α
0 commit comments