2 /* --------------------------------------------------------------------------
3 * Unparse expressions and types - for use in error messages, type checker
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.
12 * $RCSfile: output.c,v $
14 * $Date: 1999/10/20 02:16:02 $
15 * ------------------------------------------------------------------------*/
23 #define DEPTH_LIMIT 15
25 /* --------------------------------------------------------------------------
26 * Local function prototypes:
27 * ------------------------------------------------------------------------*/
29 static Void local put Args((Int,Cell));
30 static Void local putFlds Args((Cell,List));
31 static Void local putComp Args((Cell,List));
32 static Void local putQual Args((Cell));
33 static Bool local isDictVal Args((Cell));
34 static Cell local maySkipDict Args((Cell));
35 static Void local putAp Args((Int,Cell));
36 static Void local putOverInfix Args((Int,Text,Syntax,Cell));
37 static Void local putInfix Args((Int,Text,Syntax,Cell,Cell));
38 static Void local putSimpleAp Args((Cell,Int));
39 static Void local putTuple Args((Int,Cell));
40 static Int local unusedTups Args((Int,Cell));
41 static Void local unlexOp Args((Text));
43 static Void local putSigType Args((Cell));
44 static Void local putContext Args((List,List,Int));
45 static Void local putPred Args((Cell,Int));
46 static Void local putType Args((Cell,Int,Int));
47 static Void local putTyVar Args((Int));
48 static Bool local putTupleType Args((Cell,Int));
49 static Void local putApType Args((Type,Int,Int));
51 static Void local putKind Args((Kind));
52 static Void local putKinds Args((Kinds));
54 /* --------------------------------------------------------------------------
55 * Basic output routines:
56 * ------------------------------------------------------------------------*/
58 FILE *outputStream; /* current output stream */
59 Int outColumn = 0; /* current output column number */
61 #define OPEN(b) if (b) putChr('(');
62 #define CLOSE(b) if (b) putChr(')');
64 Void putChr(c) /* print single character */
70 Void putStr(s) /* print string */
73 Putc(*s,outputStream);
78 Void putInt(n) /* print integer */
80 static char intBuf[16];
81 sprintf(intBuf,"%d",n);
85 Void putPtr(p) /* print pointer */
87 static char intBuf[16];
88 sprintf(intBuf,"%p",p);
92 /* --------------------------------------------------------------------------
93 * Precedence values (See Haskell 1.3 report, p.12):
94 * ------------------------------------------------------------------------*/
96 #define ALWAYS FUN_PREC /* Always use parens (unless atomic)*/
97 /* User defined operators have prec */
98 /* in the range MIN_PREC..MAX_PREC */
99 #define ARROW_PREC MAX_PREC /* for printing -> in type exprs */
100 #define COCO_PREC (MIN_PREC-1) /* :: is left assoc, low precedence */
101 #define COND_PREC (MIN_PREC-2) /* conditional expressions */
102 #define WHERE_PREC (MIN_PREC-3) /* where expressions */
103 #define LAM_PREC (MIN_PREC-4) /* lambda abstraction */
104 #define NEVER LAM_PREC /* Never use parentheses */
107 /* --------------------------------------------------------------------------
108 * Print an expression (used to display context of type errors):
109 * ------------------------------------------------------------------------*/
111 static Int putDepth = 0; /* limits depth of printing DBG */
113 static Void local put(d,e) /* print expression e in context of */
114 Int d; /* operator of precedence d */
118 if (putDepth>DEPTH_LIMIT) {
126 case FINLIST : putChr('[');
130 while (nonNull(xs=tl(xs))) {
138 case AP : putAp(d,e);
141 case NAME : unlexVar(name(e).text);
148 case CONOPCELL : unlexVar(textOf(e));
152 case IPVAR : putChr('?');
156 case WITHEXP : OPEN(d>WHERE_PREC);
157 putStr("dlet {...} in ");
158 put(WHERE_PREC+1,fst(snd(e)));
164 case RECSEL : putChr('#');
165 unlexVar(extText(snd(e)));
169 case FREECELL : putStr("{free!}");
172 case TUPLE : putTuple(tupleOf(e),e);
175 case WILDCARD : putChr('_');
178 case ASPAT : put(NEVER,fst(snd(e)));
180 put(ALWAYS,snd(snd(e)));
183 case LAZYPAT : putChr('~');
187 case DOCOMP : putStr("do {...}");
190 case COMP : putComp(fst(snd(e)),snd(snd(e)));
193 case MONADCOMP : putComp(fst(snd(snd(e))),snd(snd(snd(e))));
196 case CHARCELL : unlexCharConst(charOf(e));
199 case INTCELL : { Int i = intOf(e);
200 if (i<0 && d>=UMINUS_PREC) putChr('(');
202 if (i<0 && d>=UMINUS_PREC) putChr(')');
206 case FLOATCELL : { Float f = floatOf(e);
207 if (f<0 && d>=UMINUS_PREC) putChr('(');
208 putStr(floatToString(f));
209 if (f<0 && d>=UMINUS_PREC) putChr(')');
213 case STRCELL : unlexStrConst(textOf(e));
216 case LETREC : OPEN(d>WHERE_PREC);
219 put(NEVER,fst(snd(e)));
222 putStr("let {...} in ");
224 put(WHERE_PREC+1,snd(snd(e)));
228 case COND : OPEN(d>COND_PREC);
230 put(COND_PREC+1,fst3(snd(e)));
232 put(COND_PREC+1,snd3(snd(e)));
234 put(COND_PREC+1,thd3(snd(e)));
238 case LAMBDA : xs = fst(snd(e));
239 if (whatIs(xs)==BIGLAM)
241 while (nonNull(xs) && isDictVal(hd(xs)))
244 put(d,snd(snd(snd(e))));
251 while (nonNull(xs=tl(xs))) {
257 put(LAM_PREC,snd(snd(snd(e))));
261 case ESIGN : OPEN(d>COCO_PREC);
262 put(COCO_PREC,fst(snd(e)));
264 putSigType(snd(snd(e)));
268 case BIGLAM : put(d,snd(snd(e)));
271 case CASE : putStr("case ");
272 put(NEVER,fst(snd(e)));
275 put(NEVER,snd(snd(e)));
282 case CONFLDS : putFlds(fst(snd(e)),snd(snd(e)));
285 case UPDFLDS : putFlds(fst3(snd(e)),thd3(snd(e)));
288 default : /*internal("put");*/
296 static Void local putFlds(exp,fs) /* Output exp using labelled fields*/
301 for (; nonNull(fs); fs=tl(fs)) {
308 Text t = isName(f) ? name(f).text :
309 isVar(f) ? textOf(f) : inventText();
310 Text s = isName(e) ? name(e).text :
311 isVar(e) ? textOf(e) : inventText();
314 if (haskell98 || s!=t) {
325 static Void local putComp(e,qs) /* print comprehension */
333 while (nonNull(qs=tl(qs))) {
341 static Void local putQual(q) /* print list comp qualifier */
344 case BOOLQUAL : put(NEVER,snd(q));
347 case QWHERE : putStr("let {...}");
350 case FROMQUAL : put(ALWAYS,fst(snd(q)));
352 put(NEVER,snd(snd(q)));
357 static Bool local isDictVal(e) /* Look for dictionary value */
362 case DICTVAR : return TRUE;
363 case NAME : return isDfun(h);
369 static Cell local maySkipDict(e) /* descend function application, */
370 Cell e; { /* ignoring dict aps */
371 while (isAp(e) && isDictVal(arg(e)))
376 static Void local putAp(d,e) /* print application (args>=1) */
384 for (h=e; isAp(h); h=fun(h)) /* find head of expression, looking*/
385 if (!isDictVal(arg(h))) /* for dictionary arguments */
388 if (args==0) { /* Special case when *all* args */
389 put(d,h); /* are dictionary values */
395 case ADDPAT : if (args==1)
396 putInfix(d,textPlus,syntaxOf(namePlus),
397 arg(e),mkInt(intValOf(fun(e))));
403 case TUPLE : OPEN(args>tupleOf(h) && d>=FUN_PREC);
404 putTuple(tupleOf(h),e);
405 CLOSE(args>tupleOf(h) && d>=FUN_PREC);
408 case NAME : if (args==1 &&
409 ((h==nameFromInt && isInt(arg(e))) ||
410 (h==nameFromDouble && isFloat(arg(e))))) {
422 case CONOPCELL : sy = defaultSyntax(t = textOf(h));
426 case EXT : if (args==2) {
431 putStr(textToStr(extText(h)));
433 put(NEVER,extField(e));
436 for (h=e; isAp(h); h=fun(h))
437 if (!isDictVal(arg(h)))
439 } while (isExt(h) && args==2);
454 default : sy = APPLIC;
460 if (sy==APPLIC) { /* print simple application */
466 else if (args==1) { /* print section of the form (e+) */
468 put(FUN_PREC-1,arg(e));
473 else if (args==2) /* infix expr of the form e1 + e2 */
474 putInfix(d,t,sy,arg(maySkipDict(fun(e))),arg(e));
475 else { /* o/w (e1 + e2) e3 ... en (n>=3) */
477 putOverInfix(args,t,sy,e);
482 static Void local putOverInfix(args,t,sy,e)
483 Int args; /* infix applied to >= 3 arguments */
488 putOverInfix(args-1,t,sy,maySkipDict(fun(e)));
490 put(FUN_PREC,arg(e));
493 putInfix(ALWAYS,t,sy,arg(maySkipDict(fun(e))),arg(e));
496 static Void local putInfix(d,t,sy,e,f) /* print infix expression */
498 Text t; /* Infix operator symbol */
499 Syntax sy; /* with name t, syntax s */
500 Cell e, f; { /* Left and right operands */
501 Syntax a = assocOf(sy);
505 put((a==LEFT_ASS ? p : 1+p), e);
509 put((a==RIGHT_ASS ? p : 1+p), f);
513 static Void local putSimpleAp(e,n) /* print application e0 e1 ... en */
517 putSimpleAp(maySkipDict(fun(e)),n-1);
519 put(FUN_PREC,arg(e));
525 static Void local putTuple(ts,e) /* Print tuple expression, allowing*/
526 Int ts; /* for possibility of either too */
527 Cell e; { /* few or too many args to constr */
530 if ((i=unusedTups(ts,e))>0) {
537 static Int local unusedTups(ts,e) /* print first part of tuple expr */
538 Int ts; /* returning number of constructor */
539 Cell e; { /* args not yet printed ... */
541 if ((ts=unusedTups(ts,fun(e))-1)>=0) {
543 putChr(ts>0?',':')');
547 put(FUN_PREC,arg(e));
553 Void unlexVar(t) /* print text as a variable name */
554 Text t; { /* operator symbols must be enclosed*/
555 String s = textToStr(t); /* in parentheses... except [] ... */
557 if ((isascii((int)(s[0])) && isalpha((int)(s[0])))
558 || s[0]=='_' || s[0]=='[' || s[0]=='(')
567 static Void local unlexOp(t) /* print text as operator name */
568 Text t; { /* alpha numeric symbols must be */
569 String s = textToStr(t); /* enclosed by backquotes */
571 if (isascii((int)(s[0])) && isalpha((int)(s[0]))) {
580 Void unlexCharConst(c)
583 putStr(unlexChar(c,'\''));
587 Void unlexStrConst(t)
589 String s = textToStr(t);
590 static Char SO = 14; /* ASCII code for '\SO' */
591 Bool lastWasSO = FALSE;
592 Bool lastWasDigit = FALSE;
593 Bool lastWasEsc = FALSE;
597 String ch = unlexChar(*s,'\"');
600 if ((lastWasSO && *ch=='H') ||
601 (lastWasEsc && lastWasDigit
602 && isascii((int)(*ch)) && isdigit((int)(*ch))))
605 lastWasEsc = (*ch=='\\');
606 lastWasSO = (*s==SO);
607 for (; *ch; c = *ch++)
609 lastWasDigit = (isascii(c) && isdigit(c));
614 /* --------------------------------------------------------------------------
615 * Print type expression:
616 * ------------------------------------------------------------------------*/
618 static Void local putSigType(t) /* print (possibly) generic type */
622 Kinds ks = polySigOf(t);
623 for (; isAp(ks); ks=tl(ks))
628 putType(t,NEVER,fr); /* Finally, print rest of type ... */
631 static Void local putContext(ps,qs,fr) /* print context list */
635 Int len = length(ps) + length(qs);
638 Bool useParens = len!=1 || isIP(fun(hd(ps)));
640 Bool useParens = len!=1;
643 for (; nonNull(ps); ps=tl(ps)) {
649 for (; nonNull(qs); qs=tl(qs)) {
659 static Void local putPred(pi,fr) /* Output predicate */
664 if (isExt(fun(pi))) {
665 putType(arg(pi),ALWAYS,fr);
667 putStr(textToStr(extText(fun(pi))));
672 if (whatIs(fun(pi)) == IPCELL) {
676 putType(arg(pi),NEVER,fr);
682 putType(arg(pi),ALWAYS,fr);
684 else if (isClass(pi))
685 putStr(textToStr(cclass(pi).text));
687 putStr(textToStr(textOf(pi)));
689 else if (whatIs(pi) == IPCELL)
690 unlexVar(textOf(pi));
693 putStr("<unknownPredicate>");
696 static Void local putType(t,prec,fr) /* print nongeneric type expression*/
701 case TYCON : putStr(textToStr(tycon(t).text));
704 case TUPLE : { Int n = tupleOf(t);
712 case POLYTYPE : { Kinds ks = polySigOf(t);
713 OPEN(prec>=ARROW_PREC);
715 for (; isAp(ks); ks=tl(ks)) {
721 putType(monotypeOf(t),NEVER,fr);
722 CLOSE(prec>=ARROW_PREC);
727 case QUAL : OPEN(prec>=ARROW_PREC);
728 if (whatIs(snd(snd(t)))==CDICTS) {
729 putContext(fst(snd(t)),fst(snd(snd(snd(t)))),fr);
731 putType(snd(snd(snd(snd(t)))),NEVER,fr);
733 putContext(fst(snd(t)),NIL,fr);
735 putType(snd(snd(t)),NEVER,fr);
737 CLOSE(prec>=ARROW_PREC);
741 case RANK2 : putType(snd(snd(t)),prec,fr);
744 case OFFSET : putTyVar(offsetOf(t));
748 case VAROPCELL : putChr('_');
752 case INTCELL : putChr('_');
756 case AP : { Cell typeHead = getHead(t);
757 Bool brackets = (argCount!=0 && prec>=ALWAYS);
760 if (typeHead==typeList) {
763 putType(arg(t),NEVER,fr);
768 else if (typeHead==typeArrow) {
770 OPEN(prec>=ARROW_PREC);
771 putType(arg(fun(t)),ARROW_PREC,fr);
773 putType(arg(t),NEVER,fr);
774 CLOSE(prec>=ARROW_PREC);
778 else if (argCount==1) {
780 putType(arg(t),ARROW_PREC,fr);
786 else if (isTuple(typeHead)) {
787 if (argCount==tupleOf(typeHead)) {
795 else if (isExt(typeHead)) {
801 putStr(textToStr(extText(typeHead)));
803 putType(extField(t),NEVER,fr);
805 typeHead = getHead(t);
806 } while (isExt(typeHead) && argCount==2);
821 putApType(t,args,fr);
826 default : putStr("(bad type)");
830 static Void local putTyVar(n) /* print type variable */
832 static String alphabet /* for the benefit of EBCDIC :-) */
833 ="abcdefghijklmnopqrstuvwxyz";
834 putChr(alphabet[n%26]);
835 if (n /= 26) /* just in case there are > 26 vars*/
839 static Bool local putTupleType(e,fr) /* print tuple of types, returning */
840 Cell e; /* TRUE if something was printed, */
841 Int fr; { /* FALSE otherwise; used to control*/
842 if (isAp(e)) { /* printing of intermed. commas */
843 if (putTupleType(fun(e),fr))
845 putType(arg(e),NEVER,fr);
851 static Void local putApType(t,n,fr) /* print type application */
856 putApType(fun(t),n-1,fr);
858 putType(arg(t),ALWAYS,fr);
861 putType(t,ALWAYS,fr);
864 /* --------------------------------------------------------------------------
865 * Print kind expression:
866 * ------------------------------------------------------------------------*/
868 static Void local putKind(k) /* print kind expression */
871 case AP : if (isAp(fst(k))) {
883 case ROW : putStr("row");
887 case STAR : putChr('*');
890 case OFFSET : putTyVar(offsetOf(k));
893 case INTCELL : putChr('_');
897 default : putStr("(bad kind)");
901 static Void local putKinds(ks) /* Print list of kinds */
905 else if (nonNull(tl(ks))) {
908 while (nonNull(ks=tl(ks))) {
918 /* --------------------------------------------------------------------------
920 * ------------------------------------------------------------------------*/
922 Void printExp(fp,e) /* print expr on specified stream */
930 Void printType(fp,t) /* print type on specified stream */
937 Void printContext(fp,qs) /* print context on spec. stream */
941 putContext(qs,NIL,0);
944 Void printPred(fp,pi) /* print predicate pi on stream */
951 Void printKind(fp,k) /* print kind k on stream */
958 Void printKinds(fp,ks) /* print list of kinds on stream */
965 Void printFD(fp,fd) /* print functional dependency */
970 for (us=fst(fd); nonNull(us); us=tl(us)) {
971 putTyVar(offsetOf(hd(us)));
972 if (nonNull(tl(us))) {
977 for (us=snd(fd); nonNull(us); us=tl(us)) {
978 putTyVar(offsetOf(hd(us)));
979 if (nonNull(tl(us))) {
985 /*-------------------------------------------------------------------------*/