{-
%
% (c) The University of Glasgow 2006
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
%
\section[FastStringEnv]{@FastStringEnv@: FastString environments}
-}

module FastStringEnv (
        -- * FastString environments (maps)
        FastStringEnv,

        -- ** Manipulating these environments
        mkFsEnv,
        emptyFsEnv, unitFsEnv,
        extendFsEnv_C, extendFsEnv_Acc, extendFsEnv,
        extendFsEnvList, extendFsEnvList_C,
        filterFsEnv,
        plusFsEnv, plusFsEnv_C, alterFsEnv,
        lookupFsEnv, lookupFsEnv_NF, delFromFsEnv, delListFromFsEnv,
        elemFsEnv, mapFsEnv,

        -- * Deterministic FastString environments (maps)
        DFastStringEnv,

        -- ** Manipulating these environments
        mkDFsEnv, emptyDFsEnv, dFsEnvElts, lookupDFsEnv
    ) where

import GhcPrelude

import UniqFM
import UniqDFM
import Maybes
import FastString


-- | A non-deterministic set of FastStrings.
-- See Note [Deterministic UniqFM] in UniqDFM for explanation why it's not
-- deterministic and why it matters. Use DFastStringEnv if the set eventually
-- gets converted into a list or folded over in a way where the order
-- changes the generated code.
type FastStringEnv a = UniqFM a  -- Domain is FastString

emptyFsEnv         :: FastStringEnv a
mkFsEnv            :: [(FastString,a)] -> FastStringEnv a
alterFsEnv         :: (Maybe a-> Maybe a) -> FastStringEnv a -> FastString -> FastStringEnv a
extendFsEnv_C      :: (a->a->a) -> FastStringEnv a -> FastString -> a -> FastStringEnv a
extendFsEnv_Acc    :: (a->b->b) -> (a->b) -> FastStringEnv b -> FastString -> a -> FastStringEnv b
extendFsEnv        :: FastStringEnv a -> FastString -> a -> FastStringEnv a
plusFsEnv          :: FastStringEnv a -> FastStringEnv a -> FastStringEnv a
plusFsEnv_C        :: (a->a->a) -> FastStringEnv a -> FastStringEnv a -> FastStringEnv a
extendFsEnvList    :: FastStringEnv a -> [(FastString,a)] -> FastStringEnv a
extendFsEnvList_C  :: (a->a->a) -> FastStringEnv a -> [(FastString,a)] -> FastStringEnv a
delFromFsEnv       :: FastStringEnv a -> FastString -> FastStringEnv a
delListFromFsEnv   :: FastStringEnv a -> [FastString] -> FastStringEnv a
elemFsEnv          :: FastString -> FastStringEnv a -> Bool
unitFsEnv          :: FastString -> a -> FastStringEnv a
lookupFsEnv        :: FastStringEnv a -> FastString -> Maybe a
lookupFsEnv_NF     :: FastStringEnv a -> FastString -> a
filterFsEnv        :: (elt -> Bool) -> FastStringEnv elt -> FastStringEnv elt
mapFsEnv           :: (elt1 -> elt2) -> FastStringEnv elt1 -> FastStringEnv elt2

