Skip to content

Commit fccb886

Browse files
Merge #805: Children finding their parents
2 parents 5223d51 + 8c6fb78 commit fccb886

File tree

22 files changed

+206
-141
lines changed

22 files changed

+206
-141
lines changed

ChangeLog.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,39 @@
2323
* `principledMakeNixStringWithSingletonContext` -> `makeNixStringWithSingletonContext`.
2424
* `principledModifyNixContents` -> `modifyNixContents`.
2525

26+
* [(link)](https://github.com/haskell-nix/hnix/pull/805/files):
27+
* Data type: `MonadFix1T t m`: `Nix.Standard` -> `Nix.Utils.Fix1`
28+
* Children found their parents:
29+
* `Binary NAtom`: `Nix.Expr.Types` -> `Nix.Atoms`
30+
* `Eq1 (NValue' t f m a)`: `Nix.Value.Equal` -> `Nix.Value` - instance was TH, become regular derivable
31+
* `Eq1 (NValueF p m)`: `Nix.Value.Equal` -> `Nix.Value`
32+
* `FromJSON NAtom`: `Nix.Expr.Types` -> `Nix.Atoms`
33+
* `ToJSON NAtom`: `Nix.Expr.Types` -> `Nix.Atoms`
34+
* `HasCitations m v (NValue t f m)`: `Nix.Pretty` -> `Nix.Cited`
35+
* `HasCitations m v (NValue' t f m a)`: `Nix.Pretty` -> `Nix.Cited`
36+
* `Hashable1 Binding`: `Nix.Expr.Types` -> `Void` - please, report if it is needed
37+
* `Hashable1 NExprF`: `Nix.Expr.Types` -> `Void` - please, report if it is needed
38+
* `Hashable1 NonEmpty`: `Nix.Expr.Types` -> `Void` - please, report if it is needed
39+
* `MonadAtomicRef (Fix1T t m)`: `Nix.Standard` -> `Nix.Utils.Fix1`
40+
* `MonadEnv (Fix1 t)`: `Nix.Standard` -> `Nix.Efffects`
41+
* `MonadEnv (Fix1T t m)`: `Nix.Standard` -> `Nix.Efffects`
42+
* `MonadExec (Fix1 t)`: `Nix.Standard` -> `Nix.Efffects`
43+
* `MonadExec (Fix1T t m)`: `Nix.Standard` -> `Nix.Efffects`
44+
* `MonadFile (Fix1T t m)`: `Nix.Standard` -> `Nix.Render`
45+
* `MonadHttp (Fix1 t)`: `Nix.Standard` -> `Nix.Efffects`
46+
* `MonadHttp (Fix1T t m)`: `Nix.Standard` -> `Nix.Efffects`
47+
* `MonadInstantiate (Fix1 t)`: `Nix.Standard` -> `Nix.Efffects`
48+
* `MonadInstantiate (Fix1T t m)`: `Nix.Standard` -> `Nix.Efffects`
49+
* `MonadIntrospect (Fix1 t)`: `Nix.Standard` -> `Nix.Efffects`
50+
* `MonadIntrospect (Fix1T t m)`: `Nix.Standard` -> `Nix.Efffects`
51+
* `MonadPaths (Fix1 t)`: `Nix.Standard` -> `Nix.Efffects`
52+
* `MonadPaths (Fix1T t m)`: `Nix.Standard` -> `Nix.Efffects`
53+
* `MonadPutStr (Fix1 t)`: `Nix.Standard` -> `Nix.Effects`
54+
* `MonadPutStr (Fix1T t m)`: `Nix.Standard` -> `Nix.Efffects`
55+
* `MonadRef (Fix1T t m)`: `Nix.Standard` -> `Nix.Utils.Fix1`
56+
* `MonadStore (Fix1T t m)`: `Nix.Standard` -> `Nix.Efffects`
57+
58+
2659
* Additional:
2760
* [(link)](https://github.com/haskell-nix/hnix/commit/7e6cd97bf3288cb584241611fdb25bf85d7e0ba7) `cabal.project`: freed from the `cryptohash-sha512` override, Hackage trustees made a revision.
2861
* [(link)](https://github.com/haskell-nix/hnix/pull/824/commits/4422eb10959115f21045f39e302314a77df4b775) To be more approachable for user understanding, the thunk representation in outputs changed from `"<CYCLE>" -> "<expr>"`.

main/Repl.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ main' iniVal = initState iniVal >>= \s -> flip evalStateT s
100100

101101
rcFile = do
102102
f <- liftIO $ Data.Text.IO.readFile ".hnixrc" `catch` handleMissing
103-
forM_ (map (words . Data.Text.unpack) $ Data.Text.lines f) $ \case
103+
forM_ (fmap (words . Data.Text.unpack) $ Data.Text.lines f) $ \case
104104
((prefix:command) : xs) | prefix == commandPrefix -> do
105105
let arguments = unwords xs
106106
optMatcher command options arguments

src/Nix/Atoms.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import Codec.Serialise
1111
#endif
1212
import Control.DeepSeq
1313
import Data.Data
14-
import Data.Fixed (mod')
14+
import Data.Fixed ( mod' )
1515
import Data.Hashable
1616
import Data.Text ( Text
1717
, pack
1818
)
1919
import GHC.Generics
20+
import Data.Binary ( Binary )
21+
import Data.Aeson.Types ( FromJSON, ToJSON )
2022

2123
-- | Atoms are values that evaluate to themselves. This means that
2224
-- they appear in both the parsed AST (in the form of literals) and
@@ -40,6 +42,10 @@ data NAtom
4042
instance Serialise NAtom
4143
#endif
4244

45+
instance Binary NAtom
46+
instance ToJSON NAtom
47+
instance FromJSON NAtom
48+
4349
-- | Translate an atom into its nix representation.
4450
atomText :: NAtom -> Text
4551
atomText (NURI t) = t

src/Nix/Cited.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import Lens.Family2.TH
1818

1919
import Nix.Expr.Types.Annotated
2020
import Nix.Scope
21+
import Nix.Value ( NValue, NValue'(NValue) )
22+
import Control.Monad.Free ( Free(Pure, Free) )
2123

2224
data Provenance m v = Provenance
2325
{ _lexicalScope :: Scopes m v
@@ -60,3 +62,15 @@ instance HasCitations m v (NCited m v a) where
6062
class HasCitations1 m v f where
6163
citations1 :: f a -> [Provenance m v]
6264
addProvenance1 :: Provenance m v -> f a -> f a
65+
66+
instance HasCitations1 m v f
67+
=> HasCitations m v (NValue' t f m a) where
68+
citations (NValue f) = citations1 f
69+
addProvenance x (NValue f) = NValue (addProvenance1 x f)
70+
71+
instance (HasCitations1 m v f, HasCitations m v t)
72+
=> HasCitations m v (NValue t f m) where
73+
citations (Pure t) = citations t
74+
citations (Free v) = citations v
75+
addProvenance x (Pure t) = Pure (addProvenance x t)
76+
addProvenance x (Free v) = Free (addProvenance x v)

src/Nix/Effects.hs

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
{-# LANGUAGE TypeFamilies #-}
1010
{-# LANGUAGE DataKinds #-}
1111
{-# LANGUAGE TypeApplications #-}
12+
{-# LANGUAGE StandaloneDeriving #-}
13+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
14+
{-# LANGUAGE UndecidableInstances #-}
15+
16+
{-# OPTIONS_GHC -Wno-orphans #-}
17+
1218

1319
module Nix.Effects where
1420

@@ -26,6 +32,7 @@ import qualified Data.Text.Encoding as T
2632
import Network.HTTP.Client hiding ( path, Proxy )
2733
import Network.HTTP.Client.TLS
2834
import Network.HTTP.Types
35+
import Nix.Utils.Fix1
2936
import Nix.Expr
3037
import Nix.Frames hiding ( Proxy )
3138
import Nix.Parser
@@ -40,7 +47,7 @@ import qualified System.Info
4047
import System.Process
4148

4249
import qualified System.Nix.Hash as Store
43-
import qualified System.Nix.Store.Remote as Store
50+
import qualified System.Nix.Store.Remote as Store.Remote
4451
import qualified System.Nix.StorePath as Store
4552

4653
-- | A path into the nix store
@@ -70,6 +77,10 @@ class (MonadFile m,
7077

7178
traceEffect :: String -> m ()
7279

80+
instance (MonadFix1T t m, MonadStore m) => MonadStore (Fix1T t m) where
81+
addToStore a b c d = lift $ addToStore a b c d
82+
addTextToStore' a b c d = lift $ addTextToStore' a b c d
83+
7384
class Monad m => MonadIntrospect m where
7485
recursiveSize :: a -> m Word
7586
default recursiveSize :: (MonadTrans t, MonadIntrospect m', m ~ t m') => a -> m Word
@@ -219,11 +230,11 @@ instance MonadHttp IO where
219230

220231

221232
class Monad m => MonadPutStr m where
222-
--TODO: Should this be used *only* when the Nix to be evaluated invokes a
223-
--`trace` operation?
224-
putStr :: String -> m ()
225-
default putStr :: (MonadTrans t, MonadPutStr m', m ~ t m') => String -> m ()
226-
putStr = lift . putStr
233+
--TODO: Should this be used *only* when the Nix to be evaluated invokes a
234+
--`trace` operation?
235+
putStr :: String -> m ()
236+
default putStr :: (MonadTrans t, MonadPutStr m', m ~ t m') => String -> m ()
237+
putStr = lift . putStr
227238

228239
putStrLn :: MonadPutStr m => String -> m ()
229240
putStrLn = putStr . (<> "\n")
@@ -243,20 +254,20 @@ type StorePathSet = HS.HashSet StorePath
243254

244255
class Monad m => MonadStore m where
245256

246-
-- | Copy the contents of a local path to the store. The resulting store
247-
-- path is returned. Note: This does not support yet support the expected
248-
-- `filter` function that allows excluding some files.
249-
addToStore :: StorePathName -> FilePath -> RecursiveFlag -> RepairFlag -> m (Either ErrorCall StorePath)
250-
default addToStore :: (MonadTrans t, MonadStore m', m ~ t m') => StorePathName -> FilePath -> RecursiveFlag -> RepairFlag -> m (Either ErrorCall StorePath)
251-
addToStore a b c d = lift $ addToStore a b c d
257+
-- | Copy the contents of a local path to the store. The resulting store
258+
-- path is returned. Note: This does not support yet support the expected
259+
-- `filter` function that allows excluding some files.
260+
addToStore :: StorePathName -> FilePath -> RecursiveFlag -> RepairFlag -> m (Either ErrorCall StorePath)
261+
default addToStore :: (MonadTrans t, MonadStore m', m ~ t m') => StorePathName -> FilePath -> RecursiveFlag -> RepairFlag -> m (Either ErrorCall StorePath)
262+
addToStore a b c d = lift $ addToStore a b c d
252263

253-
-- | Like addToStore, but the contents written to the output path is a
254-
-- regular file containing the given string.
255-
addTextToStore' :: StorePathName -> Text -> Store.StorePathSet -> RepairFlag -> m (Either ErrorCall StorePath)
256-
default addTextToStore' :: (MonadTrans t, MonadStore m', m ~ t m') => StorePathName -> Text -> Store.StorePathSet -> RepairFlag -> m (Either ErrorCall StorePath)
257-
addTextToStore' a b c d = lift $ addTextToStore' a b c d
264+
-- | Like addToStore, but the contents written to the output path is a
265+
-- regular file containing the given string.
266+
addTextToStore' :: StorePathName -> Text -> Store.StorePathSet -> RepairFlag -> m (Either ErrorCall StorePath)
267+
default addTextToStore' :: (MonadTrans t, MonadStore m', m ~ t m') => StorePathName -> Text -> Store.StorePathSet -> RepairFlag -> m (Either ErrorCall StorePath)
268+
addTextToStore' a b c d = lift $ addTextToStore' a b c d
258269

259-
parseStoreResult :: Monad m => String -> (Either String a, [Store.Logger]) -> m (Either ErrorCall a)
270+
parseStoreResult :: Monad m => String -> (Either String a, [Store.Remote.Logger]) -> m (Either ErrorCall a)
260271
parseStoreResult name res = case res of
261272
(Left msg, logs) -> return $ Left $ ErrorCall $ "Failed to execute '" <> name <> "': " <> msg <> "\n" <> show logs
262273
(Right result, _) -> return $ Right result
@@ -267,13 +278,13 @@ instance MonadStore IO where
267278
Left err -> return $ Left $ ErrorCall $ "String '" <> show name <> "' is not a valid path name: " <> err
268279
Right pathName -> do
269280
-- TODO: redesign the filter parameter
270-
res <- Store.runStore $ Store.addToStore @'Store.SHA256 pathName path recursive (const False) repair
281+
res <- Store.Remote.runStore $ Store.Remote.addToStore @'Store.SHA256 pathName path recursive (const False) repair
271282
parseStoreResult "addToStore" res >>= \case
272283
Left err -> return $ Left err
273284
Right storePath -> return $ Right $ StorePath $ T.unpack $ T.decodeUtf8 $ Store.storePathToRawFilePath storePath
274285

275286
addTextToStore' name text references repair = do
276-
res <- Store.runStore $ Store.addTextToStore name text references repair
287+
res <- Store.Remote.runStore $ Store.Remote.addTextToStore name text references repair
277288
parseStoreResult "addTextToStore" res >>= \case
278289
Left err -> return $ Left err
279290
Right path -> return $ Right $ StorePath $ T.unpack $ T.decodeUtf8 $ Store.storePathToRawFilePath path
@@ -286,3 +297,21 @@ addPath p = either throwError return =<< addToStore (T.pack $ takeFileName p) p
286297

287298
toFile_ :: (Framed e m, MonadStore m) => FilePath -> String -> m StorePath
288299
toFile_ p contents = addTextToStore (T.pack p) (T.pack contents) HS.empty False
300+
301+
-- All of the following type classes defer to the underlying 'm'.
302+
303+
deriving instance MonadPutStr (t (Fix1 t)) => MonadPutStr (Fix1 t)
304+
deriving instance MonadHttp (t (Fix1 t)) => MonadHttp (Fix1 t)
305+
deriving instance MonadEnv (t (Fix1 t)) => MonadEnv (Fix1 t)
306+
deriving instance MonadPaths (t (Fix1 t)) => MonadPaths (Fix1 t)
307+
deriving instance MonadInstantiate (t (Fix1 t)) => MonadInstantiate (Fix1 t)
308+
deriving instance MonadExec (t (Fix1 t)) => MonadExec (Fix1 t)
309+
deriving instance MonadIntrospect (t (Fix1 t)) => MonadIntrospect (Fix1 t)
310+
311+
deriving instance MonadPutStr (t (Fix1T t m) m) => MonadPutStr (Fix1T t m)
312+
deriving instance MonadHttp (t (Fix1T t m) m) => MonadHttp (Fix1T t m)
313+
deriving instance MonadEnv (t (Fix1T t m) m) => MonadEnv (Fix1T t m)
314+
deriving instance MonadPaths (t (Fix1T t m) m) => MonadPaths (Fix1T t m)
315+
deriving instance MonadInstantiate (t (Fix1T t m) m) => MonadInstantiate (Fix1T t m)
316+
deriving instance MonadExec (t (Fix1T t m) m) => MonadExec (Fix1T t m)
317+
deriving instance MonadIntrospect (t (Fix1T t m) m) => MonadIntrospect (Fix1T t m)

src/Nix/Exec.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
{-# OPTIONS_GHC -Wno-orphans #-}
1919
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
2020

21+
2122
module Nix.Exec where
2223

2324
import Prelude hiding ( putStr
@@ -153,6 +154,8 @@ currentPos = asks (view hasLens)
153154
wrapExprLoc :: SrcSpan -> NExprLocF r -> NExprLoc
154155
wrapExprLoc span x = Fix (Fix (NSym_ span "<?>") <$ x)
155156

157+
-- 2021-01-07: NOTE: This instance belongs to be beside MonadEval type class.
158+
-- Currently instance is stuck in orphanage between the requirements to be MonadEval, aka Eval stage, and emposed requirement to be MonadNix (Execution stage). MonadNix constraint tries to put the cart before horse and seems superflous, since Eval in Nix also needs and can throw exceptions. It is between `nverr` and `evalError`.
156159
instance MonadNix e t f m => MonadEval (NValue t f m) m where
157160
freeVariable var =
158161
nverr @e @t @f

src/Nix/Expr/Types.hs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@
2727
module Nix.Expr.Types where
2828

2929
#ifdef MIN_VERSION_serialise
30-
import qualified Codec.Serialise ( Serialise(decode, encode) ) -- For instance implementation function disamburgation
30+
import qualified Codec.Serialise as Serialise
3131
import Codec.Serialise ( Serialise )
3232
#endif
3333
import Control.Applicative
3434
import Control.DeepSeq
3535
import Control.Monad
3636
import Data.Aeson
3737
import Data.Aeson.TH
38+
import qualified Data.Binary as Binary
3839
import Data.Binary ( Binary )
39-
import qualified Data.Binary as Bin
4040
import Data.Data
4141
import Data.Eq.Deriving
4242
import Data.Fix
@@ -65,17 +65,14 @@ import Nix.Utils
6565
import Text.Megaparsec.Pos
6666
import Text.Read.Deriving
6767
import Text.Show.Deriving
68-
import Type.Reflection ( eqTypeRep )
6968
import qualified Type.Reflection as Reflection
69+
import Type.Reflection ( eqTypeRep )
7070

7171
type VarName = Text
7272

7373
hashAt :: VarName -> Lens' (AttrSet v) (Maybe v)
7474
hashAt = flip alterF
7575

76-
-- unfortunate orphans
77-
instance Hashable1 NonEmpty
78-
7976
-- | The main Nix expression type. As it is polimophic, has a functor,
8077
-- which allows to traverse expressions and map functions over them.
8178
-- The actual 'NExpr' type is a fixed point of this functor, defined
@@ -163,8 +160,6 @@ data NExprF r
163160
deriving (Ord, Eq, Generic, Generic1, Typeable, Data, Functor,
164161
Foldable, Traversable, Show, NFData, Hashable)
165162

166-
instance Hashable1 NExprF
167-
168163
instance NFData1 NExprF
169164

170165
#ifdef MIN_VERSION_serialise
@@ -213,8 +208,6 @@ data Binding r
213208
deriving (Generic, Generic1, Typeable, Data, Ord, Eq, Functor,
214209
Foldable, Traversable, Show, NFData, Hashable)
215210

216-
instance Hashable1 Binding
217-
218211
instance NFData1 Binding
219212

220213
#ifdef MIN_VERSION_serialise
@@ -354,16 +347,16 @@ data NKeyName r
354347
instance Serialise r => Serialise (NKeyName r)
355348

356349
instance Serialise Pos where
357-
encode x = Codec.Serialise.encode (unPos x)
358-
decode = mkPos <$> Codec.Serialise.decode
350+
encode = Serialise.encode . unPos
351+
decode = mkPos <$> Serialise.decode
359352

360353
instance Serialise SourcePos where
361-
encode (SourcePos f l c) = Codec.Serialise.encode f <> Codec.Serialise.encode l <> Codec.Serialise.encode c
362-
decode = SourcePos <$> Codec.Serialise.decode <*> Codec.Serialise.decode <*> Codec.Serialise.decode
354+
encode (SourcePos f l c) = Serialise.encode f <> Serialise.encode l <> Serialise.encode c
355+
decode = SourcePos <$> Serialise.decode <*> Serialise.decode <*> Serialise.decode
363356
#endif
364357

365358
instance Hashable Pos where
366-
hashWithSalt salt x = hashWithSalt salt (unPos x)
359+
hashWithSalt salt = hashWithSalt salt . unPos
367360

368361
instance Hashable SourcePos where
369362
hashWithSalt salt (SourcePos f l c) =
@@ -425,7 +418,7 @@ instance Traversable NKeyName where
425418
DynamicKey (Plain str) -> DynamicKey . Plain <$> traverse f str
426419
DynamicKey (Antiquoted e ) -> DynamicKey . Antiquoted <$> f e
427420
DynamicKey EscapedNewline -> pure $ DynamicKey EscapedNewline
428-
StaticKey key -> pure (StaticKey key)
421+
StaticKey key -> pure $ StaticKey key
429422

430423
-- | A selector (for example in a @let@ or an attribute set) is made up
431424
-- of strung-together key names.
@@ -525,12 +518,11 @@ instance (Binary v, Binary a) => Binary (Antiquoted v a)
525518
instance Binary a => Binary (NString a)
526519
instance Binary a => Binary (Binding a)
527520
instance Binary Pos where
528-
put x = Bin.put (unPos x)
529-
get = mkPos <$> Bin.get
521+
put = Binary.put . unPos
522+
get = mkPos <$> Binary.get
530523
instance Binary SourcePos
531524
instance Binary a => Binary (NKeyName a)
532525
instance Binary a => Binary (Params a)
533-
instance Binary NAtom
534526
instance Binary NUnaryOp
535527
instance Binary NBinaryOp
536528
instance Binary NRecordType
@@ -540,11 +532,10 @@ instance (ToJSON v, ToJSON a) => ToJSON (Antiquoted v a)
540532
instance ToJSON a => ToJSON (NString a)
541533
instance ToJSON a => ToJSON (Binding a)
542534
instance ToJSON Pos where
543-
toJSON x = toJSON (unPos x)
535+
toJSON = toJSON . unPos
544536
instance ToJSON SourcePos
545537
instance ToJSON a => ToJSON (NKeyName a)
546538
instance ToJSON a => ToJSON (Params a)
547-
instance ToJSON NAtom
548539
instance ToJSON NUnaryOp
549540
instance ToJSON NBinaryOp
550541
instance ToJSON NRecordType
@@ -558,7 +549,6 @@ instance FromJSON Pos where
558549
instance FromJSON SourcePos
559550
instance FromJSON a => FromJSON (NKeyName a)
560551
instance FromJSON a => FromJSON (Params a)
561-
instance FromJSON NAtom
562552
instance FromJSON NUnaryOp
563553
instance FromJSON NBinaryOp
564554
instance FromJSON NRecordType
@@ -576,8 +566,8 @@ $(makeTraversals ''NBinaryOp)
576566
--x $(makeLenses ''Fix)
577567

578568
class NExprAnn ann g | g -> ann where
579-
fromNExpr :: g r -> (NExprF r, ann)
580-
toNExpr :: (NExprF r, ann) -> g r
569+
fromNExpr :: g r -> (NExprF r, ann)
570+
toNExpr :: (NExprF r, ann) -> g r
581571

582572
ekey
583573
:: NExprAnn ann g
@@ -609,7 +599,7 @@ ekey _ _ f e = fromMaybe e <$> f Nothing
609599
stripPositionInfo :: NExpr -> NExpr
610600
stripPositionInfo = transport phi
611601
where
612-
phi (NSet recur binds) = NSet recur (fmap go binds)
602+
phi (NSet recur binds) = NSet recur $ fmap go binds
613603
phi (NLet binds body) = NLet (fmap go binds) body
614604
phi x = x
615605

0 commit comments

Comments
 (0)