Monoidal函子是Applicative的,但是Monoid类型类在Applicative的定义中在哪里? [英] Monoidal Functor is Applicative but where is the Monoid typeclass in the definition of Applicative?

查看:78
本文介绍了Monoidal函子是Applicative的,但是Monoid类型类在Applicative的定义中在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Applicative是Monoidal函子:

Applicative is a Monoidal Functor :

mappend :: f         -> f   -> f
$       ::  (a -> b) ->   a ->   b
<*>     :: f(a -> b) -> f a -> f b

但是在Applicative类型类的定义中我没有看到任何有关Monoid的参考,您能告诉我为什么吗?

But I don't see any reference about Monoid in the definition of the Applicative typeclass, could you tell me why ?

定义:

class Functor f => Applicative (f :: * -> *) where
  pure :: a -> f a
  (<*>) :: f (a -> b) -> f a -> f b
  GHC.Base.liftA2 :: (a -> b -> c) -> f a -> f b -> f c
  (*>) :: f a -> f b -> f b
  (<*) :: f a -> f b -> f a
  {-# MINIMAL pure, ((<*>) | liftA2) #-}

此定义中未提及该结构Monoid,但在您这样做时

No mention of that structural Monoid is provided in this definition, but when you do

> ("ab",(+1)) <*> ("cd", 5) 
>("abcd", 6)

在实现此Applicative实例时,您可以清楚地看到结构Monoid(,)String"的使用.

You can clearly see the use of a Structural Monoid "(,) String" when implementing this instance of Applicative.

另一个显示使用结构Monoid"的示例:

Another example to show that a "Structural Monoid" is used :

Prelude Data.Monoid> (2::Integer,(+1)) <*> (1::Integer,5)

<interactive>:35:1: error:
    • Could not deduce (Monoid Integer) arising from a use of ‘<*>’
      from the context: Num b
        bound by the inferred type of it :: Num b => (Integer, b)
        at <interactive>:35:1-36
    • In the expression: (2 :: Integer, (+ 1)) <*> (1 :: Integer, 5)
      In an equation for ‘it’:
          it = (2 :: Integer, (+ 1)) <*> (1 :: Integer, 5)

推荐答案

被称为"monoidal functor"的monoid不是Monoid monoid,即值级monoid.相反,它是一个 type-level monoid .即无聊的产品monoid

The monoid that's referred to with "monoidal functor" is not a Monoid monoid, i.e. a value-level monoid. It's a type-level monoid instead. Namely, the boring product monoid

type Mempty = ()
type a <> b = (a,b)

(您可能会注意到,这并不是严格意义上的等分体;只有将((a,b),c)(a,(b,c))视为同一类型时,它们才是同构的.)

(You may notice that this is not strictly speaking a monoid; it's only if you consider ((a,b),c) and (a,(b,c)) as the same type. They are sure enough isomorphic.)

要查看与Applicative的关系,请分别.单项仿函数,我们需要用其他术语来编写类.

To see what this has to do with Applicative, resp. monoidal functors, we need to write the class in other terms.

class Functor f => Monoidal f where
  pureUnit :: f Mempty
  fzip :: f a -> f b -> f (a<>b)

-- an even more "general nonsense", equivalent formulation is
-- upure :: Mempty -> f Mempty
-- fzipt :: (f a<>f b) -> f (a<>b)
-- i.e. the functor maps a monoid to a monoid (in this case the same monoid).
-- That's really the mathematical idea behind this all.

IOW

class Functor f => Monoidal f where
  pureUnit :: f ()
  fzip :: f a -> f b -> f (a,b)

根据Monoidal定义标准Applicative类的通用实例是一个简单的练习,反之亦然.

It's a simple exercise to define a generic instance of the standard Applicative class in terms of Monoidal, vice versa.

关于("ab",(+1)) <*> ("cd", 5):通常,这与Applicative并没有多大关系,而仅与专门适用于作者的作者有关.实例是

Regarding ("ab",(+1)) <*> ("cd", 5): that doesn't have much to do with Applicative in general, but only with the writer applicative specifically. The instance is

instance Monoid a => Monoidal ((,) a) where
  pureUnit = (mempty, ())
  fzip (p,a) (q,b) = (p<>q, (a,b))

这篇关于Monoidal函子是Applicative的,但是Monoid类型类在Applicative的定义中在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