module Agda.TypeChecking.Substitute.Class where
import Control.Arrow ((***), second)
import Agda.Syntax.Common
import Agda.Syntax.Internal
import Agda.TypeChecking.Free
import Agda.TypeChecking.Substitute.DeBruijn
import Agda.Utils.Empty
import Agda.Utils.List
import Agda.Utils.Impossible
class Apply t where
apply :: t -> Args -> t
applyE :: t -> Elims -> t
apply t
t Args
args = forall t. Apply t => t -> Elims -> t
applyE t
t forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a. Arg a -> Elim' a
Apply Args
args
applys :: Apply t => t -> [Term] -> t
applys :: forall t. Apply t => t -> [Term] -> t
applys t
t [Term]
vs = forall t. Apply t => t -> Args -> t
apply t
t forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a. a -> Arg a
defaultArg [Term]
vs
apply1 :: Apply t => t -> Term -> t
apply1 :: forall t. Apply t => t -> Term -> t
apply1 t
t Term
u = forall t. Apply t => t -> [Term] -> t
applys t
t [ Term
u ]
class Abstract t where
abstract :: Telescope -> t -> t
class DeBruijn (SubstArg a) => Subst a where
type SubstArg a
applySubst :: Substitution' (SubstArg a) -> a -> a
default applySubst :: (a ~ f b, Functor f, Subst b, SubstArg a ~ SubstArg b) => Substitution' (SubstArg a) -> a -> a
applySubst Substitution' (SubstArg a)
rho = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst Substitution' (SubstArg a)
rho)
type SubstWith t a = (Subst a, SubstArg a ~ t)
type EndoSubst a = SubstWith a a
type TermSubst a = SubstWith Term a
raise :: Subst a => Nat -> a -> a
raise :: forall a. Subst a => Nat -> a -> a
raise = forall a. Subst a => Nat -> Nat -> a -> a
raiseFrom Nat
0
raiseFrom :: Subst a => Nat -> Nat -> a -> a
raiseFrom :: forall a. Subst a => Nat -> Nat -> a -> a
raiseFrom Nat
n Nat
k = forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst (forall a. Nat -> Nat -> Substitution' a
raiseFromS Nat
n Nat
k)
subst :: Subst a => Int -> SubstArg a -> a -> a
subst :: forall a. Subst a => Nat -> SubstArg a -> a -> a
subst Nat
i SubstArg a
u = forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst forall a b. (a -> b) -> a -> b
$ forall a. DeBruijn a => Nat -> a -> Substitution' a
singletonS Nat
i SubstArg a
u
strengthen :: Subst a => Impossible -> a -> a
strengthen :: forall a. Subst a => Impossible -> a -> a
strengthen Impossible
err = forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst (forall a. Impossible -> Nat -> Substitution' a
strengthenS Impossible
err Nat
1)
substUnder :: Subst a => Nat -> SubstArg a -> a -> a
substUnder :: forall a. Subst a => Nat -> SubstArg a -> a -> a
substUnder Nat
n SubstArg a
u = forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst (forall a. Nat -> Substitution' a -> Substitution' a
liftS Nat
n (forall a. DeBruijn a => Nat -> a -> Substitution' a
singletonS Nat
0 SubstArg a
u))
instance Subst QName where
type SubstArg QName = Term
applySubst :: Substitution' (SubstArg QName) -> QName -> QName
applySubst Substitution' (SubstArg QName)
_ QName
q = QName
q
idS :: Substitution' a
idS :: forall a. Substitution' a
idS = forall a. Substitution' a
IdS
wkS :: Int -> Substitution' a -> Substitution' a
wkS :: forall a. Nat -> Substitution' a -> Substitution' a
wkS Nat
0 Substitution' a
rho = Substitution' a
rho
wkS Nat
n (Wk Nat
m Substitution' a
rho) = forall a. Nat -> Substitution' a -> Substitution' a
Wk (Nat
n forall a. Num a => a -> a -> a
+ Nat
m) Substitution' a
rho
wkS Nat
n (EmptyS Impossible
err) = forall a. Impossible -> Substitution' a
EmptyS Impossible
err
wkS Nat
n Substitution' a
rho = forall a. Nat -> Substitution' a -> Substitution' a
Wk Nat
n Substitution' a
rho
raiseS :: Int -> Substitution' a
raiseS :: forall a. Nat -> Substitution' a
raiseS Nat
n = forall a. Nat -> Substitution' a -> Substitution' a
wkS Nat
n forall a. Substitution' a
idS
consS :: DeBruijn a => a -> Substitution' a -> Substitution' a
consS :: forall a. DeBruijn a => a -> Substitution' a -> Substitution' a
consS a
t (Wk Nat
m Substitution' a
rho)
| Just Nat
n <- forall a. DeBruijn a => a -> Maybe Nat
deBruijnView a
t,
Nat
n forall a. Num a => a -> a -> a
+ Nat
1 forall a. Eq a => a -> a -> Bool
== Nat
m = forall a. Nat -> Substitution' a -> Substitution' a
wkS (Nat
m forall a. Num a => a -> a -> a
- Nat
1) (forall a. Nat -> Substitution' a -> Substitution' a
liftS Nat
1 Substitution' a
rho)
consS a
u Substitution' a
rho = seq :: forall a b. a -> b -> b
seq a
u (a
u forall a. a -> Substitution' a -> Substitution' a
:# Substitution' a
rho)
singletonS :: DeBruijn a => Int -> a -> Substitution' a
singletonS :: forall a. DeBruijn a => Nat -> a -> Substitution' a
singletonS Nat
n a
u = forall a b. (a -> b) -> [a] -> [b]
map forall a. DeBruijn a => Nat -> a
deBruijnVar [Nat
0..Nat
nforall a. Num a => a -> a -> a
-Nat
1] forall a. DeBruijn a => [a] -> Substitution' a -> Substitution' a
++# forall a. DeBruijn a => a -> Substitution' a -> Substitution' a
consS a
u (forall a. Nat -> Substitution' a
raiseS Nat
n)
inplaceS :: EndoSubst a => Int -> a -> Substitution' a
inplaceS :: forall a. EndoSubst a => Nat -> a -> Substitution' a
inplaceS Nat
k a
u = forall a. DeBruijn a => Nat -> a -> Substitution' a
singletonS Nat
k a
u forall a.
EndoSubst a =>
Substitution' a -> Substitution' a -> Substitution' a
`composeS` forall a. Nat -> Substitution' a -> Substitution' a
liftS (Nat
k forall a. Num a => a -> a -> a
+ Nat
1) (forall a. Nat -> Substitution' a
raiseS Nat
1)
liftS :: Int -> Substitution' a -> Substitution' a
liftS :: forall a. Nat -> Substitution' a -> Substitution' a
liftS Nat
0 Substitution' a
rho = Substitution' a
rho
liftS Nat
k Substitution' a
IdS = forall a. Substitution' a
IdS
liftS Nat
k (Lift Nat
n Substitution' a
rho) = forall a. Nat -> Substitution' a -> Substitution' a
Lift (Nat
n forall a. Num a => a -> a -> a
+ Nat
k) Substitution' a
rho
liftS Nat
k Substitution' a
rho = forall a. Nat -> Substitution' a -> Substitution' a
Lift Nat
k Substitution' a
rho
dropS :: Int -> Substitution' a -> Substitution' a
dropS :: forall a. Nat -> Substitution' a -> Substitution' a
dropS Nat
0 Substitution' a
rho = Substitution' a
rho
dropS Nat
n Substitution' a
IdS = forall a. Nat -> Substitution' a
raiseS Nat
n
dropS Nat
n (Wk Nat
m Substitution' a
rho) = forall a. Nat -> Substitution' a -> Substitution' a
wkS Nat
m (forall a. Nat -> Substitution' a -> Substitution' a
dropS Nat
n Substitution' a
rho)
dropS Nat
n (a
u :# Substitution' a
rho) = forall a. Nat -> Substitution' a -> Substitution' a
dropS (Nat
n forall a. Num a => a -> a -> a
- Nat
1) Substitution' a
rho
dropS Nat
n (EmptyS Impossible
err) = forall a. Impossible -> a
throwImpossible Impossible
err
dropS Nat
n (Lift Nat
m Substitution' a
rho)
| Nat
n forall a. Ord a => a -> a -> Bool
> Nat
m = forall a. Nat -> Substitution' a -> Substitution' a
wkS Nat
m forall a b. (a -> b) -> a -> b
$ forall a. Nat -> Substitution' a -> Substitution' a
dropS (Nat
n forall a. Num a => a -> a -> a
- Nat
m) Substitution' a
rho
| Bool
otherwise = forall a. Nat -> Substitution' a -> Substitution' a
wkS Nat
n forall a b. (a -> b) -> a -> b
$ forall a. Nat -> Substitution' a -> Substitution' a
liftS (Nat
m forall a. Num a => a -> a -> a
- Nat
n) Substitution' a
rho
dropS Nat
n (Strengthen Impossible
err Nat
m Substitution' a
rho)
| Nat
n forall a. Ord a => a -> a -> Bool
< Nat
m = forall a. Impossible -> Nat -> Substitution' a -> Substitution' a
Strengthen Impossible
err (Nat
m forall a. Num a => a -> a -> a
- Nat
n) Substitution' a
rho
| Bool
otherwise = forall a. Nat -> Substitution' a -> Substitution' a
dropS (Nat
n forall a. Num a => a -> a -> a
- Nat
m) Substitution' a
rho
composeS :: EndoSubst a => Substitution' a -> Substitution' a -> Substitution' a
composeS :: forall a.
EndoSubst a =>
Substitution' a -> Substitution' a -> Substitution' a
composeS Substitution' a
rho Substitution' a
IdS = Substitution' a
rho
composeS Substitution' a
IdS Substitution' a
sgm = Substitution' a
sgm
composeS Substitution' a
rho (EmptyS Impossible
err) = forall a. Impossible -> Substitution' a
EmptyS Impossible
err
composeS Substitution' a
rho (Wk Nat
n Substitution' a
sgm) = forall a.
EndoSubst a =>
Substitution' a -> Substitution' a -> Substitution' a
composeS (forall a. Nat -> Substitution' a -> Substitution' a
dropS Nat
n Substitution' a
rho) Substitution' a
sgm
composeS Substitution' a
rho (a
u :# Substitution' a
sgm) = forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst Substitution' a
rho a
u forall a. a -> Substitution' a -> Substitution' a
:# forall a.
EndoSubst a =>
Substitution' a -> Substitution' a -> Substitution' a
composeS Substitution' a
rho Substitution' a
sgm
composeS Substitution' a
rho (Strengthen Impossible
err Nat
n Substitution' a
sgm) = forall a. Impossible -> Nat -> Substitution' a -> Substitution' a
Strengthen Impossible
err Nat
n (forall a.
EndoSubst a =>
Substitution' a -> Substitution' a -> Substitution' a
composeS Substitution' a
rho Substitution' a
sgm)
composeS Substitution' a
rho (Lift Nat
0 Substitution' a
sgm) = forall a. HasCallStack => a
__IMPOSSIBLE__
composeS (a
u :# Substitution' a
rho) (Lift Nat
n Substitution' a
sgm) = a
u forall a. a -> Substitution' a -> Substitution' a
:# forall a.
EndoSubst a =>
Substitution' a -> Substitution' a -> Substitution' a
composeS Substitution' a
rho (forall a. Nat -> Substitution' a -> Substitution' a
liftS (Nat
n forall a. Num a => a -> a -> a
- Nat
1) Substitution' a
sgm)
composeS Substitution' a
rho (Lift Nat
n Substitution' a
sgm) = forall a. EndoSubst a => Substitution' a -> Nat -> a
lookupS Substitution' a
rho Nat
0 forall a. a -> Substitution' a -> Substitution' a
:# forall a.
EndoSubst a =>
Substitution' a -> Substitution' a -> Substitution' a
composeS Substitution' a
rho (forall a. Nat -> Substitution' a -> Substitution' a
wkS Nat
1 (forall a. Nat -> Substitution' a -> Substitution' a
liftS (Nat
n forall a. Num a => a -> a -> a
- Nat
1) Substitution' a
sgm))
splitS :: Int -> Substitution' a -> (Substitution' a, Substitution' a)
splitS :: forall a.
Nat -> Substitution' a -> (Substitution' a, Substitution' a)
splitS Nat
0 Substitution' a
rho = (Substitution' a
rho, forall a. Impossible -> Substitution' a
EmptyS HasCallStack => Impossible
impossible)
splitS Nat
n (a
u :# Substitution' a
rho) = forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second (a
u forall a. a -> Substitution' a -> Substitution' a
:#) forall a b. (a -> b) -> a -> b
$ forall a.
Nat -> Substitution' a -> (Substitution' a, Substitution' a)
splitS (Nat
n forall a. Num a => a -> a -> a
- Nat
1) Substitution' a
rho
splitS Nat
n (Lift Nat
0 Substitution' a
_) = forall a. HasCallStack => a
__IMPOSSIBLE__
splitS Nat
n (Wk Nat
m Substitution' a
rho) = forall a. Nat -> Substitution' a -> Substitution' a
wkS Nat
m forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** forall a. Nat -> Substitution' a -> Substitution' a
wkS Nat
m forall a b. (a -> b) -> a -> b
$ forall a.
Nat -> Substitution' a -> (Substitution' a, Substitution' a)
splitS Nat
n Substitution' a
rho
splitS Nat
n Substitution' a
IdS = ( forall a. Nat -> Substitution' a
raiseS Nat
n
, forall a. Nat -> Substitution' a -> Substitution' a
liftS Nat
n forall a b. (a -> b) -> a -> b
$ forall a. Impossible -> Substitution' a
EmptyS HasCallStack => Impossible
impossible
)
splitS Nat
n (Lift Nat
m Substitution' a
rho) = forall a. Nat -> Substitution' a -> Substitution' a
wkS Nat
1 forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** forall a. Nat -> Substitution' a -> Substitution' a
liftS Nat
1 forall a b. (a -> b) -> a -> b
$
forall a.
Nat -> Substitution' a -> (Substitution' a, Substitution' a)
splitS (Nat
n forall a. Num a => a -> a -> a
- Nat
1) (forall a. Nat -> Substitution' a -> Substitution' a
liftS (Nat
m forall a. Num a => a -> a -> a
- Nat
1) Substitution' a
rho)
splitS Nat
n (EmptyS Impossible
err) = forall a. HasCallStack => a
__IMPOSSIBLE__
splitS Nat
n (Strengthen Impossible
err Nat
m Substitution' a
rho)
| Nat
n forall a. Ord a => a -> a -> Bool
> Nat
m = forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second (forall a. Impossible -> Nat -> Substitution' a -> Substitution' a
Strengthen Impossible
err Nat
m) forall a b. (a -> b) -> a -> b
$
forall a.
Nat -> Substitution' a -> (Substitution' a, Substitution' a)
splitS (Nat
n forall a. Num a => a -> a -> a
- Nat
m) Substitution' a
rho
| Bool
otherwise = forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second (forall a. Impossible -> Nat -> Substitution' a -> Substitution' a
Strengthen Impossible
err Nat
n) forall a b. (a -> b) -> a -> b
$
forall a.
Nat -> Substitution' a -> (Substitution' a, Substitution' a)
splitS Nat
0 (forall a. Impossible -> Nat -> Substitution' a -> Substitution' a
Strengthen Impossible
err (Nat
m forall a. Num a => a -> a -> a
- Nat
n) Substitution' a
rho)
infixr 4 ++#
(++#) :: DeBruijn a => [a] -> Substitution' a -> Substitution' a
[a]
us ++# :: forall a. DeBruijn a => [a] -> Substitution' a -> Substitution' a
++# Substitution' a
rho = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall a. DeBruijn a => a -> Substitution' a -> Substitution' a
consS Substitution' a
rho [a]
us
prependS :: DeBruijn a => Impossible -> [Maybe a] -> Substitution' a -> Substitution' a
prependS :: forall a.
DeBruijn a =>
Impossible -> [Maybe a] -> Substitution' a -> Substitution' a
prependS Impossible
err [Maybe a]
us Substitution' a
rho = Nat -> [Maybe a] -> Substitution' a
go Nat
0 [Maybe a]
us
where
str :: Nat -> Substitution' a -> Substitution' a
str Nat
0 = forall a. a -> a
id
str Nat
n = forall a. Impossible -> Nat -> Substitution' a -> Substitution' a
Strengthen Impossible
err Nat
n
go :: Nat -> [Maybe a] -> Substitution' a
go !Nat
n (Just a
u : [Maybe a]
us) = Nat -> Substitution' a -> Substitution' a
str Nat
n (forall a. DeBruijn a => a -> Substitution' a -> Substitution' a
consS a
u (Nat -> [Maybe a] -> Substitution' a
go Nat
0 [Maybe a]
us))
go Nat
n (Maybe a
Nothing : [Maybe a]
us) = Nat -> [Maybe a] -> Substitution' a
go (Nat
1 forall a. Num a => a -> a -> a
+ Nat
n) [Maybe a]
us
go Nat
n [] = Nat -> Substitution' a -> Substitution' a
str Nat
n Substitution' a
rho
parallelS :: DeBruijn a => [a] -> Substitution' a
parallelS :: forall a. DeBruijn a => [a] -> Substitution' a
parallelS [a]
us = [a]
us forall a. DeBruijn a => [a] -> Substitution' a -> Substitution' a
++# forall a. Substitution' a
idS
strengthenS :: Impossible -> Int -> Substitution' a
strengthenS :: forall a. Impossible -> Nat -> Substitution' a
strengthenS Impossible
err Nat
n = case forall a. Ord a => a -> a -> Ordering
compare Nat
n Nat
0 of
Ordering
LT -> forall a. HasCallStack => a
__IMPOSSIBLE__
Ordering
EQ -> forall a. Substitution' a
idS
Ordering
GT -> forall a. Impossible -> Nat -> Substitution' a -> Substitution' a
Strengthen Impossible
err Nat
n forall a. Substitution' a
idS
strengthenS' :: Impossible -> Int -> Substitution' a -> Substitution' a
strengthenS' :: forall a. Impossible -> Nat -> Substitution' a -> Substitution' a
strengthenS' Impossible
err Nat
m Substitution' a
rho = case forall a. Ord a => a -> a -> Ordering
compare Nat
m Nat
0 of
Ordering
LT -> forall a. HasCallStack => a
__IMPOSSIBLE__
Ordering
EQ -> Substitution' a
rho
Ordering
GT -> case Substitution' a
rho of
Strengthen Impossible
_ Nat
n Substitution' a
rho -> forall a. Impossible -> Nat -> Substitution' a -> Substitution' a
Strengthen Impossible
err (Nat
m forall a. Num a => a -> a -> a
+ Nat
n) Substitution' a
rho
Substitution' a
_ -> forall a. Impossible -> Nat -> Substitution' a -> Substitution' a
Strengthen Impossible
err Nat
m Substitution' a
rho
lookupS :: EndoSubst a => Substitution' a -> Nat -> a
lookupS :: forall a. EndoSubst a => Substitution' a -> Nat -> a
lookupS Substitution' a
rho Nat
i = case Substitution' a
rho of
Substitution' a
IdS -> forall a. DeBruijn a => Nat -> a
deBruijnVar Nat
i
Wk Nat
n Substitution' a
IdS -> let j :: Nat
j = Nat
i forall a. Num a => a -> a -> a
+ Nat
n in
if Nat
j forall a. Ord a => a -> a -> Bool
< Nat
0 then forall a. HasCallStack => a
__IMPOSSIBLE__ else forall a. DeBruijn a => Nat -> a
deBruijnVar Nat
j
Wk Nat
n Substitution' a
rho -> forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst (forall a. Nat -> Substitution' a
raiseS Nat
n) (forall a. EndoSubst a => Substitution' a -> Nat -> a
lookupS Substitution' a
rho Nat
i)
a
u :# Substitution' a
rho | Nat
i forall a. Eq a => a -> a -> Bool
== Nat
0 -> a
u
| Nat
i forall a. Ord a => a -> a -> Bool
< Nat
0 -> forall a. HasCallStack => a
__IMPOSSIBLE__
| Bool
otherwise -> forall a. EndoSubst a => Substitution' a -> Nat -> a
lookupS Substitution' a
rho (Nat
i forall a. Num a => a -> a -> a
- Nat
1)
Strengthen Impossible
err Nat
n Substitution' a
rho
| Nat
i forall a. Ord a => a -> a -> Bool
< Nat
0 -> forall a. HasCallStack => a
__IMPOSSIBLE__
| Nat
i forall a. Ord a => a -> a -> Bool
< Nat
n -> forall a. Impossible -> a
throwImpossible Impossible
err
| Bool
otherwise -> forall a. EndoSubst a => Substitution' a -> Nat -> a
lookupS Substitution' a
rho (Nat
i forall a. Num a => a -> a -> a
- Nat
n)
Lift Nat
n Substitution' a
rho | Nat
i forall a. Ord a => a -> a -> Bool
< Nat
n -> forall a. DeBruijn a => Nat -> a
deBruijnVar Nat
i
| Bool
otherwise -> forall a. Subst a => Nat -> a -> a
raise Nat
n forall a b. (a -> b) -> a -> b
$ forall a. EndoSubst a => Substitution' a -> Nat -> a
lookupS Substitution' a
rho (Nat
i forall a. Num a => a -> a -> a
- Nat
n)
EmptyS Impossible
err -> forall a. Impossible -> a
throwImpossible Impossible
err
listS :: EndoSubst a => [(Int,a)] -> Substitution' a
listS :: forall a. EndoSubst a => [(Nat, a)] -> Substitution' a
listS ((Nat
i,a
t):[(Nat, a)]
ts) = forall a. DeBruijn a => Nat -> a -> Substitution' a
singletonS Nat
i a
t forall a.
EndoSubst a =>
Substitution' a -> Substitution' a -> Substitution' a
`composeS` forall a. EndoSubst a => [(Nat, a)] -> Substitution' a
listS [(Nat, a)]
ts
listS [] = forall a. Substitution' a
IdS
raiseFromS :: Nat -> Nat -> Substitution' a
raiseFromS :: forall a. Nat -> Nat -> Substitution' a
raiseFromS Nat
n Nat
k = forall a. Nat -> Substitution' a -> Substitution' a
liftS Nat
n forall a b. (a -> b) -> a -> b
$ forall a. Nat -> Substitution' a
raiseS Nat
k
absApp :: Subst a => Abs a -> SubstArg a -> a
absApp :: forall a. Subst a => Abs a -> SubstArg a -> a
absApp (Abs ArgName
_ a
v) SubstArg a
u = forall a. Subst a => Nat -> SubstArg a -> a -> a
subst Nat
0 SubstArg a
u a
v
absApp (NoAbs ArgName
_ a
v) SubstArg a
_ = a
v
lazyAbsApp :: Subst a => Abs a -> SubstArg a -> a
lazyAbsApp :: forall a. Subst a => Abs a -> SubstArg a -> a
lazyAbsApp (Abs ArgName
_ a
v) SubstArg a
u = forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst (SubstArg a
u forall a. a -> Substitution' a -> Substitution' a
:# forall a. Substitution' a
IdS) a
v
lazyAbsApp (NoAbs ArgName
_ a
v) SubstArg a
_ = a
v
noabsApp :: Subst a => Impossible -> Abs a -> a
noabsApp :: forall a. Subst a => Impossible -> Abs a -> a
noabsApp Impossible
err (Abs ArgName
_ a
v) = forall a. Subst a => Impossible -> a -> a
strengthen Impossible
err a
v
noabsApp Impossible
_ (NoAbs ArgName
_ a
v) = a
v
absBody :: Subst a => Abs a -> a
absBody :: forall a. Subst a => Abs a -> a
absBody (Abs ArgName
_ a
v) = a
v
absBody (NoAbs ArgName
_ a
v) = forall a. Subst a => Nat -> a -> a
raise Nat
1 a
v
mkAbs :: (Subst a, Free a) => ArgName -> a -> Abs a
mkAbs :: forall a. (Subst a, Free a) => ArgName -> a -> Abs a
mkAbs ArgName
x a
v | Nat
0 forall a. Free a => Nat -> a -> Bool
`freeIn` a
v = forall a. ArgName -> a -> Abs a
Abs ArgName
x a
v
| Bool
otherwise = forall a. ArgName -> a -> Abs a
NoAbs ArgName
x (forall a. Subst a => Nat -> a -> a
raise (-Nat
1) a
v)
reAbs :: (Subst a, Free a) => Abs a -> Abs a
reAbs :: forall a. (Subst a, Free a) => Abs a -> Abs a
reAbs (NoAbs ArgName
x a
v) = forall a. ArgName -> a -> Abs a
NoAbs ArgName
x a
v
reAbs (Abs ArgName
x a
v) = forall a. (Subst a, Free a) => ArgName -> a -> Abs a
mkAbs ArgName
x a
v
underAbs :: Subst a => (a -> b -> b) -> a -> Abs b -> Abs b
underAbs :: forall a b. Subst a => (a -> b -> b) -> a -> Abs b -> Abs b
underAbs a -> b -> b
cont a
a = \case
Abs ArgName
x b
t -> forall a. ArgName -> a -> Abs a
Abs ArgName
x forall a b. (a -> b) -> a -> b
$ a -> b -> b
cont (forall a. Subst a => Nat -> a -> a
raise Nat
1 a
a) b
t
NoAbs ArgName
x b
t -> forall a. ArgName -> a -> Abs a
NoAbs ArgName
x forall a b. (a -> b) -> a -> b
$ a -> b -> b
cont a
a b
t
underLambdas :: TermSubst a => Int -> (a -> Term -> Term) -> a -> Term -> Term
underLambdas :: forall a.
TermSubst a =>
Nat -> (a -> Term -> Term) -> a -> Term -> Term
underLambdas Nat
n a -> Term -> Term
cont = Nat -> a -> Term -> Term
loop Nat
n where
loop :: Nat -> a -> Term -> Term
loop Nat
0 a
a = a -> Term -> Term
cont a
a
loop Nat
n a
a = \case
Lam ArgInfo
h Abs Term
b -> ArgInfo -> Abs Term -> Term
Lam ArgInfo
h forall a b. (a -> b) -> a -> b
$ forall a b. Subst a => (a -> b -> b) -> a -> Abs b -> Abs b
underAbs (Nat -> a -> Term -> Term
loop forall a b. (a -> b) -> a -> b
$ Nat
nforall a. Num a => a -> a -> a
-Nat
1) a
a Abs Term
b
Term
_ -> forall a. HasCallStack => a
__IMPOSSIBLE__