Search code examples
haskellmonadsinfix-notation

Automatic lifting of infix operators to monadic infix operators


One of the nice things about Haskell is the ability to use infix notation.

1 : 2 : 3 : []    :: Num a => [a]
2 + 4 * 3 + 5     :: Num a => a

But this power is suddenly and sadly lost when the operator needs to be lifted.

liftM2 (*) (liftM2 (+) m2 m4) (liftM2 (+) m3 m5)
liftM2 (:) m1 (liftM2 (:) m2 (liftM2 (:) m3 mE))

It is possible to define similar operators in order to regain this power

(.*) = liftM2 (*)
(.+) = liftM2 (+)
(.:) = liftM2 (:)

m1, m2, m3, m4, m5 :: Monad m, Num a => m a
mE = return []     :: Monad m => m [a]
m1 .: m2 .: m3 .: mE    :: Monad m, Num a => m [a]
m2 .+ m4 .* m3 .+ m5    :: Monad m, Num a => m a

But it is tedious to need to rename every operator I want to use in a monadic context. Is there a better way? Template Haskell, perhaps?


Solution

  • You can define a new infix lift:

    v <. f = liftM2 f v
    f .> v = f v
    

    Example use:

    [3] <.(+).> [4]
    

    ...but I don't know of any real way that isn't 100% annoying.