说我有以下类型类Haskell的种类和类型约束
class Silly (t :: * -> *) where
-- details
我希望能够表达以下约束,但我不知道,如果它甚至有可能。
class (Silly s) => Willy (s t) where
-- details
基本上我想对类型构造函数,而不是整个类型的约束。这甚至可以表达吗?我甚至不知道这种限制会被调用,所以Google一直没有帮助。
编辑:我有一个数据类型
data Reasoner logic atoms a = Reasoner { unReasoner :: MassAssignment atoms -> a }
有一个Applicative
实例。我最初是为了与这些推理更容易一点的工作有一个run
功能,但因为我要带applicatives的自由组合性的优势,我定义包含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
这一切有益于MO st部分,但我现在正在用其他方式编写reasoners,并且宁愿MassType只能从类型构造函数Reasoner l as
获得,而不必拥有完整类型实例Reasoner l as a
。因此,我正在考虑将RunReasoner
类型类拆分为两个类,因为MassType不依赖于最终的类型参数。
可不可以给你打算如何使用此些方面?我非常肯定可以很简单地实现你所需要的,尽管它看起来不像你写的。 – leftaroundabout