syntax-declarations.lagda.rst 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ..
  2. ::
  3. module language.syntax-declarations where
  4. .. _syntax-declarations:
  5. *******************
  6. Syntax Declarations
  7. *******************
  8. .. note::
  9. This is a stub
  10. It is now possible to declare user-defined syntax that binds
  11. identifiers. Example:
  12. ..
  13. ::
  14. postulate
  15. ℕ ⊤ : Set
  16. suc : ℕ → ℕ
  17. ..
  18. ::
  19. module First where
  20. ::
  21. record Σ (A : Set) (B : A → Set) : Set where
  22. constructor _,_
  23. field fst : A
  24. snd : B fst
  25. syntax Σ A (λ x → B) = [ x ∈ A ] × B
  26. witness : ∀ {A B} → [ x ∈ A ] × B → A
  27. witness (x , _) = x
  28. The syntax declaration for ``Σ`` implies that ``x`` is in scope in
  29. ``B``, but not in ``A``.
  30. You can give fixity declarations along with syntax declarations:
  31. ..
  32. ::
  33. module Second where
  34. record Σ (A : Set) (B : A → Set) : Set where
  35. constructor _,_
  36. field fst : A
  37. snd : B fst
  38. ::
  39. infix 5 Σ
  40. syntax Σ A (λ x → B) = [ x ∈ A ] × B
  41. The fixity applies to the syntax, not the name; syntax declarations
  42. are also restricted to ordinary, non-operator names. The following
  43. declaration is disallowed:
  44. .. code-block:: agda
  45. syntax _==_ x y = x === y
  46. Syntax declarations must also be linear; the following declaration
  47. is disallowed:
  48. .. code-block:: agda
  49. syntax wrong x = x + x
  50. Syntax declarations can have implicit arguments. For example:
  51. ::
  52. id : ∀ {a}{A : Set a} -> A -> A
  53. id x = x
  54. syntax id {A} x = x ∈ A
  55. Unlike :ref:`mixfix operators <mixfix-operators>` that can be used unapplied
  56. using the name including all the underscores, or partially applied by replacing
  57. only some of the underscores by arguments, syntax must be fully applied.