{-# OPTIONS --without-K --safe #-}
module Definition.Untyped {a} (M : Set a) where
open import Tools.Fin
open import Tools.Nat
open import Tools.Product
open import Tools.List
import Tools.PropositionalEquality as PE
private
variable
p q r : M
n m ℓ : Nat
infixl 30 _∙_
infixr 5 _∷_
infix 30 Π_,_▷_▹_
infix 30 Σ_▷_▹_
infix 30 Σₚ_▷_▹_
infix 30 Σᵣ_▷_▹_
infix 30 Σ⟨_⟩_▷_▹_
infix 30 ⟦_⟧_▹_
infixl 30 _ₛ•ₛ_ _•ₛ_ _ₛ•_
infix 25 _[_]
infix 25 _[_]↑
infix 25 _[_,_]
infix 25 _[_]↑²
data Con (A : Nat → Set a) : Nat → Set a where
ε : Con A 0
_∙_ : {n : Nat} → Con A n → A n → Con A (1+ n)
data GenTs (A : Nat → Set a) : Nat → List Nat → Set a where
[] : {n : Nat} → GenTs A n []
_∷_ : {n b : Nat} {bs : List Nat} (t : A (b + n)) (ts : GenTs A n bs) → GenTs A n (b ∷ bs)
data SigmaMode : Set where
Σₚ Σᵣ : SigmaMode
data Kind : (ns : List Nat) → Set a where
Ukind : Kind []
Pikind : (p q : M) → Kind (0 ∷ 1 ∷ [])
Lamkind : (p : M) → Kind (1 ∷ [])
Appkind : (p : M) → Kind (0 ∷ 0 ∷ [])
Sigmakind : (q : M) → SigmaMode → Kind (0 ∷ 1 ∷ [])
Prodkind : Kind (0 ∷ 0 ∷ [])
Fstkind : Kind (0 ∷ [])
Sndkind : Kind (0 ∷ [])
Prodreckind : (p : M) → Kind (1 ∷ 0 ∷ 2 ∷ [])
Natkind : Kind []
Zerokind : Kind []
Suckind : Kind (0 ∷ [])
Natreckind : (p q : M) → Kind (1 ∷ 0 ∷ 2 ∷ 0 ∷ [])
Unitkind : Kind []
Starkind : Kind []
Emptykind : Kind []
Emptyreckind : (p : M) → Kind (0 ∷ 0 ∷ [])
data Term (n : Nat) : Set a where
var : (x : Fin n) → Term n
gen : {bs : List Nat} (k : Kind bs) (ts : GenTs Term n bs) → Term n
private
variable
A F H t u v : Term n
B E G t′ : Term (1+ n)
U : Term n
U = gen Ukind []
Π_,_▷_▹_ : (p q : M) (A : Term n) (B : Term (1+ n)) → Term n
Π p , q ▷ A ▹ B = gen (Pikind p q) (A ∷ B ∷ [])
Σᵣ_▷_▹_ : (p : M) (A : Term n) (B : Term (1+ n)) → Term n
Σᵣ q ▷ A ▹ B = gen (Sigmakind q Σᵣ) (A ∷ B ∷ [])
Σₚ_▷_▹_ : (p : M) (A : Term n) (B : Term (1+ n)) → Term n
Σₚ q ▷ A ▹ B = gen (Sigmakind q Σₚ) (A ∷ B ∷ [])
ℕ : Term n
ℕ = gen Natkind []
Empty : Term n
Empty = gen Emptykind []
Unit : Term n
Unit = gen Unitkind []
lam : (p : M) (t : Term (1+ n)) → Term n
lam p t = gen (Lamkind p) (t ∷ [])
_∘_▷_ : (t : Term n) (p : M) (u : Term n) → Term n
t ∘ p ▷ u = gen (Appkind p) (t ∷ u ∷ [])
prod : (t u : Term n) → Term n
prod t u = gen Prodkind (t ∷ u ∷ [])
fst : (t : Term n) → Term n
fst t = gen Fstkind (t ∷ [])
snd : (t : Term n) → Term n
snd t = gen Sndkind (t ∷ [])
prodrec : (p : M) (A : Term (1+ n)) (t : Term n) (u : Term (1+ (1+ n))) → Term n
prodrec p A t u = gen (Prodreckind p) (A ∷ t ∷ u ∷ [])
zero : Term n
zero = gen Zerokind []
suc : (t : Term n) → Term n
suc t = gen Suckind (t ∷ [])
natrec : (p q : M) (A : Term (1+ n)) (z : Term n)
(s : Term (1+ (1+ n))) (t : Term n) → Term n
natrec p q A z s n = gen (Natreckind p q) (A ∷ z ∷ s ∷ n ∷ [])
star : Term n
star = gen Starkind []
Emptyrec : (p : M) (A e : Term n) → Term n
Emptyrec p A e = gen (Emptyreckind p) (A ∷ e ∷ [])
data BindingType : Set a where
BΠ : (p q : M) → BindingType
BΣ : (p : M) → SigmaMode → BindingType
pattern BΠ! = BΠ _ _
pattern BΣ! = BΣ _ _
pattern BΣᵣ = BΣ _ Σᵣ
pattern BΣₚ = BΣ _ Σₚ
pattern Σ_▷_▹_ q A B = gen (Sigmakind q _) (A ∷ B ∷ [])
pattern Σ⟨_⟩_▷_▹_ m q A B = gen (Sigmakind q m) (A ∷ B ∷ [])
⟦_⟧_▹_ : BindingType → Term n → Term (1+ n) → Term n
⟦ BΠ p q ⟧ F ▹ G = Π p , q ▷ F ▹ G
⟦ BΣ q m ⟧ F ▹ G = Σ⟨ m ⟩ q ▷ F ▹ G
B-PE-injectivity : ∀ W W' → ⟦ W ⟧ F ▹ G PE.≡ ⟦ W' ⟧ H ▹ E
→ F PE.≡ H × G PE.≡ E × W PE.≡ W'
B-PE-injectivity (BΠ p q) (BΠ .p .q) PE.refl = PE.refl , PE.refl , PE.refl
B-PE-injectivity (BΣ q m) (BΣ .q .m) PE.refl = PE.refl , PE.refl , PE.refl
BΠ-PE-injectivity : ∀ {p p′ q q′} → BΠ p q PE.≡ BΠ p′ q′ → p PE.≡ p′ × q PE.≡ q′
BΠ-PE-injectivity PE.refl = PE.refl , PE.refl
BΣ-PE-injectivity : ∀ {q q′ m m′} → BΣ q m PE.≡ BΣ q′ m′ → q PE.≡ q′ × m PE.≡ m′
BΣ-PE-injectivity PE.refl = PE.refl , PE.refl
suc-PE-injectivity : suc t PE.≡ suc u → t PE.≡ u
suc-PE-injectivity PE.refl = PE.refl
data Neutral : Term n → Set a where
var : (x : Fin n) → Neutral (var x)
∘ₙ : Neutral t → Neutral (t ∘ p ▷ u)
fstₙ : Neutral t → Neutral (fst t)
sndₙ : Neutral t → Neutral (snd t)
natrecₙ : Neutral v → Neutral (natrec p q G t u v)
prodrecₙ : Neutral t → Neutral (prodrec p A t u)
Emptyrecₙ : Neutral t → Neutral (Emptyrec p A t)
data Whnf {n : Nat} : Term n → Set a where
Uₙ : Whnf U
Πₙ : Whnf (Π p , q ▷ A ▹ B)
Σₙ : ∀ {m} → Whnf (Σ⟨ m ⟩ q ▷ A ▹ B)
ℕₙ : Whnf ℕ
Unitₙ : Whnf Unit
Emptyₙ : Whnf Empty
lamₙ : Whnf (lam p t)
zeroₙ : Whnf zero
sucₙ : Whnf (suc t)
starₙ : Whnf star
prodₙ : Whnf (prod t u)
ne : Neutral t → Whnf t
U≢ne : Neutral A → U PE.≢ A
U≢ne () PE.refl
ℕ≢ne : Neutral A → ℕ PE.≢ A
ℕ≢ne () PE.refl
Empty≢ne : Neutral A → Empty PE.≢ A
Empty≢ne () PE.refl
Unit≢ne : Neutral A → Unit PE.≢ A
Unit≢ne () PE.refl
B≢ne : ∀ W → Neutral A → ⟦ W ⟧ F ▹ G PE.≢ A
B≢ne (BΠ p q) () PE.refl
B≢ne (BΣ q m) () PE.refl
U≢B : ∀ W → U PE.≢ ⟦ W ⟧ F ▹ G
U≢B (BΠ p q) ()
U≢B (BΣ q m) ()
ℕ≢B : ∀ W → ℕ PE.≢ ⟦ W ⟧ F ▹ G
ℕ≢B (BΠ p q) ()
ℕ≢B (BΣ q m) ()
Empty≢B : ∀ W → Empty PE.≢ ⟦ W ⟧ F ▹ G
Empty≢B (BΠ p q) ()
Empty≢B (BΣ q m) ()
Unit≢B : ∀ W → Unit PE.≢ ⟦ W ⟧ F ▹ G
Unit≢B (BΠ p q) ()
Unit≢B (BΣ q m) ()
zero≢ne : Neutral t → zero PE.≢ t
zero≢ne () PE.refl
suc≢ne : Neutral t → suc u PE.≢ t
suc≢ne () PE.refl
prod≢ne : Neutral v → prod t u PE.≢ v
prod≢ne () PE.refl
data Natural {n : Nat} : Term n → Set a where
zeroₙ : Natural zero
sucₙ : Natural (suc t)
ne : Neutral t → Natural t
data Type {n : Nat} : Term n → Set a where
Πₙ : Type (Π p , q ▷ A ▹ B)
Σₙ : ∀ {m} → Type (Σ⟨ m ⟩ p ▷ A ▹ B)
ℕₙ : Type ℕ
Emptyₙ : Type Empty
Unitₙ : Type Unit
ne : Neutral t → Type t
⟦_⟧-type : ∀ (W : BindingType) → Type (⟦ W ⟧ F ▹ G)
⟦ BΠ p q ⟧-type = Πₙ
⟦ BΣ q m ⟧-type = Σₙ
data Function {n : Nat} : Term n → Set a where
lamₙ : Function (lam p t)
ne : Neutral t → Function t
data Product {n : Nat} : Term n → Set a where
prodₙ : Product (prod t u)
ne : Neutral t → Product t
naturalWhnf : Natural t → Whnf t
naturalWhnf sucₙ = sucₙ
naturalWhnf zeroₙ = zeroₙ
naturalWhnf (ne x) = ne x
typeWhnf : Type A → Whnf A
typeWhnf Πₙ = Πₙ
typeWhnf Σₙ = Σₙ
typeWhnf ℕₙ = ℕₙ
typeWhnf Emptyₙ = Emptyₙ
typeWhnf Unitₙ = Unitₙ
typeWhnf (ne x) = ne x
functionWhnf : Function t → Whnf t
functionWhnf lamₙ = lamₙ
functionWhnf (ne x) = ne x
productWhnf : Product t → Whnf t
productWhnf prodₙ = prodₙ
productWhnf (ne x) = ne x
⟦_⟧ₙ : (W : BindingType) → Whnf (⟦ W ⟧ F ▹ G)
⟦_⟧ₙ (BΠ p q) = Πₙ
⟦_⟧ₙ (BΣ q m) = Σₙ
data Wk : Nat → Nat → Set where
id : {n : Nat} → Wk n n
step : {n m : Nat} → Wk m n → Wk (1+ m) n
lift : {n m : Nat} → Wk m n → Wk (1+ m) (1+ n)
infixl 30 _•_
_•_ : {l m n : Nat} → Wk l m → Wk m n → Wk l n
id • η′ = η′
step η • η′ = step (η • η′)
lift η • id = lift η
lift η • step η′ = step (η • η′)
lift η • lift η′ = lift (η • η′)
liftn : {k m : Nat} → Wk k m → (n : Nat) → Wk (n + k) (n + m)
liftn ρ 0 = ρ
liftn ρ (1+ n) = lift (liftn ρ n)
wkVar : {m n : Nat} (ρ : Wk m n) (x : Fin n) → Fin m
wkVar id x = x
wkVar (step ρ) x = (wkVar ρ x) +1
wkVar (lift ρ) x0 = x0
wkVar (lift ρ) (x +1) = (wkVar ρ x) +1
mutual
wkGen : {m n : Nat} {bs : List Nat} (ρ : Wk m n) (c : GenTs (Term) n bs) → GenTs (Term) m bs
wkGen ρ [] = []
wkGen ρ (_∷_ {b = b} t c) = (wk (liftn ρ b) t) ∷ (wkGen ρ c)
wk : {m n : Nat} (ρ : Wk m n) (t : Term n) → Term m
wk ρ (var x) = var (wkVar ρ x)
wk ρ (gen k c) = gen k (wkGen ρ c)
wk1 : Term n → Term (1+ n)
wk1 = wk (step id)
wkNeutral : ∀ ρ → Neutral t → Neutral {n = n} (wk ρ t)
wkNeutral ρ (var n) = var (wkVar ρ n)
wkNeutral ρ (∘ₙ n) = ∘ₙ (wkNeutral ρ n)
wkNeutral ρ (fstₙ n) = fstₙ (wkNeutral ρ n)
wkNeutral ρ (sndₙ n) = sndₙ (wkNeutral ρ n)
wkNeutral ρ (natrecₙ n) = natrecₙ (wkNeutral ρ n)
wkNeutral ρ (prodrecₙ n) = prodrecₙ (wkNeutral ρ n)
wkNeutral ρ (Emptyrecₙ e) = Emptyrecₙ (wkNeutral ρ e)
wkNatural : ∀ ρ → Natural t → Natural {n = n} (wk ρ t)
wkNatural ρ sucₙ = sucₙ
wkNatural ρ zeroₙ = zeroₙ
wkNatural ρ (ne x) = ne (wkNeutral ρ x)
wkType : ∀ ρ → Type t → Type {n = n} (wk ρ t)
wkType ρ Πₙ = Πₙ
wkType ρ Σₙ = Σₙ
wkType ρ ℕₙ = ℕₙ
wkType ρ Emptyₙ = Emptyₙ
wkType ρ Unitₙ = Unitₙ
wkType ρ (ne x) = ne (wkNeutral ρ x)
wkFunction : ∀ ρ → Function t → Function {n = n} (wk ρ t)
wkFunction ρ lamₙ = lamₙ
wkFunction ρ (ne x) = ne (wkNeutral ρ x)
wkProduct : ∀ ρ → Product t → Product {n = n} (wk ρ t)
wkProduct ρ prodₙ = prodₙ
wkProduct ρ (ne x) = ne (wkNeutral ρ x)
wkWhnf : ∀ ρ → Whnf t → Whnf {n = n} (wk ρ t)
wkWhnf ρ Uₙ = Uₙ
wkWhnf ρ Πₙ = Πₙ
wkWhnf ρ Σₙ = Σₙ
wkWhnf ρ ℕₙ = ℕₙ
wkWhnf ρ Emptyₙ = Emptyₙ
wkWhnf ρ Unitₙ = Unitₙ
wkWhnf ρ lamₙ = lamₙ
wkWhnf ρ prodₙ = prodₙ
wkWhnf ρ zeroₙ = zeroₙ
wkWhnf ρ sucₙ = sucₙ
wkWhnf ρ starₙ = starₙ
wkWhnf ρ (ne x) = ne (wkNeutral ρ x)
Subst : Nat → Nat → Set a
Subst m n = Fin n → Term m
head : Subst m (1+ n) → Term m
head σ = σ x0
tail : Subst m (1+ n) → Subst m n
tail σ x = σ (x +1)
substVar : (σ : Subst m n) (x : Fin n) → Term m
substVar σ x = σ x
idSubst : Subst n n
idSubst = var
wk1Subst : Subst m n → Subst (1+ m) n
wk1Subst σ x = wk1 (σ x)
liftSubst : (σ : Subst m n) → Subst (1+ m) (1+ n)
liftSubst σ x0 = var x0
liftSubst σ (x +1) = wk1Subst σ x
liftSubstn : {k m : Nat} → Subst k m → (n : Nat) → Subst (n + k) (n + m)
liftSubstn σ Nat.zero = σ
liftSubstn σ (1+ n) = liftSubst (liftSubstn σ n)
toSubst : Wk m n → Subst m n
toSubst pr x = var (wkVar pr x)
mutual
substGen : {bs : List Nat} (σ : Subst m n) (g : GenTs (Term) n bs) → GenTs (Term) m bs
substGen σ [] = []
substGen σ (_∷_ {b = b} t ts) = subst (liftSubstn σ b) t ∷ (substGen σ ts)
subst : (σ : Subst m n) (t : Term n) → Term m
subst σ (var x) = substVar σ x
subst σ (gen x c) = gen x (substGen σ c)
consSubst : Subst m n → Term m → Subst m (1+ n)
consSubst σ t x0 = t
consSubst σ t (x +1) = σ x
sgSubst : Term n → Subst n (1+ n)
sgSubst = consSubst idSubst
_ₛ•ₛ_ : Subst ℓ m → Subst m n → Subst ℓ n
_ₛ•ₛ_ σ σ′ x = subst σ (σ′ x)
_•ₛ_ : Wk ℓ m → Subst m n → Subst ℓ n
_•ₛ_ ρ σ x = wk ρ (σ x)
_ₛ•_ : Subst ℓ m → Wk m n → Subst ℓ n
_ₛ•_ σ ρ x = σ (wkVar ρ x)
_[_] : (t : Term (1+ n)) (s : Term n) → Term n
t [ s ] = subst (sgSubst s) t
_[_]↑ : (t : Term (1+ n)) (s : Term (1+ n)) → Term (1+ n)
t [ s ]↑ = subst (consSubst (wk1Subst idSubst) s) t
_[_,_] : (t : Term (1+ (1+ n))) (s s′ : Term n) → Term n
t [ s , s′ ] = subst (consSubst (consSubst idSubst s) s′) t
_[_]↑² : (t : Term (1+ n)) (s : Term (1+ (1+ n))) → Term (1+ (1+ n))
t [ s ]↑² = subst (consSubst (wk1Subst (wk1Subst idSubst)) s) t
B-subst : (σ : Subst m n) (W : BindingType) (F : Term n) (G : Term (1+ n))
→ subst σ (⟦ W ⟧ F ▹ G) PE.≡ ⟦ W ⟧ (subst σ F) ▹ (subst (liftSubst σ) G)
B-subst σ (BΠ p q) F G = PE.refl
B-subst σ (BΣ q m) F G = PE.refl