[project @ 2000-04-17 11:39:56 by simonmar]
[ghc-hetmet.git] / ghc / interpreter / static.c
index 46af0ac..16fad26 100644 (file)
  * included in the distribution.
  *
  * $RCSfile: static.c,v $
- * $Revision: 1.24 $
- * $Date: 2000/03/06 08:38:04 $
+ * $Revision: 1.40 $
+ * $Date: 2000/04/07 10:00:28 $
  * ------------------------------------------------------------------------*/
 
-#include "prelude.h"
+#include "hugsbasictypes.h"
 #include "storage.h"
-#include "backend.h"
 #include "connect.h"
-#include "link.h"
 #include "errors.h"
-#include "subst.h"
 
 /* --------------------------------------------------------------------------
  * local function prototypes:
  * ------------------------------------------------------------------------*/
 
-static Void   local kindError           Args((Int,Constr,Constr,String,Kind,Int));
-static Void   local checkQualImport     Args((Pair));
-static Void   local checkUnqualImport   Args((Triple));
-
-static Name   local lookupName          Args((Text,List));
-static List   local checkSubentities    Args((List,List,List,String,Text));
-static List   local checkExportTycon    Args((List,Text,Cell,Tycon));
-static List   local checkExportClass    Args((List,Text,Cell,Class));
-static List   local checkExport         Args((List,Text,Cell));
-static List   local checkImportEntity   Args((List,Module,Cell));
-static List   local resolveImportList   Args((Module,Cell));
-static Void   local checkImportList     Args((Pair));
-
-static Void   local importEntity        Args((Module,Cell));
-static Void   local importName          Args((Module,Name));
-static Void   local importTycon         Args((Module,Tycon));
-static Void   local importClass         Args((Module,Class));
-static List   local checkExports        Args((List));
-
-static Void   local checkTyconDefn      Args((Tycon));
-static Void   local depConstrs          Args((Tycon,List,Cell));
-static List   local addSels             Args((Int,Name,List,List));
-static List   local selectCtxt          Args((List,List));
-static Void   local checkSynonyms       Args((List));
-static List   local visitSyn            Args((List,Tycon,List));
-static Type   local instantiateSyn      Args((Type,Type));
-
-static Void   local checkClassDefn      Args((Class));
-static Cell   local depPredExp         Args((Int,List,Cell));
-static Void   local checkMems           Args((Class,List,Cell));
-static Void   local checkMems2           Args((Class,Cell));
-static Void   local addMembers          Args((Class));
-static Name   local newMember           Args((Int,Int,Cell,Type,Class));
-       Name         newDSel             Args((Class,Int));
-static Text   local generateText        Args((String,Class));
-       Int          visitClass          Args((Class));
-
-static List   local classBindings       Args((String,Class,List));
-static Name   local memberName          Args((Class,Text));
-static List   local numInsert           Args((Int,Cell,List));
-
-static List   local maybeAppendVar      Args((Cell,List));
-
-static Type   local checkSigType        Args((Int,String,Cell,Type));
-static Void   local checkOptQuantVars  Args((Int,List,List));
-static Type   local depTopType          Args((Int,List,Type));
-static Type   local depCompType         Args((Int,List,Type));
-static Type   local depTypeExp          Args((Int,List,Type));
-static Type   local depTypeVar          Args((Int,List,Text));
-static List   local checkQuantVars      Args((Int,List,List,Cell));
-static List   local otvars             Args((Cell,List));
-static Bool   local osubset            Args((List,List));
-static Void   local kindConstr          Args((Int,Int,Int,Constr));
-static Kind   local kindAtom            Args((Int,Constr));
-static Void   local kindPred            Args((Int,Int,Int,Cell));
-static Void   local kindType            Args((Int,String,Type));
-static Void   local fixKinds            Args((Void));
-
-static Void   local kindTCGroup         Args((List));
-static Void   local initTCKind          Args((Cell));
-static Void   local kindTC              Args((Cell));
-static Void   local genTC               Args((Cell));
-
-static Void   local checkInstDefn       Args((Inst));
-static Void   local insertInst          Args((Inst));
-static Bool   local instCompare         Args((Inst,Inst));
-static Name   local newInstImp          Args((Inst));
-static Void   local kindInst            Args((Inst,Int));
-static Void   local checkDerive         Args((Tycon,List,List,Cell));
-static Void   local addDerInst          Args((Int,Class,List,List,Type,Int));
-static Void   local deriveContexts      Args((List));
-static Void   local initDerInst         Args((Inst));
-static Void   local calcInstPreds       Args((Inst));
-static Void   local maybeAddPred        Args((Cell,Int,Int,List));
-static List   local calcFunDeps                Args((List));
-static Cell   local copyAdj             Args((Cell,Int,Int));
-static Void   local tidyDerInst         Args((Inst));
-static List   local otvarsZonk         Args((Cell,List,Int));
-
-static Void   local addDerivImp         Args((Inst));
-
-static Void   local checkDefaultDefns   Args((Void));
-
-static Void   local checkForeignImport Args((Name));
-static Void   local checkForeignExport Args((Name));
-
-static Cell   local tidyInfix           Args((Int,Cell));
-static Pair   local attachFixity        Args((Int,Cell));
-static Syntax local lookupSyntax        Args((Text));
-
-static Cell   local checkPat            Args((Int,Cell));
-static Cell   local checkMaybeCnkPat    Args((Int,Cell));
-static Cell   local checkApPat          Args((Int,Int,Cell));
-static Void   local addToPatVars        Args((Int,Cell));
-static Name   local conDefined          Args((Int,Cell));
-static Void   local checkIsCfun         Args((Int,Name));
-static Void   local checkCfunArgs       Args((Int,Cell,Int));
-static Cell   local checkPatType        Args((Int,String,Cell,Type));
-static Cell   local applyBtyvs          Args((Cell));
-static Cell   local bindPat             Args((Int,Cell));
-static Void   local bindPats            Args((Int,List));
-
-static List   local extractSigdecls     Args((List));
-static List   local extractFixdecls     Args((List));
-static List   local extractBindings     Args((List));
-static List   local getPatVars          Args((Int,Cell,List));
-static List   local addPatVar           Args((Int,Cell,List));
-static List   local eqnsToBindings      Args((List,List,List,List));
-static Void   local notDefined          Args((Int,List,Cell));
-static Cell   local findBinding         Args((Text,List));
-static Cell   local getAttr             Args((List,Cell));
-static Void   local addSigdecl          Args((List,Cell));
-static Void   local addFixdecl          Args((List,List,List,List,Triple));
-static Void   local dupFixity           Args((Int,Text));
-static Void   local missFixity          Args((Int,Text));
-
-static List   local dependencyAnal      Args((List));
-static List   local topDependAnal       Args((List));
-static Void   local addDepField         Args((Cell));
-static Void   local remDepField         Args((List));
-static Void   local remDepField1        Args((Cell));
-static Void   local clearScope          Args((Void));
-static Void   local withinScope         Args((List));
-static Void   local leaveScope          Args((Void));
-static Void   local saveSyntax          Args((Cell,Cell));
-
-static Void   local depBinding          Args((Cell));
-static Void   local depDefaults         Args((Class));
-static Void   local depInsts            Args((Inst));
-static Void   local depClassBindings    Args((List));
-static Void   local depAlt              Args((Cell));
-static Void   local depRhs              Args((Cell));
-static Void   local depGuard            Args((Cell));
-static Cell   local depExpr             Args((Int,Cell));
-static Void   local depPair             Args((Int,Cell));
-static Void   local depTriple           Args((Int,Cell));
-static Void   local depComp             Args((Int,Cell,List));
-static Void   local depCaseAlt          Args((Int,Cell));
-static Cell   local depVar              Args((Int,Cell));
-static Cell   local depQVar             Args((Int,Cell));
-static Void   local depConFlds          Args((Int,Cell,Bool));
-static Void   local depUpdFlds          Args((Int,Cell));
-static List   local depFields           Args((Int,Cell,List,Bool));
+static Void   local kindError           ( Int,Constr,Constr,String,Kind,Int );
+static Void   local checkQualImport     ( Pair );
+static Void   local checkUnqualImport   ( Triple );
+
+static Name   local lookupName          ( Text,List );
+static List   local checkSubentities    ( List,List,List,String,Text );
+static List   local checkExportTycon    ( List,Text,Cell,Tycon );
+static List   local checkExportClass    ( List,Text,Cell,Class );
+static List   local checkExport         ( List,Text,Cell );
+static List   local checkImportEntity   ( List,Module,Cell );
+static List   local resolveImportList   ( Module,Cell );
+static Void   local checkImportList     ( Pair );
+
+static Void   local importEntity        ( Module,Cell );
+static Void   local importName          ( Module,Name );
+static Void   local importTycon         ( Module,Tycon );
+static Void   local importClass         ( Module,Class );
+static List   local checkExports        ( List, Module );
+
+static Void   local checkTyconDefn      ( Tycon );
+static Void   local depConstrs          ( Tycon,List,Cell );
+static List   local addSels             ( Int,Name,List,List );
+static List   local selectCtxt          ( List,List );
+static Void   local checkSynonyms       ( List );
+static List   local visitSyn            ( List,Tycon,List );
+static Type   local instantiateSyn      ( Type,Type );
+
+static Void   local checkClassDefn      ( Class );
+static Cell   local depPredExp         ( Int,List,Cell );
+static Void   local checkMems           ( Class,List,Cell );
+static Void   local checkMems2          ( Class,Cell );
+static Void   local addMembers          ( Class );
+static Name   local newMember           ( Int,Int,Cell,Type,Class );
+static Text   local generateText        ( String,Class );
+
+static List   local classBindings       ( String,Class,List );
+static Name   local memberName          ( Class,Text );
+static List   local numInsert           ( Int,Cell,List );
+
+static List   local maybeAppendVar      ( Cell,List );
+
+static Type   local checkSigType        ( Int,String,Cell,Type );
+static Void   local checkOptQuantVars  ( Int,List,List );
+static Type   local depTopType          ( Int,List,Type );
+static Type   local depCompType         ( Int,List,Type );
+static Type   local depTypeExp          ( Int,List,Type );
+static Type   local depTypeVar          ( Int,List,Text );
+static List   local checkQuantVars      ( Int,List,List,Cell );
+static List   local otvars             ( Cell,List );
+static Bool   local osubset            ( List,List );
+static Void   local kindConstr          ( Int,Int,Int,Constr );
+static Kind   local kindAtom            ( Int,Constr );
+static Void   local kindPred            ( Int,Int,Int,Cell );
+static Void   local kindType            ( Int,String,Type );
+static Void   local fixKinds            ( Void );
+
+static Void   local kindTCGroup         ( List );
+static Void   local initTCKind          ( Cell );
+static Void   local kindTC              ( Cell );
+static Void   local genTC               ( Cell );
+
+static Void   local checkInstDefn       ( Inst );
+static Void   local insertInst          ( Inst );
+static Bool   local instCompare         ( Inst,Inst );
+static Name   local newInstImp          ( Inst );
+static Void   local kindInst            ( Inst,Int );
+static Void   local checkDerive         ( Tycon,List,List,Cell );
+static Void   local addDerInst          ( Int,Class,List,List,Type,Int );
+static Void   local deriveContexts      ( List );
+static Void   local initDerInst         ( Inst );
+static Void   local calcInstPreds       ( Inst );
+static Void   local maybeAddPred        ( Cell,Int,Int,List );
+static List   local calcFunDeps                ( List );
+static Cell   local copyAdj             ( Cell,Int,Int );
+static Void   local tidyDerInst         ( Inst );
+static List   local otvarsZonk         ( Cell,List,Int );
+
+static Void   local addDerivImp         ( Inst );
+
+static Void   local checkDefaultDefns   ( Void );
+
+static Void   local checkForeignImport  ( Name );
+static Void   local checkForeignExport  ( Name );
+
+static Cell   local tidyInfix           ( Int,Cell );
+static Pair   local attachFixity        ( Int,Cell );
+static Syntax local lookupSyntax        ( Text );
+
+static Cell   local checkPat            ( Int,Cell );
+static Cell   local checkMaybeCnkPat    ( Int,Cell );
+static Cell   local checkApPat          ( Int,Int,Cell );
+static Void   local addToPatVars        ( Int,Cell );
+static Name   local conDefined          ( Int,Cell );
+static Void   local checkIsCfun         ( Int,Name );
+static Void   local checkCfunArgs       ( Int,Cell,Int );
+static Cell   local checkPatType        ( Int,String,Cell,Type );
+static Cell   local applyBtyvs          ( Cell );
+static Cell   local bindPat             ( Int,Cell );
+static Void   local bindPats            ( Int,List );
+
+static List   local extractSigdecls     ( List );
+static List   local extractFixdecls     ( List );
+static List   local extractBindings     ( List );
+static List   local getPatVars          ( Int,Cell,List );
+static List   local addPatVar           ( Int,Cell,List );
+static List   local eqnsToBindings      ( List,List,List,List );
+static Void   local notDefined          ( Int,List,Cell );
+static Cell   local findBinding         ( Text,List );
+static Cell   local getAttr             ( List,Cell );
+static Void   local addSigdecl          ( List,Cell );
+static Void   local addFixdecl          ( List,List,List,List,Triple );
+static Void   local dupFixity           ( Int,Text );
+static Void   local missFixity          ( Int,Text );
+
+static List   local dependencyAnal      ( List );
+static List   local topDependAnal       ( List );
+static Void   local addDepField         ( Cell );
+static Void   local remDepField         ( List );
+static Void   local remDepField1        ( Cell );
+static Void   local clearScope          ( Void );
+static Void   local withinScope         ( List );
+static Void   local leaveScope          ( Void );
+static Void   local saveSyntax          ( Cell,Cell );
+
+static Void   local depBinding          ( Cell );
+static Void   local depDefaults         ( Class );
+static Void   local depInsts            ( Inst );
+static Void   local depClassBindings    ( List );
+static Void   local depAlt              ( Cell );
+static Void   local depRhs              ( Cell );
+static Void   local depGuard            ( Cell );
+static Cell   local depExpr             ( Int,Cell );
+static Void   local depPair             ( Int,Cell );
+static Void   local depTriple           ( Int,Cell );
+static Void   local depComp             ( Int,Cell,List );
+static Void   local depCaseAlt          ( Int,Cell );
+static Cell   local depVar              ( Int,Cell );
+static Cell   local depQVar             ( Int,Cell );
+static Void   local depConFlds          ( Int,Cell,Bool );
+static Void   local depUpdFlds          ( Int,Cell );
+static List   local depFields           ( Int,Cell,List,Bool );
 #if IPARAM
-static Void   local depWith            Args((Int,Cell));
-static List   local depDwFlds          Args((Int,Cell,List));
+static Void   local depWith            ( Int,Cell );
+static List   local depDwFlds          ( Int,Cell,List );
 #endif
 #if TREX
-static Cell   local depRecord           Args((Int,Cell));
+static Cell   local depRecord           ( Int,Cell );
 #endif
 
-static List   local tcscc               Args((List,List));
-static List   local bscc                Args((List));
+static List   local tcscc               ( List,List );
+static List   local bscc                ( List );
 
-static Void   local addRSsigdecls       Args((Pair));
-static Void   local allNoPrevDef        Args((Cell));
-static Void   local noPrevDef           Args((Int,Cell));
-static Bool   local odiff              Args((List,List));
+static Void   local addRSsigdecls       ( Pair );
+static Void   local allNoPrevDef        ( Cell );
+static Void   local noPrevDef           ( Int,Cell );
+static Bool   local odiff              ( List,List );
  
-static Void   local duplicateErrorAux   Args((Int,Module,Text,String));
+static Void   local duplicateErrorAux   ( Int,Module,Text,String );
 #define duplicateError(l,m,t,k) duplicateErrorAux(l,m,t,k)
-static Void   local checkTypeIn         Args((Pair));
+static Void   local checkTypeIn         ( Pair );
 
 /* --------------------------------------------------------------------------
  * The code in this file is arranged in roughly the following order:
@@ -260,24 +255,9 @@ Kind   extKind;                         /* Kind of extension, *->row->row  */
  * Static analysis of modules:
  * ------------------------------------------------------------------------*/
 
