{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}

-- | Check schematic dRzk declarations without treating universes as object types.
-- Family arguments are checked at their indices. Schematic rules,
-- such as function extensionality, may also be parameters of a declaration.
-- Postulates supply trusted rules; checking their statements does not prove them.
module Rzk.TypeCheck.Schematic
  ( SchematicStatement, CheckedSchematicDeclaration, checkSchematicDeclaration
  , recordSchematicDeclaration
  ) where

import Control.Monad (forM_, unless, void, when)
import Control.Monad.Except (catchError)
import Control.Monad.Reader (asks, local)
import Control.Monad.State.Strict (gets)
import Data.Bifoldable (bifoldMap)
import qualified Data.IntMap.Strict as IntMap
import qualified Data.IntSet as IntSet
import Data.Maybe (isJust)
import Control.Monad.Foil (Distinct)
import qualified Control.Monad.Foil as Foil
import Control.Monad.Free.Foil (AST (..))
import Control.Monad.Free.Foil.Annotated (AnnSig (..))
import Language.Rzk.Foil.Names (TModality (..), TypeInfo (..), VarIdent)
import Language.Rzk.Foil.Syntax
import Rzk.TypeCheck.Context
import Rzk.TypeCheck.Display (namingOfContext, ppTerm)
import Rzk.TypeCheck.Error
import Rzk.TypeCheck.Eval
import Rzk.TypeCheck.Fragment.RSTT (fragmentHead)
import Rzk.TypeCheck.Monad

-- Constructors stay private: a statement alone does not certify its proof.
newtype SchematicStatement n = SchematicStatement (TermT n)
data CheckedSchematicDeclaration n = CheckedSchematicDeclaration
  (SchematicStatement n) (Maybe (TermT n))

-- | Roles of elaborated type and domain expressions in schematic declarations.
-- These are not universe levels. The fragment's formation conditions are
-- checked separately in "Rzk.TypeCheck.Fragment.RSTT".
data Sort
  = ObjectType
    -- ^ Ordinary types that may instantiate @(A : U)@, e.g. @Unit@ or @A -> A@.
  | ParameterKind
    -- ^ Kinds of schematic data: @U@, @CUBE@, @TOPE@, family kinds such as
    -- @A -> U@, and dependent sums containing non-object components.
  | SchematicRule
    -- ^ Function signatures ending in an object type and quantifying over
    -- kinds or rules, e.g. @(A : U) -> A -> A@.
  | Shape
    -- ^ Cube or tope expressions, e.g. @2@ or @TOP@; these are not object types.
  deriving (Sort -> Sort -> Bool
(Sort -> Sort -> Bool) -> (Sort -> Sort -> Bool) -> Eq Sort
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Sort -> Sort -> Bool
== :: Sort -> Sort -> Bool
$c/= :: Sort -> Sort -> Bool
/= :: Sort -> Sort -> Bool
Eq)

-- | Validate an elaborated declaration and the definitions used by its proof.
-- This supplements dRzk typing with kind checks; it is not a separate RSTT kernel.
checkSchematicDeclaration
  :: Distinct n => TermT n -> Maybe (TermT n)
  -> TypeCheck n (CheckedSchematicDeclaration n)
checkSchematicDeclaration :: forall (n :: S).
Distinct n =>
TermT n
-> Maybe (TermT n) -> TypeCheck n (CheckedSchematicDeclaration n)
checkSchematicDeclaration TermT n
ty Maybe (TermT n)
value = do
  Int -> IntSet -> TermT n -> Maybe (TermT n) -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> Maybe (TermT n) -> TypeCheck n ()
declaration Int
256 IntSet
IntSet.empty TermT n
ty Maybe (TermT n)
value
  CheckedSchematicDeclaration n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (CheckedSchematicDeclaration n)
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SchematicStatement n
-> Maybe (TermT n) -> CheckedSchematicDeclaration n
forall (n :: S).
SchematicStatement n
-> Maybe (TermT n) -> CheckedSchematicDeclaration n
CheckedSchematicDeclaration (TermT n -> SchematicStatement n
forall (n :: S). TermT n -> SchematicStatement n
SchematicStatement TermT n
ty) Maybe (TermT n)
value)

-- | Apply the selected safe-mode policy and return whether validation succeeded.
recordSchematicDeclaration
  :: Distinct n => VarIdent -> TermT n -> Maybe (TermT n) -> TypeCheck n SchematicStatus
recordSchematicDeclaration :: forall (n :: S).
Distinct n =>
VarIdent
-> TermT n -> Maybe (TermT n) -> TypeCheck n SchematicStatus
recordSchematicDeclaration VarIdent
name TermT n
ty Maybe (TermT n)
value = 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
  if not enabled then pure SchematicUnchecked else
    (checkSchematicDeclaration ty value >> pure SchematicChecked) `catchError` \TypeErrorInScopedContext
err -> do
      case TypeErrorInScopedContext
err of
        TypeErrorInScopedContext Context n
_ (TypeErrorSchematic SchematicProblem
problem FilePath
reason) -> do
          loc <- (Context n -> Maybe LocationInfo)
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe LocationInfo)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Context n -> Maybe LocationInfo
forall (n :: S). Context n -> Maybe LocationInfo
ctxLocation
          warnings <- gets logWarningsRev
          -- Source checking already gives more precise locations for these.
          let alreadyReported = SchematicProblem
problem SchematicProblem -> SchematicProblem -> Bool
forall a. Eq a => a -> a -> Bool
== SchematicProblem
SchematicUnsupported Bool -> Bool -> Bool
&& (CheckWarning -> Bool) -> [CheckWarning] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Maybe LocationInfo -> CheckWarning -> Bool
sameDeclaration Maybe LocationInfo
loc) [CheckWarning]
warnings
          unless alreadyReported $ recordCheckWarning (RSTTScopeWarning RSTTSchematic
            (reason <> " (in " <> show name <> ")") loc)
        TypeErrorInScopedContext
_ -> FilePath
-> TypeErrorInScopedContext
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S).
Distinct n =>
FilePath -> TypeErrorInScopedContext -> TypeCheck n ()
reportIncompleteRSTTCheck (FilePath
"schematic check incomplete in " FilePath -> FilePath -> FilePath
forall a. Semigroup a => a -> a -> a
<> VarIdent -> FilePath
forall a. Show a => a -> FilePath
show VarIdent
name) TypeErrorInScopedContext
err
      SchematicStatus
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     SchematicStatus
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure SchematicStatus
SchematicRejected

