{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE LambdaCase          #-}
{-# LANGUAGE RankNTypes          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE PatternSynonyms     #-}
{-# LANGUAGE ViewPatterns        #-}
-- | Subtyping and conversion, in the checker's monad.
--
-- The library's conversion decides the common case: two values compared
-- in the context's variance. Where it answers no, the checker explains the
-- failure on the heads, with the term whose type is compared where a
-- restriction needs it: a term of an unrestricted type stands at a
-- restricted one when it agrees with each face, and a function does so
-- when its applications do. There is no unification: a mismatch is an
-- error.
module Rzk.Check.Conversion
  ( subtype
  , subtypeValues
  , convertTerms
  , convertTypes
  , holesTolerated
  ) where

import           Control.Monad            (unless, when)
import           Control.Monad.Foil       (Distinct)
import qualified Control.Monad.Foil       as Foil
import           Control.Monad.Free.Foil  (AST (..))
import qualified FreeFoil.NbE             as NbE

import           Language.Rzk.Foil.Names  (TModality (..))
import           Rzk.Check.Context
import           Rzk.Check.Error
import           Rzk.Check.Monad
import           Rzk.Check.Syntax
import           Rzk.Check.Topes
import           Rzk.Check.Value

-- | Check that the actual type of a term is a subtype of the expected type.
-- The arguments are the term, its expected type, and its actual inferred type.
subtype :: Distinct n => Core n -> Value n -> Value n -> Check n ()
subtype :: forall (n :: S).
Distinct n =>
Core n -> Value n -> Value n -> Check n ()
subtype Core n
term Value n
expected Value n
actual = Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
forall (n :: S).
Distinct n =>
Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
compareWith (Core n -> Maybe (Core n)
forall a. a -> Maybe a
Just Core n
term) Variance
Covariant Value n
expected Value n
actual

-- | Check that the second type (actual) is a subtype of the first (expected),
-- with no term at hand.
subtypeValues :: Distinct n => Value n -> Value n -> Check n ()
subtypeValues :: forall (n :: S). Distinct n => Value n -> Value n -> Check n ()
subtypeValues = Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
forall (n :: S).
Distinct n =>
Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
compareWith Maybe (Core n)
forall a. Maybe a
Nothing Variance
Covariant

-- | Two types are convertible.
convertTypes :: Distinct n => Value n -> Value n -> Check n ()
convertTypes :: forall (n :: S). Distinct n => Value n -> Value n -> Check n ()
convertTypes = Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
forall (n :: S).
Distinct n =>
Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
compareWith Maybe (Core n)
forall a. Maybe a
Nothing Variance
Invariant

-- | Two terms are convertible.
convertTerms :: Distinct n => Value n -> Value n -> Check n ()
convertTerms :: forall (n :: S). Distinct n => Value n -> Value n -> Check n ()
convertTerms = Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
forall (n :: S).
Distinct n =>
Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
compareWith Maybe (Core n)
forall a. Maybe a
Nothing Variance
Invariant

-- | In a lenient hole mode, a term that still carries a hole is work in
-- progress, and a mismatch around it is tolerated.
holesTolerated :: Core n -> Check n Bool
holesTolerated :: forall (n :: S). Core n -> Check n Bool
holesTolerated Core n
term = (Context n -> Bool) -> Check n Bool
forall (n :: S) a. (Context n -> a) -> Check n a
asksContext ((Context n -> Bool) -> Check n Bool)
-> (Context n -> Bool) -> Check n Bool
forall a b. (a -> b) -> a -> b
$ \Context n
ctx ->
  Options -> HoleMode
optHoles (Context n -> Options
forall (n :: S). Context n -> Options
ctxOptions Context n
ctx) HoleMode -> HoleMode -> Bool
forall a. Eq a => a -> a -> Bool
== HoleMode
HolesDeferred Bool -> Bool -> Bool
&& Core n -> Bool
forall (n :: S). Core n -> Bool
containsHole Core n
term

-- | Compare expected with actual in the given variance. The optional core term
-- is the actual term whose type is being compared; it is needed when checking
-- that an unrestricted term satisfies the faces of a restricted expected type.
compareWith
  :: forall n. (Distinct n)
  => Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
compareWith :: forall (n :: S).
Distinct n =>
Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
compareWith Maybe (Core n)
mterm Variance
variance Value n
expected Value n
actual =
  Action n -> Check n () -> Check n ()
forall (n :: S) a. Distinct n => Action n -> Check n a -> Check n a
performing (Value n -> Value n -> Action n
forall (n :: S). Value n -> Value n -> Action n
ActionConvert Value n
expected Value n
actual) (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ do
    ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
    let topes = Context n -> TopeContext n
forall (n :: S). Context n -> TopeContext n
ctxTopes Context n
ctx
        scope = Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx
        ctx' = Variance -> Context n -> Context n
forall (n :: S). Variance -> Context n -> Context n
withVariance Variance
variance Context n
ctx
        agrees Context n
c = 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
variance Context n
c) Value n
expected Value n
actual
    unless (entailsBottom topes || agrees ctx' || inAlternatives topes (agrees . withTopes ctx')) $
      explain mterm variance expected actual
  where
    withTopes :: Context n -> TopeContext n -> Context n
withTopes Context n
c TopeContext n
t = Context n
c { ctxTopes = t }

-- | Explain a failed value comparison, and perform the checks that need more
-- than the library's Boolean answer. In particular, an unrestricted actual term
-- at a restricted expected type must agree with each face, which requires the
-- actual term. Fresh-domain subtyping and the remaining cases produce more
-- specific diagnostics. This is not unification.
explain
  :: forall n. (Distinct n)
  => Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
explain :: forall (n :: S).
Distinct n =>
Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
explain Maybe (Core n)
mterm Variance
variance Value n
expected Value n
actual = do
  ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
  let scope = Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx
  tolerated <- maybe (pure False) holesTolerated mterm
  if tolerated then pure () else case (force expected, force actual) of
    -- both restricted: the underlying types, then the faces of the
    -- supertype covered by those of the subtype, agreeing on overlaps
    (VTypeRestricted Value n
tyE [(Value n, Value n)]
rsE, VTypeRestricted Value n
tyA [(Value n, Value n)]
rsA) -> do
      Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
forall (n :: S).
Distinct n =>
Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
compareWith Maybe (Core n)
mterm Variance
variance Value n
tyE Value n
tyA
      let ([(Value n, Value n)]
superRs, [(Value n, Value n)]
subRs) = case Variance
variance of
            Variance
Contravariant -> ([(Value n, Value n)]
rsA, [(Value n, Value n)]
rsE)
            Variance
_             -> ([(Value n, Value n)]
rsE, [(Value n, Value n)]
rsA)
      [Check n ()] -> Check n ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_
        [ Tope n -> Check n () -> Check n ()
forall (n :: S) a. Tope n -> Check n a -> Check n a
underTope Tope n
superT (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ do
            ctx' <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
            let subTopes = ((Value n, Value n) -> Tope n) -> [(Value n, Value n)] -> [Tope n]
forall a b. (a -> b) -> [a] -> [b]
map (Scope n -> Context n -> Value n -> Tope n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf Scope n
scope Context n
ctx' (Value n -> Tope n)
-> ((Value n, Value n) -> Value n) -> (Value n, Value n) -> Tope n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Value n, Value n) -> Value n
forall a b. (a, b) -> a
fst) [(Value n, Value n)]
subRs
            unless (entailsUnion (ctxTopes ctx') subTopes) $
              issueTypeError (TypeErrorTopeNotSatisfied (accessibleTopes (ctxTopes ctx')) (foldr topeOr TBot subTopes))
            sequence_
              [ underTope (topeOf scope ctx' subTope) $ convertTerms superTerm subTerm
              | (subTope, subTerm) <- subRs ]
        | (Value n
superTope, Value n
superTerm) <- [(Value n, Value n)]
superRs
        , let superT :: Tope n
superT = 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
superTope ]
      Bool -> Check n () -> Check n ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Variance
variance Variance -> Variance -> Bool
forall a. Eq a => a -> a -> Bool
== Variance
Invariant) (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$
        Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
forall (n :: S).
Distinct n =>
Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
compareWith Maybe (Core n)
mterm Variance
Covariant Value n
actual Value n
expected
    -- the restricted type is the subtype of its underlying type
    (Whnf n
_, VTypeRestricted Value n
tyA [(Value n, Value n)]
_) | Variance
variance Variance -> Variance -> Bool
forall a. Eq a => a -> a -> Bool
== Variance
Covariant ->
      Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
forall (n :: S).
Distinct n =>
Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
compareWith Maybe (Core n)
mterm Variance
variance Value n
expected Value n
tyA
    (VTypeRestricted Value n
tyE [(Value n, Value n)]
_, Whnf n
_) | Variance
variance Variance -> Variance -> Bool
forall a. Eq a => a -> a -> Bool
== Variance
Contravariant ->
      Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
forall (n :: S).
Distinct n =>
Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
compareWith Maybe (Core n)
mterm Variance
variance Value n
tyE Value n
actual
    -- a term of an unrestricted type stands at a restricted one when it
    -- agrees with each face
    (VTypeRestricted Value n
tyE [(Value n, Value n)]
rsE, Whnf n
_) | Just Core n
term <- Maybe (Core n)
mterm, Variance
variance Variance -> Variance -> Bool
forall a. Eq a => a -> a -> Bool
== Variance
Covariant -> do
      Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
forall (n :: S).
Distinct n =>
Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
compareWith Maybe (Core n)
mterm Variance
variance Value n
tyE Value n
actual
      let termV :: Value n
termV = 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 Core n
term
      [Check n ()] -> Check n ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_
        [ Tope n -> Check n () -> Check n ()
forall (n :: S) a. Tope n -> Check n a -> Check n a
underTope (Scope n -> Context n -> Value n -> Tope n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf Scope n
scope Context n
ctx Value n
tope) (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ Value n -> Value n -> Check n ()
forall (n :: S). Distinct n => Value n -> Value n -> Check n ()
convertTerms Value n
face Value n
termV
        | (Value n
tope, Value n
face) <- [(Value n, Value n)]
rsE ]
    -- two family types: the cubes, then the shapes, covariantly and on
    -- their own
    ((VTypeShape TModality
md Value n
cubeE Maybe (Value n)
mshapeE), (VTypeShape TModality
md' Value n
cubeA Maybe (Value n)
mshapeA)) | TModality
md TModality -> TModality -> Bool
forall a. Eq a => a -> a -> Bool
== TModality
md' -> do
      Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
forall (n :: S).
Distinct n =>
Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
compareWith Maybe (Core n)
forall a. Maybe a
Nothing (Variance -> Variance
opposite Variance
variance) Value n
cubeE Value n
cubeA
      case (Maybe (Value n)
mshapeE, Maybe (Value n)
mshapeA) of
        (Maybe (Value n)
Nothing, Maybe (Value n)
Nothing) -> () -> Check n ()
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
        (Just Value n
sE, Just Value n
sA) -> Scope n
-> Context n
-> TModality
-> Value n
-> Maybe (Value n)
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Scope l -> Context l -> Name l -> Check n ())
-> Check n ()
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
cubeA Maybe (Value n)
forall a. Maybe a
Nothing ((forall (l :: S).
  (DExt n l, Distinct l) =>
  Scope l -> Context l -> Name l -> Check n ())
 -> Check n ())
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Scope l -> Context l -> Name l -> Check n ())
-> Check n ()
forall a b. (a -> b) -> a -> b
$ \Scope l
scope' Context l
ctx' Name l
name -> do
          let x :: Value Pattern Sig l
x = Name l -> Value Pattern Sig l
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> Value binder sig n
NbE.vvar Name l
name
              tE :: Tope l
tE = Scope l -> Context l -> Value Pattern Sig l -> Tope l
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf Scope l
scope' Context l
ctx' (Scope l
-> Context l
-> Value Pattern Sig l
-> Value Pattern Sig l
-> Value Pattern Sig 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 Pattern Sig l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Value n
sE) Value Pattern Sig l
x)
              tA :: Tope l
tA = Scope l -> Context l -> Value Pattern Sig l -> Tope l
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf Scope l
scope' Context l
ctx' (Scope l
-> Context l
-> Value Pattern Sig l
-> Value Pattern Sig l
-> Value Pattern Sig 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 Pattern Sig l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Value n
sA) Value Pattern Sig l
x)
              topes :: TopeContext l
topes = Context l -> TopeContext l
forall (n :: S). Context n -> TopeContext n
ctxTopes Context l
ctx'
              holds :: Bool
holds = case Variance
variance of
                Variance
Covariant     -> TopeContext l -> [Tope l] -> Tope l -> Bool
forall (n :: S). TopeContext n -> [Tope n] -> Tope n -> Bool
topesEntail TopeContext l
topes [Tope l
tA] Tope l
tE
                Variance
Contravariant -> TopeContext l -> [Tope l] -> Tope l -> Bool
forall (n :: S). TopeContext n -> [Tope n] -> Tope n -> Bool
topesEntail TopeContext l
topes [Tope l
tE] Tope l
tA
                Variance
Invariant     -> TopeContext l -> Tope l -> Tope l -> Bool
forall (n :: S). TopeContext n -> Tope n -> Tope n -> Bool
equivalent TopeContext l
topes Tope l
tE Tope l
tA
              (Tope l
super, Tope l
sub) = case Variance
variance of
                Variance
Contravariant -> (Tope l
tE, Tope l
tA)
                Variance
_             -> (Tope l
tA, Tope l
tE)
          Bool -> Check n () -> Check n ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
holds (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ Context l -> Check l () -> Check n ()
forall (l :: S) a (n :: S). Context l -> Check l a -> Check n a
inContext Context l
ctx' (Check l () -> Check n ()) -> Check l () -> Check n ()
forall a b. (a -> b) -> a -> b
$
            TypeError l -> Check l ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError ([Tope l] -> Tope l -> TypeError l
forall (n :: S). [Tope n] -> Tope n -> TypeError n
TypeErrorTopeNotSatisfied [Tope l
super] Tope l
sub)
        (Maybe (Value n), Maybe (Value n))
_ -> Check n ()
mismatch
    -- a function is a subtype of another when its applications are
    (VPi (Pi Env i n
envE TModality
md Value n
domE Maybe (Value n)
mshapeE ScopedCore i
retE), VPi (Pi Env i n
envA TModality
md' Value n
domA Maybe (Value n)
mshapeA ScopedCore i
retA)) | TModality
md TModality -> TModality -> Bool
forall a. Eq a => a -> a -> Bool
== TModality
md' -> do
      Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
forall (n :: S).
Distinct n =>
Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
compareWith Maybe (Core n)
forall a. Maybe a
Nothing (Variance -> Variance
opposite Variance
variance) Value n
domE Value n
domA
      Scope n
-> Context n
-> TModality
-> Value n
-> Maybe (Value n)
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Scope l -> Context l -> Name l -> Check n ())
-> Check n ()
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
domA Maybe (Value n)
forall a. Maybe a
Nothing ((forall (l :: S).
  (DExt n l, Distinct l) =>
  Scope l -> Context l -> Name l -> Check n ())
 -> Check n ())
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Scope l -> Context l -> Name l -> Check n ())
-> Check n ()
forall a b. (a -> b) -> a -> b
$ \Scope l
scope' Context l
ctx' Name l
name -> do
        let x :: Value Pattern Sig l
x = Name l -> Value Pattern Sig l
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> Value binder sig n
NbE.vvar Name l
name
            codE :: Value Pattern Sig l
codE = Scope l
-> Context l
-> Env i l
-> ScopedCore i
-> Value Pattern Sig l
-> Value Pattern Sig l
forall (n :: S) (i :: S).
(Distinct n, Distinct i) =>
Scope n
-> Context n -> Env i n -> ScopedCore i -> Value n -> Value n
codomainAt Scope l
scope' Context l
ctx' (Env i n -> Env i l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Env i n
envE) ScopedCore i
retE Value Pattern Sig l
x
            codA :: Value Pattern Sig l
codA = Scope l
-> Context l
-> Env i l
-> ScopedCore i
-> Value Pattern Sig l
-> Value Pattern Sig l
forall (n :: S) (i :: S).
(Distinct n, Distinct i) =>
Scope n
-> Context n -> Env i n -> ScopedCore i -> Value n -> Value n
codomainAt Scope l
scope' Context l
ctx' (Env i n -> Env i l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Env i n
envA) ScopedCore i
retA Value Pattern Sig l
x
            mterm' :: Maybe (Core l)