emptyFsEnv :: FastStringEnv a
emptyFsEnv                = FastStringEnv a
forall elt. UniqFM elt
emptyUFM
unitFsEnv :: FastString -> a -> FastStringEnv a
unitFsEnv FastString
x a
y             = FastString -> a -> FastStringEnv a
forall key elt. Uniquable key => key -> elt -> UniqFM elt
unitUFM FastString
x a
y
extendFsEnv :: FastStringEnv a -> FastString -> a -> FastStringEnv a
extendFsEnv FastStringEnv a
x FastString
y a
z         = FastStringEnv a -> FastString -> a -> FastStringEnv a
forall key elt.
Uniquable key =>
UniqFM elt -> key -> elt -> UniqFM elt
addToUFM FastStringEnv a
x FastString
y a
z
extendFsEnvList :: FastStringEnv a -> [(FastString, a)] -> FastStringEnv a
extendFsEnvList FastStringEnv a
x [(FastString, a)]
l       = FastStringEnv a -> [(FastString, a)] -> FastStringEnv a
forall key elt.
Uniquable key =>
UniqFM elt -> [(key, elt)] -> UniqFM elt
addListToUFM FastStringEnv a
x [(FastString, a)]
l
lookupFsEnv :: FastStringEnv a -> FastString -> Maybe a
lookupFsEnv FastStringEnv a
x FastString
y           = FastStringEnv a -> FastString -> Maybe a
forall key elt. Uniquable key => UniqFM elt -> key -> Maybe elt
lookupUFM FastStringEnv a
x FastString
y
alterFsEnv :: (Maybe a -> Maybe a)
-> FastStringEnv a -> FastString -> FastStringEnv a
alterFsEnv                = (Maybe a -> Maybe a)
-> FastStringEnv a -> FastString -> FastStringEnv a
forall key elt.
Uniquable key =>
(Maybe elt -> Maybe elt) -> UniqFM elt -> key -> UniqFM elt
alterUFM
mkFsEnv :: [(FastString, a)] -> FastStringEnv a
mkFsEnv     [(FastString, a)]
l             = [(FastString, a)] -> FastStringEnv a
forall key elt. Uniquable key => [(key, elt)] -> UniqFM elt
listToUFM [(FastString, a)]
l
elemFsEnv :: FastString -> FastStringEnv a -> Bool
elemFsEnv FastString
x FastStringEnv a
y             = FastString -> FastStringEnv a -> Bool
forall key elt. Uniquable key => key -> UniqFM elt -> Bool
elemUFM FastString
x FastStringEnv a
y
plusFsEnv :: FastStringEnv a -> FastStringEnv a -> FastStringEnv a
plusFsEnv FastStringEnv a
x FastStringEnv a
y             = FastStringEnv a -> FastStringEnv a -> FastStringEnv a
forall elt. UniqFM elt -> UniqFM elt -> UniqFM elt
plusUFM FastStringEnv a
x FastStringEnv a
y
plusFsEnv_C :: (a -> a -> a)
-> FastStringEnv a -> FastStringEnv a -> FastStringEnv a
plusFsEnv_C a -> a -> a
f FastStringEnv a
x FastStringEnv a
y         = (a -> a -> a)
-> FastStringEnv a -> FastStringEnv a -> FastStringEnv a
forall elt.
(elt -> elt -> elt) -> UniqFM elt -> UniqFM elt -> UniqFM elt
plusUFM_C a -> a -> a
f FastStringEnv a
x FastStringEnv a
y
extendFsEnv_C :: (a -> a -> a)
-> FastStringEnv a -> FastString -> a -> FastStringEnv a
extendFsEnv_C a -> a -> a
f FastStringEnv a
x FastString
y a
z     = (a -> a -> a)
-> FastStringEnv a -> FastString -> a -> FastStringEnv a
forall key elt.
Uniquable key =>
(elt -> elt -> elt) -> UniqFM elt -> key -> elt -> UniqFM elt
addToUFM_C a -> a -> a
f FastStringEnv a
x FastString
y a
z
mapFsEnv :: (elt1 -> elt2) -> FastStringEnv elt1 -> FastStringEnv elt2
mapFsEnv elt1 -> elt2
f FastStringEnv elt1
x              = (elt1 -> elt2) -> FastStringEnv elt1 -> FastStringEnv elt2
forall elt1 elt2. (elt1 -> elt2) -> UniqFM elt1 -> UniqFM elt2
mapUFM elt1 -> elt2
f FastStringEnv elt1
x
extendFsEnv_Acc :: (a -> b -> b)
-> (a -> b)
-> FastStringEnv b
-> FastString
-> a
-> FastStringEnv b
extendFsEnv_Acc a -> b -> b
x a -> b
y FastStringEnv b
z FastString
a a
b = (a -> b -> b)
-> (a -> b)
-> FastStringEnv b
-> FastString
-> a
-> FastStringEnv b
forall key elt elts.
Uniquable key =>
(elt -> elts -> elts)
-> (elt -> elts) -> UniqFM elts -> key -> elt -> UniqFM elts
addToUFM_Acc a -> b -> b
x a -> b
y FastStringEnv b
z FastString
a a
b
extendFsEnvList_C :: (a -> a -> a)
-> FastStringEnv a -> [(FastString, a)] -> FastStringEnv a
extendFsEnvList_C a -> a -> a
x FastStringEnv a
y [(FastString, a)]
z   = (a -> a -> a)
-> FastStringEnv a -> [(FastString, a)] -> FastStringEnv a
forall key elt.
Uniquable key =>
(elt -> elt -> elt) -> UniqFM elt -> [(key, elt)] -> UniqFM elt
addListToUFM_C a -> a -> a
x FastStringEnv a
y [(FastString, a)]
z
delFromFsEnv :: FastStringEnv a -> FastString -> FastStringEnv a
delFromFsEnv FastStringEnv a
x FastString
y          = FastStringEnv a -> FastString -> FastStringEnv a
forall key elt. Uniquable key => UniqFM elt -> key -> UniqFM elt
delFromUFM FastStringEnv a
x FastString
y
delListFromFsEnv :: FastStringEnv a -> [FastString] -> FastStringEnv a
delListFromFsEnv FastStringEnv a
x [FastString]
y      = FastStringEnv a -> [FastString] -> FastStringEnv a
forall key elt. Uniquable key => UniqFM elt -> [key] -> UniqFM elt
delListFromUFM FastStringEnv a
x [FastString]
y
filterFsEnv :: (elt -> Bool) -> FastStringEnv elt -> FastStringEnv elt
filterFsEnv elt -> Bool
x FastStringEnv elt
y           = (elt -> Bool) -> FastStringEnv elt -> FastStringEnv elt
forall elt. (elt -> Bool) -> UniqFM elt -> UniqFM elt
filterUFM elt -> Bool
x FastStringEnv elt
y

