Skip to content

Commit 803af1b

Browse files
Merge #849: Treewide: Basic polymorphic refactoring
* Derivation: m clean-up * Eval: m improve readability * upd remainding (map -> fmap) * upd remaining ((++) -> (<>)) * (Just -> pure) * (return -> pure) * ((>>) -> (*>)) * ([] -> mempty) * (Nothing -> mempty)
2 parents 1939be1 + 26f5211 commit 803af1b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+464
-460
lines changed

main/Main.hs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,24 @@ main = do
5151
runWithBasicEffectsIO opts $ case readFrom opts of
5252
Just path -> do
5353
let file = addExtension (dropExtension path) "nixc"
54-
process opts (Just file) =<< liftIO (readCache path)
54+
process opts (pure file) =<< liftIO (readCache path)
5555
Nothing -> case expression opts of
56-
Just s -> handleResult opts Nothing (parseNixTextLoc s)
56+
Just s -> handleResult opts mempty (parseNixTextLoc s)
5757
Nothing -> case fromFile opts of
5858
Just "-" -> mapM_ (processFile opts) . lines =<< liftIO getContents
5959
Just path ->
6060
mapM_ (processFile opts) . lines =<< liftIO (readFile path)
6161
Nothing -> case filePaths opts of
62-
[] -> withNixContext Nothing Repl.main
62+
[] -> withNixContext mempty Repl.main
6363
["-"] ->
64-
handleResult opts Nothing
64+
handleResult opts mempty
6565
. parseNixTextLoc
6666
=<< liftIO Text.getContents
6767
paths -> mapM_ (processFile opts) paths
6868
where
6969
processFile opts path = do
7070
eres <- parseNixFileLoc path
71-
handleResult opts (Just path) eres
71+
handleResult opts (pure path) eres
7272

7373
handleResult opts mpath = \case
7474
Failure err ->
@@ -77,14 +77,14 @@ main = do
7777
else errorWithoutStackTrace
7878
)
7979
$ "Parse failed: "
80-
++ show err
80+
<> show err
8181

