{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE LambdaCase          #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE RankNTypes          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections       #-}
-- | The typing rules: 'check' and 'infer' on raw terms, with types as
-- values, producing core terms.
--
-- The algorithm is Coquand's bidirectional one (/An algorithm for
-- type-checking dependent types/, 1996), in the form of free-foil-NBE's @DependentTypes.Elaborate@
-- (Renata Shakirova and Nikolai Kudasov): a λ is checked against a Π
-- value by entering its binder with the domain and checking the body
-- against the codomain at the bound names; an inferred type is compared
-- with the expected one by subtyping ("Rzk.Check.Conversion"). The rules
-- mirror those of "Rzk.TypeCheck.Judgements" case by case, on values.
--
-- The modal constructs and @match@ are not covered yet.
module Rzk.Check.Rules
  ( check
  , infer
  , inferAs
  , checkTopeAgainstContext
  , contextEntails
  , ensureTypeAtU
  , DomainKind (..)
  , classifyType
  , evalM
  , quoteM
  , underRawScope
  ) where

import           Control.Monad            (forM, forM_, unless, when)
import           Control.Monad.Foil       (DExt, Distinct)
import qualified Control.Monad.Foil       as Foil
import           Control.Monad.Free.Foil  (AST (..), ScopedAST (..),
                                           substitute)
import           Data.List                (tails)
import           Data.Maybe               (isNothing)

import qualified FreeFoil.NbE             as NbE

import           Language.Rzk.Foil.Names  (TModality (..), VarIdent,
                                           ppVarIdentWithLocation,
                                           unmarkUnresolved)
import           Rzk.Check.Context
import           Rzk.Check.Conversion
import           Rzk.Check.Display        (namingOfContext, ppName, ppValue, ppValueIn)
import           Rzk.Check.Error
import           Rzk.Check.Monad
import           Rzk.Check.Syntax
import           Rzk.Check.Topes
import           Rzk.Check.Value

-- * Values in the monad

evalM :: Distinct n => Core n -> Check n (Value n)
evalM :: forall (n :: S). Distinct n => Core n -> Check n (Value n)
evalM Core n
t = (Context n -> Value n) -> Check n (Value n)
forall (n :: S) a. (Context n -> a) -> Check n a
asksContext ((Context n -> Value n) -> Check n (Value n))
-> (Context n -> Value n) -> Check n (Value n)
forall a b. (a -> b) -> a -> b
$ \Context n
ctx -> Scope n -> Context n -> Core n -> Value n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Core n -> Value n
evalCore (Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx) Context n
ctx Core n
t
{-# INLINE evalM #-}

-- | Check a term against a type and evaluate the elaborated term.
checkEval :: Distinct n => Raw n -> Value n -> Check n (Core n, Value n)
checkEval :: forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
term Value n
ty = do
  term' <- Raw n -> Value n -> Check n (Core n)
forall (n :: S). Distinct n => Raw n -> Value n -> Check n (Core n)
check Raw n
term Value n
ty
  (term',) <$> evalM term'
{-# INLINE checkEval #-}

-- | Infer a local value's type, or use the supplied type annotation. The result
-- is the elaborated value and its type.
inferOrCheckValue :: Distinct n => Maybe (Raw n) -> Raw n -> Check n (Core n, Value n)
inferOrCheckValue :: forall (n :: S).
Distinct n =>
Maybe (Raw n) -> Raw n -> Check n (Core n, Value n)
inferOrCheckValue Maybe (Raw n)
mannot Raw n
val = case Maybe (Raw n)
mannot of
  Maybe (Raw n)
Nothing -> Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer Raw n
val
  Just Raw n
annot -> do
    (_, annotV) <- Raw n -> Value n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
annot Value n
forall (n :: S). Value n
universe
    (, annotV) <$> check val annotV

-- | A value read back, with defined names folded. Lazy: nothing is read
-- back until a consumer looks.
quoteM :: Distinct n => Value n -> Check n (Core n)
quoteM :: forall (n :: S). Distinct n => Value n -> Check n (Core n)
quoteM Value n
v = (Context n -> Core n) -> Check n (Core n)
forall (n :: S) a. (Context n -> a) -> Check n a
asksContext ((Context n -> Core n) -> Check n (Core n))
-> (Context n -> Core n) -> Check n (Core n)
forall a b. (a -> b) -> a -> b
$ \Context n
ctx -> Scope n -> Context n -> Value n -> Core n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Core n
quoteFolded (Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx) Context n
ctx Value n
v
{-# INLINE quoteM #-}

-- * Binders

-- | Enter a raw scope: refresh its pattern against the scope if its names
-- clash, extend the context with the pattern by the given function, and
-- run the continuation on the body.
enterRawScope
  :: forall n r. (Distinct n)
  => (forall l. (Distinct l, DExt n l) => Foil.Scope l -> Pattern n l -> Context n -> Context l)
  -> ScopedRaw n
  -> (forall l. (Distinct l, DExt n l) => Pattern n l -> Raw l -> Check l r)
  -> Check n r
enterRawScope :: forall (n :: S) r.
Distinct n =>
(forall (l :: S).
 (Distinct l, DExt n l) =>
 Scope l -> Pattern n l -> Context n -> Context l)
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l r)
-> Check n r
enterRawScope forall (l :: S).
(Distinct l, DExt n l) =>
Scope l -> Pattern n l -> Context n -> Context l
extend (ScopedAST Pattern n l
pat AST Pattern (AnnSig SrcPos Sig) l
body) forall (l :: S).
(Distinct l, DExt n l) =>
Pattern n l -> Raw l -> Check l r
k = do
  Pattern n l -> Check n ()
forall (n :: S) (l :: S). Distinct n => Pattern n l -> Check n ()
warnShadowing Pattern n l
pat
  ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
  let scope = Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx
      enter :: forall l. DExt n l => Pattern n l -> Raw l -> Check n r
      enter Pattern n l
pat' Raw l
body' = case (Pattern n l -> DistinctEvidence l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct Pattern n l
pat', Pattern n l -> ExtEvidence n l
forall (pattern :: S -> S -> *) (n :: S) (l :: S).
CoSinkable pattern =>
pattern n l -> ExtEvidence n l
Foil.assertExt Pattern n l
pat') of
        (DistinctEvidence l
Foil.Distinct, ExtEvidence n l
Foil.Ext) ->
          let scope' :: Scope l
scope' = Pattern n l -> Scope n -> Scope l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> Scope n -> Scope l
Foil.extendScopePattern Pattern n l
pat' Scope n
scope
          in Context l -> Check l r -> Check n r
forall (l :: S) a (n :: S). Context l -> Check l a -> Check n a
inContext (Scope l -> Pattern n l -> Context n -> Context l
forall (l :: S).
(Distinct l, DExt n l) =>
Scope l -> Pattern n l -> Context n -> Context l
extend Scope l
scope' Pattern n l
pat' Context n
ctx) (Pattern n l -> Raw l -> Check l r
forall (l :: S).
(Distinct l, DExt n l) =>
Pattern n l -> Raw l -> Check l r
k Pattern n l
pat' Raw l
body')
      fresh = case Pattern n l -> DistinctEvidence l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct Pattern n l
pat of
        DistinctEvidence l
Foil.Distinct -> ((Maybe VarIdent, Name l) -> Bool)
-> [(Maybe VarIdent, Name l)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\(Maybe VarIdent
_, Name l
x) -> Bool -> Bool
not (Name l -> Scope n -> Bool
forall (l :: S) (n :: S). Name l -> Scope n -> Bool
Foil.member Name l
x Scope n
scope)) (Pattern n l -> [(Maybe VarIdent, Name l)]
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Pattern n l -> [(Maybe VarIdent, Name l)]
patternNames Pattern n l
pat)
  if fresh
    then case (Foil.assertExt pat, Foil.assertDistinct pat) of
      (ExtEvidence n l
Foil.Ext, DistinctEvidence l
Foil.Distinct) -> Pattern n l -> AST Pattern (AnnSig SrcPos Sig) l -> Check n r
forall (l :: S). DExt n l => Pattern n l -> Raw l -> Check n r
enter Pattern n l
pat AST Pattern (AnnSig SrcPos Sig) l
body
    else Foil.withRefreshedPattern scope pat $ \Substitution (AST Pattern (AnnSig SrcPos Sig)) n n
-> Substitution (AST Pattern (AnnSig SrcPos Sig)) l o'
extendSubst Pattern n o'
pat' Scope o'
scope' ->
      Pattern n o' -> Raw o' -> Check n r
forall (l :: S). DExt n l => Pattern n l -> Raw l -> Check n r
enter Pattern n o'
pat' (Scope o'
-> Substitution (AST Pattern (AnnSig SrcPos Sig)) l o'
-> AST Pattern (AnnSig SrcPos Sig) l
-> Raw o'
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (o :: S)
       (i :: S).
(Bifunctor sig, Distinct o, CoSinkable binder, SinkableK binder) =>
Scope o
-> Substitution (AST binder sig) i o
-> AST binder sig i
-> AST binder sig o
substitute Scope o'
scope' (Substitution (AST Pattern (AnnSig SrcPos Sig)) n n
-> Substitution (AST Pattern (AnnSig SrcPos Sig)) l o'
extendSubst Substitution (AST Pattern (AnnSig SrcPos Sig)) n n
forall (e :: S -> *) (i :: S). InjectName e => Substitution e i i
Foil.identitySubst) AST Pattern (AnnSig SrcPos Sig) l
body)

-- | Enter a raw scope bound at a domain, with a modality and a shape.
underRawScope
  :: forall n r. (Distinct n)
  => TModality -> Value n -> Maybe (Value n) -> ScopedRaw n
  -> (forall l. (Distinct l, DExt n l) => Pattern n l -> Raw l -> Check l r)
  -> Check n r
underRawScope :: forall (n :: S) r.
Distinct n =>
TModality
-> Value n
-> Maybe (Value n)
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l r)
-> Check n r
underRawScope TModality
md Value n
dom Maybe (Value n)
mshape =
  (forall (l :: S).
 (Distinct l, DExt n l) =>
 Scope l -> Pattern n l -> Context n -> Context l)
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
(forall (l :: S).
 (Distinct l, DExt n l) =>
 Scope l -> Pattern n l -> Context n -> Context l)
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l r)
-> Check n r
enterRawScope (\Scope l
scope' Pattern n l
pat Context n
ctx -> Scope l
-> Context n
-> TModality
-> Value n
-> Maybe (Value n)
-> Pattern n l
-> Context l
forall (n :: S) (l :: S).
(Distinct n, DExt n l) =>
Scope l
-> Context n
-> TModality
-> Value n
-> Maybe (Value n)
-> Pattern n l
-> Context l
underPattern Scope l
scope' Context n
ctx TModality
md Value n
dom Maybe (Value n)
mshape Pattern n l
pat)

-- | Enter a raw scope and wrap the elaborated body with its pattern.
underRawScopeBody
  :: forall n. (Distinct n)
  => TModality -> Value n -> Maybe (Value n) -> ScopedRaw n
  -> (forall l. (Distinct l, DExt n l) => Pattern n l -> Raw l -> Check l (Core l))
  -> Check n (ScopedCore n)
underRawScopeBody :: forall (n :: S).
Distinct n =>
TModality
-> Value n
-> Maybe (Value n)
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l (Core l))
-> Check n (ScopedCore n)
underRawScopeBody TModality
md Value n
dom Maybe (Value n)
mshape ScopedRaw n
body forall (l :: S).
(Distinct l, DExt n l) =>
Pattern n l -> Raw l -> Check l (Core l)
k =
  TModality
-> Value n
-> Maybe (Value n)
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l (ScopedCore n))
-> Check n (ScopedCore n)
forall (n :: S) r.
Distinct n =>
TModality
-> Value n
-> Maybe (Value n)
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l r)
-> Check n r
underRawScope TModality
md Value n
dom Maybe (Value n)
mshape ScopedRaw n
body ((forall (l :: S).
  (Distinct l, DExt n l) =>
  Pattern n l -> Raw l -> Check l (ScopedCore n))
 -> Check n (ScopedCore n))
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l (ScopedCore n))
-> Check n (ScopedCore n)
forall a b. (a -> b) -> a -> b
$ \Pattern n l
pat Raw l
bodyTerm ->
    Pattern n l -> AST Pattern Sig l -> ScopedCore n
