title: How to map over Applicative form?
body: <p>I want to map over Applicative form.</p>
  
  <p>The type of map-like function would be like below:</p>
  
  <pre><code>mapX :: (Applicative f) =&gt; (f a -&gt; f b) -&gt; f [a] -&gt; f [b]
  </code></pre>
  
  <p>used as:</p>
  
  <pre><code>result :: (Applicative f) =&gt; f [b]
  result = mapX f xs
    where f  :: f a -&gt; f b
          f = ...
          xs :: f[a]
          xs = ...
  </code></pre>
  
  <p>As the background of this post, I try to write fluid simulation program using Applicative style referring to Paul Haduk's "The Haskell School of Expression", and I want to express the simulation with Applicative style as below:</p>
  
  <pre><code>x, v, a :: Sim VArray
  x = x0 +: integral (v * dt)
  v = v0 +: integral (a * dt)
  a = (...calculate acceleration with x v...)
  
  instance Applicative Sim where
    ...
  </code></pre>
  
  <p>where Sim type means the process of simulation computation and VArray means Array of Vector (x,y,z). X, v a are the arrays of position, velocity and acceleration, respectively.</p>
  
  <p>Mapping over Applicative form comes when definining a.</p>
  
  <hr>
  
  <p>I've found one answer to my question.</p>
  
  <p>After all, my question is "How to lift high-order functions (like map
  :: (a -> b) -> [a] -> [b]) to the Applicative world?" and the answer
  I've found is "To build them using lifted first-order functions."</p>
  
  <p>For example, the "mapX" is defined with lifted first-order functions
  (headA, tailA, consA, nullA, condA) as below:</p>
  
  <pre><code>mapX :: (f a -&gt; f b) -&gt; f [a] -&gt; f [b]
  mapX f xs0 = condA (nullA xs0) (pure []) (consA (f x) (mapA f xs))
   where
     x = headA xs0
     xs = tailA xs0
  
  headA = liftA head
  
  tailA = liftA tail
  
  consA = liftA2 (:)
  
  nullA = liftA null
  
  condA b t e = liftA3 aux b t e
    where aux b t e = if b then t else e
  </code></pre>