Skip to content

Commit 7110040

Browse files
committed
Reduce: refactor
1 parent ea18e59 commit 7110040

File tree

1 file changed

+62
-41
lines changed

1 file changed

+62
-41
lines changed

src/Nix/Reduce.hs

Lines changed: 62 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,14 @@ reduce (NSym_ ann var) =
185185
fromMaybe (Fix (NSym_ ann var)) <$> lookupVar var
186186

187187
-- | Reduce binary and integer negation.
188-
reduce (NUnary_ uann op arg) = arg >>= \x -> case (op, x) of
189-
(NNeg, Fix (NConstant_ cann (NInt n))) ->
190-
pure $ Fix $ NConstant_ cann (NInt (negate n))
191-
(NNot, Fix (NConstant_ cann (NBool b))) ->
192-
pure $ Fix $ NConstant_ cann (NBool (not b))
193-
_ -> pure $ Fix $ NUnary_ uann op x
188+
reduce (NUnary_ uann op arg) =
189+
do
190+
x <- arg
191+
pure $ Fix $
192+
case (op, x) of
193+
(NNeg, Fix (NConstant_ cann (NInt n))) -> NConstant_ cann (NInt (negate n))
194+
(NNot, Fix (NConstant_ cann (NBool b))) -> NConstant_ cann (NBool (not b))
195+
_ -> NUnary_ uann op x
194196

195197
-- | Reduce function applications.
196198
--
@@ -199,25 +201,31 @@ reduce (NUnary_ uann op arg) = arg >>= \x -> case (op, x) of
199201
-- * Reduce a lambda function by adding its name to the local
200202
-- scope and recursively reducing its body.
201203
reduce (NBinary_ bann NApp fun arg) = fun >>= \case
202-
f@(Fix (NSym_ _ "import")) -> arg >>= \case
203-
-- Fix (NEnvPath_ pann origPath) -> staticImport pann origPath
204-
Fix (NLiteralPath_ pann origPath) -> staticImport pann origPath
205-
v -> pure $ Fix $ NBinary_ bann NApp f v
206-
207-
Fix (NAbs_ _ (Param name) body) -> do
208-
x <- arg
209-
pushScope (M.singleton name x) (foldFix reduce body)
204+
f@(Fix (NSym_ _ "import")) ->
205+
(\case
206+
-- Fix (NEnvPath_ pann origPath) -> staticImport pann origPath
207+
Fix (NLiteralPath_ pann origPath) -> staticImport pann origPath
208+
v -> pure $ Fix $ NBinary_ bann NApp f v
209+
) =<< arg
210+
211+
Fix (NAbs_ _ (Param name) body) ->
212+
do
213+
x <- arg
214+
pushScope
215+
(M.singleton name x)
216+
(foldFix reduce body)
210217

211218
f -> Fix . NBinary_ bann NApp f <$> arg
212219

213220
-- | Reduce an integer addition to its result.
214-
reduce (NBinary_ bann op larg rarg) = do
215-
lval <- larg
216-
rval <- rarg
217-
case (op, lval, rval) of
218-
(NPlus, Fix (NConstant_ ann (NInt x)), Fix (NConstant_ _ (NInt y))) ->
219-
pure $ Fix (NConstant_ ann (NInt (x + y)))
220-
_ -> pure $ Fix $ NBinary_ bann op lval rval
221+
reduce (NBinary_ bann op larg rarg) =
222+
do
223+
lval <- larg
224+
rval <- rarg
225+
pure $ Fix $
226+
case (op, lval, rval) of
227+
(NPlus, Fix (NConstant_ ann (NInt x)), Fix (NConstant_ _ (NInt y))) -> NConstant_ ann (NInt (x + y))
228+
_ -> NBinary_ bann op lval rval
221229

222230
-- | Reduce a select on a Set by substituting the set to the selected value.
223231
--
@@ -235,7 +243,7 @@ reduce base@(NSelect_ _ _ attrs _)
235243
sId = Fix <$> sequence base
236244
-- The selection AttrPath is composed of StaticKeys.
237245
sAttrPath (StaticKey _ : xs) = sAttrPath xs
238-
sAttrPath [] = True
246+
sAttrPath [] = True
239247
sAttrPath _ = False
240248
-- Find appropriate bind in set's binds.
241249
findBind [] _ = Nothing
@@ -322,22 +330,28 @@ reduce (NLet_ ann binds body) =
322330

