简体   繁体   English

Haskell类型类继承和参数类型类

[英]Haskell typeclass inheritance and parametric typeclass

I wish to say that a certain parametrized monad st works with a regular memory, but a subclass of my parametrized monad should impose an additional constraint on the type of memory. 我想说一个参数化的monad st与常规内存一起工作,但是我的参数化monad的子类应该对内存类型施加额外的约束。 In code: 在代码中:

class Memory m where
    ...

class State st where
    unit :: Memory m => a -> st m m a
    bind :: (Memory m1, Memory m2, Memory m3) => st m1 m2 a -> (a -> st m2 m3 b) -> st m1 m3 b

class RMemory m where
    ...

class State st => RState st where
    -- no operators

now my problem is that I wish to impose that whenever (RState st) is true, then inside the definition of (State st) Memory is replaced with RMemory; 现在我的问题是,我希望强制每当(RState st)为真,然后在(State st)定义内部内存被RMemory替换; this would turn State into something that is parametric in the typeclass of its memory. 这会将State变成在其内存的类型类中具有参数的东西。 Can this be done? 可以这样做吗?

I suspect you can't do this directly. 我怀疑你不能直接这样做。 You can however cheat very effectively by adding a level of indirection. 但是,您可以通过添加间接级别来非常有效地作弊。 See John Hughes' Restricted Data Types in Haskell for how to do it: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.39.2816 请参阅John Hughes在Haskell中的限制数据类型,了解如何执行此操作: http//citeseerx.ist.psu.edu/viewdoc/summary?doi = 10.1.1.39.2816

This is the same technique used, eg, to obtain open recursion in syb-with-class. 这与使用例如在syb-with-class中获得开放递归的技术相同。

I'm fairly certain that this should point you in the right direction. 我相当肯定这应该指向正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM