【问题标题】:Applicative implementation of Const MonoidConst Monoid 的应用实现
【发布时间】:2015-11-23 17:58:14
【问题描述】:
instance Monoid m => Applicative (Const m) where
    pure _ = Const mempty
    Const f <*> Const v = Const (f `mappend` v)

我不明白&lt;*&gt;的定义怎么能类型检查。

左侧f&lt;*&gt;的签名约束,如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 -&gt; *.

左侧fmappend的[第一个]参数。

从 Monoid 的定义

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

改名后的现状:

        mappend :: m -> m -> m

=> f :: m.

【问题讨论】:

    标签: haskell applicative monoids


    【解决方案1】:
    (<*>) :: 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
    

    这是&lt;&gt;(又名mappend)的签名。

    【讨论】:

      【解决方案2】:

      改名后的现状:

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

      => f :: m -&gt; *.

      不完全是。改名后的现状:

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

      由于Const x y 类型的值是构造函数Const 应用于x 类型的值,这意味着f :: m(和v :: m),我们从实例上下文中知道Monoid m

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多