import Control.Applicative -- Applicative functor with nothing but threaded state. 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 <*>) makeCount x = Thread (\n -> n + 1) x -- Probably makes more sense to start with the pure combination -- approach to Applicatives (Monoidal) -- unit :: f () -- <,> :: f a -> f b -> f (a,b)