I don’t understand what “lifting” is. Should I first understand monads before understanding what a “lift” is? (I’m completely ignorant about monads, too 🙂 Or can someone explain it to me with simple words?
I don’t understand what lifting is. Should I first understand monads before understanding what
Share
Lifting is more of a design pattern than a mathematical concept (although I expect someone around here will now refute me by showing how lifts are a category or something).
Typically you have some data type with a parameter. Something like
Suppose you find that a lot of uses of
Footake numeric types (Int,Doubleetc) and you keep having to write code that unwraps these numbers, adds or multiplies them, and then wraps them back up. You can short-circuit this by writing the unwrap-and-wrap code once. This function is traditionally called a "lift" because it looks like this:In other words you have a function which takes a two-argument function (such as the
(+)operator) and turns it into the equivalent function for Foos.So now you can write
Edit: more information
You can of course have
liftFoo3,liftFoo4and so on. However this is often not necessary.Start with the observation
But that is exactly the same as
fmap. So rather thanliftFoo1you would writeIf you really want complete regularity you can then say
If you can make
Foointo a functor, perhaps you can make it an applicative functor. In fact, if you can writeliftFoo2then the applicative instance looks like this:The
(<*>)operator for Foo has the typeIt applies the wrapped function to the wrapped value. So if you can implement
liftFoo2then you can write this in terms of it. Or you can implement it directly and not bother withliftFoo2, because theControl.Applicativemodule includesand likewise there are
liftAandliftA3. But you don’t actually use them very often because there is another operatorThis lets you write:
The term
myFunction <$> arg1returns a new function wrapped in Foo:This in turn can be applied to the next argument using
(<*>), and so on. So now instead of having a lift function for every arity, you just have a daisy chain of applicatives, like this: