-
Notifications
You must be signed in to change notification settings - Fork 247
/
Copy pathSubstitution.agda
187 lines (129 loc) · 4.92 KB
/
Substitution.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
------------------------------------------------------------------------
-- The Agda standard library
--
-- Substitutions
------------------------------------------------------------------------
-- Uses a variant of Conor McBride's technique from his paper
-- "Type-Preserving Renaming and Substitution".
-- See README.Data.Fin.Substitution.UntypedLambda for an example
-- of how this module can be used: a definition of substitution for
-- the untyped λ-calculus.
{-# OPTIONS --cubical-compatible --safe #-}
module Data.Fin.Substitution where
open import Data.Nat.Base hiding (_⊔_; _/_)
open import Data.Fin.Base using (Fin; zero; suc)
open import Data.Vec.Base using (Vec; []; _∷_; map; lookup)
open import Function.Base as Fun using (flip)
open import Relation.Binary.Construct.Closure.ReflexiveTransitive
as Star using (Star; ε; _◅_)
open import Level using (Level; _⊔_; 0ℓ)
open import Relation.Unary using (Pred)
private
variable
ℓ ℓ₁ ℓ₂ : Level
k m n o : ℕ
------------------------------------------------------------------------
-- General functionality
-- A Sub T m n is a substitution which, when applied to something with
-- at most m variables, yields something with at most n variables.
Sub : Pred ℕ ℓ → ℕ → ℕ → Set ℓ
Sub T m n = Vec (T n) m
-- A /reversed/ sequence of matching substitutions.
Subs : Pred ℕ ℓ → ℕ → ℕ → Set ℓ
Subs T = flip (Star (flip (Sub T)))
-- Some simple substitutions.
record Simple (T : Pred ℕ ℓ) : Set ℓ where
infix 10 _↑
infixl 10 _↑⋆_ _↑✶_
field
var : ∀ {n} → Fin n → T n -- Takes variables to Ts.
weaken : ∀ {n} → T n → T (suc n) -- Weakens Ts.
-- Lifting.
_↑ : Sub T m n → Sub T (suc m) (suc n)
ρ ↑ = var zero ∷ map weaken ρ
_↑⋆_ : Sub T m n → ∀ k → Sub T (k + m) (k + n)
ρ ↑⋆ zero = ρ
ρ ↑⋆ suc k = (ρ ↑⋆ k) ↑
_↑✶_ : Subs T m n → ∀ k → Subs T (k + m) (k + n)
ρs ↑✶ k = Star.gmap (_+_ k) (λ ρ → ρ ↑⋆ k) ρs
-- The identity substitution.
id : Sub T n n
id {zero} = []
id {suc n} = id ↑
-- Weakening.
wk⋆ : ∀ k → Sub T n (k + n)
wk⋆ zero = id
wk⋆ (suc k) = map weaken (wk⋆ k)
wk : Sub T n (suc n)
wk = wk⋆ 1
-- A substitution which only replaces the first variable.
sub : T n → Sub T (suc n) n
sub t = t ∷ id
-- Application of substitutions.
record Application (T₁ : Pred ℕ ℓ₁) (T₂ : Pred ℕ ℓ₂) : Set (ℓ₁ ⊔ ℓ₂) where
infixl 8 _/_ _/✶_
infixl 9 _⊙_
-- Post-application of substitutions to things.
field _/_ : ∀ {m n} → T₁ m → Sub T₂ m n → T₁ n
-- Reverse composition. (Fits well with post-application.)
_⊙_ : Sub T₁ m n → Sub T₂ n o → Sub T₁ m o
ρ₁ ⊙ ρ₂ = map (_/ ρ₂) ρ₁
-- Application of multiple substitutions.
_/✶_ : T₁ m → Subs T₂ m n → T₁ n
_/✶_ = Star.gfold Fun.id _ (flip _/_) {z = zero}
-- A combination of the two records above.
record Subst (T : Pred ℕ ℓ) : Set ℓ where
field
simple : Simple T
application : Application T T
open Simple simple public
open Application application public
-- Composition of multiple substitutions.
⨀ : Subs T m n → Sub T m n
⨀ ε = id
⨀ (ρ ◅ ε) = ρ -- Convenient optimisation; simplifies some proofs.
⨀ (ρ ◅ ρs) = ⨀ ρs ⊙ ρ
------------------------------------------------------------------------
-- Instantiations and code for facilitating instantiations
-- Liftings from T₁ to T₂.
record Lift (T₁ : Pred ℕ ℓ₁) (T₂ : Pred ℕ ℓ₂) : Set (ℓ₁ ⊔ ℓ₂) where
field
simple : Simple T₁
lift : ∀ {n} → T₁ n → T₂ n
open Simple simple public
-- Variable substitutions (renamings).
module VarSubst where
subst : Subst Fin
subst = record
{ simple = record { var = Fun.id; weaken = suc }
; application = record { _/_ = flip lookup }
}
open Subst subst public
-- "Term" substitutions.
record TermSubst (T : Pred ℕ 0ℓ) : Set₁ where
field
var : ∀ {n} → Fin n → T n
app : ∀ {T′ : Pred ℕ 0ℓ} → Lift T′ T → ∀ {m n} → T m → Sub T′ m n → T n
module Lifted {T′ : Pred ℕ 0ℓ} (lift : Lift T′ T) where
application : Application T T′
application = record { _/_ = app lift }
open Lift lift public
open Application application public
varLift : Lift Fin T
varLift = record { simple = VarSubst.simple; lift = var }
infixl 8 _/Var_
_/Var_ : T m → Sub Fin m n → T n
_/Var_ = app varLift
simple : Simple T
simple = record
{ var = var
; weaken = λ t → t /Var VarSubst.wk
}
termLift : Lift T T
termLift = record { simple = simple; lift = Fun.id }
subst : Subst T
subst = record
{ simple = simple
; application = Lifted.application termLift
}
open Subst subst public hiding (var; simple)