0

在 Haskell 中,如果我有一个类似的界面

class Eq a => Lol l a b where
    ...

我应该如何声明简单实例?如果我尝试类似

class Lol1 a b
instance Lol Lol1 A B

它抱怨 Lol 的第一个参数应该有 kind * -> * -> * 而 Lol1 有 kind

* -> * -> Constraint
4

3 回答 3

2

The error you're getting is a kind mismatch. Kinds are the "types" of types. For example Int has the kind *. * is used to represent any haskell value type, Ints, Bools, whatever.

So you're looking for something with the kind * -> * -> *. This takes in 2 things of kind * and returns a *. An example would be Either.

The problem is that your typeclass Lol1 doesn't have the kind * -> * -> *, it's meant to be used on the left side of a =>, those things have the kind Constraint. Other things like this are equality constraints, a ~ Int or implicit parameters.

So this leads to the inevitable conclusion that you can't use classes as parameters of other classes like below. It's just not well kinded, ever. It's like saying instance Foo (a ~ Int).

 class Foo k where
     foo :: ... => k a -> ...
 -- Or in general, On the right side of an =>
 foo' :: ... => Foo Foo -> ..

So instead you have to use datatypes,

 data Lol1 a b = ...

I can't provide any details about what ... should be without more code for Lol.

于 2013-09-20T03:48:41.723 回答
1

你读过learnyouahaskell吗?

特别是关于类型类的部分也许关于种类的部分对你的情况应该很有帮助。

于 2013-09-20T07:39:15.060 回答
0

在不了解您的程序的整个范围的情况下,我不能说这是否是您想要的解决方案。但是您确实可以使用类并将它们视为类实例中的类型。

{-# LANGUAGE 
 MultiParamTypeClasses,
 KindSignatures,
 ConstraintKinds,
 FlexibleInstances,
 UndecidableInstances
 #-}

import GHC.Prim (Constraint)

class Lol1 a b -- Lol1 has kind * -> * -> Constraint
class Lol2 a   -- Lol2 has kind * -> Constraint

-- Note the explicit 'kind' signature for type var 'l'. This is necessary
class Eq a => Lol (l :: * -> * -> Constraint) a b where

instance Eq b => Lol Lol1 b c
-- instance Eq b => Lol Lol2 b c -- won't work

data A; data B

您可以对类“类型”变量做的唯一事情是使用它来约束其他类型变量,或者将其传递给另一个类。

class (l a b) => Lol (l :: * -> * -> Constraint) a b where

instance Lol1 a b -- removing this breaks the line below
instance Lol Lol1 a b
于 2013-09-20T20:38:11.277 回答