Skip to content

Commit ab85c2e

Browse files
committed
partial refactoring using RScope
1 parent c270419 commit ab85c2e

File tree

6 files changed

+248
-240
lines changed

6 files changed

+248
-240
lines changed

src/Agda/Core/Context.agda

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Agda.Core.Context
2020
private variable
2121
@0 x y : Name
2222
@0 α β : Scope Name
23+
@0 rβ : RScope Name
2324
@0 s t u v : Term α
2425

2526
data Context : @0 Scope Name Set where
@@ -64,16 +65,11 @@ addContextTel {α} (ExtendTel {β = β} x ty telt) c =
6465
(trans (associativity (~ β) [ x ] α)
6566
(cong (λ t t <> α) (sym $ revsBindComp β x)))
6667
(addContextTel telt (c , x ∶ ty))
67-
6868
{-# COMPILE AGDA2HS addContextTel #-}
6969

70-
addContextTypeS : Context α TypeS β α Context (β <> α)
71-
addContextTypeS {α} Γ ⌈⌉ =
72-
subst0 Context
73-
(sym $ trans (cong (λ x x <> α) refl)
74-
(leftIdentity α))
75-
Γ
76-
addContextTypeS {α = α} Γ (YCons {α = β} {x = x} ty Δ) =
77-
let Γ' : Context (x ◃ (β <> α))
78-
Γ' = addContextTypeS Γ Δ , x ∶ weaken (subJoinDrop (rezzTypeS Δ) subRefl) ty in
79-
subst0 Context (associativity [ x ] β α) Γ'
70+
addContextTypeS : Context α TypeS α rβ Context (extScope α rβ)
71+
addContextTypeS Γ ⌈⌉ = Γ
72+
addContextTypeS {α = α} Γ (YCons {rβ = rβ₀} {x = x} ty Δ) =
73+
let Γ' : Context (extScope (x ◃ α) rβ₀)
74+
Γ' = addContextTypeS (Γ , x ∶ weaken {! !} ty) {! Δ !}
75+
in {! !}

src/Agda/Core/GlobalScope.agda

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ record Globals : Set where
1111
field
1212
defScope : Scope Name
1313
dataScope : Scope Name
14-
dataParScope : NameIn dataScope Scope Name
15-
dataIxScope : NameIn dataScope Scope Name
14+
dataParScope : NameIn dataScope RScope Name
15+
dataIxScope : NameIn dataScope RScope Name
1616
conScope : Scope Name
17-
fieldScope : NameIn conScope Scope Name
17+
fieldScope : NameIn conScope RScope Name
1818
open Globals public
1919
{-# COMPILE AGDA2HS Globals #-}
2020

src/Agda/Core/Reduce.agda

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ private open module @0 G = Globals globals
2626
private variable
2727
@0 x c : Name
2828
@0 α β γ cs : Scope Name
29+
@0 rγ : RScope Name
2930
@0 u v w : Term α
3031

3132
data Environment : (@0 α β : Scope Name) Set where
@@ -60,7 +61,7 @@ data Frame (@0 α : Scope Name) : Set where
6061
FApp : (u : Term α) Frame α
6162
FProj : (x : NameIn defScope) Frame α
6263
FCase : (d : NameIn dataScope) (r : Rezz (dataIxScope d))
63-
(bs : Branches α cs) (m : Type (x ◃ (dataIxScope d <> α))) Frame α
64+
(bs : Branches α cs) (m : Type (x ◃ (extScope α (dataIxScope d)))) Frame α
6465

6566
{-# COMPILE AGDA2HS Frame #-}
6667

@@ -77,7 +78,7 @@ weakenFrame s (FProj f) = FProj f
7778
weakenFrame s (FCase d r bs m) =
7879
FCase d r
7980
(weaken s bs)
80-
(weaken (subBindKeep (subJoinKeep r s)) m)
81+
(weaken (subBindKeep (subExtScopeKeep r s)) m)
8182

8283
{-# COMPILE AGDA2HS weakenFrame #-}
8384

@@ -122,7 +123,7 @@ unState r (MkState e v s) = subst (envToSubst r e) (unStack s v)
122123

123124
lookupBranch : Branches α cs (c : NameIn conScope)
124125
Maybe ( Rezz (fieldScope c)
125-
× Term (fieldScope c <> α))
126+
× Term (extScope α (fieldScope c)))
126127
lookupBranch BsNil c = Nothing
127128
lookupBranch (BsCons (BBranch c' aty u) bs) c =
128129
case decNamesIn c' c of λ where
@@ -131,27 +132,21 @@ lookupBranch (BsCons (BBranch c' aty u) bs) c =
131132

132133
{-# COMPILE AGDA2HS lookupBranch #-}
133134

134-
rezzFromEnv : β ⇒ γ Rezz β
135-
rezzFromEnv SNil = rezz _
136-
rezzFromEnv (SCons v vs) = rezzCong2 (λ (Erased x) α x ◃ α) rezzErase (rezzFromEnv vs)
137-
{-# COMPILE AGDA2HS rezzFromEnv #-}
138135

139-
extendEnvironment : β ⇒ γ Environment α γ Environment α (β <> γ)
140-
extendEnvironment vs e = aux (rezzFromEnv vs) vs e
136+
extendEnvironment : TermS β rγ Environment α β Environment α (extScope β rγ)
137+
extendEnvironment vs e = aux (rezzTermS vs) vs e
141138
where
142-
aux : Rezz β β ⇒ γ Environment α γ Environment α (β <> γ)
143-
aux r SNil e = subst0 (Environment _) (sym (leftIdentity _)) e
144-
aux r (SCons {α = α} {x = x} v vs) e =
145-
let r' = rezzUnbind r
146-
in subst0 (Environment _) (associativity _ _ _)
147-
(aux r' vs e , x ↦ raise r' v)
139+
aux : Rezz rγ TermS β rγ Environment α β Environment α (extScope β rγ)
140+
aux r ⌈⌉ e = e
141+
aux (rezz (x ◂ rγ₀)) (TSCons {α = β} {rβ = rγ₀} {x = x} v vs) e =
142+
aux (rezz rγ₀) (weaken (subBindDrop subRefl) vs) (e , x ↦ v)
148143
{-# COMPILE AGDA2HS extendEnvironment #-}
149144

150145
lookupEnvironment : Environment α β x ∈ β Either (x ∈ α) (Term β)
151146
lookupEnvironment EnvNil p = Left p
152147
lookupEnvironment (e , x ↦ v) p = inBindCase p
153-
(λ _ Right (raise (rezz _) v))
154-
(λ p mapRight (raise (rezz _)) (lookupEnvironment e p))
148+
(λ _ Right (weaken (subBindDrop subRefl) v))
149+
(λ p mapRight (weaken (subBindDrop subRefl)) (lookupEnvironment e p))
155150
{-# COMPILE AGDA2HS lookupEnvironment #-}
156151

157152
step : (rsig : Rezz sig) (s : State α) Maybe (State α)
@@ -180,7 +175,7 @@ step rsig (MkState e (TCon c vs) (FCase d r bs _ ∷ s)) =
180175
(Just (r , v)) Just (MkState
181176
(extendEnvironment vs e)
182177
v
183-
(weakenStack (subJoinDrop r subRefl) s))
178+
(weakenStack (subExtScope r subRefl) s))
184179
Nothing Nothing
185180
step rsig (MkState e (TData d ps is) s) = Nothing
186181
step rsig (MkState e (TCon c vs) (FProj f ∷ s)) = Nothing -- TODO
@@ -218,4 +213,3 @@ reduceAppView s (v ⟨ p ⟩) =
218213
(unApps v) ⟨ subst0 (λ t ReducesTo s t) (sym $ unAppsView v) p ⟩
219214

220215
{-# COMPILE AGDA2HS reduceAppView #-}
221-

src/Agda/Core/Signature.agda

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,20 @@ opaque
5151
{-# COMPILE AGDA2HS caseTelEmpty #-}
5252
{-# COMPILE AGDA2HS caseTelBind #-}
5353

54-
record Constructor (@0 pars : Scope Name) (@0 ixs : Scope Name) (@0 c : NameIn conScope) : Set where
54+
record Constructor (@0 pars : RScope Name) (@0 ixs : RScope Name) (@0 c : NameIn conScope) : Set where
5555
field
56-
conIndTypeS : TypeS (fieldScope c) pars -- the TypeS of the indexes of c
57-
conIx : ixs ⇒ (fieldScope c) <> pars -- how the indexes are constructred given parameters and c indices
56+
conIndTypeS : TypeS (extScope mempty pars) (fieldScope c) -- the TypeS of the indexes of c
57+
conIx : TermS (extScope mempty ixs) (concatRScope pars (fieldScope c)) -- how the indexes are constructred given parameters and c indices
5858
open Constructor public
5959

6060
{-# COMPILE AGDA2HS Constructor #-}
6161

62-
record Datatype (@0 pars : Scope Name) (@0 ixs : Scope Name) : Set where
62+
record Datatype (@0 pars : RScope Name) (@0 ixs : RScope Name) : Set where
6363
field
6464
dataConstructorScope : Scope Name
65-
dataSort : Sort pars
66-
dataParTypeS : TypeS pars mempty
67-
dataIxTypeS : TypeS ixs pars
65+
dataSort : Sort (extScope mempty pars)
66+
dataParTypeS : TypeS mempty pars
67+
dataIxTypeS : TypeS (extScope mempty pars) ixs
6868
dataConstructors : ((⟨ c ⟩ cp) : NameIn dataConstructorScope)
6969
Σ (c ∈ conScope) (λ p Constructor pars ixs (⟨ c ⟩ p))
7070
open Datatype public
@@ -181,3 +181,4 @@ opaque
181181
Δ₂ = subst0 (λ α₀ → Telescope α₀ γ) (sym (associativity (~ β) [ x ] α)) Δ₁
182182
⌈ x ∶ ty ◃ addTel Σ Δ₂ ⌉
183183
-}
184+

src/Agda/Core/Substitute.agda

Lines changed: 120 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
open import Scope
22

3-
open import Haskell.Prelude hiding (All; c; t)
3+
open import Haskell.Prelude hiding (All; coerce; a; b; c; t)
44
open 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)
58
open import Haskell.Extra.Erase
69

710
open import Utils.Either
@@ -18,16 +21,118 @@ module Agda.Core.Substitute
1821
private 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

24129
substTerm : α ⇒ β Term α Term β
25130
substSort : α ⇒ β Sort α Sort β
26131
substType : α ⇒ β Type α Type β
27132
substBranch : α ⇒ β Branch α c Branch β c
28133
substBranches : α ⇒ β Branches α cs Branches β cs
29-
substSubst : α ⇒ β γ ⇒ α γ ⇒ β
30-
substTypeS : α ⇒ β γ ⇛ α γ ⇛ β
134+
substTermS : α ⇒ β TermS α rγ TermS β rγ
135+
substTypeS : α ⇒ β TypeS α rγ TypeS β rγ
31136

32137
substSort 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

38143
substTerm f (TVar (⟨ x ⟩ k)) = lookupSubst f x k
39144
substTerm 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)
42147
substTerm f (TLam x v) = TLam x (substTerm (liftBindSubst f) v)
43148
substTerm f (TApp u v) = TApp (substTerm f u) (substTerm f v)
44149
substTerm f (TProj u p) = TProj (substTerm f u) p
45150
substTerm 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)
50155
substTerm f (TPi x a b) = TPi x (substType f a) (substType (liftBindSubst f) b)
51156
substTerm f (TSort s) = TSort (substSort f s)
52157
substTerm f (TLet x u v) = TLet x (substTerm f u) (substTerm (liftBindSubst f) v)
53158
substTerm 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

59164
substBranches f BsNil = BsNil
60165
substBranches 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

67172
substTypeS 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

71176
record 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 α)
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

99204
substTop : {{Substitute t}} Rezz α Term α t (x ◃ α) t α

0 commit comments

Comments
 (0)