Skip to content

Commit 46f6acd

Browse files
Update tutorials (initial part of semester) (#28)
* Update tutorial 01 (links, code blocks, typos) * Improve stack outputs with newer version * Update Haskell, JavaScript and new languages section
1 parent e0a74c6 commit 46f6acd

File tree

1 file changed

+76
-61
lines changed

1 file changed

+76
-61
lines changed

tutorials/01_fp-env.md

Lines changed: 76 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Lambda calculus is good to understand (at least basics) when you want to start w
1212
* [Lambda calculus (Stanford)](https://plato.stanford.edu/entries/lambda-calculus/)
1313
* [The Lambda Calculus for Absolute Dummies (like myself)](http://palmstroem.blogspot.cz/2012/05/lambda-calculus-for-absolute-dummies.html)
1414

15-
For FIT CTU students, there are subjects [BI-PPA](https://edux.fit.cvut.cz/courses/BI-PPA) and [MI-PSL](https://edux.fit.cvut.cz/courses/MI-PSL) which also cover basics of lambda calculus and functional programming.
15+
For FIT CTU students, there are subjects [BI-PPA](https://courses.fit.cvut.cz/BI-PPA/) and [MI-PSL](https://courses.fit.cvut.cz/MI-PSL/) which also cover basics of lambda calculus and functional programming.
1616

1717
### Function as first class-object
1818

@@ -55,7 +55,7 @@ Principles [[jdegoes](https://twitter.com/jdegoes/status/974045822424776704?s=09
5555
* [Cabal] = system for building and packaging.
5656
* [Stack] = managing Haskell projects, works with [Cabal] for you.
5757

58-
:point_right: Please these (or check if installed already) - you can follow instruction on official websites one by one or (better) install [Haskell Platform], which includes all of those and also most common packages.
58+
:point_right: Please, install these (or check if installed already) - you can follow instruction on official websites one by one or (better) install [Haskell Platform], which includes all of those and also most common packages.
5959

6060
### Editors and IDEs
6161

@@ -72,40 +72,42 @@ Most probably you will need following stuff:
7272
* [hindent] = indenter, pretty print
7373
* [stylish-haskell] = code prettifier ("good style")
7474

75-
Install those with [Cabal] (by default it will install just for you to your profile, ensure that you have `~/.cabal/bin` in your `PATH`. The installation might take a while - it has a lot of dependencies and needs to build them from Haskell source code. If you want to install something with [Cabal] to all users, use `--global` flag.
75+
Install those with [Cabal] (by default it will install just for you to your profile, ensure that you have `~/.cabal/bin` in your `PATH`) or with [Stack]. The installation might take a while - it has a lot of dependencies and needs to build them from Haskell source code. If you want to install something with [Cabal] to all users, use `--global` flag.
7676

77-
```
77+
```console
7878
$ cabal update
7979
$ cabal install hlint stylish-haskell hindent ghc-mod
80+
$ stack install hlint stylish-haskell hindent ghc-mod
8081
```
8182

8283
### Sites for searing
8384

8485
* [Hoogle] = "Google" for Haskell world
85-
* [Hayoo!] = "Yahoo" for Haskell world
8686
* [Hackage] = package archive, there are packages which can you install and use standalone or as modules for your projects (similar to PyPI for Python, RubyGems for Ruby, etc.)
8787
* [Stackage] = package archive, alternative to [Hackage], only stable packages
8888

8989
:point_right: Take a look at them...
9090

9191
### Haskell, JavaScript and new languages
9292

93-
If you like to build (frontend/backend) JavaScript applications you can do that nicely with Haskell. There are multiple options, most known are:
93+
If you like to build (frontend/backend) JavaScript applications you can do that nicely with Haskell or similar language. There are multiple options, most known are:
9494

9595
* [GHCJS]
9696
* [Haste]
9797
* [PureScript]
98-
* [Elm]
98+
* [Elm] (will be covered in later lectures)
9999

100100
This is a nice example of practical usage of Haskell for web projects! It is so much easier (and safer) to write JavaScript in Haskell than just plain JavaScript. Some of those are not just Haskell dialects or libraries but new languages deeply inspired by Haskell. For more information, read about [The JavaScript Problem](https://wiki.haskell.org/The_JavaScript_Problem). We will slightly look at this at the end of this course.
101101

102102
## Try to be interactive
103103

104-
Now you should have [GHC] installed (and others as well, but we won't need an editor for this time), you can test it out with the following command.
104+
Now you should have [GHC] installed from package or via Stack (and others as well, but we won't need an editor for this time), you can test it out with the following command.
105105

106-
```
106+
```console
107107
% ghc --version
108108
The Glorious Glasgow Haskell Compilation System, version 8.0.2
109+
% stack exec -- ghc --version
110+
The Glorious Glasgow Haskell Compilation System, version 8.0.2
109111
```
110112

111113
First, let's try the interactive environment and evaluate some basic expression in Haskell for the first time.
@@ -244,7 +246,7 @@ Prelude> 5 `mod` 3
244246
2
245247
```
246248

247-
### Giving name to expression
249+
### Giving a name to an expression
248250

249251
In GHCi you can name an expression with `let` and assignment.
250252

@@ -359,7 +361,7 @@ main = putStrLn "Hello, world!"
359361

360362
Now use `ghc` compiler to compile the file:
361363

362-
```
364+
```console
363365
% ghc 01_hw.hs
364366
[1 of 1] Compiling Main ( 01_hw.hs, 01_hw.o )
365367
Linking 01_hw ...
@@ -374,7 +376,7 @@ You can see some similar output as when you were loading a file in GHCi just her
374376

375377
And you can run the executable:
376378

377-
```
379+
```console
378380
% ./01_hw
379381
Hello, world!
380382
```
@@ -399,9 +401,9 @@ main = do
399401
putStrLn (greet name)
400402
```
401403

402-
Now how to make it work together? Do you know `Makefile`s from `C/C++`? Don't worry... GHC is a great tool and does such painful work for you (reads imports and looking up the files - you just need to have good naming of modules/files):
404+
Now how to make it work together? Do you know `Makefile` (for example, from `C/C++`)? Don't worry... GHC is a great tool and does such painful work for you (reads imports and looking up the files - you just need to have a standard naming of modules/files):
403405

404-
```
406+
```console
405407
% ghc --make Main.hs
406408
[1 of 2] Compiling HWLib ( HWLib.hs, HWLib.o )
407409
[2 of 2] Compiling Main ( Main.hs, Main.o )
@@ -418,37 +420,48 @@ Compiling application made from multiple source codes is not so complicated in t
418420

419421
Let's do the *Hello, world!* app with [Stack]. First, verify that you have it installed.
420422

421-
```
423+
```console
422424
% stack --version
423-
Version 1.4.0, Git revision e714f1dd3fade19496d91bd6a017e435a96a6bcd (4640 commits) x86_64 hpack-0.17.0
425+
Version 1.9.3, Git revision 40cf7b37526b86d1676da82167ea8758a854953b (6211 commits) x86_64 hpack-0.31.1
424426
```
425427

426428
Then you can create a new project with default template:
427429

428-
```
430+
```console
429431
% stack new HelloWorld
430432
Downloading template "new-template" to create project "HelloWorld" in HelloWorld/ ...
431433

432-
...
434+
The following parameters were needed by the template but not provided: author-name
435+
You can provide them in /home/user/.stack/config.yaml, like this:
436+
templates:
437+
params:
438+
author-name: value
439+
Or you can pass each one as parameters like this:
440+
stack new HelloWorld new-template -p "author-name:value"
441+
442+
443+
The following parameters were needed by the template but not provided: author-email, author-name, category, copyright, github-username
444+
You can provide them in /home/user/.stack/config.yaml, like this:
445+
templates:
446+
params:
447+
author-email: value
448+
author-name: value
449+
category: value
450+
copyright: value
451+
github-username: value
452+
Or you can pass each one as parameters like this:
453+
stack new HelloWorld new-template -p "author-email:value" -p "author-name:value" -p "category:value" -p "copyright:value" -p "github-username:value"
433454

434455
Looking for .cabal or package.yaml files to use to init the project.
435456
Using cabal packages:
436-
- HelloWorld/HelloWorld.cabal
457+
- HelloWorld/
437458

438-
Selecting the best among 11 snapshots...
459+
Selecting the best among 15 snapshots...
439460

440-
Downloaded lts-9.11 build plan.
441-
Missing some cabal revision files, updating indices
442-
Selected mirror https://s3.amazonaws.com/hackage.fpcomplete.com/
443-
Downloading timestamp
444-
Downloading snapshot
445-
Updating index
446-
Updated package list downloaded
447-
Populated index cache.
448-
* Matches lts-9.11
449-
450-
Selected resolver: lts-9.11
451-
Initialising configuration using resolver: lts-9.11
461+
* Matches lts-13.8
462+
463+
Selected resolver: lts-13.8
464+
Initialising configuration using resolver: lts-13.8
452465
Total number of user packages considered: 1
453466
Writing configuration to file: HelloWorld/stack.yaml
454467
All done.
@@ -481,15 +494,15 @@ main = do
481494

482495
Now you don't use GHC directly, but call it via `stack build`:
483496

484-
```
497+
```console
485498
% stack build
486499
No compiler found, expected minor version match with ghc-8.0.2 (x86_64) (based on resolver setting in /home/user/.stack/global-project/stack.yaml).
487500
To install the correct GHC into /home/user/.stack/programs/x86_64-linux/, try running "stack setup" or use the "--install-ghc" flag. To use your system GHC installation, run "stack config set system-ghc --global true", or use the "--system-ghc" flag.
488501
```
489502

490503
As you see `stack` doesn't want to use system-wide installation of `ghc` but local instead by default. Just run `stack setup` so `stack` will prepare local `ghc` (it will take some time) and then try to build.
491504

492-
```
505+
```console
493506
% stack setup
494507
Preparing to install GHC to an isolated location.
495508
This will not interfere with any system-level installation.
@@ -507,35 +520,38 @@ Using cabal packages:
507520

508521
Selecting the best among 11 snapshots...
509522

510-
* Matches lts-9.11
523+
* Matches lts-13.8
511524

512-
Selected resolver: lts-9.11
525+
Selected resolver: lts-13.8
513526
Initialising configuration using resolver: lts-9.11
514527
Total number of user packages considered: 1
515528
Writing configuration to file: stack.yaml
516529
All done.
517530

518531
% stack build
519-
Linking /home/user/.stack/setup-exe-cache/x86_64-linux/tmp-Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 ...
532+
Building all executables for `HelloWorld' once. After a successful build of all of them, only specified executables will be rebuilt.
520533
HelloWorld-0.1.0.0: configure (lib + exe)
521534
Configuring HelloWorld-0.1.0.0...
522535
HelloWorld-0.1.0.0: build (lib + exe)
523-
Preprocessing library HelloWorld-0.1.0.0...
524-
[1 of 1] Compiling Lib ( src/Lib.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build/Lib.o )
525-
Preprocessing executable 'HelloWorld-exe' for HelloWorld-0.1.0.0...
526-
[1 of 1] Compiling Main ( app/Main.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build/HelloWorld-exe/HelloWorld-exe-tmp/Main.o )
527-
Linking .stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build/HelloWorld-exe/HelloWorld-exe ...
536+
Preprocessing library for HelloWorld-0.1.0.0..
537+
Building library for HelloWorld-0.1.0.0..
538+
[1 of 2] Compiling Lib ( src/Lib.hs, .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.4.0.1/build/Lib.o )
539+
[2 of 2] Compiling Paths_HelloWorld ( .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.4.0.1/build/autogen/Paths_HelloWorld.hs, .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.4.0.1/build/Paths_HelloWorld.o )
540+
Preprocessing executable 'HelloWorld-exe' for HelloWorld-0.1.0.0..
541+
Building executable 'HelloWorld-exe' for HelloWorld-0.1.0.0..
542+
[1 of 2] Compiling Main ( app/Main.hs, .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.4.0.1/build/HelloWorld-exe/HelloWorld-exe-tmp/Main.o )
543+
[2 of 2] Compiling Paths_HelloWorld ( .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.4.0.1/build/HelloWorld-exe/autogen/Paths_HelloWorld.hs, .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.4.0.1/build/HelloWorld-exe/HelloWorld-exe-tmp/Paths_HelloWorld.o )
544+
Linking .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.4.0.1/build/HelloWorld-exe/HelloWorld-exe ...
528545
HelloWorld-0.1.0.0: copy/register
529-
Installing library in
530-
/home/user/Projects/CTU/FPCourse/files/01_hw_stack/.stack-work/install/x86_64-linux/lts-9.11/8.0.2/lib/x86_64-linux-ghc-8.0.2/HelloWorld-0.1.0.0-GwaTuTuS4ojL8nYytSjUL5
531-
Installing executable(s) in
532-
/home/user/Projects/CTU/FPCourse/files/01_hw_stack/.stack-work/install/x86_64-linux/lts-9.11/8.0.2/bin
533-
Registering HelloWorld-0.1.0.0...
546+
Installing library in /home/user/Projects/MI-AFP/tests/HelloWorld/.stack-work/install/x86_64-linux-tinfo6/lts-13.8/8.6.3/lib/x86_64-linux-ghc-8.6.3/HelloWorld-0.1.0.0-8b39YCi0nmn4QsoDKix2j8
547+
Installing executable HelloWorld-exe in /home/user/Projects/MI-AFP/tests/HelloWorld/.stack-work/install/x86_64-linux-tinfo6/lts-13.8/8.6.3/bin
548+
Registering library for HelloWorld-0.1.0.0..
549+
stack build 6.16s user 0.94s system 96% cpu 7.329 total
534550
```
535551

536552
Everything ended up OK and you are finally able to run the application (`HelloWorld-exe` is defined in `package.yaml`, thus also `HelloWorld.cabal`, and you may change it):
537553

538-
```
554+
```console
539555
% stack exec HelloWorld-exe
540556
Enter your name:
541557
Marek
@@ -544,18 +560,18 @@ Hello, Marek!
544560

545561
For debugging you can run `ghci` with project preloaded:
546562

547-
```
563+
```console
548564
% stack ghci
565+
Using main module: 1. Package `HelloWorld' component exe:HelloWorld-exe with main-is file: /home/user/Projects/MI-AFP/tests/HelloWorld/app/Main.hs
549566
The following GHC options are incompatible with GHCi and have not been passed to it: -threaded
550567
Configuring GHCi with the following packages: HelloWorld
551-
Using main module: 1. Package `HelloWorld' component exe:HelloWorld-exe with main-is file: /home/.../HelloWorld/app/Main.hs
552-
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help
553-
[1 of 1] Compiling Lib ( /home/.../HelloWorld/src/Lib.hs, interpreted )
554-
Ok, modules loaded: Lib.
555-
[2 of 2] Compiling Main ( /home/.../HelloWorld/app/Main.hs, interpreted )
556-
Ok, modules loaded: Lib, Main.
557-
Loaded GHCi configuration from /tmp/ghci18580/ghci-script
558-
*Main Lib> :browse
568+
GHCi, version 8.6.3: http://www.haskell.org/ghc/ :? for help
569+
[1 of 2] Compiling Lib ( /home/user/Projects/MI-AFP/tests/HelloWorld/src/Lib.hs, interpreted )
570+
[2 of 2] Compiling Main ( /home/user/Projects/MI-AFP/tests/HelloWorld/app/Main.hs, interpreted )
571+
Ok, two modules loaded.
572+
Loaded GHCi configuration from /tmp/haskell-stack-ghci/3b07e5cf/ghci-script
573+
*Main Lib>
574+
:browse
559575
main :: IO ()
560576
*Main Lib> :browse Lib
561577
greet :: String -> String
@@ -567,7 +583,7 @@ greet :: String -> String
567583

568584
You might have noticed that [Stack] uses `package.yaml` to generate `.cabal` and there is some `stack.yaml`. It also somehow takes care of the needed dependencies. Let's say you need to your collection of type Set. Of course, you could implement it on your own, but reinventing the wheel is unnecessary! Use `Data.Set` which is already here (we will cover details about this and other data structures in Haskell in the future).
569585

570-
If you look up `Data.Set` ([Hoogle], [Hayoo!] or [Hackage]), you will find out that it is in package `containers` licensed under BSD with maintainer email [email protected] (see [here](http://hackage.haskell.org/package/containers-0.5.11.0/docs/Data-Set.html)). If you now try to do this in your `Lib.hs`:
586+
If you look up `Data.Set` ([Hoogle], [Stackage] or [Hackage]), you will find out that it is in package `containers` licensed under BSD with maintainer email [email protected] (see [here](http://hackage.haskell.org/package/containers-0.5.11.0/docs/Data-Set.html)). If you now try to do this in your `Lib.hs`:
571587

572588
```haskell
573589
import Data.Set
@@ -577,7 +593,7 @@ namesSet = insert "Robert" (insert "Marek" empty)
577593

578594
After trying to build with `stack build` you should get this error stating that it could not find module `Data.Set`:
579595

580-
```
596+
```console
581597
/home/.../HelloWorld/src/Lib.hs:5:1: error:
582598
Could not find module ‘Data.Set’
583599
Perhaps you meant Data.Int (from base-4.10.1.0)
@@ -616,7 +632,7 @@ Further, [Stack] also provides [dependency visualization](https://docs.haskellst
616632

617633
To test out the workflow check the dummy homework [MI-AFP/hw00](https://github.com/MI-AFP/hw00) where you will learn how you should get, complete, check, and submit homework (especially useful if you are not familiar with [GitHub] and [Travis CI]). By working on such homework, you might also learn new things which you encounter in tests and skeletons.
618634

619-
For your first assignment, visit [MI-AFP/hw01](https://github.com/MI-AFP/hw01). The task consists of writing simple expressions, looking up information with [Hoogle], [Hayoo!] and/or GHCi, and working with dependencies of project.
635+
For your first assignment, visit [MI-AFP/hw01](https://github.com/MI-AFP/hw01). The task consists of writing simple expressions, looking up information with [Hoogle], [Stackage], [Hackage] and/or GHCi, and working with dependencies of project.
620636

621637
## Further reading
622638

@@ -638,7 +654,6 @@ For your first assignment, visit [MI-AFP/hw01](https://github.com/MI-AFP/hw01).
638654
[Haskell 2010]: https://www.haskell.org/onlinereport/haskell2010/
639655
[Haskell Platform]: https://www.haskell.org/platform/
640656
[Haste]: https://haste-lang.org
641-
[Hayoo!]: https://hayoo.fh-wedel.de
642657
[hindent]: https://github.com/commercialhaskell/hindent
643658
[hlint]: https://hackage.haskell.org/package/hlint
644659
[Hoogle]: https://www.haskell.org/hoogle/

0 commit comments

Comments
 (0)