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
It would be very handy to have a MonadFail instance that retrys. As a minimal motivating example, consider:
pop :: TVar [a] -> STM a
pop v = do
a:as <- readTVar v
writeTVar v as
return a
With a MonadFail instance, this is a very convenient way of writing a transaction that waits until there's a value available in the stack.
More generally, such an instance would allow one to write a transaction as if all your favorite patterns matched, and the transaction would then efficiently block until it's so.
Reacted by Samuel Gélineau and kat
Reacted by kat
Activity
samuel-gelineau-at-well-dot commented on Aug 10, 2023
Note that historically, STM used to have a Monad.fail method which was defined as fail = error. Then, when MonadFail was introduced, STM was not given a MonadFail instance with fail = error, as that would go against the spirit of MonadFail.
I don't know why STM originally had fail = error, I agree that it should be fail = retry instead. If STM currently had fail = error, then changing it to fail = retry would be a breaking change. But luckily, MonadFail was introduced a long time ago, so nobody is relying on fail = error anymore, so I do think that it would be a good time to define fail = retry!
The example is compelling, but I worry that it would turn accidental pattern match failure into deadlock, which would make debugging much harder. Happy to hear other opinions though.
Activity
samuel-gelineau-at-well-dot commentedon Aug 10, 2023
Note that historically, STM used to have a
Monad.failmethod which was defined asfail = error. Then, when MonadFail was introduced, STM was not given a MonadFail instance withfail = error, as that would go against the spirit of MonadFail.I don't know why STM originally had
fail = error, I agree that it should befail = retryinstead. If STM currently hadfail = error, then changing it tofail = retrywould be a breaking change. But luckily, MonadFail was introduced a long time ago, so nobody is relying onfail = erroranymore, so I do think that it would be a good time to definefail = retry!simonmar commentedon Aug 30, 2023
The example is compelling, but I worry that it would turn accidental pattern match failure into deadlock, which would make debugging much harder. Happy to hear other opinions though.