sameDeclaration :: Maybe LocationInfo -> CheckWarning -> Bool
sameDeclaration :: Maybe LocationInfo -> CheckWarning -> Bool
sameDeclaration Maybe LocationInfo
loc CheckWarning
warning = Bool
withinDeclaration Bool -> Bool -> Bool
&& case CheckWarning
warning of
  RSTTHoleWarning{} -> Bool
True
  RSTTScopeWarning RSTTExtension
RSTTModal FilePath
_ Maybe LocationInfo
_ -> Bool
True
  RSTTScopeWarning RSTTExtension
RSTTInductive FilePath
_ Maybe LocationInfo
_ -> Bool
True
  RSTTScopeWarning RSTTExtension
RSTTInterval FilePath
_ Maybe LocationInfo
_ -> Bool
True
  RSTTScopeWarning RSTTExtension
RSTTUnsupported FilePath
_ Maybe LocationInfo
_ -> Bool
True
  CheckWarning
_ -> Bool
False
  where
    withinDeclaration :: Bool
withinDeclaration = case (Maybe LocationInfo
loc, CheckWarning -> Maybe LocationInfo
warningLocation CheckWarning
warning) of
      (Just LocationInfo
start, Just LocationInfo
at) -> LocationInfo -> Maybe FilePath
locationFilePath LocationInfo
start Maybe FilePath -> Maybe FilePath -> Bool
forall a. Eq a => a -> a -> Bool
== LocationInfo -> Maybe FilePath
locationFilePath LocationInfo
at Bool -> Bool -> Bool
&&
        LocationInfo -> Maybe Int
locationLine LocationInfo
at Maybe Int -> Maybe Int -> Bool
forall a. Ord a => a -> a -> Bool
>= LocationInfo -> Maybe Int
locationLine LocationInfo
start
      (Maybe LocationInfo, Maybe LocationInfo)
_ -> Maybe LocationInfo
loc Maybe LocationInfo -> Maybe LocationInfo -> Bool
forall a. Eq a => a -> a -> Bool
== CheckWarning -> Maybe LocationInfo
warningLocation CheckWarning
warning

invalid :: Distinct n => String -> TermT n -> TypeCheck n a
invalid :: forall (n :: S) a.
Distinct n =>
FilePath -> TermT n -> TypeCheck n a
invalid = SchematicProblem -> FilePath -> TermT n -> TypeCheck n a
forall (n :: S) a.
Distinct n =>
SchematicProblem -> FilePath -> TermT n -> TypeCheck n a
schematicError SchematicProblem
SchematicKindMismatch

schematicError :: Distinct n => SchematicProblem -> String -> TermT n -> TypeCheck n a
schematicError :: forall (n :: S) a.
Distinct n =>
SchematicProblem -> FilePath -> TermT n -> TypeCheck n a
schematicError SchematicProblem
problem FilePath
reason TermT n
t = do
  naming <- (Context n -> Naming n)
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Naming n)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Context n -> Naming n
forall (n :: S). Context n -> Naming n
namingOfContext
  issueTypeError (TypeErrorSchematic problem (reason <> ": " <> ppTerm naming (untyped t)))

budget :: Distinct n => Int -> TypeCheck n ()
budget :: forall (n :: S). Distinct n => Int -> TypeCheck n ()
budget Int
fuel = Bool
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
fuel Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0) (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
$
  TypeError n
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S) a. Distinct n => TypeError n -> TypeCheck n a
issueTypeError (FilePath -> TypeError n
forall (n :: S). FilePath -> TypeError n
TypeErrorOther FilePath
"schematic validation limit reached")

-- Check a declaration telescope separately: a definition may name a family kind
-- or a schema, whereas an argument to a type parameter must be an object type.
declaration :: Distinct n => Int -> IntSet.IntSet -> TermT n -> Maybe (TermT n) -> TypeCheck n ()
declaration :: forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> Maybe (TermT n) -> TypeCheck n ()
declaration Int
fuel IntSet
seen TermT n
ty Maybe (TermT n)
value = do
  Int -> TypeCheck n ()
forall (n :: S). Distinct n => Int -> TypeCheck n ()
budget Int
fuel
  -- Check dependencies before reduction can obscure their failures.
  TermT n -> TypeCheck n ()
dependencies TermT n
ty
  Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
signature Int
fuel IntSet
seen 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_ (\TermT n
t -> TermT n -> TypeCheck n ()
dependencies TermT n
t TypeCheck n () -> TypeCheck n () -> TypeCheck n ()
forall a b.
ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) 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 -> m b -> m b
>> Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term Int
fuel IntSet
seen TermT n
t) Maybe (TermT n)
value
  where
    dependencies :: TermT n -> TypeCheck n ()
dependencies = (Name n -> TypeCheck n ()) -> [Name n] -> TypeCheck n ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
uses Int
fuel IntSet
seen (TermT n -> TypeCheck n ())
-> (Name n -> TermT n) -> Name n -> TypeCheck n ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name n -> TermT n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var) ([Name n] -> TypeCheck n ())
-> (TermT n -> [Name n]) -> TermT n -> TypeCheck n ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TermT n -> [Name n]
forall (n :: S). TermT n -> [Name n]
freeVarsOfTermT

signature :: Distinct n => Int -> IntSet.IntSet -> TermT n -> TypeCheck n ()
signature :: forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
signature Int
fuel IntSet
seen TermT n
ty = do
  Int -> TypeCheck n ()
forall (n :: S). Distinct n => Int -> TypeCheck n ()
budget Int
fuel
  Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
uses Int
fuel IntSet
seen TermT n
ty
  Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
inspectRedex Int
fuel IntSet
seen TermT n
ty
  Int -> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
fragmentHead Int
fuel TermT n
ty TypeCheck n (TermT n)
-> (TermT n -> TypeCheck n ()) -> TypeCheck 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
    TypeFunT TypeInfo (TermT n)
_ Binder
orig TModality
md TermT n
dom Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n)
guard ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
body -> do
      ReaderT
  (Context n)
  (ExceptT TypeErrorInScopedContext (State CheckLog))
  Sort
-> TypeCheck n ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Int
-> IntSet
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Sort
classify (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
dom)
      (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
 -> TypeCheck n ())
-> Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n)
-> TypeCheck n ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
g -> Binder
-> TModality
-> TermT n
-> ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    AST NameBinder (AnnSig TypeInfo TermSig) l -> TypeCheck l ())
-> TypeCheck n ()
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 n
dom ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
g (Int
-> IntSet
-> AST NameBinder (AnnSig TypeInfo TermSig) l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen)) Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n)
guard
      Binder
