简体   繁体   English

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

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

Given this type: 给定此类型:

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

What would be the Applicative definition. 适用的定义是什么。 I'm not able to get the pure right without adding a constraint on a like for example Num a 我不能够得到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)

You can't have it without a constraint. 没有约束就不能拥有它。 You need to do something about a and to be able to you need to have operations for that type, which you can get two ways: either use a specific type (eg, Int , or an abstract one, requiring a typeclass instance). 您需要对a做点事情,并且需要能够对该类型进行操作,您可以通过两种方式:使用特定类型(例如, Int或抽象类型,需要一个typeclass实例)。

The most general constraint that comes to mind in such a case is Monoid , which renders your instance the same as for the 2-element tuple: 在这种情况下,想到的最一般的约束是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