2 /* --------------------------------------------------------------------------
3 * Provides an implementation for the `current substitution' used during
4 * type and kind inference in both static analysis and type checking.
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: subst.c,v $
14 * $Date: 2000/03/23 14:54:21 $
15 * ------------------------------------------------------------------------*/
17 #include "hugsbasictypes.h"
23 /*#define DEBUG_TYPES*/
25 static Int numTyvars; /* no. type vars currently in use */
26 static Int maxTyvars = 0;
27 static Int nextGeneric; /* number of generics found so far */
29 Tyvar *tyvars = 0; /* storage for type variables */
30 Int typeOff; /* offset of result type */
31 Type typeIs; /* skeleton of result type */
32 Int typeFree; /* freedom in instantiated type */
33 List predsAre; /* list of predicates in type */
34 List genericVars; /* list of generic vars */
35 List btyvars = NIL; /* explicitly scoped type vars */
37 /* --------------------------------------------------------------------------
38 * local function prototypes:
39 * ------------------------------------------------------------------------*/
41 static Void local expandSubst ( Int );
42 static Int local findBtyvsInt ( Text );
43 static Type local makeTupleType ( Int );
44 static Kind local makeSimpleKind ( Int );
45 static Kind local makeVarKind ( Int );
46 static Void local expandSyn1 ( Tycon, Type *, Int * );
47 static List local listTyvar ( Int,List );
48 static List local listTyvars ( Type,Int,List );
49 static Cell local dupTyvar ( Int,List );
50 static Cell local dupTyvars ( Cell,Int,List );
51 static Pair local copyNoMark ( Cell,Int );
52 static Type local dropRank1Body ( Type,Int,Int );
53 static Type local liftRank1Body ( Type,Int );
54 static Bool local matchTypeAbove ( Type,Int,Type,Int,Int );
56 static Bool local varToVarBind ( Tyvar *,Tyvar * );
57 static Bool local varToTypeBind ( Tyvar *,Type,Int );
59 static Bool local inserter ( Type,Int,Type,Int );
60 static Int local remover ( Text,Type,Int );
61 static Int local tailVar ( Type,Int );
64 static Bool local improveAgainst ( Int,List,Cell,Int );
65 static Bool local instImprove ( Int,Class,Cell,Int );
66 static Bool local pairImprove ( Int,Class,Cell,Int,Cell,Int,Int );
68 static Bool local ipImprove ( Int,Cell,Int,Cell,Int );
71 static Bool local kvarToVarBind ( Tyvar *,Tyvar * );
72 static Bool local kvarToTypeBind ( Tyvar *,Type,Int );
74 /* --------------------------------------------------------------------------
75 * The substitution, types, and kinds:
77 * In early versions of Gofer, the `substitution' data structure was only
78 * used by the type checker, so it made sense to include support for it in
79 * type.c. This changed when kinds and kind inference where introduced,
80 * which required access to the substitution during static analysis. The
81 * links between type.c and static.c that were intially used to accomplish
82 * this have now been avoided by making the substitution visible as an
83 * independent data structure in storage.c.
85 * In the same way that values have types, type constructors (and more
86 * generally, expressions built from such constructors) have kinds.
87 * The syntax of kinds in the current implementation is very simple:
89 * kind ::= STAR -- the kind of types
90 * | kind => kind -- constructors
91 * | variables -- either INTCELL or OFFSET
93 * For various reasons, this implementation uses structure sharing, instead
94 * of a copying approach. In principal, this is fast and avoids the need to
95 * build new type expressions. Unfortunately, this implementation will not
96 * be able to handle *very* large expressions.
98 * The substitution is represented by an array of type variables each of
100 * bound a (skeletal) type expression, or NIL if the variable
101 * is not bound, or SKOLEM for a Skolem constant (i.e., an
102 * uninstantiable variable).
103 * offs offset of skeleton in bound. If isNull(bound), then offs is
104 * used to indicate whether that variable is generic (i.e. free
105 * in the current assumption set) or fixed (i.e. bound in the
106 * current assumption set). Generic variables are assigned
107 * offset numbers whilst copying type expressions (t,o) to
108 * obtain their most general form.
109 * kind kind of value bound to type variable (`type variable' is
110 * rather inaccurate -- `constructor variable' would be better).
111 * ------------------------------------------------------------------------*/
113 Void emptySubstitution() { /* clear current substitution */
115 if (maxTyvars!=NUM_TYVARS) {
129 static Void local expandSubst(n) /* add further n type variables to */
130 Int n; { /* current substituion */
131 if (numTyvars+n>maxTyvars) { /* need to expand substitution */
132 Int newMax = maxTyvars+NUM_TYVARS;
136 if (numTyvars+n>newMax) { /* safety precaution */
137 ERRMSG(0) "Substitution expanding too quickly"
141 /* It would be better to realloc() here, but that isn't portable
142 * enough for calloc()ed arrays. The following code could cause
143 * a space leak if an interrupt occurs while we're copying the
144 * array ... we won't worry about this for the time being because
145 * we don't expect to have to go through this process much (if at
146 * all) in normal use of the type checker.
149 newTvs = (Tyvar *)calloc(newMax,sizeof(Tyvar));
151 ERRMSG(0) "Too many variables (%d) in type checker", newMax
154 for (i=0; i<numTyvars;++i) { /* copy substitution */
155 newTvs[i].offs = tyvars[i].offs;
156 newTvs[i].bound = tyvars[i].bound;
157 newTvs[i].kind = tyvars[i].kind;
159 maxTyvars = 0; /* protection from SIGINT? */
160 if (tyvars) free(tyvars);
166 Int newTyvars(n) /* allocate new type variables */
167 Int n; { /* all of kind STAR */
168 Int beta = numTyvars;
171 for (numTyvars+=n; n>0; n--) {
172 tyvars[numTyvars-n].offs = UNUSED_GENERIC;
173 tyvars[numTyvars-n].bound = NIL;
174 tyvars[numTyvars-n].kind = STAR;
176 Printf("new type variable: _%d ::: ",numTyvars-n);
177 printKind(stdout,tyvars[numTyvars-n].kind);
184 Int newKindedVars(k) /* allocate new variables with */
185 Kind k; { /* specified kinds */
186 Int beta = numTyvars; /* if k = k0 -> k1 -> ... -> kn */
187 for (; isPair(k); k=snd(k)) { /* then allocate n vars with kinds */
188 expandSubst(1); /* k0, k1, ..., k(n-1) */
189 tyvars[numTyvars].offs = UNUSED_GENERIC;
190 tyvars[numTyvars].bound = NIL;
191 tyvars[numTyvars].kind = fst(k);
193 Printf("new type variable: _%d ::: ",numTyvars);
194 printKind(stdout,tyvars[numTyvars].kind);
202 Void instantiate(type) /* instantiate type, if nonNull */
208 if (nonNull(typeIs)) { /* instantiate type expression ? */
210 if (isPolyType(typeIs)) { /* Polymorphic type scheme ? */
211 Kinds ks = polySigOf(typeIs);
212 typeOff = newKindedVars(ks);
213 typeIs = monotypeOf(typeIs);
214 for (; isAp(ks); ks=arg(ks))
218 if (isQualType(typeIs)) { /* Qualified type? */
219 predsAre = fst(snd(typeIs));
220 typeIs = snd(snd(typeIs));
225 /* --------------------------------------------------------------------------
226 * Bound type variables:
227 * ------------------------------------------------------------------------*/
229 Pair findBtyvs(t) /* Look for bound tyvar */
232 for (; nonNull(bts); bts=tl(bts)) {
234 for (; nonNull(bts1); bts1=tl(bts1))
235 if (t==textOf(fst(hd(bts1))))
241 static Int local findBtyvsInt(t) /* Look for bound type variable */
242 Text t; { /* expecting to find an integer */
243 Pair p = findBtyvs(t);
245 internal("findBtyvsInt");
246 return intOf(snd(p));
249 Void markBtyvs() { /* Mark explicitly scoped vars */
251 for (; nonNull(bts); bts=tl(bts)) {
253 for (; nonNull(bts1); bts1=tl(bts1))
254 markTyvar(intOf(snd(hd(bts1))));
258 Type localizeBtyvs(t) /* Localize type to eliminate refs */
259 Type t; { /* to explicitly scoped vars */
262 case POLYTYPE : snd(snd(t)) = localizeBtyvs(snd(snd(t)));
265 case QUAL : fst(snd(t)) = localizeBtyvs(fst(snd(t)));
266 snd(snd(t)) = localizeBtyvs(snd(snd(t)));
269 case AP : fst(t) = localizeBtyvs(fst(t));
270 snd(t) = localizeBtyvs(snd(t));
274 case VAROPCELL: return mkInt(findBtyvsInt(textOf(t)));
279 /* --------------------------------------------------------------------------
280 * Dereference or bind types in subsitution:
281 * ------------------------------------------------------------------------*/
283 Tyvar *getTypeVar(t,o) /* get number of type variable */
284 Type t; /* represented by (t,o) [if any]. */
287 case INTCELL : return tyvar(intOf(t));
288 case OFFSET : return tyvar(o+offsetOf(t));
290 case VAROPCELL : return tyvar(findBtyvsInt(textOf(t)));
295 Void tyvarType(vn) /* load type held in type variable */
296 Int vn; { /* vn into (typeIs,typeOff) */
299 while ((tyv=tyvar(vn)), isBound(tyv))
300 switch(whatIs(tyv->bound)) {
301 case INTCELL : vn = intOf(tyv->bound);
304 case OFFSET : vn = offsetOf(tyv->bound)+(tyv->offs);
308 case VAROPCELL : vn = findBtyvsInt(textOf(tyv->bound));
311 default : typeIs = tyv->bound;
319 Void bindTv(vn,t,o) /* set type variable vn to (t,o) */
323 Tyvar *tyv = tyvar(vn);
327 Printf("binding type variable: _%d to ",vn);
328 printType(stdout,debugType(t,o));
333 Cell getDerefHead(t,o) /* get value at head of type exp. */
343 if ((tyv=getTypeVar(t,o)) && isBound(tyv)) {
353 /* --------------------------------------------------------------------------
354 * Expand type synonyms:
355 * ------------------------------------------------------------------------*/
357 Void expandSyn(h,ar,at,ao) /* Expand type synonym with: */
358 Tycon h; /* head h */
359 Int ar; /* ar args (NB. ar>=tycon(h).arity)*/
360 Type *at; /* original expression (*at,*ao) */
361 Int *ao; { /* expansion returned in (*at,*ao) */
362 ar -= tycon(h).arity; /* calculate surplus arguments */
365 else { /* if there are more args than the */
366 Type t = *at; /* arity, we have to do a little */
367 Int o = *ao; /* bit of work to isolate args that*/
368 Type args = NIL; /* will not be changed by expansion*/
370 while (ar-- > 0) { /* find part to expand, and the */
371 Tyvar *tyv; /* unused arguments */
372 args = cons(arg(t),args);
376 expandSyn1(h,&t,&o); /* do the expansion */
377 bindTv((i=newTyvars(1)),t,o); /* and embed the results back in */
378 tyvar(i)->kind = getKind(t,o); /* (*at, *ao) as required */
379 *at = applyToArgs(mkInt(i),args);
383 static Void local expandSyn1(h,at,ao) /* Expand type synonym with: */
384 Tycon h; /* head h, tycon(h).arity args, */
385 Type *at; /* original expression (*at,*ao) */
386 Int *ao; { /* expansion returned in (*at,*ao) */
387 Int n = tycon(h).arity;
393 *ao = newKindedVars(tycon(h).kind);
394 for (; 0<n--; t=fun(t)) {
397 internal("expandSyn1");
398 bindTv(*ao+n,arg(t),o);
402 /* --------------------------------------------------------------------------
403 * Marking fixed variables in type expressions:
404 * ------------------------------------------------------------------------*/
406 Void clearMarks() { /* Set all unbound type vars to */
407 Int i; /* unused generic variables */
408 for (i=0; i<numTyvars; ++i)
409 if (!isBound(tyvar(i)))
410 tyvar(i)->offs = UNUSED_GENERIC;
415 Void markAllVars() { /* Set all unbound type vars to */
416 Int i; /* be fixed vars */
417 for (i=0; i<numTyvars; ++i)
418 if (!isBound(tyvar(i)))
419 tyvar(i)->offs = FIXED_TYVAR;
424 Void resetGenerics() { /* Reset all generic vars to unused*/
426 for (i=0; i<numTyvars; ++i)
427 if (!isBound(tyvar(i)) && tyvar(i)->offs>=GENERIC)
428 tyvar(i)->offs = UNUSED_GENERIC;
433 Void markTyvar(vn) /* mark fixed vars in type bound to*/
434 Int vn; { /* given type variable */
435 Tyvar *tyv = tyvar(vn);
438 markType(tyv->bound, tyv->offs);
440 (tyv->offs) = FIXED_TYVAR;
443 Void markType(t,o) /* mark fixed vars in type (t,o) */
456 case AP : markType(fst(t),o);
460 case OFFSET : markTyvar(o+offsetOf(t));
463 case INTCELL : markTyvar(intOf(t));
467 case VAROPCELL : markTyvar(findBtyvsInt(textOf(t)));
470 case RANK2 : markType(snd(snd(t)),o);
473 default : internal("markType");
477 Void markPred(pi) /* Marked fixed type vars in pi */
480 Int o = intOf(snd3(pi));
482 for (; isAp(cl); cl=fun(cl))
486 /* --------------------------------------------------------------------------
487 * Copy type expression from substitution to make a single type expression:
488 * ------------------------------------------------------------------------*/
490 Type copyTyvar(vn) /* calculate most general form of */
491 Int vn; { /* type bound to given type var */
492 Tyvar *tyv = tyvar(vn);
494 if ((tyv->bound)==SKOLEM) {
496 } else if (tyv->bound) {
497 return copyType(tyv->bound,tyv->offs);
501 case FIXED_TYVAR : return mkInt(vn);
503 case UNUSED_GENERIC : (tyv->offs) = GENERIC + nextGeneric++;
504 if (nextGeneric>=(OFF_MAX-OFF_MIN+1)) {
506 "Too many quantified type variables"
509 genericVars = cons(mkInt(vn),genericVars);
511 default : return mkOffset(tyv->offs - GENERIC);
515 Type copyType(t,o) /* calculate most general form of */
516 Type t; /* type expression (t,o) */
520 case AP : { Type l = copyType(fst(t),o);/* ensure correct */
521 Type r = copyType(snd(t),o);/* eval. order */
524 case OFFSET : return copyTyvar(o+offsetOf(t));
525 case INTCELL : return copyTyvar(intOf(t));
527 case VAROPCELL : return copyTyvar(findBtyvsInt(textOf(t)));
533 Cell copyPred(pi,o) /* Copy single predicate (or part */
534 Cell pi; /* thereof) ... */
537 Cell temp = copyPred(fun(pi),o);/* to ensure correct order of eval.*/
538 return ap(temp,copyType(arg(pi),o));
544 Type zonkTyvar(vn) /* flatten type by chasing all references */
545 Int vn; { /* and collapsing OFFSETS to absolute indexes */
546 Tyvar *tyv = tyvar(vn);
549 return zonkType(tyv->bound,tyv->offs);
554 Type zonkType(t,o) /* flatten type by chasing all references */
555 Type t; /* and collapsing OFFSETS to absolute indexes */
559 case AP : { Type l = zonkType(fst(t),o);/* ensure correct */
560 Type r = zonkType(snd(t),o);/* eval. order */
563 case OFFSET : return zonkTyvar(o+offsetOf(t));
564 case INTCELL : return zonkTyvar(intOf(t));
571 Type debugTyvar(vn) /* expand type structure in full */
572 Int vn; { /* detail */
573 Tyvar *tyv = tyvar(vn);
576 return debugType(tyv->bound,tyv->offs);
585 case AP : { Type l = debugType(fst(t),o);
586 Type r = debugType(snd(t),o);
589 case OFFSET : return debugTyvar(o+offsetOf(t));
590 case INTCELL : return debugTyvar(intOf(t));
592 case VAROPCELL : return debugTyvar(findBtyvsInt(textOf(t)));
597 List debugContext(ps)
601 for (; nonNull(ps); ps=tl(ps)) {
602 p = debugPred(fst3(hd(ps)),intOf(snd3(hd(ps))));
612 return pair(debugPred(fun(pi),o),debugType(arg(pi),o));
616 #endif /*DEBUG_TYPES*/
618 Kind copyKindvar(vn) /* build kind attatched to variable*/
620 Tyvar *tyv = tyvar(vn);
622 return copyKind(tyv->bound,tyv->offs);
623 return STAR; /* any unbound variable defaults to*/
624 } /* the kind of all types */
626 Kind copyKind(k,o) /* build kind expression from */
627 Kind k; /* given skeleton */
630 case AP : { Kind l = copyKind(fst(k),o); /* ensure correct */
631 Kind r = copyKind(snd(k),o); /* eval. order */
634 case OFFSET : return copyKindvar(o+offsetOf(k));
635 case INTCELL : return copyKindvar(intOf(k));
640 /* --------------------------------------------------------------------------
641 * Copy type expression from substitution without marking:
642 * ------------------------------------------------------------------------*/
644 static List local listTyvar(vn,ns)
647 Tyvar *tyv = tyvar(vn);
650 return listTyvars(tyv->bound,tyv->offs,ns);
651 } else if (!intIsMember(vn,ns)) {
652 ns = cons(mkInt(vn),ns);
657 static List local listTyvars(t,o,ns)
662 case AP : return listTyvars(fst(t),o,
665 case OFFSET : return listTyvar(o+offsetOf(t),ns);
666 case INTCELL : return listTyvar(intOf(t),ns);
672 static Cell local dupTyvar(vn,ns)
675 Tyvar *tyv = tyvar(vn);
678 return dupTyvars(tyv->bound,tyv->offs,ns);
681 for (; nonNull(ns) && vn!=intOf(hd(ns)); ns=tl(ns)) {
688 static Cell local dupTyvars(t,o,ns)
693 case AP : { Type l = dupTyvars(fst(t),o,ns);
694 Type r = dupTyvars(snd(t),o,ns);
697 case OFFSET : return dupTyvar(o+offsetOf(t),ns);
698 case INTCELL : return dupTyvar(intOf(t),ns);
703 static Cell local copyNoMark(t,o) /* Copy a type or predicate without*/
704 Cell t; /* changing marks */
706 List ns = listTyvars(t,o,NIL);
707 Cell result = pair(ns,dupTyvars(t,o,ns));
708 for (; nonNull(ns); ns=tl(ns)) {
709 hd(ns) = tyvar(intOf(hd(ns)))->kind;
714 /* --------------------------------------------------------------------------
715 * Droping and lifting of type schemes that appear in rank 2 position:
716 * ------------------------------------------------------------------------*/
718 Type dropRank2(t,alpha,n) /* Drop a (potentially) rank2 type */
722 if (whatIs(t)==RANK2) {
723 Cell r = fst(snd(t));
726 for (t=snd(snd(t)); i>0; i--) {
727 Type a = arg(fun(t));
729 a = dropRank1(a,alpha,n);
733 t = ap(RANK2,pair(r,revOnto(as,t)));
738 Type dropRank1(t,alpha,n) /* Copy rank1 argument type t to */
739 Type t; /* make a rank1 type scheme */
742 if (n>0 && isPolyType(t))
743 t = mkPolyType(polySigOf(t),dropRank1Body(monotypeOf(t),alpha,n));
747 static Type local dropRank1Body(t,alpha,n)
752 case OFFSET : { Int m = offsetOf(t);
753 return (m>=n) ? mkOffset(m-n) : mkInt(alpha+m);
756 case POLYTYPE : return mkPolyType(polySigOf(t),
757 dropRank1Body(monotypeOf(t),alpha,n));
759 case QUAL : return ap(QUAL,dropRank1Body(snd(t),alpha,n));
761 case RANK2 : return ap(RANK2,pair(fst(snd(t)),
762 dropRank1Body(snd(snd(t)),
766 case AP : return ap(dropRank1Body(fun(t),alpha,n),
767 dropRank1Body(arg(t),alpha,n));
773 Void liftRank2Args(as,alpha,m)
780 for (m=nextGeneric; nonNull(as); as=tl(as)) {
781 Type ta = arg(fun(as));
782 ta = isPolyType(ta) ? liftRank1Body(ta,m) : copyType(ta,alpha);
788 Type liftRank2(t,alpha,m)
792 if (whatIs(t)==RANK2) {
793 Cell r = fst(snd(t));
800 for (i=intOf(r); i>0; i--) {
801 Type a = arg(fun(t));
802 a = isPolyType(a) ? liftRank1Body(a,m) : copyType(a,alpha);
806 t = ap(RANK2,pair(r,revOnto(as,copyType(t,alpha))));
809 t = copyType(t,alpha);
813 Type liftRank1(t,alpha,m)
817 if (m>0 && isPolyType(t)) {
822 t = liftRank1Body(t,nextGeneric);
827 static Type local liftRank1Body(t,n)
831 case OFFSET : return mkOffset(n+offsetOf(t));
833 case INTCELL : return copyTyvar(intOf(t));
836 case VAROPCELL : return copyTyvar(findBtyvsInt(textOf(t)));
838 case POLYTYPE : return mkPolyType(polySigOf(t),
839 liftRank1Body(monotypeOf(t),n));
841 case QUAL : return ap(QUAL,liftRank1Body(snd(t),n));
843 case RANK2 : return ap(RANK2,pair(fst(snd(t)),
844 liftRank1Body(snd(snd(t)),n)));
846 case AP : return ap(liftRank1Body(fun(t),n),
847 liftRank1Body(arg(t),n));
853 /* --------------------------------------------------------------------------
854 * Support for `kind preserving substitutions' from unification:
855 * ------------------------------------------------------------------------*/
857 Bool eqKind(k1,k2) /* check that two (mono)kinds are */
858 Kind k1, k2; { /* equal */
860 || (isPair(k1) && isPair(k2)
861 && eqKind(fst(k1),fst(k2))
862 && eqKind(snd(k1),snd(k2)));
865 Kind getKind(c,o) /* Find kind of constr during type */
866 Cell c; /* checking process */
868 if (isAp(c)) /* application */
869 return snd(getKind(fst(c),o));
871 case TUPLE : return simpleKind(tupleOf(c)); /*(,)::* -> * -> * */
872 case OFFSET : return tyvar(o+offsetOf(c))->kind;
873 case INTCELL : return tyvar(intOf(c))->kind;
875 case VAROPCELL : return tyvar(findBtyvsInt(textOf(c)))->kind;
876 case TYCON : return tycon(c).kind;
878 case EXT : return extKind;
882 Printf("getKind c = %d, whatIs=%d\n",c,whatIs(c));
885 return STAR;/* not reached */
888 /* --------------------------------------------------------------------------
889 * Find generic variables in a type:
890 * ------------------------------------------------------------------------*/
892 Type genvarTyvar(vn,vs) /* calculate list of generic vars */
893 Int vn; /* thru variable vn, prepended to */
894 List vs; { /* list vs */
895 Tyvar *tyv = tyvar(vn);
898 return genvarType(tyv->bound,tyv->offs,vs);
899 else if (tyv->offs == UNUSED_GENERIC) {
900 tyv->offs += GENERIC + nextGeneric++;
901 return cons(mkInt(vn),vs);
903 else if (tyv->offs>=GENERIC && !intIsMember(vn,vs))
904 return cons(mkInt(vn),vs);
909 List genvarType(t,o,vs) /* calculate list of generic vars */
910 Type t; /* in type expression (t,o) */
911 Int o; /* results are prepended to vs */
914 case AP : return genvarType(snd(t),o,genvarType(fst(t),o,vs));
915 case OFFSET : return genvarTyvar(o+offsetOf(t),vs);
916 case INTCELL : return genvarTyvar(intOf(t),vs);
918 case VAROPCELL : return genvarTyvar(findBtyvsInt(textOf(t)),vs);
923 /* --------------------------------------------------------------------------
925 * ------------------------------------------------------------------------*/
927 Bool doesntOccurIn(lookFor,t,o) /* Return TRUE if var lookFor */
928 Tyvar *lookFor; /* isn't referenced in (t,o) */
936 if (tyv) /* type variable */
938 else if (isAp(t)) { /* application */
939 if (doesntOccurIn(lookFor,snd(t),o))
944 else /* no variable found */
950 /* --------------------------------------------------------------------------
951 * Unification algorithm:
952 * ------------------------------------------------------------------------*/
954 char *unifyFails = 0; /* Unification error message */
955 static Int bindAbove = 0; /* Used to restrict var binding */
957 #define bindOnlyAbove(beta) bindAbove=beta
958 #define noBind() bindAbove=MAXPOSINT
959 #define unrestrictBind() bindAbove=0
961 static Bool local varToVarBind(tyv1,tyv2)/* Make binding tyv1 := tyv2 */
962 Tyvar *tyv1, *tyv2; {
963 if (tyv1!=tyv2) { /* If vars are same, nothing to do!*/
965 /* Check that either tyv1 or tyv2 is in allowed range for binding */
966 /* and is not a Skolem constant, and swap vars if nec. so we can */
969 if (tyvNum(tyv1)<bindAbove || tyv1->bound==SKOLEM) {
970 if (tyvNum(tyv2)<bindAbove || tyv2->bound==SKOLEM) {
971 unifyFails = "types do not match";
980 if (!eqKind(tyv1->kind,tyv2->kind)) {
981 unifyFails = "constructor variable kinds do not match";
985 tyv1->offs = tyvNum(tyv2);
987 Printf("vv binding tyvar: _%d to _%d\n",tyvNum(tyv1),tyvNum(tyv2));
993 static Bool local varToTypeBind(tyv,t,o)/* Make binding tyv := (t,o) */
995 Type t; /* guaranteed not to be a v'ble or */
996 Int o; { /* have synonym as outermost constr*/
997 if (tyvNum(tyv)<bindAbove) { /* Check that tyv is in range */
998 unifyFails = "types do not match";
1001 else if (tyv->bound == SKOLEM) { /* Check that it is not Skolemized */
1002 unifyFails = "cannot instantiate Skolem constant";
1005 else if (!doesntOccurIn(tyv,t,o)) /* Carry out occurs check */
1006 unifyFails = "unification would give infinite type";
1007 else if (!eqKind(tyv->kind,getKind(t,o)))
1008 unifyFails = "kinds do not match";
1013 Printf("vt binding type variable: _%d to ",tyvNum(tyv));
1014 printType(stdout,debugType(t,o));
1022 Bool unify(t1,o1,t2,o2) /* Main unification routine */
1023 Type t1,t2; /* unify (t1,o1) with (t2,o2) */
1033 return varToVarBind(tyv1,tyv2); /* t1, t2 variables */
1035 Cell h2 = getDerefHead(t2,o2); /* t1 variable, t2 not */
1036 if (isSynonym(h2) && argCount>=tycon(h2).arity) {
1037 expandSyn(h2,argCount,&t2,&o2);
1041 return varToTypeBind(tyv1,t2,o2);
1046 Cell h1 = getDerefHead(t1,o1); /* t2 variable, t1 not */
1047 if (isSynonym(h1) && argCount>=tycon(h1).arity) {
1048 expandSyn(h1,argCount,&t1,&o1);
1052 return varToTypeBind(tyv2,t1,o1);
1054 else { /* t1, t2 not vars */
1055 Type h1 = getDerefHead(t1,o1);
1057 Type h2 = getDerefHead(t2,o2);
1061 Printf("tt unifying types: ");
1062 printType(stdout,debugType(t1,o1));
1064 printType(stdout,debugType(t2,o2));
1067 if (isOffset(h1) || isInt(h1)) h1=NIL; /* represent var by NIL*/
1068 if (isOffset(h2) || isInt(h2)) h2=NIL;
1071 if (isExt(h1) || isExt(h2)) {
1072 if (a1==2 && isExt(h1) && a2==2 && isExt(h2)) {
1073 if (extText(h1)==extText(h2)) {
1074 return unify(arg(fun(t1)),o1,arg(fun(t2)),o2) &&
1075 unify(arg(t1),o1,arg(t2),o2);
1077 return inserter(t1,o1,t2,o2) &&
1078 unify(arg(t1),o1,aVar,
1079 remover(extText(h1),t2,o2));
1082 unifyFails = "rows are not compatible";
1087 if (nonNull(h1) && h1==h2) {/* Assuming well-formed types, both*/
1088 if (a1!=a2) { /* t1, t2 must have same no of args*/
1089 unifyFails = "incompatible constructors";
1093 if (!unify(arg(t1),o1,arg(t2),o2))
1104 /* Types do not match -- look for type synonyms to expand */
1106 if (isSynonym(h1) && a1>=tycon(h1).arity) {
1107 expandSyn(h1,a1,&t1,&o1);
1111 if (isSynonym(h2) && a2>=tycon(h2).arity) {
1112 expandSyn(h2,a2,&t2,&o2);
1117 if ((isNull(h1) && a1<=a2) || /* last attempt -- maybe */
1118 (isNull(h2) && a2<=a1)) { /* one head is a variable? */
1123 if (tyv1) { /* unify heads! */
1125 return varToVarBind(tyv1,tyv2);
1127 return varToTypeBind(tyv1,t2,o2);
1130 return varToTypeBind(tyv2,t1,o1);
1132 /* at this point, neither t1 nor t2 is a variable. In */
1133 /* addition, they must both be APs unless one of the */
1134 /* head variables has been bound during unification of */
1135 /* the arguments. */
1137 if (!isAp(t1) || !isAp(t2)) { /* might not be APs*/
1141 if (!unify(arg(t1),o1,arg(t2),o2)) /* o/w must be APs */
1153 static Bool local inserter(r1,o1,r,o) /* Insert first field in (r1,o1) */
1154 Type r1; /* into row (r,o), both of which */
1155 Int o1; /* are known to begin with an EXT */
1158 Text labt = extText(fun(fun(r1))); /* Find the text of the label */
1160 Printf("inserting ");
1161 printType(stdout,debugType(r1,o1));
1163 printType(stdout,debugType(r,o));
1170 Int beta; /* Test for common tail */
1171 if (tailVar(arg(r1),o1)==tyvNum(tyv)) {
1172 unifyFails = "distinct rows have common tail";
1175 beta = newTyvars(1); /* Extend row with new field */
1176 tyvar(beta)->kind = ROW;
1177 return varToTypeBind(tyv,ap(fun(r1),mkInt(beta)),o1);
1179 else if (isAp(r) && isAp(fun(r)) && isExt(fun(fun(r)))) {
1180 if (labt==extText(fun(fun(r))))/* Compare existing fields */
1181 return unify(arg(fun(r1)),o1,extField(r),o);
1182 r = extRow(r); /* Or skip to next field */
1184 else { /* Nothing else will match */
1185 unifyFails = "field mismatch";
1191 static Int local remover(l,r,o) /* Make a new row by copying (r,o) */
1192 Text l; /* but removing the l field (which */
1193 Type r; /* MUST exist) */
1196 Int beta = newTyvars(1);
1197 tyvar(beta)->kind = ROW;
1199 Printf("removing %s from",textToStr(l));
1200 printType(stdout,debugType(r,o));
1204 if (tyv || !isAp(r) || !isAp(fun(r)) || !isExt(fun(fun(r))))
1205 internal("remover");
1206 if (l==extText(fun(fun(r))))
1209 r = ap(fun(r),mkInt(remover(l,extRow(r),o)));
1215 static Int local tailVar(r,o) /* Find var at tail end of a row */
1224 else if (isAp(r) && isAp(fun(r)) && isExt(fun(fun(r)))) {
1235 Bool typeMatches(type,mt) /* test if type matches monotype mt*/
1236 Type type, mt; { /* imported from STG Hugs */
1238 if (isPolyOrQualType(type))
1240 emptySubstitution();
1242 result = unify(mt,0,type,0);
1244 emptySubstitution();
1248 Bool isProgType(ks,type) /* Test if type is of the form */
1249 List ks; /* IO t for some t. */
1254 emptySubstitution();
1255 alpha = newKindedVars(ks);
1256 beta = newTyvars(1);
1257 bindOnlyAbove(beta);
1258 result = unify(type,alpha,typeProgIO,beta);
1260 emptySubstitution();
1264 /* --------------------------------------------------------------------------
1265 * Matching predicates:
1267 * There are (at least) four situations where we need to match up pairs
1270 * 1) Testing to see if two predicates are the same (ignoring differences
1271 * caused by the use of type synonyms, for example).
1273 * 2) Matching a predicate with the head of its class so that we can
1274 * find the corresponding superclass predicates. If the predicates
1275 * have already been kind-checked, and the classes are known to be
1276 * the same, then this should never fail.
1278 * 3) Matching a predicate against the head of an instance to see if
1279 * that instance is applicable.
1281 * 4) Matching two instance heads to see if there is an overlap.
1283 * For (1), we need a matching process that does not bind any variables.
1284 * For (2) and (3), we need to use one-way matching, only allowing
1285 * variables in the class or instance head to be instantiated. For
1286 * (4), we need two-way unification.
1288 * Another situation in which both one-way and two-way unification might
1289 * be used is in an implementation of improvement. Here, a one-way match
1290 * would be used to determine applicability of a rule for improvement
1291 * that would then be followed by unification with another predicate.
1292 * One possible syntax for this might be:
1294 * instance P => pi [improves pi'] where ...
1296 * The intention here is that any predicate matching pi' can be unified
1297 * with pi to get more accurate types. A simple example of this is:
1299 * instance Collection [a] a improves Collection [a] b where ...
1301 * As soon as we know what the collection type is (in this case, a list),
1302 * we will also know what the element type is. To ensure that the rule
1303 * for improvement is valid, the compilation system will also need to use
1304 * a one-way matching process to ensure that pi is a (substitution) instance
1305 * of pi'. Another extension would be to allow more than one predicate pi'
1306 * in an improving rule. Read the paper on simplification and improvement
1307 * for technical background. Watch this space for implementation news!
1308 * ------------------------------------------------------------------------*/
1310 Bool samePred(pi1,o1,pi,o) /* Test to see if predicates are */
1311 Cell pi1; /* the same, with no binding of */
1312 Int o1; /* the variables in either one. */
1313 Cell pi; /* Assumes preds are kind correct */
1314 Int o; { /* with the same class. */
1317 result = unifyPred(pi1,o1,pi,o);
1322 Bool matchPred(pi1,o1,pi,o) /* One way match predicate (pi1,o1)*/
1323 Cell pi1; /* against (pi,o), allowing only */
1324 Int o1; /* vars in 2nd pred to be bound. */
1325 Cell pi; /* Assumes preds are kind correct */
1326 Int o; { /* with the same class and that no */
1327 Bool result; /* vars have been alloc'd since o. */
1329 result = unifyPred(pi1,o1,pi,o);
1334 Bool unifyPred(pi1,o1,pi,o) /* Unify two predicates */
1335 Cell pi1; /* Assumes preds are kind correct */
1336 Int o1; /* with the same class. */
1339 for (; isAp(pi1); pi1=fun(pi1), pi=fun(pi)) {
1340 if (!isAp(pi) || !unify(arg(pi1),o1,arg(pi),o))
1343 /* pi1 has exhausted its argument chain, we also need to check that
1344 pi has no remaining arguments. However, under this condition,
1345 the pi1 == pi will always return FALSE, giving the desired
1349 if (isIP(pi1) && isIP(pi))
1350 return textOf(pi1)==textOf(pi);
1357 static Cell trexShow = NIL; /* Used to test for show on records*/
1358 static Cell trexEq = NIL; /* Used to test for eq on records */
1361 Inst findInstFor(pi,o) /* Find matching instance for pred */
1362 Cell pi; /* (pi,o), or otherwise NIL. If a */
1363 Int o; { /* match is found, then tyvars from*/
1364 Class c = getHead(pi); /* typeOff have been initialized to*/
1365 List ins; /* allow direct use of specifics. */
1371 for (ins=cclass(c).instances; nonNull(ins); ins=tl(ins)) {
1373 Int beta = newKindedVars(inst(in).kinds);
1374 if (matchPred(pi,o,inst(in).head,beta)) {
1381 Int alpha = newKindedVars(inst(in).kinds);
1383 kspi = copyNoMark(pi,o);
1385 beta = newKindedVars(fst(kspi));
1386 if (matchPred(inst(in).head,alpha,snd(kspi),beta)) {
1397 { Bool wantShow = (c==findQualClass(trexShow));
1398 Bool wantEither = wantShow || (c==findQualClass(trexEq));
1400 if (wantEither) { /* Generate instances of */
1401 Type t = arg(pi); /* ShowRecRow and EqRecRow */
1402 Tyvar *tyv; /* on the fly */
1408 for (ins=cclass(c).instances; nonNull(ins); ins=tl(ins))
1409 if (getHead(arg(inst(hd(ins)).head))==e) {
1414 in = (wantShow ? addRecShowInst(c,e) : addRecEqInst(c,e));
1415 typeOff = newKindedVars(extKind);
1416 bindTv(typeOff,arg(fun(t)),o);
1417 bindTv(typeOff+1,arg(t),o);
1428 List findInstsFor(pi,o) /* Find matching instance for pred */
1429 Cell pi; /* (pi,o), or otherwise NIL. If a */
1430 Int o; { /* match is found, then tyvars from*/
1431 Class c = getHead(pi); /* typeOff have been initialized to*/
1432 List ins; /* allow direct use of specifics. */
1438 for (ins=cclass(c).instances; nonNull(ins); ins=tl(ins)) {
1440 Int beta = newKindedVars(inst(in).kinds);
1441 if (matchPred(pi,o,inst(in).head,beta)) {
1442 res = cons (pair (beta, in), res);
1456 /* --------------------------------------------------------------------------
1458 * ------------------------------------------------------------------------*/
1460 Void improve(line,sps,ps) /* Improve a list of predicates */
1468 for (ps1=ps; nonNull(ps1); ps1=tl(ps1)) {
1469 Cell pi = fst3(hd(ps1));
1470 Int o = intOf(snd3(hd(ps1)));
1471 Cell c = getHead(pi);
1472 if ((isClass(c) && nonNull(cclass(c).xfds)) || isIP(c)) {
1473 improved |= improveAgainst(line,sps,pi,o);
1475 improved |= instImprove(line,c,pi,o);
1476 improved |= improveAgainst(line,tl(ps1),pi,o);
1482 Void improve1(line,sps,pi,o) /* Improve a single predicate */
1488 Cell c = getHead(pi);
1491 if ((isClass(c) && nonNull(cclass(c).xfds)) || isIP(c)) {
1492 improved |= improveAgainst(line,sps,pi,o);
1494 improved |= instImprove(line,c,pi,o);
1499 Bool improveAgainst(line,ps,pi,o)
1504 Bool improved = FALSE;
1505 Cell h = getHead(pi);
1506 for (; nonNull(ps); ps=tl(ps)) {
1508 Cell pi1 = fst3(pr);
1509 Int o1 = intOf(snd3(pr));
1510 Cell h1 = getHead(pi1);
1511 /* it would be nice to optimize for the common case
1513 if (isClass(h) && isClass(h1)) {
1514 improved |= pairImprove(line,h,pi,o,pi1,o1,numTyvars);
1516 improved |= pairImprove(line,h1,pi1,o1,pi,o,numTyvars);
1519 else if (isIP(h1) && textOf(h1) == textOf(h))
1520 improved |= ipImprove(line,pi,o,pi1,o1);
1525 /* should emulate findInsts behavior of shorting out if the
1526 predicate would match a more general signature... */
1528 Bool instImprove(line,c,pi,o)
1533 Bool improved = FALSE;
1534 List ins = cclass(c).instances;
1535 for (; nonNull(ins); ins=tl(ins)) {
1537 Int alpha = newKindedVars(inst(in).kinds);
1538 improved |= pairImprove(line,c,pi,o,inst(in).head,alpha,alpha);
1544 Bool ipImprove(line,pi,o,pi1,o1)
1552 if (!sameType(t,o,t1,o1)) {
1553 if (!unify(t,o,t1,o1)) {
1554 ERRMSG(line) "Mismatching uses of implicit parameter\n"
1557 ETHEN ERRPRED(copyPred(pi1,o1));
1559 ETHEN ERRPRED(copyPred(pi,o));
1569 Bool pairImprove(line,c,pi1,o1,pi2,o2,above) /* Look for improvement of (pi1,o1)*/
1570 Int line; /* against (pi2,o2) */
1577 Bool improved = FALSE;
1578 List xfds = cclass(c).xfds;
1579 for (; nonNull(xfds); xfds=tl(xfds)) {
1580 Cell xfd = hd(xfds);
1583 for (; nonNull(hs); hs=tl(hs)) {
1585 Class d = getHead(h);
1586 alpha = newKindedVars(cclass(d).kinds);
1587 if (matchPred(pi2,o2,h,alpha))
1592 List fds = snd(xfd);
1593 for (; nonNull(fds); fds=tl(fds)) {
1594 List as = fst(hd(fds));
1596 for (; same && nonNull(as); as=tl(as)) {
1597 Int n = offsetOf(hd(as));
1598 same &= matchTypeAbove(nthArg(n,pi1),o1,
1599 mkOffset(n),alpha,above);
1601 if (isNull(as) && same) {
1602 for (as=snd(hd(fds)); same && nonNull(as); as=tl(as)) {
1603 Int n = offsetOf(hd(as));
1604 Type t1 = nthArg(n,pi1);
1605 Type t2 = mkOffset(n);
1606 if (!matchTypeAbove(t1,o1,t2,alpha,above)) {
1607 same &= unify(t1,o1,t2,alpha);
1613 "Constraints are not consistent with functional dependency"
1615 ERRTEXT "\n*** Constraint : "
1616 ETHEN ERRPRED(copyPred(pi1,o1));
1617 ERRTEXT "\n*** And constraint : "
1618 ETHEN ERRPRED(copyPred(pi2,o2));
1619 ERRTEXT "\n*** For class : "
1620 ETHEN ERRPRED(cclass(c).head);
1621 ERRTEXT "\n*** Break dependency : "
1622 ETHEN ERRFD(hd(fds));
1634 /* --------------------------------------------------------------------------
1635 * Compare type schemes:
1636 * ------------------------------------------------------------------------*/
1638 Bool sameSchemes(s,s1) /* Test to see whether two type */
1639 Type s; /* schemes are the same */
1644 Bool b = isPolyType(s); /* Check quantifiers are the same */
1645 Bool b1 = isPolyType(s1);
1647 if (b && b1 && eqKind(polySigOf(s),polySigOf(s1))) {
1648 Kind k = polySigOf(s);
1650 s1 = monotypeOf(s1);
1651 o = newKindedVars(k);
1652 for (; isAp(k); k=arg(k))
1659 b = (whatIs(s)==QUAL); /* Check that contexts are the same*/
1660 b1 = (whatIs(s1)==QUAL);
1663 List ps = fst(snd(s));
1664 List ps1 = fst(snd(s1));
1666 while (nonNull(ps) && nonNull(ps1)) {
1669 if (getHead(pi)!=getHead(pi1)
1670 || !unifyPred(pi,o,pi1,o))
1676 if (nonNull(ps) || nonNull(ps1))
1685 b = (whatIs(s)==RANK2); /* Check any rank 2 annotations */
1686 b1 = (whatIs(s1)==RANK2);
1688 if (b && b1 && intOf(fst(snd(s)))==intOf(fst(snd(s1)))) {
1689 nr2 = intOf(fst(snd(s)));
1697 for (; nr2>0; nr2--) { /* Deal with rank 2 arguments */
1698 Type t = arg(fun(s));
1699 Type t1 = arg(fun(s1));
1700 b = isPolyOrQualType(t);
1701 b1 = isPolyOrQualType(t1);
1704 t = dropRank1(t,o,m);
1705 t1 = dropRank1(t1,o,m);
1706 if (!sameSchemes(t,t1))
1713 if (!sameType(t,o,t1,o)) {
1722 return sameType(s,o,s1,o); /* Ensure body types are the same */
1725 Bool sameType(t1,o1,t,o) /* Test to see if types are */
1726 Type t1; /* the same, with no binding of */
1727 Int o1; /* the variables in either one. */
1728 Cell t; /* Assumes types are kind correct */
1729 Int o; { /* with the same kind. */
1732 result = unify(t1,o1,t,o);
1737 Bool matchType(t1,o1,t,o) /* One way match type (t1,o1) */
1738 Type t1; /* against (t,o), allowing only */
1739 Int o1; /* vars in 2nd type to be bound. */
1740 Type t; /* Assumes types are kind correct */
1741 Int o; { /* and that no vars have been */
1742 Bool result; /* alloc'd since o. */
1744 result = unify(t1,o1,t,o);
1749 static Bool local matchTypeAbove(t1,o1,t,o,a) /* match, allowing only vars */
1750 Type t1; /* allocated since `a' to be bound */
1751 Int o1; /* this is deeply hacky, since it */
1752 Type t; /* relies on careful use of the */
1753 Int o; /* substitution stack */
1757 result = unify(t1,o1,t,o);
1762 /* --------------------------------------------------------------------------
1763 * Unify kind expressions:
1764 * ------------------------------------------------------------------------*/
1766 static Bool local kvarToVarBind(tyv1,tyv2)/* Make binding tyv1 := tyv2 */
1767 Tyvar *tyv1, *tyv2; { /* for kind variable bindings */
1770 tyv1->offs = tyvNum(tyv2);
1772 Printf("vv binding kvar: _%d to _%d\n",tyvNum(tyv1),tyvNum(tyv2));
1778 static Bool local kvarToTypeBind(tyv,t,o)/* Make binding tyv := (t,o) */
1779 Tyvar *tyv; /* for kind variable bindings */
1780 Type t; /* guaranteed not to be a v'ble or */
1781 Int o; { /* have synonym as outermost constr*/
1782 if (doesntOccurIn(tyv,t,o)) {
1786 Printf("vt binding kind variable: _%d to ",tyvNum(tyv));
1787 printType(stdout,debugType(t,o));
1792 unifyFails = "unification would give infinite kind";
1796 Bool kunify(k1,o1,k2,o2) /* Unify kind expr (k1,o1) with */
1797 Kind k1,k2; /* (k2,o2) */
1806 return kvarToVarBind(kyv1,kyv2); /* k1, k2 variables */
1808 return kvarToTypeBind(kyv1,k2,o2); /* k1 variable, k2 not */
1812 return kvarToTypeBind(kyv2,k1,o1); /* k2 variable, k1 not */
1815 Printf("unifying kinds: ");
1816 printType(stdout,debugType(k1,o1));
1818 printType(stdout,debugType(k2,o2));
1821 if (k1==STAR && k2==STAR) /* k1, k2 not vars */
1824 else if (k1==ROW && k2==ROW)
1827 else if (isAp(k1) && isAp(k2))
1828 return kunify(fst(k1),o1,fst(k2),o2) &&
1829 kunify(snd(k1),o1,snd(k2),o2);
1835 /* --------------------------------------------------------------------------
1836 * Tuple type constructors: are generated as necessary. The most common
1837 * n-tuple constructors (n<MAXTUPCON) are held in a cache to avoid
1838 * repeated generation of the constructor types.
1839 * ------------------------------------------------------------------------*/
1841 #define MAXTUPCON 10
1842 static Type tupleConTypes[MAXTUPCON];
1844 Void typeTuple(e) /* find type for tuple constr, using*/
1845 Cell e; { /* tupleConTypes to cache previously*/
1846 Int n = tupleOf(e); /* calculated tuple constr. types. */
1847 typeOff = newTyvars(n);
1849 typeIs = makeTupleType(n);
1850 else if (tupleConTypes[n])
1851 typeIs = tupleConTypes[n];
1853 typeIs = tupleConTypes[n] = makeTupleType(n);
1856 static Type local makeTupleType(n) /* construct type for tuple constr. */
1857 Int n; { /* t1 -> ... -> tn -> (t1,...,tn) */
1858 Type h = mkTuple(n);
1862 h = ap(h,mkOffset(i));
1864 h = fn(mkOffset(n),h);
1868 /* --------------------------------------------------------------------------
1869 * Two forms of kind expression are used quite frequently:
1870 * * -> * -> ... -> * -> * for kinds of ->, [], ->, (,) etc...
1871 * v1 -> v2 -> ... -> vn -> vn+1 skeletons for constructor kinds
1872 * Expressions of these forms are produced by the following functions which
1873 * use a cache to avoid repeated construction of commonly used values.
1874 * A similar approach is used to store the types of tuple constructors in the
1875 * main type checker.
1876 * ------------------------------------------------------------------------*/
1878 #define MAXKINDFUN 10
1879 static Kind simpleKindCache[MAXKINDFUN];
1880 static Kind varKindCache[MAXKINDFUN];
1882 static Kind local makeSimpleKind(n) /* construct * -> ... -> * (n args)*/
1890 Kind simpleKind(n) /* return (possibly cached) simple */
1891 Int n; { /* function kind */
1893 return makeSimpleKind(n);
1894 else if (nonNull(simpleKindCache[n]))
1895 return simpleKindCache[n];
1897 return simpleKindCache[0] = STAR;
1899 return simpleKindCache[n] = ap(STAR,simpleKind(n-1));
1902 static Kind local makeVarKind(n) /* construct v0 -> .. -> vn */
1904 Kind k = mkOffset(n);
1906 k = ap(mkOffset(n),k);
1910 Void varKind(n) /* return (possibly cached) var */
1911 Int n; { /* function kind */
1912 typeOff = newKindvars(n+1);
1914 typeIs = makeVarKind(n);
1915 else if (nonNull(varKindCache[n]))
1916 typeIs = varKindCache[n];
1918 typeIs = varKindCache[n] = makeVarKind(n);
1921 /* --------------------------------------------------------------------------
1922 * Substitutution control:
1923 * ------------------------------------------------------------------------*/
1925 Void substitution(what)
1930 case RESET : emptySubstitution();
1935 case MARK : for (i=0; i<MAXTUPCON; ++i)
1936 mark(tupleConTypes[i]);
1937 for (i=0; i<MAXKINDFUN; ++i) {
1938 mark(simpleKindCache[i]);
1939 mark(varKindCache[i]);
1941 for (i=0; i<numTyvars; ++i)
1942 mark(tyvars[i].bound);
1953 case POSTPREL: break;
1955 case PREPREL : substitution(RESET);
1956 for (i=0; i<MAXTUPCON; ++i)
1957 tupleConTypes[i] = NIL;
1958 for (i=0; i<MAXKINDFUN; ++i) {
1959 simpleKindCache[i] = NIL;
1960 varKindCache[i] = NIL;
1963 trexShow = mkQCon(findText("Trex"),
1964 findText("ShowRecRow"));
1965 trexEq = mkQCon(findText("Trex"),
1966 findText("EqRecRow"));
1972 /*-------------------------------------------------------------------------*/