21 KiB
Specific topics for Haskell users
What does that <-
/ do
/ list comprehension syntactic sugar do?
Excellent article.
For understanding list and fold
For learning some common typeclasses
Useful for understanding Functor
, Applicative
, Monad
, Monoid
and other
typeclasses in general but also some Hask-specific category theory:
- The Typeclassopedia
Understanding basic Haskell error messages
Laziness, strictness, guarded recursion
-
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.
Brief demonstration
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>>
IO
-
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.
Monads and monad transformers
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.
- This talk by Tony excellently motivates monad transformers, the slides.
Also, reimplement Control.Monad
. Functions like mapM
or sequence
are good
opportunities to practice writing generic monadic code.
The NICTA course can be used as a guide to this process, which will also involve writing your own Applicative as well.
Credits:
Monad transformers
Testing, tests, specs, generative/property testing
-
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
Parsing in Haskell
-
Parser combinator tutorial for Haskell using Parsec
Parsing and generating JSON
Aeson is the standard JSON parsing solution in haskell. Available from hackage and github.
Graph algorithms and data structures
-
The fgl package particularly the purely functional shortest path algos.
-
The graphs package.
Development Environment
Emacs
Vim
Sublime Text
FAQ and working with Cabal
Fantastic FAQ
In addition to being an amazing guide for all kinds of things such as GADTs, this also covers some useful basics for Cabal
- What I wish I knew when learning Haskell also on github here.
Cabal guidelines
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.
Stackage
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
.
Hoogle and Haddock
Search code by type signature
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).
Setting up your own local instance of Hoogle
Take a look here.
Haddock
Note that these posts are slightly out of date: for example, now Hackage sports shiny new info with documentation info and build status.
What you really need to know
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'
TravisCI
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.
Frontend/JavaScript
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
Which frontend language do I use?
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.
For a more thorough understanding of laziness, NF, WHNF
Research papers about lazy lambda calculi
Parallelism/Concurrency
-
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.
Lenses and Prisms
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.
Recursion Schemes
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
GHC Core and performance tuning
Type and Category Theory
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.
Books
-
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.
Stephen's Nifty "How to get to monad" posts
Other fun topics
Parametricity, ad-hoc vs. parametric polymorphism, free theorems
-
TeX sources for the above talk.
Initial and Final, DSLs, Finally Tagless
-
The dog that didn't bark less specifically relevant but interesting.
Comonads
Yoneda / CoYoneda
-
Free monads for Less, a sequence of three articles by Edward Kmett