相关疑难解决方法(0)

在Haskell中链接/组合类型类

假设我有两个类型定义如下,功能相同但名称不同:

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的实例,或者每个类的实例是否必须明确写入?任何见解都将非常感谢,谢谢!

monads haskell typeclass

10
推荐指数
3
解决办法
860
查看次数

Haskell子类型类需要UndecidableInstances?

请考虑以下代码示例:

{-# 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)

haskell typeclass

9
推荐指数
1
解决办法
244
查看次数

Data.Vector.Binary重叠Binary [a]实例

在我的应用程序中,我需要序列化包含任意数据类型的向量,在这种情况下是双打列表.为了序列化我正在导入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的实例吗?我查看了文档,但找不到这样的实例.

我该怎么做才能序列化这个结构?

编辑:

我正在使用以下软件包版本:

  • 矢量0.6.0.2
  • 矢量二进制实例-0.1.2
  • 二进制0.5.0.2

这里还有一个显示问题的片段,这次是一个字符列表:

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)

serialization haskell vector instances overlapping

7
推荐指数
1
解决办法
392
查看次数

为什么在Haskell中不允许这个声明?

我知道FunctorApplicative应该是超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

5
推荐指数
1
解决办法
338
查看次数

有趣的类型!解决多个实例声明

我正在尝试编写一些Haskell代码,其中有多种数据类型,每种类型都可以有多个实现.为此,我将每个数据类型定义class为其方法是相关的构造函数和选择器,然后根据给定的构造函数和选择器对该类的成员实现所有操作.

例如,也许A是多项式类(方法getCoefficientsmakePolynomial),其可以具有表示作为SparsePolyDensePolyB是复数类(方法getReal,getImag并且makeComplex其可被表示为一个)ComplexCartesian或一个ComplexPolar.

我在下面复制了一个最小的例子.我有两个类A,B每个类都有一个实现.我想将这两个类的所有实例都Num自动化为实例(这需要FlexibleInstancesUndecidableInstances类型扩展).这个工作正常,当我只有一个AB,但当我尝试与两者编译时,我得到以下错误:

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)

haskell types type-extension

5
推荐指数
2
解决办法
3246
查看次数

在Haskell中包含具有默认实现的类型类

考虑以下定义:

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]"

(无论我尝试什么,编译器都给我"非法实例声明""约束不小于实例头")

顺便说一句,我可以通过其他方式定义我的FooBar类,如果这会有所帮助.

haskell typeclass

4
推荐指数
1
解决办法
1844
查看次数