forall (binder :: S -> S -> *) (n :: S) (l :: S)
       (sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST Pattern n l
pat (AST Pattern Sig l -> ScopedCore n)
-> Check l (AST Pattern Sig l) -> Check l (ScopedCore n)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern n l -> Raw l -> Check l (AST Pattern Sig l)
forall (l :: S).
(Distinct l, DExt n l) =>
Pattern n l -> Raw l -> Check l (Core l)
k Pattern n l
pat Raw l
bodyTerm

-- | Check a λ parameter written with a shape, @(t : cube | shape)@, against
-- the expected domain and, if there is one, the expected shape: the cube
-- and the shape, as values.
checkShapedParam
  :: (Distinct n)
  => Value n -> Maybe (Value n) -> Raw n -> Raw n -> Check n (Value n, Value n)
checkShapedParam :: forall (n :: S).
Distinct n =>
Value n
-> Maybe (Value n) -> Raw n -> Raw n -> Check n (Value n, Value n)
checkShapedParam Value n
dom Maybe (Value n)
mexpectedShape Raw n
param Raw n
shape = do
  (_, cubeV) <- Raw n -> Value n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
param Value n
forall (n :: S). Value n
cubeUniverse
  convertTypes dom cubeV
  (_, shapeV) <- checkEval shape (familyType Id cubeV Nothing)
  forM_ mexpectedShape $ \Value n
s -> Value n -> Value n -> Check n ()
forall (n :: S). Distinct n => Value n -> Value n -> Check n ()
convertTypes Value n
s Value n
shapeV
  pure (cubeV, shapeV)

-- | A tope family over a shape carries its domain: the body of the family
-- becomes the conjunction of the shape at the pattern and the body, unless
-- the body already entails the shape, with a warning.
carryFamilyDomain
  :: (Distinct n, DExt n l)
  => Pattern n l -> Value n -> Core l -> Check l (Core l)
carryFamilyDomain :: forall (n :: S) (l :: S).
(Distinct n, DExt n l) =>
Pattern n l -> Value n -> Core l -> Check l (Core l)
carryFamilyDomain Pattern n l
pat Value n
shape Core l
body = do
  ctx <- Check l (Context l)
forall (n :: S). Check n (Context n)
askContext
  let scope = Context l -> Scope l
forall (n :: S). Context n -> Scope n
ctxScope Context l
ctx
  bodyV <- evalM body
  let domainV = Scope l -> Context l -> Value l -> Value l -> Value l
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n -> Value n
appValue Scope l
scope Context l
ctx (Value n -> Value l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Value n
shape) (Pattern n l -> Value l
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Pattern n l -> Value l
patternValue Pattern n l
pat)
      bodyT = Scope l -> Context l -> Value l -> Tope l
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf Scope l
scope Context l
ctx Value l
bodyV
      domainT = Scope l -> Context l -> Value l -> Tope l
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf Scope l
scope Context l
ctx Value l
domainV
  if topesEntail (ctxTopes ctx) [bodyT] domainT then pure body else do
    warn <- asksContext (optWarnTopeFamilyDomain . ctxOptions)
    when warn $ recordWarning $ \Context l
c ->
      String -> String -> Maybe LocationInfo -> Warning
TopeFamilyDomainWarning (Context l -> Value l -> String
forall (n :: S). Distinct n => Context n -> Value n -> String
ppValueIn Context l
c Value l
bodyV) (Context l -> Value l -> String
forall (n :: S). Distinct n => Context n -> Value n -> String
ppValueIn Context l
c Value l
domainV) (Context l -> Maybe LocationInfo
forall (n :: S). Context n -> Maybe LocationInfo
ctxLocation Context l
c)
    domainC <- quoteM domainV
    pure (TopeAnd domainC body)

-- | Warn about each name of a binder that shadows a name in scope, as the
-- old checker did with a trace.
warnShadowing :: Distinct n => Pattern n l -> Check n ()
warnShadowing :: forall (n :: S) (l :: S). Distinct n => Pattern n l -> Check n ()
warnShadowing Pattern n l
pat = case Pattern n l -> DistinctEvidence l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct Pattern n l
pat of
  DistinctEvidence l
Foil.Distinct -> do
    ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
    sequence_
      [ recordWarning $ \Context n
c -> String -> [String] -> Maybe LocationInfo -> Warning
ShadowingWarning
          (VarIdent -> String
ppVarIdentWithLocation VarIdent
x)
          [ VarIdent -> String
ppVarIdentWithLocation VarIdent
previous | Just VarIdent
previous <- [Entry n -> Maybe VarIdent
forall (n :: S). Entry n -> Maybe VarIdent
entryName (Name n -> Context n -> Entry n
forall (n :: S). Name n -> Context n -> Entry n
lookupEntry Name n
v Context n
c)] ]
          (Context n -> Maybe LocationInfo
forall (n :: S). Context n -> Maybe LocationInfo
ctxLocation Context n
c)
      | (Just x, _) <- patternNames pat
      , Just v <- [lookupNamed x ctx] ]

-- | The type of a family over a cube: @{t : cube | shape} → TOPE@.
familyType :: TModality -> Value n -> Maybe (Value n) -> Value n
familyType :: forall (n :: S). TModality -> Value n -> Maybe (Value n) -> Value n
familyType TModality
md Value n
cube Maybe (Value n)
mshape = Sig Void (Value n) -> Value n
forall (n :: S). Sig Void (Value n) -> Value n
vnode (TModality -> Value n -> Maybe (Value n) -> Sig Void (Value n)
forall scope term.
TModality -> term -> Maybe term -> Sig scope term
TypeShapeF TModality
md Value n
cube Maybe (Value n)
mshape)

-- | The shape of a binder written with a family as its type: the family,
-- conjoined with the shape of the family's own domain when it has one.
familyShape :: Distinct n => Foil.Scope n -> Core n -> Core n -> Maybe (Core n) -> Core n
familyShape :: forall (n :: S).
Distinct n =>
Scope n -> Core n -> Core n -> Maybe (Core n) -> Core n
familyShape Scope n
scope Core n
cube Core n
family Maybe (Core n)
mdom = case Maybe (Core n)
mdom of
  Maybe (Core n)
Nothing -> Core n
family
  Just (Lambda Maybe (Param (Core n))
_ (ScopedAST Pattern n l
_ Core l
TopeTop)) -> Core n
family
  Just Core n
dom -> Scope n
-> (forall (l :: S). DExt n l => NameBinder n l -> Core n)
-> Core n
forall (n :: S) r.
Distinct n =>
Scope n -> (forall (l :: S). DExt n l => NameBinder n l -> r) -> r
Foil.withFresh Scope n
scope ((forall (l :: S). DExt n l => NameBinder n l -> Core n) -> Core n)
-> (forall (l :: S). DExt n l => NameBinder n l -> Core n)
-> Core n
forall a b. (a -> b) -> a -> b
$ \NameBinder n l
b ->
    Maybe (Param (Core n)) -> ScopedAST Pattern Sig n -> Core n
forall (n :: S). Maybe (Param (Core n)) -> ScopedCore n -> Core n
Lambda (Param (Core n) -> Maybe (Param (Core n))
forall a. a -> Maybe a
Just (TModality -> Core n -> Maybe (Core n) -> Param (Core n)
forall term. TModality -> term -> Maybe term -> Param term
Param TModality
Id Core n
cube Maybe (Core n)
forall a. Maybe a
Nothing))
      (Pattern n l -> AST Pattern Sig l -> ScopedAST Pattern Sig n
forall (binder :: S -> S -> *) (n :: S) (l :: S)
       (sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST (NameBinder n l -> Pattern n l
forall (n :: S) (l :: S). NameBinder n l -> Pattern n l
singlePattern NameBinder n l
b)
        (AST Pattern Sig l -> AST Pattern Sig l -> AST Pattern Sig l
forall (n :: S). Core n -> Core n -> Core n
TopeAnd (AST Pattern Sig l -> AST Pattern Sig l -> AST Pattern Sig l
forall (n :: S). Core n -> Core n -> Core n
App (Core n -> AST Pattern Sig l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Core n
dom) (Name l -> AST Pattern Sig l
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var (NameBinder n l -> Name l
forall (n :: S) (l :: S). NameBinder n l -> Name l
Foil.nameOf NameBinder n l
b))) (AST Pattern Sig l -> AST Pattern Sig l -> AST Pattern Sig l
forall (n :: S). Core n -> Core n -> Core n
App (Core n -> AST Pattern Sig l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Core n
family) (Name l -> AST Pattern Sig l
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var (NameBinder n l -> Name l
forall (n :: S) (l :: S). NameBinder n l -> Name l
Foil.nameOf NameBinder n l
b)))))

-- | A binder over a cube has a shape: ⊤ when none is written.
withTopShape :: Distinct n => Foil.Scope n -> Context n -> Value n -> Maybe (Value n) -> Maybe (Value n)
withTopShape :: forall (n :: S).
Distinct n =>
Scope n
-> Context n -> Value n -> Maybe (Value n) -> Maybe (Value n)
withTopShape Scope n
scope Context n
ctx Value n
dom = \case
  Maybe (Value n)
Nothing | Scope n -> Context n -> Value n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Bool
isCube Scope n
scope Context n
ctx Value n
dom -> Value n -> Maybe (Value n)
forall a. a -> Maybe a
Just (Scope n -> Value n -> Value n
forall (n :: S). Distinct n => Scope n -> Value n -> Value n
topShapeValue Scope n
scope Value n
dom)
  Maybe (Value n)
mshape -> Maybe (Value n)
mshape

-- * The layer discipline

-- | The kind of a type standing at @U@: the universes and the families
-- into them are @U@-sorted but are not ordinary types.
data DomainKind
  = DomainType
    -- ^ an ordinary type in @U@
  | DomainCubeUniverse
    -- ^ the universe @CUBE@, not an ordinary type
  | DomainCubeFamily
    -- ^ a family valued in @CUBE@
  | DomainTopeUniverse
    -- ^ the universe @TOPE@, not an ordinary type
  | DomainTopeFamily
    -- ^ a tope family over a cube
  | DomainOtherTopeFamily
    -- ^ a tope family over a domain that is not a cube
  | DomainModalTope
    -- ^ a modal cube or tope kind
  deriving (DomainKind -> DomainKind -> Bool
(DomainKind -> DomainKind -> Bool)
-> (DomainKind -> DomainKind -> Bool) -> Eq DomainKind
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DomainKind -> DomainKind -> Bool
== :: DomainKind -> DomainKind -> Bool
$c/= :: DomainKind -> DomainKind -> Bool
/= :: DomainKind -> DomainKind -> Bool
Eq, Int -> DomainKind -> ShowS
[DomainKind] -> ShowS
DomainKind -> String
(Int -> DomainKind -> ShowS)
-> (DomainKind -> String)
-> ([DomainKind] -> ShowS)
-> Show DomainKind
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DomainKind -> ShowS
showsPrec :: Int -> DomainKind -> ShowS
$cshow :: DomainKind -> String
show :: DomainKind -> String
$cshowList :: [DomainKind] -> ShowS
showList :: [DomainKind] -> ShowS
Show)

-- | Classify a type value.
classifyType :: Distinct n => Foil.Scope n -> Context n -> Value n -> DomainKind
classifyType :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> DomainKind
classifyType Scope n
scope Context n
ctx Value n
ty = case Value n -> Whnf n
forall (n :: S). Value n -> Whnf n
force (Value n -> Value n
forall (n :: S). Value n -> Value n
stripRestrictions Value n
ty) of
  Whnf n
VUniverseCube -> DomainKind
DomainCubeUniverse
  Whnf n
VUniverseTope -> DomainKind
DomainTopeUniverse
  VTypeShape TModality
_ Value n
_ Maybe (Value n)
_ -> DomainKind
DomainTopeFamily
  VTypeModal TModality
_ Value n
inner -> case Scope n -> Context n -> Value n -> DomainKind
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> DomainKind
classifyType Scope n
scope Context n
ctx Value n
inner of
    DomainKind
DomainType -> DomainKind
DomainType
    DomainKind
_          -> DomainKind
DomainModalTope
  VTypeFun Env i n
env TModality
md Value n
dom Maybe (Value n)
mshape ScopedCore i
ret ->
    let cubeDom :: Bool
cubeDom = case Maybe (Value n)
mshape of
          Just Value n
_  -> Bool
True
          Maybe (Value n)
Nothing -> Scope n -> Context n -> Value n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Bool
isCube Scope n
scope Context n
ctx Value n
dom
        inner :: DomainKind
inner = Scope n
-> Context n
-> TModality
-> Value n
-> Maybe (Value n)
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Scope l -> Context l -> Name l -> DomainKind)
-> DomainKind
forall (n :: S) r.
Distinct n =>
Scope n
-> Context n
-> TModality
-> Value n
-> Maybe (Value n)
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Scope l -> Context l -> Name l -> r)
-> r
underFreshVar Scope n
scope Context n
ctx TModality
md Value n
dom Maybe (Value n)
mshape ((forall (l :: S).
  (DExt n l, Distinct l) =>
  Scope l -> Context l -> Name l -> DomainKind)
 -> DomainKind)
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Scope l -> Context l -> Name l -> DomainKind)
-> DomainKind
forall a b. (a -> b) -> a -> b
$ \Scope l
scope' Context l
ctx' Name l
x ->
          Scope l -> Context l -> Value l -> DomainKind
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> DomainKind
classifyType Scope l
scope' Context l
ctx' (Scope l
-> Context l -> Env i l -> ScopedCore i -> Value l -> Value l
forall (n :: S) (i :: S).
(Distinct n, Distinct i) =>
Scope n
-> Context n -> Env i n -> ScopedCore i -> Value n -> Value n
codomainAt Scope l
scope' Context l
ctx' (Env i n -> Env i l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Env i n
env) ScopedCore i
ret (Name l -> Value l
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> Value binder sig n
NbE.vvar Name l
x))
    in case DomainKind
inner of
         DomainKind
DomainTopeUniverse | Bool
cubeDom -> DomainKind
DomainTopeFamily
                            | Bool
otherwise -> DomainKind
DomainOtherTopeFamily
         DomainKind
DomainTopeFamily | Bool
cubeDom -> DomainKind
DomainTopeFamily
                          | Bool
otherwise -> DomainKind
DomainOtherTopeFamily
         DomainKind
DomainOtherTopeFamily -> DomainKind
DomainOtherTopeFamily
         DomainKind
DomainModalTope -> DomainKind
DomainModalTope
         DomainKind
DomainCubeUniverse -> DomainKind
DomainCubeFamily
         DomainKind
DomainCubeFamily -> DomainKind
DomainCubeFamily
         DomainKind
_ -> DomainKind
DomainType
  Whnf n
_ -> DomainKind
DomainType

domainKindName :: DomainKind -> String
domainKindName :: DomainKind -> String
domainKindName = \case
  DomainKind
DomainType            -> String
"a type"
  DomainKind
DomainCubeUniverse    -> String
"the cube universe"
  DomainKind
DomainCubeFamily      -> String
"a cube family"
  DomainKind
DomainTopeUniverse    -> String
"the tope universe"
  DomainKind
DomainTopeFamily      -> String
"a tope family"
  DomainKind
DomainOtherTopeFamily -> String
"a tope family over a non-cube domain"
  DomainKind
DomainModalTope       -> String
"a modal cube or tope kind"

-- | Reject a universe kind standing where an ordinary type is required.
ensureTypeAtU :: Distinct n => String -> Value n -> Check n ()
ensureTypeAtU :: forall (n :: S). Distinct n => String -> Value n -> Check n ()
ensureTypeAtU String
position Value n
ty = do
  ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
  case classifyType (ctxScope ctx) ctx ty of
    DomainKind
