Skip to content

Instantly share code, notes, and snippets.

@atennapel
Created January 4, 2022 15:39
Show Gist options
  • Save atennapel/1f139d7cf2a7558a9d844941749c5e11 to your computer and use it in GitHub Desktop.
Save atennapel/1f139d7cf2a7558a9d844941749c5e11 to your computer and use it in GitHub Desktop.
Infinitary indexed datatypes signatures with uncurried index
{-# OPTIONS --type-in-type #-}
open import Agda.Builtin.Unit
open import Agda.Builtin.Sigma
open import Data.Product
data Ix : Set where
U : Ix
Pi : (A : Set) -> (A -> Ix) -> Ix
data IxTm (I : Ix) : Ix -> Set where
End : IxTm I I
App : ∀ {A B} -> IxTm I (Pi A B) -> (x : A) -> IxTm I (B x)
IxData : Ix -> Set
IxData I = IxTm I U
data Ind (I : Ix) : Set where
U : IxData I -> Ind I
Pi : (A : Set) -> (A -> Ind I) -> Ind I
data Ty (I : Ix) : Set where
U : IxData I -> Ty I
Pi : (A : Set) -> (A -> Ty I) -> Ty I
PiInd : Ind I -> Ty I -> Ty I
data Ctx (I : Ix) : Set where
Nil : Ctx I
Cons : Ty I -> Ctx I -> Ctx I
data Var {I : Ix} : Ctx I -> Ty I -> Set where
VZ : ∀ {C A} -> Var (Cons A C) A
VS : ∀ {C A B} -> Var C A -> Var (Cons B C) A
ElInd : ∀ {I} -> Ind I -> (IxData I -> Set) -> Set
ElInd (U i) X = X i
ElInd (Pi A B) X = (x : A) -> ElInd (B x) X
data Tm {I : Ix} (C : Ctx I) : Ty I -> Set where
El : ∀ {A} -> Var C A -> Tm C A
App : ∀ {A B} -> Tm C (Pi A B) -> (a : A) -> Tm C (B a)
AppInd : ∀ {A B} -> Tm C (PiInd A B) -> ElInd A (λ i -> Tm C (U i)) -> Tm C B
Data : ∀ {I} -> Ctx I -> IxData I -> Set
Data C i = Tm C (U i)
ElimInd : ∀ {I} {C : Ctx I} (P : {i : IxData I} -> Data C i -> Set) (A : Ind I) -> ElInd A (Data C) -> Set
ElimInd P (U i) t = P t
ElimInd P (Pi A B) f = (x : A) -> ElimInd P (B x) (f x)
ElimTy : ∀ {I} {C : Ctx I} (P : {i : IxData I} -> Data C i -> Set) (A : Ty I) -> Tm C A -> Set
ElimTy P (U i) x = P x
ElimTy P (Pi A B) x = (a : A) -> ElimTy P (B a) (App x a)
ElimTy {C = C} P (PiInd A B) x = (a : ElInd A (Data C)) -> ElimInd P A a -> ElimTy P B (AppInd x a)
Elim : ∀ {I} {C' : Ctx I} (P : {i : IxData I} -> Data C' i -> Set) (C : Ctx I) -> (∀ {A} -> Var C A -> Var C' A) -> Set
Elim P Nil _ = ⊤
Elim P (Cons ty ctx) k = ElimTy P ty (El (k VZ)) × Elim P ctx (λ x -> k (VS x))
Elim' : ∀ {I} (C : Ctx I) (P : {i : IxData I} -> Data C i -> Set) -> Set
Elim' C P = Elim P C (λ x -> x)
elimVar : ∀ {I} {C' : Ctx I} (P : {i : IxData I} -> Data C' i -> Set) -> ∀ {C A} (v : Var C A) (k : ∀ {A} -> Var C A -> Var C' A) -> Elim P C k -> ElimTy P A (El (k v))
elimVar P VZ k (p , _) = p
elimVar P (VS v) k (_ , ps) = elimVar P v (λ x -> (k (VS x))) ps
elimInd : ∀ {I} {C : Ctx I} (P : {i : IxData I} -> Data C i -> Set) -> (A : Ind I) -> (f : ElInd A (Data C)) -> ({i : IxData I} -> (x : Data C i) -> ElimTy P (U i) x) -> ElimInd P A f
elimInd P (U i) x ind = ind x
elimInd P (Pi A B) f ind x = elimInd P (B x) (f x) ind
{-# TERMINATING #-}
elimTm : ∀ {I} {C : Ctx I} (P : {i : IxData I} -> Data C i -> Set) (ps : Elim' C P) -> ∀ {A} (x : Tm C A) -> ElimTy P A x
elimTm P ps (El v) = elimVar P v (λ x -> x) ps
elimTm P ps (App t a) = elimTm P ps t a
elimTm P ps (AppInd {A} t a) = elimTm P ps t a (elimInd P A a (elimTm P ps)) -- I cannot erase A, this is bad!
elim : ∀ {I} (C : Ctx I) (P : {i : IxData I} -> Data C i -> Set) {i : IxData I} (x : Data C i) -> Elim' C P -> P x
elim C P x ps = elimTm P ps x
-- testing
NatCtx : Ctx U
NatCtx = Cons (U End) (Cons (PiInd (U End) (U End)) Nil)
Nat : Set
Nat = Data NatCtx End
Z : Nat
Z = El VZ
S : Nat -> Nat
S n = AppInd (El (VS VZ)) n
FinCtx : Ctx (Pi Nat λ _ -> U)
FinCtx = Cons (Pi Nat λ n -> U (App End (S n))) (Cons (Pi Nat λ n -> PiInd (U (App End n)) (U (App End (S n)))) Nil)
Fin : Nat -> Set
Fin n = Data FinCtx (App End n)
FZ : {n : Nat} -> Fin (S n)
FZ {n} = App (El VZ) n
FS : {n : Nat} -> Fin n -> Fin (S n)
FS {n} x = AppInd (App (El (VS VZ)) n) x
indFin :
(P : {n : IxData (Pi Nat λ _ -> U)} -> Data FinCtx n -> Set) -- Ugly motive because index is uncurried!
(z : {n : Nat} -> P {App End (S n)} FZ)
(s : {n : Nat} -> (x : Fin n) -> P x -> P (FS x))
{n : Nat} (x : Fin n) -> P x
indFin P z s x = elim _ P x ((λ n -> z {n}) , (λ n -> s {n}) , tt)
finToNat : ∀ {n} -> Fin n -> Nat
finToNat = indFin (λ _ -> Nat) Z (λ _ ind -> S ind)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment