initEnvs             :: Map a (ImportInfo IFace) -> M s (Desugar1Env, RenameEnv, CheckEnv, KindleEnv)
initEnvs bms         = do ims <- mapM (mkEnv . snd) bms
                          let (rs,xs,ss,rnL,rnT,rnE,ds,ws,bs,kds) 
                               = foldr mergeMod ([],[],[],[],[],[],Types [] [],[],Binds False [] [],[]) ims
                          return ((rs,rnL,ss),(rnL,rnT,rnE),(xs,ds,ws,bs),kds)

  where mergeMod (rs1,xs1,ss1,rnL1,rnT1,rnE1,ds1,ws1,bs1,kds1)
                 (rs2,xs2,ss2,rnL2,rnT2,rnE2,ds2,ws2,bs2,kds2) 
                                     = (rs1 ++ rs2, xs1 ++ xs2, ss1 ++ ss2, mergeRenamings2 rnL1 rnL2, 
                                       mergeRenamings2 rnT1 rnT2, mergeRenamings2 rnE1 rnE2,
                                       catDecls ds1 ds2, ws1++ws2, catBinds bs1 bs2,
                                       kds1 ++ kds2)

        mkEnv (unQual,IFace ns xs rs ss ds ws bs kds)
                                     = do ks  <- renaming (dom ke)
                                          ts  <- renaming (dom te'')
                                          ls' <- renaming ls -- (concatMap snd rs)
                                          return (unMod unQual rs, xs, unMod unQual ss, unMod unQual ls',unMod unQual ks,
                                                  unMod unQual ts,ds,ws,Binds r te' es,kds)
          where Types ke ds'         = ds
                Binds r te es        = bs
                te'                  = te ++ concatMap (tenvSelCon ke) ds'
                te''                 = te  ++ concatMap (tenvCon ke) ds'
                ls                   = [ s | (_,DRec _ _ _ cs) <- ds', (s,_) <- cs, not (isGenerated s) ]
                unMod b ps           = if b then [(tag0 (dropMod c),y) | (c,y) <- ps] ++ ps else ps

-- Checking that public part is closed ---------------------------------------------------------
initEnvs             :: Map a (ImportInfo IFace) -> M s (Desugar1Env, RenameEnv, CheckEnv, KindleEnv)
initEnvs bms         = do ims <- mapM (mkEnv . snd) bms
                          let (rs,xs,ss,rnL,rnT,rnE,ds,ws,bs,kds) 
                               = foldr mergeMod ([],[],[],[],[],[],Types [] [],[],Binds False [] [],[]) ims
                          return ((rs,rnL,ss),(rnL,rnT,rnE),(xs,ds,ws,bs),kds)

  where mergeMod (rs1,xs1,ss1,rnL1,rnT1,rnE1,ds1,ws1,bs1,kds1)
                 (rs2,xs2,ss2,rnL2,rnT2,rnE2,ds2,ws2,bs2,kds2) 
                                     = (rs1 ++ rs2, xs1 ++ xs2, ss1 ++ ss2, mergeRenamings2 rnL1 rnL2, 
                                       mergeRenamings2 rnT1 rnT2, mergeRenamings2 rnE1 rnE2,
                                       catDecls ds1 ds2, ws1++ws2, catBinds bs1 bs2,
                                       kds1 ++ kds2)

        mkEnv (unQual,IFace ns xs rs ss ds ws bs kds)
                                     = do ks  <- renaming (dom ke)
                                          ts  <- renaming (dom te'')
                                          ls' <- renaming ls -- (concatMap snd rs)
                                          return (unMod unQual rs, xs, unMod unQual ss, unMod unQual ls',unMod unQual ks,
                                                  unMod unQual ts,ds,ws,Binds r te' es,kds)
          where Types ke ds'         = ds
                Binds r te es        = bs
                te'                  = te ++ concatMap (tenvSelCon ke) ds'
                te''                 = te  ++ concatMap (tenvCon ke) ds'
                ls                   = [ s | (_,DRec _ _ _ cs) <- ds', (s,_) <- cs, not (isGenerated s) ]
                unMod b ps           = if b then [(tag0 (dropMod c),y) | (c,y) <- ps] ++ ps else ps

-- Checking that public part is closed ---------------------------------------------------------
initEnvs bms         = do ims <- mapM (mkEnv . snd) bms
                          let (rs,xs,ss,rnL,rnT,rnE,ds,ws,bs,kds) 
                               = foldr mergeMod ([],[],[],[],[],[],Types [] [],[],Binds False [] [],[]) ims
                          return ((rs,rnL,ss),(rnL,rnT,rnE),(xs,ds,ws,bs),kds)

  where mergeMod (rs1,xs1,ss1,rnL1,rnT1,rnE1,ds1,ws1,bs1,kds1)
                 (rs2,xs2,ss2,rnL2,rnT2,rnE2,ds2,ws2,bs2,kds2) 
                                     = (rs1 ++ rs2, xs1 ++ xs2, ss1 ++ ss2, mergeRenamings2 rnL1 rnL2, 
                                       mergeRenamings2 rnT1 rnT2, mergeRenamings2 rnE1 rnE2,
                                       catDecls ds1 ds2, ws1++ws2, catBinds bs1 bs2,
                                       kds1 ++ kds2)

        mkEnv (unQual,IFace ns xs rs ss ds ws bs kds)
                                     = do ks  <- renaming (dom ke)
                                          ts  <- renaming (dom te'')
                                          ls' <- renaming ls -- (concatMap snd rs)
                                          return (unMod unQual rs, xs, unMod unQual ss, unMod unQual ls',unMod unQual ks,
                                                  unMod unQual ts,ds,ws,Binds r te' es,kds)
          where Types ke ds'         = ds
                Binds r te es        = bs
                te'                  = te ++ concatMap (tenvSelCon ke) ds'
                te''                 = te  ++ concatMap (tenvCon ke) ds'
                ls                   = [ s | (_,DRec _ _ _ cs) <- ds', (s,_) <- cs, not (isGenerated s) ]
                unMod b ps           = if b then [(tag0 (dropMod c),y) | (c,y) <- ps] ++ ps else ps

-- Checking that public part is closed ---------------------------------------------------------