Language3.agda 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ------------------------------------------------------------------------
  2. -- A small definition of a dependently typed language, using the
  3. -- technique from McBride's "Outrageous but Meaningful Coincidences"
  4. ------------------------------------------------------------------------
  5. ------------------------------------------------------------------------
  6. -- Prelude
  7. record ⊤ : Set₁ where
  8. record Σ (A : Set₁) (B : A → Set₁) : Set₁ where
  9. constructor _,_
  10. field
  11. proj₁ : A
  12. proj₂ : B proj₁
  13. open Σ
  14. uncurry : ∀ {A : Set₁} {B : A → Set₁} {C : Σ A B → Set₁} →
  15. ((x : A) (y : B x) → C (x , y)) →
  16. ((p : Σ A B) → C p)
  17. uncurry f (x , y) = f x y
  18. record ↑ (A : Set) : Set₁ where
  19. constructor lift
  20. field
  21. lower : A
  22. ------------------------------------------------------------------------
  23. -- Contexts
  24. -- The definition of contexts is parametrised by a universe.
  25. module Context (U : Set₁) (El : U → Set₁) where
  26. mutual
  27. -- Contexts.
  28. data Ctxt : Set₁ where
  29. ε : Ctxt
  30. _▻_ : (Γ : Ctxt) → Ty Γ → Ctxt
  31. -- Types.
  32. Ty : Ctxt → Set₁
  33. Ty Γ = Env Γ → U
  34. -- Environments.
  35. Env : Ctxt → Set₁
  36. Env ε = ⊤
  37. Env (Γ ▻ σ) = Σ (Env Γ) λ γ → El (σ γ)
  38. -- Variables (de Bruijn indices).
  39. infix 4 _∋_
  40. data _∋_ : (Γ : Ctxt) → Ty Γ → Set₁ where
  41. zero : ∀ {Γ σ} → Γ ▻ σ ∋ λ γ → σ (proj₁ γ)
  42. suc : ∀ {Γ σ τ} (x : Γ ∋ τ) → Γ ▻ σ ∋ λ γ → τ (proj₁ γ)
  43. -- A lookup function.
  44. lookup : ∀ {Γ σ} → Γ ∋ σ → (γ : Env Γ) → El (σ γ)
  45. lookup zero (γ , v) = v
  46. lookup (suc x) (γ , v) = lookup x γ
  47. ------------------------------------------------------------------------
  48. -- A universe
  49. mutual
  50. data U : Set₁ where
  51. set : U
  52. el : Set → U
  53. σ π : (a : U) → (El a → U) → U
  54. El : U → Set₁
  55. El set = Set
  56. El (el A) = ↑ A
  57. El (σ a b) = Σ (El a) λ x → El (b x)
  58. El (π a b) = (x : El a) → El (b x)
  59. open Context U El
  60. -- Abbreviations.
  61. infixr 20 _⊗_
  62. infixr 10 _⇾_
  63. _⇾_ : U → U → U
  64. a ⇾ b = π a λ _ → b
  65. _⊗_ : U → U → U
  66. a ⊗ b = σ a λ _ → b
  67. -- Example.
  68. raw-somethingU : U
  69. raw-somethingU =
  70. σ set λ obj →
  71. σ (el obj ⇾ el obj ⇾ set) λ hom →
  72. (π (el obj) λ x → el (hom x x))
  73. (π (el obj) λ x → el (hom x x))
  74. ------------------------------------------------------------------------
  75. -- A language
  76. mutual
  77. infixl 30 _·_
  78. infix 4 _⊢_
  79. -- Syntax for types.
  80. data Type (Γ : Ctxt) : Ty Γ → Set₁ where
  81. set : Type Γ (λ _ → set)
  82. el : (x : Γ ⊢ λ _ → set) → Type Γ (λ γ → el (⟦ x ⟧ γ))
  83. σ : ∀ {a b} → Type Γ a → Type (Γ ▻ a) b →
  84. Type Γ (λ γ → σ (a γ) (λ v → b (γ , v)))
  85. π : ∀ {a b} → Type Γ a → Type (Γ ▻ a) b →
  86. Type Γ (λ γ → π (a γ) (λ v → b (γ , v)))
  87. -- Terms.
  88. data _⊢_ (Γ : Ctxt) : Ty Γ → Set₁ where
  89. var : ∀ {a} → Γ ∋ a → Γ ⊢ a
  90. ƛ : ∀ {a b} → Γ ▻ a ⊢ uncurry b →
  91. Γ ⊢ (λ γ → π (a γ) (λ v → b γ v))
  92. _·_ : ∀ {a} {b : (γ : Env Γ) → El (a γ) → U} →
  93. Γ ⊢ (λ γ → π (a γ) (λ v → b γ v)) →
  94. (t₂ : Γ ⊢ a) → Γ ⊢ (λ γ → b γ (⟦ t₂ ⟧ γ))
  95. -- The semantics of a term.
  96. ⟦_⟧ : ∀ {Γ a} → Γ ⊢ a → (γ : Env Γ) → El (a γ)
  97. ⟦ var x ⟧ γ = lookup x γ
  98. ⟦ ƛ t ⟧ γ = λ v → ⟦ t ⟧ (γ , v)
  99. ⟦ t₁ · t₂ ⟧ γ = (⟦ t₁ ⟧ γ) (⟦ t₂ ⟧ γ)
  100. -- Example.
  101. raw-something : Type ε (λ _ → raw-somethingU)
  102. raw-something =
  103. σ set
  104. (σ (π (el (var zero)) (π (el (var (suc zero))) set))
  105. (σ (π (el (var (suc zero)))
  106. (el (var (suc zero) · var zero · var zero)))
  107. (π (el (var (suc (suc zero))))
  108. (el (var (suc (suc zero)) · var zero · var zero))
  109. )))