Skip to content

Commit 529095d

Browse files
committed
reduce (principled->) from NixString functions, use NixString monoid
1 parent 41be26c commit 529095d

File tree

15 files changed

+139
-155
lines changed

15 files changed

+139
-155
lines changed

main/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ main = do
143143
= liftIO
144144
. putStrLn
145145
. Text.unpack
146-
. principledStringIgnoreContext
146+
. stringIgnoreContext
147147
. toXML
148148
<=< normalForm
149149
| json opts
150150
= liftIO
151151
. Text.putStrLn
152-
. principledStringIgnoreContext
152+
. stringIgnoreContext
153153
<=< nvalueToJSONNixString
154154
| strict opts
155155
= liftIO . print . prettyNValue <=< normalForm

src/Nix/Builtins.hs

Lines changed: 57 additions & 56 deletions
Large diffs are not rendered by default.

src/Nix/Convert.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ instance ( Convertible e t f m
151151
NVStr' ns -> pure $ Just ns
152152
NVPath' p ->
153153
Just
154-
. (\s -> principledMakeNixStringWithSingletonContext s (StringContext s DirectPath))
154+
. (\s -> makeNixStringWithSingletonContext s (StringContext s DirectPath))
155155
. Text.pack
156156
. unStorePath
157157
<$> addPath p
@@ -166,7 +166,7 @@ instance ( Convertible e t f m
166166
instance Convertible e t f m
167167
=> FromValue ByteString m (NValue' t f m (NValue t f m)) where
168168
fromValueMay = \case
169-
NVStr' ns -> pure $ encodeUtf8 <$> principledGetStringNoContext ns
169+
NVStr' ns -> pure $ encodeUtf8 <$> getStringNoContext ns
170170
_ -> pure Nothing
171171
fromValue v = fromValueMay v >>= \case
172172
Just b -> pure b
@@ -181,7 +181,7 @@ instance ( Convertible e t f m
181181
=> FromValue Path m (NValue' t f m (NValue t f m)) where
182182
fromValueMay = \case
183183
NVPath' p -> pure $ Just (Path p)
184-
NVStr' ns -> pure $ Path . Text.unpack <$> principledGetStringNoContext ns
184+
NVStr' ns -> pure $ Path . Text.unpack <$> getStringNoContext ns
185185
NVSet' s _ -> case M.lookup "outPath" s of
186186
Nothing -> pure Nothing
187187
Just p -> fromValueMay @Path p
@@ -303,7 +303,7 @@ instance Convertible e t f m
303303

304304
instance Convertible e t f m
305305
=> ToValue ByteString m (NValue' t f m (NValue t f m)) where
306-
toValue = pure . nvStr' . principledMakeNixStringWithoutContext . decodeUtf8
306+
toValue = pure . nvStr' . makeNixStringWithoutContext . decodeUtf8
307307

308308
instance Convertible e t f m
309309
=> ToValue Path m (NValue' t f m (NValue t f m)) where
@@ -317,7 +317,7 @@ instance ( Convertible e t f m
317317
)
318318
=> ToValue SourcePos m (NValue' t f m (NValue t f m)) where
319319
toValue (SourcePos f l c) = do
320-
f' <- toValue (principledMakeNixStringWithoutContext (Text.pack f))
320+
f' <- toValue (makeNixStringWithoutContext (Text.pack f))
321321
l' <- toValue (unPos l)
322322
c' <- toValue (unPos c)
323323
let pos = M.fromList [("file" :: Text, f'), ("line", l'), ("column", c')]
@@ -359,7 +359,7 @@ instance Convertible e t f m
359359
else pure Nothing
360360
outputs <- do
361361
let outputs =
362-
principledMakeNixStringWithoutContext <$> nlcvOutputs nlcv
362+
makeNixStringWithoutContext <$> nlcvOutputs nlcv
363363
ts :: [NValue t f m] <- traverse toValue outputs
364364
case ts of
365365
[] -> pure Nothing

src/Nix/Effects/Basic.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ findPathBy finder ls name = do
137137
Nothing -> tryPath path Nothing
138138
Just pf -> demand pf $ fromValueMay >=> \case
139139
Just (nsPfx :: NixString) ->
140-
let pfx = principledStringIgnoreContext nsPfx
140+
let pfx = stringIgnoreContext nsPfx
141141
in if not (Text.null pfx)
142142
then tryPath path (Just (Text.unpack pfx))
143143
else tryPath path Nothing
@@ -174,7 +174,7 @@ fetchTarball = flip demand $ \case
174174
where
175175
go :: Maybe (NValue t f m) -> NValue t f m -> m (NValue t f m)
176176
go msha = \case
177-
NVStr ns -> fetch (principledStringIgnoreContext ns) msha
177+
NVStr ns -> fetch (stringIgnoreContext ns) msha
178178
v ->
179179
throwError
180180
$ ErrorCall
@@ -197,7 +197,7 @@ fetchTarball = flip demand $ \case
197197
fetch uri Nothing =
198198
nixInstantiateExpr $ "builtins.fetchTarball \"" ++ Text.unpack uri ++ "\""
199199
fetch url (Just t) = demand t $ fromValue >=> \nsSha ->
200-
let sha = principledStringIgnoreContext nsSha
200+
let sha = stringIgnoreContext nsSha
201201
in nixInstantiateExpr
202202
$ "builtins.fetchTarball { "
203203
++ "url = \""

src/Nix/Effects/Derivation.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ defaultDerivationStrict = fromValue @(AttrSet (NValue t f m)) >=> \s -> do
260260
drvHash <- Store.encodeInBase Store.Base16 <$> hashDerivationModulo drv'
261261
modify (\(a, b) -> (a, MS.insert drvPath drvHash b))
262262

263-
let outputsWithContext = Map.mapWithKey (\out path -> principledMakeNixStringWithSingletonContext path (StringContext drvPath (DerivationOutput out))) (outputs drv')
264-
drvPathWithContext = principledMakeNixStringWithSingletonContext drvPath (StringContext drvPath AllOutputs)
263+
let outputsWithContext = Map.mapWithKey (\out path -> makeNixStringWithSingletonContext path (StringContext drvPath (DerivationOutput out))) (outputs drv')
264+
drvPathWithContext = makeNixStringWithSingletonContext drvPath (StringContext drvPath AllOutputs)
265265
attrSet = M.map nvStr $ M.fromList $ ("drvPath", drvPathWithContext): Map.toList outputsWithContext
266266
-- TODO: Add location information for all the entries.
267267
-- here --v
@@ -373,7 +373,7 @@ buildDerivationWithContext drvAttrs = do
373373
return name
374374

375375
extractNoCtx :: MonadNix e t f m => NixString -> WithStringContextT m Text
376-
extractNoCtx ns = case principledGetStringNoContext ns of
376+
extractNoCtx ns = case getStringNoContext ns of
377377
Nothing -> lift $ throwError $ ErrorCall $ "The string " ++ show ns ++ " is not allowed to have a context."
378378
Just v -> return v
379379

src/Nix/Eval.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ evalSetterKeyName = \case
355355
StaticKey k -> pure (Just k)
356356
DynamicKey k ->
357357
runAntiquoted "\n" assembleString (>>= fromValueMay) k <&> \case
358-
Just ns -> Just (principledStringIgnoreContext ns)
358+
Just ns -> Just (stringIgnoreContext ns)
359359
_ -> Nothing
360360

361361
assembleString
@@ -367,10 +367,10 @@ assembleString = \case
367367
Indented _ parts -> fromParts parts
368368
DoubleQuoted parts -> fromParts parts
369369
where
370-
fromParts = fmap (fmap principledStringMConcat . sequence) . traverse go
370+
fromParts = fmap (fmap stringMConcat . sequence) . traverse go
371371

372372
go = runAntiquoted "\n"
373-
(pure . Just . principledMakeNixStringWithoutContext)
373+
(pure . Just . makeNixStringWithoutContext)
374374
(>>= fromValueMay)
375375

376376
buildArgument

src/Nix/Exec.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ instance MonadNix e t f m => MonadEval (NValue t f m) m where
210210
pure $ nvStrP
211211
(Provenance
212212
scope
213-
(NStr_ span (DoubleQuoted [Plain (principledStringIgnoreContext ns)]))
213+
(NStr_ span (DoubleQuoted [Plain (stringIgnoreContext ns)]))
214214
)
215215
ns
216216
Nothing -> nverr $ ErrorCall "Failed to assemble string"
@@ -400,22 +400,22 @@ execBinaryOpForced scope span op lval rval = case op of
400400
NPlus -> case (lval, rval) of
401401
(NVConstant _, NVConstant _) -> numBinOp (+)
402402

403-
(NVStr ls, NVStr rs) -> pure $ nvStrP prov (ls `principledStringMappend` rs)
403+
(NVStr ls, NVStr rs) -> pure $ nvStrP prov (ls `mappend` rs)
404404
(NVStr ls, rs@NVPath{}) ->
405-
(\rs2 -> nvStrP prov (ls `principledStringMappend` rs2))
405+
(\rs2 -> nvStrP prov (ls `mappend` rs2))
406406
<$> coerceToString callFunc CopyToStore CoerceStringy rs
407-
(NVPath ls, NVStr rs) -> case principledGetStringNoContext rs of
407+
(NVPath ls, NVStr rs) -> case getStringNoContext rs of
408408
Just rs2 -> nvPathP prov <$> makeAbsolutePath @t @f (ls `mappend` Text.unpack rs2)
409409
Nothing -> throwError $ ErrorCall $
410410
-- data/nix/src/libexpr/eval.cc:1412
411411
"A string that refers to a store path cannot be appended to a path."
412412
(NVPath ls, NVPath rs) -> nvPathP prov <$> makeAbsolutePath @t @f (ls ++ rs)
413413

414414
(ls@NVSet{}, NVStr rs) ->
415-
(\ls2 -> nvStrP prov (ls2 `principledStringMappend` rs))
415+
(\ls2 -> nvStrP prov (ls2 `mappend` rs))
416416
<$> coerceToString callFunc DontCopyToStore CoerceStringy ls
417417
(NVStr ls, rs@NVSet{}) ->
418-
(\rs2 -> nvStrP prov (ls `principledStringMappend` rs2))
418+
(\rs2 -> nvStrP prov (ls `mappend` rs2))
419419
<$> coerceToString callFunc DontCopyToStore CoerceStringy rs
420420
_ -> unsupportedTypes
421421

@@ -473,7 +473,7 @@ execBinaryOpForced scope span op lval rval = case op of
473473
-- This function is here, rather than in 'Nix.String', because of the need to
474474
-- use 'throwError'.
475475
fromStringNoContext :: Framed e m => NixString -> m Text
476-
fromStringNoContext ns = case principledGetStringNoContext ns of
476+
fromStringNoContext ns = case getStringNoContext ns of
477477
Just str -> pure str
478478
Nothing -> throwError $ ErrorCall $ "expected string with no context, but got " ++ show ns
479479

src/Nix/Normal.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ removeEffects =
124124
(fmap Free . sequenceNValue' id)
125125

126126
opaque :: Applicative f => NValue t f m
127-
opaque = nvStr $ principledMakeNixStringWithoutContext "<CYCLE>"
127+
opaque = nvStr $ makeNixStringWithoutContext "<CYCLE>"
128128

129129
dethunk
130130
:: (MonadThunk t m (NValue t f m), MonadDataContext f m)

src/Nix/Pretty.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ valueToExpr = iterNValue (\_ _ -> thk) phi
330330
phi (NVBuiltin' name _) = Fix . NSym . pack $ "builtins." ++ name
331331
phi _ = error "Pattern synonyms foil completeness check"
332332

333-
mkStr ns = Fix $ NStr $ DoubleQuoted [Plain (principledStringIgnoreContext ns)]
333+
mkStr ns = Fix $ NStr $ DoubleQuoted [Plain (stringIgnoreContext ns)]
334334

335335
prettyNValue
336336
:: forall t f m ann . MonadDataContext f m => NValue t f m -> Doc ann
@@ -390,7 +390,7 @@ printNix = iterNValue (\_ _ -> thk) phi
390390

391391
phi :: NValue' t f m String -> String
392392
phi (NVConstant' a ) = unpack $ atomText a
393-
phi (NVStr' ns) = show $ principledStringIgnoreContext ns
393+
phi (NVStr' ns) = show $ stringIgnoreContext ns
394394
phi (NVList' l ) = "[ " ++ unwords l ++ " ]"
395395
phi (NVSet' s _) =
396396
"{ "

src/Nix/String.hs

Lines changed: 41 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,22 @@
44
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
55
module Nix.String
66
( NixString
7-
, principledGetContext
8-
, principledMakeNixString
9-
, principledMempty
7+
, getContext
8+
, makeNixString
109
, StringContext(..)
1110
, ContextFlavor(..)
1211
, NixLikeContext(..)
1312
, NixLikeContextValue(..)
1413
, toNixLikeContext
1514
, fromNixLikeContext
1615
, stringHasContext
17-
, principledIntercalateNixString
18-
, principledGetStringNoContext
19-
, principledStringIgnoreContext
20-
, principledMakeNixStringWithoutContext
21-
, principledMakeNixStringWithSingletonContext
22-
, principledModifyNixContents
23-
, principledStringMappend
24-
, principledStringMempty
25-
, principledStringMConcat
16+
, intercalateNixString
17+
, getStringNoContext
18+
, stringIgnoreContext
19+
, makeNixStringWithoutContext
20+
, makeNixStringWithSingletonContext
21+
, modifyNixContents
22+
, stringMConcat
2623
, WithStringContext
2724
, WithStringContextT(..)
2825
, extractNixString
@@ -69,6 +66,12 @@ data NixString = NixString
6966
, nsContext :: !(S.HashSet StringContext)
7067
} deriving (Eq, Ord, Show, Generic)
7168

69+
instance Semigroup NixString where
70+
NixString s1 t1 <> NixString s2 t2 = NixString (s1 <> s2) (t1 <> t2)
71+
72+
instance Monoid NixString where
73+
mempty = NixString mempty mempty
74+
7275
instance Hashable NixString
7376

7477
newtype NixLikeContext = NixLikeContext
@@ -118,72 +121,52 @@ fromNixLikeContext :: NixLikeContext -> S.HashSet StringContext
118121
fromNixLikeContext =
119122
S.fromList . join . map toStringContexts . M.toList . getNixLikeContext
120123

121-
principledGetContext :: NixString -> S.HashSet StringContext
122-
principledGetContext = nsContext
123-
124-
-- | Combine two NixStrings using mappend
125-
principledMempty :: NixString
126-
principledMempty = NixString "" mempty
127-
128-
-- | Combine two NixStrings using mappend
129-
principledStringMappend :: NixString -> NixString -> NixString
130-
principledStringMappend (NixString s1 t1) (NixString s2 t2) =
131-
NixString (s1 <> s2) (t1 <> t2)
124+
getContext :: NixString -> S.HashSet StringContext
125+
getContext = nsContext
132126

133127
-- | Combine NixStrings with a separator
134-
principledIntercalateNixString :: NixString -> [NixString] -> NixString
135-
principledIntercalateNixString _ [] = principledMempty
136-
principledIntercalateNixString _ [ns] = ns
137-
principledIntercalateNixString sep nss = NixString contents ctx
128+
intercalateNixString :: NixString -> [NixString] -> NixString
129+
intercalateNixString _ [] = mempty
130+
intercalateNixString _ [ns] = ns
131+
intercalateNixString sep nss = NixString contents ctx
138132
where
139133
contents = Text.intercalate (nsContents sep) (map nsContents nss)
140134
ctx = S.unions (nsContext sep : map nsContext nss)
141135

142-
-- | Empty string with empty context.
143-
principledStringMempty :: NixString
144-
principledStringMempty = NixString mempty mempty
145-
146136
-- | Combine NixStrings using mconcat
147-
principledStringMConcat :: [NixString] -> NixString
148-
principledStringMConcat =
149-
foldr principledStringMappend (NixString mempty mempty)
150-
151-
--instance Semigroup NixString where
152-
--NixString s1 t1 <> NixString s2 t2 = NixString (s1 <> s2) (t1 <> t2)
153-
154-
--instance Monoid NixString where
155-
-- mempty = NixString mempty mempty
156-
-- mappend = (<>)
137+
stringMConcat :: [NixString] -> NixString
138+
stringMConcat =
139+
foldr mappend (NixString mempty mempty)
157140

158141
-- | Extract the string contents from a NixString that has no context
159-
principledGetStringNoContext :: NixString -> Maybe Text
160-
principledGetStringNoContext (NixString s c) | null c = Just s
142+
getStringNoContext :: NixString -> Maybe Text
143+
getStringNoContext (NixString s c) | null c = Just s
161144
| otherwise = Nothing
162145

163146
-- | Extract the string contents from a NixString even if the NixString has an associated context
164-
principledStringIgnoreContext :: NixString -> Text
165-
principledStringIgnoreContext (NixString s _) = s
147+
stringIgnoreContext :: NixString -> Text
148+
stringIgnoreContext (NixString s _) = s
166149

167150
-- | Returns True if the NixString has an associated context
168151
stringHasContext :: NixString -> Bool
169152
stringHasContext (NixString _ c) = not (null c)
170153

171154
-- | Constructs a NixString without a context
172-
principledMakeNixStringWithoutContext :: Text -> NixString
173-
principledMakeNixStringWithoutContext = flip NixString mempty
155+
makeNixStringWithoutContext :: Text -> NixString
156+
makeNixStringWithoutContext = flip NixString mempty
174157

175158
-- | Modify the string part of the NixString, leaving the context unchanged
176-
principledModifyNixContents :: (Text -> Text) -> NixString -> NixString
177-
principledModifyNixContents f (NixString s c) = NixString (f s) c
159+
modifyNixContents :: (Text -> Text) -> NixString -> NixString
160+
modifyNixContents f (NixString s c) = NixString (f s) c
178161

179162
-- | Create a NixString using a singleton context
180-
principledMakeNixStringWithSingletonContext
163+
makeNixStringWithSingletonContext
181164
:: Text -> StringContext -> NixString
182-
principledMakeNixStringWithSingletonContext s c = NixString s (S.singleton c)
165+
makeNixStringWithSingletonContext s c = NixString s (S.singleton c)
183166

184167
-- | Create a NixString from a Text and context
185-
principledMakeNixString :: Text -> S.HashSet StringContext -> NixString
186-
principledMakeNixString = NixString
168+
makeNixString :: Text -> S.HashSet StringContext -> NixString
169+
makeNixString = NixString
187170

188171
-- | A monad for accumulating string context while producing a result string.
189172
newtype WithStringContextT m a = WithStringContextT (WriterT (S.HashSet StringContext) m a)
@@ -232,21 +215,21 @@ runWithStringContext' = runIdentity . runWithStringContextT'
232215

233216
-- | Combine two NixStrings using mappend
234217
hackyStringMappend :: NixString -> NixString -> NixString
235-
hackyStringMappend = principledStringMappend
218+
hackyStringMappend = mappend
236219

237220
-- | Combine NixStrings using mconcat
238221
hackyStringMConcat :: [NixString] -> NixString
239-
hackyStringMConcat = principledStringMConcat
222+
hackyStringMConcat = stringMConcat
240223

241224
-- | Constructs a NixString without a context
242225
hackyMakeNixStringWithoutContext :: Text -> NixString
243-
hackyMakeNixStringWithoutContext = principledMakeNixStringWithoutContext
226+
hackyMakeNixStringWithoutContext = makeNixStringWithoutContext
244227

245228
-- | Extract the string contents from a NixString even if the NixString has an associated context
246229
hackyStringIgnoreContext :: NixString -> Text
247-
hackyStringIgnoreContext = principledStringIgnoreContext
230+
hackyStringIgnoreContext = stringIgnoreContext
248231

249232
-- | Extract the string contents from a NixString that has no context
250233
hackyGetStringNoContext :: NixString -> Maybe Text
251-
hackyGetStringNoContext = principledGetStringNoContext
234+
hackyGetStringNoContext = getStringNoContext
252235

0 commit comments

Comments
 (0)