dreixel / syb

Scrap Your Boilerplate generic programming library in Haskell
http://www.cs.uu.nl/wiki/GenericProgramming/SYB
Other
44 stars 23 forks source link

Add monadic query functions #36

Open HaskellZhangSong opened 2 years ago

HaskellZhangSong commented 2 years ago

This commit adds following monadic query functions in issue #35:

everythingButM :: forall m r. Monad m => (r -> r -> r) -> GenericQ (m (r, Bool)) -> GenericQ (m r)
everythingMButM :: forall m r. Monad m => (m r -> m r -> m r) -> GenericQ (m (r, Bool)) -> GenericQ (m r)
everythingWithContextM :: forall m s r. Monad m => s -> (r -> r -> r) -> GenericQ (m (s -> (r, s))) -> GenericQ (m r)
everythingMWithContextM :: forall m s r. Monad m => s -> (m r -> m r -> m r) -> GenericQ (m (s -> (r, s))) -> GenericQ (m r)

which make query with unavoidable monadic query easier.

sergv commented 2 years ago

@HaskellZhangSong I think everythingButM and everythingMButM are reasonable generalisations of what's already in the library (although I'd probably consider having GenericQ (m (Maybe r)) instead of GenericQ (m (r, Bool)) when designing from scratch) but signatures of everythingWithContextM and everythingMWithContextM look convoluted. Why can't the context in there (the s -> (..., s) bit) be passed a part of the monad?

HaskellZhangSong commented 8 months ago

I did not think this very carefully. I just extend everythingBut by lifting (r, Bool) to m (r, Bool). Similarly applied to everythingWithContext function too.

Why can't the context in there (the s -> (..., s) bit) be passed a part of the monad?

I am not quite sure about how and what exactly you mean. Do you mean to combine m with a state monad? Could you show the code? Here, I just think the m in (m r -> m r -> m r) should be coherent with the m in (m (s -> (r, s)))

Thanks

sergv commented 8 months ago

I was thinking about something along the lines

everythingMWithContextM :: forall n s r. Monad n => (StateT s n r -> StateT s n r -> StateT s n r) -> GenericQ (StateT s n r)) -> GenericQ (StateT s n r)

I.e. to obtain everythingMWithContextM the user instantiates already defined everything funsction.

HaskellZhangSong commented 8 months ago

I think this looks more complicated and need to bring mtl library into syb.

sergv commented 8 months ago

syb doesn't have to depend on mtl (transformers would be enough) - user can instantiate existing functions themselves in their package.