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 OccEnv Int
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
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (OccEnv Int -> OccName -> Int -> OccEnv Int
forall a. OccEnv a -> OccName -> a -> OccEnv a
extendOccEnv OccEnv Int
env (RdrName -> OccName
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' Int -> Int -> Int
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 = (Int -> Bool) -> OccEnv Int -> OccEnv Int
forall elt. (elt -> Bool) -> OccEnv elt -> OccEnv elt
filterOccEnv (Int -> Int -> Bool
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) =
(-) (Int -> Int -> Int) -> Maybe Int -> Maybe (Int -> Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> OccEnv Int -> OccName -> Maybe Int
forall a. OccEnv a -> OccName -> Maybe a
lookupOccEnv OccEnv Int
env (RdrName -> OccName
forall name. HasOccName name => name -> OccName
occName RdrName
nm) Maybe (Int -> Int) -> Maybe Int -> Maybe Int
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> Maybe Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
off