core-language.lagda.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. ..
  2. ::
  3. module language.core-language where
  4. .. _core-language:
  5. *************
  6. Core language
  7. *************
  8. A program in Agda consists of a number of declarations written in an ``*.agda``
  9. file. A declaration introduces a new identifier and gives its type and
  10. definition. It is possible to declare:
  11. * :ref:`datatypes <data-types>`
  12. * :ref:`record types <record-types>` (including
  13. :ref:`coinductive records <copatterns-coinductive-records>`)
  14. * :ref:`function definitions <function-definitions>`
  15. (including :ref:`mixfix operators <mixfix-operators>` and
  16. :ref:`abstract definitions <abstract-definitions>`)
  17. * :ref:`modules <module-basics>`
  18. * local definitions :ref:`let <let-expressions>` and
  19. :ref:`where <where-blocks>`
  20. * :ref:`postulates <postulates>`
  21. * :ref:`variables <generalization-of-declared-variables>`
  22. * :ref:`pattern-synonyms <pattern-synonyms>`
  23. * :ref:`precedence <precedence>` (fixity)
  24. * :ref:`pragmas <pragmas>`, and
  25. * :ref:`program options <command-line-options>`
  26. Declarations have a signature part and a definition part. These can appear
  27. separately in the program. Names must be declared before they are used, but
  28. by separating the signature from the definition it is possible to define things
  29. in :ref:`mutual recursion <mutual-recursion>`.
  30. Grammar
  31. -------
  32. At its core, Agda is a dependently typed lambda calculus. The grammar of
  33. terms where ``a`` represents a generic term is:
  34. .. code-block:: text
  35. a ::= x -- variable
  36. | λ x → a -- lambda abstraction
  37. | f -- defined function
  38. | (x : a) → a -- function space
  39. | F -- data/record type
  40. | c a -- data/record constructor
  41. | s -- sort Seti, Setω+i
  42. Syntax overview
  43. ---------------
  44. The syntax of an Agda program is defined in terms of three key components:
  45. * **Expressions** write function bodies and types.
  46. * **Declarations** declare types, data-types, postulates, records, functions etc.
  47. * **Pragmas** define program options.
  48. There are also three main levels of syntax, corresponding to different levels
  49. of interpretation:
  50. * **Concrete** is the high-level sugared syntax, it representing exactly what
  51. the user wrote (Agda.Syntax.Concrete).
  52. * **Abstract**, before typechecking (Agda.Syntax.Abstract)
  53. * **Internal**, the full-intepreted core Agda terms, typechecked; roughly
  54. corresponding to (Agda.Syntax.Internal).
  55. The process of translating an ``*.agda`` file into an executable has several
  56. stages:
  57. .. code-block:: text
  58. *.agda file
  59. ==[ parser (Lexer.x + Parser.y) ]==>
  60. Concrete syntax
  61. ==[ nicifier (Syntax.Concrete.Definitions) ]==>
  62. 'Nice' concrete syntax
  63. ==[ scope checking (Syntax.Translation.ConcreteToAbstract) ]==>
  64. Abstract syntax
  65. ==[ type checking (TypeChecking.Rules.*) ]==>
  66. Internal syntax
  67. ==[ Agda.Compiler.ToTreeless ]==>
  68. Treeless syntax
  69. ==[ different backends (Compiler.MAlonzo.*, Compiler.JS.*, ...) ]==>
  70. Source code
  71. ==[ different compilers (GHC compiler, ...) ]==>
  72. Executable
  73. The following sections describe these stages in more detail:
  74. Lexer
  75. -----
  76. .. _Alex: http://www.haskell.org/alex
  77. Lexical analysis (aka tokenization) is the process of converting a sequence of
  78. characters (the raw ``*.agda`` file) into a sequence of tokens (strings with a
  79. meaning).
  80. The lexer in Agda is generated by Alex_, and is an adaptation of GHC's lexer.
  81. The main lexing function ``lexer`` is called by the
  82. ``Agda.Syntax.Parser.Parser`` to get the next token from the input.
  83. Parser
  84. ------
  85. .. _Happy: http://www.haskell.org/happy
  86. The parser is the component that takes the output of the lexer and builds a
  87. data structure that we will call Concrete Syntax, while checking for correct
  88. syntax.
  89. The parser is generated by Happy_.
  90. Example: when a name is a sequence of parts, the lexer just sees it as a
  91. string, the parser does the translation in this step.
  92. Concrete Syntax
  93. ---------------
  94. The concrete syntax is a raw representation of the program text without any
  95. desugaring at all. This is what the parser produces. The idea is that if we
  96. figure out how to keep the concrete syntax around, it can be printed exactly
  97. as the user wrote it.
  98. Nice Concrete Syntax
  99. --------------------
  100. The ``Nice Concrete Syntax`` is a slightly reorganized version of the
  101. ``Concrete Syntax`` that is easier to deal with internally. Among other
  102. things, it:
  103. * detects mutual blocks
  104. * assembles :ref:`definitions <module-basics>` from their isolated parts
  105. * collects fixity information of :ref:`mixfix operators <mixfix-operators>`
  106. and attaches it to definitions
  107. * emits warnings for possibly unintended but still valid declarations, which
  108. essentially is dead code such as empty ``instance`` blocks and misplaced
  109. :ref:`pragmas <pragmas>`
  110. Abstract Syntax
  111. ---------------
  112. The translation from ``Agda.Syntax.Concrete`` to ``Agda.Syntax.Abstract``
  113. involves scope analysis, figuring out infix operator precedences and tidying
  114. up definitions.
  115. The abstract syntax ``Agda.Syntax.Abstract`` is the result after desugaring
  116. and scope analysis of the concrete syntax. The type checker works on abstract
  117. syntax, producing internal syntax.
  118. Internal Syntax
  119. ---------------
  120. This is the final stage of syntax before being handed off to one of the
  121. backends. Terms are well-scoped and well-typed.
  122. While producing the ``Internal Syntax``, terms are checked for safety. This
  123. safety check means :ref:`termination check <termination-checking>` and
  124. coverage check for functions, and :ref:`positivity check <positivity-checking>`
  125. for datatypes.
  126. Type-directed operations such as
  127. :ref:`instance resolution <instance-resolution>` and disambiguation of
  128. overloaded constructors (different constructors with the same name) also
  129. happen here.
  130. The internal syntax ``Agda.Syntax.Internal`` uses the following haskell
  131. datatype to represent the grammar of a ``Term`` presented above.
  132. .. code-block:: haskell
  133. data Term = Var {-# UNPACK #-} !Int Elims -- ^ @x es@ neutral
  134. | Lam ArgInfo (Abs Term) -- ^ Terms are beta normal. Relevance is ignored
  135. | Lit Literal
  136. | Def QName Elims -- ^ @f es@, possibly a delta/iota-redex
  137. | Con ConHead ConInfo Elims
  138. -- ^ @c es@ or @record { fs = es }@
  139. -- @es@ allows only Apply and IApply eliminations,
  140. -- and IApply only for data constructors.
  141. | Pi (Dom Type) (Abs Type) -- ^ dependent or non-dependent function space
  142. | Sort Sort
  143. | Level Level
  144. | MetaV {-# UNPACK #-} !MetaId Elims
  145. Treeless Syntax
  146. ---------------
  147. The treeless syntax is intended to be used as input for the
  148. :ref:`compiler backends <compiler-backends>`. It is more low-level than the
  149. internal syntax and is not used for type checking. Some of the features of
  150. the treeless syntax are:
  151. * case expressions instead of case trees
  152. * no instantiated datatypes / constructors
  153. For instance, the :ref:`Glasgow Haskell Compiler (GHC) backend <ghc-backend>`
  154. translates the treeless syntax into a proper GHC Haskell program.
  155. Another backend that may be used is the
  156. :ref:`JavaScript backend <javascript-backend>`, which translates the treeless
  157. syntax to JavaScript code.
  158. .. _a-normal-form:
  159. The treeless representation of the program has `A-normal form
  160. <https://en.wikipedia.org/wiki/A-normal_form>`_ (ANF). That means that all the
  161. case expressions are targeting a *single* variable, and all alternatives may
  162. only peel off one constructor.
  163. The backends can handle an ANF syntax easier than a syntax of a language where
  164. one may case arbitrary expressions and use
  165. :ref:`deep patterns <with-abstraction>`.