9

我正在开发一个专门的数值数据处理库,但遇到了一个我无法解决的错误。我认为先展示一个例子然后解释我的问题会更容易。我也为奇怪的名字道歉,出于法律目的我不得不混淆。

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances     #-}

data MyError = MyError String deriving (Eq, Show)

data MyList    = MyList [Double] deriving (Eq, Show)
data NamedList = NamedList String MyList deriving (Eq, Show)

class MyNum a b ret where
    myAdd       :: a -> b -> Either MyError ret
    myLessThan  :: a -> b -> Either MyError Bool

instance MyNum MyList Double MyList where
    myAdd (MyList xs) x = Right $ MyList $ map (+x) xs
    myLessThan (MyList xs) x = Right $ all (< x) xs

instance MyNum NamedList Double NamedList where
    myAdd (NamedList n l) x = fmap (NamedList n) $ myAdd l x
    myLessThan (NamedList n l) x = myLessThan l x

如果我尝试编译它,我会收到错误

No instance for (MyNum MyList Double ret0)
  arising from a use of `myLessThan'
The type variable `ret0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there is a potential instance available:
  instance MyNum MyList Double MyList
    -- Defined at testing_instances.hs:13:10
Possible fix:
  add an instance declaration for (MyNum MyList Double ret0)
In the expression: myLessThan l x
In an equation for `myLessThan':
    myLessThan (NamedList n l) x = myLessThan l x
In the instance declaration for `MyNum NamedList Double NamedList'

因为编译器无法确定MyNum用于MyList. 它之所以有效,是myAdd因为 for 的返回类型MyNum很容易派生,但它无法弄清楚 for myLessThan。我想使用这个类型类,以便我可以轻松地在整个过程中添加细粒度的错误处理,并且因为我的实际代码具有 +、-、*、/、<、<=、> 和 >= 的等价物,并且我想要为MyNum Double MyList MyList,MyNum MyList MyList MyList和类似的实例创建NamedList. 除非有更简单的方法可以做到这一点,否则我可以拥有多态交换运算符。

但是,我无法弄清楚要myLessThan为第二个实例添加什么类型签名,以便它知道要使用哪个实例。我知道一种解决方案是将算术和比较运算符拆分为两个单独的类型类,但我想尽可能避免这样做。

4

2 回答 2

11

您可以使用函数依赖关系来指定“retab”唯一确定。

...
{-# LANGUAGE FunctionalDependencies #-}
...
class MyNum a b ret | a b -> ret where
...

这让类型检查器知道它可以选择正确的实例定义,只知道你的参数中的ab

myLessThan (NamedList n l) x = myLessThan l x

如果您定义一个具有相同但不同的附加实例,编译器现在会报错a,例如bret

instance MyNum MyList Double SomeOtherType where
于 2013-08-26T23:01:37.610 回答
4

正如 jberryman 所说,您可以使用TypeFamilies. 这就是:

-{-# LANGUAGE FlexibleInstances     #-}
+{-# LANGUAGE TypeFamilies #-}

-class MyNum a b ret where
-    myAdd       :: a -> b -> Either MyError ret
+class MyNum a b where
+    type Ret a b
+    myAdd       :: a -> b -> Either MyError (Ret a b)

-instance MyNum MyList Double MyList where
+instance MyNum MyList Double where
+    type Ret MyList Double = MyList

-instance MyNum NamedList Double NamedList where
+instance MyNum NamedList Double where
+    type Ret NamedList Double = NamedList

我只是将rettype 从 class parameter 移动到associated type Ret
这是一种TypeFamily声明有来自类参数abto的函数的方式Ret

于 2013-08-27T11:04:22.847 回答