简体   繁体   English

Const Monoid的应用实现

[英]Applicative implementation of Const Monoid

instance Monoid m => Applicative (Const m) where
    pure _ = Const mempty
    Const f <*> Const v = Const (f `mappend` v)

I do not understand how can the definition of <*> type-check. 我不明白如何定义<*>类型检查。

On the left side f is constrained by the signature of <*> as in the definition of Applicative 在左侧, f<*>的签名约束,如在Applicative的定义中那样

class Functor f => Applicative f where
    pure :: a -> f a
    (<*>) :: f (a -> b) -> f a -> f b

After changing the names to the current situation: 将名称更改为当前情况后:

    (<*>) :: c (m -> b) -> c m -> c b

=> f :: m -> * . => f :: m -> *

On the left side f is the [first] parameter of mappend . 在左侧fmappend的[first]参数。

From the definition of Monoid 从Monoid的定义

class Monoid a where
        mempty  :: a
        -- ^ Identity of 'mappend'
        mappend :: a -> a -> a

After changing the names to the current situation: 将名称更改为当前情况后:

        mappend :: m -> m -> m

=> f :: m . => f :: m

After changing the names to the current situation: 将名称更改为当前情况后:

 (<*>) :: c (m -> b) -> cm -> cb 

=> f :: m -> * . => f :: m -> *

Not quite. 不完全的。 After changing the names to the current situation: 将名称更改为当前情况后:

(<*>) :: Const m (a -> b) -> Const m a -> Const m b

Since a value of type Const xy is the constructor Const applied to a value of type x , this means f :: m (and v :: m ), and we know Monoid m from the instance context. 由于Const xy类型的值是应用于类型x的值的构造函数Const ,这意味着f :: m (和v :: m ),并且我们知道来自实例上下文的Monoid m

(<*>) :: f         (a->b) -> f         a -> f         b
      ≡  (Const m) (a->b) -> (Const m) a -> (Const m) b
      ≡  Const m (a->b) -> Const m a -> Const m b
      ≅        m        ->       m   ->       m

which is the signature of <> (aka mappend ). 这是<> (又名mappend )的签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM