Skip to content

Commit ff700be

Browse files
Merge #1006: Upd {{Value, Eval Provenance, JSON} builders, Option getters, Utils}
https://github.com/haskell-nix/hnix/wiki/Naming-style-guidelines Closes a lot of #949 Option getter names are not ideal (I can not name them properly currently because every one of them requires in-depth lookup of what was the semantic meanings implied but not expressed, as it was not documented, to name them a superset of them must be considered. And strangely, at least a couple of them, based on the code, - are redundant, a synonym of one another. But current naming is than before & push thoughts into the right direction. `NixContext` & `NixString` now have contents before the carrier, as frequently the carrier is modified, & `swap` happens once - before writing into state monad. ... Most of this is the stuff I wanted to do for a long time.
2 parents 5dc09e9 + 291ae54 commit ff700be

38 files changed

+1430
-1393
lines changed

.hlint.yaml

Lines changed: 50 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,24 @@
1919
- ignore:
2020
name: Use Foldable.forM_
2121
- hint:
22-
lhs: "pure ()"
22+
lhs: "pure mempty"
2323
note: "Use 'stub'"
2424
rhs: stub
25+
2526
- hint:
26-
lhs: "return ()"
27-
note: "Use 'stub'"
28-
rhs: stub
27+
lhs: "[]"
28+
note: "Use `mempty`"
29+
rhs: mempty
30+
2931
- hint:
30-
lhs: "(: [])"
32+
lhs: "(: mempty)"
3133
note: "Use `one`"
3234
rhs: one
3335
- hint:
34-
lhs: "(:| [])"
36+
lhs: "(:| mempty)"
3537
note: "Use `one`"
3638
rhs: one
39+
3740
- hint:
3841
lhs: Data.Sequence.singleton
3942
note: "Use `one`"
@@ -91,7 +94,7 @@
9194
note: "Use `one`"
9295
rhs: one
9396
- warn:
94-
lhs: "Control.Exception.evaluate (x `deepseq` ())"
97+
lhs: "Control.Exception.evaluate (x `deepseq` mempty)"
9598
rhs: evaluateNF_ x
9699
- warn:
97100
lhs: "void (evaluateWHNF x)"
@@ -106,19 +109,19 @@
106109
lhs: "foldl' (*) 1"
107110
rhs: product
108111
- hint:
109-
lhs: "fmap and (sequence s)"
112+
lhs: "fmap and (sequenceA s)"
110113
note: Applying this hint would mean that some actions that were being executed previously would no longer be executed.
111114
rhs: andM s
112115
- hint:
113-
lhs: "and <$> sequence s"
116+
lhs: "and <$> sequenceA s"
114117
note: Applying this hint would mean that some actions that were being executed previously would no longer be executed.
115118
rhs: andM s
116119
- hint:
117-
lhs: "fmap or (sequence s)"
120+
lhs: "fmap or (sequenceA s)"
118121
note: Applying this hint would mean that some actions that were being executed previously would no longer be executed.
119122
rhs: orM s
120123
- hint:
121-
lhs: "or <$> sequence s"
124+
lhs: "or <$> sequenceA s"
122125
note: Applying this hint would mean that some actions that were being executed previously would no longer be executed.
123126
rhs: orM s
124127
- hint:
@@ -148,32 +151,16 @@
148151
note: "Use 'asumMap'"
149152
rhs: asumMap f
150153
- hint:
151-
lhs: "asum (map f xs)"
154+
lhs: "asum (fmap f xs)"
152155
note: "Use 'asumMap'"
153156
rhs: asumMap f xs
154157
- warn:
155-
lhs: "map fst &&& map snd"
158+
lhs: "fmap fst &&& fmap snd"
156159
rhs: unzip
157160
- hint:
158161
lhs: "fmap (fmap f) x"
159162
note: "Use '(<<$>>)'"
160163
rhs: "f <<$>> x"
161-
- hint:
162-
lhs: "(\\f -> f x) <$> ff"
163-
note: Use flap operator
164-
rhs: "ff ?? x"
165-
- hint:
166-
lhs: "fmap (\\f -> f x) ff"
167-
note: Use flap operator
168-
rhs: "ff ?? x"
169-
- hint:
170-
lhs: "fmap ($ x) ff"
171-
note: Use flap operator
172-
rhs: "ff ?? x"
173-
- hint:
174-
lhs: "($ x) <$> ff"
175-
note: Use flap operator
176-
rhs: "ff ?? x"
177164
- warn:
178165
lhs: "fmap f (nonEmpty x)"
179166
rhs: viaNonEmpty f x
@@ -184,10 +171,10 @@
184171
lhs: "f <$> nonEmpty x"
185172
rhs: viaNonEmpty f x
186173
- warn:
187-
lhs: partitionEithers . map f
174+
lhs: partitionEithers . fmap f
188175
rhs: partitionWith f
189176
- warn:
190-
lhs: partitionEithers $ map f x
177+
lhs: partitionEithers $ fmap f x
191178
rhs: partitionWith f x
192179
- warn:
193180
lhs: forever
@@ -224,40 +211,16 @@
224211
lhs: "maybe (Right r) Left"
225212
rhs: maybeToLeft r
226213
- warn:
227-
lhs: "case m of [] -> return (); (x:xs) -> f (x :| xs)"
228-
rhs: whenNotNull m f
229-
- warn:
230-
lhs: "case m of [] -> pure () ; (x:xs) -> f (x :| xs)"
214+
lhs: "case m of [] -> stub ; (x:xs) -> f (x :| xs)"
231215
rhs: whenNotNull m f
232216
- warn:
233-
lhs: "case m of [] -> pass ; (x:xs) -> f (x :| xs)"
217+
lhs: "case m of (x:xs) -> f (x :| xs); [] -> stub"
234218
rhs: whenNotNull m f
235219
- warn:
236-
lhs: "case m of (x:xs) -> f (x :| xs); [] -> return ()"
237-
rhs: whenNotNull m f
238-
- warn:
239-
lhs: "case m of (x:xs) -> f (x :| xs); [] -> pure () "
240-
rhs: whenNotNull m f
241-
- warn:
242-
lhs: "case m of (x:xs) -> f (x :| xs); [] -> pass "
243-
rhs: whenNotNull m f
244-
- warn:
245-
lhs: "m >>= \\case [] -> pass ; (x:xs) -> f (x :| xs)"
220+
lhs: "m >>= \\case [] -> stub ; (x:xs) -> f (x :| xs)"
246221
rhs: whenNotNullM m f
247222
- warn:
248-
lhs: "m >>= \\case [] -> pure () ; (x:xs) -> f (x :| xs)"
249-
rhs: whenNotNullM m f
250-
- warn:
251-
lhs: "m >>= \\case [] -> return (); (x:xs) -> f (x :| xs)"
252-
rhs: whenNotNullM m f
253-
- warn:
254-
lhs: "m >>= \\case (x:xs) -> f (x :| xs); [] -> pass "
255-
rhs: whenNotNullM m f
256-
- warn:
257-
lhs: "m >>= \\case (x:xs) -> f (x :| xs); [] -> pure () "
258-
rhs: whenNotNullM m f
259-
- warn:
260-
lhs: "m >>= \\case (x:xs) -> f (x :| xs); [] -> return ()"
223+
lhs: "m >>= \\case (x:xs) -> f (x :| xs); [] -> stub"
261224
rhs: whenNotNullM m f
262225
- warn:
263226
lhs: mapMaybe leftToMaybe
@@ -292,27 +255,15 @@
292255
- warn:
293256
lhs: "MaybeT (pure m)"
294257
rhs: hoistMaybe m
295-
- warn:
296-
lhs: "MaybeT (return m)"
297-
rhs: hoistMaybe m
298258
- warn:
299259
lhs: MaybeT . pure
300260
rhs: hoistMaybe
301-
- warn:
302-
lhs: MaybeT . return
303-
rhs: hoistMaybe
304261
- warn:
305262
lhs: "ExceptT (pure m)"
306263
rhs: hoistEither m
307-
- warn:
308-
lhs: "ExceptT (return m)"
309-
rhs: hoistEither m
310264
- warn:
311265
lhs: ExceptT . pure
312266
rhs: hoistEither
313-
- warn:
314-
lhs: ExceptT . return
315-
rhs: hoistEither
316267
- warn:
317268
lhs: fromMaybe mempty
318269
rhs: maybeToMonoid
@@ -1525,16 +1476,16 @@
15251476
note: "'writeIORef' is already exported from Relude"
15261477
rhs: writeIORef
15271478
- warn:
1528-
lhs: "atomicModifyIORef ref (\\a -> (f a, ()))"
1479+
lhs: "atomicModifyIORef ref (\\a -> (f a, mempty))"
15291480
rhs: atomicModifyIORef_ ref f
15301481
- warn:
1531-
lhs: "atomicModifyIORef ref $ \\a -> (f a, ())"
1482+
lhs: "atomicModifyIORef ref $ \\a -> (f a, mempty)"
15321483
rhs: atomicModifyIORef_ ref f
15331484
- warn:
1534-
lhs: "atomicModifyIORef' ref $ \\a -> (f a, ())"
1485+
lhs: "atomicModifyIORef' ref $ \\a -> (f a, mempty)"
15351486
rhs: "atomicModifyIORef'_ ref f"
15361487
- warn:
1537-
lhs: "atomicModifyIORef' ref (\\a -> (f a, ()))"
1488+
lhs: "atomicModifyIORef' ref (\\a -> (f a, mempty))"
15381489
rhs: "atomicModifyIORef'_ ref f"
15391490
- warn:
15401491
lhs: Data.Text.IO.getLine
@@ -2767,8 +2718,32 @@
27672718
note: "Monad law"
27682719
rhs: id
27692720

