Skip to content

Commit 578b51f

Browse files
Merge #990: refactors & hlint rules
From what I know & in most cases `fold` is just as efficient as `{,m}concat` & `foldMap` as `concatMap`. I guard their effectiveness with supplying type signatures. `fold` enables `HLint` to provide further code refactoring instruction & to find function replacements by type signatures. & further refactors, work actually started from `Inference` code refactors & tantrum spread, so currently trying to carve diff into small commits.
2 parents 5737ffc + 2432e37 commit 578b51f

37 files changed

+1271
-1024
lines changed

.hlint.yaml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,3 +2696,79 @@
26962696
lhs: "maybe mempty a b"
26972697
note: "Use `whenJust`"
26982698
rhs: a `whenJust` b
2699+
2700+
- hint:
2701+
lhs: "(mempty, mempty)"
2702+
note: "Is `mempty`"
2703+
rhs: mempty
2704+
2705+
- hint:
2706+
lhs: "concat a"
2707+
note: "Use `fold`"
2708+
rhs: fold a
2709+
2710+
- hint:
2711+
lhs: "mconcat a"
2712+
note: "Use `fold`"
2713+
rhs: fold a
2714+
2715+
- hint:
2716+
lhs: "concatMap a"
2717+
note: "Use `foldMap`"
2718+
rhs: foldMap a
2719+
2720+
- hint:
2721+
lhs: "[a]"
2722+
note: "Use `one`"
2723+
rhs: (one a)
2724+
2725+
# Submitted upstream: https://github.com/ndmitchell/hlint/pull/1309 remove when merges
2726+
- hint:
2727+
lhs: "either Left f e"
2728+
note: "Use `=<<`"
2729+
rhs: f =<< e
2730+
2731+
- warn: {lhs: "init (one x)", rhs: "mempty", name: Evaluate}
2732+
- warn: {lhs: "null (one x)", rhs: "False", name: Evaluate}
2733+
- warn: {lhs: "foldr1 f (one x)", rhs: x, name: Evaluate}
2734+
- warn: {lhs: "scanr f z mempty", rhs: "one z", name: Evaluate}
2735+
- warn: {lhs: "scanr1 f mempty", rhs: "mempty", name: Evaluate}
2736+
- warn: {lhs: "scanr1 f (one x)", rhs: "one x", name: Evaluate}
2737+
- warn: {lhs: "fold (one a)", rhs: a, name: Evaluate}
2738+
- warn: {lhs: "cycle (one x)", rhs: repeat x}
2739+
- hint: {lhs: "\\x -> one x", rhs: "(one x)"}
2740+
- hint: {lhs: "elem x (one y)", rhs: x == y, note: ValidInstance Eq a}
2741+
- hint: {lhs: "notElem x (one y)", rhs: x /= y, note: ValidInstance Eq a}
2742+
- warn: {lhs: "sequenceA (one a)", rhs: "pure <$> a"}
2743+
- warn: {lhs: "head $ x <> one y", rhs: "headDef y x"}
2744+
- warn: {lhs: "pictures (one p)", rhs: p, name: Evaluate}
2745+
2746+
- hint:
2747+
lhs: "x : mempty"
2748+
note: "Use `one`"
2749+
rhs: one x
2750+
- hint:
2751+
lhs: "x :| mempty"
2752+
note: "Use `one`"
2753+
rhs: one x
2754+
2755+
- hint:
2756+
lhs: "join . fmap join"
2757+
note: "Monad law"
2758+
rhs: join . join
2759+
2760+
- hint:
2761+
lhs: "join . fmap pure"
2762+
note: "Monad law"
2763+
rhs: id
2764+
2765+
- hint:
2766+
lhs: "join . pure"
2767+
note: "Monad law"
2768+
rhs: id
2769+
2770+
- hint:
2771+
lhs: "join . (f <<$>>)"
2772+
note: "Monad law"
2773+
rhs: fmap f . join
2774+

benchmarks/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ import Criterion.Main
55
import qualified ParserBench
66

77
main :: IO ()
8-
main = defaultMain [ParserBench.benchmarks]
8+
main = defaultMain $ one ParserBench.benchmarks

main/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
123123
(\ ty -> liftIO $ putStrLn $ "Type of expression: " <>
124124
ppShow (maybeToMonoid $ Map.lookup @VarName @[Scheme] "it" $ coerce ty)
125125
)
126-
(HM.inferTop mempty [("it", stripAnnotation expr')])
126+
(HM.inferTop mempty (one ("it", stripAnnotation expr')))
127127

128128
-- liftIO $ putStrLn $ runST $
129129
-- runLintM opts . renderSymbolic =<< lint opts expr

main/Repl.hs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ main' iniVal =
111111
(words <$> lines f)
112112

113113
handleMissing e
114-
| Error.isDoesNotExistError e = pure ""
114+
| Error.isDoesNotExistError e = pure mempty
115115
| otherwise = throwM e
116116

117117
-- Replicated and slightly adjusted `optMatcher` from `System.Console.Repline`
@@ -356,8 +356,7 @@ setConfig args =
356356
-- | Prefix tab completer
357357
defaultMatcher :: MonadIO m => [(String, CompletionFunc m)]
358358
defaultMatcher =
359-
[ (":load", Console.fileCompleter)
360-
]
359+
one (":load", Console.fileCompleter)
361360

362361
completion
363362
:: (MonadNix e t f m, MonadIO m)
@@ -424,7 +423,7 @@ completeFunc reversedPrev word
424423
shortBuiltins = M.keys builtins
425424

426425
pure $ listCompletion $ toString <$>
427-
["__includes"]
426+
one "__includes"
428427
<> contextKeys
429428
<> shortBuiltins
430429

@@ -475,12 +474,12 @@ helpOptions :: (MonadNix e t f m, MonadIO m) => HelpOptions e t f m
475474
helpOptions =
476475
[ HelpOption
477476
"help"
478-
""
477+
mempty
479478
"Print help text"
480479
(help helpOptions . fromString)
481480
, HelpOption
482481
"paste"
483-
""
482+
mempty
484483
"Enter multi-line mode"
485484
(error "Unreachable")
486485
, HelpOption
@@ -490,7 +489,7 @@ helpOptions =
490489
(load . fromString)
491490
, HelpOption
492491
"browse"
493-
""
492+
mempty
494493
"Browse bindings in interpreter context"
495494
(browse . fromString)
496495
, HelpOption
@@ -500,12 +499,12 @@ helpOptions =
500499
(typeof . fromString)
501500
, HelpOption
502501
"quit"
503-
""
502+
mempty
504503
"Quit interpreter"
505504
quit
506505
, HelpOption
507506
"set"
508-
""
507+
mempty
509508
("Set REPL option"
510509
<> Prettyprinter.line
511510
<> "Available options:"
@@ -527,32 +526,32 @@ helpSetOptions :: [HelpSetOption]
527526
helpSetOptions =
528527
[ HelpSetOption
529528
"strict"
530-
""
529+
mempty
531530
"Enable strict evaluation of REPL expressions"
532531
(\x -> x { cfgStrict = True})
533532
, HelpSetOption
534533
"lazy"
535-
""
534+
mempty
536535
"Disable strict evaluation of REPL expressions"
537536
(\x -> x { cfgStrict = False})
538537
, HelpSetOption
539538
"values"
540-
""
539+
mempty
541540
"Enable printing of value provenance information"
542541
(\x -> x { cfgValues = True})
543542
, HelpSetOption
544543
"novalues"
545-
""
544+
mempty
546545
"Disable printing of value provenance information"
547546
(\x -> x { cfgValues = False})
548547
, HelpSetOption
549548
"debug"
550-
""
549+
mempty
551550
"Enable printing of REPL debug information"
552551
(\x -> x { cfgDebug = True})
553552
, HelpSetOption
554553
"nodebug"
555-
""
554+
mempty
556555
"Disable REPL debugging"
557556
(\x -> x { cfgDebug = False})
558557
]

0 commit comments

Comments
 (0)