You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think I've spotted two potential bugs in the RIO monad's MonadWriter implementation. Specifically with respect to listen and pass.
I'm only saying "bug" though on the assumption that transformers/mtl's WriterT output for equivalent actions being correct. I remain open to the issue being in the transformers implementation and not here.
Given the following MonadWriter actions:
say::MonadWriter [Text] m=>Text->m()
say x = tell [x]
actionListen::MonadWriter [Text] m=>m [Text]
actionListen =do
say "BEFORE"
(_, txts) <- listen (say "INSIDE")
pure txts
actionPass::MonadWriter [Text] m=>mInt
actionPass =do
say "START"
pass $pure (42, constmempty)
And the following action to allow us to get the results of the above actions in RIO.
runWriterRIO:: (MonadIOm, Monoidw) =>RIO (SomeRefw) a->m (a, w)
runWriterRIO action =do
ref <- newSomeRef mempty
runRIO ref do
result <- action
finalLog <- readSomeRef ref
pure (result, finalLog)
I beleive that we should expect the same result if we were compare the results of using runWriterT on the two actions above to the results of using runWriterRIO.
Here are those results. For the action that uses listen:
The two diferences from the WriterT implementation appear to be because:
the above does not start the Monoid w from mempty when running the action within listen like WriterT does
the above resets the ref to the initial pre-action value, rather than merging in the impact of the action on the Monoid into the ref with those from pre-action.
I believe the following implementation would make both consistent with WriterT:
If you think I'm on the right track and that the RIO output should match that of WriterT, I've got the above and some changes to the RIOSpec.hs tests over at this fork and am happy to send a PR. I'll push up another commit with a link to this issue in the changelog and the package.yaml bumped also.
Cheers!
The text was updated successfully, but these errors were encountered:
I have to admit that I’m not that familiar with pass and listen, and that I haven’t read through the details here carefully. However, if our behavior mismatches mtl, I would definitely welcome a PR adding a test demonstrating this and including a fix.
hello @and-pete
I see your fork looks pretty done but you haven't submitted a PR. I'd be happy to pick this up and run with it if you don't have time to bring this to completion.
Hi Michael,
I think I've spotted two potential bugs in the
RIO
monad'sMonadWriter
implementation. Specifically with respect tolisten
andpass
.I'm only saying "bug" though on the assumption that
transformers
/mtl
'sWriterT
output for equivalent actions being correct. I remain open to the issue being in thetransformers
implementation and not here.Given the following
MonadWriter
actions:And the following action to allow us to get the results of the above actions in
RIO
.I beleive that we should expect the same result if we were compare the results of using
runWriterT
on the two actions above to the results of usingrunWriterRIO
.Here are those results. For the action that uses
listen
:And for the action that uses pass
Different in both cases.
If we look at the current implementation of
listen
:The two diferences from the
WriterT
implementation appear to be because:Monoid w
frommempty
when running the action withinlisten
like WriterT doesI believe the following implementation would make both consistent with WriterT:
And in the case of the current
pass
implementation forRIO
:I believe the following should likewise fix any inconsistencies with
WriterT
inpass
:If you think I'm on the right track and that the
RIO
output should match that ofWriterT
, I've got the above and some changes to the RIOSpec.hs tests over at this fork and am happy to send a PR. I'll push up another commit with a link to this issue in thechangelog
and thepackage.yaml
bumped also.Cheers!
The text was updated successfully, but these errors were encountered: