Facilities for Disabled Families

A lot of restrictions seem to come together. Let me distract them.

type MonadNumState am = (MonadState am, Num a) 

MonadNumState is simply synonymous with limitations, so I get the benefits of functional dependencies every time I use it and I can easily put MonadNumState am in context. Now suppose I want to distract this in a family of constraints:

 class Iterator t where type MonadIter tam :: Constraint next :: (MonadIter tam) => mt ... instance Iterator Foo where type MonadIter Foo am = (MonadState am, Num a) ... instance Iterator Bar where type MonadIter Bar am = (MonadRandom m, MonadSplit am, RandomGen a) ... 

But now a not a functional dependency. next almost never used, since a cannot be inferred. What can I do? Well, I could, of course, use a type family. MonadState is written using fundeps, but it is easy to convert to family types.

 instance (MonadState sm) => MonadStateFamily m where type St m = s get' = get ... instance (MonadStateFamily m) => MonadState (St m) m where get = get' ... 

I don’t think so.

 Foo.hs:25:3: The RHS of an associated type declaration mentions type variable `s' All such variables must be bound on the LHS 

What else can I do? What I really want is to existentially quantify s . I did not find any way to do this without explicitly going into the dictionary.

So, how do I get the benefits of drives for families with disabilities?

+6
source share
1 answer

You can use an autonomous type family instead of an associated type

 type family StateType (m:: * -> *) 

Then you can define

 class MonadStateFamily m where get' :: m (StateType m) instance (MonadState sm, s ~ StateType m) => MonadStateFamily m where get' = get 

and use it on a specific monad as follows:

 type instance StateType (State s) = s getState :: State ss getState = get' 
+3
source

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


All Articles