mterm' = (Core n -> Core l) -> Maybe (Core n) -> Maybe (Core l)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Core n
term -> Core l -> Core l -> Core l
forall (n :: S). Core n -> Core n -> Core n
App (Core n -> Core l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Core n
term) (Name l -> Core l
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var Name l
name)) Maybe (Core n)
mterm
        Context l -> Check l () -> Check n ()
forall (l :: S) a (n :: S). Context l -> Check l a -> Check n a
inContext Context l
ctx' (Check l () -> Check n ()) -> Check l () -> Check n ()
forall a b. (a -> b) -> a -> b
$ do
          -- the shapes: the expected one entails the actual one, as the
          -- domain is contravariant
          case (Maybe (Value n)
mshapeE, Maybe (Value n)
mshapeA) of
            (Maybe (Value n)
Nothing, Maybe (Value n)
Nothing) -> () -> Check l ()
forall a. a -> Check l a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
            (Just Value n
sE, Just Value n
sA) -> do
              let tE :: Tope l
tE = Scope l -> Context l -> Value Pattern Sig l -> Tope l
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf Scope l
scope' Context l
ctx' (Scope l
-> Context l
-> Value Pattern Sig l
-> Value Pattern Sig l
-> Value Pattern Sig 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 Pattern Sig l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Value n
sE) Value Pattern Sig l
x)
                  tA :: Tope l
