import Data.Monoid
newtype Constant a b =
Constant { getConstant :: a }
deriving (Eq, Ord, Show)
instance Functor (Constant a) where
fmap _ (Constant a) = Constant a
instance (Monoid a) => Applicative (Constant a) where
pure x = Constant (mempty x)
_ <*> (Constant a) = Constant a
--:t (<*>)
--(<*>) :: Applicative f => f (a -> b) -> f a -> f b
--:t Constant (Sum 1)
--Constant (Sum 1) :: Num a => Constant (Sum a) b
--Constant (Sum 1) <*> Constant (Sum 2)
--Constant { getConstant = Sum { getSum = 2 }}
我的問題是爲什麼最後一個語句類型檢查?Haskell爲什麼這種類型檢查
我預期的< *>左側爲類型F的(A - > B)
凡
F =常數
(一 - > B)= 1總和?
沒有什麼我可以適用的(總和1),因爲它被完全應用,但這個語句編譯。
在你的定義'mempty x'的作品,雖然mempty'的'類型'是一個含半幺羣=>了' 。嘗試用'pure x = Constant(mempty x x x x x x 127「abc」()x x)'替換此定義。它工作嗎?爲什麼? ;) –