This is a collection of standard or at least widely used Haskell types, together with a one-sentence explanation of how their instances behave. These are only mental aids; the claims only have to work in my head, and not necessarily in Haskell. For example, I will often refer to the "value contained" in a monadic action. This makes this a bad place to start about learning about instances, it is only a reference and nothing else.
data Maybe a = Nothing
| Just a
- Eq, Ord: Compare
Just
values;Nothing
is smaller than anyJust
. - Monoid: Ignore
Nothing
,mappend
Just
contents - Functor: Map over
Just
contents, do nothing forNothing
- Applicative: "Value contained": values in
Just
. If there is aNothing
, the entire result is. - Monad
-
join
: flattenMaybe (Maybe a)
.Nothing
if there's aNothing
.join (Just (Just 1)) = Just 1
->>=
: ExtractJust
contents and pass them on; if aNothing
is encountered, the overall result isNothing
- Alternative: Ignore
Nothing
, return firstJust
value - MonadPlus: Like Alternative
First
- Monoid: Like AlternativeLast
- Monoid: "Reversed Alternative": return lastJust
value
Lists are similar to Maybe
but with the ability to hold multiple values, and
behave accordingly.
data List a = []
| a : List a
- Eq, Ord: Compare element-wise from left to right.
[]
is smaller than any non-empty list. - Monoid: Concatenation
- Functor: Map over list elements
- Applicative: Do the computation for every combination of lists involved
- Monad
-
join
: Flatten list of lists.join [[1],[2],[3,4]] = [1,2,3,4]
->>=
: Run each of the following computations for each of the bound values. - Alternative: Like monoid
- MonadPlus: Like Monoid
zipList
- Applicative: Do the computation pairwise (triplewise, ...) - Monad: Does not exist
- Functor: Function composition,
fmap = (.)
. Picture a functiona -> b
as a collection ofb
indexed bya
. Feeding ana
to the function looks up the correspondingb
.fmap
thus modifies the contents of the container. - Applicative
- "Value contained" is the action applied to the environment.
- Functions are related to SKI calculus,
K = pure
,S = (<*>)
- Monad
-
join
:join f x = f x x
- Value extracted and piped on by>>=
: LHS applied to the environment
- Functor: modify result of a computation
- Monad:
>>=
passes on the value generated by the LHS
Like IO, but with a function to extract a pure value.
Run and combine computations in an atomic environment, otherwise similar to IO.
- Alternative: Return the first action that finishes, retry if none does
- MonadPlus: Like Alternative