{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns #-}

module Nix.Cited.Basic where

import           Control.Comonad                ( Comonad )
import           Control.Comonad.Env            ( ComonadEnv )
import           Control.Monad.Catch     hiding ( catchJust )
import           Control.Monad.Reader
import           Data.Fix
import           GHC.Generics
import           Nix.Cited
import           Nix.Eval                      as Eval
import           Nix.Exec
import           Nix.Expr
import           Nix.Frames
import           Nix.Options
import           Nix.Thunk
import           Nix.Utils
import           Nix.Value

newtype Cited t f m a = Cited { Cited t f m a -> NCited m (NValue t f m) a
getCited :: NCited m (NValue t f m) a }
  deriving
    ( (forall x. Cited t f m a -> Rep (Cited t f m a) x)
-> (forall x. Rep (Cited t f m a) x -> Cited t f m a)
-> Generic (Cited t f m a)
forall x. Rep (Cited t f m a) x -> Cited t f m a
forall x. Cited t f m a -> Rep (Cited t f m a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall t (f :: * -> *) (m :: * -> *) a x.
Rep (Cited t f m a) x -> Cited t f m a
forall t (f :: * -> *) (m :: * -> *) a x.
Cited t f m a -> Rep (Cited t f m a) x
$cto :: forall t (f :: * -> *) (m :: * -> *) a x.
Rep (Cited t f m a) x -> Cited t f m a
$cfrom :: forall t (f :: * -> *) (m :: * -> *) a x.
Cited t f m a -> Rep (Cited t f m a) x
Generic
    , Typeable
    , a -> Cited t f m b -> Cited t f m a
(a -> b) -> Cited t f m a -> Cited t f m b
(forall a b. (a -> b) -> Cited t f m a -> Cited t f m b)
-> (forall a b. a -> Cited t f m b -> Cited t f m a)
-> Functor (Cited t f m)
forall a b. a -> Cited t f m b -> Cited t f m a
forall a b. (a -> b) -> Cited t f m a -> Cited t f m b
forall t (f :: * -> *) (m :: * -> *) a b.
a -> Cited t f m b -> Cited t f m a
forall t (f :: * -> *) (m :: * -> *) a b.
(a -> b) -> Cited t f m a -> Cited t f m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Cited t f m b -> Cited t f m a
$c<$ :: forall t (f :: * -> *) (m :: * -> *) a b.
a -> Cited t f m b -> Cited t f m a
fmap :: (a -> b) -> Cited t f m a -> Cited t f m b
$cfmap :: forall t (f :: * -> *) (m :: * -> *) a b.
(a -> b) -> Cited t f m a -> Cited t f m b
Functor
    , Functor (Cited t f m)
a -> Cited t f m a
Functor (Cited t f m) =>
(forall a. a -> Cited t f m a)
-> (forall a b.
    Cited t f m (a -> b) -> Cited t f m a -> Cited t f m b)
-> (forall a b c.
    (a -> b -> c) -> Cited t f m a -> Cited t f m b -> Cited t f m c)
-> (forall a b. Cited t f m a -> Cited t f m b -> Cited t f m b)
-> (forall a b. Cited t f m a -> Cited t f m b -> Cited t f m a)
-> Applicative (Cited t f m)
Cited t f m a -> Cited t f m b -> Cited t f m b
Cited t f m a -> Cited t f m b -> Cited t f m a
Cited t f m (a -> b) -> Cited t f m a -> Cited t f m b
(a -> b -> c) -> Cited t f m a -> Cited t f m b -> Cited t f m c
forall a. a -> Cited t f m a
forall a b. Cited t f m a -> Cited t f m b -> Cited t f m a
forall a b. Cited t f m a -> Cited t f m b -> Cited t f m b
forall a b. Cited t f m (a -> b) -> Cited t f m a -> Cited t f m b
forall a b c.
(a -> b -> c) -> Cited t f m a -> Cited t f m b -> Cited t f m c
forall t (f :: * -> *) (m :: * -> *). Functor (Cited t f m)
forall t (f :: * -> *) (m :: * -> *) a. a -> Cited t f m a
forall t (f :: * -> *) (m :: * -> *) a b.
Cited t f m a -> Cited t f m b -> Cited t f m a
forall t (f :: * -> *) (m :: * -> *) a b.
Cited t f m a -> Cited t f m b -> Cited t f m b
forall t (f :: * -> *) (m :: * -> *) a b.
Cited t f m (a -> b) -> Cited t f m a -> Cited t f m b
forall t (f :: * -> *) (m :: * -> *) a b c.
(a -> b -> c) -> Cited t f m a -> Cited t f m b -> Cited t f m c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: Cited t f m a -> Cited t f m b -> Cited t f m a
$c<* :: forall t (f :: * -> *) (m :: * -> *) a b.
Cited t f m a -> Cited t f m b -> Cited t f m a
*> :: Cited t f m a -> Cited t f m b -> Cited t f m b
$c*> :: forall t (f :: * -> *) (m :: * -> *) a b.
Cited t f m a -> Cited t f m b -> Cited t f m b
liftA2 :: (a -> b -> c) -> Cited t f m a -> Cited t f m b -> Cited t f m c
$cliftA2 :: forall t (f :: * -> *) (m :: * -> *) a b c.
(a -> b -> c) -> Cited t f m a -> Cited t f m b -> Cited t f m c
<*> :: Cited t f m (a -> b) -> Cited t f m a -> Cited t f m b
$c<*> :: forall t (f :: * -> *) (m :: * -> *) a b.
Cited t f m (a -> b) -> Cited t f m a -> Cited t f m b
pure :: a -> Cited t f m a
$cpure :: forall t (f :: * -> *) (m :: * -> *) a. a -> Cited t f m a
$cp1Applicative :: forall t (f :: * -> *) (m :: * -> *). Functor (Cited t f m)
Applicative
    , a -> Cited t f m a -> Bool
Cited t f m m -> m
Cited t f m a -> [a]
Cited t f m a -> Bool
Cited t f m a -> Int
Cited t f m a -> a
Cited t f m a -> a
Cited t f m a -> a
Cited t f m a -> a
(a -> m) -> Cited t f m a -> m
(a -> m) -> Cited t f m a -> m
(a -> b -> b) -> b -> Cited t f m a -> b
(a -> b -> b) -> b -> Cited t f m a -> b
(b -> a -> b) -> b -> Cited t f m a -> b
(b -> a -> b) -> b -> Cited t f m a -> b
(a -> a -> a) -> Cited t f m a -> a
(a -> a -> a) -> Cited t f m a -> a
(forall m. Monoid m => Cited t f m m -> m)
-> (forall m a. Monoid m => (a -> m) -> Cited t f m a -> m)
-> (forall m a. Monoid m => (a -> m) -> Cited t f m a -> m)
-> (forall a b. (a -> b -> b) -> b -> Cited t f m a -> b)
-> (forall a b. (a -> b -> b) -> b -> Cited t f m a -> b)
-> (forall b a. (b -> a -> b) -> b -> Cited t f m a -> b)
-> (forall b a. (b -> a -> b) -> b -> Cited t f m a -> b)
-> (forall a. (a -> a -> a) -> Cited t f m a -> a)
-> (forall a. (a -> a -> a) -> Cited t f m a -> a)
-> (forall a. Cited t f m a -> [a])
-> (forall a. Cited t f m a -> Bool)
-> (forall a. Cited t f m a -> Int)
-> (forall a. Eq a => a -> Cited t f m a -> Bool)
-> (forall a. Ord a => Cited t f m a -> a)
-> (forall a. Ord a => Cited t f m a -> a)
-> (forall a. Num a => Cited t f m a -> a)
-> (forall a. Num a => Cited t f m a -> a)
-> Foldable (Cited t f m)
forall a. Eq a => a -> Cited t f m a -> Bool
forall a. Num a => Cited t f m a -> a
forall a. Ord a => Cited t f m a -> a
forall m. Monoid m => Cited t f m m -> m
forall a. Cited t f m a -> Bool
forall a. Cited t f m a -> Int
forall a. Cited t f m a -> [a]
forall a. (a -> a -> a) -> Cited t f m a -> a
forall m a. Monoid m => (a -> m) -> Cited t f m a -> m
forall b a. (b -> a -> b) -> b -> Cited t f m a -> b
forall a b. (a -> b -> b) -> b -> Cited t f m a -> b
forall t (f :: * -> *) (m :: * -> *) a.
Eq a =>
a -> Cited t f m a -> Bool
forall t (f :: * -> *) (m :: * -> *) a. Num a => Cited t f m a -> a
forall t (f :: * -> *) (m :: * -> *) a. Ord a => Cited t f m a -> a
forall t (f :: * -> *) (m :: * -> *) m.
Monoid m =>
Cited t f m m -> m
forall t (f :: * -> *) (m :: * -> *) a. Cited t f m a -> Bool
forall t (f :: * -> *) (m :: * -> *) a. Cited t f m a -> Int
forall t (f :: * -> *) (m :: * -> *) a. Cited t f m a -> [a]
forall t (f :: * -> *) (m :: * -> *) a.
(a -> a -> a) -> Cited t f m a -> a
forall t (f :: * -> *) (m :: * -> *) m a.
Monoid m =>
(a -> m) -> Cited t f m a -> m
forall t (f :: * -> *) (m :: * -> *) b a.
(b -> a -> b) -> b -> Cited t f m a -> b
forall t (f :: * -> *) (m :: * -> *) a b.
(a -> b -> b) -> b -> Cited t f m 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 :: Cited t f m a -> a
$cproduct :: forall t (f :: * -> *) (m :: * -> *) a. Num a => Cited t f m a -> a
sum :: Cited t f m a -> a
$csum :: forall t (f :: * -> *) (m :: * -> *) a. Num a => Cited t f m a -> a
minimum :: Cited t f m a -> a
$cminimum :: forall t (f :: * -> *) (m :: * -> *) a. Ord a => Cited t f m a -> a
maximum :: Cited t f m a -> a
$cmaximum :: forall t (f :: * -> *) (m :: * -> *) a. Ord a => Cited t f m a -> a
elem :: a -> Cited t f m a -> Bool
$celem :: forall t (f :: * -> *) (m :: * -> *) a.
Eq a =>
a -> Cited t f m a -> Bool
length :: Cited t f m a -> Int
$clength :: forall t (f :: * -> *) (m :: * -> *) a. Cited t f m a -> Int
null :: Cited t f m a -> Bool
$cnull :: forall t (f :: * -> *) (m :: * -> *) a. Cited t f m a -> Bool
toList :: Cited t f m a -> [a]
$ctoList :: forall t (f :: * -> *) (m :: * -> *) a. Cited t f m a -> [a]
foldl1 :: (a -> a -> a) -> Cited t f m a -> a
$cfoldl1 :: forall t (f :: * -> *) (m :: * -> *) a.
(a -> a -> a) -> Cited t f m a -> a
foldr1 :: (a -> a -> a) -> Cited t f m a -> a
$cfoldr1 :: forall t (f :: * -> *) (m :: * -> *) a.
(a -> a -> a) -> Cited t f m a -> a
foldl' :: (b -> a -> b) -> b -> Cited t f m a -> b
$cfoldl' :: forall t (f :: * -> *) (m :: * -> *) b a.
(b -> a -> b) -> b -> Cited t f m a -> b
foldl :: (b -> a -> b) -> b -> Cited t f m a -> b
$cfoldl :: forall t (f :: * -> *) (m :: * -> *) b a.
(b -> a -> b) -> b -> Cited t f m a -> b
foldr' :: (a -> b -> b) -> b -> Cited t f m a -> b
$cfoldr' :: forall t (f :: * -> *) (m :: * -> *) a b.
(a -> b -> b) -> b -> Cited t f m a -> b
foldr :: (a -> b -> b) -> b -> Cited t f m a -> b
$cfoldr :: forall t (f :: * -> *) (m :: * -> *) a b.
(a -> b -> b) -> b -> Cited t f m a -> b
foldMap' :: (a -> m) -> Cited t f m a -> m
$cfoldMap' :: forall t (f :: * -> *) (m :: * -> *) m a.
Monoid m =>
(a -> m) -> Cited t f m a -> m
foldMap :: (a -> m) -> Cited t f m a -> m
$cfoldMap :: forall t (f :: * -> *) (m :: * -> *) m a.
Monoid m =>
(a -> m) -> Cited t f m a -> m
fold :: Cited t f m m -> m
$cfold :: forall t (f :: * -> *) (m :: * -> *) m.
Monoid m =>
Cited t f m m -> m
Foldable
    , Functor (Cited t f m)
Foldable (Cited t f m)
(Functor (Cited t f m), Foldable (Cited t f m)) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> Cited t f m a -> f (Cited t f m b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Cited t f m (f a) -> f (Cited t f m a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Cited t f m a -> m (Cited t f m b))
-> (forall (m :: * -> *) a.
    Monad m =>
    Cited t f m (m a) -> m (Cited t f m a))
-> Traversable (Cited t f m)
(a -> f b) -> Cited t f m a -> f (Cited t f m b)
forall t (f :: * -> *) (m :: * -> *). Functor (Cited t f m)
forall t (f :: * -> *) (m :: * -> *). Foldable (Cited t f m)
forall t (f :: * -> *) (m :: * -> *) (m :: * -> *) a.
Monad m =>
Cited t f m (m a) -> m (Cited t f m a)
forall t (f :: * -> *) (m :: * -> *) (f :: * -> *) a.
Applicative f =>
Cited t f m (f a) -> f (Cited t f m a)
forall t (f :: * -> *) (m :: * -> *) (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Cited t f m a -> m (Cited t f m b)
forall t (f :: * -> *) (m :: * -> *) (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Cited t f m a -> f (Cited t f m 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 =>
Cited t f m (m a) -> m (Cited t f m a)
forall (f :: * -> *) a.
Applicative f =>
Cited t f m (f a) -> f (Cited t f m a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Cited t f m a -> m (Cited t f m b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Cited t f m a -> f (Cited t f m b)
sequence :: Cited t f m (m a) -> m (Cited t f m a)
$csequence :: forall t (f :: * -> *) (m :: * -> *) (m :: * -> *) a.
Monad m =>
Cited t f m (m a) -> m (Cited t f m a)
mapM :: (a -> m b) -> Cited t f m a -> m (Cited t f m b)
$cmapM :: forall t (f :: * -> *) (m :: * -> *) (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Cited t f m a -> m (Cited t f m b)
sequenceA :: Cited t f m (f a) -> f (Cited t f m a)
$csequenceA :: forall t (f :: * -> *) (m :: * -> *) (f :: * -> *) a.
Applicative f =>
Cited t f m (f a) -> f (Cited t f m a)
traverse :: (a -> f b) -> Cited t f m a -> f (Cited t f m b)
$ctraverse :: forall t (f :: * -> *) (m :: * -> *) (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Cited t f m a -> f (Cited t f m b)
$cp2Traversable :: forall t (f :: * -> *) (m :: * -> *). Foldable (Cited t f m)
$cp1Traversable :: forall t (f :: * -> *) (m :: * -> *). Functor (Cited t f m)
Traversable
    , Functor (Cited t f m)
Functor (Cited t f m) =>
(forall a. Cited t f m a -> a)
-> (forall a. Cited t f m a -> Cited t f m (Cited t f m a))
-> (forall a b.
    (Cited t f m a -> b) -> Cited t f m a -> Cited t f m b)
-> Comonad (Cited t f m)
Cited t f m a -> a
Cited t f m a -> Cited t f m (Cited t f m a)
(Cited t f m a -> b) -> Cited t f m a -> Cited t f m b
forall a. Cited t f m a -> a
forall a. Cited t f m a -> Cited t f m (Cited t f m a)
forall a b. (Cited t f m a -> b) -> Cited t f m a -> Cited t f m b
forall t (f :: * -> *) (m :: * -> *). Functor (Cited t f m)
forall t (f :: * -> *) (m :: * -> *) a. Cited t f m a -> a
forall t (f :: * -> *) (m :: * -> *) a.
Cited t f m a -> Cited t f m (Cited t f m a)
forall t (f :: * -> *) (m :: * -> *) a b.
(Cited t f m a -> b) -> Cited t f m a -> Cited t f m b
forall (w :: * -> *).
Functor w =>
(forall a. w a -> a)
-> (forall a. w a -> w (w a))
-> (forall a b. (w a -> b) -> w a -> w b)
-> Comonad w
extend :: (Cited t f m a -> b) -> Cited t f m a -> Cited t f m b
$cextend :: forall t (f :: * -> *) (m :: * -> *) a b.
(Cited t f m a -> b) -> Cited t f m a -> Cited t f m b
duplicate :: Cited t f m a -> Cited t f m (Cited t f m a)
$cduplicate :: forall t (f :: * -> *) (m :: * -> *) a.
Cited t f m a -> Cited t f m (Cited t f m a)
extract :: Cited t f m a -> a
$cextract :: forall t (f :: * -> *) (m :: * -> *) a. Cited t f m a -> a
$cp1Comonad :: forall t (f :: * -> *) (m :: * -> *). Functor (Cited t f m)
Comonad
    , ComonadEnv [Provenance m (NValue t f m)]
    )

instance HasCitations1 m (NValue t f m) (Cited t f m) where
  citations1 :: Cited t f m a -> [Provenance m (NValue t f m)]
citations1 (Cited c :: NCited m (NValue t f m) a
c) = NCited m (NValue t f m) a -> [Provenance m (NValue t f m)]
forall (m :: * -> *) v a.
HasCitations m v a =>
a -> [Provenance m v]
citations NCited m (NValue t f m) a
c
  addProvenance1 :: Provenance m (NValue t f m) -> Cited t f m a -> Cited t f m a
addProvenance1 x :: Provenance m (NValue t f m)
x (Cited c :: NCited m (NValue t f m) a
c) = NCited m (NValue t f m) a -> Cited t f m a
forall t (f :: * -> *) (m :: * -> *) a.
NCited m (NValue t f m) a -> Cited t f m a
Cited (Provenance m (NValue t f m)
-> NCited m (NValue t f m) a -> NCited m (NValue t f m) a
forall (m :: * -> *) v a.
HasCitations m v a =>
Provenance m v -> a -> a
addProvenance Provenance m (NValue t f m)
x NCited m (NValue t f m) a
c)

instance ( Has e Options
         , Framed e m
         , MonadThunk t m v
         , Typeable m
         , Typeable f
         , Typeable u
         , MonadCatch m
         )
  => MonadThunk (Cited u f m t) m v where
  thunk :: m v -> m (Cited u f m t)
thunk mv :: m v
mv = do
    Options
opts :: Options <- (e -> Options) -> m Options
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (FoldLike Options e e Options Options -> e -> Options
forall a s t b. FoldLike a s t a b -> s -> a
view FoldLike Options e e Options Options
forall a b. Has a b => Lens' a b
hasLens)

    if Options -> Bool
thunks Options
opts
      then do
        Frames
frames :: Frames <- (e -> Frames) -> m Frames
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (FoldLike Frames e e Frames Frames -> e -> Frames
forall a s t b. FoldLike a s t a b -> s -> a
view FoldLike Frames e e Frames Frames
forall a b. Has a b => Lens' a b
hasLens)

        -- Gather the current evaluation context at the time of thunk
        -- creation, and record it along with the thunk.
        let go :: SomeException -> [Provenance m v]
go (SomeException -> Maybe (EvalFrame m v)
forall e. Exception e => SomeException -> Maybe e
fromException ->
                    Just (EvaluatingExpr scope :: Scopes m v
scope
                             (Fix (Compose (Ann s :: SrcSpan
s e :: NExprF (Fix NExprLocF)
e))))) =
                let e' :: Compose (Ann SrcSpan) NExprF (Maybe a)
e' = Ann SrcSpan (NExprF (Maybe a))
-> Compose (Ann SrcSpan) NExprF (Maybe a)
forall k k1 (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (SrcSpan -> NExprF (Maybe a) -> Ann SrcSpan (NExprF (Maybe a))
forall ann a. ann -> a -> Ann ann a
Ann SrcSpan
s (Maybe a
forall a. Maybe a
Nothing Maybe a -> NExprF (Fix NExprLocF) -> NExprF (Maybe a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ NExprF (Fix NExprLocF)
e))
                in [Scopes m v -> NExprLocF (Maybe v) -> Provenance m v
forall (m :: * -> *) v.
Scopes m v -> NExprLocF (Maybe v) -> Provenance m v
Provenance Scopes m v
scope NExprLocF (Maybe v)
forall a. Compose (Ann SrcSpan) NExprF (Maybe a)
e']
            go _ = []
            ps :: [Provenance m (NValue u f m)]
ps = (NixFrame -> [Provenance m (NValue u f m)])
-> Frames -> [Provenance m (NValue u f m)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (SomeException -> [Provenance m (NValue u f m)]
forall (m :: * -> *) v.
(Typeable m, Typeable v) =>
SomeException -> [Provenance m v]
go (SomeException -> [Provenance m (NValue u f m)])
-> (NixFrame -> SomeException)
-> NixFrame
-> [Provenance m (NValue u f m)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixFrame -> SomeException
frame) Frames
frames

        (t -> Cited u f m t) -> m t -> m (Cited u f m t)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (NCited m (NValue u f m) t -> Cited u f m t
forall t (f :: * -> *) (m :: * -> *) a.
NCited m (NValue t f m) a -> Cited t f m a
Cited (NCited m (NValue u f m) t -> Cited u f m t)
-> (t -> NCited m (NValue u f m) t) -> t -> Cited u f m t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Provenance m (NValue u f m)] -> t -> NCited m (NValue u f m) t
forall (m :: * -> *) v a. [Provenance m v] -> a -> NCited m v a
NCited [Provenance m (NValue u f m)]
ps) (m t -> m (Cited u f m t))
-> (m v -> m t) -> m v -> m (Cited u f m t)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m v -> m t
forall t (m :: * -> *) a. MonadThunk t m a => m a -> m t
thunk (m v -> m (Cited u f m t)) -> m v -> m (Cited u f m t)
forall a b. (a -> b) -> a -> b
$ m v
mv
      else (t -> Cited u f m t) -> m t -> m (Cited u f m t)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (NCited m (NValue u f m) t -> Cited u f m t
forall t (f :: * -> *) (m :: * -> *) a.
NCited m (NValue t f m) a -> Cited t f m a
Cited (NCited m (NValue u f m) t -> Cited u f m t)
-> (t -> NCited m (NValue u f m) t) -> t -> Cited u f m t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Provenance m (NValue u f m)] -> t -> NCited m (NValue u f m) t
forall (m :: * -> *) v a. [Provenance m v] -> a -> NCited m v a
NCited []) (m t -> m (Cited u f m t))
-> (m v -> m t) -> m v -> m (Cited u f m t)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m v -> m t
forall t (m :: * -> *) a. MonadThunk t m a => m a -> m t
thunk (m v -> m (Cited u f m t)) -> m v -> m (Cited u f m t)
forall a b. (a -> b) -> a -> b
$ m v
mv

  thunkId :: Cited u f m t -> ThunkId m
thunkId (Cited (NCited _ t :: t
t)) = t -> ThunkId m
forall t (m :: * -> *) a. MonadThunk t m a => t -> ThunkId m
thunkId @_ @m t
t

  queryM :: Cited u f m t -> m r -> (v -> m r) -> m r
queryM (Cited (NCited _ t :: t
t)) = t -> m r -> (v -> m r) -> m r
forall t (m :: * -> *) a r.
MonadThunk t m a =>
t -> m r -> (a -> m r) -> m r
queryM t
t

  -- | The ThunkLoop exception is thrown as an exception with MonadThrow,
  --   which does not capture the current stack frame information to provide
  --   it in a NixException, so we catch and re-throw it here using
  --   'throwError' from Frames.hs.
  force :: Cited u f m t -> (v -> m r) -> m r
force (Cited (NCited ps :: [Provenance m (NValue u f m)]
ps t :: t
t)) f :: v -> m r
f =
    m r -> (ThunkLoop -> m r) -> m r
forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> (e -> m a) -> m a
catch m r
go (forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
forall e (m :: * -> *) a.
(Framed e m, Exception ThunkLoop, MonadThrow m) =>
ThunkLoop -> m a
throwError @ThunkLoop)
   where
    go :: m r
go = case [Provenance m (NValue u f m)]
ps of
      [] -> t -> (v -> m r) -> m r
forall t (m :: * -> *) a r.
MonadThunk t m a =>
t -> (a -> m r) -> m r
force t
t v -> m r
f
      Provenance scope :: Scopes m (NValue u f m)
scope e :: NExprLocF (Maybe (NValue u f m))
e@(Compose (Ann s :: SrcSpan
s _)) : _ ->
        NixLevel -> EvalFrame m (NValue u f m) -> m r -> m r
forall s e (m :: * -> *) a.
(Framed e m, Exception s) =>
NixLevel -> s -> m a -> m a
withFrame NixLevel
Info (Scopes m (NValue u f m)
-> Fix NExprLocF -> EvalFrame m (NValue u f m)
forall (m :: * -> *) v.
Scopes m v -> Fix NExprLocF -> EvalFrame m v
ForcingExpr Scopes m (NValue u f m)
scope (SrcSpan -> NExprLocF (Maybe (NValue u f m)) -> Fix NExprLocF
forall r. SrcSpan -> NExprLocF r -> Fix NExprLocF
wrapExprLoc SrcSpan
s NExprLocF (Maybe (NValue u f m))
e)) (t -> (v -> m r) -> m r
forall t (m :: * -> *) a r.
MonadThunk t m a =>
t -> (a -> m r) -> m r
force t
t v -> m r
f)

  forceEff :: Cited u f m t -> (v -> m r) -> m r
forceEff (Cited (NCited ps :: [Provenance m (NValue u f m)]
ps t :: t
t)) f :: v -> m r
f = m r -> (ThunkLoop -> m r) -> m r
forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> (e -> m a) -> m a
catch
    m r
go
    (forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
forall e (m :: * -> *) a.
(Framed e m, Exception ThunkLoop, MonadThrow m) =>
ThunkLoop -> m a
throwError @ThunkLoop)
   where
    go :: m r
go = case [Provenance m (NValue u f m)]
ps of
      [] -> t -> (v -> m r) -> m r
forall t (m :: * -> *) a r.
MonadThunk t m a =>
t -> (a -> m r) -> m r
forceEff t
t v -> m r
f
      Provenance scope :: Scopes m (NValue u f m)
scope e :: NExprLocF (Maybe (NValue u f m))
e@(Compose (Ann s :: SrcSpan
s _)) : _ ->
        NixLevel -> EvalFrame m (NValue u f m) -> m r -> m r
forall s e (m :: * -> *) a.
(Framed e m, Exception s) =>
NixLevel -> s -> m a -> m a
withFrame NixLevel
Info (Scopes m (NValue u f m)
-> Fix NExprLocF -> EvalFrame m (NValue u f m)
forall (m :: * -> *) v.
Scopes m v -> Fix NExprLocF -> EvalFrame m v
ForcingExpr Scopes m (NValue u f m)
scope (SrcSpan -> NExprLocF (Maybe (NValue u f m)) -> Fix NExprLocF
forall r. SrcSpan -> NExprLocF r -> Fix NExprLocF
wrapExprLoc SrcSpan
s NExprLocF (Maybe (NValue u f m))
e)) (t -> (v -> m r) -> m r
forall t (m :: * -> *) a r.
MonadThunk t m a =>
t -> (a -> m r) -> m r
forceEff t
t v -> m r
f)

  further :: Cited u f m t -> (m v -> m v) -> m (Cited u f m t)
further (Cited (NCited ps :: [Provenance m (NValue u f m)]
ps t :: t
t)) f :: m v -> m v
f = NCited m (NValue u f m) t -> Cited u f m t
forall t (f :: * -> *) (m :: * -> *) a.
NCited m (NValue t f m) a -> Cited t f m a
Cited (NCited m (NValue u f m) t -> Cited u f m t)
-> (t -> NCited m (NValue u f m) t) -> t -> Cited u f m t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Provenance m (NValue u f m)] -> t -> NCited m (NValue u f m) t
forall (m :: * -> *) v a. [Provenance m v] -> a -> NCited m v a
NCited [Provenance m (NValue u f m)]
ps (t -> Cited u f m t) -> m t -> m (Cited u f m t)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t -> (m v -> m v) -> m t
forall t (m :: * -> *) a.
MonadThunk t m a =>
t -> (m a -> m a) -> m t
further t
t m v -> m v
f