2012-12-24 22 views

回答

19

Lift是一個相對較新的貢獻:

data Lift f a = Pure a | Other (f a) 

也就是說,給出一個仿函數f,您可以用純值構成f得到一個新的仿函數。

包本身給出了一個例子:

-- | An applicative functor that collects a monoid (e.g. lists) of errors. 
-- A sequence of computations fails if any of its components do, but 
-- unlike monads made with 'ErrorT' from "Control.Monad.Trans.Error", 
-- these computations continue after an error, collecting all the errors. 
type Errors e = Lift (Constant e) 

-- | Report an error. 
failure :: Monoid e => e -> Errors e a 
failure e = Other (Constant e) 

我不知道這在任何最百搭的用途,但是。

+2

它讓我想起將一個半羣延伸成一個幺半羣:它將一個應用函子但沒有純粹擴展到一個應用函數中。這在兩種情況下都是混淆的,因爲標準庫中不存在半羣/ applicative-functors-but-without-pure。 – dave4420

+14

如果你有任何可應用的同態'fug :: fu - > gu',那麼'(f:+:g)'(其中':+:'是逐點和)可以通過「保持左邊,除非你混淆有向右走「。提升是特殊情況,其中'f'是最初的應用,即身份,與其他人有獨特的同態。 – pigworker

+1

這是對@ pigworker的評論的一個公平的解析 - 但是在閱讀http://comonad.com/reader/2012/abstracting-with-applicatives/comment-page-1/#comment-107128後,我終於理解了這一點(和立即使用它!)。謝謝! – ocharles

相關問題