Skip to content

Commit

Permalink
treewide: unflip the class MonadValue demand & all its implementa…
Browse files Browse the repository at this point in the history
…tion

Argument order change has functional argument - so it is not possible to
elegantly switch into new code, and requires to go though transition.

So doing the work while doing needed refactoring at the same time.

This style of code currenly may seem more noizy, but really it is more
straight-forward, it mentions only operations & transformatios, types can be
looked in the HLS.

With the future work #850
this style of the code would radically start to simplify itself, so please bear
with me.
  • Loading branch information
Anton-Latukha committed Feb 26, 2021
1 parent cc26495 commit bf3e447
Show file tree
Hide file tree
Showing 15 changed files with 552 additions and 405 deletions.
2 changes: 1 addition & 1 deletion main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ main = do
_ -> (True, True)

forceEntry k v =
catch (pure <$> demand v pure) $ \(NixException frames) -> do
catch (pure <$> demand pure v) $ \(NixException frames) -> do
liftIO
. putStrLn
. ("Exception forcing " <>)
Expand Down
11 changes: 5 additions & 6 deletions main/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Nix hiding ( exec
)
import Nix.Scope
import Nix.Utils
import Nix.Value.Monad (demand)
import Nix.Value.Monad ( demand )

import qualified Data.List
import qualified Data.Maybe
Expand Down Expand Up @@ -395,11 +395,10 @@ completeFunc reversedPrev word
-- Stop on last subField (we care about the keys at this level)
[_] -> pure $ keys m
f:fs ->
case Data.HashMap.Lazy.lookup f m of
Nothing -> pure mempty
Just e ->
demand e
(\e' -> (fmap . fmap) (("." <> f) <>) $ algebraicComplete fs e')
maybe
(pure mempty)
(demand (\e' -> (fmap . fmap) (("." <> f) <>) $ algebraicComplete fs e'))
(Data.HashMap.Lazy.lookup f m)

in case val of
NVSet xs _ -> withMap xs
Expand Down
61 changes: 31 additions & 30 deletions src/Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,14 @@ evaluateExpression mpath evaluator handler expr = do
args <- traverse (traverse eval') $ fmap (second parseArg) (arg opts) <> fmap
(second mkStr)
(argstr opts)
evaluator mpath expr >>= \f -> demand f $ \f' ->
processResult handler =<< case f' of
NVClosure _ g -> g (argmap args)
_ -> pure f
evaluator mpath expr >>= \f ->
demand
(\f' ->
processResult handler =<< case f' of
NVClosure _ g -> g (argmap args)
_ -> pure f
)
f
where
parseArg s = case parseNixText s of
Success x -> x
Expand All @@ -137,29 +141,26 @@ processResult h val = do
where
go :: [Text.Text] -> NValue t f m -> m a
go [] v = h v
go ((Text.decimal -> Right (n,"")) : ks) v = demand v $ \case
NVList xs -> case ks of
[] -> h (xs !! n)
_ -> go ks (xs !! n)
_ ->
errorWithoutStackTrace
$ "Expected a list for selector '"
<> show n
<> "', but got: "
<> show v
go (k : ks) v = demand v $ \case
NVSet xs _ -> case M.lookup k xs of
Nothing ->
errorWithoutStackTrace
$ "Set does not contain key '"
<> Text.unpack k
<> "'"
Just v' -> case ks of
[] -> h v'
_ -> go ks v'
_ ->
errorWithoutStackTrace
$ "Expected a set for selector '"
<> Text.unpack k
<> "', but got: "
<> show v
go ((Text.decimal -> Right (n,"")) : ks) v =
demand
(\case
NVList xs ->
case ks of
[] -> h (xs !! n)
_ -> go ks (xs !! n)
_ -> errorWithoutStackTrace $ "Expected a list for selector '" <> show n <> "', but got: " <> show v
)
v
go (k : ks) v =
demand
(\case
NVSet xs _ ->
maybe
(errorWithoutStackTrace $ "Set does not contain key '" <> Text.unpack k <> "'")
(case ks of
[] -> h
_ -> go ks)
(M.lookup k xs)
_ -> errorWithoutStackTrace $ "Expected a set for selector '" <> Text.unpack k <> "', but got: " <> show v
)
v
Loading

0 comments on commit bf3e447

Please sign in to comment.