module Agda.TypeChecking.Monad.Env where


import qualified Data.List as List

import Data.Maybe (fromMaybe)


import Agda.Syntax.Common
import Agda.Syntax.Abstract.Name
import Agda.Syntax.TopLevelModuleName

import Agda.TypeChecking.Monad.Base

import Agda.Utils.FileName
import qualified Agda.Utils.SmallSet as SmallSet

import Agda.Utils.Impossible

-- | Get the name of the current module, if any.
{-# SPECIALIZE currentModule :: TCM ModuleName #-}
{-# SPECIALIZE currentModule :: ReduceM ModuleName #-}
currentModule :: MonadTCEnv m => m ModuleName
currentModule :: forall (m :: * -> *). MonadTCEnv m => m ModuleName
currentModule = forall (m :: * -> *) a. MonadTCEnv m => (TCEnv -> a) -> m a
asksTC TCEnv -> ModuleName
envCurrentModule

-- | Set the name of the current module.
withCurrentModule :: (MonadTCEnv m) => ModuleName -> m a -> m a
withCurrentModule :: forall (m :: * -> *) a. MonadTCEnv m => ModuleName -> m a -> m a
withCurrentModule ModuleName
m =
    forall (m :: * -> *) a.
MonadTCEnv m =>
(TCEnv -> TCEnv) -> m a -> m a
localTC forall a b. (a -> b) -> a -> b
$ \ TCEnv
e -> TCEnv
e { envCurrentModule :: ModuleName
envCurrentModule = ModuleName
m }

-- | Get the path of the currently checked file
getCurrentPath :: MonadTCEnv m => m AbsolutePath
getCurrentPath :: forall (m :: * -> *). MonadTCEnv m => m AbsolutePath
getCurrentPath = forall a. a -> Maybe a -> a
fromMaybe forall a. HasCallStack => a
__IMPOSSIBLE__ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a. MonadTCEnv m => (TCEnv -> a) -> m a
asksTC TCEnv -> Maybe AbsolutePath
envCurrentPath

-- | Get the number of variables bound by anonymous modules.
{-# SPECIALIZE getAnonymousVariables :: ModuleName -> TCM Nat #-}
{-# SPECIALIZE getAnonymousVariables :: ModuleName -> ReduceM Nat #-}
getAnonymousVariables :: MonadTCEnv m => ModuleName -> m Nat
getAnonymousVariables :: forall (m :: * -> *). MonadTCEnv m => ModuleName -> m Nat
getAnonymousVariables ModuleName
m = do
  [(ModuleName, Nat)]
ms <- forall (m :: * -> *) a. MonadTCEnv m => (TCEnv -> a) -> m a
asksTC TCEnv -> [(ModuleName, Nat)]
envAnonymousModules
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [ Nat
n | (ModuleName
m', Nat
n) <- [(ModuleName, Nat)]
ms, ModuleName -> [Name]
mnameToList ModuleName
m' forall a. Eq a => [a] -> [a] -> Bool
`List.isPrefixOf` ModuleName -> [Name]
mnameToList ModuleName
m ]

-- | Add variables bound by an anonymous module.
withAnonymousModule :: ModuleName -> Nat -> TCM a -> TCM a
withAnonymousModule :: forall a. ModuleName -> Nat -> TCM a -> TCM a
withAnonymousModule ModuleName
m Nat
n =
  forall (m :: * -> *) a.
MonadTCEnv m =>
(TCEnv -> TCEnv) -> m a -> m a
localTC forall a b. (a -> b) -> a -> b
$ \ TCEnv
e -> TCEnv
e { envAnonymousModules :: [(ModuleName, Nat)]
envAnonymousModules = (ModuleName
m, Nat
n) forall a. a -> [a] -> [a]
: TCEnv -> [(ModuleName, Nat)]
envAnonymousModules TCEnv
e }

-- | Set the current environment to the given
withEnv :: MonadTCEnv m => TCEnv -> m a -> m a
withEnv :: forall (m :: * -> *) a. MonadTCEnv m => TCEnv -> m a -> m a
withEnv TCEnv
env = forall (m :: * -> *) a.
MonadTCEnv m =>
(TCEnv -> TCEnv) -> m a -> m a
localTC forall a b. (a -> b) -> a -> b
$ \ TCEnv
env0 -> TCEnv
env
  -- Keep persistent settings
  { envPrintMetasBare :: Bool
envPrintMetasBare         = TCEnv -> Bool
envPrintMetasBare TCEnv
env0
  }

-- | Get the current environment
getEnv :: TCM TCEnv
getEnv :: TCM TCEnv
getEnv = forall (m :: * -> *). MonadTCEnv m => m TCEnv
askTC

-- | Set highlighting level
withHighlightingLevel :: HighlightingLevel -> TCM a -> TCM a
withHighlightingLevel :: forall a. HighlightingLevel -> TCM a -> TCM a
withHighlightingLevel HighlightingLevel
h = forall (m :: * -> *) a.
MonadTCEnv m =>
(TCEnv -> TCEnv) -> m a -> m a
localTC forall a b. (a -> b) -> a -> b
$ \ TCEnv
e -> TCEnv
e { envHighlightingLevel :: HighlightingLevel
envHighlightingLevel = HighlightingLevel
h }

-- | Restore setting for 'ExpandLast' to default.
doExpandLast :: TCM a -> TCM a
doExpandLast :: forall a. TCM a -> TCM a
doExpandLast = forall (m :: * -> *) a.
MonadTCEnv m =>
(TCEnv -> TCEnv) -> m a -> m a
localTC forall a b. (a -> b) -> a -> b
$ \ TCEnv
e -> TCEnv
e { envExpandLast :: ExpandHidden
envExpandLast = ExpandHidden -> ExpandHidden
setExpand (TCEnv -> ExpandHidden
envExpandLast TCEnv
e) }
  where
    setExpand :: ExpandHidden -> ExpandHidden
setExpand ExpandHidden
ReallyDontExpandLast = ExpandHidden
ReallyDontExpandLast
    setExpand ExpandHidden
_                    = ExpandHidden
ExpandLast

dontExpandLast :: TCM a -> TCM a
dontExpandLast :: forall a. TCM a -> TCM a
dontExpandLast = forall (m :: * -> *) a.
MonadTCEnv m =>
(TCEnv -> TCEnv) -> m a -> m a
localTC forall a b. (a -> b) -> a -> b
$ \ TCEnv
e -> TCEnv
e { envExpandLast :: ExpandHidden
envExpandLast = ExpandHidden
DontExpandLast }

reallyDontExpandLast :: TCM a -> TCM a
reallyDontExpandLast :: forall a. TCM a -> TCM a
reallyDontExpandLast = forall (m :: * -> *) a.
MonadTCEnv m =>
(TCEnv -> TCEnv) -> m a -> m a
localTC forall a b. (a -> b) -> a -> b
$ \ TCEnv
e -> TCEnv
e { envExpandLast :: ExpandHidden
envExpandLast = ExpandHidden
ReallyDontExpandLast }

-- | If the reduced did a proper match (constructor or literal pattern),
--   then record this as simplification step.
{-# SPECIALIZE performedSimplification :: TCM a -> TCM a #-}
performedSimplification :: MonadTCEnv m => m a -> m a
performedSimplification :: forall (m :: * -> *) a. MonadTCEnv m => m a -> m a
performedSimplification = forall (m :: * -> *) a.
MonadTCEnv m =>
(TCEnv -> TCEnv) -> m a -> m a
localTC forall a b. (a -> b) -> a -> b
$ \ TCEnv
e -> TCEnv
e { envSimplification :: Simplification
envSimplification = Simplification
YesSimplification }

{-# SPECIALIZE performedSimplification' :: Simplification -> TCM a -> TCM a #-}
performedSimplification' :: MonadTCEnv m => Simplification -> m a -> m a
performedSimplification' :: forall (m :: * -> *) a.
MonadTCEnv m =>
Simplification -> m a -> m a
performedSimplification' Simplification
simpl = forall (m :: * -> *) a.
MonadTCEnv m =>
(TCEnv -> TCEnv) -> m a -> m a
localTC forall a b. (a -> b) -> a -> b
$ \ TCEnv
e -> TCEnv
e { envSimplification :: Simplification
envSimplification = Simplification
simpl forall a. Monoid a => a -> a -> a
`mappend` TCEnv -> Simplification
envSimplification TCEnv
e }

getSimplification :: MonadTCEnv m => m Simplification
getSimplification :: forall (m :: * -> *). MonadTCEnv m => m Simplification
getSimplification = forall (m :: * -> *) a. MonadTCEnv m => (TCEnv -> a) -> m a
asksTC TCEnv -> Simplification
envSimplification

-- * Controlling reduction.

-- | Lens for 'AllowedReductions'.
updateAllowedReductions :: (AllowedReductions -> AllowedReductions) -> TCEnv -> TCEnv
updateAllowedReductions :: (AllowedReductions -> AllowedReductions) -> TCEnv -> TCEnv
updateAllowedReductions AllowedReductions -> AllowedReductions
f TCEnv
e = TCEnv
e { envAllowedReductions :: AllowedReductions
envAllowedReductions = AllowedReductions -> AllowedReductions
f (TCEnv -> AllowedReductions
envAllowedReductions TCEnv
e) }

modifyAllowedReductions :: MonadTCEnv m => (AllowedReductions -> AllowedReductions) -> m a -> m a
modifyAllowedReductions :: forall (m :: * -> *) a.
MonadTCEnv m =>
(AllowedReductions -> AllowedReductions) -> m a -> m a
modifyAllowedReductions = forall (m :: * -> *) a.
MonadTCEnv m =>
(TCEnv -> TCEnv) -> m a -> m a
localTC forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AllowedReductions -> AllowedReductions) -> TCEnv -> TCEnv
updateAllowedReductions

putAllowedReductions :: MonadTCEnv m => AllowedReductions -> m a -> m a
putAllowedReductions :: forall (m :: * -> *) a.
MonadTCEnv m =>
AllowedReductions -> m a -> m a
putAllowedReductions = forall (m :: * -> *) a.
MonadTCEnv m =>
(AllowedReductions -> AllowedReductions) -> m a -> m a
modifyAllowedReductions forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const

-- | Reduce @Def f vs@ only if @f@ is a projection.
onlyReduceProjections :: MonadTCEnv m => m a -> m a
onlyReduceProjections :: forall (m :: * -> *) a. MonadTCEnv m => m a -> m a
onlyReduceProjections = forall (m :: * -> *) a.
MonadTCEnv m =>
AllowedReductions -> m a -> m a
putAllowedReductions forall a b. (a -> b) -> a -> b
$ forall a. SmallSetElement a => a -> SmallSet a
SmallSet.singleton AllowedReduction
ProjectionReductions

-- | Allow all reductions except for non-terminating functions (default).
allowAllReductions :: MonadTCEnv m => m a -> m a
allowAllReductions :: forall (m :: * -> *) a. MonadTCEnv m => m a -> m a
allowAllReductions = forall (m :: * -> *) a.
MonadTCEnv m =>
AllowedReductions -> m a -> m a
putAllowedReductions AllowedReductions
allReductions

-- | Allow all reductions including non-terminating functions.
allowNonTerminatingReductions :: MonadTCEnv m => m a -> m a
allowNonTerminatingReductions :: forall (m :: * -> *) a. MonadTCEnv m => m a -> m a
allowNonTerminatingReductions = forall (m :: * -> *) a.
MonadTCEnv m =>
AllowedReductions -> m a -> m a
putAllowedReductions AllowedReductions
reallyAllReductions

-- | Allow all reductions when reducing types. Otherwise only allow
--   inlined functions to be unfolded.
onlyReduceTypes :: MonadTCEnv m => m a -> m a
onlyReduceTypes :: forall (m :: * -> *) a. MonadTCEnv m => m a -> m a
onlyReduceTypes = forall (m :: * -> *) a.
MonadTCEnv m =>
AllowedReductions -> m a -> m a
putAllowedReductions forall a b. (a -> b) -> a -> b
$ forall a. SmallSetElement a => [a] -> SmallSet a
SmallSet.fromList [AllowedReduction
TypeLevelReductions, AllowedReduction
InlineReductions]

-- | Update allowed reductions when working on types
typeLevelReductions :: MonadTCEnv m => m a -> m a
typeLevelReductions :: forall (m :: * -> *) a. MonadTCEnv m => m a -> m a
typeLevelReductions = forall (m :: * -> *) a.
MonadTCEnv m =>
(AllowedReductions -> AllowedReductions) -> m a -> m a
modifyAllowedReductions forall a b. (a -> b) -> a -> b
$ \AllowedReductions
reds -> if
  | AllowedReduction
TypeLevelReductions forall a. SmallSetElement a => a -> SmallSet a -> Bool
`SmallSet.member` AllowedReductions
reds ->
      if AllowedReduction
NonTerminatingReductions forall a. SmallSetElement a => a -> SmallSet a -> Bool
`SmallSet.member` AllowedReductions
reds
       then AllowedReductions
reallyAllReductions
       else AllowedReductions
allReductions
  | Bool
otherwise -> AllowedReductions
reds

-- * Concerning 'envInsideDotPattern'

insideDotPattern :: TCM a -> TCM a
insideDotPattern :: forall a. TCM a -> TCM a
insideDotPattern = forall (m :: * -> *) a.
MonadTCEnv m =>
(TCEnv -> TCEnv) -> m a -> m a
localTC forall a b. (a -> b) -> a -> b
$ \ TCEnv
e -> TCEnv
e { envInsideDotPattern :: Bool
envInsideDotPattern = Bool
True }

isInsideDotPattern :: TCM Bool
isInsideDotPattern :: TCM Bool
isInsideDotPattern = forall (m :: * -> *) a. MonadTCEnv m => (TCEnv -> a) -> m a
asksTC TCEnv -> Bool
envInsideDotPattern

-- | Don't use call-by-need evaluation for the given computation.
callByName :: TCM a -> TCM a
callByName :: forall a. TCM a -> TCM a
callByName = forall (m :: * -> *) a.
MonadTCEnv m =>
(TCEnv -> TCEnv) -> m a -> m a
localTC forall a b. (a -> b) -> a -> b
$ \ TCEnv
e -> TCEnv
e { envCallByNeed :: Bool
envCallByNeed = Bool
False }