Note that there are some explanatory texts on larger screens.

plurals
  1. POCan liftM differ from liftA?
    primarykey
    data
    text
    <p>According to <a href="http://www.haskell.org/wikiupload/8/85/TMR-Issue13.pdf" rel="noreferrer">the Typeclassopedia</a> (among other sources), <code>Applicative</code> logically belongs between <code>Monad</code> and <code>Pointed</code> (and thus <code>Functor</code>) in the type class hierarchy, so we would ideally have something like this if the Haskell prelude were written today:</p> <pre><code>class Functor f where fmap :: (a -&gt; b) -&gt; f a -&gt; f b class Functor f =&gt; Pointed f where pure :: a -&gt; f a class Pointed f =&gt; Applicative f where (&lt;*&gt;) :: f (a -&gt; b) -&gt; f a -&gt; f b class Applicative m =&gt; Monad m where -- either the traditional bind operation (&gt;&gt;=) :: (m a) -&gt; (a -&gt; m b) -&gt; m b -- or the join operation, which together with fmap is enough join :: m (m a) -&gt; m a -- or both with mutual default definitions f &gt;&gt;= x = join ((fmap f) x) join x = x &gt;&gt;= id -- with return replaced by the inherited pure -- ignoring fail for the purposes of discussion </code></pre> <p>(Where those default definitions were re-typed by me from the <a href="http://en.wikipedia.org/wiki/Monad_%28functional_programming%29#fmap_and_join" rel="noreferrer">explanation at Wikipedia</a>, errors being my own, but if there are errors it is at least in principle possible.)</p> <p>As the libraries are currently defined, we have:</p> <pre><code>liftA :: (Applicative f) =&gt; (a -&gt; b) -&gt; f a -&gt; f b liftM :: (Monad m) =&gt; (a -&gt; b) -&gt; m a -&gt; m b </code></pre> <p>and:</p> <pre><code>(&lt;*&gt;) :: (Applicative f) =&gt; f (a -&gt; b) -&gt; f a -&gt; f b ap :: (Monad m) =&gt; m (a -&gt; b) -&gt; m a -&gt; m b </code></pre> <p>Note the similarity between these types within each pair.</p> <p>My question is: are <code>liftM</code> (as distinct from <code>liftA</code>) and <code>ap</code> (as distinct from <code>&lt;*&gt;</code>), simply a result of the historical reality that <code>Monad</code> wasn't designed with <code>Pointed</code> and <code>Applicative</code> in mind? Or are they in some other behavioral way (potentially, for some legal <code>Monad</code> definitions) distinct from the versions that only require an <code>Applicative</code> context?</p> <p>If they are distinct, could you provide a simple set of definitions (obeying the laws required of <code>Monad</code>, <code>Applicative</code>, <code>Pointed</code>, and <code>Functor</code> definitions described in the Typeclassopedia and elsewhere but not enforced by the type system) for which <code>liftA</code> and <code>liftM</code> behave differently?</p> <p>Alternatively, if they are not distinct, could you prove their equivalence using those same laws as premises?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload