假设我有两个类型定义如下,功能相同但名称不同:
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
return :: a -> m a
class PhantomMonad p where
pbind :: p a -> (a -> p b) -> p b
preturn :: a -> p a
Run Code Online (Sandbox Code Playgroud)
有没有办法将这两个类绑定在一起,因此作为PhantomMonad实例的东西将自动成为Monad的实例,或者每个类的实例是否必须明确写入?任何见解都将非常感谢,谢谢!
请考虑以下代码示例:
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-} -- Is there a way to avoid this?
-- A generic class with a generic function.
class Foo a where
foo :: a -> a
-- A specific class with specific functions.
class Bar a where
bar :: a -> a
baz :: a -> a
-- Given the specific class functions, we can implement the generic class function.
instance Bar a => Foo a where
foo = bar . baz …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我需要序列化包含任意数据类型的向量,在这种情况下是双打列表.为了序列化我正在导入Data.Vector.Binary的向量.
在GHCi中加载模块时,会出现以下错误:
Overlapping instances for Binary [Double]
arising from a use of `decode' at Statistics.hs:57:33-42
Matching instances:
instance (Data.Vector.Generic.Base.Vector v a, Binary a) =>
Binary (v a)
-- Defined in Data.Vector.Binary
instance (Binary a) => Binary [a] -- Defined in Data.Binary
Run Code Online (Sandbox Code Playgroud)
该列表是Vector的实例吗?我查看了文档,但找不到这样的实例.
我该怎么做才能序列化这个结构?
编辑:
我正在使用以下软件包版本:
这里还有一个显示问题的片段,这次是一个字符列表:
import Data.Binary
import Data.Vector.Binary
import qualified Data.ByteString.Lazy as L
main = L.writeFile "/tmp/aaa" $ encode "hello"
Run Code Online (Sandbox Code Playgroud) 我知道Functor和Applicative应该是超Monad,但不是由于历史的原因.但是,为什么不能声明Monad一个实例Functor呢?这将产生大致相同的效果,但无需修改现有代码.如果你想这样做,GHC抱怨:
instance Functor Monad where
fmap = liftM
Class `Monad' used as a type
In the instance declaration for `Functor Monad'
Run Code Online (Sandbox Code Playgroud)
这是为什么?这可能是一个很好的理由.
我正在尝试编写一些Haskell代码,其中有多种数据类型,每种类型都可以有多个实现.为此,我将每个数据类型定义class为其方法是相关的构造函数和选择器,然后根据给定的构造函数和选择器对该类的成员实现所有操作.
例如,也许A是多项式类(方法getCoefficients和makePolynomial),其可以具有表示作为SparsePoly或DensePoly和B是复数类(方法getReal,getImag并且makeComplex其可被表示为一个)ComplexCartesian或一个ComplexPolar.
我在下面复制了一个最小的例子.我有两个类A,B每个类都有一个实现.我想将这两个类的所有实例都Num自动化为实例(这需要FlexibleInstances和UndecidableInstances类型扩展).这个工作正常,当我只有一个A或B,但当我尝试与两者编译时,我得到以下错误:
Duplicate instance declarations:
instance [overlap ok] (A a, Num x, Show (a x), Eq (a x)) =>
Num (a x)
-- Defined at test.hs:13:10-56
instance [overlap ok] (B b, Num x, Show (b x), Eq (b x)) =>
Num …Run Code Online (Sandbox Code Playgroud) 考虑以下定义:
class Foo a where
foo :: a -> Int
class Bar a where
bar :: a -> [Int]
Run Code Online (Sandbox Code Playgroud)
现在,我怎么说在Haskell中"每个Foo都是a Bar,bar默认定义为bar x = [foo x]"?
(无论我尝试什么,编译器都给我"非法实例声明"或"约束不小于实例头")
顺便说一句,我可以通过其他方式定义我的Foo和Bar类,如果这会有所帮助.