prop.lagda.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. ..
  2. ::
  3. {-# OPTIONS --prop #-}
  4. module language.prop where
  5. open import Agda.Primitive
  6. open import Agda.Builtin.Nat
  7. open import Agda.Builtin.Equality
  8. .. _prop:
  9. ****
  10. Prop
  11. ****
  12. ``Prop`` is Agda's built-in sort of *definitionally proof-irrelevant
  13. propositions*. It is similar to the sort ``Set``, but all elements of
  14. a type in ``Prop`` are considered to be (definitionally) equal.
  15. The implementation of ``Prop`` is based on the POPL 2019 paper
  16. `Definitional Proof-Irrelevance without K
  17. <https://hal.inria.fr/hal-01859964/>`_ by Gaëtan Gilbert, Jesper Cockx,
  18. Matthieu Sozeau, and Nicolas Tabareau.
  19. Usage
  20. -----
  21. Just as for ``Set``, we can define new types in ``Prop``'s as data or
  22. record types:
  23. ::
  24. data ⊥ : Prop where
  25. record ⊤ : Prop where
  26. constructor tt
  27. When defining a function from a data type in ``Prop`` to a type in
  28. ``Set``, pattern matching is restricted to the absurd pattern ``()``:
  29. ::
  30. absurd : (A : Set) → ⊥ → A
  31. absurd A ()
  32. Unlike for ``Set``, all elements of a type in ``Prop`` are
  33. definitionally equal. This implies all applications of ``absurd`` are
  34. the same:
  35. ::
  36. only-one-absurdity : {A : Set} → (p q : ⊥) → absurd A p ≡ absurd A q
  37. only-one-absurdity p q = refl
  38. Since pattern matching on datatypes in ``Prop`` is limited, it is
  39. recommended to define types in ``Prop`` as recursive functions rather
  40. than inductive datatypes. For example, the relation ``_≤_`` on natural
  41. numbers can be defined as follows:
  42. ::
  43. _≤_ : Nat → Nat → Prop
  44. zero ≤ y = ⊤
  45. suc x ≤ zero = ⊥
  46. suc x ≤ suc y = x ≤ y
  47. The induction principle for ``_≤_`` can then be defined by matching on
  48. the arguments of type ``Nat``:
  49. ::
  50. module _ (P : (m n : Nat) → Set)
  51. (pzy : (y : Nat) → P zero y)
  52. (pss : (x y : Nat) → P x y → P (suc x) (suc y)) where
  53. ≤-ind : (m n : Nat) → m ≤ n → P m n
  54. ≤-ind zero y pf = pzy y
  55. ≤-ind (suc x) (suc y) pf = pss x y (≤-ind x y pf)
  56. ≤-ind (suc _) zero ()
  57. Note that while it is also possible to define _≤_ as a
  58. datatype in Prop, it is hard to use that version because
  59. of the limitations to matching.
  60. When defining a record type in ``Set``, the types of the fields can be
  61. both in ``Set`` and ``Prop``. For example:
  62. ::
  63. record Fin (n : Nat) : Set where
  64. constructor _[_]
  65. field
  66. ⟦_⟧ : Nat
  67. proof : suc ⟦_⟧ ≤ n
  68. open Fin
  69. Fin-≡ : ∀ {n} (x y : Fin n) → ⟦ x ⟧ ≡ ⟦ y ⟧ → x ≡ y
  70. Fin-≡ x y refl = refl
  71. The predicative hierarchy of ``Prop``
  72. -------------------------------------
  73. Just as for ``Set``, Agda has a predicative hierarchy of sorts
  74. ``Prop₀`` (= ``Prop``), ``Prop₁``, ``Prop₂``, ... where ``Prop₀ :
  75. Set₁``, ``Prop₁ : Set₂``, ``Prop₂ : Set₃``, etc. Like ``Set``,
  76. ``Prop`` also supports universe polymorphism (see :ref:`universe
  77. levels <universe-levels>`), so for each ``ℓ : Level`` we have the sort
  78. ``Prop ℓ``. For example:
  79. ::
  80. True : ∀ {ℓ} → Prop (lsuc ℓ)
  81. True {ℓ} = ∀ (P : Prop ℓ) → P → P
  82. The propositional squash type
  83. -----------------------------
  84. When defining a datatype in ``Prop ℓ``, it is allowed to have
  85. constructors that take arguments in ``Set ℓ′`` for any ``ℓ′ ≤ ℓ``.
  86. For example, this allows us to define the propositional squash type
  87. and its eliminator:
  88. ::
  89. data Squash {ℓ} (A : Set ℓ) : Prop ℓ where
  90. squash : A → Squash A
  91. squash-elim : ∀ {ℓ₁ ℓ₂} (A : Set ℓ₁) (P : Prop ℓ₂) → (A → P) → Squash A → P
  92. squash-elim A P f (squash x) = f x
  93. This type allows us to simulate Agda's existing irrelevant arguments
  94. (see :ref:`irrelevance <irrelevance>`) by replacing .A with Squash A.
  95. Limitations
  96. -----------
  97. It is possible to define an equality type in Prop as follows:
  98. ::
  99. data _≐_ {ℓ} {A : Set ℓ} (x : A) : A → Prop ℓ where
  100. refl : x ≐ x
  101. However, the corresponding eliminator cannot be defined because of the
  102. limitations on pattern matching. As a consequence, this equality type
  103. is only useful for refuting impossible equations:
  104. ::
  105. 0≢1 : 0 ≐ 1 → ⊥
  106. 0≢1 ()