module Retrie.AlphaEnv
( AlphaEnv
, alphaEnvOffset
, emptyAlphaEnv
, extendAlphaEnv
, lookupAlphaEnv
, pruneAlphaEnv
, extendAlphaEnvInternal
) where
import Retrie.GHC
data AlphaEnv = AE
{ AlphaEnv -> Int
_aeNext :: !Int
, AlphaEnv -> OccEnv Int
aeEnv :: OccEnv Int
, AlphaEnv -> Int
aeOffset :: !Int
}
alphaEnvOffset :: AlphaEnv -> Int
alphaEnvOffset :: AlphaEnv -> Int
alphaEnvOffset = AlphaEnv -> Int
aeOffset
emptyAlphaEnv :: AlphaEnv
emptyAlphaEnv :: AlphaEnv
emptyAlphaEnv = Int -> OccEnv Int -> Int -> AlphaEnv
AE Int
0 forall a. OccEnv a
emptyOccEnv Int
0
extendAlphaEnvInternal :: RdrName -> AlphaEnv -> AlphaEnv
extendAlphaEnvInternal :: RdrName -> AlphaEnv -> AlphaEnv
extendAlphaEnvInternal RdrName
nm (AE Int
i OccEnv Int
env Int
off) =
Int -> OccEnv Int -> Int -> AlphaEnv
AE (Int
iforall a. Num a => a -> a -> a
+Int
1) (forall a. OccEnv a -> OccName -> a -> OccEnv a
extendOccEnv OccEnv Int
env (forall name. HasOccName name => name -> OccName
occName RdrName
nm) Int
i) Int
off
extendAlphaEnv :: RdrName -> AlphaEnv -> AlphaEnv
extendAlphaEnv :: RdrName -> AlphaEnv -> AlphaEnv
extendAlphaEnv RdrName
nm AlphaEnv
e = AlphaEnv
e' { aeOffset :: Int
aeOffset = AlphaEnv -> Int
aeOffset AlphaEnv
e' forall a. Num a => a -> a -> a
+ Int
1 }
where e' :: AlphaEnv
e' = RdrName -> AlphaEnv -> AlphaEnv
extendAlphaEnvInternal RdrName
nm AlphaEnv
e
pruneAlphaEnv :: Int -> AlphaEnv -> AlphaEnv
pruneAlphaEnv :: Int -> AlphaEnv -> AlphaEnv
pruneAlphaEnv Int
i AlphaEnv
ae = AlphaEnv
ae { aeEnv :: OccEnv Int
aeEnv = forall elt. (elt -> Bool) -> OccEnv elt -> OccEnv elt
filterOccEnv (forall a. Ord a => a -> a -> Bool
>= Int
i) (AlphaEnv -> OccEnv Int
aeEnv AlphaEnv
ae) }
lookupAlphaEnv :: RdrName -> AlphaEnv -> Maybe Int
lookupAlphaEnv :: RdrName -> AlphaEnv -> Maybe Int
lookupAlphaEnv RdrName
nm (AE Int
_ OccEnv Int
env Int
off) =
(-) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. OccEnv a -> OccName -> Maybe a
lookupOccEnv OccEnv Int
env (forall name. HasOccName name => name -> OccName
occName RdrName
nm) forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
off