Skip to content

Commit

Permalink
Merge #871: Form MonadValueF; Lint: m refactor
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Anton-Latukha authored Mar 4, 2021
2 parents 0e3e982 + fc15ee6 commit 7b811ea
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 15 deletions.
33 changes: 19 additions & 14 deletions src/Nix/Lint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,12 @@ merge context = go
)
(pure <$> l)
(pure <$> r)
if M.null m then go xs ys else (TSet (pure m) :) <$> go xs ys
bool
id
((TSet (pure m) :) <$>)
(not $ M.null m)
(go xs ys)

(TClosure{}, TClosure{}) ->
throwError $ ErrorCall "Cannot unify functions"
(TBuiltin _ _, TBuiltin _ _) ->
Expand Down Expand Up @@ -289,22 +294,21 @@ instance (MonadThunkId m, MonadAtomicRef m, MonadCatch m)
demand f (ST v)= (demand f) =<< force v
demand f (SV v)= f (SV v)


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

demandF :: (Symbolic m -> m r) -> Symbolic m -> m r
demandF f (ST v)= (demandF f) =<< force v
demandF f (SV v)= f (SV v)


instance MonadLint e m => MonadEval (Symbolic m) m where
freeVariable var = symerr $ "Undefined variable '" <> Text.unpack var <> "'"

attrMissing ks Nothing =
evalError @(Symbolic m)
$ ErrorCall
$ "Inheriting unknown attribute: "
<> intercalate "." (fmap Text.unpack (NE.toList ks))
attrMissing ks Nothing = evalError @(Symbolic m) $ ErrorCall $ "Inheriting unknown attribute: " <> intercalate "." (fmap Text.unpack (NE.toList ks))

attrMissing ks (Just s) =
evalError @(Symbolic m)
$ ErrorCall
$ "Could not look up attribute "
<> intercalate "." (fmap Text.unpack (NE.toList ks))
<> " in "
<> show s
attrMissing ks (Just s) = evalError @(Symbolic m) $ ErrorCall $ "Could not look up attribute " <> intercalate "." (fmap Text.unpack (NE.toList ks)) <> " in " <> show s

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

evalIf cond t f = do
Expand Down
35 changes: 34 additions & 1 deletion src/Nix/Standard.hs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ instance
furtherF k t = StdThunk . StdCited <$> furtherF k (_stdCited $ _stdThunk t)


-- * @instance MonadValue@
-- * @instance MonadValue (StdValue m) m@

instance ( MonadAtomicRef m
, MonadCatch m
Expand Down Expand Up @@ -256,6 +256,39 @@ instance ( MonadAtomicRef m
inform f (Free v) = Free <$> bindNValue' id (inform f) v


-- * @instance MonadValueF (StdValue m) m@

instance ( MonadAtomicRef m
, MonadCatch m
, Typeable m
, MonadReader (Context m (StdValue m)) m
, MonadThunkId m
)
=> MonadValueF (StdValue m) m where

demandF
:: ( StdValue m
-> m r
)
-> StdValue m
-> m r
demandF f v =
free
((demandF f) <=< force)
(const $ f v)
v

informF
:: ( m (StdValue m)
-> m (StdValue m)
)
-> StdValue m
-> m (StdValue m)
-- 2021-02-27: NOTE: When swapping, switch to `further`.
informF f (Pure t) = Pure <$> furtherF f t
informF f (Free v) = Free <$> bindNValue' id (informF f) v


{------------------------------------------------------------------------}

-- jww (2019-03-22): NYI
Expand Down
19 changes: 19 additions & 0 deletions src/Nix/Type/Infer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,25 @@ instance Monad m => MonadValue (Judgment s) (InferT s m) where
-> InferT s m (Judgment s)
inform f j = f (pure j)


-- 2021-02-22: NOTE: Seems like suporflous instance
instance Monad m => MonadValueF (Judgment s) (InferT s m) where

demandF
:: ( Judgment s
-> InferT s m r)
-> Judgment s
-> InferT s m r
demandF = ($)

informF
:: ( InferT s m (Judgment s)
-> InferT s m (Judgment s)
)
-> Judgment s
-> InferT s m (Judgment s)
informF f j = f (pure j)

{-
instance MonadInfer m
=> MonadThunk (JThunkT s m) (InferT s m) (Judgment s) where
Expand Down
9 changes: 9 additions & 0 deletions src/Nix/Value/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

module Nix.Value.Monad where

-- * @MonadValue@ - a main implementation class

class MonadValue v m where
defer :: m v -> m v
demand :: (v -> m r) -> v -> m r
-- | If 'v' is a thunk, 'inform' allows us to modify the action to be
-- performed by the thunk, perhaps by enriching it with scope info, for
-- example.
inform :: (m v -> m v) -> v -> m v


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

class MonadValueF v m where
demandF :: (v -> m r) -> v -> m r
informF :: (m v -> m v) -> v -> m v

0 comments on commit 7b811ea

Please sign in to comment.