DomainType -> () -> Check n ()
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    DomainKind
kind -> TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (TypeError n -> Check n ()) -> TypeError n -> Check n ()
forall a b. (a -> b) -> a -> b
$ String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorOther (String -> TypeError n) -> String -> TypeError n
forall a b. (a -> b) -> a -> b
$
      String
position String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" must be a type, got " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> DomainKind -> String
domainKindName DomainKind
kind

-- * Topes

-- | The tope context, for an error.
contextTopesM :: Check n [Tope n]
contextTopesM :: forall (n :: S). Check n [Tope n]
contextTopesM = (Context n -> [Tope n]) -> Check n [Tope n]
forall (n :: S) a. (Context n -> a) -> Check n a
asksContext (TopeContext n -> [Tope n]
forall (n :: S). TopeContext n -> [Tope n]
accessibleTopes (TopeContext n -> [Tope n])
-> (Context n -> TopeContext n) -> Context n -> [Tope n]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Context n -> TopeContext n
forall (n :: S). Context n -> TopeContext n
ctxTopes)

-- | The context entails the tope, or the tope mentions a hole and cannot
-- be decided.
contextEntails :: Distinct n => Value n -> Check n ()
contextEntails :: forall (n :: S). Distinct n => Value n -> Check n ()
contextEntails Value n
topeV = do
  ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
  let tope = Scope n -> Context n -> Value n -> Tope n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf (Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx) Context n
ctx Value n
topeV
  topes <- contextTopesM
  performing (ActionEntails topes tope) $
    unless (entails (ctxTopes ctx) tope || topeHasHole tope) $
      issueTypeError (TypeErrorTopeNotSatisfied topes tope)

-- | The context is covered by the disjunction of the topes: the coverage
-- obligation of a @recOR@.
contextEntailsUnion :: Distinct n => [Value n] -> Check n ()
contextEntailsUnion :: forall (n :: S). Distinct n => [Value n] -> Check n ()
contextEntailsUnion [Value n]
topeVs = do
  ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
  let topes = (Value n -> Tope n) -> [Value n] -> [Tope n]
