{-# OPTIONS_GHC -Wunused-imports #-}

{-# OPTIONS_GHC -fno-warn-orphans #-}

module Agda.TypeChecking.Reduce.Monad
  ( constructorForm
  , enterClosure
  , getConstInfo
  , askR, applyWhenVerboseS
  ) where

import Prelude hiding (null)

import Control.Monad         ( liftM2 )

import qualified Data.Map as Map
import Data.Maybe

import System.IO.Unsafe

import Agda.Syntax.Internal
import Agda.TypeChecking.Monad hiding (enterClosure, constructorForm)
import Agda.TypeChecking.Substitute

import Agda.Utils.Lens
import Agda.Utils.Maybe
import Agda.Utils.Monad
import Agda.Syntax.Common.Pretty () --instance only


instance HasBuiltins ReduceM where
  getBuiltinThing :: SomeBuiltin -> ReduceM (Maybe (Builtin PrimFun))
getBuiltinThing SomeBuiltin
b =
    (Maybe (Builtin PrimFun)
 -> Maybe (Builtin PrimFun) -> Maybe (Builtin PrimFun))
-> ReduceM (Maybe (Builtin PrimFun))
-> ReduceM (Maybe (Builtin PrimFun))
-> ReduceM (Maybe (Builtin PrimFun))
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 ((Builtin PrimFun -> Builtin PrimFun -> Builtin PrimFun)
-> Maybe (Builtin PrimFun)
-> Maybe (Builtin PrimFun)
-> Maybe (Builtin PrimFun)
forall a. (a -> a -> a) -> Maybe a -> Maybe a -> Maybe a
unionMaybeWith Builtin PrimFun -> Builtin PrimFun -> Builtin PrimFun
forall a. Builtin a -> Builtin a -> Builtin a
unionBuiltin)
      (SomeBuiltin
-> Map SomeBuiltin (Builtin PrimFun) -> Maybe (Builtin PrimFun)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup SomeBuiltin
b (Map SomeBuiltin (Builtin PrimFun) -> Maybe (Builtin PrimFun))
-> ReduceM (Map SomeBuiltin (Builtin PrimFun))
-> ReduceM (Maybe (Builtin PrimFun))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Lens' TCState (Map SomeBuiltin (Builtin PrimFun))
-> ReduceM (Map SomeBuiltin (Builtin PrimFun))
forall (m :: * -> *) a. ReadTCState m => Lens' TCState a -> m a
useR (Map SomeBuiltin (Builtin PrimFun)
 -> f (Map SomeBuiltin (Builtin PrimFun)))
-> TCState -> f TCState
Lens' TCState (Map SomeBuiltin (Builtin PrimFun))
stLocalBuiltins)
      (SomeBuiltin
-> Map SomeBuiltin (Builtin PrimFun) -> Maybe (Builtin PrimFun)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup SomeBuiltin
b (Map SomeBuiltin (Builtin PrimFun) -> Maybe (Builtin PrimFun))
-> ReduceM (Map SomeBuiltin (Builtin PrimFun))
-> ReduceM (Maybe (Builtin PrimFun))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Lens' TCState (Map SomeBuiltin (Builtin PrimFun))
-> ReduceM (Map SomeBuiltin (Builtin PrimFun))
forall (m :: * -> *) a. ReadTCState m => Lens' TCState a -> m a
useR (Map SomeBuiltin (Builtin PrimFun)
 -> f (Map SomeBuiltin (Builtin PrimFun)))
-> TCState -> f TCState
Lens' TCState (Map SomeBuiltin (Builtin PrimFun))
stImportedBuiltins)

constructorForm :: HasBuiltins m => Term -> m Term
constructorForm :: forall (m :: * -> *). HasBuiltins m => Term -> m Term
constructorForm Term
v = do
  Maybe Term
mz <- BuiltinId -> m (Maybe Term)
forall (m :: * -> *). HasBuiltins m => BuiltinId -> m (Maybe Term)
getBuiltin' BuiltinId
builtinZero
  Maybe Term
ms <- BuiltinId -> m (Maybe Term)
forall (m :: * -> *). HasBuiltins m => BuiltinId -> m (Maybe Term)
getBuiltin' BuiltinId
builtinSuc
  Term -> m Term
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Term -> m Term) -> Term -> m Term
forall a b. (a -> b) -> a -> b
$ Term -> Maybe Term -> Term
forall a. a -> Maybe a -> a
fromMaybe Term
v (Maybe Term -> Term) -> Maybe Term -> Term
forall a b. (a -> b) -> a -> b
$ Maybe Term -> Maybe Term -> Term -> Maybe Term
forall (m :: * -> *).
Applicative m =>
m Term -> m Term -> Term -> m Term
constructorForm' Maybe Term
mz Maybe Term
ms Term
v

enterClosure :: LensClosure c a => c -> (a -> ReduceM b) -> ReduceM b
enterClosure :: forall c a b. LensClosure c a => c -> (a -> ReduceM b) -> ReduceM b
enterClosure c
c | Closure Signature
_sig TCEnv
env ScopeInfo
scope Map ModuleName CheckpointId
cps a
x <- c
c c -> Lens' c (Closure a) -> Closure a
forall o i. o -> Lens' o i -> i
^. (Closure a -> f (Closure a)) -> c -> f c
forall b a. LensClosure b a => Lens' b (Closure a)
Lens' c (Closure a)
lensClosure = \case
  -- The \case is a hack to correctly associate the where block to the rhs
  -- rather than to the expression in the pattern guard.
  a -> ReduceM b
f -> (ReduceEnv -> ReduceEnv) -> ReduceM b -> ReduceM b
forall a. (ReduceEnv -> ReduceEnv) -> ReduceM a -> ReduceM a
localR ((TCEnv -> TCEnv) -> (TCState -> TCState) -> ReduceEnv -> ReduceEnv
mapRedEnvSt TCEnv -> TCEnv
inEnv TCState -> TCState
inState) (a -> ReduceM b
f a
x)
    where
    inEnv :: TCEnv -> TCEnv
inEnv   TCEnv
e = TCEnv
env
    inState :: TCState -> TCState
inState TCState
s =
      -- TODO: use the signature here? would that fix parts of issue 118?
      Lens' TCState ScopeInfo -> LensSet TCState ScopeInfo
forall o i. Lens' o i -> LensSet o i
set (ScopeInfo -> f ScopeInfo) -> TCState -> f TCState
Lens' TCState ScopeInfo
stScope ScopeInfo
scope (TCState -> TCState) -> TCState -> TCState
forall a b. (a -> b) -> a -> b
$ Lens' TCState (Map ModuleName CheckpointId)
-> LensSet TCState (Map ModuleName CheckpointId)
forall o i. Lens' o i -> LensSet o i
set (Map ModuleName CheckpointId -> f (Map ModuleName CheckpointId))
-> TCState -> f TCState
Lens' TCState (Map ModuleName CheckpointId)
stModuleCheckpoints Map ModuleName CheckpointId
cps TCState
s

withFreshR :: (ReadTCState m, HasFresh i) => (i -> m a) -> m a
withFreshR :: forall (m :: * -> *) i a.
(ReadTCState m, HasFresh i) =>
(i -> m a) -> m a
withFreshR i -> m a
f = do
  TCState
s <- m TCState
forall (m :: * -> *). ReadTCState m => m TCState
getTCState
  let (i
i, TCState
s') = TCState -> (i, TCState)
forall i. HasFresh i => TCState -> (i, TCState)
nextFresh TCState
s
  (TCState -> TCState) -> m a -> m a
forall a. (TCState -> TCState) -> m a -> m a
forall (m :: * -> *) a.
ReadTCState m =>
(TCState -> TCState) -> m a -> m a
withTCState (TCState -> TCState -> TCState
forall a b. a -> b -> a
const TCState
s') (i -> m a
f i
i)

instance MonadAddContext ReduceM where
  withFreshName :: forall a. Range -> ArgName -> (Name -> ReduceM a) -> ReduceM a
withFreshName Range
r ArgName
s Name -> ReduceM a
k = (NameId -> ReduceM a) -> ReduceM a
forall (m :: * -> *) i a.
(ReadTCState m, HasFresh i) =>
(i -> m a) -> m a
withFreshR ((NameId -> ReduceM a) -> ReduceM a)
-> (NameId -> ReduceM a) -> ReduceM a
forall a b. (a -> b) -> a -> b
$ \NameId
i -> Name -> ReduceM a
k (Range -> NameId -> ArgName -> Name
forall a. MkName a => Range -> NameId -> a -> Name
mkName Range
r NameId
i ArgName
s)

  addCtx :: forall a. Name -> Dom Type -> ReduceM a -> ReduceM a
addCtx = Name -> Dom Type -> ReduceM a -> ReduceM a
forall (m :: * -> *) a.
MonadAddContext m =>
Name -> Dom Type -> m a -> m a
defaultAddCtx

  addLetBinding' :: forall a.
Origin -> Name -> Term -> Dom Type -> ReduceM a -> ReduceM a
addLetBinding' = Origin -> Name -> Term -> Dom Type -> ReduceM a -> ReduceM a
forall (m :: * -> *) a.
(ReadTCState m, MonadTCEnv m) =>
Origin -> Name -> Term -> Dom Type -> m a -> m a
defaultAddLetBinding'

  updateContext :: forall a.
Substitution -> (Context -> Context) -> ReduceM a -> ReduceM a
updateContext Substitution
rho Context -> Context
f ReduceM a
ret = (CheckpointId -> ReduceM a) -> ReduceM a
forall (m :: * -> *) i a.
(ReadTCState m, HasFresh i) =>
(i -> m a) -> m a
withFreshR ((CheckpointId -> ReduceM a) -> ReduceM a)
-> (CheckpointId -> ReduceM a) -> ReduceM a
forall a b. (a -> b) -> a -> b
$ \ CheckpointId
chkpt ->
    (TCEnv -> TCEnv) -> ReduceM a -> ReduceM a
forall a. (TCEnv -> TCEnv) -> ReduceM a -> ReduceM a
forall (m :: * -> *) a.
MonadTCEnv m =>
(TCEnv -> TCEnv) -> m a -> m a
localTC (\TCEnv
e -> TCEnv
e { envContext = f $ envContext e
                     , envCurrentCheckpoint = chkpt
                     , envCheckpoints = Map.insert chkpt IdS $
                                          fmap (applySubst rho) (envCheckpoints e)
                     }) ReduceM a
ret
        -- let-bindings keep track of own their context

instance MonadDebug ReduceM where

  traceDebugMessage :: forall a.
ArgName -> VerboseLevel -> ArgName -> ReduceM a -> ReduceM a
traceDebugMessage ArgName
k VerboseLevel
n ArgName
s ReduceM a
cont = do
    ReduceEnv TCEnv
env TCState
st Maybe (MetaId -> ReduceM Bool)
_ <- ReduceM ReduceEnv
askR
    IO (ReduceM a) -> ReduceM a
forall a. IO a -> a
unsafePerformIO (IO (ReduceM a) -> ReduceM a) -> IO (ReduceM a) -> ReduceM a
forall a b. (a -> b) -> a -> b
$ do
      ((), TCState)
_ <- TCEnv -> TCState -> TCMT IO () -> IO ((), TCState)
forall (m :: * -> *) a.
MonadIO m =>
TCEnv -> TCState -> TCMT m a -> m (a, TCState)
runTCM TCEnv
env TCState
st (TCMT IO () -> IO ((), TCState)) -> TCMT IO () -> IO ((), TCState)
forall a b. (a -> b) -> a -> b
$ ArgName -> VerboseLevel -> ArgName -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
ArgName -> VerboseLevel -> ArgName -> m ()
displayDebugMessage ArgName
k VerboseLevel
n ArgName
s
      ReduceM a -> IO (ReduceM a)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ReduceM a -> IO (ReduceM a)) -> ReduceM a -> IO (ReduceM a)
forall a b. (a -> b) -> a -> b
$ ReduceM a
cont

  formatDebugMessage :: ArgName -> VerboseLevel -> TCM Doc -> ReduceM ArgName
formatDebugMessage ArgName
k VerboseLevel
n TCM Doc
d = do
    ReduceEnv TCEnv
env TCState
st Maybe (MetaId -> ReduceM Bool)
_ <- ReduceM ReduceEnv
askR
    IO (ReduceM ArgName) -> ReduceM ArgName
forall a. IO a -> a
unsafePerformIO (IO (ReduceM ArgName) -> ReduceM ArgName)
-> IO (ReduceM ArgName) -> ReduceM ArgName
forall a b. (a -> b) -> a -> b
$ do
      (ArgName
s , TCState
_) <- TCEnv -> TCState -> TCMT IO ArgName -> IO (ArgName, TCState)
forall (m :: * -> *) a.
MonadIO m =>
TCEnv -> TCState -> TCMT m a -> m (a, TCState)
runTCM TCEnv
env TCState
st (TCMT IO ArgName -> IO (ArgName, TCState))
-> TCMT IO ArgName -> IO (ArgName, TCState)
forall a b. (a -> b) -> a -> b
$ ArgName -> VerboseLevel -> TCM Doc -> TCMT IO ArgName
forall (m :: * -> *).
MonadDebug m =>
ArgName -> VerboseLevel -> TCM Doc -> m ArgName
formatDebugMessage ArgName
k VerboseLevel
n TCM Doc
d
      ReduceM ArgName -> IO (ReduceM ArgName)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ReduceM ArgName -> IO (ReduceM ArgName))
-> ReduceM ArgName -> IO (ReduceM ArgName)
forall a b. (a -> b) -> a -> b
$ ArgName -> ReduceM ArgName
forall a. a -> ReduceM a
forall (m :: * -> *) a. Monad m => a -> m a
return ArgName
s

  verboseBracket :: forall a.
ArgName -> VerboseLevel -> ArgName -> ReduceM a -> ReduceM a
verboseBracket ArgName
k VerboseLevel
n ArgName
s = ArgName
-> VerboseLevel
-> (ReduceM a -> ReduceM a)
-> ReduceM a
-> ReduceM a
forall (m :: * -> *) a.
MonadDebug m =>
ArgName -> VerboseLevel -> (m a -> m a) -> m a -> m a
applyWhenVerboseS ArgName
k VerboseLevel
n ((ReduceM a -> ReduceM a) -> ReduceM a -> ReduceM a)
-> (ReduceM a -> ReduceM a) -> ReduceM a -> ReduceM a
forall a b. (a -> b) -> a -> b
$
    ReduceM () -> (() -> ReduceM ()) -> ReduceM a -> ReduceM a
forall (m :: * -> *) a b.
Monad m =>
m a -> (a -> m ()) -> m b -> m b
bracket_ (ArgName -> VerboseLevel -> ArgName -> ReduceM ()
forall (m :: * -> *).
MonadDebug m =>
ArgName -> VerboseLevel -> ArgName -> m ()
openVerboseBracket ArgName
k VerboseLevel
n ArgName
s) (ReduceM () -> () -> ReduceM ()
forall a b. a -> b -> a
const (ReduceM () -> () -> ReduceM ()) -> ReduceM () -> () -> ReduceM ()
forall a b. (a -> b) -> a -> b
$ ArgName -> VerboseLevel -> ReduceM ()
forall (m :: * -> *).
MonadDebug m =>
ArgName -> VerboseLevel -> m ()
closeVerboseBracket ArgName
k VerboseLevel
n)

  getVerbosity :: ReduceM Verbosity
