Last active
January 5, 2022 14:34
-
-
Save atennapel/d93b35e3474ffa75367b6e67a98a953d to your computer and use it in GitHub Desktop.
Infinitary indexed datatype signatures with a single index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# OPTIONS --type-in-type #-} | |
open import Agda.Builtin.Unit | |
open import Agda.Builtin.Sigma | |
open import Agda.Builtin.Equality | |
open import Data.Product | |
data Ind (I : Set) : Set where | |
U : I -> Ind I | |
Pi : (A : Set) -> (A -> Ind I) -> Ind I | |
data Ty (I : Set) : Set where | |
U : I -> Ty I | |
Pi : (A : Set) -> (A -> Ty I) -> Ty I | |
PiInd : Ind I -> Ty I -> Ty I | |
data Ctx (I : Set) : Set where | |
Nil : Ctx I | |
Cons : Ty I -> Ctx I -> Ctx I | |
data Var {I : Set} : 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 -> (I -> Set) -> Set | |
ElInd (U i) X = X i | |
ElInd (Pi A B) X = (x : A) -> ElInd (B x) X | |
data Tm {I : Set} (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 -> I -> Set | |
Data C i = Tm C (U i) | |
ElimInd : ∀ {I} {C : Ctx I} (P : {i : I} -> Data C i -> Set) (A : Ind I) -> ElInd A (Data C) -> Set | |
ElimInd {I} 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 : 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 : 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 : I} -> Data C i -> Set) -> Set | |
Elim' C P = Elim P C (λ x -> x) | |
elimVar : ∀ {I} {C' : Ctx I} (P : {i : 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 : I} -> Data C i -> Set) -> (A : Ind I) -> (f : ElInd A (Data C)) -> ({i : 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 : 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 : I} -> Data C i -> Set) {i : I} (x : Data C i) -> Elim' C P -> P x | |
elim C P x ps = elimTm P ps x | |
-- testing | |
NatCtx : Ctx ⊤ | |
NatCtx = Cons (U tt) (Cons (PiInd (U tt) (U tt)) Nil) | |
Nat : Set | |
Nat = Data NatCtx tt | |
Z : Nat | |
Z = El VZ | |
S : Nat -> Nat | |
S n = AppInd (El (VS VZ)) n | |
FinCtx : Ctx Nat | |
FinCtx = Cons (Pi Nat λ n -> U (S n)) (Cons (Pi Nat λ n -> PiInd (U n) (U (S n))) Nil) | |
Fin : Nat -> Set | |
Fin n = Data FinCtx 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 : Nat} -> Fin n -> Set) | |
(z : {n : Nat} -> P {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