8282
Success expr -> do
8383
when (check opts) $ do
8484
expr' <- liftIO (reduceExpr mpath expr)
8585
case HM.inferTop Env.empty [("it", stripAnnotation expr')] of
86-
Left err -> errorWithoutStackTrace $ "Type error: " ++ PS.ppShow err
87-
Right ty -> liftIO $ putStrLn $ "Type of expression: " ++ PS.ppShow
86+
Left err -> errorWithoutStackTrace $ "Type error: " <> PS.ppShow err
87+
Right ty -> liftIO $ putStrLn $ "Type of expression: " <> PS.ppShow
8888
(fromJust (Map.lookup "it" (Env.types ty)))
8989

9090
-- liftIO $ putStrLn $ runST $
@@ -102,8 +102,8 @@ main = do
102102
if evaluate opts
103103
then do
104104
val <- Nix.nixEvalExprLoc mpath expr
105-
withNixContext Nothing (Repl.main' $ Just val)
106-
else withNixContext Nothing Repl.main
105+
withNixContext mempty (Repl.main' $ pure val)
106+
else withNixContext mempty Repl.main
107107

108108
process opts mpath expr
109109
| evaluate opts
@@ -165,9 +165,9 @@ main = do
165165
where
166166
go prefix s = do
167167
xs <- forM (sortOn fst (M.toList s)) $ \(k, nv) -> case nv of
168-
Free v -> pure (k, Just (Free v))
168+
Free v -> pure (k, pure (Free v))
169169
Pure (StdThunk (extract -> Thunk _ _ ref)) -> do
170-
let path = prefix ++ Text.unpack k
170+
let path = prefix <> Text.unpack k
171171
(_, descend) = filterEntry path k
172172
val <- readVar @(StandardT (StdIdT IO)) ref
173173
case val of
@@ -176,14 +176,14 @@ main = do
176176
| otherwise -> pure (k, Nothing)
177177

178178
forM_ xs $ \(k, mv) -> do
179-
let path = prefix ++ Text.unpack k
179+
let path = prefix <> Text.unpack k
180180
(report, descend) = filterEntry path k
181181
when report $ do
182182
liftIO $ putStrLn path
183183
when descend $ case mv of
184184
Nothing -> pure ()
185185
Just v -> case v of
186-
NVSet s' _ -> go (path ++ ".") s'
186+
NVSet s' _ -> go (path <> ".") s'
187187
_ -> pure ()
188188
where
189189
filterEntry path k = case (path, k) of
@@ -204,12 +204,12 @@ main = do
204204
_ -> (True, True)
205205

206206
forceEntry k v =
207-
catch (Just <$> demand v pure) $ \(NixException frames) -> do
207+
catch (pure <$> demand v pure) $ \(NixException frames) -> do
208208
liftIO
209209
. putStrLn
210-
. ("Exception forcing " ++)
211-
. (k ++)
212-
. (": " ++)
210+
. ("Exception forcing " <>)
211+
. (k <>)
212+
. (": " <>)
213213
. show
214214
=<< renderFrames @(StdValue (StandardT (StdIdT IO)))
215215
@(StdThunk (StandardT (StdIdT IO)))
@@ -228,7 +228,7 @@ main = do
228228
-> m (NValue t f m)
229229
handleReduced path (expr', eres) = do
230230
liftIO $ do
231-
putStrLn $ "Wrote winnowed expression tree to " ++ path
231+
putStrLn $ "Wrote winnowed expression tree to " <> path
232232
writeFile path $ show $ prettyNix (stripAnnotation expr')
233233
case eres of
234234
Left err -> throwM err

main/Repl.hs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ main' iniVal = initState iniVal >>= \s -> flip evalStateT s
7676
banner
7777
cmd
7878
options
79-
(Just commandPrefix)
80-
(Just "paste")
79+
(pure commandPrefix)
80+
(pure "paste")
8181
completion
82-
(rcFile >> greeter)
82+
(rcFile *> greeter)
8383
finalizer
8484
where
8585
commandPrefix = ':'
@@ -119,7 +119,7 @@ main' iniVal = initState iniVal >>= \s -> flip evalStateT s
119119
-> System.Console.Repline.Options m
120120
-> String
121121
-> m ()
122-
optMatcher s [] _ = liftIO $ putStrLn $ "No such command :" ++ s
122+
optMatcher s [] _ = liftIO $ putStrLn $ "No such command :" <> s
123123
optMatcher s ((x, m) : xs) args
124124
| s `Data.List.isPrefixOf` x = m args
125125
| otherwise = optMatcher s xs args
@@ -166,7 +166,7 @@ initState mIni = do
166166
where
167167
evalText :: (MonadNix e t f m) => Text -> m (NValue t f m)
168168
evalText expr = case parseNixTextLoc expr of
169-
Failure e -> error $ "Impossible happened: Unable to parse expression - '" ++ Data.Text.unpack expr ++ "' error was " ++ show e
169+
Failure e -> error $ "Impossible happened: Unable to parse expression - '" <> Data.Text.unpack expr <> "' error was " <> show e
170170
Success e -> do evalExprLoc e
171171

172172
type Repl e t f m = HaskelineT (StateT (IState t f m) m)
@@ -212,14 +212,14 @@ exec update source = do
212212
-- Update the interpreter state
213213
when (update && isBinding) $ do
214214
-- Set `replIt` to last entered expression
215-
put st { replIt = Just expr }
215+
put st { replIt = pure expr }
216216

217217
-- If the result value is a set, update our context with it
218218
case val of
219219
NVSet xs _ -> put st { replCtx = Data.HashMap.Lazy.union xs (replCtx st) }
220220
_ -> pure ()
221221

222-
pure $ Just val
222+
pure $ pure val
223223
where
224224
-- If parsing fails, turn the input into singleton attribute set
225225
-- and try again.
@@ -292,7 +292,7 @@ typeof
292292
typeof args = do
293293
st <- get
294294
mVal <- case Data.HashMap.Lazy.lookup line (replCtx st) of
295-
Just val -> pure $ Just val
295+
Just val -> pure $ pure val
296296
Nothing -> do
297297
exec False line
298298

@@ -329,7 +329,7 @@ completion
329329
:: (MonadNix e t f m, MonadIO m)
330330
=> CompleterStyle (StateT (IState t f m) m)
331331
completion = System.Console.Repline.Prefix
332-
(completeWordWithPrev (Just '\\') separators completeFunc)
332+
(completeWordWithPrev (pure '\\') separators completeFunc)
333333
defaultMatcher
334334
where
335335
separators :: String
@@ -360,7 +360,7 @@ completeFunc reversedPrev word
360360
= do
361361
s <- get
362362
case Data.HashMap.Lazy.lookup var (replCtx s) of
363-
Nothing -> pure []
363+
Nothing -> pure mempty
364364
Just binding -> do
365365
candidates <- lift $ algebraicComplete subFields binding
366366
pure $ notFinished <$> listCompletion (Data.Text.unpack . (var <>) <$> candidates)
@@ -396,14 +396,14 @@ completeFunc reversedPrev word
396396
[_] -> pure $ keys m
397397
f:fs ->
398398
case Data.HashMap.Lazy.lookup f m of
399-
Nothing -> pure []
399+
Nothing -> pure mempty
400400
Just e ->
401401
demand e
402402
(\e' -> (fmap . fmap) (("." <> f) <>) $ algebraicComplete fs e')
403403

404404
in case val of
405405
NVSet xs _ -> withMap xs
406-
_ -> pure []
406+
_ -> pure mempty
407407

408408
-- HelpOption inspired by Dhall Repl
409409
-- with `Doc` instead of String for syntax and doc

src/Nix.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ evaluateExpression
107107
-> m a
108108
evaluateExpression mpath evaluator handler expr = do
109109
opts :: Options <- asks (view hasLens)
110-
args <- traverse (traverse eval') $ fmap (second parseArg) (arg opts) ++ fmap
110+
args <- traverse (traverse eval') $ fmap (second parseArg) (arg opts) <> fmap
111111
(second mkStr)
112112
(argstr opts)
113113
evaluator mpath expr >>= \f -> demand f $ \f' ->
@@ -144,22 +144,22 @@ processResult h val = do
144144
_ ->
145145
errorWithoutStackTrace
146146
$ "Expected a list for selector '"
147-
++ show n
148-
++ "', but got: "
149-
++ show v
147+
<> show n
148+
<> "', but got: "
149+
<> show v
150150
go (k : ks) v = demand v $ \case
151151
NVSet xs _ -> case M.lookup k xs of
152152
Nothing ->
153153
errorWithoutStackTrace
154154
$ "Set does not contain key '"
155-
++ Text.unpack k
156-
++ "'"
155+
<> Text.unpack k
156+
<> "'"
157157
Just v' -> case ks of
158158
[] -> h v'
159159
_ -> go ks v'
160160
_ ->
161161
errorWithoutStackTrace
162162
$ "Expected a set for selector '"
163-
++ Text.unpack k
164-
++ "', but got: "
165-
++ show v
163+
<> Text.unpack k
164+
<> "', but got: "
165+
<> show v

src/Nix/Builtins.hs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -316,27 +316,27 @@ foldNixPath
316316
foldNixPath f z = do
317317
mres <- lookupVar "__includes"
318318
dirs <- case mres of
319-
Nothing -> pure []
319+
Nothing -> pure mempty
320320
Just v -> demand v $ fromValue . Deeper
321321
mPath <- getEnvVar "NIX_PATH"
322322
mDataDir <- getEnvVar "NIX_DATA_DIR"
323323
dataDir <- maybe getDataDir pure mDataDir
324324
foldrM go z
325325
$ fmap (fromInclude . stringIgnoreContext) dirs
326326
<> case mPath of
327-
Nothing -> []
327+
Nothing -> mempty
328328
Just str -> uriAwareSplit (Text.pack str)
329329
<> [ fromInclude $ Text.pack $ "nix=" <> dataDir <> "/nix/corepkgs" ]
330330
where
331331
fromInclude x | "://" `Text.isInfixOf` x = (x, PathEntryURI)
332332
| otherwise = (x, PathEntryPath)
333333
go (x, ty) rest = case Text.splitOn "=" x of
334-
[p] -> f (Text.unpack p) Nothing ty rest
335-
[n, p] -> f (Text.unpack p) (Just (Text.unpack n)) ty rest
334+
[p] -> f (Text.unpack p) mempty ty rest
335+
[n, p] -> f (Text.unpack p) (pure (Text.unpack n)) ty rest
336336
_ -> throwError $ ErrorCall $ "Unexpected entry in NIX_PATH: " <> show x
337337

338338
nixPath :: MonadNix e t f m => m (NValue t f m)
339-
nixPath = fmap nvList $ flip foldNixPath [] $ \p mn ty rest ->
339+
nixPath = fmap nvList $ flip foldNixPath mempty $ \p mn ty rest ->
340340
pure
341341
$ flip nvSet mempty ( M.fromList
342342
[ case ty of
@@ -512,7 +512,7 @@ versionComponentSeparators = ".-"
512512

513513
splitVersion :: Text -> [VersionComponent]
514514
splitVersion s = case Text.uncons s of
515-
Nothing -> []
515+
Nothing -> mempty
516516
Just (h, t)
517517
| h `elem` versionComponentSeparators
518518
-> splitVersion t
@@ -575,7 +575,7 @@ splitDrvName s =
575575
breakAfterFirstItem :: (a -> Bool) -> [a] -> ([a], [a])
576576
breakAfterFirstItem f = \case
577577
h : t -> let (a, b) = break f t in (h : a, b)
578-
[] -> ([], [])
578+
[] -> (mempty, mempty)
579579
(namePieces, versionPieces) =
580580
breakAfterFirstItem isFirstVersionPiece pieces
581581
in
@@ -825,7 +825,7 @@ elem_ x = toValue <=< anyM (valueEqM x) <=< fromValue
825825
elemAt :: [a] -> Int -> Maybe a
826826
elemAt ls i = case drop i ls of
827827
[] -> Nothing
828-
a : _ -> Just a
828+
a : _ -> pure a
829829

830830
elemAt_
831831
:: MonadNix e t f m
@@ -910,7 +910,7 @@ genericClosure = fromValue @(AttrSet (NValue t f m)) >=> \s ->
910910
-> [NValue t f m]
911911
-> Set (WValue t f m)
912912
-> m (Set (WValue t f m), [NValue t f m])
913-
go _ [] ks = pure (ks, [])
913+
go _ [] ks = pure (ks, mempty)
914914
go op (t : ts) ks = demand t $ \v -> fromValue @(AttrSet (NValue t f m)) v >>= \s -> do
915915
k <- attrsetGet "key" s
916916
demand k $ \k' -> do
@@ -1108,7 +1108,7 @@ scopedImport asetArg pathArg = fromValue @(AttrSet (NValue t f m)) asetArg >>= \
11081108
traceM $ "Current file being evaluated is: " <> show p'
11091109
pure $ takeDirectory p' </> path
11101110
clearScopes @(NValue t f m)
1111-
$ withNixContext (Just path')
1111+
$ withNixContext (pure path')
11121112
$ pushScope s
11131113
$ importPath @t @f @m path'
11141114

@@ -1480,7 +1480,7 @@ appendContext x y = demand x $ \x' -> demand y $ \y' -> case (x', y') of
14801480
allOutputs <- maybe (pure False) (demand ?? fromValue)
14811481
$ M.lookup "allOutputs" attrs
14821482
outputs <- case M.lookup "outputs" attrs of
1483-
Nothing -> pure []
1483+
Nothing -> pure mempty
14841484
Just os -> demand os $ \case
14851485
NVList vs ->
14861486
forM vs $ fmap stringIgnoreContext . fromValue

src/Nix/Cache.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ readCache path = do
2424
eres <- C.unsafeReadCompact path
2525
case eres of
2626
Left err -> error $ "Error reading cache file: " <> err
27-
Right expr -> return $ C.getCompact expr
27+
Right expr -> pure $ C.getCompact expr
2828
#else
2929
#ifdef MIN_VERSION_serialise
3030
eres <- S.deserialiseOrFail <$> BS.readFile path
3131
case eres of
3232
Left err -> error $ "Error reading cache file: " <> show err
33-
Right expr -> return expr
33+
Right expr -> pure expr
3434
#else
3535
error "readCache not implemented for this platform"
3636
#endif

src/Nix/Cited.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ data NCited m v a = NCited
3838
deriving (Generic, Typeable, Functor, Foldable, Traversable, Show)
3939

4040
instance Applicative (NCited m v) where
41-
pure = NCited []
41+
pure = NCited mempty
4242
NCited xs f <*> NCited ys x = NCited (xs <> ys) (f x)
4343

4444
instance Comonad (NCited m v) where

src/Nix/Cited/Basic.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ instance ( Has e Options
6666
(Fix (Compose (Ann s e))))) =
6767
let e' = Compose (Ann s (Nothing <$ e))
6868
in [Provenance scope e']
69-
go _ = []
69+
go _ = mempty
7070
ps = concatMap (go . frame) frames
7171

7272
fmap (Cited . NCited ps) . thunk $ mv
73-
else fmap (Cited . NCited []) . thunk $ mv
73+
else fmap (Cited . NCited mempty) . thunk $ mv
7474

7575
thunkId (Cited (NCited _ t)) = thunkId @_ @m t
7676

src/Nix/Context.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ instance Has (Context m t) Options where
3232
hasLens f a = (\x -> a { options = x }) <$> f (options a)
3333

3434
newContext :: Options -> Context m t
35-
newContext = Context emptyScopes nullSpan []
35+
newContext = Context emptyScopes nullSpan mempty

0 commit comments

Comments
 (0)