module Agda.TypeChecking.Monad.Options where

import Control.Arrow          ( (&&&) )
import Control.Monad          ( unless, when )
import Control.Monad.IO.Class ( MonadIO(..) )
import Control.Monad.Except
import Control.Monad.Reader
import Control.Monad.State
import Control.Monad.Writer

import qualified Data.Graph as Graph
import Data.List (sort)
import Data.Maybe
import Data.Map (Map)
import qualified Data.Map as Map

import System.Directory
import System.FilePath

import Agda.Syntax.Common
import qualified Agda.Syntax.Concrete as C
import qualified Agda.Syntax.Abstract as A
import Agda.Syntax.TopLevelModuleName
import Agda.TypeChecking.Monad.Debug (reportSDoc)
import Agda.TypeChecking.Warnings
import Agda.TypeChecking.Monad.Base
import Agda.TypeChecking.Monad.Imports
import Agda.TypeChecking.Monad.State
import Agda.TypeChecking.Monad.Benchmark
import Agda.Interaction.FindFile
import Agda.Interaction.Options
import qualified Agda.Interaction.Options.Lenses as Lens
import Agda.Interaction.Library
import Agda.Utils.FileName
import Agda.Utils.Functor
import qualified Agda.Utils.Graph.AdjacencyMap.Unidirectional as G
import Agda.Utils.List
import Agda.Utils.Maybe
import Agda.Utils.Pretty
import Agda.Utils.Tuple
import Agda.Utils.WithDefault

import Agda.Utils.Impossible

-- | Sets the pragma options.

setPragmaOptions :: PragmaOptions -> TCM ()
setPragmaOptions :: PragmaOptions -> TCM ()
setPragmaOptions PragmaOptions
opts = do
  Lens' PragmaOptions TCState
stPragmaOptions forall (m :: * -> *) a.
MonadTCState m =>
Lens' a TCState -> (a -> a) -> m ()
`modifyTCLens` forall a. LensSafeMode a => (SafeMode -> SafeMode) -> a -> a
Lens.mapSafeMode (forall a. LensSafeMode a => a -> SafeMode
Lens.getSafeMode PragmaOptions
opts SafeMode -> SafeMode -> SafeMode
||)
  CommandLineOptions
clo <- forall (m :: * -> *). HasOptions m => m CommandLineOptions
commandLineOptions
  let unsafe :: [FilePath]
unsafe = CommandLineOptions -> PragmaOptions -> [FilePath]
unsafePragmaOptions CommandLineOptions
clo PragmaOptions
opts
  forall (f :: * -> *). Applicative f => SafeMode -> f () -> f ()
when (forall a. LensSafeMode a => a -> SafeMode
Lens.getSafeMode PragmaOptions
opts SafeMode -> SafeMode -> SafeMode
&& SafeMode -> SafeMode
not (forall (t :: * -> *) a. Foldable t => t a -> SafeMode
null [FilePath]
unsafe)) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
(HasCallStack, MonadWarning m) =>
Warning -> m ()
warning forall a b. (a -> b) -> a -> b
$ [FilePath] -> Warning
SafeFlagPragma [FilePath]
unsafe
  forall (m :: * -> *) opts.
Monad m =>
OptM opts -> m (Either FilePath opts)
runOptM (Flag CommandLineOptions
checkOpts CommandLineOptions
clo{ optPragmaOptions :: PragmaOptions
optPragmaOptions = PragmaOptions
opts }) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Left FilePath
err   -> forall a. HasCallStack => a
__IMPOSSIBLE__
    Right CommandLineOptions
opts -> do
      Lens' PragmaOptions TCState
stPragmaOptions forall (m :: * -> *) a.
MonadTCState m =>
Lens' a TCState -> a -> m ()
`setTCLens` CommandLineOptions -> PragmaOptions
optPragmaOptions CommandLineOptions
opts
      TCM ()
updateBenchmarkingStatus

-- | Sets the command line options (both persistent and pragma options
-- are updated).
--
-- Relative include directories are made absolute with respect to the
-- current working directory. If the include directories have changed
-- then the state is reset (partly, see 'setIncludeDirs').
--
-- An empty list of relative include directories (@'Left' []@) is
-- interpreted as @["."]@.
setCommandLineOptions :: CommandLineOptions -> TCM ()
setCommandLineOptions :: CommandLineOptions -> TCM ()
setCommandLineOptions CommandLineOptions
opts = do
  AbsolutePath
root <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (FilePath -> IO AbsolutePath
absolute forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO FilePath
getCurrentDirectory)
  AbsolutePath -> CommandLineOptions -> TCM ()
setCommandLineOptions' AbsolutePath
root CommandLineOptions
opts

setCommandLineOptions'
  :: AbsolutePath
     -- ^ The base directory of relative paths.
  -> CommandLineOptions
  -> TCM ()
