Skip to content

Commit 2ba1061

Browse files
committed
{Exec, Cited.Basic, Type.Infer, Main}: m clean-up; Exec: m refactor
1 parent 06c5396 commit 2ba1061

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

main/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ main =
271271
do
272272
liftIO $
273273
do
274-
putStrLn $ "Wrote winnowed expression tree to " <> path
274+
putStrLn $ "Wrote sifted expression tree to " <> path
275275
writeFile path $ show $ prettyNix $ stripAnnotation expr'
276276
either
277277
throwM

src/Nix/Cited/Basic.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Control.Comonad.Env ( ComonadEnv )
1111
import Control.Monad.Catch hiding ( catchJust )
1212
import Nix.Cited
1313
import Nix.Eval as Eval
14+
( EvalFrame(EvaluatingExpr,ForcingExpr) )
1415
import Nix.Exec
1516
import Nix.Expr
1617
import Nix.Frames

src/Nix/Exec.hs

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ nverr :: forall e t f s m a . (MonadNix e t f m, Exception s) => s -> m a
141141
nverr = evalError @(NValue t f m)
142142

143143
currentPos :: forall e m . (MonadReader e m, Has e SrcSpan) => m SrcSpan
144-
currentPos = asks (view hasLens)
144+
currentPos = asks $ view hasLens
145145

146146
wrapExprLoc :: SrcSpan -> NExprLocF r -> NExprLoc
147-
wrapExprLoc span x = Fix (Fix (NSym_ span "<?>") <$ x)
147+
wrapExprLoc span x = Fix $ Fix (NSym_ span "<?>") <$ x
148148
{-# inline wrapExprLoc #-}
149149

150150
-- 2021-01-07: NOTE: This instance belongs to be beside MonadEval type class.
@@ -172,27 +172,27 @@ instance MonadNix e t f m => MonadEval (NValue t f m) m where
172172
)
173173
ms
174174
where
175-
attr = Text.intercalate "." (NE.toList ks)
175+
attr = Text.intercalate "." $ NE.toList ks
176176

177177
evalCurPos = do
178178
scope <- currentScopes
179179
span@(SrcSpan delta _) <- currentPos
180180
addProvenance @_ @_ @(NValue t f m)
181-
(Provenance scope (NSym_ span "__curPos")) <$>
181+
(Provenance scope $ NSym_ span "__curPos") <$>
182182
toValue delta
183183

184184
evaledSym name val = do
185185
scope <- currentScopes
186186
span <- currentPos
187187
pure $
188188
addProvenance @_ @_ @(NValue t f m)
189-
(Provenance scope (NSym_ span name))
189+
(Provenance scope $ NSym_ span name)
190190
val
191191

192192
evalConstant c = do
193193
scope <- currentScopes
194194
span <- currentPos
195-
pure $ nvConstantP (Provenance scope (NConstant_ span c)) c
195+
pure $ nvConstantP (Provenance scope $ NConstant_ span c) c
196196

197197
evalString =
198198
maybe
@@ -205,7 +205,7 @@ instance MonadNix e t f m => MonadEval (NValue t f m) m where
205205
nvStrP
206206
(Provenance
207207
scope
208-
(NStr_ span (DoubleQuoted [Plain (stringIgnoreContext ns)]))
208+
(NStr_ span $ DoubleQuoted [Plain $ stringIgnoreContext ns])
209209
)
210210
ns
211211
)
@@ -214,13 +214,13 @@ instance MonadNix e t f m => MonadEval (NValue t f m) m where
214214
evalLiteralPath p = do
215215
scope <- currentScopes
216216
span <- currentPos
217-
nvPathP (Provenance scope (NLiteralPath_ span p)) <$>
217+
nvPathP (Provenance scope $ NLiteralPath_ span p) <$>
218218
makeAbsolutePath @t @f @m p
219219

220220
evalEnvPath p = do
221221
scope <- currentScopes
222222
span <- currentPos
223-
nvPathP (Provenance scope (NEnvPath_ span p)) <$>
223+
nvPathP (Provenance scope $ NEnvPath_ span p) <$>
224224
findEnvPath @t @f @m p
225225

226226
evalUnary op arg = do
@@ -236,20 +236,23 @@ instance MonadNix e t f m => MonadEval (NValue t f m) m where
236236
evalWith c b = do
237237
scope <- currentScopes
238238
span <- currentPos
239-
(\b -> addProvenance (Provenance scope (NWith_ span Nothing (pure b))) b)
240-
<$> evalWithAttrSet c b
239+
let f = join $ addProvenance . Provenance scope . NWith_ span Nothing . pure
240+
f <$> evalWithAttrSet c b
241241

242242
evalIf c t f = do
243243
scope <- currentScopes
244244
span <- currentPos
245245
b <- fromValue c
246246

247247
let
248-
fun x y = addProvenance (Provenance scope (NIf_ span (pure c) x y))
248+
fun x y z = addProvenance (Provenance scope $ NIf_ span (pure c) x y) z
249+
-- Note: join == \ f x -> f x x
250+
false = join (fun Nothing . pure) <$> f
251+
true = join (flip fun Nothing . pure) <$> t
249252