-> TModality
-> TermT n
-> ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    AST NameBinder (AnnSig TypeInfo TermSig) l -> TypeCheck l ())
-> TypeCheck n ()
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 n
dom ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
body (Int -> IntSet -> TermT l -> TypeCheck l ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
signature (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen)
    TermT n
t -> ReaderT
  (Context n)
  (ExceptT TypeErrorInScopedContext (State CheckLog))
  Sort
-> TypeCheck n ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Int
-> IntSet
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Sort
classify (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
t)

-- Check each dependency before using its signature, even if safe mode was off
-- when it was declared. Successful earlier checks are cached in the context.
-- Check variables during traversal instead of rescanning every subtree.
uses :: Distinct n => Int -> IntSet.IntSet -> TermT n -> TypeCheck n ()
uses :: forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
uses Int
fuel IntSet
seen (Var Name n
v) = do
  info <- (VarInfo n -> VarInfo n) -> Name n -> TypeCheck n (VarInfo n)
forall (n :: S) a. (VarInfo n -> a) -> Name n -> TypeCheck n a
infoOfVar VarInfo n -> VarInfo n
forall a. a -> a
id Name n
v
  cached <- gets (IntSet.member (Foil.nameId v) . logSchematicCache)
  when (varIsTopLevel info && varSchematicStatus info /= SchematicChecked && not cached) $ do
    when (varSchematicStatus info == SchematicRejected) $
      schematicError SchematicDependency "dependency failed schematic validation" (Var v)
    when (isJust (varDataRole info)) $
      schematicError SchematicUnsupported "inductive dependency" (Var v)
    budget fuel
    when (IntSet.member (Foil.nameId v) seen) $
      issueTypeError (TypeErrorOther "cyclic schematic dependency")
    -- A caller's tope assumptions cannot justify a reusable dependency check.
    local withoutCallerTopes $ declaration (fuel - 1) (IntSet.insert (Foil.nameId v) seen)
      (varType info) (varValue info)
    modifyLog $ \CheckLog
checkLog' -> CheckLog
checkLog'
      { logSchematicCache = IntSet.insert (Foil.nameId v) (logSchematicCache checkLog') }
uses Int
_ IntSet
_ AST NameBinder (AnnSig TypeInfo TermSig) n
_ = ()
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

withoutCallerTopes :: Context n -> Context n
withoutCallerTopes :: forall (n :: S). Context n -> Context n
withoutCallerTopes Context n
ctx = Context n
ctx
  { ctxDiscreteTopes = [], ctxTopes = [], ctxTopesNF = []
  , ctxTopesNFUnion = [[]], ctxTopesEntailBottom = Just False
  , ctxTopesSaturated = SaturationUncached
  }

-- Check erased arguments and annotations before exposing the result.
inspectRedex :: Distinct n => Int -> IntSet.IntSet -> TermT n -> TypeCheck n ()
inspectRedex :: forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
inspectRedex Int
fuel IntSet
seen TermT n
original = case TermT n
original of
  LetT{} -> Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term Int
fuel IntSet
seen TermT n
original
  TypeAscT{} -> Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term Int
fuel IntSet
seen TermT n
original
  FirstT{} -> Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term Int
fuel IntSet
seen TermT n
original
  SecondT{} -> Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term Int
fuel IntSet
seen TermT n
original
  IdJT{} -> Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term Int
fuel IntSet
seen TermT n
original
  RecOrT{} -> Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term Int
fuel IntSet
seen TermT n
original
  TermT n
_ -> Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
applications Int
fuel IntSet
seen TermT n
original

classify :: Distinct n => Int -> IntSet.IntSet -> TermT n -> TypeCheck n Sort
classify :: forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Sort
classify Int
fuel IntSet
seen TermT n
original = do
  Int -> TypeCheck n ()
forall (n :: S). Distinct n => Int -> TypeCheck n ()
budget Int
fuel
  Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
uses Int
fuel IntSet
seen TermT n
original
  Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
inspectRedex Int
fuel IntSet
seen TermT n
original
  Int -> IntSet -> TermT n -> TypeCheck n Bool
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Bool
knownObjectFamily Int
fuel IntSet
seen TermT n
original TypeCheck n Bool
-> (Bool
    -> ReaderT
         (Context n)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         Sort)
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
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
    Bool
True -> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
ObjectType
    Bool
False -> Int
-> IntSet
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Sort
classifyHead Int
fuel IntSet
seen TermT n
original

classifyHead :: Distinct n => Int -> IntSet.IntSet -> TermT n -> TypeCheck n Sort
classifyHead :: forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Sort
classifyHead Int
fuel IntSet
seen TermT n
original =
  Int -> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
fragmentHead Int
fuel TermT n
original TypeCheck n (TermT n)
-> (TermT n
    -> ReaderT
         (Context n)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         Sort)
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
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
    UniverseT{} -> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
ParameterKind
    UniverseCubeT{} -> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
ParameterKind
    UniverseTopeT{} -> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
ParameterKind
    TypeFunT TypeInfo (TermT n)
_ Binder
orig TModality
md TermT n
dom Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n)
guard ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
body -> do
      d <- Int
-> IntSet
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Sort
classify (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
dom
      mapM_ (\ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
g -> Binder
-> TModality
-> TermT n
-> ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    AST NameBinder (AnnSig TypeInfo TermSig) l -> TypeCheck l ())
-> ReaderT
     (Context n) (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 n
dom ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
g (Int
-> IntSet
-> AST NameBinder (AnnSig TypeInfo TermSig) l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen)) guard
      r <- inScope orig md dom body (classify (fuel - 1) seen)
      case (d, r) of
        (Sort
ObjectType, Sort
ObjectType) -> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
ObjectType
        (Sort
Shape, Sort
ObjectType) -> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
ObjectType
        (Sort
_, Sort
ParameterKind) -> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
ParameterKind
        (Sort
_, Sort
Shape) -> FilePath
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall (n :: S) a.
Distinct n =>
FilePath -> TermT n -> TypeCheck n a
invalid FilePath
"cube or shape family" TermT n
original
        (Sort, Sort)
_ -> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
SchematicRule
    TypeSigmaT TypeInfo (TermT n)
_ Binder
orig TModality
md TermT n
a ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
b -> do
      d <- Int
-> IntSet
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Sort
classify (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
a
      r <- inScope orig md a b (classify (fuel - 1) seen)
      pure (if d == ObjectType && r == ObjectType then ObjectType else ParameterKind)
    TypeIdT TypeInfo (TermT n)
_ TermT n
a Maybe (TermT n)
mt TermT n
b -> do
      ty <- 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 (Int -> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
schematicTypeOf Int
fuel TermT n
a) 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 Maybe (TermT n)
mt
      object (fuel - 1) seen ty
      term (fuel - 1) seen a
      term (fuel - 1) seen b
      pure ObjectType
    TypeUnitT{} -> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
ObjectType
    RecBottomT{} -> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
ObjectType
    RecOrT TypeInfo (TermT n)
_ [(TermT n, TermT n)]
branches -> do
      sorts <- ((TermT n, TermT n)
 -> ReaderT
      (Context n)
      (ExceptT TypeErrorInScopedContext (State CheckLog))
      Sort)
-> [(TermT n, TermT n)]
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     [Sort]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (\(TermT n
p, TermT n
v) -> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall (n :: S) a.
Distinct n =>
TermT n -> TypeCheck n a -> TypeCheck n a
localTope TermT n
p (Int
-> IntSet
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Sort
classify (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
v)) [(TermT n, TermT n)]
branches
      case sorts of
        Sort
s : [Sort]
rest | (Sort -> Bool) -> [Sort] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Sort -> Sort -> Bool
forall a. Eq a => a -> a -> Bool
== Sort
s) [Sort]
rest -> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
s
        [Sort]
_ -> FilePath
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall (n :: S) a.
Distinct n =>
FilePath -> TermT n -> TypeCheck n a
invalid FilePath
"case split between different schematic kinds" TermT n
original
    TypeRestrictedT TypeInfo (TermT n)
_ TermT n
ty [(TermT n, TermT n)]
rs -> do
      s <- Int
-> IntSet
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Sort
classify (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
ty
      forM_ rs $ \(TermT n
p, TermT n
v) -> Int
-> IntSet
-> TermT n
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
p ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a b.
ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) 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 -> m b -> m b
>> Int
-> IntSet
-> TermT n
-> TermT n
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TermT n -> TypeCheck n ()
argument (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
ty TermT n
v
      pure s
    CubeUnitT{} -> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
Shape
    Cube2T{} -> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
Shape
    CubeProductT TypeInfo (TermT n)
_ TermT n
a TermT n
b -> Int
-> IntSet
-> TermT n
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
a ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall a b.
ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) 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 -> m b -> m b
>> Int
-> IntSet
-> TermT n
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
b ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a b.
ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) 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 -> m b -> m b
>> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
Shape
    TermT n
t -> do
      Int
-> IntSet
-> TermT n
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
termNeutral (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
t
      Int -> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
schematicTypeOf Int
fuel TermT n
t 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
>>= Int -> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
fragmentHead Int
fuel TypeCheck n (TermT n)
-> (TermT n
    -> ReaderT
         (Context n)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         Sort)
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
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
        UniverseT{} -> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
ObjectType
        UniverseCubeT{} -> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
Shape
        UniverseTopeT{} -> Sort
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sort
Shape
        TermT n
_ -> FilePath
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall (n :: S) a.
Distinct n =>
FilePath -> TermT n -> TypeCheck n a
invalid FilePath
"expected an object type, parameter kind, or schematic rule" TermT n
t

-- Reuse only universally object-valued families, after checking actual arguments.
-- Other families retain reduction-based classification in the caller's context.
knownObjectFamily :: Distinct n => Int -> IntSet.IntSet -> TermT n -> TypeCheck n Bool
knownObjectFamily :: forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Bool
knownObjectFamily Int
fuel IntSet
seen = Int
-> AST NameBinder (AnnSig TypeInfo TermSig) n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
spine Int
0
  where
    spine :: Int
-> AST NameBinder (AnnSig TypeInfo TermSig) n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
spine Int
arity (AppT TypeInfo (AST NameBinder (AnnSig TypeInfo TermSig) n)
_ AST NameBinder (AnnSig TypeInfo TermSig) n
f AST NameBinder (AnnSig TypeInfo TermSig) n
_) = Int
-> AST NameBinder (AnnSig TypeInfo TermSig) n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
spine (Int
arity Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) AST NameBinder (AnnSig TypeInfo TermSig) n
f
    spine Int
arity (Var Name n
v) = do
      info <- (VarInfo n -> VarInfo n) -> Name n -> TypeCheck n (VarInfo n)
forall (n :: S) a. (VarInfo n -> a) -> Name n -> TypeCheck n a
infoOfVar VarInfo n -> VarInfo n
forall a. a -> a
id Name n
v
      if not (varIsTopLevel info) then pure False else do
        let key = Name n -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name n
v
        cached <- gets (IntMap.lookup key . logSchematicObjectFamilies)
        summary <- case cached of
          Just Maybe Int
result -> Maybe Int
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe Int)
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Int
result
          Maybe (Maybe Int)
Nothing -> do
            Int -> Maybe Int -> TypeCheck n ()
forall {n :: S}. Int -> Maybe Int -> TypeCheck n ()
cache Int
key Maybe Int
forall a. Maybe a
Nothing
            result <- ((Context n -> Context n)
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe Int)
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe Int)
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 -> Context n
forall (n :: S). Context n -> Context n
withoutCallerTopes (ReaderT
   (Context n)
   (ExceptT TypeErrorInScopedContext (State CheckLog))
   (Maybe Int)
 -> ReaderT
      (Context n)
      (ExceptT TypeErrorInScopedContext (State CheckLog))
      (Maybe Int))
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe Int)
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe Int)
forall a b. (a -> b) -> a -> b
$
              Int
