Skip to content

Commit 82b2d8a

Browse files
infinisiljwiegley
authored andcommitted
Use NSet for both recursive and non-recursive sets
Simplifies the AST
1 parent 3db38d0 commit 82b2d8a

File tree

10 files changed

+74
-70
lines changed

10 files changed

+74
-70
lines changed

src/Nix/Eval.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ eval (NList l ) = do
141141
scope <- currentScopes
142142
for l (defer @v @m . withScopes @v scope) >>= toValue
143143

144-
eval (NSet binds) =
145-
evalBinds False (desugarBinds (eval . NSet) binds) >>= toValue
144+
eval (NSet NNonRecursive binds) =
145+
evalBinds False (desugarBinds (eval . NSet NNonRecursive) binds) >>= toValue
146146

147-
eval (NRecSet binds) =
148-
evalBinds True (desugarBinds (eval . NSet) binds) >>= toValue
147+
eval (NSet NRecursive binds) =
148+
evalBinds True (desugarBinds (eval . NSet NNonRecursive) binds) >>= toValue
149149

150150
eval (NLet binds body ) = evalBinds True binds >>= (pushScope ?? body) . fst
151151

src/Nix/Expr/Shorthands.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Text.Megaparsec.Pos ( SourcePos )
1818
mkInt :: Integer -> NExpr
1919
mkInt = Fix . mkIntF
2020

21+
2122
mkIntF :: Integer -> NExprF a
2223
mkIntF = NConstant . NInt
2324

@@ -101,10 +102,10 @@ mkParamset :: [(Text, Maybe NExpr)] -> Bool -> Params NExpr
101102
mkParamset params variadic = ParamSet params variadic Nothing
102103

103104
mkRecSet :: [Binding NExpr] -> NExpr
104-
mkRecSet = Fix . NRecSet
105+
mkRecSet = Fix . NSet NRecursive
105106

106107
mkNonRecSet :: [Binding NExpr] -> NExpr
107-
mkNonRecSet = Fix . NSet
108+
mkNonRecSet = Fix . NSet NNonRecursive
108109

109110
mkLets :: [Binding NExpr] -> NExpr -> NExpr
110111
mkLets bindings = Fix . NLet bindings
@@ -161,10 +162,9 @@ infixr 2 $=
161162
-- `let a = 1; b = 2; c = 3; in 4`.
162163
appendBindings :: [Binding NExpr] -> NExpr -> NExpr
163164
appendBindings newBindings (Fix e) = case e of
164-
NLet bindings e' -> Fix $ NLet (bindings <> newBindings) e'
165-
NSet bindings -> Fix $ NSet (bindings <> newBindings)
166-
NRecSet bindings -> Fix $ NRecSet (bindings <> newBindings)
167-
_ -> error "Can only append bindings to a set or a let"
165+
NLet bindings e' -> Fix $ NLet (bindings <> newBindings) e'
166+
NSet recur bindings -> Fix $ NSet recur (bindings <> newBindings)
167+
_ -> error "Can only append bindings to a set or a let"
168168

169169
-- | Applies a transformation to the body of a nix function.
170170
modifyFunctionBody :: (NExpr -> NExpr) -> NExpr -> NExpr
@@ -182,11 +182,11 @@ letE varName varExpr = letsE [(varName, varExpr)]
182182

183183
-- | Make an attribute set (non-recursive).
184184
attrsE :: [(Text, NExpr)] -> NExpr
185-
attrsE pairs = Fix $ NSet (map (uncurry bindTo) pairs)
185+
attrsE pairs = Fix $ NSet NNonRecursive (map (uncurry bindTo) pairs)
186186

187187
-- | Make an attribute set (recursive).
188188
recAttrsE :: [(Text, NExpr)] -> NExpr
189-
recAttrsE pairs = Fix $ NRecSet (map (uncurry bindTo) pairs)
189+
recAttrsE pairs = Fix $ NSet NRecursive (map (uncurry bindTo) pairs)
190190

191191
-- | Logical negation.
192192
mkNot :: NExpr -> NExpr

src/Nix/Expr/Types.hs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,8 @@ data NExprF r
111111
-- as @NSym "f"@ and @a@ as @NSym "a"@.
112112
| NList ![r]
113113
-- ^ A list literal.
114-
| NSet ![Binding r]
115-
-- ^ An attribute set literal, not recursive.
116-
| NRecSet ![Binding r]
117-
-- ^ An attribute set literal, recursive.
114+
| NSet !NRecordType ![Binding r]
115+
-- ^ An attribute set literal
118116
| NLiteralPath !FilePath
119117
-- ^ A path expression, which is evaluated to a store path. The path here
120118
-- can be relative, in which case it's evaluated relative to the file in
@@ -436,6 +434,16 @@ data NBinaryOp
436434
instance Serialise NBinaryOp
437435
#endif
438436

437+
data NRecordType
438+
= NNonRecursive
439+
| NRecursive
440+
deriving (Eq, Ord, Enum, Bounded, Generic, Typeable, Data, Show, Read,
441+
NFData, Hashable)
442+
443+
#ifdef MIN_VERSION_serialise
444+
instance Serialise NRecordType
445+
#endif
446+
439447
-- | Get the name out of the parameter (there might be none).
440448
paramName :: Params r -> Maybe VarName
441449
paramName (Param n ) = Just n
@@ -488,6 +496,7 @@ instance Binary a => Binary (Params a)
488496
instance Binary NAtom
489497
instance Binary NUnaryOp
490498
instance Binary NBinaryOp
499+
instance Binary NRecordType
491500
instance Binary a => Binary (NExprF a)
492501

493502
instance (ToJSON v, ToJSON a) => ToJSON (Antiquoted v a)
@@ -501,6 +510,7 @@ instance ToJSON a => ToJSON (Params a)
501510
instance ToJSON NAtom
502511
instance ToJSON NUnaryOp
503512
instance ToJSON NBinaryOp
513+
instance ToJSON NRecordType
504514
instance ToJSON a => ToJSON (NExprF a)
505515
instance ToJSON NExpr
506516

@@ -515,6 +525,7 @@ instance FromJSON a => FromJSON (Params a)
515525
instance FromJSON NAtom
516526
instance FromJSON NUnaryOp
517527
instance FromJSON NBinaryOp
528+
instance FromJSON NRecordType
518529
instance FromJSON a => FromJSON (NExprF a)
519530
instance FromJSON NExpr
520531

@@ -538,15 +549,15 @@ ekey
538549
=> NonEmpty Text
539550
-> SourcePos
540551
-> Lens' (Fix g) (Maybe (Fix g))
541-
ekey keys pos f e@(Fix x) | (NSet xs, ann) <- fromNExpr x = case go xs of
552+
ekey keys pos f e@(Fix x) | (NSet NNonRecursive xs, ann) <- fromNExpr x = case go xs of
542553
((v, [] ) : _) -> fromMaybe e <$> f (Just v)
543554
((v, r : rest) : _) -> ekey (r :| rest) pos f v
544555

545556
_ -> f Nothing <&> \case
546557
Nothing -> e
547558
Just v ->
548559
let entry = NamedVar (NE.map StaticKey keys) v pos
549-
in Fix (toNExpr (NSet (entry : xs), ann))
560+
in Fix (toNExpr (NSet NNonRecursive (entry : xs), ann))
550561
where
551562
go xs = do
552563
let keys' = NE.toList keys
@@ -563,8 +574,7 @@ ekey _ _ f e = fromMaybe e <$> f Nothing
563574
stripPositionInfo :: NExpr -> NExpr
564575
stripPositionInfo = transport phi
565576
where
566-
phi (NSet binds ) = NSet (map go binds)
567-
phi (NRecSet binds ) = NRecSet (map go binds)
577+
phi (NSet recur binds) = NSet recur (map go binds)
568578
phi (NLet binds body) = NLet (map go binds) body
569579
phi x = x
570580