2721+
- hint:
2722+
lhs: "join (f . pure)"
2723+
note: "Monad law"
2724+
rhs: (f =<< pure)
2725+
27702726
- hint:
27712727
lhs: "join . (f <<$>>)"
27722728
note: "Monad law"
27732729
rhs: fmap f . join
27742730

2731+
- hint:
2732+
lhs: "Data.Traversable.sequence"
2733+
note: "Modern name is `sequenceA`"
2734+
rhs: sequenceA
2735+
2736+
- hint:
2737+
lhs: "()"
2738+
note: "Use `mempty`"
2739+
rhs: mempty
2740+
2741+
- hint:
2742+
lhs: "return"
2743+
note: "Please, use `pure` instead. even GHC already deprecates `return` starting from `9.2`."
2744+
rhs: pure
2745+
2746+
- hint:
2747+
lhs: "map"
2748+
note: "Use `fmap`"
2749+
rhs: fmap

main/Main.hs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ import Nix.Eval
3636
main :: IO ()
3737
main =
3838
do
39-
time <- getCurrentTime
40-
opts <- execParser $ nixOptionsInfo time
39+
currentTime <- getCurrentTime
40+
opts <- execParser $ nixOptionsInfo currentTime
4141

4242
main' opts
4343

@@ -57,7 +57,7 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
5757
-- | The base case: read expressions from the last CLI directive (@[FILE]@) listed on the command line.
5858
loadFromCliFilePathList :: StdIO
5959
loadFromCliFilePathList =
60-
case filePaths of
60+
case getFilePaths of
6161
[] -> runRepl
6262
["-"] -> readExpressionFromStdin
6363
_paths -> processSeveralFiles (coerce _paths)
@@ -80,11 +80,11 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
8080
do
8181
let file = replaceExtension binaryCacheFile "nixc"
8282
processCLIOptions (pure file) =<< liftIO (readCache binaryCacheFile)
83-
) <$> readFrom
83+
) <$> getReadFrom
8484

8585
-- | The `--expr` option: read expression from the argument string
8686
loadLiteralExpression :: Maybe StdIO
87-
loadLiteralExpression = processExpr <$> expression
87+
loadLiteralExpression = processExpr <$> getExpression
8888

8989
-- | The `--file` argument: read expressions from the files listed in the argument file
9090
loadExpressionFromFile :: Maybe StdIO
@@ -96,7 +96,7 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
9696
(\case
9797
"-" -> Text.getContents
9898
_fp -> readFile _fp
99-
) <$> fromFile
99+
) <$> getFromFile
100100

101101
processExpr :: Text -> StdIO
102102
processExpr = handleResult mempty . parseNixTextLoc
@@ -110,13 +110,13 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
110110
bool
111111
errorWithoutStackTrace
112112
(liftIO . hPutStrLn stderr)
113-
ignoreErrors
113+
isIgnoreErrors
114114
$ "Parse failed: " <> show err
115115
)
116116

117117
(\ expr ->
118118
do
119-
when check $
119+
when isCheck $
120120
do
121121
expr' <- liftIO $ reduceExpr mpath expr
122122
either
@@ -138,28 +138,28 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
138138
@StdThun
139139
frames
140140

141-
when repl $
141+
when isRepl $
142142
withEmptyNixContext $
143143
bool
144144
Repl.main
145145
((Repl.main' . pure) =<< nixEvalExprLoc (coerce mpath) expr)
146-
evaluate
146+
isEvaluate
147147
)
148148

149149
-- 2021-07-15: NOTE: Logic of CLI Option processing is scattered over several functions, needs to be consolicated.
150150
processCLIOptions :: Maybe Path -> NExprLoc -> StdIO
151151
processCLIOptions mpath expr
152-
| evaluate =
152+
| isEvaluate =
153153
if
154-
| tracing -> evaluateExprWithEvaluator nixTracingEvalExprLoc expr
155-
| Just path <- reduce -> evaluateExprWithEvaluator (reduction path . coerce) expr
156-
| null arg || null argstr -> evaluateExprWithEvaluator nixEvalExprLoc expr
154+
| isTrace -> evaluateExprWith nixTracingEvalExprLoc expr
155+
| Just path <- getReduce -> evaluateExprWith (reduction path . coerce) expr
156+
| null getArg || null getArgstr -> evaluateExprWith nixEvalExprLoc expr
157157
| otherwise -> processResult printer <=< nixEvalExprLoc (coerce mpath) $ expr
158-
| xml = fail "Rendering expression trees to XML is not yet implemented"
159-
| json = fail "Rendering expression trees to JSON is not implemented"
160-
| verbose >= DebugInfo = liftIO . putStr . ppShow . stripAnnotation $ expr
161-
| cache , Just path <- mpath = liftIO . writeCache (replaceExtension path "nixc") $ expr
162-
| parseOnly = void . liftIO . Exception.evaluate . force $ expr
158+
| isXml = fail "Rendering expression trees to XML is not yet implemented"
159+
| isJson = fail "Rendering expression trees to JSON is not implemented"
160+
| getVerbosity >= DebugInfo = liftIO . putStr . ppShow . stripAnnotation $ expr
161+
| isCache , Just path <- mpath = liftIO . writeCache (replaceExtension path "nixc") $ expr
162+
| isParseOnly = void . liftIO . Exception.evaluate . force $ expr
163163
| otherwise =
164164
liftIO .
165165
renderIO
@@ -169,26 +169,26 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
169169
. stripAnnotation
170170
$ expr
171171
where
172-
evaluateExprWithEvaluator evaluator = evaluateExpression (coerce mpath) evaluator printer
172+
evaluateExprWith evaluator = evaluateExpression (coerce mpath) evaluator printer
173173

174174
printer
175175
:: StdVal
176176
-> StdIO
177177
printer
178-
| finder = findAttrs <=< fromValue @(AttrSet StdVal)
178+
| isFinder = findAttrs <=< fromValue @(AttrSet StdVal)
179179
| otherwise = printer'
180180
where
181181
printer'
182-
| xml = fun (stringIgnoreContext . toXML) normalForm
182+
| isXml = fun (ignoreContext . toXML) normalForm
183183
-- 2021-05-27: NOTE: With naive fix of the #941
184184
-- This is overall a naive printer implementation, as options should interact/respect one another.
185185
-- A nice question: "Should respect one another to what degree?": Go full combinator way, for which
186186
-- old Nix CLI is nototrious for (and that would mean to reimplement the old Nix CLI),
187187
-- OR: https://github.com/haskell-nix/hnix/issues/172 and have some sane standart/default behaviour for (most) keys.
188-
| json = fun (stringIgnoreContext . mempty . nvalueToJSONNixString) normalForm
189-
| strict = fun (show . prettyNValue) normalForm
190-
| values = fun (show . prettyNValueProv) removeEffects
191-
| otherwise = fun (show . prettyNValue) removeEffects
188+
| isJson = fun (ignoreContext . mempty . toJSONNixString) normalForm
189+
| isStrict = fun (show . prettyNValue) normalForm
190+
| isValues = fun (show . prettyNValueProv) removeEffects
191+
| otherwise = fun (show . prettyNValue) removeEffects
192192
where
193193
fun
194194
:: (b -> Text)

0 commit comments

Comments
 (0)