Skip to content

Commit

Permalink
Expr.Types: (NRecordType->Recursivity): (N->){,Non}Recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-Latukha committed Jul 8, 2021
1 parent c71e1d9 commit 0c44b87
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 55 deletions.
8 changes: 4 additions & 4 deletions src/Nix/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ eval (NList l ) =
lst <- traverse (defer @v @m . withScopes @v scope) l
toValue lst

eval (NSet NNonRecursive binds) =
eval (NSet NonRecursive binds) =
do
attrSet <- evalBinds False $ desugarBinds (eval . NSet NNonRecursive) binds
attrSet <- evalBinds False $ desugarBinds (eval . NSet NonRecursive) binds
toValue attrSet

eval (NSet NRecursive binds) =
eval (NSet Recursive binds) =
do
attrSet <- evalBinds True $ desugarBinds (eval . NSet NNonRecursive) binds
attrSet <- evalBinds True $ desugarBinds (eval . NSet NonRecursive) binds
toValue attrSet

eval (NLet binds body ) =
Expand Down
8 changes: 4 additions & 4 deletions src/Nix/Expr/Shorthands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ mkParamset :: [(Text, Maybe NExpr)] -> Bool -> Params NExpr
mkParamset params variadic = ParamSet params variadic mempty

mkRecSet :: [Binding NExpr] -> NExpr
mkRecSet = Fix . NSet NRecursive
mkRecSet = Fix . NSet Recursive

mkNonRecSet :: [Binding NExpr] -> NExpr
mkNonRecSet = Fix . NSet NNonRecursive
mkNonRecSet = Fix . NSet NonRecursive

mkLets :: [Binding NExpr] -> NExpr -> NExpr
mkLets bindings = Fix . NLet bindings
Expand Down Expand Up @@ -177,11 +177,11 @@ letE varName varExpr = letsE [(varName, varExpr)]

-- | Make an attribute set (non-recursive).
attrsE :: [(Text, NExpr)] -> NExpr
attrsE pairs = Fix $ NSet NNonRecursive $ uncurry bindTo <$> pairs
attrsE pairs = Fix $ NSet NonRecursive $ uncurry bindTo <$> pairs

-- | Make an attribute set (recursive).
recAttrsE :: [(Text, NExpr)] -> NExpr
recAttrsE pairs = Fix $ NSet NRecursive $ uncurry bindTo <$> pairs
recAttrsE pairs = Fix $ NSet Recursive $ uncurry bindTo <$> pairs

-- | Logical negation.
mkNot :: NExpr -> NExpr
Expand Down
20 changes: 10 additions & 10 deletions src/Nix/Expr/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,13 @@ $(deriveOrd1 ''Binding)
$(makeTraversals ''Binding)


-- ** @NRecordType@
-- ** @Recursivity@

