Skip to content

Commit 2555ffe

Browse files
Merge #878: unflip {mkNixDoc, nvSet{,',P}}; a lot of refactors; some optimizations
2 parents d24d41c + a802a10 commit 2555ffe

File tree

24 files changed

+1541
-1124
lines changed

24 files changed

+1541
-1124
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@
137137
MonadPaths (Fix1 t) :: Nix.Standard -> Nix.Effects
138138
MonadPutStr (Fix1 t) :: Nix.Standard -> Nix.Effects
139139
```
140+
* [(link)](https://github.com/haskell-nix/hnix/pull/878/files) `nvSet{,',P}`: got unflipped, now accept source position argument before the value.
140141

142+
* [(link)](https://github.com/haskell-nix/hnix/pull/878/files) `mkNixDoc`: got unflipped.
141143

142144
* Additional:
143145
* [(link)](https://github.com/haskell-nix/hnix/commit/7e6cd97bf3288cb584241611fdb25bf85d7e0ba7) `cabal.project`: freed from the `cryptohash-sha512` override, Hackage trustees made a revision.

src/Nix.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ evaluateExpression mpath evaluator handler expr = do
129129

130130
eval' = normalForm <=< nixEvalExpr mpath
131131

132-
argmap args = nvSet (M.fromList args) mempty
132+
argmap args = nvSet mempty (M.fromList args)
133133

134134
processResult
135135
:: forall e t f m a

src/Nix/Builtins.hs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,14 @@ withNixContext mpath action =
120120
)
121121
mpath
122122

123-
builtins :: (MonadNix e t f m, Scoped (NValue t f m) m)
124-
=> m (Scopes m (NValue t f m))
123+
builtins
124+
:: ( MonadNix e t f m
125+
, Scoped (NValue t f m) m
126+
)
127+
=> m (Scopes m (NValue t f m))
125128
builtins =
126129
do
127-
ref <- defer $ (`nvSet` M.empty) <$> buildMap
130+
ref <- defer $ nvSet mempty <$> buildMap
128131
lst <- ([("builtins", ref)] <>) <$> topLevelBuiltins
129132
pushScope (M.fromList lst) currentScopes
130133
where
@@ -394,7 +397,7 @@ nixPath :: MonadNix e t f m => m (NValue t f m)
394397
nixPath = fmap nvList $ flip foldNixPath mempty $
395398
\p mn ty rest ->
396399
pure $
397-
flip nvSet
400+
nvSet
398401
mempty
399402
(M.fromList
400403
[case ty of
@@ -883,9 +886,10 @@ catAttrs attrName xs =
883886
n <- fromStringNoContext =<< fromValue attrName
884887
l <- fromValue @[NValue t f m] xs
885888

886-
fmap (nvList . catMaybes) $
887-
forM l $
888-
fmap (M.lookup n) . fromValue <=< demand
889+
nvList . catMaybes <$>
890+
traverse
891+
(fmap (M.lookup n) . fromValue <=< demand)
892+
l
889893

890894
baseNameOf :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
891895
baseNameOf x = do
@@ -1010,7 +1014,7 @@ genList f nixN =
10101014
n <- fromValue @Integer nixN
10111015
bool
10121016
(throwError $ ErrorCall $ "builtins.genList: Expected a non-negative number, got " <> show n)
1013-
(toValue =<< forM [0 .. n - 1] (defer . callFunc f <=< toValue))
1017+
(toValue =<< traverse (defer . callFunc f <=< toValue) [0 .. n - 1])
10141018
(n >= 0)
10151019

10161020
-- We wrap values solely to provide an Ord instance for genericClosure
@@ -1195,7 +1199,7 @@ intersectAttrs set1 set2 =
11951199
(s1, p1) <- fromValue @(AttrSet (NValue t f m), AttrSet SourcePos) set1
11961200
(s2, p2) <- fromValue @(AttrSet (NValue t f m), AttrSet SourcePos) set2
11971201

1198-
pure $ nvSet (s2 `M.intersection` s1) (p2 `M.intersection` p1)
1202+
pure $ nvSet (p2 `M.intersection` p1) (s2 `M.intersection` s1)
11991203

12001204
functionArgs
12011205
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
@@ -1311,7 +1315,7 @@ throw_ mnv =
13111315

13121316
import_
13131317
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
1314-
import_ = scopedImport (nvSet M.empty M.empty)
1318+
import_ = scopedImport (nvSet mempty mempty)
13151319

13161320
scopedImport
13171321
:: forall e t f m
@@ -1433,17 +1437,17 @@ listToAttrs lst =
14331437
do
14341438
l <- fromValue @[NValue t f m] lst
14351439
fmap
1436-
((`nvSet` M.empty) . M.fromList . reverse)
1437-
(forM l $
1440+
(nvSet mempty . M.fromList . reverse)
1441+
(traverse
14381442
(\ nvattrset ->
14391443
do
1440-
a <- fromValue @(AttrSet (NValue t f m)) nvattrset
1441-
n <- fromValue =<< demand =<< attrsetGet "name" a
1442-
name <- fromStringNoContext n
1444+
a <- fromValue @(AttrSet (NValue t f m)) =<< demand nvattrset
1445+
name <- fromStringNoContext =<< fromValue =<< demand =<< attrsetGet "name" a
14431446
val <- attrsetGet "value" a
14441447

14451448
pure (name, val)
1446-
) <=< demand
1449+
)
1450+
l
14471451
)
14481452

14491453
-- prim_hashString from nix/src/libexpr/primops.cc
@@ -1596,7 +1600,7 @@ fromJSON nvjson =
15961600

15971601
where
15981602
jsonToNValue = \case
1599-
A.Object m -> (`nvSet` M.empty) <$> traverse jsonToNValue m
1603+
A.Object m -> nvSet mempty <$> traverse jsonToNValue m
16001604
A.Array l -> nvList <$> traverse jsonToNValue (V.toList l)
16011605
A.String s -> pure $ nvStr $ makeNixStringWithoutContext s
16021606
A.Number n ->
@@ -1643,12 +1647,12 @@ tryEval
16431647
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
16441648
tryEval e = catch (onSuccess <$> demand e) (pure . onError)
16451649
where
1646-
onSuccess v = flip nvSet M.empty $ M.fromList
1650+
onSuccess v = nvSet mempty $ M.fromList
16471651
[ ("success", nvConstant (NBool True))
16481652
, ("value", v)]
16491653

16501654
onError :: SomeException -> NValue t f m
1651-
onError _ = flip nvSet M.empty $ M.fromList
1655+
onError _ = nvSet mempty $ M.fromList
16521656
[ ("success", nvConstant (NBool False))
16531657
, ("value" , nvConstant (NBool False))
16541658
]
@@ -1664,7 +1668,7 @@ trace_ msg action =
16641668
traceEffect @t @f @m . Text.unpack . stringIgnoreContext =<< fromValue msg
16651669
pure action
16661670

1667-
-- 2018-09-08: NOTE: Remember of error context is so far not implemented
1671+
-- Please, can function remember error context
16681672
addErrorContext
16691673
:: forall e t f m
16701674
. MonadNix e t f m
@@ -1755,7 +1759,7 @@ getContext =
17551759
(NVStr ns) -> do
17561760
let context = getNixLikeContext $ toNixLikeContext $ NixString.getContext ns
17571761
valued :: M.HashMap Text (NValue t f m) <- sequenceA $ M.map toValue context
1758-
pure $ nvSet valued M.empty
1762+
pure $ nvSet mempty valued
17591763
x -> throwError $ ErrorCall $ "Invalid type for builtins.getContext: " <> show x) <=< demand
17601764

17611765
appendContext

src/Nix/Convert.hs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ instance ( Convertible e t f m
391391
l' <- toValue (unPos l)
392392
c' <- toValue (unPos c)
393393
let pos = M.fromList [("file" :: Text, f'), ("line", l'), ("column", c')]
394-
pure $ nvSet' pos mempty
394+
pure $ nvSet' mempty pos
395395

396396
-- | With 'ToValue', we can always act recursively
397397
instance Convertible e t f m
@@ -404,33 +404,35 @@ instance (Convertible e t f m, ToValue a m (NValue t f m))
404404

405405
instance Convertible e t f m
406406
=> ToValue (AttrSet (NValue t f m)) m (NValue' t f m (NValue t f m)) where
407-
toValue s = pure $ nvSet' s mempty
407+
toValue s = pure $ nvSet' mempty s
408408

409409
instance (Convertible e t f m, ToValue a m (NValue t f m))
410410
=> ToValue (AttrSet a) m (Deeper (NValue' t f m (NValue t f m))) where
411-
toValue s = (Deeper .) . nvSet' <$> traverse toValue s <*> pure mempty
411+
toValue s = (\ v s -> Deeper $ nvSet' s v) <$> (traverse (toValue) s) <*> pure mempty
412412

413413
instance Convertible e t f m
414414
=> ToValue (AttrSet (NValue t f m), AttrSet SourcePos) m
415415
(NValue' t f m (NValue t f m)) where
416-
toValue (s, p) = pure $ nvSet' s p
416+
toValue (s, p) = pure $ nvSet' p s
417417

418418
instance (Convertible e t f m, ToValue a m (NValue t f m))
419419
=> ToValue (AttrSet a, AttrSet SourcePos) m
420420
(Deeper (NValue' t f m (NValue t f m))) where
421-
toValue (s, p) = (Deeper .) . nvSet' <$> traverse toValue s <*> pure p
421+
toValue (s, p) = (\ v s -> Deeper $ nvSet' s v) <$> (traverse (toValue) s) <*> pure p
422422

423423
instance Convertible e t f m
424424
=> ToValue NixLikeContextValue m (NValue' t f m (NValue t f m)) where
425425
toValue nlcv = do
426426
path <-
427-
if nlcvPath nlcv
428-
then pure <$> toValue True
429-
else pure Nothing
427+
bool
428+
(pure Nothing)
429+
(pure <$> toValue True)
430+
(nlcvPath nlcv)
430431
allOutputs <-
431-
if nlcvAllOutputs nlcv
432-
then pure <$> toValue True
433-
else pure Nothing
432+
bool
433+
(pure Nothing)
434+
(pure <$> toValue True)
435+
(nlcvAllOutputs nlcv)
434436
outputs <- do
435437
let
436438
outputs = makeNixStringWithoutContext <$> nlcvOutputs nlcv
@@ -440,7 +442,7 @@ instance Convertible e t f m
440442
(pure Nothing)
441443
(fmap pure . toValue)
442444
ts
443-
pure $ flip nvSet' M.empty $ M.fromList $ catMaybes
445+
pure $ nvSet' mempty $ M.fromList $ catMaybes
444446
[ ("path",) <$> path
445447
, ("allOutputs",) <$> allOutputs
446448
, ("outputs",) <$> outputs

0 commit comments

Comments
 (0)