如何为具有三个参数的数据类型编写应用实例?

问题描述

我试图为两种数据类型编写一些实例。 (ThreeThree')但是 我很难实现 Applicative (和 Monad )实例。 在一点帮助下,我想我可以解决问题。

数据类型如下。

data Three a b c = T a b c  deriving (Eq,Show)
data Three' a b  = T' a b b deriving (Eq,Show)

我的代码如下。第一个用于数据类型Three。我认为除了Monad实例之外,其他一切都很好。

data Three a b c = Three a b c deriving (Show,Eq)

-- Semigroup instance
instance (Semigroup a,Semigroup b,Semigroup c) => Semigroup (Three a b c) where
  (Three a b c) <> (Three a' b' c') = Three (a <> a') (b <> b') (c <> c')

-- Monoid instance
instance (Monoid a,Monoid b,Monoid c) => Monoid (Three a b c) where
  mempty = Three mempty mempty mempty 

-- Functor instance
instance Functor (Three a b) where
  fmap f (Three a b c) = Three a b (f c) 

-- Applicative instance
instance (Monoid a,Monoid b) => Applicative (Three a b) where
  pure = Three mempty mempty 
  (Three a b f) <*> (Three a' b' c') = Three (a <> a') (b <> b') (f c')

-- Monad instance (<----- Here! Difficult!)

-- Foldable instance
instance Foldable (Three a b) where
  foldMap f (Three a b c) = f c

-- Arbitrary instance
instance (Arbitrary a,Arbitrary b,Arbitrary c) => Arbitrary (Three a b c) where
  arbitrary = Three <$> arbitrary <*> arbitrary <*> arbitrary

-- EqProp instance
instance (Eq a,Eq b,Eq c) => EqProp (Three a b c) where
  (=-=) = eq

-- Test
test_Three = do 
  let trigger = undefined :: Three (Sum Int,String,Sum Int) (Sum Int,Sum Int)
  quickBatch $ monoid trigger
  quickBatch $ functor trigger
  quickBatch $ applicative trigger
  -- quickBatch $ monad trigger    -- Monad instance was not yet implemented.

第二个用于数据类型Three'。我无法在此处实现Applicative和Monad实例。争论的数目使我感到困惑。如何在此处为Three' datatytpe实现Applicative和Monad实例?

data Three' a b = Three' a b b deriving (Show,Semigroup b) => Semigroup (Three' a b) where
  Three' a b c <> Three' a' b' c' = Three' (a <> a') (b <> b') (c <> c')
  -- Three' a b b <> Three' a' b' b' = Three'  (a <> a') (b <> b') (b <> b')  -- TODO: Error

-- Monoid instance 
instance (Monoid a,Monoid b) => Monoid (Three' a b) where
  mempty = Three' mempty mempty mempty

-- Functor instance
instance Functor (Three' a) where
  fmap f (Three' a b c) = Three' a (f b) (f c)

-- Applicative instance (<-------HERE! Difficult.)
{-
-- the first trial - incorrect
instance (Monoid a) => Applicative (Three' a) where
  pure = Three' mempty mempty 
  (Three' a f g) <*> (Three' a' b' c') = Three' (a <> a') (f b') (g c')
-}

{-
-- the second trial - incorrect
instance (Monoid a) => Applicative (Three' a) where
  pure = Three' mempty mempty 
  (Three' a b f) <*> (Three' a' b' c') = Three' (a <> a') (b <> b') (f c')
-}

-- Monad instance (<-------HERE! Difficult.)

-- Foldable instance
instance Foldable (Three' a) where
  foldMap f (Three' a b b') = f b `mappend` f b'

-- Arbitrary instance
instance (Arbitrary a,Arbitrary b) => Arbitrary (Three' a b) where
  arbitrary = Three' <$> arbitrary <*> arbitrary <*> arbitrary

-- EqProp instance
instance (Eq a,Eq b) => EqProp (Three' a b) where (=-=) = eq

-- Test
test_Three' = do 
  let trigger = undefined :: Three' (Sum Int,Sum Int) 
  quickBatch $ monoid trigger
  quickBatch $ functor trigger

非常感谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...