setCommandLineOptions' :: AbsolutePath -> CommandLineOptions -> TCM ()
setCommandLineOptions' AbsolutePath
root CommandLineOptions
opts = do
  forall (m :: * -> *) opts.
Monad m =>
OptM opts -> m (Either FilePath opts)
runOptM (Flag CommandLineOptions
checkOpts CommandLineOptions
opts) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Left FilePath
err   -> forall a. HasCallStack => a
__IMPOSSIBLE__
    Right CommandLineOptions
opts -> do
      [AbsolutePath]
incs <- case CommandLineOptions -> [AbsolutePath]
optAbsoluteIncludePaths CommandLineOptions
opts of
        [] -> do
          CommandLineOptions
opts' <- AbsolutePath -> CommandLineOptions -> TCM CommandLineOptions
setLibraryPaths AbsolutePath
root CommandLineOptions
opts
          let incs :: [FilePath]
incs = CommandLineOptions -> [FilePath]
optIncludePaths CommandLineOptions
opts'
          [FilePath] -> AbsolutePath -> TCM ()
setIncludeDirs [FilePath]
incs AbsolutePath
root
          forall (m :: * -> *). HasOptions m => m [AbsolutePath]
getIncludeDirs
        [AbsolutePath]
incs -> forall (m :: * -> *) a. Monad m => a -> m a
return [AbsolutePath]
incs
      forall (m :: * -> *).
MonadTCState m =>
(TCState -> TCState) -> m ()
modifyTC forall a b. (a -> b) -> a -> b
$ forall a. LensCommandLineOptions a => CommandLineOptions -> a -> a
Lens.setCommandLineOptions CommandLineOptions
opts{ optAbsoluteIncludePaths :: [AbsolutePath]
optAbsoluteIncludePaths = [AbsolutePath]
incs }
      PragmaOptions -> TCM ()
setPragmaOptions (CommandLineOptions -> PragmaOptions
optPragmaOptions CommandLineOptions
opts)
      TCM ()
updateBenchmarkingStatus

libToTCM :: LibM a -> TCM a
libToTCM :: forall a. LibM a -> TCM a
libToTCM LibM a
m = do
  Map FilePath ProjectConfig
cachedConfs <- forall (m :: * -> *) a. ReadTCState m => Lens' a TCState -> m a
useTC Lens' (Map FilePath ProjectConfig) TCState
stProjectConfigs
  Map FilePath AgdaLibFile
