built-ins.lagda.rst 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. ..
  2. ::
  3. {-# OPTIONS --rewriting --sized-types #-}
  4. module language.built-ins where
  5. open import Agda.Builtin.Equality public
  6. open import Agda.Primitive
  7. postulate String : Set
  8. {-# BUILTIN STRING String #-}
  9. data ⊥ : Set where
  10. record _×_ (A B : Set) : Set where
  11. constructor _,_
  12. field proj₁ : A
  13. proj₂ : B
  14. open _×_ public
  15. .. _built-ins:
  16. *********
  17. Built-ins
  18. *********
  19. .. contents::
  20. :depth: 1
  21. :local:
  22. The Agda type checker knows about, and has special treatment for, a number of
  23. different concepts. The most prominent is natural numbers, which has a special
  24. representation as Haskell integers and support for fast arithmetic. The surface
  25. syntax of these concepts are not fixed, however, so in order to use the special
  26. treatment of natural numbers (say) you define an appropriate data type and then
  27. bind that type to the natural number concept using a ``BUILTIN`` pragma.
  28. Some built-in types support primitive functions that have no corresponding Agda
  29. definition. These functions are declared using the ``primitive`` keyword by
  30. giving their type signature.
  31. Using the built-in types
  32. ------------------------
  33. While it is possible to define your own versions of the built-in types and bind
  34. them using ``BUILTIN`` pragmas, it is recommended to use the definitions in the
  35. ``Agda.Builtin`` modules. These modules are installed when you install Agda and
  36. so are always available. For instance, built-in natural numbers are defined in
  37. ``Agda.Builtin.Nat``. The `standard library <std-lib_>`_ and the agda-prelude_
  38. reexport the definitions from these modules.
  39. .. _agda-prelude: https://github.com/UlfNorell/agda-prelude
  40. .. _std-lib: https://github.com/agda/agda-stdlib
  41. .. _built-in-unit:
  42. The unit type
  43. -------------
  44. .. code-block:: agda
  45. module Agda.Builtin.Unit
  46. The unit type is bound to the built-in ``UNIT`` as follows::
  47. record ⊤ : Set where
  48. {-# BUILTIN UNIT ⊤ #-}
  49. Agda needs to know about the unit type since some of the primitive operations
  50. in the :ref:`reflected type checking monad <reflection-tc-monad>` return values
  51. in the unit type.
  52. .. _built-in-sigma:
  53. The Σ-type
  54. ----------
  55. .. code-block:: agda
  56. module Agda.Builtin.Sigma
  57. The built-in ``Σ``-type of dependent pairs is defined as follows::
  58. record Σ {a b} (A : Set a) (B : A → Set b) : Set (a ⊔ b) where
  59. constructor _,_
  60. field
  61. fst : A
  62. snd : B fst
  63. open Σ public
  64. infixr 4 _,_
  65. {-# BUILTIN SIGMA Σ #-}
  66. .. _built-in-list:
  67. Lists
  68. -----
  69. .. code-block:: agda
  70. module Agda.Builtin.List
  71. Built-in lists are bound using the ``LIST`` built-in::
  72. data List {a} (A : Set a) : Set a where
  73. [] : List A
  74. _∷_ : (x : A) (xs : List A) → List A
  75. {-# BUILTIN LIST List #-}
  76. infixr 5 _∷_
  77. The constructors are bound automatically when binding the type. Lists are not
  78. required to be level polymorphic; ``List : Set → Set`` is also accepted.
  79. As with booleans, the effect of binding the ``LIST`` built-in is to let
  80. you use primitive functions working with lists, such as ``primStringToList``
  81. and ``primStringFromList``, and letting the :ref:`GHC backend <ghc-backend>`
  82. know to compile the List type to Haskell lists.
  83. ..
  84. ::
  85. -- common functions on lists used in other files for examples
  86. _++_ : ∀ {a} {A : Set a} → List A → List A → List A
  87. [] ++ ys = ys
  88. (x ∷ xs) ++ ys = x ∷ (xs ++ ys)
  89. map : ∀ {a b} {A : Set a} {B : Set b} → (A → B) → List A → List B
  90. map f [] = []
  91. map f (x ∷ xs) = f x ∷ map f xs
  92. [_] : ∀ {a} {A : Set a} → A → List A
  93. [ x ] = x ∷ []
  94. .. _built-in-maybe:
  95. Maybe
  96. -----
  97. .. code-block:: agda
  98. module Agda.Builtin.Maybe
  99. Built-in maybe type is bound using the ``MAYBE`` built-in::
  100. data Maybe {a} (A : Set a) : Set a where
  101. nothing : Maybe A
  102. just : A → Maybe A
  103. {-# BUILTIN MAYBE Maybe #-}
  104. The constructors are bound automatically when binding the type. Maybe is not
  105. required to be level polymorphic; ``Maybe : Set → Set`` is also accepted.
  106. As with list, the effect of binding the ``MAYBE`` built-in is to let
  107. you use primitive functions working with maybes, such as ``primStringUncons``
  108. that returns the head and tail of a string (if it is non empty), and letting
  109. the :ref:`GHC backend <ghc-backend>` know to compile the Maybe type to Haskell
  110. maybes.
  111. .. _built-in-bool:
  112. Booleans
  113. --------
  114. .. code-block:: agda
  115. module Agda.Builtin.Bool where
  116. Built-in booleans are bound using the ``BOOL``, ``TRUE`` and ``FALSE`` built-ins::
  117. data Bool : Set where
  118. false true : Bool
  119. {-# BUILTIN BOOL Bool #-}
  120. {-# BUILTIN TRUE true #-}
  121. {-# BUILTIN FALSE false #-}
  122. Note that unlike for natural numbers, you need to bind the constructors
  123. separately. The reason for this is that Agda cannot tell which constructor
  124. should correspond to true and which to false, since you are free to name them
  125. whatever you like.
  126. The effect of binding the boolean type is that you can then use primitive
  127. functions returning booleans, such as built-in ``NATEQUALS``, and letting the
  128. :ref:`GHC backend <ghc-backend>` know to compile the type to Haskell `Bool`.
  129. ..
  130. ::
  131. infixl 1 if_then_else_
  132. if_then_else_ : {A : Set} → Bool → A → A → A
  133. if true then x else _ = x
  134. if false then _ else y = y
  135. .. _built-in-nat:
  136. Natural numbers
  137. ---------------
  138. .. code-block:: agda
  139. module Agda.Builtin.Nat
  140. Built-in natural numbers are bound using the ``NATURAL`` built-in as follows::
  141. data Nat : Set where
  142. zero : Nat
  143. suc : Nat → Nat
  144. {-# BUILTIN NATURAL Nat #-}
  145. The names of the data type and the constructors can be chosen freely, but the
  146. shape of the datatype needs to match the one given above (modulo the order of
  147. the constructors). Note that the constructors need not be bound explicitly.
  148. Binding the built-in natural numbers as above has the following effects:
  149. - The use of :ref:`natural number literals <lexical-structure-int-literals>` is
  150. enabled. By default the type of a natural number literal will be ``Nat``, but
  151. it can be :ref:`overloaded <literal-overloading>` to include other types as
  152. well.
  153. - Closed natural numbers are represented as Haskell integers at compile-time.
  154. - The compiler backends :ref:`compile natural numbers <compile-nat>` to the
  155. appropriate number type in the target language.
  156. - Enabled binding the built-in natural number functions described below.
  157. Functions on natural numbers
  158. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  159. There are a number of built-in functions on natural numbers. These are special
  160. in that they have both an Agda definition and a primitive implementation. The
  161. primitive implementation is used to evaluate applications to closed terms, and
  162. the Agda definition is used otherwise. This lets you prove things about the
  163. functions while still enjoying good performance of compile-time evaluation. The
  164. built-in functions are the following::
  165. _+_ : Nat → Nat → Nat
  166. zero + m = m
  167. suc n + m = suc (n + m)
  168. {-# BUILTIN NATPLUS _+_ #-}
  169. _-_ : Nat → Nat → Nat
  170. n - zero = n
  171. zero - suc m = zero
  172. suc n - suc m = n - m
  173. {-# BUILTIN NATMINUS _-_ #-}
  174. _*_ : Nat → Nat → Nat
  175. zero * m = zero
  176. suc n * m = (n * m) + m
  177. {-# BUILTIN NATTIMES _*_ #-}
  178. _==_ : Nat → Nat → Bool
  179. zero == zero = true
  180. suc n == suc m = n == m
  181. _ == _ = false
  182. {-# BUILTIN NATEQUALS _==_ #-}
  183. _<_ : Nat → Nat → Bool
  184. _ < zero = false
  185. zero < suc _ = true
  186. suc n < suc m = n < m
  187. {-# BUILTIN NATLESS _<_ #-}
  188. div-helper : Nat → Nat → Nat → Nat → Nat
  189. div-helper k m zero j = k
  190. div-helper k m (suc n) zero = div-helper (suc k) m n m
  191. div-helper k m (suc n) (suc j) = div-helper k m n j
  192. {-# BUILTIN NATDIVSUCAUX div-helper #-}
  193. mod-helper : Nat → Nat → Nat → Nat → Nat
  194. mod-helper k m zero j = k
  195. mod-helper k m (suc n) zero = mod-helper 0 m n m
  196. mod-helper k m (suc n) (suc j) = mod-helper (suc k) m n j
  197. {-# BUILTIN NATMODSUCAUX mod-helper #-}
  198. The Agda definitions are checked to make sure that they really define the
  199. corresponding built-in function. The definitions are not required to be exactly
  200. those given above, for instance, addition and multiplication can be defined by
  201. recursion on either argument, and you can swap the arguments to the addition in
  202. the recursive case of multiplication.
  203. The ``NATDIVSUCAUX`` and ``NATMODSUCAUX`` are built-ins bind helper functions
  204. for defining natural number division and modulo operations, and satisfy the
  205. properties
  206. .. code-block:: agda
  207. div n (suc m) ≡ div-helper 0 m n m
  208. mod n (suc m) ≡ mod-helper 0 m n m
  209. .. _built-in-integer:
  210. Machine words
  211. -------------
  212. .. code-block:: agda
  213. module Agda.Builtin.Word
  214. module Agda.Builtin.Word.Properties
  215. Agda supports built-in 64-bit machine words, bound with the ``WORD64`` built-in::
  216. postulate Word64 : Set
  217. {-# BUILTIN WORD64 Word64 #-}
  218. Machine words can be converted to and from natural numbers using the following primitives::
  219. primitive
  220. primWord64ToNat : Word64 → Nat
  221. primWord64FromNat : Nat → Word64
  222. Converting to a natural number is the trivial embedding, and converting from a natural number
  223. gives you the remainder modulo :math:`2^{64}`. The proof of the former theorem::
  224. primitive
  225. primWord64ToNatInjective : ∀ a b → primWord64ToNat a ≡ primWord64ToNat b → a ≡ b
  226. is in the ``Properties`` module. The proof of the latter theorem is not primitive,
  227. but can be defined in a library using :ref:`primTrustMe`.
  228. Basic arithmetic operations can be defined on ``Word64`` by converting to
  229. natural numbers, performing the corresponding operation, and then converting
  230. back. The compiler will optimise these to use 64-bit arithmetic. For
  231. instance::
  232. addWord : Word64 → Word64 → Word64
  233. addWord a b = primWord64FromNat (primWord64ToNat a + primWord64ToNat b)
  234. subWord : Word64 → Word64 → Word64
  235. subWord a b = primWord64FromNat ((primWord64ToNat a + 18446744073709551616) - primWord64ToNat b)
  236. These compile to primitive addition and subtraction on 64-bit words, which in the
  237. :ref:`GHC backend<ghc-backend>` map to operations on Haskell 64-bit words
  238. (``Data.Word.Word64``).
  239. Integers
  240. --------
  241. .. code-block:: agda
  242. module Agda.Builtin.Int
  243. Built-in integers are bound with the ``INTEGER`` built-in to a data type with
  244. two constructors: one for positive and one for negative numbers. The built-ins
  245. for the constructors are ``INTEGERPOS`` and ``INTEGERNEGSUC``.
  246. ::
  247. data Int : Set where
  248. pos : Nat → Int
  249. negsuc : Nat → Int
  250. {-# BUILTIN INTEGER Int #-}
  251. {-# BUILTIN INTEGERPOS pos #-}
  252. {-# BUILTIN INTEGERNEGSUC negsuc #-}
  253. Here ``negsuc n`` represents the integer ``-n - 1``. Unlike for natural
  254. numbers, there is no special representation of integers at compile-time since
  255. the overhead of using the data type compared to Haskell integers is not that
  256. big.
  257. Built-in integers support the following primitive operation (given a
  258. suitable binding for :ref:`String <built-in-string>`)::
  259. primitive
  260. primShowInteger : Int → String
  261. .. _built-in-float:
  262. Floats
  263. ------
  264. .. code-block:: agda
  265. module Agda.Builtin.Float
  266. module Agda.Builtin.Float.Properties
  267. Floating point numbers are bound with the ``FLOAT`` built-in::
  268. postulate Float : Set
  269. {-# BUILTIN FLOAT Float #-}
  270. This lets you use :ref:`floating point literals
  271. <lexical-structure-float-literals>`. Floats are represented by the
  272. type checker as IEEE 754 binary64 double precision floats, with the
  273. restriction that there is exactly one NaN value. The following
  274. primitive functions are available (with suitable bindings for
  275. :ref:`Nat <built-in-nat>`, :ref:`Bool <built-in-bool>`,
  276. :ref:`String <built-in-string>`, :ref:`Int <built-in-integer>`,
  277. :ref:`Maybe_<built-in-maybe>`)::
  278. primitive
  279. -- Relations
  280. primFloatIsInfinite : Float → Bool
  281. primFloatIsNaN : Float → Bool
  282. primFloatIsNegativeZero : Float → Bool
  283. -- Conversions
  284. primNatToFloat : Nat → Float
  285. primIntToFloat : Int → Float
  286. primFloatToRatio : Float → (Σ Int λ _ → Int)
  287. primRatioToFloat : Int → Int → Float
  288. primShowFloat : Float → String
  289. -- Operations
  290. primFloatPlus : Float → Float → Float
  291. primFloatMinus : Float → Float → Float
  292. primFloatTimes : Float → Float → Float
  293. primFloatDiv : Float → Float → Float
  294. primFloatPow : Float → Float → Float
  295. primFloatNegate : Float → Float
  296. primFloatSqrt : Float → Float
  297. primFloatExp : Float → Float
  298. primFloatLog : Float → Float
  299. primFloatSin : Float → Float
  300. primFloatCos : Float → Float
  301. primFloatTan : Float → Float
  302. primFloatASin : Float → Float
  303. primFloatACos : Float → Float
  304. primFloatATan : Float → Float
  305. primFloatATan2 : Float → Float → Float
  306. primFloatSinh : Float → Float
  307. primFloatCosh : Float → Float
  308. primFloatTanh : Float → Float
  309. primFloatASinh : Float → Float
  310. primFloatACosh : Float → Float
  311. primFloatATanh : Float → Float
  312. ..
  313. ::
  314. private
  315. NaN : Float
  316. NaN = primFloatDiv 0.0 0.0
  317. Inf : Float
  318. Inf = primFloatDiv 1.0 0.0
  319. -Inf : Float
  320. -Inf = primFloatNegate Inf
  321. _&&_ : Bool → Bool → Bool
  322. false && _ = false
  323. true && x = x
  324. not : Bool → Bool
  325. not false = true
  326. not true = false
  327. The primitive binary relations implement their IEEE 754 equivalents, which means
  328. that ``primFloatEquality`` is not reflexive, and ``primFloatInequality`` and
  329. ``primFloatLess`` are not total. (Specifically, NaN is not related to anything,
  330. including itself.)
  331. The ``primFloatIsSafeInteger`` function determines whether the value is a number
  332. that is a safe integer, i.e., is within the range where the arithmetic
  333. operations do not lose precision.
  334. Floating point numbers can be converted to their raw representation using the primitive::
  335. primitive
  336. primFloatToWord64 : Float → Word64
  337. which normalises all ``NaN`` to a canonical ``NaN`` with an injectivity proof::
  338. primFloatToWord64Injective : ∀ a b → primFloatToWord64 a ≡ primFloatToWord64 b → a ≡ b
  339. in the ``Properties`` module. These primitives can be used to define a safe
  340. decidable propositional equality with the :option:`--safe` option. The function
  341. ``primFloatToWord64`` cannot be guaranteed to be consistent across backends,
  342. therefore relying on the specific result may result in inconsistencies.
  343. The rounding operations (``primFloatRound``, ``primFloatFloor``, and
  344. ``primFloatCeiling``) return a value of type ``Maybe Int``, and return ``nothing``
  345. when applied to NaN or the infinities::
  346. primitive
  347. primFloatRound : Float → Maybe Int
  348. primFloatFloor : Float → Maybe Int
  349. primFloatCeiling : Float → Maybe Int
  350. The ``primFloatDecode`` function decodes a floating-point number to its mantissa
  351. and exponent, normalised such that the mantissa is the smallest possible
  352. integer. It fails when applied to NaN or the infinities, returning ``nothing``.
  353. The ``primFloatEncode`` function encodes a pair of a mantissa and exponent to a
  354. floating-point number. It fails when the resulting number cannot be represented
  355. as a float. Note that ``primFloatEncode`` may result in a loss of precision.
  356. primitive
  357. primFloatDecode : Float → Maybe (Σ Int λ _ → Int)
  358. primFloatEncode : Int → Int → Maybe Float
  359. .. _built-in-char:
  360. Characters
  361. ----------
  362. .. code-block:: agda
  363. module Agda.Builtin.Char
  364. module Agda.Builtin.Char.Properties
  365. The character type is bound with the ``CHARACTER`` built-in::
  366. postulate Char : Set
  367. {-# BUILTIN CHAR Char #-}
  368. Binding the character type lets you use :ref:`character literals
  369. <lexical-structure-char-literals>`. The following primitive functions
  370. are available on characters (given suitable bindings for
  371. :ref:`Bool <built-in-bool>`, :ref:`Nat <built-in-nat>` and
  372. :ref:`String <built-in-string>`)::
  373. primitive
  374. primIsLower : Char → Bool
  375. primIsDigit : Char → Bool
  376. primIsAlpha : Char → Bool
  377. primIsSpace : Char → Bool
  378. primIsAscii : Char → Bool
  379. primIsLatin1 : Char → Bool
  380. primIsPrint : Char → Bool
  381. primIsHexDigit : Char → Bool
  382. primToUpper : Char → Char
  383. primToLower : Char → Char
  384. primCharToNat : Char → Nat
  385. primNatToChar : Nat → Char
  386. primShowChar : Char → String
  387. These functions are implemented by the corresponding Haskell functions from
  388. `Data.Char <data-char_>`_ (``ord`` and ``chr`` for ``primCharToNat`` and
  389. ``primNatToChar``). To make ``primNatToChar`` total ``chr`` is applied to the
  390. natural number modulo ``0x110000``. Furthermore, to match the behaviour of
  391. strings, `surrogate code points <surrogate_>`_ are mapped to the replacement
  392. character ``U+FFFD``.
  393. Converting to a natural number is the obvious embedding, and its proof::
  394. primitive
  395. primCharToNatInjective : ∀ a b → primCharToNat a ≡ primCharToNat b → a ≡ b
  396. can be found in the ``Properties`` module.
  397. .. _data-char: https://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Char.html
  398. .. _surrogate: https://www.unicode.org/glossary/#surrogate_code_point
  399. .. _built-in-string:
  400. Strings
  401. -------
  402. .. code-block:: agda
  403. module Agda.Builtin.String
  404. module Agda.Builtin.String.Properties
  405. The string type is bound with the ``STRING`` built-in:
  406. .. code-block:: agda
  407. postulate String : Set
  408. {-# BUILTIN STRING String #-}
  409. Binding the string type lets you use :ref:`string literals
  410. <lexical-structure-string-literals>`. The following primitive
  411. functions are available on strings (given suitable bindings for
  412. :ref:`Bool <built-in-bool>`, :ref:`Char <built-in-char>` and
  413. :ref:`List <built-in-list>`)::
  414. primitive
  415. primStringUncons : String → Maybe (Σ Char (λ _ → String))
  416. primStringToList : String → List Char
  417. primStringFromList : List Char → String
  418. primStringAppend : String → String → String
  419. primStringEquality : String → String → Bool
  420. primShowString : String → String
  421. String literals can be :ref:`overloaded <overloaded-strings>`.
  422. Converting to and from a list is injective, and their proofs::
  423. primitive
  424. primStringToListInjective : ∀ a b → primStringToList a ≡ primStringToList b → a ≡ b
  425. primStringFromListInjective : ∀ a b → primStringFromList a ≡ primStringFromList b → a ≡ b
  426. can found in the ``Properties`` module.
  427. Strings cannot represent `unicode surrogate code points <surrogate_>`_
  428. (characters in the range ``U+D800`` to ``U+DFFF``). These are replaced by the
  429. unicode replacement character ``U+FFFD`` if they appear in string literals.
  430. .. _built-in-equality:
  431. Equality
  432. --------
  433. .. code-block:: agda
  434. module Agda.Builtin.Equality
  435. The identity type can be bound to the built-in ``EQUALITY`` as follows
  436. .. code-block:: agda
  437. infix 4 _≡_
  438. data _≡_ {a} {A : Set a} (x : A) : A → Set a where
  439. refl : x ≡ x
  440. {-# BUILTIN EQUALITY _≡_ #-}
  441. This lets you use proofs of type ``lhs ≡ rhs`` in the :ref:`rewrite
  442. construction <with-rewrite>`.
  443. Other variants of the identity type are also accepted as built-in:
  444. .. code-block:: agda
  445. data _≡_ {A : Set} : (x y : A) → Set where
  446. refl : (x : A) → x ≡ x
  447. The type of ``primEraseEquality`` has to match the flavor of identity type.
  448. .. _primEraseEquality:
  449. .. code-block:: agda
  450. module Agda.Builtin.Equality.Erase
  451. Binding the built-in equality type also enables the ``primEraseEquality`` primitive::
  452. primitive
  453. primEraseEquality : ∀ {a} {A : Set a} {x y : A} → x ≡ y → x ≡ y
  454. The function takes a proof of an equality between two values ``x`` and ``y`` and stays
  455. stuck on it until ``x`` and ``y`` actually become definitionally equal. Whenever that
  456. is the case, ``primEraseEquality e`` reduces to ``refl``.
  457. One use of ``primEraseEquality`` is to replace an equality proof computed using an expensive
  458. function (e.g. a proof by reflection) by one which is trivially ``refl`` on the diagonal.
  459. .. _primtrustme:
  460. primTrustMe
  461. ~~~~~~~~~~~
  462. .. code-block:: agda
  463. module Agda.Builtin.TrustMe
  464. From the ``primEraseEquality`` primitive, we can derive a notion of ``primTrustMe``::
  465. primTrustMe : ∀ {a} {A : Set a} {x y : A} → x ≡ y
  466. primTrustMe {x = x} {y} = primEraseEquality unsafePrimTrustMe
  467. where postulate unsafePrimTrustMe : x ≡ y
  468. As can be seen from the type, ``primTrustMe`` must be used with the
  469. utmost care to avoid inconsistencies. What makes it different from a
  470. postulate is that if ``x`` and ``y`` are actually definitionally
  471. equal, ``primTrustMe`` reduces to ``refl``. One use of ``primTrustMe``
  472. is to lift the primitive boolean equality on built-in types like
  473. :ref:`String <built-in-string>` to something that returns a proof
  474. object::
  475. eqString : (a b : String) → Maybe (a ≡ b)
  476. eqString a b = if primStringEquality a b
  477. then just primTrustMe
  478. else nothing
  479. With this definition ``eqString "foo" "foo"`` computes to ``just refl``.
  480. Sorts
  481. -----
  482. The primitive sorts used in Agda's type system (`Set`, `Prop`, and
  483. `Setω`) are declared using ``BUILTIN`` pragmas in the
  484. ``Agda.Primitive`` module. These pragmas should not be used directly
  485. in other modules, but it is possible to rename these builtin sorts
  486. when importing ``Agda.Primitive``.
  487. ..
  488. This code cannot be typechecked because the identifiers are already bound
  489. in Agda.Primitive and are auto-imported.
  490. .. code-block:: agda
  491. {-# BUILTIN TYPE Set #-}
  492. {-# BUILTIN PROP Prop #-}
  493. {-# BUILTIN SETOMEGA Setω #-}
  494. The primitive sorts `Set` and `Prop` are automatically imported at the
  495. top of every top-level Agda module, unless the
  496. ``--no-import-sorts`` flag is enabled.
  497. Universe levels
  498. ---------------
  499. .. code-block:: agda
  500. module Agda.Primitive
  501. :ref:`Universe levels <universe-levels>` are also declared using ``BUILTIN``
  502. pragmas. In contrast to the ``Agda.Builtin`` modules, the ``Agda.Primitive`` module
  503. is auto-imported and thus it is not possible to change the level built-ins. For
  504. reference these are the bindings:
  505. ..
  506. This code cannot be typechecked because the identifiers are already bound
  507. in Agda.Primitive and are auto-imported.
  508. .. code-block:: agda
  509. postulate
  510. Level : Set
  511. lzero : Level
  512. lsuc : Level → Level
  513. _⊔_ : Level → Level → Level
  514. {-# BUILTIN LEVEL Level #-}
  515. {-# BUILTIN LEVELZERO lzero #-}
  516. {-# BUILTIN LEVELSUC lsuc #-}
  517. {-# BUILTIN LEVELMAX _⊔_ #-}
  518. .. _builtin_sized_types:
  519. Sized types
  520. -----------
  521. ..
  522. ::
  523. module Size where
  524. .. code-block:: agda
  525. module Agda.Builtin.Size
  526. The built-ins for :ref:`sized types <sized-types>` are different from other
  527. built-ins in that the names are defined by the ``BUILTIN`` pragma. Hence, to
  528. bind the size primitives it is enough to write::
  529. {-# BUILTIN SIZEUNIV SizeUniv #-} -- SizeUniv : SizeUniv
  530. {-# BUILTIN SIZE Size #-} -- Size : SizeUniv
  531. {-# BUILTIN SIZELT Size<_ #-} -- Size<_ : ..Size → SizeUniv
  532. {-# BUILTIN SIZESUC ↑_ #-} -- ↑_ : Size → Size
  533. {-# BUILTIN SIZEINF ∞ #-} -- ∞ : Size
  534. {-# BUILTIN SIZEMAX _⊔ˢ_ #-} -- _⊔ˢ_ : Size → Size → Size
  535. Coinduction
  536. -----------
  537. ..
  538. ::
  539. module Coinduction where
  540. .. code-block:: agda
  541. module Agda.Builtin.Coinduction
  542. The following built-ins are used for coinductive definitions::
  543. postulate
  544. ∞ : ∀ {a} (A : Set a) → Set a
  545. ♯_ : ∀ {a} {A : Set a} → A → ∞ A
  546. ♭ : ∀ {a} {A : Set a} → ∞ A → A
  547. {-# BUILTIN INFINITY ∞ #-}
  548. {-# BUILTIN SHARP ♯_ #-}
  549. {-# BUILTIN FLAT ♭ #-}
  550. See :ref:`coinduction` for more information.
  551. IO
  552. --
  553. .. code-block:: agda
  554. module Agda.Builtin.IO
  555. The sole purpose of binding the built-in ``IO`` type is to let Agda check that
  556. the ``main`` function has the right type (see :ref:`compilers`).
  557. ::
  558. postulate IO : Set → Set
  559. {-# BUILTIN IO IO #-}
  560. Literal overloading
  561. -------------------
  562. .. code-block:: agda
  563. module Agda.Builtin.FromNat
  564. module Agda.Builtin.FromNeg
  565. module Agda.Builtin.FromString
  566. The machinery for :ref:`overloading literals <literal-overloading>` uses
  567. built-ins for the conversion functions.
  568. Reflection
  569. ----------
  570. .. code-block:: agda
  571. module Agda.Builtin.Reflection
  572. The reflection machinery has built-in types for representing Agda programs. See
  573. :doc:`reflection` for a detailed description.
  574. .. _builtin-rewrite:
  575. Rewriting
  576. ---------
  577. The experimental and totally unsafe :doc:`rewriting machinery <rewriting>` (not
  578. to be confused with the :ref:`rewrite construct <with-rewrite>`) has a built-in
  579. ``REWRITE`` for the rewriting relation::
  580. postulate _↦_ : ∀ {a} {A : Set a} → A → A → Set a
  581. {-# BUILTIN REWRITE _↦_ #-}
  582. This builtin is bound to the :ref:`builtin equality type
  583. <built-in-equality>` from ``Agda.Builtin.Equality`` in
  584. ``Agda.Builtin.Equality.Rewrite``.
  585. Static values
  586. -------------
  587. The ``STATIC`` pragma can be used to mark definitions which should
  588. be normalised before compilation. The typical use case for this is
  589. to mark the interpreter of an embedded language as ``STATIC``:
  590. .. code-block:: agda
  591. {-# STATIC <Name> #-}
  592. Strictness
  593. ----------
  594. .. code-block:: agda
  595. module Agda.Builtin.Strict
  596. There are two primitives for controlling evaluation order::
  597. primitive
  598. primForce : ∀ {a b} {A : Set a} {B : A → Set b} (x : A) → (∀ x → B x) → B x
  599. primForceLemma : ∀ {a b} {A : Set a} {B : A → Set b} (x : A) (f : ∀ x → B x) → primForce x f ≡ f x
  600. where ``_≡_`` is the :ref:`built-in equality <built-in-equality>`. At compile-time
  601. ``primForce x f`` evaluates to ``f x`` when ``x`` is in weak head normal form (whnf),
  602. i.e. one of the following:
  603. - a constructor application
  604. - a literal
  605. - a lambda abstraction
  606. - a type constructor application (data or record type)
  607. - a function type
  608. - a universe (``Set _``)
  609. Similarly ``primForceLemma x f``, which lets you reason about programs using
  610. ``primForce``, evaluates to ``refl`` when ``x`` is in whnf. At run-time,
  611. ``primForce e f`` is compiled (by the GHC :ref:`backend <compilers>`)
  612. to ``let x = e in seq x (f x)``.
  613. For example, consider the following function::
  614. -- pow’ n a = a 2ⁿ
  615. pow’ : Nat → Nat → Nat
  616. pow’ zero a = a
  617. pow’ (suc n) a = pow’ n (a + a)
  618. There is a space leak here (both for compile-time and run-time evaluation),
  619. caused by unevaluated ``a + a`` thunks. This problem can be fixed with
  620. ``primForce``::
  621. infixr 0 _$!_
  622. _$!_ : ∀ {a b} {A : Set a} {B : A → Set b} → (∀ x → B x) → ∀ x → B x
  623. f $! x = primForce x f
  624. -- pow n a = a 2ⁿ
  625. pow : Nat → Nat → Nat
  626. pow zero a = a
  627. pow (suc n) a = pow n $! a + a