Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create "Getting started" tutorial. #6669

Merged
merged 1 commit into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 169 additions & 0 deletions Cabal/doc/getting-started.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
Getting Started with Haskell and Cabal
======================================

Installing the Haskell toolchain
--------------------------------

To install the Haskell toolchain follow the `ghcup instructions
<https://www.haskell.org/ghcup/>`__ if you're using Linux or Mac, or follow
`this guide <https://hub.zhox.com/posts/introducing-haskell-dev/>`__ if you're
using Windows.


Creating a new application
--------------------------

Let's start by creating a simple Haskell application from scratch where we'll
learn about a Haskell package's directory structure, how to run the executable,
and how to add external dependencies.


Initializing the application
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Start by creating a ``myfirstapp`` directory to hold the project files, these
instructions work in unix shells and PowerShell (if you're on Windows).

.. code-block:: console
$ mkdir myfirstapp
$ cd myfirstapp
Once you have an empty directory we can initialize our package:

.. code-block:: console
$ cabal init --cabal-version=2.4 --license=NONE -p myfirstapp
This will generate the following files:

.. code-block:: console
$ ls
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ls would work in powershell, in dos we have to use dir (the output will be different in any case but whatever)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like that if we ask windows users to use PowerShell than there're only small tweaks needed? casing for cmd would make it all too messy and hard to follow.

Copy link
Member

@jneira jneira Apr 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, powershell is fine nowadays imo, it is even the default console in windows 10 (and available in previous versions)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some words about it working on unix shells and PowerShell if you're a Windows user.

CHANGELOG.md
m-renaud marked this conversation as resolved.
Show resolved Hide resolved
Main.hs
myfirstapp.cabal
Setup.hs
``Main.hs`` is where your package's code lives. By default ``cabal init``
creates an executable with the same name as the package ``myfirstapp`` in this
case, you can instruct ``cabal init`` to generate just a library (with
``--lib``) or both a library and executable with (``--libandexe``); for the full
set of options see ``cabal init --help``.

``myfirstapp.cabal`` is Cabal's metadata file which describes your package and
its dependencies. We'll be updating this file in a little bit when we add an
external dependency to our package.


Running the application
^^^^^^^^^^^^^^^^^^^^^^^

As mentioned above, ``cabal init`` with no arguments generates a package with a
single executable that prints ``"Hello, Haskell!"`` to the terminal. To run the
executable enter the following command:

``cabal run :myfirstapp``
m-renaud marked this conversation as resolved.
Show resolved Hide resolved

You should see the following output in the terminal:

.. code-block:: console
$ cabal run :myfirstapp
...
Hello, Haskell!
.. note:: The ``:`` prefix in ``:myfirstapp`` signifies that the the
``myfirstapp`` target is part of the current package.

Notice that we didn't need to run a `build` command before ``cabal run``, this
is because ``cabal run`` first determines if the code needs to be re-built
before running the executable. If you just want to build a target you can do so
with ``cabal build``:

``cabal build :myfirstapp``


Adding dependencies
^^^^^^^^^^^^^^^^^^^

Next we'll add an external dependency to our application. `Hackage
<https://hackage.haskell.org/>`__ is the Haskell community's central `package`
archive of open source software.

In our application, we'll use a package called `haskell-say
<https://hackage.haskell.org/package/haskell-say>`__ to print text to the
m-renaud marked this conversation as resolved.
Show resolved Hide resolved
terminal with some embellishment.

.. TIP::
If you installed ``cabal`` a while ago but haven't used it recently you may
need to update the package index, you can do this by running ``cabal
update``.

In our ``myfirstapp.cabal`` file we'll update the ``build-depends`` attribute of
the ``executable myfirstapp`` section to include ``haskell-say``:

.. code-block:: cabal
executable myfirstapp
main-is: Main.hs
build-depends:
base >=4.11 && <4.12,
haskell-say ^>=1.0.0.0
.. NOTE::
``^>=1.0.0.0`` means use version 1.0.0.0 of the library or any more recent
minor release with the same major version.

Next we'll update ``Main.hs`` to use the ``HaskellSay`` library:

.. code-block:: haskell
module Main where
import HaskellSay (haskellSay)
main :: IO ()
main =
haskellSay "Hello, Haskell! You're using a function from another package!"
``import HaskellSay (haskellSay)`` brings the ``haskellSay`` function from the
module named ``HaskellSay`` into scope. The ``HaskellSay`` module is defined in
the ``haskell-say`` packages that we added a dependency on above.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to your previous comment. I think it would be actually good to find some library which has few transitive dependencies.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that in a (yet to be written) walkthrough on working with multiple packages in a project is where we could pull in more deps, wanted to keep this as simple as possible.

Copy link
Collaborator

@phadej phadej Apr 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not about multiple packages. It's about showing that cabal-install does work for you. You don't have to worry (as a beginner) if a dependency has own dependencies, it just works :)

I'm irritated by recent article where is recommended to shave your dependencies to the minimum. No, use your own common sense.


Now you can build and re-run your code to see the new output:

.. code-block:: console
$ cabal run
________________________________________________________
phadej marked this conversation as resolved.
Show resolved Hide resolved
/ \
| Hello, Haskell! You're using a function from another |
| package! |
\____ _____________________________________________/
\ /
\ /
\/
_____ _____
\ \ \ \
\ \ \ \
\ \ \ \
\ \ \ \ \-----------|
\ \ \ \ \ |
\ \ \ \ \---------|
/ / / \
/ / / \ \-------|
/ / / ^ \ \ |
/ / / / \ \ \ ----|
/ / / / \ \
/____/ /____/ \____\
phadej marked this conversation as resolved.
Show resolved Hide resolved
What Next?
----------

Now that you know how to set up a simple Haskell package using Cabal, check out
some of thee resources on the Haskell website's `documentation page
m-renaud marked this conversation as resolved.
Show resolved Hide resolved
<https://www.haskell.org/documentation/>`__ or read more about packages and
Cabal on the `introduction <intro.html>`__ page.
1 change: 1 addition & 0 deletions Cabal/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Welcome to the Cabal User Guide
:maxdepth: 2
:numbered:

getting-started
intro
config-and-install
concepts-and-development
Expand Down