Haskell种类和类型约束

时间:2013-11-26 20:06:22

标签: haskell ghc

说我有以下类型类

class Silly (t :: * -> *) where
    -- details

我希望能够表达以下约束,但我不确定它是否可能。

class (Silly s) => Willy (s t) where
    -- details

基本上我想对类型构造函数设置约束,而不是整个类型。这甚至可以表达吗?我甚至不确定这种约束会被称为什么,所以谷歌一直没有帮助。

编辑:我有一个数据类型

data Reasoner logic atoms a = Reasoner { unReasoner :: MassAssignment atoms -> a }

具有Applicative个实例。我最初有一个run函数,以便更容易地使用这些Reasoners,但由于我想利用应用程序的自由可组合性,我定义了一个包含run的类型类,ala

class RunReasoner r where
    type MassType r
    type ResultType r
    run :: r -> MassType r -> ResultType r

以下实例

instance RunReasoner (Reasoner l as a) where
    type MassType (Reasoner l as a) = MassAssignment as
    type ResultType (Reasoner l as a) = a
    run = -- details

instance (RunReasoner (r2 t)) => RunReasoner (Compose (Reasoner l1 as1) r2 t) where
    type MassType (Compose (Reasoner l1 as1) r2 t) = MassAssignment as1
    type ResultType (Compose (Reasoner l1 as1) r2 t) = r2 t
    run = -- details

这样我就可以编写诸如

之类的代码
expression `run` mass1 `run` mass2 `run` ... `run` massN

这在大多数情况下都是好的,但我现在正在以其他方式编写reasoners,并且更喜欢MassType只能从类型构造函数Reasoner l as获得,而不是必须拥有完整的类型实例Reasoner l as a。因此我考虑将RunReasoner类型类拆分为两个,因为MassType不依赖于最终的类型参数。

1 个答案:

答案 0 :(得分:5)

您可以创建独立类型系列

type family MassType (r :: * -> *)
type instance MassType (Reasoner l as) = as