hello-world.lagda.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ..
  2. ::
  3. module getting-started.hello-world where
  4. .. _hello-world:
  5. *********************
  6. 'Hello world' in Agda
  7. *********************
  8. This section contains two minimal Agda programs that can be used to
  9. test if you have installed Agda correctly: one for using Agda
  10. interactively as a proof assistant, and one for compiling Agda
  11. programs to an executable binary. For a more in-depth introduction to
  12. using Agda, see :ref:`A taste of Agda <a-taste-of-agda>` or the
  13. :ref:`list of tutorials <tutorial-list>`.
  14. Hello, Agda!
  15. ============
  16. Below is is a small 'hello world' program in Agda (defined in a file
  17. ``hello.agda``).
  18. .. code-block:: agda
  19. data Greeting : Set where
  20. hello : Greeting
  21. greet : Greeting
  22. greet = hello
  23. This program defines a :ref:`data type <data-types>` called
  24. ``Greeting`` with one constructor ``hello``, and a :ref:`function
  25. definition <function-definitions>` ``greet`` of type ``Greeting`` that
  26. returns ``hello``.
  27. To load the Agda file, open it in Emacs and load it by pressing ``C-c
  28. C-l`` (``Ctrl+c`` followed by ``Ctrl+l``). You should now see that the
  29. code is highlighted and there should be a message ``*All done*``. If
  30. this is the case, congratulations! You have correctly installed Agda
  31. and the Agda mode for Emacs. If you also want to compile your Agda
  32. programs, continue with the next section.
  33. Hello, World!
  34. =============
  35. Below is a complete executable 'hello world' program in Agda (defined
  36. in a file ``hello-world.agda``)
  37. .. code-block:: agda
  38. module hello-world where
  39. open import Agda.Builtin.IO using (IO)
  40. open import Agda.Builtin.Unit using (⊤)
  41. open import Agda.Builtin.String using (String)
  42. postulate putStrLn : String → IO ⊤
  43. {-# FOREIGN GHC import qualified Data.Text as T #-}
  44. {-# COMPILE GHC putStrLn = putStrLn . T.unpack #-}
  45. main : IO ⊤
  46. main = putStrLn "Hello world!"
  47. This code is self-contained and has several declarations:
  48. 1. Imports of the ``ÌO``, ``⊤`` and ``String`` types from the Agda Builtin
  49. library.
  50. 2. A postulate of the function type ``putStrLn``.
  51. 3. Two :ref:`pragmas <pragmas>` that tell Agda how to compile the function ``putStrLn``.
  52. 4. A definition of the function ``main``.
  53. To compile the Agda file, either open it in Emacs and press ``C-c C-x
  54. C-c`` or run ``agda --compile hello-world.agda`` from the command
  55. line. This will create a binary ``hello-world`` in the current
  56. directory that prints ``Hello world!``. To find out more about the
  57. ``agda`` command, use ``agda --help``.
  58. .. note::
  59. As you can see from this example, by default Agda includes only
  60. minimal library support through the ``Builtin`` modules. The `Agda
  61. Standard Library <std-lib_>`_ provides bindings for most commonly
  62. used Haskell functions, including ``putStrLn``. For a version of
  63. this 'hello world' program that uses the standard library, see
  64. :ref:`building-an-executable-agda-program`.
  65. .. _std-lib: https://github.com/agda/agda-stdlib