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
Justvalues;Nothingis smaller than anyJust. - Monoid: Ignore
Nothing,mappendJustcontents - Functor: Map over
Justcontents, do nothing forNothing - Applicative: "Value contained": values in
Just. If there is aNothing, the entire result is. - Monad
-
join: flattenMaybe (Maybe a).Nothingif there's aNothing.join (Just (Just 1)) = Just 1->>=: ExtractJustcontents and pass them on; if aNothingis encountered, the overall result isNothing - Alternative: Ignore
Nothing, return firstJustvalue - MonadPlus: Like Alternative
First- Monoid: Like AlternativeLast- Monoid: "Reversed Alternative": return lastJustvalue
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 -> bas a collection ofbindexed bya. Feeding anato the function looks up the correspondingb.fmapthus 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