Skip to content

Commit 61f7e3b

Browse files
committed
Improve Hello World example
1 parent 51748a6 commit 61f7e3b

File tree

2 files changed

+147
-51
lines changed

2 files changed

+147
-51
lines changed

doc/commands/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ title: Commands
77

88
Some of Stack's features will not be needed regularly or by all users. This part
99
of the guide and the part on [configuration](../configure/index.md) provide
10-
information about somethose features, organised as a reference guide. Some of
11-
the features are complex and separate pages are dedicated to them.
10+
information about some of those features, organised as a reference guide. Some
11+
of the features are complex and separate pages are dedicated to them.
1212

1313
## Stack commands (thematic)
1414

doc/tutorial/hello_world_example.md

Lines changed: 145 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,77 @@
1-
<div class="hidden-warning"><a href="https://docs.haskellstack.org/"><img src="https://cdn.jsdelivr.net/gh/commercialhaskell/stack/doc/img/hidden-warning.svg"></a></div>
1+
<div class="hidden-warning"><a href="https://docs.haskellstack.org/"><img src="https://cdn.jsdelivr.net/gh/commercialhaskell/stack/doc/img/hidden-warning.svg"></a></div>
22

3-
# 1. A Hello World Example
3+
# 1. A Hello World example
44

5-
With Stack installed, let's create a new project from a template and walk
6-
through the most common Stack commands.
5+
With Stack installed, let's create a new project and walk through the most
6+
common Stack commands.
77

8-
In this guide, an initial `$` represents the command line prompt. The prompt may
9-
differ in the terminal on your operating system. Unless stated otherwise, the
10-
working directory is the project's root directory.
8+
In this guide, unless stated otherwise, the working directory is the project's
9+
root directory.
1110

1211
## The `stack new` command
1312

14-
We'll start off with the `stack new` command to create a new *project*, that
15-
will contain a Haskell *package* of the same name. So let's pick a valid
16-
package name first:
13+
We'll start off with the [`stack new`](../commands/new_command.md) command to
14+
create a new *project* from a project template. We'll use the `new-template`
15+
project template. This template is used by default, but in our example we will
16+
refer to it expressly.
1717

18-
> A package is identified by a globally-unique package name, which consists
19-
> of one or more alphanumeric words separated by hyphens. To avoid ambiguity,
20-
> each of these words should contain at least one letter.
18+
That template will create a project with a package of the same name. So, we need
19+
to pick a name for the project that is a valid package name. We'll call our
20+
project `helloworld`.
2121