src/Nix/Expr/Types/Annotated.hs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,8 @@ pattern NStr_ ann x = Compose (Ann ann (NStr x))
210210
pattern NList_ :: SrcSpan -> [r] -> NExprLocF r
211211
pattern NList_ ann x = Compose (Ann ann (NList x))
212212

213-
pattern NSet_ :: SrcSpan -> [Binding r] -> NExprLocF r
214-
pattern NSet_ ann x = Compose (Ann ann (NSet x))
215-
216-
pattern NRecSet_ :: SrcSpan -> [Binding r] -> NExprLocF r
217-
pattern NRecSet_ ann x = Compose (Ann ann (NRecSet x))
213+
pattern NSet_ :: SrcSpan -> NRecordType -> [Binding r] -> NExprLocF r
214+
pattern NSet_ ann recur x = Compose (Ann ann (NSet recur x))
218215

219216
pattern NLiteralPath_ :: SrcSpan -> FilePath -> NExprLocF r
220217
pattern NLiteralPath_ ann x = Compose (Ann ann (NLiteralPath x))

src/Nix/Parser.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ nixLet = annotateLocation1
252252
-- Let expressions `let {..., body = ...}' are just desugared
253253
-- into `(rec {..., body = ...}).body'.
254254
letBody = (\x -> NSelect x (StaticKey "body" :| []) Nothing) <$> aset
255-
aset = annotateLocation1 $ NRecSet <$> braces nixBinders
255+
aset = annotateLocation1 $ NSet NRecursive <$> braces nixBinders
256256

257257
nixIf :: Parser NExprLoc
258258
nixIf = annotateLocation1
@@ -418,7 +418,7 @@ keyName = dynamicKey <+> staticKey where
418418

419419
nixSet :: Parser NExprLoc
420420
nixSet = annotateLocation1 ((isRec <*> braces nixBinders) <?> "set")
421-
where isRec = (reserved "rec" $> NRecSet <?> "recursive set") <+> pure NSet
421+
where isRec = (reserved "rec" $> NSet NRecursive <?> "recursive set") <+> pure (NSet NNonRecursive)
422422

423423
parseNixFile :: MonadFile m => FilePath -> m (Result NExpr)
424424
parseNixFile =

