相关疑难解决方法(0)

Haskell Constraint不小于实例头

有些戒指可以配备标准功能:

class (Ring.C a) => EuclideanDomain a where
  norm :: a -> Integer
Run Code Online (Sandbox Code Playgroud)

使用此功能,可以以明显的方式订购戒指:

compare x y = compare (norm x) (norm y)
Run Code Online (Sandbox Code Playgroud)

但我不知道如何表明这一点.我试着这样做

instance (EuclideanDomain a, Eq a) => Ord a where
Run Code Online (Sandbox Code Playgroud)

但这给了我一些警告,当我启用相关的编译器标志时,它告诉我"约束不小于实例头" - 如果我启用UndecidableInstances,一切都会变成地狱.

有办法做我想要的吗?

haskell

31
推荐指数
2
解决办法
4091
查看次数

需要灵活的实例?

我想将Haskell类型的Convertible实例写入其C表示

它看起来像这样:

instance Convertible Variable (IO (Ptr ())) where
Run Code Online (Sandbox Code Playgroud)

现在GHC抱怨:

 Illegal instance declaration for `Convertible
                                    Variable (IO (Ptr ()))'
  (All instance types must be of the form (T a1 ... an)
   where a1 ... an are *distinct type variables*,
   and each type variable appears at most once in the instance head.
   Use -XFlexibleInstances if you want to disable this.)
In the instance declaration for `Convertible Variable (IO (Ptr ()))'
Run Code Online (Sandbox Code Playgroud)

如果你的实例声明中有自由类型,我认为需要灵活实例,但事实并非如此.我可以在添加正确的pragma时进行编译,但有人可以解释为什么我需要这个吗?

haskell typeclass

7
推荐指数
2
解决办法
4164
查看次数

标签 统计

haskell ×2

typeclass ×1