{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE LambdaCase          #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE PatternSynonyms     #-}
{-# LANGUAGE RankNTypes          #-}
{-# LANGUAGE ScopedTypeVariables #-}

-- | Diagnostics for declarations outside the RSTT fragment.
--
-- For RSTT, see Riehl and Shulman,
-- <https://arxiv.org/abs/1705.07442 RS17> (2017), Figure 4 and Appendix A.2 for extension types.
-- For the fragment conditions, see Kudasov, Sim and Ahrens,
-- <https://arxiv.org/abs/2607.12207 KSA26> (2026), §5.
--
-- * Free-standing restrictions in assumptions, motives, and types passed as data.
-- * Cube domains, shape conditions, and assumed boundaries depending on outer points.
-- * Schematic variables bound inside terms below the parameter prefix.
-- * Modal constructs, auxiliary intervals, and involutions.
-- * Inductive constructors and eliminators.
-- * Any syntax outside the explicit RSTT allow-list.
--
-- Free-standing restrictions along concluded codomains are allowed.
-- Assumptions and postulates remain trusted; universe levels are not checked.
module Rzk.TypeCheck.Fragment.RSTT (
  fragmentHead,
  recordFragmentUses,
  recordSyntaxUses,
) where

import           Control.Applicative      ((<|>))
import           Control.Monad            (forM_, unless, when)
import           Control.Monad.Except     (catchError)
import           Control.Monad.Reader     (asks, local)
import           Data.Bifoldable          (bifoldMap)
import           Data.List                (intercalate)
import           Data.Maybe               (fromMaybe)
import qualified Data.IntSet              as IntSet
import qualified Control.Monad.Foil       as Foil

import           Control.Monad.Foil       (Distinct)
import           Control.Monad.Free.Foil  (AST (Node, Var), ScopedAST (..))

import           Control.Monad.Free.Foil.Annotated (AnnSig (..))
import           Language.Rzk.Foil.Names  (Binder, TModality (..),
                                           TypeInfo (..), VarIdent, binderName)
import           Language.Rzk.Foil.Syntax
import           Rzk.TypeCheck.Context
import           Rzk.TypeCheck.Display
import           Rzk.TypeCheck.Eval
import           Rzk.TypeCheck.Error
import           Rzk.TypeCheck.MetaPrefix (isMetaType)
import           Rzk.TypeCheck.Monad

-- Distinguish concluded types, assumed types, and terms (including type-valued data).
data CheckedPosition
  = InTail
  | InAssumption FragmentUse
  | InTerm PrefixPosition

-- A schematic argument may have its own parameter prefix.
data PrefixPosition = InParameterPrefix | OutsideParameterPrefix
  deriving (PrefixPosition -> PrefixPosition -> Bool
(PrefixPosition -> PrefixPosition -> Bool)
-> (PrefixPosition -> PrefixPosition -> Bool) -> Eq PrefixPosition
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PrefixPosition -> PrefixPosition -> Bool
== :: PrefixPosition -> PrefixPosition -> Bool
$c/= :: PrefixPosition -> PrefixPosition -> Bool
/= :: PrefixPosition -> PrefixPosition -> Bool
Eq)

-- | Report restriction and schematic-binder violations in a declaration.
-- Violations are errors when @rstt-safe = "error"@.
recordFragmentUses
  :: forall n. Distinct n
  => VarIdent          -- ^ the declaration
  -> TermT n           -- ^ its type
  -> Maybe (TermT n)   -- ^ its value, for a definition
  -> Bool              -- ^ does it supply an assumption rather than a proof?
  -> TypeCheck n ()
recordFragmentUses :: forall (n :: S).
Distinct n =>
VarIdent -> TermT n -> Maybe (TermT n) -> Bool -> TypeCheck n ()
recordFragmentUses VarIdent
defName TermT n
ty Maybe (TermT n)
mval Bool
isAssumption = do
  restrictions <- (Context n -> Bool)
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Context n -> Bool
forall (n :: S). Context n -> Bool
ctxWarnFreeStandingRestriction
  binders <- asks ctxWarnMetaBinder
  shapes <- asks ctxWarnShapeDependency
  when (restrictions || binders || shapes) $
    localVerbosity Silent $
      forM_ [SourceTypes, ComputedTypes] $ \FragmentView
view ->
        FragmentView
-> VarIdent
-> TermT n
-> Maybe (TermT n)
-> Bool
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S).
Distinct n =>
FragmentView
-> VarIdent -> TermT n -> Maybe (TermT n) -> Bool -> TypeCheck n ()
checkFragmentUses FragmentView
view VarIdent
defName TermT n
ty Maybe (TermT n)
mval Bool
isAssumption ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
-> (TypeErrorInScopedContext
    -> ReaderT
         (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a.
ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (TypeErrorInScopedContext
    -> ReaderT
         (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a)
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
`catchError`
          String
-> TypeErrorInScopedContext
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S).
Distinct n =>
String -> TypeErrorInScopedContext -> TypeCheck n ()
reportIncompleteRSTTCheck (String
"RSTT fragment check incomplete in " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> VarIdent -> String
forall a. Show a => a -> String
show VarIdent
defName)

-- Keep source checks even when reduction discards an argument or annotation.
data FragmentView = SourceTypes | ComputedTypes
  deriving (FragmentView -> FragmentView -> Bool
(FragmentView -> FragmentView -> Bool)
-> (FragmentView -> FragmentView -> Bool) -> Eq FragmentView
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FragmentView -> FragmentView -> Bool
== :: FragmentView -> FragmentView -> Bool
$c/= :: FragmentView -> FragmentView -> Bool
/= :: FragmentView -> FragmentView -> Bool
Eq)

checkFragmentUses
  :: forall n. Distinct n
  => FragmentView -> VarIdent -> TermT n -> Maybe (TermT n) -> Bool -> TypeCheck n ()
checkFragmentUses :: forall (n :: S).
Distinct n =>
FragmentView
-> VarIdent -> TermT n -> Maybe (TermT n) -> Bool -> TypeCheck n ()
checkFragmentUses FragmentView
view VarIdent
defName TermT n
ty Maybe (TermT n)
mval Bool
isAssumption = do
  CheckedPosition -> TermT n -> TypeCheck n ()
forall (l :: S).
Distinct l =>
CheckedPosition -> TermT l -> TypeCheck l ()
go (if Bool
isAssumption then FragmentUse -> CheckedPosition
InAssumption FragmentUse
UseBinder else CheckedPosition
InTail) TermT n
ty
  (TermT n -> TypeCheck n ()) -> Maybe (TermT n) -> TypeCheck n ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (CheckedPosition -> TermT n -> TypeCheck n ()
forall (l :: S).
Distinct l =>
CheckedPosition -> TermT l -> TypeCheck l ()
go (PrefixPosition -> CheckedPosition
InTerm PrefixPosition
InParameterPrefix)) Maybe (TermT n)
mval
  where
    go :: forall l. Distinct l => CheckedPosition -> TermT l -> TypeCheck l ()
    go :: forall (l :: S).
Distinct l =>
CheckedPosition -> TermT l -> TypeCheck l ()
go (InTerm PrefixPosition
prefixPosition) TermT l
t = PrefixPosition -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
prefixPosition TermT l
t
    go CheckedPosition
InTail TermT l
t = TermT l -> TypeCheck l ()
forall (l :: S). Distinct l => TermT l -> TypeCheck l ()
goTail TermT l
t
    go (InAssumption FragmentUse
use) TermT l
t = FragmentUse -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
use TermT l
t

    expose :: forall l. Distinct l => TermT l -> TypeCheck l (TermT l)
    expose :: forall (l :: S). Distinct l => TermT l -> TypeCheck l (TermT l)
expose = case FragmentView
view of
      FragmentView
SourceTypes -> TermT l -> TypeCheck l (TermT l)
forall a.
a
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
      FragmentView
ComputedTypes -> Int -> TermT l -> TypeCheck l (TermT l)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
fragmentHead Int
256

    -- Allow restrictions along the spine of concluded codomains.
    goTail :: forall l. Distinct l => TermT l -> TypeCheck l ()
    goTail :: forall (l :: S). Distinct l => TermT l -> TypeCheck l ()
goTail TermT l
original = TermT l -> TypeCheck l (TermT l)
forall (l :: S). Distinct l => TermT l -> TypeCheck l (TermT l)
expose TermT l
original TypeCheck l (TermT l)
-> (TermT l
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a b.
ReaderT
  (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \TermT l
t -> case TermT l
t of
      TypeRestrictedT TypeInfo (TermT l)
_ TermT l
ty' [(TermT l, TermT l)]
rs -> do
        TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S). Distinct l => TermT l -> TypeCheck l ()
goTail TermT l
ty'
        [(TermT l, TermT l)]
-> ((TermT l, TermT l)
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(TermT l, TermT l)]
rs (((TermT l, TermT l)
  -> ReaderT
       (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
 -> ReaderT
      (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ((TermT l, TermT l)
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a b. (a -> b) -> a -> b
$ \(TermT l
tope, TermT l
term) -> do
          PrefixPosition
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
tope
          PrefixPosition
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
term
      TypeFunT TypeInfo (TermT l)
_ Binder
orig TModality
md TermT l
param Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
mtope ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
ret -> do
        CheckedPosition
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
CheckedPosition -> TermT l -> TypeCheck l ()
checkShapeDependencies CheckedPosition
InTail TermT l
t
        FragmentUse
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
UseBinder TermT l
param
        Binder
-> TModality
-> TermT l
-> Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
Binder
-> TModality -> TermT l -> Maybe (ScopedTermT l) -> TypeCheck l ()
checkDomain Binder
orig TModality
md TermT l
param Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
mtope
        Binder
-> TModality
-> TermT l
-> ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    AST NameBinder (AnnSig TypeInfo TermSig) l -> TypeCheck l ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (sig :: * -> * -> *) (n :: S) a.
(Bifunctor sig, Distinct n) =>
Binder
-> TModality
-> TermT n
-> ScopedAST NameBinder sig n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    AST NameBinder sig l -> TypeCheck l a)
-> TypeCheck n a
inScope Binder
orig TModality
md TermT l
param ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
ret TermT l -> TypeCheck l ()
forall (l :: S).
(DExt l l, Distinct l) =>
AST NameBinder (AnnSig TypeInfo TermSig) l -> TypeCheck l ()
forall (l :: S). Distinct l => TermT l -> TypeCheck l ()
goTail
      RecOrT TypeInfo (TermT l)
_ [(TermT l, TermT l)]
rs -> [(TermT l, TermT l)]
-> ((TermT l, TermT l)
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(TermT l, TermT l)]
rs (((TermT l, TermT l)
  -> ReaderT
       (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
 -> ReaderT
      (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ((TermT l, TermT l)
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a b. (a -> b) -> a -> b
$ \(TermT l
tope, TermT l
term) -> do
        PrefixPosition
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
tope
        TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S). Distinct l => TermT l -> TypeCheck l ()
goTail TermT l
term
      TermT l
_ -> FragmentUse
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
UseConcluded TermT l
t

    -- Assumed restrictions must sit directly under a shape-Π.
    goAssumed :: forall l. Distinct l => FragmentUse -> TermT l -> TypeCheck l ()
    goAssumed :: forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed = PrefixPosition -> FragmentUse -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
PrefixPosition -> FragmentUse -> TermT l -> TypeCheck l ()
goAssumedAt PrefixPosition
OutsideParameterPrefix

    goAssumedAt
      :: forall l. Distinct l
      => PrefixPosition -> FragmentUse -> TermT l -> TypeCheck l ()
    goAssumedAt :: forall (l :: S).
Distinct l =>
PrefixPosition -> FragmentUse -> TermT l -> TypeCheck l ()
goAssumedAt PrefixPosition
prefixPosition FragmentUse
use TermT l
original = TermT l -> TypeCheck l (TermT l)
forall (l :: S). Distinct l => TermT l -> TypeCheck l (TermT l)
expose TermT l
original TypeCheck l (TermT l)
-> (TermT l
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a b.
ReaderT
  (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \TermT l
t -> case TermT l
t of
      TypeRestrictedT TypeInfo (TermT l)
_ TermT l
ty' [(TermT l, TermT l)]
rs -> do
        FragmentUse
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
reportFreeStanding FragmentUse
use TermT l
t
        FragmentUse
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
use TermT l
ty'
        [(TermT l, TermT l)]
-> ((TermT l, TermT l)
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(TermT l, TermT l)]
rs (((TermT l, TermT l)
  -> ReaderT
       (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
 -> ReaderT
      (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ((TermT l, TermT l)
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a b. (a -> b) -> a -> b
$ \(TermT l
tope, TermT l
term) -> do
          PrefixPosition
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
tope
          PrefixPosition
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
term
      TypeFunT TypeInfo (TermT l)
_ Binder
orig TModality
md TermT l
param Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
mtope ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
ret -> do
        CheckedPosition
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
CheckedPosition -> TermT l -> TypeCheck l ()
checkShapeDependencies (FragmentUse -> CheckedPosition
InAssumption FragmentUse
use) TermT l
t
        FragmentUse
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
UseBinder TermT l
param
        Binder
-> TModality
-> TermT l
-> Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
Binder
-> TModality -> TermT l -> Maybe (ScopedTermT l) -> TypeCheck l ()
checkDomain Binder
orig TModality
md TermT l
param Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
mtope
        shape <- Bool -> TermT l -> TypeCheck l Bool
forall (l :: S). Distinct l => Bool -> TermT l -> TypeCheck l Bool
isShapeBinder (Bool
-> (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l -> Bool)
-> Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
-> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Bool -> ScopedAST NameBinder (AnnSig TypeInfo TermSig) l -> Bool
forall a b. a -> b -> a
const Bool
True) Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
mtope) TermT l
param
        inScope orig md param ret $
          if shape then goExtCodomain use else goAssumed use
      TypeSigmaT TypeInfo (TermT l)
_ Binder
orig TModality
md TermT l
a ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
b -> do
        FragmentUse
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
UseBinder TermT l
a
        Binder
-> TModality
-> TermT l
-> ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    AST NameBinder (AnnSig TypeInfo TermSig) l -> TypeCheck l ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (sig :: * -> * -> *) (n :: S) a.
(Bifunctor sig, Distinct n) =>
Binder
-> TModality
-> TermT n
-> ScopedAST NameBinder sig n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    AST NameBinder sig l -> TypeCheck l a)
-> TypeCheck n a
inScope Binder
orig TModality
md TermT l
a ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
b (FragmentUse -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
use)
      -- Identity-type arguments must be ext-style; endpoints remain terms.
      TypeIdT TypeInfo (TermT l)
_ TermT l
a Maybe (TermT l)
mtA TermT l
b -> do
        PrefixPosition
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
a
        (TermT l
 -> ReaderT
      (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> Maybe (TermT l)
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (FragmentUse
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
UseIdentity) Maybe (TermT l)
mtA
        PrefixPosition
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
b
      LambdaT TypeInfo (TermT l)
info Binder
orig Maybe
  (LambdaParam
     (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l) (TermT l))
mparam ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
body -> do
        md <- case Maybe
  (LambdaParam
     (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l) (TermT l))
mparam of
          Just (LambdaParam TModality
md TermT l
param Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
mtope) -> do
            FragmentUse
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
UseBinder TermT l
param
            Binder
-> TModality
-> TermT l
-> Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
Binder
-> TModality -> TermT l -> Maybe (ScopedTermT l) -> TypeCheck l ()
checkDomain Binder
orig TModality
md TermT l
param Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
mtope
            Bool
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (PrefixPosition
prefixPosition PrefixPosition -> PrefixPosition -> Bool
forall a. Eq a => a -> a -> Bool
== PrefixPosition
OutsideParameterPrefix) (ReaderT
   (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
 -> ReaderT
      (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a b. (a -> b) -> a -> b
$ Binder
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S). Distinct l => Binder -> TermT l -> TypeCheck l ()
reportMetaBinder Binder
orig TermT l
param
            TModality
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     TModality
forall a.
a
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TModality
md
          Maybe
  (LambdaParam
     (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l) (TermT l))
Nothing -> TModality
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     TModality
forall a.
a
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TModality
Id
        dom <- binderType info mparam
        inScope orig md dom body (goAssumedAt prefixPosition use)
      -- recordSyntaxUses reports modal syntax; descend here for restriction checks.
      TypeModalT TypeInfo (TermT l)
_ TModality
_ TermT l
ty' -> FragmentUse
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
use TermT l
ty'
      RecOrT TypeInfo (TermT l)
_ [(TermT l, TermT l)]
rs -> [(TermT l, TermT l)]
-> ((TermT l, TermT l)
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(TermT l, TermT l)]
rs (((TermT l, TermT l)
  -> ReaderT
       (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
 -> ReaderT
      (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ((TermT l, TermT l)
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a b. (a -> b) -> a -> b
$ \(TermT l
tope, TermT l
term) -> do
        PrefixPosition
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
tope
        FragmentUse
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
use TermT l
term
      Var{} -> ()
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a.
a
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      TermT l
_ -> PrefixPosition
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
t

    -- Direct shape codomains admit boundary clipping to the binder's domain.
    goExtCodomain
      :: forall l. Distinct l => FragmentUse -> TermT l -> TypeCheck l ()
    goExtCodomain :: forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goExtCodomain FragmentUse
use TermT l
original = TermT l -> TypeCheck l (TermT l)
forall (l :: S). Distinct l => TermT l -> TypeCheck l (TermT l)
expose TermT l
original TypeCheck l (TermT l)
-> (TermT l
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a b.
ReaderT
  (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \TermT l
t -> case TermT l
t of
      TypeRestrictedT TypeInfo (TermT l)
_ TermT l
ty' [(TermT l, TermT l)]
rs -> do
        FragmentUse
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
use TermT l
ty'
        [(TermT l, TermT l)]
-> ((TermT l, TermT l)
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(TermT l, TermT l)]
rs (((TermT l, TermT l)
  -> ReaderT
       (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
 -> ReaderT
      (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ((TermT l, TermT l)
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a b. (a -> b) -> a -> b
$ \(TermT l
face, TermT l
term) -> do
          PrefixPosition
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
face
          PrefixPosition
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
term
      TermT l
_ -> FragmentUse
-> TermT l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
use TermT l
t

    -- Topes may contain applications with their own binders and assumed types.
    checkDomain
      :: forall l. Distinct l
      => Binder -> TModality -> TermT l -> Maybe (ScopedTermT l) -> TypeCheck l ()
    checkDomain :: forall (l :: S).
Distinct l =>
Binder
-> TModality -> TermT l -> Maybe (ScopedTermT l) -> TypeCheck l ()
checkDomain Binder
orig TModality
md TermT l
param =
      (ScopedTermT l
 -> ReaderT
      (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> Maybe (ScopedTermT l)
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\ScopedTermT l
tope -> Binder
-> TModality
-> TermT l
-> ScopedTermT l
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    AST NameBinder (AnnSig TypeInfo TermSig) l -> TypeCheck l ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (sig :: * -> * -> *) (n :: S) a.
(Bifunctor sig, Distinct n) =>
Binder
-> TModality
-> TermT n
-> ScopedAST NameBinder sig n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    AST NameBinder sig l -> TypeCheck l a)
-> TypeCheck n a
inScope Binder
orig TModality
md TermT l
param ScopedTermT l
tope (PrefixPosition
-> AST NameBinder (AnnSig TypeInfo TermSig) l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix))

    -- RS17 Appendix A.2 requires shapes independent of ambient cube points.
    -- Schematic families are judgements over cube contexts, not extension types.
    checkShapeDependencies :: forall l. Distinct l => CheckedPosition -> TermT l -> TypeCheck l ()
    checkShapeDependencies :: forall (l :: S).
Distinct l =>
CheckedPosition -> TermT l -> TypeCheck l ()
checkShapeDependencies CheckedPosition
position TermT l
t = do
      enabled <- (Context l -> Bool)
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Context l -> Bool
forall (n :: S). Context n -> Bool
ctxWarnShapeDependency
      when enabled $ case t of
        TypeFunT TypeInfo (TermT l)
_ Binder
orig TModality
md TermT l
param Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
mtope ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
ret -> do
          schematic <- TermT l
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall (n :: S). Distinct n => TermT n -> TypeCheck n Bool
isMetaType TermT l
t
          shape <- isShapeBinder (maybe False (const True) mtope) param
          when shape $ do
            scope <- asks ctxScope
            let check :: forall k. Distinct k => Foil.Name k -> TermT k -> TermT k -> TypeCheck k ()
                check Name k
point TermT k
cube TermT k
original = do
                  String -> Name k -> TermT k -> TypeCheck k ()
forall (l :: S).
Distinct l =>
String -> Name l -> TermT l -> TypeCheck l ()
reportShapeDependency String
"cube domain" Name k
point TermT k
cube
                  Bool -> TypeCheck k () -> TypeCheck k ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
schematic (TypeCheck k () -> TypeCheck k ())
-> TypeCheck k () -> TypeCheck k ()
forall a b. (a -> b) -> a -> b
$ TermT k -> TypeCheck k (TermT k)
forall (l :: S). Distinct l => TermT l -> TypeCheck l (TermT l)
expose TermT k
original TypeCheck k (TermT k)
-> (TermT k -> TypeCheck k ()) -> TypeCheck k ()
forall a b.
ReaderT
  (Context k) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context k) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context k) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \TermT k
body -> case (CheckedPosition
position, TermT k
body) of
                    (InAssumption FragmentUse
_, TypeRestrictedT TypeInfo (TermT k)
_ TermT k
_ [(TermT k, TermT k)]
rs) ->
                      [(TermT k, TermT k)]
-> ((TermT k, TermT k) -> TypeCheck k ()) -> TypeCheck k ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(TermT k, TermT k)]
rs (((TermT k, TermT k) -> TypeCheck k ()) -> TypeCheck k ())
-> ((TermT k, TermT k) -> TypeCheck k ()) -> TypeCheck k ()
forall a b. (a -> b) -> a -> b
$ \(TermT k
face, TermT k
_) -> String -> Name k -> TermT k -> TypeCheck k ()
forall (l :: S).
Distinct l =>
String -> Name l -> TermT l -> TypeCheck l ()
reportShapeDependency String
"restriction boundary" Name k
point TermT k
face
                    (CheckedPosition, TermT k)
_ -> () -> TypeCheck k ()
forall a.
a
-> ReaderT
     (Context k) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
            case mtope of
              Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
Nothing -> Scope l
-> ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
-> (forall (l :: S).
    DExt l l =>
    NameBinder l l
    -> AST NameBinder (AnnSig TypeInfo TermSig) l
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (sig :: * -> * -> *) (n :: S) r.
(Bifunctor sig, Distinct n) =>
Scope n
-> ScopedAST NameBinder sig n
-> (forall (l :: S).
    DExt n l =>
    NameBinder n l -> AST NameBinder sig l -> r)
-> r
withScopedT Scope l
scope ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
ret ((forall (l :: S).
  DExt l l =>
  NameBinder l l
  -> AST NameBinder (AnnSig TypeInfo TermSig) l
  -> ReaderT
       (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
 -> ReaderT
      (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> (forall (l :: S).
    DExt l l =>
    NameBinder l l
    -> AST NameBinder (AnnSig TypeInfo TermSig) l
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a b. (a -> b) -> a -> b
$ \NameBinder l l
binder AST NameBinder (AnnSig TypeInfo TermSig) l
body ->
                NameBinder l l
-> Binder
-> TModality
-> TermT l
-> Maybe (TermT l)
-> TypeCheck l ()
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S) (l :: S) a.
(Distinct n, DExt n l) =>
NameBinder n l
-> Binder
-> TModality
-> TermT n
-> Maybe (TermT n)
-> TypeCheck l a
-> TypeCheck n a
underBinder NameBinder l l
binder Binder
orig TModality
md TermT l
param Maybe (TermT l)
forall a. Maybe a
Nothing (TypeCheck l ()
 -> ReaderT
      (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> TypeCheck l ()
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a b. (a -> b) -> a -> b
$ Name l
-> AST NameBinder (AnnSig TypeInfo TermSig) l
-> AST NameBinder (AnnSig TypeInfo TermSig) l
-> TypeCheck l ()
forall (k :: S).
Distinct k =>
Name k -> TermT k -> TermT k -> TypeCheck k ()
check (NameBinder l l -> Name l
forall (n :: S) (l :: S). NameBinder n l -> Name l
Foil.nameOf NameBinder l l
binder) (TermT l -> AST NameBinder (AnnSig TypeInfo TermSig) l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink TermT l
param) AST NameBinder (AnnSig TypeInfo TermSig) l
body
              Just ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
tope -> Scope l
-> ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
-> ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
-> (forall (l :: S).
    DExt l l =>
    NameBinder l l
    -> TermT l
    -> TermT l
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S) r.
Distinct n =>
Scope n
-> ScopedTermT n
-> ScopedTermT n
-> (forall (l :: S).
    DExt n l =>
    NameBinder n l -> TermT l -> TermT l -> r)
-> r
withScopedT2 Scope l
scope ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
tope ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
ret ((forall (l :: S).
  DExt l l =>
  NameBinder l l
  -> TermT l
  -> TermT l
  -> ReaderT
       (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
 -> ReaderT
      (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> (forall (l :: S).
    DExt l l =>
    NameBinder l l
    -> TermT l
    -> TermT l
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a b. (a -> b) -> a -> b
$ \NameBinder l l
binder TermT l
domain TermT l
body ->
                NameBinder l l
-> Binder
-> TModality
-> TermT l
-> Maybe (TermT l)
-> TypeCheck l ()
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S) (l :: S) a.
(Distinct n, DExt n l) =>
NameBinder n l
-> Binder
-> TModality
-> TermT n
-> Maybe (TermT n)
-> TypeCheck l a
-> TypeCheck n a
underBinder NameBinder l l
binder Binder
orig TModality
md TermT l
param Maybe (TermT l)
forall a. Maybe a
Nothing (TypeCheck l ()
 -> ReaderT
      (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> TypeCheck l ()
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a b. (a -> b) -> a -> b
$ do
                  Bool -> TypeCheck l () -> TypeCheck l ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
schematic (TypeCheck l () -> TypeCheck l ())
-> TypeCheck l () -> TypeCheck l ()
forall a b. (a -> b) -> a -> b
$
                    String -> Name l -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
String -> Name l -> TermT l -> TypeCheck l ()
reportShapeDependency String
"shape domain" (NameBinder l l -> Name l
forall (n :: S) (l :: S). NameBinder n l -> Name l
Foil.nameOf NameBinder l l
binder) TermT l
domain
                  Name l -> TermT l -> TermT l -> TypeCheck l ()
forall (k :: S).
Distinct k =>
Name k -> TermT k -> TermT k -> TypeCheck k ()
check (NameBinder l l -> Name l
forall (n :: S) (l :: S). NameBinder n l -> Name l
Foil.nameOf NameBinder l l
binder) (TermT l -> TermT l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink TermT l
param) TermT l
body
        TermT l
_ -> ()
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a.
a
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

    reportShapeDependency
      :: forall l. Distinct l => String -> Foil.Name l -> TermT l -> TypeCheck l ()
    reportShapeDependency :: forall (l :: S).
Distinct l =>
String -> Name l -> TermT l -> TypeCheck l ()
reportShapeDependency String
what Name l
point TermT l
tope = do
      points <- Name l -> TermT l -> TypeCheck l [Name l]
forall (l :: S).
Distinct l =>
Name l -> TermT l -> TypeCheck l [Name l]
outerPoints Name l
point TermT l
tope
      unless (null points) $ do
        naming <- asks namingOfContext
        loc <- asks ctxLocation
        let render = Naming l -> Term l -> String
forall (n :: S). Naming n -> Term n -> String
ppTerm Naming l
naming (Term l -> String) -> (TermT l -> Term l) -> TermT l -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TermT l -> Term l
forall (n :: S). TermT n -> Term n
untyped
        recordCheckWarning $ RSTTScopeWarning RSTTShapeDependency
          ("context-dependent " <> what <> " " <> render tope
            <> " (outer cube points: " <> intercalate ", " (map (render . Var) points)
            <> "; in " <> show defName <> ")") loc

    -- Follow aliases and variable types without reducing away dependencies.
    outerPoints :: forall l. Distinct l => Foil.Name l -> TermT l -> TypeCheck l [Foil.Name l]
    outerPoints :: forall (l :: S).
Distinct l =>
Name l -> TermT l -> TypeCheck l [Name l]
outerPoints Name l
point TermT l
t = IntSet
-> [Name l]
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     [Name l]
forall {l :: S}.
Distinct l =>
IntSet
-> [Name l]
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     [Name l]
go (Int -> IntSet
IntSet.singleton (Name l -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name l
point)) (TermT l -> [Name l]
forall (n :: S). TermT n -> [Name n]
freeVarsOfTermT TermT l
t)
      where
        go :: IntSet
-> [Name l]
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     [Name l]
go IntSet
_ [] = [Name l]
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     [Name l]
forall a.
a
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
        go IntSet
seen (Name l
v : [Name l]
vs)
          | Name l -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name l
v Int -> IntSet -> Bool
`IntSet.member` IntSet
seen = IntSet
-> [Name l]
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     [Name l]
go IntSet
seen [Name l]
vs
          | Bool
otherwise = do
              ty <- Name l -> TypeCheck l (TermT l)
forall (n :: S). Name n -> TypeCheck n (TermT n)
typeOfVar Name l
v
              value <- valueOfVar v
              cube <- typeOfUncomputed ty >>= whnfT
              let here = case (Maybe (TermT l)
value, TermT l
cube) of
                    (Maybe (TermT l)
Nothing, UniverseCubeT{}) -> [Name l
v]
                    (Maybe (TermT l), TermT l)
_ -> []
                  dependencies = TermT l -> [Name l]
forall (n :: S). TermT n -> [Name n]
freeVarsOfTermT TermT l
ty [Name l] -> [Name l] -> [Name l]
forall a. Semigroup a => a -> a -> a
<> (TermT l -> [Name l]) -> Maybe (TermT l) -> [Name l]
forall m a. Monoid m => (a -> m) -> Maybe a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap TermT l -> [Name l]
forall (n :: S). TermT n -> [Name n]
freeVarsOfTermT Maybe (TermT l)
value
              rest <- go (IntSet.insert (Foil.nameId v) seen) (dependencies <> vs)
              pure (here <> rest)

    -- Check term binders and route type-valued bodies through goData.
    goTerm :: forall l. Distinct l => PrefixPosition -> TermT l -> TypeCheck l ()
    goTerm :: forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
InParameterPrefix TermT l
t | Bool -> Bool
not (TermT l -> Bool
forall (l :: S). TermT l -> Bool
isLambda TermT l
t) = PrefixPosition -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goDataAt PrefixPosition
InParameterPrefix TermT l
t
    goTerm PrefixPosition
prefixPosition TermT l
t = case TermT l
t of
      Var{} -> () -> TypeCheck l ()
forall a.
a
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

      -- Function types returned by lets or branches still introduce shapes.
      TypeFunT{} -> FragmentUse -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
UseData TermT l
t

      -- The stored type is synthesised by elaboration; it is not assumed.
      ReflT TypeInfo (TermT l)
_ Maybe (TermT l, Maybe (TermT l))
mx -> Maybe (TermT l, Maybe (TermT l))
-> ((TermT l, Maybe (TermT l)) -> TypeCheck l ()) -> TypeCheck l ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe (TermT l, Maybe (TermT l))
mx (((TermT l, Maybe (TermT l)) -> TypeCheck l ()) -> TypeCheck l ())
-> ((TermT l, Maybe (TermT l)) -> TypeCheck l ()) -> TypeCheck l ()
forall a b. (a -> b) -> a -> b
$ \(TermT l
x, Maybe (TermT l)
_mxty) -> PrefixPosition -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
x

      TypeAscT TypeInfo (TermT l)
_ TermT l
term' TermT l
ty' -> do
        PrefixPosition -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
term'
        TermT l -> TypeCheck l ()
forall (l :: S). Distinct l => TermT l -> TypeCheck l ()
goTail TermT l
ty'

      LambdaT TypeInfo (TermT l)
info Binder
orig Maybe
  (LambdaParam
     (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l) (TermT l))
mparam ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
body -> do
        CheckedPosition -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
CheckedPosition -> TermT l -> TypeCheck l ()
checkShapeDependencies CheckedPosition
InTail (TypeInfo (TermT l) -> TermT l
forall term. TypeInfo term -> term
infoType TypeInfo (TermT l)
info)
        md <- case Maybe
  (LambdaParam
     (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l) (TermT l))
mparam of
          Just (LambdaParam TModality
md TermT l
param Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
mtope) -> do
            FragmentUse -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
UseBinder TermT l
param
            Binder
-> TModality
-> TermT l
-> Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
-> TypeCheck l ()
forall (l :: S).
Distinct l =>
Binder
-> TModality -> TermT l -> Maybe (ScopedTermT l) -> TypeCheck l ()
checkDomain Binder
orig TModality
md TermT l
param Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
mtope
            Bool -> TypeCheck l () -> TypeCheck l ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (PrefixPosition
prefixPosition PrefixPosition -> PrefixPosition -> Bool
forall a. Eq a => a -> a -> Bool
== PrefixPosition
OutsideParameterPrefix) (TypeCheck l () -> TypeCheck l ())
-> TypeCheck l () -> TypeCheck l ()
forall a b. (a -> b) -> a -> b
$ Binder -> TermT l -> TypeCheck l ()
forall (l :: S). Distinct l => Binder -> TermT l -> TypeCheck l ()
reportMetaBinder Binder
orig TermT l
param
            TModality
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     TModality
forall a.
a
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TModality
md
          Maybe
  (LambdaParam
     (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l) (TermT l))
Nothing -> TModality
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     TModality
forall a.
a
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TModality
Id
        dom <- binderType info mparam
        inScope orig md dom body (goTerm prefixPosition)

      AppT{} -> do
        let (TermT l
h, [(TermT l, TermT l)]
args) = TermT l -> [(TermT l, TermT l)] -> (TermT l, [(TermT l, TermT l)])
forall (n :: S).
TermT n -> [(TermT n, TermT n)] -> (TermT n, [(TermT n, TermT n)])
collectSpine TermT l
t []
        PrefixPosition -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
h
        [(TermT l, TermT l)]
-> ((TermT l, TermT l) -> TypeCheck l ()) -> TypeCheck l ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(TermT l, TermT l)]
args (((TermT l, TermT l) -> TypeCheck l ()) -> TypeCheck l ())
-> ((TermT l, TermT l) -> TypeCheck l ()) -> TypeCheck l ()
forall a b. (a -> b) -> a -> b
$ \(TermT l
fnode, TermT l
arg) -> do
          -- Substitution can create a forbidden type in a later argument domain.
          Bool -> TypeCheck l () -> TypeCheck l ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (FragmentView
view FragmentView -> FragmentView -> Bool
forall a. Eq a => a -> a -> Bool
== FragmentView
ComputedTypes) (TypeCheck l () -> TypeCheck l ())
-> TypeCheck l () -> TypeCheck l ()
forall a b. (a -> b) -> a -> b
$
            TermT l -> TypeCheck l (TermT l)
forall (n :: S). TermT n -> TypeCheck n (TermT n)
typeOfUncomputed TermT l
fnode TypeCheck l (TermT l)
-> (TermT l
    -> ReaderT
         (Context l)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         (Maybe (TermT l)))
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe (TermT l))
forall a b.
ReaderT
  (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= TermT l
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe (TermT l))
forall (l :: S).
Distinct l =>
TermT l -> TypeCheck l (Maybe (TermT l))
funDomain ReaderT
  (Context l)
  (ExceptT TypeErrorInScopedContext (State CheckLog))
  (Maybe (TermT l))
-> (Maybe (TermT l) -> TypeCheck l ()) -> TypeCheck l ()
forall a b.
ReaderT
  (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (TermT l -> TypeCheck l ()) -> Maybe (TermT l) -> TypeCheck l ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (FragmentUse -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
UseBinder)
          -- Schema arguments may bind their own schematic parameters.
          plumbing <- TermT l -> TypeCheck l Bool
forall (n :: S). Distinct n => TermT n -> TypeCheck n Bool
domainIsMeta TermT l
fnode
          if plumbing then goTerm InParameterPrefix arg else goData arg

      -- The motive is assumed: the eliminator binds its variables at it.
      IdJT TypeInfo (TermT l)
_ TermT l
tA TermT l
a TermT l
tC TermT l
d TermT l
x TermT l
p -> do
        TermT l -> TypeCheck l ()
forall (l :: S). Distinct l => TermT l -> TypeCheck l ()
goData TermT l
tA
        PrefixPosition -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
a
        FragmentUse -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
UseMotive TermT l
tC
        PrefixPosition -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
d
        PrefixPosition -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
x
        PrefixPosition -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
p

      -- Preserve prefix status for parameters bound after a schematic split.
      MatchT TypeInfo (TermT l)
_ TModality
_md TermT l
scrut Maybe (TermT l)
mmotive [(VarIdent, TermT l)]
branches -> do
        PrefixPosition -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix TermT l
scrut
        (TermT l -> TypeCheck l ()) -> Maybe (TermT l) -> TypeCheck l ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (FragmentUse -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
UseMotive) Maybe (TermT l)
mmotive
        [(VarIdent, TermT l)]
-> ((VarIdent, TermT l) -> TypeCheck l ()) -> TypeCheck l ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(VarIdent, TermT l)]
branches (((VarIdent, TermT l) -> TypeCheck l ()) -> TypeCheck l ())
-> ((VarIdent, TermT l) -> TypeCheck l ()) -> TypeCheck l ()
forall a b. (a -> b) -> a -> b
$ \(VarIdent
_con, TermT l
branch) -> PrefixPosition -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
prefixPosition TermT l
branch

      PairT TypeInfo (TermT l)
_ TermT l
l TermT l
r -> do
        TermT l -> TypeCheck l ()
forall (l :: S). Distinct l => TermT l -> TypeCheck l ()
goData TermT l
l
        TermT l -> TypeCheck l ()
forall (l :: S). Distinct l => TermT l -> TypeCheck l ()
goData TermT l
r

      -- The let annotation records the value's concluded type.
      LetT TypeInfo (TermT l)
_ Binder
orig Maybe (TermT l)
manno TermT l
value ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
body -> do
        (TermT l -> TypeCheck l ()) -> Maybe (TermT l) -> TypeCheck l ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ TermT l -> TypeCheck l ()
forall (l :: S). Distinct l => TermT l -> TypeCheck l ()
goTail Maybe (TermT l)
manno
        TermT l -> TypeCheck l ()
forall (l :: S). Distinct l => TermT l -> TypeCheck l ()
goData TermT l
value
        let dom :: TermT l
dom = TermT l -> Maybe (TermT l) -> TermT l
forall a. a -> Maybe a -> a
fromMaybe TermT l
forall (n :: S). TermT n
universeT (Maybe (TermT l)
manno Maybe (TermT l) -> Maybe (TermT l) -> Maybe (TermT l)
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> TermT l -> Maybe (TermT l)
forall (l :: S). TermT l -> Maybe (TermT l)
valueType TermT l
value)
        Binder
-> TModality
-> TermT l
-> Maybe (TermT l)
-> ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    AST NameBinder (AnnSig TypeInfo TermSig) l -> TypeCheck l ())
-> TypeCheck l ()
forall (sig :: * -> * -> *) (n :: S) a.
(Bifunctor sig, Distinct n) =>
Binder
-> TModality
-> TermT n
-> Maybe (TermT n)
-> ScopedAST NameBinder sig n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    AST NameBinder sig l -> TypeCheck l a)
-> TypeCheck n a
inScopeWith Binder
orig TModality
Id TermT l
dom (TermT l -> Maybe (TermT l)
forall a. a -> Maybe a
Just TermT l
value) ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
body (PrefixPosition -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix)

      LetModT TypeInfo (TermT l)
_ Binder
orig TModality
_nu TModality
mu Maybe (TermT l)
manno Maybe (TermT l)
mmotive TermT l
value ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
body -> do
        (TermT l -> TypeCheck l ()) -> Maybe (TermT l) -> TypeCheck l ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ TermT l -> TypeCheck l ()
forall (l :: S). Distinct l => TermT l -> TypeCheck l ()
goTail Maybe (TermT l)
manno
        (TermT l -> TypeCheck l ()) -> Maybe (TermT l) -> TypeCheck l ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (FragmentUse -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
goAssumed FragmentUse
UseMotive) Maybe (TermT l)
mmotive
        TermT l -> TypeCheck l ()
forall (l :: S). Distinct l => TermT l -> TypeCheck l ()
goData TermT l
value
        Binder
-> TModality
-> TermT l
-> ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    AST NameBinder (AnnSig TypeInfo TermSig) l -> TypeCheck l ())
-> TypeCheck l ()
forall (sig :: * -> * -> *) (n :: S) a.
(Bifunctor sig, Distinct n) =>
Binder
-> TModality
-> TermT n
-> ScopedAST NameBinder sig n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    AST NameBinder sig l -> TypeCheck l a)
-> TypeCheck n a
inScope Binder
orig TModality
mu (TermT l -> Maybe (TermT l) -> TermT l
forall a. a -> Maybe a -> a
fromMaybe TermT l
forall (n :: S). TermT n
universeT (Maybe (TermT l)
manno Maybe (TermT l) -> Maybe (TermT l) -> Maybe (TermT l)
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> TermT l -> Maybe (TermT l)
forall (l :: S). TermT l -> Maybe (TermT l)
valueType TermT l
value)) ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
body
          (PrefixPosition -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix)

      Node (AnnSig TypeInfo (TermT l)
_ TermSig
  (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l) (TermT l)
f) ->
        (TermT l -> TypeCheck l ()) -> [TermT l] -> TypeCheck l ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (PrefixPosition -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goTerm PrefixPosition
OutsideParameterPrefix) ((ScopedAST NameBinder (AnnSig TypeInfo TermSig) l -> [TermT l])
-> (TermT l -> [TermT l])
-> TermSig
     (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l) (TermT l)
-> [TermT l]
forall m a b. Monoid m => (a -> m) -> (b -> m) -> TermSig a b -> m
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap ([TermT l]
-> ScopedAST NameBinder (AnnSig TypeInfo TermSig) l -> [TermT l]
forall a b. a -> b -> a
const []) (TermT l -> [TermT l] -> [TermT l]
forall a. a -> [a] -> [a]
:[]) TermSig
  (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l) (TermT l)
f)

    -- Type-valued data may later become binder or motive types. A proof
    -- quantified over a universe must still be walked as a term.
    goData :: forall l. Distinct l => TermT l -> TypeCheck l ()
    goData :: forall (l :: S). Distinct l => TermT l -> TypeCheck l ()
goData = PrefixPosition -> TermT l -> TypeCheck l ()
forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goDataAt PrefixPosition
OutsideParameterPrefix

    goDataAt :: forall l. Distinct l => PrefixPosition -> TermT l -> TypeCheck l ()
    goDataAt :: forall (l :: S).
Distinct l =>
PrefixPosition -> TermT l -> TypeCheck l ()
goDataAt PrefixPosition
prefixPosition TermT l
t = do
      isType <- TermT l -> TypeCheck l Bool
forall (n :: S). Distinct n => TermT n -> TypeCheck n Bool
landsInUniverse TermT l
t
      if isType then goAssumedAt prefixPosition UseData t else goTerm OutsideParameterPrefix t

    isLambda :: forall l. TermT l -> Bool
    isLambda :: forall (l :: S). TermT l -> Bool
isLambda LambdaT{} = Bool
True
    isLambda AST NameBinder (AnnSig TypeInfo TermSig) l
_         = Bool
False

    valueType :: forall l. TermT l -> Maybe (TermT l)
    valueType :: forall (l :: S). TermT l -> Maybe (TermT l)
valueType TermT l
value = TypeInfo (TermT l) -> TermT l
forall term. TypeInfo term -> term
infoType (TypeInfo (TermT l) -> TermT l)
-> Maybe (TypeInfo (TermT l)) -> Maybe (TermT l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TermT l -> Maybe (TypeInfo (TermT l))
forall (n :: S). TermT n -> Maybe (TypeInfo (TermT n))
typeInfoOf TermT l
value

    binderType
      :: forall l. Distinct l
      => TypeInfo (TermT l) -> Maybe (LambdaParam (ScopedTermT l) (TermT l))
      -> TypeCheck l (TermT l)
    binderType :: forall (l :: S).
Distinct l =>
TypeInfo (TermT l)
-> Maybe (LambdaParam (ScopedTermT l) (TermT l))
-> TypeCheck l (TermT l)
binderType TypeInfo (TermT l)
_info (Just (LambdaParam TModality
_ TermT l
param Maybe (ScopedTermT l)
_mtope)) = TermT l
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT l)
forall a.
a
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TermT l
param
    binderType TypeInfo (TermT l)
info Maybe (LambdaParam (ScopedTermT l) (TermT l))
Nothing = TermT l -> Maybe (TermT l) -> TermT l
forall a. a -> Maybe a -> a
fromMaybe TermT l
forall (n :: S). TermT n
universeT (Maybe (TermT l) -> TermT l)
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe (TermT l))
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TermT l
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe (TermT l))
forall (l :: S).
Distinct l =>
TermT l -> TypeCheck l (Maybe (TermT l))
funDomain (TypeInfo (TermT l) -> TermT l
forall term. TypeInfo term -> term
infoType TypeInfo (TermT l)
info)

    -- A cube domain without a tope denotes the unrestricted shape.
    isShapeBinder :: forall l. Distinct l => Bool -> TermT l -> TypeCheck l Bool
    isShapeBinder :: forall (l :: S). Distinct l => Bool -> TermT l -> TypeCheck l Bool
isShapeBinder Bool
True TermT l
_ = Bool
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall a.
a
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
    isShapeBinder Bool
False TermT l
param =
      TermT l -> TypeCheck l (TermT l)
forall (n :: S). TermT n -> TypeCheck n (TermT n)
typeOfUncomputed TermT l
param TypeCheck l (TermT l)
-> (TermT l -> TypeCheck l (TermT l)) -> TypeCheck l (TermT l)
forall a b.
ReaderT
  (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= TermT l -> TypeCheck l (TermT l)
forall (l :: S). Distinct l => TermT l -> TypeCheck l (TermT l)
whnfT TypeCheck l (TermT l)
-> (TermT l
    -> ReaderT
         (Context l)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         Bool)
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall a b.
ReaderT
  (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        UniverseCubeT{} -> Bool
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall a.
a
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
        TermT l
_               -> Bool
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall a.
a
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False

    -- Is the Π-domain of this function node schematic?
    domainIsMeta :: forall l. Distinct l => TermT l -> TypeCheck l Bool
    domainIsMeta :: forall (n :: S). Distinct n => TermT n -> TypeCheck n Bool
domainIsMeta TermT l
f = do
      tf <- TermT l -> TypeCheck l (TermT l)
forall (n :: S). TermT n -> TypeCheck n (TermT n)
typeOfUncomputed TermT l
f
      whnfT (stripTypeRestrictions tf) >>= \case
        TypeFunT TypeInfo (TermT l)
_ Binder
_ TModality
_ TermT l
dom Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
_ ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
_ -> TermT l
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall (n :: S). Distinct n => TermT n -> TypeCheck n Bool
isMetaType TermT l
dom
        TermT l
_                      -> Bool
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall a.
a
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False

    -- Recognise types and type families by their final codomain.
    landsInUniverse :: forall l. Distinct l => TermT l -> TypeCheck l Bool
    landsInUniverse :: forall (n :: S). Distinct n => TermT n -> TypeCheck n Bool
landsInUniverse TermT l
t =
      TermT l -> TypeCheck l (TermT l)
forall (n :: S). TermT n -> TypeCheck n (TermT n)
typeOfUncomputed TermT l
t TypeCheck l (TermT l)
-> (TermT l
    -> ReaderT
         (Context l)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         Bool)
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall a b.
ReaderT
  (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= TermT l
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall (n :: S). Distinct n => TermT n -> TypeCheck n Bool
go
      where
        go :: forall k. Distinct k => TermT k -> TypeCheck k Bool
        go :: forall (n :: S). Distinct n => TermT n -> TypeCheck n Bool
go TermT k
ty = TermT k -> TypeCheck k (TermT k)
forall (l :: S). Distinct l => TermT l -> TypeCheck l (TermT l)
whnfT (TermT k -> TermT k
forall (n :: S). TermT n -> TermT n
stripTypeRestrictions TermT k
ty) TypeCheck k (TermT k)
-> (TermT k
    -> ReaderT
         (Context k)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         Bool)
-> ReaderT
     (Context k)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall a b.
ReaderT
  (Context k) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context k) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context k) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
          UniverseT{}     -> Bool
-> ReaderT
     (Context k)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall a.
a
-> ReaderT
     (Context k) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
          UniverseCubeT{} -> Bool
-> ReaderT
     (Context k)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall a.
a
-> ReaderT
     (Context k) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
          UniverseTopeT{} -> Bool
-> ReaderT
     (Context k)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall a.
a
-> ReaderT
     (Context k) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
          TypeFunT TypeInfo (TermT k)
_ Binder
orig TModality
md TermT k
param Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) k)
_mtope ScopedAST NameBinder (AnnSig TypeInfo TermSig) k
ret -> Binder
-> TModality
-> TermT k
-> ScopedAST NameBinder (AnnSig TypeInfo TermSig) k
-> (forall (l :: S).
    (DExt k l, Distinct l) =>
    AST NameBinder (AnnSig TypeInfo TermSig) l -> TypeCheck l Bool)
-> ReaderT
     (Context k)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall (sig :: * -> * -> *) (n :: S) a.
(Bifunctor sig, Distinct n) =>
Binder
-> TModality
-> TermT n
-> ScopedAST NameBinder sig n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    AST NameBinder sig l -> TypeCheck l a)
-> TypeCheck n a
inScope Binder
orig TModality
md TermT k
param ScopedAST NameBinder (AnnSig TypeInfo TermSig) k
ret TermT l -> TypeCheck l Bool
forall (l :: S).
(DExt k l, Distinct l) =>
AST NameBinder (AnnSig TypeInfo TermSig) l -> TypeCheck l Bool
forall (n :: S). Distinct n => TermT n -> TypeCheck n Bool
go
          TermT k
_               -> Bool
-> ReaderT
     (Context k)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall a.
a
-> ReaderT
     (Context k) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False

    funDomain :: forall l. Distinct l => TermT l -> TypeCheck l (Maybe (TermT l))
    funDomain :: forall (l :: S).
Distinct l =>
TermT l -> TypeCheck l (Maybe (TermT l))
funDomain TermT l
tf =
      TermT l -> TypeCheck l (TermT l)
forall (l :: S). Distinct l => TermT l -> TypeCheck l (TermT l)
whnfT (TermT l -> TermT l
forall (n :: S). TermT n -> TermT n
stripTypeRestrictions TermT l
tf) TypeCheck l (TermT l)
-> (TermT l
    -> ReaderT
         (Context l)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         (Maybe (TermT l)))
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe (TermT l))
forall a b.
ReaderT
  (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        TypeFunT TypeInfo (TermT l)
_ Binder
_ TModality
_ TermT l
dom Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) l)
_ ScopedAST NameBinder (AnnSig TypeInfo TermSig) l
_ -> Maybe (TermT l)
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe (TermT l))
forall a.
a
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TermT l -> Maybe (TermT l)
forall a. a -> Maybe a
Just TermT l
dom)
        TermT l
_                      -> Maybe (TermT l)
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe (TermT l))
forall a.
a
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (TermT l)
forall a. Maybe a
Nothing

    reportMetaBinder :: forall l. Distinct l => Binder -> TermT l -> TypeCheck l ()
    reportMetaBinder :: forall (l :: S). Distinct l => Binder -> TermT l -> TypeCheck l ()
reportMetaBinder Binder
orig TermT l
param = do
      enabled <- (Context l -> Bool)
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Context l -> Bool
forall (n :: S). Context n -> Bool
ctxWarnMetaBinder
      when enabled $ do
        meta <- isMetaType param
        when meta $ do
          naming <- asks namingOfContext
          loc <- asks ctxLocation
          recordCheckWarning $ MetaBinderWarning
            defName
            (fromMaybe "_" (binderName orig))
            (ppTerm naming (untyped param))
            loc

    reportFreeStanding :: forall l. Distinct l => FragmentUse -> TermT l -> TypeCheck l ()
    reportFreeStanding :: forall (l :: S).
Distinct l =>
FragmentUse -> TermT l -> TypeCheck l ()
reportFreeStanding FragmentUse
use TermT l
t = do
      enabled <- (Context l -> Bool)
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Context l -> Bool
forall (n :: S). Context n -> Bool
ctxWarnFreeStandingRestriction
      when enabled $ do
        naming <- asks namingOfContext
        loc <- asks ctxLocation
        recordCheckWarning $
          FreeStandingRestrictionWarning defName (ppTerm naming (untyped t)) use loc

-- Expose type constructors without consulting cached normal forms or simplifying
-- their guards. Keep this reducer in step with the object-level rules in Eval.
fragmentHead :: Distinct n => Int -> TermT n -> TypeCheck n (TermT n)
fragmentHead :: forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
fragmentHead Int
fuel TermT n
t
  | Int
fuel Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = TypeError n -> TypeCheck n (TermT n)
forall (n :: S) a. Distinct n => TypeError n -> TypeCheck n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorOther String
"RSTT fragment reduction limit reached")
  | Bool
otherwise = case TermT n
t of
      Var Name n
v -> Name n -> TypeCheck n (Maybe (TermT n))
forall (n :: S). Name n -> TypeCheck n (Maybe (TermT n))
valueOfVar Name n
v TypeCheck n (Maybe (TermT n))
-> (Maybe (TermT n) -> TypeCheck n (TermT n))
-> TypeCheck n (TermT n)
forall a b.
ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= TypeCheck n (TermT n)
-> (TermT n -> TypeCheck n (TermT n))
-> Maybe (TermT n)
-> TypeCheck n (TermT n)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (TermT n -> TypeCheck n (TermT n)
neutral TermT n
t) TermT n -> TypeCheck n (TermT n)
step
      AppT TypeInfo (TermT n)
info TermT n
f TermT n
x -> TermT n -> TypeCheck n (TermT n)
step TermT n
f TypeCheck n (TermT n)
-> (TermT n -> TypeCheck n (TermT n)) -> TypeCheck n (TermT n)
forall a b.
ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        LambdaT TypeInfo (TermT n)
_ Binder
_ Maybe
  (LambdaParam
     (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n) (TermT n))
_ ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
body -> ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
-> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
ScopedTermT n -> TermT n -> TypeCheck n (TermT n)
instantiate ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
body TermT n
x TypeCheck n (TermT n)
-> (TermT n -> TypeCheck n (TermT n)) -> TypeCheck n (TermT n)
forall a b.
ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= TermT n -> TypeCheck n (TermT n)
step
        TermT n
f' -> TermT n -> TypeCheck n (TermT n)
neutral (TypeInfo (TermT n) -> TermT n -> TermT n -> TermT n
forall {binder :: S -> S -> *} {ann :: * -> *} {n :: S}.
ann (AST binder (AnnSig ann TermSig) n)
-> AST binder (AnnSig ann TermSig) n
-> AST binder (AnnSig ann TermSig) n
-> AST binder (AnnSig ann TermSig) n
AppT TypeInfo (TermT n)
info TermT n
f' TermT n
x)
      LetT TypeInfo (TermT n)
_ Binder
_ Maybe (TermT n)
_ TermT n
value ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
body -> ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
-> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
ScopedTermT n -> TermT n -> TypeCheck n (TermT n)
instantiate ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
body TermT n
value TypeCheck n (TermT n)
-> (TermT n -> TypeCheck n (TermT n)) -> TypeCheck n (TermT n)
forall a b.
ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= TermT n -> TypeCheck n (TermT n)
step
      TypeAscT TypeInfo (TermT n)
_ TermT n
value TermT n
_ -> TermT n -> TypeCheck n (TermT n)
step TermT n
value
      FirstT TypeInfo (TermT n)
info TermT n
pair -> TermT n -> TypeCheck n (TermT n)
step TermT n
pair TypeCheck n (TermT n)
-> (TermT n -> TypeCheck n (TermT n)) -> TypeCheck n (TermT n)
forall a b.
ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        PairT TypeInfo (TermT n)
_ TermT n
a TermT n
_ -> TermT n -> TypeCheck n (TermT n)
step TermT n
a
        TermT n
pair' -> TermT n -> TypeCheck n (TermT n)
neutral (TypeInfo (TermT n) -> TermT n -> TermT n
forall {binder :: S -> S -> *} {ann :: * -> *} {n :: S}.
ann (AST binder (AnnSig ann TermSig) n)
-> AST binder (AnnSig ann TermSig) n
-> AST binder (AnnSig ann TermSig) n
FirstT TypeInfo (TermT n)
info TermT n
pair')
      SecondT TypeInfo (TermT n)
info TermT n
pair -> TermT n -> TypeCheck n (TermT n)
step TermT n
pair TypeCheck n (TermT n)
-> (TermT n -> TypeCheck n (TermT n)) -> TypeCheck n (TermT n)
forall a b.
ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        PairT TypeInfo (TermT n)
_ TermT n
_ TermT n
b -> TermT n -> TypeCheck n (TermT n)
step TermT n
b
        TermT n
pair' -> TermT n -> TypeCheck n (TermT n)
neutral (TypeInfo (TermT n) -> TermT n -> TermT n
forall {binder :: S -> S -> *} {ann :: * -> *} {n :: S}.
ann (AST binder (AnnSig ann TermSig) n)
-> AST binder (AnnSig ann TermSig) n
-> AST binder (AnnSig ann TermSig) n
SecondT TypeInfo (TermT n)
info TermT n
pair')
      IdJT TypeInfo (TermT n)
info TermT n
a TermT n
x TermT n
motive TermT n
d TermT n
y TermT n
proof -> TermT n -> TypeCheck n (TermT n)
step TermT n
proof TypeCheck n (TermT n)
-> (TermT n -> TypeCheck n (TermT n)) -> TypeCheck n (TermT n)
forall a b.
ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        ReflT{} -> TermT n -> TypeCheck n (TermT n)
step TermT n
d
        TermT n
proof' -> TermT n -> TypeCheck n (TermT n)
neutral (TypeInfo (TermT n)
-> TermT n
-> TermT n
-> TermT n
-> TermT n
-> TermT n
-> TermT n
-> TermT n
forall {binder :: S -> S -> *} {ann :: * -> *} {n :: S}.
ann (AST binder (AnnSig ann TermSig) n)
-> AST binder (AnnSig ann TermSig) n
-> AST binder (AnnSig ann TermSig) n
-> AST binder (AnnSig ann TermSig) n
-> AST binder (AnnSig ann TermSig) n
-> AST binder (AnnSig ann TermSig) n
-> AST binder (AnnSig ann TermSig) n
-> AST binder (AnnSig ann TermSig) n
IdJT TypeInfo (TermT n)
info TermT n
a TermT n
x TermT n
motive TermT n
d TermT n
y TermT n
proof')
      RecOrT TypeInfo (TermT n)
_ [(TermT n, TermT n)]
rs -> [(TermT n, TermT n)] -> TypeCheck n (Maybe (TermT n))
forall (n :: S).
Distinct n =>
[(TermT n, TermT n)] -> TypeCheck n (Maybe (TermT n))
firstMatching [(TermT n, TermT n)]
rs TypeCheck n (Maybe (TermT n))
-> (Maybe (TermT n) -> TypeCheck n (TermT n))
-> TypeCheck n (TermT n)
forall a b.
ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= TypeCheck n (TermT n)
-> (TermT n -> TypeCheck n (TermT n))
-> Maybe (TermT n)
-> TypeCheck n (TermT n)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (TermT n -> TypeCheck n (TermT n)
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TermT n
t) TermT n -> TypeCheck n (TermT n)
step
      -- In particular, stop at TypeRestrictedT before reducing its topes.
      TermT n
_ -> TermT n -> TypeCheck n (TermT n)
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TermT n
t
  where
    step :: TermT n -> TypeCheck n (TermT n)
step = Int -> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
fragmentHead (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    neutral :: TermT n -> TypeCheck n (TermT n)
neutral TermT n
term = do
      ty <- TermT n -> TypeCheck n (TermT n)
forall (n :: S). TermT n -> TypeCheck n (TermT n)
typeOfUncomputed TermT n
term TypeCheck n (TermT n)
-> (TermT n -> TypeCheck n (TermT n)) -> TypeCheck n (TermT n)
forall a b.
ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> (a
    -> ReaderT
         (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b)
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= TermT n -> TypeCheck n (TermT n)
step
      tryRestriction ty >>= maybe (pure term) step

-- TODO: Share this application-spine helper with MetaPrefix in the syntax layer.
collectSpine :: TermT n -> [(TermT n, TermT n)] -> (TermT n, [(TermT n, TermT n)])
collectSpine :: forall (n :: S).
TermT n -> [(TermT n, TermT n)] -> (TermT n, [(TermT n, TermT n)])
collectSpine (AppT TypeInfo (AST NameBinder (AnnSig TypeInfo TermSig) n)
_ AST NameBinder (AnnSig TypeInfo TermSig) n
f AST NameBinder (AnnSig TypeInfo TermSig) n
x) [(AST NameBinder (AnnSig TypeInfo TermSig) n,
  AST NameBinder (AnnSig TypeInfo TermSig) n)]
acc = AST NameBinder (AnnSig TypeInfo TermSig) n
-> [(AST NameBinder (AnnSig TypeInfo TermSig) n,
     AST NameBinder (AnnSig TypeInfo TermSig) n)]
-> (AST NameBinder (AnnSig TypeInfo TermSig) n,
    [(AST NameBinder (AnnSig TypeInfo TermSig) n,
      AST NameBinder (AnnSig TypeInfo TermSig) n)])
forall (n :: S).
TermT n -> [(TermT n, TermT n)] -> (TermT n, [(TermT n, TermT n)])
collectSpine AST NameBinder (AnnSig TypeInfo TermSig) n
f ((AST NameBinder (AnnSig TypeInfo TermSig) n
f, AST NameBinder (AnnSig TypeInfo TermSig) n
x) (AST NameBinder (AnnSig TypeInfo TermSig) n,
 AST NameBinder (AnnSig TypeInfo TermSig) n)
-> [(AST NameBinder (AnnSig TypeInfo TermSig) n,
     AST NameBinder (AnnSig TypeInfo TermSig) n)]
-> [(AST NameBinder (AnnSig TypeInfo TermSig) n,
     AST NameBinder (AnnSig TypeInfo TermSig) n)]
forall a. a -> [a] -> [a]
: [(AST NameBinder (AnnSig TypeInfo TermSig) n,
  AST NameBinder (AnnSig TypeInfo TermSig) n)]
acc)
collectSpine AST NameBinder (AnnSig TypeInfo TermSig) n
h [(AST NameBinder (AnnSig TypeInfo TermSig) n,
  AST NameBinder (AnnSig TypeInfo TermSig) n)]
acc            = (AST NameBinder (AnnSig TypeInfo TermSig) n
h, [(AST NameBinder (AnnSig TypeInfo TermSig) n,
  AST NameBinder (AnnSig TypeInfo TermSig) n)]
acc)

-- | Check explicit syntax before typechecking erases source positions and sugars.
-- Report each outermost forbidden node at its nearest available source position.
recordSyntaxUses :: forall n. Distinct n => Term n -> TypeCheck n ()
recordSyntaxUses :: forall (n :: S). Distinct n => Term n -> TypeCheck n ()
recordSyntaxUses Term n
term = do
  enabled <- (Context n -> Bool)
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Context n -> Bool
forall (n :: S). Context n -> Bool
rsttSafeEnabled
  when enabled $ do
    roles <- mapM (\Name n
v -> (,) (Name n -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name n
v) (Maybe (DataRole n) -> (Int, Maybe (DataRole n)))
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe (DataRole n))
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Int, Maybe (DataRole n))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (VarInfo n -> Maybe (DataRole n))
-> Name n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe (DataRole n))
forall (n :: S) a. (VarInfo n -> a) -> Name n -> TypeCheck n a
infoOfVar VarInfo n -> Maybe (DataRole n)
forall (n :: S). VarInfo n -> Maybe (DataRole n)
varDataRole Name n
v)
      (freeVarsOfTerm term)
    let inductive = [Int] -> IntSet
IntSet.fromList [Int
v | (Int
v, Just DataRole n
_) <- [(Int, Maybe (DataRole n))]
roles]
    loc <- asks ctxLocation
    forM_ (go inductive loc term) $ \CheckWarning
warning ->
      (Context n -> Context n)
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a.
(Context n -> Context n)
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (\Context n
ctx -> Context n
ctx { ctxLocation = warningLocation warning }) (ReaderT
   (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
 -> ReaderT
      (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ())
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a b. (a -> b) -> a -> b
$
        CheckWarning
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S). Distinct n => CheckWarning -> TypeCheck n ()
recordCheckWarning CheckWarning
warning
  where
    -- Free names retain their identities under binders; bound names are fresh.
    go :: forall l. IntSet.IntSet -> Maybe LocationInfo -> Term l -> [CheckWarning]
    go :: forall (l :: S).
IntSet -> Maybe LocationInfo -> Term l -> [CheckWarning]
go IntSet
inductive Maybe LocationInfo
loc (Var Name l
v)
      | Name l -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name l
v Int -> IntSet -> Bool
`IntSet.member` IntSet
inductive =
          [RSTTExtension -> String -> Maybe LocationInfo -> CheckWarning
RSTTScopeWarning RSTTExtension
RSTTInductive String
"inductive constructor or eliminator" Maybe LocationInfo
loc]
      | Bool
otherwise = []
    go IntSet
inductive Maybe LocationInfo
loc t :: AST NameBinder (AnnSig SrcPos TermSig) l
t@(Node (AnnSig SrcPos (AST NameBinder (AnnSig SrcPos TermSig) l)
_ TermSig
  (ScopedAST NameBinder (AnnSig SrcPos TermSig) l)
  (AST NameBinder (AnnSig SrcPos TermSig) l)
sig)) =
      let here :: Maybe LocationInfo
here = case AST NameBinder (AnnSig SrcPos TermSig) l -> Maybe RzkPosition
forall (n :: S). Term n -> Maybe RzkPosition
positionOfTerm AST NameBinder (AnnSig SrcPos TermSig) l
t of
            Maybe RzkPosition
Nothing -> Maybe LocationInfo
loc
            Just RzkPosition
pos -> RzkPosition -> LocationInfo -> LocationInfo
atPosition RzkPosition
pos (LocationInfo -> LocationInfo)
-> Maybe LocationInfo -> Maybe LocationInfo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe LocationInfo
loc
      in if TermSig
  (ScopedAST NameBinder (AnnSig SrcPos TermSig) l)
  (AST NameBinder (AnnSig SrcPos TermSig) l)
-> Bool
forall scope term. TermSig scope term -> Bool
allowedNode TermSig
  (ScopedAST NameBinder (AnnSig SrcPos TermSig) l)
  (AST NameBinder (AnnSig SrcPos TermSig) l)
sig
        then (ScopedAST NameBinder (AnnSig SrcPos TermSig) l -> [CheckWarning])
-> (AST NameBinder (AnnSig SrcPos TermSig) l -> [CheckWarning])
-> TermSig
     (ScopedAST NameBinder (AnnSig SrcPos TermSig) l)
     (AST NameBinder (AnnSig SrcPos TermSig) l)
-> [CheckWarning]
forall m a b. Monoid m => (a -> m) -> (b -> m) -> TermSig a b -> m
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap (\(ScopedAST NameBinder l l
_ AST NameBinder (AnnSig SrcPos TermSig) l
body) -> IntSet
-> Maybe LocationInfo
-> AST NameBinder (AnnSig SrcPos TermSig) l
-> [CheckWarning]
forall (l :: S).
IntSet -> Maybe LocationInfo -> Term l -> [CheckWarning]
go IntSet
inductive Maybe LocationInfo
here AST NameBinder (AnnSig SrcPos TermSig) l
body)
                       (IntSet
-> Maybe LocationInfo
-> AST NameBinder (AnnSig SrcPos TermSig) l
-> [CheckWarning]
forall (l :: S).
IntSet -> Maybe LocationInfo -> Term l -> [CheckWarning]
go IntSet
inductive Maybe LocationInfo
here) TermSig
  (ScopedAST NameBinder (AnnSig SrcPos TermSig) l)
  (AST NameBinder (AnnSig SrcPos TermSig) l)
sig
        else let (RSTTExtension
extension, String
feature) = TermSig
  (ScopedAST NameBinder (AnnSig SrcPos TermSig) l)
  (AST NameBinder (AnnSig SrcPos TermSig) l)
-> (RSTTExtension, String)
forall scope term. TermSig scope term -> (RSTTExtension, String)
syntaxExtension TermSig
  (ScopedAST NameBinder (AnnSig SrcPos TermSig) l)
  (AST NameBinder (AnnSig SrcPos TermSig) l)
sig
             in [RSTTExtension -> String -> Maybe LocationInfo -> CheckWarning
RSTTScopeWarning RSTTExtension
extension String
feature Maybe LocationInfo
here]

-- New constructors are forbidden until explicitly admitted here.
allowedNode :: TermSig scope term -> Bool
allowedNode :: forall scope term. TermSig scope term -> Bool
allowedNode = \case
  TermSig scope term
UniverseF -> Bool
True
  TermSig scope term
UniverseCubeF -> Bool
True
  TermSig scope term
UniverseTopeF -> Bool
True
  TermSig scope term
CubeUnitF -> Bool
True
  TermSig scope term
CubeUnitStarF -> Bool
True
  TermSig scope term
Cube2F -> Bool
True
  TermSig scope term
Cube2_0F -> Bool
True
  TermSig scope term
Cube2_1F -> Bool
True
  CubeProductF{} -> Bool
True
  TermSig scope term
TopeTopF -> Bool
True
  TermSig scope term
TopeBottomF -> Bool
True
  TopeEQF{} -> Bool
True
  TopeLEQF{} -> Bool
True
  TopeAndF{} -> Bool
True
  TopeOrF{} -> Bool
True
  TermSig scope term
RecBottomF -> Bool
True
  RecOrF{} -> Bool
True
  TypeFunF Binder
_ TModality
Id term
_ Maybe scope
_ scope
_ -> Bool
True
  TypeSigmaF Binder
_ TModality
Id term
_ scope
_ -> Bool
True
  TypeIdF{} -> Bool
True
  AppF{} -> Bool
True
  LetF{} -> Bool
True
  LambdaF Binder
_ Maybe (LambdaParam scope term)
Nothing scope
_ -> Bool
True
  LambdaF Binder
_ (Just (LambdaParam TModality
Id term
_ Maybe scope
_)) scope
_ -> Bool
True
  PairF{} -> Bool
True
  FirstF{} -> Bool
True
  SecondF{} -> Bool
True
  ReflF{} -> Bool
True
  IdJF{} -> Bool
True
  TermSig scope term
UnitF -> Bool
True
  TermSig scope term
TypeUnitF -> Bool
True
  TypeAscF{} -> Bool
True
  TypeRestrictedF{} -> Bool
True
  -- Hole checking also handles unresolved identifiers and unfinished obligations.
  HoleF{} -> Bool
True
  TermSig scope term
_ -> Bool
False

-- Classification improves diagnostics; it never grants admission.
syntaxExtension :: TermSig scope term -> (RSTTExtension, String)
syntaxExtension :: forall scope term. TermSig scope term -> (RSTTExtension, String)
syntaxExtension = \case
  TypeFunF{} -> String -> (RSTTExtension, String)
forall {b}. b -> (RSTTExtension, b)
modal String
"modal function binder"
  TypeSigmaF{} -> String -> (RSTTExtension, String)
forall {b}. b -> (RSTTExtension, b)
modal String
"modal pair binder"
  LambdaF{} -> String -> (RSTTExtension, String)
forall {b}. b -> (RSTTExtension, b)
modal String
"modal lambda binder"
  TypeModalF{} -> String -> (RSTTExtension, String)
forall {b}. b -> (RSTTExtension, b)
modal String
"modal type"
  ModAppF{} -> String -> (RSTTExtension, String)
forall {b}. b -> (RSTTExtension, b)
modal String
"modal introduction"
  ModExtractF{} -> String -> (RSTTExtension, String)
forall {b}. b -> (RSTTExtension, b)
modal String
"modal extraction"
  LetModF{} -> String -> (RSTTExtension, String)
forall {b}. b -> (RSTTExtension, b)
modal String
"modal let binding"
  TermSig scope term
CubeIF -> String -> (RSTTExtension, String)
forall {b}. b -> (RSTTExtension, b)
interval String
"auxiliary interval II"
  TermSig scope term
CubeI_0F -> String -> (RSTTExtension, String)
forall {b}. b -> (RSTTExtension, b)
interval String
"auxiliary interval endpoint 0_I"
  TermSig scope term
CubeI_1F -> String -> (RSTTExtension, String)
forall {b}. b -> (RSTTExtension, b)
interval String
"auxiliary interval endpoint 1_I"
  CubeFlipF{} -> String -> (RSTTExtension, String)
forall {b}. b -> (RSTTExtension, b)
interval String
"interval flip"
  CubeUnflipF{} -> String -> (RSTTExtension, String)
forall {b}. b -> (RSTTExtension, b)
interval String
"interval unflip"
  TopeInvF{} -> String -> (RSTTExtension, String)
forall {b}. b -> (RSTTExtension, b)
interval String
"tope involution"
  TopeUninvF{} -> String -> (RSTTExtension, String)
forall {b}. b -> (RSTTExtension, b)
interval String
"inverse tope involution"
  MatchF{} -> (RSTTExtension
RSTTInductive, String
"inductive match")
  MatchArmF{} -> (RSTTExtension
RSTTInductive, String
"inductive match arm")
  CubeSupF{} -> (RSTTExtension
RSTTUnsupported, String
"cube supremum (sup)")
  CubeInfF{} -> (RSTTExtension
RSTTUnsupported, String
"cube infimum (inf)")
  TermSig scope term
_ -> (RSTTExtension
RSTTUnsupported, String
"unrecognised syntax")
  where
    modal :: b -> (RSTTExtension, b)
modal = (,) RSTTExtension
RSTTModal
    interval :: b -> (RSTTExtension, b)
interval = (,) RSTTExtension
RSTTInterval