[project @ 2000-03-22 18:14:22 by sewardj]
[ghc-hetmet.git] / ghc / interpreter / static.c
index 47a91b9..25896a0 100644 (file)
  * included in the distribution.
  *
  * $RCSfile: static.c,v $
- * $Revision: 1.10 $
- * $Date: 1999/10/15 21:40:55 $
+ * $Revision: 1.31 $
+ * $Date: 2000/03/22 18:14:23 $
  * ------------------------------------------------------------------------*/
 
 #include "prelude.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 Void   local depPredExp          Args((Int,List,Cell));
-static Void   local checkMems           Args((Class,List,Cell));
-static Void   local addMembers          Args((Class));
-static Name   local newMember           Args((Int,Int,Cell,Type,Class));
-static Name   local newDSel             Args((Class,Int));
-static Name   local newDBuild           Args((Class));
-static Text   local generateText        Args((String,Class));
-static Int    local 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 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 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 Cell   local copyAdj             Args((Cell,Int,Int));
-static Void   local tidyDerInst         Args((Inst));
-
-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,Bool,Cell );
+static List   local resolveImportList   ( Module,Cell,Bool );
+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            ( 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 Void   local duplicateErrorAux   Args((Int,Module,Text,String));
+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   ( 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:
@@ -249,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);
 }
 
@@ -343,15 +334,28 @@ Text   textParent; {
     return imports;
 }
 
-static List local checkImportEntity(imports,exporter,entity)
+static List local checkImportEntity(imports,exporter,priv,entity)
 List   imports; /* Accumulated list of things to import */
 Module exporter;
-Cell   entity; { /* Entry from import list */
+Bool priv;
+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;
+    if (priv) {
+      es = module(exporter).names;
+      es = dupOnto(module(exporter).tycons,es);
+      es = dupOnto(module(exporter).classes,es);
+    } else {
+      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)) {
@@ -362,10 +366,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:;
@@ -380,8 +385,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);
                         }
                     }
                 }
@@ -392,6 +398,18 @@ Cell   entity; { /* Entry from import list */
             if (isIdent(entity) && name(e).text == t) {
                 imports = cons(e,imports);
             }
+        } else if (isTycon(e) && priv) {
+           if (tycon(e).text == t) {
+               imports = cons(e,imports);
+               return dupOnto(tycon(e).defn,imports);
+           }
+        } else if (isClass(e) && priv) {
+           if (cclass(e).text == t) {
+               imports = cons(e,imports);
+               return dupOnto(cclass(e).members,imports);
+           }
+        } else if (whatIs(e) == TUPLE && priv) {
+         // do nothing
         } else {
             internal("checkImportEntity3");
         }
@@ -405,9 +423,10 @@ Cell   entity; { /* Entry from import list */
     return imports;
 }
 
-static List local resolveImportList(m,impList)
+static List local resolveImportList(m,impList,priv)
 Module m;  /* exporting module */
-Cell   impList; {
+Cell impList; 
+Bool priv; {
     List imports = NIL;
     if (DOTDOT == impList) {
         List es = module(m).exports;
@@ -431,7 +450,7 @@ Cell   impList; {
             }
         }
     } else {
-        map1Accum(checkImportEntity,imports,m,impList);
+        map2Accum(checkImportEntity,imports,m,priv,impList);
     }
     return imports;
 }
@@ -444,20 +463,21 @@ 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;
          */
-        hidden  = resolveImportList(m, snd(impList));
-        imports = resolveImportList(m, DOTDOT);
+        hidden  = resolveImportList(m, snd(impList),FALSE);
+        imports = resolveImportList(m, DOTDOT,FALSE);
+    } else if (isPair(impList) && STAR == fst(impList)) {
+      List privileged;
+      imports = resolveImportList(m, DOTDOT, FALSE);
+      privileged = resolveImportList(m, snd(impList),TRUE);
+      imports = dupOnto(privileged,imports);
     } else {
-        imports = resolveImportList(m, impList);
+        imports = resolveImportList(m, impList,FALSE);
     }
+
     for(; nonNull(imports); imports=tl(imports)) {
         Cell e = hd(imports);
         if (!cellIsMember(e,hidden))
@@ -474,6 +494,7 @@ Cell e; {
     switch (whatIs(e)) {
       case NAME  : importName(source,e); 
                    break;
+      case TUPLE:
       case TYCON : importTycon(source,e); 
                    break;
       case CLASS : importClass(source,e);
@@ -487,7 +508,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)
@@ -665,12 +687,12 @@ Cell e; {
             EEND;
         }
     }
-    return 0; /* NOTREACHED */
+    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;
 
@@ -837,10 +859,10 @@ Cell  cd; {                             /* definitions (w or w/o deriving) */
     for (i=0; i<tycon(t).arity; ++i)    /* build representation for tycon  */
         lhs = ap(lhs,mkOffset(i));      /* applied to full comp. of args   */
 
-    if (whatIs(cs)==QUAL) {             /* allow for possible context      */
+    if (isQualType(cs)) {              /* allow for possible context      */
         ctxt = fst(snd(cs));
         cs   = snd(snd(cs));
-        map2Proc(depPredExp,line,tyvars,ctxt);
+       map2Over(depPredExp,line,tyvars,ctxt);
         h98CheckCtxt(line,"context",TRUE,ctxt,NIL);
     }
 
@@ -866,17 +888,17 @@ Cell  cd; {                             /* definitions (w or w/o deriving) */
             sig = checkQuantVars(line,evs,sig,con);
         }
 
-        if (whatIs(con)==QUAL) {        /* Local predicates                */
+       if (isQualType(con)) {          /* Local predicates                */
             List us;
             lps     = fst(snd(con));
-            for (us = typeVarsIn(lps,NIL,NIL); nonNull(us); us=tl(us))
+           for (us = typeVarsIn(lps,NIL,NIL,NIL); nonNull(us); us=tl(us))
                 if (!varIsMember(textOf(hd(us)),evs)) {
                     ERRMSG(line)
                         "Variable \"%s\" in constraint is not locally bound",
                         textToStr(textOf(hd(us)))
                     EEND;
                 }
-            map2Proc(depPredExp,line,sig,lps);
+           map2Over(depPredExp,line,sig,lps);
             con     = snd(snd(con));
             arity   = length(lps);
         }
@@ -927,10 +949,10 @@ Cell  cd; {                             /* definitions (w or w/o deriving) */
             Type ty  = fun(con);
             Type cmp = arg(con);
             fun(con) = typeArrow;
-            if (isPolyType(cmp)) {
+           if (isPolyOrQualType(cmp)) {
                 if (nonNull(derivs)) {
                     ERRMSG(line) "Cannot derive instances for types" ETHEN
-                    ERRTEXT      " with polymorphic components"
+                   ERRTEXT      " with polymorphic or qualified components"
                     EEND;
                 }
                 if (nr2==0)
@@ -942,8 +964,9 @@ Cell  cd; {                             /* definitions (w or w/o deriving) */
             con      = ty;
         }
 
-        if (nr2>0)                      /* Add rank 2 annotation           */
-            type = ap(RANK2,pair(mkInt(nr2),type));
+       if (nr2>0) {                    /* Add rank 2 annotation           */
+           type = ap(RANK2,pair(mkInt(nr2-length(lps)),type));
+       }
 
         if (nonNull(evs)) {             /* Add existential annotation      */
             if (nonNull(derivs)) {
@@ -1202,32 +1225,35 @@ Type env; {                             /* values for OFFSET type vars     */
  *   stages of static analysis.
  * ------------------------------------------------------------------------*/
 
-Void classDefn(line,head,ms)           /* process new class definition     */
-Int  line;                             /* definition line number           */
-Cell head;                             /* class header :: ([Supers],Class) */
-List ms; {                             /* class definition body            */
+Void classDefn(line,head,ms,fds)       /* process new class definition    */
+Int  line;                            /* definition line number           */
+Cell head;                            /* class header :: ([Supers],Class) */
+List ms;                              /* class definition body            */
+List fds; {                           /* functional dependencies          */
     Text ct    = textOf(getHead(snd(head)));
     Int  arity = argCount;
 
     if (nonNull(findClass(ct))) {
-        ERRMSG(line) "Repeated definition of class \"%s\"",
-                     textToStr(ct)
-        EEND;
+       ERRMSG(line) "Repeated definition of class \"%s\"",
+                    textToStr(ct)
+       EEND;
     } else if (nonNull(findTycon(ct))) {
-        ERRMSG(line) "\"%s\" used as both class and type constructor",
-                     textToStr(ct)
-        EEND;
+       ERRMSG(line) "\"%s\" used as both class and type constructor",
+                    textToStr(ct)
+       EEND;
     } else {
-        Class nw           = newClass(ct);
-        cclass(nw).line    = line;
-        cclass(nw).arity   = arity;
-        cclass(nw).head    = snd(head);
-        cclass(nw).supers  = fst(head);
-        cclass(nw).members = ms;
-        cclass(nw).level   = 0;
-        classDefns         = cons(nw,classDefns);
-        if (arity!=1)
-            h98DoesntSupport(line,"multiple parameter classes");
+       Class nw           = newClass(ct);
+       cclass(nw).line    = line;
+       cclass(nw).arity   = arity;
+       cclass(nw).head    = snd(head);
+       cclass(nw).supers  = fst(head);
+       cclass(nw).members = ms;
+       cclass(nw).level   = 0;
+       cclass(nw).fds     = fds;
+       cclass(nw).xfds    = NIL;
+       classDefns         = cons(nw,classDefns);
+       if (arity!=1)
+           h98DoesntSupport(line,"multiple parameter classes");
     }
 }
 
@@ -1277,14 +1303,62 @@ Class c; {
         tyvars = cons(arg(temp),tyvars);
     }
 
-    for (temp=cclass(c).head; args>0; temp=fun(temp), args--) {
-        arg(temp) = mkOffset(args);
+    for (fs=cclass(c).fds; nonNull(fs); fs=tl(fs)) {
+       Pair fd = hd(fs);
+       List vs = snd(fd);
+
+       /* Check for trivial dependency
+        */
+       if (isNull(vs)) {
+           ERRMSG(cclass(c).line) "Functional dependency is trivial"
+           EEND;
+       }
+
+       /* Check for duplicated vars on right hand side, and for vars on
+        * right that also appear on the left:
+        */
+       for (vs=snd(fd); nonNull(vs); vs=tl(vs)) {
+           if (varIsMember(textOf(hd(vs)),fst(fd))) {
+               ERRMSG(cclass(c).line)
+                   "Trivial dependency for variable \"%s\"",
+                   textToStr(textOf(hd(vs)))
+               EEND;
+           }
+           if (varIsMember(textOf(hd(vs)),tl(vs))) {
+               ERRMSG(cclass(c).line)
+                   "Repeated variable \"%s\" in functional dependency",
+                   textToStr(textOf(hd(vs)))
+               EEND;
+           }
+           hd(vs) = depTypeVar(cclass(c).line,tyvars,textOf(hd(vs)));
+       }
+
+       /* Check for duplicated vars on left hand side:
+        */
+       for (vs=fst(fd); nonNull(vs); vs=tl(vs)) {
+           if (varIsMember(textOf(hd(vs)),tl(vs))) {
+               ERRMSG(cclass(c).line)
+                   "Repeated variable \"%s\" in functional dependency",
+                   textToStr(textOf(hd(vs)))
+               EEND;
+           }
+           hd(vs) = depTypeVar(cclass(c).line,tyvars,textOf(hd(vs)));
+       }
+    }
+
+    if (cclass(c).arity==0) {
+       cclass(c).head = c;
+    } else {
+       Int args = cclass(c).arity - 1;
+       for (temp=cclass(c).head; args>0; temp=fun(temp), args--) {
+           arg(temp) = mkOffset(args);
+       }
+       arg(temp) = mkOffset(0);
+       fun(temp) = c;
     }
-    arg(temp) = mkOffset(0);
-    fun(temp) = c;
 
-    tcDeps              = NIL;          /* find dependents                 */
-    map2Proc(depPredExp,cclass(c).line,tyvars,cclass(c).supers);
+    tcDeps             = NIL;          /* find dependents                 */
+    map2Over(depPredExp,cclass(c).line,tyvars,cclass(c).supers);
     h98CheckCtxt(cclass(c).line,"class definition",FALSE,cclass(c).supers,NIL);
     cclass(c).numSupers = length(cclass(c).supers);
     cclass(c).defaults  = extractBindings(cclass(c).members);   /* defaults*/
@@ -1297,20 +1371,96 @@ Class c; {
     tcDeps              = NIL;
 }
 
-static Void local depPredExp(line,tyvars,pred)
+
+/* --------------------------------------------------------------------------
+ * Functional dependencies are inherited from superclasses.
+ * For example, if I've got the following classes:
+ *
+ * class C a b | a -> b
+ * class C [b] a => D a b
+ *
+ * then C will have the dependency ([a], [b]) as expected, and D will inherit
+ * the dependency ([b], [a]) from C.
+ * When doing pairwise improvement, we have to consider not just improving
+ * when we see a pair of Cs or a pair of Ds in the context, but when we've
+ * got a C and a D as well.  In this case, we only improve when the
+ * predicate in question matches the type skeleton in the relevant superclass
+ * constraint.  E.g., we improve the pair (C [Int] a, D b Int) (unifying
+ * a and b), but we don't improve the pair (C Int a, D b Int).
+ * To implement functional dependency inheritance, we calculate
+ * the closure of all functional dependencies, and store the result
+ * in an additional field `xfds' (extended functional dependencies).
+ * The `xfds' field is a list of functional dependency lists, annotated
+ * with a list of predicate skeletons constraining when improvement can
+ * happen against this dependency list.  For example, the xfds field
+ * for C above would be:
+ *     [([C a b], [([a], [b])])]
+ * and the xfds field for D would be:
+ *     [([C [b] a, D a b], [([b], [a])])]
+ * Self-improvement (of a C with a C, or a D with a D) is treated as a
+ * special case of an inherited dependency.
+ * ------------------------------------------------------------------------*/
+static List local inheritFundeps ( Class c, Cell pi, Int o )
+{
+    Int alpha = newKindedVars(cclass(c).kinds);
+    List scs = cclass(c).supers;
+    List xfds = NIL;
+    Cell this = NIL;
+    /* better not fail ;-) */
+    if (!matchPred(pi,o,cclass(c).head,alpha))
+       internal("inheritFundeps - predicate failed to match it's own head!");
+    this = copyPred(pi,o);
+    for (; nonNull(scs); scs=tl(scs)) {
+       Class s = getHead(hd(scs));
+       if (isClass(s)) {
+           List sfds = inheritFundeps(s,hd(scs),alpha);
+           for (; nonNull(sfds); sfds=tl(sfds)) {
+               Cell h = hd(sfds);
+               xfds = cons(pair(cons(this,fst(h)),snd(h)),xfds);
+           }
+       }
+    }
+    if (nonNull(cclass(c).fds)) {
+       List fds = NIL, fs = cclass(c).fds;
+       for (; nonNull(fs); fs=tl(fs)) {
+           fds = cons(pair(otvars(this,fst(hd(fs))),
+                           otvars(this,snd(hd(fs)))),fds);
+       }
+       xfds = cons(pair(cons(this,NIL),fds),xfds);
+    }
+    return xfds;
+}
+
+static Void local extendFundeps ( Class c )
+{ 
+    Int alpha;
+    emptySubstitution();
+    alpha = newKindedVars(cclass(c).kinds);
+    cclass(c).xfds = inheritFundeps(c,cclass(c).head,alpha);
+
+    /* we can now check for ambiguity */
+    map1Proc(checkMems2,c,fst(cclass(c).members));
+}
+
+
+static Cell local depPredExp(line,tyvars,pred)
 Int  line;
 List tyvars;
 Cell pred; {
-    Int  args = 1;                      /* parser guarantees >=1 args      */
-    Cell h    = fun(pred);
+    Int  args = 0;
+    Cell prev = NIL;
+    Cell h    = pred;
     for (; isAp(h); args++) {
-        arg(pred) = depTypeExp(line,tyvars,arg(pred));
-        pred      = h;
-        h         = fun(pred);
+       arg(h) = depTypeExp(line,tyvars,arg(h));
+       prev   = h;
+       h      = fun(h);
+    }
+
+    if (args==0) {
+       h98DoesntSupport(line,"tag classes");
+    } else if (args!=1) {
+       h98DoesntSupport(line,"multiple parameter classes");
     }
-    arg(pred) = depTypeExp(line,tyvars,arg(pred));
-    if (args!=1)
-        h98DoesntSupport(line,"multiple parameter classes");
 
     if (isQCon(h)) {                    /* standard class constraint       */
         Class c = findQualClass(h);
@@ -1318,7 +1468,11 @@ Cell pred; {
             ERRMSG(line) "Undefined class \"%s\"", identToStr(h)
             EEND;
         }
-        fun(pred) = c;
+       if (isNull(prev)) {
+           pred = c;
+       } else {
+           fun(prev) = c;
+       }
         if (args!=cclass(c).arity) {
             ERRMSG(line) "Wrong number of arguments for class \"%s\"",
                         textToStr(cclass(c).text)
@@ -1336,9 +1490,14 @@ Cell pred; {
         }
     }
 #endif
-    else {                              /* check for other kinds of pred   */
-        internal("depPredExp");         /* ... but there aren't any!       */
+    else 
+#if IPARAM
+         if (whatIs(h) != IPCELL)
+#endif
+    {
+       internal("depPredExp");
     }
+    return pred;
 }
 
 static Void local checkMems(c,tyvars,m) /* check member function details   */
@@ -1350,11 +1509,20 @@ Cell  m; {
     Type t    = thd3(m);
     List sig  = NIL;
     List tvs  = NIL;
+    List xtvs = NIL;
+
+    if (isPolyType(t)) {
+       xtvs = fst(snd(t));
+       t    = monotypeOf(t);
+    }
+  
 
-    tyvars    = typeVarsIn(t,NIL,tyvars);/* Look for extra type vars.      */
+    tyvars    = typeVarsIn(t,NIL,xtvs,tyvars);
+                                       /* Look for extra type vars.       */
+    checkOptQuantVars(line,xtvs,tyvars);
 
-    if (whatIs(t)==QUAL) {              /* Overloaded member signatures?   */
-        map2Proc(depPredExp,line,tyvars,fst(snd(t)));
+    if (isQualType(t)) {               /* Overloaded member signatures?   */
+       map2Over(depPredExp,line,tyvars,fst(snd(t)));
     } else {
         t = ap(QUAL,pair(NIL,t));
     }
@@ -1365,7 +1533,9 @@ Cell  m; {
     for (tvs=tyvars; nonNull(tvs); tvs=tl(tvs)){/* Quantify                */
         sig = ap(NIL,sig);
     }
-    t       = mkPolyType(sig,t);
+    if (nonNull(sig)) {
+       t = mkPolyType(sig,t);
+    }
     thd3(m) = t;                                /* Save type               */
     take(cclass(c).arity,tyvars);               /* Delete extra type vars  */
 
@@ -1375,6 +1545,14 @@ Cell  m; {
     h98CheckType(line,"member type",hd(vs),t);
 }
 
+static Void local checkMems2(c,m) /* check member function details   */
+Class c;
+Cell  m; {
+    Int  line = intOf(fst3(m));
+    List vs   = snd3(m);
+    Type t    = thd3(m);
+}
+
 static Void local addMembers(c)         /* Add definitions of member funs  */
 Class c; {                              /* and other parts of class struct.*/
     List ms  = fst(cclass(c).members);
@@ -1424,14 +1602,20 @@ Class c; {                              /* and other parts of class struct.*/
 */
 
     mno                  = cclass(c).numSupers + cclass(c).numMembers;
-    cclass(c).dcon       = addPrimCfun(generateText("Make.%s",c),mno,0,NIL);
-    implementCfun(cclass(c).dcon,NIL); /* ADR addition */
+    /* cclass(c).dcon       = addPrimCfun(generateText("Make.%s",c),mno,0,NIL); */
+    cclass(c).dcon       = addPrimCfun(generateText(":D%s",c),mno,0,NIL);
+    /* implementCfun(cclass(c).dcon,NIL);
+       Don't manufacture a wrapper fn for dictionary constructors.
+       Applications of dictionary constructors are always saturated,
+       and translate.c:stgExpr() special-cases saturated constructor apps.
+    */
 
     if (mno==1) {                       /* Single entry dicts use newtype  */
         name(cclass(c).dcon).defn = nameId;
-        name(hd(cclass(c).members)).number = mfunNo(0);
+       if (nonNull(cclass(c).members)) {
+           name(hd(cclass(c).members)).number = mfunNo(0);
+       }
     }
-    cclass(c).dbuild     = newDBuild(c);
     cclass(c).defaults   = classBindings("class",c,cclass(c).defaults);
 }
 
@@ -1455,17 +1639,17 @@ Class parent; {
     name(m).arity    = 1;
     name(m).number   = mfunNo(no);
     name(m).type     = t;
-    name(m).inlineMe = TRUE;
     return m;
 }
 
-static Name local newDSel(c,no)         /* Make definition for dict selectr*/
+Name newDSel(c,no)                      /* Make definition for dict selectr*/
 Class c;
 Int   no; {
     Name s;
     char buf[16];
 
-    sprintf(buf,"sc%d.%s",no,"%s");
+    /* sprintf(buf,"sc%d.%s",no,"%s"); */
+    sprintf(buf,"$p%d%s",no+1,"%s");
     s                = newName(generateText(buf,c),c);
     name(s).line     = cclass(c).line;
     name(s).arity    = 1;
@@ -1473,14 +1657,6 @@ Int   no; {
     return s;
 }
 
-static Name local newDBuild(c)          /* Make definition for builder     */
-Class c; {
-    Name b           = newName(generateText("class.%s",c),c);
-    name(b).line     = cclass(c).line;
-    name(b).arity    = cclass(c).numSupers+1;
-    return b;
-}
-
 #define MAX_GEN  128
 
 static Text local generateText(sk,c)    /* We need to generate names for   */
@@ -1497,7 +1673,7 @@ Class  c; {                             /* to each class.                  */
     return findText(buffer);
 }
 
-static Int local visitClass(c)          /* visit class defn to check that  */
+       Int visitClass(c)                /* visit class defn to check that  */
 Class c; {                              /* class hierarchy is acyclic      */
 #if TREX
     if (isExt(c)) {                     /* special case for lacks preds    */
@@ -1588,41 +1764,50 @@ List xs; {
  * occur in the type expression when read from left to right.
  * ------------------------------------------------------------------------*/
 
-List typeVarsIn(ty,us,vs)               /* Calculate list of type variables*/
-Cell ty;                                /* used in type expression, reading*/
-List us;                                /* from left to right ignoring any */
-List vs; {                              /* listed in us.                   */
+List local typeVarsIn(ty,us,ws,vs)      /*Calculate list of type variables*/
+Cell ty;                               /* used in type expression, reading*/
+List us;                               /* from left to right ignoring any */
+List ws;                               /* listed in us.                   */
+List vs; {                             /* ws = explicitly quantified vars */
+    if (isNull(ty)) return vs;
     switch (whatIs(ty)) {
-        case AP        : return typeVarsIn(snd(ty),us,
-                                           typeVarsIn(fst(ty),us,vs));
+        case DICTAP    : return typeVarsIn(snd(snd(ty)),us,ws,vs);
+        case UNBOXEDTUP: return typeVarsIn(snd(ty),us,ws,vs);
 
-        case VARIDCELL :
-        case VAROPCELL : if (nonNull(findBtyvs(textOf(ty)))
-                             || varIsMember(textOf(ty),us)) {
-                             return vs;
-                         } else {
-                             return maybeAppendVar(ty,vs);
-                         }
+       case AP        : return typeVarsIn(snd(ty),us,ws,
+                                          typeVarsIn(fst(ty),us,ws,vs));
 
-        case POLYTYPE  : return typeVarsIn(monotypeOf(ty),polySigOf(ty),vs);
+       case VARIDCELL :
+       case VAROPCELL : if ((nonNull(findBtyvs(textOf(ty)))
+                             && !varIsMember(textOf(ty),ws))
+                            || varIsMember(textOf(ty),us)) {
+                            return vs;
+                        } else {
+                            return maybeAppendVar(ty,vs);
+                        }
 
-        case QUAL      : {   List qs = fst(snd(ty));
-                             for (; nonNull(qs); qs=tl(qs)) {
-                                 vs = typeVarsIn(hd(qs),us,vs);
-                             }
-                             return typeVarsIn(snd(snd(ty)),us,vs);
-                         }
+       case POLYTYPE  : return typeVarsIn(monotypeOf(ty),polySigOf(ty),ws,vs);
 
-        case BANG      : return typeVarsIn(snd(ty),us,vs);
+       case QUAL      : {   vs = typeVarsIn(fst(snd(ty)),us,ws,vs);
+                            return typeVarsIn(snd(snd(ty)),us,ws,vs);
+                        }
 
-        case LABC      : {   List fs = snd(snd(ty));
-                             for (; nonNull(fs); fs=tl(fs)) {
-                                vs = typeVarsIn(snd(hd(fs)),us,vs);
-                             }
-                             return vs;
-                         }
+       case BANG      : return typeVarsIn(snd(ty),us,ws,vs);
+
+       case LABC      : {   List fs = snd(snd(ty));
+                            for (; nonNull(fs); fs=tl(fs)) {
+                               vs = typeVarsIn(snd(hd(fs)),us,ws,vs);
+                            }
+                            return vs;
+                        }
+        case TUPLE:
+        case TYCON:
+        case CONIDCELL:
+        case QUALIDENT: return vs;
+
+        default: fprintf(stderr, " bad tag = %d\n", whatIs(ty));internal("typeVarsIn");
     }
-    return vs;
+    assert(0);
 }
 
 static List local maybeAppendVar(v,vs) /* append variable to list if not   */
@@ -1661,13 +1846,21 @@ Int    line;                            /* Check validity of type expr in  */
 String where;                           /* explicit type signature         */
 Cell   e;
 Type   type; {
-    List tvs  = typeVarsIn(type,NIL,NIL);
-    Int  n    = length(tvs);
-    List sunk = unkindTypes;
+    List tvs  = NIL;
+    List sunk = NIL;
+    List xtvs = NIL;
+
+    if (isPolyType(type)) {
+       xtvs = fst(snd(type));
+       type = monotypeOf(type);
+    }
+    tvs  = typeVarsIn(type,NIL,xtvs,NIL);
+    sunk = unkindTypes;
+    checkOptQuantVars(line,xtvs,tvs);
 
-    if (whatIs(type)==QUAL) {
-        map2Proc(depPredExp,line,tvs,fst(snd(type)));
-        snd(snd(type)) = depTopType(line,tvs,snd(snd(type)));
+    if (isQualType(type)) {
+       map2Over(depPredExp,line,tvs,fst(snd(type)));
+       snd(snd(type)) = depTopType(line,tvs,snd(snd(type)));
 
         if (isAmbiguous(type)) {
             ambigError(line,where,e,type);
@@ -1676,8 +1869,8 @@ Type   type; {
         type = depTopType(line,tvs,type);
     }
 
-    if (n>0) {
-        if (n>=NUM_OFFSETS) {
+    if (nonNull(tvs)) {
+       if (length(tvs) >= (OFF_MAX-OFF_MIN+1)) {
             ERRMSG(line) "Too many type variables in %s\n", where
             EEND;
         } else {
@@ -1698,6 +1891,34 @@ Type   type; {
     return type;
 }
 
+static Void local checkOptQuantVars(line,xtvs,tvs)
+Int  line;
+List xtvs;                             /* Explicitly quantified vars      */
+List tvs; {                            /* Implicitly quantified vars      */
+    if (nonNull(xtvs)) {
+       List vs = tvs;
+       for (; nonNull(vs); vs=tl(vs)) {
+           if (!varIsMember(textOf(hd(vs)),xtvs)) {
+               ERRMSG(line) "Quantifier does not mention type variable \"%s\"",
+                            textToStr(textOf(hd(vs)))
+               EEND;
+           }
+       }
+       for (vs=xtvs; nonNull(vs); vs=tl(vs)) {
+           if (!varIsMember(textOf(hd(vs)),tvs)) {
+               ERRMSG(line) "Quantified type variable \"%s\" is not used",
+                            textToStr(textOf(hd(vs)))
+               EEND;
+           }
+           if (varIsMember(textOf(hd(vs)),tl(vs))) {
+               ERRMSG(line) "Quantified type variable \"%s\" is repeated",
+                            textToStr(textOf(hd(vs)))
+               EEND;
+           }
+       }
+    }
+}
+
 static Type local depTopType(l,tvs,t)   /* Check top-level of type sig     */
 Int  l;
 List tvs;
@@ -1708,7 +1929,7 @@ Type t; {
     Int  i    = 1;
     for (; getHead(t1)==typeArrow && argCount==2; ++i) {
         arg(fun(t1)) = depCompType(l,tvs,arg(fun(t1)));
-        if (isPolyType(arg(fun(t1)))) {
+       if (isPolyOrQualType(arg(fun(t1)))) {
             nr2 = i;
         }
         prev = t1;
@@ -1729,32 +1950,28 @@ static Type local depCompType(l,tvs,t)  /* Check component type for constr */
 Int  l;
 List tvs;
 Type t; {
-    if (isPolyType(t)) {
-        Int  ntvs = length(tvs);
-        List nfr  = NIL;
-        if (isPolyType(t)) {
-            List vs  = fst(snd(t));
-            t        = monotypeOf(t);
-            tvs      = checkQuantVars(l,vs,tvs,t);
-            nfr      = replicate(length(vs),NIL);
-        }
-        if (whatIs(t)==QUAL) {
-            map2Proc(depPredExp,l,tvs,fst(snd(t)));
-            snd(snd(t)) = depTypeExp(l,tvs,snd(snd(t)));
-            if (isAmbiguous(t)) {
-                ambigError(l,"type component",NIL,t);
-            }
-        } else {
-            t = depTypeExp(l,tvs,t);
-        }
-        if (isNull(nfr)) {
-            return t;
-        }
-        take(ntvs,tvs);
-        return mkPolyType(nfr,t);
-    } else {
-        return depTypeExp(l,tvs,t);
+  Int  ntvs = length(tvs);
+  List nfr  = NIL;
+  if (isPolyType(t)) {
+    List vs  = fst(snd(t));
+    t        = monotypeOf(t);
+    tvs      = checkQuantVars(l,vs,tvs,t);
+    nfr      = replicate(length(vs),NIL);
+  }
+  if (isQualType(t)) {
+    map2Over(depPredExp,l,tvs,fst(snd(t)));
+    snd(snd(t)) = depTypeExp(l,tvs,snd(snd(t)));
+    if (isAmbiguous(t)) {
+      ambigError(l,"type component",NIL,t);
     }
+  } else {
+    t = depTypeExp(l,tvs,t);
+  }
+  if (isNull(nfr)) {
+    return t;
+  }
+  take(ntvs,tvs);
+  return mkPolyType(nfr,t);
 }
 
 static Type local depTypeExp(line,tyvars,type)
@@ -1802,20 +2019,24 @@ static Type local depTypeVar(line,tyvars,tv)
 Int  line;
 List tyvars;
 Text tv; {
-    Int  offset = 0;
-    Cell vt     = findBtyvs(tv);
+    Int offset = 0;
+    Int found  = (-1);
 
-    if (nonNull(vt)) {
-        return fst(vt);
-    }
-    for (; nonNull(tyvars) && tv!=textOf(hd(tyvars)); offset++) {
-        tyvars = tl(tyvars);
+    for (; nonNull(tyvars); offset++) {
+       if (tv==textOf(hd(tyvars))) {
+           found = offset;
+       }
+       tyvars = tl(tyvars);
     }
-    if (isNull(tyvars)) {
-        ERRMSG(line) "Undefined type variable \"%s\"", textToStr(tv)
-        EEND;
+    if (found<0) {
+       Cell vt = findBtyvs(tv);
+       if (nonNull(vt)) {
+           return fst(vt);
+       }
+       ERRMSG(line) "Undefined type variable \"%s\"", textToStr(tv)
+       EEND;
     }
-    return mkOffset(offset);
+    return mkOffset(found);
 }
 
 static List local checkQuantVars(line,vs,tvs,body)
@@ -1824,7 +2045,7 @@ List vs;                                /* variables to quantify over      */
 List tvs;                               /* variables already in scope      */
 Cell body; {                            /* type/constr for scope of vars   */
     if (nonNull(vs)) {
-        List bvs = typeVarsIn(body,NIL,NIL);
+       List bvs = typeVarsIn(body,NIL,NIL,NIL);
         List us  = vs;
         for (; nonNull(us); us=tl(us)) {
             Text u = textOf(hd(us));
@@ -1833,11 +2054,13 @@ Cell body; {                            /* type/constr for scope of vars   */
                              textToStr(u)
                 EEND;
             }
+#if 0
             if (varIsMember(u,tvs)) {
                 ERRMSG(line) "Local quantifier for %s hides an outer use",
                              textToStr(u)
                 EEND;
             }
+#endif
             if (!varIsMember(u,bvs)) {
                 ERRMSG(line) "Locally quantified variable %s is not used",
                              textToStr(u)
@@ -1878,22 +2101,161 @@ List vs; {
     }
 }
 
-Bool isAmbiguous(type)                  /* Determine whether type is       */
-Type type; {                            /* ambiguous                       */
+List zonkTyvarsIn(t,vs)
+Type t;
+List vs; {
+    switch (whatIs(t)) {
+       case AP       : return zonkTyvarsIn(fun(t),
+                                zonkTyvarsIn(arg(t),vs));
+
+       case INTCELL  : if (cellIsMember(t,vs))
+                           return vs;
+                       else
+                           return cons(t,vs);
+
+       /* this case will lead to a type error --
+          much better than reporting an internal error ;-) */
+       /* case OFFSET   : internal("zonkTyvarsIn"); */
+
+       default       : return vs;
+    }
+}
+
+static List local otvars(pi,os)                /* os is a list of offsets that    */
+Cell pi;                               /* refer to the arguments of pi;   */
+List os; {                             /* find list of offsets in those   */
+    List us = NIL;                     /* positions                       */
+    for (; nonNull(os); os=tl(os)) {
+       us = offsetTyvarsIn(nthArg(offsetOf(hd(os)),pi),us);
+    }
+    return us;
+}
+
+static List local otvarsZonk(pi,os,o)  /* same as above, but zonks        */
+Cell pi;
+List os; {
+    List us = NIL;
+    for (; nonNull(os); os=tl(os)) {
+        Type t = zonkType(nthArg(offsetOf(hd(os)),pi),o);
+       us = zonkTyvarsIn(t,us);
+    }
+    return us;
+}
+
+static Bool local odiff(us,vs)
+List us, vs; {
+    while (nonNull(us) && cellIsMember(hd(us),vs)) {
+       us = tl(us);
+    }
+    return us;
+}
+
+static Bool local osubset(us,vs)       /* Determine whether us is subset  */
+List us, vs; {                         /* of vs                           */
+    while (nonNull(us) && cellIsMember(hd(us),vs)) {
+       us = tl(us);
+    }
+    return isNull(us);
+}
+
+List oclose(fds,vs)    /* Compute closure of vs wrt to fds*/
+List fds;
+List vs; {
+    Bool changed = TRUE;
+    while (changed) {
+       List fds1 = NIL;
+       changed = FALSE;
+        while (nonNull(fds)) {
+           Cell fd   = hd(fds);
+           List next = tl(fds);
+           if (osubset(fst(fd),vs)) {  /* Test if fd applies              */
+               List os = snd(fd);
+               for (; nonNull(os); os=tl(os)) {
+                   if (!cellIsMember(hd(os),vs)) {
+                       vs      = cons(hd(os),vs);
+                       changed = TRUE;
+                   }
+               }
+           } else {                    /* Didn't apply this time, so keep */
+               tl(fds) = fds1;
+               fds1    = fds;
+           }
+           fds = next;
+       }
+       fds = fds1;
+    }
+    return vs;
+}
+
+Bool isAmbiguous(type)                 /* Determine whether type is       */
+Type type; {                           /* ambiguous                       */
     if (isPolyType(type)) {
-        type = monotypeOf(type);
+       type = monotypeOf(type);
     }
-    if (whatIs(type)==QUAL) {           /* only qualified types can be     */
-        List tvps = offsetTyvarsIn(fst(snd(type)),NIL); /* ambiguous       */
-        List tvts = offsetTyvarsIn(snd(snd(type)),NIL);
-        while (nonNull(tvps) && cellIsMember(hd(tvps),tvts)) {
-            tvps = tl(tvps);
-        }
-        return nonNull(tvps);
+    if (isQualType(type)) {            /* only qualified types can be     */
+       List ps   = fst(snd(type));     /* ambiguous                       */
+       List tvps = offsetTyvarsIn(ps,NIL);
+       List tvts = offsetTyvarsIn(snd(snd(type)),NIL);
+       List fds  = calcFunDeps(ps);
+
+       tvts = oclose(fds,tvts);        /* Close tvts under fds            */
+       return !osubset(tvps,tvts);
     }
     return FALSE;
 }
 
+List calcFunDeps(ps)
+List ps; {
+    List fds  = NIL;
+    for (; nonNull(ps); ps=tl(ps)) {/* Calc functional dependencies       */
+       Cell pi = hd(ps);
+       Cell c  = getHead(pi);
+       if (isClass(c)) {
+           List xfs = cclass(c).xfds;
+           for (; nonNull(xfs); xfs=tl(xfs)) {
+               List fs = snd(hd(xfs));
+               for (; nonNull(fs); fs=tl(fs)) {
+                   fds = cons(pair(otvars(pi,fst(hd(fs))),
+                                   otvars(pi,snd(hd(fs)))),fds);
+               }
+           }
+       }
+#if IPARAM
+       else if (isIP(c)) {
+           fds = cons(pair(NIL,offsetTyvarsIn(arg(pi),NIL)),fds);
+       }
+#endif
+    }
+    return fds;
+}
+
+List calcFunDepsPreds(ps)
+List ps; {
+    List fds  = NIL;
+    for (; nonNull(ps); ps=tl(ps)) {/* Calc functional dependencies       */
+       Cell pi3 = hd(ps);
+       Cell pi = fst3(pi3);
+       Cell c  = getHead(pi);
+       Int o = intOf(snd3(pi3));
+       if (isClass(c)) {
+           List xfs = cclass(c).xfds;
+           for (; nonNull(xfs); xfs=tl(xfs)) {
+               List fs = snd(hd(xfs));
+               for (; nonNull(fs); fs=tl(fs)) {
+                   fds = cons(pair(otvarsZonk(pi,fst(hd(fs)),o),
+                                   otvarsZonk(pi,snd(hd(fs)),o)),fds);
+               }
+           }
+       }
+#if IPARAM
+       else if (isIP(c)) {
+           fds = cons(pair(NIL,zonkTyvarsIn(arg(pi),NIL)),fds);
+       }
+#endif
+    }
+    return fds;
+}
+
 Void ambigError(line,where,e,type)      /* produce error message for       */
 Int    line;                            /* ambiguity                       */
 String where;
@@ -2032,12 +2394,19 @@ Int  alpha;
 Int  m;
 Cell pi; {
 #if TREX
-    if (isExt(fun(pi))) {
+    if (isAp(pi) && isExt(fun(pi))) {
         static String lackspred = "lacks predicate";
         checkKind(l,alpha,m,arg(pi),NIL,lackspred,ROW,0);
         return;
     }
 #endif
+#if IPARAM
+    if (isAp(pi) && whatIs(fun(pi)) == IPCELL) {
+       static String ippred = "iparam predicate";
+       checkKind(l,alpha,m,arg(pi),NIL,ippred,STAR,0);
+       return;
+    }
+#endif
     {   static String predicate = "class constraint";
         Class c  = getHead(pi);
         List  as = getArgs(pi);
@@ -2116,10 +2485,10 @@ Cell c; {
         Int n    = cclass(c).arity;
         Int beta = newKindvars(n);
         cclass(c).kinds = NIL;
-        do {
+       while (n>0) {
             n--;
             cclass(c).kinds = pair(mkInt(beta+n),cclass(c).kinds);
-        } while (n>0);
+        }
     }
 }
 
@@ -2134,7 +2503,7 @@ Cell c; {                               /* is well-kinded                  */
         switch (whatIs(tycon(c).what)) {
             case NEWTYPE     :
             case DATATYPE    : {   List cs = tycon(c).defn;
-                                   if (whatIs(cs)==QUAL) {
+                                  if (isQualType(cs)) {
                                        map3Proc(kindPred,line,beta,m,
                                                                 fst(snd(cs)));
                                        tycon(c).defn = cs = snd(snd(cs));
@@ -2230,7 +2599,9 @@ Name nameListMonad = NIL;               /* builder function for List Monad */
 static Void local checkInstDefn(in)     /* Validate instance declaration   */
 Inst in; {
     Int  line   = inst(in).line;
-    List tyvars = typeVarsIn(inst(in).head,NIL,NIL);
+    List tyvars = typeVarsIn(inst(in).head,NIL,NIL,NIL);
+    List tvps = NIL, tvts = NIL;
+    List fds = NIL;
 
     if (haskell98) {                    /* Check for `simple' type         */
         List tvs = NIL;
@@ -2255,7 +2626,10 @@ Inst in; {
         }
     }
 
-    depPredExp(line,tyvars,inst(in).head);
+    /* add in the tyvars from the `specifics' so that we don't
+       prematurely complain about undefined tyvars */
+    tyvars = typeVarsIn(inst(in).specifics,NIL,NIL,tyvars);
+    inst(in).head = depPredExp(line,tyvars,inst(in).head);
 
     if (haskell98) {
         Type h = getHead(arg(inst(in).head));
@@ -2265,7 +2639,20 @@ Inst in; {
         }
     }
 
-    map2Proc(depPredExp,line,tyvars,inst(in).specifics);
+    map2Over(depPredExp,line,tyvars,inst(in).specifics);
+
+    /* OK, now we start over, and test for ambiguity */
+    tvts = offsetTyvarsIn(inst(in).head,NIL);
+    tvps = offsetTyvarsIn(inst(in).specifics,NIL);
+    fds  = calcFunDeps(inst(in).specifics);
+    tvts = oclose(fds,tvts);
+    tvts = odiff(tvps,tvts);
+    if (!isNull(tvts)) {
+       ERRMSG(line) "Undefined type variable \"%s\"",
+         textToStr(textOf(nth(offsetOf(hd(tvts)),tyvars)))
+       EEND;
+    }
+
     h98CheckCtxt(line,"instance definition",FALSE,inst(in).specifics,NIL);
     inst(in).numSpecifics = length(inst(in).specifics);
     inst(in).c            = getHead(inst(in).head);
@@ -2273,6 +2660,30 @@ Inst in; {
         ERRMSG(line) "Illegal predicate in instance declaration"
         EEND;
     }
+
+    if (nonNull(cclass(inst(in).c).fds)) {
+        List fds = cclass(inst(in).c).fds;
+        for (; nonNull(fds); fds=tl(fds)) {
+            List as = otvars(inst(in).head, fst(hd(fds)));
+            List bs = otvars(inst(in).head, snd(hd(fds)));
+           List fs = calcFunDeps(inst(in).specifics);
+           as = oclose(fs,as);
+            if (!osubset(bs,as)) {
+               ERRMSG(inst(in).line)
+                  "Instance is more general than a dependency allows"
+               ETHEN
+               ERRTEXT "\n*** Instance         : "
+               ETHEN ERRPRED(inst(in).head);
+               ERRTEXT "\n*** For class        : "
+               ETHEN ERRPRED(cclass(inst(in).c).head);
+               ERRTEXT "\n*** Under dependency : "
+               ETHEN ERRFD(hd(fds));
+               ERRTEXT "\n"
+               EEND;
+            }
+        }
+    }
+
     kindInst(in,length(tyvars));
     insertInst(in);
 
@@ -2302,6 +2713,48 @@ Inst in; {
     List  ins  = cclass(c).instances;
     List  prev = NIL;
 
+    if (nonNull(cclass(c).fds)) {      /* Check for conflicts with fds    */
+       List ins1 = cclass(c).instances;
+       for (; nonNull(ins1); ins1=tl(ins1)) {
+           List fds = cclass(c).fds;
+           substitution(RESET);
+           for (; nonNull(fds); fds=tl(fds)) {
+               Int  alpha = newKindedVars(inst(in).kinds);
+               Int  beta  = newKindedVars(inst(hd(ins1)).kinds);
+               List as    = fst(hd(fds));
+               Bool same  = TRUE;
+               for (; same && nonNull(as); as=tl(as)) {
+                   Int n = offsetOf(hd(as));
+                   same &= unify(nthArg(n,inst(in).head),alpha,
+                                 nthArg(n,inst(hd(ins1)).head),beta);
+               }
+               if (isNull(as) && same) {
+                   for (as=snd(hd(fds)); same && nonNull(as); as=tl(as)) {
+                       Int n = offsetOf(hd(as));
+                       same &= sameType(nthArg(n,inst(in).head),alpha,
+                                        nthArg(n,inst(hd(ins1)).head),beta);
+                   }
+                   if (!same) {
+                       ERRMSG(inst(in).line)
+                          "Instances are not consistent with dependencies"
+                       ETHEN
+                       ERRTEXT "\n*** This instance    : "
+                       ETHEN ERRPRED(inst(in).head);
+                       ERRTEXT "\n*** Conflicts with   : "
+                       ETHEN ERRPRED(inst(hd(ins)).head);
+                       ERRTEXT "\n*** For class        : "
+                       ETHEN ERRPRED(cclass(c).head);
+                       ERRTEXT "\n*** Under dependency : "
+                       ETHEN ERRFD(hd(fds));
+                       ERRTEXT "\n"
+                       EEND;
+                   }
+               }
+           }
+       }
+    }
+
+
     substitution(RESET);
     while (nonNull(ins)) {              /* Look for overlap w/ other insts */
         Int alpha = newKindedVars(inst(in).kinds);
@@ -2320,6 +2773,11 @@ Inst in; {
                     continue;
                 }
             }
+#if MULTI_INST
+           if (multiInstRes && nonNull(inst(in).specifics)) {
+               break;
+           } else {
+#endif
             ERRMSG(inst(in).line) "Overlapping instances for class \"%s\"",
                                   textToStr(cclass(c).text)
             ETHEN
@@ -2331,6 +2789,9 @@ Inst in; {
             ERRTEXT "\n"
             EEND;
         }
+#if MULTI_INST
+           }
+#endif
         prev = ins;                     /* No overlap detected, so move on */
         ins  = tl(ins);                 /* to next instance                */
     }
@@ -2399,10 +2860,10 @@ List  p;                                /* context p, component types ts   */
 List  ts;                               /* and named class ct              */
 Cell  ct; {
     Int   line = tycon(t).line;
-    Class c    = findClass(textOf(ct));
+    Class c    = findQualClass(ct);
     if (isNull(c)) {
         ERRMSG(line) "Unknown class \"%s\" in derived instance",
-                     textToStr(textOf(ct))
+                    identToStr(ct)
         EEND;
     }
     addDerInst(line,c,p,dupList(ts),t,tycon(t).arity);
@@ -2561,6 +3022,8 @@ Inst in; {                              /* of the context for a derived    */
     List ps     = snd(snd(inst(in).specifics));
     List spcs   = fst(snd(inst(in).specifics));
     Int  beta   = inst(in).numSpecifics;
+    Int  its    = 1;
+    Int  factor = 1+length(ps);
 
 #ifdef DEBUG_DERIVING
     Printf("calcInstPreds: ");
@@ -2571,6 +3034,19 @@ Inst in; {                              /* of the context for a derived    */
     while (nonNull(ps)) {
         Cell p = hd(ps);
         ps     = tl(ps);
+       if (its++ >= factor*cutoff) {
+           Cell bpi = inst(in).head;
+           ERRMSG(inst(in).line) "\n*** Cannot derive " ETHEN ERRPRED(bpi);
+           ERRTEXT " after %d iterations.", its-1   ETHEN
+           ERRTEXT
+               "\n*** This may indicate that the problem is undecidable.  However,\n"
+           ETHEN ERRTEXT
+               "*** you may still try to increase the cutoff limit using the -c\n"
+           ETHEN ERRTEXT
+               "*** option and then try again.  (The current setting is -c%d)\n",
+               cutoff
+           EEND;
+       }
         if (isInt(fst(p))) {                    /* Delayed substitution?   */
             List qs = snd(p);
             for (; nonNull(hd(qs)); qs=tl(qs)) {
@@ -2677,7 +3153,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);
@@ -2794,15 +3270,18 @@ static Void local checkDefaultDefns() { /* check that default types are    */
  * Foreign import declarations are Hugs' equivalent of GHC's ccall mechanism.
  * They are used to "import" C functions into a module.
  * They are usually not written by hand but, rather, generated automatically
- * by GreenCard, IDL compilers or whatever.
+ * by GreenCard, IDL compilers or whatever.  We support foreign import 
+ * (static) and foreign import dynamic.  In the latter case, extName==NIL.
  *
  * Foreign export declarations generate C wrappers for Hugs functions.
  * Hugs only provides "foreign export dynamic" because it's not obvious
  * what "foreign export static" would mean in an interactive setting.
  * ------------------------------------------------------------------------*/
 
-Void foreignImport(line,extName,intName,type) /* Handle foreign imports    */
+Void foreignImport(line,callconv,extName,intName,type) 
+                                              /* Handle foreign imports    */
 Cell line;
+Text callconv;
 Pair extName;
 Cell intName;
 Cell type; {
@@ -2816,10 +3295,11 @@ Cell type; {
         ERRMSG(l) "Redeclaration of foreign \"%s\"", textToStr(t)
         EEND;
     }
-    name(n).line = l;
-    name(n).defn = extName;
-    name(n).type = type;
-    foreignImports = cons(n,foreignImports);
+    name(n).line     = l;
+    name(n).defn     = extName;
+    name(n).type     = type;
+    name(n).callconv = callconv;
+    foreignImports   = cons(n,foreignImports);
 }
 
 static Void local checkForeignImport(p)   /* Check foreign import          */
@@ -2836,8 +3316,10 @@ Name p; {
     implementForeignImport(p);
 }
 
-Void foreignExport(line,extName,intName,type)/* Handle foreign exports    */
+Void foreignExport(line,callconv,extName,intName,type)
+                                              /* Handle foreign exports    */
 Cell line;
+Text callconv;
 Cell extName;
 Cell intName;
 Cell type; {
@@ -2851,10 +3333,11 @@ Cell type; {
         ERRMSG(l) "Redeclaration of foreign \"%s\"", textToStr(t)
         EEND;
     }
-    name(n).line = l;
-    name(n).defn = NIL;  /* nothing to say */
-    name(n).type = type;
-    foreignExports = cons(n,foreignExports);
+    name(n).line     = l;
+    name(n).defn     = NIL;  /* nothing to say */
+    name(n).type     = type;
+    name(n).callconv = callconv;
+    foreignExports   = cons(n,foreignExports);
 }
 
 static Void local checkForeignExport(p)       /* Check foreign export      */
@@ -2942,7 +3425,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     */
@@ -2960,7 +3442,6 @@ Cell p; {
         arg(p)           = checkPat(l,v);
         return p;
     }
-#endif
     return checkApPat(l,0,p);
 }
 
@@ -3071,14 +3552,14 @@ Int    l;
 String wh;
 Cell   e;
 Type   t; {
-    List tvs = typeVarsIn(t,NIL,NIL);
+    List tvs = typeVarsIn(t,NIL,NIL,NIL);
     h98DoesntSupport(l,"pattern type annotations");
     for (; nonNull(tvs); tvs=tl(tvs)) {
         Int beta    = newKindvars(1);
         hd(btyvars) = cons(pair(hd(tvs),mkInt(beta)), hd(btyvars));
     }
     t = checkSigType(l,"pattern type",e,t);
-    if (isPolyType(t) || whatIs(t)==QUAL || whatIs(t)==RANK2) {
+    if (isPolyOrQualType(t) || whatIs(t)==RANK2) {
         ERRMSG(l) "Illegal syntax in %s type annotation", wh
         EEND;
     }
@@ -4068,6 +4549,10 @@ Cell e; {
                           break;
 #endif
 
+#if IPARAM
+       case IPVAR      :
+#endif
+
         case NAME       :
         case TUPLE      :
         case STRCELL    :
@@ -4114,6 +4599,11 @@ Cell e; {
         case UPDFLDS    : depUpdFlds(line,e);
                           break;
 
+#if IPARAM
+       case WITHEXP    : depWith(line,e);
+                         break;
+#endif
+
         case ASPAT      : ERRMSG(line) "Illegal `@' in expression"
                           EEND;
 
@@ -4227,9 +4717,12 @@ Cell e; {
         EEND;
     }
 
+#if 0
+    what is this for??
     if (!moduleThisScript(name(n).mod)) {
         return n;
     }
+#endif
     /* 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.
@@ -4280,7 +4773,7 @@ Bool isP; {
         if (isPolyType(t)) {            /* Find tycon that c belongs to    */
             t = monotypeOf(t);
         }
-        if (whatIs(t)==QUAL) {
+       if (isQualType(t)) {
             t = snd(snd(t));
         }
         if (whatIs(t)==CDICTS) {
@@ -4413,6 +4906,27 @@ Bool isP; {
     return cs;
 }
 
+#if IPARAM
+static Void local depWith(line,e)      /* check with using fields         */
+Int  line;
+Cell e; {
+    fst(snd(e)) = depExpr(line,fst(snd(e)));
+    snd(snd(e)) = depDwFlds(line,e,snd(snd(e)));
+}
+
+static List local depDwFlds(l,e,fs)/* check field binding list    */
+Int  l;
+Cell e;
+List fs;
+{
+    Cell c = fs;
+    for (; nonNull(c); c=tl(c)) {      /* for each field binding          */
+       snd(hd(c)) = depExpr(l,snd(hd(c)));
+    }
+    return fs;
+}
+#endif
+
 #if TREX
 static Cell local depRecord(line,e)     /* find dependents of record and   */
 Int  line;                              /* sort fields into approp. order  */
@@ -4492,8 +5006,25 @@ Void checkExp() {                       /* Top level static check on Expr  */
     staticAnalysis(RESET);
 }
 
-Void checkDefns() {                     /* Top level static analysis       */
-    Module thisModule = lastModule();
+#if EXPLAIN_INSTANCE_RESOLUTION
+Void checkContext(void) {              /* Top level static check on Expr  */
+    List vs, qs;
+
+    staticAnalysis(RESET);
+    clearScope();                      /* Analyse expression in the scope */
+    withinScope(NIL);                  /* of no local bindings            */
+    qs = inputContext;
+    for (vs = NIL; nonNull(qs); qs=tl(qs)) {
+       vs = typeVarsIn(hd(qs),NIL,NIL,vs);
+    }
+    map2Proc(depPredExp,0,vs,inputContext);
+    leaveScope();
+    staticAnalysis(RESET);
+}
+#endif
+
+Void checkDefns ( Module thisModule ) { /* Top level static analysis       */
+
     staticAnalysis(RESET);
 
     setCurrModule(thisModule);
@@ -4510,36 +5041,47 @@ Void checkDefns() {                     /* Top level static analysis       */
         /* Every module (including the Prelude) implicitly contains 
          * "import qualified Prelude" 
          */
-        module(thisModule).qualImports=cons(pair(mkCon(textPrelude),modulePrelude),
-                                            module(thisModule).qualImports);
+        module(thisModule).qualImports
+           =cons(pair(mkCon(textPrelude),modulePrelude),
+                 module(thisModule).qualImports);
     }
     mapProc(checkImportList, unqualImports);
 
-    linkPreludeTC();                    /* Get prelude tycons and classes  */
+    /* Note: there's a lot of side-effecting going on here, so
+       don't monkey about with the order of operations here unless
+       you know what you are doing */
+    if (!combined) linkPreludeTC();     /* Get prelude tycons and classes  */
+
     mapProc(checkTyconDefn,tyconDefns); /* validate tycon definitions      */
     checkSynonyms(tyconDefns);          /* check synonym definitions       */
     mapProc(checkClassDefn,classDefns); /* process class definitions       */
     mapProc(kindTCGroup,tcscc(tyconDefns,classDefns)); /* attach kinds     */
+    mapProc(visitClass,classDefns);    /* check class hierarchy           */
+    mapProc(extendFundeps,classDefns);  /* finish class definitions       */
+                                       /* (convenient if we do this after */
+                                       /* calling `visitClass' so that we */
+                                       /* know the class hierarchy is     */
+                                       /* acyclic)                        */
+
     mapProc(addMembers,classDefns);     /* add definitions for member funs */
-    mapProc(visitClass,classDefns);     /* check class hierarchy           */
-    linkPreludeCM();                    /* Get prelude cfuns and mfuns     */
+
+    if (!combined) linkPreludeCM();     /* Get prelude cfuns and mfuns     */
     
     instDefns = rev(instDefns);         /* process instance definitions    */
     mapProc(checkInstDefn,instDefns);
 
     setCurrModule(thisModule);
+    mapProc(addRSsigdecls,typeInDefns);        /* add sigdecls for RESTRICTSYN    */
+    valDefns   = eqnsToBindings(valDefns,tyconDefns,classDefns,/*primDefns*/NIL);
+    mapProc(allNoPrevDef,valDefns);    /* check against previous defns    */
     mapProc(addDerivImp,derivedInsts);  /* Add impls for derived instances */
     deriveContexts(derivedInsts);       /* Calculate derived inst contexts */
     instDefns  = appendOnto(instDefns,derivedInsts);
     checkDefaultDefns();                /* validate default definitions    */
 
-    mapProc(addRSsigdecls,typeInDefns); /* add sigdecls for RESTRICTSYN    */
-    valDefns   = eqnsToBindings(valDefns,tyconDefns,classDefns, NIL/*primDefns*/ );
-    tyconDefns = NIL;
-
     mapProc(allNoPrevDef,valDefns);     /* check against previous defns    */
 
-    linkPreludeNames();
+    if (!combined) linkPrimNames();     /* link primitive names           */
 
     mapProc(checkForeignImport,foreignImports); /* check foreign imports   */
     mapProc(checkForeignExport,foreignExports); /* check foreign exports   */
@@ -4550,7 +5092,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  */
 
@@ -4567,6 +5110,9 @@ Void checkDefns() {                     /* Top level static analysis       */
     staticAnalysis(RESET);
 }
 
+
+
+
 static Void local addRSsigdecls(pr)     /* add sigdecls from TYPE ... IN ..*/
 Pair pr; {
     List vs = snd(pr);                  /* get list of variables           */
@@ -4687,7 +5233,7 @@ Type   t; {
         Type ty = t;
         if (isPolyType(t))
             t = monotypeOf(t);
-        if (whatIs(t)==QUAL) {
+       if (isQualType(t)) {
             Cell pi = h98Context(TRUE,fst(snd(t)));
             if (nonNull(pi)) {
                 ERRMSG(line) "Illegal Haskell 98 class constraint in %s",wh
@@ -4745,11 +5291,12 @@ Int what; {
 #endif
                        break;
 
-        case INSTALL : staticAnalysis(RESET);
+        case POSTPREL: break;
+
+        case PREPREL : staticAnalysis(RESET);
 #if TREX
                        extKind = pair(STAR,pair(ROW,ROW));
 #endif
-                       break;
     }
 }