cachedLibs  <- forall (m :: * -> *) a. ReadTCState m => Lens' a TCState -> m a
useTC Lens' (Map FilePath AgdaLibFile) TCState
stAgdaLibFiles

  ((Either Doc a
z, [LibWarning]
warns), (Map FilePath ProjectConfig
cachedConfs', Map FilePath AgdaLibFile
cachedLibs')) <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$
    (forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
`runStateT` (Map FilePath ProjectConfig
cachedConfs, Map FilePath AgdaLibFile
cachedLibs)) forall a b. (a -> b) -> a -> b
$ forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
runWriterT forall a b. (a -> b) -> a -> b
$ forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT LibM a
m

  forall (m :: * -> *) a.
MonadTCState m =>
Lens' a TCState -> (a -> a) -> m ()
modifyTCLens Lens' (Map FilePath ProjectConfig) TCState
stProjectConfigs forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const Map FilePath ProjectConfig
cachedConfs'
  forall (m :: * -> *) a.
MonadTCState m =>
Lens' a TCState -> (a -> a) -> m ()
modifyTCLens Lens' (Map FilePath AgdaLibFile) TCState
stAgdaLibFiles   forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const Map FilePath AgdaLibFile
cachedLibs'

  forall (f :: * -> *). Applicative f => SafeMode -> f () -> f ()
unless (forall (t :: * -> *) a. Foldable t => t a -> SafeMode
null [LibWarning]
warns) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
(HasCallStack, MonadWarning m) =>
[Warning] -> m ()
warnings forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map LibWarning -> Warning
LibraryWarning [LibWarning]
warns
  case Either Doc a
z of
    Left Doc
s  -> forall (m :: * -> *) a.
(HasCallStack, MonadTCError m) =>
TypeError -> m a
typeError forall a b. (a -> b) -> a -> b
$ Doc -> TypeError
GenericDocError Doc
s
    Right a
x -> forall (m :: * -> *) a. Monad m => a -> m a
return a
x

-- | Returns the library files for a given file.

getAgdaLibFiles
  :: AbsolutePath        -- ^ The file name.
  -> TopLevelModuleName  -- ^ The top-level module name.
  -> TCM [AgdaLibFile]
getAgdaLibFiles :: AbsolutePath -> TopLevelModuleName -> TCM [AgdaLibFile]
getAgdaLibFiles AbsolutePath
f TopLevelModuleName
m = do
  SafeMode
useLibs <- CommandLineOptions -> SafeMode
optUseLibs forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasOptions m => m CommandLineOptions
commandLineOptions
  if | SafeMode
useLibs   -> forall a. LibM a -> TCM a
libToTCM forall a b. (a -> b) -> a -> b
$ forall a. [AgdaLibFile] -> LibErrorIO a -> LibM a
mkLibM [] forall a b. (a -> b) -> a -> b
$ FilePath -> LibErrorIO [AgdaLibFile]
getAgdaLibFiles' FilePath
root
     | SafeMode
otherwise -> forall (m :: * -> *) a. Monad m => a -> m a
return []
  where
  root :: FilePath
root = AbsolutePath -> FilePath
filePath (AbsolutePath -> TopLevelModuleName -> AbsolutePath
projectRoot AbsolutePath
f TopLevelModuleName
m)

-- | Returns the library options for a given file.

getLibraryOptions
  :: AbsolutePath        -- ^ The file name.
  -> TopLevelModuleName  -- ^ The top-level module name.
  -> TCM [OptionsPragma]
getLibraryOptions :: AbsolutePath -> TopLevelModuleName -> TCM [[FilePath]]
getLibraryOptions AbsolutePath
f TopLevelModuleName
m = forall a b. (a -> b) -> [a] -> [b]
map AgdaLibFile -> [FilePath]
_libPragmas forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AbsolutePath -> TopLevelModuleName -> TCM [AgdaLibFile]
getAgdaLibFiles AbsolutePath
f TopLevelModuleName
m

setLibraryPaths
  :: AbsolutePath
     -- ^ The base directory of relative paths.
  -> CommandLineOptions
  -> TCM CommandLineOptions
setLibraryPaths :: AbsolutePath -> CommandLineOptions -> TCM CommandLineOptions
setLibraryPaths AbsolutePath
root CommandLineOptions
o =
  CommandLineOptions -> TCM CommandLineOptions
setLibraryIncludes forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< AbsolutePath -> CommandLineOptions -> TCM CommandLineOptions
addDefaultLibraries AbsolutePath
root CommandLineOptions
o

setLibraryIncludes :: CommandLineOptions -> TCM CommandLineOptions
setLibraryIncludes :: CommandLineOptions -> TCM CommandLineOptions
setLibraryIncludes CommandLineOptions
o
  | SafeMode -> SafeMode
not (CommandLineOptions -> SafeMode
optUseLibs CommandLineOptions
o) = forall (f :: * -> *) a. Applicative f => a -> f a
pure CommandLineOptions
o
  | SafeMode
otherwise = do
    let libs :: [FilePath]
libs = CommandLineOptions -> [FilePath]
optLibraries CommandLineOptions
o
    [AgdaLibFile]
installed <- forall a. LibM a -> TCM a
libToTCM forall a b. (a -> b) -> a -> b
$ Maybe FilePath -> LibM [AgdaLibFile]
getInstalledLibraries (CommandLineOptions -> Maybe FilePath
optOverrideLibrariesFile CommandLineOptions
o)
    [FilePath]
paths     <- forall a. LibM a -> TCM a
libToTCM forall a b. (a -> b) -> a -> b
$ Maybe FilePath -> [AgdaLibFile] -> [FilePath] -> LibM [FilePath]
libraryIncludePaths (CommandLineOptions -> Maybe FilePath
optOverrideLibrariesFile CommandLineOptions
o) [AgdaLibFile]
installed [FilePath]
libs
    forall (m :: * -> *) a. Monad m => a -> m a
return CommandLineOptions
o{ optIncludePaths :: [FilePath]
optIncludePaths = [FilePath]
paths forall a. [a] -> [a] -> [a]
++ CommandLineOptions -> [FilePath]
optIncludePaths CommandLineOptions
o }

addDefaultLibraries
  :: AbsolutePath
     -- ^ The base directory of relative paths.
  -> CommandLineOptions
  -> TCM CommandLineOptions
addDefaultLibraries :: AbsolutePath -> CommandLineOptions -> TCM CommandLineOptions
addDefaultLibraries AbsolutePath
root CommandLineOptions
o
  | SafeMode -> SafeMode
not (forall (t :: * -> *) a. Foldable t => t a -> SafeMode
null forall a b. (a -> b) -> a -> b
$ CommandLineOptions -> [FilePath]
optLibraries CommandLineOptions
o) SafeMode -> SafeMode -> SafeMode
|| SafeMode -> SafeMode
not (CommandLineOptions -> SafeMode
optUseLibs CommandLineOptions
o) = forall (f :: * -> *) a. Applicative f => a -> f a
pure CommandLineOptions
o
  | SafeMode
otherwise = do
  ([FilePath]
libs, [FilePath]
incs) <- forall a. LibM a -> TCM a
libToTCM forall a b. (a -> b) -> a -> b
$ FilePath -> SafeMode -> LibM ([FilePath], [FilePath])
getDefaultLibraries (AbsolutePath -> FilePath
filePath AbsolutePath
root) (CommandLineOptions -> SafeMode
optDefaultLibs CommandLineOptions
o)
  forall (m :: * -> *) a. Monad m => a -> m a
return CommandLineOptions
o{ optIncludePaths :: [FilePath]
optIncludePaths = [FilePath]
incs forall a. [a] -> [a] -> [a]
++ CommandLineOptions -> [FilePath]
optIncludePaths CommandLineOptions
o, optLibraries :: [FilePath]
optLibraries = [FilePath]
libs }

-- This function is only called when an interactor exists
-- (i.e. when Agda actually does something).
addTrustedExecutables
  :: CommandLineOptions
  -> TCM CommandLineOptions
addTrustedExecutables :: CommandLineOptions -> TCM CommandLineOptions
addTrustedExecutables CommandLineOptions
o = do
  Map ExeName FilePath
trustedExes <- forall a. LibM a -> TCM a
libToTCM forall a b. (a -> b) -> a -> b
$ LibM (Map ExeName FilePath)
getTrustedExecutables
  -- Wen, 2020-06-03
  -- Replace the map wholesale instead of computing the union because this function
  -- should never be called more than once, and doing so either has the same result
  -- or is a security risk.
  forall (m :: * -> *) a. Monad m => a -> m a
return CommandLineOptions
o{ optTrustedExecutables :: Map ExeName FilePath
optTrustedExecutables = Map ExeName FilePath
trustedExes }

setOptionsFromPragma :: OptionsPragma -> TCM ()
setOptionsFromPragma :: [FilePath] -> TCM ()
setOptionsFromPragma [FilePath]
ps = do
    CommandLineOptions
opts <- forall (m :: * -> *). HasOptions m => m CommandLineOptions
commandLineOptions
    forall (m :: * -> *) opts.
Monad m =>
OptM opts -> m (Either FilePath opts)
runOptM ([FilePath] -> CommandLineOptions -> OptM PragmaOptions
parsePragmaOptions [FilePath]
ps CommandLineOptions
opts) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Left FilePath
err    -> forall (m :: * -> *) a.
(HasCallStack, MonadTCError m) =>
TypeError -> m a
typeError forall a b. (a -> b) -> a -> b
$ FilePath -> TypeError
GenericError FilePath
err
      Right PragmaOptions
opts' -> PragmaOptions -> TCM ()
setPragmaOptions PragmaOptions
opts'

-- | Disable display forms.
enableDisplayForms :: MonadTCEnv m => m a -> m a
enableDisplayForms :: forall (m :: * -> *) a. MonadTCEnv m => m a -> m a
enableDisplayForms =
  forall (m :: * -> *) a.
MonadTCEnv m =>
(TCEnv -> TCEnv) -> m a -> m a
localTC forall a b. (a -> b) -> a -> b
$ \TCEnv
e -> TCEnv
e { envDisplayFormsEnabled :: SafeMode
envDisplayFormsEnabled = SafeMode
True }

-- | Disable display forms.
disableDisplayForms :: MonadTCEnv m => m a -> m a
disableDisplayForms :: forall (m :: * -> *) a. MonadTCEnv m => m a -> m a
disableDisplayForms =
  forall (m :: * -> *) a.
MonadTCEnv m =>
(TCEnv -> TCEnv) -> m a -> m a
localTC forall a b. (a -> b) -> a -> b
$ \TCEnv
e -> TCEnv
e { envDisplayFormsEnabled :: SafeMode
envDisplayFormsEnabled = SafeMode
False }

-- | Check if display forms are enabled.
displayFormsEnabled :: MonadTCEnv m => m Bool
displayFormsEnabled :: forall (m :: * -> *). MonadTCEnv m => m SafeMode
displayFormsEnabled = forall (m :: * -> *) a. MonadTCEnv m => (TCEnv -> a) -> m a
asksTC TCEnv -> SafeMode
envDisplayFormsEnabled

-- | Gets the include directories.
--
-- Precondition: 'optAbsoluteIncludePaths' must be nonempty (i.e.
-- 'setCommandLineOptions' must have run).

getIncludeDirs :: HasOptions m => m [AbsolutePath]
getIncludeDirs :: forall (m :: * -> *). HasOptions m => m [AbsolutePath]
getIncludeDirs = do
  [AbsolutePath]
incs <- CommandLineOptions -> [AbsolutePath]
optAbsoluteIncludePaths forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasOptions m => m CommandLineOptions
commandLineOptions
  case [AbsolutePath]
incs of
    [] -> forall a. HasCallStack => a
__IMPOSSIBLE__
    [AbsolutePath]
_  -> forall (m :: * -> *) a. Monad m => a -> m a
return [AbsolutePath]
incs

-- | Makes the given directories absolute and stores them as include
-- directories.
--
-- If the include directories change, then the state is reset
-- (completely, except for the include directories and some other
-- things).
--
-- An empty list is interpreted as @["."]@.

setIncludeDirs :: [FilePath]    -- ^ New include directories.
               -> AbsolutePath  -- ^ The base directory of relative paths.
               -> TCM ()
setIncludeDirs :: [FilePath] -> AbsolutePath -> TCM ()
setIncludeDirs [FilePath]
incs AbsolutePath
root = do
  -- save the previous include dirs
  [AbsolutePath]
oldIncs <- forall (m :: * -> *) a. ReadTCState m => (TCState -> a) -> m a
getsTC forall a. LensIncludePaths a => a -> [AbsolutePath]
Lens.getAbsoluteIncludePaths

  -- Add the current dir if no include path is given
  [FilePath]
incs <- forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ if forall (t :: * -> *) a. Foldable t => t a -> SafeMode
null [FilePath]
incs then [FilePath
"."] else [FilePath]
incs
  -- Make paths absolute
  [AbsolutePath]
incs <- forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$  forall a b. (a -> b) -> [a] -> [b]
map (FilePath -> AbsolutePath
mkAbsolute forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AbsolutePath -> FilePath
filePath AbsolutePath
root FilePath -> FilePath -> FilePath
</>)) [FilePath]
incs

  -- Andreas, 2013-10-30  Add default include dir
      -- NB: This is an absolute file name, but
      -- Agda.Utils.FilePath wants to check absoluteness anyway.
  AbsolutePath
primdir <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ FilePath -> AbsolutePath
mkAbsolute forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO FilePath
getPrimitiveLibDir
      -- We add the default dir at the end, since it is then
      -- printed last in error messages.
      -- Might also be useful to overwrite default imports...
  [AbsolutePath]
incs <- forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall b a. Ord b => (a -> b) -> [a] -> [a]
nubOn forall a. a -> a
id forall a b. (a -> b) -> a -> b
$ [AbsolutePath]
incs forall a. [a] -> [a] -> [a]
++ [AbsolutePath
primdir]

  forall (m :: * -> *).
MonadDebug m =>
FilePath -> VerboseLevel -> TCM Doc -> m ()
reportSDoc FilePath
"setIncludeDirs" VerboseLevel
10 forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *). Foldable t => t Doc -> Doc
vcat
    [ Doc
"Old include directories:"
    , VerboseLevel -> Doc -> Doc
nest VerboseLevel
2 forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *). Foldable t => t Doc -> Doc
vcat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a. Pretty a => a -> Doc
pretty [AbsolutePath]
oldIncs
    , Doc
"New include directories:"
    , VerboseLevel -> Doc -> Doc
nest VerboseLevel
2 forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *). Foldable t => t Doc -> Doc
vcat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a. Pretty a => a -> Doc
pretty [AbsolutePath]
incs
    ]

  -- Check whether the include dirs have changed.  If yes, reset state.
  -- Andreas, 2013-10-30 comments:
  -- The logic, namely using the include-dirs variable as a driver
  -- for the interaction, qualifies for a code-obfuscation contest.
  -- I guess one Boolean more in the state cost 10.000 EUR at the time
  -- of this implementation...
  --
  -- Andreas, perhaps you have misunderstood something: If the include
  -- directories have changed and we do not reset the decoded modules,
  -- then we might (depending on how the rest of the code works) end
  -- up in a situation in which we use the contents of the file
  -- "old-path/M.agda", when the user actually meant
  -- "new-path/M.agda".
  forall (f :: * -> *). Applicative f => SafeMode -> f () -> f ()
when (forall a. Ord a => [a] -> [a]
sort [AbsolutePath]
oldIncs forall a. Eq a => a -> a -> SafeMode
/= forall a. Ord a => [a] -> [a]
sort [AbsolutePath]
incs) forall a b. (a -> b) -> a -> b
$ do
    InteractionOutputCallback
