因此,我有一对类型类,我将经常一起使用它们,我希望避免每次都指定这两个类。基本上,而不是把
:: (Ord a, Fractional a, Ord b, Fractional b, ... Ord z, Fractional z) =>在我所有类型规范的开头,我宁愿把
:: (OrdFractional a, OrdFractional b, ... OrdFractional z)因此,我最初的想法是声明一个新的类型类
module Example where
class (Fractional a, Ord a) => OrdFractional a
example :: (OrdFractional a, OrdFractional b) => (a,b) -> (a,b) -> (a,b) -> Bool
example (x1,y1) (x2,y2) (x3,y3) = (x1/x2 < x2/x3) && (y1/y2 < y2/y3)但这并没有像我希望的那样自动工作:
% ghci
Prelude> :l Example.hs
Ok, modules loaded: Example.
Prelude Example> example (1::Float,3::Float) (2,2) (3,1)
<interactive>:1:0:
No instance for (OrdFractional Float)
arising from a use of `example' at <interactive>:1:0-39
Possible fix:
add an instance declaration for (OrdFractional Float)
In the expression: example (1 :: Float, 3 :: Float) (2, 2) (3, 1)
In the definition of `it':
it = example (1 :: Float, 3 :: Float) (2, 2) (3, 1)手动创建实例看起来很拖拉,因此,接下来,我想我可能会尝试自动创建实例:
module Example where
class OrdFractional a
instance (Fractional a, Ord a) => OrdFractional a
example :: (OrdFractional a, OrdFractional b) => (a,b) -> (a,b) -> (a,b) -> Bool
example (x1,y1) (x2,y2) (x3,y3) = (x1/x2 < x2/x3) && (y1/y2 < y2/y3)但是编译器不喜欢这样:
ghc -c Example.hs
Example.hs:4:0:
Illegal instance declaration for `OrdFractional a'
(All instance types must be of the form (T a1 ... an)
where a1 ... an are 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 `OrdFractional a'那么有没有办法让我这么做呢?
发布于 2012-11-29 15:42:02
随着GHC7.4中引入的ConstraintKinds扩展,约束现在是种类Constraint的类型,因此您可以使用普通类型同义词来获得您想要的:
{-# LANGUAGE ConstraintKinds #-}
type OrdFractional a = (Ord a, Fractional a)发布于 2008-11-20 22:02:01
您需要的是一个类别名。有人提议将其添加到http://repetae.net/recent/out/classalias.html的Haskell中
发布于 2009-07-03 05:44:47
当编译器显示"Use -XFlexibleInstances“时,您应该尝试添加
{-# LANGUAGE FlexibleInstances #-}转到源代码的顶部(当然,请阅读文档以了解它的功能!)
在此特定情况下,这将使您的代码正常工作:
{-# LANGUAGE FlexibleInstances, UndecidableInstances #-}为了在实例头上启用=>上下文,需要灵活的实例,并且需要不可判定的实例,因为在处理OrdFractional a上下文时,编译器可能会终止将Fractional a和Ord a添加到上下文中--这不能直接帮助最终确定a,并且在适当的可怕环境中,类型检查可能会出现分歧;编译器真的不喜欢这样。(如果编译器永远运行或内存耗尽,您可能也不会喜欢。)
https://stackoverflow.com/questions/306284
复制相似问题