8

Given an applicative functor f, I had an idea of making a new applicative functor Rev f like f but with the order of effects reversed. Here it is:

import Control.Applicative

newtype Rev f a = Rev {unRev :: f a}

instance Functor f => Functor (Rev f) where
  fmap f (Rev fx) = Rev (fmap f fx)

instance Applicative f => Applicative (Rev f) where
  pure x = Rev (pure x)
  (Rev ff) <*> (Rev fx) = Rev (pure (\x f -> f x) <*> fx <*> ff)

My questions are

  1. Is that a valid Applicative instance (does it obey the Applicative laws)?
  2. Does this construction have a name? Is there a module somewhere this is hiding in?
Cactus
  • 27,075
  • 9
  • 69
  • 149
PyRulez
  • 10,513
  • 10
  • 42
  • 87
  • 1
    If you're interested in `Backwards` applicative functors, you may also like `Data.Functor.Reverse`, which folds and traverses backwards. You may also find the `tardis` state transformer interesting. – dfeuer Dec 13 '15 at 03:51

1 Answers1

12

The friendly folks on IRC pointed to the Backwards applicative offered by the transformers package. You may also like the (<**>) operator available in the standard library.

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380