{-# language AllowAmbiguousTypes #-}
{-# language ConstraintKinds #-}
{-# language IncoherentInstances #-}
{-# language TypeFamilies #-}
{-# language UndecidableInstances #-}

{-# options_ghc -fno-warn-name-shadowing #-}

-- | Although there are a lot of instances in this file, really it's just a
--   combinatorial explosion of the following combinations:
--
--   - Several Haskell types being converted to/from Nix wrappers
--   - Several types of Nix wrappers
--   - Whether to be shallow or deep while unwrapping

module Nix.Convert where

import           Nix.Prelude
import           Control.Monad.Free
import qualified Data.HashMap.Lazy             as M
import           Nix.Atoms
import           Nix.Effects
import           Nix.Expr.Types
import           Nix.Frames
import           Nix.String
import           Nix.Value
import           Nix.Value.Monad
import           Nix.Thunk                      ( MonadThunk(force) )

newtype Deeper a = Deeper a
  deriving (Typeable, forall a b. a -> Deeper b -> Deeper a
forall a b. (a -> b) -> Deeper a -> Deeper b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Deeper b -> Deeper a
$c<$ :: forall a b. a -> Deeper b -> Deeper a
fmap :: forall a b. (a -> b) -> Deeper a -> Deeper b
$cfmap :: forall a b. (a -> b) -> Deeper a -> Deeper b
Functor, forall a. Eq a => a -> Deeper a -> Bool
forall a. Num a => Deeper a -> a
forall a. Ord a => Deeper a -> a
forall m. Monoid m => Deeper m -> m
forall a. Deeper a -> Bool
forall a. Deeper a -> Int
forall a. Deeper a -> [a]
forall a. (a -> a -> a) -> Deeper a -> a
forall m a. Monoid m => (a -> m) -> Deeper a -> m
forall b a. (b -> a -> b) -> b -> Deeper a -> b
forall a b. (a -> b -> b) -> b -> Deeper a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => Deeper a -> a
$cproduct :: forall a. Num a => Deeper a -> a
sum :: forall a. Num a => Deeper a -> a
$csum :: forall a. Num a => Deeper a -> a
minimum :: forall a. Ord a => Deeper a -> a
$cminimum :: forall a. Ord a => Deeper a -> a
maximum :: forall a. Ord a => Deeper a -> a
$cmaximum :: forall a. Ord a => Deeper a -> a
elem :: forall a. Eq a => a -> Deeper a -> Bool
$celem :: forall a. Eq a => a -> Deeper a -> Bool
length :: forall a. Deeper a -> Int
$clength :: forall a. Deeper a -> Int
null :: forall a. Deeper a -> Bool
$cnull :: forall a. Deeper a -> Bool
toList :: forall a. Deeper a -> [a]
$ctoList :: forall a. Deeper a -> [a]
foldl1 :: forall a. (a -> a -> a) -> Deeper a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Deeper a -> a
foldr1 :: forall a. (a -> a -> a) -> Deeper a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Deeper a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Deeper a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Deeper a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Deeper a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Deeper a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Deeper a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Deeper a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Deeper a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Deeper a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> Deeper a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Deeper a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Deeper a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Deeper a -> m
fold :: forall m. Monoid m => Deeper m -> m
$cfold :: forall m. Monoid m => Deeper m -> m
Foldable, Functor Deeper
Foldable Deeper
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Deeper (m a) -> m (Deeper a)
forall (f :: * -> *) a.
Applicative f =>
Deeper (f a) -> f (Deeper a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Deeper a -> m (Deeper b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Deeper a -> f (Deeper b)
sequence :: forall (m :: * -> *) a. Monad m => Deeper (m a) -> m (Deeper a)
$csequence :: forall (m :: * -> *) a. Monad m => Deeper (m a) -> m (Deeper a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Deeper a -> m (Deeper b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Deeper a -> m (Deeper b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Deeper (f a) -> f (Deeper a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Deeper (f a) -> f (Deeper a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Deeper a -> f (Deeper b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Deeper a -> f (Deeper b)
Traversable)

type CoerceDeeperToNValue t f m = Deeper (NValue t f m) -> NValue t f m
type CoerceDeeperToNValue' t f m = Deeper (NValue' t f m (NValue t f m)) -> NValue' t f m (NValue t f m)

{-

IMPORTANT NOTE

We used to have Text instances of FromValue, ToValue, FromNix, and ToNix.
However, we're removing these instances because they are dangerous due to the
fact that they hide the way string contexts are handled. It's better to have to
explicitly handle string context in a way that is appropriate for the situation.

Do not add these instances back!

-}


type Convertible e t f m
  = (Framed e m, MonadDataErrorContext t f m, MonadThunk t m (NValue t f m))

-- | Transform Nix -> Hask. Run function. Convert Hask -> Nix.
inHask :: forall a1 a2 v b m . (Monad m, FromValue a1 m v, ToValue a2 m b) => (a1 -> a2) -> v -> m b
inHask :: forall a1 a2 v b (m :: * -> *).
(Monad m, FromValue a1 m v, ToValue a2 m b) =>
(a1 -> a2) -> v -> m b
inHask a1 -> a2
f = forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. a1 -> a2
f forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue

inHaskM :: forall a1 a2 v b m . (Monad m, FromValue a1 m v, ToValue a2 m b) => (a1 -> m a2) -> v -> m b
inHaskM :: forall a1 a2 v b (m :: * -> *).
(Monad m, FromValue a1 m v, ToValue a2 m b) =>
(a1 -> m a2) -> v -> m b
inHaskM a1 -> m a2
f = forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< a1 -> m a2
f forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue

-- | Maybe transform Nix -> Hask. Run function. Convert Hask -> Nix.
inHaskMay :: forall a1 a2 v b m . (Monad m, FromValue a1 m v, ToValue a2 m b) => (Maybe a1 -> a2) -> v -> m b
inHaskMay :: forall a1 a2 v b (m :: * -> *).
(Monad m, FromValue a1 m v, ToValue a2 m b) =>
(Maybe a1 -> a2) -> v -> m b
inHaskMay Maybe a1 -> a2
f = forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a1 -> a2
f forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay


-- * FromValue

class FromValue a m v where
  fromValue    :: v -> m a
  fromValueMay :: v -> m (Maybe a)

traverseFromValue
  :: ( Applicative m
     , Traversable t
     , FromValue b m a
     )
  => t a
  -> m (Maybe (t b))
traverseFromValue :: forall (m :: * -> *) (t :: * -> *) b a.
(Applicative m, Traversable t, FromValue b m a) =>
t a -> m (Maybe (t b))
traverseFromValue = forall (m :: * -> *) (n :: * -> *) (t :: * -> *) a b.
(Applicative m, Applicative n, Traversable t) =>
(a -> m (n b)) -> t a -> m (n (t b))
traverse2 forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay

traverseToValue
  :: ( Traversable t
     , Applicative f
     , ToValue a f b
     )
  => t a
  -> f (t b)
traverseToValue :: forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f, ToValue a f b) =>
t a -> f (t b)
traverseToValue = forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue

-- Please, hide these helper function from export, to be sure they get optimized away.
fromMayToValue
  :: forall t f m a e
  . ( Convertible e t f m
    , FromValue a m (NValue' t f m (NValue t f m))
    )
  => ValueType
  -> NValue' t f m (NValue t f m)
  -> m a
fromMayToValue :: forall t (f :: * -> *) (m :: * -> *) a e.
(Convertible e t f m,
 FromValue a m (NValue' t f m (NValue t f m))) =>
ValueType -> NValue' t f m (NValue t f m) -> m a
fromMayToValue ValueType
t NValue' t f m (NValue t f m)
v =
  forall b a. b -> (a -> b) -> Maybe a -> b
maybe
    (forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError forall a b. (a -> b) -> a -> b
$ forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m ValueType
t (forall (f :: * -> *) a. f (Free f a) -> Free f a
Free NValue' t f m (NValue t f m)
v))
    forall (f :: * -> *) a. Applicative f => a -> f a
pure
    forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay NValue' t f m (NValue t f m)
v

fromMayToDeeperValue
  :: forall t f m a e m1
  . ( Convertible e t f m
    , FromValue (m1 a) m (Deeper (NValue' t f m (NValue t f m)))
    )
  => ValueType
  -> Deeper (NValue' t f m (NValue t f m))
  -> m (m1 a)
fromMayToDeeperValue :: forall t (f :: * -> *) (m :: * -> *) a e (m1 :: * -> *).
(Convertible e t f m,
 FromValue (m1 a) m (Deeper (NValue' t f m (NValue t f m)))) =>
ValueType -> Deeper (NValue' t f m (NValue t f m)) -> m (m1 a)
fromMayToDeeperValue ValueType
t Deeper (NValue' t f m (NValue t f m))
v =
  forall b a. b -> (a -> b) -> Maybe a -> b
maybe
    (forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError forall a b. (a -> b) -> a -> b
$ forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m ValueType
t forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. f (Free f a) -> Free f a
Free forall a b. (a -> b) -> a -> b
$ (coerce :: forall a b. Coercible a b => a -> b
coerce :: CoerceDeeperToNValue' t f m) Deeper (NValue' t f m (NValue t f m))
v)
    forall (f :: * -> *) a. Applicative f => a -> f a
pure
    forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay Deeper (NValue' t f m (NValue t f m))
v

instance ( Convertible e t f m
         , MonadValue (NValue t f m) m
         , FromValue a m (NValue' t f m (NValue t f m))
         )
  => FromValue a m (NValue t f m) where

  fromValueMay :: NValue t f m -> m (Maybe a)
fromValueMay =
    forall a b (f :: * -> *).
(a -> b) -> (f (Free f a) -> b) -> Free f a -> b
free
      (forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall t (m :: * -> *) a. MonadThunk t m a => t -> m a
force)
      forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay
      forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall v (m :: * -> *). MonadValue v m => v -> m v
demand

  fromValue :: NValue t f m -> m a
fromValue =
    forall a b (f :: * -> *).
(a -> b) -> (f (Free f a) -> b) -> Free f a -> b
free
      (forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall t (m :: * -> *) a. MonadThunk t m a => t -> m a
force)
      forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue
      forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall v (m :: * -> *). MonadValue v m => v -> m v
demand

instance ( Convertible e t f m
         , MonadValue (NValue t f m) m
         , FromValue a m (Deeper (NValue' t f m (NValue t f m)))
         )
  => FromValue a m (Deeper (NValue t f m)) where

  fromValueMay :: Deeper (NValue t f m) -> m (Maybe a)
  fromValueMay :: Deeper (NValue t f m) -> m (Maybe a)
fromValueMay (Deeper NValue t f m
v) =
    forall a b (f :: * -> *).
(a -> b) -> (f (Free f a) -> b) -> Free f a -> b
free
      ((forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Deeper a
Deeper) forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall t (m :: * -> *) a. MonadThunk t m a => t -> m a
force)  -- these places are complex in types
      (forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Deeper a
Deeper)
      forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
v

  fromValue :: Deeper (NValue t f m) -> m a
fromValue (Deeper NValue t f m
v) =
    forall a b (f :: * -> *).
(a -> b) -> (f (Free f a) -> b) -> Free f a -> b
free
      ((forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Deeper a
Deeper) forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall t (m :: * -> *) a. MonadThunk t m a => t -> m a
force)
      (forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Deeper a
Deeper)
      forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall v (m :: * -> *). MonadValue v m => v -> m v
demand NValue t f m
v

instance Convertible e t f m
  => FromValue () m (NValue' t f m (NValue t f m)) where

  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe ())
fromValueMay =
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVConstant' NAtom
NNull -> forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub
        NValue' t f m (NValue t f m)
_                 -> forall a. Monoid a => a
mempty

  fromValue :: NValue' t f m (NValue t f m) -> m ()
fromValue = forall t (f :: * -> *) (m :: * -> *) a e.
(Convertible e t f m,
 FromValue a m (NValue' t f m (NValue t f m))) =>
ValueType -> NValue' t f m (NValue t f m) -> m a
fromMayToValue ValueType
TNull

instance Convertible e t f m
  => FromValue Bool m (NValue' t f m (NValue t f m)) where

  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe Bool)
fromValueMay =
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVConstant' (NBool Bool
b) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
b
        NValue' t f m (NValue t f m)
_                     -> forall a. Maybe a
Nothing

  fromValue :: NValue' t f m (NValue t f m) -> m Bool
fromValue = forall t (f :: * -> *) (m :: * -> *) a e.
(Convertible e t f m,
 FromValue a m (NValue' t f m (NValue t f m))) =>
ValueType -> NValue' t f m (NValue t f m) -> m a
fromMayToValue ValueType
TBool

instance Convertible e t f m
  => FromValue Int m (NValue' t f m (NValue t f m)) where

  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe Int)
fromValueMay =
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVConstant' (NInt Integer
b) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Num a => Integer -> a
fromInteger Integer
b
        NValue' t f m (NValue t f m)
_                    -> forall a. Maybe a
Nothing

  fromValue :: NValue' t f m (NValue t f m) -> m Int
fromValue = forall t (f :: * -> *) (m :: * -> *) a e.
(Convertible e t f m,
 FromValue a m (NValue' t f m (NValue t f m))) =>
ValueType -> NValue' t f m (NValue t f m) -> m a
fromMayToValue ValueType
TInt

instance Convertible e t f m
  => FromValue Integer m (NValue' t f m (NValue t f m)) where

  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe Integer)
fromValueMay =
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVConstant' (NInt Integer
b) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Integer
b
        NValue' t f m (NValue t f m)
_                    -> forall a. Maybe a
Nothing

  fromValue :: NValue' t f m (NValue t f m) -> m Integer
fromValue = forall t (f :: * -> *) (m :: * -> *) a e.
(Convertible e t f m,
 FromValue a m (NValue' t f m (NValue t f m))) =>
ValueType -> NValue' t f m (NValue t f m) -> m a
fromMayToValue ValueType
TInt

instance Convertible e t f m
  => FromValue Float m (NValue' t f m (NValue t f m)) where

  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe Float)
fromValueMay =
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVConstant' (NFloat Float
b) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Float
b
        NVConstant' (NInt   Integer
i) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Num a => Integer -> a
fromInteger Integer
i
        NValue' t f m (NValue t f m)
_                      -> forall a. Maybe a
Nothing

  fromValue :: NValue' t f m (NValue t f m) -> m Float
fromValue = forall t (f :: * -> *) (m :: * -> *) a e.
(Convertible e t f m,
 FromValue a m (NValue' t f m (NValue t f m))) =>
ValueType -> NValue' t f m (NValue t f m) -> m a
fromMayToValue ValueType
TFloat

instance ( Convertible e t f m
         , MonadValue (NValue t f m) m
         , MonadEffects t f m
         )
  => FromValue NixString m (NValue' t f m (NValue t f m)) where

  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe NixString)
fromValueMay =
    \case
      NVStr' NixString
ns -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure NixString
ns
      NVPath' Path
p ->
        (\VarName
path -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ StringContext -> VarName -> NixString
mkNixStringWithSingletonContext (ContextFlavor -> VarName -> StringContext
StringContext ContextFlavor
DirectPath VarName
path) VarName
path) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsString a => String -> a
fromString forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
          forall e (m :: * -> *).
(Framed e m, MonadStore m) =>
Path -> m StorePath
addPath Path
p
      NVSet' PositionSet
_ AttrSet (NValue t f m)
s ->
        forall b a. b -> (a -> b) -> Maybe a -> b
maybe
          forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub
          forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay
          (forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup VarName
"outPath" AttrSet (NValue t f m)
s)
      NValue' t f m (NValue t f m)
_ -> forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub

  --  2021-07-18: NOTE: There may be cases where conversion wrongly marks the content to have a context.
  --  See: https://github.com/haskell-nix/hnix/pull/958#issuecomment-881949183 thread.
  fromValue :: NValue' t f m (NValue t f m) -> m NixString
fromValue = forall t (f :: * -> *) (m :: * -> *) a e.
(Convertible e t f m,
 FromValue a m (NValue' t f m (NValue t f m))) =>
ValueType -> NValue' t f m (NValue t f m) -> m a
fromMayToValue forall a b. (a -> b) -> a -> b
$ TStringContext -> ValueType
TString TStringContext
HasContext

instance Convertible e t f m
  => FromValue ByteString m (NValue' t f m (NValue t f m)) where

  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe ByteString)
fromValueMay =
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVStr' NixString
ns -> forall a b. ConvertUtf8 a b => a -> b
encodeUtf8 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NixString -> Maybe Text
getStringNoContext NixString
ns
        NValue' t f m (NValue t f m)
_         -> forall a. Monoid a => a
mempty

  fromValue :: NValue' t f m (NValue t f m) -> m ByteString
fromValue = forall t (f :: * -> *) (m :: * -> *) a e.
(Convertible e t f m,
 FromValue a m (NValue' t f m (NValue t f m))) =>
ValueType -> NValue' t f m (NValue t f m) -> m a
fromMayToValue forall a b. (a -> b) -> a -> b
$ TStringContext -> ValueType
TString forall a. Monoid a => a
mempty

instance Convertible e t f m
  => FromValue Text m (NValue' t f m (NValue t f m)) where

  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe Text)
fromValueMay =
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVStr' NixString
ns -> NixString -> Maybe Text
getStringNoContext NixString
ns
        NValue' t f m (NValue t f m)
_         -> forall a. Monoid a => a
mempty

  fromValue :: NValue' t f m (NValue t f m) -> m Text
fromValue = forall t (f :: * -> *) (m :: * -> *) a e.
(Convertible e t f m,
 FromValue a m (NValue' t f m (NValue t f m))) =>
ValueType -> NValue' t f m (NValue t f m) -> m a
fromMayToValue forall a b. (a -> b) -> a -> b
$ TStringContext -> ValueType
TString forall a. Monoid a => a
mempty

instance ( Convertible e t f m
         , MonadValue (NValue t f m) m
         )
  => FromValue Path m (NValue' t f m (NValue t f m)) where

  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe Path)
fromValueMay =
    \case
      NVPath' Path
p  -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ coerce :: forall a b. Coercible a b => a -> b
coerce Path
p
      NVStr'  NixString
ns -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ coerce :: forall a b. Coercible a b => a -> b
coerce forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToString a => a -> String
toString forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NixString -> Maybe Text
getStringNoContext  NixString
ns
      NVSet' PositionSet
_ AttrSet (NValue t f m)
s ->
        forall b a. b -> (a -> b) -> Maybe a -> b
maybe
          forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub
          (forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay @Path)
          (forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup VarName
"outPath" AttrSet (NValue t f m)
s)
      NValue' t f m (NValue t f m)
_ -> forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub

  fromValue :: NValue' t f m (NValue t f m) -> m Path
fromValue = forall t (f :: * -> *) (m :: * -> *) a e.
(Convertible e t f m,
 FromValue a m (NValue' t f m (NValue t f m))) =>
ValueType -> NValue' t f m (NValue t f m) -> m a
fromMayToValue ValueType
TPath

instance Convertible e t f m
  => FromValue [NValue t f m] m (NValue' t f m (NValue t f m)) where

  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe [NValue t f m])
fromValueMay =
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVList' [NValue t f m]
l -> forall (f :: * -> *) a. Applicative f => a -> f a
pure [NValue t f m]
l
        NValue' t f m (NValue t f m)
_         -> forall a. Monoid a => a
mempty

  fromValue :: NValue' t f m (NValue t f m) -> m [NValue t f m]
fromValue = forall t (f :: * -> *) (m :: * -> *) a e.
(Convertible e t f m,
 FromValue a m (NValue' t f m (NValue t f m))) =>
ValueType -> NValue' t f m (NValue t f m) -> m a
fromMayToValue ValueType
TList

instance ( Convertible e t f m
         , FromValue a m (NValue t f m)
         )
  => FromValue [a] m (Deeper (NValue' t f m (NValue t f m))) where
  fromValueMay :: Deeper (NValue' t f m (NValue t f m)) -> m (Maybe [a])
fromValueMay =
    \case
      Deeper (NVList' [NValue t f m]
l) -> forall (m :: * -> *) (t :: * -> *) b a.
(Applicative m, Traversable t, FromValue b m a) =>
t a -> m (Maybe (t b))
traverseFromValue [NValue t f m]
l
      Deeper (NValue' t f m (NValue t f m))
_                  -> forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub


  fromValue :: Deeper (NValue' t f m (NValue t f m)) -> m [a]
fromValue = forall t (f :: * -> *) (m :: * -> *) a e (m1 :: * -> *).
(Convertible e t f m,
 FromValue (m1 a) m (Deeper (NValue' t f m (NValue t f m)))) =>
ValueType -> Deeper (NValue' t f m (NValue t f m)) -> m (m1 a)
fromMayToDeeperValue ValueType
TList

instance Convertible e t f m
  => FromValue (AttrSet (NValue t f m)) m (NValue' t f m (NValue t f m)) where

  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe (AttrSet (NValue t f m)))
fromValueMay =
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVSet' PositionSet
_ AttrSet (NValue t f m)
s -> forall (f :: * -> *) a. Applicative f => a -> f a
pure AttrSet (NValue t f m)
s
        NValue' t f m (NValue t f m)
_          -> forall a. Monoid a => a
mempty

  fromValue :: NValue' t f m (NValue t f m) -> m (AttrSet (NValue t f m))
fromValue = forall t (f :: * -> *) (m :: * -> *) a e.
(Convertible e t f m,
 FromValue a m (NValue' t f m (NValue t f m))) =>
ValueType -> NValue' t f m (NValue t f m) -> m a
fromMayToValue ValueType
TSet

instance ( Convertible e t f m
         , FromValue a m (NValue t f m)
         )
  => FromValue (AttrSet a) m (Deeper (NValue' t f m (NValue t f m))) where

  fromValueMay :: Deeper (NValue' t f m (NValue t f m)) -> m (Maybe (AttrSet a))
fromValueMay =
    \case
      Deeper (NVSet' PositionSet
_ AttrSet (NValue t f m)
s) -> forall (m :: * -> *) (t :: * -> *) b a.
(Applicative m, Traversable t, FromValue b m a) =>
t a -> m (Maybe (t b))
traverseFromValue AttrSet (NValue t f m)
s
      Deeper (NValue' t f m (NValue t f m))
_                   -> forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub

  fromValue :: Deeper (NValue' t f m (NValue t f m)) -> m (AttrSet a)
fromValue = forall t (f :: * -> *) (m :: * -> *) a e (m1 :: * -> *).
(Convertible e t f m,
 FromValue (m1 a) m (Deeper (NValue' t f m (NValue t f m)))) =>
ValueType -> Deeper (NValue' t f m (NValue t f m)) -> m (m1 a)
fromMayToDeeperValue ValueType
TSet

instance Convertible e t f m
  => FromValue (AttrSet (NValue t f m), PositionSet) m
              (NValue' t f m (NValue t f m)) where

  fromValueMay :: NValue' t f m (NValue t f m)
-> m (Maybe (AttrSet (NValue t f m), PositionSet))
fromValueMay =
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVSet' PositionSet
p AttrSet (NValue t f m)
s -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (AttrSet (NValue t f m)
s, PositionSet
p)
        NValue' t f m (NValue t f m)
_          -> forall a. Monoid a => a
mempty

  fromValue :: NValue' t f m (NValue t f m)
-> m (AttrSet (NValue t f m), PositionSet)
fromValue = forall t (f :: * -> *) (m :: * -> *) a e.
(Convertible e t f m,
 FromValue a m (NValue' t f m (NValue t f m))) =>
ValueType -> NValue' t f m (NValue t f m) -> m a
fromMayToValue ValueType
TSet

instance ( Convertible e t f m
         , FromValue a m (NValue t f m)
         )
  => FromValue (AttrSet a, PositionSet) m
              (Deeper (NValue' t f m (NValue t f m))) where

  fromValueMay :: Deeper (NValue' t f m (NValue t f m))
-> m (Maybe (AttrSet a, PositionSet))
fromValueMay =
    \case
      Deeper (NVSet' PositionSet
p AttrSet (NValue t f m)
s) -> (, PositionSet
p) forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
<<$>> forall (m :: * -> *) (t :: * -> *) b a.
(Applicative m, Traversable t, FromValue b m a) =>
t a -> m (Maybe (t b))
traverseFromValue AttrSet (NValue t f m)
s
      Deeper (NValue' t f m (NValue t f m))
_                   -> forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub

  fromValue :: Deeper (NValue' t f m (NValue t f m)) -> m (AttrSet a, PositionSet)
fromValue = forall t (f :: * -> *) (m :: * -> *) a e (m1 :: * -> *).
(Convertible e t f m,
 FromValue (m1 a) m (Deeper (NValue' t f m (NValue t f m)))) =>
ValueType -> Deeper (NValue' t f m (NValue t f m)) -> m (m1 a)
fromMayToDeeperValue ValueType
TSet

-- This instance needs IncoherentInstances, and only because of ToBuiltin
instance ( Convertible e t f m
         , FromValue a m (NValue' t f m (NValue t f m))
         )
  => FromValue a m (Deeper (NValue' t f m (NValue t f m))) where
  fromValueMay :: Deeper (NValue' t f m (NValue t f m)) -> m (Maybe a)
fromValueMay = forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay forall b c a. (b -> c) -> (a -> b) -> a -> c
. (coerce :: forall a b. Coercible a b => a -> b
coerce :: CoerceDeeperToNValue' t f m)
  fromValue :: Deeper (NValue' t f m (NValue t f m)) -> m a
fromValue    = forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. (coerce :: forall a b. Coercible a b => a -> b
coerce :: CoerceDeeperToNValue' t f m)


-- * ToValue

class ToValue a m v where
  toValue :: a -> m v

instance (Convertible e t f m
  , ToValue a m (NValue' t f m (NValue t f m))
  )
  => ToValue a m (NValue t f m) where
  toValue :: a -> m (NValue t f m)
toValue a
v = forall (f :: * -> *) a. f (Free f a) -> Free f a
Free forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue a
v

instance ( Convertible e t f m
  , ToValue a m (Deeper (NValue' t f m (NValue t f m)))
  )
  => ToValue a m (Deeper (NValue t f m)) where
  toValue :: a -> m (Deeper (NValue t f m))
toValue a
v = forall (f :: * -> *) a. f (Free f a) -> Free f a
Free forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
<<$>> forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue a
v

instance Convertible e t f m
  => ToValue () m (NValue' t f m (NValue t f m)) where
  toValue :: () -> m (NValue' t f m (NValue t f m))
toValue = forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (w :: * -> *) t (m :: * -> *) a.
NVConstraint w =>
NValue' t w m a
NVNull'

instance Convertible e t f m
  => ToValue Bool m (NValue' t f m (NValue t f m)) where
  toValue :: Bool -> m (NValue' t f m (NValue t f m))
toValue = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (w :: * -> *) t (m :: * -> *) a.
NVConstraint w =>
NAtom -> NValue' t w m a
NVConstant' forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> NAtom
NBool

instance Convertible e t f m
  => ToValue Int m (NValue' t f m (NValue t f m)) where
  toValue :: Int -> m (NValue' t f m (NValue t f m))
toValue = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (w :: * -> *) t (m :: * -> *) a.
NVConstraint w =>
NAtom -> NValue' t w m a
NVConstant' forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> NAtom
NInt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Integral a => a -> Integer
toInteger

instance Convertible e t f m
  => ToValue Integer m (NValue' t f m (NValue t f m)) where
  toValue :: Integer -> m (NValue' t f m (NValue t f m))
toValue = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (w :: * -> *) t (m :: * -> *) a.
NVConstraint w =>
NAtom -> NValue' t w m a
NVConstant' forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> NAtom
NInt

instance Convertible e t f m
  => ToValue Float m (NValue' t f m (NValue t f m)) where
  toValue :: Float -> m (NValue' t f m (NValue t f m))
toValue = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (w :: * -> *) t (m :: * -> *) a.
NVConstraint w =>
NAtom -> NValue' t w m a
NVConstant' forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> NAtom
NFloat

instance Convertible e t f m
  => ToValue NixString m (NValue' t f m (NValue t f m)) where
  toValue :: NixString -> m (NValue' t f m (NValue t f m))
toValue = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (w :: * -> *) t (m :: * -> *) a.
NVConstraint w =>
NixString -> NValue' t w m a
NVStr'

instance Convertible e t f m
  => ToValue ByteString m (NValue' t f m (NValue t f m)) where
  toValue :: ByteString -> m (NValue' t f m (NValue t f m))
toValue = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (w :: * -> *) t (m :: * -> *) a.
NVConstraint w =>
NixString -> NValue' t w m a
NVStr' forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> NixString
mkNixStringWithoutContext forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. ConvertUtf8 a b => b -> a
decodeUtf8

instance Convertible e t f m
  => ToValue Text m (NValue' t f m (NValue t f m)) where
  toValue :: Text -> m (NValue' t f m (NValue t f m))
toValue = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (w :: * -> *) t (m :: * -> *) a.
NVConstraint w =>
NixString -> NValue' t w m a
NVStr' forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> NixString
mkNixStringWithoutContext

instance Convertible e t f m
  => ToValue Path m (NValue' t f m (NValue t f m)) where
  toValue :: Path -> m (NValue' t f m (NValue t f m))
toValue = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (w :: * -> *) t (m :: * -> *) a.
NVConstraint w =>
Path -> NValue' t w m a
NVPath'

instance Convertible e t f m
  => ToValue StorePath m (NValue' t f m (NValue t f m)) where
  toValue :: StorePath -> m (NValue' t f m (NValue t f m))
toValue = forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue @Path forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce

instance Convertible e t f m
  => ToValue NSourcePos m (NValue' t f m (NValue t f m)) where
  toValue :: NSourcePos -> m (NValue' t f m (NValue t f m))
toValue (NSourcePos Path
f NPos
l NPos
c) = do
    NValue t f m
f' <- forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue forall a b. (a -> b) -> a -> b
$ Text -> NixString
mkNixStringWithoutContext forall a b. (a -> b) -> a -> b
$ forall a. IsString a => String -> a
fromString forall a b. (a -> b) -> a -> b
$ coerce :: forall a b. Coercible a b => a -> b
coerce Path
f
    NValue t f m
l' <- forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue forall a b. (a -> b) -> a -> b
$ Pos -> Int
unPos forall a b. (a -> b) -> a -> b
$ coerce :: forall a b. Coercible a b => a -> b
coerce NPos
l
    NValue t f m
c' <- forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue forall a b. (a -> b) -> a -> b
$ Pos -> Int
unPos forall a b. (a -> b) -> a -> b
$ coerce :: forall a b. Coercible a b => a -> b
coerce NPos
c
    let pos :: HashMap VarName (NValue t f m)
pos = forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList [(VarName
"file" :: VarName, NValue t f m
f'), (VarName
"line", NValue t f m
l'), (VarName
"column", NValue t f m
c')]
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall (w :: * -> *) a t (m :: * -> *).
NVConstraint w =>
PositionSet -> AttrSet a -> NValue' t w m a
NVSet' forall a. Monoid a => a
mempty HashMap VarName (NValue t f m)
pos

-- | With 'ToValue', we can always act recursively
instance Convertible e t f m
  => ToValue [NValue t f m] m (NValue' t f m (NValue t f m)) where
  toValue :: [NValue t f m] -> m (NValue' t f m (NValue t f m))
toValue = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (w :: * -> *) a t (m :: * -> *).
NVConstraint w =>
[a] -> NValue' t w m a
NVList'

instance (Convertible e t f m
  , ToValue a m (NValue t f m)
  )
  => ToValue [a] m (Deeper (NValue' t f m (NValue t f m))) where
  toValue :: [a] -> m (Deeper (NValue' t f m (NValue t f m)))
toValue [a]
l = forall a. a -> Deeper a
Deeper forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (w :: * -> *) a t (m :: * -> *).
NVConstraint w =>
[a] -> NValue' t w m a
NVList' forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f, ToValue a f b) =>
t a -> f (t b)
traverseToValue [a]
l

instance Convertible e t f m
  => ToValue (AttrSet (NValue t f m)) m (NValue' t f m (NValue t f m)) where
  toValue :: AttrSet (NValue t f m) -> m (NValue' t f m (NValue t f m))
toValue AttrSet (NValue t f m)
s = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall (w :: * -> *) a t (m :: * -> *).
NVConstraint w =>
PositionSet -> AttrSet a -> NValue' t w m a
NVSet' forall a. Monoid a => a
mempty AttrSet (NValue t f m)
s

instance (Convertible e t f m, ToValue a m (NValue t f m))
  => ToValue (AttrSet a) m (Deeper (NValue' t f m (NValue t f m))) where
  toValue :: AttrSet a -> m (Deeper (NValue' t f m (NValue t f m)))
toValue AttrSet a
s =
    forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (\ AttrSet (NValue t f m)
v PositionSet
s -> forall a. a -> Deeper a
Deeper forall a b. (a -> b) -> a -> b
$ forall (w :: * -> *) a t (m :: * -> *).
NVConstraint w =>
PositionSet -> AttrSet a -> NValue' t w m a
NVSet' PositionSet
s AttrSet (NValue t f m)
v)
      (forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f, ToValue a f b) =>
t a -> f (t b)
traverseToValue AttrSet a
s)
      forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub

instance Convertible e t f m
  => ToValue (AttrSet (NValue t f m), PositionSet) m
            (NValue' t f m (NValue t f m)) where
  toValue :: (AttrSet (NValue t f m), PositionSet)
-> m (NValue' t f m (NValue t f m))
toValue (AttrSet (NValue t f m)
s, PositionSet
p) = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall (w :: * -> *) a t (m :: * -> *).
NVConstraint w =>
PositionSet -> AttrSet a -> NValue' t w m a
NVSet' PositionSet
p AttrSet (NValue t f m)
s

instance (Convertible e t f m, ToValue a m (NValue t f m))
  => ToValue (AttrSet a, PositionSet) m
            (Deeper (NValue' t f m (NValue t f m))) where
  toValue :: (AttrSet a, PositionSet)
-> m (Deeper (NValue' t f m (NValue t f m)))
toValue (AttrSet a
s, PositionSet
p) =
    forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (\ AttrSet (NValue t f m)
v PositionSet
s -> forall a. a -> Deeper a
Deeper forall a b. (a -> b) -> a -> b
$ forall (w :: * -> *) a t (m :: * -> *).
NVConstraint w =>
PositionSet -> AttrSet a -> NValue' t w m a
NVSet' PositionSet
s AttrSet (NValue t f m)
v)
      (forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f, ToValue a f b) =>
t a -> f (t b)
traverseToValue AttrSet a
s)
      (forall (f :: * -> *) a. Applicative f => a -> f a
pure PositionSet
p)

instance Convertible e t f m
  => ToValue NixLikeContextValue m (NValue' t f m (NValue t f m)) where
  toValue :: NixLikeContextValue -> m (NValue' t f m (NValue t f m))
toValue NixLikeContextValue
nlcv = do
    let
      g :: (NixLikeContextValue -> Bool) -> m (Maybe (NValue t f m))
g NixLikeContextValue -> Bool
f =
        forall a. a -> a -> Bool -> a
bool
          (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing)
          (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue Bool
True)
          (NixLikeContextValue -> Bool
f NixLikeContextValue
nlcv)
    Maybe (NValue t f m)
path <- (NixLikeContextValue -> Bool) -> m (Maybe (NValue t f m))
g NixLikeContextValue -> Bool
nlcvPath
    Maybe (NValue t f m)
allOutputs <- (NixLikeContextValue -> Bool) -> m (Maybe (NValue t f m))
g NixLikeContextValue -> Bool
nlcvAllOutputs
    Maybe (NValue t f m)
outputs <- do
      let
        outputs :: [NixString]
outputs = Text -> NixString
mkNixStringWithoutContext forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NixLikeContextValue -> [Text]
nlcvOutputs NixLikeContextValue
nlcv

      [NValue t f m]
ts :: [NValue t f m] <- forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f, ToValue a f b) =>
t a -> f (t b)
traverseToValue [NixString]
outputs
      forall (t :: * -> *) b a. Foldable t => b -> (t a -> b) -> t a -> b
handlePresence
        (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing)
        (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue)
        [NValue t f m]
ts
    pure $ forall (w :: * -> *) a t (m :: * -> *).
NVConstraint w =>
PositionSet -> AttrSet a -> NValue' t w m a
NVSet' forall a. Monoid a => a
mempty forall a b. (a -> b) -> a -> b
$ forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList forall a b. (a -> b) -> a -> b
$ forall a. [Maybe a] -> [a]
catMaybes
      [ (VarName
"path"      ,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (NValue t f m)
path
      , (VarName
"allOutputs",) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (NValue t f m)
allOutputs
      , (VarName
"outputs"   ,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (NValue t f m)
outputs
      ]

instance Convertible e t f m => ToValue () m (NExprF (NValue t f m)) where
  toValue :: () -> m (NExprF (NValue t f m))
toValue = forall a b. a -> b -> a
const forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r. NAtom -> NExprF r
NConstant forall a b. (a -> b) -> a -> b
$ NAtom
NNull

instance Convertible e t f m => ToValue Bool m (NExprF (NValue t f m)) where
  toValue :: Bool -> m (NExprF (NValue t f m))
toValue = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r. NAtom -> NExprF r
NConstant forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> NAtom
NBool