Chapter 1. Type Theory
We begin with importing the Agda primitives and renaming them to match the notation of book.
module Chapter1.Book where open import Agda.Primitive public renaming ( Set to Universe ; lsuc to infix 1 _βΊ ; SetΟ to π€Ο) variable πΎ πΏ π π : Level π° : (β : Level) β Universe (β βΊ) π° β = Universe β π°β = Universe lzero _βΊβΊ : (β : Level) β Level β βΊβΊ = (β βΊ) βΊ universe-of : {β : Level} (A : π° β) β Level universe-of {β} A = β
Section 1.3 Universes and families
-- Workaround to have cumulativity data Raised (πΏ : Level) (A : π° πΎ) : π° (πΎ β πΏ) where raise : A β Raised πΏ A
Section 1.4 Dependent function types
Ξ : {A : π° πΎ} (B : A β π° πΏ) β π° (πΎ β πΏ) Ξ {πΎ} {πΏ} {A} B = (x : A) β B x -Ξ : (A : π° πΎ) (B : A β π° πΏ) β π° (πΎ β πΏ) -Ξ A B = Ξ B infixr -1 -Ξ syntax -Ξ A (Ξ» x β b) = Ξ x κ A , b id : {A : π° πΎ} β A β A id a = a ππ : (A : π° πΎ) β A β A ππ A = id swap : Ξ A κ π° πΎ , Ξ B κ π° πΏ , Ξ C κ π° π , ((A β B β C) β (B β A β C)) swap A B C g b a = g a b -- Helpers domain : {A : π° πΎ} {B : π° πΏ} β (A β B) β π° πΎ domain {πΎ} {πΏ} {A} {B} f = A codomain : {A : π° πΎ} {B : π° πΏ} β (A β B) β π° πΏ codomain {πΎ} {πΏ} {A} {B} f = B
Section 1.5 Product types
We define the product type as a particular case of the dependent pair type, see the next section.
data π : π°β where β : π ind-π : (A : π β π° πΎ) β A β β (x : π) β A x ind-π A a β = a
Section 1.6 Dependent pairs types
record Ξ£ {A : π° πΎ} (B : A β π° πΏ) : π° (πΎ β πΏ) where constructor _,_ field x : A y : B x infixr 50 _,_ -Ξ£ : (A : π° πΎ) (B : A β π° πΏ) β π° (πΎ β πΏ) -Ξ£ A B = Ξ£ B infixr -1 -Ξ£ syntax -Ξ£ A (Ξ» x β y) = Ξ£ x κ A , y _Γ_ : (A : π° πΎ) (B : π° πΏ) β π° (πΎ β πΏ) A Γ B = Ξ£ x κ A , B infixr 30 _Γ_ rec-Ξ£ : {A : π° πΎ} {B : A β π° πΏ} {C : π° π} β ((x : A) (y : B x) β C) β Ξ£ B β C rec-Ξ£ g (x , y) = g x y ind-Ξ£ : {A : π° πΎ} {B : A β π° πΏ} {C : Ξ£ B β π° π} β ((x : A) (y : B x) β C (x , y)) β ((x , y) : Ξ£ B) β C (x , y) ind-Ξ£ g (x , y) = g x y prβ : {A : π° πΎ} {B : A β π° πΏ} β Ξ£ B β A prβ (x , y) = x prβ : {A : π° πΎ} {B : A β π° πΏ} β (z : Ξ£ B) β B (prβ z) prβ (x , y) = y -- The type-theoretic axiom of choice ac : {A : π° πΎ} {B : π° πΏ} {R : A β B β π° π} β (Ξ x κ A , Ξ£ y κ B , R x y) β (Ξ£ f κ (A β B) , Ξ x κ A , R x (f x)) ac g = ((Ξ» x β prβ (g x)) , (Ξ» x β prβ (g x))) -- The magma examples Magma : {πΎ : Level} β π° (πΎ βΊ) Magma {πΎ} = Ξ£ A κ π° πΎ , (A β A β A) PointedMagma : {πΎ : Level} β π° (πΎ βΊ) PointedMagma {πΎ} = Ξ£ A κ π° πΎ , ((A β A β A) Γ A)
Section 1.7 Coproduct types
data _β_ (A : π° πΎ) (B : π° πΏ) : π° (πΎ β πΏ) where inl : A β A β B inr : B β A β B infixr 20 _β_ β-rec : {A : π° πΎ} {B : π° πΏ} (C : π° π) β ((x : A) β C) β ((y : B) β C) β (A β B β C) β-rec C f g (inl x) = f x β-rec C f g (inr y) = g y β-ind : {A : π° πΎ} {B : π° πΏ} (C : A β B β π° π) β ((x : A) β C (inl x)) β ((y : B) β C (inr y)) β (z : A β B) β C z β-ind C f g (inl x) = f x β-ind C f g (inr y) = g y data π : π°β where ind-π : (A : π β π° πΎ) β (x : π) β A x ind-π A () -- Simple helper !π : (A : π° πΎ) β π β A !π A = ind-π (Ξ» _ β A)
Section 1.8 The type of booleans
π : π°β π = π β π pattern β = inl β pattern β = inr β π-rec : (C : π° πΎ) β C β C β π β C π-rec C cβ cβ β = cβ π-rec C cβ cβ β = cβ ind-π : (A : π β π° πΎ) β A β β A β β (n : π) β A n ind-π A aβ aβ β = aβ ind-π A aβ aβ β = aβ
Section 1.9 The natural numbers
data β : π°β where zero : β succ : β β β {-# BUILTIN NATURAL β #-} double : β β β double 0 = 0 double (succ n) = succ (succ n) add-β : β β β β β add-β 0 n = n add-β (succ m) n = succ (add-β m n) rec-β : (C : π° πΎ) β C β (β β C β C) β β β C rec-β C cβ cβ zero = cβ rec-β C cβ cβ (succ n) = cβ n (rec-β C cβ cβ n) ind-β : (C : β β π° πΎ) β C 0 β ((n : β) β C n β C (succ n)) β (n : β) β C n ind-β C cβ cβ 0 = cβ ind-β C cβ cβ (succ n) = cβ n (ind-β C cβ cβ n)
Section 1.11 Propositions as types
Β¬ : π° πΎ β π° πΎ Β¬ A = A β π -- Helpers ¬¬ ¬¬¬ : π° πΎ β π° πΎ ¬¬ A = Β¬ (Β¬ A) ¬¬¬ A = Β¬ (¬¬ A) de-Morgan : {A : π° πΎ} {B : π° πΏ} β (Β¬ A Γ Β¬ B) β (Β¬ (A β B)) de-Morgan (f , g) (inl a) = f a de-Morgan (f , g) (inr b) = g b Ξ -of-Γ : {A : π° πΎ} {P : A β π° πΏ} {Q : A β π° πΏ} β (Ξ x κ A , P x Γ Q x) β ((Ξ x κ A , P x) Γ (Ξ x κ A , Q x)) Ξ -of-Γ f = ((Ξ» x β prβ (f x)) , (Ξ» x β prβ (f x)))
Section 1.12 Identity types
data Id (A : π° πΎ) : A β A β π° πΎ where refl : (x : A) β Id A x x infix 0 Id _β‘_ : {A : π° πΎ} β A β A β π° πΎ x β‘ y = Id _ x y infix 0 _β‘_ {-# BUILTIN EQUALITY _β‘_ #-} {-# BUILTIN REWRITE _β‘_ #-} ind-β‘ : (A : π° πΎ) (C : (x y : A) β x β‘ y β π° πΏ) β ((x : A) β C x x (refl x)) β (x y : A) (p : x β‘ y) β C x y p ind-β‘ A C c x x (refl x) = c x based-ind-β‘ : (A : π° πΎ) (a : A) (C : (x : A) β a β‘ x β π° πΏ) (c : C a (refl a)) β (x : A) (p : a β‘ x) β C x p based-ind-β‘ A a C c .a (refl .a) = c --- Equivalence of path induction and based path induction based-ind-β‘βind-β‘ : (A : π° πΎ) (C : (x y : A) β x β‘ y β π° πΏ) β ((x : A) β C x x (refl x)) β (x y : A) (p : x β‘ y) β C x y p based-ind-β‘βind-β‘ A C c x y p = based-ind-β‘ A x (C x) (c x) y p ind-β‘βbased-ind-β‘ : (A : π° πΎ) (a : A) (C : (x : A) β a β‘ x β π° πΏ) (c : C a (refl a)) β (x : A) (p : a β‘ x) β C x p ind-β‘βbased-ind-β‘ {πΎ} {πΏ} A a C c x p = ind-β‘ A (Ξ» x y p β (C : ((z : A) β (x β‘ z) β π° πΏ)) β C x (refl x) β C y p) (Ξ» x C d β d) a x p C c --- Disequality _β’_ : {A : π° πΎ} β A β A β π° πΎ x β’ y = Β¬ (x β‘ y)