22-
(From the
23-
[Cabal users guide](https://www.haskell.org/cabal/users-guide/developing-packages.html#developing-packages))
22+
??? question "How do project packages relate to projects?"
2423

25-
We'll call our project `helloworld`, and we'll use the `new-template` project
26-
template. This template is used by default, but in our example we will refer to
27-
it expressly. Other templates are available. For further information about
28-
templates, see the `stack templates` command
29-
[documentation](../commands/templates_command.md).
24+
A project can have one or more packages. Each project package has its own
25+
root directory. In the case of a single-package project, the project
26+
directory and the package directory can be the same directory.
27+
28+
??? question "What is a valid package name?"
29+
30+
A valid package name consists of one or more alphanumeric words separated by
31+
hyphens. Each word must contain at least one letter. That is, the word must
32+
not be interpreted as a number.
33+
34+
The names of packages are intended to be unique.
35+
36+
??? question "Are other project templates available?"
37+
38+
Yes. For further information about project templates, command:
39+
~~~text
40+
stack templates
41+
~~~
3042

3143
From the root directory for all our Haskell projects, we command:
3244

3345
~~~text
3446
stack new helloworld new-template
3547
~~~
3648

37-
For this first Stack command, there's quite a bit of initial setup it needs to
38-
do (such as downloading the list of packages available upstream), so you'll see
39-
a lot of output. Over the course of this guide a lot of the content will begin
40-
to make more sense.
49+
For this first Stack command, Stack will do some setting up. For example, it
50+
will create the [Stack root](../topics/stack_root.md) directory.
4151

42-
After creating the project directory, and obtaining and populating the project
43-
template, Stack will initialise its own project-level configuration. For further
44-
information about setting paramaters to populate templates, see the YAML
45-
configuration
46-
[documentation](../configure/yaml/yaml_configuration.md#templates). For further
47-
information about initialisation, see the
48-
[`stack init`](../commands/init_command.md) command documentation. The
49-
`stack new` and `stack init` commands have options and flags in common.
52+
Other than any setting up, Stack will:
53+
* create the project directory;
54+
* download the project template;
55+
* attempt to populate the project template based on parameters; and
56+
* create and initialise Stack's project-level configuration file.
5057

51-
!!! info
58+
Unless the parameters have been configured, Stack will note that parameters were
59+
needed by the template but not provided. That can be ignored for now.
5260

53-
Pass the `--bare` flag to cause Stack to create the project in the current
54-
working directory rather than in a new project directory.
61+
??? question "How can I configure project template paramaters?"
5562

56-
!!! info
63+
For further information, see the
64+
[`templates`](../configure/yaml/non-project.md#templates) non-project
65+
specific configuration option.
5766

58-
Parameters to populate project templates can be set at the command line with
59-
the `--param <key>:<value>` (or `-p`) option.
67+
As noted in Stack's output, parameters to populate project templates can
68+
also be set at the command line by using the options of the `stack new`
69+
command.
70+
71+
??? question "Can I create a new project in the current working directory?"
72+
73+
Yes. Pass the `--bare` flag to cause Stack to create the project in the
74+
current working directory rather than in a new project directory.
6075

6176
We now have a project in the `helloworld` directory! We will change to that
6277
directory, with command:
@@ -67,26 +82,77 @@ cd helloworld
6782

6883
## The `stack build` command
6984

70-
Next, we'll run the most important Stack command, `stack build`:
85+
Next, we'll run the most important Stack command,
86+
[`stack build`](../commands/build_command.md). We command:
7187

7288
~~~text
7389
stack build
74-
# installing ... building ...
7590
~~~
7691

77-
Stack needs a version of GHC in order to build your project. Stack will discover
78-
that you are missing it and will install it for you.
92+
Stack needs a version of GHC and, on Windows, a version of MSYS2, in order to
93+
build your project. Stack will discover that you are missing it and will install
94+
it for you.
7995

8096
You'll get intermediate download percentage statistics while the download is
8197
occurring. This command may take some time, depending on download speeds.
8298

83-
!!! note
99+
??? question "Can I use that version of GHC by commanding `ghc`?"
100+
101+
No. GHC will be installed to the Stack programs directory, which is likely
102+
not on the PATH, so commanding `ghc` will not work. However, that version of
103+
GHC can be used in the Stack environment. For more information, see the
104+
[`stack exec`](../commands/exec_command.md) command,
105+
[`stack ghc`](../commands/ghc_command.md) command, and
106+
[`stack runghc`](../commands/ghc_command.md) command documentation.
107+
108+
Once a version of GHC and, on Windows, a version of MSYS2, is installed, Stack
109+
will then build your project. The end of the output should look similar to this:
84110

85-
GHC will be installed to your Stack programs directory, so calling `ghc` on
86-
the command line won't work. See the `stack exec`, `stack ghc`, and
87-
`stack runghc` commands below for more information.
111+
=== "Unix-like"
112+
113+
~~~text
114+
...
115+
helloworld> configure (lib + exe)
116+
Configuring helloworld-0.1.0.0...
117+
helloworld> build (lib + exe) with ghc-9.6.6
118+
Preprocessing library for helloworld-0.1.0.0..
119+
Building library for helloworld-0.1.0.0..
120+
[1 of 2] Compiling Lib
121+
[2 of 2] Compiling Paths_helloworld
122+
Preprocessing executable 'helloworld-exe' for helloworld-0.1.0.0..
123+
Building executable 'helloworld-exe' for helloworld-0.1.0.0..
124+
[1 of 2] Compiling Main
125+
[2 of 2] Compiling Paths_helloworld
126+
[3 of 3] Linking .stack-work/dist/x86_64-linux-tinfo6/ghc-9.6.6/build/helloworld-exe/helloworld-exe
127+
helloworld> copy/register
128+
Installing library in .../helloworld/.stack-work/install/x86_64-linux-tinfo6/a2caceceda039eb4f791856f85a68f9582d4daf3d0527344693ff3d1fcd92ba4/9.6.6/lib/x86_64-linux-ghc-9.6.6/helloworld-0.1.0.0-KFyX8zLxDvzLZURq3JaCVX
129+
Installing executable helloworld-exe in .../helloworld/.stack-work/install/x86_64-linux-tinfo6/a2caceceda039eb4f791856f85a68f9582d4daf3d0527344693ff3d1fcd92ba4/9.6.6/bin
130+
Registering library for helloworld-0.1.0.0..
131+
~~~
88132

89-
Once a version of GHC is installed, Stack will then build your project.
133+
=== "Windows"
134+
135+
~~~text
136+
...
137+
helloworld> configure (lib + exe)
138+
Configuring helloworld-0.1.0.0...
139+
helloworld> build (lib + exe) with ghc-9.6.6
140+
Preprocessing library for helloworld-0.1.0.0..
141+
Building library for helloworld-0.1.0.0..
142+
[1 of 2] Compiling Lib
143+
[2 of 2] Compiling Paths_helloworld
144+
Preprocessing executable 'helloworld-exe' for helloworld-0.1.0.0..
145+
Building executable 'helloworld-exe' for helloworld-0.1.0.0..
146+
[1 of 2] Compiling Main
147+
[2 of 2] Compiling Paths_helloworld
148+
[3 of 3] Linking .stack-work\dist\effaccc7\build\helloworld-exe\helloworld-exe.exe
149+
helloworld> copy/register
150+
Installing library in ...\helloworld\.stack-work\install\c8c71a24\lib\x86_64-windows-ghc-9.6.6\helloworld-0.1.0.0-KFyX8zLxDvzLZURq3JaCVX
151+
Installing executable helloworld-exe in ...\helloworld\.stack-work\install\c8c71a24\bin
152+
Registering library for helloworld-0.1.0.0..
153+
~~~
154+
155+
On Windows, Stack uses hashes of certain information to keep paths short.
90156

91157
## The `stack exec` command
92158

@@ -96,14 +162,44 @@ Windows, `helloworld-exe.exe`). We'll explain more in the next section, but, for
96162
now, just notice that the executables are installed in a location in our
97163
project's `.stack-work` directory.
98164

99-
Now, Let's use the `stack exec` command to run our executable (which just
100-
outputs "someFunc"):
165+
Now, let's use the [`stack exec`](../commands/exec_command.md) command to run
166+
our executable. We command:
101167

102168
~~~text
103169
stack exec helloworld-exe
170+
~~~
171+
172+
and the output is just:
173+
~~~text
104174
someFunc
105175
~~~
106176

177+
??? question "Why is the output just `someFunc`?"
178+
179+
The code in the `new-template` project template is very simple. The package
180+
has a Haskell module `Lib`:
181+
~~~haskell
182+
module Lib
183+
( someFunc
184+
) where
185+
186+
someFunc :: IO ()
187+
someFunc = putStrLn "someFunc"
188+
~~~
189+
190+
and a Haskell module `Main`:
191+
~~~haskell
192+
module Main (main) where
193+
194+
import Lib
195+
196+
main :: IO ()
197+
main = someFunc
198+
~~~
199+
200+
`putStrLn "someFunc"` is an action that, when executed, outputs the string
201+
`someFunc` to the standard output channel.
202+
107203
`stack exec` works by providing the same reproducible environment that was used
108204
to build your project to the command that you are running. Thus, it knew where
109205
to find `helloworld-exe` even though it is hidden in the `.stack-work`

0 commit comments

Comments
 (0)