简体   繁体   中英

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. 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; this would turn State into something that is parametric in the typeclass of its memory. 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

This is the same technique used, eg, to obtain open recursion in syb-with-class.

I'm fairly certain that this should point you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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