learnhaskell/specific_topics.md
2015-05-02 15:05:12 -05:00

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:

Understanding basic Haskell error messages


Laziness, strictness, guarded recursion

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

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.

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:

  • Reddit comment by htmltyp and Crandom here.

  • Reddit comment by jozefg here.

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

Parsing and generating JSON

Aeson is the standard JSON parsing solution in haskell. Available from hackage and github.

Graph algorithms and data structures

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

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

  1. Fix your hackage documentation

  2. Hackage documentation v2

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:

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

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.

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:

Books

Stephen's Nifty "How to get to monad" posts

Other fun topics

Parametricity, ad-hoc vs. parametric polymorphism, free theorems

Initial and Final, DSLs, Finally Tagless

Comonads

Yoneda / CoYoneda

Propositions vs. Judgments (computation)

Dependent typing

Statically linking binaries