Rename mapM to liftM

Aug 23, 2010 at 10:10 PM

I suggest renaming mapM to liftM to match Haskell naming conventions: I’d also like to introduce  liftM2:

    //  Monad m /* m1 m2 */ => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
    let inline liftM2 m1 m2 f m_a1 m_a2 =
        let bind1 m_a1 f = (^m1: (member Bind: ^m_a1 -> (^a1 -> ^m_r) -> ^m_r) m1, m_a1, f)
        let unit2 x = (^m2: (member Return: ^b -> ^m_r) m2, x)
        let bind2 m_a2 f = (^m2: (member Bind: ^m_a2 -> (^a2 -> ^m_r) -> ^m_r) m2, m_a2, f)
        bind1 m_a1 (fun a1 -> bind2 m_a2 (fun a2 -> unit2 (f a1 a2)))
and generalized C# support:
    // Monad m => (a -> b) -> m a -> m b
    let inline select m f m_a = liftM m (applyFunc f) m_a
    // Monad m /* m1 m2 */ => (a1 -> m a2) -> (a1 -> a2 -> r) -> m r
    let inline selectMany m1 m2 selector projector m_a1 =
        let bind1 m_a1 f = (^m1: (member Bind: ^m_a1 -> (^a1 -> ^m_r) -> ^m_r) m1, m_a1, f)
        bind1 m_a1 (fun a -> let m_a2 = applyFunc selector a in
                             liftM2 m1 m2 (applyFunc2 projector) m_a1 m_a2)
extensions methods could be specialized like following:
    let inline Select (m, f) = option f m
    let inline SelectMany (m, s, p) = Combinators.selectMany option option s p m

(this one for the Option monad, not posted yet).

Ideas, conclusions, should I proceed with changes?

Aug 23, 2010 at 10:34 PM
Edited Aug 23, 2010 at 10:37 PM
A couple of comments:
I prefer to use a,b,c,d instead of a1,a2,a3:
liftM :: (Monad m) => (a -> b) -> (m a -> m b)
liftM2 :: (Monad m) => (a -> b -> c) -> (m a -> m b -> m c)
and can we avoid the underscores...something like ma instead of m_a....
Otherwise, looks good.

Aug 24, 2010 at 4:21 AM

I use numbers to correlate with workflow builders (m1, m2, bind1, bind2 - we need more than one due to rank restriction). But I not insist on it, I will use a, b, c if you want. Workflow builder names will remain with numbers (m1, m2) because ma, mb will stand for monadic values.


Aug 24, 2010 at 4:45 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Aug 24, 2010 at 7:13 AM

ok, go ahead with the changes.