src/Nix/Pretty.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,16 @@ exprFNixDoc = \case
235235
$ vsep
236236
$ concat
237237
$ [[lbracket], map (wrapParens appOpNonAssoc) xs, [rbracket]]
238-
NSet [] -> simpleExpr $ lbrace <> rbrace
239-
NSet xs ->
238+
NSet NNonRecursive [] -> simpleExpr $ lbrace <> rbrace
239+
NSet NNonRecursive xs ->
240240
simpleExpr
241241
$ group
242242
$ nest 2
243243
$ vsep
244244
$ concat
245245
$ [[lbrace], map prettyBind xs, [rbrace]]
246-
NRecSet [] -> simpleExpr $ recPrefix <> lbrace <> rbrace
247-
NRecSet xs ->
246+
NSet NRecursive [] -> simpleExpr $ recPrefix <> lbrace <> rbrace
247+
NSet NRecursive xs ->
248248
simpleExpr
249249
$ group
250250
$ nest 2
@@ -330,7 +330,7 @@ valueToExpr = iterNValue (\_ _ -> thk) phi
330330
phi (NVConstant' a ) = Fix $ NConstant a
331331
phi (NVStr' ns) = mkStr ns
332332
phi (NVList' l ) = Fix $ NList l
333-
phi (NVSet' s p ) = Fix $ NSet
333+
phi (NVSet' s p ) = Fix $ NSet NNonRecursive
334334
[ NamedVar (StaticKey k :| []) v (fromMaybe nullPos (M.lookup k p))
335335
| (k, v) <- toList s
336336
]

src/Nix/Reduce.hs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ reduce base@(NSelect_ _ _ attrs _)
210210
n@(NamedVar (a' :| _) _ _) | a' == a -> Just n
211211
_ -> findBind xs attrs
212212
-- Follow the attrpath recursively in sets.
213-
inspectSet (NSet_ _ binds) attrs = case findBind binds attrs of
213+
inspectSet (NSet_ _ NNonRecursive binds) attrs = case findBind binds attrs of
214214
Just (NamedVar _ e _) -> case NE.uncons attrs of
215215
(_, Just attrs) -> inspectSet (unFix e) attrs
216216
_ -> pure e
@@ -221,18 +221,18 @@ reduce base@(NSelect_ _ _ attrs _)
221221

222222
-- | Reduce a set by inlining its binds outside of the set
223223
-- if none of the binds inherit the super set.
224-
reduce e@(NSet_ ann binds) = do
224+
reduce e@(NSet_ ann NNonRecursive binds) = do
225225
let usesInherit = flip any binds $ \case
226226
Inherit{} -> True
227227
_ -> False
228228
if usesInherit
229-
then clearScopes @NExprLoc $ Fix . NSet_ ann <$> traverse sequence binds
229+
then clearScopes @NExprLoc $ Fix . NSet_ ann NNonRecursive <$> traverse sequence binds
230230
else Fix <$> sequence e
231231

232232
-- Encountering a 'rec set' construction eliminates any hope of inlining
233233
-- definitions.
234-
reduce (NRecSet_ ann binds) =
235-
clearScopes @NExprLoc $ Fix . NRecSet_ ann <$> traverse sequence binds
234+
reduce (NSet_ ann NRecursive binds) =
235+
clearScopes @NExprLoc $ Fix . NSet_ ann NRecursive <$> traverse sequence binds
236236

237237
-- Encountering a 'with' construction eliminates any hope of inlining
238238
-- definitions.
@@ -320,11 +320,9 @@ pruneTree opts = cataM $ \(FlaggedF (b, Compose x)) -> do
320320

321321
NList l | reduceLists opts -> Just $ NList (catMaybes l)
322322
| otherwise -> Just $ NList (map (fromMaybe nNull) l)
323-
NSet binds | reduceSets opts -> Just $ NSet (mapMaybe sequence binds)
324-
| otherwise -> Just $ NSet (map (fmap (fromMaybe nNull)) binds)
325-
NRecSet binds
326-
| reduceSets opts -> Just $ NRecSet (mapMaybe sequence binds)
327-
| otherwise -> Just $ NRecSet (map (fmap (fromMaybe nNull)) binds)
323+
NSet recur binds
324+
| reduceSets opts -> Just $ NSet recur (mapMaybe sequence binds)
325+
| otherwise -> Just $ NSet recur (map (fmap (fromMaybe nNull)) binds)
328326

329327
NLet binds (Just body@(Fix (Compose (Ann _ x)))) ->
330328
Just $ case mapMaybe pruneBinding binds of

src/Nix/TH.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ freeVars e = case unFix e of
4343
(NStr string ) -> foldMap freeVars string
4444
(NSym var ) -> Set.singleton var
4545
(NList list ) -> foldMap freeVars list
46-
(NSet bindings) -> foldMap bindFree bindings
47-
(NRecSet bindings) -> foldMap bindFree bindings \\ foldMap bindDefs bindings
46+
(NSet NNonRecursive bindings) -> foldMap bindFree bindings
47+
(NSet NRecursive bindings) -> foldMap bindFree bindings \\ foldMap bindDefs bindings
4848
(NLiteralPath _ ) -> Set.empty
4949
(NEnvPath _ ) -> Set.empty
5050
(NUnary _ expr ) -> freeVars expr

0 commit comments

Comments
 (0)