2013-11-26 103 views
2

说我有以下类型类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不依赖于最终的类型参数。

+0

可不可以给你打算如何使用此些方面?我非常肯定可以很简单地实现你所需要的,尽管它看起来不像你写的。 – leftaroundabout

回答

5

你可以做一个独立的家庭类型

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

这个简单的答案让我重新思考了我设计我的类型的方式,然后让我找到了一个可行的解决方案。谢谢。 – bstamour

+0

我很高兴,不客气:) –