getVerbosity      = ReduceM Verbosity
forall (m :: * -> *). HasOptions m => m Verbosity
defaultGetVerbosity
  getProfileOptions :: ReduceM ProfileOptions
getProfileOptions = ReduceM ProfileOptions
forall (m :: * -> *). HasOptions m => m ProfileOptions
defaultGetProfileOptions
  isDebugPrinting :: ReduceM Bool
isDebugPrinting   = ReduceM Bool
forall (m :: * -> *). MonadTCEnv m => m Bool
defaultIsDebugPrinting
  nowDebugPrinting :: forall a. ReduceM a -> ReduceM a
nowDebugPrinting  = ReduceM a -> ReduceM a
forall (m :: * -> *) a. MonadTCEnv m => m a -> m a
defaultNowDebugPrinting

instance HasConstInfo ReduceM where
  getRewriteRulesFor :: QName -> ReduceM RewriteRules
getRewriteRulesFor = QName -> ReduceM RewriteRules
forall (m :: * -> *).
(ReadTCState m, MonadTCEnv m) =>
QName -> m RewriteRules
defaultGetRewriteRulesFor
  getConstInfo' :: QName -> ReduceM (Either SigError Definition)
getConstInfo' QName
q = do
    ReduceEnv TCEnv
env TCState
st Maybe (MetaId -> ReduceM Bool)
_ <- ReduceM ReduceEnv
askR
    TCState -> TCEnv -> QName -> ReduceM (Either SigError Definition)
forall (m :: * -> *).
(HasOptions m, MonadDebug m, MonadTCEnv m) =>
TCState -> TCEnv -> QName -> m (Either SigError Definition)
defaultGetConstInfo TCState
st TCEnv
env QName
q

instance PureTCM ReduceM where