|
| 1 | +Getting Started with Haskell and Cabal |
| 2 | +====================================== |
| 3 | + |
| 4 | +Installing the Haskell toolchain |
| 5 | +-------------------------------- |
| 6 | + |
| 7 | +To install the Haskell toolchain follow the `ghcup instructions |
| 8 | +<https://www.haskell.org/ghcup/>`__ if you're using Linux or Mac, or follow |
| 9 | +`this guide <https://hub.zhox.com/posts/introducing-haskell-dev/>`__ if you're |
| 10 | +using Windows. |
| 11 | + |
| 12 | + |
| 13 | +Creating a new application |
| 14 | +-------------------------- |
| 15 | + |
| 16 | +Let's start by creating a simple Haskell application from scratch where we'll |
| 17 | +learn about a Haskell package's directory structure, how to run the executable, |
| 18 | +and how to add external dependencies. |
| 19 | + |
| 20 | + |
| 21 | +Initializing the application |
| 22 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 23 | + |
| 24 | +Start by creating a ``myfirstapp`` directory to hold the project files, these |
| 25 | +instructions work in unix shells and PowerShell (if you're on Windows). |
| 26 | + |
| 27 | +.. code-block:: console |
| 28 | +
|
| 29 | + $ mkdir myfirstapp |
| 30 | + $ cd myfirstapp |
| 31 | +
|
| 32 | +Once you have an empty directory we can initialize our package: |
| 33 | + |
| 34 | +.. code-block:: console |
| 35 | +
|
| 36 | + $ cabal init --cabal-version=2.4 --license=NONE -p myfirstapp |
| 37 | +
|
| 38 | +This will generate the following files: |
| 39 | + |
| 40 | +.. code-block:: console |
| 41 | +
|
| 42 | + $ ls |
| 43 | + CHANGELOG.md |
| 44 | + Main.hs |
| 45 | + myfirstapp.cabal |
| 46 | + Setup.hs |
| 47 | +
|
| 48 | +
|
| 49 | +``Main.hs`` is where your package's code lives. By default ``cabal init`` |
| 50 | +creates an executable with the same name as the package ``myfirstapp`` in this |
| 51 | +case, you can instruct ``cabal init`` to generate just a library (with |
| 52 | +``--lib``) or both a library and executable with (``--libandexe``); for the full |
| 53 | +set of options see ``cabal init --help``. |
| 54 | + |
| 55 | +``myfirstapp.cabal`` is Cabal's metadata file which describes your package and |
| 56 | +its dependencies. We'll be updating this file in a little bit when we add an |
| 57 | +external dependency to our package. |
| 58 | + |
| 59 | + |
| 60 | +Running the application |
| 61 | +^^^^^^^^^^^^^^^^^^^^^^^ |
| 62 | + |
| 63 | +As mentioned above, ``cabal init`` with no arguments generates a package with a |
| 64 | +single executable that prints ``"Hello, Haskell!"`` to the terminal. To run the |
| 65 | +executable enter the following command: |
| 66 | + |
| 67 | +``cabal run :myfirstapp`` |
| 68 | + |
| 69 | +You should see the following output in the terminal: |
| 70 | + |
| 71 | +.. code-block:: console |
| 72 | +
|
| 73 | + $ cabal run :myfirstapp |
| 74 | + ... |
| 75 | + Hello, Haskell! |
| 76 | +
|
| 77 | +.. note:: The ``:`` prefix in ``:myfirstapp`` signifies that the the |
| 78 | + ``myfirstapp`` target is part of the current package. |
| 79 | + |
| 80 | +Notice that we didn't need to run a `build` command before ``cabal run``, this |
| 81 | +is because ``cabal run`` first determines if the code needs to be re-built |
| 82 | +before running the executable. If you just want to build a target you can do so |
| 83 | +with ``cabal build``: |
| 84 | + |
| 85 | +``cabal build :myfirstapp`` |
| 86 | + |
| 87 | + |
| 88 | +Adding dependencies |
| 89 | +^^^^^^^^^^^^^^^^^^^ |
| 90 | + |
| 91 | +Next we'll add an external dependency to our application. `Hackage |
| 92 | +<https://hackage.haskell.org/>`__ is the Haskell community's central `package` |
| 93 | +archive of open source software. |
| 94 | + |
| 95 | +In our application, we'll use a package called `haskell-say |
| 96 | +<https://hackage.haskell.org/package/haskell-say>`__ to print text to the |
| 97 | +terminal with some embellishment. |
| 98 | + |
| 99 | +.. TIP:: |
| 100 | + If you installed ``cabal`` a while ago but haven't used it recently you may |
| 101 | + need to update the package index, you can do this by running ``cabal |
| 102 | + update``. |
| 103 | + |
| 104 | +In our ``myfirstapp.cabal`` file we'll update the ``build-depends`` attribute of |
| 105 | +the ``executable myfirstapp`` section to include ``haskell-say``: |
| 106 | + |
| 107 | +.. code-block:: cabal |
| 108 | +
|
| 109 | + executable myfirstapp |
| 110 | + main-is: Main.hs |
| 111 | + build-depends: |
| 112 | + base >=4.11 && <4.12, |
| 113 | + haskell-say ^>=1.0.0.0 |
| 114 | +
|
| 115 | +.. NOTE:: |
| 116 | + ``^>=1.0.0.0`` means use version 1.0.0.0 of the library or any more recent |
| 117 | + minor release with the same major version. |
| 118 | + |
| 119 | +Next we'll update ``Main.hs`` to use the ``HaskellSay`` library: |
| 120 | + |
| 121 | +.. code-block:: haskell |
| 122 | +
|
| 123 | + module Main where |
| 124 | +
|
| 125 | + import HaskellSay (haskellSay) |
| 126 | +
|
| 127 | + main :: IO () |
| 128 | + main = |
| 129 | + haskellSay "Hello, Haskell! You're using a function from another package!" |
| 130 | +
|
| 131 | +``import HaskellSay (haskellSay)`` brings the ``haskellSay`` function from the |
| 132 | +module named ``HaskellSay`` into scope. The ``HaskellSay`` module is defined in |
| 133 | +the ``haskell-say`` packages that we added a dependency on above. |
| 134 | + |
| 135 | +Now you can build and re-run your code to see the new output: |
| 136 | + |
| 137 | +.. code-block:: console |
| 138 | +
|
| 139 | + $ cabal run |
| 140 | + ________________________________________________________ |
| 141 | + / \ |
| 142 | + | Hello, Haskell! You're using a function from another | |
| 143 | + | package! | |
| 144 | + \____ _____________________________________________/ |
| 145 | + \ / |
| 146 | + \ / |
| 147 | + \/ |
| 148 | + _____ _____ |
| 149 | + \ \ \ \ |
| 150 | + \ \ \ \ |
| 151 | + \ \ \ \ |
| 152 | + \ \ \ \ \-----------| |
| 153 | + \ \ \ \ \ | |
| 154 | + \ \ \ \ \---------| |
| 155 | + / / / \ |
| 156 | + / / / \ \-------| |
| 157 | + / / / ^ \ \ | |
| 158 | + / / / / \ \ \ ----| |
| 159 | + / / / / \ \ |
| 160 | + /____/ /____/ \____\ |
| 161 | +
|
| 162 | +
|
| 163 | +What Next? |
| 164 | +---------- |
| 165 | + |
| 166 | +Now that you know how to set up a simple Haskell package using Cabal, check out |
| 167 | +some of thee resources on the Haskell website's `documentation page |
| 168 | +<https://www.haskell.org/documentation/>`__ or read more about packages and |
| 169 | +Cabal on the `introduction <intro.html>`__ page. |
0 commit comments