Skip to content

Commit 6d181b6

Browse files
committed
Reduce: clean-up with annotations
1 parent 4a7d586 commit 6d181b6

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

src/Nix/Reduce.hs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ staticImport pann path = do
112112
cur =
113113
NamedVar
114114
(StaticKey "__cur_file" :| mempty)
115-
(Fix (NLiteralPathAnnF pann path))
115+
(NLiteralPathAnn pann path)
116116
pos
117-
x' = Fix $ NLetAnnF span [cur] x
117+
x' = NLetAnn span [cur] x
118118
modify $ first $ HM.insert path x'
119119
local
120120
(const (pure path, mempty)) $
@@ -152,17 +152,17 @@ reduce
152152
-- | Reduce the variable to its value if defined.
153153
-- Leave it as it is otherwise.
154154
reduce (NSymAnnF ann var) =
155-
fromMaybe (Fix (NSymAnnF ann var)) <$> lookupVar var
155+
fromMaybe (NSymAnn ann var) <$> lookupVar var
156156

157157
-- | Reduce binary and integer negation.
158158
reduce (NUnaryAnnF uann op arg) =
159159
do
160160
x <- arg
161-
pure $ Fix $
161+
pure $
162162
case (op, x) of
163-
(NNeg, Fix (NConstantAnnF cann (NInt n))) -> NConstantAnnF cann $ NInt $ negate n
164-
(NNot, Fix (NConstantAnnF cann (NBool b))) -> NConstantAnnF cann $ NBool $ not b
165-
_ -> NUnaryAnnF uann op x
163+
(NNeg, NConstantAnn cann (NInt n)) -> NConstantAnn cann $ NInt $ negate n
164+
(NNot, NConstantAnn cann (NBool b)) -> NConstantAnn cann $ NBool $ not b
165+
_ -> NUnaryAnn uann op x
166166

167167
-- | Reduce function applications.
168168
--
@@ -171,31 +171,31 @@ reduce (NUnaryAnnF uann op arg) =
171171
-- * Reduce a lambda function by adding its name to the local
172172
-- scope and recursively reducing its body.
173173
reduce (NBinaryAnnF bann NApp fun arg) = fun >>= \case
174-
f@(Fix (NSymAnnF _ "import")) ->
174+
f@(NSymAnn _ "import") ->
175175
(\case
176-
-- Fix (NEnvPathAnnF pann origPath) -> staticImport pann origPath
177-
Fix (NLiteralPathAnnF pann origPath) -> staticImport pann origPath
178-
v -> pure $ Fix $ NBinaryAnnF bann NApp f v
176+
-- NEnvPathAnn pann origPath -> staticImport pann origPath
177+
NLiteralPathAnn pann origPath -> staticImport pann origPath
178+
v -> pure $ NBinaryAnn bann NApp f v
179179
) =<< arg
180180

181-
Fix (NAbsAnnF _ (Param name) body) ->
181+
NAbsAnn _ (Param name) body ->
182182
do
183183
x <- arg
184184
pushScope
185185
(coerce $ HM.singleton name x)
186186
(foldFix reduce body)
187187

188-
f -> Fix . NBinaryAnnF bann NApp f <$> arg
188+
f -> NBinaryAnn bann NApp f <$> arg
189189

190190
-- | Reduce an integer addition to its result.
191191
reduce (NBinaryAnnF bann op larg rarg) =
192192
do
193193
lval <- larg
194194
rval <- rarg
195-
pure $ Fix $
195+
pure $
196196
case (op, lval, rval) of
197-
(NPlus, Fix (NConstantAnnF ann (NInt x)), Fix (NConstantAnnF _ (NInt y))) -> NConstantAnnF ann $ NInt $ x + y
198-
_ -> NBinaryAnnF bann op lval rval
197+
(NPlus, NConstantAnn ann (NInt x), NConstantAnn _ (NInt y)) -> NConstantAnn ann $ NInt $ x + y
198+
_ -> NBinaryAnn bann op lval rval
199199

200200
-- | Reduce a select on a Set by substituting the set to the selected value.
201201
--
@@ -245,18 +245,18 @@ reduce e@(NSetAnnF ann NonRecursive binds) =
245245

