+%************************************************************************
+%* *
+\subsection{The local environment}
+%* *
+%************************************************************************
+
+\begin{code}
+tcLookup_maybe :: Name -> TcM (Maybe TcTyThing)
+tcLookup_maybe name
+ = getLclEnv `thenM` \ local_env ->
+ case lookupNameEnv (tcl_env local_env) name of
+ Just thing -> returnM (Just thing)
+ Nothing -> tcLookupGlobal_maybe name `thenM` \ mb_res ->
+ returnM (case mb_res of
+ Just thing -> Just (AGlobal thing)
+ Nothing -> Nothing)
+
+tcLookup :: Name -> TcM TcTyThing
+tcLookup name
+ = tcLookup_maybe name `thenM` \ maybe_thing ->
+ case maybe_thing of
+ Just thing -> returnM thing
+ other -> notFound "tcLookup" name
+ -- Extract the IdInfo from an IfaceSig imported from an interface file
+
+tcLookupId :: Name -> TcM Id
+-- Used when we aren't interested in the binding level
+tcLookupId name
+ = tcLookup name `thenM` \ thing ->
+ case thing of
+ ATcId tc_id lvl -> returnM tc_id
+ AGlobal (AnId id) -> returnM id
+ other -> pprPanic "tcLookupId" (ppr name)
+
+tcLookupIdLvl :: Name -> TcM (Id, Level)
+tcLookupIdLvl name
+ = tcLookup name `thenM` \ thing ->
+ case thing of
+ ATcId tc_id lvl -> returnM (tc_id, lvl)
+ AGlobal (AnId id) -> returnM (id, impLevel)
+ other -> pprPanic "tcLookupIdLvl" (ppr name)
+
+tcLookupLocalIds :: [Name] -> TcM [TcId]
+-- We expect the variables to all be bound, and all at
+-- the same level as the lookup. Only used in one place...
+tcLookupLocalIds ns
+ = getLclEnv `thenM` \ env ->
+ returnM (map (lookup (tcl_env env) (metaLevel (tcl_level env))) ns)
+ where
+ lookup lenv lvl name
+ = case lookupNameEnv lenv name of
+ Just (ATcId id lvl1) -> ASSERT( lvl == lvl1 ) id
+ other -> pprPanic "tcLookupLocalIds" (ppr name)
+
+getLclEnvElts :: TcM [TcTyThing]
+getLclEnvElts = getLclEnv `thenM` \ env ->
+ return (nameEnvElts (tcl_env env))
+
+getInLocalScope :: TcM (Name -> Bool)
+ -- Ids only
+getInLocalScope = getLclEnv `thenM` \ env ->
+ let
+ lcl_env = tcl_env env
+ in
+ return (`elemNameEnv` lcl_env)
+\end{code}