2 /* --------------------------------------------------------------------------
3 * Unparse expressions and types - for use in error messages, type checker
6 * Hugs 98 is Copyright (c) Mark P Jones, Alastair Reid and the Yale
7 * Haskell Group 1994-99, and is distributed as Open Source software
8 * under the Artistic License; see the file "Artistic" that is included
9 * in the distribution for details.
11 * $RCSfile: output.c,v $
13 * $Date: 1999/04/27 10:06:57 $
14 * ------------------------------------------------------------------------*/
22 #define DEPTH_LIMIT 15
24 /* --------------------------------------------------------------------------
25 * Local function prototypes:
26 * ------------------------------------------------------------------------*/
28 static Void local put Args((Int,Cell));
29 static Void local putFlds Args((Cell,List));
30 static Void local putComp Args((Cell,List));
31 static Void local putQual Args((Cell));
32 static Bool local isDictVal Args((Cell));
33 static Cell local maySkipDict Args((Cell));
34 static Void local putAp Args((Int,Cell));
35 static Void local putOverInfix Args((Int,Text,Syntax,Cell));
36 static Void local putInfix Args((Int,Text,Syntax,Cell,Cell));
37 static Void local putSimpleAp Args((Cell,Int));
38 static Void local putTuple Args((Int,Cell));
39 static Int local unusedTups Args((Int,Cell));
40 static Void local unlexOp Args((Text));
42 static Void local putSigType Args((Cell));
43 static Void local putContext Args((List,List,Int));
44 static Void local putPred Args((Cell,Int));
45 static Void local putType Args((Cell,Int,Int));
46 static Void local putTyVar Args((Int));
47 static Bool local putTupleType Args((Cell,Int));
48 static Void local putApType Args((Type,Int,Int));
50 static Void local putKind Args((Kind));
51 static Void local putKinds Args((Kinds));
53 /* --------------------------------------------------------------------------
54 * Basic output routines:
55 * ------------------------------------------------------------------------*/
57 FILE *outputStream; /* current output stream */
58 Int outColumn = 0; /* current output column number */
60 #define OPEN(b) if (b) putChr('(');
61 #define CLOSE(b) if (b) putChr(')');
63 Void putChr(c) /* print single character */
69 Void putStr(s) /* print string */
72 Putc(*s,outputStream);
77 Void putInt(n) /* print integer */
79 static char intBuf[16];
80 sprintf(intBuf,"%d",n);
84 Void putPtr(p) /* print pointer */
86 static char intBuf[16];
87 sprintf(intBuf,"%p",p);
91 /* --------------------------------------------------------------------------
92 * Precedence values (See Haskell 1.3 report, p.12):
93 * ------------------------------------------------------------------------*/
95 #define ALWAYS FUN_PREC /* Always use parens (unless atomic)*/
96 /* User defined operators have prec */
97 /* in the range MIN_PREC..MAX_PREC */
98 #define ARROW_PREC MAX_PREC /* for printing -> in type exprs */
99 #define COCO_PREC (MIN_PREC-1) /* :: is left assoc, low precedence */
100 #define COND_PREC (MIN_PREC-2) /* conditional expressions */
101 #define WHERE_PREC (MIN_PREC-3) /* where expressions */
102 #define LAM_PREC (MIN_PREC-4) /* lambda abstraction */
103 #define NEVER LAM_PREC /* Never use parentheses */
106 /* --------------------------------------------------------------------------
107 * Print an expression (used to display context of type errors):
108 * ------------------------------------------------------------------------*/
110 static Int putDepth = 0; /* limits depth of printing DBG */
112 static Void local put(d,e) /* print expression e in context of */
113 Int d; /* operator of precedence d */
117 if (putDepth>DEPTH_LIMIT) {
125 case FINLIST : putChr('[');
129 while (nonNull(xs=tl(xs))) {
137 case AP : putAp(d,e);
140 case NAME : unlexVar(name(e).text);
147 case CONOPCELL : unlexVar(textOf(e));
151 case RECSEL : putChr('#');
152 unlexVar(extText(snd(e)));
156 case FREECELL : putStr("{free!}");
159 case TUPLE : putTuple(tupleOf(e),e);
162 case WILDCARD : putChr('_');
165 case ASPAT : put(NEVER,fst(snd(e)));
167 put(ALWAYS,snd(snd(e)));
170 case LAZYPAT : putChr('~');
174 case DOCOMP : putStr("do {...}");
177 case COMP : putComp(fst(snd(e)),snd(snd(e)));
180 case MONADCOMP : putComp(fst(snd(snd(e))),snd(snd(snd(e))));
183 case CHARCELL : unlexCharConst(charOf(e));
186 case INTCELL : { Int i = intOf(e);
187 if (i<0 && d>=UMINUS_PREC) putChr('(');
189 if (i<0 && d>=UMINUS_PREC) putChr(')');
193 case FLOATCELL : { Float f = floatOf(e);
194 if (f<0 && d>=UMINUS_PREC) putChr('(');
195 putStr(floatToString(f));
196 if (f<0 && d>=UMINUS_PREC) putChr(')');
200 case STRCELL : unlexStrConst(textOf(e));
203 case LETREC : OPEN(d>WHERE_PREC);
206 put(NEVER,fst(snd(e)));
209 putStr("let {...} in ");
211 put(WHERE_PREC+1,snd(snd(e)));
215 case COND : OPEN(d>COND_PREC);
217 put(COND_PREC+1,fst3(snd(e)));
219 put(COND_PREC+1,snd3(snd(e)));
221 put(COND_PREC+1,thd3(snd(e)));
225 case LAMBDA : xs = fst(snd(e));
226 if (whatIs(xs)==BIGLAM)
228 while (nonNull(xs) && isDictVal(hd(xs)))
231 put(d,snd(snd(snd(e))));
238 while (nonNull(xs=tl(xs))) {
244 put(LAM_PREC,snd(snd(snd(e))));
248 case ESIGN : OPEN(d>COCO_PREC);
249 put(COCO_PREC,fst(snd(e)));
251 putSigType(snd(snd(e)));
255 case BIGLAM : put(d,snd(snd(e)));
258 case CASE : putStr("case ");
259 put(NEVER,fst(snd(e)));
262 put(NEVER,snd(snd(e)));
269 case CONFLDS : putFlds(fst(snd(e)),snd(snd(e)));
272 case UPDFLDS : putFlds(fst3(snd(e)),thd3(snd(e)));
275 default : /*internal("put");*/
283 static Void local putFlds(exp,fs) /* Output exp using labelled fields*/
288 for (; nonNull(fs); fs=tl(fs)) {
295 Text t = isName(f) ? name(f).text :
296 isVar(f) ? textOf(f) : inventText();
297 Text s = isName(e) ? name(e).text :
298 isVar(e) ? textOf(e) : inventText();
301 if (haskell98 || s!=t) {
312 static Void local putComp(e,qs) /* print comprehension */
320 while (nonNull(qs=tl(qs))) {
328 static Void local putQual(q) /* print list comp qualifier */
331 case BOOLQUAL : put(NEVER,snd(q));
334 case QWHERE : putStr("let {...}");
337 case FROMQUAL : put(ALWAYS,fst(snd(q)));
339 put(NEVER,snd(snd(q)));
344 static Bool local isDictVal(e) /* Look for dictionary value */
349 case DICTVAR : return TRUE;
350 case NAME : return isDfun(h);
356 static Cell local maySkipDict(e) /* descend function application, */
357 Cell e; { /* ignoring dict aps */
358 while (isAp(e) && isDictVal(arg(e)))
363 static Void local putAp(d,e) /* print application (args>=1) */
371 for (h=e; isAp(h); h=fun(h)) /* find head of expression, looking*/
372 if (!isDictVal(arg(h))) /* for dictionary arguments */
375 if (args==0) { /* Special case when *all* args */
376 put(d,h); /* are dictionary values */
382 case ADDPAT : if (args==1)
383 putInfix(d,textPlus,syntaxOf(namePlus),
384 arg(e),mkInt(intValOf(fun(e))));
390 case TUPLE : OPEN(args>tupleOf(h) && d>=FUN_PREC);
391 putTuple(tupleOf(h),e);
392 CLOSE(args>tupleOf(h) && d>=FUN_PREC);
395 case NAME : if (args==1 &&
396 ((h==nameFromInt && isInt(arg(e))) ||
397 (h==nameFromDouble && isFloat(arg(e))))) {
409 case CONOPCELL : sy = defaultSyntax(t = textOf(h));
413 case EXT : if (args==2) {
418 putStr(textToStr(extText(h)));
420 put(NEVER,extField(e));
423 for (h=e; isAp(h); h=fun(h))
424 if (!isDictVal(arg(h)))
426 } while (isExt(h) && args==2);
441 default : sy = APPLIC;
447 if (sy==APPLIC) { /* print simple application */
453 else if (args==1) { /* print section of the form (e+) */
455 put(FUN_PREC-1,arg(e));
460 else if (args==2) /* infix expr of the form e1 + e2 */
461 putInfix(d,t,sy,arg(maySkipDict(fun(e))),arg(e));
462 else { /* o/w (e1 + e2) e3 ... en (n>=3) */
464 putOverInfix(args,t,sy,e);
469 static Void local putOverInfix(args,t,sy,e)
470 Int args; /* infix applied to >= 3 arguments */
475 putOverInfix(args-1,t,sy,maySkipDict(fun(e)));
477 put(FUN_PREC,arg(e));
480 putInfix(ALWAYS,t,sy,arg(maySkipDict(fun(e))),arg(e));
483 static Void local putInfix(d,t,sy,e,f) /* print infix expression */
485 Text t; /* Infix operator symbol */
486 Syntax sy; /* with name t, syntax s */
487 Cell e, f; { /* Left and right operands */
488 Syntax a = assocOf(sy);
492 put((a==LEFT_ASS ? p : 1+p), e);
496 put((a==RIGHT_ASS ? p : 1+p), f);
500 static Void local putSimpleAp(e,n) /* print application e0 e1 ... en */
504 putSimpleAp(maySkipDict(fun(e)),n-1);
506 put(FUN_PREC,arg(e));
512 static Void local putTuple(ts,e) /* Print tuple expression, allowing*/
513 Int ts; /* for possibility of either too */
514 Cell e; { /* few or too many args to constr */
517 if ((i=unusedTups(ts,e))>0) {
524 static Int local unusedTups(ts,e) /* print first part of tuple expr */
525 Int ts; /* returning number of constructor */
526 Cell e; { /* args not yet printed ... */
528 if ((ts=unusedTups(ts,fun(e))-1)>=0) {
530 putChr(ts>0?',':')');
534 put(FUN_PREC,arg(e));
540 Void unlexVar(t) /* print text as a variable name */
541 Text t; { /* operator symbols must be enclosed*/
542 String s = textToStr(t); /* in parentheses... except [] ... */
544 if ((isascii((int)(s[0])) && isalpha((int)(s[0])))
545 || s[0]=='_' || s[0]=='[' || s[0]=='(')
554 static Void local unlexOp(t) /* print text as operator name */
555 Text t; { /* alpha numeric symbols must be */
556 String s = textToStr(t); /* enclosed by backquotes */
558 if (isascii((int)(s[0])) && isalpha((int)(s[0]))) {
567 Void unlexCharConst(c)
570 putStr(unlexChar(c,'\''));
574 Void unlexStrConst(t)
576 String s = textToStr(t);
577 static Char SO = 14; /* ASCII code for '\SO' */
578 Bool lastWasSO = FALSE;
579 Bool lastWasDigit = FALSE;
580 Bool lastWasEsc = FALSE;
584 String ch = unlexChar(*s,'\"');
587 if ((lastWasSO && *ch=='H') ||
588 (lastWasEsc && lastWasDigit
589 && isascii((int)(*ch)) && isdigit((int)(*ch))))
592 lastWasEsc = (*ch=='\\');
593 lastWasSO = (*s==SO);
594 for (; *ch; c = *ch++)
596 lastWasDigit = (isascii(c) && isdigit(c));
601 /* --------------------------------------------------------------------------
602 * Print type expression:
603 * ------------------------------------------------------------------------*/
605 static Void local putSigType(t) /* print (possibly) generic type */
609 Kinds ks = polySigOf(t);
610 for (; isAp(ks); ks=tl(ks))
615 putType(t,NEVER,fr); /* Finally, print rest of type ... */
618 static Void local putContext(ps,qs,fr) /* print context list */
622 Int len = length(ps) + length(qs);
627 for (; nonNull(ps); ps=tl(ps)) {
633 for (; nonNull(qs); qs=tl(qs)) {
644 static Void local putPred(pi,fr) /* Output predicate */
649 if (isExt(fun(pi))) {
650 putType(arg(pi),ALWAYS,fr);
652 putStr(textToStr(extText(fun(pi))));
658 putType(arg(pi),ALWAYS,fr);
660 else if (isClass(pi))
661 putStr(textToStr(cclass(pi).text));
663 putStr(textToStr(textOf(pi)));
665 putStr("<unknownPredicate>");
668 static Void local putType(t,prec,fr) /* print nongeneric type expression*/
673 case TYCON : putStr(textToStr(tycon(t).text));
676 case TUPLE : { Int n = tupleOf(t);
684 case POLYTYPE : { Kinds ks = polySigOf(t);
685 OPEN(prec>=ARROW_PREC);
687 for (; isAp(ks); ks=tl(ks)) {
693 putType(monotypeOf(t),NEVER,fr);
694 CLOSE(prec>=ARROW_PREC);
699 case QUAL : OPEN(prec>=ARROW_PREC);
700 if (whatIs(snd(snd(t)))==CDICTS) {
701 putContext(fst(snd(t)),fst(snd(snd(snd(t)))),fr);
703 putType(snd(snd(snd(snd(t)))),NEVER,fr);
705 putContext(fst(snd(t)),NIL,fr);
707 putType(snd(snd(t)),NEVER,fr);
709 CLOSE(prec>=ARROW_PREC);
713 case RANK2 : putType(snd(snd(t)),prec,fr);
716 case OFFSET : putTyVar(offsetOf(t));
720 case VAROPCELL : putChr('_');
724 case INTCELL : putChr('_');
728 case AP : { Cell typeHead = getHead(t);
729 Bool brackets = (argCount!=0 && prec>=ALWAYS);
732 if (typeHead==typeList) {
735 putType(arg(t),NEVER,fr);
740 else if (typeHead==typeArrow) {
742 OPEN(prec>=ARROW_PREC);
743 putType(arg(fun(t)),ARROW_PREC,fr);
745 putType(arg(t),NEVER,fr);
746 CLOSE(prec>=ARROW_PREC);
749 else if (argCount==1) {
751 putType(arg(t),ARROW_PREC,fr);
756 else if (isTuple(typeHead)) {
757 if (argCount==tupleOf(typeHead)) {
765 else if (isExt(typeHead)) {
771 putStr(textToStr(extText(typeHead)));
773 putType(extField(t),NEVER,fr);
775 typeHead = getHead(t);
776 } while (isExt(typeHead) && argCount==2);
791 putApType(t,args,fr);
796 default : putStr("(bad type)");
800 static Void local putTyVar(n) /* print type variable */
802 static String alphabet /* for the benefit of EBCDIC :-) */
803 ="abcdefghijklmnopqrstuvwxyz";
804 putChr(alphabet[n%26]);
805 if (n /= 26) /* just in case there are > 26 vars*/
809 static Bool local putTupleType(e,fr) /* print tuple of types, returning */
810 Cell e; /* TRUE if something was printed, */
811 Int fr; { /* FALSE otherwise; used to control*/
812 if (isAp(e)) { /* printing of intermed. commas */
813 if (putTupleType(fun(e),fr))
815 putType(arg(e),NEVER,fr);
821 static Void local putApType(t,n,fr) /* print type application */
826 putApType(fun(t),n-1,fr);
828 putType(arg(t),ALWAYS,fr);
831 putType(t,ALWAYS,fr);
834 /* --------------------------------------------------------------------------
835 * Print kind expression:
836 * ------------------------------------------------------------------------*/
838 static Void local putKind(k) /* print kind expression */
841 case AP : if (isAp(fst(k))) {
853 case ROW : putStr("row");
857 case STAR : putChr('*');
860 case OFFSET : putTyVar(offsetOf(k));
863 case INTCELL : putChr('_');
867 default : putStr("(bad kind)");
871 static Void local putKinds(ks) /* Print list of kinds */
875 else if (nonNull(tl(ks))) {
878 while (nonNull(ks=tl(ks))) {
888 /* --------------------------------------------------------------------------
890 * ------------------------------------------------------------------------*/
892 Void printExp(fp,e) /* print expr on specified stream */
900 Void printType(fp,t) /* print type on specified stream */
907 Void printContext(fp,qs) /* print context on spec. stream */
911 putContext(qs,NIL,0);
914 Void printPred(fp,pi) /* print predicate pi on stream */
921 Void printKind(fp,k) /* print kind k on stream */
928 Void printKinds(fp,ks) /* print list of kinds on stream */
935 /*-------------------------------------------------------------------------*/