X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2Ftypecheck%2FTcHsSyn.lhs;h=345011bb23e464240081eae311a90502bfc36010;hb=f36fb2ce821caf594c1db5669dd10ca082f66361;hp=005fec5b706127e0256d88bc3458e27ce7f5ffd8;hpb=6c381e873e222417d9a67aeec77b9555eca7b7a8;p=ghc-hetmet.git diff --git a/ghc/compiler/typecheck/TcHsSyn.lhs b/ghc/compiler/typecheck/TcHsSyn.lhs index 005fec5..345011b 100644 --- a/ghc/compiler/typecheck/TcHsSyn.lhs +++ b/ghc/compiler/typecheck/TcHsSyn.lhs @@ -8,45 +8,58 @@ checker. \begin{code} module TcHsSyn ( - TcIdBndr(..), TcIdOcc(..), + TcMonoBinds, TcHsBinds, TcPat, + TcExpr, TcGRHSsAndBinds, TcGRHS, TcMatch, + TcStmt, TcArithSeqInfo, TcRecordBinds, + TcHsModule, TcCoreExpr, TcDictBinds, - TcMonoBinds(..), TcHsBinds(..), TcBind(..), TcPat(..), TcExpr(..), TcGRHSsAndBinds(..), - TcGRHS(..), TcMatch(..), TcQual(..), TcStmt(..), TcArithSeqInfo(..), TcHsModule(..), - - TypecheckedHsBinds(..), TypecheckedBind(..), TypecheckedMonoBinds(..), - TypecheckedPat(..), TypecheckedHsExpr(..), TypecheckedArithSeqInfo(..), - TypecheckedQual(..), TypecheckedStmt(..), TypecheckedMatch(..), - TypecheckedHsModule(..), TypecheckedGRHSsAndBinds(..), TypecheckedGRHS(..), + TypecheckedHsBinds, + TypecheckedMonoBinds, TypecheckedPat, + TypecheckedHsExpr, TypecheckedArithSeqInfo, + TypecheckedStmt, + TypecheckedMatch, TypecheckedHsModule, + TypecheckedGRHSsAndBinds, TypecheckedGRHS, + TypecheckedRecordBinds, TypecheckedDictBinds, mkHsTyApp, mkHsDictApp, mkHsTyLam, mkHsDictLam, - zonkBinds, - zonkInst, - zonkId, -- TcIdBndr s -> NF_TcM s Id - unZonkId -- Id -> NF_TcM s (TcIdBndr s) + -- re-exported from TcEnv + TcIdOcc(..), TcIdBndr, tcIdType, tcIdTyVars, tcInstId, + + maybeBoxedPrimType, + + zonkTopBinds, zonkBinds, zonkMonoBinds, zonkTcId ) where -import Ubiq{-uitous-} +#include "HsVersions.h" -- friends: import HsSyn -- oodles of it -import Id ( GenId(..), IdDetails, PragmaInfo, -- Can meddle modestly with Ids - DictVar(..) +import Id ( idType, dataConArgTys, mkIdWithNewType, Id ) -- others: -import TcMonad -import TcType ( TcType(..), TcMaybe, TcTyVar(..), - zonkTcTypeToType, zonkTcTyVarToTyVar, - tcInstType +import Name ( NamedThing(..) ) +import BasicTypes ( IfaceFlavour, Unused ) +import TcEnv ( tcLookupGlobalValueMaybe, tcExtendGlobalValEnv, tcGetGlobalValEnv, + TcIdOcc(..), TcIdBndr, GlobalValueEnv, + tcIdType, tcIdTyVars, tcInstId ) -import Usage ( UVar(..) ) -import Util ( panic ) -import PprType ( GenType, GenTyVar ) -- instances -import TyVar ( GenTyVar ) -- instances +import TcMonad +import TcType ( TcType, TcMaybe, TcTyVar, TcBox, + zonkTcTypeToType, zonkTcTyVarToTyVar, zonkTcType + ) +import TyCon ( isDataTyCon ) +import Type ( mkTyVarTy, splitAlgTyConApp_maybe, isUnpointedType, Type ) +import TyVar ( TyVar, TyVarEnv, emptyTyVarEnv, growTyVarEnvList ) +import TysPrim ( voidTy ) +import CoreSyn ( GenCoreExpr ) import Unique ( Unique ) -- instances +import Bag +import UniqFM +import Outputable \end{code} @@ -61,35 +74,33 @@ At the end of type checking we zonk everything to @Typechecked...@ datatypes, which have immutable type variables in them. \begin{code} -type TcIdBndr s = GenId (TcType s) -- Binders are all TcTypes -data TcIdOcc s = TcId (TcIdBndr s) -- Bindees may be either - | RealId Id - -type TcHsBinds s = HsBinds (TcTyVar s) UVar (TcIdOcc s) (TcPat s) -type TcBind s = Bind (TcTyVar s) UVar (TcIdOcc s) (TcPat s) -type TcMonoBinds s = MonoBinds (TcTyVar s) UVar (TcIdOcc s) (TcPat s) -type TcPat s = OutPat (TcTyVar s) UVar (TcIdOcc s) -type TcExpr s = HsExpr (TcTyVar s) UVar (TcIdOcc s) (TcPat s) -type TcGRHSsAndBinds s = GRHSsAndBinds (TcTyVar s) UVar (TcIdOcc s) (TcPat s) -type TcGRHS s = GRHS (TcTyVar s) UVar (TcIdOcc s) (TcPat s) -type TcMatch s = Match (TcTyVar s) UVar (TcIdOcc s) (TcPat s) -type TcQual s = Qual (TcTyVar s) UVar (TcIdOcc s) (TcPat s) -type TcStmt s = Stmt (TcTyVar s) UVar (TcIdOcc s) (TcPat s) -type TcArithSeqInfo s = ArithSeqInfo (TcTyVar s) UVar (TcIdOcc s) (TcPat s) -type TcHsModule s = HsModule (TcTyVar s) UVar (TcIdOcc s) (TcPat s) - -type TypecheckedPat = OutPat TyVar UVar Id -type TypecheckedMonoBinds = MonoBinds TyVar UVar Id TypecheckedPat -type TypecheckedHsBinds = HsBinds TyVar UVar Id TypecheckedPat -type TypecheckedBind = Bind TyVar UVar Id TypecheckedPat -type TypecheckedHsExpr = HsExpr TyVar UVar Id TypecheckedPat -type TypecheckedArithSeqInfo = ArithSeqInfo TyVar UVar Id TypecheckedPat -type TypecheckedQual = Qual TyVar UVar Id TypecheckedPat -type TypecheckedStmt = Stmt TyVar UVar Id TypecheckedPat -type TypecheckedMatch = Match TyVar UVar Id TypecheckedPat -type TypecheckedGRHSsAndBinds = GRHSsAndBinds TyVar UVar Id TypecheckedPat -type TypecheckedGRHS = GRHS TyVar UVar Id TypecheckedPat -type TypecheckedHsModule = HsModule TyVar UVar Id TypecheckedPat +type TcHsBinds s = HsBinds (TcBox s) (TcIdOcc s) (TcPat s) +type TcMonoBinds s = MonoBinds (TcBox s) (TcIdOcc s) (TcPat s) +type TcDictBinds s = TcMonoBinds s +type TcPat s = OutPat (TcBox s) (TcIdOcc s) +type TcExpr s = HsExpr (TcBox s) (TcIdOcc s) (TcPat s) +type TcGRHSsAndBinds s = GRHSsAndBinds (TcBox s) (TcIdOcc s) (TcPat s) +type TcGRHS s = GRHS (TcBox s) (TcIdOcc s) (TcPat s) +type TcMatch s = Match (TcBox s) (TcIdOcc s) (TcPat s) +type TcStmt s = Stmt (TcBox s) (TcIdOcc s) (TcPat s) +type TcArithSeqInfo s = ArithSeqInfo (TcBox s) (TcIdOcc s) (TcPat s) +type TcRecordBinds s = HsRecordBinds (TcBox s) (TcIdOcc s) (TcPat s) +type TcHsModule s = HsModule (TcBox s) (TcIdOcc s) (TcPat s) + +type TcCoreExpr s = GenCoreExpr (TcIdOcc s) (TcIdOcc s) (TcBox s) + +type TypecheckedPat = OutPat Unused Id +type TypecheckedMonoBinds = MonoBinds Unused Id TypecheckedPat +type TypecheckedDictBinds = TypecheckedMonoBinds +type TypecheckedHsBinds = HsBinds Unused Id TypecheckedPat +type TypecheckedHsExpr = HsExpr Unused Id TypecheckedPat +type TypecheckedArithSeqInfo = ArithSeqInfo Unused Id TypecheckedPat +type TypecheckedStmt = Stmt Unused Id TypecheckedPat +type TypecheckedMatch = Match Unused Id TypecheckedPat +type TypecheckedGRHSsAndBinds = GRHSsAndBinds Unused Id TypecheckedPat +type TypecheckedGRHS = GRHS Unused Id TypecheckedPat +type TypecheckedRecordBinds = HsRecordBinds Unused Id TypecheckedPat +type TypecheckedHsModule = HsModule Unused Id TypecheckedPat \end{code} \begin{code} @@ -106,123 +117,184 @@ mkHsDictLam [] expr = expr mkHsDictLam dicts expr = DictLam dicts expr \end{code} +%************************************************************************ +%* * +\subsection[BackSubst-HsBinds]{Running a substitution over @HsBinds@} +%* * +%************************************************************************ +Some gruesome hackery for desugaring ccalls. It's here because if we put it +in Type.lhs we get irritating loops, and it's only used by TcInstDcls.lhs and +DsCCall.lhs. \begin{code} -instance Eq (TcIdOcc s) where - (TcId id1) == (TcId id2) = id1 == id2 - (RealId id1) == (RealId id2) = id1 == id2 - -instance Outputable (TcIdOcc s) where - ppr sty (TcId id) = ppr sty id - ppr sty (RealId id) = ppr sty id - -instance NamedThing (TcIdOcc s) where - getOccurrenceName (TcId id) = getOccurrenceName id - getOccurrenceName (RealId id) = getOccurrenceName id +maybeBoxedPrimType :: Type -> Maybe (Id, Type) +maybeBoxedPrimType ty + = case splitAlgTyConApp_maybe ty of -- Data type, + Just (tycon, tys_applied, [data_con]) | isDataTyCon tycon -- with exactly one constructor + -> case (dataConArgTys data_con tys_applied) of + [data_con_arg_ty] -- Applied to exactly one type, + | isUnpointedType data_con_arg_ty -- which is primitive + -> Just (data_con, data_con_arg_ty) + other_cases -> Nothing + other_cases -> Nothing \end{code} - %************************************************************************ %* * \subsection[BackSubst-HsBinds]{Running a substitution over @HsBinds@} %* * %************************************************************************ +@zonkTcId@ just works on TcIdOccs. It's used when zonking Method insts. + \begin{code} -zonkId :: TcIdOcc s -> NF_TcM s Id -unZonkId :: Id -> NF_TcM s (TcIdBndr s) +zonkTcId :: TcIdOcc s -> NF_TcM s (TcIdOcc s) +zonkTcId tc_id@(RealId id) = returnNF_Tc tc_id +zonkTcId (TcId id) + = zonkTcType (idType id) `thenNF_Tc` \ ty' -> + returnNF_Tc (TcId (mkIdWithNewType id ty')) +\end{code} -zonkId (RealId id) = returnNF_Tc id +This zonking pass runs over the bindings -zonkId (TcId (Id u ty details prags info)) - = zonkTcTypeToType ty `thenNF_Tc` \ ty' -> - returnNF_Tc (Id u ty' details prags info) + a) to convert TcTyVars to TyVars etc, dereferencing any bindings etc + b) convert unbound TcTyVar to Void + c) convert each TcIdBndr to an Id by zonking its type -unZonkId (Id u ty details prags info) - = tcInstType [] ty `thenNF_Tc` \ ty' -> - returnNF_Tc (Id u ty' details prags info) -\end{code} +We pass an environment around so that -\begin{code} -zonkInst :: (TcIdOcc s, TcExpr s) -> NF_TcM s (Id, TypecheckedHsExpr) -zonkInst (id, expr) - = zonkId id `thenNF_Tc` \ id' -> - zonkExpr expr `thenNF_Tc` \ expr' -> - returnNF_Tc (id', expr') -\end{code} + a) we know which TyVars are unbound + b) we maintain sharing; eg an Id is zonked at its binding site and they + all occurrences of that Id point to the common zonked copy -\begin{code} -zonkBinds :: TcHsBinds s -> NF_TcM s TypecheckedHsBinds +Actually, since this is all in the Tc monad, it's convenient to keep the +mapping from TcIds to Ids in the GVE of the Tc monad. (Those TcIds +were previously in the LVE of the Tc monad.) -zonkBinds EmptyBinds = returnNF_Tc EmptyBinds +It's all pretty boring stuff, because HsSyn is such a large type, and +the environment manipulation is tiresome. -zonkBinds (ThenBinds binds1 binds2) - = zonkBinds binds1 `thenNF_Tc` \ new_binds1 -> - zonkBinds binds2 `thenNF_Tc` \ new_binds2 -> - returnNF_Tc (ThenBinds new_binds1 new_binds2) +\begin{code} +extend_te te tyvars = growTyVarEnvList te [(tyvar, mkTyVarTy tyvar) | tyvar <- tyvars] + +zonkIdBndr :: TyVarEnv Type -> TcIdOcc s -> NF_TcM s Id +zonkIdBndr te (RealId id) = returnNF_Tc id +zonkIdBndr te (TcId id) + = zonkTcTypeToType te (idType id) `thenNF_Tc` \ ty' -> + returnNF_Tc (mkIdWithNewType id ty') + + +zonkIdOcc :: TcIdOcc s -> NF_TcM s Id +zonkIdOcc (RealId id) = returnNF_Tc id +zonkIdOcc (TcId id) + = tcLookupGlobalValueMaybe (getName id) `thenNF_Tc` \ maybe_id' -> + let + new_id = case maybe_id' of + Just id' -> id' + Nothing -> pprTrace "zonkIdOcc: " (ppr id) $ + mkIdWithNewType id voidTy + in + returnNF_Tc new_id +\end{code} -zonkBinds (SingleBind bind) - = zonkBind bind `thenNF_Tc` \ new_bind -> - returnNF_Tc (SingleBind new_bind) -zonkBinds (AbsBinds tyvars dicts locprs dict_binds val_bind) - = mapNF_Tc zonkTcTyVarToTyVar tyvars `thenNF_Tc` \ new_tyvars -> - mapNF_Tc zonkId dicts `thenNF_Tc` \ new_dicts -> - mapNF_Tc subst_pair locprs `thenNF_Tc` \ new_locprs -> - mapNF_Tc subst_bind dict_binds `thenNF_Tc` \ new_dict_binds -> - zonkBind val_bind `thenNF_Tc` \ new_val_bind -> - returnNF_Tc (AbsBinds new_tyvars new_dicts new_locprs new_dict_binds new_val_bind) +\begin{code} +zonkTopBinds :: TcMonoBinds s -> NF_TcM s (TypecheckedMonoBinds, GlobalValueEnv) +zonkTopBinds binds -- Top level is implicitly recursive + = fixNF_Tc (\ ~(_, new_ids) -> + tcExtendGlobalValEnv (bagToList new_ids) $ + zonkMonoBinds emptyTyVarEnv binds `thenNF_Tc` \ (binds', new_ids) -> + tcGetGlobalValEnv `thenNF_Tc` \ env -> + returnNF_Tc ((binds', env), new_ids) + ) `thenNF_Tc` \ (stuff, _) -> + returnNF_Tc stuff + + +zonkBinds :: TyVarEnv Type + -> TcHsBinds s + -> NF_TcM s (TypecheckedHsBinds, TcEnv s) + +zonkBinds te binds + = go binds (\ binds' -> tcGetEnv `thenNF_Tc` \ env -> returnNF_Tc (binds', env)) where - subst_pair (l, g) - = zonkId l `thenNF_Tc` \ new_l -> - zonkId g `thenNF_Tc` \ new_g -> - returnNF_Tc (new_l, new_g) - - subst_bind (v, e) - = zonkId v `thenNF_Tc` \ new_v -> - zonkExpr e `thenNF_Tc` \ new_e -> - returnNF_Tc (new_v, new_e) + -- go :: TcHsBinds s -> (TypecheckedHsBinds -> NF_TcM s (TypecheckedHsBinds, TcEnv s)) + -- -> NF_TcM s (TypecheckedHsBinds, TcEnv s) + go (ThenBinds b1 b2) thing_inside = go b1 $ \ b1' -> + go b2 $ \ b2' -> + thing_inside (b1' `ThenBinds` b2') + + go EmptyBinds thing_inside = thing_inside EmptyBinds + + go (MonoBind bind sigs is_rec) thing_inside + = ASSERT( null sigs ) + fixNF_Tc (\ ~(_, new_ids) -> + tcExtendGlobalValEnv (bagToList new_ids) $ + zonkMonoBinds te bind `thenNF_Tc` \ (new_bind, new_ids) -> + thing_inside (MonoBind new_bind [] is_rec) `thenNF_Tc` \ stuff -> + returnNF_Tc (stuff, new_ids) + ) `thenNF_Tc` \ (stuff, _) -> + returnNF_Tc stuff \end{code} \begin{code} ------------------------------------------------------------------------- -zonkBind :: TcBind s -> NF_TcM s TypecheckedBind +zonkMonoBinds :: TyVarEnv Type + -> TcMonoBinds s + -> NF_TcM s (TypecheckedMonoBinds, Bag Id) -zonkBind EmptyBind = returnNF_Tc EmptyBind +zonkMonoBinds te EmptyMonoBinds = returnNF_Tc (EmptyMonoBinds, emptyBag) -zonkBind (NonRecBind mbinds) - = zonkMonoBinds mbinds `thenNF_Tc` \ new_mbinds -> - returnNF_Tc (NonRecBind new_mbinds) +zonkMonoBinds te (AndMonoBinds mbinds1 mbinds2) + = zonkMonoBinds te mbinds1 `thenNF_Tc` \ (b1', ids1) -> + zonkMonoBinds te mbinds2 `thenNF_Tc` \ (b2', ids2) -> + returnNF_Tc (b1' `AndMonoBinds` b2', ids1 `unionBags` ids2) -zonkBind (RecBind mbinds) - = zonkMonoBinds mbinds `thenNF_Tc` \ new_mbinds -> - returnNF_Tc (RecBind new_mbinds) +zonkMonoBinds te (PatMonoBind pat grhss_w_binds locn) + = zonkPat te pat `thenNF_Tc` \ (new_pat, ids) -> + zonkGRHSsAndBinds te grhss_w_binds `thenNF_Tc` \ new_grhss_w_binds -> + returnNF_Tc (PatMonoBind new_pat new_grhss_w_binds locn, ids) -------------------------------------------------------------------------- -zonkMonoBinds :: TcMonoBinds s -> NF_TcM s TypecheckedMonoBinds +zonkMonoBinds te (VarMonoBind var expr) + = zonkIdBndr te var `thenNF_Tc` \ new_var -> + zonkExpr te expr `thenNF_Tc` \ new_expr -> + returnNF_Tc (VarMonoBind new_var new_expr, unitBag new_var) -zonkMonoBinds EmptyMonoBinds = returnNF_Tc EmptyMonoBinds +zonkMonoBinds te (CoreMonoBind var core_expr) + = zonkIdBndr te var `thenNF_Tc` \ new_var -> + returnNF_Tc (CoreMonoBind new_var core_expr, unitBag new_var) -zonkMonoBinds (AndMonoBinds mbinds1 mbinds2) - = zonkMonoBinds mbinds1 `thenNF_Tc` \ new_mbinds1 -> - zonkMonoBinds mbinds2 `thenNF_Tc` \ new_mbinds2 -> - returnNF_Tc (AndMonoBinds new_mbinds1 new_mbinds2) +zonkMonoBinds te (FunMonoBind var inf ms locn) + = zonkIdBndr te var `thenNF_Tc` \ new_var -> + mapNF_Tc (zonkMatch te) ms `thenNF_Tc` \ new_ms -> + returnNF_Tc (FunMonoBind new_var inf new_ms locn, unitBag new_var) -zonkMonoBinds (PatMonoBind pat grhss_w_binds locn) - = zonkPat pat `thenNF_Tc` \ new_pat -> - zonkGRHSsAndBinds grhss_w_binds `thenNF_Tc` \ new_grhss_w_binds -> - returnNF_Tc (PatMonoBind new_pat new_grhss_w_binds locn) -zonkMonoBinds (VarMonoBind var expr) - = zonkId var `thenNF_Tc` \ new_var -> - zonkExpr expr `thenNF_Tc` \ new_expr -> - returnNF_Tc (VarMonoBind new_var new_expr) - -zonkMonoBinds (FunMonoBind name ms locn) - = zonkId name `thenNF_Tc` \ new_name -> - mapNF_Tc zonkMatch ms `thenNF_Tc` \ new_ms -> - returnNF_Tc (FunMonoBind new_name new_ms locn) +zonkMonoBinds te (AbsBinds tyvars dicts exports val_bind) + = mapNF_Tc zonkTcTyVarToTyVar tyvars `thenNF_Tc` \ new_tyvars -> + let + new_te = extend_te te new_tyvars + in + mapNF_Tc (zonkIdBndr new_te) dicts `thenNF_Tc` \ new_dicts -> + + tcExtendGlobalValEnv new_dicts $ + fixNF_Tc (\ ~(_, _, val_bind_ids) -> + tcExtendGlobalValEnv (bagToList val_bind_ids) $ + zonkMonoBinds new_te val_bind `thenNF_Tc` \ (new_val_bind, val_bind_ids) -> + mapNF_Tc (zonkExport new_te) exports `thenNF_Tc` \ new_exports -> + returnNF_Tc (new_val_bind, new_exports, val_bind_ids) + ) `thenNF_Tc ` \ (new_val_bind, new_exports, _) -> + let + new_globals = listToBag [global | (_, global, local) <- new_exports] + in + returnNF_Tc (AbsBinds new_tyvars new_dicts new_exports new_val_bind, + new_globals) + where + zonkExport te (tyvars, global, local) + = mapNF_Tc zonkTcTyVarToTyVar tyvars `thenNF_Tc` \ new_tyvars -> + zonkIdBndr te global `thenNF_Tc` \ new_global -> + zonkIdOcc local `thenNF_Tc` \ new_local -> + returnNF_Tc (new_tyvars, new_global, new_local) \end{code} %************************************************************************ @@ -232,35 +304,41 @@ zonkMonoBinds (FunMonoBind name ms locn) %************************************************************************ \begin{code} -zonkMatch :: TcMatch s -> NF_TcM s TypecheckedMatch +zonkMatch :: TyVarEnv Type + -> TcMatch s -> NF_TcM s TypecheckedMatch -zonkMatch (PatMatch pat match) - = zonkPat pat `thenNF_Tc` \ new_pat -> - zonkMatch match `thenNF_Tc` \ new_match -> +zonkMatch te (PatMatch pat match) + = zonkPat te pat `thenNF_Tc` \ (new_pat, ids) -> + tcExtendGlobalValEnv (bagToList ids) $ + zonkMatch te match `thenNF_Tc` \ new_match -> returnNF_Tc (PatMatch new_pat new_match) -zonkMatch (GRHSMatch grhss_w_binds) - = zonkGRHSsAndBinds grhss_w_binds `thenNF_Tc` \ new_grhss_w_binds -> +zonkMatch te (GRHSMatch grhss_w_binds) + = zonkGRHSsAndBinds te grhss_w_binds `thenNF_Tc` \ new_grhss_w_binds -> returnNF_Tc (GRHSMatch new_grhss_w_binds) -------------------------------------------------------------------------- -zonkGRHSsAndBinds :: TcGRHSsAndBinds s - -> NF_TcM s TypecheckedGRHSsAndBinds +zonkMatch te (SimpleMatch expr) + = zonkExpr te expr `thenNF_Tc` \ new_expr -> + returnNF_Tc (SimpleMatch new_expr) -zonkGRHSsAndBinds (GRHSsAndBindsOut grhss binds ty) - = mapNF_Tc zonk_grhs grhss `thenNF_Tc` \ new_grhss -> - zonkBinds binds `thenNF_Tc` \ new_binds -> - zonkTcTypeToType ty `thenNF_Tc` \ new_ty -> +------------------------------------------------------------------------- +zonkGRHSsAndBinds :: TyVarEnv Type + -> TcGRHSsAndBinds s + -> NF_TcM s TypecheckedGRHSsAndBinds + +zonkGRHSsAndBinds te (GRHSsAndBindsOut grhss binds ty) + = zonkBinds te binds `thenNF_Tc` \ (new_binds, new_env) -> + tcSetEnv new_env $ + let + zonk_grhs (GRHS guard expr locn) + = zonkStmts te guard `thenNF_Tc` \ (new_guard, new_env) -> + tcSetEnv new_env $ + zonkExpr te expr `thenNF_Tc` \ new_expr -> + returnNF_Tc (GRHS new_guard new_expr locn) + in + mapNF_Tc zonk_grhs grhss `thenNF_Tc` \ new_grhss -> + zonkTcTypeToType te ty `thenNF_Tc` \ new_ty -> returnNF_Tc (GRHSsAndBindsOut new_grhss new_binds new_ty) - where - zonk_grhs (GRHS guard expr locn) - = zonkExpr guard `thenNF_Tc` \ new_guard -> - zonkExpr expr `thenNF_Tc` \ new_expr -> - returnNF_Tc (GRHS new_guard new_expr locn) - - zonk_grhs (OtherwiseGRHS expr locn) - = zonkExpr expr `thenNF_Tc` \ new_expr -> - returnNF_Tc (OtherwiseGRHS new_expr locn) \end{code} %************************************************************************ @@ -269,196 +347,222 @@ zonkGRHSsAndBinds (GRHSsAndBindsOut grhss binds ty) %* * %************************************************************************ -ToDo: panic on things that can't be in @TypecheckedHsExpr@. - \begin{code} -zonkExpr :: TcExpr s -> NF_TcM s TypecheckedHsExpr +zonkExpr :: TyVarEnv Type + -> TcExpr s -> NF_TcM s TypecheckedHsExpr -zonkExpr (HsVar name) - = zonkId name `thenNF_Tc` \ new_name -> - returnNF_Tc (HsVar new_name) +zonkExpr te (HsVar id) + = zonkIdOcc id `thenNF_Tc` \ id' -> + returnNF_Tc (HsVar id') -zonkExpr (HsLitOut lit ty) - = zonkTcTypeToType ty `thenNF_Tc` \ new_ty -> +zonkExpr te (HsLit _) = panic "zonkExpr te:HsLit" + +zonkExpr te (HsLitOut lit ty) + = zonkTcTypeToType te ty `thenNF_Tc` \ new_ty -> returnNF_Tc (HsLitOut lit new_ty) -zonkExpr (HsLam match) - = zonkMatch match `thenNF_Tc` \ new_match -> +zonkExpr te (HsLam match) + = zonkMatch te match `thenNF_Tc` \ new_match -> returnNF_Tc (HsLam new_match) -zonkExpr (HsApp e1 e2) - = zonkExpr e1 `thenNF_Tc` \ new_e1 -> - zonkExpr e2 `thenNF_Tc` \ new_e2 -> +zonkExpr te (HsApp e1 e2) + = zonkExpr te e1 `thenNF_Tc` \ new_e1 -> + zonkExpr te e2 `thenNF_Tc` \ new_e2 -> returnNF_Tc (HsApp new_e1 new_e2) -zonkExpr (OpApp e1 op e2) - = zonkExpr e1 `thenNF_Tc` \ new_e1 -> - zonkExpr op `thenNF_Tc` \ new_op -> - zonkExpr e2 `thenNF_Tc` \ new_e2 -> - returnNF_Tc (OpApp new_e1 new_op new_e2) +zonkExpr te (OpApp e1 op fixity e2) + = zonkExpr te e1 `thenNF_Tc` \ new_e1 -> + zonkExpr te op `thenNF_Tc` \ new_op -> + zonkExpr te e2 `thenNF_Tc` \ new_e2 -> + returnNF_Tc (OpApp new_e1 new_op fixity new_e2) + +zonkExpr te (NegApp _ _) = panic "zonkExpr te:NegApp" +zonkExpr te (HsPar _) = panic "zonkExpr te:HsPar" -zonkExpr (SectionL expr op) - = zonkExpr expr `thenNF_Tc` \ new_expr -> - zonkExpr op `thenNF_Tc` \ new_op -> +zonkExpr te (SectionL expr op) + = zonkExpr te expr `thenNF_Tc` \ new_expr -> + zonkExpr te op `thenNF_Tc` \ new_op -> returnNF_Tc (SectionL new_expr new_op) -zonkExpr (SectionR op expr) - = zonkExpr op `thenNF_Tc` \ new_op -> - zonkExpr expr `thenNF_Tc` \ new_expr -> +zonkExpr te (SectionR op expr) + = zonkExpr te op `thenNF_Tc` \ new_op -> + zonkExpr te expr `thenNF_Tc` \ new_expr -> returnNF_Tc (SectionR new_op new_expr) -zonkExpr (CCall fun args may_gc is_casm result_ty) - = mapNF_Tc zonkExpr args `thenNF_Tc` \ new_args -> - zonkTcTypeToType result_ty `thenNF_Tc` \ new_result_ty -> - returnNF_Tc (CCall fun new_args may_gc is_casm new_result_ty) - -zonkExpr (HsSCC label expr) - = zonkExpr expr `thenNF_Tc` \ new_expr -> - returnNF_Tc (HsSCC label new_expr) - -zonkExpr (HsCase expr ms src_loc) - = zonkExpr expr `thenNF_Tc` \ new_expr -> - mapNF_Tc zonkMatch ms `thenNF_Tc` \ new_ms -> +zonkExpr te (HsCase expr ms src_loc) + = zonkExpr te expr `thenNF_Tc` \ new_expr -> + mapNF_Tc (zonkMatch te) ms `thenNF_Tc` \ new_ms -> returnNF_Tc (HsCase new_expr new_ms src_loc) -zonkExpr (HsLet binds expr) - = zonkBinds binds `thenNF_Tc` \ new_binds -> - zonkExpr expr `thenNF_Tc` \ new_expr -> +zonkExpr te (HsIf e1 e2 e3 src_loc) + = zonkExpr te e1 `thenNF_Tc` \ new_e1 -> + zonkExpr te e2 `thenNF_Tc` \ new_e2 -> + zonkExpr te e3 `thenNF_Tc` \ new_e3 -> + returnNF_Tc (HsIf new_e1 new_e2 new_e3 src_loc) + +zonkExpr te (HsLet binds expr) + = zonkBinds te binds `thenNF_Tc` \ (new_binds, new_env) -> + tcSetEnv new_env $ + zonkExpr te expr `thenNF_Tc` \ new_expr -> returnNF_Tc (HsLet new_binds new_expr) -zonkExpr (HsDoOut stmts m_id mz_id src_loc) - = zonkStmts stmts `thenNF_Tc` \ new_stmts -> - zonkId m_id `thenNF_Tc` \ m_new -> - zonkId mz_id `thenNF_Tc` \ mz_new -> - returnNF_Tc (HsDoOut new_stmts m_new mz_new src_loc) +zonkExpr te (HsDo _ _ _) = panic "zonkExpr te:HsDo" -zonkExpr (ListComp expr quals) - = zonkExpr expr `thenNF_Tc` \ new_expr -> - zonkQuals quals `thenNF_Tc` \ new_quals -> - returnNF_Tc (ListComp new_expr new_quals) +zonkExpr te (HsDoOut do_or_lc stmts return_id then_id zero_id ty src_loc) + = zonkStmts te stmts `thenNF_Tc` \ (new_stmts, _) -> + zonkTcTypeToType te ty `thenNF_Tc` \ new_ty -> + zonkIdOcc return_id `thenNF_Tc` \ new_return_id -> + zonkIdOcc then_id `thenNF_Tc` \ new_then_id -> + zonkIdOcc zero_id `thenNF_Tc` \ new_zero_id -> + returnNF_Tc (HsDoOut do_or_lc new_stmts new_return_id new_then_id new_zero_id + new_ty src_loc) ---ExplicitList: not in typechecked exprs +zonkExpr te (ExplicitList _) = panic "zonkExpr te:ExplicitList" -zonkExpr (ExplicitListOut ty exprs) - = zonkTcTypeToType ty `thenNF_Tc` \ new_ty -> - mapNF_Tc zonkExpr exprs `thenNF_Tc` \ new_exprs -> +zonkExpr te (ExplicitListOut ty exprs) + = zonkTcTypeToType te ty `thenNF_Tc` \ new_ty -> + mapNF_Tc (zonkExpr te) exprs `thenNF_Tc` \ new_exprs -> returnNF_Tc (ExplicitListOut new_ty new_exprs) -zonkExpr (ExplicitTuple exprs) - = mapNF_Tc zonkExpr exprs `thenNF_Tc` \ new_exprs -> +zonkExpr te (ExplicitTuple exprs) + = mapNF_Tc (zonkExpr te) exprs `thenNF_Tc` \ new_exprs -> returnNF_Tc (ExplicitTuple new_exprs) -zonkExpr (RecordCon con rbinds) - = panic "zonkExpr:RecordCon" -zonkExpr (RecordUpd exp rbinds) - = panic "zonkExpr:RecordUpd" +zonkExpr te (HsCon con_id tys exprs) + = mapNF_Tc (zonkTcTypeToType te) tys `thenNF_Tc` \ new_tys -> + mapNF_Tc (zonkExpr te) exprs `thenNF_Tc` \ new_exprs -> + returnNF_Tc (HsCon con_id new_tys new_exprs) -zonkExpr (HsIf e1 e2 e3 src_loc) - = zonkExpr e1 `thenNF_Tc` \ new_e1 -> - zonkExpr e2 `thenNF_Tc` \ new_e2 -> - zonkExpr e3 `thenNF_Tc` \ new_e3 -> - returnNF_Tc (HsIf new_e1 new_e2 new_e3 src_loc) +zonkExpr te (RecordCon con_id con_expr rbinds) + = zonkIdOcc con_id `thenNF_Tc` \ new_con_id -> + zonkExpr te con_expr `thenNF_Tc` \ new_con_expr -> + zonkRbinds te rbinds `thenNF_Tc` \ new_rbinds -> + returnNF_Tc (RecordCon new_con_id new_con_expr new_rbinds) + +zonkExpr te (RecordUpd _ _) = panic "zonkExpr te:RecordUpd" -zonkExpr (ArithSeqOut expr info) - = zonkExpr expr `thenNF_Tc` \ new_expr -> - zonkArithSeq info `thenNF_Tc` \ new_info -> +zonkExpr te (RecordUpdOut expr ty dicts rbinds) + = zonkExpr te expr `thenNF_Tc` \ new_expr -> + zonkTcTypeToType te ty `thenNF_Tc` \ new_ty -> + mapNF_Tc zonkIdOcc dicts `thenNF_Tc` \ new_dicts -> + zonkRbinds te rbinds `thenNF_Tc` \ new_rbinds -> + returnNF_Tc (RecordUpdOut new_expr new_ty new_dicts new_rbinds) + +zonkExpr te (ExprWithTySig _ _) = panic "zonkExpr te:ExprWithTySig" +zonkExpr te (ArithSeqIn _) = panic "zonkExpr te:ArithSeqIn" + +zonkExpr te (ArithSeqOut expr info) + = zonkExpr te expr `thenNF_Tc` \ new_expr -> + zonkArithSeq te info `thenNF_Tc` \ new_info -> returnNF_Tc (ArithSeqOut new_expr new_info) -zonkExpr (TyLam tyvars expr) +zonkExpr te (CCall fun args may_gc is_casm result_ty) + = mapNF_Tc (zonkExpr te) args `thenNF_Tc` \ new_args -> + zonkTcTypeToType te result_ty `thenNF_Tc` \ new_result_ty -> + returnNF_Tc (CCall fun new_args may_gc is_casm new_result_ty) + +zonkExpr te (HsSCC label expr) + = zonkExpr te expr `thenNF_Tc` \ new_expr -> + returnNF_Tc (HsSCC label new_expr) + +zonkExpr te (TyLam tyvars expr) = mapNF_Tc zonkTcTyVarToTyVar tyvars `thenNF_Tc` \ new_tyvars -> - zonkExpr expr `thenNF_Tc` \ new_expr -> + let + new_te = extend_te te new_tyvars + in + zonkExpr new_te expr `thenNF_Tc` \ new_expr -> returnNF_Tc (TyLam new_tyvars new_expr) -zonkExpr (TyApp expr tys) - = zonkExpr expr `thenNF_Tc` \ new_expr -> - mapNF_Tc zonkTcTypeToType tys `thenNF_Tc` \ new_tys -> +zonkExpr te (TyApp expr tys) + = zonkExpr te expr `thenNF_Tc` \ new_expr -> + mapNF_Tc (zonkTcTypeToType te) tys `thenNF_Tc` \ new_tys -> returnNF_Tc (TyApp new_expr new_tys) -zonkExpr (DictLam dicts expr) - = mapNF_Tc zonkId dicts `thenNF_Tc` \ new_dicts -> - zonkExpr expr `thenNF_Tc` \ new_expr -> +zonkExpr te (DictLam dicts expr) + = mapNF_Tc (zonkIdBndr te) dicts `thenNF_Tc` \ new_dicts -> + tcExtendGlobalValEnv new_dicts $ + zonkExpr te expr `thenNF_Tc` \ new_expr -> returnNF_Tc (DictLam new_dicts new_expr) -zonkExpr (DictApp expr dicts) - = zonkExpr expr `thenNF_Tc` \ new_expr -> - mapNF_Tc zonkId dicts `thenNF_Tc` \ new_dicts -> +zonkExpr te (DictApp expr dicts) + = zonkExpr te expr `thenNF_Tc` \ new_expr -> + mapNF_Tc zonkIdOcc dicts `thenNF_Tc` \ new_dicts -> returnNF_Tc (DictApp new_expr new_dicts) -zonkExpr (ClassDictLam dicts methods expr) - = mapNF_Tc zonkId dicts `thenNF_Tc` \ new_dicts -> - mapNF_Tc zonkId methods `thenNF_Tc` \ new_methods -> - zonkExpr expr `thenNF_Tc` \ new_expr -> - returnNF_Tc (ClassDictLam new_dicts new_methods new_expr) - -zonkExpr (Dictionary dicts methods) - = mapNF_Tc zonkId dicts `thenNF_Tc` \ new_dicts -> - mapNF_Tc zonkId methods `thenNF_Tc` \ new_methods -> - returnNF_Tc (Dictionary new_dicts new_methods) -zonkExpr (SingleDict name) - = zonkId name `thenNF_Tc` \ new_name -> - returnNF_Tc (SingleDict new_name) ------------------------------------------------------------------------- -zonkArithSeq :: TcArithSeqInfo s -> NF_TcM s TypecheckedArithSeqInfo +zonkArithSeq :: TyVarEnv Type + -> TcArithSeqInfo s -> NF_TcM s TypecheckedArithSeqInfo -zonkArithSeq (From e) - = zonkExpr e `thenNF_Tc` \ new_e -> +zonkArithSeq te (From e) + = zonkExpr te e `thenNF_Tc` \ new_e -> returnNF_Tc (From new_e) -zonkArithSeq (FromThen e1 e2) - = zonkExpr e1 `thenNF_Tc` \ new_e1 -> - zonkExpr e2 `thenNF_Tc` \ new_e2 -> +zonkArithSeq te (FromThen e1 e2) + = zonkExpr te e1 `thenNF_Tc` \ new_e1 -> + zonkExpr te e2 `thenNF_Tc` \ new_e2 -> returnNF_Tc (FromThen new_e1 new_e2) -zonkArithSeq (FromTo e1 e2) - = zonkExpr e1 `thenNF_Tc` \ new_e1 -> - zonkExpr e2 `thenNF_Tc` \ new_e2 -> +zonkArithSeq te (FromTo e1 e2) + = zonkExpr te e1 `thenNF_Tc` \ new_e1 -> + zonkExpr te e2 `thenNF_Tc` \ new_e2 -> returnNF_Tc (FromTo new_e1 new_e2) -zonkArithSeq (FromThenTo e1 e2 e3) - = zonkExpr e1 `thenNF_Tc` \ new_e1 -> - zonkExpr e2 `thenNF_Tc` \ new_e2 -> - zonkExpr e3 `thenNF_Tc` \ new_e3 -> +zonkArithSeq te (FromThenTo e1 e2 e3) + = zonkExpr te e1 `thenNF_Tc` \ new_e1 -> + zonkExpr te e2 `thenNF_Tc` \ new_e2 -> + zonkExpr te e3 `thenNF_Tc` \ new_e3 -> returnNF_Tc (FromThenTo new_e1 new_e2 new_e3) ------------------------------------------------------------------------- -zonkQuals :: [TcQual s] -> NF_TcM s [TypecheckedQual] +zonkStmts :: TyVarEnv Type + -> [TcStmt s] -> NF_TcM s ([TypecheckedStmt], TcEnv s) -zonkQuals quals - = mapNF_Tc zonk_qual quals - where - zonk_qual (GeneratorQual pat expr) - = zonkPat pat `thenNF_Tc` \ new_pat -> - zonkExpr expr `thenNF_Tc` \ new_expr -> - returnNF_Tc (GeneratorQual new_pat new_expr) +zonkStmts te [] = tcGetEnv `thenNF_Tc` \ env -> + returnNF_Tc ([], env) + +zonkStmts te [ReturnStmt expr] + = zonkExpr te expr `thenNF_Tc` \ new_expr -> + tcGetEnv `thenNF_Tc` \ env -> + returnNF_Tc ([ReturnStmt new_expr], env) + +zonkStmts te (ExprStmt expr locn : stmts) + = zonkExpr te expr `thenNF_Tc` \ new_expr -> + zonkStmts te stmts `thenNF_Tc` \ (new_stmts, new_env) -> + returnNF_Tc (ExprStmt new_expr locn : new_stmts, new_env) + +zonkStmts te (GuardStmt expr locn : stmts) + = zonkExpr te expr `thenNF_Tc` \ new_expr -> + zonkStmts te stmts `thenNF_Tc` \ (new_stmts, new_env) -> + returnNF_Tc (GuardStmt new_expr locn : new_stmts, new_env) + +zonkStmts te (LetStmt binds : stmts) + = zonkBinds te binds `thenNF_Tc` \ (new_binds, new_env) -> + tcSetEnv new_env $ + zonkStmts te stmts `thenNF_Tc` \ (new_stmts, new_env2) -> + returnNF_Tc (LetStmt new_binds : new_stmts, new_env2) + +zonkStmts te (BindStmt pat expr locn : stmts) + = zonkPat te pat `thenNF_Tc` \ (new_pat, ids) -> + zonkExpr te expr `thenNF_Tc` \ new_expr -> + tcExtendGlobalValEnv (bagToList ids) $ + zonkStmts te stmts `thenNF_Tc` \ (new_stmts, new_env) -> + returnNF_Tc (BindStmt new_pat new_expr locn : new_stmts, new_env) - zonk_qual (FilterQual expr) - = zonkExpr expr `thenNF_Tc` \ new_expr -> - returnNF_Tc (FilterQual new_expr) - zonk_qual (LetQual binds) - = zonkBinds binds `thenNF_Tc` \ new_binds -> - returnNF_Tc (LetQual new_binds) ------------------------------------------------------------------------- -zonkStmts :: [TcStmt s] -> NF_TcM s [TypecheckedStmt] +zonkRbinds :: TyVarEnv Type + -> TcRecordBinds s -> NF_TcM s TypecheckedRecordBinds -zonkStmts stmts - = mapNF_Tc zonk_stmt stmts +zonkRbinds te rbinds + = mapNF_Tc zonk_rbind rbinds where - zonk_stmt (BindStmt pat expr src_loc) - = zonkPat pat `thenNF_Tc` \ new_pat -> - zonkExpr expr `thenNF_Tc` \ new_expr -> - returnNF_Tc (BindStmt new_pat new_expr src_loc) - - zonk_stmt (ExprStmt expr src_loc) - = zonkExpr expr `thenNF_Tc` \ new_expr -> - returnNF_Tc (ExprStmt new_expr src_loc) - - zonk_stmt (LetStmt binds) - = zonkBinds binds `thenNF_Tc` \ new_binds -> - returnNF_Tc (LetStmt new_binds) + zonk_rbind (field, expr, pun) + = zonkExpr te expr `thenNF_Tc` \ new_expr -> + zonkIdOcc field `thenNF_Tc` \ new_field -> + returnNF_Tc (new_field, new_expr, pun) \end{code} %************************************************************************ @@ -468,58 +572,84 @@ zonkStmts stmts %************************************************************************ \begin{code} -zonkPat :: TcPat s -> NF_TcM s TypecheckedPat - -zonkPat (WildPat ty) - = zonkTcTypeToType ty `thenNF_Tc` \ new_ty -> - returnNF_Tc (WildPat new_ty) - -zonkPat (VarPat v) - = zonkId v `thenNF_Tc` \ new_v -> - returnNF_Tc (VarPat new_v) - -zonkPat (LazyPat pat) - = zonkPat pat `thenNF_Tc` \ new_pat -> - returnNF_Tc (LazyPat new_pat) - -zonkPat (AsPat n pat) - = zonkId n `thenNF_Tc` \ new_n -> - zonkPat pat `thenNF_Tc` \ new_pat -> - returnNF_Tc (AsPat new_n new_pat) - -zonkPat (ConPat n ty pats) - = zonkTcTypeToType ty `thenNF_Tc` \ new_ty -> - mapNF_Tc zonkPat pats `thenNF_Tc` \ new_pats -> - returnNF_Tc (ConPat n new_ty new_pats) - -zonkPat (ConOpPat pat1 op pat2 ty) - = zonkPat pat1 `thenNF_Tc` \ new_pat1 -> - zonkPat pat2 `thenNF_Tc` \ new_pat2 -> - zonkTcTypeToType ty `thenNF_Tc` \ new_ty -> - returnNF_Tc (ConOpPat new_pat1 op new_pat2 new_ty) - -zonkPat (ListPat ty pats) - = zonkTcTypeToType ty `thenNF_Tc` \ new_ty -> - mapNF_Tc zonkPat pats `thenNF_Tc` \ new_pats -> - returnNF_Tc (ListPat new_ty new_pats) - -zonkPat (TuplePat pats) - = mapNF_Tc zonkPat pats `thenNF_Tc` \ new_pats -> - returnNF_Tc (TuplePat new_pats) - -zonkPat (LitPat lit ty) - = zonkTcTypeToType ty `thenNF_Tc` \ new_ty -> - returnNF_Tc (LitPat lit new_ty) - -zonkPat (NPat lit ty expr) - = zonkTcTypeToType ty `thenNF_Tc` \ new_ty -> - zonkExpr expr `thenNF_Tc` \ new_expr -> - returnNF_Tc (NPat lit new_ty new_expr) - -zonkPat (DictPat ds ms) - = mapNF_Tc zonkId ds `thenNF_Tc` \ new_ds -> - mapNF_Tc zonkId ms `thenNF_Tc` \ new_ms -> - returnNF_Tc (DictPat new_ds new_ms) +zonkPat :: TyVarEnv Type + -> TcPat s -> NF_TcM s (TypecheckedPat, Bag Id) + +zonkPat te (WildPat ty) + = zonkTcTypeToType te ty `thenNF_Tc` \ new_ty -> + returnNF_Tc (WildPat new_ty, emptyBag) + +zonkPat te (VarPat v) + = zonkIdBndr te v `thenNF_Tc` \ new_v -> + returnNF_Tc (VarPat new_v, unitBag new_v) + +zonkPat te (LazyPat pat) + = zonkPat te pat `thenNF_Tc` \ (new_pat, ids) -> + returnNF_Tc (LazyPat new_pat, ids) + +zonkPat te (AsPat n pat) + = zonkIdBndr te n `thenNF_Tc` \ new_n -> + zonkPat te pat `thenNF_Tc` \ (new_pat, ids) -> + returnNF_Tc (AsPat new_n new_pat, new_n `consBag` ids) + +zonkPat te (ConPat n ty pats) + = zonkTcTypeToType te ty `thenNF_Tc` \ new_ty -> + zonkPats te pats `thenNF_Tc` \ (new_pats, ids) -> + returnNF_Tc (ConPat n new_ty new_pats, ids) + +zonkPat te (ConOpPat pat1 op pat2 ty) + = zonkPat te pat1 `thenNF_Tc` \ (new_pat1, ids1) -> + zonkPat te pat2 `thenNF_Tc` \ (new_pat2, ids2) -> + zonkTcTypeToType te ty `thenNF_Tc` \ new_ty -> + returnNF_Tc (ConOpPat new_pat1 op new_pat2 new_ty, ids1 `unionBags` ids2) + +zonkPat te (ListPat ty pats) + = zonkTcTypeToType te ty `thenNF_Tc` \ new_ty -> + zonkPats te pats `thenNF_Tc` \ (new_pats, ids) -> + returnNF_Tc (ListPat new_ty new_pats, ids) + +zonkPat te (TuplePat pats) + = zonkPats te pats `thenNF_Tc` \ (new_pats, ids) -> + returnNF_Tc (TuplePat new_pats, ids) + +zonkPat te (RecPat n ty rpats) + = zonkTcTypeToType te ty `thenNF_Tc` \ new_ty -> + mapAndUnzipNF_Tc zonk_rpat rpats `thenNF_Tc` \ (new_rpats, ids_s) -> + returnNF_Tc (RecPat n new_ty new_rpats, unionManyBags ids_s) + where + zonk_rpat (f, pat, pun) + = zonkPat te pat `thenNF_Tc` \ (new_pat, ids) -> + returnNF_Tc ((f, new_pat, pun), ids) + +zonkPat te (LitPat lit ty) + = zonkTcTypeToType te ty `thenNF_Tc` \ new_ty -> + returnNF_Tc (LitPat lit new_ty, emptyBag) + +zonkPat te (NPat lit ty expr) + = zonkTcTypeToType te ty `thenNF_Tc` \ new_ty -> + zonkExpr te expr `thenNF_Tc` \ new_expr -> + returnNF_Tc (NPat lit new_ty new_expr, emptyBag) + +zonkPat te (NPlusKPat n k ty e1 e2) + = zonkIdBndr te n `thenNF_Tc` \ new_n -> + zonkTcTypeToType te ty `thenNF_Tc` \ new_ty -> + zonkExpr te e1 `thenNF_Tc` \ new_e1 -> + zonkExpr te e2 `thenNF_Tc` \ new_e2 -> + returnNF_Tc (NPlusKPat new_n k new_ty new_e1 new_e2, unitBag new_n) + +zonkPat te (DictPat ds ms) + = mapNF_Tc (zonkIdBndr te) ds `thenNF_Tc` \ new_ds -> + mapNF_Tc (zonkIdBndr te) ms `thenNF_Tc` \ new_ms -> + returnNF_Tc (DictPat new_ds new_ms, + listToBag new_ds `unionBags` listToBag new_ms) + + +zonkPats te [] + = returnNF_Tc ([], emptyBag) +zonkPats te (pat:pats) + = zonkPat te pat `thenNF_Tc` \ (pat', ids1) -> + zonkPats te pats `thenNF_Tc` \ (pats', ids2) -> + returnNF_Tc (pat':pats', ids1 `unionBags` ids2) \end{code}