haskell - Const Monoid 的应用实现

标签 haskell applicative monoids

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 -> * .

在左侧fmappend的[第一个]参数.

来自幺半群的定义

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/

相关文章:

haskell - 应用仿函数作为幺半群仿函数

scala - Monoid 类型注释

haskell - Haskell Monad 法则如何从 Monoid 法则推导出来?

haskell - 为什么实例只匹配头部?

monads - 为什么验证会违反 monad 法则?

haskell - 在计算嵌套深度的方法中使用可重叠的捕获所有实例是否安全?

haskell - 是否可以使 (a, a) 成为 Haskell 中类型类的成员

haskell - 实际使用中邻接是什么意思?

haskell - 仅使用一元绑定(bind)语法表达 do block

parsing - 避免具有许多类似构造函数的数据类型的代码重复