246246
bool
247247
(Fix <$> sequence e)
248-
(clearScopes @NExprLoc $ Fix . NSetAnnF ann NonRecursive <$> traverse sequence binds)
248+
(clearScopes @NExprLoc $ NSetAnn ann NonRecursive <$> traverse sequence binds)
249249
usesInherit
250250

251251
-- Encountering a 'rec set' construction eliminates any hope of inlining
252252
-- definitions.
253253
reduce (NSetAnnF ann Recursive binds) =
254-
clearScopes @NExprLoc $ Fix . NSetAnnF ann Recursive <$> traverse sequence binds
254+
clearScopes @NExprLoc $ NSetAnn ann Recursive <$> traverse sequence binds
255255

256256
-- Encountering a 'with' construction eliminates any hope of inlining
257257
-- definitions.
258258
reduce (NWithAnnF ann scope body) =
259-
clearScopes @NExprLoc $ Fix <$> liftA2 (NWithAnnF ann) scope body
259+
clearScopes @NExprLoc $ liftA2 (NWithAnn ann) scope body
260260

261261
-- | Reduce a let binds section by pushing lambdas,
262262
-- constants and strings to the body scope.
@@ -271,9 +271,9 @@ reduce (NLetAnnF ann binds body) =
271271
let
272272
defcase =
273273
\case
274-
d@(Fix NAbsAnnF {}) -> pure (name, d)
275-
d@(Fix NConstantAnnF{}) -> pure (name, d)
276-
d@(Fix NStrAnnF {}) -> pure (name, d)
274+
d@(NAbsAnn {}) -> pure (name, d)
275+
d@(NConstantAnn{}) -> pure (name, d)
276+
d@(NStrAnn {}) -> pure (name, d)
277277
_ -> Nothing
278278
in
279279
defcase <$> def
@@ -287,7 +287,7 @@ reduce (NLetAnnF ann binds body) =
287287
-- NamedVar (StaticKey name _ :| []) _ ->
288288
-- name `S.member` names
289289
-- _ -> True
290-
pure $ Fix $ NLetAnnF ann binds' body'
290+
pure $ NLetAnn ann binds' body'
291291
-- where
292292
-- go m [] = pure m
293293
-- go m (x:xs) = case x of
@@ -300,15 +300,15 @@ reduce (NLetAnnF ann binds body) =
300300
-- the condition is a boolean constant.
301301
reduce e@(NIfAnnF _ b t f) =
302302
(\case
303-
Fix (NConstantAnnF _ (NBool b')) -> if b' then t else f
304-
_ -> Fix <$> sequence e
303+
NConstantAnn _ (NBool b') -> bool f t b'
304+
_ -> Fix <$> sequence e
305305
) =<< b
306306

307307
-- | Reduce an assert atom to its encapsulated
308308
-- symbol if the assertion is a boolean constant.
309309
reduce e@(NAssertAnnF _ b body) =
310310
(\case
311-
Fix (NConstantAnnF _ (NBool b')) | b' -> body
311+
NConstantAnn _ (NBool b') | b' -> body
312312
_ -> Fix <$> sequence e
313313
) =<< b
314314

@@ -319,10 +319,10 @@ reduce (NAbsAnnF ann params body) = do
319319
let
320320
scope = coerce $
321321
case params' of
322-
Param name -> one (name, Fix $ NSymAnnF ann name)
322+
Param name -> one (name, NSymAnn ann name)
323323
ParamSet _ _ pset ->
324-
HM.fromList $ (\(k, _) -> (k, Fix $ NSymAnnF ann k)) <$> pset
325-
Fix . NAbsAnnF ann params' <$> pushScope scope body
324+
HM.fromList $ (\(k, _) -> (k, NSymAnn ann k)) <$> pset
325+
NAbsAnn ann params' <$> pushScope scope body
326326

327327
reduce v = Fix <$> sequence v
328328

@@ -349,7 +349,7 @@ pruneTree opts =
349349
\(FlaggedF (b, Compose x)) ->
350350
bool
351351
Nothing
352-
(Fix . Compose <$> traverse prune x)
352+
(annUnitToAnn <$> traverse prune x)
353353
<$> liftIO (readIORef b)
354354
where
355355
prune :: NExprF (Maybe NExprLoc) -> Maybe (NExprF NExprLoc)

0 commit comments

Comments
 (0)