Skip to content

Conversation

@kostmo
Copy link
Contributor

@kostmo kostmo commented Apr 3, 2023

Closes #832

@kostmo kostmo changed the title Warn when things are unnecessarily monadic Warn when functions are unnecessarily monadic Apr 3, 2023
@kostmo kostmo marked this pull request as ready for review April 3, 2023 08:41
@kostmo kostmo mentioned this pull request Apr 4, 2023
@kostmo kostmo force-pushed the gratuitously-monadic branch from 9acdb90 to 1eb12bd Compare August 14, 2023 23:09
Copy link
Collaborator

@shayne-fletcher shayne-fletcher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very cool. lgtm.

@kostmo kostmo force-pushed the gratuitously-monadic branch from 1eb12bd to 80a294d Compare August 14, 2023 23:42
@kostmo
Copy link
Contributor Author

kostmo commented Aug 15, 2023

Interestingly, this new hint found two true positives in the hlint codebase, one of which I chose to fix (eb70b4e) and the other to ignore (d1c50ed).

@googleson78
Copy link
Contributor

googleson78 commented Aug 15, 2023

How does this do with something that isn't monadic but uses do?

e.g. these examples

{-# LANGUAGE BlockArguments #-}
x = 
  (+)
    do 3 + 5
    do 4 * 7
    
y = do
  let z = 5
  z

f bla do
  g x y z

I would really like to get warnings on none of these, personally, because they are quite useful in specific cases.

@googleson78
Copy link
Contributor

That's awesome, thanks!

@kostmo
Copy link
Contributor Author

kostmo commented Aug 15, 2023

How does this do with something that isn't monadic but uses do?

I am pleased to report that none of these examples emit a warning. I have added them as test cases (4b8904e). The rule is pretty conservative, and only triggers when pure or return are present.

@kostmo kostmo force-pushed the gratuitously-monadic branch from 2a5ec92 to e4ad48e Compare August 15, 2023 16:58
@kostmo kostmo force-pushed the gratuitously-monadic branch from 1eda112 to af40db1 Compare August 15, 2023 23:18
Copy link
Collaborator

@shayne-fletcher shayne-fletcher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shayne-fletcher
Copy link
Collaborator

shayne-fletcher commented Aug 16, 2023

on

f x = do
  pure x

we get

/L.hs:(4,1)-(5,8): Suggestion: Unnecessarily monadic
Found:
  f x = do pure x
Perhaps:
  Demote `f` to a pure function

the ideal i guess would be

/L.hs:(4,1)-(5,8): Suggestion: Unnecessarily monadic
Found:
  f x = do pure x
Perhaps:
  f x = x

not a blocker i would suppose but is that within reach?

@kostmo
Copy link
Contributor Author

kostmo commented Aug 17, 2023

not a blocker i would suppose but is that within reach?

The type signature of f would also have to change.

@shayne-fletcher
Copy link
Collaborator

let's land this. we can always work in-situ if neil comes back with suggestions later.

@shayne-fletcher shayne-fletcher merged commit 7a14dcf into ndmitchell:master Aug 20, 2023
@kostmo kostmo deleted the gratuitously-monadic branch August 20, 2023 20:05
@ndmitchell
Copy link
Owner

Thanks for the patch, and thanks Shayne for merging. I didn't notice this, and have unfortunately not been paying attention for a bit too long 😞 . Afraid I don't think this can work as an HLint hint. Firstly, there is a bug, in that if you have:

foo [] = pure 1
foo (_:_) = print 1

This reports that foo is unnecessarily monadic. But it's not - you have to consider all equations at once.

The other issue is that there isn't a good "fix" for this, as doing so necessarily changes the signature, and thus you have to update all callers. No other HLint hints have that property, and I'm not sure if it's something we want to start or not.

I guess we could have it as an off by default hint, but it still does things that HLint otherwise doesn't ever do, so I'm not sure it fits. Sorry for the effort you two invested in this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warn when things are unnecessarily monadic

4 participants