forall a b. (a -> b) -> [a] -> [b]
map (Scope n -> Context n -> Value n -> Tope n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf (Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx) Context n
ctx) [Value n]
topeVs
  ctxTopes' <- contextTopesM
  performing (ActionEntailsUnion ctxTopes' topes) $
    unless (entailsUnion (ctxTopes ctx) topes || any topeHasHole topes) $
      issueTypeError (TypeErrorTopeNotSatisfied ctxTopes' (foldr topeOr TBot topes))

topeHasHole :: Tope n -> Bool
topeHasHole :: forall (n :: S). Tope n -> Bool
topeHasHole = \case
  THole Maybe VarIdent
_ -> Bool
True
  TEq Point n
x Point n
y -> Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
x Bool -> Bool -> Bool
|| Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
y
  TLeq Point n
x Point n
y -> Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
x Bool -> Bool -> Bool
|| Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
y
  TAnd Tope n
l Tope n
r -> Tope n -> Bool
forall (n :: S). Tope n -> Bool
topeHasHole Tope n
l Bool -> Bool -> Bool
|| Tope n -> Bool
forall (n :: S). Tope n -> Bool
topeHasHole Tope n
r
  TOr Tope n
l Tope n
r -> Tope n -> Bool
forall (n :: S). Tope n -> Bool
topeHasHole Tope n
l Bool -> Bool -> Bool
|| Tope n -> Bool
forall (n :: S). Tope n -> Bool
topeHasHole Tope n
r
  TInv Tope n
t -> Tope n -> Bool
forall (n :: S). Tope n -> Bool
topeHasHole Tope n
t
  TUninv Tope n
t -> Tope n -> Bool
forall (n :: S). Tope n -> Bool
topeHasHole Tope n
t
  TModal TModality
_ Tope n
t -> Tope n -> Bool
forall (n :: S). Tope n -> Bool
topeHasHole Tope n
t
  TModApp TModality
_ Tope n
t -> Tope n -> Bool
forall (n :: S). Tope n -> Bool
topeHasHole Tope n
t
  TNeutral Point n
p -> Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
p
  Tope n
_ -> Bool
False

pointHasHole :: Point n -> Bool
pointHasHole :: forall (n :: S). Point n -> Bool
pointHasHole = \case
  PHole Maybe VarIdent
_ -> Bool
True
  PPair Point n
a Point n
b -> Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
a Bool -> Bool -> Bool
|| Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
b
  PFst Point n
p -> Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
p
  PSnd Point n
p -> Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
p
  PSup Point n
a Point n
b -> Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
a Bool -> Bool -> Bool
|| Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
b
  PInf Point n
a Point n
b -> Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
a Bool -> Bool -> Bool
|| Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
b
  PFlip Point n
p -> Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
p
  PUnflip Point n
p -> Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
p
  PModApp TModality
_ Point n
p -> Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
p
  PModExtract TModality
_ TModality
_ Point n
p -> Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
p
  PApp Cube
_ Point n
f Point n
a -> Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
f Bool -> Bool -> Bool
|| Point n -> Bool
forall (n :: S). Point n -> Bool
pointHasHole Point n
a
  Point n
_ -> Bool
False

-- | Diagnose a @recOR@ guard or a restriction face against the context: a
-- tope disjoint from a consistent context is vacuous, an error; a tope
-- that overhangs the context is reported when the option asks.
checkTopeAgainstContext :: Distinct n => String -> Value n -> Check n ()
checkTopeAgainstContext :: forall (n :: S). Distinct n => String -> Value n -> Check n ()
checkTopeAgainstContext String
what Value n
topeV = do
  ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
  let topes = Context n -> TopeContext n
forall (n :: S). Context n -> TopeContext n
ctxTopes Context n
ctx
      tope = Scope n -> Context n -> Value n -> Tope n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf (Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx) Context n
ctx Value n
topeV
  unless (entailsBottom topes) $
    if disjoint topes tope && not (topeHasHole tope)
      then issueTypeError (TypeErrorTopeContextDisjoint tope (filter (/= TTop) (accessibleTopes topes)))
      else when (optWarnOverhang (ctxOptions ctx) && not (entailedBy topes tope)) $
        recordWarning $ \Context n
c ->
          let naming :: Naming n
naming = Context n -> Naming n
forall (n :: S). Context n -> Naming n
namingOfContext Context n
c
          in String -> String -> [String] -> Maybe LocationInfo -> Warning
OverhangWarning String
what (Naming n -> Tope n -> String
forall (n :: S). Naming n -> Tope n -> String
ppTope Naming n
naming Tope n
tope)
               ((Tope n -> String) -> [Tope n] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Naming n -> Tope n -> String
forall (n :: S). Naming n -> Tope n -> String
ppTope Naming n
naming) ((Tope n -> Bool) -> [Tope n] -> [Tope n]
forall a. (a -> Bool) -> [a] -> [a]
filter (Tope n -> Tope n -> Bool
forall a. Eq a => a -> a -> Bool
/= Tope n
forall (n :: S). Tope n
TTop) (TopeContext n -> [Tope n]
forall (n :: S). TopeContext n -> [Tope n]
accessibleTopes TopeContext n
topes))) (Context n -> Maybe LocationInfo
forall (n :: S). Context n -> Maybe LocationInfo
ctxLocation Context n
c)

-- | Two faces, or two branches, agree where their topes overlap.
checkCoherence :: Distinct n => (Value n, Value n) -> (Value n, Value n) -> Check n ()
checkCoherence :: forall (n :: S).
Distinct n =>
(Value n, Value n) -> (Value n, Value n) -> Check n ()
checkCoherence (Value n
ltope, Value n
lterm) (Value n
rtope, Value n
rterm) = do
  ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
  let scope = Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx
  underTope (topeOf scope ctx (topeAndValue ltope rtope)) $
    convertTerms lterm rterm

-- * Holes

-- | A hole in checking position: an error in strict mode; otherwise its
-- goal and context are recorded and it stands for a term of the goal.
checkHole :: Distinct n => Maybe VarIdent -> Value n -> Check n (Core n)
checkHole :: forall (n :: S).
Distinct n =>
Maybe VarIdent -> Value n -> Check n (Core n)
checkHole Maybe VarIdent
mname Value n
goal = do
  mode <- (Context n -> HoleMode) -> Check n HoleMode
forall (n :: S) a. (Context n -> a) -> Check n a
asksContext (Options -> HoleMode
optHoles (Options -> HoleMode)
-> (Context n -> Options) -> Context n -> HoleMode
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Context n -> Options
forall (n :: S). Context n -> Options
ctxOptions)
  case mode of
    HoleMode
HolesAreErrors -> TypeError n -> Check n (Core n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (Maybe VarIdent -> Value n -> TypeError n
forall (n :: S). Maybe VarIdent -> Value n -> TypeError n
TypeErrorUnsolvedHole Maybe VarIdent
mname Value n
goal)
    HoleMode
_ -> do
      (Context n -> HoleInfo) -> Check n ()
forall (n :: S). (Context n -> HoleInfo) -> Check n ()
recordHole ((Context n -> HoleInfo) -> Check n ())
-> (Context n -> HoleInfo) -> Check n ()
forall a b. (a -> b) -> a -> b
$ \Context n
ctx ->
        let naming :: Naming n
naming = Context n -> Naming n
forall (n :: S). Context n -> Naming n
namingOfContext Context n
ctx
            scope :: Scope n
scope = Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx
            entry :: (Name n, Entry n) -> (String, String)
entry (Name n
x, Entry n
e) = (Naming n -> Name n -> String
forall (n :: S). Naming n -> Name n -> String
ppName Naming n
naming Name n
x, Context n -> Value n -> String
forall (n :: S). Distinct n => Context n -> Value n -> String
ppValueIn Context n
ctx (Entry n -> Value n
forall (n :: S). Entry n -> Value n
entryType Entry n
e))
            locals :: [(Name n, Entry n)]
locals = [ (Name n
x, Entry n
e) | (Name n
x, Entry n
e) <- Context n -> [(Name n, Entry n)]
forall (n :: S). Context n -> [(Name n, Entry n)]
entriesInScope Context n
ctx, Entry n -> EntryKind
forall (n :: S). Entry n -> EntryKind
entryKind Entry n
e EntryKind -> EntryKind -> Bool
forall a. Eq a => a -> a -> Bool
== EntryKind
EntryLocal ]
            isCubeEntry :: Entry n -> Bool
isCubeEntry Entry n
e = Value n -> Cube
forall (n :: S). Value n -> Cube
cubeOfType (Entry n -> Value n
forall (n :: S). Entry n -> Value n
entryType Entry n
e) Cube -> Cube -> Bool
forall a. Eq a => a -> a -> Bool
/= Cube
KOther
        in HoleInfo
          { holeName :: Maybe VarIdent
holeName = Maybe VarIdent
mname
          , holeGoal :: String
holeGoal = Scope n -> Context n -> Value n -> String
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> String
ppValue Scope n
scope Context n
ctx Value n
goal
          , holeTermVars :: [(String, String)]
holeTermVars = [ (Name n, Entry n) -> (String, String)
entry (Name n, Entry n)
xe | xe :: (Name n, Entry n)
xe@(Name n
_, Entry n
e) <- [(Name n, Entry n)]
locals, Bool -> Bool
not (Entry n -> Bool
forall {n :: S}. Entry n -> Bool
isCubeEntry Entry n
e) ]
          , holeCubeVars :: [(String, String)]
holeCubeVars = [ (Name n, Entry n) -> (String, String)
entry (Name n, Entry n)
xe | xe :: (Name n, Entry n)
xe@(Name n
_, Entry n
e) <- [(Name n, Entry n)]
locals, Entry n -> Bool
forall {n :: S}. Entry n -> Bool
isCubeEntry Entry n
e ]
          , holeTopes :: [String]
holeTopes = [ Naming n -> Tope n -> String
forall (n :: S). Naming n -> Tope n -> String
ppTope Naming n
naming (ModalTope n -> Tope n
forall (n :: S). ModalTope n -> Tope n
tTope ModalTope n
mt) | ModalTope n
mt <- TopeContext n -> [ModalTope n]
forall (n :: S). TopeContext n -> [ModalTope n]
contextTopes (Context n -> TopeContext n
forall (n :: S). Context n -> TopeContext n
ctxTopes Context n
ctx), ModalTope n -> Tope n
forall (n :: S). ModalTope n -> Tope n
tTope ModalTope n
mt Tope n -> Tope n -> Bool
forall a. Eq a => a -> a -> Bool
/= Tope n
forall (n :: S). Tope n
TTop ]
          , holeLocation :: Maybe LocationInfo
holeLocation = Context n -> Maybe LocationInfo
forall (n :: S). Context n -> Maybe LocationInfo
ctxLocation Context n
ctx
          }
      Core n -> Check n (Core n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe VarIdent -> Core n
forall (n :: S). Maybe VarIdent -> Core n
Hole Maybe VarIdent
mname)

-- * Checking

check :: forall n. Distinct n => Raw n -> Value n -> Check n (Core n)
check :: forall (n :: S). Distinct n => Raw n -> Value n -> Check n (Core n)
check Raw n
term Value n
ty = Action n -> Check n (Core n) -> Check n (Core n)
forall (n :: S) a. Distinct n => Action n -> Check n a -> Check n a
performing (Raw n -> Value n -> Action n
forall (n :: S). Raw n -> Value n -> Action n
ActionCheck Raw n
term Value n
ty) (Check n (Core n) -> Check n (Core n))
-> Check n (Core n) -> Check n (Core n)
forall a b. (a -> b) -> a -> b
$ case Raw n
term of
  RHole (Just VarIdent
name) | Just VarIdent
x <- VarIdent -> Maybe VarIdent
unmarkUnresolved VarIdent
name ->
    TypeError n -> Check n (Core n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (VarIdent -> TypeError n
forall (n :: S). VarIdent -> TypeError n
TypeErrorUndefined VarIdent
x)
  RHole Maybe VarIdent
mname -> Maybe VarIdent -> Value n -> Check n (Core n)
forall (n :: S).
Distinct n =>
Maybe VarIdent -> Value n -> Check n (Core n)
checkHole Maybe VarIdent
mname Value n
ty
  Raw n
_ -> do
    ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
    let scope = Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx
    case force ty of
      -- even under an absurd context the term must be well-formed
      Whnf n
VRecBottom -> do
        _ <- Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer Raw n
term
        pure RecBottom

      VTypeRestricted Value n
ty' [(Value n, Value n)]
rs -> case Raw n
term of
        RRecOr [(Raw n, Raw n)]
branches -> Value n -> [(Raw n, Raw n)] -> Check n (Core n)
forall (n :: S).
Distinct n =>
Value n -> [(Raw n, Raw n)] -> Check n (Core n)
checkRecOrAgainst Value n
ty [(Raw n, Raw n)]
branches
        Raw n
_ -> do
          (term', termV) <- Raw n -> Value n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
term Value n
ty'
          forM_ rs $ \(Value n
tope, Value n
face) -> do
            String -> Value n -> Check n ()
forall (n :: S). Distinct n => String -> Value n -> Check n ()
checkTopeAgainstContext String
"restriction face" Value n
tope
            Tope n -> Check n () -> Check n ()
forall (n :: S) a. Tope n -> Check n a -> Check n a
underTope (Scope n -> Context n -> Value n -> Tope n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf Scope n
scope Context n
ctx Value n
tope) (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ Value n -> Value n -> Check n ()
forall (n :: S). Distinct n => Value n -> Value n -> Check n ()
convertTerms Value n
face Value n
termV
          pure term'

      Whnf n
tyW -> case Raw n
term of
        -- a λ against a family type: its body is a tope under the binder,
        -- carrying the family's domain
        RLambda Maybe (Param (Raw n))
mparam ScopedRaw n
body | Just (Family TModality
md' Value n
cube' Maybe (Value n)
mshape') <- Value n -> Maybe (Family n)
forall (n :: S). Value n -> Maybe (Family n)
familyOf (Whnf n -> Value n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Whnf binder sig n -> Value binder sig n
NbE.fromWhnf Whnf n
tyW) -> do
          mparam' <- Maybe (Param (Raw n))
-> (Param (Raw n) -> Check n (Param (Value n)))
-> Check n (Maybe (Param (Value n)))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM Maybe (Param (Raw n))
mparam ((Param (Raw n) -> Check n (Param (Value n)))
 -> Check n (Maybe (Param (Value n))))
-> (Param (Raw n) -> Check n (Param (Value n)))
-> Check n (Maybe (Param (Value n)))
forall a b. (a -> b) -> a -> b
$ \(Param TModality
md Raw n
param Maybe (Raw n)
mshape) -> do
            Bool -> Check n () -> Check n ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TModality
md TModality -> TModality -> Bool
forall a. Eq a => a -> a -> Bool
/= TModality
md') (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (TypeError n -> Check n ()) -> TypeError n -> Check n ()
forall a b. (a -> b) -> a -> b
$ String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorOther
              String
"modality mismatch in lambda parameter"
            cubeV <- case Maybe (Raw n)
mshape of
              Maybe (Raw n)
Nothing -> do
                (param', paramTy) <- Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer Raw n
param
                paramV <- evalM param'
                case familyOf paramTy of
                  Just (Family TModality
_ Value n
cube Maybe (Value n)
_) -> Value n -> Value n -> Check n ()
forall (n :: S). Distinct n => Value n -> Value n -> Check n ()
convertTypes Value n
cube' Value n
cube Check n () -> Check n (Value n) -> Check n (Value n)
forall a b. Check n a -> Check n b -> Check n b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Value n -> Check n (Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value n
cube
                  Maybe (Family n)
Nothing -> Value n -> Value n -> Check n ()
forall (n :: S). Distinct n => Value n -> Value n -> Check n ()
convertTypes Value n
cube' Value n
paramV Check n () -> Check n (Value n) -> Check n (Value n)
forall a b. Check n a -> Check n b -> Check n b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Value n -> Check n (Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value n
paramV
              Just Raw n
shape -> (Value n, Value n) -> Value n
forall a b. (a, b) -> a
fst ((Value n, Value n) -> Value n)
-> Check n (Value n, Value n) -> Check n (Value n)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value n
-> Maybe (Value n) -> Raw n -> Raw n -> Check n (Value n, Value n)
forall (n :: S).
Distinct n =>
Value n
-> Maybe (Value n) -> Raw n -> Raw n -> Check n (Value n, Value n)
checkShapedParam Value n
cube' Maybe (Value n)
mshape' Raw n
param Raw n
shape
            pure (Param md cubeV mshape')
          let mshapeAssumed = Maybe (Value n)
-> (Param (Value n) -> Maybe (Value n))
-> Maybe (Param (Value n))
-> Maybe (Value n)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Maybe (Value n)
mshape' Param (Value n) -> Maybe (Value n)
forall term. Param term -> Maybe term
paramShape Maybe (Param (Value n))
mparam'
          body' <- underRawScopeBody md' cube' mshapeAssumed body $ \Pattern n l
pat Raw l
bodyTerm -> do
            bodyIn <- Raw l -> Value l -> Check l (Core l)
forall (n :: S). Distinct n => Raw n -> Value n -> Check n (Core n)
check Raw l
bodyTerm Value l
forall (n :: S). Value n
topeUniverse
            case mshapeAssumed of
              Just Value n
shape -> Pattern n l -> Value n -> Core l -> Check l (Core l)
forall (n :: S) (l :: S).
(Distinct n, DExt n l) =>
Pattern n l -> Value n -> Core l -> Check l (Core l)
carryFamilyDomain Pattern n l
pat Value n
shape Core l
bodyIn
              Maybe (Value n)
Nothing -> Core l -> Check l (Core l)
forall a. a -> Check l a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Core l
bodyIn
          cubeC <- quoteM cube'
          mshapeC <- traverse quoteM mshapeAssumed
          pure (Lambda (Just (Param md' cubeC mshapeC)) body')

        RLambda Maybe (Param (Raw n))
mparam ScopedRaw n
body -> case Value n -> Maybe (Pi n)
forall (n :: S). Value n -> Maybe (Pi n)
piOf (Whnf n -> Value n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Whnf binder sig n -> Value binder sig n
NbE.fromWhnf Whnf n
tyW) of
          Just (Pi Env i n
env TModality
md' Value n
dom' Maybe (Value n)
mshape' ScopedCore i
ret) -> do
            -- the λ's own parameter, if written, must agree with the Π's
            mparam' <- Maybe (Param (Raw n))
-> (Param (Raw n) -> Check n (Param (Value n)))
-> Check n (Maybe (Param (Value n)))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM Maybe (Param (Raw n))
mparam ((Param (Raw n) -> Check n (Param (Value n)))
 -> Check n (Maybe (Param (Value n))))
-> (Param (Raw n) -> Check n (Param (Value n)))
-> Check n (Maybe (Param (Value n)))
forall a b. (a -> b) -> a -> b
$ \(Param TModality
md Raw n
param Maybe (Raw n)
mshape) -> do
              Bool -> Check n () -> Check n ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TModality
md TModality -> TModality -> Bool
forall a. Eq a => a -> a -> Bool
/= TModality
md') (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (TypeError n -> Check n ()) -> TypeError n -> Check n ()
forall a b. (a -> b) -> a -> b
$ String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorOther
                String
"modality mismatch in lambda parameter"
              case Maybe (Raw n)
mshape of
                Maybe (Raw n)
Nothing -> do
                  (param', paramTy) <- Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer Raw n
param
                  paramV <- evalM param'
                  -- a shape written as the parameter type: the domain is its cube
                  case familyOf paramTy of
                    Just (Family TModality
_ Value n
cube Maybe (Value n)
mdomShape) -> do
                      Value n -> Value n -> Check n ()
forall (n :: S). Distinct n => Value n -> Value n -> Check n ()
convertTypes Value n
dom' Value n
cube
                      cubeC <- Value n -> Check n (Core n)
forall (n :: S). Distinct n => Value n -> Check n (Core n)
quoteM Value n
cube
                      mdomC <- traverse quoteM mdomShape
                      shapeV <- evalM (familyShape scope cubeC param' mdomC)
                      case mshape' of
                        Just Value n
shape' -> Value n -> Value n -> Check n ()
forall (n :: S). Distinct n => Value n -> Value n -> Check n ()
subtypeValues Value n
shape' Value n
shapeV
                        Maybe (Value n)
Nothing -> () -> Check n ()
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
                      pure (Param md cube (Just shapeV))
                    Maybe (Family n)
Nothing -> do
                      Value n -> Value n -> Check n ()
forall (n :: S). Distinct n => Value n -> Value n -> Check n ()
convertTypes Value n
dom' Value n
paramV
                      Param (Value n) -> Check n (Param (Value n))
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TModality -> Value n -> Maybe (Value n) -> Param (Value n)
forall term. TModality -> term -> Maybe term -> Param term
Param TModality
md Value n
paramV (Scope n
-> Context n -> Value n -> Maybe (Value n) -> Maybe (Value n)
forall (n :: S).
Distinct n =>
Scope n
-> Context n -> Value n -> Maybe (Value n) -> Maybe (Value n)
withTopShape Scope n
scope Context n
ctx Value n
paramV Maybe (Value n)
forall a. Maybe a
Nothing))
                Just Raw n
shape -> do
                  (cubeV, shapeV) <- Value n
-> Maybe (Value n) -> Raw n -> Raw n -> Check n (Value n, Value n)
forall (n :: S).
Distinct n =>
Value n
-> Maybe (Value n) -> Raw n -> Raw n -> Check n (Value n, Value n)
checkShapedParam Value n
dom' Maybe (Value n)
mshape' Raw n
param Raw n
shape
                  pure (Param md cubeV (Just shapeV))
            let mshapeAssumed = case Maybe (Param (Value n))
mparam' of
                  Just (Param TModality
_ Value n
_ (Just Value n
s)) -> Value n -> Maybe (Value n)
forall a. a -> Maybe a
Just Value n
s
                  Maybe (Param (Value n))
_ -> Maybe (Value n)
mshape'
            body' <- underRawScopeBody md' dom' mshapeAssumed body $ \Pattern n l
pat Raw l
bodyTerm -> do
              ctxIn <- Check l (Context l)
forall (n :: S). Check n (Context n)
askContext
              let scopeIn = Context l -> Scope l
forall (n :: S). Context n -> Scope n
ctxScope Context l
ctxIn
                  retIn = Scope l
-> Context l -> Env i l -> ScopedCore i -> Value l -> Value l
forall (n :: S) (i :: S).
(Distinct n, Distinct i) =>
Scope n
-> Context n -> Env i n -> ScopedCore i -> Value n -> Value n
codomainAt Scope l
scopeIn Context l
ctxIn (Env i n -> Env i l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Env i n
env) ScopedCore i
ret (Pattern n l -> Value l
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Pattern n l -> Value l
patternValue Pattern n l
pat)
              bodyIn <- check bodyTerm retIn
              -- A tope family checked against a shape domain carries the
              -- domain: its body becomes the conjunction, unless the body
              -- already entails the domain
              case (force retIn, mshapeAssumed) of
                (Whnf l
VUniverseTope, Just Value n
shape) -> Pattern n l -> Value n -> Core l -> Check l (Core l)
forall (n :: S) (l :: S).
(Distinct n, DExt n l) =>
Pattern n l -> Value n -> Core l -> Check l (Core l)
carryFamilyDomain Pattern n l
pat Value n
shape Core l
bodyIn
                (Whnf l, Maybe (Value n))
_ -> Core l -> Check l (Core l)
forall a. a -> Check l a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Core l
bodyIn
            domC <- quoteM dom'
            mshapeC <- traverse quoteM mshapeAssumed
            pure (Lambda (Just (Param md' domC mshapeC)) body')
          Maybe (Pi n)
Nothing -> TypeError n -> Check n (Core n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (Raw n -> Value n -> TypeError n
forall (n :: S). Raw n -> Value n -> TypeError n
TypeErrorUnexpectedLambda Raw n
term Value n
ty)

        RLet Maybe (Raw n)
mannot Raw n
val ScopedRaw n
body -> do
          (val', valTy) <- Action n -> Check n (Core n, Value n) -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => Action n -> Check n a -> Check n a
performing (Maybe VarIdent -> Action n
forall (n :: S). Maybe VarIdent -> Action n
ActionCheckLetValue (ScopedRaw n -> Maybe VarIdent
forall (n :: S). ScopedRaw n -> Maybe VarIdent
patternName ScopedRaw n
body)) (Check n (Core n, Value n) -> Check n (Core n, Value n))
-> Check n (Core n, Value n) -> Check n (Core n, Value n)
forall a b. (a -> b) -> a -> b
$ Maybe (Raw n) -> Raw n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Maybe (Raw n) -> Raw n -> Check n (Core n, Value n)
inferOrCheckValue Maybe (Raw n)
mannot Raw n
val
          valV <- evalM val'
          body' <- underLetBody valTy valV body $ \Pattern n l
_ Raw l
bodyTerm ->
            Raw l -> Value l -> Check l (Core l)
forall (n :: S). Distinct n => Raw n -> Value n -> Check n (Core n)
check Raw l
bodyTerm (Value n -> Value l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Value n
ty)
          valTyC <- quoteM valTy
          pure (Let (Just valTyC) val' body')

        RPair Raw n
l Raw n
r -> case Whnf n
tyW of
          VCubeProduct Value n
a Value n
b -> do
            l' <- Raw n -> Value n -> Check n (Core n)
forall (n :: S). Distinct n => Raw n -> Value n -> Check n (Core n)
check Raw n
l Value n
a
            r' <- check r b
            pure (Pair l' r')
          Whnf n
_ | Just (Sigma Env i n
env TModality
md Value n
a ScopedCore i
b) <- Value n -> Maybe (Sigma n)
forall (n :: S). Value n -> Maybe (Sigma n)
sigmaOf (Whnf n -> Value n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Whnf binder sig n -> Value binder sig n
NbE.fromWhnf Whnf n
tyW) -> do
            Bool -> Check n () -> Check n ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TModality
md TModality -> TModality -> Bool
forall a. Eq a => a -> a -> Bool
/= TModality
Id) (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"a modal Σ type")
            l' <- Raw n -> Value n -> Check n (Core n)
forall (n :: S). Distinct n => Raw n -> Value n -> Check n (Core n)
check Raw n
l Value n
a
            case force a of
              Whnf n
VUniverse -> Core n -> Check n (Value n)
forall (n :: S). Distinct n => Core n -> Check n (Value n)
evalM Core n
l' Check n (Value n) -> (Value n -> Check n ()) -> Check n ()
forall a b. Check n a -> (a -> Check n b) -> Check n b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> Value n -> Check n ()
forall (n :: S). Distinct n => String -> Value n -> Check n ()
ensureTypeAtU String
"a pair component at type U"
              Whnf n
_ -> () -> Check n ()
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
            lV <- evalM l'
            r' <- check r (codomainAt scope ctx env b lV)
            pure (Pair l' r')
          Whnf n
_ -> TypeError n -> Check n (Core n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (Raw n -> Value n -> TypeError n
forall (n :: S). Raw n -> Value n -> TypeError n
TypeErrorUnexpectedPair Raw n
term Value n
ty)

        RRefl Maybe (Raw n, Maybe (Raw n))
mx -> case Whnf n
tyW of
          VTypeId Value n
y Maybe (Value n)
mtA Value n
z -> do
            tA <- case Maybe (Value n)
mtA of
              Just Value n
tA -> Value n -> Check n (Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value n
tA
              Maybe (Value n)
Nothing -> TypeError n -> Check n (Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorOther String
"an identity type without its carrier")
            forM_ mx $ \(Raw n
x, Maybe (Raw n)
mxty) -> do
              Maybe (Raw n) -> (Raw n -> Check n ()) -> Check n ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe (Raw n)
mxty ((Raw n -> Check n ()) -> Check n ())
-> (Raw n -> Check n ()) -> Check n ()
forall a b. (a -> b) -> a -> b
$ \Raw n
xty -> do
                (_, xtyV) <- Raw n -> Value n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
xty Value n
forall (n :: S). Value n
universe
                convertTypes tA xtyV
              (_, xV) <- Raw n -> Value n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
x Value n
tA
              convertTerms xV y
              convertTerms xV z
            when (isNothing mx) $ convertTerms y z
            pure (Refl Nothing)
          Whnf n
_ -> TypeError n -> Check n (Core n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (Raw n -> Value n -> TypeError n
forall (n :: S). Raw n -> Value n -> TypeError n
TypeErrorUnexpectedRefl Raw n
term Value n
ty)

        RRecOr [(Raw n, Raw n)]
rs -> case Whnf n
tyW of
          Whnf n
VUniverseTope -> TypeError n -> Check n (Core n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (TypeError n -> Check n (Core n))
-> TypeError n -> Check n (Core n)
forall a b. (a -> b) -> a -> b
$ String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorOther String
"a recOR cannot be used as a tope"
          Whnf n
_ -> Value n -> [(Raw n, Raw n)] -> Check n (Core n)
forall (n :: S).
Distinct n =>
Value n -> [(Raw n, Raw n)] -> Check n (Core n)
checkRecOrAgainst Value n
ty [(Raw n, Raw n)]
rs

        RMatch{} -> TypeError n -> Check n (Core n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"match")
        RLetMod{} -> TypeError n -> Check n (Core n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"let mod")
        RModApp{} -> TypeError n -> Check n (Core n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"mod")

        -- a neutral term is inferred, then its type compared with the
        -- expected one
        Raw n
_ -> do
          (term', inferred) <- Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer Raw n
term
          subtype term' ty inferred
          pure term'

-- | The name a let binds, for the trace.
patternName :: ScopedRaw n -> Maybe VarIdent
patternName :: forall (n :: S). ScopedRaw n -> Maybe VarIdent
patternName (ScopedAST Pattern n l
pat AST Pattern (AnnSig SrcPos Sig) l
_) = case Pattern n l
pat of
  PatternVar Maybe VarIdent
x NameBinder n l
_ -> Maybe VarIdent
x
  Pattern n l
_              -> Maybe VarIdent
forall a. Maybe a
Nothing

-- | Enter the scope of a let: its names have the components of the value.
underLet
  :: forall n r. (Distinct n)
  => Value n -> Value n -> ScopedRaw n
  -> (forall l. (Distinct l, DExt n l) => Pattern n l -> Raw l -> Check l r)
  -> Check n r
underLet :: forall (n :: S) r.
Distinct n =>
Value n
-> Value n
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l r)
-> Check n r
underLet Value n
ty Value n
val = (forall (l :: S).
 (Distinct l, DExt n l) =>
 Scope l -> Pattern n l -> Context n -> Context l)
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
(forall (l :: S).
 (Distinct l, DExt n l) =>
 Scope l -> Pattern n l -> Context n -> Context l)
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l r)
-> Check n r
enterRawScope ((forall (l :: S).
  (Distinct l, DExt n l) =>
  Scope l -> Pattern n l -> Context n -> Context l)
 -> ScopedRaw n
 -> (forall (l :: S).
     (Distinct l, DExt n l) =>
     Pattern n l -> Raw l -> Check l r)
 -> Check n r)
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Scope l -> Pattern n l -> Context n -> Context l)
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l r)
-> Check n r
forall a b. (a -> b) -> a -> b
$ \Scope l
scope' Pattern n l
pat Context n
ctx ->
  Pattern n l -> [Entry l] -> Context n -> Context l
forall (n :: S) (l :: S).
(Distinct n, DExt n l) =>
Pattern n l -> [Entry l] -> Context n -> Context l
enterPattern Pattern n l
pat
    (Scope l
-> Context l
-> Maybe LocationInfo
-> Value l
-> Value l
-> Pattern n l
-> [Entry l]
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Scope l
-> Context l
-> Maybe LocationInfo
-> Value l
-> Value l
-> Pattern n l
-> [Entry l]
letEntries Scope l
scope' (Context n -> Context l
forall (n :: S) (l :: S). DExt n l => Context n -> Context l
sinkContextUnchecked Context n
ctx) (Context n -> Maybe LocationInfo
forall (n :: S). Context n -> Maybe LocationInfo
ctxLocation Context n
ctx) (Value n -> Value l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Value n
ty) (Value n -> Value l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Value n
val) Pattern n l
pat)
    Context n
ctx

-- | Enter a local definition's scope and wrap the elaborated body with its
-- pattern.
underLetBody
  :: forall n. (Distinct n)
  => Value n -> Value n -> ScopedRaw n
  -> (forall l. (Distinct l, DExt n l) => Pattern n l -> Raw l -> Check l (Core l))
  -> Check n (ScopedCore n)
underLetBody :: forall (n :: S).
Distinct n =>
Value n
-> Value n
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l (Core l))
-> Check n (ScopedCore n)
underLetBody Value n
ty Value n
val ScopedRaw n
body forall (l :: S).
(Distinct l, DExt n l) =>
Pattern n l -> Raw l -> Check l (Core l)
k = Value n
-> Value n
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l (ScopedCore n))
-> Check n (ScopedCore n)
forall (n :: S) r.
Distinct n =>
Value n
-> Value n
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l r)
-> Check n r
underLet Value n
ty Value n
val ScopedRaw n
body ((forall (l :: S).
  (Distinct l, DExt n l) =>
  Pattern n l -> Raw l -> Check l (ScopedCore n))
 -> Check n (ScopedCore n))
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l (ScopedCore n))
-> Check n (ScopedCore n)
forall a b. (a -> b) -> a -> b
$ \Pattern n l
pat Raw l
bodyTerm ->
  Pattern n l -> AST Pattern Sig l -> ScopedCore n
forall (binder :: S -> S -> *) (n :: S) (l :: S)
       (sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST Pattern n l
pat (AST Pattern Sig l -> ScopedCore n)
-> Check l (AST Pattern Sig l) -> Check l (ScopedCore n)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern n l -> Raw l -> Check l (AST Pattern Sig l)
forall (l :: S).
(Distinct l, DExt n l) =>
Pattern n l -> Raw l -> Check l (Core l)
k Pattern n l
pat Raw l
bodyTerm

-- | A @recOR@ against a known type: every branch is checked against it
-- under its guard, the branches cohere, and the guards cover the context.
checkRecOrAgainst :: Distinct n => Value n -> [(Raw n, Raw n)] -> Check n (Core n)
checkRecOrAgainst :: forall (n :: S).
Distinct n =>
Value n -> [(Raw n, Raw n)] -> Check n (Core n)
checkRecOrAgainst Value n
ty [(Raw n, Raw n)]
rs = do
  ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
  let scope = Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx
  branches <- forM rs $ \(Raw n
tope, Raw n
branch) -> do
    (tope', topeV) <- Raw n -> Value n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
tope Value n
forall (n :: S). Value n
topeUniverse
    checkTopeAgainstContext "recOR branch guard" topeV
    (branch', branchV) <- underTope (topeOf scope ctx topeV) $ checkEval branch ty
    pure ((tope', branch'), (topeV, branchV))
  sequence_ [ checkCoherence l r | l : rest <- tails (map snd branches), r <- rest ]
  contextEntailsUnion (map (fst . snd) branches)
  tyC <- quoteM ty
  pure (RecOr tyC (map fst branches))

-- * Inference

-- | Infer a term and compare its type's sort with the expected one.
inferAs :: Distinct n => Value n -> Raw n -> Check n (Core n, Value n)
inferAs :: forall (n :: S).
Distinct n =>
Value n -> Raw n -> Check n (Core n, Value n)
inferAs Value n
expectedKind Raw n
term = do
  (term', ty) <- Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer Raw n
term
  kind <- sortOf ty
  subtypeValues expectedKind kind
  pure (term', ty)

-- | The sort of a type value: the universe it lives in. The universes are
-- all @U@-sorted; a cube is in @CUBE@, a tope in @TOPE@; anything else is
-- a type, in @U@.
sortOf :: Distinct n => Value n -> Check n (Value n)
sortOf :: forall (n :: S). Distinct n => Value n -> Check n (Value n)
sortOf Value n
ty = do
  ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
  pure $ case force ty of
    Whnf n
VUniverse -> Value n
forall (n :: S). Value n
universe
    Whnf n
VUniverseCube -> Value n
forall (n :: S). Value n
universe
    Whnf n
VUniverseTope -> Value n
forall (n :: S). Value n
universe
    Whnf n
_ | Value n -> Cube
forall (n :: S). Value n -> Cube
cubeOfType Value n
ty Cube -> Cube -> Bool
forall a. Eq a => a -> a -> Bool
/= Cube
KOther -> Value n
forall (n :: S). Value n
cubeUniverse
    Whnf n
_ -> case Scope n -> Context n -> Value n -> Maybe (Value n)
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Maybe (Value n)
neutralType (Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx) Context n
ctx Value n
ty of
      Just Value n
s -> Value n -> Value n
forall (n :: S). Value n -> Value n
stripRestrictions Value n
s
      Maybe (Value n)
Nothing -> Value n
forall (n :: S). Value n
universe

-- | Infer a cube point used by @≡@. A bare hole here has an unknown cube.
inferPoint :: Distinct n => Raw n -> Check n (Core n, Value n)
inferPoint :: forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
inferPoint term :: Raw n
term@((RHole (Just VarIdent
name)))
  | Just VarIdent
x <- VarIdent -> Maybe VarIdent
unmarkUnresolved VarIdent
name = Action n -> Check n (Core n, Value n) -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => Action n -> Check n a -> Check n a
performing (Raw n -> Action n
forall (n :: S). Raw n -> Action n
ActionInfer Raw n
term) (Check n (Core n, Value n) -> Check n (Core n, Value n))
-> Check n (Core n, Value n) -> Check n (Core n, Value n)
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (VarIdent -> TypeError n
forall (n :: S). VarIdent -> TypeError n
TypeErrorUndefined VarIdent
x)
inferPoint term :: Raw n
term@((RHole Maybe VarIdent
mname)) = Action n -> Check n (Core n, Value n) -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => Action n -> Check n a -> Check n a
performing (Raw n -> Action n
forall (n :: S). Raw n -> Action n
ActionInfer Raw n
term) (Check n (Core n, Value n) -> Check n (Core n, Value n))
-> Check n (Core n, Value n) -> Check n (Core n, Value n)
forall a b. (a -> b) -> a -> b
$ do
  mode <- (Context n -> HoleMode) -> Check n HoleMode
forall (n :: S) a. (Context n -> a) -> Check n a
asksContext (Options -> HoleMode
optHoles (Options -> HoleMode)
-> (Context n -> Options) -> Context n -> HoleMode
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Context n -> Options
forall (n :: S). Context n -> Options
ctxOptions)
  case mode of
    HoleMode
HolesAreErrors -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (Raw n -> TypeError n
forall (n :: S). Raw n -> TypeError n
TypeErrorCannotInferHole Raw n
term)
    HoleMode
_ -> do
      let cube :: Value n
cube = Maybe VarIdent -> Value n
forall (n :: S). Maybe VarIdent -> Value n
vhole Maybe VarIdent
forall a. Maybe a
Nothing
      hole <- Maybe VarIdent -> Value n -> Check n (Core n)
forall (n :: S).
Distinct n =>
Maybe VarIdent -> Value n -> Check n (Core n)
checkHole Maybe VarIdent
mname Value n
forall (n :: S). Value n
cube
      pure (hole, cube)
inferPoint Raw n
term = Value n -> Raw n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Value n -> Raw n -> Check n (Core n, Value n)
inferAs Value n
forall (n :: S). Value n
cubeUniverse Raw n
term

-- | Infer a point of a directed cube, @2@ or @𝕀@, for @≤@.
inferDirectedPoint :: Distinct n => Raw n -> Check n (Core n, Value n)
inferDirectedPoint :: forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
inferDirectedPoint Raw n
term = Action n -> Check n (Core n, Value n) -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => Action n -> Check n a -> Check n a
performing (Raw n -> Action n
forall (n :: S). Raw n -> Action n
ActionInfer Raw n
term) (Check n (Core n, Value n) -> Check n (Core n, Value n))
-> Check n (Core n, Value n) -> Check n (Core n, Value n)
forall a b. (a -> b) -> a -> b
$ do
  (point, cube) <- Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
inferPoint Raw n
term
  case force cube of
    VHole Maybe VarIdent
_ -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
point, Value n
cube)
    Whnf n
VCube2 -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
point, Value n
cube)
    Whnf n
VCubeI -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
point, Value n
cube)
    Whnf n
_ -> do
      ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
      issueTypeError $ TypeErrorOther $
        "the (t ≤ s) tope expects a point in a directed cube (2 or 𝕀); got " <> ppValueIn ctx cube

infer :: forall n. Distinct n => Raw n -> Check n (Core n, Value n)
infer :: forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer Raw n
tt = Action n -> Check n (Core n, Value n) -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => Action n -> Check n a -> Check n a
performing (Raw n -> Action n
forall (n :: S). Raw n -> Action n
ActionInfer Raw n
tt) (Check n (Core n, Value n) -> Check n (Core n, Value n))
-> Check n (Core n, Value n) -> Check n (Core n, Value n)
forall a b. (a -> b) -> a -> b
$ case Raw n
tt of
  RHole (Just VarIdent
name) | Just VarIdent
x <- VarIdent -> Maybe VarIdent
unmarkUnresolved VarIdent
name ->
    TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (VarIdent -> TypeError n
forall (n :: S). VarIdent -> TypeError n
TypeErrorUndefined VarIdent
x)
  RHole Maybe VarIdent
_ -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (Raw n -> TypeError n
forall (n :: S). Raw n -> TypeError n
TypeErrorCannotInferHole Raw n
tt)
  Var Name n
x -> do
    entry <- (Context n -> Entry n) -> Check n (Entry n)
forall (n :: S) a. (Context n -> a) -> Check n a
asksContext (Name n -> Context n -> Entry n
forall (n :: S). Name n -> Context n -> Entry n
lookupEntry Name n
x)
    pure (Var x, entryType entry)

  Raw n
RUniverse -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
forall (n :: S). Core n
Universe, Value n
forall (n :: S). Value n
universe)
  Raw n
RUniverseCube -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
forall (n :: S). Core n
UniverseCube, Value n
forall (n :: S). Value n
universe)
  Raw n
RUniverseTope -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
forall (n :: S). Core n
UniverseTope, Value n
forall (n :: S). Value n
universe)

  Raw n
RCubeUnit -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
forall (n :: S). Core n
CubeUnit, Value n
forall (n :: S). Value n
cubeUniverse)
  Raw n
RCubeUnitStar -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
forall (n :: S). Core n
CubeUnitStar, Sig Void (Value n) -> Value n
forall (n :: S). Sig Void (Value n) -> Value n
vnode Sig Void (Value n)
forall scope term. Sig scope term
CubeUnitF)
  Raw n
RCube2 -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
forall (n :: S). Core n
Cube2, Value n
forall (n :: S). Value n
cubeUniverse)
  Raw n
RCube2_0 -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
forall (n :: S). Core n
Cube2_0, Value n
forall (n :: S). Value n
vcube2)
  Raw n
RCube2_1 -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
forall (n :: S). Core n
Cube2_1, Value n
forall (n :: S). Value n
vcube2)
  Raw n
RCubeI -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
forall (n :: S). Core n
CubeI, Value n
forall (n :: S). Value n
cubeUniverse)
  Raw n
RCubeI_0 -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
forall (n :: S). Core n
CubeI_0, Value n
forall (n :: S). Value n
vcubeI)
  Raw n
RCubeI_1 -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
forall (n :: S). Core n
CubeI_1, Value n
forall (n :: S). Value n
vcubeI)
  RCubeProduct Raw n
l Raw n
r -> do
    l' <- Raw n -> Value n -> Check n (Core n)
forall (n :: S). Distinct n => Raw n -> Value n -> Check n (Core n)
check Raw n
l Value n
forall (n :: S). Value n
cubeUniverse
    r' <- check r cubeUniverse
    pure (CubeProduct l' r', cubeUniverse)

  RCubeFlip{} -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"flip")
  RCubeUnflip{} -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"unflip")

  RCubeSup Raw n
l Raw n
r -> String
-> (Core n -> Core n -> Core n)
-> Raw n
-> Raw n
-> Check n (Core n, Value n)
lattice String
"sup" Core n -> Core n -> Core n
forall (n :: S). Core n -> Core n -> Core n
CubeSup Raw n
l Raw n
r
  RCubeInf Raw n
l Raw n
r -> String
-> (Core n -> Core n -> Core n)
-> Raw n
-> Raw n
-> Check n (Core n, Value n)
lattice String
"inf" Core n -> Core n -> Core n
forall (n :: S). Core n -> Core n -> Core n
CubeInf Raw n
l Raw n
r

  RPair Raw n
l Raw n
r -> do
    (l', lt) <- Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer Raw n
l
    (r', rt) <- infer r
    ltSort <- sortOf lt
    case force ltSort of
      Whnf n
VUniverseCube -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n -> Core n -> Core n
forall (n :: S). Core n -> Core n -> Core n
Pair Core n
l' Core n
r', Sig Void (Value n) -> Value n
forall (n :: S). Sig Void (Value n) -> Value n
vnode (Value n -> Value n -> Sig Void (Value n)
forall scope term. term -> term -> Sig scope term
CubeProductF Value n
lt Value n
rt))
      Whnf n
_ -> do
        -- a non-dependent pair
        ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
        rtC <- quoteM rt
        let sigma = Scope n
-> (forall (l :: S). DExt n l => NameBinder n l -> Value n)
-> Value n
forall (n :: S) r.
Distinct n =>
Scope n -> (forall (l :: S). DExt n l => NameBinder n l -> r) -> r
Foil.withFresh (Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx) ((forall (l :: S). DExt n l => NameBinder n l -> Value n)
 -> Value n)
-> (forall (l :: S). DExt n l => NameBinder n l -> Value n)
-> Value n
forall a b. (a -> b) -> a -> b
$ \NameBinder n l
b ->
              Substitution (Value' 'MayGlue Pattern Sig) n n
-> Sig (ScopedAST Pattern Sig n) (Value n) -> Value n
forall (i :: S) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
Distinct i =>
Substitution (Value binder sig) i n
-> sig (ScopedAST binder sig i) (Value binder sig n)
-> Value binder sig n
NbE.vsuspended Substitution (Value' 'MayGlue Pattern Sig) n n
forall (e :: S -> *) (i :: S). InjectName e => Substitution e i i
Foil.identitySubst
                (TModality
-> Value n
-> ScopedAST Pattern Sig n
-> Sig (ScopedAST Pattern Sig n) (Value n)
forall scope term. TModality -> term -> scope -> Sig scope term
TypeSigmaF TModality
Id Value n
lt (Pattern n l -> AST Pattern Sig l -> ScopedAST Pattern Sig n
forall (binder :: S -> S -> *) (n :: S) (l :: S)
       (sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST (NameBinder n l -> Pattern n l
forall (n :: S) (l :: S). NameBinder n l -> Pattern n l
singlePattern NameBinder n l
b) (Core n -> AST Pattern Sig l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Core n
rtC)))
        pure (Pair l' r', sigma)

  RFirst Raw n
t -> do
    (t', ty) <- Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer Raw n
t
    case force (stripRestrictions ty) of
      Whnf n
VRecBottom -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n -> Core n
forall (n :: S). Core n -> Core n
First Core n
t', Value n
forall (n :: S). Value n
vrecBottom)
      VCubeProduct Value n
a Value n
_ -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n -> Core n
forall (n :: S). Core n -> Core n
First Core n
t', Value n
a)
      Whnf n
_ | Just (Sigma Env i n
_ TModality
_ Value n
a ScopedCore i
_) <- Value n -> Maybe (Sigma n)
forall (n :: S). Value n -> Maybe (Sigma n)
sigmaOf Value n
ty -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n -> Core n
forall (n :: S). Core n -> Core n
First Core n
t', Value n
a)
      Whnf n
_ -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (Core n -> Value n -> TypeError n
forall (n :: S). Core n -> Value n -> TypeError n
TypeErrorNotPair Core n
t' Value n
ty)

  RSecond Raw n
t -> do
    (t', ty) <- Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer Raw n
t
    ctx <- askContext
    let scope = Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx
    case force (stripRestrictions ty) of
      Whnf n
VRecBottom -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n -> Core n
forall (n :: S). Core n -> Core n
Second Core n
t', Value n
forall (n :: S). Value n
vrecBottom)
      VCubeProduct Value n
_ Value n
b -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n -> Core n
forall (n :: S). Core n -> Core n
Second Core n
t', Value n
b)
      Whnf n
_ | Just (Sigma Env i n
env TModality
_ Value n
_ ScopedCore i
b) <- Value n -> Maybe (Sigma n)
forall (n :: S). Value n -> Maybe (Sigma n)
sigmaOf Value n
ty -> do
        tV <- Core n -> Check n (Value n)
forall (n :: S). Distinct n => Core n -> Check n (Value n)
evalM Core n
t'
        pure (Second t', codomainAt scope ctx env b (firstValue tV))
      Whnf n
_ -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (Core n -> Value n -> TypeError n
forall (n :: S). Core n -> Value n -> TypeError n
TypeErrorNotPair Core n
t' Value n
ty)

  Raw n
RTypeUnit -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
forall (n :: S). Core n
TypeUnit, Value n
forall (n :: S). Value n
universe)
  Raw n
RUnit -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
forall (n :: S). Core n
Unit, Value n
forall (n :: S). Value n
vtypeUnit)

  Raw n
RTopeTop -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
forall (n :: S). Core n
TopeTop, Value n
forall (n :: S). Value n
topeUniverse)
  Raw n
RTopeBottom -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
forall (n :: S). Core n
TopeBottom, Value n
forall (n :: S). Value n
topeUniverse)

  RTopeEQ Raw n
l Raw n
r -> do
    (l', lt) <- Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
inferPoint Raw n
l
    r' <- check r lt
    pure (TopeEQ l' r', topeUniverse)

  RTopeLEQ Raw n
l Raw n
r -> do
    (l', lTy) <- Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
inferDirectedPoint Raw n
l
    (r', rTy) <- inferDirectedPoint r
    case (force lTy, force rTy) of
      ((VHole Maybe VarIdent
_), Whnf n
_) -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n -> Core n -> Core n
forall (n :: S). Core n -> Core n -> Core n
TopeLEQ Core n
l' Core n
r', Value n
forall (n :: S). Value n
topeUniverse)
      (Whnf n
_, (VHole Maybe VarIdent
_)) -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n -> Core n -> Core n
forall (n :: S). Core n -> Core n -> Core n
TopeLEQ Core n
l' Core n
r', Value n
forall (n :: S). Value n
topeUniverse)
      (Whnf n
VCube2, Whnf n
VCube2) -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n -> Core n -> Core n
forall (n :: S). Core n -> Core n -> Core n
TopeLEQ Core n
l' Core n
r', Value n
forall (n :: S). Value n
topeUniverse)
      (Whnf n
VCubeI, Whnf n
VCubeI) -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n -> Core n -> Core n
forall (n :: S). Core n -> Core n -> Core n
TopeLEQ Core n
l' Core n
r', Value n
forall (n :: S). Value n
topeUniverse)
      (Whnf n
VCubeI, Whnf n
VCube2) -> do
        r'' <- Raw n -> Value n -> Check n (Core n)
forall (n :: S). Distinct n => Raw n -> Value n -> Check n (Core n)
check Raw n
r Value n
forall (n :: S). Value n
vcubeI
        pure (TopeLEQ l' r'', topeUniverse)
      (Whnf n
VCube2, Whnf n
VCubeI) -> do
        l'' <- Raw n -> Value n -> Check n (Core n)
forall (n :: S). Distinct n => Raw n -> Value n -> Check n (Core n)
check Raw n
l Value n
forall (n :: S). Value n
vcubeI
        pure (TopeLEQ l'' r', topeUniverse)
      (Whnf n, Whnf n)
_ -> do
        ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
        issueTypeError $ TypeErrorOther $
          "the (t ≤ s) tope expects points in interval cubes (2 or 𝕀); got "
            <> ppValueIn ctx lTy <> " and " <> ppValueIn ctx rTy

  RTopeAnd Raw n
l Raw n
r -> do
    l' <- Raw n -> Value n -> Check n (Core n)
forall (n :: S). Distinct n => Raw n -> Value n -> Check n (Core n)
check Raw n
l Value n
forall (n :: S). Value n
topeUniverse
    r' <- check r topeUniverse
    pure (TopeAnd l' r', topeUniverse)

  RTopeOr Raw n
l Raw n
r -> do
    l' <- Raw n -> Value n -> Check n (Core n)
forall (n :: S). Distinct n => Raw n -> Value n -> Check n (Core n)
check Raw n
l Value n
forall (n :: S). Value n
topeUniverse
    r' <- check r topeUniverse
    pure (TopeOr l' r', topeUniverse)

  RTopeInv{} -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"a tope inversion")
  RTopeUninv{} -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"a tope inversion")

  Raw n
RRecBottom -> do
    Value n -> Check n ()
forall (n :: S). Distinct n => Value n -> Check n ()
contextEntails Value n
forall (n :: S). Value n
vtopeBottom
    (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n
forall (n :: S). Core n
RecBottom, Value n
forall (n :: S). Value n
vrecBottom)

  RRecOr [(Raw n, Raw n)]
rs -> do
    ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
    let scope = Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx
    branches <- forM rs $ \(Raw n
tope, Raw n
branch) -> do
      (tope', topeV) <- Raw n -> Value n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
tope Value n
forall (n :: S). Value n
topeUniverse
      checkTopeAgainstContext "recOR branch guard" topeV
      (branch', ty) <- underTope (topeOf scope ctx topeV) $ inferAs universe branch
      branchV <- evalM branch'
      pure ((tope', branch'), (topeV, branchV), (topeV, ty))
    sequence_ [ checkCoherence l r | l : rest <- tails [ b | (_, b, _) <- branches ], r <- rest ]
    contextEntailsUnion [ t | (_, (t, _), _) <- branches ]
    let ty = Scope n -> Context n -> [(Value n, Value n)] -> Value n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> [(Value n, Value n)] -> Value n
recOrValue Scope n
scope Context n
ctx [ (Value n, Value n)
t | ((Core n, Core n)
_, (Value n, Value n)
_, (Value n, Value n)
t) <- [((Core n, Core n), (Value n, Value n), (Value n, Value n))]
branches ]
    tyC <- quoteM ty
    pure (RecOr tyC [ b | (b, _, _) <- branches ], ty)

  RTypeFun TModality
md Raw n
a Maybe (Raw n)
Nothing ScopedRaw n
b -> do
    Bool -> Check n () -> Check n ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TModality
md TModality -> TModality -> Bool
forall a. Eq a => a -> a -> Bool
/= TModality
Id) (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"a modal binder")
    (a', aTy) <- Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer Raw n
a
    aV <- evalM a'
    ctx <- askContext
    let scope = Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx
    case force aTy of
      -- a type, including TOPE: a bare tope parameter
      Whnf n
VUniverse -> do
        b' <- TModality
-> Value n
-> Maybe (Value n)
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l (Core l))
-> Check n (ScopedAST Pattern Sig n)
forall (n :: S).
Distinct n =>
TModality
-> Value n
-> Maybe (Value n)
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l (Core l))
-> Check n (ScopedCore n)
underRawScopeBody TModality
md Value n
aV Maybe (Value n)
forall a. Maybe a
Nothing ScopedRaw n
b ((forall (l :: S).
  (Distinct l, DExt n l) =>
  Pattern n l -> Raw l -> Check l (Core l))
 -> Check n (ScopedAST Pattern Sig n))
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l (Core l))
-> Check n (ScopedAST Pattern Sig n)
forall a b. (a -> b) -> a -> b
$ \Pattern n l
_ Raw l
bTerm -> Raw l -> Value l -> Check l (Core l)
forall (n :: S). Distinct n => Raw n -> Value n -> Check n (Core n)
check Raw l
bTerm Value l
forall (n :: S). Value n
universe
        pure (TypeFun md a' Nothing b', universe)
      -- a cube: the shape is ⊤
      Whnf n
VUniverseCube -> do
        let shapeV :: Value n
shapeV = Scope n -> Value n -> Value n
forall (n :: S). Distinct n => Scope n -> Value n -> Value n
topShapeValue Scope n
scope Value n
aV
        shapeC <- Value n -> Check n (Core n)
forall (n :: S). Distinct n => Value n -> Check n (Core n)
quoteM Value n
shapeV
        piOrFamily md a' shapeC aV shapeV b
      -- a shape: a tope family over a cube
      Whnf n
_ | Just (Family TModality
_ Value n
cube Maybe (Value n)
mdomShape) <- Value n -> Maybe (Family n)
forall (n :: S). Value n -> Maybe (Family n)
familyOf Value n
aTy -> do
          cubeC <- Value n -> Check n (Core n)
forall (n :: S). Distinct n => Value n -> Check n (Core n)
quoteM Value n
cube
          mdomC <- traverse quoteM mdomShape
          let shapeC = Scope n -> Core n -> Core n -> Maybe (Core n) -> Core n
forall (n :: S).
Distinct n =>
Scope n -> Core n -> Core n -> Maybe (Core n) -> Core n
familyShape Scope n
scope Core n
cubeC Core n
a' Maybe (Core n)
mdomC
          shapeV <- evalM shapeC
          piOrFamily md cubeC shapeC cube shapeV b
      Whnf n
_ -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (Raw n -> Value n -> TypeError n
forall (n :: S). Raw n -> Value n -> TypeError n
TypeErrorInvalidArgumentType Raw n
a Value n
aTy)

  RTypeFun TModality
md Raw n
cube (Just Raw n
shape) ScopedRaw n
ret -> do
    Bool -> Check n () -> Check n ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TModality
md TModality -> TModality -> Bool
forall a. Eq a => a -> a -> Bool
/= TModality
Id) (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"a modal binder")
    (cube', cubeV) <- Raw n -> Value n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
cube Value n
forall (n :: S). Value n
cubeUniverse
    (shape', shapeV) <- checkEval shape (familyType Id cubeV Nothing)
    piOrFamily md cube' shape' cubeV shapeV ret

  RTypeSigma TModality
md Raw n
a ScopedRaw n
b -> do
    Bool -> Check n () -> Check n ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TModality
md TModality -> TModality -> Bool
forall a. Eq a => a -> a -> Bool
/= TModality
Id) (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"a modal Σ type")
    (a', aV) <- Raw n -> Value n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
a Value n
forall (n :: S). Value n
universe
    ensureTypeAtU "a Σ component" aV
    b' <- underRawScopeBody md aV Nothing b $ \Pattern n l
_ Raw l
bTerm -> do
      bIn <- Raw l -> Value l -> Check l (Core l)
forall (n :: S). Distinct n => Raw n -> Value n -> Check n (Core n)
check Raw l
bTerm Value l
forall (n :: S). Value n
universe
      evalM bIn >>= ensureTypeAtU "a Σ component"
      pure bIn
    pure (TypeSigma md a' b', universe)

  RTypeId Raw n
x (Just Raw n
tA) Raw n
y -> do
    (tA', tAV) <- Raw n -> Value n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
tA Value n
forall (n :: S). Value n
universe
    ensureTypeAtU "the carrier of an identity type" tAV
    x' <- check x tAV
    y' <- check y tAV
    pure (TypeId x' (Just tA') y', universe)

  RTypeId Raw n
x Maybe (Raw n)
Nothing Raw n
y -> do
    (x', tAV) <- Value n -> Raw n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Value n -> Raw n -> Check n (Core n, Value n)
inferAs Value n
forall (n :: S). Value n
universe Raw n
x
    ensureTypeAtU "the carrier of an identity type" tAV
    y' <- check y tAV
    tA' <- quoteM tAV
    pure (TypeId x' (Just tA') y', universe)

  RApp Raw n
f Raw n
x -> do
    (f', fTy) <- Value n -> Raw n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Value n -> Raw n -> Check n (Core n, Value n)
inferAs Value n
forall (n :: S). Value n
universe Raw n
f
    ctx <- askContext
    let scope = Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx
    case piOf fTy of
      Maybe (Pi n)
_ | Just (Family TModality
md Value n
cube Maybe (Value n)
_) <- Value n -> Maybe (Family n)
forall (n :: S). Value n -> Maybe (Family n)
familyOf Value n
fTy -> do
        Bool -> Check n () -> Check n ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TModality
md TModality -> TModality -> Bool
forall a. Eq a => a -> a -> Bool
/= TModality
Id) (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"a modal function")
        x' <- Raw n -> Value n -> Check n (Core n)
forall (n :: S). Distinct n => Raw n -> Value n -> Check n (Core n)
check Raw n
x Value n
cube
        pure (App f' x', topeUniverse)
      Just (Pi Env i n
env TModality
md Value n
a Maybe (Value n)
mshape ScopedCore i
b) -> do
        Bool -> Check n () -> Check n ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TModality
md TModality -> TModality -> Bool
forall a. Eq a => a -> a -> Bool
/= TModality
Id) (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"a modal function")
        (x', xV) <- Raw n -> Value n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
x Value n
a
        case force a of
          Whnf n
VUniverse -> String -> Value n -> Check n ()
forall (n :: S). Distinct n => String -> Value n -> Check n ()
ensureTypeAtU String
"a type argument" Value n
xV
          Whnf n
_ -> () -> Check n ()
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
        forM_ mshape $ \Value n
shape -> Value n -> Check n ()
forall (n :: S). Distinct n => Value n -> Check n ()
contextEntails (Scope n -> Context n -> Value n -> Value n -> Value n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n -> Value n
appValue Scope n
scope Context n
ctx Value n
shape Value n
xV)
        pure (App f' x', codomainAt scope ctx env b xV)
      Maybe (Pi n)
Nothing -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (Core n -> Value n -> TypeError n
forall (n :: S). Core n -> Value n -> TypeError n
TypeErrorNotFunction Core n
f' Value n
fTy)

  RLambda Maybe (Param (Raw n))
Nothing ScopedRaw n
_ -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (Raw n -> TypeError n
forall (n :: S). Raw n -> TypeError n
TypeErrorCannotInferBareLambda Raw n
tt)

  RLambda (Just (Param TModality
md Raw n
ty Maybe (Raw n)
Nothing)) ScopedRaw n
body -> do
    Bool -> Check n () -> Check n ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TModality
md TModality -> TModality -> Bool
forall a. Eq a => a -> a -> Bool
/= TModality
Id) (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"a modal binder")
    (ty', tyTy) <- Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer Raw n
ty
    tyV <- evalM ty'
    ctx <- askContext
    let scope = Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx
    (dom, mshape) <- case force tyTy of
      Whnf n
VUniverse -> (Value n, Maybe (Value n)) -> Check n (Value n, Maybe (Value n))
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Value n
tyV, Maybe (Value n)
forall a. Maybe a
Nothing)
      Whnf n
VUniverseCube -> (Value n, Maybe (Value n)) -> Check n (Value n, Maybe (Value n))
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Value n
tyV, Value n -> Maybe (Value n)
forall a. a -> Maybe a
Just (Scope n -> Value n -> Value n
forall (n :: S). Distinct n => Scope n -> Value n -> Value n
topShapeValue Scope n
scope Value n
tyV))
      Whnf n
_ | Just (Family TModality
_ Value n
cube Maybe (Value n)
mdomShape) <- Value n -> Maybe (Family n)
forall (n :: S). Value n -> Maybe (Family n)
familyOf Value n
tyTy -> do
          cubeC <- Value n -> Check n (Core n)
forall (n :: S). Distinct n => Value n -> Check n (Core n)
quoteM Value n
cube
          mdomC <- traverse quoteM mdomShape
          shapeV <- evalM (familyShape scope cubeC ty' mdomC)
          pure (cube, Just shapeV)
      Whnf n
_ -> TypeError n -> Check n (Value n, Maybe (Value n))
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (Raw n -> Value n -> TypeError n
forall (n :: S). Raw n -> Value n -> TypeError n
TypeErrorInvalidArgumentType Raw n
ty Value n
tyTy)
    inferLambda md dom mshape body

  RLambda (Just (Param TModality
md Raw n
cube (Just Raw n
shape))) ScopedRaw n
body -> do
    Bool -> Check n () -> Check n ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TModality
md TModality -> TModality -> Bool
forall a. Eq a => a -> a -> Bool
/= TModality
Id) (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"a modal binder")
    (_, cubeV) <- Raw n -> Value n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
cube Value n
forall (n :: S). Value n
cubeUniverse
    (_, shapeV) <- checkEval shape (familyType Id cubeV Nothing)
    inferLambda md cubeV (Just shapeV) body

  RLet Maybe (Raw n)
mannot Raw n
val ScopedRaw n
body -> do
    (val', valTy) <- Action n -> Check n (Core n, Value n) -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => Action n -> Check n a -> Check n a
performing (Maybe VarIdent -> Action n
forall (n :: S). Maybe VarIdent -> Action n
ActionCheckLetValue (ScopedRaw n -> Maybe VarIdent
forall (n :: S). ScopedRaw n -> Maybe VarIdent
patternName ScopedRaw n
body)) (Check n (Core n, Value n) -> Check n (Core n, Value n))
-> Check n (Core n, Value n) -> Check n (Core n, Value n)
forall a b. (a -> b) -> a -> b
$ Maybe (Raw n) -> Raw n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Maybe (Raw n) -> Raw n -> Check n (Core n, Value n)
inferOrCheckValue Maybe (Raw n)
mannot Raw n
val
    valV <- evalM val'
    valTyC <- quoteM valTy
    (body', retScoped) <- underLet valTy valV body $ \Pattern n l
pat Raw l
bodyTerm -> do
      (bodyIn, retIn) <- Raw l -> Check l (Core l, Value l)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer Raw l
bodyTerm
      retC <- quoteM retIn
      pure (ScopedAST pat bodyIn, ScopedAST pat retC)
    ctx <- askContext
    let ret = Scope n
-> Context n
-> Substitution (Value' 'MayGlue Pattern Sig) n n
-> ScopedAST Pattern Sig n
-> Value n
-> Value n
forall (n :: S) (i :: S).
(Distinct n, Distinct i) =>
Scope n
-> Context n -> Env i n -> ScopedCore i -> Value n -> Value n
evalScope (Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx) Context n
ctx Substitution (Value' 'MayGlue Pattern Sig) n n
forall (e :: S -> *) (i :: S). InjectName e => Substitution e i i
Foil.identitySubst ScopedAST Pattern Sig n
retScoped Value n
valV
    pure (Let (Just valTyC) val' body', ret)

  RLetMod{} -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"let mod")

  RRefl Maybe (Raw n, Maybe (Raw n))
Nothing -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (Raw n -> TypeError n
forall (n :: S). Raw n -> TypeError n
TypeErrorCannotInferBareRefl Raw n
tt)
  RRefl (Just (Raw n
x, Maybe (Raw n)
Nothing)) -> do
    (x', ty) <- Value n -> Raw n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Value n -> Raw n -> Check n (Core n, Value n)
inferAs Value n
forall (n :: S). Value n
universe Raw n
x
    xV <- evalM x'
    tyC <- quoteM ty
    pure (Refl (Just (x', Just tyC)), vnode (TypeIdF xV (Just ty) xV))
  RRefl (Just (Raw n
x, Just Raw n
ty)) -> do
    (ty', tyV) <- Raw n -> Value n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
ty Value n
forall (n :: S). Value n
universe
    (x', xV) <- checkEval x tyV
    pure (Refl (Just (x', Just ty')), vnode (TypeIdF xV (Just tyV) xV))

  RIdJ Raw n
tA Raw n
a Raw n
tC Raw n
d Raw n
x Raw n
p -> do
    (tA', tAV) <- Raw n -> Value n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
tA Value n
forall (n :: S). Value n
universe
    (a', aV) <- checkEval a tAV
    ctx <- askContext
    let scope = Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx
        -- (z : A) → (a =_A z) → U
        motiveTy = Scope n
-> (forall (l :: S). DExt n l => NameBinder n l -> Value n)
-> Value n
forall (n :: S) r.
Distinct n =>
Scope n -> (forall (l :: S). DExt n l => NameBinder n l -> r) -> r
Foil.withFresh Scope n
scope ((forall (l :: S). DExt n l => NameBinder n l -> Value n)
 -> Value n)
-> (forall (l :: S). DExt n l => NameBinder n l -> Value n)
-> Value n
forall a b. (a -> b) -> a -> b
$ \NameBinder n l
zB ->
          let z :: Name l
z = NameBinder n l -> Name l
forall (n :: S) (l :: S). NameBinder n l -> Name l
Foil.nameOf NameBinder n l
zB
              scopeZ :: Scope l
scopeZ = NameBinder n l -> Scope n -> Scope l
forall (n :: S) (l :: S). NameBinder n l -> Scope n -> Scope l
Foil.extendScope NameBinder n l
zB Scope n
scope
              idType :: Core l
idType = Core l -> Maybe (Core l) -> Core l -> Core l
forall (n :: S). Core n -> Maybe (Core n) -> Core n -> Core n
TypeId (Core n -> Core l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Core n
a') (Core l -> Maybe (Core l)
forall a. a -> Maybe a
Just (Core n -> Core l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Core n
tA')) (Name l -> Core l
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var Name l
z)
          in Scope l
-> (forall (l :: S). DExt l l => NameBinder l l -> Value n)
-> Value n
forall (n :: S) r.
Distinct n =>
Scope n -> (forall (l :: S). DExt n l => NameBinder n l -> r) -> r
Foil.withFresh Scope l
scopeZ ((forall (l :: S). DExt l l => NameBinder l l -> Value n)
 -> Value n)
-> (forall (l :: S). DExt l l => NameBinder l l -> Value n)
-> Value n
forall a b. (a -> b) -> a -> b
$ \NameBinder l l
pB ->
               Substitution (Value' 'MayGlue Pattern Sig) n n
-> Sig (ScopedAST Pattern Sig n) (Value n) -> Value n
forall (i :: S) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
Distinct i =>
Substitution (Value binder sig) i n
-> sig (ScopedAST binder sig i) (Value binder sig n)
-> Value binder sig n
NbE.vsuspended Substitution (Value' 'MayGlue Pattern Sig) n n
forall (e :: S -> *) (i :: S). InjectName e => Substitution e i i
Foil.identitySubst
                 (TModality
-> Value n
-> Maybe (Value n)
-> ScopedAST Pattern Sig n
-> Sig (ScopedAST Pattern Sig n) (Value n)
forall scope term.
TModality -> term -> Maybe term -> scope -> Sig scope term
TypeFunF TModality
Id Value n
tAV Maybe (Value n)
forall a. Maybe a
Nothing
                   (Pattern n l -> Core l -> ScopedAST Pattern Sig n
forall (binder :: S -> S -> *) (n :: S) (l :: S)
       (sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST (NameBinder n l -> Pattern n l
forall (n :: S) (l :: S). NameBinder n l -> Pattern n l
singlePattern NameBinder n l
zB)
                     (TModality -> Core l -> Maybe (Core l) -> ScopedCore l -> Core l
forall (n :: S).
TModality -> Core n -> Maybe (Core n) -> ScopedCore n -> Core n
TypeFun TModality
Id Core l
idType Maybe (Core l)
forall a. Maybe a
Nothing (Pattern l l -> AST Pattern Sig l -> ScopedCore l
forall (binder :: S -> S -> *) (n :: S) (l :: S)
       (sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST (NameBinder l l -> Pattern l l
forall (n :: S) (l :: S). NameBinder n l -> Pattern n l
singlePattern NameBinder l l
pB) AST Pattern Sig l
forall (n :: S). Core n
Universe))))
    (tC', tCV) <- checkEval tC motiveTy
    let reflAt = Sig Void (Value n) -> Value n
forall (n :: S). Sig Void (Value n) -> Value n
vnode (Maybe (Value n, Maybe (Value n)) -> Sig Void (Value n)
forall scope term. Maybe (term, Maybe term) -> Sig scope term
ReflF Maybe (Value n, Maybe (Value n))
forall a. Maybe a
Nothing)
        typeOfD = Scope n -> Context n -> Value n -> Value n -> Value n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n -> Value n
appValue Scope n
scope Context n
ctx (Scope n -> Context n -> Value n -> Value n -> Value n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n -> Value n
appValue Scope n
scope Context n
ctx Value n
tCV Value n
aV) Value n
forall (n :: S). Value n
reflAt
    d' <- check d typeOfD
    (x', xV) <- checkEval x tAV
    (p', pV) <- checkEval p (vnode (TypeIdF aV (Just tAV) xV))
    pure (IdJ tA' a' tC' d' x' p', appValue scope ctx (appValue scope ctx tCV xV) pV)

  -- an annotation is built by evaluation and never written
  RAnn Raw n
term Raw n
_ -> Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer Raw n
term

  RTypeAsc Raw n
term Raw n
ty -> do
    (ty', _) <- Value n -> Raw n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Value n -> Raw n -> Check n (Core n, Value n)
inferAs Value n
forall (n :: S). Value n
universe Raw n
ty
    tyV <- evalM ty'
    term' <- check term tyV
    pure (TypeAsc term' ty', tyV)

  RTypeRestricted Raw n
ty [(Raw n, Raw n)]
rs -> do
    (ty', tyV) <- Raw n -> Value n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
ty Value n
forall (n :: S). Value n
universe
    ctx <- askContext
    let scope = Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx
    rs' <- forM rs $ \(Raw n
tope, Raw n
term) -> do
      (tope', topeV) <- Raw n -> Value n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw n
tope Value n
forall (n :: S). Value n
topeUniverse
      (term', termV) <- underTope (topeOf scope ctx topeV) $ checkEval term tyV
      pure ((tope', term'), (topeV, termV))
    sequence_ [ checkCoherence l r | l : rest <- tails (map snd rs'), r <- rest ]
    pure (TypeRestricted ty' (map fst rs'), universe)

  RTypeModal{} -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"a modal type")
  RModApp{} -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"mod")
  RModExtract{} -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"extract")
  RMatch{} -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"match")
  RMatchArm{} -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
"a match arm")
  RTypeShape{} -> String -> Check n (Core n, Value n)
forall a. String -> a
panicImpossible String
"a family type in a raw term"
  where
    lattice :: String -> (Core n -> Core n -> Core n) -> Raw n -> Raw n -> Check n (Core n, Value n)
    lattice :: String
-> (Core n -> Core n -> Core n)
-> Raw n
-> Raw n
-> Check n (Core n, Value n)
lattice String
name Core n -> Core n -> Core n
mk Raw n
l Raw n
r = do
      (l', lTy) <- Value n -> Raw n -> Check n (Core n, Value n)
forall (n :: S).
Distinct n =>
Value n -> Raw n -> Check n (Core n, Value n)
inferAs Value n
forall (n :: S). Value n
cubeUniverse Raw n
l
      (r', rTy) <- inferAs cubeUniverse r
      case (force lTy, force rTy) of
        (Whnf n
VCube2, Whnf n
VCube2) -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n -> Core n -> Core n
mk Core n
l' Core n
r', Value n
forall (n :: S). Value n
vcube2)
        (Whnf n
VCubeI, Whnf n
VCubeI) -> (Core n, Value n) -> Check n (Core n, Value n)
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Core n -> Core n -> Core n
mk Core n
l' Core n
r', Value n
forall (n :: S). Value n
vcubeI)
        (Whnf n
VCubeI, Whnf n
VCube2) -> do
          r'' <- Raw n -> Value n -> Check n (Core n)
forall (n :: S). Distinct n => Raw n -> Value n -> Check n (Core n)
check Raw n
r Value n
forall (n :: S). Value n
vcubeI
          pure (mk l' r'', vcubeI)
        (Whnf n
VCube2, Whnf n
VCubeI) -> do
          l'' <- Raw n -> Value n -> Check n (Core n)
forall (n :: S). Distinct n => Raw n -> Value n -> Check n (Core n)
check Raw n
l Value n
forall (n :: S). Value n
vcubeI
          pure (mk l'' r', vcubeI)
        (Whnf n, Whnf n)
_ -> TypeError n -> Check n (Core n, Value n)
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> Value n -> Value n -> TypeError n
forall (n :: S). String -> Value n -> Value n -> TypeError n
TypeErrorNotIntervalCube String
name Value n
lTy Value n
rTy)

-- | A Π over a cube with a shape, or, when its codomain is @TOPE@, the
-- type of a family over that shape.
piOrFamily
  :: (Distinct n)
  => TModality -> Core n -> Core n -> Value n -> Value n -> ScopedRaw n -> Check n (Core n, Value n)
piOrFamily :: forall (n :: S).
Distinct n =>
TModality
-> Core n
-> Core n
-> Value n
-> Value n
-> ScopedRaw n
-> Check n (Core n, Value n)
piOrFamily TModality
md Core n
cubeC Core n
shapeC Value n
cubeV Value n
shapeV ScopedRaw n
ret = do
  (ret', isTope) <- TModality
-> Value n
-> Maybe (Value n)
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l (ScopedCore n, Bool))
-> Check n (ScopedCore n, Bool)
forall (n :: S) r.
Distinct n =>
TModality
-> Value n
-> Maybe (Value n)
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l r)
-> Check n r
underRawScope TModality
md Value n
cubeV (Value n -> Maybe (Value n)
forall a. a -> Maybe a
Just Value n
shapeV) ScopedRaw n
ret ((forall (l :: S).
  (Distinct l, DExt n l) =>
  Pattern n l -> Raw l -> Check l (ScopedCore n, Bool))
 -> Check n (ScopedCore n, Bool))
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l (ScopedCore n, Bool))
-> Check n (ScopedCore n, Bool)
forall a b. (a -> b) -> a -> b
$ \Pattern n l
pat Raw l
retTerm -> do
    (retIn, retV) <- Raw l -> Value l -> Check l (Core l, Value l)
forall (n :: S).
Distinct n =>
Raw n -> Value n -> Check n (Core n, Value n)
checkEval Raw l
retTerm Value l
forall (n :: S). Value n
universe
    let isTope = case Value l -> Whnf l
forall (n :: S). Value n -> Whnf n
force Value l
retV of
          Whnf l
VUniverseTope -> Bool
True
          Whnf l
_ -> Bool
False
    pure (ScopedAST pat retIn, isTope)
  if isTope
    then pure (TypeShape md cubeC (Just shapeC), universe)
    else pure (TypeFun md cubeC (Just shapeC) ret', universe)

-- | Infer a λ whose parameter is known: the body is inferred under it, and
-- the λ's type is the Π over the pattern with the body's type as codomain.
inferLambda
  :: (Distinct n)
  => TModality -> Value n -> Maybe (Value n) -> ScopedRaw n -> Check n (Core n, Value n)
inferLambda :: forall (n :: S).
Distinct n =>
TModality
-> Value n
-> Maybe (Value n)
-> ScopedRaw n
-> Check n (Core n, Value n)
inferLambda TModality
md Value n
dom Maybe (Value n)
mshape ScopedRaw n
body = do
  (body', retScoped, isTope) <- TModality
-> Value n
-> Maybe (Value n)
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l (ScopedCore n, ScopedCore n, Bool))
-> Check n (ScopedCore n, ScopedCore n, Bool)
forall (n :: S) r.
Distinct n =>
TModality
-> Value n
-> Maybe (Value n)
-> ScopedRaw n
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l r)
-> Check n r
underRawScope TModality
md Value n
dom Maybe (Value n)
mshape ScopedRaw n
body ((forall (l :: S).
  (Distinct l, DExt n l) =>
  Pattern n l -> Raw l -> Check l (ScopedCore n, ScopedCore n, Bool))
 -> Check n (ScopedCore n, ScopedCore n, Bool))
-> (forall (l :: S).
    (Distinct l, DExt n l) =>
    Pattern n l -> Raw l -> Check l (ScopedCore n, ScopedCore n, Bool))
-> Check n (ScopedCore n, ScopedCore n, Bool)
forall a b. (a -> b) -> a -> b
$ \Pattern n l
pat Raw l
bodyTerm -> do
    (bodyIn, retIn) <- Raw l -> Check l (Core l, Value l)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer Raw l
bodyTerm
    retC <- quoteM retIn
    let isTope = case Value l -> Whnf l
forall (n :: S). Value n -> Whnf n
force Value l
retIn of
          Whnf l
VUniverseTope -> Bool
True
          Whnf l
_ -> Bool
False
    pure (ScopedAST pat bodyIn, ScopedAST pat retC, isTope)
  domC <- quoteM dom
  mshapeC <- traverse quoteM mshape
  let ty | Bool
isTope, Just Value n
_ <- Maybe (Value n)
mshape = TModality -> Value n -> Maybe (Value n) -> Value n
forall (n :: S). TModality -> Value n -> Maybe (Value n) -> Value n
familyType TModality
md Value n
dom Maybe (Value n)
mshape
         | Bool
otherwise = Substitution (Value' 'MayGlue Pattern Sig) n n
-> Sig (ScopedCore n) (Value n) -> Value n
forall (i :: S) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
Distinct i =>
Substitution (Value binder sig) i n
-> sig (ScopedAST binder sig i) (Value binder sig n)
-> Value binder sig n
NbE.vsuspended Substitution (Value' 'MayGlue Pattern Sig) n n
forall (e :: S -> *) (i :: S). InjectName e => Substitution e i i
Foil.identitySubst (TModality
-> Value n
-> Maybe (Value n)
-> ScopedCore n
-> Sig (ScopedCore n) (Value n)
forall scope term.
TModality -> term -> Maybe term -> scope -> Sig scope term
TypeFunF TModality
md Value n
dom Maybe (Value n)
mshape ScopedCore n
retScoped)
  pure (Lambda (Just (Param md domC mshapeC)) body', ty)