250253
bool
251-
( (\ f' -> fun Nothing (pure f') f') <$> f )
252-
( (\ t' -> fun (pure t') Nothing t') <$> t )
254+
false
255+
true
253256
b
254257

255258
evalAssert c body =
@@ -260,25 +263,23 @@ instance MonadNix e t f m => MonadEval (NValue t f m) m where
260263
(nverr $ Assertion span c)
261264
(do
262265
scope <- currentScopes
263-
(\b ->
264-
addProvenance (Provenance scope (NAssert_ span (pure c) (pure b))) b
265-
) <$>
266-
body
266+
let f = join (addProvenance . Provenance scope . NAssert_ span (pure c) . pure)
267+
f <$> body
267268
)
268269
b
269270

270271
evalApp f x = do
271272
scope <- currentScopes
272273
span <- currentPos
273-
addProvenance (Provenance scope (NBinary_ span NApp (pure f) Nothing)) <$>
274+
addProvenance (Provenance scope $ NBinary_ span NApp (pure f) Nothing) <$>
274275
(callFunc f =<< defer x)
275276

276277
evalAbs p k = do
277278
scope <- currentScopes
278279
span <- currentPos
279280
pure $
280281
nvClosureP
281-
(Provenance scope (NAbs_ span (Nothing <$ p) Nothing))
282+
(Provenance scope $ NAbs_ span (Nothing <$ p) Nothing)
282283
(void p)
283284
(\arg -> snd <$> k (pure arg) (\_ b -> ((), ) <$> b))
284285

@@ -293,7 +294,7 @@ callFunc
293294
-> m (NValue t f m)
294295
callFunc fun arg =
295296
do
296-
frames :: Frames <- asks (view hasLens)
297+
frames :: Frames <- asks $ view hasLens
297298
when (length frames > 2000) $ throwError $ ErrorCall "Function call stack exhausted"
298299

299300
fun' <- demand fun
@@ -304,8 +305,8 @@ callFunc fun arg =
304305
span <- currentPos
305306
withFrame Info ((Calling @m @(NValue t f m)) name span) (f arg)
306307
(NVSet m _) | Just f <- M.lookup "__functor" m ->
307-
((`callFunc` arg) <=< (`callFunc` fun')) =<< demand f
308-
x -> throwError $ ErrorCall $ "Attempt to call non-function: " <> show x
308+
(`callFunc` arg) =<< (`callFunc` fun') =<< demand f
309+
_x -> throwError $ ErrorCall $ "Attempt to call non-function: " <> show _x
309310

310311
execUnaryOp
311312
:: (Framed e m, MonadCited t f m, Show t)
@@ -321,12 +322,12 @@ execUnaryOp scope span op arg = do
321322
(NNeg, NInt i ) -> unaryOp $ NInt (-i)
322323
(NNeg, NFloat f) -> unaryOp $ NFloat (-f)
323324
(NNot, NBool b ) -> unaryOp $ NBool (not b)
324-
_ ->
325-
throwError $ ErrorCall $ "unsupported argument type for unary operator " <> show op
326-
x ->
327-
throwError $ ErrorCall $ "argument to unary operator must evaluate to an atomic type: " <> show x
325+
_seq ->
326+
throwError $ ErrorCall $ "unsupported argument type for unary operator " <> show _seq
327+
_x ->
328+
throwError $ ErrorCall $ "argument to unary operator must evaluate to an atomic type: " <> show _x
328329
where
329-
unaryOp = pure . nvConstantP (Provenance scope (NUnary_ span op (pure arg)))
330+
unaryOp = pure . nvConstantP (Provenance scope $ NUnary_ span op $ pure arg)
330331

331332
execBinaryOp
332333
:: forall e t f m
@@ -371,7 +372,7 @@ execBinaryOp scope span op lval rarg =
371372
)
372373
=<< fromValue lval
373374

374-
boolOp rval = toBoolOp (pure rval)
375+
boolOp rval = toBoolOp $ pure rval
375376

376377
bypass = toBoolOp Nothing
377378

@@ -511,7 +512,7 @@ addTracing k v = do
511512
#else
512513
show $ void x
513514
#endif
514-
else prettyNix (Fix (Fix (NSym "?") <$ x))
515+
else prettyNix $ Fix $ Fix (NSym "?") <$ x
515516
msg x = pretty ("eval: " <> replicate depth ' ') <> x
516517
loc <- renderLocation span $ msg rendered <> " ...\n"
517518
putStr $ show loc
@@ -526,7 +527,7 @@ evalExprLoc expr =
526527
let
527528
pTracedAdi =
528529
bool
529-
(adi Eval.evalContent Eval.addMetaInfo)
530+
Eval.framedEvalExprLoc
530531
(join . (`runReaderT` (0 :: Int)) .
531532
adi
532533
(addTracing Eval.evalContent)

src/Nix/Type/Infer.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ import Nix.Atoms
5252
import Nix.Convert
5353
import Nix.Eval ( MonadEval(..) )
5454
import qualified Nix.Eval as Eval
55+
( eval
56+
, evalWithAttrSet
57+
)
5558
import Nix.Expr.Types
5659
import Nix.Expr.Types.Annotated
5760
import Nix.Fresh

0 commit comments

Comments
 (0)