Skip to content

Commit 7b811ea

Browse files
Merge #871: Form MonadValueF; Lint: m refactor
Currenly simply duplicates, but this would allow me to `demand -> demandF` first and get working code, and so then working on switching to new `demand` would be easier, and this safe path also allows to use old version, `demandF`, in a couple of places if something, until everything figures-out. Towards #850.
2 parents 0e3e982 + fc15ee6 commit 7b811ea

File tree

4 files changed

+81
-15
lines changed

4 files changed

+81
-15
lines changed

src/Nix/Lint.hs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,12 @@ merge context = go
203203
)
204204
(pure <$> l)
205205
(pure <$> r)
206-
if M.null m then go xs ys else (TSet (pure m) :) <$> go xs ys
206+
bool
207+
id
208+
((TSet (pure m) :) <$>)
209+
(not $ M.null m)
210+
(go xs ys)
211+
207212
(TClosure{}, TClosure{}) ->
208213
throwError $ ErrorCall "Cannot unify functions"
209214
(TBuiltin _ _, TBuiltin _ _) ->
@@ -289,22 +294,21 @@ instance (MonadThunkId m, MonadAtomicRef m, MonadCatch m)
289294
demand f (ST v)= (demand f) =<< force v
290295
demand f (SV v)= f (SV v)
291296

297+
298+
instance (MonadThunkId m, MonadAtomicRef m, MonadCatch m)
299+
=> MonadValueF (Symbolic m) m where
300+
301+
demandF :: (Symbolic m -> m r) -> Symbolic m -> m r
302+
demandF f (ST v)= (demandF f) =<< force v
303+
demandF f (SV v)= f (SV v)
304+
305+
292306
instance MonadLint e m => MonadEval (Symbolic m) m where
293307
freeVariable var = symerr $ "Undefined variable '" <> Text.unpack var <> "'"
294308

295-
attrMissing ks Nothing =
296-
evalError @(Symbolic m)
297-
$ ErrorCall
298-
$ "Inheriting unknown attribute: "
299-
<> intercalate "." (fmap Text.unpack (NE.toList ks))
309+
attrMissing ks Nothing = evalError @(Symbolic m) $ ErrorCall $ "Inheriting unknown attribute: " <> intercalate "." (fmap Text.unpack (NE.toList ks))
300310

301-
attrMissing ks (Just s) =
302-
evalError @(Symbolic m)
303-
$ ErrorCall
304-
$ "Could not look up attribute "
305-
<> intercalate "." (fmap Text.unpack (NE.toList ks))
306-
<> " in "
307-
<> show s
311+
attrMissing ks (Just s) = evalError @(Symbolic m) $ ErrorCall $ "Could not look up attribute " <> intercalate "." (fmap Text.unpack (NE.toList ks)) <> " in " <> show s
308312

309313
evalCurPos = do
310314
f <- mkSymbolic [TPath]
@@ -344,7 +348,8 @@ instance MonadLint e m => MonadEval (Symbolic m) m where
344348
(unpackSymbolic >=> \case
345349
NMany [TSet (Just s')] -> pure s'
346350
NMany [TSet Nothing] -> error "NYI: with unknown"
347-
_ -> throwError $ ErrorCall "scope must be a set in with statement")
351+
_ -> throwError $ ErrorCall "scope must be a set in with statement"
352+
)
348353
s
349354

350355
evalIf cond t f = do

src/Nix/Standard.hs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ instance
219219
furtherF k t = StdThunk . StdCited <$> furtherF k (_stdCited $ _stdThunk t)
220220

221221

222-
-- * @instance MonadValue@
222+
-- * @instance MonadValue (StdValue m) m@
223223

224224
instance ( MonadAtomicRef m
225225
, MonadCatch m
@@ -256,6 +256,39 @@ instance ( MonadAtomicRef m
256256
inform f (Free v) = Free <$> bindNValue' id (inform f) v
257257

258258

259+
-- * @instance MonadValueF (StdValue m) m@
260+
261+
instance ( MonadAtomicRef m
262+
, MonadCatch m
263+
, Typeable m
264+
, MonadReader (Context m (StdValue m)) m
265+
, MonadThunkId m
266+
)
267+
=> MonadValueF (StdValue m) m where
268+
269+
demandF
270+
:: ( StdValue m
271+
-> m r
272+
)
273+
-> StdValue m
274+
-> m r
275+
demandF f v =
276+
free
277+
((demandF f) <=< force)
278+
(const $ f v)
279+
v
280+
281+
informF
282+
:: ( m (StdValue m)
283+
-> m (StdValue m)
284+
)
285+
-> StdValue m
286+
-> m (StdValue m)
287+
-- 2021-02-27: NOTE: When swapping, switch to `further`.
288+
informF f (Pure t) = Pure <$> furtherF f t
289+
informF f (Free v) = Free <$> bindNValue' id (informF f) v
290+
291+
259292
{------------------------------------------------------------------------}
260293

261294
-- jww (2019-03-22): NYI

src/Nix/Type/Infer.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,25 @@ instance Monad m => MonadValue (Judgment s) (InferT s m) where
421421
-> InferT s m (Judgment s)
422422
inform f j = f (pure j)
423423

424+
425+
-- 2021-02-22: NOTE: Seems like suporflous instance
426+
instance Monad m => MonadValueF (Judgment s) (InferT s m) where
427+
428+
demandF
429+
:: ( Judgment s
430+
-> InferT s m r)
431+
-> Judgment s
432+
-> InferT s m r
433+
demandF = ($)
434+
435+
informF
436+
:: ( InferT s m (Judgment s)
437+
-> InferT s m (Judgment s)
438+
)
439+
-> Judgment s
440+
-> InferT s m (Judgment s)
441+
informF f j = f (pure j)
442+
424443
{-
425444
instance MonadInfer m
426445
=> MonadThunk (JThunkT s m) (InferT s m) (Judgment s) where

src/Nix/Value/Monad.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@
22

33
module Nix.Value.Monad where
44

5+
-- * @MonadValue@ - a main implementation class
6+
57
class MonadValue v m where
68
defer :: m v -> m v
79
demand :: (v -> m r) -> v -> m r
810
-- | If 'v' is a thunk, 'inform' allows us to modify the action to be
911
-- performed by the thunk, perhaps by enriching it with scope info, for
1012
-- example.
1113
inform :: (m v -> m v) -> v -> m v
14+
15+
16+
-- * @MonadValueF@ - a Kleisli-able customization class
17+
18+
class MonadValueF v m where
19+
demandF :: (v -> m r) -> v -> m r
20+
informF :: (m v -> m v) -> v -> m v

0 commit comments

Comments
 (0)