ho <- forall (m :: * -> *). ReadTCState m => m InteractionOutputCallback
getInteractionOutputCallback
    [TCWarning]
tcWarnings <- forall (m :: * -> *) a. ReadTCState m => Lens' a TCState -> m a
useTC Lens' [TCWarning] TCState
stTCWarnings -- restore already generated warnings
    Map FilePath ProjectConfig
projectConfs <- forall (m :: * -> *) a. ReadTCState m => Lens' a TCState -> m a
useTC Lens' (Map FilePath ProjectConfig) TCState
stProjectConfigs  -- restore cached project configs & .agda-lib
    Map FilePath AgdaLibFile
agdaLibFiles <- forall (m :: * -> *) a. ReadTCState m => Lens' a TCState -> m a
useTC Lens' (Map FilePath AgdaLibFile) TCState
stAgdaLibFiles    -- files, since they use absolute paths
    DecodedModules
decodedModules <- TCM DecodedModules
getDecodedModules
    (DecodedModules
keptDecodedModules, ModuleToSource
modFile) <- [AbsolutePath]
-> DecodedModules -> TCM (DecodedModules, ModuleToSource)
modulesToKeep [AbsolutePath]
incs DecodedModules
decodedModules
    TCM ()
resetAllState
    forall (m :: * -> *) a.
