[project @ 2000-02-03 13:55:21 by sewardj]
[ghc-hetmet.git] / ghc / interpreter / hugs.c
index 2f426c5..f15a624 100644 (file)
@@ -2,14 +2,15 @@
 /* --------------------------------------------------------------------------
  * Command interpreter
  *
- * Hugs 98 is Copyright (c) Mark P Jones, Alastair Reid and the Yale
- * Haskell Group 1994-99, and is distributed as Open Source software
- * under the Artistic License; see the file "Artistic" that is included
- * in the distribution for details.
+ * The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
+ * Yale Haskell Group, and the Oregon Graduate Institute of Science and
+ * Technology, 1994-1999, All rights reserved.  It is distributed as
+ * free software under the license in the file "License", which is
+ * included in the distribution.
  *
  * $RCSfile: hugs.c,v $
- * $Revision: 1.7 $
- * $Date: 1999/06/07 17:22:43 $
+ * $Revision: 1.37 $
+ * $Date: 2000/02/03 13:55:21 $
  * ------------------------------------------------------------------------*/
 
 #include <setjmp.h>
 #include "Rts.h"
 #include "RtsAPI.h"
 #include "Schedule.h"
-
+#include "Assembler.h"                                /* DEBUG_LoadSymbols */
 
 Bool haskell98 = TRUE;                  /* TRUE => Haskell 98 compatibility*/
 
+#if EXPLAIN_INSTANCE_RESOLUTION
+Bool showInstRes = FALSE;
+#endif
+#if MULTI_INST
+Bool multiInstRes = FALSE;
+#endif
+
 /* --------------------------------------------------------------------------
  * Local function prototypes:
  * ------------------------------------------------------------------------*/
@@ -82,12 +90,15 @@ static Void   local forgetScriptsFrom Args((Script));
 static Void   local setLastEdit       Args((String,Int));
 static Void   local failed            Args((Void));
 static String local strCopy           Args((String));
-
+static Void   local browseit         Args((Module,String,Bool));
+static Void   local browse           Args((Void));
 
 /* --------------------------------------------------------------------------
  * Machine dependent code for Hugs interpreter:
  * ------------------------------------------------------------------------*/
 
+       Bool   combined      = TRUE;
+
 #include "machdep.c"
 #ifdef WANT_TIMER
 #include "timer.c"
@@ -97,14 +108,15 @@ static String local strCopy           Args((String));
  * Local data areas:
  * ------------------------------------------------------------------------*/
 
-static Bool   printing     = FALSE;     /* TRUE => currently printing value*/
-static Bool   showStats    = FALSE;     /* TRUE => print stats after eval  */
-static Bool   listScripts  = TRUE;      /* TRUE => list scripts after loading*/
-static Bool   addType      = FALSE;     /* TRUE => print type with value   */
-static Bool   useDots      = RISCOS;    /* TRUE => use dots in progress    */
-static Bool   quiet        = FALSE;     /* TRUE => don't show progress     */
+static Bool   printing      = FALSE;    /* TRUE => currently printing value*/
+static Bool   showStats     = FALSE;    /* TRUE => print stats after eval  */
+static Bool   listScripts   = TRUE;   /* TRUE => list scripts after loading*/
+static Bool   addType       = FALSE;    /* TRUE => print type with value   */
+static Bool   useDots       = RISCOS;   /* TRUE => use dots in progress    */
+static Bool   quiet         = FALSE;    /* TRUE => don't show progress     */
+static Bool   lastWasObject = FALSE;
        Bool   preludeLoaded = FALSE;
-       Bool   optimise      = FALSE;
+       Bool   debugSC       = FALSE;
 
 typedef 
    struct { 
@@ -146,6 +158,8 @@ static Int    hpSize     = DEFAULTHEAP; /* Desired heap size               */
        String hugsEdit   = 0;           /* String for editor command       */
        String hugsPath   = 0;           /* String for file search path     */
 
+       List  ifaces_outstanding = NIL;
+
 #if REDIRECT_OUTPUT
 static Bool disableOutput = FALSE;      /* redirect output to buffer?      */
 #endif
@@ -158,6 +172,7 @@ String bool2str ( Bool b )
 void ppSmStack ( String who )
 {
    int i, j;
+return;
    fflush(stdout);fflush(stderr);
    printf ( "\n" );
    printf ( "ppSmStack %s:  numScripts = %d   namesUpto = %d  needsImports = %s\n",
@@ -178,10 +193,9 @@ void ppSmStack ( String who )
                   scriptInfo[i].path
              );
    }
-   //   printf ( "\n" );
    fflush(stdout);fflush(stderr);
-ppScripts();
-ppModules();
+   ppScripts();
+   ppModules();
    printf ( "\n" );
 }
 
