{-# OPTIONS_GHC -Wunused-imports #-}

-- Initially authored by Andreas, 2013-10-22.

-- | A bidirectional type checker for internal syntax.
--
--   Performs checking on unreduced terms.
--   With the exception that projection-like function applications
--   have to be reduced since they break bidirectionality.

module Mikan.TypeChecking.CheckInternal
  ( checkType, infer, inferSpine
  , CheckInternal(..)
  , Action(..), defaultAction
  ) where

import Control.Monad

import Mikan.Syntax.Common
import Mikan.Syntax.Internal
import Mikan.Syntax.Common.Pretty (prettyShow)
import Mikan.Syntax.Position (noRange)

import Mikan.TypeChecking.Rules.Cubical
import Mikan.TypeChecking.Conversion
import Mikan.TypeChecking.Datatypes
import Mikan.TypeChecking.Level
import Mikan.TypeChecking.Monad
import Mikan.TypeChecking.Pretty
import Mikan.TypeChecking.ProjectionLike (elimView, LoneProjectionLikeToLambda(..))
import Mikan.TypeChecking.Records (shouldBeProjectible)
import Mikan.TypeChecking.Substitute
import Mikan.TypeChecking.Sort
import Mikan.TypeChecking.Telescope

import Mikan.Utils.Functor (($>))
import Mikan.Utils.Maybe
import Mikan.Utils.Size

import Mikan.Utils.Impossible

-- * Bidirectional rechecker

-- | Entry point for e.g. checking WithFunctionType.
checkType :: Type -> TCM ()
checkType :: Type -> TCM ()
checkType Type
t = Constraint -> TCM () -> TCM ()
forall (m :: * -> *).
MonadConstraint m =>
Constraint -> m () -> m ()
catchConstraint (Type -> Constraint
CheckType Type
t) (TCM () -> TCM ()) -> TCM () -> TCM ()
forall a b. (a -> b) -> a -> b
$ Type -> TCM ()
forall a. (CheckInternal a, TypeOf a ~ ()) => a -> TCM ()
inferInternal Type
t

-- | 'checkInternal' traverses the whole 'Term', and we can use this
--   traversal to modify the term.
data Action = Action
  { Action -> Type -> Term -> TCM Term
preAction  :: Type -> Term -> TCM Term
    -- ^ Called on each subterm before the checker runs.
  , Action -> Type -> Term -> TCM Term
postAction :: Type -> Term -> TCM Term
    -- ^ Called on each subterm after the type checking.
  , Action -> Term -> TCM Term
elimViewAction :: Term -> TCM Term
    -- ^ Called for bringing projection-like funs in post-fix form
  , Action -> Term -> TCM Term
dropParamsAction :: Term -> TCM Term
    -- ^ Called for dropping parameters from projections when putting a
    -- transformed term in a type used in internal checking.
  }

-- | The default action is to not change the 'Term' at all.
defaultAction :: Action
defaultAction :: Action
defaultAction = Action
  { preAction :: Type -> Term -> TCM Term
preAction        = (Term -> TCM Term) -> Type -> Term -> TCM Term
forall a b. a -> b -> a
const Term -> TCM Term
forall a. a -> TCMT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  , postAction :: Type -> Term -> TCM Term
postAction       = (Term -> TCM Term) -> Type -> Term -> TCM Term
forall a b. a -> b -> a
const Term -> TCM Term
forall a. a -> TCMT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  , elimViewAction :: Term -> TCM Term
elimViewAction   = LoneProjectionLikeToLambda -> Term -> TCM Term
forall (m :: * -> *).
PureTCM m =>
LoneProjectionLikeToLambda -> Term -> m Term
elimView LoneProjectionLikeToLambda
LoneProjectionLikeToLambda
  , dropParamsAction :: Term -> TCM Term
dropParamsAction = Term -> TCM Term
forall a. a -> TCMT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  }

class CheckInternal a where
  checkInternal' :: Action -> a -> Comparison -> TypeOf a -> TCM a

  checkInternal :: a -> Comparison -> TypeOf a -> TCM ()
  checkInternal a
v Comparison
cmp TypeOf a
t = TCMT IO a -> TCM ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (TCMT IO a -> TCM ()) -> TCMT IO a -> TCM ()
forall a b. (a -> b) -> a -> b
$ Action -> a -> Comparison -> TypeOf a -> TCMT IO a
forall a.
CheckInternal a =>
Action -> a -> Comparison -> TypeOf a -> TCMT IO a
checkInternal' Action
defaultAction a
v Comparison
cmp TypeOf a
t

  inferInternal' :: (TypeOf a ~ ()) => Action -> a -> TCM a
  inferInternal' Action
act a
v = Action -> a -> Comparison -> TypeOf a -> TCMT IO a
forall a.
CheckInternal a =>
Action -> a -> Comparison -> TypeOf a -> TCMT IO a
checkInternal' Action
act a
v Comparison
CmpEq ()

  inferInternal :: (TypeOf a ~ ()) => a -> TCM ()
  inferInternal a
v = a -> Comparison -> TypeOf a -> TCM ()
forall a. CheckInternal a => a -> Comparison -> TypeOf a -> TCM ()
checkInternal a
v Comparison
CmpEq ()

instance CheckInternal Type where
  checkInternal' :: Action -> Type -> Comparison -> TypeOf Type -> TCM Type
checkInternal' Action
action (El Sort' Term
s Term
t) Comparison
cmp TypeOf Type
_ = do
    VerboseKey -> VerboseLevel -> TCMT IO Doc -> TCM ()
forall (m :: * -> *).
MonadDebug m =>
VerboseKey -> VerboseLevel -> TCMT IO Doc -> m ()
reportSDoc VerboseKey
"tc.check.internal" VerboseLevel
20 (TCMT IO Doc -> TCM ()) -> TCMT IO Doc -> TCM ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
      [ TCMT IO Doc
"checking internal type "
      , VerboseLevel -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => VerboseLevel -> m Doc -> m Doc
nest VerboseLevel
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep [ Term -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
forall (m :: * -> *). MonadPretty m => Term -> m Doc
prettyTCM Term
t TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> TCMT IO Doc
":"
                     , VerboseLevel -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => VerboseLevel -> m Doc -> m Doc
nest VerboseLevel
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ Sort' Term -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
forall (m :: * -> *). MonadPretty m => Sort' Term -> m Doc
prettyTCM Sort' Term
s ]
      ]
    t' <- Action -> Term -> Comparison -> TypeOf Term -> TCM Term
forall a.
CheckInternal a =>
Action -> a -> Comparison -> TypeOf a -> TCMT IO a
checkInternal' Action
action Term
t Comparison
cmp (Sort' Term -> Type
sort Sort' Term
s)
    s' <- sortOf t'
    reportSDoc "tc.check.internal" 30 $
      "checking if sort" <+> prettyTCM s' <+>
      "of" <+> prettyTCM t' <+>
      "fits in given sort" <+> prettyTCM s
    compareSort cmp s' s
    return (El s t')

instance CheckInternal Term where
  checkInternal' :: Action -> Term -> Comparison -> Type -> TCM Term
  checkInternal' :: Action -> Term -> Comparison -> Type -> TCM Term
checkInternal' Action
action Term
v Comparison
cmp Type
t = VerboseKey -> VerboseLevel -> VerboseKey -> TCM Term -> TCM Term
forall a.
VerboseKey -> VerboseLevel -> VerboseKey -> TCMT IO a -> TCMT IO a
forall (m :: * -> *) a.
MonadDebug m =>
VerboseKey -> VerboseLevel -> VerboseKey -> m a -> m a
verboseBracket VerboseKey
"tc.check.internal" VerboseLevel
20 VerboseKey
"" (TCM Term -> TCM Term) -> TCM Term -> TCM Term
forall a b. (a -> b) -> a -> b
$ do

    -- Debug print
    VerboseKey -> VerboseLevel -> TCM () -> TCM ()
forall (m :: * -> *).
MonadDebug m =>
VerboseKey -> VerboseLevel -> m () -> m ()
verboseS VerboseKey
"tc.check.internal" VerboseLevel
20 do
      VerboseKey -> VerboseLevel -> TCMT IO Doc -> TCM ()
forall (m :: * -> *).
MonadDebug m =>
VerboseKey -> VerboseLevel -> TCMT IO Doc -> m ()
reportSDoc VerboseKey
"tc.check.internal" VerboseLevel
20 (TCMT IO Doc -> TCM ()) -> TCMT IO Doc -> TCM ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
        [ TCMT IO Doc
"checking internal "
        , VerboseLevel -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => VerboseLevel -> m Doc -> m Doc
nest VerboseLevel
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep [ Term -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
forall (m :: * -> *). MonadPretty m => Term -> m Doc
prettyTCM Term
v TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> TCMT IO Doc
":"
                      , VerboseLevel -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => VerboseLevel -> m Doc -> m Doc
nest VerboseLevel
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ Type -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
forall (m :: * -> *). MonadPretty m => Type -> m Doc
prettyTCM Type
t ] ]
      VerboseKey -> VerboseLevel -> TCMT IO Doc -> TCM ()
forall (m :: * -> *).
MonadDebug m =>
VerboseKey -> VerboseLevel -> TCMT IO Doc -> m ()
reportSDoc VerboseKey
"tc.check.internal" VerboseLevel
60 (TCMT IO Doc -> TCM ()) -> TCMT IO Doc -> TCM ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
        [ TCMT IO Doc
"checking internal with DB indices"
        , VerboseLevel -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => VerboseLevel -> m Doc -> m Doc
nest VerboseLevel
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep [ Term -> TCMT IO Doc
forall (m :: * -> *) a. (Applicative m, Pretty a) => a -> m Doc
pretty Term
v TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> TCMT IO Doc
":"
                      , VerboseLevel -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => VerboseLevel -> m Doc -> m Doc
nest VerboseLevel
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ Type -> TCMT IO Doc
forall (m :: * -> *) a. (Applicative m, Pretty a) => a -> m Doc
pretty Type
t ] ]
      ctx <- TCMT IO Telescope
forall (m :: * -> *). MonadTCEnv m => m Telescope
getContextTelescope
      unless (null ctx) $ reportSDoc "tc.check.internal" 30 $ sep
        [ "In context"
        , nest 2 $ sep [ prettyTCM ctx ] ]

    -- Bring projection-like funs in post-fix form,
    -- (even lone ones by default).
    v <- Action -> Term -> TCM Term
elimViewAction Action
action (Term -> TCM Term) -> TCM Term -> TCM Term
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Action -> Type -> Term -> TCM Term
preAction Action
action Type
t Term
v
    postAction action t =<< case v of
      Var VerboseLevel
i Elims
es   -> do
        d <- VerboseLevel -> TCMT IO (Dom Type)
forall (m :: * -> *).
(MonadDebug m, MonadTCEnv m) =>
VerboseLevel -> m (Dom Type)
domOfBV VerboseLevel
i
        n <- nameOfBV i

        reportSDoc "tc.check.internal" 30 $ fsep
          [ "variable" , prettyTCM (var i) , "has type" , prettyTCM (unDom d)
          ]

        checkSpine action (unDom d, Var i) es cmp t

      Def QName
f Elims
es -> do  -- f is not projection(-like)!
        a <- Definition -> Type
defType (Definition -> Type) -> TCMT IO Definition -> TCM Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QName -> TCMT IO Definition
forall (m :: * -> *).
(HasConstInfo m, HasCallStack) =>
QName -> m Definition
getConstInfo QName
f

        -- if f is a cubical primitive, we must check the boundary
        -- conditions it imposes on its arguments when we are done
        -- checking its argument prefix.
        check <- isConstrainedPrimitive f >>= \case
          Just ArgsCheck
act -> (Type -> Elims -> TCM Term) -> TCMT IO (Type -> Elims -> TCM Term)
forall a. a -> TCMT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure \Type
t Elims
es -> QName -> Elims -> Term
Def QName
f (Elims -> Term) -> TCMT IO Elims -> TCM Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ArgsCheck
act (Range -> [Range]
forall a. a -> [a]
repeat Range
forall a. Range' a
noRange) Elims
es Type
t
          Maybe ArgsCheck
Nothing  -> (Type -> Elims -> TCM Term) -> TCMT IO (Type -> Elims -> TCM Term)
forall a. a -> TCMT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure \Type
t Elims
es -> Term -> TCM Term
forall a. a -> TCMT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (QName -> Elims -> Term
Def QName
f Elims
es)
        let st = CSState{ csInType :: Type
csInType = Type
a, csInHead :: Type -> Elims -> TCM Term
csInHead = Type -> Elims -> TCM Term
check, csOutHead :: Elims -> Term
csOutHead = QName -> Elims -> Term
Def QName
f }
        checkSpine' action st es cmp t

      MetaV MetaId
x Elims
es -> do
        -- we assume that meta instantiations are well-typed.
        -- if double-checking is enabled, they are checked when the meta
        -- is solved.
        a <- MetaId -> TCM Type
forall (m :: * -> *). ReadTCState m => MetaId -> m Type
getMetaType MetaId
x
        reportSDoc "tc.check.internal.meta" 30 $ "metavariable" <+> prettyTCM x <+> "has type" <+> prettyTCM a
        checkSpine action (a, MetaV x) es cmp t

      Con ConHead
c ConInfo
ci Elims
vs -> ConHead
-> Elims
-> Type
-> (QName
    -> Type -> Args -> Type -> Elims -> Telescope -> Type -> TCM Term)
-> TCM Term
forall (m :: * -> *) a.
(PureTCM m, MonadBlock m, MonadTCError m) =>
ConHead
-> Elims
-> Type
-> (QName
    -> Type -> Args -> Type -> Elims -> Telescope -> Type -> m a)
-> m a
fullyApplyCon ConHead
c Elims
vs Type
t \QName
_d Type
_dt Args
_pars Type
a Elims
vs' Telescope
tel Type
t -> do
        -- We need to fully apply the constructor to make getConType work!
        Action -> TypeOf Elims -> Elims -> Comparison -> Type -> TCM Term
checkSpine Action
action (Type
a, ConHead -> ConInfo -> Elims -> Term
Con ConHead
c ConInfo
ci) Elims
vs' Comparison
cmp Type
t TCM Term -> (Term -> TCM Term) -> TCM Term
forall a b. TCMT IO a -> (a -> TCMT IO b) -> TCMT IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
          Con ConHead
c ConInfo
ci Elims
vs2 ->
            -- Strip away the extra arguments
            Term -> TCM Term
forall a. a -> TCMT IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Term -> TCM Term) -> Term -> TCM Term
forall a b. (a -> b) -> a -> b
$ Substitution' (SubstArg Term) -> Term -> Term
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst (Impossible -> VerboseLevel -> Substitution' Term
forall a. Impossible -> VerboseLevel -> Substitution' a
strengthenS Impossible
HasCallStack => Impossible
impossible (Telescope -> VerboseLevel
forall a. Sized a => a -> VerboseLevel
size Telescope
tel))
              (Term -> Term) -> Term -> Term
forall a b. (a -> b) -> a -> b
$ ConHead -> ConInfo -> Elims -> Term
Con ConHead
c ConInfo
ci (Elims -> Term) -> Elims -> Term
forall a b. (a -> b) -> a -> b
$ VerboseLevel -> Elims -> Elims
forall a. VerboseLevel -> [a] -> [a]
take (Elims -> VerboseLevel
forall a. [a] -> VerboseLevel
forall (t :: * -> *) a. Foldable t => t a -> VerboseLevel
length Elims
vs) Elims
vs2
          Term
_ -> TCM Term
forall a. HasCallStack => a
__IMPOSSIBLE__

      Lit Literal
l -> do
        lt <- Literal -> TCM Type
forall (m :: * -> *).
(HasBuiltins m, MonadError TCErr m, MonadTCEnv m, ReadTCState m) =>
Literal -> m Type
litType Literal
l
        compareType cmp lt t
        return $ Lit l

      Lam ArgInfo
ai Abs Term
vb -> do
        (a, absb, bnd) <- Type -> TCMT IO (Dom Type, Abs Type, Boundary)
forall (m :: * -> *).
(PureTCM m, MonadBlock m, MonadTCError m) =>
Type -> m (Dom Type, Abs Type, Boundary)
shouldBePiOrPath Type
t
        ai <- checkArgInfo action ai $ domInfo a
        let name = [Suggestion] -> ShortText
suggests [ Abs Term -> Suggestion
forall a. Suggest a => a -> Suggestion
Suggestion Abs Term
vb , Abs Type -> Suggestion
forall a. Suggest a => a -> Suggestion
Suggestion Abs Type
absb ]

        addContext (name, a) $ do
          let !b = Abs Type -> Type
forall a. Subst a => Abs a -> a
absBody Abs Type
absb
          bd <- checkInternal' action (absBody vb) cmp b
            >>= equalTermToBoundary b bnd
          pure $ Lam ai $ Abs (absName vb) bd

      Pi Dom Type
a Abs Type
b -> do
        s <- Type -> TCMT IO (Sort' Term)
forall (m :: * -> *).
(PureTCM m, MonadBlock m, MonadError TCErr m) =>
Type -> m (Sort' Term)
shouldBeSort Type
t
        reportSDoc "tc.check.internal" 30 $ "pi type should have sort" <+> prettyTCM s
        let sa  = Dom Type -> Sort' Term
forall a. LensSort a => a -> Sort' Term
getSort Dom Type
a
            sb  = Type -> Sort' Term
forall a. LensSort a => a -> Sort' Term
getSort (Abs Type -> Type
forall a. Abs a -> a
unAbs Abs Type
b)
            mkDom Term
v = Sort' Term -> Term -> Type
forall t a. Sort' t -> a -> Type'' t a
El Sort' Term
sa Term
v Type -> Dom Type -> Dom Type
forall a b. a -> Dom' Term b -> Dom' Term a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Dom Type
a
            mkRng Term
v = (Type -> Type) -> Abs Type -> Abs Type
forall a b. (a -> b) -> Abs a -> Abs b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Term
v Term -> Type -> Type
forall a b. a -> Type'' Term b -> Type'' Term a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$) Abs Type
b
            -- Preserve NoAbs
            goInside = case Abs Type
b of
              Abs{}   -> (ShortText, Dom Type) -> TCM Term -> TCM Term
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
forall (m :: * -> *) a.
MonadAddContext m =>
(ShortText, Dom Type) -> m a -> m a
addContext (Abs Type -> ShortText
forall a. Abs a -> ShortText
absName Abs Type
b, Dom Type
a)
              NoAbs{} -> TCM Term -> TCM Term
forall a. a -> a
id
        a <- mkDom <$> checkInternal' action (unEl $ unDom a) CmpLeq (sort sa)
        v' <- goInside $ Pi a . mkRng <$> checkInternal' action (unEl $ unAbs b) CmpLeq (sort sb)
        s' <- sortOf v -- Issue #6205: do not use v' since it might not be valid syntax
        reportSDoc "tc.check.internal" 30 $
          "checking if sort" <+> prettyTCM s' <+>
          "of pi type" <+> prettyTCM v <+>
          "fits in given sort" <+> prettyTCM s
        compareSort cmp s' s
        return v'

      Sort Sort' Term
s -> do
        VerboseKey -> VerboseLevel -> TCMT IO Doc -> TCM ()
forall (m :: * -> *).
MonadDebug m =>
VerboseKey -> VerboseLevel -> TCMT IO Doc -> m ()
reportSDoc VerboseKey
"tc.check.internal" VerboseLevel
30 (TCMT IO Doc -> TCM ()) -> TCMT IO Doc -> TCM ()
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"checking sort" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Sort' Term -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
forall (m :: * -> *). MonadPretty m => Sort' Term -> m Doc
prettyTCM Sort' Term
s
        s <- Action -> Sort' Term -> TCMT IO (Sort' Term)
forall a.
(CheckInternal a, TypeOf a ~ ()) =>
Action -> a -> TCMT IO a
inferInternal' Action
action Sort' Term
s
        s' <- inferUnivSort s
        s'' <- shouldBeSort t
        reportSDoc "tc.check.internal" 30 $
          "checking if sort" <+> prettyTCM s' <+>
          "of universe type" <+> prettyTCM v <+>
          "fits in" <+> prettyTCM t
        compareSort cmp s' s''
        return $ Sort s

      Level Level
l -> do
        l <- Action -> Level -> TCM Level
forall a.
(CheckInternal a, TypeOf a ~ ()) =>
Action -> a -> TCMT IO a
inferInternal' Action
action Level
l
        lt <- levelType'
        compareType cmp lt t
        return $ Level l

      DontCare Term
v -> Term -> Term
DontCare (Term -> Term) -> TCM Term -> TCM Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Action -> Term -> Comparison -> TypeOf Term -> TCM Term
forall a.
CheckInternal a =>
Action -> a -> Comparison -> TypeOf a -> TCMT IO a
checkInternal' Action
action Term
v Comparison
cmp Type
TypeOf Term
t

      -- Jesper, 2023-02-23: these can appear because of eta-expansion of
      -- records with irrelevant fields
      Dummy DummyTermKind
s Elims
_ -> Term -> TCM Term
forall a. a -> TCMT IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Term
v -- __IMPOSSIBLE_VERBOSE__ s

-- | @checkArgInfo actual expected@.
--
--   The @expected@ 'ArgInfo' comes from the type.
--   The @actual@ 'ArgInfo' comes from the term and can be updated
--   by an action.
checkArgInfo :: Action -> ArgInfo -> ArgInfo -> TCM ArgInfo
checkArgInfo :: Action -> ArgInfo -> ArgInfo -> TCM ArgInfo
checkArgInfo Action
action ArgInfo
ai ArgInfo
ai' = do
  Hiding -> Hiding -> TCM ()
checkHiding (ArgInfo -> Hiding
forall a. LensHiding a => a -> Hiding
getHiding ArgInfo
ai) (ArgInfo -> Hiding
forall a. LensHiding a => a -> Hiding
getHiding ArgInfo
ai')
  ArgInfo -> TCM ArgInfo
forall a. a -> TCMT IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ArgInfo
ai

checkHiding :: Hiding -> Hiding -> TCM ()
checkHiding :: Hiding -> Hiding -> TCM ()
checkHiding Hiding
h Hiding
h' = Bool -> TCM () -> TCM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Hiding -> Hiding -> Bool
forall a b. (LensHiding a, LensHiding b) => a -> b -> Bool
sameHiding Hiding
h Hiding
h') (TCM () -> TCM ()) -> TCM () -> TCM ()
forall a b. (a -> b) -> a -> b
$ TypeError -> TCM ()
forall (m :: * -> *) e a.
(HasCallStack, MonadTCError m, Diagnostic e) =>
e -> m a
typeError (TypeError -> TCM ()) -> TypeError -> TCM ()
forall a b. (a -> b) -> a -> b
$ Hiding -> Hiding -> TypeError
HidingMismatch Hiding
h Hiding
h'

-- | Infer type of a neutral term.
infer :: Term -> TCM Type
infer :: Term -> TCM Type
infer Term
u = do
  VerboseKey -> VerboseLevel -> TCMT IO Doc -> TCM ()
forall (m :: * -> *).
MonadDebug m =>
VerboseKey -> VerboseLevel -> TCMT IO Doc -> m ()
reportSDoc VerboseKey
"tc.check.internal" VerboseLevel
20 (TCMT IO Doc -> TCM ()) -> TCMT IO Doc -> TCM ()
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"CheckInternal.infer" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Term -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
forall (m :: * -> *). MonadPretty m => Term -> m Doc
prettyTCM Term
u
  case Term
u of
    Var VerboseLevel
i Elims
es -> do
      a <- VerboseLevel -> TCM Type
forall (m :: * -> *).
(MonadDebug m, MonadTCEnv m) =>
VerboseLevel -> m Type
typeOfBV VerboseLevel
i
      view _1 <$> inferSpine defaultAction (a, Var i) es
    Def QName
f Elims
es -> do
      TCMT IO (Maybe Projection) -> (Projection -> TCM ()) -> TCM ()
forall (m :: * -> *) a.
Monad m =>
m (Maybe a) -> (a -> m ()) -> m ()
whenJustM (QName -> TCMT IO (Maybe Projection)
forall (m :: * -> *).
HasConstInfo m =>
QName -> m (Maybe Projection)
isProjection QName
f) ((Projection -> TCM ()) -> TCM ())
-> (Projection -> TCM ()) -> TCM ()
forall a b. (a -> b) -> a -> b
$ \Projection
_ -> TCM ()
forall (m :: * -> *) a. MonadDebug m => m a
nonInferable
      a <- Definition -> Type
defType (Definition -> Type) -> TCMT IO Definition -> TCM Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QName -> TCMT IO Definition
forall (m :: * -> *).
(HasConstInfo m, HasCallStack) =>
QName -> m Definition
getConstInfo QName
f
      view _1 <$> inferSpine defaultAction (a, Def f) es
    MetaV MetaId
x Elims
es -> do -- we assume meta instantiations to be well-typed
      a <- MetaId -> TCM Type
forall (m :: * -> *). ReadTCState m => MetaId -> m Type
getMetaType MetaId
x
      view _1 <$> inferSpine defaultAction (a, MetaV x) es
    Term
_ -> TCM Type
forall (m :: * -> *) a. MonadDebug m => m a
nonInferable
  where
    nonInferable :: MonadDebug m => m a
    nonInferable :: forall (m :: * -> *) a. MonadDebug m => m a
nonInferable = VerboseKey -> m a
forall (m :: * -> *) a.
(HasCallStack, MonadDebug m) =>
VerboseKey -> m a
__IMPOSSIBLE_VERBOSE__ (VerboseKey -> m a) -> VerboseKey -> m a
forall a b. (a -> b) -> a -> b
$ [VerboseKey] -> VerboseKey
unlines
      [ VerboseKey
"CheckInternal.infer: non-inferable term:"
      , VerboseKey
"  " VerboseKey -> VerboseKey -> VerboseKey
forall a. [a] -> [a] -> [a]
++ Term -> VerboseKey
forall a. Pretty a => a -> VerboseKey
prettyShow Term
u
      ]
instance CheckInternal Elims where
  checkInternal' :: Action -> Elims -> Comparison -> TypeOf Elims -> TCMT IO Elims
checkInternal' Action
action Elims
es Comparison
cmp TypeOf Elims
thd = Getting Elims (Type, Elims, Term) Elims
-> (Type, Elims, Term) -> Elims
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Elims (Type, Elims, Term) Elims
forall s t a b. Field2 s t a b => Lens s t a b
Lens (Type, Elims, Term) (Type, Elims, Term) Elims Elims
_2 ((Type, Elims, Term) -> Elims)
-> TCMT IO (Type, Elims, Term) -> TCMT IO Elims
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Action -> TypeOf Elims -> Elims -> TCMT IO (Type, Elims, Term)
inferSpine Action
action TypeOf Elims
thd Elims
es

-- | The data necessary to 'checkInternal' a spine of eliminations.
--
-- Checking elimination spines is somewhat complicated because type
-- dependency requires that we check later eliminations against
-- /pre/transformed versions of the earlier eliminations, but the use of
-- 'checkInternal' for parameter reconstruction forbids us from using
-- the /post/transformed versions of later eliminations, since these
-- might have prefix proper projections.
--
-- Therefore, we need to keep track of two different "heads": one
-- constructs a legitimate term that can be given to the rest of the
-- elaborator, while the other constructs the output of 'checkInternal'
-- (which might be somewhat illegal). The fields that are guaranteed to
-- have types that can be operated on are labelled "csIn".
--
-- We can't simply check the eliminations with a blanked out post-action
-- then apply the post-action since we would only do that at the head of
-- the term, but the post-action needs to be applied everywhere.
-- Instead, the 'Action' type has a field for undoing the post-action
-- whenever this is relevant.
data CheckSpineState = CSState
  { CheckSpineState -> Type
csInType  :: Type
    -- ^ The type of the head applied to no eliminations.
  , CheckSpineState -> Type -> Elims -> TCM Term
csInHead  :: Type -> Elims -> TCM Term
    -- ^ Construct the head applied to some eliminations, possibly
    -- imposing boundary constraints on the arguments.
    --
    -- The eliminations given here have are all legitimate terms, and
    -- will have had the post-traversal undone.
  , CheckSpineState -> Elims -> Term
csOutHead :: Elims -> Term
    -- ^ Construct the output of the internal checking traversal.
    --
    -- The eliminations given here are subject to the post-traversal.
  }

pureCSS :: Type -> (Elims -> Term) -> CheckSpineState
pureCSS :: Type -> (Elims -> Term) -> CheckSpineState
pureCSS Type
ty Elims -> Term
hd = Type
-> (Type -> Elims -> TCM Term)
-> (Elims -> Term)
-> CheckSpineState
CSState Type
ty (\Type
_ Elims
es -> Term -> TCM Term
forall a. a -> TCMT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Term -> TCM Term) -> Term -> TCM Term
forall a b. (a -> b) -> a -> b
$! Elims -> Term
hd Elims
es) Elims -> Term
hd

-- | Apply an elimination (that has already been 'checkInternal'd) to a
-- 'CheckSpineState', producing the type in the resulting state from the
-- given abstraction.
--
-- This function takes care to put only legitimate terms in the "in"
-- positions of the 'CheckSpineState'.
appCSS :: Action -> CheckSpineState -> Abs Type -> Elim -> TCM CheckSpineState
appCSS :: Action
-> CheckSpineState -> Abs Type -> Elim -> TCM CheckSpineState
appCSS Action
action CheckSpineState
incss Abs Type
b Elim
e = case Elim
e of
  Apply (Arg ArgInfo
ai Term
v) -> do
    t_v <- Action -> Term -> TCM Term
dropParamsAction Action
action Term
v
    pure CSState
      { csInType  = b `absApp` t_v
      , csInHead  = \Type
ty Elims
es -> CheckSpineState -> Type -> Elims -> TCM Term
csInHead  CheckSpineState
incss Type
ty (Arg Term -> Elim
forall a. Arg a -> Elim' a
Apply (ArgInfo -> Term -> Arg Term
forall e. ArgInfo -> e -> Arg e
Arg ArgInfo
ai Term
t_v)Elim -> Elims -> Elims
forall a. a -> [a] -> [a]
:Elims
es)
      , csOutHead = \Elims
es    -> CheckSpineState -> Elims -> Term
csOutHead CheckSpineState
incss    (Elim
eElim -> Elims -> Elims
forall a. a -> [a] -> [a]
:Elims
es)
      }
  IApply Term
x Term
y Term
r -> do
    t_x <- Action -> Term -> TCM Term
dropParamsAction Action
action Term
x
    t_y <- dropParamsAction action x
    t_r <- dropParamsAction action r
    pure CSState
      { csInType  = b `absApp` t_r
      , csInHead  = \Type
ty Elims
es -> CheckSpineState -> Type -> Elims -> TCM Term
csInHead  CheckSpineState
incss Type
ty (Term -> Term -> Term -> Elim
forall a. a -> a -> a -> Elim' a
IApply Term
t_x Term
t_y Term
t_rElim -> Elims -> Elims
forall a. a -> [a] -> [a]
:Elims
es)
      , csOutHead = \Elims
es    -> CheckSpineState -> Elims -> Term
csOutHead CheckSpineState
incss    (Elim
eElim -> Elims -> Elims
forall a. a -> [a] -> [a]
:Elims
es)
      }
  Proj{} -> TCM CheckSpineState
forall a. HasCallStack => a
__IMPOSSIBLE__

inferSpine :: Action -> TypeOf Elims -> Elims -> TCM (Type, Elims, Term)
inferSpine :: Action -> TypeOf Elims -> Elims -> TCMT IO (Type, Elims, Term)
inferSpine Action
act (Type
t, Elims -> Term
hd) Elims
es = Action -> CheckSpineState -> Elims -> TCMT IO (Type, Elims, Term)
inferSpine' Action
act (Type -> (Elims -> Term) -> CheckSpineState
pureCSS Type
t Elims -> Term
hd) Elims
es

-- | Like 'checkSpine'', but without imposing side-conditions on the
-- eliminations.
checkSpine
  :: Action
  -> TypeOf Elims
  -- ^ Data necessary to type the eliminations i.e. the type of the head
  -- and a function to build it (without checking any side-conditions it
  -- might impose).
  -> Elims
  -- ^ The eliminations @es@.
  -> Comparison
  -- ^ Check (@CmpLeq@) or infer (@CmpEq@) the final type.
  -> Type
  -- ^ Expected type of the application @self es@.
  -> TCM Term -- ^ The application after modification by the @Action@.
checkSpine :: Action -> TypeOf Elims -> Elims -> Comparison -> Type -> TCM Term
checkSpine Action
act (Type
t, Elims -> Term
hd) Elims
es Comparison
cmp Type
ty = Action
-> CheckSpineState -> Elims -> Comparison -> Type -> TCM Term
checkSpine' Action
act (Type -> (Elims -> Term) -> CheckSpineState
pureCSS Type
t Elims -> Term
hd) Elims
es Comparison
cmp Type
ty

-- | @'inferSpine'' action state es@ checks that a given initialised
-- 'CheckSpineState' can be applied to some eliminations, returning the
-- eliminated type, the transformed eliminations, and the transformed
-- term.
inferSpine'
  :: Action
  -- ^ How to transform terms on the way in and out.
  -> CheckSpineState
  -- ^ The state to check against.
  -> Elims
  -- ^ The eliminations to check.
  -> TCM (Type, Elims, Term)
  -- ^ Return: the type, the checked and transformed eliminations, and
  -- the transformed term.
inferSpine' :: Action -> CheckSpineState -> Elims -> TCMT IO (Type, Elims, Term)
inferSpine' Action
action CheckSpineState
css Elims
es = CheckSpineState
-> (Elims -> Elims) -> Elims -> TCMT IO (Type, Elims, Term)
loop CheckSpineState
css Elims -> Elims
forall a. a -> a
id Elims
es where
  loop
    :: CheckSpineState   -- accumulating type and pretransformed term (legal)
    -> (Elims -> Elims)  -- accumulating transformed eliminations (in the right order)
    -> Elims             -- input eliminations
    -> TCM (Type, Elims, Term)
  loop :: CheckSpineState
-> (Elims -> Elims) -> Elims -> TCMT IO (Type, Elims, Term)
loop css :: CheckSpineState
css@CSState{ csInType :: CheckSpineState -> Type
csInType = Type
t, csInHead :: CheckSpineState -> Type -> Elims -> TCM Term
csInHead = Type -> Elims -> TCM Term
hdIn, csOutHead :: CheckSpineState -> Elims -> Term
csOutHead = Elims -> Term
hdOut } Elims -> Elims
acc [] = do
    -- this incantation is kinda funny, but we need to apply the in head
    -- to check constraints on the head before proceeding (which must
    -- happen with the param-dropped version of the values, since they
    -- will be conversion-checked)
    _ <- Type -> Elims -> TCM Term
hdIn Type
t []
    -- now we can build the output term and output accumulations:
    let
      !v  = Elims -> Term
hdOut []
      !es = Elims -> Elims
acc []
    return (t, es, v)

  loop css :: CheckSpineState
css@CSState{ csInType :: CheckSpineState -> Type
csInType = Type
t, csInHead :: CheckSpineState -> Type -> Elims -> TCM Term
csInHead = Type -> Elims -> TCM Term
hdIn, csOutHead :: CheckSpineState -> Elims -> Term
csOutHead = Elims -> Term
hdOut } Elims -> Elims
acc (Elim
e:Elims
es) = do
    VerboseKey -> VerboseLevel -> TCMT IO Doc -> TCM ()
forall (m :: * -> *).
MonadDebug m =>
VerboseKey -> VerboseLevel -> TCMT IO Doc -> m ()
reportSDoc VerboseKey
"tc.check.internal" VerboseLevel
30 (TCMT IO Doc -> TCM ()) -> TCMT IO Doc -> TCM ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
      [ TCMT IO Doc
"inferring spine: "
      , TCMT IO Doc
"type t = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Type -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
forall (m :: * -> *). MonadPretty m => Type -> m Doc
prettyTCM Type
t
      , TCMT IO Doc
"self v = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Term -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
forall (m :: * -> *). MonadPretty m => Term -> m Doc
prettyTCM (Elims -> Term
hdOut [])
      , TCMT IO Doc
"eliminated by e = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Elim -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
forall (m :: * -> *). MonadPretty m => Elim -> m Doc
prettyTCM Elim
e
      ]

    case Elim
e of
      -- case: application
      Apply (Arg ArgInfo
ai Term
v) -> do
        (a, b) <- Type -> TCMT IO (Dom Type, Abs Type)
forall (m :: * -> *).
(PureTCM m, MonadBlock m, MonadTCError m) =>
Type -> m (Dom Type, Abs Type)
shouldBePi Type
t
        ai <- checkArgInfo action ai $ domInfo a
        v <- checkInternal' action v CmpLeq $ unDom a
        let e' = Arg Term -> Elim
forall a. Arg a -> Elim' a
Apply (ArgInfo -> Term -> Arg Term
forall e. ArgInfo -> e -> Arg e
Arg ArgInfo
ai Term
v)
        css <- appCSS action css b e'
        loop css (acc . (e':)) es

      -- case: interval application. here we also check that the stored
      -- endpoints have the correct types, not just that the argument is
      -- of interval type.
      IApply Term
x Term
y Term
r -> do
        (a, b) <- Type -> TCMT IO (Dom Type, Abs Type)
forall (m :: * -> *).
(PureTCM m, MonadBlock m, MonadTCError m) =>
Type -> m (Dom Type, Abs Type)
shouldBePath Type
t
        r <- checkInternal' action r CmpLeq (unDom a)
        x <- checkInternal' action x CmpLeq . absApp b =<< primIZero
        y <- checkInternal' action y CmpLeq . absApp b =<< primIOne
        let e' = Term -> Term -> Term -> Elim
forall a. a -> a -> a -> Elim' a
IApply Term
x Term
y Term
r
        css <- appCSS action css b e'
        loop css (acc . (e':)) es

      -- case: projection or projection-like.
      Proj ProjOrigin
o QName
f -> do
        -- invert the accumulations built so far, checking any
        -- constraint imposed by the head on its arguments.
        self <- Type -> Elims -> TCM Term
hdIn Type
t []
        reportSDoc "tc.check.internal" 30 $ sep
          [ "inferring spine: "
          , "type t = " <+> prettyTCM t
          , "self v = " <+> prettyTCM self
          , "eliminated by e = " <+> prettyTCM e
          ]
        t' <- shouldBeProjectible self t o f
        proj <- fromMaybe __IMPOSSIBLE__ <$> isProjection f
        let
          -- the rest of the type-checker can't cope with
          -- projection-like functions in spines, but checkInternal puts
          -- them there so there's always a "head" to infer.
          --
          -- we'll now have discharged the head constraints and /might/
          -- need to change what is the head, if the thing on the spine
          -- was actually projection-like. this is handled by
          -- shouldBeProjectible (through projectTyped -> applyDef).
          projHd Term
self Elims
es = if Projection -> Bool
isProperProjection_ Projection
proj
            then Term
self Term -> Elims -> Term
forall t. Apply t => t -> Elims -> t
`applyE` (Elim
eElim -> Elims -> Elims
forall a. a -> [a] -> [a]
:Elims
es)
            else QName -> Elims -> Term
Def QName
f (Arg Term -> Elim
forall a. Arg a -> Elim' a
Apply (Projection -> Arg QName
projFromType Projection
proj Arg QName -> Term -> Arg Term
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Term
self)Elim -> Elims -> Elims
forall a. a -> [a] -> [a]
:Elims
es)

          st' = CSState
            { csInType :: Type
csInType  = Type
t'
            , csInHead :: Type -> Elims -> TCM Term
csInHead  = \Type
_ Elims
es' -> Term -> TCM Term
forall a. a -> TCMT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Term -> TCM Term) -> Term -> TCM Term
forall a b. (a -> b) -> a -> b
$! Term -> Elims -> Term
projHd Term
self Elims
es'
            , csOutHead :: Elims -> Term
csOutHead = \Elims
es'   -> Term -> Elims -> Term
projHd (Elims -> Term
hdOut []) Elims
es'
            }
        loop st' (acc . (e:)) es

checkSpine'
  :: Action
  -> CheckSpineState
  -- ^ Data necessary to type the eliminations i.e. the type of the head
  -- and a function to build it (without checking any side-conditions it
  -- might impose).
  -> Elims
  -- ^ The eliminations @es@.
  -> Comparison
  -- ^ Check (@CmpLeq@) or infer (@CmpEq@) the final type.
  -> Type
  -- ^ Expected type of the application @self es@.
  -> TCM Term -- ^ The application after modification by the @Action@.
checkSpine' :: Action
-> CheckSpineState -> Elims -> Comparison -> Type -> TCM Term
checkSpine' Action
action CheckSpineState
css Elims
es Comparison
cmp Type
t = do
  VerboseKey -> VerboseLevel -> TCMT IO Doc -> TCM ()
forall (m :: * -> *).
MonadDebug m =>
VerboseKey -> VerboseLevel -> TCMT IO Doc -> m ()
reportSDoc VerboseKey
"tc.check.internal" VerboseLevel
20 (TCMT IO Doc -> TCM ()) -> TCMT IO Doc -> TCM ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
    [ TCMT IO Doc
"checking spine "
    , VerboseLevel -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => VerboseLevel -> m Doc -> m Doc
nest VerboseLevel
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep [ TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => m Doc -> m Doc
parens (Type -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
forall (m :: * -> *). MonadPretty m => Type -> m Doc
prettyTCM (CheckSpineState -> Type
csInType CheckSpineState
css))
                   , VerboseLevel -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => VerboseLevel -> m Doc -> m Doc
nest VerboseLevel
4 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ Elims -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
forall (m :: * -> *). MonadPretty m => Elims -> m Doc
prettyTCM Elims
es TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> TCMT IO Doc
":"
                   , VerboseLevel -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => VerboseLevel -> m Doc -> m Doc
nest VerboseLevel
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ Type -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
forall (m :: * -> *). MonadPretty m => Type -> m Doc
prettyTCM Type
t ] ]
  (t', _, tm) <- Action -> CheckSpineState -> Elims -> TCMT IO (Type, Elims, Term)
inferSpine' Action
action CheckSpineState
css Elims
es
  reportSDoc "tc.check.internal" 30 $ sep
    [ "checking if "
    , prettyTCM t'
    , "is a subtype of"
    , prettyTCM t
    , "tm = " <+> pretty tm
    ]
  tm <$ compareType cmp t' t

instance CheckInternal Sort where
  checkInternal' :: Action
-> Sort' Term
-> Comparison
-> TypeOf (Sort' Term)
-> TCMT IO (Sort' Term)
checkInternal' Action
action Sort' Term
s Comparison
cmp TypeOf (Sort' Term)
_ = case Sort' Term
s of
    Univ Univ
u Level
l -> Univ -> Level -> Sort' Term
forall t. Univ -> Level' t -> Sort' t
Univ Univ
u (Level -> Sort' Term) -> TCM Level -> TCMT IO (Sort' Term)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Action -> Level -> TCM Level
forall a.
(CheckInternal a, TypeOf a ~ ()) =>
Action -> a -> TCMT IO a
inferInternal' Action
action Level
l
    Inf Univ
u Integer
n  -> Sort' Term -> TCMT IO (Sort' Term)
forall a. a -> TCMT IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Sort' Term -> TCMT IO (Sort' Term))
-> Sort' Term -> TCMT IO (Sort' Term)
forall a b. (a -> b) -> a -> b
$ Univ -> Integer -> Sort' Term
forall t. Univ -> Integer -> Sort' t
Inf Univ
u Integer
n
    Sort' Term
LevelUniv -> Sort' Term -> TCMT IO (Sort' Term)
forall a. a -> TCMT IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Sort' Term
forall t. Sort' t
LevelUniv
    Sort' Term
CofUniv   -> Sort' Term -> TCMT IO (Sort' Term)
forall a. a -> TCMT IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Sort' Term
forall t. Sort' t
CofUniv
    Sort' Term
IntervalUniv -> Sort' Term -> TCMT IO (Sort' Term)
forall a. a -> TCMT IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Sort' Term
forall t. Sort' t
IntervalUniv
    PiSort Dom' Term Term
dom Sort' Term
s1 Abs (Sort' Term)
s2 -> do
      let a :: Term
a = Dom' Term Term -> Term
forall t e. Dom' t e -> e
unDom Dom' Term Term
dom
      s1' <- Action -> Sort' Term -> TCMT IO (Sort' Term)
forall a.
(CheckInternal a, TypeOf a ~ ()) =>
Action -> a -> TCMT IO a
inferInternal' Action
action Sort' Term
s1
      a' <- checkInternal' action a CmpLeq $ sort s1'
      let dom' = Dom' Term Term
dom Dom' Term Term -> Term -> Dom' Term Term
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Term
a'
      s2' <- mapAbstraction (El s1' <$> dom') (inferInternal' action) s2
      return $ PiSort dom' s1' s2'
    FunSort Sort' Term
s1 Sort' Term
s2 -> do
      s1' <- Action -> Sort' Term -> TCMT IO (Sort' Term)
forall a.
(CheckInternal a, TypeOf a ~ ()) =>
Action -> a -> TCMT IO a
inferInternal' Action
action Sort' Term
s1
      s2' <- inferInternal' action s2
      return $ FunSort s1' s2'
    UnivSort Sort' Term
s -> Sort' Term -> Sort' Term
forall t. Sort' t -> Sort' t
UnivSort (Sort' Term -> Sort' Term)
-> TCMT IO (Sort' Term) -> TCMT IO (Sort' Term)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Action -> Sort' Term -> TCMT IO (Sort' Term)
forall a.
(CheckInternal a, TypeOf a ~ ()) =>
Action -> a -> TCMT IO a
inferInternal' Action
action Sort' Term
s
    MetaS MetaId
x Elims
es -> do -- we assume sort meta instantiations to be well-formed
      a <- MetaId -> TCM Type
forall (m :: * -> *). ReadTCState m => MetaId -> m Type
getMetaType MetaId
x
      inferSpine action (a, Sort . MetaS x) es >>= \case
        (Type
_, Elims
_, Sort Sort' Term
s) -> Sort' Term -> TCMT IO (Sort' Term)
forall a. a -> TCMT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort' Term
s
        (Type
_, Elims
_, Term
t)      -> TypeError -> TCMT IO (Sort' Term)
forall (m :: * -> *) e a.
(HasCallStack, MonadTCError m, Diagnostic e) =>
e -> m a
typeError (TypeError -> TCMT IO (Sort' Term))
-> TypeError -> TCMT IO (Sort' Term)
forall a b. (a -> b) -> a -> b
$ Type -> TypeError
ShouldBeASort (Sort' Term -> Term -> Type
forall t a. Sort' t -> a -> Type'' t a
El Sort' Term
HasCallStack => Sort' Term
__DUMMY_SORT__ Term
t)
    DefS QName
d Elims
es -> do
      a <- Definition -> Type
defType (Definition -> Type) -> TCMT IO Definition -> TCM Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QName -> TCMT IO Definition
forall (m :: * -> *).
(HasConstInfo m, HasCallStack) =>
QName -> m Definition
getConstInfo QName
d
      inferSpine action (a, Sort . DefS d) es >>= \case
        (Type
_, Elims
_, Sort Sort' Term
s) -> Sort' Term -> TCMT IO (Sort' Term)
forall a. a -> TCMT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort' Term
s
        (Type
_, Elims
_, Term
t)      -> TypeError -> TCMT IO (Sort' Term)
forall (m :: * -> *) e a.
(HasCallStack, MonadTCError m, Diagnostic e) =>
e -> m a
typeError (TypeError -> TCMT IO (Sort' Term))
-> TypeError -> TCMT IO (Sort' Term)
forall a b. (a -> b) -> a -> b
$ Type -> TypeError
ShouldBeASort (Sort' Term -> Term -> Type
forall t a. Sort' t -> a -> Type'' t a
El Sort' Term
HasCallStack => Sort' Term
__DUMMY_SORT__ Term
t)
    DummyS VerboseKey
s -> VerboseKey -> TCMT IO (Sort' Term)
forall (m :: * -> *) a.
(HasCallStack, MonadDebug m) =>
VerboseKey -> m a
__IMPOSSIBLE_VERBOSE__ VerboseKey
s

instance CheckInternal Level where
  checkInternal' :: Action -> Level -> Comparison -> TypeOf Level -> TCM Level
checkInternal' Action
action (Max Integer
n [PlusLevel' Term]
ls) Comparison
_ TypeOf Level
_ = Integer -> [PlusLevel' Term] -> Level
forall t. Integer -> [PlusLevel' t] -> Level' t
Max Integer
n ([PlusLevel' Term] -> Level)
-> TCMT IO [PlusLevel' Term] -> TCM Level
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (PlusLevel' Term -> TCMT IO (PlusLevel' Term))
-> [PlusLevel' Term] -> TCMT IO [PlusLevel' Term]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Action -> PlusLevel' Term -> TCMT IO (PlusLevel' Term)
forall a.
(CheckInternal a, TypeOf a ~ ()) =>
Action -> a -> TCMT IO a
inferInternal' Action
action) [PlusLevel' Term]
ls

instance CheckInternal PlusLevel where
  checkInternal' :: Action
-> PlusLevel' Term
-> Comparison
-> TypeOf (PlusLevel' Term)
-> TCMT IO (PlusLevel' Term)
checkInternal' Action
action (Plus Integer
k Term
l) Comparison
_ TypeOf (PlusLevel' Term)
_ = Integer -> Term -> PlusLevel' Term
forall t. Integer -> t -> PlusLevel' t
Plus Integer
k (Term -> PlusLevel' Term) -> TCM Term -> TCMT IO (PlusLevel' Term)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Term -> TCM Term
checkLevelAtom Term
l
    where
    checkLevelAtom :: Term -> TCM Term
checkLevelAtom Term
l = do
      lvl <- TCM Type
forall (m :: * -> *). HasBuiltins m => m Type
levelType'
      checkInternal' action l CmpLeq lvl