A cheat-sheet for Haskell & PureScript typeclasses
One-page overview of a bunch of different Haskell & PureScript typeclasses. Starting with the PureScript Class Hierarchy diagram, and similar on the Haskell Typeclassopedia. Very much a work in progress: info and terminology may be completely wrong!
Map a value inside a context to a new value:
class Functor f where fmap :: (a -> b) -> f a -> f b
With a function (or, more intuitively, a partial computation) inside a context, map a value also inside the same context:
class Functor f => Applicative f where (<*>) :: f (a -> b) -> f a -> f b
Also provide a way to lift a value into a context:
pure :: a -> f a
For instance, how could we sum
Just 3? 1
> :t fmap (+) fmap (+) :: (Functor f, Num a) => f a -> f (a -> a) > :t fmap (+) (Just 2) fmap (+) (Just 2) :: Num a => Maybe (a -> a)
Ah, now we can use
fmap (+) (Just 2) <*> (Just 3)
So when you have a function
a -> b -> c, partially apply it, and
lift into the functor, you end up with
f (b -> c), then to apply
it to a second parameter of type
b, you use
<*>, and end up with
f c. Thus:
<*>is just function application within a computational context.2
Use the result of a previous computation as input into another computation, i.e. sequence computations.
One consequence is that a sequence may not preserve the structure of
an input. An example: when under
Maybe, functors and
applicatives can’t change the result from
Just a to
since none of the parameters return
f b, they only return
need to “upgrade” to a monad to be able to use a function like
S with a binary operation
<> that combines two elements of
S into another element of
class Semigroup a where (<>) :: a -> a -> a
<> must be associative.
Extension of a Semigroup with an identity element
i, such that
<> i == i <> s == s.
Set of objects with composable morphisms. E.g. like a category, but without requiring an identity element.
class Semigroupoid a where compose :: forall b c d. a c d -> a b c -> a b d
(->) is a semigroupoid, with
(.) :: (b -> c) ->
(a -> b) -> (a -> c).
Semigroupoid with an identity element.
To be less facetious, a category is a set of objects, composable
morphisms, and an identity element, such that
id <<< p = p <<< id = p
A Group is a Monoid with inverses. Numbers form a group with
as the inverse of a value
a + -a = zero
types that support addition and multiplication.
Semiring that also supports subtraction.
a value of type
b `arr` ccan be thought of as a computation which takes values of type
bas input, and produces values of type