[<<][compsci][>>][..]

Sun Apr 8 11:18:39 CEST 2012

## Experimenting with Applicative

EDIT: Probably mostly meaningless..
Let's play with this a bit. Take the Functor (s ->). A functor value
F o is then (s -> o), i.e. an output where state is abstracted. The
<*> for the Applicative version of this would chain together
(s -> (i -> o))
(s -> i)
to
(s -> o)
This is actually the environment variable from [1]. Can this
implement state machines? Doesn't look like it because 's' can't
depend on the valueo of the i->o or i types, because those types are
completely general (container!) and thus not accessible to the
implementation of <*>. What could happen though is a fixed update
s->s that's applied on every <*>, i.e. the increment of a counter.
Let's construct an example of "just" threading state.
import Control.Applicative
data Thread s a = Thread (s -> s) a
runThread (Thread inc a) = (a, inc 0)
appThread :: Thread s (a -> b) -> Thread s a -> Thread s b
appThread (Thread cf f) (Thread ca a) = Thread cb b where
b = f a
cb = ca . cf
instance Applicative (Thread s) where
(<*>) = appThread
pure a = Thread (\n -> n) a
instance Functor (Thread s) where
fmap f = (pure f <*>)
An example of this would be a counter, wich counts the number of
elements used in a computation.
makeCount x = Thread (\n -> n + 1) x
[1] http://www.soi.city.ac.uk/~ross/papers/Applicative.pdf

[Reply][About]

[<<][compsci][>>][..]