module Mem where newtype Mem s a = Mem { runMem :: s -> (a,s) } instance Monoid a => Monoid (Mem s a) where mempty = Mem $ \x -> (mempty, x) mappend (Mem f1) (Mem f2) = Mem f where f x = ((mappend (fst $ f1 x) (fst $ f2 x)), snd $ f2 $ snd $ f1 x)