繁体   English   中英

数据2 ab = 2 ab的应用程序纯函数是什么

[英]What is the Applicative's pure for Data Two a b = Two a b

给定此类型:

data Two a b = Two a b deriving (Eq, Show)

适用的定义是什么。 我不能够得到pure权利,没有上添加约束a例如像Num a

instance (Num a, Monoid a) => Applicative (Two a) where
  pure b = Two 1 b
  Two f f' <*> Two a b = Two a (f' b)

没有约束就不能拥有它。 您需要对a做点事情,并且需要能够对该类型进行操作,您可以通过两种方式:使用特定类型(例如, Int或抽象类型,需要一个typeclass实例)。

在这种情况下,想到的最一般的约束是Monoid ,它使您的实例与2元素元组相同:

instance Monoid a => Applicative ((,) a) where
    pure x = (mempty, x)
    (u, f) <*> (v, x) = (u `mappend` v, f x)

暂无
暂无

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

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