Skip to content

Commit 9c63891

Browse files
committed
Standard: add type aliases St{andardIO,d{Val,Thunk,IO}}
1 parent 400f546 commit 9c63891

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

main/Main.hs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import qualified Data.Text.IO as Text
1919
import Text.Show.Pretty ( ppShow )
2020
import Nix hiding ( force )
2121
import Nix.Convert
22-
import Nix.Fresh.Basic
2322
import Nix.Json
2423
import Nix.Options.Parser
2524
import Nix.Standard
@@ -47,7 +46,7 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
4746
where
4847
-- 2021-07-15: NOTE: This logic should be weaved stronger through CLI options logic (OptParse-Applicative code)
4948
-- As this logic is not stated in the CLI documentation, for example. So user has no knowledge of these.
50-
execContentsFilesOrRepl :: StandardT (StdIdT IO) ()
49+
execContentsFilesOrRepl :: StdIO
5150
execContentsFilesOrRepl =
5251
fromMaybe
5352
loadFromCliFilePathList
@@ -57,6 +56,7 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
5756
)
5857
where
5958
-- | The base case: read expressions from the last CLI directive (@[FILE]@) listed on the command line.
59+
loadFromCliFilePathList :: StdIO
6060
loadFromCliFilePathList =
6161
case filePaths of
6262
[] -> runRepl
@@ -71,12 +71,13 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
7171
expr <- liftIO Text.getContents
7272
processExpr expr
7373

74-
processSeveralFiles :: [Path] -> StandardT (StdIdT IO) ()
74+
processSeveralFiles :: [Path] -> StdIO
7575
processSeveralFiles = traverse_ processFile
7676
where
7777
processFile path = handleResult (pure path) =<< parseNixFileLoc path
7878

7979
-- | The `--read` option: load expression from a serialized file.
80+
loadBinaryCacheFile :: Maybe StdIO
8081
loadBinaryCacheFile =
8182
(\ (binaryCacheFile :: Path) ->
8283
do
@@ -85,9 +86,11 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
8586
) <$> readFrom
8687

8788
-- | The `--expr` option: read expression from the argument string
89+
loadLiteralExpression :: Maybe StdIO
8890
loadLiteralExpression = processExpr <$> expression
8991

9092
-- | The `--file` argument: read expressions from the files listed in the argument file
93+
loadExpressionFromFile :: Maybe StdIO
9194
loadExpressionFromFile =
9295
-- We can start use Text as in the base case, requires changing Path -> Text
9396
-- But that is a gradual process:
@@ -99,6 +102,7 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
99102
) <$> fromFile
100103

101104
processExpr text = handleResult Nothing $ parseNixTextLoc text
105+
processExpr :: Text -> StdIO
102106

103107
withEmptyNixContext = withNixContext mempty
104108

@@ -133,8 +137,8 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
133137
NixException frames ->
134138
errorWithoutStackTrace . show =<<
135139
renderFrames
136-
@(StdValue (StandardT (StdIdT IO)))
137-
@(StdThunk (StandardT (StdIdT IO)))
140+
@StdVal
141+
@StdThun
138142
frames
139143

140144
when repl $
@@ -149,7 +153,7 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
149153
)
150154

151155
-- 2021-07-15: NOTE: Logic of CLI Option processing is scattered over several functions, needs to be consolicated.
152-
processCLIOptions :: Maybe Path -> NExprLoc -> StandardT (StdIdT IO) ()
156+
processCLIOptions :: Maybe Path -> NExprLoc -> StdIO
153157
processCLIOptions mpath expr
154158
| evaluate =
155159
if
@@ -174,7 +178,10 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
174178
evaluateExprWithEvaluator evaluator = evaluateExpression (coerce mpath) evaluator printer
175179

176180
printer
177-
| finder = findAttrs <=< fromValue @(AttrSet (StdValue (StandardT (StdIdT IO))))
181+
:: StdVal
182+
-> StdIO
183+
printer
184+
| finder = findAttrs <=< fromValue @(AttrSet StdVal)
178185
| otherwise = printer'
179186
where
180187
printer'
@@ -191,16 +198,17 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
191198
where
192199
go
193200
:: (b -> Text)
194-
-> (a -> StandardT (StdIdT IO) b)
201+
-> (a -> StandardIO b)
195202
-> a
196-
-> StandardT (StdIdT IO) ()
197203
go g f = liftIO . Text.putStrLn . g <=< f
204+
-> StdIO
198205

199206
findAttrs
200-
:: AttrSet (StdValue (StandardT (StdIdT IO)))
201-
-> StandardT (StdIdT IO) ()
207+
:: AttrSet StdVal
208+
-> StdIO
202209
findAttrs = go mempty
203210
where
211+
go :: Text -> AttrSet StdVal -> StdIO
204212
go prefix s =
205213
do
206214
xs <-
@@ -214,7 +222,7 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
214222
path = prefix <> k
215223
(_, descend) = filterEntry path k
216224

217-
val <- readRef @(StandardT (StdIdT IO)) ref
225+
val <- readRef @StandardIO ref
218226
bool
219227
(pure Nothing)
220228
(forceEntry path nv)
@@ -267,10 +275,10 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
267275
_ -> (True , True )
268276

269277
forceEntry
270-
:: MonadValue a (StandardT (StdIdT IO))
278+
:: MonadValue a StandardIO
271279
=> Text
272280
-> a
273-
-> StandardT (StdIdT IO) (Maybe a)
281+
-> StandardIO (Maybe a)
274282
forceEntry k v =
275283
catch
276284
(pure <$> demand v)

src/Nix/Standard.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ newtype StdCited m a =
5959
newtype StdThunk m =
6060
StdThunk
6161
(StdCited m (NThunkF m (StdValue m)))
62-
6362
type StdValue' m = NValue' (StdThunk m) (StdCited m) m (StdValue m)
6463
type StdValue m = NValue (StdThunk m) (StdCited m) m
64+
type StandardIO = StandardT (StdIdT IO)
65+
type StdVal = StdValue StandardIO
66+
type StdThun = StdThunk StandardIO
67+
type StdIO = StandardIO ()
6568

6669
-- | Type alias:
6770
--
@@ -371,5 +374,5 @@ runWithBasicEffects opts =
371374
i <- newRef (1 :: Int)
372375
runFreshIdT i action
373376

374-
runWithBasicEffectsIO :: Options -> StandardT (StdIdT IO) a -> IO a
377+
runWithBasicEffectsIO :: Options -> StandardIO a -> IO a
375378
runWithBasicEffectsIO = runWithBasicEffects

tests/TestCommon.hs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{-# language PartialTypeSignatures #-}
21

32
module TestCommon where
43

@@ -8,15 +7,14 @@ import Data.Time
87
import Data.Text.IO as Text
98
import Nix
109
import Nix.Standard
11-
import Nix.Fresh.Basic
1210
import System.Environment
1311
import System.IO
1412
import System.PosixCompat.Files
1513
import System.PosixCompat.Temp
1614
import System.Process
1715
import Test.Tasty.HUnit
1816

19-
hnixEvalFile :: Options -> Path -> IO (StdValue (StandardT (StdIdT IO)))
17+
hnixEvalFile :: Options -> Path -> IO StdVal
2018
hnixEvalFile opts file =
2119
do
2220
parseResult <- parseNixFileLoc file
@@ -31,20 +29,20 @@ hnixEvalFile opts file =
3129
NixException frames ->
3230
errorWithoutStackTrace . show
3331
=<< renderFrames
34-
@(StdValue (StandardT (StdIdT IO)))
35-
@(StdThunk (StandardT (StdIdT IO)))
36-
frames
32+
@StdVal
33+
@StdThun
34+
frames
3735
)
3836
parseResult
3937

40-
hnixEvalText :: Options -> Text -> IO (StdValue (StandardT (StdIdT IO)))
38+
hnixEvalText :: Options -> Text -> IO StdVal
4139
hnixEvalText opts src =
4240
either
4341
(\ err -> fail $ toString $ "Parsing failed for expression `" <> src <> "`.\n" <> show err)
4442
(\ expr ->
4543
runWithBasicEffects opts $ normalForm =<< nixEvalExpr mempty expr
4644
)
47-
(parseNixText src)
45+
$ parseNixText src
4846

4947
nixEvalString :: Text -> IO Text
5048
nixEvalString expr =

0 commit comments

Comments
 (0)