darcs

Patch 2226 require MonadThrow from the base apply monads

Title require MonadThrow from the base apply monads
Superseder Nosy List bfrk
Related Issues
Status accepted Assigned To bfrk
Milestone

Created on 2021-11-20.11:30:25 by bfrk, last changed 2023-06-06.08:49:59 by bfrk.

Files
File name Status Uploaded Type Edit Remove
patch-preview.txt bfrk, 2021-11-20.11:30:24 text/x-darcs-patch
patch-preview.txt bfrk, 2023-04-01.15:49:09 text/x-darcs-patch
require-monadthrow-from-the-base-apply-monads.dpatch bfrk, 2021-11-20.11:30:24 application/x-darcs-patch
use-strictidentity-as-the-pure-apply-monad.dpatch bfrk, 2023-04-01.15:49:09 application/x-darcs-patch
See mailing list archives for discussion on individual patches.
Messages
msg22931 (view) Author: bfrk Date: 2021-11-20.11:30:24
Something I wanted to do for a long time and finally got around to.

Historical background: we used to take the ability to throw an error in a
computation for granted in any Monad, due to the mis-guided inclusion of the
'fail' method in the Monad class. This has been fixed several ghc versions
ago. To cope with that, I replaced calls to 'fail' with calls to 'throw'
(with manually crafted IOExceptions) when applying a patch fails. But using
the (pure) 'throw' is unclean; it is better to make the requirements for the
base monad explicit by using a MonadThrow constraint, which allows non-IO
base monads to use a pure implementation of 'throwM'.

1 patch for repository http://darcs.net/screened:

patch c2737fa99e15af26c36602ec445ffeb4a10db25d
Author: Ben Franksen <ben.franksen@online.de>
Date:   Sun Sep 19 14:00:41 CEST 2021
  * require MonadThrow from the base apply monads

  This avoids having to throw from pure code when applying patches in a monad
  that is not based on IO. In Prim.FileUUID we now correctly handle all patch
  application errors as exceptions. In D.P.ApplyMonad, the FilePathMonad is
  now a StateT over a Pure monad with an instance MonadThrow that calls error;
  in the harness, the Fail monad is now a synonym for Either SomeException.
Attachments
msg23241 (view) Author: ganesh Date: 2023-04-01.08:59:38
This seems like a good idea.

> hunk ./src/Darcs/Patch/ApplyMonad.hs 114
> +instance MonadThrow Pure where
> +  throwM e = Pure (error (show e))

I don't think this instance satisfies the law for `throwM` described
at https://hackage.haskell.org/package/exceptions-0.10.7/docs/Control-
Monad-Catch.html :

> throwM e >> x = throwM e

I'm guessing it probably doesn't break anything now, given we
also previously just called `error`, but it doesn't feel like a
good long-term state to leave our code in.

> hunk ./harness/Darcs/Test/Patch/RepoModel.hs 15
> + unFail = either (error.show) id

Minor code style comment - in modern Haskell it's better to put spaces
around operators because the language keeps evolving to steal
punctuation for syntax :-)
msg23246 (view) Author: bfrk Date: 2023-04-01.15:12:06
>> hunk ./harness/Darcs/Test/Patch/RepoModel.hs 15
>> + unFail = either (error.show) id
> 
> Minor code style comment - in modern Haskell it's better to put spaces
> around operators because the language keeps evolving to steal
> punctuation for syntax :-)

Agreed. I was just sloppy here.

>> hunk ./src/Darcs/Patch/ApplyMonad.hs 114
>> +instance MonadThrow Pure where
>> +  throwM e = Pure (error (show e))
> 
> I don't think this instance satisfies the law for `throwM` described
> at https://hackage.haskell.org/package/exceptions-0.10.7/docs/Control-
> Monad-Catch.html :
> 
>> throwM e >> x = throwM e

Wouldn't it be enough to make 'Pure' strict to fix that?
msg23247 (view) Author: bfrk Date: 2023-04-01.15:19:28
Oops, it is a newtype, so it is already strict. What needs to be 
strict is then >>=, right?
msg23248 (view) Author: ganesh Date: 2023-04-01.15:20:09
> > throwM e >> x = throwM e

> Wouldn't it be enough to make 'Pure' strict to fix that?

Yes, I think so. Though strictness in the context of monadic operations
always confuses me a bit.
msg23249 (view) Author: ganesh Date: 2023-04-01.15:23:57
> Oops, it is a newtype, so it is already strict. What needs to be 
> strict is then >>=, right?

And any custom definition of `>>` if there is one. Plus we need
to make sure that the `Applicative` and `Functor` instances are 
consistent with that.

I sort of feel there should be some way to express this in the
datatype, but I'm not sure.
msg23250 (view) Author: bfrk Date: 2023-04-01.15:24:49
I guess that means using
https://hackage.haskell.org/package/strict-identity

Is that worth the trouble? Perhaps.
msg23251 (view) Author: bfrk Date: 2023-04-01.15:49:09
1 patch for repository http://darcs.net/screened:

patch 3d1d622014233786680f69ce0f7fdea5d401bea3
Author: Ben Franksen <ben.franksen@online.de>
Date:   Sat Apr  1 17:44:46 CEST 2023
  * use StrictIdentity as the Pure apply monad

  This is so that we can satisfy the laws for MonadThrow.
Attachments
msg23256 (view) Author: bfrk Date: 2023-04-01.20:19:14
I haven't screened the follow-up yet so if you think adding a new 
dependency isn't a good idea, we can simply ignore it.
msg23257 (view) Author: ganesh Date: 2023-04-01.20:21:44
No, I think it's the right thing to do.
msg23259 (view) Author: bfrk Date: 2023-04-01.20:39:23
Okay, screened. And, BTW, thanks for spotting this! It would never 
have occurred to me that the instance violates a law.
History
Date User Action Args
2021-11-20 11:30:25bfrkcreate
2021-11-20 11:31:19bfrksetstatus: needs-screening -> needs-review
2023-04-01 08:59:38ganeshsetstatus: needs-review -> followup-requested
assignedto: bfrk
messages: + msg23241
2023-04-01 15:12:06bfrksetmessages: + msg23246
2023-04-01 15:19:28bfrksetmessages: + msg23247
2023-04-01 15:20:09ganeshsetmessages: + msg23248
2023-04-01 15:23:57ganeshsetmessages: + msg23249
2023-04-01 15:24:49bfrksetmessages: + msg23250
2023-04-01 15:49:09bfrksetfiles: + patch-preview.txt, use-strictidentity-as-the-pure-apply-monad.dpatch
messages: + msg23251
2023-04-01 20:19:16bfrksetmessages: + msg23256
2023-04-01 20:21:44ganeshsetstatus: followup-requested -> accepted-pending-tests
messages: + msg23257
2023-04-01 20:39:24bfrksetmessages: + msg23259
2023-06-06 08:49:59bfrksetstatus: accepted-pending-tests -> accepted