Skip to content

Instantly share code, notes, and snippets.

@atennapel
Created January 4, 2022 14:26
Show Gist options
  • Save atennapel/28471645c5a1fb50437d590bc8277d7a to your computer and use it in GitHub Desktop.
Save atennapel/28471645c5a1fb50437d590bc8277d7a to your computer and use it in GitHub Desktop.
Non-inductive datatype signatures
{-# OPTIONS --type-in-type #-}
open import Agda.Builtin.Unit
open import Agda.Builtin.Sigma
open import Data.Product
data Ty : Set where
U : Ty
Pi : (A : Set) -> (A -> Ty) -> Ty
data Ctx : Set where
Nil : Ctx
Cons : Ty -> Ctx -> Ctx
data Var : Ctx -> Ty -> Set where
VZ : ∀ {C A} -> Var (Cons A C) A
VS : ∀ {C A B} -> Var C A -> Var (Cons B C) A
data Tm (C : Ctx) : Ty -> Set where
El : ∀ {A} -> Var C A -> Tm C A
App : ∀ {A B} -> Tm C (Pi A B) -> (a : A) -> Tm C (B a)
Data : Ctx -> Set
Data C = Tm C U
ElimTy : ∀ {C} (P : Data C -> Set) (A : Ty) -> Tm C A -> Set
ElimTy P U x = P x
ElimTy P (Pi A B) x = (a : A) -> ElimTy P (B a) (App x a)
Elim : ∀ {C'} (P : Data C' -> Set) (C : Ctx) -> (∀ {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' : (C : Ctx) (P : Data C -> Set) -> Set
Elim' C P = Elim P C (λ x -> x)
elimVar : ∀ {C'} (P : Data C' -> 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
elimTm : ∀ {C} (P : Data C -> 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
elim : (C : Ctx) (P : Data C -> Set) (x : Data C) -> Elim' C P -> P x
elim C P x ps = elimTm P ps x
-- testing
Fun : Set -> Ty -> Ty
Fun A B = Pi A λ _ -> B
SumCtx : Set -> Set -> Ctx
SumCtx A B = Cons (Fun A U) (Cons (Fun B U) Nil)
Sum : Set -> Set -> Set
Sum A B = Data (SumCtx A B)
Left : ∀ {A B} -> A -> Sum A B
Left x = App (El VZ) x
Right : ∀ {A B} -> B -> Sum A B
Right x = App (El (VS VZ)) x
indSum : ∀ {A B} (P : Sum A B -> Set) -> ((x : A) -> P (Left x)) -> ((x : B) -> P (Right x)) -> (x : Sum A B) -> P x
indSum {A} {B} P left right x = elim (SumCtx A B) P x (left , right , tt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment