Skip to content

Commit 0fdd17b

Browse files
Merge #985: Prelude & Nix.Utils
We essentially had 2 preludes, now they are merged & allow to control what parts of `relude` are exposed. It clean-up imports quite a bit & provides a set of frequently used functions. `relude` is a pretty good prelude, but it still has flaws (for example it exposes a mix of `Text` & `String` functions, which is weird from the outside). If we want to use `Text` internally - that should be it, conversion can still be needed (for `base` ^ GHC reasons), but at least is would be only on the IO margins (which are `prelude` functions), not 2 conversions in the middle of data flow. This setup helps to program with supplying `Text` type functions & support of stronger types. Now we have both the most flexible `Prelude` setup & the `Nix.Utils` that holds custom code, nice.
2 parents 31c9e99 + 0071387 commit 0fdd17b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+547
-487
lines changed

.hlint.yaml

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
name: Use Foldable.forM_
2121
- hint:
2222
lhs: "pure ()"
23-
note: "Use 'pass'"
24-
rhs: pass
23+
note: "Use 'stub'"
24+
rhs: stub
2525
- hint:
2626
lhs: "return ()"
27-
note: "Use 'pass'"
28-
rhs: pass
27+
note: "Use 'stub'"
28+
rhs: stub
2929
- hint:
3030
lhs: "(: [])"
3131
note: "Use `one`"
@@ -2667,11 +2667,32 @@
26672667
lhs: sum xs / length xs
26682668
note: "Use `average` from `Relude.Extra.Foldable`"
26692669
rhs: average xs
2670+
26702671
- hint:
26712672
lhs: "\\a -> (a, a)"
2672-
note: "Use `dup` from `Relude.Extra.Tuple`"
2673+
note: "Use `dup`"
26732674
rhs: dup
26742675

26752676
- warn:
26762677
lhs: "() <$ a"
26772678
rhs: void a
2679+
2680+
- hint:
2681+
lhs: "pass"
2682+
note: "Use 'stub'"
2683+
rhs: stub
2684+
2685+
- hint:
2686+
lhs: "bool mempty a b"
2687+
note: "Use `whenTrue`"
2688+
rhs: a `whenTrue` b
2689+
2690+
- hint:
2691+
lhs: "bool a mempty b"
2692+
note: "Use `whenFalse`"
2693+
rhs: a `whenFalse` b
2694+
2695+
- hint:
2696+
lhs: "maybe mempty a b"
2697+
note: "Use `whenJust`"
2698+
rhs: a `whenJust` b

benchmarks/ParserBench.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module ParserBench (benchmarks) where
22

3-
import Nix.Utils
43
import Nix.Parser
54

65
import Criterion

hnix.cabal

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ flag profiling
341341

342342
library
343343
exposed-modules:
344+
Prelude
344345
Nix
346+
Nix.Utils
345347
Nix.Atoms
346348
Nix.Builtins
347349
Nix.Cache
@@ -383,7 +385,6 @@ library
383385
Nix.Type.Env
384386
Nix.Type.Infer
385387
Nix.Type.Type
386-
Nix.Utils
387388
Nix.Utils.Fix1
388389
Nix.Value
389390
Nix.Value.Equal
@@ -392,13 +393,13 @@ library
392393
Nix.XML
393394
other-modules:
394395
Paths_hnix
396+
Nix.Unused
395397
autogen-modules:
396398
Paths_hnix
397399
hs-source-dirs:
398400
src
399401
mixins:
400402
base hiding (Prelude)
401-
, relude (Relude as Prelude)
402403
, relude
403404
ghc-options:
404405
-Wall
@@ -477,6 +478,7 @@ library
477478
, DeriveLift
478479
, FlexibleContexts
479480
, FlexibleInstances
481+
, ScopedTypeVariables
480482
, StandaloneDeriving
481483
, TypeApplications
482484
, TypeSynonymInstances
@@ -532,7 +534,6 @@ executable hnix
532534
, time
533535
mixins:
534536
base hiding (Prelude)
535-
, relude (Relude as Prelude)
536537
, relude
537538
default-extensions:
538539
OverloadedStrings
@@ -544,6 +545,7 @@ executable hnix
544545
, DeriveLift
545546
, FlexibleContexts
546547
, FlexibleInstances
548+
, ScopedTypeVariables
547549
, StandaloneDeriving
548550
, TypeApplications
549551
, TypeSynonymInstances
@@ -579,7 +581,6 @@ test-suite hnix-tests
579581
TestCommon
580582
mixins:
581583
base hiding (Prelude)
582-
, relude (Relude as Prelude)
583584
, relude
584585
hs-source-dirs:
585586
tests
@@ -623,6 +624,7 @@ test-suite hnix-tests
623624
, DeriveLift
624625
, FlexibleContexts
625626
, FlexibleInstances
627+
, ScopedTypeVariables
626628
, StandaloneDeriving
627629
, TypeApplications
628630
, TypeSynonymInstances
@@ -650,7 +652,6 @@ benchmark hnix-benchmarks
650652
benchmarks
651653
mixins:
652654
base hiding (Prelude)
653-
, relude (Relude as Prelude)
654655
, relude
655656
ghc-options:
656657
-Wall
@@ -676,6 +677,7 @@ benchmark hnix-benchmarks
676677
, DeriveLift
677678
, FlexibleContexts
678679
, FlexibleInstances
680+
, ScopedTypeVariables
679681
, StandaloneDeriving
680682
, TypeApplications
681683
, TypeSynonymInstances