tA = Scope l -> Context l -> Value Pattern Sig l -> Tope l
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Tope n
topeOf Scope l
scope' Context l
ctx' (Scope l
-> Context l
-> Value Pattern Sig l
-> Value Pattern Sig l
-> Value Pattern Sig 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 Pattern Sig l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Value n
sA) Value Pattern Sig l
x)
                  topes :: TopeContext l
topes = Context l -> TopeContext l
forall (n :: S). Context n -> TopeContext n
ctxTopes Context l
ctx'
                  holds :: Bool
holds = case Variance
variance of
                    Variance
Covariant     -> TopeContext l -> Tope l -> Bool
forall (n :: S). TopeContext n -> Tope n -> Bool
entails (Tope l -> TopeContext l -> TopeContext l
forall (n :: S). Tope n -> TopeContext n -> TopeContext n
assume Tope l
tE TopeContext l
topes) Tope l
tA
                    Variance
Contravariant -> TopeContext l -> Tope l -> Bool
forall (n :: S). TopeContext n -> Tope n -> Bool
entails (Tope l -> TopeContext l -> TopeContext l
forall (n :: S). Tope n -> TopeContext n -> TopeContext n
assume Tope l
tA TopeContext l
topes) Tope l
tE
                    Variance
Invariant     -> TopeContext l -> Tope l -> Bool
forall (n :: S). TopeContext n -> Tope n -> Bool
entails (Tope l -> TopeContext l -> TopeContext l
forall (n :: S). Tope n -> TopeContext n -> TopeContext n
assume Tope l
tE TopeContext l
topes) Tope l
tA Bool -> Bool -> Bool
&& TopeContext l -> Tope l -> Bool
forall (n :: S). TopeContext n -> Tope n -> Bool
entails (Tope l -> TopeContext l -> TopeContext l
forall (n :: S). Tope n -> TopeContext n -> TopeContext n
assume Tope l
tA TopeContext l
topes) Tope l
tE
                  (Tope l
super, Tope l
sub) = case Variance
variance of
                    Variance
Contravariant -> (Tope l
tA, Tope l
tE)
                    Variance
_             -> (Tope l
tE, Tope l
tA)
              Bool -> Check l () -> Check l ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
holds (Check l () -> Check l ()) -> Check l () -> Check l ()
forall a b. (a -> b) -> a -> b
$
                TypeError l -> Check l ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError ([Tope l] -> Tope l -> TypeError l
forall (n :: S). [Tope n] -> Tope n -> TypeError n
TypeErrorTopeNotSatisfied (TopeContext l -> [Tope l]
forall (n :: S). TopeContext n -> [Tope n]
accessibleTopes (Tope l -> TopeContext l -> TopeContext l
forall (n :: S). Tope n -> TopeContext n -> TopeContext n
assume Tope l
super TopeContext l
topes)) Tope l
sub)
            (Maybe (Value n), Maybe (Value n))
_ -> Context n -> Check n () -> Check l ()
forall (l :: S) a (n :: S). Context l -> Check l a -> Check n a
inContext Context n
ctx Check n ()
mismatch
          Maybe (Core l)
-> Variance
-> Value Pattern Sig l
-> Value Pattern Sig l
-> Check l ()
forall (n :: S).
Distinct n =>
Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
compareWith Maybe (Core l)
mterm' Variance
variance Value Pattern Sig l
codE Value Pattern Sig l
codA
    -- two lambdas: their bodies under a fresh variable of the actual one's
    -- domain, where both shapes hold, so that the mismatch is reported
    -- where it is
    (VLambda Env i n
envE Maybe (Param (Value n))
mpE ScopedCore i
bodyE, VLambda Env i n
envA Maybe (Param (Value n))
mpA ScopedCore i
bodyA) -> do
      let (TModality
md, Value n
dom) = case (Maybe (Param (Value n))
mpA, Maybe (Param (Value n))
mpE) 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)
      Scope n
-> Context n
-> TModality
-> Value n
-> Maybe (Value n)
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Scope l -> Context l -> Name l -> Check n ())
-> Check n ()
forall (n :: S) r.
Distinct n =>
Scope n
-> Context n
-> TModality
-> Value n
-> Maybe (Value n)
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Scope l -> Context l -> Name l -> r)
-> r
underFreshVar Scope n
scope Context n
ctx TModality
md Value n
dom (Maybe (Param (Value n))
mpA 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) ((forall (l :: S).
  (DExt n l, Distinct l) =>
  Scope l -> Context l -> Name l -> Check n ())
 -> Check n ())
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Scope l -> Context l -> Name l -> Check n ())
-> Check n ()
forall a b. (a -> b) -> a -> b
$ \Scope l
scope' Context l
ctx' Name l
name -> do
        let x :: Value Pattern Sig l
x = Name l -> Value Pattern Sig l
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> Value binder sig n
NbE.vvar Name l
name
            ctx'' :: Context l
ctx'' = case Maybe (Param (Value n))
mpE 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 of
              Maybe (Value n)
Nothing    -> Context l
ctx'
              Just Value n
shape -> Scope l -> Context l -> Value Pattern Sig 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 Pattern Sig l
-> Value Pattern Sig l
-> Value Pattern Sig 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 Pattern Sig l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Value n
shape) Value Pattern Sig l
x)
        Context l -> Check l () -> Check n ()
forall (l :: S) a (n :: S). Context l -> Check l a -> Check n a
inContext Context l
ctx'' (Check l () -> Check n ()) -> Check l () -> Check n ()
forall a b. (a -> b) -> a -> b
$
          Maybe (Core l)
-> Variance
-> Value Pattern Sig l
-> Value Pattern Sig l
-> Check l ()
forall (n :: S).
Distinct n =>
Maybe (Core n) -> Variance -> Value n -> Value n -> Check n ()
compareWith Maybe (Core l)
forall a. Maybe a
Nothing Variance
variance
            (Scope l
-> Context l
-> Env i l
-> ScopedCore i
-> Value Pattern Sig l
-> Value Pattern Sig 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 n -> Env i l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Env i n
envE) ScopedCore i
bodyE Value Pattern Sig l
x)
            (Scope l
-> Context l
-> Env i l
-> ScopedCore i
-> Value Pattern Sig l
-> Value Pattern Sig 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 n -> Env i l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Env i n
envA) ScopedCore i
bodyA Value Pattern Sig l
x)
    -- identity types: the carriers, then the endpoints
    ((VTypeId Value n
xE Maybe (Value n)
mtE Value n
yE), (VTypeId Value n
xA Maybe (Value n)
mtA Value n
yA)) -> do
      case (Maybe (Value n)
mtE, Maybe (Value n)
mtA) of
        (Just Value n
tE, Just Value n
tA) -> Value n -> Value n -> Check n ()
forall (n :: S). Distinct n => Value n -> Value n -> Check n ()
convertTypes Value n
tE Value n
tA
        (Maybe (Value n), Maybe (Value n))
_ -> () -> Check n ()
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      Value n -> Value n -> Check n ()
forall (n :: S). Distinct n => Value n -> Value n -> Check n ()
convertTerms Value n
xE Value n
xA
      Value n -> Value n -> Check n ()
forall (n :: S). Distinct n => Value n -> Value n -> Check n ()
convertTerms Value n
yE Value n
yA
    (Whnf n, Whnf n)
_ -> Check n ()
mismatch
  where
    mismatch :: Check n ()
mismatch = case Maybe (Core n)
mterm of
      Just Core n
term -> TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (Core n -> Value n -> Value n -> TypeError n
forall (n :: S). Core n -> Value n -> Value n -> TypeError n
TypeErrorUnify Core n
term Value n
expected Value n
actual)
      Maybe (Core n)
Nothing   -> TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (Value n -> Value n -> TypeError n
forall (n :: S). Value n -> Value n -> TypeError n
TypeErrorUnifyTerms Value n
expected Value n
actual)


pattern VPi :: Pi n -> Whnf n
pattern $mVPi :: forall {r} {n :: S}. Whnf n -> (Pi n -> r) -> ((# #) -> r) -> r
VPi p <- (piOf . NbE.fromWhnf -> Just p)