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/11/29 18:59:29 $
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));
55 /* --------------------------------------------------------------------------
56 * Basic output routines:
57 * ------------------------------------------------------------------------*/
59 FILE *outputStream; /* current output stream */
60 Int outColumn = 0; /* current output column number */
62 #define OPEN(b) if (b) putChr('(');
63 #define CLOSE(b) if (b) putChr(')');
65 Void putChr(c) /* print single character */
71 Void putStr(s) /* print string */
74 Putc(*s,outputStream);
79 Void putInt(n) /* print integer */
81 static char intBuf[16];
82 sprintf(intBuf,"%d",n);
86 Void putPtr(p) /* print pointer */
88 static char intBuf[16];
89 sprintf(intBuf,"%p",p);
93 /* --------------------------------------------------------------------------
94 * Precedence values (See Haskell 1.3 report, p.12):
95 * ------------------------------------------------------------------------*/
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 */
108 /* --------------------------------------------------------------------------
109 * Print an expression (used to display context of type errors):
110 * ------------------------------------------------------------------------*/
112 static Int putDepth = 0; /* limits depth of printing DBG */
114 static Void local put(d,e) /* print expression e in context of */
115 Int d; /* operator of precedence d */
119 if (putDepth>DEPTH_LIMIT) {
127 case FINLIST : putChr('[');
131 while (nonNull(xs=tl(xs))) {
139 case AP : putAp(d,e);
142 case NAME : unlexVar(name(e).text);
149 case CONOPCELL : unlexVar(textOf(e));
153 case IPVAR : putChr('?');
157 case WITHEXP : OPEN(d>WHERE_PREC);
158 putStr("dlet {...} in ");
159 put(WHERE_PREC+1,fst(snd(e)));
165 case RECSEL : putChr('#');
166 unlexVar(extText(snd(e)));
170 case FREECELL : putStr("{free!}");
173 case TUPLE : putTuple(tupleOf(e),e);
176 case WILDCARD : putChr('_');
179 case ASPAT : put(NEVER,fst(snd(e)));
181 put(ALWAYS,snd(snd(e)));
184 case LAZYPAT : putChr('~');
188 case DOCOMP : putStr("do {...}");
191 case COMP : putComp(fst(snd(e)),snd(snd(e)));
194 case MONADCOMP : putComp(fst(snd(snd(e))),snd(snd(snd(e))));
197 case CHARCELL : unlexCharConst(charOf(e));
200 case INTCELL : { Int i = intOf(e);
201 if (i<0 && d>=UMINUS_PREC) putChr('(');
203 if (i<0 && d>=UMINUS_PREC) putChr(')');
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(')');
214 case STRCELL : unlexStrConst(textOf(e));
217 case LETREC : OPEN(d>WHERE_PREC);
220 put(NEVER,fst(snd(e)));
223 putStr("let {...} in ");
225 put(WHERE_PREC+1,snd(snd(e)));
229 case COND : OPEN(d>COND_PREC);
231 put(COND_PREC+1,fst3(snd(e)));
233 put(COND_PREC+1,snd3(snd(e)));
235 put(COND_PREC+1,thd3(snd(e)));
239 case LAMBDA : xs = fst(snd(e));
240 if (whatIs(xs)==BIGLAM)
242 while (nonNull(xs) && isDictVal(hd(xs)))
245 put(d,snd(snd(snd(e))));
252 while (nonNull(xs=tl(xs))) {
258 put(LAM_PREC,snd(snd(snd(e))));
262 case ESIGN : OPEN(d>COCO_PREC);
263 put(COCO_PREC,fst(snd(e)));
265 putSigType(snd(snd(e)));
269 case BIGLAM : put(d,snd(snd(e)));
272 case CASE : putStr("case ");
273 put(NEVER,fst(snd(e)));
276 put(NEVER,snd(snd(e)));
283 case CONFLDS : putFlds(fst(snd(e)),snd(snd(e)));
286 case UPDFLDS : putFlds(fst3(snd(e)),thd3(snd(e)));
289 default : /*internal("put");*/
297 static Void local putFlds(exp,fs) /* Output exp using labelled fields*/
302 for (; nonNull(fs); fs=tl(fs)) {
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();
315 if (haskell98 || s!=t) {
326 static Void local putComp(e,qs) /* print comprehension */
334 while (nonNull(qs=tl(qs))) {
342 static Void local putQual(q) /* print list comp qualifier */
345 case BOOLQUAL : put(NEVER,snd(q));
348 case QWHERE : putStr("let {...}");
351 case FROMQUAL : put(ALWAYS,fst(snd(q)));
353 put(NEVER,snd(snd(q)));
358 static Bool local isDictVal(e) /* Look for dictionary value */
363 case DICTVAR : return TRUE;
364 case NAME : return isDfun(h);
370 static Cell local maySkipDict(e) /* descend function application, */
371 Cell e; { /* ignoring dict aps */
372 while (isAp(e) && isDictVal(arg(e)))
377 static Void local putAp(d,e) /* print application (args>=1) */
381 Text t = 0; /* bogus init to keep gcc -O happy */
385 for (h=e; isAp(h); h=fun(h)) /* find head of expression, looking*/
386 if (!isDictVal(arg(h))) /* for dictionary arguments */
389 if (args==0) { /* Special case when *all* args */
390 put(d,h); /* are dictionary values */
396 case ADDPAT : if (args==1)
397 putInfix(d,textPlus,syntaxOf(namePlus),
398 arg(e),mkInt(intValOf(fun(e))));
404 case TUPLE : OPEN(args>tupleOf(h) && d>=FUN_PREC);
405 putTuple(tupleOf(h),e);
406 CLOSE(args>tupleOf(h) && d>=FUN_PREC);
409 case NAME : if (args==1 &&
410 ((h==nameFromInt && isInt(arg(e))) ||
411 (h==nameFromDouble && isFloat(arg(e))))) {
423 case CONOPCELL : sy = defaultSyntax(t = textOf(h));
427 case EXT : if (args==2) {
432 putStr(textToStr(extText(h)));
434 put(NEVER,extField(e));
437 for (h=e; isAp(h); h=fun(h))
438 if (!isDictVal(arg(h)))
440 } while (isExt(h) && args==2);
455 default : sy = APPLIC;
461 if (sy==APPLIC) { /* print simple application */
467 else if (args==1) { /* print section of the form (e+) */
469 put(FUN_PREC-1,arg(e));
474 else if (args==2) /* infix expr of the form e1 + e2 */
475 putInfix(d,t,sy,arg(maySkipDict(fun(e))),arg(e));
476 else { /* o/w (e1 + e2) e3 ... en (n>=3) */
478 putOverInfix(args,t,sy,e);
483 static Void local putOverInfix(args,t,sy,e)
484 Int args; /* infix applied to >= 3 arguments */
489 putOverInfix(args-1,t,sy,maySkipDict(fun(e)));
491 put(FUN_PREC,arg(e));
494 putInfix(ALWAYS,t,sy,arg(maySkipDict(fun(e))),arg(e));
497 static Void local putInfix(d,t,sy,e,f) /* print infix expression */
499 Text t; /* Infix operator symbol */
500 Syntax sy; /* with name t, syntax s */
501 Cell e, f; { /* Left and right operands */
502 Syntax a = assocOf(sy);
506 put((a==LEFT_ASS ? p : 1+p), e);
510 put((a==RIGHT_ASS ? p : 1+p), f);
514 static Void local putSimpleAp(e,n) /* print application e0 e1 ... en */
518 putSimpleAp(maySkipDict(fun(e)),n-1);
520 put(FUN_PREC,arg(e));
526 static Void local putTuple(ts,e) /* Print tuple expression, allowing*/
527 Int ts; /* for possibility of either too */
528 Cell e; { /* few or too many args to constr */
531 if ((i=unusedTups(ts,e))>0) {
538 static Int local unusedTups(ts,e) /* print first part of tuple expr */
539 Int ts; /* returning number of constructor */
540 Cell e; { /* args not yet printed ... */
542 if ((ts=unusedTups(ts,fun(e))-1)>=0) {
544 putChr(ts>0?',':')');
548 put(FUN_PREC,arg(e));
556 if ((isascii((int)(s[0])) && isalpha((int)(s[0])))
557 || s[0]=='_' || s[0]=='[' || s[0]=='('
559 || (s[0]==':' && s[1]=='D')
569 Void unlexVar(t) /* print text as a variable name */
570 Text t; { /* operator symbols must be enclosed*/
571 unlexVarStr(textToStr(t)); /* in parentheses... except [] ... */
574 static Void local unlexOp(t) /* print text as operator name */
575 Text t; { /* alpha numeric symbols must be */
576 String s = textToStr(t); /* enclosed by backquotes */
578 if (isascii((int)(s[0])) && isalpha((int)(s[0]))) {
587 Void unlexCharConst(c)
590 putStr(unlexChar(c,'\''));
594 Void unlexStrConst(t)
596 String s = textToStr(t);
597 static Char SO = 14; /* ASCII code for '\SO' */
598 Bool lastWasSO = FALSE;
599 Bool lastWasDigit = FALSE;
600 Bool lastWasEsc = FALSE;
604 String ch = unlexChar(*s,'\"');
607 if ((lastWasSO && *ch=='H') ||
608 (lastWasEsc && lastWasDigit
609 && isascii((int)(*ch)) && isdigit((int)(*ch))))
612 lastWasEsc = (*ch=='\\');
613 lastWasSO = (*s==SO);
614 for (; *ch; c = *ch++)
616 lastWasDigit = (isascii(c) && isdigit(c));
621 /* --------------------------------------------------------------------------
622 * Print type expression:
623 * ------------------------------------------------------------------------*/
625 static Void local putSigType(t) /* print (possibly) generic type */
629 Kinds ks = polySigOf(t);
630 for (; isAp(ks); ks=tl(ks))
635 putType(t,NEVER,fr); /* Finally, print rest of type ... */
638 static Void local putContext(ps,qs,fr) /* print context list */
642 Int len = length(ps) + length(qs);
645 Bool useParens = len!=1 || isIP(fun(hd(ps)));
647 Bool useParens = len!=1;
651 for (; nonNull(ps); ps=tl(ps)) {
657 for (; nonNull(qs); qs=tl(qs)) {
667 static Void local putPred(pi,fr) /* Output predicate */
672 if (isExt(fun(pi))) {
673 putType(arg(pi),ALWAYS,fr);
675 putStr(textToStr(extText(fun(pi))));
680 if (whatIs(fun(pi)) == IPCELL) {
684 putType(arg(pi),NEVER,fr);
690 putType(arg(pi),ALWAYS,fr);
692 else if (isClass(pi))
693 putStr(textToStr(cclass(pi).text));
695 putStr(textToStr(textOf(pi)));
697 else if (whatIs(pi) == IPCELL)
698 unlexVar(textOf(pi));
701 putStr("<unknownPredicate>");
704 static Void local putType(t,prec,fr) /* print nongeneric type expression*/
709 case TYCON : putStr(textToStr(tycon(t).text));
712 case TUPLE : { Int n = tupleOf(t);
720 case POLYTYPE : { Kinds ks = polySigOf(t);
721 OPEN(prec>=ARROW_PREC);
723 for (; isAp(ks); ks=tl(ks)) {
729 putType(monotypeOf(t),NEVER,fr);
730 CLOSE(prec>=ARROW_PREC);
735 case QUAL : OPEN(prec>=ARROW_PREC);
736 if (whatIs(snd(snd(t)))==CDICTS) {
737 putContext(fst(snd(t)),fst(snd(snd(snd(t)))),fr);
739 putType(snd(snd(snd(snd(t)))),NEVER,fr);
741 putContext(fst(snd(t)),NIL,fr);
743 putType(snd(snd(t)),NEVER,fr);
745 CLOSE(prec>=ARROW_PREC);
749 case RANK2 : putType(snd(snd(t)),prec,fr);
752 case OFFSET : putTyVar(offsetOf(t));
756 case VAROPCELL : putChr('_');
760 case INTCELL : putChr('_');
764 case AP : { Cell typeHead = getHead(t);
765 Bool brackets = (argCount!=0 && prec>=ALWAYS);
768 if (typeHead==typeList) {
771 putType(arg(t),NEVER,fr);
776 else if (typeHead==typeArrow) {
778 OPEN(prec>=ARROW_PREC);
779 putType(arg(fun(t)),ARROW_PREC,fr);
781 putType(arg(t),NEVER,fr);
782 CLOSE(prec>=ARROW_PREC);
786 else if (argCount==1) {
788 putType(arg(t),ARROW_PREC,fr);
794 else if (isTuple(typeHead)) {
795 if (argCount==tupleOf(typeHead)) {
803 else if (isExt(typeHead)) {
809 putStr(textToStr(extText(typeHead)));
811 putType(extField(t),NEVER,fr);
813 typeHead = getHead(t);
814 } while (isExt(typeHead) && argCount==2);
829 putApType(t,args,fr);
834 default : putStr("(bad type)");
838 static Void local putTyVar(n) /* print type variable */
840 static String alphabet /* for the benefit of EBCDIC :-) */
841 ="abcdefghijklmnopqrstuvwxyz";
842 putChr(alphabet[n%26]);
843 if (n /= 26) /* just in case there are > 26 vars*/
847 static Bool local putTupleType(e,fr) /* print tuple of types, returning */
848 Cell e; /* TRUE if something was printed, */
849 Int fr; { /* FALSE otherwise; used to control*/
850 if (isAp(e)) { /* printing of intermed. commas */
851 if (putTupleType(fun(e),fr))
853 putType(arg(e),NEVER,fr);
859 static Void local putApType(t,n,fr) /* print type application */
864 putApType(fun(t),n-1,fr);
866 putType(arg(t),ALWAYS,fr);
869 putType(t,ALWAYS,fr);
872 /* --------------------------------------------------------------------------
873 * Print kind expression:
874 * ------------------------------------------------------------------------*/
876 static Void local putKind(k) /* print kind expression */
879 case AP : if (isAp(fst(k))) {
891 case ROW : putStr("row");
895 case STAR : putChr('*');
898 case OFFSET : putTyVar(offsetOf(k));
901 case INTCELL : putChr('_');
905 default : putStr("(bad kind)");
909 static Void local putKinds(ks) /* Print list of kinds */
913 else if (nonNull(tl(ks))) {
916 while (nonNull(ks=tl(ks))) {
926 /* --------------------------------------------------------------------------
928 * ------------------------------------------------------------------------*/
930 extern FILE *mystdout Args((Void));
932 /* We use this from the gdb command line when debugging */
936 Void printExp(fp,e) /* print expr on specified stream */
944 Void printType(fp,t) /* print type on specified stream */
951 Void printContext(fp,qs) /* print context on spec. stream */
955 putContext(qs,NIL,0);
958 Void printPred(fp,pi) /* print predicate pi on stream */
965 Void printKind(fp,k) /* print kind k on stream */
972 Void printKinds(fp,ks) /* print list of kinds on stream */
979 Void printFD(fp,fd) /* print functional dependency */
984 for (us=fst(fd); nonNull(us); us=tl(us)) {
985 putTyVar(offsetOf(hd(us)));
986 if (nonNull(tl(us))) {
991 for (us=snd(fd); nonNull(us); us=tl(us)) {
992 putTyVar(offsetOf(hd(us)));
993 if (nonNull(tl(us))) {
999 /*-------------------------------------------------------------------------*/