{-# 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.Expr.Types.Annotated
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, a -> Deeper b -> Deeper a
(a -> b) -> Deeper a -> Deeper b
(forall a b. (a -> b) -> Deeper a -> Deeper b)
-> (forall a b. a -> Deeper b -> Deeper a) -> Functor Deeper
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
<$ :: a -> Deeper b -> Deeper a
$c<$ :: forall a b. a -> Deeper b -> Deeper a
fmap :: (a -> b) -> Deeper a -> Deeper b
$cfmap :: forall a b. (a -> b) -> Deeper a -> Deeper b
Functor, Deeper a -> Bool
(a -> m) -> Deeper a -> m
(a -> b -> b) -> b -> Deeper a -> b
(forall m. Monoid m => Deeper m -> m)
-> (forall m a. Monoid m => (a -> m) -> Deeper a -> m)
-> (forall m a. Monoid m => (a -> m) -> Deeper a -> m)
-> (forall a b. (a -> b -> b) -> b -> Deeper a -> b)
-> (forall a b. (a -> b -> b) -> b -> Deeper a -> b)
-> (forall b a. (b -> a -> b) -> b -> Deeper a -> b)
-> (forall b a. (b -> a -> b) -> b -> Deeper a -> b)
-> (forall a. (a -> a -> a) -> Deeper a -> a)
-> (forall a. (a -> a -> a) -> Deeper a -> a)
-> (forall a. Deeper a -> [a])
-> (forall a. Deeper a -> Bool)
-> (forall a. Deeper a -> Int)
-> (forall a. Eq a => a -> Deeper a -> Bool)
-> (forall a. Ord a => Deeper a -> a)
-> (forall a. Ord a => Deeper a -> a)
-> (forall a. Num a => Deeper a -> a)
-> (forall a. Num a => Deeper a -> a)
-> Foldable Deeper
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 :: Deeper a -> a
$cproduct :: forall a. Num a => Deeper a -> a
sum :: Deeper a -> a
$csum :: forall a. Num a => Deeper a -> a
minimum :: Deeper a -> a
$cminimum :: forall a. Ord a => Deeper a -> a
maximum :: Deeper a -> a
$cmaximum :: forall a. Ord a => Deeper a -> a
elem :: a -> Deeper a -> Bool
$celem :: forall a. Eq a => a -> Deeper a -> Bool
length :: Deeper a -> Int
$clength :: forall a. Deeper a -> Int
null :: Deeper a -> Bool
$cnull :: forall a. Deeper a -> Bool
toList :: Deeper a -> [a]
$ctoList :: forall a. Deeper a -> [a]
foldl1 :: (a -> a -> a) -> Deeper a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Deeper a -> a
foldr1 :: (a -> a -> a) -> Deeper a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Deeper a -> a
foldl' :: (b -> a -> b) -> b -> Deeper a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Deeper a -> b
foldl :: (b -> a -> b) -> b -> Deeper a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Deeper a -> b
foldr' :: (a -> b -> b) -> b -> Deeper a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Deeper a -> b
foldr :: (a -> b -> b) -> b -> Deeper a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Deeper a -> b
foldMap' :: (a -> m) -> Deeper a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Deeper a -> m
foldMap :: (a -> m) -> Deeper a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Deeper a -> m
fold :: Deeper m -> m
$cfold :: forall m. Monoid m => Deeper m -> m
Foldable, Functor Deeper
Foldable Deeper
Functor Deeper
-> Foldable Deeper
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Deeper a -> f (Deeper b))
-> (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 (m :: * -> *) a.
    Monad m =>
    Deeper (m a) -> m (Deeper a))
