{-# LANGUAGE DataKinds             #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE GADTs                 #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PatternSynonyms       #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE StandaloneDeriving    #-}
{-# LANGUAGE TypeApplications      #-}
-- The 'HasHeads' instance of the signature is an orphan: the class is
-- free-foil-NBE's and the signature is "Rzk.Check.Syntax"'s, and it
-- belongs with the rules here.
{-# OPTIONS_GHC -Wno-orphans #-}
-- | Values: Rzk's rules as an 'Eval' instance of free-foil-NBE on the
-- checker's context.
--
-- Evaluation is the library's; this module gives it the elimination rules
-- (β, the projections, path induction, @let@, a @recOR@ whose guard the tope
-- context entails, the lattice and flip operations of the cube layer, the
-- connectives of the tope layer), the η laws of λ and pairs, what each
-- binder adds to the context, how two nodes compare (subtyping in the
-- context's variance, holes, topes by entailment), and the refinement of a
-- neutral by a restriction of its type.
--
-- The algorithm is free-foil-NBE's (Renata Shakirova and Nikolai Kudasov):
-- normalisation by evaluation generic in the signature, conversion checking
-- after Coquand (/An algorithm for type-checking dependent types/, 1996),
-- the untyped η for surjective pairs of Abel and Coquand (2007), glued
-- evaluation and approximate conversion checking after Kovács's smalltt, and
-- subtyping as conversion with the direction in the context, as in Rocq's
-- kernel and in Rzk's own unification. The rules follow the evaluator and
-- the unification of "Rzk.TypeCheck".
module Rzk.Check.Value
  ( -- * Values
    Value
  , Whnf
  , pattern VUniverse
  , pattern VUniverseCube
  , pattern VUniverseTope
  , pattern VCubeUnit
  , pattern VCubeUnitStar
  , pattern VCube2
  , pattern VCube2_0
  , pattern VCube2_1
  , pattern VCubeI
  , pattern VCubeI_0
  , pattern VCubeI_1
  , pattern VCubeProduct
  , pattern VCubeFlip
  , pattern VCubeUnflip
  , pattern VCubeSup
  , pattern VCubeInf
  , pattern VTopeTop
  , pattern VTopeBottom
  , pattern VTopeEQ
  , pattern VTopeLEQ
  , pattern VTopeAnd
  , pattern VTopeOr
  , pattern VTopeInv
  , pattern VTopeUninv
  , pattern VRecBottom
  , pattern VRecOr
  , pattern VTypeFun
  , pattern VTypeShape
  , pattern VTypeSigma
  , pattern VTypeId
  , pattern VApp
  , pattern VLet
  , pattern VLambda
  , pattern VPair
  , pattern VFirst
  , pattern VSecond
  , pattern VRefl
  , pattern VIdJ
  , pattern VMatch
  , pattern VMatchArm
  , pattern VUnit
  , pattern VTypeUnit
  , pattern VTypeAsc
  , pattern VAnn
  , pattern VTypeRestricted
  , pattern VTypeModal
  , pattern VModApp
  , pattern VModExtract
  , pattern VLetMod
  , pattern VHole
  , Env
    -- * Evaluation and readback
  , evalCore
  , evalScope
  , quoteIn
  , quoteFolded
  , quoteWhnfFolded
  , quoteWhnfCore
  , nfCore
  , force
  , conv
  , convertible
    -- * Values the rules build
  , vnode
  , universe
  , cubeUniverse
  , topeUniverse
  , vcube2
  , vcubeI
  , vtopeBottom
  , vunit
  , vtypeUnit
  , vhole
  , vrecBottom
    -- * Elimination
  , appValue
  , firstValue
  , secondValue
  , codomainAt
  , Pi (..)
  , piOf
  , Sigma (..)
  , sigmaOf
  , Family (..)
  , familyOf
  , isTopShape
  , stripRestrictions
  , restrictionsOf
    -- * Patterns
  , patternValue
  , bindPattern
  , patternEntries
  , letEntries
  , singlePattern
  , underPattern
  , underFreshVar
  , isCube
  , topShapeValue
    -- * The tope layer
  , topeOf
  , pointOf
  , cubeOfType
  , entailsValue
  , assumeValue
  , topeAndValue
  , recOrValue
    -- * Types of neutrals
  , neutralType
    -- * Holes
  ) where

import           Control.Applicative         ((<|>))
import           Control.Monad.Foil          (DExt, Distinct, NameBinder, Scope)
import qualified Control.Monad.Foil          as Foil
import           Control.Monad.Free.Foil     (AST (..), ScopedAST (..))
import           Data.Bifoldable             (bifoldMap)
import           Data.Bifunctor              (bimap)
import           Data.List                   (intercalate)
import           Data.Maybe                  (fromMaybe, listToMaybe)
import           Data.Monoid                 (Any (..))
import           Data.Void                   (Void)
import           Unsafe.Coerce               (unsafeCoerce)

import           FreeFoil.NbE                (EtaScoped (..), EtaTerm (..),
                                              Eval (..), Extend (..),
                                              GlueSpeculation (..),
                                              HasDefinitions (..),
                                              HasHeads (..))
import qualified FreeFoil.NbE                as NbE
import qualified FreeFoil.NbE.Conversion     as Conv

import           Language.Rzk.Foil.Names     (TModality (..), VarIdent)
import           Rzk.Check.Context
import           Rzk.Check.Syntax
import           Rzk.Check.Topes

-- * Values

type Whnf = NbE.Whnf Pattern Sig

type Env i n = Foil.Substitution (NbE.Value Pattern Sig) i n

-- A node's constructor, for fingerprints.
deriving instance Show (Sig () ())

-- * The library's hooks

-- | A defined name is glued to its value; any other name is a neutral.
instance HasDefinitions Context Pattern Sig where
  {-# INLINE contextDefinition #-}
  contextDefinition :: forall (n :: S).
Scope n -> Context n -> Name n -> Value Pattern Sig n
contextDefinition Scope n
_ Context n
ctx Name n
x = case Entry n -> Maybe (Value Pattern Sig n)
forall (n :: S). Entry n -> Maybe (Value n)
entryValue (Name n -> Context n -> Entry n
forall (n :: S). Name n -> Context n -> Entry n
lookupEntry Name n
x Context n
ctx) of
    Just Value Pattern Sig n
value -> Name n -> Value Pattern Sig n -> Value Pattern Sig n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> Value binder sig n -> Value binder sig n
NbE.vglued Name n
x Value Pattern Sig n
value
    Maybe (Value Pattern Sig n)
Nothing    -> Name n -> Value Pattern Sig n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> Value binder sig n
NbE.vvar Name n
x
  {-# INLINE contextUnfolding #-}
  contextUnfolding :: forall (n :: S).
Scope n
-> Context n
-> Value Pattern Sig n
-> Value Pattern Sig n
-> Value Pattern Sig n
contextUnfolding Scope n
_ Context n
ctx Value Pattern Sig n
folded Value Pattern Sig n
unfolded = case Context n -> Unfolding
forall (n :: S). Context n -> Unfolding
ctxUnfolding Context n
ctx of
    Unfolding
Folded   -> Value Pattern Sig n
folded
    Unfolding
Unfolded -> Value Pattern Sig n
unfolded
  {-# INLINE contextGlueSpeculation #-}
  contextGlueSpeculation :: forall (n :: S).
Scope n
-> Context n
-> Name n
-> Value Pattern Sig n
-> Name n
-> Value Pattern Sig n
-> GlueSpeculation Context n
contextGlueSpeculation Scope n
_ Context n
ctx Name n
x Value Pattern Sig n
_ Name n
y Value Pattern Sig n
_ = case Context n -> Approximation
forall (n :: S). Context n -> Approximation
ctxApproximation Context n
ctx of
    Approximation
ApproxRigid
      | Name n
x Name n -> Name n -> Bool
forall a. Eq a => a -> a -> Bool
== Name n
y    -> Context n -> Maybe (Context n) -> GlueSpeculation Context n
forall (ctx :: S -> *) (n :: S).
ctx n -> Maybe (ctx n) -> GlueSpeculation ctx n
TryFolded Context n
ctx { ctxApproximation = ApproxFlex } (Context n -> Maybe (Context n)
forall a. a -> Maybe a
Just Context n
ctx { ctxApproximation = ApproxFull })
      | Bool
otherwise -> Context n -> GlueSpeculation Context n
forall (ctx :: S -> *) (n :: S). ctx n -> GlueSpeculation ctx n
CompareUnfolded Context n
ctx
    Approximation
ApproxFlex
      | Name n
x Name n -> Name n -> Bool
forall a. Eq a => a -> a -> Bool
== Name n
y    -> Context n -> Maybe (Context n) -> GlueSpeculation Context n
forall (ctx :: S -> *) (n :: S).
ctx n -> Maybe (ctx n) -> GlueSpeculation ctx n
TryFolded Context n
ctx Maybe (Context n)
forall a. Maybe a
Nothing
      | Bool
otherwise -> GlueSpeculation Context n
forall (ctx :: S -> *) (n :: S). GlueSpeculation ctx n
Unrelated
    -- Two equal names are compared by their arguments first here too:
    -- unfolding them again at every level would normalise a repeated
    -- definition once per occurrence, exponentially in its nesting, where
    -- the old checker compared spines on one definition by congruence.
    Approximation
ApproxFull
      | Name n
x Name n -> Name n -> Bool
forall a. Eq a => a -> a -> Bool
== Name n
y    -> Context n -> Maybe (Context n) -> GlueSpeculation Context n
forall (ctx :: S -> *) (n :: S).
ctx n -> Maybe (ctx n) -> GlueSpeculation ctx n
TryFolded Context n
ctx { ctxApproximation = ApproxFlex } (Context n -> Maybe (Context n)
forall a. a -> Maybe a
Just Context n
ctx)
      | Bool
otherwise -> Context n -> GlueSpeculation Context n
forall (ctx :: S -> *) (n :: S). ctx n -> GlueSpeculation ctx n
CompareUnfolded Context n
ctx
  {-# INLINE contextUnfoldGlued #-}
  contextUnfoldGlued :: forall (n :: S).
Scope n
-> Context n -> Name n -> Value Pattern Sig n -> Maybe (Context n)
contextUnfoldGlued Scope n
_ Context n
ctx Name n
_ Value Pattern Sig n
_ = case Context n -> Approximation
forall (n :: S). Context n -> Approximation
ctxApproximation Context n
ctx of
    Approximation
ApproxFlex -> Maybe (Context n)
forall a. Maybe a
Nothing
    Approximation
_          -> Context n -> Maybe (Context n)
forall a. a -> Maybe a
Just Context n
ctx

-- | The heads: the function of an application, the pair of a projection,
-- the path of an induction.
instance HasHeads Sig where
  {-# INLINE headsWith #-}
  headsWith :: forall b d a. (b -> d) -> (b -> d) -> Sig a b -> Sig a d
headsWith b -> d
hd b -> d
other = \case
    AppF b
f b
x           -> d -> d -> Sig a d
forall scope term. term -> term -> Sig scope term
AppF (b -> d
hd b
f) (b -> d
other b
x)
    FirstF b
p           -> d -> Sig a d
forall scope term. term -> Sig scope term
FirstF (b -> d
hd b
p)
    SecondF b
p          -> d -> Sig a d
forall scope term. term -> Sig scope term
SecondF (b -> d
hd b
p)
    IdJF b
tA b
a b
tC b
d b
x b
p -> d -> d -> d -> d -> d -> d -> Sig a d
forall scope term.
term -> term -> term -> term -> term -> term -> Sig scope term
IdJF (b -> d
other b
tA) (b -> d
other b
a) (b -> d
other b
tC) (b -> d
other b
d) (b -> d
other b
x) (b -> d
hd b
p)
    AnnF b
t b
ty          -> d -> d -> Sig a d
forall scope term. term -> term -> Sig scope term
AnnF (b -> d
hd b
t) (b -> d
other b
ty)
    Sig a b
node               -> (a -> a) -> (b -> d) -> Sig a b -> Sig a d
forall a b c d. (a -> b) -> (c -> d) -> Sig a c -> Sig b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> a
forall a. a -> a
id b -> d
other Sig a b
node

{-# SPECIALIZE NbE.eval ::
      (Distinct o, Distinct i) => Scope o -> Context o -> Env i o -> Core i -> Value o #-}
{-# SPECIALIZE NbE.quote :: Distinct n => Scope n -> Context n -> Value n -> Core n #-}
{-# SPECIALIZE NbE.nf :: Distinct n => Scope n -> Context n -> Core n -> Core n #-}
{-# SPECIALIZE Conv.conv :: Distinct n => Scope n -> Context n -> Value n -> Value n -> Bool #-}
{-# SPECIALIZE Conv.convertible :: Distinct n => Scope n -> Context n -> Core n -> Core n -> Bool #-}

instance Eval Context Pattern Sig where
  {-# INLINABLE evalSig #-}
  evalSig :: forall (o :: S) (i :: S).
(Distinct o, Distinct i) =>
Scope o
-> Context o
-> Substitution (Value Pattern Sig) i o
-> Sig (ScopedAST Pattern Sig i) (AST Pattern Sig i)
-> Value Pattern Sig o
evalSig Scope o
scope Context o
ctx Substitution (Value Pattern Sig) i o
env = \case
    AppF AST Pattern Sig i
fun AST Pattern Sig i
arg -> Scope o
-> Context o
-> Value Pattern Sig o
-> Value Pattern Sig o
-> Value Pattern Sig o
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n -> Value n
appValue Scope o
scope Context o
ctx (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
fun) (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
arg)
    FirstF AST Pattern Sig i
p -> Scope o -> Context o -> Value Pattern Sig o -> Value Pattern Sig o
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
firstValueIn Scope o
scope Context o
ctx (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
p)
    SecondF AST Pattern Sig i
p -> Scope o -> Context o -> Value Pattern Sig o -> Value Pattern Sig o
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
secondValueIn Scope o
scope Context o
ctx (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
p)
    AnnF AST Pattern Sig i
term AST Pattern Sig i
_ -> AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
term
    LetF Maybe (AST Pattern Sig i)
_mty AST Pattern Sig i
val ScopedAST Pattern Sig i
body -> Scope o
-> Context o
-> Substitution (Value Pattern Sig) i o
-> ScopedAST Pattern Sig i
-> Value Pattern Sig o
-> Value Pattern Sig o
forall (n :: S) (i :: S).
(Distinct n, Distinct i) =>
Scope n
-> Context n -> Env i n -> ScopedCore i -> Value n -> Value n
evalScope Scope o
scope Context o
ctx Substitution (Value Pattern Sig) i o
env ScopedAST Pattern Sig i
body (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
val)
    TypeAscF AST Pattern Sig i
term AST Pattern Sig i
_ty -> AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
term
    IdJF AST Pattern Sig i
tA AST Pattern Sig i
a AST Pattern Sig i
tC AST Pattern Sig i
d AST Pattern Sig i
x AST Pattern Sig i
p -> Scope o
-> Context o
-> Value Pattern Sig o
-> Value Pattern Sig o
-> Value Pattern Sig o
-> Value Pattern Sig o
-> Value Pattern Sig o
-> Value Pattern Sig o
-> Value Pattern Sig o
forall (n :: S).
Distinct n =>
Scope n
-> Context n
-> Value n
-> Value n
-> Value n
-> Value n
-> Value n
-> Value n
-> Value n
idJValueIn Scope o
scope Context o
ctx (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
tA) (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
a) (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
tC) (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
d) (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
x) (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
p)
    -- A face whose tope is ⊥ restricts nothing, and a type with no face
    -- left is its underlying type, as the old checker's weak head normal
    -- form dropped them. Such a face arises by instantiation, as with the
    -- subshape ⊥ of a shape.
    TypeRestrictedF AST Pattern Sig i
ty [(AST Pattern Sig i, AST Pattern Sig i)]
rs ->
      case [ (Value Pattern Sig o
tope', AST Pattern Sig i
face) | (AST Pattern Sig i
tope, AST Pattern Sig i
face) <- [(AST Pattern Sig i, AST Pattern Sig i)]
rs, let tope' :: Value Pattern Sig o
tope' = AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
tope, Bool -> Bool
not (Value Pattern Sig o -> Bool
forall (n :: S). Value n -> Bool
isBottomTope Value Pattern Sig o
tope') ] of
        []  -> AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
ty
        [(Value Pattern Sig o, AST Pattern Sig i)]
rs' -> Sig Void (Value Pattern Sig o) -> Value Pattern Sig o
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value Pattern Sig o
-> [(Value Pattern Sig o, Value Pattern Sig o)]
-> Sig Void (Value Pattern Sig o)
forall scope term. term -> [(term, term)] -> Sig scope term
TypeRestrictedF (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
ty) [ (Value Pattern Sig o
tope', AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
face) | (Value Pattern Sig o
tope', AST Pattern Sig i
face) <- [(Value Pattern Sig o, AST Pattern Sig i)]
rs' ])
    -- The branch whose guard the tope context entails; otherwise the one
    -- branch if they are all the same term; otherwise stuck.
    RecOrF Maybe (AST Pattern Sig i)
mty [(AST Pattern Sig i, AST Pattern Sig i)]
rs ->
      let rs' :: [(Value Pattern Sig o, AST Pattern Sig i)]
rs' = [ (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
guard, AST Pattern Sig i
branch) | (AST Pattern Sig i
guard, AST Pattern Sig i
branch) <- [(AST Pattern Sig i, AST Pattern Sig i)]
rs ]
      in case [ AST Pattern Sig i
branch | (Value Pattern Sig o
guard, AST Pattern Sig i
branch) <- [(Value Pattern Sig o, AST Pattern Sig i)]
rs', Scope o -> Context o -> Value Pattern Sig o -> Bool
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Bool
entailsValue Scope o
scope Context o
ctx Value Pattern Sig o
guard ] of
           AST Pattern Sig i
branch : [AST Pattern Sig i]
_ -> AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
branch
           [] -> case ((AST Pattern Sig i, AST Pattern Sig i) -> AST Pattern Sig i)
-> [(AST Pattern Sig i, AST Pattern Sig i)] -> [AST Pattern Sig i]
forall a b. (a -> b) -> [a] -> [b]
map (AST Pattern Sig i, AST Pattern Sig i) -> AST Pattern Sig i
forall a b. (a, b) -> b
snd [(AST Pattern Sig i, AST Pattern Sig i)]
rs of
             AST Pattern Sig i
branch : [AST Pattern Sig i]
branches
               | (AST Pattern Sig i -> Bool) -> [AST Pattern Sig i] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (AST Pattern Sig i -> AST Pattern Sig i -> Bool
forall (n :: S). Distinct n => Core n -> Core n -> Bool
eqCore AST Pattern Sig i
branch) [AST Pattern Sig i]
branches -> AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
branch
             [AST Pattern Sig i]
_ -> Sig Void (Value Pattern Sig o) -> Value Pattern Sig o
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Maybe (Value Pattern Sig o)
-> [(Value Pattern Sig o, Value Pattern Sig o)]
-> Sig Void (Value Pattern Sig o)
forall scope term. Maybe term -> [(term, term)] -> Sig scope term
RecOrF ((AST Pattern Sig i -> Value Pattern Sig o)
-> Maybe (AST Pattern Sig i) -> Maybe (Value Pattern Sig o)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AST Pattern Sig i -> Value Pattern Sig o
ev Maybe (AST Pattern Sig i)
mty) [ (Value Pattern Sig o
guard, AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
branch) | (Value Pattern Sig o
guard, AST Pattern Sig i
branch) <- [(Value Pattern Sig o, AST Pattern Sig i)]
rs' ])
    -- the cube layer
    CubeFlipF AST Pattern Sig i
t -> case Value Pattern Sig o -> Whnf Pattern Sig o
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
t) of
      VCubeUnflip Value Pattern Sig o
t' -> Value Pattern Sig o
t'
      Whnf Pattern Sig o
VCube2_0 -> Value Pattern Sig o -> Value Pattern Sig o
forall {binder :: S -> S -> *} {n :: S}.
Value binder Sig n -> Value binder Sig n
opp (Sig Void (Value Pattern Sig o) -> Value Pattern Sig o
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig o)
forall scope term. Sig scope term
Cube2_1F)
      Whnf Pattern Sig o
VCube2_1 -> Value Pattern Sig o -> Value Pattern Sig o
forall {binder :: S -> S -> *} {n :: S}.
Value binder Sig n -> Value binder Sig n
opp (Sig Void (Value Pattern Sig o) -> Value Pattern Sig o
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig o)
forall scope term. Sig scope term
Cube2_0F)
      Whnf Pattern Sig o
VCubeI_0 -> Value Pattern Sig o -> Value Pattern Sig o
forall {binder :: S -> S -> *} {n :: S}.
Value binder Sig n -> Value binder Sig n
opp (Sig Void (Value Pattern Sig o) -> Value Pattern Sig o
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig o)
forall scope term. Sig scope term
CubeI_1F)
      Whnf Pattern Sig o
VCubeI_1 -> Value Pattern Sig o -> Value Pattern Sig o
forall {binder :: S -> S -> *} {n :: S}.
Value binder Sig n -> Value binder Sig n
opp (Sig Void (Value Pattern Sig o) -> Value Pattern Sig o
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig o)
forall scope term. Sig scope term
CubeI_0F)
      NbE.Stuck Value Pattern Sig o
t' -> Sig Void (Value Pattern Sig o) -> Value Pattern Sig o
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value Pattern Sig o -> Sig Void (Value Pattern Sig o)
forall scope term. term -> Sig scope term
CubeFlipF Value Pattern Sig o
t')
    CubeUnflipF AST Pattern Sig i
t -> case Value Pattern Sig o -> Whnf Pattern Sig o
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
t) of
      VCubeFlip Value Pattern Sig o
t' -> Value Pattern Sig o
t'
      VModApp TModality
Op Value Pattern Sig o
p | Whnf Pattern Sig o
VCube2_0 <- Value Pattern Sig o -> Whnf Pattern Sig o
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value Pattern Sig o
p -> Sig Void (Value Pattern Sig o) -> Value Pattern Sig o
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig o)
forall scope term. Sig scope term
Cube2_1F
      VModApp TModality
Op Value Pattern Sig o
p | Whnf Pattern Sig o
VCube2_1 <- Value Pattern Sig o -> Whnf Pattern Sig o
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value Pattern Sig o
p -> Sig Void (Value Pattern Sig o) -> Value Pattern Sig o
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig o)
forall scope term. Sig scope term
Cube2_0F
      VModApp TModality
Op Value Pattern Sig o
p | Whnf Pattern Sig o
VCubeI_0 <- Value Pattern Sig o -> Whnf Pattern Sig o
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value Pattern Sig o
p -> Sig Void (Value Pattern Sig o) -> Value Pattern Sig o
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig o)
forall scope term. Sig scope term
CubeI_1F
      VModApp TModality
Op Value Pattern Sig o
p | Whnf Pattern Sig o
VCubeI_1 <- Value Pattern Sig o -> Whnf Pattern Sig o
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value Pattern Sig o
p -> Sig Void (Value Pattern Sig o) -> Value Pattern Sig o
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig o)
forall scope term. Sig scope term
CubeI_0F
      NbE.Stuck Value Pattern Sig o
t' -> Sig Void (Value Pattern Sig o) -> Value Pattern Sig o
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value Pattern Sig o -> Sig Void (Value Pattern Sig o)
forall scope term. term -> Sig scope term
CubeUnflipF Value Pattern Sig o
t')
    CubeSupF AST Pattern Sig i
l AST Pattern Sig i
r -> Value Pattern Sig o -> Value Pattern Sig o -> Value Pattern Sig o
forall (n :: S). Value n -> Value n -> Value n
supValue (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
l) (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
r)
    CubeInfF AST Pattern Sig i
l AST Pattern Sig i
r -> Value Pattern Sig o -> Value Pattern Sig o -> Value Pattern Sig o
forall (n :: S). Value n -> Value n -> Value n
infValue (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
l) (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
r)
    -- the tope layer
    TopeAndF AST Pattern Sig i
l AST Pattern Sig i
r -> case Value Pattern Sig o -> Whnf Pattern Sig o
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
l) of
      Whnf Pattern Sig o
VTopeBottom -> Value Pattern Sig o
forall (n :: S). Value n
vtopeBottom
      NbE.Stuck Value Pattern Sig o
l' -> case Value Pattern Sig o -> Whnf Pattern Sig o
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
r) of
        Whnf Pattern Sig o
VTopeBottom -> Value Pattern Sig o
forall (n :: S). Value n
vtopeBottom
        NbE.Stuck Value Pattern Sig o
r' -> Sig Void (Value Pattern Sig o) -> Value Pattern Sig o
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value Pattern Sig o
-> Value Pattern Sig o -> Sig Void (Value Pattern Sig o)
forall scope term. term -> term -> Sig scope term
TopeAndF Value Pattern Sig o
l' Value Pattern Sig o
r')
    TopeOrF AST Pattern Sig i
l AST Pattern Sig i
r -> case (Value Pattern Sig o -> Whnf Pattern Sig o
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
l), Value Pattern Sig o -> Whnf Pattern Sig o
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force (AST Pattern Sig i -> Value Pattern Sig o
ev AST Pattern Sig i
r)) of
      (Whnf Pattern Sig o
VTopeBottom, Whnf Pattern Sig o
r') -> Whnf Pattern Sig o -> Value Pattern Sig o
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Whnf binder sig n -> Value binder sig n
NbE.fromWhnf Whnf Pattern Sig o
r'
      (Whnf Pattern Sig o
l', Whnf Pattern Sig o
VTopeBottom) -> Whnf Pattern Sig o -> Value Pattern Sig o
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Whnf binder sig n -> Value binder sig n
NbE.fromWhnf Whnf Pattern Sig o
l'
      (Whnf Pattern Sig o
l', Whnf Pattern Sig o
r') -> Sig Void (Value Pattern Sig o) -> Value Pattern Sig o
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value Pattern Sig o
-> Value Pattern Sig o -> Sig Void (Value Pattern Sig o)
forall scope term. term -> term -> Sig scope term
TopeOrF (Whnf Pattern Sig o -> Value Pattern Sig o
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Whnf binder sig n -> Value binder sig n
NbE.fromWhnf Whnf Pattern Sig o
l') (Whnf Pattern Sig o -> Value Pattern Sig o
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Whnf binder sig n -> Value binder sig n
NbE.fromWhnf Whnf Pattern Sig o
r'))
    Sig (ScopedAST Pattern Sig i) (AST Pattern Sig i)
node -> Scope o
-> Context o
-> Substitution (Value Pattern Sig) i o
-> Sig (ScopedAST Pattern Sig i) (AST Pattern Sig i)
-> Value Pattern Sig o
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (o :: S) (i :: S).
(Eval ctx binder sig, Distinct o, Distinct i) =>
Scope o
-> ctx o
-> Substitution (Value binder sig) i o
-> sig (ScopedAST binder sig i) (AST binder sig i)
-> Value binder sig o
NbE.evalNode Scope o
scope Context o
ctx Substitution (Value Pattern Sig) i o
env Sig (ScopedAST Pattern Sig i) (AST Pattern Sig i)
node
    where
      ev :: AST Pattern Sig i -> Value Pattern Sig o
ev = Scope o
-> Context o
-> Substitution (Value Pattern Sig) i o
-> AST Pattern Sig i
-> Value Pattern Sig o
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (o :: S) (i :: S).
(Eval ctx binder sig, Distinct o, Distinct i) =>
Scope o
-> ctx o
-> Substitution (Value binder sig) i o
-> AST binder sig i
-> Value binder sig o
NbE.eval Scope o
scope Context o
ctx Substitution (Value Pattern Sig) i o
env
      opp :: Value binder Sig n -> Value binder Sig n
opp = Sig Void (Value binder Sig n) -> Value binder Sig n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Sig Void (Value binder Sig n) -> Value binder Sig n)
-> (Value binder Sig n -> Sig Void (Value binder Sig n))
-> Value binder Sig n
-> Value binder Sig n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TModality -> Value binder Sig n -> Sig Void (Value binder Sig n)
forall scope term. TModality -> term -> Sig scope term
ModAppF TModality
Op

  -- The observers of a λ apply the other value to the pattern; those of a
  -- pair project it.
  {-# INLINE etaMatchWith #-}
  etaMatchWith :: forall a (n :: S) c b d.
(a -> EtaScoped Context Pattern Sig n -> c)
-> (b -> EtaTerm Context Pattern Sig n -> d)
-> Sig a b
-> Maybe (Sig c d)
etaMatchWith a -> EtaScoped Context Pattern Sig n -> c
scoped b -> EtaTerm Context Pattern Sig n -> d
term = \case
    -- the domain annotation is not compared: both sides have the same type
    LambdaF Maybe (Param b)
_ a
body -> Sig c d -> Maybe (Sig c d)
forall a. a -> Maybe a
Just (Maybe (Param d) -> c -> Sig c d
forall scope term. Maybe (Param term) -> scope -> Sig scope term
LambdaF Maybe (Param d)
forall a. Maybe a
Nothing (a -> EtaScoped Context Pattern Sig n -> c
scoped a
body ((forall (l :: S).
 (Eval Context Pattern Sig, Distinct n, DExt n l) =>
 Scope l
 -> Context l
 -> Pattern n l
 -> Value Pattern Sig l
 -> Value Pattern Sig l)
-> EtaScoped Context Pattern Sig n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
(forall (l :: S).
 (Eval ctx binder sig, Distinct n, DExt n l) =>
 Scope l
 -> ctx l -> binder n l -> Value binder sig l -> Value binder sig l)
-> EtaScoped ctx binder sig n
EtaScoped Scope l -> Context l -> Pattern n l -> Value l -> Value l
forall (l :: S).
(Eval Context Pattern Sig, Distinct n, DExt n l) =>
Scope l
-> Context l
-> Pattern n l
-> Value Pattern Sig l
-> Value Pattern Sig l
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Scope l -> Context l -> Pattern n l -> Value l -> Value l
appliedToPattern)))
    PairF b
a b
b -> Sig c d -> Maybe (Sig c d)
forall a. a -> Maybe a
Just (d -> d -> Sig c d
forall scope term. term -> term -> Sig scope term
PairF (b -> EtaTerm Context Pattern Sig n -> d
term b
a (((Eval Context Pattern Sig, Distinct n) =>
 Scope n -> Context n -> Value Pattern Sig n -> Value Pattern Sig n)
-> EtaTerm Context Pattern Sig n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
((Eval ctx binder sig, Distinct n) =>
 Scope n -> ctx n -> Value binder sig n -> Value binder sig n)
-> EtaTerm ctx binder sig n
EtaTerm (\Scope n
_ Context n
_ -> Value Pattern Sig n -> Value Pattern Sig n
forall (n :: S). Value n -> Value n
firstValue))) (b -> EtaTerm Context Pattern Sig n -> d
term b
b (((Eval Context Pattern Sig, Distinct n) =>
 Scope n -> Context n -> Value Pattern Sig n -> Value Pattern Sig n)
-> EtaTerm Context Pattern Sig n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
((Eval ctx binder sig, Distinct n) =>
 Scope n -> ctx n -> Value binder sig n -> Value binder sig n)
-> EtaTerm ctx binder sig n
EtaTerm (\Scope n
_ Context n
_ -> Value Pattern Sig n -> Value Pattern Sig n
forall (n :: S). Value n -> Value n
secondValue))))
    Sig a b
_ -> Maybe (Sig c d)
forall a. Maybe a
Nothing

  -- What a binder adds to the context.
  {-# INLINE extendScopedWith #-}
  extendScopedWith :: forall (n :: S) a c d.
Distinct n =>
(a -> Extend Context Pattern Sig n -> c)
-> (Value Pattern Sig n -> d)
-> Sig a (Value Pattern Sig n)
-> Sig c d
extendScopedWith a -> Extend Context Pattern Sig n -> c
scoped Value Pattern Sig n -> d
term = \case
    LambdaF Maybe (Param (Value Pattern Sig n))
mparam a
body ->
      Maybe (Param d) -> c -> Sig c d
forall scope term. Maybe (Param term) -> scope -> Sig scope term
LambdaF ((Param (Value Pattern Sig n) -> Param d)
-> Maybe (Param (Value Pattern Sig n)) -> Maybe (Param d)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Value Pattern Sig n -> d)
-> Param (Value Pattern Sig n) -> Param d
forall a b. (a -> b) -> Param a -> Param b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Value Pattern Sig n -> d
term) Maybe (Param (Value Pattern Sig n))
mparam) (a -> Extend Context Pattern Sig n -> c
scoped a
body (Maybe (Param (Value Pattern Sig n)) -> Extend Context Pattern Sig n
forall (n :: S).
Maybe (Param (Value n)) -> Extend Context Pattern Sig n
lambdaBinder Maybe (Param (Value Pattern Sig n))
mparam))
    TypeFunF TModality
md Value Pattern Sig n
dom Maybe (Value Pattern Sig n)
mshape a
ret ->
      TModality -> d -> Maybe d -> c -> Sig c d
forall scope term.
TModality -> term -> Maybe term -> scope -> Sig scope term
TypeFunF TModality
md (Value Pattern Sig n -> d
term Value Pattern Sig n
dom) ((Value Pattern Sig n -> d)
-> Maybe (Value Pattern Sig n) -> Maybe d
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Value Pattern Sig n -> d
term Maybe (Value Pattern Sig n)
mshape) (a -> Extend Context Pattern Sig n -> c
scoped a
ret (TModality
-> Value Pattern Sig n
-> Maybe (Value Pattern Sig n)
-> Extend Context Pattern Sig n
forall (n :: S).
TModality
-> Value n -> Maybe (Value n) -> Extend Context Pattern Sig n
piBinder TModality
md Value Pattern Sig n
dom Maybe (Value Pattern Sig n)
mshape))
    TypeSigmaF TModality
md Value Pattern Sig n
a a
b ->
      TModality -> d -> c -> Sig c d
forall scope term. TModality -> term -> scope -> Sig scope term
TypeSigmaF TModality
md (Value Pattern Sig n -> d
term Value Pattern Sig n
a) (a -> Extend Context Pattern Sig n -> c
scoped a
b (TModality -> Value Pattern Sig n -> Extend Context Pattern Sig n
forall (n :: S).
TModality -> Value n -> Extend Context Pattern Sig n
sigmaBinder TModality
md Value Pattern Sig n
a))
    LetF Maybe (Value Pattern Sig n)
mty Value Pattern Sig n
val a
body ->
      Maybe d -> d -> c -> Sig c d
forall scope term. Maybe term -> term -> scope -> Sig scope term
LetF ((Value Pattern Sig n -> d)
-> Maybe (Value Pattern Sig n) -> Maybe d
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Value Pattern Sig n -> d
term Maybe (Value Pattern Sig n)
mty) (Value Pattern Sig n -> d
term Value Pattern Sig n
val) (a -> Extend Context Pattern Sig n -> c
scoped a
body (Maybe (Value Pattern Sig n)
-> Value Pattern Sig n -> Extend Context Pattern Sig n
forall (n :: S).
Maybe (Value n) -> Value n -> Extend Context Pattern Sig n
letBinder Maybe (Value Pattern Sig n)
mty Value Pattern Sig n
val))
    LetModF TModality
app TModality
inn Maybe (Value Pattern Sig n)
mty Maybe (Value Pattern Sig n)
mmotive Value Pattern Sig n
val a
body ->
      TModality -> TModality -> Maybe d -> Maybe d -> d -> c -> Sig c d
forall scope term.
TModality
-> TModality
-> Maybe term
-> Maybe term
-> term
-> scope
-> Sig scope term
LetModF TModality
app TModality
inn ((Value Pattern Sig n -> d)
-> Maybe (Value Pattern Sig n) -> Maybe d
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Value Pattern Sig n -> d
term Maybe (Value Pattern Sig n)
mty) ((Value Pattern Sig n -> d)
-> Maybe (Value Pattern Sig n) -> Maybe d
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Value Pattern Sig n -> d
term Maybe (Value Pattern Sig n)
mmotive) (Value Pattern Sig n -> d
term Value Pattern Sig n
val)
        (a -> Extend Context Pattern Sig n -> c
scoped a
body Extend Context Pattern Sig n
forall (n :: S). Extend Context Pattern Sig n
opaqueBinder)
    MatchArmF a
body -> c -> Sig c d
forall scope term. scope -> Sig scope term
MatchArmF (a -> Extend Context Pattern Sig n -> c
scoped a
body Extend Context Pattern Sig n
forall (n :: S). Extend Context Pattern Sig n
opaqueBinder)
    Sig a (Value Pattern Sig n)
node -> (a -> c)
-> (Value Pattern Sig n -> d)
-> Sig a (Value Pattern Sig n)
-> Sig c d
forall a b c d. (a -> b) -> (c -> d) -> Sig a c -> Sig b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (c -> a -> c
forall a b. a -> b -> a
const c
forall {a}. a
noScopedPosition) Value Pattern Sig n -> d
term Sig a (Value Pattern Sig n)
node
    where
      noScopedPosition :: a
noScopedPosition = String -> a
forall a. String -> a
panicImpossible String
"a scoped position of a node without scoped positions"

  -- Two values that differ are equal all the same if they are points and
  -- the tope context entails their equation, as the old unification
  -- compared every two terms whose type is a cube; or if they are equal in
  -- every alternative of a disjunctive tope context, as the old unification
  -- retried a failed comparison in each alternative (a neutral restricted
  -- on the faces of a horn is its face's term in each of them).
  {-# INLINE relateMismatch #-}
  relateMismatch :: forall (n :: S).
(Eval Context Pattern Sig, Distinct n) =>
Scope n
-> Context n -> Value Pattern Sig n -> Value Pattern Sig n -> Bool
relateMismatch Scope n
scope Context n
ctx Value Pattern Sig n
l Value Pattern Sig n
r = Bool
pointsEqual Bool -> Bool -> Bool
|| Bool
equalInEveryAlternative
    where
      pointsEqual :: Bool
pointsEqual =
        Scope n -> Context n -> Value Pattern Sig n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Bool
isPointValue Scope n
scope Context n
ctx Value Pattern Sig n
l Bool -> Bool -> Bool
&& Scope n -> Context n -> Value Pattern Sig n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Bool
isPointValue Scope n
scope Context n
ctx Value Pattern Sig n
r
          Bool -> Bool -> Bool
&& TopeContext n -> Tope n -> Bool
forall (n :: S). TopeContext n -> Tope n -> Bool
entails (Context n -> TopeContext n
forall (n :: S). Context n -> TopeContext n
ctxTopes Context n
ctx) (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq (Scope n -> Context n -> Value Pattern Sig n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value Pattern Sig n
l) (Scope n -> Context n -> Value Pattern Sig n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value Pattern Sig n
r))
      equalInEveryAlternative :: Bool
equalInEveryAlternative = case TopeContext n -> [TopeContext n]
forall (n :: S). TopeContext n -> [TopeContext n]
alternatives (Context n -> TopeContext n
forall (n :: S). Context n -> TopeContext n
ctxTopes Context n
ctx) of
        []   -> Bool
False
        [TopeContext n]
alts -> (TopeContext n -> Bool) -> [TopeContext n] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\TopeContext n
topes -> Scope n
-> Context n -> Value Pattern Sig n -> Value Pattern Sig n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n -> Bool
conv Scope n
scope Context n
ctx { ctxTopes = topes } Value Pattern Sig n
l Value Pattern Sig n
r) [TopeContext n]
alts

  -- Subtyping in the context's variance; the cases of the old unification
  -- that are not a structural comparison of the children.
  {-# INLINE relateWith #-}
  relateWith :: forall m (n :: S) a a'.
(Monoid m, Distinct n) =>
Scope n
-> Context n
-> (a -> a' -> Context n -> Extend Context Pattern Sig n -> m)
-> (Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m)
-> (Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m)
-> Sig a (Value Pattern Sig n)
-> Sig a' (Value Pattern Sig n)
-> Maybe m
relateWith Scope n
scope Context n
ctx a -> a' -> Context n -> Extend Context Pattern Sig n -> m
scoped Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
hd Sig a (Value Pattern Sig n)
l Sig a' (Value Pattern Sig n)
r = case (Sig a (Value Pattern Sig n)
l, Sig a' (Value Pattern Sig n)
r) of
    -- the annotation of a neutral takes no part in the comparison
    (AnnF Value Pattern Sig n
nL Value Pattern Sig n
_, AnnF Value Pattern Sig n
nR Value Pattern Sig n
_) -> m -> Maybe m
forall a. a -> Maybe a
Just (Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
nL Value Pattern Sig n
nR Context n
ctx)
    (AnnF Value Pattern Sig n
nL Value Pattern Sig n
_, Sig a' (Value Pattern Sig n)
_) | Just Value Pattern Sig n
rv <- Sig a' (Value Pattern Sig n) -> Maybe (Value Pattern Sig n)
forall a (n :: S). Sig a (Value n) -> Maybe (Value n)
nodeValue Sig a' (Value Pattern Sig n)
r -> m -> Maybe m
forall a. a -> Maybe a
Just (Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
nL Value Pattern Sig n
rv Context n
ctx)
    (Sig a (Value Pattern Sig n)
_, AnnF Value Pattern Sig n
nR Value Pattern Sig n
_) | Just Value Pattern Sig n
lv <- Sig a (Value Pattern Sig n) -> Maybe (Value Pattern Sig n)
forall a (n :: S). Sig a (Value n) -> Maybe (Value n)
nodeValue Sig a (Value Pattern Sig n)
l -> m -> Maybe m
forall a. a -> Maybe a
Just (Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
lv Value Pattern Sig n
nR Context n
ctx)
    -- a hole stands for a term of the expected type
    (HoleF Maybe VarIdent
_, Sig a' (Value Pattern Sig n)
_) -> m -> Maybe m
forall a. a -> Maybe a
Just m
forall a. Monoid a => a
mempty
    (Sig a (Value Pattern Sig n)
_, HoleF Maybe VarIdent
_) -> m -> Maybe m
forall a. a -> Maybe a
Just m
forall a. Monoid a => a
mempty
    -- recBOT inhabits every type, unit is the only point of Unit
    (Sig a (Value Pattern Sig n)
RecBottomF, Sig a' (Value Pattern Sig n)
_) -> m -> Maybe m
forall a. a -> Maybe a
Just m
forall a. Monoid a => a
mempty
    (Sig a (Value Pattern Sig n)
_, Sig a' (Value Pattern Sig n)
RecBottomF) -> m -> Maybe m
forall a. a -> Maybe a
Just m
forall a. Monoid a => a
mempty
    (Sig a (Value Pattern Sig n)
UnitF, Sig a' (Value Pattern Sig n)
_) -> m -> Maybe m
forall a. a -> Maybe a
Just m
forall a. Monoid a => a
mempty
    (Sig a (Value Pattern Sig n)
_, Sig a' (Value Pattern Sig n)
UnitF) -> m -> Maybe m
forall a. a -> Maybe a
Just m
forall a. Monoid a => a
mempty
    -- the modal fragment is not compared yet
    (TypeModalF{}, Sig a' (Value Pattern Sig n)
_) -> Maybe m
forall a. Maybe a
Nothing
    (Sig a (Value Pattern Sig n)
_, TypeModalF{}) -> Maybe m
forall a. Maybe a
Nothing
    (ModAppF{}, Sig a' (Value Pattern Sig n)
_) -> Maybe m
forall a. Maybe a
Nothing
    (Sig a (Value Pattern Sig n)
_, ModAppF{}) -> Maybe m
forall a. Maybe a
Nothing
    (ModExtractF{}, Sig a' (Value Pattern Sig n)
_) -> Maybe m
forall a. Maybe a
Nothing
    (Sig a (Value Pattern Sig n)
_, ModExtractF{}) -> Maybe m
forall a. Maybe a
Nothing
    (LetModF{}, Sig a' (Value Pattern Sig n)
_) -> Maybe m
forall a. Maybe a
Nothing
    (Sig a (Value Pattern Sig n)
_, LetModF{}) -> Maybe m
forall a. Maybe a
Nothing

    -- two recORs are coherent branch by branch: under each overlap of their
    -- guards the branches agree; a recOR against another term agrees with
    -- it under each guard
    (RecOrF Maybe (Value Pattern Sig n)
_ [(Value Pattern Sig n, Value Pattern Sig n)]
rsL, RecOrF Maybe (Value Pattern Sig n)
_ [(Value Pattern Sig n, Value Pattern Sig n)]
rsR) -> m -> Maybe m
forall a. a -> Maybe a
Just (m -> Maybe m) -> m -> Maybe m
forall a b. (a -> b) -> a -> b
$ [m] -> m
forall a. Monoid a => [a] -> a
mconcat
      [ Value Pattern Sig n -> (Context n -> m) -> m
underOverlap (Value Pattern Sig n -> Value Pattern Sig n -> Value Pattern Sig n
forall (n :: S). Value n -> Value n -> Value n
topeAndValue Value Pattern Sig n
tL Value Pattern Sig n
tR) (Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
bL Value Pattern Sig n
bR)
      | (Value Pattern Sig n
tL, Value Pattern Sig n
bL) <- [(Value Pattern Sig n, Value Pattern Sig n)]
rsL, (Value Pattern Sig n
tR, Value Pattern Sig n
bR) <- [(Value Pattern Sig n, Value Pattern Sig n)]
rsR ]
    (RecOrF Maybe (Value Pattern Sig n)
_ [(Value Pattern Sig n, Value Pattern Sig n)]
rsL, Sig a' (Value Pattern Sig n)
_) | Just Value Pattern Sig n
rv <- Sig a' (Value Pattern Sig n) -> Maybe (Value Pattern Sig n)
forall a (n :: S). Sig a (Value n) -> Maybe (Value n)
nodeValue Sig a' (Value Pattern Sig n)
r -> m -> Maybe m
forall a. a -> Maybe a
Just (m -> Maybe m) -> m -> Maybe m
forall a b. (a -> b) -> a -> b
$ [m] -> m
forall a. Monoid a => [a] -> a
mconcat
      [ Value Pattern Sig n -> (Context n -> m) -> m
underOverlap Value Pattern Sig n
tL (Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
bL Value Pattern Sig n
rv) | (Value Pattern Sig n
tL, Value Pattern Sig n
bL) <- [(Value Pattern Sig n, Value Pattern Sig n)]
rsL ]
    (Sig a (Value Pattern Sig n)
_, RecOrF Maybe (Value Pattern Sig n)
_ [(Value Pattern Sig n, Value Pattern Sig n)]
rsR) | Just Value Pattern Sig n
lv <- Sig a (Value Pattern Sig n) -> Maybe (Value Pattern Sig n)
forall a (n :: S). Sig a (Value n) -> Maybe (Value n)
nodeValue Sig a (Value Pattern Sig n)
l -> m -> Maybe m
forall a. a -> Maybe a
Just (m -> Maybe m) -> m -> Maybe m
forall a b. (a -> b) -> a -> b
$ [m] -> m
forall a. Monoid a => [a] -> a
mconcat
      [ Value Pattern Sig n -> (Context n -> m) -> m
underOverlap Value Pattern Sig n
tR (Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
lv Value Pattern Sig n
bR) | (Value Pattern Sig n
tR, Value Pattern Sig n
bR) <- [(Value Pattern Sig n, Value Pattern Sig n)]
rsR ]

    -- two topes are compared by entailment in the context's variance: the
    -- subtype's tope entails the supertype's
    (Sig a (Value Pattern Sig n), Sig a' (Value Pattern Sig n))
_ | Sig a (Value Pattern Sig n) -> Bool
forall a b. Sig a b -> Bool
isTopeNode Sig a (Value Pattern Sig n)
l, Sig a' (Value Pattern Sig n) -> Bool
forall a b. Sig a b -> Bool
isTopeNode Sig a' (Value Pattern Sig n)
r, Just Value Pattern Sig n
lv <- Sig a (Value Pattern Sig n) -> Maybe (Value Pattern Sig n)
forall a (n :: S). Sig a (Value n) -> Maybe (Value n)
nodeValue Sig a (Value Pattern Sig n)
l, Just Value Pattern Sig n
rv <- Sig a' (Value Pattern Sig n) -> Maybe (Value Pattern Sig n)
forall a (n :: S). Sig a (Value n) -> Maybe (Value n)
nodeValue Sig a' (Value Pattern Sig n)
r ->
      let tl :: Tope n
tl = Scope n -> Context n -> Value Pattern Sig n -> Tope n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf Scope n
scope Context n
ctx Value Pattern Sig n
lv
          tr :: Tope n
tr = Scope n -> Context n -> Value Pattern Sig n -> Tope n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf Scope n
scope Context n
ctx Value Pattern Sig n
rv
          topes :: TopeContext n
topes = Context n -> TopeContext n
forall (n :: S). Context n -> TopeContext n
ctxTopes Context n
ctx
      in Bool -> Maybe m
forall {a}. Monoid a => Bool -> Maybe a
guarded (Bool -> Maybe m) -> Bool -> Maybe m
forall a b. (a -> b) -> a -> b
$ case Context n -> Variance
forall (n :: S). Context n -> Variance
ctxVariance Context n
ctx of
           Variance
Covariant     -> TopeContext n -> [Tope n] -> Tope n -> Bool
forall (n :: S). TopeContext n -> [Tope n] -> Tope n -> Bool
topesEntail TopeContext n
topes [Tope n
tr] Tope n
tl
           Variance
Contravariant -> TopeContext n -> [Tope n] -> Tope n -> Bool
forall (n :: S). TopeContext n -> [Tope n] -> Tope n -> Bool
topesEntail TopeContext n
topes [Tope n
tl] Tope n
tr
           Variance
Invariant     -> TopeContext n -> Tope n -> Tope n -> Bool
forall (n :: S). TopeContext n -> Tope n -> Tope n -> Bool
equivalent TopeContext n
topes Tope n
tl Tope n
tr

    -- the cube 2 is a subcube of 𝕀
    (Sig a (Value Pattern Sig n)
CubeIF, Sig a' (Value Pattern Sig n)
Cube2F) | Variance
Covariant <- Context n -> Variance
forall (n :: S). Context n -> Variance
ctxVariance Context n
ctx     -> m -> Maybe m
forall a. a -> Maybe a
Just m
forall a. Monoid a => a
mempty
    (Sig a (Value Pattern Sig n)
Cube2F, Sig a' (Value Pattern Sig n)
CubeIF) | Variance
Contravariant <- Context n -> Variance
forall (n :: S). Context n -> Variance
ctxVariance Context n
ctx -> m -> Maybe m
forall a. a -> Maybe a
Just m
forall a. Monoid a => a
mempty

    -- refl is refl: its endpoints are those of its type
    (ReflF Maybe (Value Pattern Sig n, Maybe (Value Pattern Sig n))
_, ReflF Maybe (Value Pattern Sig n, Maybe (Value Pattern Sig n))
_) -> m -> Maybe m
forall a. a -> Maybe a
Just m
forall a. Monoid a => a
mempty

    -- two lambdas: their domains, and the bodies where both shapes hold,
    -- both by conversion, since a lambda is a term
    (LambdaF Maybe (Param (Value Pattern Sig n))
mpL a
bodyL, LambdaF Maybe (Param (Value Pattern Sig n))
mpR a'
bodyR) -> m -> Maybe m
forall a. a -> Maybe a
Just (m -> Maybe m) -> m -> Maybe m
forall a b. (a -> b) -> a -> b
$
      (case (Maybe (Param (Value Pattern Sig n))
mpL, Maybe (Param (Value Pattern Sig n))
mpR) of
         (Just Param (Value Pattern Sig n)
pL, Just Param (Value Pattern Sig n)
pR) -> Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term (Param (Value Pattern Sig n) -> Value Pattern Sig n
forall term. Param term -> term
paramDomain Param (Value Pattern Sig n)
pL) (Param (Value Pattern Sig n) -> Value Pattern Sig n
forall term. Param term -> term
paramDomain Param (Value Pattern Sig n)
pR) (Variance -> Context n -> Context n
forall (n :: S). Variance -> Context n -> Context n
withVariance Variance
Invariant Context n
ctx)
         (Maybe (Param (Value Pattern Sig n)),
 Maybe (Param (Value Pattern Sig n)))
_ -> m
forall a. Monoid a => a
mempty)
      m -> m -> m
forall a. Semigroup a => a -> a -> a
<> a -> a' -> Context n -> Extend Context Pattern Sig n -> m
scoped a
bodyL a'
bodyR (Variance -> Context n -> Context n
forall (n :: S). Variance -> Context n -> Context n
withVariance Variance
Invariant Context n
ctx) (Maybe (Param (Value Pattern Sig n))
-> Maybe (Param (Value Pattern Sig n))
-> Extend Context Pattern Sig n
forall (n :: S).
Maybe (Param (Value n))
-> Maybe (Param (Value n)) -> Extend Context Pattern Sig n
lambdasBinder Maybe (Param (Value Pattern Sig n))
mpL Maybe (Param (Value Pattern Sig n))
mpR)

    -- a family type: the cube in the opposite variance, the shape in the
    -- context's variance (the family's subtyping is covariant in it, and
    -- the tope context plays no part)
    (TypeShapeF TModality
md Value Pattern Sig n
cubeL Maybe (Value Pattern Sig n)
mshapeL, TypeShapeF TModality
md' Value Pattern Sig n
cubeR Maybe (Value Pattern Sig n)
mshapeR)
      | TModality
md TModality -> TModality -> Bool
forall a. Eq a => a -> a -> Bool
== TModality
md' ->
          let domain :: m
domain = Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
cubeL Value Pattern Sig n
cubeR (Variance -> Context n -> Context n
forall (n :: S). Variance -> Context n -> Context n
withVariance (Variance -> Variance
opposite (Context n -> Variance
forall (n :: S). Context n -> Variance
ctxVariance Context n
ctx)) Context n
ctx)
          in case (Maybe (Value Pattern Sig n)
mshapeL, Maybe (Value Pattern Sig n)
mshapeR) of
               (Maybe (Value Pattern Sig n)
Nothing, Maybe (Value Pattern Sig n)
Nothing) -> m -> Maybe m
forall a. a -> Maybe a
Just m
domain
               (Just Value Pattern Sig n
sL, Just Value Pattern Sig n
sR) -> m -> Maybe m
forall a. a -> Maybe a
Just (m
domain m -> m -> m
forall a. Semigroup a => a -> a -> a
<> Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
sL Value Pattern Sig n
sR Context n
ctx)
               (Maybe (Value Pattern Sig n), Maybe (Value Pattern Sig n))
_ -> Maybe m
forall a. Maybe a
Nothing

    -- Π: the domain in the opposite variance, the shapes as shapes, the
    -- codomain under the binder
    (TypeFunF TModality
md Value Pattern Sig n
domL Maybe (Value Pattern Sig n)
mshapeL a
retL, TypeFunF TModality
md' Value Pattern Sig n
domR Maybe (Value Pattern Sig n)
mshapeR a'
retR)
      | TModality
md TModality -> TModality -> Bool
forall a. Eq a => a -> a -> Bool
== TModality
md' ->
          let ctx' :: Context n
ctx' = Variance -> Context n -> Context n
forall (n :: S). Variance -> Context n -> Context n
withVariance (Variance -> Variance
opposite (Context n -> Variance
forall (n :: S). Context n -> Variance
ctxVariance Context n
ctx)) Context n
ctx
              domain :: m
domain = Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
domL Value Pattern Sig n
domR Context n
ctx'
              shapes :: Maybe m
shapes = case (Maybe (Value Pattern Sig n)
mshapeL, Maybe (Value Pattern Sig n)
mshapeR) of
                (Maybe (Value Pattern Sig n)
Nothing, Maybe (Value Pattern Sig n)
Nothing) -> m -> Maybe m
forall a. a -> Maybe a
Just m
forall a. Monoid a => a
mempty
                (Just Value Pattern Sig n
sL, Just Value Pattern Sig n
sR) -> m -> Maybe m
forall a. a -> Maybe a
Just (Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
sL Value Pattern Sig n
sR Context n
ctx')
                (Maybe (Value Pattern Sig n), Maybe (Value Pattern Sig n))
_ -> Maybe m
forall a. Maybe a
Nothing
          in (m -> m) -> Maybe m -> Maybe m
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\m
s -> m
domain m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
s m -> m -> m
forall a. Semigroup a => a -> a -> a
<> a -> a' -> Context n -> Extend Context Pattern Sig n -> m
scoped a
retL a'
retR Context n
ctx (TModality
-> Value Pattern Sig n
-> Maybe (Value Pattern Sig n)
-> Maybe (Value Pattern Sig n)
-> Extend Context Pattern Sig n
forall (n :: S).
TModality
-> Value n
-> Maybe (Value n)
-> Maybe (Value n)
-> Extend Context Pattern Sig n
pisBinder TModality
md Value Pattern Sig n
domR Maybe (Value Pattern Sig n)
mshapeL Maybe (Value Pattern Sig n)
mshapeR)) Maybe m
shapes
    (TypeSigmaF TModality
md Value Pattern Sig n
aL a
bL, TypeSigmaF TModality
md' Value Pattern Sig n
aR a'
bR)
      | TModality
md TModality -> TModality -> Bool
forall a. Eq a => a -> a -> Bool
== TModality
md' -> m -> Maybe m
forall a. a -> Maybe a
Just (Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
aL Value Pattern Sig n
aR Context n
ctx m -> m -> m
forall a. Semigroup a => a -> a -> a
<> a -> a' -> Context n -> Extend Context Pattern Sig n -> m
scoped a
bL a'
bR Context n
ctx (TModality -> Value Pattern Sig n -> Extend Context Pattern Sig n
forall (n :: S).
TModality -> Value n -> Extend Context Pattern Sig n
sigmaBinder TModality
md Value Pattern Sig n
aR))
    -- identity types: the carrier and the endpoints are terms, which agree
    -- by conversion; subtyping between endpoints that are types would let
    -- transport along a path identify any two elements
    (TypeIdF Value Pattern Sig n
xL Maybe (Value Pattern Sig n)
tAL Value Pattern Sig n
yL, TypeIdF Value Pattern Sig n
xR Maybe (Value Pattern Sig n)
tAR Value Pattern Sig n
yR) -> m -> Maybe m
forall a. a -> Maybe a
Just (m -> Maybe m) -> m -> Maybe m
forall a b. (a -> b) -> a -> b
$
      (case (Maybe (Value Pattern Sig n)
tAL, Maybe (Value Pattern Sig n)
tAR) of
         (Just Value Pattern Sig n
tL, Just Value Pattern Sig n
tR) -> Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
tL Value Pattern Sig n
tR (Variance -> Context n -> Context n
forall (n :: S). Variance -> Context n -> Context n
withVariance Variance
Invariant Context n
ctx)
         (Maybe (Value Pattern Sig n), Maybe (Value Pattern Sig n))
_ -> m
forall a. Monoid a => a
mempty)
        m -> m -> m
forall a. Semigroup a => a -> a -> a
<> Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
xL Value Pattern Sig n
xR (Variance -> Context n -> Context n
forall (n :: S). Variance -> Context n -> Context n
withVariance Variance
Invariant Context n
ctx) m -> m -> m
forall a. Semigroup a => a -> a -> a
<> Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
yL Value Pattern Sig n
yR (Variance -> Context n -> Context n
forall (n :: S). Variance -> Context n -> Context n
withVariance Variance
Invariant Context n
ctx)
    (AppF Value Pattern Sig n
fL Value Pattern Sig n
xL, AppF Value Pattern Sig n
fR Value Pattern Sig n
xR) -> m -> Maybe m
forall a. a -> Maybe a
Just (Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
hd Value Pattern Sig n
fL Value Pattern Sig n
fR Context n
ctx m -> m -> m
forall a. Semigroup a => a -> a -> a
<> Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
xL Value Pattern Sig n
xR (Variance -> Context n -> Context n
forall (n :: S). Variance -> Context n -> Context n
withVariance Variance
Invariant Context n
ctx))

    -- extension types: the underlying types, and the faces of the supertype
    -- covered by the faces of the subtype, agreeing where they overlap
    (TypeRestrictedF Value Pattern Sig n
tyL [(Value Pattern Sig n, Value Pattern Sig n)]
rsL, TypeRestrictedF Value Pattern Sig n
tyR [(Value Pattern Sig n, Value Pattern Sig n)]
rsR)
      | [(Value Pattern Sig n, Value Pattern Sig n)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Value Pattern Sig n, Value Pattern Sig n)]
rsL Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [(Value Pattern Sig n, Value Pattern Sig n)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Value Pattern Sig n, Value Pattern Sig n)]
rsR
      , [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and (((Value Pattern Sig n, Value Pattern Sig n)
 -> (Value Pattern Sig n, Value Pattern Sig n) -> Bool)
-> [(Value Pattern Sig n, Value Pattern Sig n)]
-> [(Value Pattern Sig n, Value Pattern Sig n)]
-> [Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\(Value Pattern Sig n
tL, Value Pattern Sig n
_) (Value Pattern Sig n
tR, Value Pattern Sig n
_) -> Scope n -> Context n -> Value Pattern Sig n -> Tope n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf Scope n
scope Context n
ctx Value Pattern Sig n
tL Tope n -> Tope n -> Bool
forall a. Eq a => a -> a -> Bool
== Scope n -> Context n -> Value Pattern Sig n -> Tope n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf Scope n
scope Context n
ctx Value Pattern Sig n
tR) [(Value Pattern Sig n, Value Pattern Sig n)]
rsL [(Value Pattern Sig n, Value Pattern Sig n)]
rsR) ->
          -- the terms of a face need to agree only under the face
          m -> Maybe m
forall a. a -> Maybe a
Just (Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
tyL Value Pattern Sig n
tyR Context n
ctx m -> m -> m
forall a. Semigroup a => a -> a -> a
<> [m] -> m
forall a. Monoid a => [a] -> a
mconcat (((Value Pattern Sig n, Value Pattern Sig n)
 -> (Value Pattern Sig n, Value Pattern Sig n) -> m)
-> [(Value Pattern Sig n, Value Pattern Sig n)]
-> [(Value Pattern Sig n, Value Pattern Sig n)]
-> [m]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\(Value Pattern Sig n
tL, Value Pattern Sig n
bL) (Value Pattern Sig n
_, Value Pattern Sig n
bR) -> Value Pattern Sig n -> (Context n -> m) -> m
underOverlap Value Pattern Sig n
tL (Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
bL Value Pattern Sig n
bR)) [(Value Pattern Sig n, Value Pattern Sig n)]
rsL [(Value Pattern Sig n, Value Pattern Sig n)]
rsR))
    (TypeRestrictedF Value Pattern Sig n
tyL [(Value Pattern Sig n, Value Pattern Sig n)]
rsL, TypeRestrictedF Value Pattern Sig n
tyR [(Value Pattern Sig n, Value Pattern Sig n)]
rsR) -> (m -> m) -> Maybe m -> Maybe m
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
tyL Value Pattern Sig n
tyR Context n
ctx m -> m -> m
forall a. Semigroup a => a -> a -> a
<>) (Maybe m -> Maybe m) -> Maybe m -> Maybe m
forall a b. (a -> b) -> a -> b
$
      case Context n -> Variance
forall (n :: S). Context n -> Variance
ctxVariance Context n
ctx of
        Variance
Covariant     -> [(Value Pattern Sig n, Value Pattern Sig n)]
-> [(Value Pattern Sig n, Value Pattern Sig n)] -> Maybe m
facesCovered [(Value Pattern Sig n, Value Pattern Sig n)]
rsL [(Value Pattern Sig n, Value Pattern Sig n)]
rsR
        Variance
Contravariant -> [(Value Pattern Sig n, Value Pattern Sig n)]
-> [(Value Pattern Sig n, Value Pattern Sig n)] -> Maybe m
facesCovered [(Value Pattern Sig n, Value Pattern Sig n)]
rsR [(Value Pattern Sig n, Value Pattern Sig n)]
rsL
        Variance
Invariant     -> m -> m -> m
forall a. Semigroup a => a -> a -> a
(<>) (m -> m -> m) -> Maybe m -> Maybe (m -> m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Value Pattern Sig n, Value Pattern Sig n)]
-> [(Value Pattern Sig n, Value Pattern Sig n)] -> Maybe m
facesCovered [(Value Pattern Sig n, Value Pattern Sig n)]
rsL [(Value Pattern Sig n, Value Pattern Sig n)]
rsR Maybe (m -> m) -> Maybe m -> Maybe m
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [(Value Pattern Sig n, Value Pattern Sig n)]
-> [(Value Pattern Sig n, Value Pattern Sig n)] -> Maybe m
facesCovered [(Value Pattern Sig n, Value Pattern Sig n)]
rsR [(Value Pattern Sig n, Value Pattern Sig n)]
rsL
    -- a restricted type against an unrestricted one is left to the
    -- checker's subtyping, which has the term the faces are checked on

    (Sig a (Value Pattern Sig n), Sig a' (Value Pattern Sig n))
_ -> Scope n
-> Context n
-> (a -> a' -> Context n -> Extend Context Pattern Sig n -> m)
-> (Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m)
-> (Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m)
-> Sig a (Value Pattern Sig n)
-> Sig a' (Value Pattern Sig n)
-> Maybe m
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       m (n :: S) a a'.
(Eval ctx binder sig, ZipMatchFoldK sig, Monoid m, Distinct n) =>
Scope n
-> ctx n
-> (a -> a' -> ctx n -> Extend ctx binder sig n -> m)
-> (Value binder sig n -> Value binder sig n -> ctx n -> m)
-> (Value binder sig n -> Value binder sig n -> ctx n -> m)
-> sig a (Value binder sig n)
-> sig a' (Value binder sig n)
-> Maybe m
NbE.relateNodes Scope n
scope (Variance -> Context n -> Context n
forall (n :: S). Variance -> Context n -> Context n
withVariance Variance
Invariant Context n
ctx) a -> a' -> Context n -> Extend Context Pattern Sig n -> m
scoped Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
hd Sig a (Value Pattern Sig n)
l Sig a' (Value Pattern Sig n)
r
    where
      guarded :: Bool -> Maybe a
guarded Bool
holds = if Bool
holds then a -> Maybe a
forall a. a -> Maybe a
Just a
forall a. Monoid a => a
mempty else Maybe a
forall a. Maybe a
Nothing

      -- A comparison under a tope, which holds trivially when the tope
      -- makes the context contradictory.
      underOverlap :: Value Pattern Sig n -> (Context n -> m) -> m
underOverlap Value Pattern Sig n
tope Context n -> m
compare' =
        let ctx' :: Context n
ctx' = Scope n -> Context n -> Value Pattern Sig n -> Context n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Context n
assumeValue Scope n
scope Context n
ctx Value Pattern Sig n
tope
        in if TopeContext n -> Bool
forall (n :: S). TopeContext n -> Bool
entailsBottom (Context n -> TopeContext n
forall (n :: S). Context n -> TopeContext n
ctxTopes Context n
ctx') then m
forall a. Monoid a => a
mempty else Context n -> m
compare' Context n
ctx'

      -- The faces of the supertype are covered by the faces of the subtype:
      -- under each face of the supertype, the subtype's topes cover the
      -- context, and the boundary terms agree under each overlap. The sides
      -- of the comparison are kept whichever side is the supertype.
      facesCovered :: [(Value Pattern Sig n, Value Pattern Sig n)]
-> [(Value Pattern Sig n, Value Pattern Sig n)] -> Maybe m
facesCovered [(Value Pattern Sig n, Value Pattern Sig n)]
superRs [(Value Pattern Sig n, Value Pattern Sig n)]
subRs = ([m] -> m) -> Maybe [m] -> Maybe m
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [m] -> m
forall a. Monoid a => [a] -> a
mconcat (Maybe [m] -> Maybe m) -> Maybe [m] -> Maybe m
forall a b. (a -> b) -> a -> b
$ [Maybe m] -> Maybe [m]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence
        [ let face :: Context n
face = Scope n -> Context n -> Value Pattern Sig n -> Context n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Context n
assumeValue Scope n
scope Context n
ctx Value Pattern Sig n
tope
          in if TopeContext n -> Bool
forall (n :: S). TopeContext n -> Bool
entailsBottom (Context n -> TopeContext n
forall (n :: S). Context n -> TopeContext n
ctxTopes Context n
face) then m -> Maybe m
forall a. a -> Maybe a
Just m
forall a. Monoid a => a
mempty
             else if TopeContext n -> [Tope n] -> Bool
forall (n :: S). TopeContext n -> [Tope n] -> Bool
entailsUnion (Context n -> TopeContext n
forall (n :: S). Context n -> TopeContext n
ctxTopes Context n
face) (((Value Pattern Sig n, Value Pattern Sig n) -> Tope n)
-> [(Value Pattern Sig n, Value Pattern Sig n)] -> [Tope n]
forall a b. (a -> b) -> [a] -> [b]
map (Scope n -> Context n -> Value Pattern Sig n -> Tope n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf Scope n
scope Context n
face (Value Pattern Sig n -> Tope n)
-> ((Value Pattern Sig n, Value Pattern Sig n)
    -> Value Pattern Sig n)
-> (Value Pattern Sig n, Value Pattern Sig n)
-> Tope n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Value Pattern Sig n, Value Pattern Sig n) -> Value Pattern Sig n
forall a b. (a, b) -> a
fst) [(Value Pattern Sig n, Value Pattern Sig n)]
subRs)
               then m -> Maybe m
forall a. a -> Maybe a
Just (m -> Maybe m) -> m -> Maybe m
forall a b. (a -> b) -> a -> b
$ [m] -> m
forall a. Monoid a => [a] -> a
mconcat
                 [ let overlap :: Context n
overlap = Scope n -> Context n -> Value Pattern Sig n -> Context n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Context n
assumeValue Scope n
scope Context n
face Value Pattern Sig n
tope'
                   in if TopeContext n -> Bool
forall (n :: S). TopeContext n -> Bool
entailsBottom (Context n -> TopeContext n
forall (n :: S). Context n -> TopeContext n
ctxTopes Context n
overlap) then m
forall a. Monoid a => a
mempty else Value Pattern Sig n -> Context n -> m
sided Value Pattern Sig n
term' Context n
overlap
                 | (Value Pattern Sig n
tope', Value Pattern Sig n
term') <- [(Value Pattern Sig n, Value Pattern Sig n)]
subRs ]
               else Maybe m
forall a. Maybe a
Nothing
        | (Value Pattern Sig n
tope, Value Pattern Sig n
term0) <- [(Value Pattern Sig n, Value Pattern Sig n)]
superRs
        , let sided :: Value Pattern Sig n -> Context n -> m
sided Value Pattern Sig n
term' Context n
ctx' = case Context n -> Variance
forall (n :: S). Context n -> Variance
ctxVariance Context n
ctx of
                Variance
Contravariant -> Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
term' Value Pattern Sig n
term0 Context n
ctx'
                Variance
_             -> Value Pattern Sig n -> Value Pattern Sig n -> Context n -> m
term Value Pattern Sig n
term0 Value Pattern Sig n
term' Context n
ctx'
        ]

  -- A neutral reduces by a restriction of its type whose tope holds, and
  -- a point of the unit type is unit. A variable's type is its entry's; a
  -- neutral built by evaluation carries its type ('AnnF'), and refinement
  -- reads only the top of it, as the old checker's weak head normal form
  -- read the type of a term.
  {-# INLINE refineVar #-}
  refineVar :: forall (n :: S).
(Eval Context Pattern Sig, Distinct n) =>
Scope n -> Context n -> Name n -> Value Pattern Sig n
refineVar Scope n
scope Context n
ctx Name n
x = Scope n -> Context n -> Value n -> Value n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
refineNeutral Scope n
scope Context n
ctx (Name n -> Value n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> Value binder sig n
NbE.vvar Name n
x)

  {-# INLINE refineSig #-}
  refineSig :: forall (n :: S) a.
(Eval Context Pattern Sig, Distinct n) =>
Scope n
-> Context n
-> Value Pattern Sig n
-> Sig a (Value Pattern Sig n)
-> Value Pattern Sig n
refineSig Scope n
scope Context n
ctx Value Pattern Sig n
value Sig a (Value Pattern Sig n)
node = case Sig a (Value Pattern Sig n)
node of
    AppF{}    -> Scope n -> Context n -> Value Pattern Sig n -> Value Pattern Sig n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
refineNeutral Scope n
scope Context n
ctx Value Pattern Sig n
value
    FirstF{}  -> Scope n -> Context n -> Value Pattern Sig n -> Value Pattern Sig n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
refineNeutral Scope n
scope Context n
ctx Value Pattern Sig n
value
    SecondF{} -> Scope n -> Context n -> Value Pattern Sig n -> Value Pattern Sig n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
refineNeutral Scope n
scope Context n
ctx Value Pattern Sig n
value
    IdJF{}    -> Scope n -> Context n -> Value Pattern Sig n -> Value Pattern Sig n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
refineNeutral Scope n
scope Context n
ctx Value Pattern Sig n
value
    AnnF{}    -> Scope n -> Context n -> Value Pattern Sig n -> Value Pattern Sig n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
refineNeutral Scope n
scope Context n
ctx Value Pattern Sig n
value
    -- a recOR evaluated before its guard was assumed takes the branch the
    -- context now entails, as the old checker decided it in its weak head
    -- normal form (a type cached in an annotation may hold one)
    RecOrF Maybe (Value Pattern Sig n)
_ [(Value Pattern Sig n, Value Pattern Sig n)]
rs | Value Pattern Sig n
b : [Value Pattern Sig n]
_ <- [ Value Pattern Sig n
b | (Value Pattern Sig n
guard, Value Pattern Sig n
b) <- [(Value Pattern Sig n, Value Pattern Sig n)]
rs, Scope n -> Context n -> Value Pattern Sig n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Bool
entailsValue Scope n
scope Context n
ctx Value Pattern Sig n
guard ] -> Scope n -> Context n -> Value Pattern Sig n -> Value Pattern Sig n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
(Eval ctx binder sig, Distinct n) =>
Scope n -> ctx n -> Value binder sig n -> Value binder sig n
NbE.refine Scope n
scope Context n
ctx Value Pattern Sig n
b
    Sig a (Value Pattern Sig n)
_         -> Value Pattern Sig n
value

  -- A pattern stands for its names paired; bound to a value, its leaves
  -- get the projections.
  {-# INLINE binderValue #-}
  binderValue :: forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Scope l -> Context l -> Pattern n l -> Maybe (Value Pattern Sig l)
binderValue Scope l
_ Context l
_ Pattern n l
pat = Value Pattern Sig l -> Maybe (Value Pattern Sig l)
forall a. a -> Maybe a
Just (Pattern n l -> Value Pattern Sig l
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Pattern n l -> Value l
patternValue Pattern n l
pat)

  {-# INLINE bindValue #-}
  bindValue :: forall (o :: S) (i :: S) (l :: S).
Distinct o =>
Scope o
-> Context o
-> Substitution (Value Pattern Sig) i o
-> Pattern i l
-> Value Pattern Sig o
-> Maybe (Substitution (Value Pattern Sig) l o)
bindValue Scope o
_ Context o
_ Substitution (Value Pattern Sig) i o
env Pattern i l
pat Value Pattern Sig o
v = Substitution (Value Pattern Sig) l o
-> Maybe (Substitution (Value Pattern Sig) l o)
forall a. a -> Maybe a
Just (Substitution (Value Pattern Sig) i o
-> Pattern i l
-> Value Pattern Sig o
-> Substitution (Value Pattern Sig) l o
forall (i :: S) (n :: S) (l :: S).
Env i n -> Pattern i l -> Value n -> Env l n
bindPattern Substitution (Value Pattern Sig) i o
env Pattern i l
pat Value Pattern Sig o
v)

-- | Is a value a point of a cube: an endpoint, an operation on points, or
-- a neutral whose type is a cube? A pair of points is not asked about:
-- conversion compares its components, which are.
isPointValue :: Distinct n => Scope n -> Context n -> Value n -> Bool
isPointValue :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Bool
isPointValue Scope n
scope Context n
ctx Value n
v = case Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value n
v of
  NbE.VNode Sig Void (Value n)
node -> case Sig Void (Value n)
node of
    Sig Void (Value n)
CubeUnitStarF -> Bool
True
    Sig Void (Value n)
Cube2_0F      -> Bool
True
    Sig Void (Value n)
Cube2_1F      -> Bool
True
    Sig Void (Value n)
CubeI_0F      -> Bool
True
    Sig Void (Value n)
CubeI_1F      -> Bool
True
    CubeSupF{}    -> Bool
True
    CubeInfF{}    -> Bool
True
    CubeFlipF{}   -> Bool
True
    CubeUnflipF{} -> Bool
True
    PairF{}       -> Bool
False
    AnnF Value n
_ Value n
ty | Just Value n
t <- Value n -> Maybe (Value n)
forall (n :: S). Value n -> Maybe (Value n)
annotationType Value n
ty -> 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
t
    Sig Void (Value n)
_             -> Bool
neutralOfCube
  NbE.VVar Name n
_ -> Bool
neutralOfCube
  Whnf Pattern Sig n
_ -> Bool
False
  where
    neutralOfCube :: Bool
neutralOfCube = Bool -> (Value n -> Bool) -> Maybe (Value n) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (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) (Scope n -> Context n -> Value n -> Maybe (Value n)
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Maybe (Value n)
neutralType Scope n
scope Context n
ctx Value n
v)

-- | Is a tope value ⊥?
isBottomTope :: Value n -> Bool
isBottomTope :: forall (n :: S). Value n -> Bool
isBottomTope Value n
v = case Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value n
v of
  Whnf Pattern Sig n
VTopeBottom -> Bool
True
  Whnf Pattern Sig n
_                     -> Bool
False

-- | A node without scoped positions, as a value.
nodeValue :: Sig a (Value n) -> Maybe (Value n)
nodeValue :: forall a (n :: S). Sig a (Value n) -> Maybe (Value n)
nodeValue Sig a (Value n)
node
  | Any -> Bool
getAny ((a -> Any) -> (Value n -> Any) -> Sig a (Value n) -> Any
forall m a b. Monoid m => (a -> m) -> (b -> m) -> Sig a b -> m
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap (Any -> a -> Any
forall a b. a -> b -> a
const (Bool -> Any
Any Bool
True)) (Any -> Value n -> Any
forall a b. a -> b -> a
const Any
forall a. Monoid a => a
mempty) Sig a (Value n)
node) = Maybe (Value n)
forall a. Maybe a
Nothing
  | Bool
otherwise = Value n -> Maybe (Value n)
forall a. a -> Maybe a
Just (Sig Void (Value n) -> Value n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode ((a -> Void)
-> (Value n -> Value n) -> Sig a (Value n) -> Sig Void (Value n)
forall a b c d. (a -> b) -> (c -> d) -> Sig a c -> Sig b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (Void -> a -> Void
forall a b. a -> b -> a
const Void
forall {a}. a
noScoped) Value n -> Value n
forall a. a -> a
id Sig a (Value n)
node))
  where
    noScoped :: a
noScoped = String -> a
forall a. String -> a
panicImpossible String
"a scoped position of a node without scoped positions"

-- | Refine a neutral by its type: a restriction whose tope the context
-- entails gives its term; the unit type gives unit. The type of a neutral
-- carries the restrictions of the types of the prefixes of its spine (see
-- 'neutralType'): in @g t x@ with @g : (t : ψ) → (X → A) [ϕ t ↦ f t]@, it
-- is @g t@ that is @f t@ under @ϕ t@, so @g t x@ is @f t x@. The result,
-- like the term of a restriction, is refined in turn, since it may be a
-- neutral restricted further (a component of a pair restricted under a
-- smaller tope).
refineNeutral :: Distinct n => Scope n -> Context n -> Value n -> Value n
refineNeutral :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
refineNeutral Scope n
scope Context n
ctx Value n
value = Value n -> Maybe (Value n) -> Value n
forall a. a -> Maybe a -> a
fromMaybe Value n
value (Scope n -> Context n -> Value n -> Maybe (Value n)
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Maybe (Value n)
refinedNeutral Scope n
scope Context n
ctx Value n
value)

-- | A neutral refined by its type, or 'Nothing' if the type has no
-- restriction the context entails and is not unit.
refinedNeutral :: Distinct n => Scope n -> Context n -> Value n -> Maybe (Value n)
refinedNeutral :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Maybe (Value n)
refinedNeutral Scope n
scope Context n
ctx Value n
value = do
  ty <- Scope n -> Context n -> Value n -> Maybe (Value n)
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Maybe (Value n)
neutralType Scope n
scope Context n
ctx Value n
value
  case NbE.force (stripRestrictions ty) of
    Whnf Pattern Sig n
VTypeUnit -> Value n -> Maybe (Value n)
forall a. a -> Maybe a
Just Value n
forall (n :: S). Value n
vunit
    Whnf Pattern Sig n
_ -> Scope n -> Context n -> Value n -> Value n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
refineNeutral Scope n
scope Context n
ctx (Value n -> Value n) -> Maybe (Value n) -> Maybe (Value n)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
           [Value n] -> Maybe (Value n)
forall a. [a] -> Maybe a
listToMaybe [ Value n
t | (Value n
tope, Value n
t) <- Value n -> [(Value n, Value n)]
forall (n :: S). Value n -> [(Value n, Value n)]
restrictionsOf Value n
ty, Scope n -> Context n -> Value n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Bool
entailsValue Scope n
scope Context n
ctx Value n
tope ]

-- * Evaluation and readback

-- | A core term as a value, in the scope it was checked in.
evalCore :: Distinct n => Scope n -> Context n -> Core n -> Value n
{-# INLINE evalCore #-}
evalCore :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Core n -> Value n
evalCore Scope n
scope Context n
ctx = Scope n
-> Context n
-> Substitution (Value Pattern Sig) n n
-> AST Pattern Sig n
-> Value Pattern Sig n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (o :: S) (i :: S).
(Eval ctx binder sig, Distinct o, Distinct i) =>
Scope o
-> ctx o
-> Substitution (Value binder sig) i o
-> AST binder sig i
-> Value binder sig o
NbE.eval Scope n
scope Context n
ctx Substitution (Value Pattern Sig) n n
forall (e :: S -> *) (i :: S). InjectName e => Substitution e i i
Foil.identitySubst

-- | A scoped term at a value for its pattern.
evalScope :: (Distinct n, Distinct i) => Scope n -> Context n -> Env i n -> ScopedCore i -> Value n -> Value n
{-# INLINE evalScope #-}
evalScope :: forall (n :: S) (i :: S).
(Distinct n, Distinct i) =>
Scope n
-> Context n -> Env i n -> ScopedCore i -> Value n -> Value n
evalScope Scope n
scope Context n
ctx Env i n
env (ScopedAST Pattern i l
pat AST Pattern Sig l
body) Value n
arg = case Pattern i l -> DistinctEvidence l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct Pattern i l
pat of
  DistinctEvidence l
Foil.Distinct -> Scope n
-> Context n
-> Substitution (Value Pattern Sig) l n
-> AST Pattern Sig l
-> Value n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (o :: S) (i :: S).
(Eval ctx binder sig, Distinct o, Distinct i) =>
Scope o
-> ctx o
-> Substitution (Value binder sig) i o
-> AST binder sig i
-> Value binder sig o
NbE.eval Scope n
scope Context n
ctx (Env i n
-> Pattern i l -> Value n -> Substitution (Value Pattern Sig) l n
forall (i :: S) (n :: S) (l :: S).
Env i n -> Pattern i l -> Value n -> Env l n
bindPattern Env i n
env Pattern i l
pat Value n
arg) AST Pattern Sig l
body

quoteIn :: Distinct n => Scope n -> Context n -> Value n -> Core n
{-# INLINE quoteIn #-}
quoteIn :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Core n
quoteIn Scope n
scope Context n
ctx = Core n -> Core n
forall (n :: S). Core n -> Core n
stripAnnotations (Core n -> Core n) -> (Value n -> Core n) -> Value n -> Core n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scope n -> Context n -> Value n -> Core n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
(Eval ctx binder sig, Distinct n, CoSinkable binder) =>
Scope n -> ctx n -> Value binder sig n -> AST binder sig n
NbE.quote Scope n
scope Context n
ctx

-- | Read a value back with defined names folded, as the checker's own terms
-- spell them.
quoteFolded :: Distinct n => Scope n -> Context n -> Value n -> Core n
quoteFolded :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Core n
quoteFolded Scope n
scope Context n
ctx = Core n -> Core n
forall (n :: S). Core n -> Core n
stripAnnotations (Core n -> Core n) -> (Value n -> Core n) -> Value n -> Core n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scope n -> Context n -> Value n -> Core n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
(Eval ctx binder sig, Distinct n, CoSinkable binder) =>
Scope n -> ctx n -> Value binder sig n -> AST binder sig n
NbE.quote Scope n
scope Context n
ctx { ctxUnfolding = Folded }

-- | Read back the weak head normal form of a value: the head unfolded and
-- reduced, its arguments read back with defined names folded, as the old
-- checker's @#compute-whnf@ printed them.
quoteWhnfFolded :: Distinct n => Scope n -> Context n -> Value n -> Core n
quoteWhnfFolded :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Core n
quoteWhnfFolded Scope n
scope Context n
ctx =
  Core n -> Core n
forall (n :: S). Core n -> Core n
stripAnnotations (Core n -> Core n) -> (Value n -> Core n) -> Value n -> Core n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scope n -> Context n -> Value n -> Core n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
(Eval ctx binder sig, Distinct n, CoSinkable binder,
 SinkableK binder) =>
Scope n -> ctx n -> Value binder sig n -> AST binder sig n
NbE.quoteWhnf Scope n
scope Context n
ctx { ctxUnfolding = Folded } (Value n -> Core n) -> (Value n -> Value n) -> Value n -> Core n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Whnf Pattern Sig n -> Value n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Whnf binder sig n -> Value binder sig n
NbE.fromWhnf (Whnf Pattern Sig n -> Value n)
-> (Value n -> Whnf Pattern Sig n) -> Value n -> Value n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force

-- | Evaluate a core term and read back its weak head normal form.
quoteWhnfCore :: Distinct n => Scope n -> Context n -> Core n -> Core n
quoteWhnfCore :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Core n -> Core n
quoteWhnfCore Scope n
scope Context n
ctx = Scope n -> Context n -> Value n -> Core n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Core n
quoteWhnfFolded Scope n
scope Context n
ctx (Value n -> Core n) -> (Core n -> Value n) -> Core n -> Core n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scope n -> Context n -> Core n -> Value n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Core n -> Value n
evalCore Scope n
scope Context n
ctx

nfCore :: Distinct n => Scope n -> Context n -> Core n -> Core n
{-# INLINE nfCore #-}
nfCore :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Core n -> Core n
nfCore Scope n
scope Context n
ctx = Core n -> Core n
forall (n :: S). Core n -> Core n
stripAnnotations (Core n -> Core n) -> (Core n -> Core n) -> Core n -> Core n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scope n -> Context n -> Core n -> Core n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
(Eval ctx binder sig, Distinct n, CoSinkable binder) =>
Scope n -> ctx n -> AST binder sig n -> AST binder sig n
NbE.nf Scope n
scope Context n
ctx

force :: Value n -> Whnf n
{-# INLINE force #-}
force :: forall (n :: S). Value n -> Whnf n
force = Value Pattern Sig n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force

-- | Compare an expected value with an actual one in the context's variance.
conv :: Distinct n => Scope n -> Context n -> Value n -> Value n -> Bool
{-# INLINE conv #-}
conv :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n -> Bool
conv = Scope n
-> Context n -> Value Pattern Sig n -> Value Pattern Sig n -> Bool
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
(Eval ctx binder sig, Distinct n, UnifiablePattern binder) =>
Scope n
-> ctx n -> Value binder sig n -> Value binder sig n -> Bool
Conv.conv

convertible :: Distinct n => Scope n -> Context n -> Core n -> Core n -> Bool
{-# INLINE convertible #-}
convertible :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Core n -> Core n -> Bool
convertible = Scope n
-> Context n -> AST Pattern Sig n -> AST Pattern Sig n -> Bool
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
(Eval ctx binder sig, Distinct n, UnifiablePattern binder) =>
Scope n -> ctx n -> AST binder sig n -> AST binder sig n -> Bool
Conv.convertible

-- * Values the rules build

vnode :: Sig Void (Value n) -> Value n
{-# INLINE vnode #-}
vnode :: forall (n :: S). Sig Void (Value n) -> Value n
vnode = Sig Void (Value Pattern Sig n) -> Value Pattern Sig n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode

universe, cubeUniverse, topeUniverse, vcube2, vcubeI, vtopeBottom, vunit, vtypeUnit, vrecBottom :: Value n
universe :: forall (n :: S). Value n
universe = Sig Void (Value Pattern Sig n) -> Value Pattern Sig n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig n)
forall scope term. Sig scope term
UniverseF
cubeUniverse :: forall (n :: S). Value n
cubeUniverse = Sig Void (Value Pattern Sig n) -> Value Pattern Sig n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig n)
forall scope term. Sig scope term
UniverseCubeF
topeUniverse :: forall (n :: S). Value n
topeUniverse = Sig Void (Value Pattern Sig n) -> Value Pattern Sig n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig n)
forall scope term. Sig scope term
UniverseTopeF
vcube2 :: forall (n :: S). Value n
vcube2 = Sig Void (Value Pattern Sig n) -> Value Pattern Sig n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig n)
forall scope term. Sig scope term
Cube2F
vcubeI :: forall (n :: S). Value n
vcubeI = Sig Void (Value Pattern Sig n) -> Value Pattern Sig n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig n)
forall scope term. Sig scope term
CubeIF
vtopeBottom :: forall (n :: S). Value n
vtopeBottom = Sig Void (Value Pattern Sig n) -> Value Pattern Sig n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig n)
forall scope term. Sig scope term
TopeBottomF
vunit :: forall (n :: S). Value n
vunit = Sig Void (Value Pattern Sig n) -> Value Pattern Sig n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig n)
forall scope term. Sig scope term
UnitF
vtypeUnit :: forall (n :: S). Value n
vtypeUnit = Sig Void (Value Pattern Sig n) -> Value Pattern Sig n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig n)
forall scope term. Sig scope term
TypeUnitF
vrecBottom :: forall (n :: S). Value n
vrecBottom = Sig Void (Value Pattern Sig n) -> Value Pattern Sig n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode Sig Void (Value Pattern Sig n)
forall scope term. Sig scope term
RecBottomF

vhole :: Maybe VarIdent -> Value n
vhole :: forall (n :: S). Maybe VarIdent -> Value n
vhole = Sig Void (Value Pattern Sig n) -> Value Pattern Sig n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Sig Void (Value Pattern Sig n) -> Value Pattern Sig n)
-> (Maybe VarIdent -> Sig Void (Value Pattern Sig n))
-> Maybe VarIdent
-> Value Pattern Sig n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe VarIdent -> Sig Void (Value Pattern Sig n)
forall scope term. Maybe VarIdent -> Sig scope term
HoleF

-- * Weak head normal forms

-- | A weak head normal form of each constructor: @VTypeFun@ for 'TypeFunF',
-- and so on. A node with scoped subterms is suspended under its environment,
-- which the pattern binds first.
pattern VUniverse :: Whnf n
pattern $mVUniverse :: forall {r} {n :: S}. Whnf n -> ((# #) -> r) -> ((# #) -> r) -> r
VUniverse <- NbE.VNode UniverseF
pattern VUniverseCube :: Whnf n
pattern $mVUniverseCube :: forall {r} {n :: S}. Whnf n -> ((# #) -> r) -> ((# #) -> r) -> r
VUniverseCube <- NbE.VNode UniverseCubeF
pattern VUniverseTope :: Whnf n
pattern $mVUniverseTope :: forall {r} {n :: S}. Whnf n -> ((# #) -> r) -> ((# #) -> r) -> r
VUniverseTope <- NbE.VNode UniverseTopeF
pattern VCubeUnit :: Whnf n
pattern $mVCubeUnit :: forall {r} {n :: S}. Whnf n -> ((# #) -> r) -> ((# #) -> r) -> r
VCubeUnit <- NbE.VNode CubeUnitF
pattern VCubeUnitStar :: Whnf n
pattern $mVCubeUnitStar :: forall {r} {n :: S}. Whnf n -> ((# #) -> r) -> ((# #) -> r) -> r
VCubeUnitStar <- NbE.VNode CubeUnitStarF
pattern VCube2 :: Whnf n
pattern $mVCube2 :: forall {r} {n :: S}. Whnf n -> ((# #) -> r) -> ((# #) -> r) -> r
VCube2 <- NbE.VNode Cube2F
pattern VCube2_0 :: Whnf n
pattern $mVCube2_0 :: forall {r} {n :: S}. Whnf n -> ((# #) -> r) -> ((# #) -> r) -> r
VCube2_0 <- NbE.VNode Cube2_0F
pattern VCube2_1 :: Whnf n
pattern $mVCube2_1 :: forall {r} {n :: S}. Whnf n -> ((# #) -> r) -> ((# #) -> r) -> r
VCube2_1 <- NbE.VNode Cube2_1F
pattern VCubeI :: Whnf n
pattern $mVCubeI :: forall {r} {n :: S}. Whnf n -> ((# #) -> r) -> ((# #) -> r) -> r
VCubeI <- NbE.VNode CubeIF
pattern VCubeI_0 :: Whnf n
pattern $mVCubeI_0 :: forall {r} {n :: S}. Whnf n -> ((# #) -> r) -> ((# #) -> r) -> r
VCubeI_0 <- NbE.VNode CubeI_0F
pattern VCubeI_1 :: Whnf n
pattern $mVCubeI_1 :: forall {r} {n :: S}. Whnf n -> ((# #) -> r) -> ((# #) -> r) -> r
VCubeI_1 <- NbE.VNode CubeI_1F
pattern VCubeProduct :: Value n -> Value n -> Whnf n
pattern $mVCubeProduct :: forall {r} {n :: S}.
Whnf n -> (Value n -> Value n -> r) -> ((# #) -> r) -> r
VCubeProduct x1 x2 <- NbE.VNode (CubeProductF x1 x2)
pattern VCubeFlip :: Value n -> Whnf n
pattern $mVCubeFlip :: forall {r} {n :: S}. Whnf n -> (Value n -> r) -> ((# #) -> r) -> r
VCubeFlip x1 <- NbE.VNode (CubeFlipF x1)
pattern VCubeUnflip :: Value n -> Whnf n
pattern $mVCubeUnflip :: forall {r} {n :: S}. Whnf n -> (Value n -> r) -> ((# #) -> r) -> r
VCubeUnflip x1 <- NbE.VNode (CubeUnflipF x1)
pattern VCubeSup :: Value n -> Value n -> Whnf n
pattern $mVCubeSup :: forall {r} {n :: S}.
Whnf n -> (Value n -> Value n -> r) -> ((# #) -> r) -> r
VCubeSup x1 x2 <- NbE.VNode (CubeSupF x1 x2)
pattern VCubeInf :: Value n -> Value n -> Whnf n
pattern $mVCubeInf :: forall {r} {n :: S}.
Whnf n -> (Value n -> Value n -> r) -> ((# #) -> r) -> r
VCubeInf x1 x2 <- NbE.VNode (CubeInfF x1 x2)
pattern VTopeTop :: Whnf n
pattern $mVTopeTop :: forall {r} {n :: S}. Whnf n -> ((# #) -> r) -> ((# #) -> r) -> r
VTopeTop <- NbE.VNode TopeTopF
pattern VTopeBottom :: Whnf n
pattern $mVTopeBottom :: forall {r} {n :: S}. Whnf n -> ((# #) -> r) -> ((# #) -> r) -> r
VTopeBottom <- NbE.VNode TopeBottomF
pattern VTopeEQ :: Value n -> Value n -> Whnf n
pattern $mVTopeEQ :: forall {r} {n :: S}.
Whnf n -> (Value n -> Value n -> r) -> ((# #) -> r) -> r
VTopeEQ x1 x2 <- NbE.VNode (TopeEQF x1 x2)
pattern VTopeLEQ :: Value n -> Value n -> Whnf n
pattern $mVTopeLEQ :: forall {r} {n :: S}.
Whnf n -> (Value n -> Value n -> r) -> ((# #) -> r) -> r
VTopeLEQ x1 x2 <- NbE.VNode (TopeLEQF x1 x2)
pattern VTopeAnd :: Value n -> Value n -> Whnf n
pattern $mVTopeAnd :: forall {r} {n :: S}.
Whnf n -> (Value n -> Value n -> r) -> ((# #) -> r) -> r
VTopeAnd x1 x2 <- NbE.VNode (TopeAndF x1 x2)
pattern VTopeOr :: Value n -> Value n -> Whnf n
pattern $mVTopeOr :: forall {r} {n :: S}.
Whnf n -> (Value n -> Value n -> r) -> ((# #) -> r) -> r
VTopeOr x1 x2 <- NbE.VNode (TopeOrF x1 x2)
pattern VTopeInv :: Value n -> Whnf n
pattern $mVTopeInv :: forall {r} {n :: S}. Whnf n -> (Value n -> r) -> ((# #) -> r) -> r
VTopeInv x1 <- NbE.VNode (TopeInvF x1)
pattern VTopeUninv :: Value n -> Whnf n
pattern $mVTopeUninv :: forall {r} {n :: S}. Whnf n -> (Value n -> r) -> ((# #) -> r) -> r
VTopeUninv x1 <- NbE.VNode (TopeUninvF x1)
pattern VRecBottom :: Whnf n
pattern $mVRecBottom :: forall {r} {n :: S}. Whnf n -> ((# #) -> r) -> ((# #) -> r) -> r
VRecBottom <- NbE.VNode RecBottomF
pattern VRecOr :: Value n -> [(Value n, Value n)] -> Whnf n
pattern $mVRecOr :: forall {r} {n :: S}.
Whnf n
-> (Value n -> [(Value n, Value n)] -> r) -> ((# #) -> r) -> r
VRecOr ty rs <- NbE.VNode (RecOrF (Just ty) rs)
pattern VTypeFun :: () => Distinct i => Env i n -> TModality -> Value n -> Maybe (Value n) -> ScopedCore i -> Whnf n
pattern $mVTypeFun :: forall {r} {n :: S}.
Whnf n
-> (forall {i :: S}.
    Distinct i =>
    Env i n
    -> TModality -> Value n -> Maybe (Value n) -> ScopedCore i -> r)
-> ((# #) -> r)
-> r
VTypeFun env x1 x2 x3 x4 <- NbE.VSuspended env (TypeFunF x1 x2 x3 x4)
pattern VTypeShape :: TModality -> Value n -> Maybe (Value n) -> Whnf n
pattern $mVTypeShape :: forall {r} {n :: S}.
Whnf n
-> (TModality -> Value n -> Maybe (Value n) -> r)
-> ((# #) -> r)
-> r
VTypeShape x1 x2 x3 <- NbE.VNode (TypeShapeF x1 x2 x3)
pattern VTypeSigma :: () => Distinct i => Env i n -> TModality -> Value n -> ScopedCore i -> Whnf n
pattern $mVTypeSigma :: forall {r} {n :: S}.
Whnf n
-> (forall {i :: S}.
    Distinct i =>
    Env i n -> TModality -> Value n -> ScopedCore i -> r)
-> ((# #) -> r)
-> r
VTypeSigma env x1 x2 x3 <- NbE.VSuspended env (TypeSigmaF x1 x2 x3)
pattern VTypeId :: Value n -> Maybe (Value n) -> Value n -> Whnf n
pattern $mVTypeId :: forall {r} {n :: S}.
Whnf n
-> (Value n -> Maybe (Value n) -> Value n -> r)
-> ((# #) -> r)
-> r
VTypeId x1 x2 x3 <- NbE.VNode (TypeIdF x1 x2 x3)
pattern VApp :: Value n -> Value n -> Whnf n
pattern $mVApp :: forall {r} {n :: S}.
Whnf n -> (Value n -> Value n -> r) -> ((# #) -> r) -> r
VApp x1 x2 <- NbE.VNode (AppF x1 x2)
pattern VLet :: () => Distinct i => Env i n -> Maybe (Value n) -> Value n -> ScopedCore i -> Whnf n
pattern $mVLet :: forall {r} {n :: S}.
Whnf n
-> (forall {i :: S}.
    Distinct i =>
    Env i n -> Maybe (Value n) -> Value n -> ScopedCore i -> r)
-> ((# #) -> r)
-> r
VLet env x1 x2 x3 <- NbE.VSuspended env (LetF x1 x2 x3)
pattern VLambda :: () => Distinct i => Env i n -> Maybe (Param (Value n)) -> ScopedCore i -> Whnf n
pattern $mVLambda :: forall {r} {n :: S}.
Whnf n
-> (forall {i :: S}.
    Distinct i =>
    Env i n -> Maybe (Param (Value n)) -> ScopedCore i -> r)
-> ((# #) -> r)
-> r
VLambda env x1 x2 <- NbE.VSuspended env (LambdaF x1 x2)
pattern VPair :: Value n -> Value n -> Whnf n
pattern $mVPair :: forall {r} {n :: S}.
Whnf n -> (Value n -> Value n -> r) -> ((# #) -> r) -> r
VPair x1 x2 <- NbE.VNode (PairF x1 x2)
pattern VFirst :: Value n -> Whnf n
pattern $mVFirst :: forall {r} {n :: S}. Whnf n -> (Value n -> r) -> ((# #) -> r) -> r
VFirst x1 <- NbE.VNode (FirstF x1)
pattern VSecond :: Value n -> Whnf n
pattern $mVSecond :: forall {r} {n :: S}. Whnf n -> (Value n -> r) -> ((# #) -> r) -> r
VSecond x1 <- NbE.VNode (SecondF x1)
pattern VRefl :: Maybe (Value n, Maybe (Value n)) -> Whnf n
pattern $mVRefl :: forall {r} {n :: S}.
Whnf n
-> (Maybe (Value n, Maybe (Value n)) -> r) -> ((# #) -> r) -> r
VRefl x1 <- NbE.VNode (ReflF x1)
pattern VIdJ :: Value n -> Value n -> Value n -> Value n -> Value n -> Value n -> Whnf n
pattern $mVIdJ :: forall {r} {n :: S}.
Whnf n
-> (Value n
    -> Value n -> Value n -> Value n -> Value n -> Value n -> r)
-> ((# #) -> r)
-> r
VIdJ x1 x2 x3 x4 x5 x6 <- NbE.VNode (IdJF x1 x2 x3 x4 x5 x6)
pattern VMatch :: TModality -> Value n -> Maybe (Value n) -> [(VarIdent, Value n)] -> Whnf n
pattern $mVMatch :: forall {r} {n :: S}.
Whnf n
-> (TModality
    -> Value n -> Maybe (Value n) -> [(VarIdent, Value n)] -> r)
-> ((# #) -> r)
-> r
VMatch x1 x2 x3 x4 <- NbE.VNode (MatchF x1 x2 x3 x4)
pattern VMatchArm :: () => Distinct i => Env i n -> ScopedCore i -> Whnf n
pattern $mVMatchArm :: forall {r} {n :: S}.
Whnf n
-> (forall {i :: S}. Distinct i => Env i n -> ScopedCore i -> r)
-> ((# #) -> r)
-> r
VMatchArm env x1 <- NbE.VSuspended env (MatchArmF x1)
pattern VUnit :: Whnf n
pattern $mVUnit :: forall {r} {n :: S}. Whnf n -> ((# #) -> r) -> ((# #) -> r) -> r
VUnit <- NbE.VNode UnitF
pattern VTypeUnit :: Whnf n
pattern $mVTypeUnit :: forall {r} {n :: S}. Whnf n -> ((# #) -> r) -> ((# #) -> r) -> r
VTypeUnit <- NbE.VNode TypeUnitF
pattern VTypeAsc :: Value n -> Value n -> Whnf n
pattern $mVTypeAsc :: forall {r} {n :: S}.
Whnf n -> (Value n -> Value n -> r) -> ((# #) -> r) -> r
VTypeAsc x1 x2 <- NbE.VNode (TypeAscF x1 x2)
pattern VAnn :: Value n -> Value n -> Whnf n
pattern $mVAnn :: forall {r} {n :: S}.
Whnf n -> (Value n -> Value n -> r) -> ((# #) -> r) -> r
VAnn x1 x2 <- NbE.VNode (AnnF x1 x2)
pattern VTypeRestricted :: Value n -> [(Value n, Value n)] -> Whnf n
pattern $mVTypeRestricted :: forall {r} {n :: S}.
Whnf n
-> (Value n -> [(Value n, Value n)] -> r) -> ((# #) -> r) -> r
VTypeRestricted x1 x2 <- NbE.VNode (TypeRestrictedF x1 x2)
pattern VTypeModal :: TModality -> Value n -> Whnf n
pattern $mVTypeModal :: forall {r} {n :: S}.
Whnf n -> (TModality -> Value n -> r) -> ((# #) -> r) -> r
VTypeModal x1 x2 <- NbE.VNode (TypeModalF x1 x2)
pattern VModApp :: TModality -> Value n -> Whnf n
pattern $mVModApp :: forall {r} {n :: S}.
Whnf n -> (TModality -> Value n -> r) -> ((# #) -> r) -> r
VModApp x1 x2 <- NbE.VNode (ModAppF x1 x2)
pattern VModExtract :: TModality -> TModality -> Value n -> Whnf n
pattern $mVModExtract :: forall {r} {n :: S}.
Whnf n
-> (TModality -> TModality -> Value n -> r) -> ((# #) -> r) -> r
VModExtract x1 x2 x3 <- NbE.VNode (ModExtractF x1 x2 x3)
pattern VLetMod :: () => Distinct i => Env i n -> TModality -> TModality -> Maybe (Value n) -> Maybe (Value n) -> Value n -> ScopedCore i -> Whnf n
pattern $mVLetMod :: forall {r} {n :: S}.
Whnf n
-> (forall {i :: S}.
    Distinct i =>
    Env i n
    -> TModality
    -> TModality
    -> Maybe (Value n)
    -> Maybe (Value n)
    -> Value n
    -> ScopedCore i
    -> r)
-> ((# #) -> r)
-> r
VLetMod env x1 x2 x3 x4 x5 x6 <- NbE.VSuspended env (LetModF x1 x2 x3 x4 x5 x6)
pattern VHole :: Maybe VarIdent -> Whnf n
pattern $mVHole :: forall {r} {n :: S}.
Whnf n -> (Maybe VarIdent -> r) -> ((# #) -> r) -> r
VHole x1 <- NbE.VNode (HoleF x1)

-- * Elimination

-- | Apply a function value: a λ enters its body under the pattern bound to
-- the argument; a neutral stays stuck, with the shape of a tope family it
-- is an application of as a side condition.
appValue :: Distinct n => Scope n -> Context n -> Value n -> Value n -> Value n
{-# INLINE appValue #-}
appValue :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n -> Value n
appValue Scope n
scope Context n
ctx Value n
fun Value n
arg = Value n -> (Whnf Pattern Sig n -> Value n) -> Value n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n
-> (Whnf binder sig n -> Value binder sig n) -> Value binder sig n
NbE.eliminate Value n
fun ((Whnf Pattern Sig n -> Value n) -> Value n)
-> (Whnf Pattern Sig n -> Value n) -> Value n
forall a b. (a -> b) -> a -> b
$ \case
  VLambda Env i n
env Maybe (Param (Value n))
_ ScopedCore i
body -> Scope n
-> Context n -> Env i n -> ScopedCore i -> 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 Scope n
scope Context n
ctx Env i n
env ScopedCore i
body Value n
arg
  Whnf Pattern Sig n
VRecBottom -> Value n
forall (n :: S). Value n
vrecBottom
  NbE.Stuck Value n
fun' -> 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 Scope n
scope Context n
ctx Value n
fun' of
    Just Value n
ty | Just (Family TModality
_ Value n
_ (Just Value n
shape)) <- Value n -> Maybe (Family n)
forall (n :: S). Value n -> Maybe (Family n)
familyOf Value n
ty
            , Bool -> Bool
not (Value n -> Bool
forall (n :: S). Value n -> Bool
isTopShape Value n
shape) ->
        Value n -> Value n -> Value n
forall (n :: S). Value n -> Value n -> Value n
topeAndValue (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
arg) (Sig Void (Value n) -> Value n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value n -> Value n -> Sig Void (Value n)
forall scope term. term -> term -> Sig scope term
AppF Value n
fun' Value n
arg))
    Maybe (Value n)
_ -> Scope n -> Context n -> Value n -> Value n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
annotated Scope n
scope Context n
ctx (Sig Void (Value n) -> Value n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value n -> Value n -> Sig Void (Value n)
forall scope term. term -> term -> Sig scope term
AppF Value n
fun' Value n
arg))

firstValue :: Value n -> Value n
{-# INLINE firstValue #-}
firstValue :: forall (n :: S). Value n -> Value n
firstValue Value n
p = Value n -> (Whnf Pattern Sig n -> Value n) -> Value n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n
-> (Whnf binder sig n -> Value binder sig n) -> Value binder sig n
NbE.eliminate Value n
p ((Whnf Pattern Sig n -> Value n) -> Value n)
-> (Whnf Pattern Sig n -> Value n) -> Value n
forall a b. (a -> b) -> a -> b
$ \case
  VPair Value n
a Value n
_ -> Value n
a
  NbE.Stuck Value n
p' -> Sig Void (Value n) -> Value n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value n -> Sig Void (Value n)
forall scope term. term -> Sig scope term
FirstF Value n
p')

secondValue :: Value n -> Value n
{-# INLINE secondValue #-}
secondValue :: forall (n :: S). Value n -> Value n
secondValue Value n
p = Value n -> (Whnf Pattern Sig n -> Value n) -> Value n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n
-> (Whnf binder sig n -> Value binder sig n) -> Value binder sig n
NbE.eliminate Value n
p ((Whnf Pattern Sig n -> Value n) -> Value n)
-> (Whnf Pattern Sig n -> Value n) -> Value n
forall a b. (a -> b) -> a -> b
$ \case
  VPair Value n
_ Value n
b -> Value n
b
  NbE.Stuck Value n
p' -> Sig Void (Value n) -> Value n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value n -> Sig Void (Value n)
forall scope term. term -> Sig scope term
SecondF Value n
p')

-- | A projection, a neutral annotated with its type.
firstValueIn :: Distinct n => Scope n -> Context n -> Value n -> Value n
{-# INLINE firstValueIn #-}
firstValueIn :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
firstValueIn Scope n
scope Context n
ctx Value n
p = Value n -> (Whnf Pattern Sig n -> Value n) -> Value n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n
-> (Whnf binder sig n -> Value binder sig n) -> Value binder sig n
NbE.eliminate Value n
p ((Whnf Pattern Sig n -> Value n) -> Value n)
-> (Whnf Pattern Sig n -> Value n) -> Value n
forall a b. (a -> b) -> a -> b
$ \case
  VPair Value n
a Value n
_ -> Value n
a
  Whnf Pattern Sig n
VRecBottom -> Value n
forall (n :: S). Value n
vrecBottom
  NbE.Stuck Value n
p' -> Scope n -> Context n -> Value n -> Value n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
annotated Scope n
scope Context n
ctx (Sig Void (Value n) -> Value n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value n -> Sig Void (Value n)
forall scope term. term -> Sig scope term
FirstF Value n
p'))

secondValueIn :: Distinct n => Scope n -> Context n -> Value n -> Value n
{-# INLINE secondValueIn #-}
secondValueIn :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
secondValueIn Scope n
scope Context n
ctx Value n
p = Value n -> (Whnf Pattern Sig n -> Value n) -> Value n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n
-> (Whnf binder sig n -> Value binder sig n) -> Value binder sig n
NbE.eliminate Value n
p ((Whnf Pattern Sig n -> Value n) -> Value n)
-> (Whnf Pattern Sig n -> Value n) -> Value n
forall a b. (a -> b) -> a -> b
$ \case
  VPair Value n
_ Value n
b -> Value n
b
  Whnf Pattern Sig n
VRecBottom -> Value n
forall (n :: S). Value n
vrecBottom
  NbE.Stuck Value n
p' -> Scope n -> Context n -> Value n -> Value n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
annotated Scope n
scope Context n
ctx (Sig Void (Value n) -> Value n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value n -> Sig Void (Value n)
forall scope term. term -> Sig scope term
SecondF Value n
p'))

-- | Path induction, a neutral annotated with its type.
idJValueIn :: Distinct n => Scope n -> Context n -> Value n -> Value n -> Value n -> Value n -> Value n -> Value n -> Value n
{-# INLINE idJValueIn #-}
idJValueIn :: forall (n :: S).
Distinct n =>
Scope n
-> Context n
-> Value n
-> Value n
-> Value n
-> Value n
-> Value n
-> Value n
-> Value n
idJValueIn Scope n
scope Context n
ctx Value n
tA Value n
a Value n
tC Value n
d Value n
x Value n
p = Value n -> (Whnf Pattern Sig n -> Value n) -> Value n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n
-> (Whnf binder sig n -> Value binder sig n) -> Value binder sig n
NbE.eliminate Value n
p ((Whnf Pattern Sig n -> Value n) -> Value n)
-> (Whnf Pattern Sig n -> Value n) -> Value n
forall a b. (a -> b) -> a -> b
$ \case
  VRefl Maybe (Value n, Maybe (Value n))
_ -> Value n
d
  Whnf Pattern Sig n
VRecBottom -> Value n
forall (n :: S). Value n
vrecBottom
  NbE.Stuck Value n
p' -> Scope n -> Context n -> Value n -> Value n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
annotated Scope n
scope Context n
ctx (Sig Void (Value n) -> Value n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value n
-> Value n
-> Value n
-> Value n
-> Value n
-> Value n
-> Sig Void (Value n)
forall scope term.
term -> term -> term -> term -> term -> term -> Sig scope term
IdJF Value n
tA Value n
a Value n
tC Value n
d Value n
x Value n
p'))

-- | A neutral with its type, computed lazily and once, from the type of
-- the neutral it eliminates (which is read off that neutral's annotation).
-- Other stuck values need no type annotation.
annotated :: Distinct n => Scope n -> Context n -> Value n -> Value n
{-# INLINE annotated #-}
annotated :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n
annotated Scope n
scope Context n
ctx Value n
v = 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 Scope n
scope Context n
ctx Value n
v of
  Just Value n
ty -> Sig Void (Value n) -> Value n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value n -> Value n -> Sig Void (Value n)
forall scope term. term -> term -> Sig scope term
AnnF Value n
v Value n
ty)
  Maybe (Value n)
Nothing
    | Value n -> Bool
forall (n :: S). Value n -> Bool
isNeutralValue Value n
v -> String -> Value n
forall a. String -> a
panicImpossible (String
"a neutral without a type: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Core n -> String
forall (n :: S). Distinct n => Core n -> String
fingerprint (Scope n -> Context n -> Value n -> Core n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Core n
quoteIn Scope n
scope Context n
ctx Value n
v))
    | Bool
otherwise -> Value n
v

-- | Is a value headed by a neutral? Canonical values can be stuck under an
-- elimination during conversion without being neutrals themselves.
isNeutralValue :: Value n -> Bool
isNeutralValue :: forall (n :: S). Value n -> Bool
isNeutralValue Value n
v = case Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.forceFolded Value n
v of
  NbE.VVar Name n
_ -> Bool
True
  VAnn Value n
n Value n
_ -> Value n -> Bool
forall (n :: S). Value n -> Bool
isNeutralValue Value n
n
  VApp Value n
f Value n
_ -> Value n -> Bool
forall (n :: S). Value n -> Bool
isNeutralValue Value n
f
  VFirst Value n
p -> Value n -> Bool
forall (n :: S). Value n -> Bool
isNeutralValue Value n
p
  VSecond Value n
p -> Value n -> Bool
forall (n :: S). Value n -> Bool
isNeutralValue Value n
p
  VIdJ Value n
_ Value n
_ Value n
_ Value n
_ Value n
_ Value n
p -> Value n -> Bool
forall (n :: S). Value n -> Bool
isNeutralValue Value n
p
  VRecOr Value n
_ [(Value n, Value n)]
_ -> Bool
True
  Whnf Pattern Sig n
_ -> Bool
False

-- | The type an annotation carries, if it is known.
annotationType :: Value n -> Maybe (Value n)
annotationType :: forall (n :: S). Value n -> Maybe (Value n)
annotationType Value n
ty = case Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value n
ty of
  VHole Maybe VarIdent
Nothing -> Maybe (Value n)
forall a. Maybe a
Nothing
  Whnf Pattern Sig n
_                         -> Value n -> Maybe (Value n)
forall a. a -> Maybe a
Just Value n
ty

-- | The join of two points, normalised at the endpoints.
supValue :: Value n -> Value n -> Value n
supValue :: forall (n :: S). Value n -> Value n -> Value n
supValue Value n
l Value n
r = case (Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value n
l, Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value n
r) of
  (Whnf Pattern Sig n
VCube2_0, Whnf Pattern Sig n
_) -> Value n
r
  (Whnf Pattern Sig n
_, Whnf Pattern Sig n
VCube2_0) -> Value n
l
  (Whnf Pattern Sig n
VCube2_1, Whnf Pattern Sig n
_) -> Value n
l
  (Whnf Pattern Sig n
_, Whnf Pattern Sig n
VCube2_1) -> Value n
r
  (Whnf Pattern Sig n
VCubeI_0, Whnf Pattern Sig n
_) -> Value n
r
  (Whnf Pattern Sig n
_, Whnf Pattern Sig n
VCubeI_0) -> Value n
l
  (Whnf Pattern Sig n
VCubeI_1, Whnf Pattern Sig n
_) -> Value n
l
  (Whnf Pattern Sig n
_, Whnf Pattern Sig n
VCubeI_1) -> Value n
r
  (Whnf Pattern Sig n, Whnf Pattern Sig n)
_ -> Sig Void (Value n) -> Value n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value n -> Value n -> Sig Void (Value n)
forall scope term. term -> term -> Sig scope term
CubeSupF Value n
l Value n
r)

-- | The meet of two points, normalised at the endpoints and distributed
-- over a join.
infValue :: Value n -> Value n -> Value n
infValue :: forall (n :: S). Value n -> Value n -> Value n
infValue Value n
l Value n
r = case (Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value n
l, Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value n
r) of
  (Whnf Pattern Sig n
VCube2_0, Whnf Pattern Sig n
_) -> Value n
l
  (Whnf Pattern Sig n
_, Whnf Pattern Sig n
VCube2_0) -> Value n
r
  (Whnf Pattern Sig n
VCube2_1, Whnf Pattern Sig n
_) -> Value n
r
  (Whnf Pattern Sig n
_, Whnf Pattern Sig n
VCube2_1) -> Value n
l
  (Whnf Pattern Sig n
VCubeI_0, Whnf Pattern Sig n
_) -> Value n
l
  (Whnf Pattern Sig n
_, Whnf Pattern Sig n
VCubeI_0) -> Value n
r
  (Whnf Pattern Sig n
VCubeI_1, Whnf Pattern Sig n
_) -> Value n
r
  (Whnf Pattern Sig n
_, Whnf Pattern Sig n
VCubeI_1) -> Value n
l
  ((VCubeSup Value n
a Value n
b), Whnf Pattern Sig n
_) -> Value n -> Value n -> Value n
forall (n :: S). Value n -> Value n -> Value n
supValue (Value n -> Value n -> Value n
forall (n :: S). Value n -> Value n -> Value n
infValue Value n
a Value n
r) (Value n -> Value n -> Value n
forall (n :: S). Value n -> Value n -> Value n
infValue Value n
b Value n
r)
  (Whnf Pattern Sig n
_, (VCubeSup Value n
a Value n
b)) -> Value n -> Value n -> Value n
forall (n :: S). Value n -> Value n -> Value n
supValue (Value n -> Value n -> Value n
forall (n :: S). Value n -> Value n -> Value n
infValue Value n
l Value n
a) (Value n -> Value n -> Value n
forall (n :: S). Value n -> Value n -> Value n
infValue Value n
l Value n
b)
  (Whnf Pattern Sig n, Whnf Pattern Sig n)
_ -> Sig Void (Value n) -> Value n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value n -> Value n -> Sig Void (Value n)
forall scope term. term -> term -> Sig scope term
CubeInfF Value n
l Value n
r)

-- | The codomain of a scope at a value for its pattern.
codomainAt :: (Distinct n, Distinct i) => Scope n -> Context n -> Env i n -> ScopedCore i -> Value n -> Value n
{-# INLINE codomainAt #-}
codomainAt :: forall (n :: S) (i :: S).
(Distinct n, Distinct i) =>
Scope n
-> Context n -> Env i n -> ScopedCore i -> Value n -> Value n
codomainAt = Scope n
-> Context n -> Env i n -> ScopedCore i -> 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

-- | A Π type value, opened: the environment its codomain is suspended
-- under, its modality, its domain, its shape and its codomain.
data Pi n where
  Pi :: Distinct i => Env i n -> TModality -> Value n -> Maybe (Value n) -> ScopedCore i -> Pi n

piOf :: Value n -> Maybe (Pi n)
piOf :: forall (n :: S). Value n -> Maybe (Pi n)
piOf Value n
v = case Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force (Value n -> Value n
forall (n :: S). Value n -> Value n
stripRestrictions Value n
v) of
  VTypeFun Env i n
env TModality
md Value n
dom Maybe (Value n)
mshape ScopedCore i
ret -> Pi n -> Maybe (Pi n)
forall a. a -> Maybe a
Just (Env i n
-> TModality -> Value n -> Maybe (Value n) -> ScopedCore i -> Pi n
forall (i :: S) (n :: S).
Distinct i =>
Env i n
-> TModality -> Value n -> Maybe (Value n) -> ScopedCore i -> Pi n
Pi Env i n
env TModality
md Value n
dom Maybe (Value n)
mshape ScopedCore i
ret)
  Whnf Pattern Sig n
_ -> Maybe (Pi n)
forall a. Maybe a
Nothing

-- | The type of a tope family: its modality, its cube and its shape.
data Family n = Family TModality (Value n) (Maybe (Value n))

familyOf :: Value n -> Maybe (Family n)
familyOf :: forall (n :: S). Value n -> Maybe (Family n)
familyOf Value n
v = case Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force (Value n -> Value n
forall (n :: S). Value n -> Value n
stripRestrictions Value n
v) of
  VTypeShape TModality
md Value n
cube Maybe (Value n)
mshape -> Family n -> Maybe (Family n)
forall a. a -> Maybe a
Just (TModality -> Value n -> Maybe (Value n) -> Family n
forall (n :: S).
TModality -> Value n -> Maybe (Value n) -> Family n
Family TModality
md Value n
cube Maybe (Value n)
mshape)
  Whnf Pattern Sig n
_ -> Maybe (Family n)
forall a. Maybe a
Nothing

-- | Is a shape the one that is ⊤ everywhere?
isTopShape :: Value n -> Bool
isTopShape :: forall (n :: S). Value n -> Bool
isTopShape Value n
v = case Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value n
v of
  VLambda Env i n
_ Maybe (Param (Value n))
_ (ScopedAST Pattern i l
_ Core l
TopeTop) -> Bool
True
  Whnf Pattern Sig n
_ -> Bool
False

-- | A Σ type value, opened.
data Sigma n where
  Sigma :: Distinct i => Env i n -> TModality -> Value n -> ScopedCore i -> Sigma n

sigmaOf :: Value n -> Maybe (Sigma n)
sigmaOf :: forall (n :: S). Value n -> Maybe (Sigma n)
sigmaOf Value n
v = case Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force (Value n -> Value n
forall (n :: S). Value n -> Value n
stripRestrictions Value n
v) of
  VTypeSigma Env i n
env TModality
md Value n
a ScopedCore i
b -> Sigma n -> Maybe (Sigma n)
forall a. a -> Maybe a
Just (Env i n -> TModality -> Value n -> ScopedCore i -> Sigma n
forall (i :: S) (n :: S).
Distinct i =>
Env i n -> TModality -> Value n -> ScopedCore i -> Sigma n
Sigma Env i n
env TModality
md Value n
a ScopedCore i
b)
  Whnf Pattern Sig n
_ -> Maybe (Sigma n)
forall a. Maybe a
Nothing

-- | A type value with its restrictions peeled off.
stripRestrictions :: Value n -> Value n
stripRestrictions :: forall (n :: S). Value n -> Value n
stripRestrictions Value n
v = case Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value n
v of
  VTypeRestricted Value n
ty [(Value n, Value n)]
_ -> Value n -> Value n
forall (n :: S). Value n -> Value n
stripRestrictions Value n
ty
  Whnf Pattern Sig n
_                                -> Value n
v

-- | The restrictions of a type value, outermost first.
restrictionsOf :: Value n -> [(Value n, Value n)]
restrictionsOf :: forall (n :: S). Value n -> [(Value n, Value n)]
restrictionsOf Value n
v = case Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value n
v of
  VTypeRestricted Value n
ty [(Value n, Value n)]
rs -> [(Value n, Value n)]
rs [(Value n, Value n)]
-> [(Value n, Value n)] -> [(Value n, Value n)]
forall a. Semigroup a => a -> a -> a
<> Value n -> [(Value n, Value n)]
forall (n :: S). Value n -> [(Value n, Value n)]
restrictionsOf Value n
ty
  Whnf Pattern Sig n
_                                 -> []

-- * Patterns

-- | A pattern as a value of its own scope: its names, paired.
patternValue :: (Distinct n, Distinct l) => Pattern n l -> Value l
patternValue :: forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Pattern n l -> Value l
patternValue = \case
  PatternVar Maybe VarIdent
_ NameBinder n l
b    -> Name l -> Value l
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> Value binder sig n
NbE.vvar (NameBinder n l -> Name l
forall (n :: S) (l :: S). NameBinder n l -> Name l
Foil.nameOf NameBinder n l
b)
  PatternWildcard NameBinder n l
b -> Name l -> Value l
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> Value binder sig n
NbE.vvar (NameBinder n l -> Name l
forall (n :: S) (l :: S). NameBinder n l -> Name l
Foil.nameOf NameBinder n l
b)
  Pattern n l
PatternUnit       -> Value l
forall (n :: S). Value n
vunit
  PatternPair Pattern n i
l Pattern i l
r -> case (Pattern n i -> DistinctEvidence i
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct Pattern n i
l, Pattern i l -> ExtEvidence i l
forall (pattern :: S -> S -> *) (n :: S) (l :: S).
CoSinkable pattern =>
pattern n l -> ExtEvidence n l
Foil.assertExt Pattern i l
r) of
    (DistinctEvidence i
Foil.Distinct, ExtEvidence i l
Foil.Ext) ->
      Sig Void (Value l) -> Value l
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value l -> Value l -> Sig Void (Value l)
forall scope term. term -> term -> Sig scope term
PairF (Value' 'MayGlue Pattern Sig i -> Value l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink (Pattern n i -> Value' 'MayGlue Pattern Sig i
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Pattern n l -> Value l
patternValue Pattern n i
l)) (Pattern i l -> Value l
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Pattern n l -> Value l
patternValue Pattern i l
r))

-- | An environment extended with a pattern bound to a value: each leaf
-- gets the projection of the value at its position, computed when the
-- name is looked up.
bindPattern :: Env i n -> Pattern i l -> Value n -> Env l n
bindPattern :: forall (i :: S) (n :: S) (l :: S).
Env i n -> Pattern i l -> Value n -> Env l n
bindPattern Env i n
env Pattern i l
pat Value n
v = case Pattern i l
pat of
  PatternVar Maybe VarIdent
_ NameBinder i l
b    -> Env i n -> NameBinder i l -> Value n -> Env l n
forall (e :: S -> *) (i :: S) (o :: S) (i' :: S).
Substitution e i o -> NameBinder i i' -> e o -> Substitution e i' o
Foil.addSubst Env i n
env NameBinder i l
b Value n
v
  PatternWildcard NameBinder i l
b -> Env i n -> NameBinder i l -> Value n -> Env l n
forall (e :: S -> *) (i :: S) (o :: S) (i' :: S).
Substitution e i o -> NameBinder i i' -> e o -> Substitution e i' o
Foil.addSubst Env i n
env NameBinder i l
b Value n
v
  Pattern i l
PatternUnit       -> Env i n
Env l n
env
  PatternPair Pattern i i
l Pattern i l
r   -> Env i n -> Pattern i l -> Value n -> Env l n
forall (i :: S) (n :: S) (l :: S).
Env i n -> Pattern i l -> Value n -> Env l n
bindPattern (Env i n -> Pattern i i -> Value n -> Env i n
forall (i :: S) (n :: S) (l :: S).
Env i n -> Pattern i l -> Value n -> Env l n
bindPattern Env i n
env Pattern i i
l (Value n -> Value n
forall (n :: S). Value n -> Value n
firstValue Value n
v)) Pattern i l
r (Value n -> Value n
forall (n :: S). Value n -> Value n
secondValue Value n
v)

-- | The pattern binding one name.
singlePattern :: NameBinder n l -> Pattern n l
singlePattern :: forall (n :: S) (l :: S). NameBinder n l -> Pattern n l
singlePattern = Maybe VarIdent -> NameBinder n l -> Pattern n l
forall (n :: S) (l :: S).
Maybe VarIdent -> NameBinder n l -> Pattern n l
PatternVar Maybe VarIdent
forall a. Maybe a
Nothing

-- | The entries of a pattern's names, in binding order, from the type of
-- the value it is bound to: a pair pattern destructures a Σ type or a cube
-- product. A leaf whose type cannot be read gets a hole for a type.
--
-- The context is the one under the pattern, without the pattern's own
-- entries yet: the codomain of a Σ is evaluated at the left component's
-- names, which the environment binds, so the entries are never consulted
-- while they are built.
patternEntries
  :: forall n l. (Distinct n, Distinct l)
  => Scope l -> Context l -> TModality -> Maybe LocationInfo -> Value l -> Pattern n l -> [Entry l]
patternEntries :: forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Scope l
-> Context l
-> TModality
-> Maybe LocationInfo
-> Value l
-> Pattern n l
-> [Entry l]
patternEntries Scope l
scope Context l
ctx TModality
md Maybe LocationInfo
loc = Value l -> Pattern n l -> [Entry l]
forall (x :: S) (y :: S).
(Distinct x, Distinct y) =>
Value l -> Pattern x y -> [Entry l]
go
  where
    go :: (Distinct x, Distinct y) => Value l -> Pattern x y -> [Entry l]
    go :: forall (x :: S) (y :: S).
(Distinct x, Distinct y) =>
Value l -> Pattern x y -> [Entry l]
go Value l
ty = \case
      PatternVar Maybe VarIdent
x NameBinder x y
_    -> [Maybe VarIdent -> Value l -> Entry l
entryOf Maybe VarIdent
x Value l
ty]
      PatternWildcard NameBinder x y
_ -> [Maybe VarIdent -> Value l -> Entry l
entryOf Maybe VarIdent
forall a. Maybe a
Nothing Value l
ty]
      Pattern x y
PatternUnit       -> []
      PatternPair Pattern x i
l Pattern i y
r -> case Pattern x i -> DistinctEvidence i
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct Pattern x i
l of
        DistinctEvidence i
Foil.Distinct -> case Value l -> Whnf Pattern Sig l
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force (Value l -> Value l
forall (n :: S). Value n -> Value n
stripRestrictions Value l
ty) of
          VTypeSigma Env i l
env TModality
_ Value l
a ScopedCore i
b ->
            Value l -> Pattern x i -> [Entry l]
forall (x :: S) (y :: S).
(Distinct x, Distinct y) =>
Value l -> Pattern x y -> [Entry l]
go ((Value l -> Value l) -> Value l -> Value l
inherit Value l -> Value l
forall (n :: S). Value n -> Value n
firstValue Value l
a) Pattern x i
l
              [Entry l] -> [Entry l] -> [Entry l]
forall a. Semigroup a => a -> a -> a
<> Value l -> Pattern i y -> [Entry l]
forall (x :: S) (y :: S).
(Distinct x, Distinct y) =>
Value l -> Pattern x y -> [Entry l]
go ((Value l -> Value l) -> Value l -> Value l
inherit Value l -> Value l
forall (n :: S). Value n -> Value n
secondValue (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
evalScope Scope l
scope Context l
ctx Env i l
env ScopedCore i
b (Pattern x i -> Value l
forall (x :: S) (y :: S).
(Distinct x, Distinct y) =>
Pattern x y -> Value l
patternValueIn Pattern x i
l))) Pattern i y
r
          VCubeProduct Value l
a Value l
b -> Value l -> Pattern x i -> [Entry l]
forall (x :: S) (y :: S).
(Distinct x, Distinct y) =>
Value l -> Pattern x y -> [Entry l]
go ((Value l -> Value l) -> Value l -> Value l
inherit Value l -> Value l
forall (n :: S). Value n -> Value n
firstValue Value l
a) Pattern x i
l [Entry l] -> [Entry l] -> [Entry l]
forall a. Semigroup a => a -> a -> a
<> Value l -> Pattern i y -> [Entry l]
forall (x :: S) (y :: S).
(Distinct x, Distinct y) =>
Value l -> Pattern x y -> [Entry l]
go ((Value l -> Value l) -> Value l -> Value l
inherit Value l -> Value l
forall (n :: S). Value n -> Value n
secondValue Value l
b) Pattern i y
r
          Whnf Pattern Sig l
_ -> Value l -> Pattern x i -> [Entry l]
forall (x :: S) (y :: S).
(Distinct x, Distinct y) =>
Value l -> Pattern x y -> [Entry l]
go (Maybe VarIdent -> Value l
forall (n :: S). Maybe VarIdent -> Value n
vhole Maybe VarIdent
forall a. Maybe a
Nothing) Pattern x i
l [Entry l] -> [Entry l] -> [Entry l]
forall a. Semigroup a => a -> a -> a
<> Value l -> Pattern i y -> [Entry l]
forall (x :: S) (y :: S).
(Distinct x, Distinct y) =>
Value l -> Pattern x y -> [Entry l]
go (Maybe VarIdent -> Value l
forall (n :: S). Maybe VarIdent -> Value n
vhole Maybe VarIdent
forall a. Maybe a
Nothing) Pattern i y
r
      where
        -- A component of a restricted pair is restricted to the same
        -- component of each boundary pair.
        inherit :: (Value l -> Value l) -> Value l -> Value l
inherit Value l -> Value l
project Value l
component = case Value l -> [(Value l, Value l)]
forall (n :: S). Value n -> [(Value n, Value n)]
restrictionsOf Value l
ty of
          [] -> Value l
component
          [(Value l, Value l)]
rs -> Sig Void (Value l) -> Value l
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value l -> [(Value l, Value l)] -> Sig Void (Value l)
forall scope term. term -> [(term, term)] -> Sig scope term
TypeRestrictedF Value l
component [ (Value l
tope, Value l -> Value l
project Value l
face) | (Value l
tope, Value l
face) <- [(Value l, Value l)]
rs ])
    -- The names of a subpattern also belong to the whole pattern's scope.
    -- free-foil does not expose the prefix-to-final-scope evidence here.
    patternValueIn :: (Distinct x, Distinct y) => Pattern x y -> Value l
    patternValueIn :: forall (x :: S) (y :: S).
(Distinct x, Distinct y) =>
Pattern x y -> Value l
patternValueIn = Value y -> Value l
forall a b. a -> b
unsafeCoerce (Value y -> Value l)
-> (Pattern x y -> Value y) -> Pattern x y -> Value l
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pattern x y -> Value y
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Pattern n l -> Value l
patternValue
    entryOf :: Maybe VarIdent -> Value l -> Entry l
entryOf Maybe VarIdent
mname Value l
t = TModality
-> Maybe VarIdent -> Maybe LocationInfo -> Value l -> Entry l
forall (n :: S).
TModality
-> Maybe VarIdent -> Maybe LocationInfo -> Value n -> Entry n
localEntry TModality
md Maybe VarIdent
mname Maybe LocationInfo
loc Value l
t

-- | The entries of a let's pattern: each name has its component of the
-- value.
letEntries
  :: forall n l. (Distinct n, Distinct l)
  => Scope l -> Context l -> Maybe LocationInfo -> Value l -> Value l -> Pattern n l -> [Entry l]
letEntries :: 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 l
ctx Maybe LocationInfo
loc Value l
ty Value l
val Pattern n l
pat =
  (Entry l -> Value l -> Entry l)
-> [Entry l] -> [Value l] -> [Entry l]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\Entry l
entry Value l
v -> Entry l
entry { entryValue = Just v })
    (Scope l
-> Context l
-> TModality
-> Maybe LocationInfo
-> Value l
-> Pattern n l
-> [Entry l]
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Scope l
-> Context l
-> TModality
-> Maybe LocationInfo
-> Value l
-> Pattern n l
-> [Entry l]
patternEntries Scope l
scope Context l
ctx TModality
Id Maybe LocationInfo
loc Value l
ty Pattern n l
pat)
    (Pattern n l -> Value l -> [Value l]
forall (x :: S) (y :: S). Pattern x y -> Value l -> [Value l]
leafValues Pattern n l
pat Value l
val)
  where
    leafValues :: Pattern x y -> Value l -> [Value l]
    leafValues :: forall (x :: S) (y :: S). Pattern x y -> Value l -> [Value l]
leafValues Pattern x y
p Value l
v = case Pattern x y
p of
      PatternVar Maybe VarIdent
_ NameBinder x y
_    -> [Value l
v]
      PatternWildcard NameBinder x y
_ -> [Value l
v]
      Pattern x y
PatternUnit       -> []
      PatternPair Pattern x i
l Pattern i y
r   -> Pattern x i -> Value l -> [Value l]
forall (x :: S) (y :: S). Pattern x y -> Value l -> [Value l]
leafValues Pattern x i
l (Value l -> Value l
forall (n :: S). Value n -> Value n
firstValue Value l
v) [Value l] -> [Value l] -> [Value l]
forall a. Semigroup a => a -> a -> a
<> Pattern i y -> Value l -> [Value l]
forall (x :: S) (y :: S). Pattern x y -> Value l -> [Value l]
leafValues Pattern i y
r (Value l -> Value l
forall (n :: S). Value n -> Value n
secondValue Value l
v)

-- * Binders

-- | The context under a pattern bound at a type, with a shape assumed for
-- it.
underPattern
  :: (Distinct n, DExt n l)
  => Scope l -> Context n -> TModality -> Value n -> Maybe (Value n) -> Pattern n l -> Context l
underPattern :: 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
ty Maybe (Value n)
mshape Pattern n l
pat =
  let ctx0 :: Context l
ctx0 = Context n -> Context l
forall (n :: S) (l :: S). DExt n l => Context n -> Context l
sinkContextUnchecked Context n
ctx
      entries :: [Entry l]
entries = Scope l
-> Context l
-> TModality
-> Maybe LocationInfo
-> Value l
-> Pattern n l
-> [Entry l]
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Scope l
-> Context l
-> TModality
-> Maybe LocationInfo
-> Value l
-> Pattern n l
-> [Entry l]
patternEntries Scope l
scope' Context l
ctx0 TModality
md (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) Pattern n l
pat
      ctx' :: Context l
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 [Entry l]
entries Context n
ctx
  in Scope l -> Pattern n l -> Maybe (Value n) -> Context l -> Context l
forall (n :: S) (l :: S).
(Distinct n, DExt n l) =>
Scope l -> Pattern n l -> Maybe (Value n) -> Context l -> Context l
assumeShapeAt Scope l
scope' Pattern n l
pat Maybe (Value n)
mshape Context l
ctx'

-- | A context with a binder's shape assumed at the pattern bound by it.
assumeShapeAt
  :: (Distinct n, DExt n l)
  => Scope l -> Pattern n l -> Maybe (Value n) -> Context l -> Context l
assumeShapeAt :: forall (n :: S) (l :: S).
(Distinct n, DExt n l) =>
Scope l -> Pattern n l -> Maybe (Value n) -> Context l -> Context l
assumeShapeAt Scope l
scope' Pattern n l
pat Maybe (Value n)
mshape Context l
ctx = case Maybe (Value n)
mshape of
  Maybe (Value n)
Nothing    -> Context l
ctx
  Just Value n
shape -> Scope l -> Context l -> Value l -> Context l
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Context n
assumeValue Scope l
scope' Context l
ctx (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))

-- | The context under one fresh name of a type: the scope, the context
-- with the name's entry, and the name.
underFreshVar
  :: Distinct n
  => Scope n -> Context n -> TModality -> Value n -> Maybe (Value n)
  -> (forall l. (DExt n l, Distinct l) => Scope l -> Context l -> Foil.Name l -> r) -> r
underFreshVar :: 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
ty Maybe (Value n)
mshape forall (l :: S).
(DExt n l, Distinct l) =>
Scope l -> Context l -> Name l -> r
k = Scope n -> (forall (l :: S). DExt n l => NameBinder n l -> r) -> r
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 -> r) -> r)
-> (forall (l :: S). DExt n l => NameBinder n l -> r) -> r
forall a b. (a -> b) -> a -> b
$ \NameBinder n l
b ->
  let pat :: Pattern n l
pat = NameBinder n l -> Pattern n l
forall (n :: S) (l :: S). NameBinder n l -> Pattern n l
singlePattern NameBinder n l
b
      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 Scope l -> Context l -> Name l -> r
forall (l :: S).
(DExt n l, Distinct l) =>
Scope l -> Context l -> Name l -> r
k Scope l
scope' (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
ty Maybe (Value n)
mshape Pattern n l
pat) (NameBinder n l -> Name l
forall (n :: S) (l :: S). NameBinder n l -> Name l
Foil.nameOf NameBinder n l
b)

-- | Does a value stand for a cube: by its head, or by the sort of the
-- entry at its head?
isCube :: Distinct n => Scope n -> Context n -> Value n -> Bool
isCube :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Bool
isCube Scope n
scope Context n
ctx Value n
v = Value n -> Cube
forall (n :: S). Value n -> Cube
cubeOfType Value n
v Cube -> Cube -> Bool
forall a. Eq a => a -> a -> Bool
/= Cube
KOther Bool -> Bool -> Bool
|| 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 Scope n
scope Context n
ctx Value n
v of
  Just Value n
s | Whnf n
VUniverseCube <- Value n -> Whnf n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force (Value n -> Value n
forall (n :: S). Value n -> Value n
stripRestrictions Value n
s) -> Bool
True
  Maybe (Value n)
_ -> Bool
False

-- | The shape of a binder over a cube with no restriction: the family
-- that is ⊤ everywhere.
topShapeValue :: Distinct n => Scope n -> Value n -> Value n
topShapeValue :: forall (n :: S). Distinct n => Scope n -> Value n -> Value n
topShapeValue Scope n
scope Value n
cube = 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
b ->
  Substitution (Value 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 Pattern Sig) n n
forall (e :: S -> *) (i :: S). InjectName e => Substitution e i i
Foil.identitySubst
    (Maybe (Param (Value n))
-> ScopedAST Pattern Sig n
-> Sig (ScopedAST Pattern Sig n) (Value n)
forall scope term. Maybe (Param term) -> scope -> Sig scope term
LambdaF (Param (Value n) -> Maybe (Param (Value n))
forall a. a -> Maybe a
Just (TModality -> Value n -> Maybe (Value n) -> Param (Value n)
forall term. TModality -> term -> Maybe term -> Param term
Param TModality
Id Value n
cube Maybe (Value 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
forall (n :: S). Core n
TopeTop))

-- | The binder of a λ, from its parameter: its domain and modality, and its
-- shape, assumed for the body. A λ without a parameter binds names of an
-- unknown type.
lambdaBinder :: Maybe (Param (Value n)) -> Extend Context Pattern Sig n
{-# INLINE lambdaBinder #-}
lambdaBinder :: forall (n :: S).
Maybe (Param (Value n)) -> Extend Context Pattern Sig n
lambdaBinder Maybe (Param (Value n))
mparam = (forall (l :: S).
 (Eval Context Pattern Sig, Distinct n, DExt n l) =>
 Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
-> Extend Context Pattern Sig n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
(forall (l :: S).
 (Eval ctx binder sig, Distinct n, DExt n l) =>
 Scope n -> binder n l -> Scope l -> ctx n -> ctx l)
-> Extend ctx binder sig n
Extend ((forall (l :: S).
  (Eval Context Pattern Sig, Distinct n, DExt n l) =>
  Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
 -> Extend Context Pattern Sig n)
-> (forall (l :: S).
    (Eval Context Pattern Sig, Distinct n, DExt n l) =>
    Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
-> Extend Context Pattern Sig n
forall a b. (a -> b) -> a -> b
$ \Scope n
_ Pattern n l
pat Scope l
scope' Context n
ctx -> case Maybe (Param (Value n))
mparam of
  Just (Param TModality
md Value n
dom Maybe (Value n)
mshape) -> 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
  Maybe (Param (Value n))
Nothing -> 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
Id (Maybe VarIdent -> Value n
forall (n :: S). Maybe VarIdent -> Value n
vhole Maybe VarIdent
forall a. Maybe a
Nothing) Maybe (Value n)
forall a. Maybe a
Nothing Pattern n l
pat

-- | The binder of a Π: its domain, and its shape assumed for the codomain.
piBinder :: TModality -> Value n -> Maybe (Value n) -> Extend Context Pattern Sig n
{-# INLINE piBinder #-}
piBinder :: forall (n :: S).
TModality
-> Value n -> Maybe (Value n) -> Extend Context Pattern Sig n
piBinder TModality
md Value n
dom Maybe (Value n)
mshape = (forall (l :: S).
 (Eval Context Pattern Sig, Distinct n, DExt n l) =>
 Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
-> Extend Context Pattern Sig n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
(forall (l :: S).
 (Eval ctx binder sig, Distinct n, DExt n l) =>
 Scope n -> binder n l -> Scope l -> ctx n -> ctx l)
-> Extend ctx binder sig n
Extend ((forall (l :: S).
  (Eval Context Pattern Sig, Distinct n, DExt n l) =>
  Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
 -> Extend Context Pattern Sig n)
-> (forall (l :: S).
    (Eval Context Pattern Sig, Distinct n, DExt n l) =>
    Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
-> Extend Context Pattern Sig n
forall a b. (a -> b) -> a -> b
$ \Scope n
_ Pattern n l
pat Scope l
scope' 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

sigmaBinder :: TModality -> Value n -> Extend Context Pattern Sig n
{-# INLINE sigmaBinder #-}
sigmaBinder :: forall (n :: S).
TModality -> Value n -> Extend Context Pattern Sig n
sigmaBinder TModality
md Value n
a = (forall (l :: S).
 (Eval Context Pattern Sig, Distinct n, DExt n l) =>
 Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
-> Extend Context Pattern Sig n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
(forall (l :: S).
 (Eval ctx binder sig, Distinct n, DExt n l) =>
 Scope n -> binder n l -> Scope l -> ctx n -> ctx l)
-> Extend ctx binder sig n
Extend ((forall (l :: S).
  (Eval Context Pattern Sig, Distinct n, DExt n l) =>
  Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
 -> Extend Context Pattern Sig n)
-> (forall (l :: S).
    (Eval Context Pattern Sig, Distinct n, DExt n l) =>
    Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
-> Extend Context Pattern Sig n
forall a b. (a -> b) -> a -> b
$ \Scope n
_ Pattern n l
pat Scope l
scope' 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
a Maybe (Value n)
forall a. Maybe a
Nothing Pattern n l
pat

-- | The binder of a let: its names have the components of the value.
letBinder :: Maybe (Value n) -> Value n -> Extend Context Pattern Sig n
{-# INLINE letBinder #-}
letBinder :: forall (n :: S).
Maybe (Value n) -> Value n -> Extend Context Pattern Sig n
letBinder Maybe (Value n)
mty Value n
val = (forall (l :: S).
 (Eval Context Pattern Sig, Distinct n, DExt n l) =>
 Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
-> Extend Context Pattern Sig n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
(forall (l :: S).
 (Eval ctx binder sig, Distinct n, DExt n l) =>
 Scope n -> binder n l -> Scope l -> ctx n -> ctx l)
-> Extend ctx binder sig n
Extend ((forall (l :: S).
  (Eval Context Pattern Sig, Distinct n, DExt n l) =>
  Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
 -> Extend Context Pattern Sig n)
-> (forall (l :: S).
    (Eval Context Pattern Sig, Distinct n, DExt n l) =>
    Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
-> Extend Context Pattern Sig n
forall a b. (a -> b) -> a -> b
$ \Scope n
_ Pattern n l
pat Scope l
scope' Context n
ctx ->
  let ctx0 :: Context l
ctx0 = Context n -> Context l
forall (n :: S) (l :: S). DExt n l => Context n -> Context l
sinkContextUnchecked Context n
ctx
      ty :: Value n
ty = Value n -> (Value n -> Value n) -> Maybe (Value n) -> Value n
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Maybe VarIdent -> Value n
forall (n :: S). Maybe VarIdent -> Value n
vhole Maybe VarIdent
forall a. Maybe a
Nothing) Value n -> Value n
forall a. a -> a
id Maybe (Value n)
mty
      entries :: [Entry l]
entries = 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 l
ctx0 (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
  in 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 [Entry l]
entries Context n
ctx

-- | A binder whose names have no known type (a construct not yet checked
-- on values).
opaqueBinder :: Extend Context Pattern Sig n
{-# INLINE opaqueBinder #-}
opaqueBinder :: forall (n :: S). Extend Context Pattern Sig n
opaqueBinder = (forall (l :: S).
 (Eval Context Pattern Sig, Distinct n, DExt n l) =>
 Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
-> Extend Context Pattern Sig n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
(forall (l :: S).
 (Eval ctx binder sig, Distinct n, DExt n l) =>
 Scope n -> binder n l -> Scope l -> ctx n -> ctx l)
-> Extend ctx binder sig n
Extend ((forall (l :: S).
  (Eval Context Pattern Sig, Distinct n, DExt n l) =>
  Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
 -> Extend Context Pattern Sig n)
-> (forall (l :: S).
    (Eval Context Pattern Sig, Distinct n, DExt n l) =>
    Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
-> Extend Context Pattern Sig n
forall a b. (a -> b) -> a -> b
$ \Scope n
_ Pattern n l
pat Scope l
scope' 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
Id (Maybe VarIdent -> Value n
forall (n :: S). Maybe VarIdent -> Value n
vhole Maybe VarIdent
forall a. Maybe a
Nothing) Maybe (Value n)
forall a. Maybe a
Nothing Pattern n l
pat

-- | The binder the codomains of two Π types are compared under: the right
-- one's domain, and both shapes. The codomains need to agree only where
-- the supertype's shape holds, and the comparison of the shapes, part of
-- the same comparison, makes that the conjunction of both.
pisBinder :: TModality -> Value n -> Maybe (Value n) -> Maybe (Value n) -> Extend Context Pattern Sig n
{-# INLINE pisBinder #-}
pisBinder :: forall (n :: S).
TModality
-> Value n
-> Maybe (Value n)
-> Maybe (Value n)
-> Extend Context Pattern Sig n
pisBinder TModality
md Value n
dom Maybe (Value n)
mshapeL Maybe (Value n)
mshapeR = (forall (l :: S).
 (Eval Context Pattern Sig, Distinct n, DExt n l) =>
 Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
-> Extend Context Pattern Sig n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
(forall (l :: S).
 (Eval ctx binder sig, Distinct n, DExt n l) =>
 Scope n -> binder n l -> Scope l -> ctx n -> ctx l)
-> Extend ctx binder sig n
Extend ((forall (l :: S).
  (Eval Context Pattern Sig, Distinct n, DExt n l) =>
  Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
 -> Extend Context Pattern Sig n)
-> (forall (l :: S).
    (Eval Context Pattern Sig, Distinct n, DExt n l) =>
    Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
-> Extend Context Pattern Sig n
forall a b. (a -> b) -> a -> b
$ \Scope n
_ Pattern n l
pat Scope l
scope' Context n
ctx ->
  let ctx' :: Context l
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)
forall a. Maybe a
Nothing Pattern n l
pat
  in Scope l -> Pattern n l -> Maybe (Value n) -> Context l -> Context l
forall (n :: S) (l :: S).
(Distinct n, DExt n l) =>
Scope l -> Pattern n l -> Maybe (Value n) -> Context l -> Context l
assumeShapeAt Scope l
scope' Pattern n l
pat Maybe (Value n)
mshapeR (Scope l -> Pattern n l -> Maybe (Value n) -> Context l -> Context l
forall (n :: S) (l :: S).
(Distinct n, DExt n l) =>
Scope l -> Pattern n l -> Maybe (Value n) -> Context l -> Context l
assumeShapeAt Scope l
scope' Pattern n l
pat Maybe (Value n)
mshapeL Context l
ctx')

-- | The binder two lambdas are compared under: the right one's domain, and
-- both shapes, since the bodies agree only where both are defined.
lambdasBinder :: Maybe (Param (Value n)) -> Maybe (Param (Value n)) -> Extend Context Pattern Sig n
{-# INLINE lambdasBinder #-}
lambdasBinder :: forall (n :: S).
Maybe (Param (Value n))
-> Maybe (Param (Value n)) -> Extend Context Pattern Sig n
lambdasBinder Maybe (Param (Value n))
mpL Maybe (Param (Value n))
mpR = (forall (l :: S).
 (Eval Context Pattern Sig, Distinct n, DExt n l) =>
 Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
-> Extend Context Pattern Sig n
forall (ctx :: S -> *) (binder :: S -> S -> *) (sig :: * -> * -> *)
       (n :: S).
(forall (l :: S).
 (Eval ctx binder sig, Distinct n, DExt n l) =>
 Scope n -> binder n l -> Scope l -> ctx n -> ctx l)
-> Extend ctx binder sig n
Extend ((forall (l :: S).
  (Eval Context Pattern Sig, Distinct n, DExt n l) =>
  Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
 -> Extend Context Pattern Sig n)
-> (forall (l :: S).
    (Eval Context Pattern Sig, Distinct n, DExt n l) =>
    Scope n -> Pattern n l -> Scope l -> Context n -> Context l)
-> Extend Context Pattern Sig n
forall a b. (a -> b) -> a -> b
$ \Scope n
_ Pattern n l
pat Scope l
scope' Context n
ctx ->
  let (TModality
md, Value n
dom) = case (Maybe (Param (Value n))
mpR, Maybe (Param (Value n))
mpL) of
        (Just Param (Value n)
p, Maybe (Param (Value n))
_) -> (Param (Value n) -> TModality
forall term. Param term -> TModality
paramModality Param (Value n)
p, Param (Value n) -> Value n
forall term. Param term -> term
paramDomain Param (Value n)
p)
        (Maybe (Param (Value n))
_, Just Param (Value n)
p) -> (Param (Value n) -> TModality
forall term. Param term -> TModality
paramModality Param (Value n)
p, Param (Value n) -> Value n
forall term. Param term -> term
paramDomain Param (Value n)
p)
        (Maybe (Param (Value n)), Maybe (Param (Value n)))
_           -> (TModality
Id, Maybe VarIdent -> Value n
forall (n :: S). Maybe VarIdent -> Value n
vhole Maybe VarIdent
forall a. Maybe a
Nothing)
      ctx' :: Context l
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)
forall a. Maybe a
Nothing Pattern n l
pat
  in Scope l -> Pattern n l -> Maybe (Value n) -> Context l -> Context l
forall (n :: S) (l :: S).
(Distinct n, DExt n l) =>
Scope l -> Pattern n l -> Maybe (Value n) -> Context l -> Context l
assumeShapeAt Scope l
scope' Pattern n l
pat (Maybe (Param (Value n))
mpR Maybe (Param (Value n))
-> (Param (Value n) -> Maybe (Value n)) -> Maybe (Value n)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Param (Value n) -> Maybe (Value n)
forall term. Param term -> Maybe term
paramShape) (Scope l -> Pattern n l -> Maybe (Value n) -> Context l -> Context l
forall (n :: S) (l :: S).
(Distinct n, DExt n l) =>
Scope l -> Pattern n l -> Maybe (Value n) -> Context l -> Context l
assumeShapeAt Scope l
scope' Pattern n l
pat (Maybe (Param (Value n))
mpL Maybe (Param (Value n))
-> (Param (Value n) -> Maybe (Value n)) -> Maybe (Value n)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Param (Value n) -> Maybe (Value n)
forall term. Param term -> Maybe term
paramShape) Context l
ctx')

-- | The observer of a λ's body: the other value applied to the pattern.
appliedToPattern :: (Distinct n, Distinct l) => Scope l -> Context l -> Pattern n l -> Value l -> Value l
{-# INLINE appliedToPattern #-}
appliedToPattern :: forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Scope l -> Context l -> Pattern n l -> Value l -> Value l
appliedToPattern Scope l
scope Context l
ctx Pattern n l
pat Value l
other = 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 l
other (Pattern n l -> Value l
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Pattern n l -> Value l
patternValue Pattern n l
pat)

-- * The tope layer

-- | Assume a tope value.
assumeValue :: Distinct n => Scope n -> Context n -> Value n -> Context n
assumeValue :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Context n
assumeValue Scope n
scope Context n
ctx Value n
tope = Context n
ctx { ctxTopes = assume (topeOf scope ctx tope) (ctxTopes ctx) }

-- | Does the context entail a tope value?
entailsValue :: Distinct n => Scope n -> Context n -> Value n -> Bool
entailsValue :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Bool
entailsValue Scope n
scope Context n
ctx Value n
tope = TopeContext n -> Tope n -> Bool
forall (n :: S). TopeContext n -> Tope n -> Bool
entails (Context n -> TopeContext n
forall (n :: S). Context n -> TopeContext n
ctxTopes Context n
ctx) (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)

-- | A @recOR@ of values, simplified as evaluation simplifies one: the branch
-- whose guard the context entails, otherwise the one branch if all are
-- convertible, otherwise the @recOR@ itself.
recOrValue :: Distinct n => Scope n -> Context n -> [(Value n, Value n)] -> Value n
recOrValue :: 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)]
rs =
  case [ Value n
b | (Value n
guard, Value n
b) <- [(Value n, Value n)]
rs, Scope n -> Context n -> Value n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Bool
entailsValue Scope n
scope Context n
ctx Value n
guard ] of
    Value n
b : [Value n]
_ -> Value n
b
    [] -> case [(Value n, Value n)]
rs of
      (Value n
_, Value n
b) : [(Value n, Value n)]
rest | ((Value n, Value n) -> Bool) -> [(Value n, Value n)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Scope n -> Context n -> Value n -> Value n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Value n -> Bool
conv Scope n
scope (Variance -> Context n -> Context n
forall (n :: S). Variance -> Context n -> Context n
withVariance Variance
Invariant Context n
ctx) Value n
b (Value n -> Bool)
-> ((Value n, Value n) -> Value n) -> (Value n, Value n) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Value n, Value n) -> Value n
forall a b. (a, b) -> b
snd) [(Value n, Value n)]
rest -> Value n
b
      [(Value n, Value n)]
_ -> Sig Void (Value n) -> Value n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Maybe (Value n) -> [(Value n, Value n)] -> Sig Void (Value n)
forall scope term. Maybe term -> [(term, term)] -> Sig scope term
RecOrF (Value n -> Maybe (Value n)
forall a. a -> Maybe a
Just Value n
forall (n :: S). Value n
universe) [(Value n, Value n)]
rs)

-- | The conjunction of two tope values, with ⊥ absorbing.
topeAndValue :: Value n -> Value n -> Value n
topeAndValue :: forall (n :: S). Value n -> Value n -> Value n
topeAndValue Value n
l Value n
r = case (Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value n
l, Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value n
r) of
  (Whnf Pattern Sig n
VTopeBottom, Whnf Pattern Sig n
_) -> Value n
forall (n :: S). Value n
vtopeBottom
  (Whnf Pattern Sig n
_, Whnf Pattern Sig n
VTopeBottom) -> Value n
forall (n :: S). Value n
vtopeBottom
  (Whnf Pattern Sig n
VTopeTop, Whnf Pattern Sig n
_) -> Value n
r
  (Whnf Pattern Sig n
_, Whnf Pattern Sig n
VTopeTop) -> Value n
l
  (Whnf Pattern Sig n, Whnf Pattern Sig n)
_ -> Sig Void (Value n) -> Value n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value n -> Value n -> Sig Void (Value n)
forall scope term. term -> term -> Sig scope term
TopeAndF Value n
l Value n
r)

isTopeNode :: Sig a b -> Bool
isTopeNode :: forall a b. Sig a b -> Bool
isTopeNode = \case
  Sig a b
TopeTopF     -> Bool
True
  Sig a b
TopeBottomF  -> Bool
True
  TopeEQF{}    -> Bool
True
  TopeLEQF{}   -> Bool
True
  TopeAndF{}   -> Bool
True
  TopeOrF{}    -> Bool
True
  TopeInvF{}   -> Bool
True
  TopeUninvF{} -> Bool
True
  Sig a b
_            -> Bool
False

-- | A tope value read into the solver's syntax.
topeOf :: Distinct n => Scope n -> Context n -> Value n -> Tope n
topeOf :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf Scope n
scope Context n
ctx Value n
v = case Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value n
v of
  Whnf Pattern Sig n
VTopeTop -> Tope n
forall (n :: S). Tope n
TTop
  Whnf Pattern Sig n
VTopeBottom -> Tope n
forall (n :: S). Tope n
TBot
  VTopeEQ Value n
x Value n
y -> Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
x) (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
y)
  VTopeLEQ Value n
x Value n
y -> Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
x) (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
y)
  VTopeAnd Value n
l Value n
r -> Tope n -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n -> Tope n
TAnd (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
l) (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
r)
  VTopeOr Value n
l Value n
r -> Tope n -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n -> Tope n
TOr (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
l) (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
r)
  VTopeInv Value n
t -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n
TInv (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
t)
  VTopeUninv Value n
t -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n
TUninv (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
t)
  VTypeModal TModality
md Value n
t -> TModality -> Tope n -> Tope n
forall (n :: S). TModality -> Tope n -> Tope n
TModal TModality
md (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
t)
  VModApp TModality
md Value n
t -> TModality -> Tope n -> Tope n
forall (n :: S). TModality -> Tope n -> Tope n
TModApp TModality
md (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
t)
  VHole Maybe VarIdent
m -> Maybe VarIdent -> Tope n
forall (n :: S). Maybe VarIdent -> Tope n
THole Maybe VarIdent
m
  Whnf Pattern Sig n
_ -> Point n -> Tope n
forall (n :: S). Point n -> Tope n
TNeutral (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
v)

-- | A point value read into the solver's syntax. A variable carries its
-- cube; a defined point is unfolded.
pointOf :: Distinct n => Scope n -> Context n -> Value n -> Point n
pointOf :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
v = case Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value n
v of
  NbE.VVar Name n
x -> Name n -> Cube -> Point n
forall (n :: S). Name n -> Cube -> Point n
PVar Name n
x (Value n -> Cube
forall (n :: S). Value n -> Cube
cubeOfType (Entry n -> Value n
forall (n :: S). Entry n -> Value n
entryType (Name n -> Context n -> Entry n
forall (n :: S). Name n -> Context n -> Entry n
lookupEntry Name n
x Context n
ctx)))
  VAnn Value n
n Value n
_ -> Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
n
  Whnf Pattern Sig n
VCubeUnitStar -> Point n
forall (n :: S). Point n
PStar
  Whnf Pattern Sig n
VCube2_0 -> Point n
forall (n :: S). Point n
P2_0
  Whnf Pattern Sig n
VCube2_1 -> Point n
forall (n :: S). Point n
P2_1
  Whnf Pattern Sig n
VCubeI_0 -> Point n
forall (n :: S). Point n
PI_0
  Whnf Pattern Sig n
VCubeI_1 -> Point n
forall (n :: S). Point n
PI_1
  VPair Value n
a Value n
b -> Point n -> Point n -> Point n
forall (n :: S). Point n -> Point n -> Point n
PPair (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
a) (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
b)
  VFirst Value n
p -> Point n -> Point n
forall (n :: S). Point n -> Point n
PFst (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
p)
  VSecond Value n
p -> Point n -> Point n
forall (n :: S). Point n -> Point n
PSnd (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
p)
  VCubeSup Value n
l Value n
r -> Point n -> Point n -> Point n
forall (n :: S). Point n -> Point n -> Point n
PSup (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
l) (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
r)
  VCubeInf Value n
l Value n
r -> Point n -> Point n -> Point n
forall (n :: S). Point n -> Point n -> Point n
PInf (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
l) (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
r)
  VCubeFlip Value n
p -> Point n -> Point n
forall (n :: S). Point n -> Point n
PFlip (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
p)
  VCubeUnflip Value n
p -> Point n -> Point n
forall (n :: S). Point n -> Point n
PUnflip (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
p)
  VModApp TModality
md Value n
p -> TModality -> Point n -> Point n
forall (n :: S). TModality -> Point n -> Point n
PModApp TModality
md (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
p)
  VModExtract TModality
app TModality
inn Value n
p -> TModality -> TModality -> Point n -> Point n
forall (n :: S). TModality -> TModality -> Point n -> Point n
PModExtract TModality
app TModality
inn (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
p)
  VHole Maybe VarIdent
m -> Maybe VarIdent -> Point n
forall (n :: S). Maybe VarIdent -> Point n
PHole Maybe VarIdent
m
  VApp Value n
f Value n
a ->
    Cube -> Point n -> Point n -> Point n
forall (n :: S). Cube -> Point n -> Point n -> Point n
PApp (Cube -> (Value n -> Cube) -> Maybe (Value n) -> Cube
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Cube
KOther Value n -> Cube
forall (n :: S). Value n -> Cube
cubeOfType (Scope n -> Context n -> Value n -> Maybe (Value n)
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Maybe (Value n)
neutralType Scope n
scope Context n
ctx Value n
v)) (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
f) (Scope n -> Context n -> Value n -> Point n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Point n
pointOf Scope n
scope Context n
ctx Value n
a)
  Whnf Pattern Sig n
_ -> String -> Point n
forall (n :: S). String -> Point n
POpaque (Core n -> String
forall (n :: S). Distinct n => Core n -> String
fingerprint (Scope n -> Context n -> Value n -> Core n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Core n
quoteIn Scope n
scope Context n
ctx Value n
v))

-- | A cube value, as far as the solver tells cubes apart.
cubeOfType :: Value n -> Cube
cubeOfType :: forall (n :: S). Value n -> Cube
cubeOfType Value n
v = case Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force (Value n -> Value n
forall (n :: S). Value n -> Value n
stripRestrictions Value n
v) of
  Whnf Pattern Sig n
VCubeUnit -> Cube
KUnit
  Whnf Pattern Sig n
VCube2 -> Cube
K2
  Whnf Pattern Sig n
VCubeI -> Cube
KI
  VCubeProduct Value n
a Value n
b -> Cube -> Cube -> Cube
KProd (Value n -> Cube
forall (n :: S). Value n -> Cube
cubeOfType Value n
a) (Value n -> Cube
forall (n :: S). Value n -> Cube
cubeOfType Value n
b)
  Whnf Pattern Sig n
_ -> Cube
KOther

-- | A term as a string that equal terms share: the constructors and the
-- names.
fingerprint :: Distinct n => Core n -> String
fingerprint :: forall (n :: S). Distinct n => Core n -> String
fingerprint (Var Name n
x) = String
"v" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show (Name n -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name n
x)
fingerprint (Node Sig (ScopedAST Pattern Sig n) (AST Pattern Sig n)
sig) =
  Sig () () -> String
forall a. Show a => a -> String
show ((ScopedAST Pattern Sig n -> ())
-> (AST Pattern Sig n -> ())
-> Sig (ScopedAST Pattern Sig n) (AST Pattern Sig n)
-> Sig () ()
forall a b c d. (a -> b) -> (c -> d) -> Sig a c -> Sig b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (() -> ScopedAST Pattern Sig n -> ()
forall a b. a -> b -> a
const ()) (() -> AST Pattern Sig n -> ()
forall a b. a -> b -> a
const ()) Sig (ScopedAST Pattern Sig n) (AST Pattern Sig n)
sig)
    String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"(" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"," ((ScopedAST Pattern Sig n -> [String])
-> (AST Pattern Sig n -> [String])
-> Sig (ScopedAST Pattern Sig n) (AST Pattern Sig n)
-> [String]
forall m a b. Monoid m => (a -> m) -> (b -> m) -> Sig a b -> m
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap ScopedAST Pattern Sig n -> [String]
forall (n' :: S). Distinct n' => ScopedCore n' -> [String]
scoped (String -> [String]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> [String])
-> (AST Pattern Sig n -> String) -> AST Pattern Sig n -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AST Pattern Sig n -> String
forall (n :: S). Distinct n => Core n -> String
fingerprint) Sig (ScopedAST Pattern Sig n) (AST Pattern Sig n)
sig) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")"
  where
    scoped :: Distinct n' => ScopedCore n' -> [String]
    scoped :: forall (n' :: S). Distinct n' => ScopedCore n' -> [String]
scoped (ScopedAST Pattern n' l
pat AST Pattern Sig 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 of
      DistinctEvidence l
Foil.Distinct ->
        [ String
"\\" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"," [ Int -> String
forall a. Show a => a -> String
show (Name l -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name l
x) | (Maybe VarIdent
_, Name l
x) <- 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 ] String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"." String -> ShowS
forall a. Semigroup a => a -> a -> a
<> AST Pattern Sig l -> String
forall (n :: S). Distinct n => Core n -> String
fingerprint AST Pattern Sig l
body ]

-- * Types of neutrals

-- | The type of a neutral, read off its head and its eliminations: an
-- application has the codomain of its function's type at the argument, a
-- projection the component of the pair's Σ type, a path induction the
-- motive at its endpoints. A glued head keeps its declared type. An
-- elimination of a neutral whose type is restricted is restricted by the
-- same elimination of the terms: under @ϕ@, a neutral @f : (X → A) [ϕ ↦ g]@
-- is @g@, so @f a : A [ϕ ↦ g a]@. A neutral built by evaluation carries its
-- type ('AnnF'), so each step reads only the type of the neutral it
-- eliminates.
neutralType :: Distinct n => Scope n -> Context n -> Value n -> Maybe (Value n)
neutralType :: forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Maybe (Value n)
neutralType Scope n
scope Context n
ctx Value n
v = case Value n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.forceFolded Value n
v of
  NbE.VVar Name n
x -> Value n -> Maybe (Value n)
forall a. a -> Maybe a
Just (Entry n -> Value n
forall (n :: S). Entry n -> Value n
entryType (Name n -> Context n -> Entry n
forall (n :: S). Name n -> Context n -> Entry n
lookupEntry Name n
x Context n
ctx))
  VAnn Value n
n Value n
ty -> Value n -> Maybe (Value n)
forall (n :: S). Value n -> Maybe (Value n)
annotationType Value n
ty Maybe (Value n) -> Maybe (Value n) -> Maybe (Value n)
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Scope n -> Context n -> Value n -> Maybe (Value n)
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Maybe (Value n)
neutralType Scope n
scope Context n
ctx Value n
n
  VRecOr Value n
ty [(Value n, Value n)]
_ -> Value n -> Maybe (Value n)
forall a. a -> Maybe a
Just Value n
ty
  VApp Value n
f Value n
a -> do
    fTy <- Scope n -> Context n -> Value n -> Maybe (Value n)
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Maybe (Value n)
neutralType Scope n
scope Context n
ctx Value n
f
    case familyOf fTy of
      Just Family n
_ -> Value n -> Maybe (Value n)
forall a. a -> Maybe a
Just Value n
forall (n :: S). Value n
topeUniverse
      Maybe (Family n)
Nothing -> do
        Pi env _ _ _ ret <- Value n -> Maybe (Pi n)
forall (n :: S). Value n -> Maybe (Pi n)
piOf Value n
fTy
        Just (inherit fTy (\Value n
g -> 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
g Value n
a) (codomainAt scope ctx env ret a))
  VFirst Value n
p -> do
    ty <- Scope n -> Context n -> Value n -> Maybe (Value n)
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Maybe (Value n)
neutralType Scope n
scope Context n
ctx Value n
p
    inherit ty (firstValueIn scope ctx) <$> case NbE.force (stripRestrictions ty) of
      VTypeSigma Env i n
_ TModality
_ Value n
a ScopedCore i
_ -> Value n -> Maybe (Value n)
forall a. a -> Maybe a
Just Value n
a
      VCubeProduct Value n
a Value n
_ -> Value n -> Maybe (Value n)
forall a. a -> Maybe a
Just Value n
a
      Whnf Pattern Sig n
_ -> Maybe (Value n)
forall a. Maybe a
Nothing
  VSecond Value n
p -> do
    ty <- Scope n -> Context n -> Value n -> Maybe (Value n)
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Maybe (Value n)
neutralType Scope n
scope Context n
ctx Value n
p
    inherit ty (secondValueIn scope ctx) <$> case NbE.force (stripRestrictions ty) of
      VTypeSigma Env i n
env TModality
_ Value n
_ ScopedCore i
b -> Value n -> Maybe (Value n)
forall a. a -> Maybe a
Just (Scope n
-> Context n -> Env i n -> ScopedCore i -> 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
codomainAt Scope n
scope Context n
ctx Env i n
env ScopedCore i
b (Value n -> Value n
forall (n :: S). Value n -> Value n
firstValue Value n
p))
      VCubeProduct Value n
_ Value n
b -> Value n -> Maybe (Value n)
forall a. a -> Maybe a
Just Value n
b
      Whnf Pattern Sig n
_ -> Maybe (Value n)
forall a. Maybe a
Nothing
  VIdJ Value n
tA Value n
a Value n
tC Value n
d Value n
x Value n
p ->
    let motive :: Value n
motive = 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
tC Value n
x) Value n
p
    in Value n -> Maybe (Value n)
forall a. a -> Maybe a
Just (Value n -> (Value n -> Value n) -> Maybe (Value n) -> Value n
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Value n
motive (\Value n
ty -> Value n -> (Value n -> Value n) -> Value n -> Value n
forall {n :: S}.
Value n -> (Value n -> Value n) -> Value n -> Value n
inherit Value n
ty (Scope n
-> Context n
-> Value n
-> Value n
-> Value n
-> Value n
-> Value n
-> Value n
-> Value n
forall (n :: S).
Distinct n =>
Scope n
-> Context n
-> Value n
-> Value n
-> Value n
-> Value n
-> Value n
-> Value n
-> Value n
idJValueIn Scope n
scope Context n
ctx Value n
tA Value n
a Value n
tC Value n
d Value n
x) Value n
motive) (Scope n -> Context n -> Value n -> Maybe (Value n)
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Maybe (Value n)
neutralType Scope n
scope Context n
ctx Value n
p))
  Whnf Pattern Sig n
_ -> Maybe (Value n)
forall a. Maybe a
Nothing
  where
    inherit :: Value n -> (Value n -> Value n) -> Value n -> Value n
inherit Value n
from Value n -> Value n
eliminate Value n
ty = case Value n -> [(Value n, Value n)]
forall (n :: S). Value n -> [(Value n, Value n)]
restrictionsOf Value n
from of
      [] -> Value n
ty
      [(Value n, Value n)]
rs -> Sig Void (Value n) -> Value n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig Void (Value binder sig n) -> Value binder sig n
NbE.vnode (Value n -> [(Value n, Value n)] -> Sig Void (Value n)
forall scope term. term -> [(term, term)] -> Sig scope term
TypeRestrictedF Value n
ty [ (Value n
tope, Value n -> Value n
eliminate Value n
t) | (Value n
tope, Value n
t) <- [(Value n, Value n)]
rs ])