如何解决如何为具有三个参数的数据类型编写应用实例?
我试图为两种数据类型编写一些实例。 (Three
和Three'
)但是
我很难实现 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 举报,一经查实,本站将立刻删除。