instance Monoid m => Applicative (Const m) where
pure _ = Const mempty
Const f <*> Const v = Const (f `mappend` v)
我不明白<*>
怎么定义类型检查。
在左侧f
受到 <*>
签名的约束正如 Applicative 的定义
class Functor f => Applicative f where
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
改名后变成现在的情况:
(<*>) :: c (m -> b) -> c m -> c b
=> f :: m -> *
.
在左侧f
是mappend
的[第一个]参数.
来自幺半群的定义
class Monoid a where
mempty :: a
-- ^ Identity of 'mappend'
mappend :: a -> a -> a
改名后变成现在的情况:
mappend :: m -> m -> m
=> f :: m
.
最佳答案
After changing the names to the current situation:
(<*>) :: c (m -> b) -> c m -> c b
=>
f :: m -> *
.
不完全是。改名后变成现在的情况:
(<*>) :: Const m (a -> b) -> Const m a -> Const m b
由于 Const x y
类型的值是应用于 x
类型值的构造函数 Const
,这意味着 f : : m
(和 v::m
),并且我们从实例上下文中知道 Monoid m
。
关于haskell - Const Monoid 的应用实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33877487/