Excellent article.
Useful for understanding Functor
, Applicative
, Monad
, Monoid
and other
typeclasses in general but also some Hask-specific category theory:
- The Typeclassopedia
-
Heinrich Apfelmus' article series at https://hackhands.com/guide-lazy-evaluation-haskell/ is excellent.
-
Marlow's book about parallelism and concurrency has one of the best introductions to laziness and normal form I've found. Use other material too if it doesn't stick immediately.
-
SO question 'Does haskell have laziness?'
-
Johan Tibell's slides from a talk on reasoning about laziness.
let a = 1 : a -- guarded recursion, (:) is lazy and can be pattern matched.
let (v : _) = a
> v
1
> head a -- head a == v
1
let a = 1 * a -- not guarded, (*) is strict
> a
*** Exception: <<loop>>
-
Haddocks for System.IO.Unsafe.unsafePerformIO Read the docs and note implementation of unsafeDupablePerformIO
Comment from Reddit thread by glaebhoerl
Interesting side note: GHC needs to hide the state token representation behind an abstract IO type because the state token must always be used linearly (not duplicated or dropped), but the type system can't enforce this. Clean, another lazy Haskell-like language, has uniqueness types (which are like linear types and possibly different in ways I'm not aware of), and they expose the World-passing directly and provide a (non-abstract) IO monad only for convenience.
Do not do these until you understand typeclasses, Monoid, Functor, and Applicative!
Implement the standard library monads ( List, Maybe, Cont, Error, Reader, Writer, State ) for yourself to understand them better. Then maybe write an monadic interpreter for a small expression language using Monad Transformers Step by Step paper (mentioned in 'monad transformers' below).
Writing many interpreters by just changing the monad to change the semantics can help convey what's going on.
Also, reimplement Control.Monad
. Functions like mapM
or sequence
are good
opportunities to practice writing generic monadic code.
The FP course can be used as a guide to this process, which will also involve writing your own Applicative as well.
Credits:
-
This tutorial by Kazu Yamamoto is fantastic.
-
Simple-Conduit: Good simple library for learning how streaming IO works in general, knowledge transferrable to libraries like Pipes and Conduit
-
Parser combinator tutorial for Haskell using Parsec
Aeson is the standard JSON parsing solution in haskell. Available from hackage and github.
-
The fgl package particularly the purely functional shortest path algos.
-
The graphs package.
Cabal Hell was a problem for Haskell users before the introduction of sandboxes. Installing outside of a sandbox will install into your user package-db. This is not a good idea except for foundational packages like Cabal, alex, and happy. Nothing else should be installed in the user or global package-dbs unless you know what you're doing.
Some best practices for avoiding cabal hell are available here.
To experiment with a package or start a project, begin by doing
cabal sandbox init
in a new directory.
Put briefly:
-
Always use sandboxes for installing new packages, building new or existing projects, or starting experiments
-
Use
cabal repl
to start a project-scoped ghci instance
The sandbox-based approach I suggest should avoid package-dependency problems, but it's incompatible with the way the Haskell Platform provides pre-built packages. If you're still learning Haskell and don't understand how ghc-pkg and Cabal work, avoid platform and instead use the install instructions earlier in the guide.
For any users (usually Yesod users) that have build problems, consider Stackage.
- A good summary of Stackage is here.
In the author's opinion, Stackage is usually more useful than cabal freeze
.
The Hoogle search engine can search by type.
For example, look at the search results for (a -> b) -> [a] -> [b]
here.
Also hosted by fpcomplete here.
Also Hayoo (which has all of hackage enabled for search by default).
Take a look here.
Note that these posts are slightly out of date: for example, now Hackage sports shiny new info with documentation info and build status.
In order to have haddocks include documentation for related packages, you have
to set documentation: True
in your ~/.cabal/config
. If it was left on the
default (False
) or set to False
, you'll have to delete all your packages and
reinstall before generating haddocks.
The other thing to keep in mind is that due to the way the $pkg
parameter gets
interpolated by cabal, not by you, the html-location
and content-location
parameters must be in single quotes and entered into a shell or contained in a
shell script. They will not work in a Makefile, because it will think they are
Make variables!
#! /usr/bin/env sh
# You can write it one one line by skipping the backslashes
cabal haddock --hoogle --hyperlink-source \
--html-location='http://hackage.haskell.org/package/$pkg/docs' \
--contents-location='http://hackage.haskell.org/package/$pkg'
If you're as big a fan of TravisCI as I am, then I
strongly recommend you take a look at
multi-ghc-travis by as the basis of
the travis.yml
for your Haskell projects.
We have an embarrassment of riches! There are three main choices I would recommend:
-
Haste a Haskell to JavaScript compiler
-
- Not strictly Haskell like Haste and GHCJS, but a popular choice among Haskellers
- Written in and inspired by haskell
- Try purescript in you browser here
- Great guide for getting started
GHCJS and Haste are both fully Haskell. GHCJS will work with more Haskell packages than Haste, but this doesn't affect a lot of frontend projects. PureScript isn't Haskell at all, so direct code sharing with your backend will not work.
GHCJS has the fattest runtime payload overhead at about 100kb (luite is working on this). Haste and PureScript are competitive.
PureScript has the best JS tooling integration (uses gulp/grunt/bower), GHCJS and Haste integrate better with Haskell's tooling (Cabal).
All three are great choices and will work for most frontend projects.
-
Parallel and Concurrent Programming in Haskell. This book by Simon Marlow is probably the best I've ever read on the topics of Parallelism and Concurrency.
-
A thorough walk-through on testing & incremental development of a multi-threaded application in Haskell.
After you're comfortable with Haskell, strongly consider learning Lenses and Prisms, even if just as a "user". You don't need to understand the underlying category for it to be useful.
People vastly overestimate the difficulty of using Lens. Anybody comfortable with Functor/Foldable/Traversable (or even just the first one) can leverage lenses and prisms to make their life happier.
If you've ever done something like: (fmap . fmap)
you were "lensing" in your
head.
I recommend these two tutorials/introductions:
Look here for more information: Lens package on hackage.
Some of the crazy *-morphism words you've heard are actually about recursion. NB - before tackling this material you should know how to implement foldr for lists and at least one other data structure, such as a tree. (folds are catamorphisms) Knowing how to implement an unfold (anamorphism) for the same will round things out a bit.
This material dovetails with traversable and foldable.
-
Don't fear the cat - Good demonstration of how hylomorphism is the composition of cata and ana.
-
Recursion Schemes - This field guide is excellent.
-
Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire
Not needed to actually write Haskell, just for those interested!
If you want to follow up on type and category theory:
-
The haskell wikibook has nice diagrams
-
Category Theory on haskellwiki, also has good links to other resources
-
Categories from scratch, Includes some practical examples.
-
Pierce's Great Works in PL list.
-
Quora Question: What is the best textbook for category theory? Kmett's recommendations
-
Awodey and MacLane. The standard textbooks on category theory.
-
Harper's Practical Foundations for Programming Languages is the best PL focused intro to type theory I've read.
Generics are usually used to generate typeclass instances, but you'll see TH used for that purpose too.
-
https://ocharles.org.uk/blog/posts/2014-04-26-constructing-generically.html
-
Current docs: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#generic-programming
-
Simple example, nice for avoiding gnarly escape sequences in strings: https://hackage.haskell.org/package/raw-strings-qq-1.0.2/docs/Text-RawString-QQ.html
-
Tutorial: https://www.fpcomplete.com/user/marcin/quasiquotation-101
-
http://www.parsonsmatt.org/programming/2015/11/15/template_haskell.html (Great tutorial, strongly recommended even if the Ruby doesn't mean much to you)
-
https://ocharles.org.uk/blog/guest-posts/2014-12-22-template-haskell.html
-
Current docs: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#template-haskell
-
OCaml and TH slides: https://nicolaspouillard.fr/talks/cufp-metaprogramming-tutorial-slides.pdf
-
http://illustratedhaskell.org/index.php/2011/09/24/template-haskell-tutorial/ (old)
-
https://stackoverflow.com/questions/10857030/whats-so-bad-about-template-haskell (warnings about TH)
-
TeX sources for the above talk.
-
The dog that didn't bark less specifically relevant but interesting.
-
Free monads for Less, a sequence of three articles by Edward Kmett