MonadTCState m =>
Lens' a TCState -> a -> m ()
setTCLens Lens' [TCWarning] TCState
stTCWarnings [TCWarning]
tcWarnings
    forall (m :: * -> *) a.
MonadTCState m =>
Lens' a TCState -> a -> m ()
setTCLens Lens' (Map FilePath ProjectConfig) TCState
stProjectConfigs Map FilePath ProjectConfig
projectConfs
    forall (m :: * -> *) a.
MonadTCState m =>
Lens' a TCState -> a -> m ()
setTCLens Lens' (Map FilePath AgdaLibFile) TCState
stAgdaLibFiles Map FilePath AgdaLibFile
agdaLibFiles
    InteractionOutputCallback -> TCM ()
setInteractionOutputCallback InteractionOutputCallback
ho
    DecodedModules -> TCM ()
setDecodedModules DecodedModules
keptDecodedModules
    forall (m :: * -> *) a.
MonadTCState m =>
Lens' a TCState -> a -> m ()
setTCLens Lens' ModuleToSource TCState
stModuleToSource ModuleToSource
modFile

  forall (m :: * -> *). MonadTCState m => [AbsolutePath] -> m ()
Lens.putAbsoluteIncludePaths [AbsolutePath]
incs
  where
  -- A decoded module is kept if its top-level module name is resolved
  -- to the same absolute path using the old and the new include
  -- directories, and the same applies to all dependencies.
  --
  -- File system accesses are cached using the ModuleToSource data
  -- structure: For the old include directories this should mean that
  -- the file system is not accessed, but the file system is accessed
  -- for the new include directories, and certain changes to the file
  -- system could lead to interfaces being discarded. A new
  -- ModuleToSource structure, constructed using the new include
  -- directories, is returned.
  modulesToKeep
    :: [AbsolutePath]  -- New include directories.
    -> DecodedModules  -- Old decoded modules.
    -> TCM (DecodedModules, ModuleToSource)
  modulesToKeep :: [AbsolutePath]
-> DecodedModules -> TCM (DecodedModules, ModuleToSource)
modulesToKeep [AbsolutePath]
incs DecodedModules
old = DecodedModules
-> ModuleToSource
-> [ModuleInfo]
-> TCM (DecodedModules, ModuleToSource)
process forall k a. Map k a
Map.empty forall k a. Map k a
Map.empty [ModuleInfo]
modules
    where
    -- A graph with one node per module in old, and an edge from m to
    -- n if the module corresponding to m imports the module
    -- corresponding to n.
    dependencyGraph :: G.Graph TopLevelModuleName ()
    dependencyGraph :: Graph TopLevelModuleName ()
dependencyGraph =
      forall n e. Ord n => [n] -> Graph n e
G.fromNodes
        [ Interface -> TopLevelModuleName
iTopLevelModuleName forall a b. (a -> b) -> a -> b
$ ModuleInfo -> Interface
miInterface ModuleInfo
m
        | ModuleInfo
m <- forall k a. Map k a -> [a]
Map.elems DecodedModules
old
        ]
        forall n e. Ord n => Graph n e -> Graph n e -> Graph n e
`G.union`
      forall n e. Ord n => [Edge n e] -> Graph n e
G.fromEdges
        [ G.Edge
            { source :: TopLevelModuleName
source = Interface -> TopLevelModuleName
iTopLevelModuleName forall a b. (a -> b) -> a -> b
$ ModuleInfo -> Interface
miInterface ModuleInfo
m
            , target :: TopLevelModuleName
target = TopLevelModuleName
d
            , label :: ()
label = ()
            }
        | ModuleInfo
m      <- forall k a. Map k a -> [a]
Map.elems DecodedModules
old
        , (TopLevelModuleName
d, Hash
_) <- Interface -> [(TopLevelModuleName, Hash)]
iImportedModules forall a b. (a -> b) -> a -> b
$ ModuleInfo -> Interface
miInterface ModuleInfo
m
        ]

    -- All the modules from old, sorted so that all of a module's
    -- dependencies precede it in the list.
    modules :: [ModuleInfo]
    modules :: [ModuleInfo]
modules =
      forall a b. (a -> b) -> [a] -> [b]