323331
-- | Reduce an if to the relevant path if
324332
-- the condition is a boolean constant.
325-
reduce e@(NIf_ _ b t f) = b >>= \case
326-
Fix (NConstant_ _ (NBool b')) -> if b' then t else f
327-
_ -> Fix <$> sequence e
333+
reduce e@(NIf_ _ b t f) =
334+
(\case
335+
Fix (NConstant_ _ (NBool b')) -> if b' then t else f
336+
_ -> Fix <$> sequence e
337+
) =<< b
328338

329339
-- | Reduce an assert atom to its encapsulated
330340
-- symbol if the assertion is a boolean constant.
331-
reduce e@(NAssert_ _ b body) = b >>= \case
332-
Fix (NConstant_ _ (NBool b')) | b' -> body
333-
_ -> Fix <$> sequence e
341+
reduce e@(NAssert_ _ b body) =
342+
(\case
343+
Fix (NConstant_ _ (NBool b')) | b' -> body
344+
_ -> Fix <$> sequence e
345+
) =<< b
334346

335347
reduce (NAbs_ ann params body) = do
336348
params' <- sequence params
337349
-- Make sure that variable definitions in scope do not override function
338350
-- arguments.
339-
let args = case params' of
340-
Param name -> M.singleton name (Fix (NSym_ ann name))
351+
let
352+
args =
353+
case params' of
354+
Param name -> M.singleton name (Fix (NSym_ ann name))
341355
ParamSet pset _ _ ->
342356
M.fromList $ fmap (\(k, _) -> (k, Fix (NSym_ ann k))) pset
343357
Fix . NAbs_ ann params' <$> pushScope args body
@@ -346,7 +360,7 @@ reduce v = Fix <$> sequence v
346360

347361
-- newtype FlaggedF f r = FlaggedF { flagged :: (IORef Bool, f r) }
348362
newtype FlaggedF f r = FlaggedF (IORef Bool, f r)
349-
deriving (Functor, Foldable, Traversable)
363+
deriving (Functor, Foldable, Traversable)
350364

351365
instance Show (f r) => Show (FlaggedF f r) where
352366
show (FlaggedF (_, x)) = show x
@@ -362,9 +376,16 @@ flagExprLoc = foldFixM $ \x -> do
362376
-- stripFlags = foldFix $ Fix . snd . flagged
363377

364378
pruneTree :: MonadIO n => Options -> Flagged NExprLocF -> n (Maybe NExprLoc)
365-
pruneTree opts = foldFixM $ \(FlaggedF (b, Compose x)) -> do
366-
used <- liftIO $ readIORef b
367-
pure $ if used then Fix . Compose <$> traverse prune x else Nothing
379+
pruneTree opts =
380+
foldFixM $
381+
\(FlaggedF (b, Compose x)) ->
382+
do
383+
used <- liftIO $ readIORef b
384+
pure $
385+
bool
386+
Nothing
387+
(Fix . Compose <$> traverse prune x)
388+
used
368389
where
369390
prune :: NExprF (Maybe NExprLoc) -> Maybe (NExprF NExprLoc)
370391
prune = \case
@@ -389,7 +410,7 @@ pruneTree opts = foldFixM $ \(FlaggedF (b, Compose x)) -> do
389410

390411
-- These are the only short-circuiting binary operators
391412
NBinary NAnd (Just (Fix (Compose (Ann _ larg)))) _ -> pure larg
392-
NBinary NOr (Just (Fix (Compose (Ann _ larg)))) _ -> pure larg
413+
NBinary NOr (Just (Fix (Compose (Ann _ larg)))) _ -> pure larg
393414

394415
-- If the function was never called, it means its argument was in a
395416
-- thunk that was forced elsewhere.
@@ -427,16 +448,16 @@ pruneTree opts = foldFixM $ \(FlaggedF (b, Compose x)) -> do
427448
:: Antiquoted Text (Maybe NExprLoc) -> Maybe (Antiquoted Text NExprLoc)
428449
pruneAntiquotedText (Plain v) = pure (Plain v)
429450
pruneAntiquotedText EscapedNewline = pure EscapedNewline
430-
pruneAntiquotedText (Antiquoted Nothing ) = Nothing
431451
pruneAntiquotedText (Antiquoted (Just k)) = pure (Antiquoted k)
452+
pruneAntiquotedText (Antiquoted Nothing ) = Nothing
432453

433454
pruneAntiquoted
434455
:: Antiquoted (NString (Maybe NExprLoc)) (Maybe NExprLoc)
435456
-> Maybe (Antiquoted (NString NExprLoc) NExprLoc)
436457
pruneAntiquoted (Plain v) = pure (Plain (pruneString v))
437458
pruneAntiquoted EscapedNewline = pure EscapedNewline
438-
pruneAntiquoted (Antiquoted Nothing ) = Nothing
439459
pruneAntiquoted (Antiquoted (Just k)) = pure (Antiquoted k)
460+
pruneAntiquoted (Antiquoted Nothing ) = Nothing
440461

441462
pruneKeyName :: NKeyName (Maybe NExprLoc) -> NKeyName NExprLoc
442463
pruneKeyName (StaticKey n) = StaticKey n
@@ -453,12 +474,12 @@ pruneTree opts = foldFixM $ \(FlaggedF (b, Compose x)) -> do
453474
| otherwise = ParamSet (fmap (second (fmap (fromMaybe nNull))) xs) b n
454475

455476
pruneBinding :: Binding (Maybe NExprLoc) -> Maybe (Binding NExprLoc)
456-
pruneBinding (NamedVar _ Nothing _) = Nothing
457-
pruneBinding (NamedVar xs (Just x) pos) =
477+
pruneBinding (NamedVar _ Nothing _) = Nothing
478+
pruneBinding (NamedVar xs (Just x) pos) =
458479
pure (NamedVar (NE.map pruneKeyName xs) x pos)
459480
pruneBinding (Inherit _ [] _) = Nothing
460-
pruneBinding (Inherit (join -> Nothing) _ _) = Nothing
461-
pruneBinding (Inherit (join -> m) xs pos) =
481+
pruneBinding (Inherit (join -> Nothing) _ _) = Nothing
482+
pruneBinding (Inherit (join -> m) xs pos) =
462483
pure (Inherit m (fmap pruneKeyName xs) pos)
463484

464485
reducingEvalExpr

0 commit comments

Comments
 (0)