-> Traversable Deeper
(a -> f b) -> Deeper a -> f (Deeper b)
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 :: Deeper (m a) -> m (Deeper a)
$csequence :: forall (m :: * -> *) a. Monad m => Deeper (m a) -> m (Deeper a)
mapM :: (a -> m b) -> Deeper a -> m (Deeper b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Deeper a -> m (Deeper b)
sequenceA :: Deeper (f a) -> f (Deeper a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Deeper (f a) -> f (Deeper a)
traverse :: (a -> f b) -> Deeper a -> f (Deeper b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Deeper a -> f (Deeper b)
$cp2Traversable :: Foldable Deeper
$cp1Traversable :: Functor Deeper
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 :: (a1 -> a2) -> v -> m b
inHask a1 -> a2
f = a2 -> m b
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (a2 -> m b) -> (a1 -> a2) -> a1 -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a1 -> a2
f (a1 -> m b) -> (v -> m a1) -> v -> m b
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< v -> m a1
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 :: (a1 -> m a2) -> v -> m b
inHaskM a1 -> m a2
f = a2 -> m b
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (a2 -> m b) -> (v -> m a2) -> v -> m b
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< a1 -> m a2
f (a1 -> m a2) -> (v -> m a1) -> v -> m a2
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< v -> m a1
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 :: (Maybe a1 -> a2) -> v -> m b
inHaskMay Maybe a1 -> a2
f = a2 -> m b
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (a2 -> m b) -> (Maybe a1 -> a2) -> Maybe a1 -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a1 -> a2
f (Maybe a1 -> m b) -> (v -> m (Maybe a1)) -> v -> m b
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< v -> m (Maybe a1)
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 :: t a -> m (Maybe (t b))
traverseFromValue = (a -> m (Maybe b)) -> t a -> m (Maybe (t b))
forall (m :: * -> *) (n :: * -> *) (t :: * -> *) a b.
(Applicative m, Applicative n, Traversable t) =>
(a -> m (n b)) -> t a -> m (n (t b))
traverse2 a -> m (Maybe b)
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 :: t a -> f (t b)
traverseToValue = (a -> f b) -> t a -> f (t b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
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 :: ValueType -> NValue' t f m (NValue t f m) -> m a
fromMayToValue ValueType
t NValue' t f m (NValue t f m)
v =
  m a -> (a -> m a) -> Maybe a -> m a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
    (ValueFrame t f m -> m a
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m a) -> ValueFrame t f m -> m a
forall a b. (a -> b) -> a -> b
$ ValueType -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m ValueType
t (NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free NValue' t f m (NValue t f m)
v))
    a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    (Maybe a -> m a) -> m (Maybe a) -> m a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< NValue' t f m (NValue t f m) -> m (Maybe a)
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 :: 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 =
  m (m1 a) -> (m1 a -> m (m1 a)) -> Maybe (m1 a) -> m (m1 a)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
    (ValueFrame t f m -> m (m1 a)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m (m1 a)) -> ValueFrame t f m -> m (m1 a)
forall a b. (a -> b) -> a -> b
$ ValueType -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m ValueType
t (NValue t f m -> ValueFrame t f m)
-> NValue t f m -> ValueFrame t f m
forall a b. (a -> b) -> a -> b
$ NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (NValue' t f m (NValue t f m) -> NValue t f m)
-> NValue' t f m (NValue t f m) -> NValue t f m
forall a b. (a -> b) -> a -> b
$ (Deeper (NValue' t f m (NValue t f m))
-> NValue' t f m (NValue t f m)
coerce :: CoerceDeeperToNValue' t f m) Deeper (NValue' t f m (NValue t f m))
v)
    m1 a -> m (m1 a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    (Maybe (m1 a) -> m (m1 a)) -> m (Maybe (m1 a)) -> m (m1 a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Deeper (NValue' t f m (NValue t f m)) -> m (Maybe (m1 a))
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 =
    (t -> m (Maybe a))
-> (NValue' t f m (NValue t f m) -> m (Maybe a))
-> NValue t f m
-> m (Maybe a)
forall a b (f :: * -> *).
(a -> b) -> (f (Free f a) -> b) -> Free f a -> b
free
      (NValue t f m -> m (Maybe a)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay (NValue t f m -> m (Maybe a))
-> (t -> m (NValue t f m)) -> t -> m (Maybe a)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< t -> m (NValue t f m)
forall t (m :: * -> *) a. MonadThunk t m a => t -> m a
force)
      NValue' t f m (NValue t f m) -> m (Maybe a)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay
      (NValue t f m -> m (Maybe a))
-> (NValue t f m -> m (NValue t f m))
-> NValue t f m
-> m (Maybe a)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< NValue t f m -> m (NValue t f m)
forall v (m :: * -> *). MonadValue v m => v -> m v
demand

  fromValue :: NValue t f m -> m a
fromValue =
    (t -> m a)
-> (NValue' t f m (NValue t f m) -> m a) -> NValue t f m -> m a
forall a b (f :: * -> *).
(a -> b) -> (f (Free f a) -> b) -> Free f a -> b
free
      (NValue t f m -> m a
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue t f m -> m a) -> (t -> m (NValue t f m)) -> t -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< t -> m (NValue t f m)
forall t (m :: * -> *) a. MonadThunk t m a => t -> m a
force)
      NValue' t f m (NValue t f m) -> m a
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue
      (NValue t f m -> m a)
-> (NValue t f m -> m (NValue t f m)) -> NValue t f m -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< NValue t f m -> m (NValue t f m)
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) =
    (t -> m (Maybe a))
-> (NValue' t f m (NValue t f m) -> m (Maybe a))
-> NValue t f m
-> m (Maybe a)
forall a b (f :: * -> *).
(a -> b) -> (f (Free f a) -> b) -> Free f a -> b
free
      ((Deeper (NValue t f m) -> m (Maybe a)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay (Deeper (NValue t f m) -> m (Maybe a))
-> (NValue t f m -> Deeper (NValue t f m))
-> NValue t f m
-> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NValue t f m -> Deeper (NValue t f m)
forall a. a -> Deeper a
Deeper) (NValue t f m -> m (Maybe a))
-> (t -> m (NValue t f m)) -> t -> m (Maybe a)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< t -> m (NValue t f m)
forall t (m :: * -> *) a. MonadThunk t m a => t -> m a
force)  -- these places are complex in types
      (Deeper (NValue' t f m (NValue t f m)) -> m (Maybe a)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay (Deeper (NValue' t f m (NValue t f m)) -> m (Maybe a))
-> (NValue' t f m (NValue t f m)
    -> Deeper (NValue' t f m (NValue t f m)))
-> NValue' t f m (NValue t f m)
-> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NValue' t f m (NValue t f m)
-> Deeper (NValue' t f m (NValue t f m))
forall a. a -> Deeper a
Deeper)
      (NValue t f m -> m (Maybe a)) -> m (NValue t f m) -> m (Maybe a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< NValue t f m -> m (NValue t f m)
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) =
    (t -> m a)
-> (NValue' t f m (NValue t f m) -> m a) -> NValue t f m -> m a
forall a b (f :: * -> *).
(a -> b) -> (f (Free f a) -> b) -> Free f a -> b
free
      ((Deeper (NValue t f m) -> m a
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (Deeper (NValue t f m) -> m a)
-> (NValue t f m -> Deeper (NValue t f m)) -> NValue t f m -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NValue t f m -> Deeper (NValue t f m)
forall a. a -> Deeper a
Deeper) (NValue t f m -> m a) -> (t -> m (NValue t f m)) -> t -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< t -> m (NValue t f m)
forall t (m :: * -> *) a. MonadThunk t m a => t -> m a
force)
      (Deeper (NValue' t f m (NValue t f m)) -> m a
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (Deeper (NValue' t f m (NValue t f m)) -> m a)
-> (NValue' t f m (NValue t f m)
    -> Deeper (NValue' t f m (NValue t f m)))
-> NValue' t f m (NValue t f m)
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NValue' t f m (NValue t f m)
-> Deeper (NValue' t f m (NValue t f m))
forall a. a -> Deeper a
Deeper)
      (NValue t f m -> m a) -> m (NValue t f m) -> m a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< NValue t f m -> m (NValue t f m)
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 =
    Maybe () -> m (Maybe ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe () -> m (Maybe ()))
-> (NValue' t f m (NValue t f m) -> Maybe ())
-> NValue' t f m (NValue t f m)
-> m (Maybe ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVConstant' NAtom
NNull -> Maybe ()
forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub
        NValue' t f m (NValue t f m)
_                 -> Maybe ()
forall a. Monoid a => a
mempty

  fromValue :: NValue' t f m (NValue t f m) -> m ()
fromValue = ValueType -> NValue' t f m (NValue t f m) -> m ()
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 =
    Maybe Bool -> m (Maybe Bool)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Bool -> m (Maybe Bool))
-> (NValue' t f m (NValue t f m) -> Maybe Bool)
-> NValue' t f m (NValue t f m)
-> m (Maybe Bool)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVConstant' (NBool Bool
b) -> Bool -> Maybe Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
b
        NValue' t f m (NValue t f m)
_                     -> Maybe Bool
forall a. Maybe a
Nothing

  fromValue :: NValue' t f m (NValue t f m) -> m Bool
fromValue = ValueType -> NValue' t f m (NValue t f m) -> m Bool
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 =
    Maybe Int -> m (Maybe Int)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Int -> m (Maybe Int))
-> (NValue' t f m (NValue t f m) -> Maybe Int)
-> NValue' t f m (NValue t f m)
-> m (Maybe Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVConstant' (NInt Integer
b) -> Int -> Maybe Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
b
        NValue' t f m (NValue t f m)
_                    -> Maybe Int
forall a. Maybe a
Nothing

  fromValue :: NValue' t f m (NValue t f m) -> m Int
fromValue = ValueType -> NValue' t f m (NValue t f m) -> m Int
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 =
    Maybe Integer -> m (Maybe Integer)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Integer -> m (Maybe Integer))
-> (NValue' t f m (NValue t f m) -> Maybe Integer)
-> NValue' t f m (NValue t f m)
-> m (Maybe Integer)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVConstant' (NInt Integer
b) -> Integer -> Maybe Integer
forall (f :: * -> *) a. Applicative f => a -> f a
pure Integer
b
        NValue' t f m (NValue t f m)
_                    -> Maybe Integer
forall a. Maybe a
Nothing

  fromValue :: NValue' t f m (NValue t f m) -> m Integer
fromValue = ValueType -> NValue' t f m (NValue t f m) -> m Integer
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 =
    Maybe Float -> m (Maybe Float)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Float -> m (Maybe Float))
-> (NValue' t f m (NValue t f m) -> Maybe Float)
-> NValue' t f m (NValue t f m)
-> m (Maybe Float)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVConstant' (NFloat Float
b) -> Float -> Maybe Float
forall (f :: * -> *) a. Applicative f => a -> f a
pure Float
b
        NVConstant' (NInt   Integer
i) -> Float -> Maybe Float
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Float -> Maybe Float) -> Float -> Maybe Float
forall a b. (a -> b) -> a -> b
$ Integer -> Float
forall a. Num a => Integer -> a
fromInteger Integer
i
        NValue' t f m (NValue t f m)
_                      -> Maybe Float
forall a. Maybe a
Nothing

  fromValue :: NValue' t f m (NValue t f m) -> m Float
fromValue = ValueType -> NValue' t f m (NValue t f m) -> m Float
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 -> Maybe NixString -> m (Maybe NixString)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe NixString -> m (Maybe NixString))
-> Maybe NixString -> m (Maybe NixString)
forall a b. (a -> b) -> a -> b
$ NixString -> Maybe NixString
forall (f :: * -> *) a. Applicative f => a -> f a
pure NixString
ns
      NVPath' Path
p ->
        (\VarName
path -> NixString -> Maybe NixString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NixString -> Maybe NixString) -> NixString -> Maybe NixString
forall a b. (a -> b) -> a -> b
$ StringContext -> VarName -> NixString
mkNixStringWithSingletonContext (ContextFlavor -> VarName -> StringContext
StringContext ContextFlavor
DirectPath VarName
path) VarName
path) (VarName -> Maybe NixString)
-> (StorePath -> VarName) -> StorePath -> Maybe NixString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> VarName
forall a. IsString a => String -> a
fromString (String -> VarName)
-> (StorePath -> String) -> StorePath -> VarName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StorePath -> String
coerce (StorePath -> Maybe NixString)
-> m StorePath -> m (Maybe NixString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
          Path -> m StorePath
forall e (m :: * -> *).
(Framed e m, MonadStore m) =>
Path -> m StorePath
addPath Path
p
      NVSet' PositionSet
_ AttrSet (NValue t f m)
s ->
        m (Maybe NixString)
-> (NValue t f m -> m (Maybe NixString))
-> Maybe (NValue t f m)
-> m (Maybe NixString)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
          m (Maybe NixString)
forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub
          NValue t f m -> m (Maybe NixString)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay
          (VarName -> AttrSet (NValue t f m) -> Maybe (NValue t f m)
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)
_ -> m (Maybe NixString)
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 = ValueType -> NValue' t f m (NValue t f m) -> m NixString
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 -> NValue' t f m (NValue t f m) -> m NixString)
-> ValueType -> NValue' t f m (NValue t f m) -> m NixString
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 =
    Maybe ByteString -> m (Maybe ByteString)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe ByteString -> m (Maybe ByteString))
-> (NValue' t f m (NValue t f m) -> Maybe ByteString)
-> NValue' t f m (NValue t f m)
-> m (Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVStr' NixString
ns -> Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8 (Text -> ByteString) -> Maybe Text -> Maybe ByteString
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)
_         -> Maybe ByteString
forall a. Monoid a => a
mempty

  fromValue :: NValue' t f m (NValue t f m) -> m ByteString
fromValue = ValueType -> NValue' t f m (NValue t f m) -> m ByteString
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 -> NValue' t f m (NValue t f m) -> m ByteString)
-> ValueType -> NValue' t f m (NValue t f m) -> m ByteString
forall a b. (a -> b) -> a -> b
$ TStringContext -> ValueType
TString TStringContext
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 =
    Maybe Text -> m (Maybe Text)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Text -> m (Maybe Text))
-> (NValue' t f m (NValue t f m) -> Maybe Text)
-> NValue' t f m (NValue t f m)
-> m (Maybe Text)
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)
_         -> Maybe Text
forall a. Monoid a => a
mempty

  fromValue :: NValue' t f m (NValue t f m) -> m Text
fromValue = ValueType -> NValue' t f m (NValue t f m) -> m Text
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 -> NValue' t f m (NValue t f m) -> m Text)
-> ValueType -> NValue' t f m (NValue t f m) -> m Text
forall a b. (a -> b) -> a -> b
$ TStringContext -> ValueType
TString TStringContext
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  -> Maybe Path -> m (Maybe Path)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Path -> m (Maybe Path)) -> Maybe Path -> m (Maybe Path)
forall a b. (a -> b) -> a -> b
$ Path -> Maybe Path
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Path -> Maybe Path) -> Path -> Maybe Path
forall a b. (a -> b) -> a -> b
$ Path -> Path
coerce Path
p
      NVStr'  NixString
ns -> Maybe Path -> m (Maybe Path)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Path -> m (Maybe Path)) -> Maybe Path -> m (Maybe Path)
forall a b. (a -> b) -> a -> b
$ String -> Path
coerce (String -> Path) -> (Text -> String) -> Text -> Path
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
forall a. ToString a => a -> String
toString (Text -> Path) -> Maybe Text -> Maybe Path
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 ->
        m (Maybe Path)
-> (NValue t f m -> m (Maybe Path))
-> Maybe (NValue t f m)
-> m (Maybe Path)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
          m (Maybe Path)
forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub
          (forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
forall (m :: * -> *) v. FromValue Path m v => v -> m (Maybe Path)
fromValueMay @Path)
          (VarName -> AttrSet (NValue t f m) -> Maybe (NValue t f m)
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)
_ -> m (Maybe Path)
forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub

  fromValue :: NValue' t f m (NValue t f m) -> m Path
fromValue = ValueType -> NValue' t f m (NValue t f m) -> m Path
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 =
    Maybe [NValue t f m] -> m (Maybe [NValue t f m])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe [NValue t f m] -> m (Maybe [NValue t f m]))
-> (NValue' t f m (NValue t f m) -> Maybe [NValue t f m])
-> NValue' t f m (NValue t f m)
-> m (Maybe [NValue t f m])
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVList' [NValue t f m]
l -> [NValue t f m] -> Maybe [NValue t f m]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [NValue t f m]
l
        NValue' t f m (NValue t f m)
_         -> Maybe [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 = ValueType -> NValue' t f m (NValue t f m) -> m [NValue t f m]
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) -> [NValue t f m] -> m (Maybe [a])
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))
_                  -> m (Maybe [a])
forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub


  fromValue :: Deeper (NValue' t f m (NValue t f m)) -> m [a]
fromValue = ValueType -> Deeper (NValue' t f m (NValue t f m)) -> m [a]
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 =
    Maybe (AttrSet (NValue t f m))
-> m (Maybe (AttrSet (NValue t f m)))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (AttrSet (NValue t f m))
 -> m (Maybe (AttrSet (NValue t f m))))
-> (NValue' t f m (NValue t f m) -> Maybe (AttrSet (NValue t f m)))
-> NValue' t f m (NValue t f m)
-> m (Maybe (AttrSet (NValue t f m)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVSet' PositionSet
_ AttrSet (NValue t f m)
s -> AttrSet (NValue t f m) -> Maybe (AttrSet (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure AttrSet (NValue t f m)
s
        NValue' t f m (NValue t f m)
_          -> Maybe (AttrSet (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 = ValueType
-> NValue' t f m (NValue t f m) -> m (AttrSet (NValue t f m))
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) -> AttrSet (NValue t f m) -> m (Maybe (AttrSet a))
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))
_                   -> m (Maybe (AttrSet a))
forall (f :: * -> *) a. (Applicative f, Monoid a) => f a
stub

  fromValue :: Deeper (NValue' t f m (NValue t f m)) -> m (AttrSet a)
fromValue = ValueType -> Deeper (NValue' t f m (NValue t f m)) -> m (AttrSet a)
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 =
    Maybe (AttrSet (NValue t f m), PositionSet)
-> m (Maybe (AttrSet (NValue t f m), PositionSet))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (AttrSet (NValue t f m), PositionSet)
 -> m (Maybe (AttrSet (NValue t f m), PositionSet)))
-> (NValue' t f m (NValue t f m)
    -> Maybe (AttrSet (NValue t f m), PositionSet))
-> NValue' t f m (NValue t f m)
-> m (Maybe (AttrSet (NValue t f m), PositionSet))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      \case
        NVSet' PositionSet
p AttrSet (NValue t f m)
s -> (AttrSet (NValue t f m), PositionSet)
-> Maybe (AttrSet (NValue t f m), PositionSet)
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)
_          -> Maybe (AttrSet (NValue t f m), PositionSet)
forall a. Monoid a => a
mempty

  fromValue :: NValue' t f m (NValue t f m)
-> m (AttrSet (NValue t f m), PositionSet)
fromValue = ValueType
-> NValue' t f m (NValue t f m)
-> m (AttrSet (NValue t f m), PositionSet)
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) (AttrSet a -> (AttrSet a, PositionSet))
-> m (Maybe (AttrSet a)) -> m (Maybe (AttrSet a, PositionSet))
forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
<<$>> AttrSet (NValue t f m) -> m (Maybe (AttrSet a))
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))
_                   -> m (Maybe (AttrSet a, PositionSet))
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 = ValueType
-> Deeper (NValue' t f m (NValue t f m))
-> m (AttrSet a, PositionSet)
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 = NValue' t f m (NValue t f m) -> m (Maybe a)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay (NValue' t f m (NValue t f m) -> m (Maybe a))
-> (Deeper (NValue' t f m (NValue t f m))
    -> NValue' t f m (NValue t f m))
-> Deeper (NValue' t f m (NValue t f m))
-> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Deeper (NValue' t f m (NValue t f m))
-> NValue' t f m (NValue t f m)
coerce :: CoerceDeeperToNValue' t f m)
  fromValue :: Deeper (NValue' t f m (NValue t f m)) -> m a
fromValue    = NValue' t f m (NValue t f m) -> m a
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue' t f m (NValue t f m) -> m a)
-> (Deeper (NValue' t f m (NValue t f m))
    -> NValue' t f m (NValue t f m))
-> Deeper (NValue' t f m (NValue t f m))
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Deeper (NValue' t f m (NValue t f m))
-> NValue' t f m (NValue t f m)
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 = NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (NValue' t f m (NValue t f m) -> NValue t f m)
-> m (NValue' t f m (NValue t f m)) -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m (NValue' t f m (NValue t f m))
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 = NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (NValue' t f m (NValue t f m) -> NValue t f m)
-> m (Deeper (NValue' t f m (NValue t f m)))
-> m (Deeper (NValue t f m))
forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
<<$>> a -> m (Deeper (NValue' t f m (NValue t f m)))
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 = m (NValue' t f m (NValue t f m))
-> () -> m (NValue' t f m (NValue t f m))
forall a b. a -> b -> a
const (m (NValue' t f m (NValue t f m))
 -> () -> m (NValue' t f m (NValue t f m)))
-> m (NValue' t f m (NValue t f m))
-> ()
-> m (NValue' t f m (NValue t f m))
forall a b. (a -> b) -> a -> b
$ NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
NValue' t f m r
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 = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> (Bool -> NValue' t f m (NValue t f m))
-> Bool
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NAtom -> NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
NAtom -> NValue' t f m r
mkNVConstant' (NAtom -> NValue' t f m (NValue t f m))
-> (Bool -> NAtom) -> Bool -> NValue' t f m (NValue t f m)
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 = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> (Int -> NValue' t f m (NValue t f m))
-> Int
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NAtom -> NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
NAtom -> NValue' t f m r
mkNVConstant' (NAtom -> NValue' t f m (NValue t f m))
-> (Int -> NAtom) -> Int -> NValue' t f m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> NAtom
NInt (Integer -> NAtom) -> (Int -> Integer) -> Int -> NAtom
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
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 = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> (Integer -> NValue' t f m (NValue t f m))
-> Integer
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NAtom -> NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
NAtom -> NValue' t f m r
mkNVConstant' (NAtom -> NValue' t f m (NValue t f m))
-> (Integer -> NAtom) -> Integer -> NValue' t f m (NValue t f m)
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 = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> (Float -> NValue' t f m (NValue t f m))
-> Float
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NAtom -> NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
NAtom -> NValue' t f m r
mkNVConstant' (NAtom -> NValue' t f m (NValue t f m))
-> (Float -> NAtom) -> Float -> NValue' t f m (NValue t f m)
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 = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> (NixString -> NValue' t f m (NValue t f m))
-> NixString
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixString -> NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
NixString -> NValue' t f m r
mkNVStr'

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 = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> (ByteString -> NValue' t f m (NValue t f m))
-> ByteString
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixString -> NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
NixString -> NValue' t f m r
mkNVStr' (NixString -> NValue' t f m (NValue t f m))
-> (ByteString -> NixString)
-> ByteString
-> NValue' t f m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> NixString
mkNixStringWithoutContext (Text -> NixString)
-> (ByteString -> Text) -> ByteString -> NixString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
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 = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> (Text -> NValue' t f m (NValue t f m))
-> Text
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixString -> NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
NixString -> NValue' t f m r
mkNVStr' (NixString -> NValue' t f m (NValue t f m))
-> (Text -> NixString) -> Text -> NValue' t f m (NValue t f m)
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 = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> (Path -> NValue' t f m (NValue t f m))
-> Path
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path -> NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
Path -> NValue' t f m r
mkNVPath' (Path -> NValue' t f m (NValue t f m))
-> (Path -> Path) -> Path -> NValue' t f m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path -> Path
coerce

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
forall (m :: * -> *) v. ToValue Path m v => Path -> m v
toValue @Path (Path -> m (NValue' t f m (NValue t f m)))
-> (StorePath -> Path)
-> StorePath
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StorePath -> Path
coerce

instance Convertible e t f m
  => ToValue SourcePos m (NValue' t f m (NValue t f m)) where
  toValue :: SourcePos -> m (NValue' t f m (NValue t f m))
toValue (SourcePos String
f Pos
l Pos
c) = do
    NValue t f m
f' <- NixString -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (NixString -> m (NValue t f m)) -> NixString -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ Text -> NixString
mkNixStringWithoutContext (Text -> NixString) -> Text -> NixString
forall a b. (a -> b) -> a -> b
$ String -> Text
forall a. IsString a => String -> a
fromString String
f
    NValue t f m
l' <- Int -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Int -> m (NValue t f m)) -> Int -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ Pos -> Int
unPos Pos
l
    NValue t f m
c' <- Int -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Int -> m (NValue t f m)) -> Int -> m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ Pos -> Int
unPos Pos
c
    let pos :: HashMap VarName (NValue t f m)
pos = [(VarName, NValue t f m)] -> HashMap VarName (NValue t f m)
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')]
    NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall a b. (a -> b) -> a -> b
$ PositionSet
-> HashMap VarName (NValue t f m) -> NValue' t f m (NValue t f m)
forall (f :: * -> *) r t (m :: * -> *).
Applicative f =>
PositionSet -> AttrSet r -> NValue' t f m r
mkNVSet' PositionSet
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 = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> ([NValue t f m] -> NValue' t f m (NValue t f m))
-> [NValue t f m]
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [NValue t f m] -> NValue' t f m (NValue t f m)
forall (f :: * -> *) r t (m :: * -> *).
Applicative f =>
[r] -> NValue' t f m r
mkNVList'

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 = NValue' t f m (NValue t f m)
-> Deeper (NValue' t f m (NValue t f m))
forall a. a -> Deeper a
Deeper (NValue' t f m (NValue t f m)
 -> Deeper (NValue' t f m (NValue t f m)))
-> ([NValue t f m] -> NValue' t f m (NValue t f m))
-> [NValue t f m]
-> Deeper (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [NValue t f m] -> NValue' t f m (NValue t f m)
forall (f :: * -> *) r t (m :: * -> *).
Applicative f =>
[r] -> NValue' t f m r
mkNVList' ([NValue t f m] -> Deeper (NValue' t f m (NValue t f m)))
-> m [NValue t f m] -> m (Deeper (NValue' t f m (NValue t f m)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a] -> m [NValue t f m]
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 = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall a b. (a -> b) -> a -> b
$ PositionSet
-> AttrSet (NValue t f m) -> NValue' t f m (NValue t f m)
forall (f :: * -> *) r t (m :: * -> *).
Applicative f =>
PositionSet -> AttrSet r -> NValue' t f m r
mkNVSet' PositionSet
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 =
    (AttrSet (NValue t f m)
 -> PositionSet -> Deeper (NValue' t f m (NValue t f m)))
-> m (AttrSet (NValue t f m))
-> m PositionSet
-> m (Deeper (NValue' t f m (NValue t f m)))
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 -> NValue' t f m (NValue t f m)
-> Deeper (NValue' t f m (NValue t f m))
forall a. a -> Deeper a
Deeper (NValue' t f m (NValue t f m)
 -> Deeper (NValue' t f m (NValue t f m)))
-> NValue' t f m (NValue t f m)
-> Deeper (NValue' t f m (NValue t f m))
forall a b. (a -> b) -> a -> b
$ PositionSet
-> AttrSet (NValue t f m) -> NValue' t f m (NValue t f m)
forall (f :: * -> *) r t (m :: * -> *).
Applicative f =>
PositionSet -> AttrSet r -> NValue' t f m r
mkNVSet' PositionSet
s AttrSet (NValue t f m)
v)
      (AttrSet a -> m (AttrSet (NValue t f m))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f, ToValue a f b) =>
t a -> f (t b)
traverseToValue AttrSet a
s)
      m PositionSet
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) = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall a b. (a -> b) -> a -> b
$ PositionSet
-> AttrSet (NValue t f m) -> NValue' t f m (NValue t f m)
forall (f :: * -> *) r t (m :: * -> *).
Applicative f =>
PositionSet -> AttrSet r -> NValue' t f m r
mkNVSet' 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) =
    (AttrSet (NValue t f m)
 -> PositionSet -> Deeper (NValue' t f m (NValue t f m)))
-> m (AttrSet (NValue t f m))
-> m PositionSet
-> m (Deeper (NValue' t f m (NValue t f m)))
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 -> NValue' t f m (NValue t f m)
-> Deeper (NValue' t f m (NValue t f m))
forall a. a -> Deeper a
Deeper (NValue' t f m (NValue t f m)
 -> Deeper (NValue' t f m (NValue t f m)))
-> NValue' t f m (NValue t f m)
-> Deeper (NValue' t f m (NValue t f m))
forall a b. (a -> b) -> a -> b
$ PositionSet
-> AttrSet (NValue t f m) -> NValue' t f m (NValue t f m)
forall (f :: * -> *) r t (m :: * -> *).
Applicative f =>
PositionSet -> AttrSet r -> NValue' t f m r
mkNVSet' PositionSet
s AttrSet (NValue t f m)
v)
      (AttrSet a -> m (AttrSet (NValue t f m))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f, ToValue a f b) =>
t a -> f (t b)
traverseToValue AttrSet a
s)
      (PositionSet -> m PositionSet
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 =
        m (Maybe (NValue t f m))
-> m (Maybe (NValue t f m)) -> Bool -> m (Maybe (NValue t f m))
forall a. a -> a -> Bool -> a
bool
          (Maybe (NValue t f m) -> m (Maybe (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (NValue t f m)
forall a. Maybe a
Nothing)
          (NValue t f m -> Maybe (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue t f m -> Maybe (NValue t f m))
-> m (NValue t f m) -> m (Maybe (NValue t f m))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> m (NValue t f m)
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 (Text -> NixString) -> [Text] -> [NixString]
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] <- [NixString] -> m [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
      m (Maybe (NValue t f m))
-> ([NValue t f m] -> m (Maybe (NValue t f m)))
-> [NValue t f m]
-> m (Maybe (NValue t f m))
forall (t :: * -> *) b a. Foldable t => b -> (t a -> b) -> t a -> b
list
        (Maybe (NValue t f m) -> m (Maybe (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (NValue t f m)
forall a. Maybe a
Nothing)
        ((NValue t f m -> Maybe (NValue t f m))
-> m (NValue t f m) -> m (Maybe (NValue t f m))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NValue t f m -> Maybe (NValue t f m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (m (NValue t f m) -> m (Maybe (NValue t f m)))
-> ([NValue t f m] -> m (NValue t f m))
-> [NValue t f m]
-> m (Maybe (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [NValue t f m] -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue)
        [NValue t f m]
ts
    pure $ PositionSet
-> AttrSet (NValue t f m) -> NValue' t f m (NValue t f m)
forall (f :: * -> *) r t (m :: * -> *).
Applicative f =>
PositionSet -> AttrSet r -> NValue' t f m r
mkNVSet' PositionSet
forall a. Monoid a => a
mempty (AttrSet (NValue t f m) -> NValue' t f m (NValue t f m))
-> AttrSet (NValue t f m) -> NValue' t f m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ [(VarName, NValue t f m)] -> AttrSet (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList ([(VarName, NValue t f m)] -> AttrSet (NValue t f m))
-> [(VarName, NValue t f m)] -> AttrSet (NValue t f m)
forall a b. (a -> b) -> a -> b
$ [Maybe (VarName, NValue t f m)] -> [(VarName, NValue t f m)]
forall a. [Maybe a] -> [a]
catMaybes
      [ (VarName
"path"      ,) (NValue t f m -> (VarName, NValue t f m))
-> Maybe (NValue t f m) -> Maybe (VarName, NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (NValue t f m)
path
      , (VarName
"allOutputs",) (NValue t f m -> (VarName, NValue t f m))
-> Maybe (NValue t f m) -> Maybe (VarName, NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (NValue t f m)
allOutputs
      , (VarName
"outputs"   ,) (NValue t f m -> (VarName, NValue t f m))
-> Maybe (NValue t f m) -> Maybe (VarName, NValue t f m)
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 = m (NExprF (NValue t f m)) -> () -> m (NExprF (NValue t f m))
forall a b. a -> b -> a
const (m (NExprF (NValue t f m)) -> () -> m (NExprF (NValue t f m)))
-> (NAtom -> m (NExprF (NValue t f m)))
-> NAtom
-> ()
-> m (NExprF (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NExprF (NValue t f m) -> m (NExprF (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NExprF (NValue t f m) -> m (NExprF (NValue t f m)))
-> (NAtom -> NExprF (NValue t f m))
-> NAtom
-> m (NExprF (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NAtom -> NExprF (NValue t f m)
forall r. NAtom -> NExprF r
NConstant (NAtom -> () -> m (NExprF (NValue t f m)))
-> NAtom -> () -> m (NExprF (NValue t f m))
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 = NExprF (NValue t f m) -> m (NExprF (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NExprF (NValue t f m) -> m (NExprF (NValue t f m)))
-> (Bool -> NExprF (NValue t f m))
-> Bool
-> m (NExprF (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NAtom -> NExprF (NValue t f m)
forall r. NAtom -> NExprF r
NConstant (NAtom -> NExprF (NValue t f m))
-> (Bool -> NAtom) -> Bool -> NExprF (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> NAtom
NBool