[project @ 2000-03-23 14:54:20 by sewardj]
[ghc-hetmet.git] / ghc / interpreter / output.c
1
2 /* --------------------------------------------------------------------------
3  * Unparse expressions and types - for use in error messages, type checker
4  * and for debugging.
5  *
6  * The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
7  * Yale Haskell Group, and the Oregon Graduate Institute of Science and
8  * Technology, 1994-1999, All rights reserved.  It is distributed as
9  * free software under the license in the file "License", which is
10  * included in the distribution.
11  *
12  * $RCSfile: output.c,v $
13  * $Revision: 1.17 $
14  * $Date: 2000/03/23 14:54:21 $
15  * ------------------------------------------------------------------------*/
16
17 #include "hugsbasictypes.h"
18 #include "storage.h"
19 #include "connect.h"
20 #include "errors.h"
21 #include <ctype.h>
22
23 #define DEPTH_LIMIT     15
24
25 /* --------------------------------------------------------------------------
26  * Local function prototypes:
27  * ------------------------------------------------------------------------*/
28
29 static Void local put            ( Int,Cell );
30 static Void local putFlds        ( Cell,List );
31 static Void local putComp        ( Cell,List );
32 static Void local putQual        ( Cell );
33 static Bool local isDictVal      ( Cell );
34 static Cell local maySkipDict    ( Cell );
35 static Void local putAp          ( Int,Cell );
36 static Void local putOverInfix   ( Int,Text,Syntax,Cell );
37 static Void local putInfix       ( Int,Text,Syntax,Cell,Cell );
38 static Void local putSimpleAp    ( Cell,Int );
39 static Void local putTuple       ( Int,Cell );
40 static Int  local unusedTups     ( Int,Cell );
41 static Void local unlexOp        ( Text );
42
43 static Void local putSigType     ( Cell );
44 static Void local putContext     ( List,List,Int );
45 static Void local putPred        ( Cell,Int );
46 static Void local putType        ( Cell,Int,Int );
47 static Void local putTyVar       ( Int );
48 static Bool local putTupleType   ( Cell,Int );
49 static Void local putApType      ( Type,Int,Int );
50
51 static Void local putKind        ( Kind );
52 static Void local putKinds       ( Kinds );
53
54
55 /* --------------------------------------------------------------------------
56  * Basic output routines:
57  * ------------------------------------------------------------------------*/
58
59 FILE *outputStream;                    /* current output stream            */
60 Int  outColumn = 0;                    /* current output column number     */
61                                                                        
62 #define OPEN(b)    if (b) putChr('(');                                 
63 #define CLOSE(b)   if (b) putChr(')');                                 
64                                                                        
65 Void putChr(c)                         /* print single character           */
66 Int c; {                                                               
67     Putc(c,outputStream);                                              
68     outColumn++;                                                       
69 }                                                                      
70                                                                        
71 Void putStr(s)                        /* print string                     */
72 String s; {                                                            
73     for (; *s; s++) {                                                  
74         Putc(*s,outputStream);                                         
75         outColumn++;                                                   
76     }                                                                  
77 }                                                                      
78                                                                        
79 Void putInt(n)                        /* print integer                    */
80 Int n; {
81     static char intBuf[16];
82     sprintf(intBuf,"%d",n);
83     putStr(intBuf);
84 }
85
86 Void putPtr(p)                        /* print pointer                    */
87 Ptr p; {
88     static char intBuf[16];
89     sprintf(intBuf,"%p",p);
90     putStr(intBuf);
91 }
92
93 /* --------------------------------------------------------------------------
94  * Precedence values (See Haskell 1.3 report, p.12):
95  * ------------------------------------------------------------------------*/
96
97 #define ALWAYS      FUN_PREC           /* Always use parens (unless atomic)*/
98                                        /* User defined operators have prec */
99                                        /* in the range MIN_PREC..MAX_PREC  */
100 #define ARROW_PREC  MAX_PREC           /* for printing -> in type exprs    */
101 #define COCO_PREC   (MIN_PREC-1)       /* :: is left assoc, low precedence */
102 #define COND_PREC   (MIN_PREC-2)       /* conditional expressions          */
103 #define WHERE_PREC  (MIN_PREC-3)       /* where expressions                */
104 #define LAM_PREC    (MIN_PREC-4)       /* lambda abstraction               */
105 #define NEVER       LAM_PREC           /* Never use parentheses            */
106
107
108 /* --------------------------------------------------------------------------
109  * Print an expression (used to display context of type errors):
110  * ------------------------------------------------------------------------*/
111
112 static Int putDepth = 0;               /* limits depth of printing DBG     */
113
114 static Void local put(d,e)             /* print expression e in context of */
115 Int  d;                                /* operator of precedence d         */
116 Cell e; {
117     List xs;
118
119     if (putDepth>DEPTH_LIMIT) {
120         putStr("...");
121         return;
122     }
123     else
124         putDepth++;
125
126     switch (whatIs(e)) {
127         case FINLIST    : putChr('[');
128                           xs = snd(e);
129                           if (nonNull(xs)) {
130                               put(NEVER,hd(xs));
131                               while (nonNull(xs=tl(xs))) {
132                                   putChr(',');
133                                   put(NEVER,hd(xs));
134                               }
135                           }
136                           putChr(']');
137                           break;
138
139         case AP         : putAp(d,e);
140                           break;
141
142         case NAME       : unlexVar(name(e).text);
143                           break;
144
145         case VARIDCELL  :
146         case VAROPCELL  :
147         case DICTVAR    :
148         case CONIDCELL  :
149         case CONOPCELL  : unlexVar(textOf(e));
150                           break;
151
152 #if IPARAM
153         case IPVAR      : putChr('?');
154                           unlexVar(textOf(e));
155                           break;
156
157         case WITHEXP    : OPEN(d>WHERE_PREC);
158                           putStr("dlet {...} in ");
159                           put(WHERE_PREC+1,fst(snd(e)));
160                           CLOSE(d>WHERE_PREC);
161                           break;
162 #endif
163
164 #if TREX
165         case RECSEL     : putChr('#');
166                           unlexVar(extText(snd(e)));
167                           break;
168 #endif
169
170         case FREECELL   : putStr("{free!}");
171                           break;
172
173         case TUPLE      : putTuple(tupleOf(e),e);
174                           break;
175
176         case WILDCARD   : putChr('_');
177                           break;
178
179         case ASPAT      : put(NEVER,fst(snd(e)));
180                           putChr('@');
181                           put(ALWAYS,snd(snd(e)));
182                           break;
183
184         case LAZYPAT    : putChr('~');
185                           put(ALWAYS,snd(e));
186                           break;
187
188         case DOCOMP     : putStr("do {...}");
189                           break;
190
191         case COMP       : putComp(fst(snd(e)),snd(snd(e)));
192                           break;
193
194         case MONADCOMP  : putComp(fst(snd(snd(e))),snd(snd(snd(e))));
195                           break;
196
197         case CHARCELL   : unlexCharConst(charOf(e));
198                           break;
199
200         case INTCELL    : {   Int i = intOf(e);
201                               if (i<0 && d>=UMINUS_PREC) putChr('(');
202                               putInt(i);
203                               if (i<0 && d>=UMINUS_PREC) putChr(')');
204                           }
205                           break;
206
207         case FLOATCELL  : {   Float f = floatOf(e);
208                               if (f<0 && d>=UMINUS_PREC) putChr('(');
209                               putStr(floatToString(e));
210                               if (f<0 && d>=UMINUS_PREC) putChr(')');
211                           }
212                           break;
213
214         case STRCELL    : unlexStrConst(textOf(e));
215                           break;
216
217         case LETREC     : OPEN(d>WHERE_PREC);
218 #if 0
219                           putStr("let {");
220                           put(NEVER,fst(snd(e)));
221                           putStr("} in ");
222 #else
223                           putStr("let {...} in ");
224 #endif
225                           put(WHERE_PREC+1,snd(snd(e)));
226                           CLOSE(d>WHERE_PREC);
227                           break;
228
229         case COND       : OPEN(d>COND_PREC);
230                           putStr("if ");
231                           put(COND_PREC+1,fst3(snd(e)));
232                           putStr(" then ");
233                           put(COND_PREC+1,snd3(snd(e)));
234                           putStr(" else ");
235                           put(COND_PREC+1,thd3(snd(e)));
236                           CLOSE(d>COND_PREC);
237                           break;
238
239         case LAMBDA     : xs = fst(snd(e));
240                           if (whatIs(xs)==BIGLAM)
241                               xs = snd(snd(xs));
242                           while (nonNull(xs) && isDictVal(hd(xs)))
243                               xs = tl(xs);
244                           if (isNull(xs)) {
245                               put(d,snd(snd(snd(e))));
246                               break;
247                           }
248                           OPEN(d>LAM_PREC);
249                           putChr('\\');
250                           if (nonNull(xs)) {
251                               put(ALWAYS,hd(xs));
252                               while (nonNull(xs=tl(xs))) {
253                                   putChr(' ');
254                                   put(ALWAYS,hd(xs));
255                               }
256                           }
257                           putStr(" -> ");
258                           put(LAM_PREC,snd(snd(snd(e))));
259                           CLOSE(d>LAM_PREC);
260                           break;
261
262         case ESIGN      : OPEN(d>COCO_PREC);
263                           put(COCO_PREC,fst(snd(e)));
264                           putStr(" :: ");
265                           putSigType(snd(snd(e)));
266                           CLOSE(d>COCO_PREC);
267                           break;
268
269         case BIGLAM     : put(d,snd(snd(e)));
270                           break;
271
272         case CASE       : putStr("case ");
273                           put(NEVER,fst(snd(e)));
274 #if 0
275                           putStr(" of {");
276                           put(NEVER,snd(snd(e)));
277                           putChr('}');
278 #else
279                           putStr(" of {...}");
280 #endif
281                           break;
282
283         case CONFLDS    : putFlds(fst(snd(e)),snd(snd(e)));
284                           break;
285
286         case UPDFLDS    : putFlds(fst3(snd(e)),thd3(snd(e)));
287                           break;
288
289         default         : /*internal("put");*/
290                           putChr('$');
291                           putInt(e);
292                           break;
293     }
294     putDepth--;
295 }
296
297 static Void local putFlds(exp,fs)       /* Output exp using labelled fields*/
298 Cell exp;
299 List fs; {
300     put(ALWAYS,exp);
301     putChr('{');
302     for (; nonNull(fs); fs=tl(fs)) {
303         Cell v = hd(fs);
304         if (isVar(v))
305             put(NEVER,v);
306         else {
307             Cell f = fst(v);
308             Cell e = snd(v);
309             Text t = isName(f) ? name(f).text :
310                      isVar(f)  ? textOf(f)    : inventText();
311             Text s = isName(e) ? name(e).text :
312                      isVar(e)  ? textOf(e)    : inventText();
313
314             put(NEVER,f);
315             if (haskell98 || s!=t) {
316                 putStr(" = ");
317                 put(NEVER,e);
318             }
319         }
320         if (nonNull(tl(fs)))
321             putStr(", ");
322     }
323     putChr('}');
324 }
325
326 static Void local putComp(e,qs)         /* print comprehension             */
327 Cell e;
328 List qs; {
329     putStr("[ ");
330     put(NEVER,e);
331     if (nonNull(qs)) {
332         putStr(" | ");
333         putQual(hd(qs));
334         while (nonNull(qs=tl(qs))) {
335             putStr(", ");
336             putQual(hd(qs));
337         }
338     }
339     putStr(" ]");
340 }
341
342 static Void local putQual(q)            /* print list comp qualifier       */
343 Cell q; {
344     switch (whatIs(q)) {
345         case BOOLQUAL : put(NEVER,snd(q));
346                         return;
347
348         case QWHERE   : putStr("let {...}");
349                         return;
350
351         case FROMQUAL : put(ALWAYS,fst(snd(q)));
352                         putStr("<-");
353                         put(NEVER,snd(snd(q)));
354                         return;
355     }
356 }
357
358 static Bool local isDictVal(e)          /* Look for dictionary value       */
359 Cell e; {
360 #if 0   /* was !DEBUG_CODE -- is it necessary? */
361     Cell h = getHead(e);
362     switch (whatIs(h)) {
363         case DICTVAR : return TRUE;
364         case NAME    : return isDfun(h);
365     }
366 #endif
367     return FALSE;
368 }
369
370 static Cell local maySkipDict(e)        /* descend function application,   */
371 Cell e; {                               /* ignoring dict aps               */
372     while (isAp(e) && isDictVal(arg(e)))
373         e = fun(e);
374     return e;
375 }
376
377 static Void local putAp(d,e)            /* print application (args>=1)     */
378 Int  d;
379 Cell e; {
380     Cell   h;
381     Text   t = 0;                       /* bogus init to keep gcc -O happy */
382     Syntax sy;
383     Int    args = 0;
384
385     for (h=e; isAp(h); h=fun(h))        /* find head of expression, looking*/
386         if (!isDictVal(arg(h)))         /* for dictionary arguments        */
387             args++;
388
389     if (args==0) {                      /* Special case when *all* args    */
390         put(d,h);                       /* are dictionary values           */
391         return;
392     }
393
394     switch (whatIs(h)) {
395         case ADDPAT     : if (args==1)
396                               putInfix(d,textPlus,syntaxOf(namePlus),
397                                          arg(e),mkInt(intValOf(fun(e))));
398                           else
399                               putStr("ADDPAT");
400                           return;
401
402         case TUPLE      : OPEN(args>tupleOf(h) && d>=FUN_PREC);
403                           putTuple(tupleOf(h),e);
404                           CLOSE(args>tupleOf(h) && d>=FUN_PREC);
405                           return;
406
407         case NAME       : if (args==1 &&
408                               ((h==nameFromInt     && isInt(arg(e)))    ||
409                                (h==nameFromDouble  && isFloat(arg(e))))) {
410                               put(d,arg(e));
411                               return;
412                           }
413                           t  = name(h).text;
414                           sy = syntaxOf(h);
415                           break;
416
417         case VARIDCELL  :
418         case VAROPCELL  :
419         case DICTVAR    :
420         case CONIDCELL  :
421         case CONOPCELL  : sy = defaultSyntax(t = textOf(h));
422                           break;
423
424 #if TREX
425         case EXT        : if (args==2) {
426                               String punc = "(";
427                               do {
428                                   putStr(punc);
429                                   punc = ", ";
430                                   putStr(textToStr(extText(h)));
431                                   putStr("=");
432                                   put(NEVER,extField(e));
433                                   args = 0;
434                                   e    = extRow(e);
435                                   for (h=e; isAp(h); h=fun(h))
436                                       if (!isDictVal(arg(h)))
437                                           args++;
438                               } while (isExt(h) && args==2);
439                               if (e!=nameNoRec) {
440                                   putStr(" | ");
441                                   put(NEVER,e);
442                               }
443                               putChr(')');
444                               return;
445                           }
446                           else if (args<2)
447                               internal("putExt");
448                           else
449                               args-=2;
450                           break;
451 #endif
452
453         default         : sy = APPLIC;
454                           break;
455     }
456
457     e = maySkipDict(e);
458
459     if (sy==APPLIC) {                   /* print simple application        */
460         OPEN(d>=FUN_PREC);
461         putSimpleAp(e,args);
462         CLOSE(d>=FUN_PREC);
463         return;
464     }
465     else if (args==1) {                 /* print section of the form (e+)  */
466         putChr('(');
467         put(FUN_PREC-1,arg(e));
468         putChr(' ');
469         unlexOp(t);
470         putChr(')');
471     }
472     else if (args==2)                  /* infix expr of the form e1 + e2   */
473         putInfix(d,t,sy,arg(maySkipDict(fun(e))),arg(e));
474     else {                             /* o/w (e1 + e2) e3 ... en   (n>=3) */
475         OPEN(d>=FUN_PREC);
476         putOverInfix(args,t,sy,e);
477         CLOSE(d>=FUN_PREC);
478     }
479 }
480
481 static Void local putOverInfix(args,t,sy,e)
482 Int    args;                           /* infix applied to >= 3 arguments  */
483 Text   t;
484 Syntax sy;
485 Cell   e; {
486     if (args>2) {
487         putOverInfix(args-1,t,sy,maySkipDict(fun(e)));
488         putChr(' ');
489         put(FUN_PREC,arg(e));
490     }
491     else
492         putInfix(ALWAYS,t,sy,arg(maySkipDict(fun(e))),arg(e));
493 }
494
495 static Void local putInfix(d,t,sy,e,f)  /* print infix expression          */
496 Int    d;
497 Text   t;                               /* Infix operator symbol           */
498 Syntax sy;                              /* with name t, syntax s           */
499 Cell   e, f; {                          /* Left and right operands         */
500     Syntax a = assocOf(sy);
501     Int    p = precOf(sy);
502
503     OPEN(d>p);
504     put((a==LEFT_ASS ? p : 1+p), e);
505     putChr(' ');
506     unlexOp(t);
507     putChr(' ');
508     put((a==RIGHT_ASS ? p : 1+p), f);
509     CLOSE(d>p);
510 }
511
512 static Void local putSimpleAp(e,n)      /* print application e0 e1 ... en  */
513 Cell e; 
514 Int  n; {
515     if (n>0) {
516         putSimpleAp(maySkipDict(fun(e)),n-1);
517         putChr(' ');
518         put(FUN_PREC,arg(e));
519     }
520     else
521         put(FUN_PREC,e);
522 }
523
524 static Void local putTuple(ts,e)        /* Print tuple expression, allowing*/
525 Int  ts;                                /* for possibility of either too   */
526 Cell e; {                               /* few or too many args to constr  */
527     Int i;
528     putChr('(');
529     if ((i=unusedTups(ts,e))>0) {
530         while (--i>0)
531             putChr(',');
532         putChr(')');
533     }
534 }
535
536 static Int local unusedTups(ts,e)       /* print first part of tuple expr  */
537 Int  ts;                                /* returning number of constructor */
538 Cell e; {                               /* args not yet printed ...        */
539     if (isAp(e)) {
540         if ((ts=unusedTups(ts,fun(e))-1)>=0) {
541             put(NEVER,arg(e));
542             putChr(ts>0?',':')');
543         }
544         else {
545             putChr(' ');
546             put(FUN_PREC,arg(e));
547         }
548     }
549     return ts;
550 }
551
552 Void unlexVarStr(s)
553 String s; {
554     if ((isascii((int)(s[0])) && isalpha((int)(s[0]))) 
555         || s[0]=='_' || s[0]=='[' || s[0]=='('
556         || s[0]=='$'
557         || (s[0]==':' && s[1]=='D')
558        )
559         putStr(s);
560     else {
561         putChr('(');
562         putStr(s);
563         putChr(')');
564     }
565 }
566
567 Void unlexVar(t)                       /* print text as a variable name    */
568 Text t; {                              /* operator symbols must be enclosed*/
569     unlexVarStr(textToStr(t));         /* in parentheses... except [] ...  */
570 }
571
572 static Void local unlexOp(t)           /* print text as operator name      */
573 Text t; {                              /* alpha numeric symbols must be    */
574     String s = textToStr(t);           /* enclosed by backquotes           */
575
576     if (isascii((int)(s[0])) && isalpha((int)(s[0]))) {
577         putChr('`');
578         putStr(s);
579         putChr('`');
580     }
581     else
582         putStr(s);
583 }
584
585 Void unlexCharConst(c)
586 Cell c; {
587     putChr('\'');
588     putStr(unlexChar(c,'\''));
589     putChr('\'');
590 }
591
592 Void unlexStrConst(t)
593 Text t; {
594     String s            = textToStr(t);
595     static Char SO      = 14;          /* ASCII code for '\SO'             */
596     Bool   lastWasSO    = FALSE;
597     Bool   lastWasDigit = FALSE;
598     Bool   lastWasEsc   = FALSE;
599
600     putChr('\"');
601     for (; *s; s++) {
602         String ch = unlexChar(*s,'\"');
603         Char   c  = ' ';
604
605         if ((lastWasSO && *ch=='H') ||
606                 (lastWasEsc && lastWasDigit 
607                  && isascii((int)(*ch)) && isdigit((int)(*ch))))
608             putStr("\\&");
609
610         lastWasEsc   = (*ch=='\\');
611         lastWasSO    = (*s==SO);
612         for (; *ch; c = *ch++)
613             putChr(*ch);
614         lastWasDigit = (isascii(c) && isdigit(c));
615     }
616     putChr('\"');
617 }
618
619 /* --------------------------------------------------------------------------
620  * Print type expression:
621  * ------------------------------------------------------------------------*/
622
623 static Void local putSigType(t)         /* print (possibly) generic type   */
624 Cell t; {
625     Int fr = 0;
626     if (isPolyType(t)) {
627         Kinds ks = polySigOf(t);
628         for (; isAp(ks); ks=tl(ks))
629             fr++;
630         t = monotypeOf(t);
631     }
632
633     putType(t,NEVER,fr);                /* Finally, print rest of type ... */
634 }
635
636 static Void local putContext(ps,qs,fr)  /* print context list              */
637 List ps;
638 List qs;
639 Int  fr; {
640     Int len = length(ps) + length(qs);
641     Int c   = len;
642 #if IPARAM
643     Bool useParens = len!=1 || isIP(fun(hd(ps)));
644 #else
645     Bool useParens = len!=1;
646 #endif
647     if (useParens)
648         putChr('(');
649     for (; nonNull(ps); ps=tl(ps)) {
650         putPred(hd(ps),fr);
651         if (--c > 0) {
652             putStr(", ");
653         }
654     }
655     for (; nonNull(qs); qs=tl(qs)) {
656         putPred(hd(qs),fr);
657         if (--c > 0) {
658             putStr(", ");
659         }
660     }
661     if (useParens)
662         putChr(')');
663 }
664
665 static Void local putPred(pi,fr)        /* Output predicate                */
666 Cell pi;
667 Int  fr; {
668     if (isAp(pi)) {
669 #if TREX
670         if (isExt(fun(pi))) {
671             putType(arg(pi),ALWAYS,fr);
672             putChr('\\');
673             putStr(textToStr(extText(fun(pi))));
674             return;
675         }
676 #endif
677 #if IPARAM
678         if (whatIs(fun(pi)) == IPCELL) {
679             putChr('?');
680             putPred(fun(pi),fr);
681             putStr(" :: ");
682             putType(arg(pi),NEVER,fr);
683             return;
684         }
685 #endif
686         putPred(fun(pi),fr);
687         putChr(' ');
688         putType(arg(pi),ALWAYS,fr);
689     }
690     else if (isClass(pi))
691         putStr(textToStr(cclass(pi).text));
692     else if (isCon(pi))
693         putStr(textToStr(textOf(pi)));
694 #if IPARAM
695     else if (whatIs(pi) == IPCELL)
696         unlexVar(textOf(pi));
697 #endif
698     else
699         putStr("<unknownPredicate>");
700 }
701
702 static Void local putType(t,prec,fr)    /* print nongeneric type expression*/
703 Cell t;
704 Int  prec;
705 Int  fr; {
706     switch(whatIs(t)) {
707         case TYCON     : putStr(textToStr(tycon(t).text));
708                          break;
709
710         case TUPLE     : {   Int n = tupleOf(t);
711                              putChr('(');
712                              while (--n > 0)
713                                  putChr(',');
714                              putChr(')');
715                          }
716                          break;
717
718         case POLYTYPE  : {   Kinds ks = polySigOf(t);
719                              OPEN(prec>=ARROW_PREC);
720                              putStr("forall ");
721                              for (; isAp(ks); ks=tl(ks)) {
722                                  putTyVar(fr++);
723                                  if (isAp(tl(ks)))
724                                      putChr(' ');
725                              }
726                              putStr(". ");
727                              putType(monotypeOf(t),NEVER,fr);
728                              CLOSE(prec>=ARROW_PREC);
729                          }
730                          break;
731
732         case CDICTS    :
733         case QUAL      : OPEN(prec>=ARROW_PREC);
734                          if (whatIs(snd(snd(t)))==CDICTS) {
735                              putContext(fst(snd(t)),fst(snd(snd(snd(t)))),fr);
736                              putStr(" => ");
737                              putType(snd(snd(snd(snd(t)))),NEVER,fr);
738                          } else {
739                              putContext(fst(snd(t)),NIL,fr);
740                              putStr(" => ");
741                              putType(snd(snd(t)),NEVER,fr);
742                          }
743                          CLOSE(prec>=ARROW_PREC);
744                          break;
745
746         case EXIST     :
747         case RANK2     : putType(snd(snd(t)),prec,fr);
748                          break;
749
750         case OFFSET    : putTyVar(offsetOf(t));
751                          break;
752
753         case VARIDCELL :
754         case VAROPCELL : putChr('_');
755                          unlexVar(textOf(t));
756                          break;
757
758         case INTCELL   : putChr('_');
759                          putInt(intOf(t));
760                          break;
761
762         case AP       : {   Cell typeHead = getHead(t);
763                             Bool brackets = (argCount!=0 && prec>=ALWAYS);
764                             Int  args    = argCount;
765
766                             if (typeHead==typeList) {
767                                 if (argCount==1) {
768                                     putChr('[');
769                                     putType(arg(t),NEVER,fr);
770                                     putChr(']');
771                                     return;
772                                 }
773                             }
774                             else if (typeHead==typeArrow) {
775                                 if (argCount==2) {
776                                     OPEN(prec>=ARROW_PREC);
777                                     putType(arg(fun(t)),ARROW_PREC,fr);
778                                     putStr(" -> ");
779                                     putType(arg(t),NEVER,fr);
780                                     CLOSE(prec>=ARROW_PREC);
781                                     return;
782                                 }
783 #if 0
784                                 else if (argCount==1) {
785                                     putChr('(');
786                                     putType(arg(t),ARROW_PREC,fr);
787                                     putStr("->)");
788                                     return;
789                                 }
790 #endif
791                             }
792                             else if (isTuple(typeHead)) {
793                                 if (argCount==tupleOf(typeHead)) {
794                                     putChr('(');
795                                     putTupleType(t,fr);
796                                     putChr(')');
797                                     return;
798                                 }
799                             }
800 #if TREX
801                             else if (isExt(typeHead)) {
802                                 if (args==2) {
803                                     String punc = "(";
804                                     do {
805                                         putStr(punc);
806                                         punc = ", ";
807                                         putStr(textToStr(extText(typeHead)));
808                                         putStr(" :: ");
809                                         putType(extField(t),NEVER,fr);
810                                         t        = extRow(t);
811                                         typeHead = getHead(t);
812                                     } while (isExt(typeHead) && argCount==2);
813                                     if (t!=typeNoRow) {
814                                         putStr(" | ");
815                                         putType(t,NEVER,fr);
816                                     }
817                                     putChr(')');
818                                     return;
819                                 }
820                                 else if (args<2)
821                                     internal("putExt");
822                                 else
823                                     args-=2;
824                             }
825 #endif
826                             OPEN(brackets);
827                             putApType(t,args,fr);
828                             CLOSE(brackets);
829                         }
830                         break;
831
832         default       : putStr("(bad type)");
833     }
834 }
835
836 static Void local putTyVar(n)           /* print type variable             */
837 Int n; {
838     static String alphabet              /* for the benefit of EBCDIC :-)   */
839                 ="abcdefghijklmnopqrstuvwxyz";
840     putChr(alphabet[n%26]);
841     if (n /= 26)                        /* just in case there are > 26 vars*/
842         putInt(n);
843 }
844
845 static Bool local putTupleType(e,fr)    /* print tuple of types, returning */
846 Cell e;                                 /* TRUE if something was printed,  */
847 Int  fr; {                              /* FALSE otherwise; used to control*/
848     if (isAp(e)) {                      /* printing of intermed. commas    */
849         if (putTupleType(fun(e),fr))
850             putChr(',');
851         putType(arg(e),NEVER,fr);
852         return TRUE;
853     }
854     return FALSE;
855 }
856
857 static Void local putApType(t,n,fr)     /* print type application          */
858 Cell t;
859 Int  n;
860 Int  fr; {
861     if (n>0) {
862         putApType(fun(t),n-1,fr);
863         putChr(' ');
864         putType(arg(t),ALWAYS,fr);
865     }
866     else
867         putType(t,ALWAYS,fr);
868 }
869
870 /* --------------------------------------------------------------------------
871  * Print kind expression:
872  * ------------------------------------------------------------------------*/
873
874 static Void local putKind(k)            /* print kind expression           */
875 Kind k; {
876     switch (whatIs(k)) {
877         case AP      : if (isAp(fst(k))) {
878                            putChr('(');
879                            putKind(fst(k));
880                            putChr(')');
881                        }
882                        else
883                            putKind(fst(k));
884                        putStr(" -> ");
885                        putKind(snd(k));
886                        break;
887
888 #if TREX
889         case ROW     : putStr("row");
890                        break;
891 #endif
892
893         case STAR    : putChr('*');
894                        break;
895
896         case OFFSET  : putTyVar(offsetOf(k));
897                        break;
898
899         case INTCELL : putChr('_');
900                        putInt(intOf(k));
901                        break;
902
903         default      : putStr("(bad kind)");
904     }
905 }
906
907 static Void local putKinds(ks)          /* Print list of kinds             */
908 Kinds ks; {
909     if (isNull(ks))
910         putStr("()");
911     else if (nonNull(tl(ks))) {
912         putChr('(');
913         putKind(hd(ks));
914         while (nonNull(ks=tl(ks))) {
915             putChr(',');
916             putKind(hd(ks));
917         }
918         putChr(')');
919     }
920     else
921         putKind(hd(ks));
922 }
923
924 /* --------------------------------------------------------------------------
925  * Main drivers:
926  * ------------------------------------------------------------------------*/
927
928 FILE *mystdout ( Void ) {
929   /* We use this from the gdb command line when debugging */
930   return stdout;
931 }
932
933 Void printExp(fp,e)                     /* print expr on specified stream  */
934 FILE *fp;
935 Cell e; {
936     outputStream = fp;
937     putDepth     = 0;
938     put(NEVER,e);
939 }
940
941 Void printType(fp,t)                    /* print type on specified stream  */
942 FILE *fp;
943 Cell t; {
944     outputStream = fp;
945     putSigType(t);
946 }
947
948 Void printContext(fp,qs)                /* print context on spec. stream   */
949 FILE *fp;
950 List qs; {
951     outputStream = fp;
952     putContext(qs,NIL,0);
953 }
954
955 Void printPred(fp,pi)                   /* print predicate pi on stream    */
956 FILE *fp;
957 Cell pi; {
958     outputStream = fp;
959     putPred(pi,0);
960 }
961
962 Void printKind(fp,k)                    /* print kind k on stream          */
963 FILE *fp;
964 Kind k; {
965     outputStream = fp;
966     putKind(k);
967 }
968
969 Void printKinds(fp,ks)                  /* print list of kinds on stream   */
970 FILE  *fp;
971 Kinds ks; {
972     outputStream = fp;
973     putKinds(ks);
974 }
975
976 Void printFD(fp,fd)                     /* print functional dependency     */
977 FILE* fp;
978 Pair  fd; {
979     List us;
980     outputStream = fp;
981     for (us=fst(fd); nonNull(us); us=tl(us)) {
982         putTyVar(offsetOf(hd(us)));
983         if (nonNull(tl(us))) {
984             putChr(' ');
985         }
986     }
987     putStr(" -> ");
988     for (us=snd(fd); nonNull(us); us=tl(us)) {
989         putTyVar(offsetOf(hd(us)));
990         if (nonNull(tl(us))) {
991             putChr(' ');
992         }
993     }
994 }
995   
996 /*-------------------------------------------------------------------------*/