-> IntSet
-> Int
-> AST NameBinder (AnnSig TypeInfo TermSig) n
-> AST NameBinder (AnnSig TypeInfo TermSig) n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe Int)
forall (n :: S).
Distinct n =>
Int
-> IntSet -> Int -> TermT n -> TermT n -> TypeCheck n (Maybe Int)
objectFamilyArity Int
fuel IntSet
seen Int
0 (VarInfo n -> AST NameBinder (AnnSig TypeInfo TermSig) n
forall (n :: S). VarInfo n -> TermT n
varType VarInfo n
info) (Name n -> AST NameBinder (AnnSig TypeInfo TermSig) n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var Name n
v))
              ReaderT
  (Context n)
  (ExceptT TypeErrorInScopedContext (State CheckLog))
  (Maybe Int)
-> (TypeErrorInScopedContext
    -> ReaderT
         (Context n)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         (Maybe Int))
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe Int)
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` \TypeErrorInScopedContext
_ -> Maybe Int
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe Int)
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Int
forall a. Maybe a
Nothing
            cache key result
            pure result
        pure (summary == Just arity)
    spine Int
_ AST NameBinder (AnnSig TypeInfo TermSig) n
_ = Bool
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Bool
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
    cache :: Int -> Maybe Int -> TypeCheck n ()
cache Int
key Maybe Int
result = (CheckLog -> CheckLog) -> TypeCheck n ()
forall (n :: S). (CheckLog -> CheckLog) -> TypeCheck n ()
modifyLog ((CheckLog -> CheckLog) -> TypeCheck n ())
-> (CheckLog -> CheckLog) -> TypeCheck n ()
forall a b. (a -> b) -> a -> b
$ \CheckLog
checkLog' -> CheckLog
checkLog'
      { logSchematicObjectFamilies = IntMap.insert key result (logSchematicObjectFamilies checkLog') }

-- Probe at fresh parameters; never generalise a concrete instance.
objectFamilyArity :: Distinct n => Int -> IntSet.IntSet -> Int -> TermT n -> TermT n -> TypeCheck n (Maybe Int)
objectFamilyArity :: forall (n :: S).
Distinct n =>
Int
-> IntSet -> Int -> TermT n -> TermT n -> TypeCheck n (Maybe Int)
objectFamilyArity Int
fuel IntSet
seen Int
arity TermT n
ty TermT n
value = do
  Int -> TypeCheck n ()
forall (n :: S). Distinct n => Int -> TypeCheck n ()
budget Int
fuel
  Int -> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
fragmentHead Int
fuel TermT n
ty TypeCheck n (TermT n)
-> (TermT n
    -> ReaderT
         (Context n)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         (Maybe Int))
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe Int)
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
    TypeFunT TypeInfo (TermT n)
_ Binder
orig TModality
md TermT n
dom Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n)
_ ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
body ->
      Binder
-> TModality
-> TermT n
-> ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    AST NameBinder (AnnSig TypeInfo TermSig) l
    -> TypeCheck l (Maybe Int))
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe Int)
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 n
dom ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
body ((forall (l :: S).
  (DExt n l, Distinct l) =>
  AST NameBinder (AnnSig TypeInfo TermSig) l
  -> TypeCheck l (Maybe Int))
 -> ReaderT
      (Context n)
      (ExceptT TypeErrorInScopedContext (State CheckLog))
      (Maybe Int))
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    AST NameBinder (AnnSig TypeInfo TermSig) l
    -> TypeCheck l (Maybe Int))
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe Int)
forall a b. (a -> b) -> a -> b
$ \AST NameBinder (AnnSig TypeInfo TermSig) l
cod -> do
        bound <- (Context l -> [Name l])
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     [Name l]
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Context l -> [Name l]
forall (n :: S). Context n -> [Name n]
ctxBound
        case bound of
          Name l
v : [Name l]
_ -> Int
-> IntSet
-> Int
-> AST NameBinder (AnnSig TypeInfo TermSig) l
-> AST NameBinder (AnnSig TypeInfo TermSig) l
-> TypeCheck l (Maybe Int)
forall (n :: S).
Distinct n =>
Int
-> IntSet -> Int -> TermT n -> TermT n -> TypeCheck n (Maybe Int)
objectFamilyArity (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen (Int
arity Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) AST NameBinder (AnnSig TypeInfo TermSig) l
cod
            (AST NameBinder (AnnSig TypeInfo TermSig) l
-> AST NameBinder (AnnSig TypeInfo TermSig) l
-> AST NameBinder (AnnSig TypeInfo TermSig) l
-> AST NameBinder (AnnSig TypeInfo TermSig) l
forall (n :: S). TermT n -> TermT n -> TermT n -> TermT n
appT AST NameBinder (AnnSig TypeInfo TermSig) l
cod (TermT n -> 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 n
value) (Name l -> AST NameBinder (AnnSig TypeInfo TermSig) l
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var Name l
v))
          [] -> TypeError l -> TypeCheck l (Maybe Int)
forall (n :: S) a. Distinct n => TypeError n -> TypeCheck n a
issueTypeError (FilePath -> TypeError l
forall (n :: S). FilePath -> TypeError n
TypeErrorOther FilePath
"missing schematic index")
    UniverseT{} -> do
      s <- Int -> IntSet -> TermT n -> TypeCheck n Sort
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Sort
classify (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
value
      pure (if s == ObjectType then Just arity else Nothing)
    TermT n
_ -> Maybe Int
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Maybe Int)
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Int
forall a. Maybe a
Nothing

object :: Distinct n => Int -> IntSet.IntSet -> TermT n -> TypeCheck n ()
object :: forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
object Int
fuel IntSet
seen TermT n
t = do
  s <- Int -> IntSet -> TermT n -> TypeCheck n Sort
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Sort
classify (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
t
  unless (s == ObjectType) (invalid "expected an object type" t)

-- A type-family argument is checked pointwise. In particular U, A -> U, and
-- (A : U) -> A cannot instantiate a parameter declared simply as A : U.
argument :: Distinct n => Int -> IntSet.IntSet -> TermT n -> TermT n -> TypeCheck n ()
argument :: forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TermT n -> TypeCheck n ()
argument Int
fuel IntSet
seen TermT n
dom TermT n
arg = do
  Int -> TypeCheck n ()
forall (n :: S). Distinct n => Int -> TypeCheck n ()
budget Int
fuel
  Int -> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
functionHead Int
fuel TermT n
dom TypeCheck n (TermT n)
-> (TermT n -> TypeCheck n ()) -> TypeCheck 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
    UniverseT{} -> Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
object (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
arg
    TypeFunT TypeInfo (TermT n)
_ Binder
orig TModality
md TermT n
a Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n)
guard ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
body -> do
      s <- Int -> IntSet -> TermT n -> TypeCheck n Sort
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Sort
classify (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
dom
      if s == ParameterKind then do
        -- Eta-expand only for checking; the ordinary checker has checked the
        -- argument's type. The body is inspected with the same fresh index.
        inScope orig md a body $ \AST NameBinder (AnnSig TypeInfo TermSig) l
cod -> do
          ctx <- (Context l -> Context l)
-> ReaderT
     (Context l)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (Context l)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Context l -> Context l
forall a. a -> a
id
          case ctxBound ctx of
            Name l
v : [Name l]
_ -> Int
-> IntSet
-> AST NameBinder (AnnSig TypeInfo TermSig) l
-> AST NameBinder (AnnSig TypeInfo TermSig) l
-> TypeCheck l ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TermT n -> TypeCheck n ()
argument (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen AST NameBinder (AnnSig TypeInfo TermSig) l
cod (AST NameBinder (AnnSig TypeInfo TermSig) l
-> AST NameBinder (AnnSig TypeInfo TermSig) l
-> AST NameBinder (AnnSig TypeInfo TermSig) l
-> AST NameBinder (AnnSig TypeInfo TermSig) l
forall (n :: S). TermT n -> TermT n -> TermT n -> TermT n
appT AST NameBinder (AnnSig TypeInfo TermSig) l
cod (TermT n -> 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 n
arg) (Name l -> AST NameBinder (AnnSig TypeInfo TermSig) l
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var Name l
v))
            [] -> TypeError l -> TypeCheck l ()
forall (n :: S) a. Distinct n => TypeError n -> TypeCheck n a
issueTypeError (FilePath -> TypeError l
forall (n :: S). FilePath -> TypeError n
TypeErrorOther FilePath
"missing schematic index")
        mapM_ (\ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
g -> Binder
-> TModality
-> TermT n
-> ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    AST NameBinder (AnnSig TypeInfo TermSig) l -> TypeCheck l ())
-> TypeCheck n ()
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 n
a ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
g (Int
-> IntSet
-> AST NameBinder (AnnSig TypeInfo TermSig) l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen)) guard
      else term (fuel - 1) seen arg
    TermT n
_ -> Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
arg

-- Section abstraction can leave application annotations describing an earlier
-- telescope. Derive elimination types from the current signatures instead.
-- TODO: Share this reconstruction with Eval when repairing annotations there.
schematicTypeOf :: Distinct n => Int -> TermT n -> TypeCheck n (TermT n)
schematicTypeOf :: forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
schematicTypeOf Int
fuel TermT n
t = do
  Int -> TypeCheck n ()
forall (n :: S). Distinct n => Int -> TypeCheck n ()
budget Int
fuel
  case TermT n
t of
    AppT TypeInfo (TermT n)
_ TermT n
f TermT n
x -> Int
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
schematicTypeOf (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) TermT n
f ReaderT
  (Context n)
  (ExceptT TypeErrorInScopedContext (State CheckLog))
  (TermT n)
-> (TermT n
    -> ReaderT
         (Context n)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         (TermT n))
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (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
>>= Int
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
functionHead Int
fuel ReaderT
  (Context n)
  (ExceptT TypeErrorInScopedContext (State CheckLog))
  (TermT n)
-> (TermT n
    -> ReaderT
         (Context n)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         (TermT n))
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (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
      TypeFunT TypeInfo (TermT n)
_ Binder
_ TModality
_ TermT n
_ Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n)
_ ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
ret -> ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT n)
forall (n :: S).
Distinct n =>
ScopedTermT n -> TermT n -> TypeCheck n (TermT n)
instantiate ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
ret TermT n
x
      TermT n
_ -> FilePath
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT n)
forall (n :: S) a.
Distinct n =>
FilePath -> TermT n -> TypeCheck n a
invalid FilePath
"application without a function type" TermT n
f
    FirstT TypeInfo (TermT n)
_ TermT n
p -> Int
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
schematicTypeOf (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) TermT n
p ReaderT
  (Context n)
  (ExceptT TypeErrorInScopedContext (State CheckLog))
  (TermT n)
-> (TermT n
    -> ReaderT
         (Context n)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         (TermT n))
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (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
>>= Int
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
functionHead Int
fuel ReaderT
  (Context n)
  (ExceptT TypeErrorInScopedContext (State CheckLog))
  (TermT n)
-> (TermT n
    -> ReaderT
         (Context n)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         (TermT n))
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (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
      TypeSigmaT TypeInfo (TermT n)
_ Binder
_ TModality
_ TermT n
a ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
_ -> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT n)
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TermT n
a
      CubeProductT TypeInfo (TermT n)
_ TermT n
a TermT n
_ -> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT n)
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TermT n
a
      TermT n
_ -> FilePath
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT n)
forall (n :: S) a.
Distinct n =>
FilePath -> TermT n -> TypeCheck n a
invalid FilePath
"projection without a product type" TermT n
p
    SecondT TypeInfo (TermT n)
_ TermT n
p -> Int
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
schematicTypeOf (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) TermT n
p ReaderT
  (Context n)
  (ExceptT TypeErrorInScopedContext (State CheckLog))
  (TermT n)
-> (TermT n
    -> ReaderT
         (Context n)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         (TermT n))
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (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
>>= Int
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
functionHead Int
fuel ReaderT
  (Context n)
  (ExceptT TypeErrorInScopedContext (State CheckLog))
  (TermT n)
-> (TermT n
    -> ReaderT
         (Context n)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         (TermT n))
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (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
      TypeSigmaT TypeInfo (TermT n)
_ Binder
_ TModality
_ TermT n
a ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
b -> ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT n)
forall (n :: S).
Distinct n =>
ScopedTermT n -> TermT n -> TypeCheck n (TermT n)
instantiate ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
b (TermT n -> TermT n -> TermT n
forall (n :: S). TermT n -> TermT n -> TermT n
firstT TermT n
a TermT n
p)
      CubeProductT TypeInfo (TermT n)
_ TermT n
_ TermT n
b -> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT n)
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TermT n
b
      TermT n
_ -> FilePath
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT n)
forall (n :: S) a.
Distinct n =>
FilePath -> TermT n -> TypeCheck n a
invalid FilePath
"projection without a product type" TermT n
p
    TermT n
_ -> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TermT n)
forall (n :: S). TermT n -> TypeCheck n (TermT n)
typeOfUncomputed TermT n
t

-- Boundaries constrain a function without changing its parameter kinds.
functionHead :: Distinct n => Int -> TermT n -> TypeCheck n (TermT n)
functionHead :: forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
functionHead Int
fuel TermT n
t = do
  Int -> TypeCheck n ()
forall (n :: S). Distinct n => Int -> TypeCheck n ()
budget Int
fuel
  Int -> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
fragmentHead Int
fuel TermT n
t 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
    TypeRestrictedT TypeInfo (TermT n)
_ TermT n
base [(TermT n, TermT n)]
_ -> Int -> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
functionHead (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) TermT n
base
    TermT n
head' -> 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
head'

applications :: Distinct n => Int -> IntSet.IntSet -> TermT n -> TypeCheck n ()
applications :: forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
applications Int
fuel IntSet
seen (AppT TypeInfo (AST NameBinder (AnnSig TypeInfo TermSig) n)
_ AST NameBinder (AnnSig TypeInfo TermSig) n
f AST NameBinder (AnnSig TypeInfo TermSig) n
x) = do
  Int -> TypeCheck n ()
forall (n :: S). Distinct n => Int -> TypeCheck n ()
budget Int
fuel
  Int
-> IntSet
-> AST NameBinder (AnnSig TypeInfo TermSig) n
-> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen AST NameBinder (AnnSig TypeInfo TermSig) n
f
  Int
-> AST NameBinder (AnnSig TypeInfo TermSig) n
-> TypeCheck n (AST NameBinder (AnnSig TypeInfo TermSig) n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
schematicTypeOf Int
fuel AST NameBinder (AnnSig TypeInfo TermSig) n
f TypeCheck n (AST NameBinder (AnnSig TypeInfo TermSig) n)
-> (AST NameBinder (AnnSig TypeInfo TermSig) n
    -> TypeCheck n (AST NameBinder (AnnSig TypeInfo TermSig) n))
-> TypeCheck n (AST NameBinder (AnnSig TypeInfo TermSig) 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
>>= Int
-> AST NameBinder (AnnSig TypeInfo TermSig) n
-> TypeCheck n (AST NameBinder (AnnSig TypeInfo TermSig) n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
functionHead Int
fuel TypeCheck n (AST NameBinder (AnnSig TypeInfo TermSig) n)
-> (AST NameBinder (AnnSig TypeInfo TermSig) n -> TypeCheck n ())
-> TypeCheck 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
    TypeFunT TypeInfo (AST NameBinder (AnnSig TypeInfo TermSig) n)
_ Binder
_ TModality
_ AST NameBinder (AnnSig TypeInfo TermSig) n
dom Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n)
_ ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
_ -> Int
-> IntSet
-> AST NameBinder (AnnSig TypeInfo TermSig) n
-> AST NameBinder (AnnSig TypeInfo TermSig) n
-> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TermT n -> TypeCheck n ()
argument (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen AST NameBinder (AnnSig TypeInfo TermSig) n
dom AST NameBinder (AnnSig TypeInfo TermSig) n
x
    AST NameBinder (AnnSig TypeInfo TermSig) n
_ -> FilePath
-> AST NameBinder (AnnSig TypeInfo TermSig) n -> TypeCheck n ()
forall (n :: S) a.
Distinct n =>
FilePath -> TermT n -> TypeCheck n a
invalid FilePath
"application without a schematic or object function type" AST NameBinder (AnnSig TypeInfo TermSig) n
f
applications Int
_ IntSet
_ AST NameBinder (AnnSig TypeInfo TermSig) n
_ = () -> TypeCheck n ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

term :: Distinct n => Int -> IntSet.IntSet -> TermT n -> TypeCheck n ()
term :: forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term Int
fuel IntSet
seen TermT n
t = do
  Int -> TypeCheck n ()
forall (n :: S). Distinct n => Int -> TypeCheck n ()
budget Int
fuel
  Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
uses Int
fuel IntSet
seen TermT n
t
  case TermT n
t of
    TypeFunT{} -> ReaderT
  (Context n)
  (ExceptT TypeErrorInScopedContext (State CheckLog))
  Sort
-> TypeCheck n ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Int
-> IntSet
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Sort
classify (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
t)
    TypeSigmaT{} -> ReaderT
  (Context n)
  (ExceptT TypeErrorInScopedContext (State CheckLog))
  Sort
-> TypeCheck n ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Int
-> IntSet
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Sort
classify (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
t)
    TypeIdT{} -> ReaderT
  (Context n)
  (ExceptT TypeErrorInScopedContext (State CheckLog))
  Sort
-> TypeCheck n ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Int
-> IntSet
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Sort
classify (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
t)
    TypeRestrictedT{} -> ReaderT
  (Context n)
  (ExceptT TypeErrorInScopedContext (State CheckLog))
  Sort
-> TypeCheck n ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Int
-> IntSet
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     Sort
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n Sort
classify (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
t)
    LambdaT TypeInfo (TermT n)
info Binder
orig Maybe
  (LambdaParam
     (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n) (TermT n))
param ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
body -> do
      (md, dom) <- case Maybe
  (LambdaParam
     (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n) (TermT n))
param of
        Just (LambdaParam TModality
md TermT n
dom Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n)
guard) -> do
          (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
 -> TypeCheck n ())
-> Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n)
-> TypeCheck n ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
g -> Binder
-> TModality
-> TermT n
-> ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    AST NameBinder (AnnSig TypeInfo TermSig) l -> TypeCheck l ())
-> TypeCheck n ()
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 n
dom ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
g (Int
-> IntSet
-> AST NameBinder (AnnSig TypeInfo TermSig) l
-> ReaderT
     (Context l) (ExceptT TypeErrorInScopedContext (State CheckLog)) ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen)) Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n)
guard
          (TModality, TermT n)
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TModality, TermT n)
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TModality
md, TermT n
dom)
        Maybe
  (LambdaParam
     (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n) (TermT n))
Nothing -> Int -> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
functionHead Int
fuel (TypeInfo (TermT n) -> TermT n
forall term. TypeInfo term -> term
infoType TypeInfo (TermT n)
info) TypeCheck n (TermT n)
-> (TermT n
    -> ReaderT
         (Context n)
         (ExceptT TypeErrorInScopedContext (State CheckLog))
         (TModality, TermT n))
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TModality, 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
          TypeFunT TypeInfo (TermT n)
_ Binder
_ TModality
md TermT n
dom Maybe (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n)
_ ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
_ -> (TModality, TermT n)
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TModality, TermT n)
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TModality
md, TermT n
dom)
          TermT n
_ -> FilePath
-> TermT n
-> ReaderT
     (Context n)
     (ExceptT TypeErrorInScopedContext (State CheckLog))
     (TModality, TermT n)
forall (n :: S) a.
Distinct n =>
FilePath -> TermT n -> TypeCheck n a
invalid FilePath
"lambda without a function type" TermT n
t
      void (classify (fuel - 1) seen dom)
      inScope orig md dom body (term (fuel - 1) seen)
    AppT{} -> Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
applications Int
fuel IntSet
seen TermT n
t
    LetT TypeInfo (TermT n)
_ Binder
orig Maybe (TermT n)
annotation TermT n
value ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
body -> do
      (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_ (Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
signature (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen) Maybe (TermT n)
annotation
      Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
value
      ty <- 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 (Int -> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
schematicTypeOf Int
fuel TermT n
value) 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 Maybe (TermT n)
annotation
      inScopeWith orig Id ty (Just value) body (term (fuel - 1) seen)
    TypeAscT TypeInfo (TermT n)
_ TermT n
value TermT n
ty -> Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
signature (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
ty TypeCheck n () -> TypeCheck n () -> TypeCheck n ()
forall a b.
ReaderT
  (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) 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 -> m b -> m b
>> Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
value
    IdJT TypeInfo (TermT n)
_ TermT n
a TermT n
x TermT n
motive TermT n
d TermT n
y TermT n
p -> do
      Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
object (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
a
      -- J eliminates equality into object types, not into schematic kinds.
      motiveType <- Int -> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
schematicTypeOf Int
fuel TermT n
motive
      argument (fuel - 1) seen motiveType motive
      mapM_ (term (fuel - 1) seen) [x, motive, d, y, p]
    TermT n
_ -> Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
termNeutral Int
fuel IntSet
seen TermT n
t

-- This is deliberately an allow-list. New syntax cannot inherit certification.
termNeutral :: Distinct n => Int -> IntSet.IntSet -> TermT n -> TypeCheck n ()
termNeutral :: forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
termNeutral Int
fuel IntSet
seen TermT n
t = case TermT n
t of
  Var{} -> () -> TypeCheck n ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  UniverseT{} -> () -> TypeCheck n ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  UniverseCubeT{} -> () -> TypeCheck n ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  UniverseTopeT{} -> () -> TypeCheck n ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  CubeUnitT{} -> () -> TypeCheck n ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  CubeUnitStarT{} -> () -> TypeCheck n ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  Cube2T{} -> () -> TypeCheck n ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  Cube2_0T{} -> () -> TypeCheck n ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  Cube2_1T{} -> () -> TypeCheck n ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  TypeUnitT{} -> () -> TypeCheck n ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  UnitT{} -> () -> TypeCheck n ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  TopeTopT{} -> () -> TypeCheck n ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  TopeBottomT{} -> () -> TypeCheck n ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  CubeProductT{} -> TypeCheck n ()
children
  CubeSupT{} -> TypeCheck n ()
children
  CubeInfT{} -> TypeCheck n ()
children
  TopeEQT{} -> TypeCheck n ()
children
  TopeLEQT{} -> TypeCheck n ()
children
  TopeAndT{} -> TypeCheck n ()
children
  TopeOrT{} -> TypeCheck n ()
children
  PairT TypeInfo (TermT n)
_ TermT n
x TermT n
y -> do
    ty <- Int -> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
schematicTypeOf Int
fuel TermT n
t
    functionHead fuel ty >>= \case
      TypeSigmaT TypeInfo (TermT n)
_ Binder
_ TModality
_ TermT n
a ScopedAST NameBinder (AnnSig TypeInfo TermSig) n
b -> do
        Int -> IntSet -> TermT n -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TermT n -> TypeCheck n ()
argument (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen TermT n
a TermT n
x
        cod <- 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
b TermT n
x
        argument (fuel - 1) seen cod y
      CubeProductT{} -> TypeCheck n ()
children
      TermT n
_ -> FilePath -> TermT n -> TypeCheck n ()
forall (n :: S) a.
Distinct n =>
FilePath -> TermT n -> TypeCheck n a
invalid FilePath
"pair without a product type" TermT n
t
  FirstT{} -> TypeCheck n ()
children
  SecondT{} -> TypeCheck n ()
children
  ReflT TypeInfo (TermT n)
_ Maybe (TermT n, Maybe (TermT n))
mx -> Maybe (TermT n, Maybe (TermT n))
-> ((TermT n, Maybe (TermT n)) -> TypeCheck n ()) -> TypeCheck n ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe (TermT n, Maybe (TermT n))
mx (((TermT n, Maybe (TermT n)) -> TypeCheck n ()) -> TypeCheck n ())
-> ((TermT n, Maybe (TermT n)) -> TypeCheck n ()) -> TypeCheck n ()
forall a b. (a -> b) -> a -> b
$ \(TermT n
x, Maybe (TermT n)
mty) -> do
    ty <- 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 (Int -> TermT n -> TypeCheck n (TermT n)
forall (n :: S).
Distinct n =>
Int -> TermT n -> TypeCheck n (TermT n)
schematicTypeOf Int
fuel TermT n
x) 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 Maybe (TermT n)
mty
    object (fuel - 1) seen ty
    term (fuel - 1) seen x
  -- Elaboration checks the absurd tope context; recBOT has a cyclic type annotation.
  RecBottomT{} -> () -> TypeCheck n ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  RecOrT{} -> TypeCheck n ()
children
  AppT{} -> Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
applications Int
fuel IntSet
seen TermT n
t
  TermT n
_ -> SchematicProblem -> FilePath -> TermT n -> TypeCheck n ()
forall (n :: S) a.
Distinct n =>
SchematicProblem -> FilePath -> TermT n -> TypeCheck n a
schematicError SchematicProblem
SchematicUnsupported FilePath
"unsupported schematic expression" TermT n
t
  where
    children :: TypeCheck n ()
children = case TermT n
t of
      Node (AnnSig TypeInfo (TermT n)
_ TermSig
  (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n) (TermT n)
sig) -> (TermT n -> TypeCheck n ()) -> [TermT n] -> TypeCheck n ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Int -> IntSet -> TermT n -> TypeCheck n ()
forall (n :: S).
Distinct n =>
Int -> IntSet -> TermT n -> TypeCheck n ()
term (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) IntSet
seen) ((ScopedAST NameBinder (AnnSig TypeInfo TermSig) n -> [TermT n])
-> (TermT n -> [TermT n])
-> TermSig
     (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n) (TermT n)
-> [TermT n]
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 n]
-> ScopedAST NameBinder (AnnSig TypeInfo TermSig) n -> [TermT n]
forall a b. a -> b -> a
const []) (TermT n -> [TermT n] -> [TermT n]
forall a. a -> [a] -> [a]
:[]) TermSig
  (ScopedAST NameBinder (AnnSig TypeInfo TermSig) n) (TermT n)
sig)
      Var{} -> () -> TypeCheck n ()
forall a.
a
-> ReaderT
     (Context n) (ExceptT TypeErrorInScopedContext (State CheckLog)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()