{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fmax-pmcheck-iterations=10000000 #-}
module OptCoercion ( optCoercion, checkAxInstCo ) where
#include "HsVersions.h"
import GhcPrelude
import DynFlags
import TyCoRep
import Coercion
import Type hiding( substTyVarBndr, substTy )
import TcType ( exactTyCoVarsOfType )
import TyCon
import CoAxiom
import VarSet
import VarEnv
import Outputable
import FamInstEnv ( flattenTys )
import Pair
import ListSetOps ( getNth )
import Util
import Unify
import InstEnv
import Control.Monad ( zipWithM )
optCoercion :: DynFlags -> TCvSubst -> Coercion -> NormalCo
optCoercion :: DynFlags -> TCvSubst -> Coercion -> Coercion
optCoercion dflags :: DynFlags
dflags env :: TCvSubst
env co :: Coercion
co
| DynFlags -> Bool
hasNoOptCoercion DynFlags
dflags = HasCallStack => TCvSubst -> Coercion -> Coercion
TCvSubst -> Coercion -> Coercion
substCo TCvSubst
env Coercion
co
| Bool
otherwise = TCvSubst -> Coercion -> Coercion
optCoercion' TCvSubst
env Coercion
co
optCoercion' :: TCvSubst -> Coercion -> NormalCo
optCoercion' :: TCvSubst -> Coercion -> Coercion
optCoercion' env :: TCvSubst
env co :: Coercion
co
| Bool
debugIsOn
= let out_co :: Coercion
out_co = LiftingContext -> Bool -> Coercion -> Coercion
opt_co1 LiftingContext
lc Bool
False Coercion
co
(Pair in_ty1 :: Type
in_ty1 in_ty2 :: Type
in_ty2, in_role :: Role
in_role) = Coercion -> (Pair Type, Role)
coercionKindRole Coercion
co
(Pair out_ty1 :: Type
out_ty1 out_ty2 :: Type
out_ty2, out_role :: Role
out_role) = Coercion -> (Pair Type, Role)
coercionKindRole Coercion
out_co
in
ASSERT2( substTy env in_ty1 `eqType` out_ty1 &&
substTy env in_ty2 `eqType` out_ty2 &&
in_role == out_role
, text "optCoercion changed types!"
$$ hang (text "in_co:") 2 (ppr co)
$$ hang (text "in_ty1:") 2 (ppr in_ty1)
$$ hang (text "in_ty2:") 2 (ppr in_ty2)
$$ hang (text "out_co:") 2 (ppr out_co)
$$ hang (text "out_ty1:") 2 (ppr out_ty1)
$$ hang (text "out_ty2:") 2 (ppr out_ty2)
$$ hang (text "subst:") 2 (ppr env) )
Coercion
out_co
| Bool
otherwise = LiftingContext -> Bool -> Coercion -> Coercion
opt_co1 LiftingContext
lc Bool
False Coercion
co
where
lc :: LiftingContext
lc = TCvSubst -> LiftingContext
mkSubstLiftingContext TCvSubst
env
type NormalCo = Coercion
type NormalNonIdCo = NormalCo
type SymFlag = Bool
type ReprFlag = Bool
opt_co1 :: LiftingContext
-> SymFlag
-> Coercion -> NormalCo
opt_co1 :: LiftingContext -> Bool -> Coercion -> Coercion
opt_co1 env :: LiftingContext
env sym :: Bool
sym co :: Coercion
co = LiftingContext -> Bool -> Role -> Coercion -> Coercion
opt_co2 LiftingContext
env Bool
sym (Coercion -> Role
coercionRole Coercion
co) Coercion
co
opt_co2 :: LiftingContext
-> SymFlag
-> Role
-> Coercion -> NormalCo
opt_co2 :: LiftingContext -> Bool -> Role -> Coercion -> Coercion
opt_co2 env :: LiftingContext
env sym :: Bool
sym Phantom co :: Coercion
co = LiftingContext -> Bool -> Coercion -> Coercion
opt_phantom LiftingContext
env Bool
sym Coercion
co
opt_co2 env :: LiftingContext
env sym :: Bool
sym r :: Role
r co :: Coercion
co = LiftingContext
-> Bool -> Maybe Role -> Role -> Coercion -> Coercion
opt_co3 LiftingContext
env Bool
sym Maybe Role
forall a. Maybe a
Nothing Role
r Coercion
co
opt_co3 :: LiftingContext -> SymFlag -> Maybe Role -> Role -> Coercion -> NormalCo
opt_co3 :: LiftingContext
-> Bool -> Maybe Role -> Role -> Coercion -> Coercion
opt_co3 env :: LiftingContext
env sym :: Bool
sym (Just Phantom) _ co :: Coercion
co = LiftingContext -> Bool -> Coercion -> Coercion
opt_phantom LiftingContext
env Bool
sym Coercion
co
opt_co3 env :: LiftingContext
env sym :: Bool
sym (Just Representational) r :: Role
r co :: Coercion
co = LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
True Role
r Coercion
co
opt_co3 env :: LiftingContext
env sym :: Bool
sym _ r :: Role
r co :: Coercion
co = LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
False Role
r Coercion
co
opt_co4, opt_co4_wrap :: LiftingContext -> SymFlag -> ReprFlag -> Role -> Coercion -> NormalCo
opt_co4_wrap :: LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap = LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4
opt_co4 :: LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4 env :: LiftingContext
env _ rep :: Bool
rep r :: Role
r (Refl ty :: Type
ty)
= ASSERT2( r == Nominal, text "Expected role:" <+> ppr r $$
text "Found role:" <+> ppr Nominal $$
text "Type:" <+> ppr ty )
HasDebugCallStack => Role -> LiftingContext -> Type -> Coercion
Role -> LiftingContext -> Type -> Coercion
liftCoSubst (Bool -> Role -> Role
chooseRole Bool
rep Role
r) LiftingContext
env Type
ty
opt_co4 env :: LiftingContext
env _ rep :: Bool
rep r :: Role
r (GRefl _r :: Role
_r ty :: Type
ty MRefl)
= ASSERT2( r == _r, text "Expected role:" <+> ppr r $$
text "Found role:" <+> ppr _r $$
text "Type:" <+> ppr ty )
HasDebugCallStack => Role -> LiftingContext -> Type -> Coercion
Role -> LiftingContext -> Type -> Coercion
liftCoSubst (Bool -> Role -> Role
chooseRole Bool
rep Role
r) LiftingContext
env Type
ty
opt_co4 env :: LiftingContext
env sym :: Bool
sym rep :: Bool
rep r :: Role
r (GRefl _r :: Role
_r ty :: Type
ty (MCo co :: Coercion
co))
= ASSERT2( r == _r, text "Expected role:" <+> ppr r $$
text "Found role:" <+> ppr _r $$
text "Type:" <+> ppr ty )
if Coercion -> Bool
isGReflCo Coercion
co Bool -> Bool -> Bool
|| Coercion -> Bool
isGReflCo Coercion
co'
then HasDebugCallStack => Role -> LiftingContext -> Type -> Coercion
Role -> LiftingContext -> Type -> Coercion
liftCoSubst Role
r' LiftingContext
env Type
ty
else Bool -> Coercion -> Coercion
wrapSym Bool
sym (Coercion -> Coercion) -> Coercion -> Coercion
forall a b. (a -> b) -> a -> b
$ Role -> Type -> Coercion -> Coercion -> Coercion
mkCoherenceRightCo Role
r' Type
ty' Coercion
co' (HasDebugCallStack => Role -> LiftingContext -> Type -> Coercion
Role -> LiftingContext -> Type -> Coercion
liftCoSubst Role
r' LiftingContext
env Type
ty)
where
r' :: Role
r' = Bool -> Role -> Role
chooseRole Bool
rep Role
r
ty' :: Type
ty' = HasCallStack => TCvSubst -> Type -> Type
TCvSubst -> Type -> Type
substTy (LiftingContext -> TCvSubst
lcSubstLeft LiftingContext
env) Type
ty
co' :: Coercion
co' = LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4 LiftingContext
env Bool
False Bool
False Role
Nominal Coercion
co
opt_co4 env :: LiftingContext
env sym :: Bool
sym rep :: Bool
rep r :: Role
r (SymCo co :: Coercion
co) = LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env (Bool -> Bool
not Bool
sym) Bool
rep Role
r Coercion
co
opt_co4 env :: LiftingContext
env sym :: Bool
sym rep :: Bool
rep r :: Role
r g :: Coercion
g@(TyConAppCo _r :: Role
_r tc :: TyCon
tc cos :: [Coercion]
cos)
= ASSERT( r == _r )
case (Bool
rep, Role
r) of
(True, Nominal) ->
HasDebugCallStack => Role -> TyCon -> [Coercion] -> Coercion
Role -> TyCon -> [Coercion] -> Coercion
mkTyConAppCo Role
Representational TyCon
tc
((Maybe Role -> Role -> Coercion -> Coercion)
-> [Maybe Role] -> [Role] -> [Coercion] -> [Coercion]
forall a b c d. (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
zipWith3 (LiftingContext
-> Bool -> Maybe Role -> Role -> Coercion -> Coercion
opt_co3 LiftingContext
env Bool
sym)
((Role -> Maybe Role) -> [Role] -> [Maybe Role]
forall a b. (a -> b) -> [a] -> [b]
map Role -> Maybe Role
forall a. a -> Maybe a
Just (TyCon -> [Role]
tyConRolesRepresentational TyCon
tc))
(Role -> [Role]
forall a. a -> [a]
repeat Role
Nominal)
[Coercion]
cos)
(False, Nominal) ->
HasDebugCallStack => Role -> TyCon -> [Coercion] -> Coercion
Role -> TyCon -> [Coercion] -> Coercion
mkTyConAppCo Role
Nominal TyCon
tc ((Coercion -> Coercion) -> [Coercion] -> [Coercion]
forall a b. (a -> b) -> [a] -> [b]
map (LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
False Role
Nominal) [Coercion]
cos)
(_, Representational) ->
HasDebugCallStack => Role -> TyCon -> [Coercion] -> Coercion
Role -> TyCon -> [Coercion] -> Coercion
mkTyConAppCo Role
r TyCon
tc ((Role -> Coercion -> Coercion)
-> [Role] -> [Coercion] -> [Coercion]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (LiftingContext -> Bool -> Role -> Coercion -> Coercion
opt_co2 LiftingContext
env Bool
sym)
(TyCon -> [Role]
tyConRolesRepresentational TyCon
tc)
[Coercion]
cos)
(_, Phantom) -> String -> SDoc -> Coercion
forall a. HasCallStack => String -> SDoc -> a
pprPanic "opt_co4 sees a phantom!" (Coercion -> SDoc
forall a. Outputable a => a -> SDoc
ppr Coercion
g)
opt_co4 env :: LiftingContext
env sym :: Bool
sym rep :: Bool
rep r :: Role
r (AppCo co1 :: Coercion
co1 co2 :: Coercion
co2)
= Coercion -> Coercion -> Coercion
mkAppCo (LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
rep Role
r Coercion
co1)
(LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
False Role
Nominal Coercion
co2)
opt_co4 env :: LiftingContext
env sym :: Bool
sym rep :: Bool
rep r :: Role
r (ForAllCo tv :: TyCoVar
tv k_co :: Coercion
k_co co :: Coercion
co)
= case LiftingContext
-> Bool
-> TyCoVar
-> Coercion
-> (LiftingContext, TyCoVar, Coercion)
optForAllCoBndr LiftingContext
env Bool
sym TyCoVar
tv Coercion
k_co of
(env' :: LiftingContext
env', tv' :: TyCoVar
tv', k_co' :: Coercion
k_co') -> TyCoVar -> Coercion -> Coercion -> Coercion
mkForAllCo TyCoVar
tv' Coercion
k_co' (Coercion -> Coercion) -> Coercion -> Coercion
forall a b. (a -> b) -> a -> b
$
LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env' Bool
sym Bool
rep Role
r Coercion
co
opt_co4 env :: LiftingContext
env sym :: Bool
sym rep :: Bool
rep r :: Role
r (FunCo _r :: Role
_r co1 :: Coercion
co1 co2 :: Coercion
co2)
= ASSERT( r == _r )
if Bool
rep
then Role -> Coercion -> Coercion -> Coercion
mkFunCo Role
Representational Coercion
co1' Coercion
co2'
else Role -> Coercion -> Coercion -> Coercion
mkFunCo Role
r Coercion
co1' Coercion
co2'
where
co1' :: Coercion
co1' = LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
rep Role
r Coercion
co1
co2' :: Coercion
co2' = LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
rep Role
r Coercion
co2
opt_co4 env :: LiftingContext
env sym :: Bool
sym rep :: Bool
rep r :: Role
r (CoVarCo cv :: TyCoVar
cv)
| Just co :: Coercion
co <- TCvSubst -> TyCoVar -> Maybe Coercion
lookupCoVar (LiftingContext -> TCvSubst
lcTCvSubst LiftingContext
env) TyCoVar
cv
= LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap (LiftingContext -> LiftingContext
zapLiftingContext LiftingContext
env) Bool
sym Bool
rep Role
r Coercion
co
| Type
ty1 Type -> Type -> Bool
`eqType` Type
ty2
= Role -> Type -> Coercion
mkReflCo (Bool -> Role -> Role
chooseRole Bool
rep Role
r) Type
ty1
| Bool
otherwise
= ASSERT( isCoVar cv1 )
Bool -> Role -> Coercion -> Coercion
wrapRole Bool
rep Role
r (Coercion -> Coercion) -> Coercion -> Coercion
forall a b. (a -> b) -> a -> b
$ Bool -> Coercion -> Coercion
wrapSym Bool
sym (Coercion -> Coercion) -> Coercion -> Coercion
forall a b. (a -> b) -> a -> b
$
TyCoVar -> Coercion
CoVarCo TyCoVar
cv1
where
Pair ty1 :: Type
ty1 ty2 :: Type
ty2 = HasDebugCallStack => TyCoVar -> Pair Type
TyCoVar -> Pair Type
coVarTypes TyCoVar
cv1
cv1 :: TyCoVar
cv1 = case InScopeSet -> TyCoVar -> Maybe TyCoVar
lookupInScope (LiftingContext -> InScopeSet
lcInScopeSet LiftingContext
env) TyCoVar
cv of
Just cv1 :: TyCoVar
cv1 -> TyCoVar
cv1
Nothing -> WARN( True, text "opt_co: not in scope:"
<+> ppr cv $$ ppr env)
TyCoVar
cv
opt_co4 _ _ _ _ (HoleCo h :: CoercionHole
h)
= String -> SDoc -> Coercion
forall a. HasCallStack => String -> SDoc -> a
pprPanic "opt_univ fell into a hole" (CoercionHole -> SDoc
forall a. Outputable a => a -> SDoc
ppr CoercionHole
h)
opt_co4 env :: LiftingContext
env sym :: Bool
sym rep :: Bool
rep r :: Role
r (AxiomInstCo con :: CoAxiom Branched
con ind :: Int
ind cos :: [Coercion]
cos)
= ASSERT( r == coAxiomRole con )
Bool -> Role -> Coercion -> Coercion
wrapRole Bool
rep (CoAxiom Branched -> Role
forall (br :: BranchFlag). CoAxiom br -> Role
coAxiomRole CoAxiom Branched
con) (Coercion -> Coercion) -> Coercion -> Coercion
forall a b. (a -> b) -> a -> b
$
Bool -> Coercion -> Coercion
wrapSym Bool
sym (Coercion -> Coercion) -> Coercion -> Coercion
forall a b. (a -> b) -> a -> b
$
CoAxiom Branched -> Int -> [Coercion] -> Coercion
AxiomInstCo CoAxiom Branched
con Int
ind ((Role -> Coercion -> Coercion)
-> [Role] -> [Coercion] -> [Coercion]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (LiftingContext -> Bool -> Role -> Coercion -> Coercion
opt_co2 LiftingContext
env Bool
False)
(CoAxBranch -> [Role]
coAxBranchRoles (CoAxiom Branched -> Int -> CoAxBranch
forall (br :: BranchFlag). CoAxiom br -> Int -> CoAxBranch
coAxiomNthBranch CoAxiom Branched
con Int
ind))
[Coercion]
cos)
opt_co4 env :: LiftingContext
env sym :: Bool
sym rep :: Bool
rep r :: Role
r (UnivCo prov :: UnivCoProvenance
prov _r :: Role
_r t1 :: Type
t1 t2 :: Type
t2)
= ASSERT( r == _r )
LiftingContext
-> Bool -> UnivCoProvenance -> Role -> Type -> Type -> Coercion
opt_univ LiftingContext
env Bool
sym UnivCoProvenance
prov (Bool -> Role -> Role
chooseRole Bool
rep Role
r) Type
t1 Type
t2
opt_co4 env :: LiftingContext
env sym :: Bool
sym rep :: Bool
rep r :: Role
r (TransCo co1 :: Coercion
co1 co2 :: Coercion
co2)
| Bool
sym = InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
in_scope Coercion
co2' Coercion
co1'
| Bool
otherwise = InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
in_scope Coercion
co1' Coercion
co2'
where
co1' :: Coercion
co1' = LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
rep Role
r Coercion
co1
co2' :: Coercion
co2' = LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
rep Role
r Coercion
co2
in_scope :: InScopeSet
in_scope = LiftingContext -> InScopeSet
lcInScopeSet LiftingContext
env
opt_co4 env :: LiftingContext
env _sym :: Bool
_sym rep :: Bool
rep r :: Role
r (NthCo _r :: Role
_r n :: Int
n co :: Coercion
co)
| Just (ty :: Type
ty, _) <- Coercion -> Maybe (Type, Role)
isReflCo_maybe Coercion
co
, Just (_tc :: TyCon
_tc, args :: [Type]
args) <- ASSERT( r == _r )
HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty
= HasDebugCallStack => Role -> LiftingContext -> Type -> Coercion
Role -> LiftingContext -> Type -> Coercion
liftCoSubst (Bool -> Role -> Role
chooseRole Bool
rep Role
r) LiftingContext
env ([Type]
args [Type] -> Int -> Type
forall a. Outputable a => [a] -> Int -> a
`getNth` Int
n)
| Just (ty :: Type
ty, _) <- Coercion -> Maybe (Type, Role)
isReflCo_maybe Coercion
co
, Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 0
, Just (tv :: TyCoVar
tv, _) <- Type -> Maybe (TyCoVar, Type)
splitForAllTy_maybe Type
ty
= HasDebugCallStack => Role -> LiftingContext -> Type -> Coercion
Role -> LiftingContext -> Type -> Coercion
liftCoSubst (Bool -> Role -> Role
chooseRole Bool
rep Role
r) LiftingContext
env (TyCoVar -> Type
varType TyCoVar
tv)
opt_co4 env :: LiftingContext
env sym :: Bool
sym rep :: Bool
rep r :: Role
r (NthCo r1 :: Role
r1 n :: Int
n (TyConAppCo _ _ cos :: [Coercion]
cos))
= ASSERT( r == r1 )
LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
rep Role
r ([Coercion]
cos [Coercion] -> Int -> Coercion
forall a. Outputable a => [a] -> Int -> a
`getNth` Int
n)
opt_co4 env :: LiftingContext
env sym :: Bool
sym rep :: Bool
rep r :: Role
r (NthCo _r :: Role
_r n :: Int
n (ForAllCo _ eta :: Coercion
eta _))
= ASSERT( r == _r )
ASSERT( n == 0 )
LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
rep Role
Nominal Coercion
eta
opt_co4 env :: LiftingContext
env sym :: Bool
sym rep :: Bool
rep r :: Role
r (NthCo _r :: Role
_r n :: Int
n co :: Coercion
co)
| TyConAppCo _ _ cos :: [Coercion]
cos <- Coercion
co'
, let nth_co :: Coercion
nth_co = [Coercion]
cos [Coercion] -> Int -> Coercion
forall a. Outputable a => [a] -> Int -> a
`getNth` Int
n
= if Bool
rep Bool -> Bool -> Bool
&& (Role
r Role -> Role -> Bool
forall a. Eq a => a -> a -> Bool
== Role
Nominal)
then LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap (LiftingContext -> LiftingContext
zapLiftingContext LiftingContext
env) Bool
False Bool
True Role
Nominal Coercion
nth_co
else Coercion
nth_co
| ForAllCo _ eta :: Coercion
eta _ <- Coercion
co'
= if Bool
rep
then LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap (LiftingContext -> LiftingContext
zapLiftingContext LiftingContext
env) Bool
False Bool
True Role
Nominal Coercion
eta
else Coercion
eta
| Bool
otherwise
= Bool -> Role -> Coercion -> Coercion
wrapRole Bool
rep Role
r (Coercion -> Coercion) -> Coercion -> Coercion
forall a b. (a -> b) -> a -> b
$ Role -> Int -> Coercion -> Coercion
NthCo Role
r Int
n Coercion
co'
where
co' :: Coercion
co' = LiftingContext -> Bool -> Coercion -> Coercion
opt_co1 LiftingContext
env Bool
sym Coercion
co
opt_co4 env :: LiftingContext
env sym :: Bool
sym rep :: Bool
rep r :: Role
r (LRCo lr :: LeftOrRight
lr co :: Coercion
co)
| Just pr_co :: (Coercion, Coercion)
pr_co <- Coercion -> Maybe (Coercion, Coercion)
splitAppCo_maybe Coercion
co
= ASSERT( r == Nominal )
LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
rep Role
Nominal (LeftOrRight -> (Coercion, Coercion) -> Coercion
forall p. LeftOrRight -> (p, p) -> p
pick_lr LeftOrRight
lr (Coercion, Coercion)
pr_co)
| Just pr_co :: (Coercion, Coercion)
pr_co <- Coercion -> Maybe (Coercion, Coercion)
splitAppCo_maybe Coercion
co'
= ASSERT( r == Nominal )
if Bool
rep
then LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap (LiftingContext -> LiftingContext
zapLiftingContext LiftingContext
env) Bool
False Bool
True Role
Nominal (LeftOrRight -> (Coercion, Coercion) -> Coercion
forall p. LeftOrRight -> (p, p) -> p
pick_lr LeftOrRight
lr (Coercion, Coercion)
pr_co)
else LeftOrRight -> (Coercion, Coercion) -> Coercion
forall p. LeftOrRight -> (p, p) -> p
pick_lr LeftOrRight
lr (Coercion, Coercion)
pr_co
| Bool
otherwise
= Bool -> Role -> Coercion -> Coercion
wrapRole Bool
rep Role
Nominal (Coercion -> Coercion) -> Coercion -> Coercion
forall a b. (a -> b) -> a -> b
$ LeftOrRight -> Coercion -> Coercion
LRCo LeftOrRight
lr Coercion
co'
where
co' :: Coercion
co' = LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
False Role
Nominal Coercion
co
pick_lr :: LeftOrRight -> (p, p) -> p
pick_lr CLeft (l :: p
l, _) = p
l
pick_lr CRight (_, r :: p
r) = p
r
opt_co4 env :: LiftingContext
env sym :: Bool
sym rep :: Bool
rep r :: Role
r (InstCo co1 :: Coercion
co1 arg :: Coercion
arg)
| Just (tv :: TyCoVar
tv, kind_co :: Coercion
kind_co, co_body :: Coercion
co_body) <- Coercion -> Maybe (TyCoVar, Coercion, Coercion)
splitForAllCo_ty_maybe Coercion
co1
= LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap (LiftingContext -> TyCoVar -> Coercion -> LiftingContext
extendLiftingContext LiftingContext
env TyCoVar
tv
(Role -> Type -> Coercion -> Coercion -> Coercion
mkCoherenceRightCo Role
Nominal Type
t2 (Coercion -> Coercion
mkSymCo Coercion
kind_co) Coercion
sym_arg))
Bool
sym Bool
rep Role
r Coercion
co_body
| Just (cv :: TyCoVar
cv, kind_co :: Coercion
kind_co, co_body :: Coercion
co_body) <- Coercion -> Maybe (TyCoVar, Coercion, Coercion)
splitForAllCo_co_maybe Coercion
co1
, CoercionTy h1 :: Coercion
h1 <- Type
t1
, CoercionTy h2 :: Coercion
h2 <- Type
t2
= let new_co :: Coercion
new_co = TyCoVar -> Coercion -> Coercion -> Coercion -> Coercion
mk_new_co TyCoVar
cv (LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
False Role
Nominal Coercion
kind_co) Coercion
h1 Coercion
h2
in LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap (LiftingContext -> TyCoVar -> Coercion -> LiftingContext
extendLiftingContext LiftingContext
env TyCoVar
cv Coercion
new_co) Bool
sym Bool
rep Role
r Coercion
co_body
| Just (tv' :: TyCoVar
tv', kind_co' :: Coercion
kind_co', co_body' :: Coercion
co_body') <- Coercion -> Maybe (TyCoVar, Coercion, Coercion)
splitForAllCo_ty_maybe Coercion
co1'
= LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap (LiftingContext -> TyCoVar -> Coercion -> LiftingContext
extendLiftingContext (LiftingContext -> LiftingContext
zapLiftingContext LiftingContext
env) TyCoVar
tv'
(Role -> Type -> Coercion -> Coercion -> Coercion
mkCoherenceRightCo Role
Nominal Type
t2' (Coercion -> Coercion
mkSymCo Coercion
kind_co') Coercion
arg'))
Bool
False Bool
False Role
r' Coercion
co_body'
| Just (cv' :: TyCoVar
cv', kind_co' :: Coercion
kind_co', co_body' :: Coercion
co_body') <- Coercion -> Maybe (TyCoVar, Coercion, Coercion)
splitForAllCo_co_maybe Coercion
co1'
, CoercionTy h1' :: Coercion
h1' <- Type
t1'
, CoercionTy h2' :: Coercion
h2' <- Type
t2'
= let new_co :: Coercion
new_co = TyCoVar -> Coercion -> Coercion -> Coercion -> Coercion
mk_new_co TyCoVar
cv' Coercion
kind_co' Coercion
h1' Coercion
h2'
in LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap (LiftingContext -> TyCoVar -> Coercion -> LiftingContext
extendLiftingContext (LiftingContext -> LiftingContext
zapLiftingContext LiftingContext
env) TyCoVar
cv' Coercion
new_co)
Bool
False Bool
False Role
r' Coercion
co_body'
| Bool
otherwise = Coercion -> Coercion -> Coercion
InstCo Coercion
co1' Coercion
arg'
where
co1' :: Coercion
co1' = LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
rep Role
r Coercion
co1
r' :: Role
r' = Bool -> Role -> Role
chooseRole Bool
rep Role
r
arg' :: Coercion
arg' = LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
False Role
Nominal Coercion
arg
sym_arg :: Coercion
sym_arg = Bool -> Coercion -> Coercion
wrapSym Bool
sym Coercion
arg'
Pair t1 :: Type
t1 t2 :: Type
t2 = Coercion -> Pair Type
coercionKind Coercion
sym_arg
Pair t1' :: Type
t1' t2' :: Type
t2' = Coercion -> Pair Type
coercionKind Coercion
arg'
mk_new_co :: TyCoVar -> Coercion -> Coercion -> Coercion -> Coercion
mk_new_co cv :: TyCoVar
cv kind_co :: Coercion
kind_co h1 :: Coercion
h1 h2 :: Coercion
h2
= let
r2 :: Role
r2 = TyCoVar -> Role
coVarRole TyCoVar
cv
kind_co' :: Coercion
kind_co' = Role -> Role -> Coercion -> Coercion
downgradeRole Role
r2 Role
Nominal Coercion
kind_co
n1 :: Coercion
n1 = HasDebugCallStack => Role -> Int -> Coercion -> Coercion
Role -> Int -> Coercion -> Coercion
mkNthCo Role
r2 2 Coercion
kind_co'
n2 :: Coercion
n2 = HasDebugCallStack => Role -> Int -> Coercion -> Coercion
Role -> Int -> Coercion -> Coercion
mkNthCo Role
r2 3 Coercion
kind_co'
in Role -> Coercion -> Coercion -> Coercion -> Coercion
mkProofIrrelCo Role
Nominal (Type -> Coercion
Refl (Coercion -> Type
coercionType Coercion
h1)) Coercion
h1
(Coercion
n1 Coercion -> Coercion -> Coercion
`mkTransCo` Coercion
h2 Coercion -> Coercion -> Coercion
`mkTransCo` (Coercion -> Coercion
mkSymCo Coercion
n2))
opt_co4 env :: LiftingContext
env sym :: Bool
sym _rep :: Bool
_rep r :: Role
r (KindCo co :: Coercion
co)
= ASSERT( r == Nominal )
let kco' :: Coercion
kco' = Coercion -> Coercion
promoteCoercion Coercion
co in
case Coercion
kco' of
KindCo co' :: Coercion
co' -> Coercion -> Coercion
promoteCoercion (LiftingContext -> Bool -> Coercion -> Coercion
opt_co1 LiftingContext
env Bool
sym Coercion
co')
_ -> LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
False Role
Nominal Coercion
kco'
opt_co4 env :: LiftingContext
env sym :: Bool
sym _ r :: Role
r (SubCo co :: Coercion
co)
= ASSERT( r == Representational )
LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
True Role
Nominal Coercion
co
opt_co4 env :: LiftingContext
env sym :: Bool
sym rep :: Bool
rep r :: Role
r (AxiomRuleCo co :: CoAxiomRule
co cs :: [Coercion]
cs)
= ASSERT( r == coaxrRole co )
Bool -> Role -> Coercion -> Coercion
wrapRole Bool
rep Role
r (Coercion -> Coercion) -> Coercion -> Coercion
forall a b. (a -> b) -> a -> b
$
Bool -> Coercion -> Coercion
wrapSym Bool
sym (Coercion -> Coercion) -> Coercion -> Coercion
forall a b. (a -> b) -> a -> b
$
CoAxiomRule -> [Coercion] -> Coercion
AxiomRuleCo CoAxiomRule
co ((Role -> Coercion -> Coercion)
-> [Role] -> [Coercion] -> [Coercion]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (LiftingContext -> Bool -> Role -> Coercion -> Coercion
opt_co2 LiftingContext
env Bool
False) (CoAxiomRule -> [Role]
coaxrAsmpRoles CoAxiomRule
co) [Coercion]
cs)
opt_phantom :: LiftingContext -> SymFlag -> Coercion -> NormalCo
opt_phantom :: LiftingContext -> Bool -> Coercion -> Coercion
opt_phantom env :: LiftingContext
env sym :: Bool
sym co :: Coercion
co
= LiftingContext
-> Bool -> UnivCoProvenance -> Role -> Type -> Type -> Coercion
opt_univ LiftingContext
env Bool
sym (Coercion -> UnivCoProvenance
PhantomProv (Coercion -> Coercion
mkKindCo Coercion
co)) Role
Phantom Type
ty1 Type
ty2
where
Pair ty1 :: Type
ty1 ty2 :: Type
ty2 = Coercion -> Pair Type
coercionKind Coercion
co
opt_univ :: LiftingContext -> SymFlag -> UnivCoProvenance -> Role
-> Type -> Type -> Coercion
opt_univ :: LiftingContext
-> Bool -> UnivCoProvenance -> Role -> Type -> Type -> Coercion
opt_univ env :: LiftingContext
env sym :: Bool
sym (PhantomProv h :: Coercion
h) _r :: Role
_r ty1 :: Type
ty1 ty2 :: Type
ty2
| Bool
sym = Coercion -> Type -> Type -> Coercion
mkPhantomCo Coercion
h' Type
ty2' Type
ty1'
| Bool
otherwise = Coercion -> Type -> Type -> Coercion
mkPhantomCo Coercion
h' Type
ty1' Type
ty2'
where
h' :: Coercion
h' = LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4 LiftingContext
env Bool
sym Bool
False Role
Nominal Coercion
h
ty1' :: Type
ty1' = HasCallStack => TCvSubst -> Type -> Type
TCvSubst -> Type -> Type
substTy (LiftingContext -> TCvSubst
lcSubstLeft LiftingContext
env) Type
ty1
ty2' :: Type
ty2' = HasCallStack => TCvSubst -> Type -> Type
TCvSubst -> Type -> Type
substTy (LiftingContext -> TCvSubst
lcSubstRight LiftingContext
env) Type
ty2
opt_univ env :: LiftingContext
env sym :: Bool
sym prov :: UnivCoProvenance
prov role :: Role
role oty1 :: Type
oty1 oty2 :: Type
oty2
| Just (tc1 :: TyCon
tc1, tys1 :: [Type]
tys1) <- HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
oty1
, Just (tc2 :: TyCon
tc2, tys2 :: [Type]
tys2) <- HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
oty2
, TyCon
tc1 TyCon -> TyCon -> Bool
forall a. Eq a => a -> a -> Bool
== TyCon
tc2
, [Type] -> [Type] -> Bool
forall a b. [a] -> [b] -> Bool
equalLength [Type]
tys1 [Type]
tys2
= let roles :: [Role]
roles = Role -> TyCon -> [Role]
tyConRolesX Role
role TyCon
tc1
arg_cos :: [Coercion]
arg_cos = (Role -> Type -> Type -> Coercion)
-> [Role] -> [Type] -> [Type] -> [Coercion]
forall a b c d. (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
zipWith3 (UnivCoProvenance -> Role -> Type -> Type -> Coercion
mkUnivCo UnivCoProvenance
prov') [Role]
roles [Type]
tys1 [Type]
tys2
arg_cos' :: [Coercion]
arg_cos' = (Role -> Coercion -> Coercion)
-> [Role] -> [Coercion] -> [Coercion]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4 LiftingContext
env Bool
sym Bool
False) [Role]
roles [Coercion]
arg_cos
in
HasDebugCallStack => Role -> TyCon -> [Coercion] -> Coercion
Role -> TyCon -> [Coercion] -> Coercion
mkTyConAppCo Role
role TyCon
tc1 [Coercion]
arg_cos'
| Just (tv1 :: TyCoVar
tv1, ty1 :: Type
ty1) <- Type -> Maybe (TyCoVar, Type)
splitForAllTy_ty_maybe Type
oty1
, Just (tv2 :: TyCoVar
tv2, ty2 :: Type
ty2) <- Type -> Maybe (TyCoVar, Type)
splitForAllTy_ty_maybe Type
oty2
= let k1 :: Type
k1 = TyCoVar -> Type
tyVarKind TyCoVar
tv1
k2 :: Type
k2 = TyCoVar -> Type
tyVarKind TyCoVar
tv2
eta :: Coercion
eta = UnivCoProvenance -> Role -> Type -> Type -> Coercion
mkUnivCo UnivCoProvenance
prov' Role
Nominal Type
k1 Type
k2
ty2' :: Type
ty2' = HasCallStack => [TyCoVar] -> [Type] -> Type -> Type
[TyCoVar] -> [Type] -> Type -> Type
substTyWith [TyCoVar
tv2] [TyCoVar -> Type
TyVarTy TyCoVar
tv1 Type -> Coercion -> Type
`mkCastTy` Coercion
eta] Type
ty2
(env' :: LiftingContext
env', tv1' :: TyCoVar
tv1', eta' :: Coercion
eta') = LiftingContext
-> Bool
-> TyCoVar
-> Coercion
-> (LiftingContext, TyCoVar, Coercion)
optForAllCoBndr LiftingContext
env Bool
sym TyCoVar
tv1 Coercion
eta
in
TyCoVar -> Coercion -> Coercion -> Coercion
mkForAllCo TyCoVar
tv1' Coercion
eta' (LiftingContext
-> Bool -> UnivCoProvenance -> Role -> Type -> Type -> Coercion
opt_univ LiftingContext
env' Bool
sym UnivCoProvenance
prov' Role
role Type
ty1 Type
ty2')
| Just (cv1 :: TyCoVar
cv1, ty1 :: Type
ty1) <- Type -> Maybe (TyCoVar, Type)
splitForAllTy_co_maybe Type
oty1
, Just (cv2 :: TyCoVar
cv2, ty2 :: Type
ty2) <- Type -> Maybe (TyCoVar, Type)
splitForAllTy_co_maybe Type
oty2
= let k1 :: Type
k1 = TyCoVar -> Type
varType TyCoVar
cv1
k2 :: Type
k2 = TyCoVar -> Type
varType TyCoVar
cv2
r' :: Role
r' = TyCoVar -> Role
coVarRole TyCoVar
cv1
eta :: Coercion
eta = UnivCoProvenance -> Role -> Type -> Type -> Coercion
mkUnivCo UnivCoProvenance
prov' Role
Nominal Type
k1 Type
k2
eta_d :: Coercion
eta_d = Role -> Role -> Coercion -> Coercion
downgradeRole Role
r' Role
Nominal Coercion
eta
n_co :: Coercion
n_co = (Coercion -> Coercion
mkSymCo (Coercion -> Coercion) -> Coercion -> Coercion
forall a b. (a -> b) -> a -> b
$ HasDebugCallStack => Role -> Int -> Coercion -> Coercion
Role -> Int -> Coercion -> Coercion
mkNthCo Role
r' 2 Coercion
eta_d) Coercion -> Coercion -> Coercion
`mkTransCo`
(TyCoVar -> Coercion
mkCoVarCo TyCoVar
cv1) Coercion -> Coercion -> Coercion
`mkTransCo`
(HasDebugCallStack => Role -> Int -> Coercion -> Coercion
Role -> Int -> Coercion -> Coercion
mkNthCo Role
r' 3 Coercion
eta_d)
ty2' :: Type
ty2' = [TyCoVar] -> [Coercion] -> Type -> Type
substTyWithCoVars [TyCoVar
cv2] [Coercion
n_co] Type
ty2
(env' :: LiftingContext
env', cv1' :: TyCoVar
cv1', eta' :: Coercion
eta') = LiftingContext
-> Bool
-> TyCoVar
-> Coercion
-> (LiftingContext, TyCoVar, Coercion)
optForAllCoBndr LiftingContext
env Bool
sym TyCoVar
cv1 Coercion
eta
in
TyCoVar -> Coercion -> Coercion -> Coercion
mkForAllCo TyCoVar
cv1' Coercion
eta' (LiftingContext
-> Bool -> UnivCoProvenance -> Role -> Type -> Type -> Coercion
opt_univ LiftingContext
env' Bool
sym UnivCoProvenance
prov' Role
role Type
ty1 Type
ty2')
| Bool
otherwise
= let ty1 :: Type
ty1 = TCvSubst -> Type -> Type
substTyUnchecked (LiftingContext -> TCvSubst
lcSubstLeft LiftingContext
env) Type
oty1
ty2 :: Type
ty2 = TCvSubst -> Type -> Type
substTyUnchecked (LiftingContext -> TCvSubst
lcSubstRight LiftingContext
env) Type
oty2
(a :: Type
a, b :: Type
b) | Bool
sym = (Type
ty2, Type
ty1)
| Bool
otherwise = (Type
ty1, Type
ty2)
in
UnivCoProvenance -> Role -> Type -> Type -> Coercion
mkUnivCo UnivCoProvenance
prov' Role
role Type
a Type
b
where
prov' :: UnivCoProvenance
prov' = case UnivCoProvenance
prov of
UnsafeCoerceProv -> UnivCoProvenance
prov
PhantomProv kco :: Coercion
kco -> Coercion -> UnivCoProvenance
PhantomProv (Coercion -> UnivCoProvenance) -> Coercion -> UnivCoProvenance
forall a b. (a -> b) -> a -> b
$ LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
False Role
Nominal Coercion
kco
ProofIrrelProv kco :: Coercion
kco -> Coercion -> UnivCoProvenance
ProofIrrelProv (Coercion -> UnivCoProvenance) -> Coercion -> UnivCoProvenance
forall a b. (a -> b) -> a -> b
$ LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
False Role
Nominal Coercion
kco
PluginProv _ -> UnivCoProvenance
prov
opt_transList :: InScopeSet -> [NormalCo] -> [NormalCo] -> [NormalCo]
opt_transList :: InScopeSet -> [Coercion] -> [Coercion] -> [Coercion]
opt_transList is :: InScopeSet
is = (Coercion -> Coercion -> Coercion)
-> [Coercion] -> [Coercion] -> [Coercion]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is)
opt_trans :: InScopeSet -> NormalCo -> NormalCo -> NormalCo
opt_trans :: InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans is :: InScopeSet
is co1 :: Coercion
co1 co2 :: Coercion
co2
| Coercion -> Bool
isReflCo Coercion
co1 = Coercion
co2
| Bool
otherwise = InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans1 InScopeSet
is Coercion
co1 Coercion
co2
opt_trans1 :: InScopeSet -> NormalNonIdCo -> NormalCo -> NormalCo
opt_trans1 :: InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans1 is :: InScopeSet
is co1 :: Coercion
co1 co2 :: Coercion
co2
| Coercion -> Bool
isReflCo Coercion
co2 = Coercion
co1
| Bool
otherwise = InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans2 InScopeSet
is Coercion
co1 Coercion
co2
opt_trans2 :: InScopeSet -> NormalNonIdCo -> NormalNonIdCo -> NormalCo
opt_trans2 :: InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans2 is :: InScopeSet
is (TransCo co1a :: Coercion
co1a co1b :: Coercion
co1b) co2 :: Coercion
co2
= InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is Coercion
co1a (InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is Coercion
co1b Coercion
co2)
opt_trans2 is :: InScopeSet
is co1 :: Coercion
co1 co2 :: Coercion
co2
| Just co :: Coercion
co <- InScopeSet -> Coercion -> Coercion -> Maybe Coercion
opt_trans_rule InScopeSet
is Coercion
co1 Coercion
co2
= Coercion
co
opt_trans2 is :: InScopeSet
is co1 :: Coercion
co1 (TransCo co2a :: Coercion
co2a co2b :: Coercion
co2b)
| Just co1_2a :: Coercion
co1_2a <- InScopeSet -> Coercion -> Coercion -> Maybe Coercion
opt_trans_rule InScopeSet
is Coercion
co1 Coercion
co2a
= if Coercion -> Bool
isReflCo Coercion
co1_2a
then Coercion
co2b
else InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans1 InScopeSet
is Coercion
co1_2a Coercion
co2b
opt_trans2 _ co1 :: Coercion
co1 co2 :: Coercion
co2
= Coercion -> Coercion -> Coercion
mkTransCo Coercion
co1 Coercion
co2
opt_trans_rule :: InScopeSet -> NormalNonIdCo -> NormalNonIdCo -> Maybe NormalCo
opt_trans_rule :: InScopeSet -> Coercion -> Coercion -> Maybe Coercion
opt_trans_rule is :: InScopeSet
is in_co1 :: Coercion
in_co1@(GRefl r1 :: Role
r1 t1 :: Type
t1 (MCo co1 :: Coercion
co1)) in_co2 :: Coercion
in_co2@(GRefl r2 :: Role
r2 _ (MCo co2 :: Coercion
co2))
= ASSERT( r1 == r2 )
String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "GRefl" Coercion
in_co1 Coercion
in_co2 (Coercion -> Maybe Coercion) -> Coercion -> Maybe Coercion
forall a b. (a -> b) -> a -> b
$
Role -> Type -> Coercion -> Coercion
mkGReflRightCo Role
r1 Type
t1 (InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is Coercion
co1 Coercion
co2)
opt_trans_rule is :: InScopeSet
is in_co1 :: Coercion
in_co1@(NthCo r1 :: Role
r1 d1 :: Int
d1 co1 :: Coercion
co1) in_co2 :: Coercion
in_co2@(NthCo r2 :: Role
r2 d2 :: Int
d2 co2 :: Coercion
co2)
| Int
d1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
d2
, Coercion -> Role
coercionRole Coercion
co1 Role -> Role -> Bool
forall a. Eq a => a -> a -> Bool
== Coercion -> Role
coercionRole Coercion
co2
, Coercion
co1 Coercion -> Coercion -> Bool
`compatible_co` Coercion
co2
= ASSERT( r1 == r2 )
String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "PushNth" Coercion
in_co1 Coercion
in_co2 (Coercion -> Maybe Coercion) -> Coercion -> Maybe Coercion
forall a b. (a -> b) -> a -> b
$
HasDebugCallStack => Role -> Int -> Coercion -> Coercion
Role -> Int -> Coercion -> Coercion
mkNthCo Role
r1 Int
d1 (InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is Coercion
co1 Coercion
co2)
opt_trans_rule is :: InScopeSet
is in_co1 :: Coercion
in_co1@(LRCo d1 :: LeftOrRight
d1 co1 :: Coercion
co1) in_co2 :: Coercion
in_co2@(LRCo d2 :: LeftOrRight
d2 co2 :: Coercion
co2)
| LeftOrRight
d1 LeftOrRight -> LeftOrRight -> Bool
forall a. Eq a => a -> a -> Bool
== LeftOrRight
d2
, Coercion
co1 Coercion -> Coercion -> Bool
`compatible_co` Coercion
co2
= String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "PushLR" Coercion
in_co1 Coercion
in_co2 (Coercion -> Maybe Coercion) -> Coercion -> Maybe Coercion
forall a b. (a -> b) -> a -> b
$
LeftOrRight -> Coercion -> Coercion
mkLRCo LeftOrRight
d1 (InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is Coercion
co1 Coercion
co2)
opt_trans_rule is :: InScopeSet
is in_co1 :: Coercion
in_co1@(InstCo co1 :: Coercion
co1 ty1 :: Coercion
ty1) in_co2 :: Coercion
in_co2@(InstCo co2 :: Coercion
co2 ty2 :: Coercion
ty2)
| Coercion
ty1 Coercion -> Coercion -> Bool
`eqCoercion` Coercion
ty2
, Coercion
co1 Coercion -> Coercion -> Bool
`compatible_co` Coercion
co2
= String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "TrPushInst" Coercion
in_co1 Coercion
in_co2 (Coercion -> Maybe Coercion) -> Coercion -> Maybe Coercion
forall a b. (a -> b) -> a -> b
$
Coercion -> Coercion -> Coercion
mkInstCo (InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is Coercion
co1 Coercion
co2) Coercion
ty1
opt_trans_rule is :: InScopeSet
is in_co1 :: Coercion
in_co1@(UnivCo p1 :: UnivCoProvenance
p1 r1 :: Role
r1 tyl1 :: Type
tyl1 _tyr1 :: Type
_tyr1)
in_co2 :: Coercion
in_co2@(UnivCo p2 :: UnivCoProvenance
p2 r2 :: Role
r2 _tyl2 :: Type
_tyl2 tyr2 :: Type
tyr2)
| Just prov' :: UnivCoProvenance
prov' <- UnivCoProvenance -> UnivCoProvenance -> Maybe UnivCoProvenance
opt_trans_prov UnivCoProvenance
p1 UnivCoProvenance
p2
= ASSERT( r1 == r2 )
String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "UnivCo" Coercion
in_co1 Coercion
in_co2 (Coercion -> Maybe Coercion) -> Coercion -> Maybe Coercion
forall a b. (a -> b) -> a -> b
$
UnivCoProvenance -> Role -> Type -> Type -> Coercion
mkUnivCo UnivCoProvenance
prov' Role
r1 Type
tyl1 Type
tyr2
where
opt_trans_prov :: UnivCoProvenance -> UnivCoProvenance -> Maybe UnivCoProvenance
opt_trans_prov UnsafeCoerceProv UnsafeCoerceProv = UnivCoProvenance -> Maybe UnivCoProvenance
forall a. a -> Maybe a
Just UnivCoProvenance
UnsafeCoerceProv
opt_trans_prov (PhantomProv kco1 :: Coercion
kco1) (PhantomProv kco2 :: Coercion
kco2)
= UnivCoProvenance -> Maybe UnivCoProvenance
forall a. a -> Maybe a
Just (UnivCoProvenance -> Maybe UnivCoProvenance)
-> UnivCoProvenance -> Maybe UnivCoProvenance
forall a b. (a -> b) -> a -> b
$ Coercion -> UnivCoProvenance
PhantomProv (Coercion -> UnivCoProvenance) -> Coercion -> UnivCoProvenance
forall a b. (a -> b) -> a -> b
$ InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is Coercion
kco1 Coercion
kco2
opt_trans_prov (ProofIrrelProv kco1 :: Coercion
kco1) (ProofIrrelProv kco2 :: Coercion
kco2)
= UnivCoProvenance -> Maybe UnivCoProvenance
forall a. a -> Maybe a
Just (UnivCoProvenance -> Maybe UnivCoProvenance)
-> UnivCoProvenance -> Maybe UnivCoProvenance
forall a b. (a -> b) -> a -> b
$ Coercion -> UnivCoProvenance
ProofIrrelProv (Coercion -> UnivCoProvenance) -> Coercion -> UnivCoProvenance
forall a b. (a -> b) -> a -> b
$ InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is Coercion
kco1 Coercion
kco2
opt_trans_prov (PluginProv str1 :: String
str1) (PluginProv str2 :: String
str2) | String
str1 String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
str2 = UnivCoProvenance -> Maybe UnivCoProvenance
forall a. a -> Maybe a
Just UnivCoProvenance
p1
opt_trans_prov _ _ = Maybe UnivCoProvenance
forall a. Maybe a
Nothing
opt_trans_rule is :: InScopeSet
is in_co1 :: Coercion
in_co1@(TyConAppCo r1 :: Role
r1 tc1 :: TyCon
tc1 cos1 :: [Coercion]
cos1) in_co2 :: Coercion
in_co2@(TyConAppCo r2 :: Role
r2 tc2 :: TyCon
tc2 cos2 :: [Coercion]
cos2)
| TyCon
tc1 TyCon -> TyCon -> Bool
forall a. Eq a => a -> a -> Bool
== TyCon
tc2
= ASSERT( r1 == r2 )
String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "PushTyConApp" Coercion
in_co1 Coercion
in_co2 (Coercion -> Maybe Coercion) -> Coercion -> Maybe Coercion
forall a b. (a -> b) -> a -> b
$
HasDebugCallStack => Role -> TyCon -> [Coercion] -> Coercion
Role -> TyCon -> [Coercion] -> Coercion
mkTyConAppCo Role
r1 TyCon
tc1 (InScopeSet -> [Coercion] -> [Coercion] -> [Coercion]
opt_transList InScopeSet
is [Coercion]
cos1 [Coercion]
cos2)
opt_trans_rule is :: InScopeSet
is in_co1 :: Coercion
in_co1@(FunCo r1 :: Role
r1 co1a :: Coercion
co1a co1b :: Coercion
co1b) in_co2 :: Coercion
in_co2@(FunCo r2 :: Role
r2 co2a :: Coercion
co2a co2b :: Coercion
co2b)
= ASSERT( r1 == r2 )
String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "PushFun" Coercion
in_co1 Coercion
in_co2 (Coercion -> Maybe Coercion) -> Coercion -> Maybe Coercion
forall a b. (a -> b) -> a -> b
$
Role -> Coercion -> Coercion -> Coercion
mkFunCo Role
r1 (InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is Coercion
co1a Coercion
co2a) (InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is Coercion
co1b Coercion
co2b)
opt_trans_rule is :: InScopeSet
is in_co1 :: Coercion
in_co1@(AppCo co1a :: Coercion
co1a co1b :: Coercion
co1b) in_co2 :: Coercion
in_co2@(AppCo co2a :: Coercion
co2a co2b :: Coercion
co2b)
= InScopeSet
-> Coercion
-> Coercion
-> Coercion
-> [Coercion]
-> Coercion
-> [Coercion]
-> Maybe Coercion
opt_trans_rule_app InScopeSet
is Coercion
in_co1 Coercion
in_co2 Coercion
co1a [Coercion
co1b] Coercion
co2a [Coercion
co2b]
opt_trans_rule is :: InScopeSet
is co1 :: Coercion
co1@(TyConAppCo r :: Role
r tc :: TyCon
tc cos1 :: [Coercion]
cos1) co2 :: Coercion
co2
| Just cos2 :: [Coercion]
cos2 <- TyCon -> Coercion -> Maybe [Coercion]
etaTyConAppCo_maybe TyCon
tc Coercion
co2
= ASSERT( cos1 `equalLength` cos2 )
String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "EtaCompL" Coercion
co1 Coercion
co2 (Coercion -> Maybe Coercion) -> Coercion -> Maybe Coercion
forall a b. (a -> b) -> a -> b
$
HasDebugCallStack => Role -> TyCon -> [Coercion] -> Coercion
Role -> TyCon -> [Coercion] -> Coercion
mkTyConAppCo Role
r TyCon
tc (InScopeSet -> [Coercion] -> [Coercion] -> [Coercion]
opt_transList InScopeSet
is [Coercion]
cos1 [Coercion]
cos2)
opt_trans_rule is :: InScopeSet
is co1 :: Coercion
co1 co2 :: Coercion
co2@(TyConAppCo r :: Role
r tc :: TyCon
tc cos2 :: [Coercion]
cos2)
| Just cos1 :: [Coercion]
cos1 <- TyCon -> Coercion -> Maybe [Coercion]
etaTyConAppCo_maybe TyCon
tc Coercion
co1
= ASSERT( cos1 `equalLength` cos2 )
String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "EtaCompR" Coercion
co1 Coercion
co2 (Coercion -> Maybe Coercion) -> Coercion -> Maybe Coercion
forall a b. (a -> b) -> a -> b
$
HasDebugCallStack => Role -> TyCon -> [Coercion] -> Coercion
Role -> TyCon -> [Coercion] -> Coercion
mkTyConAppCo Role
r TyCon
tc (InScopeSet -> [Coercion] -> [Coercion] -> [Coercion]
opt_transList InScopeSet
is [Coercion]
cos1 [Coercion]
cos2)
opt_trans_rule is :: InScopeSet
is co1 :: Coercion
co1@(AppCo co1a :: Coercion
co1a co1b :: Coercion
co1b) co2 :: Coercion
co2
| Just (co2a :: Coercion
co2a,co2b :: Coercion
co2b) <- Coercion -> Maybe (Coercion, Coercion)
etaAppCo_maybe Coercion
co2
= InScopeSet
-> Coercion
-> Coercion
-> Coercion
-> [Coercion]
-> Coercion
-> [Coercion]
-> Maybe Coercion
opt_trans_rule_app InScopeSet
is Coercion
co1 Coercion
co2 Coercion
co1a [Coercion
co1b] Coercion
co2a [Coercion
co2b]
opt_trans_rule is :: InScopeSet
is co1 :: Coercion
co1 co2 :: Coercion
co2@(AppCo co2a :: Coercion
co2a co2b :: Coercion
co2b)
| Just (co1a :: Coercion
co1a,co1b :: Coercion
co1b) <- Coercion -> Maybe (Coercion, Coercion)
etaAppCo_maybe Coercion
co1
= InScopeSet
-> Coercion
-> Coercion
-> Coercion
-> [Coercion]
-> Coercion
-> [Coercion]
-> Maybe Coercion
opt_trans_rule_app InScopeSet
is Coercion
co1 Coercion
co2 Coercion
co1a [Coercion
co1b] Coercion
co2a [Coercion
co2b]
opt_trans_rule is :: InScopeSet
is co1 :: Coercion
co1 co2 :: Coercion
co2
| Just (tv1 :: TyCoVar
tv1, eta1 :: Coercion
eta1, r1 :: Coercion
r1) <- Coercion -> Maybe (TyCoVar, Coercion, Coercion)
splitForAllCo_ty_maybe Coercion
co1
, Just (tv2 :: TyCoVar
tv2, eta2 :: Coercion
eta2, r2 :: Coercion
r2) <- Coercion -> Maybe (TyCoVar, Coercion, Coercion)
etaForAllCo_ty_maybe Coercion
co2
= TyCoVar
-> Coercion
-> Coercion
-> TyCoVar
-> Coercion
-> Coercion
-> Maybe Coercion
push_trans TyCoVar
tv1 Coercion
eta1 Coercion
r1 TyCoVar
tv2 Coercion
eta2 Coercion
r2
| Just (tv2 :: TyCoVar
tv2, eta2 :: Coercion
eta2, r2 :: Coercion
r2) <- Coercion -> Maybe (TyCoVar, Coercion, Coercion)
splitForAllCo_ty_maybe Coercion
co2
, Just (tv1 :: TyCoVar
tv1, eta1 :: Coercion
eta1, r1 :: Coercion
r1) <- Coercion -> Maybe (TyCoVar, Coercion, Coercion)
etaForAllCo_ty_maybe Coercion
co1
= TyCoVar
-> Coercion
-> Coercion
-> TyCoVar
-> Coercion
-> Coercion
-> Maybe Coercion
push_trans TyCoVar
tv1 Coercion
eta1 Coercion
r1 TyCoVar
tv2 Coercion
eta2 Coercion
r2
where
push_trans :: TyCoVar
-> Coercion
-> Coercion
-> TyCoVar
-> Coercion
-> Coercion
-> Maybe Coercion
push_trans tv1 :: TyCoVar
tv1 eta1 :: Coercion
eta1 r1 :: Coercion
r1 tv2 :: TyCoVar
tv2 eta2 :: Coercion
eta2 r2 :: Coercion
r2
= String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "EtaAllTy_ty" Coercion
co1 Coercion
co2 (Coercion -> Maybe Coercion) -> Coercion -> Maybe Coercion
forall a b. (a -> b) -> a -> b
$
TyCoVar -> Coercion -> Coercion -> Coercion
mkForAllCo TyCoVar
tv1 (InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is Coercion
eta1 Coercion
eta2) (InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is' Coercion
r1 Coercion
r2')
where
is' :: InScopeSet
is' = InScopeSet
is InScopeSet -> TyCoVar -> InScopeSet
`extendInScopeSet` TyCoVar
tv1
r2' :: Coercion
r2' = [TyCoVar] -> [Type] -> Coercion -> Coercion
substCoWithUnchecked [TyCoVar
tv2] [Type -> Coercion -> Type
mkCastTy (TyCoVar -> Type
TyVarTy TyCoVar
tv1) Coercion
eta1] Coercion
r2
opt_trans_rule is :: InScopeSet
is co1 :: Coercion
co1 co2 :: Coercion
co2
| Just (cv1 :: TyCoVar
cv1, eta1 :: Coercion
eta1, r1 :: Coercion
r1) <- Coercion -> Maybe (TyCoVar, Coercion, Coercion)
splitForAllCo_co_maybe Coercion
co1
, Just (cv2 :: TyCoVar
cv2, eta2 :: Coercion
eta2, r2 :: Coercion
r2) <- Coercion -> Maybe (TyCoVar, Coercion, Coercion)
etaForAllCo_co_maybe Coercion
co2
= TyCoVar
-> Coercion
-> Coercion
-> TyCoVar
-> Coercion
-> Coercion
-> Maybe Coercion
push_trans TyCoVar
cv1 Coercion
eta1 Coercion
r1 TyCoVar
cv2 Coercion
eta2 Coercion
r2
| Just (cv2 :: TyCoVar
cv2, eta2 :: Coercion
eta2, r2 :: Coercion
r2) <- Coercion -> Maybe (TyCoVar, Coercion, Coercion)
splitForAllCo_co_maybe Coercion
co2
, Just (cv1 :: TyCoVar
cv1, eta1 :: Coercion
eta1, r1 :: Coercion
r1) <- Coercion -> Maybe (TyCoVar, Coercion, Coercion)
etaForAllCo_co_maybe Coercion
co1
= TyCoVar
-> Coercion
-> Coercion
-> TyCoVar
-> Coercion
-> Coercion
-> Maybe Coercion
push_trans TyCoVar
cv1 Coercion
eta1 Coercion
r1 TyCoVar
cv2 Coercion
eta2 Coercion
r2
where
push_trans :: TyCoVar
-> Coercion
-> Coercion
-> TyCoVar
-> Coercion
-> Coercion
-> Maybe Coercion
push_trans cv1 :: TyCoVar
cv1 eta1 :: Coercion
eta1 r1 :: Coercion
r1 cv2 :: TyCoVar
cv2 eta2 :: Coercion
eta2 r2 :: Coercion
r2
= String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "EtaAllTy_co" Coercion
co1 Coercion
co2 (Coercion -> Maybe Coercion) -> Coercion -> Maybe Coercion
forall a b. (a -> b) -> a -> b
$
TyCoVar -> Coercion -> Coercion -> Coercion
mkForAllCo TyCoVar
cv1 (InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is Coercion
eta1 Coercion
eta2) (InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is' Coercion
r1 Coercion
r2')
where
is' :: InScopeSet
is' = InScopeSet
is InScopeSet -> TyCoVar -> InScopeSet
`extendInScopeSet` TyCoVar
cv1
role :: Role
role = TyCoVar -> Role
coVarRole TyCoVar
cv1
eta1' :: Coercion
eta1' = Role -> Role -> Coercion -> Coercion
downgradeRole Role
role Role
Nominal Coercion
eta1
n1 :: Coercion
n1 = HasDebugCallStack => Role -> Int -> Coercion -> Coercion
Role -> Int -> Coercion -> Coercion
mkNthCo Role
role 2 Coercion
eta1'
n2 :: Coercion
n2 = HasDebugCallStack => Role -> Int -> Coercion -> Coercion
Role -> Int -> Coercion -> Coercion
mkNthCo Role
role 3 Coercion
eta1'
r2' :: Coercion
r2' = HasCallStack => TCvSubst -> Coercion -> Coercion
TCvSubst -> Coercion -> Coercion
substCo ([TyCoVar] -> [Coercion] -> TCvSubst
HasDebugCallStack => [TyCoVar] -> [Coercion] -> TCvSubst
zipCvSubst [TyCoVar
cv2] [(Coercion -> Coercion
mkSymCo Coercion
n1) Coercion -> Coercion -> Coercion
`mkTransCo`
(TyCoVar -> Coercion
mkCoVarCo TyCoVar
cv1) Coercion -> Coercion -> Coercion
`mkTransCo` Coercion
n2])
Coercion
r2
opt_trans_rule is :: InScopeSet
is co1 :: Coercion
co1 co2 :: Coercion
co2
| Just (sym :: Bool
sym, con :: CoAxiom Branched
con, ind :: Int
ind, cos1 :: [Coercion]
cos1) <- Maybe (Bool, CoAxiom Branched, Int, [Coercion])
co1_is_axiom_maybe
, Bool
True <- Bool
sym
, Just cos2 :: [Coercion]
cos2 <- Bool -> CoAxiom Branched -> Int -> Coercion -> Maybe [Coercion]
forall (br :: BranchFlag).
Bool -> CoAxiom br -> Int -> Coercion -> Maybe [Coercion]
matchAxiom Bool
sym CoAxiom Branched
con Int
ind Coercion
co2
, let newAxInst :: Coercion
newAxInst = CoAxiom Branched -> Int -> [Coercion] -> Coercion
AxiomInstCo CoAxiom Branched
con Int
ind (InScopeSet -> [Coercion] -> [Coercion] -> [Coercion]
opt_transList InScopeSet
is ((Coercion -> Coercion) -> [Coercion] -> [Coercion]
forall a b. (a -> b) -> [a] -> [b]
map Coercion -> Coercion
mkSymCo [Coercion]
cos2) [Coercion]
cos1)
, Maybe CoAxBranch
Nothing <- Coercion -> Maybe CoAxBranch
checkAxInstCo Coercion
newAxInst
= String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "TrPushSymAxR" Coercion
co1 Coercion
co2 (Coercion -> Maybe Coercion) -> Coercion -> Maybe Coercion
forall a b. (a -> b) -> a -> b
$ Coercion -> Coercion
SymCo Coercion
newAxInst
| Just (sym :: Bool
sym, con :: CoAxiom Branched
con, ind :: Int
ind, cos1 :: [Coercion]
cos1) <- Maybe (Bool, CoAxiom Branched, Int, [Coercion])
co1_is_axiom_maybe
, Bool
False <- Bool
sym
, Just cos2 :: [Coercion]
cos2 <- Bool -> CoAxiom Branched -> Int -> Coercion -> Maybe [Coercion]
forall (br :: BranchFlag).
Bool -> CoAxiom br -> Int -> Coercion -> Maybe [Coercion]
matchAxiom Bool
sym CoAxiom Branched
con Int
ind Coercion
co2
, let newAxInst :: Coercion
newAxInst = CoAxiom Branched -> Int -> [Coercion] -> Coercion
AxiomInstCo CoAxiom Branched
con Int
ind (InScopeSet -> [Coercion] -> [Coercion] -> [Coercion]
opt_transList InScopeSet
is [Coercion]
cos1 [Coercion]
cos2)
, Maybe CoAxBranch
Nothing <- Coercion -> Maybe CoAxBranch
checkAxInstCo Coercion
newAxInst
= String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "TrPushAxR" Coercion
co1 Coercion
co2 Coercion
newAxInst
| Just (sym :: Bool
sym, con :: CoAxiom Branched
con, ind :: Int
ind, cos2 :: [Coercion]
cos2) <- Maybe (Bool, CoAxiom Branched, Int, [Coercion])
co2_is_axiom_maybe
, Bool
True <- Bool
sym
, Just cos1 :: [Coercion]
cos1 <- Bool -> CoAxiom Branched -> Int -> Coercion -> Maybe [Coercion]
forall (br :: BranchFlag).
Bool -> CoAxiom br -> Int -> Coercion -> Maybe [Coercion]
matchAxiom (Bool -> Bool
not Bool
sym) CoAxiom Branched
con Int
ind Coercion
co1
, let newAxInst :: Coercion
newAxInst = CoAxiom Branched -> Int -> [Coercion] -> Coercion
AxiomInstCo CoAxiom Branched
con Int
ind (InScopeSet -> [Coercion] -> [Coercion] -> [Coercion]
opt_transList InScopeSet
is [Coercion]
cos2 ((Coercion -> Coercion) -> [Coercion] -> [Coercion]
forall a b. (a -> b) -> [a] -> [b]
map Coercion -> Coercion
mkSymCo [Coercion]
cos1))
, Maybe CoAxBranch
Nothing <- Coercion -> Maybe CoAxBranch
checkAxInstCo Coercion
newAxInst
= String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "TrPushSymAxL" Coercion
co1 Coercion
co2 (Coercion -> Maybe Coercion) -> Coercion -> Maybe Coercion
forall a b. (a -> b) -> a -> b
$ Coercion -> Coercion
SymCo Coercion
newAxInst
| Just (sym :: Bool
sym, con :: CoAxiom Branched
con, ind :: Int
ind, cos2 :: [Coercion]
cos2) <- Maybe (Bool, CoAxiom Branched, Int, [Coercion])
co2_is_axiom_maybe
, Bool
False <- Bool
sym
, Just cos1 :: [Coercion]
cos1 <- Bool -> CoAxiom Branched -> Int -> Coercion -> Maybe [Coercion]
forall (br :: BranchFlag).
Bool -> CoAxiom br -> Int -> Coercion -> Maybe [Coercion]
matchAxiom (Bool -> Bool
not Bool
sym) CoAxiom Branched
con Int
ind Coercion
co1
, let newAxInst :: Coercion
newAxInst = CoAxiom Branched -> Int -> [Coercion] -> Coercion
AxiomInstCo CoAxiom Branched
con Int
ind (InScopeSet -> [Coercion] -> [Coercion] -> [Coercion]
opt_transList InScopeSet
is [Coercion]
cos1 [Coercion]
cos2)
, Maybe CoAxBranch
Nothing <- Coercion -> Maybe CoAxBranch
checkAxInstCo Coercion
newAxInst
= String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "TrPushAxL" Coercion
co1 Coercion
co2 Coercion
newAxInst
| Just (sym1 :: Bool
sym1, con1 :: CoAxiom Branched
con1, ind1 :: Int
ind1, cos1 :: [Coercion]
cos1) <- Maybe (Bool, CoAxiom Branched, Int, [Coercion])
co1_is_axiom_maybe
, Just (sym2 :: Bool
sym2, con2 :: CoAxiom Branched
con2, ind2 :: Int
ind2, cos2 :: [Coercion]
cos2) <- Maybe (Bool, CoAxiom Branched, Int, [Coercion])
co2_is_axiom_maybe
, CoAxiom Branched
con1 CoAxiom Branched -> CoAxiom Branched -> Bool
forall a. Eq a => a -> a -> Bool
== CoAxiom Branched
con2
, Int
ind1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
ind2
, Bool
sym1 Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool -> Bool
not Bool
sym2
, let branch :: CoAxBranch
branch = CoAxiom Branched -> Int -> CoAxBranch
forall (br :: BranchFlag). CoAxiom br -> Int -> CoAxBranch
coAxiomNthBranch CoAxiom Branched
con1 Int
ind1
qtvs :: [TyCoVar]
qtvs = CoAxBranch -> [TyCoVar]
coAxBranchTyVars CoAxBranch
branch [TyCoVar] -> [TyCoVar] -> [TyCoVar]
forall a. [a] -> [a] -> [a]
++ CoAxBranch -> [TyCoVar]
coAxBranchCoVars CoAxBranch
branch
lhs :: Type
lhs = CoAxiom Branched -> Int -> Type
forall (br :: BranchFlag). CoAxiom br -> Int -> Type
coAxNthLHS CoAxiom Branched
con1 Int
ind1
rhs :: Type
rhs = CoAxBranch -> Type
coAxBranchRHS CoAxBranch
branch
pivot_tvs :: TyCoVarSet
pivot_tvs = Type -> TyCoVarSet
exactTyCoVarsOfType (if Bool
sym2 then Type
rhs else Type
lhs)
, (TyCoVar -> Bool) -> [TyCoVar] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TyCoVar -> TyCoVarSet -> Bool
`elemVarSet` TyCoVarSet
pivot_tvs) [TyCoVar]
qtvs
= String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "TrPushAxSym" Coercion
co1 Coercion
co2 (Coercion -> Maybe Coercion) -> Coercion -> Maybe Coercion
forall a b. (a -> b) -> a -> b
$
if Bool
sym2
then Role -> [TyCoVar] -> [Coercion] -> Type -> Coercion
liftCoSubstWith Role
role [TyCoVar]
qtvs (InScopeSet -> [Coercion] -> [Coercion] -> [Coercion]
opt_transList InScopeSet
is [Coercion]
cos1 ((Coercion -> Coercion) -> [Coercion] -> [Coercion]
forall a b. (a -> b) -> [a] -> [b]
map Coercion -> Coercion
mkSymCo [Coercion]
cos2)) Type
lhs
else Role -> [TyCoVar] -> [Coercion] -> Type -> Coercion
liftCoSubstWith Role
role [TyCoVar]
qtvs (InScopeSet -> [Coercion] -> [Coercion] -> [Coercion]
opt_transList InScopeSet
is ((Coercion -> Coercion) -> [Coercion] -> [Coercion]
forall a b. (a -> b) -> [a] -> [b]
map Coercion -> Coercion
mkSymCo [Coercion]
cos1) [Coercion]
cos2) Type
rhs
where
co1_is_axiom_maybe :: Maybe (Bool, CoAxiom Branched, Int, [Coercion])
co1_is_axiom_maybe = Coercion -> Maybe (Bool, CoAxiom Branched, Int, [Coercion])
isAxiom_maybe Coercion
co1
co2_is_axiom_maybe :: Maybe (Bool, CoAxiom Branched, Int, [Coercion])
co2_is_axiom_maybe = Coercion -> Maybe (Bool, CoAxiom Branched, Int, [Coercion])
isAxiom_maybe Coercion
co2
role :: Role
role = Coercion -> Role
coercionRole Coercion
co1
opt_trans_rule _ co1 :: Coercion
co1 co2 :: Coercion
co2
| (Pair ty1 :: Type
ty1 _, r :: Role
r) <- Coercion -> (Pair Type, Role)
coercionKindRole Coercion
co1
, Pair _ ty2 :: Type
ty2 <- Coercion -> Pair Type
coercionKind Coercion
co2
, Type
ty1 Type -> Type -> Bool
`eqType` Type
ty2
= String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule "RedTypeDirRefl" Coercion
co1 Coercion
co2 (Coercion -> Maybe Coercion) -> Coercion -> Maybe Coercion
forall a b. (a -> b) -> a -> b
$
Role -> Type -> Coercion
mkReflCo Role
r Type
ty2
opt_trans_rule _ _ _ = Maybe Coercion
forall a. Maybe a
Nothing
opt_trans_rule_app :: InScopeSet
-> Coercion
-> Coercion
-> Coercion
-> [Coercion]
-> Coercion
-> [Coercion]
-> Maybe Coercion
opt_trans_rule_app :: InScopeSet
-> Coercion
-> Coercion
-> Coercion
-> [Coercion]
-> Coercion
-> [Coercion]
-> Maybe Coercion
opt_trans_rule_app is :: InScopeSet
is orig_co1 :: Coercion
orig_co1 orig_co2 :: Coercion
orig_co2 co1a :: Coercion
co1a co1bs :: [Coercion]
co1bs co2a :: Coercion
co2a co2bs :: [Coercion]
co2bs
| AppCo co1aa :: Coercion
co1aa co1ab :: Coercion
co1ab <- Coercion
co1a
, Just (co2aa :: Coercion
co2aa, co2ab :: Coercion
co2ab) <- Coercion -> Maybe (Coercion, Coercion)
etaAppCo_maybe Coercion
co2a
= InScopeSet
-> Coercion
-> Coercion
-> Coercion
-> [Coercion]
-> Coercion
-> [Coercion]
-> Maybe Coercion
opt_trans_rule_app InScopeSet
is Coercion
orig_co1 Coercion
orig_co2 Coercion
co1aa (Coercion
co1abCoercion -> [Coercion] -> [Coercion]
forall a. a -> [a] -> [a]
:[Coercion]
co1bs) Coercion
co2aa (Coercion
co2abCoercion -> [Coercion] -> [Coercion]
forall a. a -> [a] -> [a]
:[Coercion]
co2bs)
| AppCo co2aa :: Coercion
co2aa co2ab :: Coercion
co2ab <- Coercion
co2a
, Just (co1aa :: Coercion
co1aa, co1ab :: Coercion
co1ab) <- Coercion -> Maybe (Coercion, Coercion)
etaAppCo_maybe Coercion
co1a
= InScopeSet
-> Coercion
-> Coercion
-> Coercion
-> [Coercion]
-> Coercion
-> [Coercion]
-> Maybe Coercion
opt_trans_rule_app InScopeSet
is Coercion
orig_co1 Coercion
orig_co2 Coercion
co1aa (Coercion
co1abCoercion -> [Coercion] -> [Coercion]
forall a. a -> [a] -> [a]
:[Coercion]
co1bs) Coercion
co2aa (Coercion
co2abCoercion -> [Coercion] -> [Coercion]
forall a. a -> [a] -> [a]
:[Coercion]
co2bs)
| Bool
otherwise
= ASSERT( co1bs `equalLength` co2bs )
String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule ("EtaApps:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show ([Coercion] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Coercion]
co1bs)) Coercion
orig_co1 Coercion
orig_co2 (Coercion -> Maybe Coercion) -> Coercion -> Maybe Coercion
forall a b. (a -> b) -> a -> b
$
let Pair _ rt1a :: Type
rt1a = Coercion -> Pair Type
coercionKind Coercion
co1a
(Pair lt2a :: Type
lt2a _, rt2a :: Role
rt2a) = Coercion -> (Pair Type, Role)
coercionKindRole Coercion
co2a
Pair _ rt1bs :: [Type]
rt1bs = (Coercion -> Pair Type) -> [Coercion] -> Pair [Type]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Coercion -> Pair Type
coercionKind [Coercion]
co1bs
Pair lt2bs :: [Type]
lt2bs _ = (Coercion -> Pair Type) -> [Coercion] -> Pair [Type]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Coercion -> Pair Type
coercionKind [Coercion]
co2bs
rt2bs :: [Role]
rt2bs = (Coercion -> Role) -> [Coercion] -> [Role]
forall a b. (a -> b) -> [a] -> [b]
map Coercion -> Role
coercionRole [Coercion]
co2bs
kcoa :: Coercion
kcoa = Coercion -> Coercion
mkKindCo (Coercion -> Coercion) -> Coercion -> Coercion
forall a b. (a -> b) -> a -> b
$ Type -> Type -> Coercion
buildCoercion Type
lt2a Type
rt1a
kcobs :: [Coercion]
kcobs = (Coercion -> Coercion) -> [Coercion] -> [Coercion]
forall a b. (a -> b) -> [a] -> [b]
map Coercion -> Coercion
mkKindCo ([Coercion] -> [Coercion]) -> [Coercion] -> [Coercion]
forall a b. (a -> b) -> a -> b
$ (Type -> Type -> Coercion) -> [Type] -> [Type] -> [Coercion]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Type -> Type -> Coercion
buildCoercion [Type]
lt2bs [Type]
rt1bs
co2a' :: Coercion
co2a' = Role -> Type -> Coercion -> Coercion -> Coercion
mkCoherenceLeftCo Role
rt2a Type
lt2a Coercion
kcoa Coercion
co2a
co2bs' :: [Coercion]
co2bs' = (Role -> Type -> Coercion -> Coercion)
-> [Role] -> [Type] -> [Coercion] -> [Coercion]
forall a b c d. (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
zipWith3 Role -> Type -> Coercion -> Coercion
mkGReflLeftCo [Role]
rt2bs [Type]
lt2bs [Coercion]
kcobs
co2bs'' :: [Coercion]
co2bs'' = (Coercion -> Coercion -> Coercion)
-> [Coercion] -> [Coercion] -> [Coercion]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Coercion -> Coercion -> Coercion
mkTransCo [Coercion]
co2bs' [Coercion]
co2bs
in
Coercion -> [Coercion] -> Coercion
mkAppCos (InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is Coercion
co1a Coercion
co2a')
((Coercion -> Coercion -> Coercion)
-> [Coercion] -> [Coercion] -> [Coercion]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (InScopeSet -> Coercion -> Coercion -> Coercion
opt_trans InScopeSet
is) [Coercion]
co1bs [Coercion]
co2bs'')
fireTransRule :: String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule :: String -> Coercion -> Coercion -> Coercion -> Maybe Coercion
fireTransRule _rule :: String
_rule _co1 :: Coercion
_co1 _co2 :: Coercion
_co2 res :: Coercion
res
= Coercion -> Maybe Coercion
forall a. a -> Maybe a
Just Coercion
res
checkAxInstCo :: Coercion -> Maybe CoAxBranch
checkAxInstCo :: Coercion -> Maybe CoAxBranch
checkAxInstCo (AxiomInstCo ax :: CoAxiom Branched
ax ind :: Int
ind cos :: [Coercion]
cos)
= let branch :: CoAxBranch
branch = CoAxiom Branched -> Int -> CoAxBranch
forall (br :: BranchFlag). CoAxiom br -> Int -> CoAxBranch
coAxiomNthBranch CoAxiom Branched
ax Int
ind
tvs :: [TyCoVar]
tvs = CoAxBranch -> [TyCoVar]
coAxBranchTyVars CoAxBranch
branch
cvs :: [TyCoVar]
cvs = CoAxBranch -> [TyCoVar]
coAxBranchCoVars CoAxBranch
branch
incomps :: [CoAxBranch]
incomps = CoAxBranch -> [CoAxBranch]
coAxBranchIncomps CoAxBranch
branch
(tys :: [Type]
tys, cotys :: [Type]
cotys) = [TyCoVar] -> [Type] -> ([Type], [Type])
forall b a. [b] -> [a] -> ([a], [a])
splitAtList [TyCoVar]
tvs ((Coercion -> Type) -> [Coercion] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map (Pair Type -> Type
forall a. Pair a -> a
pFst (Pair Type -> Type) -> (Coercion -> Pair Type) -> Coercion -> Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Coercion -> Pair Type
coercionKind) [Coercion]
cos)
co_args :: [Coercion]
co_args = (Type -> Coercion) -> [Type] -> [Coercion]
forall a b. (a -> b) -> [a] -> [b]
map Type -> Coercion
stripCoercionTy [Type]
cotys
subst :: TCvSubst
subst = [TyCoVar] -> [Type] -> TCvSubst
HasDebugCallStack => [TyCoVar] -> [Type] -> TCvSubst
zipTvSubst [TyCoVar]
tvs [Type]
tys TCvSubst -> TCvSubst -> TCvSubst
`composeTCvSubst`
[TyCoVar] -> [Coercion] -> TCvSubst
HasDebugCallStack => [TyCoVar] -> [Coercion] -> TCvSubst
zipCvSubst [TyCoVar]
cvs [Coercion]
co_args
target :: [Type]
target = HasCallStack => TCvSubst -> [Type] -> [Type]
TCvSubst -> [Type] -> [Type]
Type.substTys TCvSubst
subst (CoAxBranch -> [Type]
coAxBranchLHS CoAxBranch
branch)
in_scope :: InScopeSet
in_scope = TyCoVarSet -> InScopeSet
mkInScopeSet (TyCoVarSet -> InScopeSet) -> TyCoVarSet -> InScopeSet
forall a b. (a -> b) -> a -> b
$
[TyCoVarSet] -> TyCoVarSet
unionVarSets ((CoAxBranch -> TyCoVarSet) -> [CoAxBranch] -> [TyCoVarSet]
forall a b. (a -> b) -> [a] -> [b]
map ([Type] -> TyCoVarSet
tyCoVarsOfTypes ([Type] -> TyCoVarSet)
-> (CoAxBranch -> [Type]) -> CoAxBranch -> TyCoVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoAxBranch -> [Type]
coAxBranchLHS) [CoAxBranch]
incomps)
flattened_target :: [Type]
flattened_target = InScopeSet -> [Type] -> [Type]
flattenTys InScopeSet
in_scope [Type]
target in
[Type] -> [CoAxBranch] -> Maybe CoAxBranch
check_no_conflict [Type]
flattened_target [CoAxBranch]
incomps
where
check_no_conflict :: [Type] -> [CoAxBranch] -> Maybe CoAxBranch
check_no_conflict :: [Type] -> [CoAxBranch] -> Maybe CoAxBranch
check_no_conflict _ [] = Maybe CoAxBranch
forall a. Maybe a
Nothing
check_no_conflict flat :: [Type]
flat (b :: CoAxBranch
b@CoAxBranch { cab_lhs :: CoAxBranch -> [Type]
cab_lhs = [Type]
lhs_incomp } : rest :: [CoAxBranch]
rest)
| UnifyResultM TCvSubst
SurelyApart <- (TyCoVar -> BindFlag) -> [Type] -> [Type] -> UnifyResultM TCvSubst
tcUnifyTysFG TyCoVar -> BindFlag
instanceBindFun [Type]
flat [Type]
lhs_incomp
= [Type] -> [CoAxBranch] -> Maybe CoAxBranch
check_no_conflict [Type]
flat [CoAxBranch]
rest
| Bool
otherwise
= CoAxBranch -> Maybe CoAxBranch
forall a. a -> Maybe a
Just CoAxBranch
b
checkAxInstCo _ = Maybe CoAxBranch
forall a. Maybe a
Nothing
wrapSym :: SymFlag -> Coercion -> Coercion
wrapSym :: Bool -> Coercion -> Coercion
wrapSym sym :: Bool
sym co :: Coercion
co | Bool
sym = Coercion -> Coercion
mkSymCo Coercion
co
| Bool
otherwise = Coercion
co
wrapRole :: ReprFlag
-> Role
-> Coercion -> Coercion
wrapRole :: Bool -> Role -> Coercion -> Coercion
wrapRole False _ = Coercion -> Coercion
forall a. a -> a
id
wrapRole True current :: Role
current = Role -> Role -> Coercion -> Coercion
downgradeRole Role
Representational Role
current
chooseRole :: ReprFlag
-> Role
-> Role
chooseRole :: Bool -> Role -> Role
chooseRole True _ = Role
Representational
chooseRole _ r :: Role
r = Role
r
isAxiom_maybe :: Coercion -> Maybe (Bool, CoAxiom Branched, Int, [Coercion])
isAxiom_maybe :: Coercion -> Maybe (Bool, CoAxiom Branched, Int, [Coercion])
isAxiom_maybe (SymCo co :: Coercion
co)
| Just (sym :: Bool
sym, con :: CoAxiom Branched
con, ind :: Int
ind, cos :: [Coercion]
cos) <- Coercion -> Maybe (Bool, CoAxiom Branched, Int, [Coercion])
isAxiom_maybe Coercion
co
= (Bool, CoAxiom Branched, Int, [Coercion])
-> Maybe (Bool, CoAxiom Branched, Int, [Coercion])
forall a. a -> Maybe a
Just (Bool -> Bool
not Bool
sym, CoAxiom Branched
con, Int
ind, [Coercion]
cos)
isAxiom_maybe (AxiomInstCo con :: CoAxiom Branched
con ind :: Int
ind cos :: [Coercion]
cos)
= (Bool, CoAxiom Branched, Int, [Coercion])
-> Maybe (Bool, CoAxiom Branched, Int, [Coercion])
forall a. a -> Maybe a
Just (Bool
False, CoAxiom Branched
con, Int
ind, [Coercion]
cos)
isAxiom_maybe _ = Maybe (Bool, CoAxiom Branched, Int, [Coercion])
forall a. Maybe a
Nothing
matchAxiom :: Bool
-> CoAxiom br -> Int -> Coercion -> Maybe [Coercion]
matchAxiom :: Bool -> CoAxiom br -> Int -> Coercion -> Maybe [Coercion]
matchAxiom sym :: Bool
sym ax :: CoAxiom br
ax@(CoAxiom { co_ax_tc :: forall (br :: BranchFlag). CoAxiom br -> TyCon
co_ax_tc = TyCon
tc }) ind :: Int
ind co :: Coercion
co
| CoAxBranch { cab_tvs :: CoAxBranch -> [TyCoVar]
cab_tvs = [TyCoVar]
qtvs
, cab_cvs :: CoAxBranch -> [TyCoVar]
cab_cvs = []
, cab_roles :: CoAxBranch -> [Role]
cab_roles = [Role]
roles
, cab_lhs :: CoAxBranch -> [Type]
cab_lhs = [Type]
lhs
, cab_rhs :: CoAxBranch -> Type
cab_rhs = Type
rhs } <- CoAxiom br -> Int -> CoAxBranch
forall (br :: BranchFlag). CoAxiom br -> Int -> CoAxBranch
coAxiomNthBranch CoAxiom br
ax Int
ind
, Just subst :: LiftingContext
subst <- TyCoVarSet -> Type -> Coercion -> Maybe LiftingContext
liftCoMatch ([TyCoVar] -> TyCoVarSet
mkVarSet [TyCoVar]
qtvs)
(if Bool
sym then (TyCon -> [Type] -> Type
mkTyConApp TyCon
tc [Type]
lhs) else Type
rhs)
Coercion
co
, (TyCoVar -> Bool) -> [TyCoVar] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TyCoVar -> LiftingContext -> Bool
`isMappedByLC` LiftingContext
subst) [TyCoVar]
qtvs
= (Role -> TyCoVar -> Maybe Coercion)
-> [Role] -> [TyCoVar] -> Maybe [Coercion]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM (LiftingContext -> Role -> TyCoVar -> Maybe Coercion
liftCoSubstTyVar LiftingContext
subst) [Role]
roles [TyCoVar]
qtvs
| Bool
otherwise
= Maybe [Coercion]
forall a. Maybe a
Nothing
compatible_co :: Coercion -> Coercion -> Bool
compatible_co :: Coercion -> Coercion -> Bool
compatible_co co1 :: Coercion
co1 co2 :: Coercion
co2
= Type
x1 Type -> Type -> Bool
`eqType` Type
x2
where
Pair _ x1 :: Type
x1 = Coercion -> Pair Type
coercionKind Coercion
co1
Pair x2 :: Type
x2 _ = Coercion -> Pair Type
coercionKind Coercion
co2
etaForAllCo_ty_maybe :: Coercion -> Maybe (TyVar, Coercion, Coercion)
etaForAllCo_ty_maybe :: Coercion -> Maybe (TyCoVar, Coercion, Coercion)
etaForAllCo_ty_maybe co :: Coercion
co
| Just (tv :: TyCoVar
tv, kind_co :: Coercion
kind_co, r :: Coercion
r) <- Coercion -> Maybe (TyCoVar, Coercion, Coercion)
splitForAllCo_ty_maybe Coercion
co
= (TyCoVar, Coercion, Coercion)
-> Maybe (TyCoVar, Coercion, Coercion)
forall a. a -> Maybe a
Just (TyCoVar
tv, Coercion
kind_co, Coercion
r)
| Pair ty1 :: Type
ty1 ty2 :: Type
ty2 <- Coercion -> Pair Type
coercionKind Coercion
co
, Just (tv1 :: TyCoVar
tv1, _) <- Type -> Maybe (TyCoVar, Type)
splitForAllTy_ty_maybe Type
ty1
, Type -> Bool
isForAllTy_ty Type
ty2
, let kind_co :: Coercion
kind_co = HasDebugCallStack => Role -> Int -> Coercion -> Coercion
Role -> Int -> Coercion -> Coercion
mkNthCo Role
Nominal 0 Coercion
co
= (TyCoVar, Coercion, Coercion)
-> Maybe (TyCoVar, Coercion, Coercion)
forall a. a -> Maybe a
Just ( TyCoVar
tv1, Coercion
kind_co
, Coercion -> Coercion -> Coercion
mkInstCo Coercion
co (Role -> Type -> Coercion -> Coercion
mkGReflRightCo Role
Nominal (TyCoVar -> Type
TyVarTy TyCoVar
tv1) Coercion
kind_co))
| Bool
otherwise
= Maybe (TyCoVar, Coercion, Coercion)
forall a. Maybe a
Nothing
etaForAllCo_co_maybe :: Coercion -> Maybe (CoVar, Coercion, Coercion)
etaForAllCo_co_maybe :: Coercion -> Maybe (TyCoVar, Coercion, Coercion)
etaForAllCo_co_maybe co :: Coercion
co
| Just (cv :: TyCoVar
cv, kind_co :: Coercion
kind_co, r :: Coercion
r) <- Coercion -> Maybe (TyCoVar, Coercion, Coercion)
splitForAllCo_co_maybe Coercion
co
= (TyCoVar, Coercion, Coercion)
-> Maybe (TyCoVar, Coercion, Coercion)
forall a. a -> Maybe a
Just (TyCoVar
cv, Coercion
kind_co, Coercion
r)
| Pair ty1 :: Type
ty1 ty2 :: Type
ty2 <- Coercion -> Pair Type
coercionKind Coercion
co
, Just (cv1 :: TyCoVar
cv1, _) <- Type -> Maybe (TyCoVar, Type)
splitForAllTy_co_maybe Type
ty1
, Type -> Bool
isForAllTy_co Type
ty2
= let kind_co :: Coercion
kind_co = HasDebugCallStack => Role -> Int -> Coercion -> Coercion
Role -> Int -> Coercion -> Coercion
mkNthCo Role
Nominal 0 Coercion
co
r :: Role
r = TyCoVar -> Role
coVarRole TyCoVar
cv1
l_co :: Coercion
l_co = TyCoVar -> Coercion
mkCoVarCo TyCoVar
cv1
kind_co' :: Coercion
kind_co' = Role -> Role -> Coercion -> Coercion
downgradeRole Role
r Role
Nominal Coercion
kind_co
r_co :: Coercion
r_co = (Coercion -> Coercion
mkSymCo (HasDebugCallStack => Role -> Int -> Coercion -> Coercion
Role -> Int -> Coercion -> Coercion
mkNthCo Role
r 2 Coercion
kind_co')) Coercion -> Coercion -> Coercion
`mkTransCo`
Coercion
l_co Coercion -> Coercion -> Coercion
`mkTransCo`
(HasDebugCallStack => Role -> Int -> Coercion -> Coercion
Role -> Int -> Coercion -> Coercion
mkNthCo Role
r 3 Coercion
kind_co')
in (TyCoVar, Coercion, Coercion)
-> Maybe (TyCoVar, Coercion, Coercion)
forall a. a -> Maybe a
Just ( TyCoVar
cv1, Coercion
kind_co
, Coercion -> Coercion -> Coercion
mkInstCo Coercion
co (Role -> Coercion -> Coercion -> Coercion -> Coercion
mkProofIrrelCo Role
Nominal Coercion
kind_co Coercion
l_co Coercion
r_co))
| Bool
otherwise
= Maybe (TyCoVar, Coercion, Coercion)
forall a. Maybe a
Nothing
etaAppCo_maybe :: Coercion -> Maybe (Coercion,Coercion)
etaAppCo_maybe :: Coercion -> Maybe (Coercion, Coercion)
etaAppCo_maybe co :: Coercion
co
| Just (co1 :: Coercion
co1,co2 :: Coercion
co2) <- Coercion -> Maybe (Coercion, Coercion)
splitAppCo_maybe Coercion
co
= (Coercion, Coercion) -> Maybe (Coercion, Coercion)
forall a. a -> Maybe a
Just (Coercion
co1,Coercion
co2)
| (Pair ty1 :: Type
ty1 ty2 :: Type
ty2, Nominal) <- Coercion -> (Pair Type, Role)
coercionKindRole Coercion
co
, Just (_,t1 :: Type
t1) <- Type -> Maybe (Type, Type)
splitAppTy_maybe Type
ty1
, Just (_,t2 :: Type
t2) <- Type -> Maybe (Type, Type)
splitAppTy_maybe Type
ty2
, let isco1 :: Bool
isco1 = Type -> Bool
isCoercionTy Type
t1
, let isco2 :: Bool
isco2 = Type -> Bool
isCoercionTy Type
t2
, Bool
isco1 Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool
isco2
= (Coercion, Coercion) -> Maybe (Coercion, Coercion)
forall a. a -> Maybe a
Just (LeftOrRight -> Coercion -> Coercion
LRCo LeftOrRight
CLeft Coercion
co, LeftOrRight -> Coercion -> Coercion
LRCo LeftOrRight
CRight Coercion
co)
| Bool
otherwise
= Maybe (Coercion, Coercion)
forall a. Maybe a
Nothing
etaTyConAppCo_maybe :: TyCon -> Coercion -> Maybe [Coercion]
etaTyConAppCo_maybe :: TyCon -> Coercion -> Maybe [Coercion]
etaTyConAppCo_maybe tc :: TyCon
tc (TyConAppCo _ tc2 :: TyCon
tc2 cos2 :: [Coercion]
cos2)
= ASSERT( tc == tc2 ) Just cos2
etaTyConAppCo_maybe tc :: TyCon
tc co :: Coercion
co
| TyCon -> Bool
mightBeUnsaturatedTyCon TyCon
tc
, (Pair ty1 :: Type
ty1 ty2 :: Type
ty2, r :: Role
r) <- Coercion -> (Pair Type, Role)
coercionKindRole Coercion
co
, Just (tc1 :: TyCon
tc1, tys1 :: [Type]
tys1) <- HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty1
, Just (tc2 :: TyCon
tc2, tys2 :: [Type]
tys2) <- HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty2
, TyCon
tc1 TyCon -> TyCon -> Bool
forall a. Eq a => a -> a -> Bool
== TyCon
tc2
, TyCon -> Role -> Bool
isInjectiveTyCon TyCon
tc Role
r
, let n :: Int
n = [Type] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Type]
tys1
, [Type]
tys2 [Type] -> Int -> Bool
forall a. [a] -> Int -> Bool
`lengthIs` Int
n
= ASSERT( tc == tc1 )
[Coercion] -> Maybe [Coercion]
forall a. a -> Maybe a
Just (Int -> Coercion -> [Role] -> [Coercion]
decomposeCo Int
n Coercion
co (Role -> TyCon -> [Role]
tyConRolesX Role
r TyCon
tc1))
| Bool
otherwise
= Maybe [Coercion]
forall a. Maybe a
Nothing
optForAllCoBndr :: LiftingContext -> Bool
-> TyCoVar -> Coercion -> (LiftingContext, TyCoVar, Coercion)
optForAllCoBndr :: LiftingContext
-> Bool
-> TyCoVar
-> Coercion
-> (LiftingContext, TyCoVar, Coercion)
optForAllCoBndr env :: LiftingContext
env sym :: Bool
sym
= Bool
-> (Coercion -> Coercion)
-> LiftingContext
-> TyCoVar
-> Coercion
-> (LiftingContext, TyCoVar, Coercion)
substForAllCoBndrUsingLC Bool
sym (LiftingContext -> Bool -> Bool -> Role -> Coercion -> Coercion
opt_co4_wrap LiftingContext
env Bool
sym Bool
False Role
Nominal) LiftingContext
env