What is “lifting” in Haskell?

后端 未结 5 606
南方客
南方客 2020-12-22 16:07

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 some

5条回答
  •  有刺的猬
    2020-12-22 16:33

    Paul's and yairchu's are both good explanations.

    I'd like to add that the function being lifted can have an arbitrary number of arguments and that they don't have to be of the same type. For example, you could also define a liftFoo1:

    liftFoo1 :: (a -> b) -> Foo a -> Foo b
    

    In general, the lifting of functions that take 1 argument is captured in the type class Functor, and the lifting operation is called fmap:

    fmap :: Functor f => (a -> b) -> f a -> f b
    

    Note the similarity with liftFoo1's type. In fact, if you have liftFoo1, you can make Foo an instance of Functor:

    instance Functor Foo where
      fmap = liftFoo1
    

    Furthermore, the generalization of lifting to an arbitrary number of arguments is called applicative style. Don't bother diving into this until you grasp the lifting of functions with a fixed number of arguments. But when you do, Learn you a Haskell has a good chapter on this. The Typeclassopedia is another good document that describes Functor and Applicative (as well as other type classes; scroll down to the right chapter in that document).

    Hope this helps!

提交回复
热议问题