-- | 'NRecordType' distinguishes between recursive and non-recursive attribute
-- | Distinguishes between recursive and non-recursive. Mainly for attribute
-- sets.
data NRecordType
= NNonRecursive -- ^ > { ... }
| NRecursive -- ^ > rec { ... }
data Recursivity
= NonRecursive -- ^ > { ... }
| Recursive -- ^ > rec { ... }
deriving
( Eq, Ord, Enum, Bounded, Generic
, Typeable, Data, NFData, Serialise, Binary, ToJSON, FromJSON
Expand Down Expand Up @@ -483,11 +483,11 @@ data NExprF r
-- ^ A list literal.
--
-- > NList [x,y] ~ [ x y ]
| NSet !NRecordType ![Binding r]
| NSet !Recursivity ![Binding r]
-- ^ An attribute set literal
--
-- > NSet NRecursive [NamedVar x y _] ~ rec { x = y; }
-- > NSet NNonRecursive [Inherit Nothing [x] _] ~ { inherit x; }
-- > NSet Recursive [NamedVar x y _] ~ rec { x = y; }
-- > NSet NonRecursive [Inherit Nothing [x] _] ~ { inherit x; }
| NLiteralPath !FilePath
-- ^ A path expression, which is evaluated to a store path. The path here
-- can be relative, in which case it's evaluated relative to the file in
Expand Down Expand Up @@ -652,7 +652,7 @@ ekey
=> NonEmpty Text
-> SourcePos
-> Lens' (Fix g) (Maybe (Fix g))
ekey keys pos f e@(Fix x) | (NSet NNonRecursive xs, ann) <- fromNExpr x =
ekey keys pos f e@(Fix x) | (NSet NonRecursive xs, ann) <- fromNExpr x =
case go xs of
((v, [] ) : _) -> fromMaybe e <$> f (pure v)
((v, r : rest) : _) -> ekey (r :| rest) pos f v
Expand All @@ -662,7 +662,7 @@ ekey keys pos f e@(Fix x) | (NSet NNonRecursive xs, ann) <- fromNExpr x =
e
(\ v ->
let entry = NamedVar (StaticKey <$> keys) v pos in
Fix $ toNExpr ( NSet NNonRecursive $ [entry] <> xs, ann )
Fix $ toNExpr ( NSet NonRecursive $ [entry] <> xs, ann )
)
<$>
f Nothing
Expand Down
2 changes: 1 addition & 1 deletion src/Nix/Expr/Types/Annotated.hs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pattern NSym_ ann x = AnnFP ann (NSym x)
pattern NList_ :: SrcSpan -> [r] -> NExprLocF r
pattern NList_ ann x = AnnFP ann (NList x)

pattern NSet_ :: SrcSpan -> NRecordType -> [Binding r] -> NExprLocF r
pattern NSet_ :: SrcSpan -> Recursivity -> [Binding r] -> NExprLocF r
pattern NSet_ ann recur x = AnnFP ann (NSet recur x)

pattern NLiteralPath_ :: SrcSpan -> FilePath -> NExprLocF r
Expand Down
4 changes: 2 additions & 2 deletions src/Nix/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ nixLet = annotateLocation1
-- Let expressions `let {..., body = ...}' are just desugared
-- into `(rec {..., body = ...}).body'.
letBody = (\x -> NSelect x (StaticKey "body" :| mempty) Nothing) <$> aset
aset = annotateLocation1 $ NSet NRecursive <$> braces nixBinders
aset = annotateLocation1 $ NSet Recursive <$> braces nixBinders

nixIf :: Parser NExprLoc
nixIf = annotateLocation1
Expand Down Expand Up @@ -472,7 +472,7 @@ keyName = dynamicKey <+> staticKey
nixSet :: Parser NExprLoc
nixSet = annotateLocation1 ((isRec <*> braces nixBinders) <?> "set")
where
isRec = (reserved "rec" $> NSet NRecursive <?> "recursive set") <+> pure (NSet NNonRecursive)
isRec = (reserved "rec" $> NSet Recursive <?> "recursive set") <+> pure (NSet NonRecursive)

parseNixFile :: MonadFile m => FilePath -> m (Result NExpr)
parseNixFile =
Expand Down
6 changes: 3 additions & 3 deletions src/Nix/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ exprFNixDoc = \case
NStr str -> simpleExpr $ prettyString str
NList xs ->
prettyContainer "[" (wrapParens appOpNonAssoc) "]" xs
NSet NNonRecursive xs ->
NSet NonRecursive xs ->
prettyContainer "{" prettyBind "}" xs
NSet NRecursive xs ->
NSet Recursive xs ->
prettyContainer "rec {" prettyBind "}" xs
NAbs args body ->
leastPrecedence $
Expand Down Expand Up @@ -318,7 +318,7 @@ valueToExpr = iterNValueByDiscardWith thk (Fix . phi)
phi (NVConstant' a ) = NConstant a
phi (NVStr' ns ) = NStr $ DoubleQuoted [Plain (stringIgnoreContext ns)]
phi (NVList' l ) = NList l
phi (NVSet' s p) = NSet NNonRecursive
phi (NVSet' s p) = NSet NonRecursive
[ NamedVar (StaticKey k :| mempty) v (fromMaybe nullPos (M.lookup k p))
| (k, v) <- toList s
]
Expand Down
10 changes: 5 additions & 5 deletions src/Nix/Reduce.hs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ reduce base@(NSelect_ _ _ attrs _)
n@(NamedVar (a' :| _) _ _) | a' == a -> pure n
_ -> findBind xs attrs
-- Follow the attrpath recursively in sets.
inspectSet (NSet_ _ NNonRecursive binds) attrs = case findBind binds attrs of
inspectSet (NSet_ _ NonRecursive binds) attrs = case findBind binds attrs of
Just (NamedVar _ e _) -> case NE.uncons attrs of
(_, Just attrs) -> inspectSet (unFix e) attrs
_ -> pure e
Expand All @@ -231,7 +231,7 @@ reduce base@(NSelect_ _ _ attrs _)

-- | Reduce a set by inlining its binds outside of the set
-- if none of the binds inherit the super set.
reduce e@(NSet_ ann NNonRecursive binds) =
reduce e@(NSet_ ann NonRecursive binds) =
do
let
usesInherit =
Expand All @@ -244,13 +244,13 @@ reduce e@(NSet_ ann NNonRecursive binds) =

bool
(Fix <$> sequence e)
(clearScopes @NExprLoc $ Fix . NSet_ ann NNonRecursive <$> traverse sequence binds)
(clearScopes @NExprLoc $ Fix . NSet_ ann NonRecursive <$> traverse sequence binds)
usesInherit

-- Encountering a 'rec set' construction eliminates any hope of inlining
-- definitions.
reduce (NSet_ ann NRecursive binds) =
clearScopes @NExprLoc $ Fix . NSet_ ann NRecursive <$> traverse sequence binds
reduce (NSet_ ann Recursive binds) =
clearScopes @NExprLoc $ Fix . NSet_ ann Recursive <$> traverse sequence binds

-- Encountering a 'with' construction eliminates any hope of inlining
-- definitions.
Expand Down
4 changes: 2 additions & 2 deletions src/Nix/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ freeVars e = case unFix e of
(NStr string ) -> mapFreeVars string
(NSym var ) -> one var
(NList list ) -> mapFreeVars list
(NSet NNonRecursive bindings) -> bindFreeVars bindings
(NSet NRecursive bindings) -> Set.difference (bindFreeVars bindings) (bindDefs bindings)
(NSet NonRecursive bindings) -> bindFreeVars bindings
(NSet Recursive bindings) -> Set.difference (bindFreeVars bindings) (bindDefs bindings)
(NLiteralPath _ ) -> mempty
(NEnvPath _ ) -> mempty
(NUnary _ expr ) -> freeVars expr
Expand Down
44 changes: 22 additions & 22 deletions tests/ParserTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,51 +69,51 @@ case_constant_uri = do
assertParseFail "+:acdcd"

case_simple_set = do
assertParseText "{ a = 23; b = 4; }" $ Fix $ NSet NNonRecursive
assertParseText "{ a = 23; b = 4; }" $ Fix $ NSet NonRecursive
[ NamedVar (mkSelector "a") (mkInt 23) nullPos
, NamedVar (mkSelector "b") (mkInt 4) nullPos
]
assertParseFail "{ a = 23 }"

case_set_inherit = do
assertParseText "{ e = 3; inherit a b; }" $ Fix $ NSet NNonRecursive
assertParseText "{ e = 3; inherit a b; }" $ Fix $ NSet NonRecursive
[ NamedVar (mkSelector "e") (mkInt 3) nullPos
, Inherit Nothing (StaticKey <$> ["a", "b"]) nullPos
]
assertParseText "{ inherit; }" $ Fix $ NSet NNonRecursive [ Inherit Nothing mempty nullPos ]
assertParseText "{ inherit; }" $ Fix $ NSet NonRecursive [ Inherit Nothing mempty nullPos ]

case_set_scoped_inherit = assertParseText "{ inherit (a) b c; e = 4; inherit(a)b c; }" $ Fix $ NSet NNonRecursive
case_set_scoped_inherit = assertParseText "{ inherit (a) b c; e = 4; inherit(a)b c; }" $ Fix $ NSet NonRecursive
[ Inherit (pure (mkSym "a")) (StaticKey <$> ["b", "c"]) nullPos
, NamedVar (mkSelector "e") (mkInt 4) nullPos
, Inherit (pure (mkSym "a")) (StaticKey <$> ["b", "c"]) nullPos
]

case_set_rec = assertParseText "rec { a = 3; b = a; }" $ Fix $ NSet NRecursive
case_set_rec = assertParseText "rec { a = 3; b = a; }" $ Fix $ NSet Recursive
[ NamedVar (mkSelector "a") (mkInt 3) nullPos
, NamedVar (mkSelector "b") (mkSym "a") nullPos
]

case_set_complex_keynames = do
assertParseText "{ \"\" = null; }" $ Fix $ NSet NNonRecursive
assertParseText "{ \"\" = null; }" $ Fix $ NSet NonRecursive
[ NamedVar (DynamicKey (Plain (DoubleQuoted mempty)) :| mempty) mkNull nullPos ]
assertParseText "{ a.b = 3; a.c = 4; }" $ Fix $ NSet NNonRecursive
assertParseText "{ a.b = 3; a.c = 4; }" $ Fix $ NSet NonRecursive
[ NamedVar (StaticKey "a" :| [StaticKey "b"]) (mkInt 3) nullPos
, NamedVar (StaticKey "a" :| [StaticKey "c"]) (mkInt 4) nullPos
]
assertParseText "{ ${let a = \"b\"; in a} = 4; }" $ Fix $ NSet NNonRecursive
assertParseText "{ ${let a = \"b\"; in a} = 4; }" $ Fix $ NSet NonRecursive
[ NamedVar (DynamicKey (Antiquoted letExpr) :| mempty) (mkInt 4) nullPos ]
assertParseText "{ \"a${let a = \"b\"; in a}c\".e = 4; }" $ Fix $ NSet NNonRecursive
assertParseText "{ \"a${let a = \"b\"; in a}c\".e = 4; }" $ Fix $ NSet NonRecursive
[ NamedVar (DynamicKey (Plain str) :| [StaticKey "e"]) (mkInt 4) nullPos ]
where
letExpr = Fix $ NLet [NamedVar (mkSelector "a") (mkStr "b") nullPos] (mkSym "a")
str = DoubleQuoted [Plain "a", Antiquoted letExpr, Plain "c"]

case_set_inherit_direct = assertParseText "{ inherit ({a = 3;}); }" $ Fix $ NSet NNonRecursive
[ Inherit (pure $ Fix $ NSet NNonRecursive [NamedVar (mkSelector "a") (mkInt 3) nullPos]) mempty nullPos
case_set_inherit_direct = assertParseText "{ inherit ({a = 3;}); }" $ Fix $ NSet NonRecursive
[ Inherit (pure $ Fix $ NSet NonRecursive [NamedVar (mkSelector "a") (mkInt 3) nullPos]) mempty nullPos
]

case_inherit_selector = do
assertParseText "{ inherit \"a\"; }" $ Fix $ NSet NNonRecursive
assertParseText "{ inherit \"a\"; }" $ Fix $ NSet NonRecursive
[Inherit Nothing [DynamicKey (Plain (DoubleQuoted [Plain "a"]))] nullPos]
assertParseFail "{ inherit a.x; }"

Expand All @@ -124,7 +124,7 @@ case_int_null_list = assertParseText "[1 2 3 null 4]" $ Fix (NList (fmap (Fix .

case_mixed_list = do
assertParseText "[{a = 3;}.a (if true then null else false) null false 4 [] c.d or null]" $ Fix $ NList
[ Fix (NSelect (Fix (NSet NNonRecursive [NamedVar (mkSelector "a") (mkInt 3) nullPos]))
[ Fix (NSelect (Fix (NSet NonRecursive [NamedVar (mkSelector "a") (mkInt 3) nullPos]))
(mkSelector "a") Nothing)
, Fix (NIf (mkBool True) mkNull (mkBool False))
, mkNull, mkBool False, mkInt 4, Fix (NList mempty)
Expand All @@ -141,7 +141,7 @@ case_lambda_or_uri = do
assertParseText "a :b" $ Fix $ NAbs (Param "a") (mkSym "b")
assertParseText "a c:def" $ Fix $ NBinary NApp (mkSym "a") (mkStr "c:def")
assertParseText "c:def: c" $ Fix $ NBinary NApp (mkStr "c:def:") (mkSym "c")
assertParseText "a:{}" $ Fix $ NAbs (Param "a") $ Fix $ NSet NNonRecursive mempty
assertParseText "a:{}" $ Fix $ NAbs (Param "a") $ Fix $ NSet NonRecursive mempty
assertParseText "a:[a]" $ Fix $ NAbs (Param "a") $ Fix $ NList [mkSym "a"]
assertParseFail "def:"

Expand Down Expand Up @@ -182,7 +182,7 @@ case_simple_let = do
case_let_body = assertParseText "let { body = 1; }" letBody
where
letBody = Fix $ NSelect aset (mkSelector "body") Nothing
aset = Fix $ NSet NRecursive [NamedVar (mkSelector "body") (mkInt 1) nullPos]
aset = Fix $ NSet Recursive [NamedVar (mkSelector "body") (mkInt 1) nullPos]

case_nested_let = do
assertParseText "let a = 4; in let b = 5; in a" $ Fix $ NLet
Expand Down Expand Up @@ -253,11 +253,11 @@ case_select = do
assertParseText "a.e . d or null" $ Fix $ NSelect (mkSym "a")
(StaticKey "e" :| [StaticKey "d"])
(pure mkNull)
assertParseText "{}.\"\"or null" $ Fix $ NSelect (Fix (NSet NNonRecursive mempty))
assertParseText "{}.\"\"or null" $ Fix $ NSelect (Fix (NSet NonRecursive mempty))
(DynamicKey (Plain (DoubleQuoted mempty)) :| mempty) (pure mkNull)
assertParseText "{ a = [1]; }.a or [2] ++ [3]" $ Fix $ NBinary NConcat
(Fix (NSelect
(Fix (NSet NNonRecursive [NamedVar (StaticKey "a" :| mempty)
(Fix (NSet NonRecursive [NamedVar (StaticKey "a" :| mempty)
(Fix (NList [Fix (NConstant (NInt 1))]))
nullPos]))
(StaticKey "a" :| mempty)
Expand All @@ -267,14 +267,14 @@ case_select = do
case_select_path = do
assertParseText "f ./." $ Fix $ NBinary NApp (mkSym "f") (mkPath False "./.")
assertParseText "f.b ../a" $ Fix $ NBinary NApp select (mkPath False "../a")
assertParseText "{}./def" $ Fix $ NBinary NApp (Fix (NSet NNonRecursive mempty)) (mkPath False "./def")
assertParseText "{}./def" $ Fix $ NBinary NApp (Fix (NSet NonRecursive mempty)) (mkPath False "./def")
assertParseText "{}.\"\"./def" $ Fix $ NBinary NApp
(Fix $ NSelect (Fix (NSet NNonRecursive mempty)) (DynamicKey (Plain (DoubleQuoted mempty)) :| mempty) Nothing)
(Fix $ NSelect (Fix (NSet NonRecursive mempty)) (DynamicKey (Plain (DoubleQuoted mempty)) :| mempty) Nothing)
(mkPath False "./def")
where select = Fix $ NSelect (mkSym "f") (mkSelector "b") Nothing

case_select_keyword = do
assertParseText "{ false = \"foo\"; }" $ Fix $ NSet NNonRecursive [NamedVar (mkSelector "false") (mkStr "foo") nullPos]
assertParseText "{ false = \"foo\"; }" $ Fix $ NSet NonRecursive [NamedVar (mkSelector "false") (mkStr "foo") nullPos]

case_fun_app = do
assertParseText "f a b" $ Fix $ NBinary NApp (Fix $ NBinary NApp (mkSym "f") (mkSym "a")) (mkSym "b")
Expand Down Expand Up @@ -311,8 +311,8 @@ case_operators = do
assertParseText "1 + (if true then 2 else 3)" $ mkOper2 NPlus (mkInt 1) $ Fix $ NIf
(mkBool True) (mkInt 2) (mkInt 3)
assertParseText "{ a = 3; } // rec { b = 4; }" $ mkOper2 NUpdate
(Fix $ NSet NNonRecursive [NamedVar (mkSelector "a") (mkInt 3) nullPos])
(Fix $ NSet NRecursive [NamedVar (mkSelector "b") (mkInt 4) nullPos])
(Fix $ NSet NonRecursive [NamedVar (mkSelector "a") (mkInt 3) nullPos])
(Fix $ NSet Recursive [NamedVar (mkSelector "b") (mkInt 4) nullPos])
assertParseText "--a" $ mkOper NNeg $ mkOper NNeg $ mkSym "a"
assertParseText "a - b - c" $ mkOper2 NMinus
(mkOper2 NMinus (mkSym "a") (mkSym "b")) $
Expand Down
4 changes: 2 additions & 2 deletions tests/PrettyParseTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ genExpr = Gen.sized $ \(Size n) -> Fix <$> if n < 2
genStr = NStr <$> genString
genSym = NSym <$> asciiText
genList = NList <$> fairList genExpr
genSet = NSet NNonRecursive <$> fairList genBinding
genRecSet = NSet NRecursive <$> fairList genBinding
genSet = NSet NonRecursive <$> fairList genBinding
genRecSet = NSet Recursive <$> fairList genBinding
genLiteralPath = NLiteralPath . ("./" <>) <$> asciiString
genEnvPath = NEnvPath <$> asciiString
genUnary = liftA2 NUnary Gen.enumBounded genExpr
Expand Down

0 comments on commit 0c44b87

Please sign in to comment.