【发布时间】:2017-08-31 18:14:08
【问题描述】:
-
我试图回答这个问题: "给定代数数据类型
data Maybe a = Nothing | Just a选择正确的实例声明,表明类型构造函数
Maybe是Monad。”(摘自此处:“DelftX: FP101x Introduction to Functional Programming”。 -
我试图回答它的方法是依次编译每个可能的答案,例如,这个:
instance Monad Maybe where return x = Just x Nothing >>= _ = Nothing (Just x ) >>= f = f x -
我无法编译它,因为它已经在前奏中定义了。
HwEx9.hs:16:10: error: Duplicate instance declarations: instance Monad Maybe -- Defined at HwEx9.hs:16:10 instance Monad Maybe -- Defined in `GHC.Base'
我的问题是:如何编译它?
【问题讨论】:
-
最简单的方法:定义自己的
Maybe-like 类型。 -
没有办法避免为给定类型导入类型类实例。 (进一步查看stackoverflow.com/a/8731340/6476589)
-
改成MyMaybe?
标签: haskell duplicates instance monads redefinition