lookupFsEnv_NF :: FastStringEnv a -> FastString -> a
lookupFsEnv_NF FastStringEnv a
env FastString
n = String -> Maybe a -> a
forall a. HasCallStack => String -> Maybe a -> a
expectJust String
"lookupFsEnv_NF" (FastStringEnv a -> FastString -> Maybe a
forall a. FastStringEnv a -> FastString -> Maybe a
lookupFsEnv FastStringEnv a
env FastString
n)

-- Deterministic FastStringEnv
-- See Note [Deterministic UniqFM] in UniqDFM for explanation why we need
-- DFastStringEnv.

type DFastStringEnv a = UniqDFM a  -- Domain is FastString

emptyDFsEnv :: DFastStringEnv a
emptyDFsEnv :: DFastStringEnv a
emptyDFsEnv = DFastStringEnv a
forall elt. UniqDFM elt
emptyUDFM

dFsEnvElts :: DFastStringEnv a -> [a]
dFsEnvElts :: DFastStringEnv a -> [a]
dFsEnvElts = DFastStringEnv a -> [a]
forall elt. UniqDFM elt -> [elt]
eltsUDFM

mkDFsEnv :: [(FastString,a)] -> DFastStringEnv a
mkDFsEnv :: [(FastString, a)] -> DFastStringEnv a
mkDFsEnv [(FastString, a)]
l = [(FastString, a)] -> DFastStringEnv a
forall key elt. Uniquable key => [(key, elt)] -> UniqDFM elt
listToUDFM [(FastString, a)]
l

lookupDFsEnv :: DFastStringEnv a -> FastString -> Maybe a
lookupDFsEnv :: DFastStringEnv a -> FastString -> Maybe a
lookupDFsEnv = DFastStringEnv a -> FastString -> Maybe a
forall key elt. Uniquable key => UniqDFM elt -> key -> Maybe elt
lookupUDFM