-#if HSCRIPT
-String reloadModule;
-#endif
-
-Void startModule(nm)                             /* switch to a new module */
-Cell nm; {
-    Module m;
-    if (!isCon(nm)) internal("startModule");
-    if (isNull(m = findModule(textOf(nm))))
-        m = newModule(textOf(nm));
-    else if (!isPreludeScript()) {
-        /* You're allowed to break the rules in the Prelude! */
-#if HSCRIPT
-        reloadModule = textToStr(textOf(nm));
-#endif
-        ERRMSG(0) "Module \"%s\" already loaded", textToStr(textOf(nm))
-        EEND;
-    }
+Void startModule ( Module m )                    /* switch to a new module */
+{
+    if (isNull(m)) internal("startModule");
     setCurrModule(m);
 }
 
@@ -357,12 +337,18 @@ Text   textParent; {
 static List local checkImportEntity(imports,exporter,entity)
 List   imports; /* Accumulated list of things to import */
 Module exporter;
-Cell   entity; { /* Entry from import list */
+Cell entity; { /* Entry from import list */
     List oldImports = imports;
     Text t  = isIdent(entity) ? textOf(entity) : textOf(fst(entity));
-    List es = module(exporter).exports; 
+    List es = NIL;
+    es = module(exporter).exports; 
+
     for(; nonNull(es); es=tl(es)) {
-        Cell e = hd(es); /* :: Entity | (Entity, NIL|DOTDOT) */
+        Cell e = hd(es); /* :: Entity
+                            | (Entity, NIL|DOTDOT)
+                            | tycon 
+                            | class
+                         */
         if (isPair(e)) {
             Cell f = fst(e);
             if (isTycon(f)) {
@@ -373,10 +359,11 @@ Cell   entity; { /* Entry from import list */
                         case NEWTYPE:
                         case DATATYPE:
                             if (DOTDOT == snd(entity)) {
-                                imports=dupOnto(tycon(f).defn,imports);
+                                imports = dupOnto(tycon(f).defn,imports);
                             } else {
-                                imports=checkSubentities(imports,snd(entity),tycon(f).defn,
-                                                         "constructor of type",t);
+                                imports = checkSubentities(
+                                             imports,snd(entity),tycon(f).defn,
+                                             "constructor of type",t);
                             }
                             break;
                         default:;
@@ -391,8 +378,9 @@ Cell   entity; { /* Entry from import list */
                         if (DOTDOT == snd(entity)) {
                             return dupOnto(cclass(f).members,imports);
                         } else {
-                            return checkSubentities(imports,snd(entity),cclass(f).members,
-                                   "member of class",t);
+                            return checkSubentities(
+                                      imports,snd(entity),cclass(f).members,
+                                      "member of class",t);
                         }
                     }
                 }
@@ -418,7 +406,7 @@ Cell   entity; { /* Entry from import list */
 
 static List local resolveImportList(m,impList)
 Module m;  /* exporting module */
-Cell   impList; {
+Cell impList; {
     List imports = NIL;
     if (DOTDOT == impList) {
         List es = module(m).exports;
@@ -455,11 +443,6 @@ Pair importSpec; {
     List   imports = NIL; /* entities we want to import */
     List   hidden  = NIL; /* entities we want to hide   */
 
-    if (moduleThisScript(m)) { 
-        ERRMSG(0) "Module \"%s\" recursively imports itself",
-                  textToStr(module(m).text)
-        EEND;
-    }
     if (isPair(impList) && HIDDEN == fst(impList)) {
         /* Somewhat inefficient - but obviously correct:
          * imports = importsOf("module Foo") `setDifference` hidden;
@@ -500,7 +483,8 @@ Module source;
 Name n; {
     Name clash = addName(n);
     if (nonNull(clash) && clash!=n) {
-        ERRMSG(0) "Entity \"%s\" imported from module \"%s\" already defined in module \"%s\"",
+        ERRMSG(0) "Entity \"%s\" imported from module \"%s\""
+                  " already defined in module \"%s\"",
                   textToStr(name(n).text), 
                   textToStr(module(source).text),
                   textToStr(module(name(clash).mod).text)
@@ -681,9 +665,9 @@ Cell e; {
     return exports; /* NOTUSED */
 }
 
-static List local checkExports(exports)
-List exports; {
-    Module m  = lastModule();
+static List local checkExports ( List exports, Module thisModule )
+{
+    Module m  = thisModule;
     Text   mt = module(m).text;
     List   es = NIL;
 
@@ -1020,6 +1004,7 @@ Cell  cd; {                             /* definitions (w or w/o deriving) */
             name(n).defn = nameId;
         } else {
             implementCfun(n,scs);
+            name(n).hasStrict = nonNull(scs);
         }
 
         hd(cs) = n;
@@ -1861,7 +1846,7 @@ Type   type; {
     }
 
     if (nonNull(tvs)) {
-       if (length(tvs)>=NUM_OFFSETS) {
+       if (length(tvs) >= (OFF_MAX-OFF_MIN+1)) {
             ERRMSG(line) "Too many type variables in %s\n", where
             EEND;
         } else {
@@ -3144,7 +3129,7 @@ Int  beta; {
                               return copyAdj(tyv->bound,tyv->offs,beta);
                           }
                           vn -= beta;
-                          if (vn<0 || vn>=NUM_OFFSETS) {
+                          if (vn<0 || vn>=(OFF_MAX-OFF_MIN+1)) {
                               internal("copyAdj");
                           }
                           return mkOffset(vn);
@@ -3278,15 +3263,14 @@ Cell intName;
 Cell type; {
     Text t = textOf(intName);
     Name n = findName(t);
-    Int  l = intOf(line);
 
     if (isNull(n)) {
         n = newName(t,NIL);
     } else if (name(n).defn!=PREDEFINED) {
-        ERRMSG(l) "Redeclaration of foreign \"%s\"", textToStr(t)
+        ERRMSG(line) "Redeclaration of foreign \"%s\"", textToStr(t)
         EEND;
     }
-    name(n).line     = l;
+    name(n).line     = line;
     name(n).defn     = extName;
     name(n).type     = type;
     name(n).callconv = callconv;
@@ -3316,15 +3300,14 @@ Cell intName;
 Cell type; {
     Text t = textOf(intName);
     Name n = findName(t);
-    Int  l = intOf(line);
 
     if (isNull(n)) {
         n = newName(t,NIL);
     } else if (name(n).defn!=PREDEFINED) {
-        ERRMSG(l) "Redeclaration of foreign \"%s\"", textToStr(t)
+        ERRMSG(line) "Redeclaration of foreign \"%s\"", textToStr(t)
         EEND;
     }
-    name(n).line     = l;
+    name(n).line     = line;
     name(n).defn     = NIL;  /* nothing to say */
     name(n).type     = type;
     name(n).callconv = callconv;
@@ -3416,7 +3399,6 @@ Cell p; {
 static Cell local checkMaybeCnkPat(l,p)/* Check applicative pattern with   */
 Int  l;                                /* the possibility of n+k pattern   */
 Cell p; {
-#if NPLUSK
     Cell h = getHead(p);
 
     if (argCount==2 && isVar(h) && textOf(h)==textPlus) {       /* n+k     */
@@ -3434,7 +3416,6 @@ Cell p; {
         arg(p)           = checkPat(l,v);
         return p;
     }
-#endif
     return checkApPat(l,0,p);
 }
 
@@ -4503,7 +4484,7 @@ Cell g; {                              /* expression                       */
 static Cell local depExpr(line,e)      /* find dependents of expression    */
 Int  line;
 Cell e; {
-  //    Printf( "\n\n"); print(e,100); Printf("\n");
+  //Printf( "\n\n"); print(e,100); Printf("\n");
   //printExp(stdout,e);
     switch (whatIs(e)) {
 
@@ -4710,9 +4691,6 @@ Cell e; {
         EEND;
     }
 
-    if (!moduleThisScript(name(n).mod)) {
-        return n;
-    }
     /* Later phases of the system cannot cope if we resolve references
      * to unprocessed objects too early.  This is the main reason that
      * we cannot cope with recursive modules at the moment.
@@ -5013,8 +4991,9 @@ Void checkContext(void) {         /* Top level static check on Expr  */
 }
 #endif
 
-Void checkDefns() {                     /* Top level static analysis       */
-    Module thisModule = lastModule();
+Void checkDefns ( Module thisModule ) { /* Top level static analysis       */
+    Text modName = module(thisModule).text;
+
     staticAnalysis(RESET);
 
     setCurrModule(thisModule);
@@ -5023,16 +5002,17 @@ Void checkDefns() {                     /* Top level static analysis       */
     mapProc(checkQualImport,  module(thisModule).qualImports);
     mapProc(checkUnqualImport,unqualImports);
     /* Add "import Prelude" if there`s no explicit import */
-    if (thisModule!=modulePrelude
-        && isNull(cellAssoc(modulePrelude,unqualImports))
-        && isNull(cellRevAssoc(modulePrelude,module(thisModule).qualImports))) {
-        unqualImports = cons(pair(modulePrelude,DOTDOT),unqualImports);
+    if (modName == textPrelPrim || modName == textPrelude) {
+      /* Nothing. */
+    } else if (isNull(cellAssoc(modulePrelude,unqualImports))
+              && isNull(cellRevAssoc(modulePrelude,module(thisModule).qualImports))) {
+      unqualImports = cons(pair(modulePrelude,DOTDOT),unqualImports);
     } else {
-        /* Every module (including the Prelude) implicitly contains 
-         * "import qualified Prelude" 
-         */
-        module(thisModule).qualImports=cons(pair(mkCon(textPrelude),modulePrelude),
-                                            module(thisModule).qualImports);
+      /* Every module implicitly contains "import qualified Prelude" 
+       */
+      module(thisModule).qualImports
+       =cons(pair(mkCon(textPrelude),modulePrelude),
+             module(thisModule).qualImports);
     }
     mapProc(checkImportList, unqualImports);
 
@@ -5070,7 +5050,7 @@ Void checkDefns() {                     /* Top level static analysis       */
 
     mapProc(allNoPrevDef,valDefns);     /* check against previous defns    */
 
-    if (!combined) linkPreludeNames();  /* link names in Prelude           */
+    if (!combined) linkPrimNames();     /* link primitive names           */
 
     mapProc(checkForeignImport,foreignImports); /* check foreign imports   */
     mapProc(checkForeignExport,foreignExports); /* check foreign exports   */
@@ -5081,7 +5061,8 @@ Void checkDefns() {                     /* Top level static analysis       */
     /* export list.  Note that this has to happen before dependency        */
     /* analysis so that references to Prelude.foo will be resolved         */
     /* when compiling the prelude.                                         */
-    module(thisModule).exports = checkExports(module(thisModule).exports);
+    module(thisModule).exports 
+       = checkExports ( module(thisModule).exports, thisModule );
 
     mapProc(checkTypeIn,typeInDefns);   /* check restricted synonym defns  */