map (\case
              Graph.CyclicSCC{} ->
                -- Agda does not allow cycles in the dependency graph.
                forall a. HasCallStack => a
__IMPOSSIBLE__
              Graph.AcyclicSCC TopLevelModuleName
m ->
                case forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup TopLevelModuleName
m DecodedModules
old of
                  Just ModuleInfo
m  -> ModuleInfo
m
                  Maybe ModuleInfo
Nothing -> forall a. HasCallStack => a
__IMPOSSIBLE__) forall a b. (a -> b) -> a -> b
$
      forall n e. Ord n => Graph n e -> [SCC n]
G.sccs' Graph TopLevelModuleName ()
dependencyGraph

    process ::
      Map TopLevelModuleName ModuleInfo -> ModuleToSource ->
      [ModuleInfo] -> TCM (DecodedModules, ModuleToSource)
    process :: DecodedModules
-> ModuleToSource
-> [ModuleInfo]
-> TCM (DecodedModules, ModuleToSource)
process !DecodedModules
keep !ModuleToSource
modFile [] = forall (m :: * -> *) a. Monad m => a -> m a
return
      ( forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList forall a b. (a -> b) -> a -> b
$
        forall k a. Map k a -> [(k, a)]
Map.toList DecodedModules
keep
      , ModuleToSource
modFile
      )
    process DecodedModules
keep ModuleToSource
modFile (ModuleInfo
m : [ModuleInfo]
ms) = do
      let deps :: [TopLevelModuleName]
deps     = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ Interface -> [(TopLevelModuleName, Hash)]
iImportedModules forall a b. (a -> b) -> a -> b
$ ModuleInfo -> Interface
miInterface ModuleInfo
m
          depsKept :: SafeMode
depsKept = forall (t :: * -> *) a.
Foldable t =>
(a -> SafeMode) -> t a -> SafeMode
all (forall k a. Ord k => k -> Map k a -> SafeMode
`Map.member` DecodedModules
keep) [TopLevelModuleName]
deps
      (DecodedModules
keep, ModuleToSource
modFile) <-
        if SafeMode -> SafeMode
not SafeMode
depsKept then forall (m :: * -> *) a. Monad m => a -> m a
return (DecodedModules
keep, ModuleToSource
modFile) else do
        let t :: TopLevelModuleName
t = Interface -> TopLevelModuleName
iTopLevelModuleName forall a b. (a -> b) -> a -> b
$ ModuleInfo -> Interface
miInterface ModuleInfo
m
        Either FindError SourceFile
oldF            <- TopLevelModuleName -> TCM (Either FindError SourceFile)
findFile' TopLevelModuleName
t
        (Either FindError SourceFile
newF, ModuleToSource
modFile) <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ [AbsolutePath]
-> TopLevelModuleName
-> ModuleToSource
-> IO (Either FindError SourceFile, ModuleToSource)
findFile'' [AbsolutePath]
incs TopLevelModuleName
t ModuleToSource
modFile
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case (Either FindError SourceFile
oldF, Either FindError SourceFile
newF) of
          (Right SourceFile
f1, Right SourceFile
f2) | SourceFile
f1 forall a. Eq a => a -> a -> SafeMode
== SourceFile
f2 ->
            (forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert TopLevelModuleName
t ModuleInfo
m DecodedModules
keep, ModuleToSource
modFile)
          (Either FindError SourceFile, Either FindError SourceFile)
_ -> (DecodedModules
keep, ModuleToSource
modFile)
      DecodedModules
-> ModuleToSource
-> [ModuleInfo]
-> TCM (DecodedModules, ModuleToSource)
process DecodedModules
keep ModuleToSource
modFile [ModuleInfo]
ms

isPropEnabled :: HasOptions m => m Bool
isPropEnabled :: forall (m :: * -> *). HasOptions m => m SafeMode
isPropEnabled = PragmaOptions -> SafeMode
optProp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasOptions m => m PragmaOptions
pragmaOptions

isTwoLevelEnabled :: HasOptions m => m Bool
isTwoLevelEnabled :: forall (m :: * -> *). HasOptions m => m SafeMode
isTwoLevelEnabled = forall (b :: SafeMode). KnownBool b => WithDefault b -> SafeMode
collapseDefault forall b c a. (b -> c) -> (a -> b) -> a -> c
. PragmaOptions -> WithDefault 'False
optTwoLevel forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasOptions m => m PragmaOptions
pragmaOptions

