Language.agda 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ------------------------------------------------------------------------
  2. -- A small definition of a dependently typed language, using the
  3. -- technique from McBride's "Outrageous but Meaningful Coincidences"
  4. ------------------------------------------------------------------------
  5. -- The code contains an example, a partial definition of categories,
  6. -- which triggers the use of an enormous amount of memory (with the
  7. -- development version of Agda which is current at the time of
  8. -- writing). I'm not entirely sure if the code is correct: 2.5G heap
  9. -- doesn't seem to suffice to typecheck this code. /NAD
  10. module Language where
  11. ------------------------------------------------------------------------
  12. -- Prelude
  13. record ⊤ : Set₁ where
  14. record Σ (A : Set₁) (B : A → Set₁) : Set₁ where
  15. constructor _,_
  16. field
  17. proj₁ : A
  18. proj₂ : B proj₁
  19. open Σ
  20. uncurry : ∀ {A : Set₁} {B : A → Set₁} {C : Σ A B → Set₁} →
  21. ((x : A) (y : B x) → C (x , y)) →
  22. ((p : Σ A B) → C p)
  23. uncurry f (x , y) = f x y
  24. record ↑ (A : Set) : Set₁ where
  25. constructor lift
  26. field
  27. lower : A
  28. ------------------------------------------------------------------------
  29. -- Contexts
  30. -- The definition of contexts is parametrised by a universe.
  31. module Context (U : Set₁) (El : U → Set₁) where
  32. mutual
  33. -- Contexts.
  34. data Ctxt : Set₁ where
  35. ε : Ctxt
  36. _▻_ : (Γ : Ctxt) → Ty Γ → Ctxt
  37. -- Types.
  38. Ty : Ctxt → Set₁
  39. Ty Γ = Env Γ → U
  40. -- Environments.
  41. Env : Ctxt → Set₁
  42. Env ε = ⊤
  43. Env (Γ ▻ σ) = Σ (Env Γ) λ γ → El (σ γ)
  44. -- Variables (de Bruijn indices).
  45. infix 4 _∋_
  46. data _∋_ : (Γ : Ctxt) → Ty Γ → Set₁ where
  47. zero : ∀ {Γ σ} → Γ ▻ σ ∋ λ γ → σ (proj₁ γ)
  48. suc : ∀ {Γ σ τ} (x : Γ ∋ τ) → Γ ▻ σ ∋ λ γ → τ (proj₁ γ)
  49. -- A lookup function.
  50. lookup : ∀ {Γ σ} → Γ ∋ σ → (γ : Env Γ) → El (σ γ)
  51. lookup zero (γ , v) = v
  52. lookup (suc x) (γ , v) = lookup x γ
  53. ------------------------------------------------------------------------
  54. -- A universe
  55. mutual
  56. data U : Set₁ where
  57. set : U
  58. el : Set → U
  59. σ π : (a : U) → (El a → U) → U
  60. El : U → Set₁
  61. El set = Set
  62. El (el A) = ↑ A
  63. El (σ a b) = Σ (El a) λ x → El (b x)
  64. El (π a b) = (x : El a) → El (b x)
  65. open Context U El
  66. -- Abbreviations.
  67. infixr 20 _⊗_
  68. infixr 10 _⇾_
  69. _⇾_ : U → U → U
  70. a ⇾ b = π a λ _ → b
  71. _⊗_ : U → U → U
  72. a ⊗ b = σ a λ _ → b
  73. -- Example.
  74. raw-categoryU : U
  75. raw-categoryU =
  76. σ set λ obj →
  77. σ (el obj ⇾ el obj ⇾ set) λ hom →
  78. (π (el obj) λ x → el (hom x x))
  79. (π (el obj) λ x → π (el obj) λ y → π (el obj) λ z →
  80. el (hom x y) ⇾ el (hom y z) ⇾ el (hom x z))
  81. ------------------------------------------------------------------------
  82. -- A language
  83. mutual
  84. infixl 30 _·_
  85. infix 4 _⊢_
  86. -- Syntax for types.
  87. data Type : (Γ : Ctxt) → Ty Γ → Set₁ where
  88. set : ∀ {Γ} → Type Γ (λ _ → set)
  89. el : ∀ {Γ} (x : Γ ⊢ λ _ → set) → Type Γ (λ γ → el (⟦ x ⟧ γ))
  90. σ : ∀ {Γ a b} → Type Γ a → Type (Γ ▻ a) b →
  91. Type Γ (λ γ → σ (a γ) (λ v → b (γ , v)))
  92. π : ∀ {Γ a b} → Type Γ a → Type (Γ ▻ a) b →
  93. Type Γ (λ γ → π (a γ) (λ v → b (γ , v)))
  94. -- Terms.
  95. data _⊢_ : (Γ : Ctxt) → Ty Γ → Set₁ where
  96. var : ∀ {Γ a} → Γ ∋ a → Γ ⊢ a
  97. ƛ : ∀ {Γ a b} → Γ ▻ a ⊢ uncurry b →
  98. Γ ⊢ (λ γ → π (a γ) (λ v → b γ v))
  99. _·_ : ∀ {Γ a} {b : (γ : Env Γ) → El (a γ) → U} →
  100. Γ ⊢ (λ γ → π (a γ) (λ v → b γ v)) →
  101. (t₂ : Γ ⊢ a) → Γ ⊢ (λ γ → b γ (⟦ t₂ ⟧ γ))
  102. -- The semantics of a term.
  103. ⟦_⟧ : ∀ {Γ a} → Γ ⊢ a → (γ : Env Γ) → El (a γ)
  104. ⟦ var x ⟧ γ = lookup x γ
  105. ⟦ ƛ t ⟧ γ = λ v → ⟦ t ⟧ (γ , v)
  106. ⟦ t₁ · t₂ ⟧ γ = (⟦ t₁ ⟧ γ) (⟦ t₂ ⟧ γ)
  107. -- Example.
  108. raw-category : Type ε (λ _ → raw-categoryU)
  109. raw-category =
  110. -- Objects.
  111. σ set
  112. -- Morphisms.
  113. (σ (π (el (var zero)) (π (el (var (suc zero))) set))
  114. -- Identity.
  115. (σ (π (el (var (suc zero)))
  116. (el (var (suc zero) · var zero · var zero)))
  117. -- Composition.
  118. (π (el (var (suc (suc zero)))) -- X.
  119. (π (el (var (suc (suc (suc zero))))) -- Y.
  120. (π (el (var (suc (suc (suc (suc zero)))))) -- Z.
  121. (π (el (var (suc (suc (suc (suc zero)))) ·
  122. var (suc (suc zero)) ·
  123. var (suc zero))) -- Hom X Y.
  124. (π (el (var (suc (suc (suc (suc (suc zero))))) ·
  125. var (suc (suc zero)) ·
  126. var (suc zero))) -- Hom Y Z.
  127. (el (var (suc (suc (suc (suc (suc (suc zero)))))) ·
  128. var (suc (suc (suc (suc zero)))) ·
  129. var (suc (suc zero))))) -- Hom X Z.
  130. ))))))