main/Main.hs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
{-# language MultiWayIf #-}
2-
{-# language ScopedTypeVariables #-}
32
{-# language TypeFamilies #-}
43
{-# language RecordWildCards #-}
54

65
module Main ( main ) where
76

8-
import Nix.Utils
7+
import Relude as Prelude ( force )
98
import Control.Comonad ( extract )
109
import qualified Control.Exception as Exception
1110
import GHC.Err ( errorWithoutStackTrace )
1211
import Control.Monad.Free
1312
import Control.Monad.Ref ( MonadRef(readRef) )
1413
import Control.Monad.Catch
15-
import System.IO ( hPutStrLn
16-
, getContents
17-
)
14+
import System.IO ( hPutStrLn )
1815
import qualified Data.HashMap.Lazy as M
1916
import qualified Data.Map as Map
20-
import Data.Maybe ( fromJust )
21-
import qualified Data.String as String
2217
import Data.Time
2318
import qualified Data.Text.IO as Text
2419
import Text.Show.Pretty ( ppShow )
@@ -56,14 +51,14 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
5651
execContentsFilesOrRepl :: StandardT (StdIdT IO) ()
5752
execContentsFilesOrRepl =
5853
fromMaybe
59-
loadFromCLIFilePathList
54+
loadFromCliFilePathList
6055
( loadBinaryCacheFile <|>
6156
loadLiteralExpression <|>
6257
loadExpressionFromFile
6358
)
6459
where
6560
-- | The base case: read expressions from the last CLI directive (@[FILE]@) listed on the command line.
66-
loadFromCLIFilePathList =
61+
loadFromCliFilePathList =
6762
case filePaths of
6863
[] -> runRepl
6964
["-"] -> readExpressionFromStdin
@@ -83,10 +78,10 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
8378

8479
-- | The `--read` option: load expression from a serialized file.
8580
loadBinaryCacheFile =
86-
(\binaryCacheFile ->
81+
(\ (binaryCacheFile :: Path) ->
8782
do
88-
let file = replaceExtension binaryCacheFile "nixc"
89-
processCLIOptions (Just $ coerce file) =<< liftIO (readCache $ coerce binaryCacheFile)
83+
let file = coerce $ (replaceExtension . coerce) binaryCacheFile "nixc"
84+
processCLIOptions (Just file) =<< liftIO (readCache binaryCacheFile)
9085
) <$> readFrom
9186

9287
-- | The `--expr` option: read expression from the argument string
@@ -97,9 +92,9 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
9792
-- We can start use Text as in the base case, requires changing Path -> Text
9893
-- But that is a gradual process:
9994
-- https://github.com/haskell-nix/hnix/issues/912
100-
(processSeveralFiles . (coerce <$>) . String.lines <=< liftIO) .
95+
(processSeveralFiles . (coerce . toString <$>) . lines <=< liftIO) .
10196
(\case
102-
"-" -> getContents
97+
"-" -> Text.getContents
10398
_fp -> readFile _fp
10499
) <$> fromFile
105100

@@ -126,7 +121,7 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
126121
either
127122
(\ err -> errorWithoutStackTrace $ "Type error: " <> ppShow err)
128123
(\ ty -> liftIO $ putStrLn $ "Type of expression: " <>
129-
ppShow (fromJust $ Map.lookup @VarName @[Scheme] "it" (coerce ty))
124+
ppShow (maybeToMonoid $ Map.lookup @VarName @[Scheme] "it" $ coerce ty)
130125
)
131126
(HM.inferTop mempty [("it", stripAnnotation expr')])
132127

@@ -159,7 +154,7 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
159154
| evaluate =
160155
if
161156
| tracing -> evaluateExprWithEvaluator nixTracingEvalExprLoc expr
162-
| Just path <- reduce -> evaluateExprWithEvaluator (reduction (coerce path) . coerce) expr
157+
| Just path <- reduce -> evaluateExprWithEvaluator (reduction path . coerce) expr
163158
| null arg || null argstr -> evaluateExprWithEvaluator nixEvalExprLoc expr
164159
| otherwise -> processResult printer <=< nixEvalExprLoc (coerce mpath) $ expr
165160
| xml = fail "Rendering expression trees to XML is not yet implemented"
@@ -245,10 +240,10 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
245240
liftIO $ Text.putStrLn path
246241
when descend $
247242
maybe
248-
pass
243+
stub
249244
(\case
250245
NVSet _ s' -> go (path <> ".") s'
251-
_ -> pass
246+
_ -> stub
252247
)
253248
mv
254249
)

0 commit comments

Comments
 (0)