说我有以下类型类
class Silly (t :: * -> *) where
-- details
Run Code Online (Sandbox Code Playgroud)
我希望能够表达以下约束,但我不确定它是否可能.
class (Silly s) => Willy (s t) where
-- details
Run Code Online (Sandbox Code Playgroud)
基本上我想对类型构造函数设置约束,而不是整个类型.这甚至可以表达吗?我甚至不确定这种约束会被称为什么,所以谷歌一直没有帮助.
编辑:我有一个数据类型
data Reasoner logic atoms a = Reasoner { unReasoner :: MassAssignment atoms -> a }
Run Code Online (Sandbox Code Playgroud)
有一个Applicative实例.我最初有一个run函数,以便使这些Reasoners更容易,但由于我想利用应用程序的自由可组合性,我定义了一个包含的类型run,ala
class RunReasoner r where
type MassType r
type ResultType r
run :: r -> MassType r -> ResultType r
Run Code Online (Sandbox Code Playgroud)
具有以下实例
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
Run Code Online (Sandbox Code Playgroud)
这样我就可以编写代码了
expression `run` mass1 `run` mass2 `run` ... `run` massN
Run Code Online (Sandbox Code Playgroud)
这在大多数情况下都是好的,但我现在正在以其他方式组成reasoners,并且如果MassType仅从类型构造函数可用Reasoner l as,而不是必须具有完整类型实例,则更愿意Reasoner l as a.因此我考虑将RunReasoner类型类拆分为两个,因为MassType不依赖于最终的类型参数.
你可以创建一个独立的类型系列
type family MassType (r :: * -> *)
type instance MassType (Reasoner l as) = as
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
208 次 |
| 最近记录: |