@@ -219,15 +233,24 @@ char *argv[]; {
        main after loading scripts.  Useful for running the nofib suite.    */
     if (argc > 1 && (strcmp(argv[1],"+Q") == 0 || strcmp(argv[1],"-Q")==0)) {
        autoMain = TRUE;
-       hugsEnableOutput(0);
+       if (strcmp(argv[1],"-Q") == 0) {
+        hugsEnableOutput(0);
+       }
     }
 
-    Printf("__   __ __  __  ____   ___     _______________________________________________\n");
-    Printf("||   || ||  || ||  || ||__     Hugs 98: The Nottingham and Yale Haskell system\n");
-    Printf("||___|| ||__|| ||__||  __||    Copyright (c) 1994-1999\n");
-    Printf("||---||         ___||          World Wide Web: http://haskell.org/hugs\n");
-    Printf("||   ||                        Report bugs to: hugs-bugs@haskell.org\n");
-    Printf("||   || Version: %s _______________________________________________\n\n",HUGS_VERSION);
+    Printf("__   __ __  __  ____   ___      _________________________________________\n");
+    Printf("||   || ||  || ||  || ||__      STGHugs: Based on the Haskell 98 standard\n");
+    Printf("||___|| ||__|| ||__||  __||     Copyright (c) 1994-1999\n");
+    Printf("||---||         ___||           World Wide Web: http://haskell.org/hugs\n");
+    Printf("||   ||                         Report bugs to: hugs-bugs@haskell.org\n");
+    Printf("||   || Version: %s _________________________________________\n\n",HUGS_VERSION);
+
+    /* Get the absolute path to the directory containing the hugs 
+       executable, so that we know where the Prelude and nHandle.so/.dll are.
+       We do this by reading env var STGHUGSDIR.  This needs to succeed, so
+       setInstallDir won't return unless it succeeds.
+    */
+    setInstallDir ( argv[0] );
 
 #if SYMANTEC_C
     Printf("   Ported to Macintosh by Hans Aberg, compiled " __DATE__ ".\n\n");
@@ -263,13 +286,14 @@ String argv[]; {
     namesUpto     = 1;
 
 #if HUGS_FOR_WINDOWS
-    hugsEdit      = strCopy(fromEnv("EDITOR","c:\\windows\notepad.exe"));
+    hugsEdit      = strCopy(fromEnv("EDITOR","c:\\windows\\notepad.exe"));
 #elif SYMANTEC_C
     hugsEdit      = "";
 #else
     hugsEdit      = strCopy(fromEnv("EDITOR",NULL));
 #endif
-    hugsPath      = strCopy(HUGSPATH); readOptions("-p\"%s> \" -r$$");
+    hugsPath      = strCopy(HUGSPATH);
+    readOptions("-p\"%s> \" -r$$");
 #if USE_REGISTRY
     projectPath   = strCopy(readRegChildStrings(HKEY_LOCAL_MACHINE,ProjectRoot,
                                                 "HUGSPATH", PATHSEP, ""));
@@ -282,10 +306,23 @@ String argv[]; {
    startupHaskell (argc,argv);
    argc = prog_argc; argv = prog_argv;
 
-    namesUpto = numScripts = 0;
-    addStackEntry("Prelude");
+   namesUpto = numScripts = 0;
+
+   /* Pre-scan flags to see if -c or +c is present.  This needs to
+      precede adding the stack entry for Prelude.  On the other hand,
+      that stack entry needs to be made before the cmd line args are
+      properly examined.  Hence the following pre-scan of them.
+   */
+   for (i=1; i < argc; ++i) {
+      if (strcmp(argv[i], "--")==0) break;
+      if (strcmp(argv[i], "-c")==0) combined = FALSE;
+      if (strcmp(argv[i], "+c")==0) combined = TRUE;
+   }
+
+   addStackEntry("Prelude");
+   if (combined) addStackEntry("PrelHugs");
 
-   for (i=1; i<argc; ++i) {            /* process command line arguments  */
+   for (i=1; i < argc; ++i) {            /* process command line arguments  */
         if (strcmp(argv[i], "--")==0) break;
         if (strcmp(argv[i],"+")==0 && i+1<argc) {
             if (proj) {
@@ -300,12 +337,16 @@ String argv[]; {
         }
     }
 
-#ifdef DEBUG
-    DEBUG_LoadSymbols(argv_0_orig);
+#if DEBUG
+    { 
+       char exe_name[N_INSTALLDIR + 6];
+       strcpy(exe_name, installDir);
+       strcat(exe_name, "hugs");
+       DEBUG_LoadSymbols(exe_name);
+    }
 #endif
 
 
-
 #if 0
     if (!scriptName[0]) {
         Printf("Prelude not found on current path: \"%s\"\n",
@@ -315,12 +356,19 @@ String argv[]; {
 #endif
 
     if (haskell98) {
-        Printf("Haskell 98 mode: Restart with command line option -98 to enable extensions\n\n");
+        Printf("Haskell 98 mode: Restart with command line option -98 to enable extensions\n");
+    } else {
+        Printf("Hugs mode: Restart with command line option +98 for Haskell 98 mode\n");
+    }
+
+    if (combined) {
+        Printf("Combined mode: Restart with command line -c for standalone mode\n\n" );
     } else {
-        Printf("Hugs mode: Restart with command line option +98 for Haskell 98 mode\n\n");
+        Printf("Standalone mode: Restart with command line +c for combined mode\n\n" );
     }
  
-    everybody(INSTALL);
+    everybody(PREPREL);
+
     evalModule = findText("");      /* evaluate wrt last module by default */
     if (proj) {
         if (namesUpto>1) {
@@ -338,6 +386,7 @@ String argv[]; {
 
 struct options {                        /* command line option toggles     */
     char   c;                           /* table defined in main app.      */
+    int    h98;
     String description;
     Bool   *flag;
 };
@@ -361,7 +410,7 @@ Bool state; {                           /* given state                     */
     Int count = 0;
     Int i;
     for (i=0; toggle[i].c; ++i)
-        if (*toggle[i].flag == state) {
+       if (*toggle[i].flag == state && (!haskell98 || toggle[i].h98)) {
             if (count==0)
                 Putchar((char)(state ? '+' : '-'));
             Putchar(toggle[i].c);
@@ -377,8 +426,11 @@ static Void local optionInfo() {        /* Print information about command */
     Int    i;
 
     Printf("TOGGLES: groups begin with +/- to turn options on/off resp.\n");
-    for (i=0; toggle[i].c; ++i)
-        Printf(fmtc,toggle[i].c,toggle[i].description);
+    for (i=0; toggle[i].c; ++i) {
+       if (!haskell98 || toggle[i].h98) {
+           Printf(fmtc,toggle[i].c,toggle[i].description);
+       }
+    }
 
     Printf("\nOTHER OPTIONS: (leading + or - makes no difference)\n");
     Printf(fmts,"hnum","Set heap size (cannot be changed within Hugs)");
@@ -414,8 +466,8 @@ ToDo
     Printf("\nPreprocessor    : -F");
     printString(preprocessor);
 #endif
-    Printf("\nCompatibility   : %s", haskell98 ? "Haskell 98"
-                                               : "Hugs Extensions");
+    Printf("\nCompatibility   : %s", haskell98 ? "Haskell 98 (+98)"
+                                              : "Hugs Extensions (-98)");
     Putchar('\n');
 }
 
@@ -463,6 +515,7 @@ static String local optionsToStr() {          /* convert options to string */
         PUTC(toggle[i].c);
         PUTC(' ');
     }
+    PUTS(haskell98 ? "+98 " : "-98 ");
     PUTInt('h',hpSize);  PUTC(' ');
     PUTStr('p',prompt);
     PUTStr('r',repeatStr);
@@ -543,6 +596,16 @@ String s; {                             /* return FALSE if none found.     */
             case 'h' : setHeapSize(s+1);
                        return TRUE;
 
+            case 'c' : if (heapBuilt()) {
+                          FPrintf(stderr, 
+                                  "You can't enable/disable combined"
+                                  " operation inside Hugs\n" );
+                       } else {
+                         /* don't do anything, since pre-scan of args
+                             will have got it already */
+                       }
+                       return TRUE;
+
             case 'D' : /* hack */
                 {
                     extern void setRtsFlags( int x );
@@ -553,7 +616,9 @@ String s; {                             /* return FALSE if none found.     */
             default  : if (strcmp("98",s)==0) {
                            if (heapBuilt() && ((state && !haskell98) ||
                                                (!state && haskell98))) {
-                               FPrintf(stderr,"Haskell 98 compatibility cannot be changed while the interpreter is running\n");
+                               FPrintf(stderr,
+                                       "Haskell 98 compatibility cannot be changed"
+                                       " while the interpreter is running\n");
                            } else {
                                haskell98 = state;
                            }
@@ -579,7 +644,7 @@ String s; {
 #if USE_REGISTRY
             FPrintf(stderr,"Change to heap size will not take effect until you rerun Hugs\n");
 #else
-            FPrintf(stderr,"Cannot change heap size\n");
+            FPrintf(stderr,"You cannot change heap size from inside Hugs\n");
 #endif
         } else {
             heapSize = hpSize;
@@ -657,6 +722,11 @@ static struct cmd cmds[] = {
  {":names",  NAMES},  {":info", INFO},    {":project", PROJECT},
  {":dump",   DUMP},   {":ztats", STATS},
  {":module",SETMODULE}, 
+ {":browse", BROWSE},
+#if EXPLAIN_INSTANCE_RESOLUTION
+ {":xplain", XPLAIN},
+#endif
+ {":version", PNTVER},
  {"",      EVAL},
  {0,0}
 };
@@ -679,10 +749,15 @@ static Void local menu() {
     Printf(":set                help on command line options\n");
     Printf(":names [pat]        list names currently in scope\n");
     Printf(":info <names>       describe named objects\n");
+    Printf(":browse <modules>   browse names defined in <modules>\n");
+#if EXPLAIN_INSTANCE_RESOLUTION
+    Printf(":xplain <context>   explain instance resolution for <context>\n");
+#endif
     Printf(":find <name>        edit module containing definition of name\n");
     Printf(":!command           shell escape\n");
     Printf(":cd dir             change directory\n");
     Printf(":gc                 force garbage collection\n");
+    Printf(":version            print Hugs version\n");
     Printf(":dump <name>        print STG code for named fn\n");
 #ifdef CRUDE_PROFILING
     Printf(":ztats <name>       print reduction stats\n");
@@ -704,22 +779,32 @@ static Void local forHelp() {
  * ------------------------------------------------------------------------*/
 
 struct options toggle[] = {             /* List of command line toggles    */
-    {'s', "Print no. reductions/cells after eval", &showStats},
-    {'t', "Print type after evaluation",           &addType},
-    /*ToDo??    {'f', "Terminate evaluation on first error",   &failOnError},*/
-    {'g', "Print no. cells recovered after gc",    &gcMessages},
-    {'l', "Literate modules as default",           &literateScripts},
-    {'e', "Warn about errors in literate modules", &literateErrors},
-    {'.', "Print dots to show progress",           &useDots},
-    {'q', "Print nothing to show progress",        &quiet},
-    {'w', "Always show which modules are loaded",  &listScripts},
-    {'k', "Show kind errors in full",              &kindExpert},
-    {'o', "Allow overlapping instances",           &allowOverlap},
-    {'O', "Optimise (improve?) generated code",    &optimise},
+    {'s', 1, "Print no. reductions/cells after eval", &showStats},
+    {'t', 1, "Print type after evaluation",           &addType},
+    {'g', 1, "Print no. cells recovered after gc",    &gcMessages},
+    {'l', 1, "Literate modules as default",           &literateScripts},
+    {'e', 1, "Warn about errors in literate modules", &literateErrors},
+    {'.', 1, "Print dots to show progress",           &useDots},
+    {'q', 1, "Print nothing to show progress",        &quiet},
+    {'w', 1, "Always show which modules are loaded",  &listScripts},
+    {'k', 1, "Show kind errors in full",              &kindExpert},
+    {'o', 0, "Allow overlapping instances",           &allowOverlap},
+
+
+#if DEBUG_CODE
+    {'D', 1, "Debug: show generated code",            &debugCode},
+#endif
+#if EXPLAIN_INSTANCE_RESOLUTION
+    {'x', 1, "Explain instance resolution",           &showInstRes},
+#endif
+#if MULTI_INST
+    {'m', 0, "Use multi instance resolution",         &multiInstRes},
+#endif
 #if DEBUG_CODE
-    {'D', "Debug: show generated code",            &debugCode},
+    {'D', 1, "Debug: show generated G code",          &debugCode},
 #endif
-    {0,   0,                                       0}
+    {'S', 1, "Debug: show generated SC code",         &debugSC},
+    {0,   0, 0,                                       0}
 };
 
 static Void local set() {               /* change command line options from*/
@@ -803,7 +888,8 @@ static Void local makeStackEntry ( ScriptInfo* ent, String iname )
         );
    if (!ok) {
       ERRMSG(0) 
-         "Can't file source or object+interface for module \"%s\"",
+         "Can't find source or object+interface for module \"%s\"",
+         /* "Can't find source for module \"%s\"", */
          iname
       EEND;
    }
@@ -811,9 +897,17 @@ static Void local makeStackEntry ( ScriptInfo* ent, String iname )
    if (!(sAvail || (oAvail && iAvail))) 
       internal("chase");
    /* Load objects in preference to sources if both are available */
-   fromObj = sAvail
+   /* 11 Oct 99: disable object loading in the interim.
+      Will probably only reinstate when HEP becomes available.
+   */
+   if (combined) {
+      fromObj = sAvail
                 ? (oAvail && iAvail && timeEarlier(sTime,oTime))
                 : TRUE;
+   } else {
+      fromObj = FALSE;
+   }
+
    /* ToDo: namesUpto overflow */
    ent->modName     = strCopy(iname);
    ent->details     = TRUE;
@@ -832,12 +926,12 @@ static Void local makeStackEntry ( ScriptInfo* ent, String iname )
 static Void nukeEnding( String s )
 {
     Int l = strlen(s);
-    if (l > 2 && strncmp(s+l-2,".o"  ,3)==0) s[l-2] = 0; else
-    if (l > 3 && strncmp(s+l-3,".hi" ,3)==0) s[l-3] = 0; else
-    if (l > 3 && strncmp(s+l-3,".hs" ,3)==0) s[l-3] = 0; else
-    if (l > 4 && strncmp(s+l-4,".lhs",4)==0) s[l-4] = 0; else
-    if (l > 4 && strncmp(s+l-4,".dll",4)==0) s[l-4] = 0; else
-    if (l > 4 && strncmp(s+l-4,".DLL",4)==0) s[l-4] = 0;
+    if (l > 4 && strncmp(s+l-4,".u_o" ,4)==0) s[l-4] = 0; else
+    if (l > 5 && strncmp(s+l-5,".u_hi",5)==0) s[l-5] = 0; else
+    if (l > 3 && strncmp(s+l-3,".hs"  ,3)==0) s[l-3] = 0; else
+    if (l > 4 && strncmp(s+l-4,".lhs" ,4)==0) s[l-4] = 0; else
+    if (l > 4 && strncmp(s+l-4,".dll" ,4)==0) s[l-4] = 0; else
+    if (l > 4 && strncmp(s+l-4,".DLL" ,4)==0) s[l-4] = 0;
 }
 
 static Void local addStackEntry(s)     /* Add script to list of scripts    */
@@ -872,6 +966,7 @@ String s; {                            /* to be read in ...                */
 /* Return TRUE if no imports were needed; FALSE otherwise. */
 static Bool local addScript(stacknum)   /* read single file                */
 Int stacknum; {
+   Bool didPrelude;
    static char name[FILENAME_MAX+1];
    Int len = scriptInfo[stacknum].size;
 
@@ -882,16 +977,23 @@ Int stacknum; {
 
     //   setLastEdit(name,0);
 
-   nameObj[0] = 0;
    strcpy(name, scriptInfo[stacknum].path);
    strcat(name, scriptInfo[stacknum].modName);
    if (scriptInfo[stacknum].fromSource)
       strcat(name, scriptInfo[stacknum].srcExt); else
-      strcat(name, ".hi");
+      strcat(name, ".u_hi");
 
    scriptFile = name;
 
    if (scriptInfo[stacknum].fromSource) {
+      if (lastWasObject) {
+         didPrelude = processInterfaces();
+         if (didPrelude) {
+            preludeLoaded = TRUE;
+            everybody(POSTPREL);
+         }
+      }
+      lastWasObject = FALSE;
       Printf("Reading script \"%s\":\n",name);
       needsImports = FALSE;
       parseScript(name,len);
@@ -900,6 +1002,12 @@ Int stacknum; {
       typeCheckDefns();
       compileDefns();
    } else {
+      Cell    iface;
+      List    imports;
+      ZTriple iface_info;
+      char    nameObj[FILENAME_MAX+1];
+      Int     sizeObj;
+
       Printf("Reading  iface \"%s\":\n", name);
       scriptFile = name;
       needsImports = FALSE;
@@ -910,13 +1018,21 @@ Int stacknum; {
       strcat(nameObj, DLL_ENDING);
       sizeObj = scriptInfo[stacknum].oSize;
 
-      loadInterface(name,len);
+      iface = readInterface(name,len);
+      imports = zsnd(iface); iface = zfst(iface);
+
+      if (nonNull(imports)) chase(imports);
       scriptFile = 0;
+      lastWasObject = TRUE;
+
+      iface_info = ztriple(iface, findText(nameObj), mkInt(sizeObj) );
+      ifaces_outstanding = cons(iface_info,ifaces_outstanding);
+
       if (needsImports) return FALSE;
    }
  
    scriptFile = 0;
-   preludeLoaded = TRUE;
+
    return TRUE;
 }
 
@@ -1037,7 +1153,9 @@ Int n; {                                /* loading everything after and    */
     Time timeStamp;                     /* including the first script which*/
     Long fileSize;                      /* has been either changed or added*/
     static char name[FILENAME_MAX+1];
+    Bool didPrelude;
 
+    lastWasObject = FALSE;
     ppSmStack("readscripts-begin");
 #if HUGS_FOR_WINDOWS
     SetCursor(LoadCursor(NULL, IDC_WAIT));
@@ -1078,7 +1196,7 @@ Int n; {                                /* loading everything after and    */
         strcat(name, scriptInfo[n].modName);
         if (scriptInfo[n].fromSource)
            strcat(name, scriptInfo[n].srcExt); else
-           strcat(name, ".hi");  //ToDo: should be .o
+           strcat(name, ".u_hi");  //ToDo: should be .o
         getFileInfo(name,&timeStamp, &fileSize);
         if (timeChanged(timeStamp,scriptInfo[n].lastChange)) {
            dropScriptsFrom(n-1);
@@ -1092,7 +1210,7 @@ Int n; {                                /* loading everything after and    */
     //numScripts = 0;
 
     while (numScripts < namesUpto) {
-ppSmStack ( "readscripts-loop2" );
+       ppSmStack ( "readscripts-loop2" );
 
        if (scriptInfo[numScripts].fromSource) {
 
@@ -1101,10 +1219,11 @@ ppSmStack ( "readscripts-loop2" );
           nextNumScripts = NUM_SCRIPTS; //bogus initialisation
           if (addScript(numScripts)) {
              numScripts++;
-assert(nextNumScripts==NUM_SCRIPTS);
+             assert(nextNumScripts==NUM_SCRIPTS);
           }
           else
              dropScriptsFrom(numScripts-1);
+
        } else {
       
           if (scriptInfo[numScripts].objLoaded) {
@@ -1118,25 +1237,41 @@ assert(nextNumScripts==NUM_SCRIPTS);
              nextNumScripts = NUM_SCRIPTS;
              if (addScript(numScripts)) {
                 numScripts++;
-assert(nextNumScripts==NUM_SCRIPTS);
+                assert(nextNumScripts==NUM_SCRIPTS);
              } else {
                //while (!scriptInfo[numScripts].fromSource && numScripts > 0)
                //   numScripts--;
                //if (scriptInfo[numScripts].fromSource)
                //   numScripts++;
                 numScripts = nextNumScripts;
-assert(nextNumScripts<NUM_SCRIPTS);
+                assert(nextNumScripts<NUM_SCRIPTS);
              }
           }
        }
-if (numScripts==namesUpto) ppSmStack( "readscripts-final") ;
+       if (numScripts==namesUpto) ppSmStack( "readscripts-final") ;
+    }
+
+    didPrelude = processInterfaces();
+    if (didPrelude) {
+       preludeLoaded = TRUE;
+       everybody(POSTPREL);
     }
 
-    finishInterfaces();
 
     { Int  m     = namesUpto-1;
       Text mtext = findText(scriptInfo[m].modName);
-      setCurrModule(mtext);
+
+      /* Hack to avoid starting up in PrelHugs */
+      if (mtext == findText("PrelHugs")) mtext = findText("Prelude");
+
+
+      /* Commented out till we understand what
+       * this is trying to do.
+       * Problem, you cant find a module till later.
+       */
+#if 0
+       setCurrModule(findModule(mtext)); 
+#endif
       evalModule = mtext;
     }
 
@@ -1155,7 +1290,7 @@ static Void local whatScripts() {       /* list scripts in current session */
     if (projectLoaded)
         Printf(" (project: %s)",currProject);
     for (i=0; i<numScripts; ++i)
-        Printf("\n%s",scriptInfo[i].modName);
+      Printf("\n%s%s",scriptInfo[i].path, scriptInfo[i].modName);
     Putchar('\n');
 }
 
@@ -1254,8 +1389,9 @@ static Void local evaluator() {        /* evaluate expr and print value    */
                                        /* allocated during evaluation      */
     parseExp();
     checkExp();
-    defaultDefns = evalDefaults;
+    defaultDefns = combined ? stdDefaults : evalDefaults;
     type         = typeCheckExp(TRUE);
+
     if (isPolyType(type)) {
         ks = polySigOf(type);
         bd = monotypeOf(type);
@@ -1276,8 +1412,8 @@ static Void local evaluator() {        /* evaluate expr and print value    */
 #endif
 
 #if 1
-    if (typeMatches(type,ap(typeIO,typeUnit))) {
-        inputExpr = ap(nameRunIO,inputExpr);
+    if (isProgType(ks,bd)) {
+        inputExpr = ap(nameRunIO_toplevel,inputExpr);
         evalExp();
         Putchar('\n');
     } else {
@@ -1289,9 +1425,9 @@ static Void local evaluator() {        /* evaluate expr and print value    */
             ERRTEXT   "\n"
             EEND;
         }
-        inputExpr = ap2(findName(findText("show")),d,inputExpr);
-        inputExpr = ap(findName(findText("putStr")), inputExpr);
-        inputExpr = ap(nameRunIO, inputExpr);
+        inputExpr = ap2(nameShow,           d,inputExpr);
+        inputExpr = ap (namePutStr,         inputExpr);
+        inputExpr = ap (nameRunIO_toplevel, inputExpr);
 
         evalExp(); printf("\n");
         if (addType) {
@@ -1300,14 +1436,15 @@ static Void local evaluator() {        /* evaluate expr and print value    */
             Putchar('\n');
         }
     }
-#endif
 
-#if 0
+#else
+
    printf ( "result type is " );
    printType ( stdout, type );
    printf ( "\n" );
    evalExp();
    printf ( "\n" );
+
 #endif
 
 }
@@ -1349,6 +1486,83 @@ static Void local showtype() {         /* print type of expression (if any)*/
     Putchar('\n');
 }
 
+
+static Void local browseit(mod,t,all)
+Module mod; 
+String t;
+Bool all; {
+    if (nonNull(mod)) {
+       Cell cs;
+       if (nonNull(t))
+           Printf("module %s where\n",textToStr(module(mod).text));
+       for (cs = module(mod).names; nonNull(cs); cs=tl(cs)) {
+           Name nm = hd(cs);
+           /* only look at things defined in this module,
+              unless `all' flag is set */
+           if (all || name(nm).mod == mod) {
+               /* unwanted artifacts, like lambda lifted values,
+                  are in the list of names, but have no types */
+               if (nonNull(name(nm).type)) {
+                   printExp(stdout,nm);
+                   Printf(" :: ");
+                   printType(stdout,name(nm).type);
+                   if (isCfun(nm)) {
+                       Printf("  -- data constructor");
+                   } else if (isMfun(nm)) {
+                       Printf("  -- class member");
+                   } else if (isSfun(nm)) {
+                       Printf("  -- selector function");
+                   }
+                   Printf("\n");
+               }
+           }
+       }
+    } else {
+      if (isNull(mod)) {
+       Printf("Unknown module %s\n",t);
+      }
+    }
+}
+
+static Void local browse() {            /* browse modules                  */
+    Int    count = 0;                   /* or give menu of commands        */
+    String s;
+    Bool all = FALSE;
+
+    setCurrModule(findEvalModule());
+    startNewScript(0);                  /* for recovery of storage         */
+    for (; (s=readFilename())!=0; count++)
+       if (strcmp(s,"all") == 0) {
+           all = TRUE;
+           --count;
+       } else
+           browseit(findModule(findText(s)),s,all);
+    if (count == 0) {
+       browseit(findEvalModule(),NULL,all);
+    }
+}
+
+#if EXPLAIN_INSTANCE_RESOLUTION
+static Void local xplain() {         /* print type of expression (if any)*/
+    Cell d;
+    Bool sir = showInstRes;
+
+    setCurrModule(findEvalModule());
+    startNewScript(0);                 /* Enables recovery of storage      */
+                                      /* allocated during evaluation      */
+    parseContext();
+    checkContext();
+    showInstRes = TRUE;
+    d = provePred(NIL,NIL,hd(inputContext));
+    if (isNull(d)) {
+       fprintf(stdout, "not Sat\n");
+    } else {
+       fprintf(stdout, "Sat\n");
+    }
+    showInstRes = sir;
+}
+#endif
+
 /* --------------------------------------------------------------------------
  * Enhanced help system:  print current list of scripts or give information
  * about an object.
@@ -1441,7 +1655,6 @@ static Void local dumpStg( void ) {       /* print STG stuff                 */
 
         if (isNull(n) && whatIs(v)==STGVAR) {
            Printf ( "\n{- `%s' has no nametable entry -}\n", s );
-           Printf ( "{- stgSize of body is %d -}\n\n", stgSize(stgVarBody(v)));
            printStg(stderr, v );
         } else
         if (isNull(n)) {
@@ -1454,8 +1667,6 @@ static Void local dumpStg( void ) {       /* print STG stuff                 */
            Printf ( "Doesn't have a STG tree: %s\n", s );
         } else {
            Printf ( "\n{- stgVar of `%s' is id%d -}\n", s, -name(n).stgVar);
-           Printf ( "{- stgSize of body is %d -}\n\n", 
-                    stgSize(stgVarBody(name(n).stgVar)));
            printStg(stderr, name(n).stgVar);
         }
     }
@@ -1481,7 +1692,6 @@ Text t; {
     Tycon  tc  = findTycon(t);
     Class  cl  = findClass(t);
     Name   nm  = findName(t);
-    Module mod = findModule(t);
 
     if (nonNull(tc)) {                  /* as a type constructor           */
         Type t = tc;
@@ -1570,11 +1780,25 @@ Text t; {
             Printf(" => ");
         }
         printPred(stdout,cclass(cl).head);
+
+       if (nonNull(cclass(cl).fds)) {
+           List   fds = cclass(cl).fds;
+           String pre = " | ";
+           for (; nonNull(fds); fds=tl(fds)) {
+               Printf(pre);
+               printFD(stdout,hd(fds));
+               pre = ", ";
+           }
+       }
+
         if (nonNull(cclass(cl).members)) {
             List ms = cclass(cl).members;
             Printf(" where");
             do {
-                Type t = monotypeOf(name(hd(ms)).type);
+               Type t = name(hd(ms)).type;
+                if (isPolyType(t)) {
+                   t = monotypeOf(t);
+               }
                 Printf("\n  ");
                 printExp(stdout,hd(ms));
                 Printf(" :: ");
@@ -1607,7 +1831,7 @@ Text t; {
         } else {
             Printf("<unknown type>");
         }
-
+printf("\n");print(name(nm).type,10);printf("\n");
         if (isCfun(nm)) {
             Printf("  -- data constructor");
         } else if (isMfun(nm)) {
@@ -1618,32 +1842,8 @@ Text t; {
         Printf("\n\n");
     }
 
-    if (nonNull(mod)) {                 /* as a module                     */
-        List t;
-        Printf("-- module\n");
-
-        Printf("\n-- values\n");
-        for (t=module(mod).names; nonNull(t); t=tl(t)) {
-           Name nm = hd(t);
-           Printf ( "%s ", textToStr(name(nm).text));
-        }
-
-        Printf("\n\n-- type constructors\n");
-        for (t=module(mod).tycons; nonNull(t); t=tl(t)) {
-           Tycon tc = hd(t);
-           Printf ( "%s ", textToStr(tycon(tc).text));
-        }
-
-        Printf("\n\n-- classes\n");
-        for (t=module(mod).classes; nonNull(t); t=tl(t)) {
-           Class cl = hd(t);
-           Printf ( "%s ", textToStr(cclass(cl).text));
-        }
-
-        Printf("\n\n");
-    }
 
-    if (isNull(tc) && isNull(cl) && isNull(nm) && isNull(mod)) {
+    if (isNull(tc) && isNull(cl) && isNull(nm)) {
         Printf("Unknown reference `%s'\n",textToStr(t));
     }
 }
@@ -1771,6 +1971,9 @@ String argv[]; {
         forHelp();
     }
 
+    /* initialize calls startupHaskell, which trashes our signal handlers */
+    breakOn(TRUE);
+
     for (;;) {
         Command cmd;
         everybody(RESET);               /* reset to sensible initial state */
@@ -1807,6 +2010,12 @@ String argv[]; {
                           break;
             case TYPEOF : showtype();
                           break;
+           case BROWSE : browse();
+                         break;
+#if EXPLAIN_INSTANCE_RESOLUTION
+           case XPLAIN : xplain();
+                         break;
+#endif
             case NAMES  : listNames();
                           break;
             case HELP   : menu();
@@ -1827,6 +2036,9 @@ String argv[]; {
                           break;
             case INFO   : info();
                           break;
+           case PNTVER: Printf("-- Hugs Version %s\n",
+                                HUGS_VERSION);
+                         break;
             case DUMP   : dumpStg();
                           break;
             case QUIT   : return;
@@ -1861,7 +2073,12 @@ static Int    charCount;
 Void setGoal(what, t)                  /* Set goal for what to be t        */
 String what;
 Target t; {
-    if (quiet) return;
+    if (quiet)
+      return;
+#if EXPLAIN_INSTANCE_RESOLUTION
+    if (showInstRes)
+      return;
+#endif
     currTarget = (t?t:1);
     aiming     = TRUE;
     if (useDots) {
@@ -1877,7 +2094,12 @@ Target t; {
 
 Void soFar(t)                          /* Indicate progress towards goal   */
 Target t; {                            /* has now reached t                */
-    if (quiet) return;
+    if (quiet)
+      return;
+#if EXPLAIN_INSTANCE_RESOLUTION
+    if (showInstRes)
+      return;
+#endif
     if (useDots) {
         Int newPos = (Int)((maxPos * ((long)t))/currTarget);
 
@@ -1895,7 +2117,12 @@ Target t; {                            /* has now reached t                */
 }
 
 Void done() {                          /* Goal has now been achieved       */
-    if (quiet) return;
+    if (quiet)
+      return;
+#if EXPLAIN_INSTANCE_RESOLUTION
+    if (showInstRes)
+      return;
+#endif
     if (useDots) {
         while (maxPos>currPos++)
             Putchar('.');
@@ -2042,7 +2269,7 @@ String s; {
 
 /* ----------------------------------------------------------------------- */
 
-#define BufferSize 5000               /* size of redirected output buffer  */
+#define BufferSize 10000              /* size of redirected output buffer  */
 
 typedef struct _HugsStream {
     char buffer[BufferSize];          /* buffer for redirected output      */
@@ -2195,8 +2422,11 @@ FILE* fp; {
 
 Void everybody(what)            /* send command `what' to each component of*/
 Int what; {                     /* system to respond as appropriate ...    */
+#if 0
+  fprintf ( stderr, "EVERYBODY %d\n", what );
+#endif
     machdep(what);              /* The order of calling each component is  */
-    storage(what);              /* important for the INSTALL command       */
+    storage(what);              /* important for the PREPREL command       */
     substitution(what);
     input(what);
     translateControl(what);
@@ -2206,7 +2436,6 @@ Int what; {                     /* system to respond as appropriate ...    */
     typeChecker(what);
     compiler(what);   
     codegen(what);
-    optimiser(what);
 }
 
 /* --------------------------------------------------------------------------