{-# SPECIALIZE hasUniversePolymorphism :: TCM Bool #-}
hasUniversePolymorphism :: HasOptions m => m Bool
hasUniversePolymorphism :: forall (m :: * -> *). HasOptions m => m SafeMode
hasUniversePolymorphism = PragmaOptions -> SafeMode
optUniversePolymorphism forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasOptions m => m PragmaOptions
pragmaOptions

showImplicitArguments :: HasOptions m => m Bool
showImplicitArguments :: forall (m :: * -> *). HasOptions m => m SafeMode
showImplicitArguments = PragmaOptions -> SafeMode
optShowImplicit forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasOptions m => m PragmaOptions
pragmaOptions

showIrrelevantArguments :: HasOptions m => m Bool
showIrrelevantArguments :: forall (m :: * -> *). HasOptions m => m SafeMode
showIrrelevantArguments = PragmaOptions -> SafeMode
optShowIrrelevant forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasOptions m => m PragmaOptions
pragmaOptions

showIdentitySubstitutions :: HasOptions m => m Bool
showIdentitySubstitutions :: forall (m :: * -> *). HasOptions m => m SafeMode
showIdentitySubstitutions = PragmaOptions -> SafeMode
optShowIdentitySubstitutions forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasOptions m => m PragmaOptions
pragmaOptions

-- | Switch on printing of implicit and irrelevant arguments.
--   E.g. for reification in with-function generation.
--
--   Restores all 'PragmaOptions' after completion.
--   Thus, do not attempt to make persistent 'PragmaOptions'
--   changes in a `withShowAllArguments` bracket.

withShowAllArguments :: ReadTCState m => m a -> m a
withShowAllArguments :: forall (m :: * -> *) a. ReadTCState m => m a -> m a
withShowAllArguments = forall (m :: * -> *) a. ReadTCState m => SafeMode -> m a -> m a
withShowAllArguments' SafeMode
True

withShowAllArguments' :: ReadTCState m => Bool -> m a -> m a
withShowAllArguments' :: forall (m :: * -> *) a. ReadTCState m => SafeMode -> m a -> m a
withShowAllArguments' SafeMode
yes = forall (m :: * -> *) a.
ReadTCState m =>
(PragmaOptions -> PragmaOptions) -> m a -> m a
withPragmaOptions forall a b. (a -> b) -> a -> b
$ \ PragmaOptions
opts ->
  PragmaOptions
opts { optShowImplicit :: SafeMode
optShowImplicit = SafeMode
yes, optShowIrrelevant :: SafeMode
optShowIrrelevant = SafeMode
yes }

-- | Change 'PragmaOptions' for a computation and restore afterwards.
withPragmaOptions :: ReadTCState m => (PragmaOptions -> PragmaOptions) -> m a -> m a
withPragmaOptions :: forall (m :: * -> *) a.
ReadTCState m =>
(PragmaOptions -> PragmaOptions) -> m a -> m a
withPragmaOptions = forall (m :: * -> *) a b.
ReadTCState m =>
Lens' a TCState -> (a -> a) -> m b -> m b
locallyTCState Lens' PragmaOptions TCState
stPragmaOptions

positivityCheckEnabled :: HasOptions m => m Bool
positivityCheckEnabled :: forall (m :: * -> *). HasOptions m => m SafeMode
positivityCheckEnabled = SafeMode -> SafeMode
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. PragmaOptions -> SafeMode
optDisablePositivity forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasOptions m => m PragmaOptions
pragmaOptions

{-# SPECIALIZE typeInType :: TCM Bool #-}
typeInType :: HasOptions m => m Bool
typeInType :: forall (m :: * -> *). HasOptions m => m SafeMode
typeInType = SafeMode -> SafeMode
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. PragmaOptions -> SafeMode
optUniverseCheck forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasOptions m => m PragmaOptions
pragmaOptions

etaEnabled :: HasOptions m => m Bool
etaEnabled :: forall (m :: * -> *). HasOptions m => m SafeMode
etaEnabled = PragmaOptions -> SafeMode
optEta forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasOptions m => m PragmaOptions
pragmaOptions

maxInstanceSearchDepth :: HasOptions m => m Int
maxInstanceSearchDepth :: forall (m :: * -> *). HasOptions m => m VerboseLevel
maxInstanceSearchDepth = PragmaOptions -> VerboseLevel
optInstanceSearchDepth forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasOptions m => m PragmaOptions
pragmaOptions

maxInversionDepth :: HasOptions m => m Int
maxInversionDepth :: forall (m :: * -> *). HasOptions m => m VerboseLevel
maxInversionDepth = PragmaOptions -> VerboseLevel
optInversionMaxDepth forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasOptions m => m PragmaOptions
pragmaOptions

-- | Returns the 'Language' currently in effect.

getLanguage :: HasOptions m => m Language
getLanguage :: forall (m :: * -> *). HasOptions m => m Language
getLanguage = do
  PragmaOptions
opts <- forall (m :: * -> *). HasOptions m => m PragmaOptions
pragmaOptions
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
    if SafeMode -> SafeMode
not (forall (b :: SafeMode). KnownBool b => WithDefault b -> SafeMode
collapseDefault (PragmaOptions -> WithDefault 'False
optWithoutK PragmaOptions
opts)) then Language
WithK else
    case PragmaOptions -> Maybe Cubical
optCubical PragmaOptions
opts of
      Just Cubical
variant -> Cubical -> Language
Cubical Cubical
variant
      Maybe Cubical
Nothing      -> Language
WithoutK