My "solution" is a lot less interesting

lift1 = lift
lift2 = lift . lift1
lift3 = lift . lift2
lift4 = lift . lift3
lift5 = lift . lift4

What you want seemingly requires dependent types, because the result type depends on the Int that you pass in. Haskell is not well equipped to handle this, though you could probably whip up some Template Haskell to get the job done for cases where the Int can be determined at compile time.


-- Dan Burton


On Wed, Oct 16, 2013 at 2:06 PM, Wvv <vitea3v@rambler.ru> wrote:
I try to write a function "liftN", but I'm not satisfied with result.

lift :: (MonadTrans t) => Monad m => m a -> t m a

liftN n
š š | n < 1 š š š = error "liftN: n<1"
š š | n == 1 š š = lift
š š | otherwise = lift . (liftN (n-1))

1) I know(?), that it is impossible to write liftN now: typechecker can't
decide which signature it is.
2) Main reason is finite recursive constraint

3) Let's think, it is possible to write
Main candidates: promoted data and data-kinds

For example,
lift . lift . lift
š :: (Monad (t1 (t2 m)), Monad (t2 m), Monad m, MonadTrans t,
š š š MonadTrans t1, MonadTrans t2) =>
š š šm a -> t (t1 (t2 m)) a

My best solution looks like:

data MT = M | T Int MT
liftN :: forall tm. ( forall x n.
š š š š š š š š š šMonad 'M, Monad (x :: MT),
š š š š š š š š š šMonadTrans ('T n),
š š š š š) => Int -> 'M a -> š('T 0) (tm :: MT) a
liftN n
š š š| n < 1 š š = error "liftN: šn < 1"
š š š| n == 1 š š= lift
š š š| otherwise = lift . (liftN (n-1))


Am I miss something?
I think this function could look much prettier.
But I don't know how.



--
View this message in context: http://haskell.1045720.n5.nabble.com/liftN-tp5738612.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe