Haskell class inheritance and parametric typeclass

I want to say that a certain parameterized monad st works with normal memory, but a subclass of my parameterized monad should impose an additional restriction on the type of memory. In code:

class Memory m where ... class State st where unit :: Memory m => a -> st mma 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 want to impose that when (RState st) is true, then inside the definition of (State st) the memory is replaced with RMemory; this would turn the state into something parametric into the memory class of its memory. It can be done?

+4
source share
1 answer

I suspect you cannot do this directly. However, you can cheat very effectively by adding a level of indirection. See John Hughes' Limited Data Types in Haskell for how to do this: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.39.2816

This is the same method that is used, for example, to get open recursion in a syb-co-class.

I am sure this should point you in the right direction.

+3
source

Source: https://habr.com/ru/post/1315491/


All Articles