function-types.lagda.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ..
  2. ::
  3. {-# OPTIONS --allow-unsolved-metas --rewriting --sized-types #-}
  4. module language.function-types where
  5. open import language.built-ins
  6. data Vec (A : Set) : Nat → Set where
  7. nil : {n : Nat} → Vec A n
  8. cons : {n : Nat} (a : A) (as : Vec A n) → Vec A (suc n)
  9. _is-the-same-as_ = _≡_
  10. .. _function-types:
  11. **************
  12. Function Types
  13. **************
  14. Function types are written ``(x : A) → B``, or in the case of non-dependent functions simply ``A → B``. For instance, the type of the addition function for natural numbers is:
  15. ..
  16. ::
  17. example-hidden₁ : Set
  18. example-hidden₁ =
  19. ::
  20. Nat → Nat → Nat
  21. and the type of the addition function for vectors is:
  22. ..
  23. ::
  24. example-hidden₂ : Set₁
  25. example-hidden₂ =
  26. ::
  27. (A : Set) → (n : Nat) → (u : Vec A n) → (v : Vec A n) → Vec A n
  28. where ``Set`` is the type of sets and ``Vec A n`` is the type of vectors with ``n`` elements of type ``A``. Arrows between consecutive hypotheses of the form ``(x : A)`` may also be omitted, and ``(x : A) (y : A)`` may be shortened to ``(x y : A)``:
  29. ..
  30. ::
  31. example-hidden₃ : Set₁
  32. example-hidden₃ =
  33. ::
  34. (A : Set) (n : Nat)(u v : Vec A n) → Vec A n
  35. Functions are constructed by lambda abstractions, which can be either typed or untyped. For instance, both expressions below have type ``(A : Set) → A → A`` (the second expression checks against other types as well):
  36. ..
  37. ::
  38. example₁ example₂ : (A : Set) (x : A) → A
  39. ::
  40. example₁ = \ (A : Set)(x : A) → x
  41. example₂ = \ A x → x
  42. You can also use the Unicode symbol ``λ`` (type “\\lambda” or “\\Gl” in the Emacs Agda mode) instead of ``\`` (type “\\\\” in the Emacs Agda mode).
  43. The application of a function ``f : (x : A) → B`` to an argument ``a : A`` is written ``f a`` and the type of this is ``B[x := a]``.
  44. .. _notational-conventions:
  45. Notational conventions
  46. ----------------------
  47. Function types:
  48. ..
  49. ::
  50. module hidden₁ (A B C : Set) where
  51. ::
  52. prop₁ : ((x : A) (y : B) → C) is-the-same-as ((x : A) → (y : B) → C)
  53. prop₂ : ((x y : A) → C) is-the-same-as ((x : A)(y : A) → C)
  54. prop₃ : (forall (x : A) → C) is-the-same-as ((x : A) → C)
  55. prop₄ : (forall x → C) is-the-same-as ((x : _) → C)
  56. prop₅ : (forall x y → C) is-the-same-as (forall x → forall y → C)
  57. ..
  58. ::
  59. prop₁ = refl
  60. prop₂ = refl
  61. prop₃ = refl
  62. prop₄ = refl
  63. prop₅ = refl
  64. You can also use the Unicode symbol ``∀`` (type “\\all” in the Emacs Agda mode) instead of ``forall``.
  65. Functional abstraction:
  66. ..
  67. ::
  68. prop-hidden₁ : (A : Set) (e : A) →
  69. ::
  70. (\x y → e) is-the-same-as (\x → (\y → e))
  71. ..
  72. ::
  73. prop-hidden₁ _ _ = refl
  74. Functional application:
  75. ..
  76. ::
  77. prop-hidden₅ : (A B C : Set) (f : A → B → C) (a : A) (b : B) →
  78. ::
  79. (f a b) is-the-same-as ((f a) b)
  80. ..
  81. ::
  82. prop-hidden₅ _ _ _ _ _ _ = refl