stevana / coverage-guided-pbt

BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Fail to ensure consistent code formatting #1

Closed abailly closed 1 month ago

abailly commented 1 month ago

I am trying to play with the code and possibly suggest changes, but I am facing a very basic issue which is annoying: Although I configured my editor (Emacs + eglot) to use stylish-haskell, I am seeing differences when formatting an otherwise unchanged source file:

% stylish-haskell --version
stylish-haskell 0.14.6.0
% stylish-haskell -v -i src/Statistics.hs
/Users/arnaud/projects/coverage-guided-pbt/.stylish-haskell.yaml exists
Loading configuration at /Users/arnaud/projects/coverage-guided-pbt/.stylish-haskell.yaml
Parsing /Users/arnaud/projects/coverage-guided-pbt/coverage-guided-pbt.cabal...
Gathered default-extensions: []
Enabled Imports (ghc-lib-parser) step
Enabled LanguagePragmas step
Enabled Cases step
Enabled TrailingWhitespace step
Extra language extensions: []
Exit code behavior: normal
 % git diff src/Statistics.hs
diff --git a/src/Statistics.hs b/src/Statistics.hs
index a734dce..38c8f4b 100644
--- a/src/Statistics.hs
+++ b/src/Statistics.hs
@@ -7,27 +7,29 @@ import Data.List
 choose :: Int -> Int -> Rational
 choose n k = fromIntegral (factorial n) / fromIntegral (factorial k * factorial (n - k))

-factorial :: Integral a => a -> a
-factorial n = product [1..n]
+factorial :: (Integral a) => a -> a
+factorial n = product [1 .. n]

 -- https://en.wikipedia.org/wiki/Binomial_distribution
 probSuccess :: Int -> Int -> Rational -> Rational
-probSuccess k n p = (n `choose` k) * (p^k) * (1 - p)^(n - k)
+probSuccess k n p = (n `choose` k) * (p ^ k) * (1 - p) ^ (n - k)

 probSuccess_ :: Int -> Rational -> Rational
-probSuccess_ n p = fromIntegral n * p * (1 - p)^(n - 1)
+probSuccess_ n p = fromIntegral n * p * (1 - p) ^ (n - 1)

 -- From augustss: https://stackoverflow.com/a/30979717
--- | Convert a 'Rational' to a 'String' using the given number of decimals.
--- If the number of decimals is not given the full precision is showed using (DDD) for repeating digits.
--- E.g., 13.7/3 is shown as \"4.5(6)\".
+
+{- | Convert a 'Rational' to a 'String' using the given number of decimals.
+If the number of decimals is not given the full precision is showed using (DDD) for repeating digits.
+E.g., 13.7/3 is shown as \"4.5(6)\".
+-}
...

What am I doing wrong?

I am not using nix.

stevana commented 1 month ago

Strange, here's me doing the same commands:

$ stylish-haskell --version
stylish-haskell 0.14.5.0

$ stylish-haskell -v -i src/Statistics.hs
./coverage-guided-pbt/.stylish-haskell.yaml exists
Loading configuration at ./coverage-guided-pbt/.stylish-haskell.yaml
Parsing ./coverage-guided-pbt/coverage-guided-pbt.cabal...
Gathered default-extensions: []
Enabled Imports (ghc-lib-parser) step
Enabled LanguagePragmas step
Enabled Cases step
Enabled TrailingWhitespace step
Extra language extensions: []
Exit code behavior: normal

$ git diff src/Statistics.hs
# No changes

I can't imagine the minor version difference to be the problem (the CHANGELOG doesn't mention anything suspicious).

I assume you haven't changed coverage-guided-pbt/.stylish-haskell.yaml?

While your output clearly says that it's reading the above config file, the docs say that stylish-haskell looks for configs in other directories. Maybe double check that those somehow are not overriding the local one somehow (even though they shouldn't according to the docs)?

abailly commented 1 month ago

When I checked out the file afresh, then running same commands does not produce any difference. It seems I had another formatter in place (fourmolu) and then stylish-haskell did not reformat the code.

% git co src/Statistics.hs
% fourmolu -I src/Statistics.hs
% git diff --stat src/Statistics.hs
 src/Statistics.hs | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
% stylish-haskell -i src/Statistics.hs
arnaud@mac-mini-2 coverage-guided-pbt % git diff --stat src/Statistics.hs
 src/Statistics.hs | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)

That's why I ditched stylish-haskell: I like my formatters to just format in a "canonical" way the code so that I don't have to think about it anymore :)

abailly commented 1 month ago

On a side note: The code requires GHC 9.10.1 (through base >= 4.20) but there's no stylish-haskell plugin for HLS on this GHC version 🤷

stevana commented 1 month ago

cabal init inserted that constraint, I've just loosened it.