Skip to content

Commit ac17af6

Browse files
class MonadValue: move Kleisli out of inform (#874)
This the last change in the `class MonadValue`. After this, there are finishing touches to close the #850
1 parent d06b8d7 commit ac17af6

File tree

5 files changed

+31
-35
lines changed

5 files changed

+31
-35
lines changed

src/Nix/Eval.hs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ eval (NAbs params body) = do
163163
scope <- currentScopes :: m (Scopes m v)
164164
evalAbs params $ \arg k -> withScopes scope $ do
165165
args <- buildArgument params arg
166-
pushScope args (k (fmap (inform (withScopes scope)) args) body)
166+
pushScope args (k (fmap (withScopes scope . inform) args) body)
167167

168168
eval (NSynHole name) = synHole name
169169

@@ -427,13 +427,14 @@ buildArgument params arg = do
427427
-> Text
428428
-> These v (Maybe (m v))
429429
-> Maybe (AttrSet v -> m v)
430-
assemble scope isVariadic k = \case
431-
That Nothing -> pure $ const $ evalError @v $ ErrorCall $ "Missing value for parameter: " <>show k
432-
That (Just f) -> pure $ \args -> defer $ withScopes scope $ pushScope args f
433-
This _
434-
| isVariadic -> Nothing
435-
| otherwise -> pure $ const $ evalError @v $ ErrorCall $ "Unexpected parameter: " <> show k
436-
These x _ -> pure (const (pure x))
430+
assemble scope isVariadic k =
431+
\case
432+
That Nothing -> pure $ const $ evalError @v $ ErrorCall $ "Missing value for parameter: " <>show k
433+
That (Just f) -> pure $ \args -> defer $ withScopes scope $ pushScope args f
434+
This _
435+
| isVariadic -> Nothing
436+
| otherwise -> pure $ const $ evalError @v $ ErrorCall $ "Unexpected parameter: " <> show k
437+
These x _ -> pure (const (pure x))
437438

438439
addSourcePositions
439440
:: (MonadReader e m, Has e SrcSpan) => Transform NExprLocF (m a)

src/Nix/Lint.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,16 +283,15 @@ instance (MonadThunkId m, MonadAtomicRef m, MonadCatch m)
283283
defer = fmap ST . thunk
284284

285285
demand :: Symbolic m -> m (Symbolic m)
286-
demand = undefined
287-
-- demand (ST v)= demand =<< force v
288-
-- demand (SV v)= f (SV v)
286+
demand (ST v)= demand =<< force v
287+
demand (SV v)= pure (SV v)
289288

290289

291290
instance (MonadThunkId m, MonadAtomicRef m, MonadCatch m)
292291
=> MonadValueF (Symbolic m) m where
293292

294293
demandF :: (Symbolic m -> m r) -> Symbolic m -> m r
295-
demandF f (ST v)= (demandF f) =<< force v
294+
demandF f (ST v)= demandF f =<< force v
296295
demandF f (SV v)= f (SV v)
297296

298297

src/Nix/Standard.hs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,10 @@ instance
244244
v
245245

246246
inform
247-
:: ( m (StdValue m)
248-
-> m (StdValue m)
249-
)
250-
-> StdValue m
247+
:: StdValue m
251248
-> m (StdValue m)
252-
-- 2021-02-27: NOTE: When swapping, switch to `further`.
253-
inform f (Pure t) = Pure <$> furtherF f t
254-
inform f (Free v) = Free <$> bindNValue' id (inform f) v
249+
inform (Pure t) = Pure <$> further t
250+
inform (Free v) = Free <$> bindNValue' id inform v
255251

256252

257253
-- * @instance MonadValueF (StdValue m) m@
@@ -283,7 +279,7 @@ instance
283279
)
284280
-> StdValue m
285281
-> m (StdValue m)
286-
-- 2021-02-27: NOTE: When swapping, switch to `further`.
282+
-- 2021-02-27: NOTE: Switch to `further` and `inform`. Probably just informF f = f <=< inform
287283
informF f (Pure t) = Pure <$> furtherF f t
288284
informF f (Free v) = Free <$> bindNValue' id (informF f) v
289285

src/Nix/Type/Infer.hs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,15 @@ instance Monad m => MonadThrow (InferT s m) where
388388
throwM = throwError . EvaluationError
389389

390390
instance Monad m => MonadCatch (InferT s m) where
391-
catch m h = catchError m $ \case
392-
EvaluationError e -> maybe
393-
(error $ "Exception was not an exception: " <> show e)
394-
h
395-
(fromException (toException e))
396-
err -> error $ "Unexpected error: " <> show err
391+
catch m h =
392+
catchError m $
393+
\case
394+
EvaluationError e ->
395+
maybe
396+
(error $ "Exception was not an exception: " <> show e)
397+
h
398+
(fromException (toException e))
399+
err -> error $ "Unexpected error: " <> show err
397400

398401
type MonadInfer m
399402
= ({- MonadThunkId m,-}
@@ -409,15 +412,12 @@ instance Monad m => MonadValue (Judgment s) (InferT s m) where
409412
demand
410413
:: Judgment s
411414
-> InferT s m (Judgment s)
412-
demand = demandF pure
415+
demand = pure
413416

414417
inform
415-
:: ( InferT s m (Judgment s)
416-
-> InferT s m (Judgment s)
417-
)
418-
-> Judgment s
418+
:: Judgment s
419419
-> InferT s m (Judgment s)
420-
inform f j = f (pure j)
420+
inform = pure
421421

422422

423423
-- 2021-02-22: NOTE: Seems like suporflous instance
@@ -428,15 +428,15 @@ instance Monad m => MonadValueF (Judgment s) (InferT s m) where
428428
-> InferT s m r)
429429
-> Judgment s
430430
-> InferT s m r
431-
demandF = ($)
431+
demandF f a = f a
432432

433433
informF
434434
:: ( InferT s m (Judgment s)
435435
-> InferT s m (Judgment s)
436436
)
437437
-> Judgment s
438438
-> InferT s m (Judgment s)
439-
informF f j = f (pure j)
439+
informF f j = f $ pure j
440440

441441
{-
442442
instance MonadInfer m

src/Nix/Value/Monad.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class MonadValue v m where
1010
-- | If 'v' is a thunk, 'inform' allows us to modify the action to be
1111
-- performed by the thunk, perhaps by enriching it with scope info, for
1212
-- example.
13-
inform :: (m v -> m v) -> v -> m v
13+
inform :: v -> m v
1414

1515

1616
-- * @MonadValueF@ - a Kleisli-able customization class

0 commit comments

Comments
 (0)