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