2 /* --------------------------------------------------------------------------
3 * Static Analysis for Hugs
5 * The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
6 * Yale Haskell Group, and the Oregon Graduate Institute of Science and
7 * Technology, 1994-1999, All rights reserved. It is distributed as
8 * free software under the license in the file "License", which is
9 * included in the distribution.
11 * $RCSfile: static.c,v $
13 * $Date: 1999/11/29 18:59:30 $
14 * ------------------------------------------------------------------------*/
24 /* --------------------------------------------------------------------------
25 * local function prototypes:
26 * ------------------------------------------------------------------------*/
28 static Void local kindError Args((Int,Constr,Constr,String,Kind,Int));
29 static Void local checkQualImport Args((Pair));
30 static Void local checkUnqualImport Args((Triple));
32 static Name local lookupName Args((Text,List));
33 static List local checkSubentities Args((List,List,List,String,Text));
34 static List local checkExportTycon Args((List,Text,Cell,Tycon));
35 static List local checkExportClass Args((List,Text,Cell,Class));
36 static List local checkExport Args((List,Text,Cell));
37 static List local checkImportEntity Args((List,Module,Cell));
38 static List local resolveImportList Args((Module,Cell));
39 static Void local checkImportList Args((Pair));
41 static Void local importEntity Args((Module,Cell));
42 static Void local importName Args((Module,Name));
43 static Void local importTycon Args((Module,Tycon));
44 static Void local importClass Args((Module,Class));
45 static List local checkExports Args((List));
47 static Void local checkTyconDefn Args((Tycon));
48 static Void local depConstrs Args((Tycon,List,Cell));
49 static List local addSels Args((Int,Name,List,List));
50 static List local selectCtxt Args((List,List));
51 static Void local checkSynonyms Args((List));
52 static List local visitSyn Args((List,Tycon,List));
53 static Type local instantiateSyn Args((Type,Type));
55 static Void local checkClassDefn Args((Class));
56 static Cell local depPredExp Args((Int,List,Cell));
57 static Void local checkMems Args((Class,List,Cell));
58 static Void local checkMems2 Args((Class,Cell));
59 static Void local addMembers Args((Class));
60 static Name local newMember Args((Int,Int,Cell,Type,Class));
61 static Name local newDSel Args((Class,Int));
62 static Text local generateText Args((String,Class));
63 static Int local visitClass Args((Class));
65 static List local classBindings Args((String,Class,List));
66 static Name local memberName Args((Class,Text));
67 static List local numInsert Args((Int,Cell,List));
69 static List local maybeAppendVar Args((Cell,List));
71 static Type local checkSigType Args((Int,String,Cell,Type));
72 static Void local checkOptQuantVars Args((Int,List,List));
73 static Type local depTopType Args((Int,List,Type));
74 static Type local depCompType Args((Int,List,Type));
75 static Type local depTypeExp Args((Int,List,Type));
76 static Type local depTypeVar Args((Int,List,Text));
77 static List local checkQuantVars Args((Int,List,List,Cell));
78 static List local otvars Args((Cell,List));
79 static Bool local osubset Args((List,List));
80 static Void local kindConstr Args((Int,Int,Int,Constr));
81 static Kind local kindAtom Args((Int,Constr));
82 static Void local kindPred Args((Int,Int,Int,Cell));
83 static Void local kindType Args((Int,String,Type));
84 static Void local fixKinds Args((Void));
86 static Void local kindTCGroup Args((List));
87 static Void local initTCKind Args((Cell));
88 static Void local kindTC Args((Cell));
89 static Void local genTC Args((Cell));
91 static Void local checkInstDefn Args((Inst));
92 static Void local insertInst Args((Inst));
93 static Bool local instCompare Args((Inst,Inst));
94 static Name local newInstImp Args((Inst));
95 static Void local kindInst Args((Inst,Int));
96 static Void local checkDerive Args((Tycon,List,List,Cell));
97 static Void local addDerInst Args((Int,Class,List,List,Type,Int));
98 static Void local deriveContexts Args((List));
99 static Void local initDerInst Args((Inst));
100 static Void local calcInstPreds Args((Inst));
101 static Void local maybeAddPred Args((Cell,Int,Int,List));
102 static List local calcFunDeps Args((List));
103 static Cell local copyAdj Args((Cell,Int,Int));
104 static Void local tidyDerInst Args((Inst));
105 static List local otvarsZonk Args((Cell,List,Int));
107 static Void local addDerivImp Args((Inst));
109 static Void local checkDefaultDefns Args((Void));
111 static Void local checkForeignImport Args((Name));
112 static Void local checkForeignExport Args((Name));
114 static Cell local tidyInfix Args((Int,Cell));
115 static Pair local attachFixity Args((Int,Cell));
116 static Syntax local lookupSyntax Args((Text));
118 static Cell local checkPat Args((Int,Cell));
119 static Cell local checkMaybeCnkPat Args((Int,Cell));
120 static Cell local checkApPat Args((Int,Int,Cell));
121 static Void local addToPatVars Args((Int,Cell));
122 static Name local conDefined Args((Int,Cell));
123 static Void local checkIsCfun Args((Int,Name));
124 static Void local checkCfunArgs Args((Int,Cell,Int));
125 static Cell local checkPatType Args((Int,String,Cell,Type));
126 static Cell local applyBtyvs Args((Cell));
127 static Cell local bindPat Args((Int,Cell));
128 static Void local bindPats Args((Int,List));
130 static List local extractSigdecls Args((List));
131 static List local extractFixdecls Args((List));
132 static List local extractBindings Args((List));
133 static List local getPatVars Args((Int,Cell,List));
134 static List local addPatVar Args((Int,Cell,List));
135 static List local eqnsToBindings Args((List,List,List,List));
136 static Void local notDefined Args((Int,List,Cell));
137 static Cell local findBinding Args((Text,List));
138 static Cell local getAttr Args((List,Cell));
139 static Void local addSigdecl Args((List,Cell));
140 static Void local addFixdecl Args((List,List,List,List,Triple));
141 static Void local dupFixity Args((Int,Text));
142 static Void local missFixity Args((Int,Text));
144 static List local dependencyAnal Args((List));
145 static List local topDependAnal Args((List));
146 static Void local addDepField Args((Cell));
147 static Void local remDepField Args((List));
148 static Void local remDepField1 Args((Cell));
149 static Void local clearScope Args((Void));
150 static Void local withinScope Args((List));
151 static Void local leaveScope Args((Void));
152 static Void local saveSyntax Args((Cell,Cell));
154 static Void local depBinding Args((Cell));
155 static Void local depDefaults Args((Class));
156 static Void local depInsts Args((Inst));
157 static Void local depClassBindings Args((List));
158 static Void local depAlt Args((Cell));
159 static Void local depRhs Args((Cell));
160 static Void local depGuard Args((Cell));
161 static Cell local depExpr Args((Int,Cell));
162 static Void local depPair Args((Int,Cell));
163 static Void local depTriple Args((Int,Cell));
164 static Void local depComp Args((Int,Cell,List));
165 static Void local depCaseAlt Args((Int,Cell));
166 static Cell local depVar Args((Int,Cell));
167 static Cell local depQVar Args((Int,Cell));
168 static Void local depConFlds Args((Int,Cell,Bool));
169 static Void local depUpdFlds Args((Int,Cell));
170 static List local depFields Args((Int,Cell,List,Bool));
172 static Void local depWith Args((Int,Cell));
173 static List local depDwFlds Args((Int,Cell,List));
176 static Cell local depRecord Args((Int,Cell));
179 static List local tcscc Args((List,List));
180 static List local bscc Args((List));
182 static Void local addRSsigdecls Args((Pair));
183 static Void local allNoPrevDef Args((Cell));
184 static Void local noPrevDef Args((Int,Cell));
185 static Bool local odiff Args((List,List));
187 static Void local duplicateErrorAux Args((Int,Module,Text,String));
188 #define duplicateError(l,m,t,k) duplicateErrorAux(l,m,t,k)
189 static Void local checkTypeIn Args((Pair));
191 /* --------------------------------------------------------------------------
192 * The code in this file is arranged in roughly the following order:
193 * - Kind inference preliminaries
194 * - Module declarations
195 * - Type declarations (data, type, newtype, type in)
196 * - Class declarations
198 * - Instance declarations
199 * - Default declarations
200 * - Primitive definitions
202 * - Infix expressions
203 * - Value definitions
204 * - Top-level static analysis and control
205 * - Haskell 98 compatibility tests
206 * ------------------------------------------------------------------------*/
208 /* --------------------------------------------------------------------------
209 * Kind checking preliminaries:
210 * ------------------------------------------------------------------------*/
212 Bool kindExpert = FALSE; /* TRUE => display kind errors in */
215 static Void local kindError(l,c,in,wh,k,o)
216 Int l; /* line number near constuctor exp */
217 Constr c; /* constructor */
218 Constr in; /* context (if any) */
219 String wh; /* place in which error occurs */
220 Kind k; /* expected kind (k,o) */
221 Int o; { /* inferred kind (typeIs,typeOff) */
224 if (!kindExpert) { /* for those with a fear of kinds */
225 ERRMSG(l) "Illegal type" ETHEN
227 ERRTEXT " \"" ETHEN ERRTYPE(in);
230 ERRTEXT " in %s\n", wh
234 ERRMSG(l) "Kind error in %s", wh ETHEN
236 ERRTEXT "\n*** expression : " ETHEN ERRTYPE(in);
238 ERRTEXT "\n*** constructor : " ETHEN ERRTYPE(c);
239 ERRTEXT "\n*** kind : " ETHEN ERRKIND(copyType(typeIs,typeOff));
240 ERRTEXT "\n*** does not match : " ETHEN ERRKIND(copyType(k,o));
242 ERRTEXT "\n*** because : %s", unifyFails ETHEN
248 #define shouldKind(l,c,in,wh,k,o) if (!kunify(typeIs,typeOff,k,o)) \
249 kindError(l,c,in,wh,k,o)
250 #define checkKind(l,a,m,c,in,wh,k,o) kindConstr(l,a,m,c); \
251 shouldKind(l,c,in,wh,k,o)
252 #define inferKind(k,o) typeIs=k; typeOff=o
254 static List unkindTypes; /* types in need of kind annotation*/
256 Kind extKind; /* Kind of extension, *->row->row */
259 /* --------------------------------------------------------------------------
260 * Static analysis of modules:
261 * ------------------------------------------------------------------------*/
267 Void startModule(nm) /* switch to a new module */
270 if (!isCon(nm)) internal("startModule");
271 if (isNull(m = findModule(textOf(nm))))
272 m = newModule(textOf(nm));
273 else if (!isPreludeScript()) {
274 /* You're allowed to break the rules in the Prelude! */
276 reloadModule = textToStr(textOf(nm));
278 ERRMSG(0) "Module \"%s\" already loaded", textToStr(textOf(nm))
284 Void setExportList(exps) /* Add export list to current module */
286 module(currentModule).exports = exps;
289 Void addQualImport(orig,new) /* Add to qualified import list */
290 Cell orig; /* Original name of module */
291 Cell new; { /* Name module is called within this module (or NIL) */
292 module(currentModule).qualImports =
293 cons(pair(isNull(new)?orig:new,orig),module(currentModule).qualImports);
296 Void addUnqualImport(mod,entities) /* Add to unqualified import list */
297 Cell mod; /* Name of module */
298 List entities; { /* List of entity names */
299 unqualImports = cons(pair(mod,entities),unqualImports);
302 static Void local checkQualImport(i) /* Process qualified import */
304 Module m = findModid(snd(i));
306 ERRMSG(0) "Module \"%s\" not previously loaded",
307 textToStr(textOf(snd(i)))
313 static Void local checkUnqualImport(i) /* Process unqualified import */
315 Module m = findModid(fst(i));
317 ERRMSG(0) "Module \"%s\" not previously loaded",
318 textToStr(textOf(fst(i)))
324 static Name local lookupName(t,nms) /* find text t in list of Names */
326 List nms; { /* :: [Name] */
327 for(; nonNull(nms); nms=tl(nms)) {
328 if (t == name(hd(nms)).text)
334 static List local checkSubentities(imports,named,wanted,description,textParent)
336 List named; /* :: [ Q?(Var|Con)(Id|Op) ] */
337 List wanted; /* :: [Name] */
338 String description; /* "<constructor>|<member> of <type>|<class>" */
340 for(; nonNull(named); named=tl(named)) {
342 /* ToDo: ignores qualifier; doesn't check that entity is in scope */
343 Text t = isPair(snd(x)) ? qtextOf(x) : textOf(x);
344 Name n = lookupName(t,wanted);
346 ERRMSG(0) "Entity \"%s\" is not a %s \"%s\"",
349 textToStr(textParent)
352 imports = cons(n,imports);
357 static List local checkImportEntity(imports,exporter,entity)
358 List imports; /* Accumulated list of things to import */
360 Cell entity; { /* Entry from import list */
361 List oldImports = imports;
362 Text t = isIdent(entity) ? textOf(entity) : textOf(fst(entity));
363 List es = module(exporter).exports;
364 for(; nonNull(es); es=tl(es)) {
365 Cell e = hd(es); /* :: Entity | (Entity, NIL|DOTDOT) */
369 if (tycon(f).text == t) {
370 imports = cons(f,imports);
371 if (!isIdent(entity)) {
372 switch (tycon(f).what) {
375 if (DOTDOT == snd(entity)) {
376 imports=dupOnto(tycon(f).defn,imports);
378 imports=checkSubentities(imports,snd(entity),tycon(f).defn,
379 "constructor of type",t);
383 /* deliberate fall thru */
387 } else if (isClass(f)) {
388 if (cclass(f).text == t) {
389 imports = cons(f,imports);
390 if (!isIdent(entity)) {
391 if (DOTDOT == snd(entity)) {
392 return dupOnto(cclass(f).members,imports);
394 return checkSubentities(imports,snd(entity),cclass(f).members,
395 "member of class",t);
400 internal("checkImportEntity2");
402 } else if (isName(e)) {
403 if (isIdent(entity) && name(e).text == t) {
404 imports = cons(e,imports);
407 internal("checkImportEntity3");
410 if (imports == oldImports) {
411 ERRMSG(0) "Unknown entity \"%s\" imported from module \"%s\"",
413 textToStr(module(exporter ).text)
419 static List local resolveImportList(m,impList)
420 Module m; /* exporting module */
423 if (DOTDOT == impList) {
424 List es = module(m).exports;
425 for(; nonNull(es); es=tl(es)) {
428 imports = cons(e,imports);
431 List subentities = NIL;
432 imports = cons(c,imports);
434 && (tycon(c).what == DATATYPE
435 || tycon(c).what == NEWTYPE))
436 subentities = tycon(c).defn;
438 subentities = cclass(c).members;
439 if (DOTDOT == snd(e)) {
440 imports = dupOnto(subentities,imports);
445 map1Accum(checkImportEntity,imports,m,impList);
450 static Void local checkImportList(importSpec) /*Import a module unqualified*/
452 Module m = fst(importSpec);
453 Cell impList = snd(importSpec);
455 List imports = NIL; /* entities we want to import */
456 List hidden = NIL; /* entities we want to hide */
458 if (moduleThisScript(m)) {
459 ERRMSG(0) "Module \"%s\" recursively imports itself",
460 textToStr(module(m).text)
463 if (isPair(impList) && HIDDEN == fst(impList)) {
464 /* Somewhat inefficient - but obviously correct:
465 * imports = importsOf("module Foo") `setDifference` hidden;
467 hidden = resolveImportList(m, snd(impList));
468 imports = resolveImportList(m, DOTDOT);
470 imports = resolveImportList(m, impList);
472 for(; nonNull(imports); imports=tl(imports)) {
473 Cell e = hd(imports);
474 if (!cellIsMember(e,hidden))
477 /* ToDo: hang onto the imports list for processing export list entries
478 * of the form "module Foo"
482 static Void local importEntity(source,e)
486 case NAME : importName(source,e);
488 case TYCON : importTycon(source,e);
490 case CLASS : importClass(source,e);
492 default: internal("importEntity");
496 static Void local importName(source,n)
499 Name clash = addName(n);
500 if (nonNull(clash) && clash!=n) {
501 ERRMSG(0) "Entity \"%s\" imported from module \"%s\" already defined in module \"%s\"",
502 textToStr(name(n).text),
503 textToStr(module(source).text),
504 textToStr(module(name(clash).mod).text)
509 static Void local importTycon(source,tc)
512 Tycon clash=addTycon(tc);
513 if (nonNull(clash) && clash!=tc) {
514 ERRMSG(0) "Tycon \"%s\" imported from \"%s\" already defined in module \"%s\"",
515 textToStr(tycon(tc).text),
516 textToStr(module(source).text),
517 textToStr(module(tycon(clash).mod).text)
520 if (nonNull(findClass(tycon(tc).text))) {
521 ERRMSG(0) "Import of type constructor \"%s\" clashes with class in module \"%s\"",
522 textToStr(tycon(tc).text),
523 textToStr(module(tycon(tc).mod).text)
528 static Void local importClass(source,c)
531 Class clash=addClass(c);
532 if (nonNull(clash) && clash!=c) {
533 ERRMSG(0) "Class \"%s\" imported from \"%s\" already defined in module \"%s\"",
534 textToStr(cclass(c).text),
535 textToStr(module(source).text),
536 textToStr(module(cclass(clash).mod).text)
539 if (nonNull(findTycon(cclass(c).text))) {
540 ERRMSG(0) "Import of class \"%s\" clashes with type constructor in module \"%s\"",
541 textToStr(cclass(c).text),
542 textToStr(module(source).text)
547 static List local checkExportTycon(exports,mt,spec,tc)
552 if (DOTDOT == spec || SYNONYM == tycon(tc).what) {
553 return cons(pair(tc,DOTDOT), exports);
555 return cons(pair(tc,NIL), exports);
559 static List local checkExportClass(exports,mt,spec,cl)
564 if (DOTDOT == spec) {
565 return cons(pair(cl,DOTDOT), exports);
567 return cons(pair(cl,NIL), exports);
571 static List local checkExport(exports,mt,e) /* Process entry in export list*/
577 List origExports = exports;
578 if (nonNull(export=findQualName(e))) {
579 exports=cons(export,exports);
581 if (isQCon(e) && nonNull(export=findQualTycon(e))) {
582 exports = checkExportTycon(exports,mt,NIL,export);
584 if (isQCon(e) && nonNull(export=findQualClass(e))) {
585 /* opaque class export */
586 exports = checkExportClass(exports,mt,NIL,export);
588 if (exports == origExports) {
589 ERRMSG(0) "Unknown entity \"%s\" exported from module \"%s\"",
595 } else if (MODULEENT == fst(e)) {
596 Module m = findModid(snd(e));
597 /* ToDo: shouldn't allow export of module we didn't import */
599 ERRMSG(0) "Unknown module \"%s\" exported from module \"%s\"",
600 textToStr(textOf(snd(e))),
604 if (m == currentModule) {
605 /* Exporting the current module exports local definitions */
607 for(xs=module(m).classes; nonNull(xs); xs=tl(xs)) {
608 if (cclass(hd(xs)).mod==m)
609 exports = checkExportClass(exports,mt,DOTDOT,hd(xs));
611 for(xs=module(m).tycons; nonNull(xs); xs=tl(xs)) {
612 if (tycon(hd(xs)).mod==m)
613 exports = checkExportTycon(exports,mt,DOTDOT,hd(xs));
615 for(xs=module(m).names; nonNull(xs); xs=tl(xs)) {
616 if (name(hd(xs)).mod==m)
617 exports = cons(hd(xs),exports);
620 /* Exporting other modules imports all things imported
621 * unqualified from it.
622 * ToDo: we reexport everything exported by a module -
623 * whether we imported it or not. This gives the wrong
624 * result for "module M(module N) where import N(x)"
626 exports = dupOnto(module(m).exports,exports);
630 Cell ident = fst(e); /* class name or type name */
631 Cell parts = snd(e); /* members or constructors */
633 if (isQCon(ident) && nonNull(nm=findQualTycon(ident))) {
634 switch (tycon(nm).what) {
637 ERRMSG(0) "Explicit constructor list given for type synonym"
638 " \"%s\" in export list of module \"%s\"",
643 return cons(pair(nm,DOTDOT),exports);
645 ERRMSG(0) "Transparent export of restricted type synonym"
646 " \"%s\" in export list of module \"%s\"",
650 return exports; /* Not reached */
654 return cons(pair(nm,DOTDOT),exports);
656 exports = checkSubentities(exports,parts,tycon(nm).defn,
657 "constructor of type",
659 return cons(pair(nm,DOTDOT), exports);
662 internal("checkExport1");
664 } else if (isQCon(ident) && nonNull(nm=findQualClass(ident))) {
665 if (DOTDOT == parts) {
666 return cons(pair(nm,DOTDOT),exports);
668 exports = checkSubentities(exports,parts,cclass(nm).members,
669 "member of class",cclass(nm).text);
670 return cons(pair(nm,DOTDOT), exports);
673 ERRMSG(0) "Explicit export list given for non-class/datatype \"%s\" in export list of module \"%s\"",
679 return exports; /* NOTUSED */
682 static List local checkExports(exports)
684 Module m = lastModule();
685 Text mt = module(m).text;
688 map1Accum(checkExport,es,mt,exports);
691 for(xs=es; nonNull(xs); xs=tl(xs)) {
692 Printf(" %s", textToStr(textOfEntity(hd(xs))));
699 /* --------------------------------------------------------------------------
700 * Static analysis of type declarations:
702 * Type declarations come in two forms:
703 * - data declarations - define new constructed data types
704 * - type declarations - define new type synonyms
706 * A certain amount of work is carried out as the declarations are
707 * read during parsing. In particular, for each type constructor
708 * definition encountered:
709 * - check that there is no previous definition of constructor
710 * - ensure type constructor not previously used as a class name
711 * - make a new entry in the type constructor table
712 * - record line number of declaration
713 * - Build separate lists of newly defined constructors for later use.
714 * ------------------------------------------------------------------------*/
716 Void tyconDefn(line,lhs,rhs,what) /* process new type definition */
717 Int line; /* definition line number */
718 Cell lhs; /* left hand side of definition */
719 Cell rhs; /* right hand side of definition */
720 Cell what; { /* SYNONYM/DATATYPE/etc... */
721 Text t = textOf(getHead(lhs));
723 if (nonNull(findTycon(t))) {
724 ERRMSG(line) "Repeated definition of type constructor \"%s\"",
728 else if (nonNull(findClass(t))) {
729 ERRMSG(line) "\"%s\" used as both class and type constructor",
734 Tycon nw = newTycon(t);
735 tyconDefns = cons(nw,tyconDefns);
736 tycon(nw).line = line;
737 tycon(nw).arity = argCount;
738 tycon(nw).what = what;
739 if (what==RESTRICTSYN) {
740 h98DoesntSupport(line,"restricted type synonyms");
741 typeInDefns = cons(pair(nw,snd(rhs)),typeInDefns);
744 tycon(nw).defn = pair(lhs,rhs);
748 Void setTypeIns(bs) /* set local synonyms for given */
749 List bs; { /* binding group */
750 List cvs = typeInDefns;
751 for (; nonNull(cvs); cvs=tl(cvs)) {
752 Tycon c = fst(hd(cvs));
753 List vs = snd(hd(cvs));
754 for (tycon(c).what = RESTRICTSYN; nonNull(vs); vs=tl(vs)) {
755 if (nonNull(findBinding(textOf(hd(vs)),bs))) {
756 tycon(c).what = SYNONYM;
763 Void clearTypeIns() { /* clear list of local synonyms */
764 for (; nonNull(typeInDefns); typeInDefns=tl(typeInDefns))
765 tycon(fst(hd(typeInDefns))).what = RESTRICTSYN;
768 /* --------------------------------------------------------------------------
769 * Further analysis of Type declarations:
771 * In order to allow the definition of mutually recursive families of
772 * data types, the static analysis of the right hand sides of type
773 * declarations cannot be performed until all of the type declarations
776 * Once parsing is complete, we carry out the following:
778 * - check format of lhs, extracting list of bound vars and ensuring that
779 * there are no repeated variables and no Skolem variables.
780 * - run dependency analysis on rhs to check that only bound type vars
781 * appear in type and that all constructors are defined.
782 * Replace type variables by offsets, constructors by Tycons.
783 * - use list of dependents to sort into strongly connected components.
784 * - ensure that there is not more than one synonym in each group.
785 * - kind-check each group of type definitions.
787 * - check that there are no previous definitions for constructor
788 * functions in data type definitions.
789 * - install synonym expansions and constructor definitions.
790 * ------------------------------------------------------------------------*/
792 static List tcDeps = NIL; /* list of dependent tycons/classes*/
794 static Void local checkTyconDefn(d) /* validate type constructor defn */
796 Cell lhs = fst(tycon(d).defn);
797 Cell rhs = snd(tycon(d).defn);
798 Int line = tycon(d).line;
799 List tyvars = getArgs(lhs);
801 /* check for repeated tyvars on lhs*/
802 for (temp=tyvars; nonNull(temp); temp=tl(temp))
803 if (nonNull(varIsMember(textOf(hd(temp)),tl(temp)))) {
804 ERRMSG(line) "Repeated type variable \"%s\" on left hand side",
805 textToStr(textOf(hd(temp)))
809 tcDeps = NIL; /* find dependents */
810 switch (whatIs(tycon(d).what)) {
812 case SYNONYM : rhs = depTypeExp(line,tyvars,rhs);
813 if (cellIsMember(d,tcDeps)) {
814 ERRMSG(line) "Recursive type synonym \"%s\"",
815 textToStr(tycon(d).text)
821 case NEWTYPE : depConstrs(d,tyvars,rhs);
825 default : internal("checkTyconDefn");
830 tycon(d).kind = tcDeps;
834 static Void local depConstrs(t,tyvars,cd)
835 Tycon t; /* Define constructor functions and*/
836 List tyvars; /* do dependency analysis for data */
837 Cell cd; { /* definitions (w or w/o deriving) */
838 Int line = tycon(t).line;
843 List derivs = snd(cd);
844 List compTypes = NIL;
848 for (i=0; i<tycon(t).arity; ++i) /* build representation for tycon */
849 lhs = ap(lhs,mkOffset(i)); /* applied to full comp. of args */
851 if (isQualType(cs)) { /* allow for possible context */
854 map2Over(depPredExp,line,tyvars,ctxt);
855 h98CheckCtxt(line,"context",TRUE,ctxt,NIL);
858 if (nonNull(cs) && isNull(tl(cs))) /* Single constructor datatype? */
861 for (; nonNull(cs); cs=tl(cs)) { /* For each constructor function: */
863 List sig = dupList(tyvars);
864 List evs = NIL; /* locally quantified vars */
865 List lps = NIL; /* locally bound predicates */
866 List ctxt1 = ctxt; /* constructor function context */
867 List scs = NIL; /* strict components */
868 List fs = NONE; /* selector names */
869 Type type = lhs; /* constructor function type */
870 Int arity = 0; /* arity of constructor function */
871 Int nr2 = 0; /* Number of rank 2 args */
872 Name n; /* name for constructor function */
874 if (whatIs(con)==POLYTYPE) { /* Locally quantified vars */
877 sig = checkQuantVars(line,evs,sig,con);
880 if (isQualType(con)) { /* Local predicates */
883 for (us = typeVarsIn(lps,NIL,NIL,NIL); nonNull(us); us=tl(us))
884 if (!varIsMember(textOf(hd(us)),evs)) {
886 "Variable \"%s\" in constraint is not locally bound",
887 textToStr(textOf(hd(us)))
890 map2Over(depPredExp,line,sig,lps);
895 if (whatIs(con)==LABC) { /* Skeletize constr components */
896 Cell fls = snd(snd(con)); /* get field specifications */
899 for (; nonNull(fls); fls=tl(fls)) { /* for each field spec: */
900 List vs = fst(hd(fls));
901 Type t = snd(hd(fls)); /* - scrutinize type */
902 Bool banged = whatIs(t)==BANG;
903 t = depCompType(line,sig,(banged ? arg(t) : t));
904 while (nonNull(vs)) { /* - add named components */
912 scs = cons(mkInt(arity),scs);
916 scs = rev(scs); /* put strict comps in ascend ord */
918 else { /* Non-labelled constructor */
921 for (; isAp(c); c=fun(c))
923 for (compNo=arity, c=con; isAp(c); c=fun(c)) {
925 if (whatIs(t)==BANG) {
926 scs = cons(mkInt(compNo),scs);
930 arg(c) = depCompType(line,sig,t);
934 if (nonNull(ctxt1)) /* Extract relevant part of context*/
935 ctxt1 = selectCtxt(ctxt1,offsetTyvarsIn(con,NIL));
937 for (i=arity; isAp(con); i--) { /* Calculate type of constructor */
940 fun(con) = typeArrow;
941 if (isPolyOrQualType(cmp)) {
942 if (nonNull(derivs)) {
943 ERRMSG(line) "Cannot derive instances for types" ETHEN
944 ERRTEXT " with polymorphic or qualified components"
950 if (nonNull(derivs)) /* and build list of components */
951 compTypes = cons(cmp,compTypes);
956 if (nr2>0) { /* Add rank 2 annotation */
957 type = ap(RANK2,pair(mkInt(nr2-length(lps)),type));
960 if (nonNull(evs)) { /* Add existential annotation */
961 if (nonNull(derivs)) {
962 ERRMSG(line) "Cannot derive instances for types" ETHEN
963 ERRTEXT " with existentially typed components"
968 "Cannot use selectors with existentially typed components"
971 type = ap(EXIST,pair(mkInt(length(evs)),type));
974 if (nonNull(lps)) { /* Add local preds part to type */
975 type = ap(CDICTS,pair(lps,type));
978 if (nonNull(ctxt1)) { /* Add context part to type */
979 type = ap(QUAL,pair(ctxt1,type));
982 if (nonNull(sig)) { /* Add quantifiers to type */
984 for (; nonNull(ts1); ts1=tl(ts1)) {
987 type = mkPolyType(sig,type);
990 n = findName(textOf(con)); /* Allocate constructor fun name */
992 n = newName(textOf(con),NIL);
993 } else if (name(n).defn!=PREDEFINED) {
994 duplicateError(line,name(n).mod,name(n).text,
995 "constructor function");
997 name(n).arity = arity; /* Save constructor fun details */
1000 name(n).number = cfunNo(conNo++);
1001 name(n).type = type;
1002 if (tycon(t).what==NEWTYPE) {
1005 "A newtype constructor cannot have class constraints"
1010 "A newtype constructor must have exactly one argument"
1015 "Illegal strictess annotation for newtype constructor"
1018 name(n).defn = nameId;
1020 implementCfun(n,scs);
1025 sels = addSels(line,n,fs,sels);
1029 if (nonNull(sels)) {
1031 fst(cd) = appendOnto(fst(cd),sels);
1032 selDefns = cons(sels,selDefns);
1035 if (nonNull(derivs)) { /* Generate derived instances */
1036 map3Proc(checkDerive,t,ctxt,compTypes,derivs);
1040 Int userArity(c) /* Find arity for cfun, ignoring */
1041 Name c; { /* CDICTS parameters */
1042 Int a = name(c).arity;
1043 Type t = name(c).type;
1045 if (isPolyType(t)) {
1048 if ((w=whatIs(t))==QUAL) {
1049 w = whatIs(t=snd(snd(t)));
1052 a -= length(fst(snd(t)));
1058 static List local addSels(line,c,fs,ss) /* Add fields to selector list */
1059 Int line; /* line number of constructor */
1060 Name c; /* corresponding constr function */
1061 List fs; /* list of fields (varids) */
1062 List ss; { /* list of existing selectors */
1064 cfunSfuns = cons(pair(c,fs),cfunSfuns);
1065 for (; nonNull(fs); fs=tl(fs), ++sn) {
1067 Text t = textOf(hd(fs));
1069 if (nonNull(varIsMember(t,tl(fs)))) {
1070 ERRMSG(line) "Repeated field name \"%s\" for constructor \"%s\"",
1071 textToStr(t), textToStr(name(c).text)
1075 while (nonNull(ns) && t!=name(hd(ns)).text) {
1080 name(hd(ns)).defn = cons(pair(c,mkInt(sn)),name(hd(ns)).defn);
1082 Name n = findName(t);
1084 ERRMSG(line) "Repeated definition for selector \"%s\"",
1089 name(n).line = line;
1090 name(n).number = SELNAME;
1091 name(n).defn = singleton(pair(c,mkInt(sn)));
1098 static List local selectCtxt(ctxt,vs) /* calculate subset of context */
1105 for (; nonNull(ctxt); ctxt=tl(ctxt)) {
1106 List us = offsetTyvarsIn(hd(ctxt),NIL);
1107 for (; nonNull(us) && cellIsMember(hd(us),vs); us=tl(us)) {
1110 ps = cons(hd(ctxt),ps);
1117 static Void local checkSynonyms(ts) /* Check for mutually recursive */
1118 List ts; { /* synonyms */
1120 for (; nonNull(ts); ts=tl(ts)) { /* build list of all synonyms */
1122 switch (whatIs(tycon(t).what)) {
1124 case RESTRICTSYN : syns = cons(t,syns);
1128 while (nonNull(syns)) { /* then visit each synonym */
1129 syns = visitSyn(NIL,hd(syns),syns);
1133 static List local visitSyn(path,t,syns) /* visit synonym definition to look*/
1134 List path; /* for cycles */
1137 if (cellIsMember(t,path)) { /* every elt in path depends on t */
1138 ERRMSG(tycon(t).line)
1139 "Type synonyms \"%s\" and \"%s\" are mutually recursive",
1140 textToStr(tycon(t).text), textToStr(tycon(hd(path)).text)
1143 List ds = tycon(t).kind;
1145 for (; nonNull(ds); ds=tl(ds)) {
1146 if (cellIsMember(hd(ds),syns)) {
1147 if (isNull(path1)) {
1148 path1 = cons(t,path);
1150 syns = visitSyn(path1,hd(ds),syns);
1154 tycon(t).defn = fullExpand(tycon(t).defn);
1155 return removeCell(t,syns);
1158 /* --------------------------------------------------------------------------
1159 * Expanding out all type synonyms in a type expression:
1160 * ------------------------------------------------------------------------*/
1162 Type fullExpand(t) /* find full expansion of type exp */
1163 Type t; { /* assuming that all relevant */
1164 Cell h = t; /* synonym defns of lower rank have*/
1165 Int n = 0; /* already been fully expanded */
1167 for (args=NIL; isAp(h); h=fun(h), n++) {
1168 args = cons(fullExpand(arg(h)),args);
1170 t = applyToArgs(h,args);
1171 if (isSynonym(h) && n>=tycon(h).arity) {
1172 if (n==tycon(h).arity) {
1173 t = instantiateSyn(tycon(h).defn,t);
1176 while (--n > tycon(h).arity) {
1179 fun(p) = instantiateSyn(tycon(h).defn,fun(p));
1185 static Type local instantiateSyn(t,env) /* instantiate type according using*/
1186 Type t; /* env to determine appropriate */
1187 Type env; { /* values for OFFSET type vars */
1188 switch (whatIs(t)) {
1189 case AP : return ap(instantiateSyn(fun(t),env),
1190 instantiateSyn(arg(t),env));
1192 case OFFSET : return nthArg(offsetOf(t),env);
1198 /* --------------------------------------------------------------------------
1199 * Static analysis of class declarations:
1201 * Performed in a similar manner to that used for type declarations.
1203 * The first part of the static analysis is performed as the declarations
1204 * are read during parsing. The parser ensures that:
1205 * - the class header and all superclass predicates are of the form
1208 * The classDefn() function:
1209 * - ensures that there is no previous definition for class
1210 * - checks that class name has not previously been used as a type constr.
1211 * - make new entry in class table
1212 * - record line number of declaration
1213 * - build list of classes defined in current script for use in later
1214 * stages of static analysis.
1215 * ------------------------------------------------------------------------*/
1217 Void classDefn(line,head,ms,fds) /* process new class definition */
1218 Int line; /* definition line number */
1219 Cell head; /* class header :: ([Supers],Class) */
1220 List ms; /* class definition body */
1221 List fds; { /* functional dependencies */
1222 Text ct = textOf(getHead(snd(head)));
1223 Int arity = argCount;
1225 if (nonNull(findClass(ct))) {
1226 ERRMSG(line) "Repeated definition of class \"%s\"",
1229 } else if (nonNull(findTycon(ct))) {
1230 ERRMSG(line) "\"%s\" used as both class and type constructor",
1234 Class nw = newClass(ct);
1235 cclass(nw).line = line;
1236 cclass(nw).arity = arity;
1237 cclass(nw).head = snd(head);
1238 cclass(nw).supers = fst(head);
1239 cclass(nw).members = ms;
1240 cclass(nw).level = 0;
1241 cclass(nw).fds = fds;
1242 cclass(nw).xfds = NIL;
1243 classDefns = cons(nw,classDefns);
1245 h98DoesntSupport(line,"multiple parameter classes");
1249 /* --------------------------------------------------------------------------
1250 * Further analysis of class declarations:
1252 * Full static analysis of class definitions must be postponed until the
1253 * complete script has been read and all static analysis on type definitions
1254 * has been completed.
1256 * Once this has been achieved, we carry out the following checks on each
1258 * - check that variables in header are distinct
1259 * - replace head by skeleton
1260 * - check superclass declarations, replace by skeletons
1261 * - split body of class into members and declarations
1262 * - make new name entry for each member function
1263 * - record member function number (eventually an offset into dictionary!)
1264 * - no member function has a previous definition ...
1265 * - no member function is mentioned more than once in the list of members
1266 * - each member function type is valid, replace vars by offsets
1267 * - qualify each member function type by class header
1268 * - only bindings for members appear in defaults
1269 * - only function bindings appear in defaults
1270 * - check that extended class hierarchy does not contain any cycles
1271 * ------------------------------------------------------------------------*/
1273 static Void local checkClassDefn(c) /* validate class definition */
1276 Int args = cclass(c).arity - 1;
1277 Cell temp = cclass(c).head;
1281 for (; isAp(temp); temp=fun(temp)) {
1282 if (!isVar(arg(temp))) {
1283 ERRMSG(cclass(c).line) "Type variable required in class head"
1286 if (nonNull(varIsMember(textOf(arg(temp)),tyvars))) {
1287 ERRMSG(cclass(c).line)
1288 "Repeated type variable \"%s\" in class head",
1289 textToStr(textOf(arg(temp)))
1292 tyvars = cons(arg(temp),tyvars);
1295 for (fs=cclass(c).fds; nonNull(fs); fs=tl(fs)) {
1299 /* Check for trivial dependency
1302 ERRMSG(cclass(c).line) "Functional dependency is trivial"
1306 /* Check for duplicated vars on right hand side, and for vars on
1307 * right that also appear on the left:
1309 for (vs=snd(fd); nonNull(vs); vs=tl(vs)) {
1310 if (varIsMember(textOf(hd(vs)),fst(fd))) {
1311 ERRMSG(cclass(c).line)
1312 "Trivial dependency for variable \"%s\"",
1313 textToStr(textOf(hd(vs)))
1316 if (varIsMember(textOf(hd(vs)),tl(vs))) {
1317 ERRMSG(cclass(c).line)
1318 "Repeated variable \"%s\" in functional dependency",
1319 textToStr(textOf(hd(vs)))
1322 hd(vs) = depTypeVar(cclass(c).line,tyvars,textOf(hd(vs)));
1325 /* Check for duplicated vars on left hand side:
1327 for (vs=fst(fd); nonNull(vs); vs=tl(vs)) {
1328 if (varIsMember(textOf(hd(vs)),tl(vs))) {
1329 ERRMSG(cclass(c).line)
1330 "Repeated variable \"%s\" in functional dependency",
1331 textToStr(textOf(hd(vs)))
1334 hd(vs) = depTypeVar(cclass(c).line,tyvars,textOf(hd(vs)));
1338 if (cclass(c).arity==0) {
1341 Int args = cclass(c).arity - 1;
1342 for (temp=cclass(c).head; args>0; temp=fun(temp), args--) {
1343 arg(temp) = mkOffset(args);
1345 arg(temp) = mkOffset(0);
1349 tcDeps = NIL; /* find dependents */
1350 map2Over(depPredExp,cclass(c).line,tyvars,cclass(c).supers);
1351 h98CheckCtxt(cclass(c).line,"class definition",FALSE,cclass(c).supers,NIL);
1352 cclass(c).numSupers = length(cclass(c).supers);
1353 cclass(c).defaults = extractBindings(cclass(c).members); /* defaults*/
1354 ss = extractSigdecls(cclass(c).members);
1355 fs = extractFixdecls(cclass(c).members);
1356 cclass(c).members = pair(ss,fs);
1357 map2Proc(checkMems,c,tyvars,ss);
1359 cclass(c).kinds = tcDeps;
1364 /* --------------------------------------------------------------------------
1365 * Functional dependencies are inherited from superclasses.
1366 * For example, if I've got the following classes:
1368 * class C a b | a -> b
1369 * class C [b] a => D a b
1371 * then C will have the dependency ([a], [b]) as expected, and D will inherit
1372 * the dependency ([b], [a]) from C.
1373 * When doing pairwise improvement, we have to consider not just improving
1374 * when we see a pair of Cs or a pair of Ds in the context, but when we've
1375 * got a C and a D as well. In this case, we only improve when the
1376 * predicate in question matches the type skeleton in the relevant superclass
1377 * constraint. E.g., we improve the pair (C [Int] a, D b Int) (unifying
1378 * a and b), but we don't improve the pair (C Int a, D b Int).
1379 * To implement functional dependency inheritance, we calculate
1380 * the closure of all functional dependencies, and store the result
1381 * in an additional field `xfds' (extended functional dependencies).
1382 * The `xfds' field is a list of functional dependency lists, annotated
1383 * with a list of predicate skeletons constraining when improvement can
1384 * happen against this dependency list. For example, the xfds field
1385 * for C above would be:
1386 * [([C a b], [([a], [b])])]
1387 * and the xfds field for D would be:
1388 * [([C [b] a, D a b], [([b], [a])])]
1389 * Self-improvement (of a C with a C, or a D with a D) is treated as a
1390 * special case of an inherited dependency.
1391 * ------------------------------------------------------------------------*/
1392 static List local inheritFundeps ( Class c, Cell pi, Int o )
1394 Int alpha = newKindedVars(cclass(c).kinds);
1395 List scs = cclass(c).supers;
1398 /* better not fail ;-) */
1399 if (!matchPred(pi,o,cclass(c).head,alpha))
1400 internal("inheritFundeps - predicate failed to match it's own head!");
1401 this = copyPred(pi,o);
1402 for (; nonNull(scs); scs=tl(scs)) {
1403 Class s = getHead(hd(scs));
1405 List sfds = inheritFundeps(s,hd(scs),alpha);
1406 for (; nonNull(sfds); sfds=tl(sfds)) {
1408 xfds = cons(pair(cons(this,fst(h)),snd(h)),xfds);
1412 if (nonNull(cclass(c).fds)) {
1413 List fds = NIL, fs = cclass(c).fds;
1414 for (; nonNull(fs); fs=tl(fs)) {
1415 fds = cons(pair(otvars(this,fst(hd(fs))),
1416 otvars(this,snd(hd(fs)))),fds);
1418 xfds = cons(pair(cons(this,NIL),fds),xfds);
1423 static Void local extendFundeps ( Class c )
1426 emptySubstitution();
1427 alpha = newKindedVars(cclass(c).kinds);
1428 cclass(c).xfds = inheritFundeps(c,cclass(c).head,alpha);
1430 /* we can now check for ambiguity */
1431 map1Proc(checkMems2,c,fst(cclass(c).members));
1435 static Cell local depPredExp(line,tyvars,pred)
1442 for (; isAp(h); args++) {
1443 arg(h) = depTypeExp(line,tyvars,arg(h));
1449 h98DoesntSupport(line,"tag classes");
1450 } else if (args!=1) {
1451 h98DoesntSupport(line,"multiple parameter classes");
1454 if (isQCon(h)) { /* standard class constraint */
1455 Class c = findQualClass(h);
1457 ERRMSG(line) "Undefined class \"%s\"", identToStr(h)
1465 if (args!=cclass(c).arity) {
1466 ERRMSG(line) "Wrong number of arguments for class \"%s\"",
1467 textToStr(cclass(c).text)
1470 if (cellIsMember(c,classDefns) && !cellIsMember(c,tcDeps)) {
1471 tcDeps = cons(c,tcDeps);
1475 else if (isExt(h)) { /* Lacks predicate */
1476 if (args!=1) { /* parser shouldn't let this happen*/
1477 ERRMSG(line) "Wrong number of arguments for lacks predicate"
1484 if (whatIs(h) != IPCELL)
1487 internal("depPredExp");
1492 static Void local checkMems(c,tyvars,m) /* check member function details */
1496 Int line = intOf(fst3(m));
1503 if (isPolyType(t)) {
1509 tyvars = typeVarsIn(t,NIL,xtvs,tyvars);
1510 /* Look for extra type vars. */
1511 checkOptQuantVars(line,xtvs,tyvars);
1513 if (isQualType(t)) { /* Overloaded member signatures? */
1514 map2Over(depPredExp,line,tyvars,fst(snd(t)));
1516 t = ap(QUAL,pair(NIL,t));
1519 fst(snd(t)) = cons(cclass(c).head,fst(snd(t)));/* Add main predicate */
1520 snd(snd(t)) = depTopType(line,tyvars,snd(snd(t)));
1522 for (tvs=tyvars; nonNull(tvs); tvs=tl(tvs)){/* Quantify */
1526 t = mkPolyType(sig,t);
1528 thd3(m) = t; /* Save type */
1529 take(cclass(c).arity,tyvars); /* Delete extra type vars */
1531 if (isAmbiguous(t)) {
1532 ambigError(line,"class declaration",hd(vs),t);
1534 h98CheckType(line,"member type",hd(vs),t);
1537 static Void local checkMems2(c,m) /* check member function details */
1540 Int line = intOf(fst3(m));
1545 static Void local addMembers(c) /* Add definitions of member funs */
1546 Class c; { /* and other parts of class struct.*/
1547 List ms = fst(cclass(c).members);
1548 List fs = snd(cclass(c).members);
1549 List ns = NIL; /* List of names */
1550 Int mno; /* Member function number */
1552 for (mno=0; mno<cclass(c).numSupers; mno++) {
1553 ns = cons(newDSel(c,mno),ns);
1555 cclass(c).dsels = rev(ns); /* Save dictionary selectors */
1557 for (mno=1, ns=NIL; nonNull(ms); ms=tl(ms)) {
1558 Int line = intOf(fst3(hd(ms)));
1559 List vs = rev(snd3(hd(ms)));
1560 Type t = thd3(hd(ms));
1561 for (; nonNull(vs); vs=tl(vs)) {
1562 ns = cons(newMember(line,mno++,hd(vs),t,c),ns);
1565 cclass(c).members = rev(ns); /* Save list of members */
1566 cclass(c).numMembers = length(cclass(c).members);
1568 for (; nonNull(fs); fs=tl(fs)) { /* fixity declarations */
1569 Int line = intOf(fst3(hd(fs)));
1570 List ops = snd3(hd(fs));
1571 Syntax s = intOf(thd3(hd(fs)));
1572 for (; nonNull(ops); ops=tl(ops)) {
1573 Name n = nameIsMember(textOf(hd(ops)),cclass(c).members);
1575 missFixity(line,textOf(hd(ops)));
1576 } else if (name(n).syntax!=NO_SYNTAX) {
1577 dupFixity(line,textOf(hd(ops)));
1583 /* Not actually needed just yet; for the time being, dictionary code will
1584 not be passed through the type checker.
1586 cclass(c).dtycon = addPrimTycon(generateText("Dict.%s",c),
1593 mno = cclass(c).numSupers + cclass(c).numMembers;
1594 /* cclass(c).dcon = addPrimCfun(generateText("Make.%s",c),mno,0,NIL); */
1595 cclass(c).dcon = addPrimCfun(generateText(":D%s",c),mno,0,NIL);
1596 /* implementCfun(cclass(c).dcon,NIL);
1597 Don't manufacture a wrapper fn for dictionary constructors.
1598 Applications of dictionary constructors are always saturated,
1599 and translate.c:stgExpr() special-cases saturated constructor apps.
1602 if (mno==1) { /* Single entry dicts use newtype */
1603 name(cclass(c).dcon).defn = nameId;
1604 if (nonNull(cclass(c).members)) {
1605 name(hd(cclass(c).members)).number = mfunNo(0);
1608 cclass(c).defaults = classBindings("class",c,cclass(c).defaults);
1611 static Name local newMember(l,no,v,t,parent)
1612 Int l; /* Make definition for member fn */
1617 Name m = findName(textOf(v));
1620 m = newName(textOf(v),parent);
1621 } else if (name(m).defn!=PREDEFINED) {
1622 ERRMSG(l) "Repeated definition for member function \"%s\"",
1623 textToStr(name(m).text)
1629 name(m).number = mfunNo(no);
1634 static Name local newDSel(c,no) /* Make definition for dict selectr*/
1640 /* sprintf(buf,"sc%d.%s",no,"%s"); */
1641 sprintf(buf,"$p%d%s",no+1,"%s");
1642 s = newName(generateText(buf,c),c);
1643 name(s).line = cclass(c).line;
1645 name(s).number = DFUNNAME;
1651 static Text local generateText(sk,c) /* We need to generate names for */
1652 String sk; /* certain objects corresponding */
1653 Class c; { /* to each class. */
1654 String cname = textToStr(cclass(c).text);
1655 char buffer[MAX_GEN+1];
1657 if ((strlen(sk)+strlen(cname))>=MAX_GEN) {
1658 ERRMSG(0) "Please use a shorter name for class \"%s\"", cname
1661 sprintf(buffer,sk,cname);
1662 return findText(buffer);
1665 static Int local visitClass(c) /* visit class defn to check that */
1666 Class c; { /* class hierarchy is acyclic */
1668 if (isExt(c)) { /* special case for lacks preds */
1672 if (cclass(c).level < 0) { /* already visiting this class? */
1673 ERRMSG(cclass(c).line) "Class hierarchy for \"%s\" is not acyclic",
1674 textToStr(cclass(c).text)
1676 } else if (cclass(c).level == 0) { /* visiting class for first time */
1677 List scs = cclass(c).supers;
1679 cclass(c).level = (-1);
1680 for (; nonNull(scs); scs=tl(scs)) {
1681 Int l = visitClass(getHead(hd(scs)));
1684 cclass(c).level = 1+lev; /* level = 1 + max level of supers */
1686 return cclass(c).level;
1689 /* --------------------------------------------------------------------------
1690 * Process class and instance declaration binding groups:
1691 * ------------------------------------------------------------------------*/
1693 static List local classBindings(where,c,bs)
1694 String where; /* Check validity of bindings bs */
1695 Class c; /* for class c (or an inst of c) */
1696 List bs; { /* sort into approp. member order */
1699 for (; nonNull(bs); bs=tl(bs)) {
1701 Cell body = snd(snd(b));
1704 if (!isVar(fst(b))) { /* Only allow function bindings */
1705 ERRMSG(rhsLine(snd(body)))
1706 "Pattern binding illegal in %s declaration", where
1710 if (isNull(mnm=memberName(c,textOf(fst(b))))) {
1711 ERRMSG(rhsLine(snd(hd(body))))
1712 "No member \"%s\" in class \"%s\"",
1713 textToStr(textOf(fst(b))), textToStr(cclass(c).text)
1717 nbs = numInsert(mfunOf(mnm)-1,b,nbs);
1722 static Name local memberName(c,t) /* return name of member function */
1723 Class c; /* with name t in class c */
1724 Text t; { /* return NIL if not a member */
1725 List ms = cclass(c).members;
1726 for (; nonNull(ms); ms=tl(ms)) {
1727 if (t==name(hd(ms)).text) {
1734 static List local numInsert(n,x,xs) /* insert x at nth position in xs, */
1735 Int n; /* filling gaps with NIL */
1738 List start = isNull(xs) ? cons(NIL,NIL) : xs;
1740 for (xs=start; 0<n--; xs=tl(xs)) {
1741 if (isNull(tl(xs))) {
1742 tl(xs) = cons(NIL,NIL);
1749 /* --------------------------------------------------------------------------
1750 * Calculate set of variables appearing in a given type expression (possibly
1751 * qualified) as a list of distinct values. The order in which variables
1752 * appear in the list is the same as the order in which those variables
1753 * occur in the type expression when read from left to right.
1754 * ------------------------------------------------------------------------*/
1756 List local typeVarsIn(ty,us,ws,vs) /*Calculate list of type variables*/
1757 Cell ty; /* used in type expression, reading*/
1758 List us; /* from left to right ignoring any */
1759 List ws; /* listed in us. */
1760 List vs; { /* ws = explicitly quantified vars */
1761 switch (whatIs(ty)) {
1762 case AP : return typeVarsIn(snd(ty),us,ws,
1763 typeVarsIn(fst(ty),us,ws,vs));
1766 case VAROPCELL : if ((nonNull(findBtyvs(textOf(ty)))
1767 && !varIsMember(textOf(ty),ws))
1768 || varIsMember(textOf(ty),us)) {
1771 return maybeAppendVar(ty,vs);
1774 case POLYTYPE : return typeVarsIn(monotypeOf(ty),polySigOf(ty),ws,vs);
1776 case QUAL : { vs = typeVarsIn(fst(snd(ty)),us,ws,vs);
1777 return typeVarsIn(snd(snd(ty)),us,ws,vs);
1780 case BANG : return typeVarsIn(snd(ty),us,ws,vs);
1782 case LABC : { List fs = snd(snd(ty));
1783 for (; nonNull(fs); fs=tl(fs)) {
1784 vs = typeVarsIn(snd(hd(fs)),us,ws,vs);
1792 static List local maybeAppendVar(v,vs) /* append variable to list if not */
1793 Cell v; /* already included */
1799 while (nonNull(c)) {
1800 if (textOf(hd(c))==t) {
1808 tl(p) = cons(v,NIL);
1816 /* --------------------------------------------------------------------------
1817 * Static analysis for type expressions is required to:
1818 * - ensure that each type constructor or class used has been defined.
1819 * - replace type variables by offsets, constructor names by Tycons.
1820 * - ensure that the type is well-kinded.
1821 * ------------------------------------------------------------------------*/
1823 static Type local checkSigType(line,where,e,type)
1824 Int line; /* Check validity of type expr in */
1825 String where; /* explicit type signature */
1832 if (isPolyType(type)) {
1833 xtvs = fst(snd(type));
1834 type = monotypeOf(type);
1836 tvs = typeVarsIn(type,NIL,xtvs,NIL);
1838 checkOptQuantVars(line,xtvs,tvs);
1840 if (isQualType(type)) {
1841 map2Over(depPredExp,line,tvs,fst(snd(type)));
1842 snd(snd(type)) = depTopType(line,tvs,snd(snd(type)));
1844 if (isAmbiguous(type)) {
1845 ambigError(line,where,e,type);
1848 type = depTopType(line,tvs,type);
1852 if (length(tvs)>=NUM_OFFSETS) {
1853 ERRMSG(line) "Too many type variables in %s\n", where
1857 for (; nonNull(ts); ts=tl(ts)) {
1860 type = mkPolyType(tvs,type);
1865 kindType(line,"type expression",type);
1869 h98CheckType(line,where,e,type);
1873 static Void local checkOptQuantVars(line,xtvs,tvs)
1875 List xtvs; /* Explicitly quantified vars */
1876 List tvs; { /* Implicitly quantified vars */
1877 if (nonNull(xtvs)) {
1879 for (; nonNull(vs); vs=tl(vs)) {
1880 if (!varIsMember(textOf(hd(vs)),xtvs)) {
1881 ERRMSG(line) "Quantifier does not mention type variable \"%s\"",
1882 textToStr(textOf(hd(vs)))
1886 for (vs=xtvs; nonNull(vs); vs=tl(vs)) {
1887 if (!varIsMember(textOf(hd(vs)),tvs)) {
1888 ERRMSG(line) "Quantified type variable \"%s\" is not used",
1889 textToStr(textOf(hd(vs)))
1892 if (varIsMember(textOf(hd(vs)),tl(vs))) {
1893 ERRMSG(line) "Quantified type variable \"%s\" is repeated",
1894 textToStr(textOf(hd(vs)))
1901 static Type local depTopType(l,tvs,t) /* Check top-level of type sig */
1909 for (; getHead(t1)==typeArrow && argCount==2; ++i) {
1910 arg(fun(t1)) = depCompType(l,tvs,arg(fun(t1)));
1911 if (isPolyOrQualType(arg(fun(t1)))) {
1917 if (nonNull(prev)) {
1918 arg(prev) = depTypeExp(l,tvs,t1);
1920 t = depTypeExp(l,tvs,t1);
1923 t = ap(RANK2,pair(mkInt(nr2),t));
1928 static Type local depCompType(l,tvs,t) /* Check component type for constr */
1932 Int ntvs = length(tvs);
1934 if (isPolyType(t)) {
1935 List vs = fst(snd(t));
1937 tvs = checkQuantVars(l,vs,tvs,t);
1938 nfr = replicate(length(vs),NIL);
1940 if (isQualType(t)) {
1941 map2Over(depPredExp,l,tvs,fst(snd(t)));
1942 snd(snd(t)) = depTypeExp(l,tvs,snd(snd(t)));
1943 if (isAmbiguous(t)) {
1944 ambigError(l,"type component",NIL,t);
1947 t = depTypeExp(l,tvs,t);
1953 return mkPolyType(nfr,t);
1956 static Type local depTypeExp(line,tyvars,type)
1960 switch (whatIs(type)) {
1961 case AP : fst(type) = depTypeExp(line,tyvars,fst(type));
1962 snd(type) = depTypeExp(line,tyvars,snd(type));
1965 case VARIDCELL : return depTypeVar(line,tyvars,textOf(type));
1967 case QUALIDENT : if (isQVar(type)) {
1968 ERRMSG(line) "Qualified type variables not allowed"
1971 /* deliberate fall through */
1972 case CONIDCELL : { Tycon tc = findQualTycon(type);
1975 "Undefined type constructor \"%s\"",
1979 if (cellIsMember(tc,tyconDefns) &&
1980 !cellIsMember(tc,tcDeps)) {
1981 tcDeps = cons(tc,tcDeps);
1987 case EXT : h98DoesntSupport(line,"extensible records");
1992 default : internal("depTypeExp");
1997 static Type local depTypeVar(line,tyvars,tv)
2004 for (; nonNull(tyvars); offset++) {
2005 if (tv==textOf(hd(tyvars))) {
2008 tyvars = tl(tyvars);
2011 Cell vt = findBtyvs(tv);
2015 ERRMSG(line) "Undefined type variable \"%s\"", textToStr(tv)
2018 return mkOffset(found);
2021 static List local checkQuantVars(line,vs,tvs,body)
2023 List vs; /* variables to quantify over */
2024 List tvs; /* variables already in scope */
2025 Cell body; { /* type/constr for scope of vars */
2027 List bvs = typeVarsIn(body,NIL,NIL,NIL);
2029 for (; nonNull(us); us=tl(us)) {
2030 Text u = textOf(hd(us));
2031 if (varIsMember(u,tl(us))) {
2032 ERRMSG(line) "Duplicated quantified variable %s",
2037 if (varIsMember(u,tvs)) {
2038 ERRMSG(line) "Local quantifier for %s hides an outer use",
2043 if (!varIsMember(u,bvs)) {
2044 ERRMSG(line) "Locally quantified variable %s is not used",
2049 tvs = appendOnto(tvs,vs);
2054 /* --------------------------------------------------------------------------
2055 * Check for ambiguous types:
2056 * A type Preds => type is ambiguous if not (TV(P) `subset` TV(type))
2057 * ------------------------------------------------------------------------*/
2059 List offsetTyvarsIn(t,vs) /* add list of offset tyvars in t */
2060 Type t; /* to list vs */
2062 switch (whatIs(t)) {
2063 case AP : return offsetTyvarsIn(fun(t),
2064 offsetTyvarsIn(arg(t),vs));
2066 case OFFSET : if (cellIsMember(t,vs))
2071 case QUAL : return offsetTyvarsIn(snd(t),vs);
2073 case POLYTYPE : return offsetTyvarsIn(monotypeOf(t),vs);
2074 /* slightly inaccurate, but won't matter here */
2077 case RANK2 : return offsetTyvarsIn(snd(snd(t)),vs);
2079 default : return vs;
2083 List zonkTyvarsIn(t,vs)
2086 switch (whatIs(t)) {
2087 case AP : return zonkTyvarsIn(fun(t),
2088 zonkTyvarsIn(arg(t),vs));
2090 case INTCELL : if (cellIsMember(t,vs))
2095 /* this case will lead to a type error --
2096 much better than reporting an internal error ;-) */
2097 /* case OFFSET : internal("zonkTyvarsIn"); */
2099 default : return vs;
2103 static List local otvars(pi,os) /* os is a list of offsets that */
2104 Cell pi; /* refer to the arguments of pi; */
2105 List os; { /* find list of offsets in those */
2106 List us = NIL; /* positions */
2107 for (; nonNull(os); os=tl(os)) {
2108 us = offsetTyvarsIn(nthArg(offsetOf(hd(os)),pi),us);
2113 static List local otvarsZonk(pi,os,o) /* same as above, but zonks */
2117 for (; nonNull(os); os=tl(os)) {
2118 Type t = zonkType(nthArg(offsetOf(hd(os)),pi),o);
2119 us = zonkTyvarsIn(t,us);
2124 static Bool local odiff(us,vs)
2126 while (nonNull(us) && cellIsMember(hd(us),vs)) {
2132 static Bool local osubset(us,vs) /* Determine whether us is subset */
2133 List us, vs; { /* of vs */
2134 while (nonNull(us) && cellIsMember(hd(us),vs)) {
2140 List oclose(fds,vs) /* Compute closure of vs wrt to fds*/
2143 Bool changed = TRUE;
2147 while (nonNull(fds)) {
2149 List next = tl(fds);
2150 if (osubset(fst(fd),vs)) { /* Test if fd applies */
2152 for (; nonNull(os); os=tl(os)) {
2153 if (!cellIsMember(hd(os),vs)) {
2154 vs = cons(hd(os),vs);
2158 } else { /* Didn't apply this time, so keep */
2169 Bool isAmbiguous(type) /* Determine whether type is */
2170 Type type; { /* ambiguous */
2171 if (isPolyType(type)) {
2172 type = monotypeOf(type);
2174 if (isQualType(type)) { /* only qualified types can be */
2175 List ps = fst(snd(type)); /* ambiguous */
2176 List tvps = offsetTyvarsIn(ps,NIL);
2177 List tvts = offsetTyvarsIn(snd(snd(type)),NIL);
2178 List fds = calcFunDeps(ps);
2180 tvts = oclose(fds,tvts); /* Close tvts under fds */
2181 return !osubset(tvps,tvts);
2186 List calcFunDeps(ps)
2189 for (; nonNull(ps); ps=tl(ps)) {/* Calc functional dependencies */
2191 Cell c = getHead(pi);
2193 List xfs = cclass(c).xfds;
2194 for (; nonNull(xfs); xfs=tl(xfs)) {
2195 List fs = snd(hd(xfs));
2196 for (; nonNull(fs); fs=tl(fs)) {
2197 fds = cons(pair(otvars(pi,fst(hd(fs))),
2198 otvars(pi,snd(hd(fs)))),fds);
2204 fds = cons(pair(NIL,offsetTyvarsIn(arg(pi),NIL)),fds);
2211 List calcFunDepsPreds(ps)
2214 for (; nonNull(ps); ps=tl(ps)) {/* Calc functional dependencies */
2216 Cell pi = fst3(pi3);
2217 Cell c = getHead(pi);
2218 Int o = intOf(snd3(pi3));
2220 List xfs = cclass(c).xfds;
2221 for (; nonNull(xfs); xfs=tl(xfs)) {
2222 List fs = snd(hd(xfs));
2223 for (; nonNull(fs); fs=tl(fs)) {
2224 fds = cons(pair(otvarsZonk(pi,fst(hd(fs)),o),
2225 otvarsZonk(pi,snd(hd(fs)),o)),fds);
2231 fds = cons(pair(NIL,zonkTyvarsIn(arg(pi),NIL)),fds);
2238 Void ambigError(line,where,e,type) /* produce error message for */
2239 Int line; /* ambiguity */
2243 ERRMSG(line) "Ambiguous type signature in %s", where ETHEN
2244 ERRTEXT "\n*** ambiguous type : " ETHEN ERRTYPE(type);
2246 ERRTEXT "\n*** assigned to : " ETHEN ERREXPR(e);
2252 /* --------------------------------------------------------------------------
2253 * Kind inference for simple types:
2254 * ------------------------------------------------------------------------*/
2256 static Void local kindConstr(line,alpha,m,c)
2257 Int line; /* Determine kind of constructor */
2261 Cell h = getHead(c);
2265 Printf("kindConstr: alpha=%d, m=%d, c=",alpha,m);
2266 printType(stdout,c);
2270 switch (whatIs(h)) {
2271 case POLYTYPE : if (n!=0) {
2272 internal("kindConstr1");
2274 static String pt = "polymorphic type";
2275 Type t = dropRank1(c,alpha,m);
2276 Kinds ks = polySigOf(t);
2279 for (; isAp(ks); ks=tl(ks)) {
2282 beta = newKindvars(m1);
2283 unkindTypes = cons(pair(mkInt(beta),t),unkindTypes);
2284 checkKind(line,beta,m1,monotypeOf(t),NIL,pt,STAR,0);
2289 case QUAL : if (n!=0) {
2290 internal("kindConstr2");
2292 map3Proc(kindPred,line,alpha,m,fst(snd(c)));
2293 kindConstr(line,alpha,m,snd(snd(c)));
2297 case RANK2 : kindConstr(line,alpha,m,snd(snd(c)));
2301 case EXT : if (n!=2) {
2303 "Illegal use of row in " ETHEN ERRTYPE(c);
2310 case TYCON : if (isSynonym(h) && n<tycon(h).arity) {
2312 "Not enough arguments for type synonym \"%s\"",
2313 textToStr(tycon(h).text)
2319 if (n==0) { /* trivial case, no arguments */
2320 typeIs = kindAtom(alpha,c);
2321 } else { /* non-trivial application */
2322 static String app = "constructor application";
2332 typeIs = kindAtom(alpha,h); /* h :: v1 -> ... -> vn -> w */
2333 shouldKind(line,h,c,app,k,beta);
2335 for (i=n; i>0; --i) { /* ci :: vi for each 1 <- 1..n */
2336 checkKind(line,alpha,m,arg(a),c,app,aVar,beta+i-1);
2339 tyvarType(beta+n); /* inferred kind is w */
2343 static Kind local kindAtom(alpha,c) /* Find kind of atomic constructor */
2346 switch (whatIs(c)) {
2347 case TUPLE : return simpleKind(tupleOf(c)); /*(,)::* -> * -> * */
2348 case OFFSET : return mkInt(alpha+offsetOf(c));
2349 case TYCON : return tycon(c).kind;
2350 case INTCELL : return c;
2352 case VAROPCELL : { Cell vt = findBtyvs(textOf(c));
2358 case EXT : return extKind;
2362 Printf("kindAtom(%d,whatIs(%d)) on ",alpha,whatIs(c));
2363 printType(stdout,c);
2366 internal("kindAtom");
2367 return STAR;/* not reached */
2370 static Void local kindPred(l,alpha,m,pi)/* Check kinds of arguments in pred*/
2376 if (isAp(pi) && isExt(fun(pi))) {
2377 static String lackspred = "lacks predicate";
2378 checkKind(l,alpha,m,arg(pi),NIL,lackspred,ROW,0);
2383 if (isAp(pi) && whatIs(fun(pi)) == IPCELL) {
2384 static String ippred = "iparam predicate";
2385 checkKind(l,alpha,m,arg(pi),NIL,ippred,STAR,0);
2389 { static String predicate = "class constraint";
2390 Class c = getHead(pi);
2391 List as = getArgs(pi);
2392 Kinds ks = cclass(c).kinds;
2394 while (nonNull(ks)) {
2395 checkKind(l,alpha,m,hd(as),NIL,predicate,hd(ks),0);
2402 static Void local kindType(line,wh,type)/* check that (poss qualified) type*/
2403 Int line; /* is well-kinded */
2406 checkKind(line,0,0,type,NIL,wh,STAR,0);
2409 static Void local fixKinds() { /* add kind annotations to types */
2410 for (; nonNull(unkindTypes); unkindTypes=tl(unkindTypes)) {
2411 Pair pr = hd(unkindTypes);
2412 Int beta = intOf(fst(pr));
2413 Cell qts = polySigOf(snd(pr));
2415 if (isNull(hd(qts))) {
2416 hd(qts) = copyKindvar(beta++);
2418 internal("fixKinds");
2420 if (nonNull(tl(qts))) {
2428 Printf("Type expression: ");
2429 printType(stdout,snd(pr));
2431 printKind(stdout,polySigOf(snd(pr)));
2437 /* --------------------------------------------------------------------------
2438 * Kind checking of groups of type constructors and classes:
2439 * ------------------------------------------------------------------------*/
2441 static Void local kindTCGroup(tcs) /* find kinds for mutually rec. gp */
2442 List tcs; { /* of tycons and classes */
2443 emptySubstitution();
2445 mapProc(initTCKind,tcs);
2446 mapProc(kindTC,tcs);
2449 emptySubstitution();
2452 static Void local initTCKind(c) /* build initial kind/arity for c */
2454 if (isTycon(c)) { /* Initial kind of tycon is: */
2455 Int beta = newKindvars(1); /* v1 -> ... -> vn -> vn+1 */
2456 varKind(tycon(c).arity); /* where n is the arity of c. */
2457 bindTv(beta,typeIs,typeOff); /* For data definitions, vn+1 == * */
2458 switch (whatIs(tycon(c).what)) {
2460 case DATATYPE : bindTv(typeOff+tycon(c).arity,STAR,0);
2462 tycon(c).kind = mkInt(beta);
2464 Int n = cclass(c).arity;
2465 Int beta = newKindvars(n);
2466 cclass(c).kinds = NIL;
2469 cclass(c).kinds = pair(mkInt(beta+n),cclass(c).kinds);
2474 static Void local kindTC(c) /* check each part of a tycon/class*/
2475 Cell c; { /* is well-kinded */
2477 static String cfun = "constructor function";
2478 static String tsyn = "synonym definition";
2479 Int line = tycon(c).line;
2480 Int beta = tyvar(intOf(tycon(c).kind))->offs;
2481 Int m = tycon(c).arity;
2482 switch (whatIs(tycon(c).what)) {
2484 case DATATYPE : { List cs = tycon(c).defn;
2485 if (isQualType(cs)) {
2486 map3Proc(kindPred,line,beta,m,
2488 tycon(c).defn = cs = snd(snd(cs));
2490 for (; hasCfun(cs); cs=tl(cs)) {
2491 kindType(line,cfun,name(hd(cs)).type);
2496 default : checkKind(line,beta,m,tycon(c).defn,NIL,
2500 else { /* scan type exprs in class defn to*/
2501 List ms = fst(cclass(c).members);
2502 Int m = cclass(c).arity; /* determine the class signature */
2503 Int beta = newKindvars(m);
2504 kindPred(cclass(c).line,beta,m,cclass(c).head);
2505 map3Proc(kindPred,cclass(c).line,beta,m,cclass(c).supers);
2506 for (; nonNull(ms); ms=tl(ms)) {
2507 Int line = intOf(fst3(hd(ms)));
2508 Type type = thd3(hd(ms));
2509 kindType(line,"member function type signature",type);
2514 static Void local genTC(c) /* generalise kind inferred for */
2515 Cell c; { /* given tycon/class */
2517 tycon(c).kind = copyKindvar(intOf(tycon(c).kind));
2519 Printf("%s :: ",textToStr(tycon(c).text));
2520 printKind(stdout,tycon(c).kind);
2524 Kinds ks = cclass(c).kinds;
2525 for (; nonNull(ks); ks=tl(ks)) {
2526 hd(ks) = copyKindvar(intOf(hd(ks)));
2529 Printf("%s :: ",textToStr(cclass(c).text));
2530 printKinds(stdout,cclass(c).kinds);
2536 /* --------------------------------------------------------------------------
2537 * Static analysis of instance declarations:
2539 * The first part of the static analysis is performed as the declarations
2540 * are read during parsing:
2541 * - make new entry in instance table
2542 * - record line number of declaration
2543 * - build list of instances defined in current script for use in later
2544 * stages of static analysis.
2545 * ------------------------------------------------------------------------*/
2547 Void instDefn(line,head,ms) /* process new instance definition */
2548 Int line; /* definition line number */
2549 Cell head; /* inst header :: (context,Class) */
2550 List ms; { /* instance members */
2551 Inst nw = newInst();
2552 inst(nw).line = line;
2553 inst(nw).specifics = fst(head);
2554 inst(nw).head = snd(head);
2555 inst(nw).implements = ms;
2556 instDefns = cons(nw,instDefns);
2559 /* --------------------------------------------------------------------------
2560 * Further static analysis of instance declarations:
2562 * Makes the following checks:
2563 * - Class part of header has form C (T a1 ... an) where C is a known
2564 * class, and T is a known datatype constructor (or restricted synonym),
2565 * and there is no previous C-T instance, and (T a1 ... an) has a kind
2566 * appropriate for the class C.
2567 * - Each element of context is a valid class expression, with type vars
2568 * drawn from a1, ..., an.
2569 * - All bindings are function bindings
2570 * - All bindings define member functions for class C
2571 * - Arrange bindings into appropriate order for member list
2572 * - No top level type signature declarations
2573 * ------------------------------------------------------------------------*/
2575 Bool allowOverlap = FALSE; /* TRUE => allow overlapping insts */
2576 Name nameListMonad = NIL; /* builder function for List Monad */
2578 static Void local checkInstDefn(in) /* Validate instance declaration */
2580 Int line = inst(in).line;
2581 List tyvars = typeVarsIn(inst(in).head,NIL,NIL,NIL);
2582 List tvps = NIL, tvts = NIL;
2585 if (haskell98) { /* Check for `simple' type */
2587 Cell t = arg(inst(in).head);
2588 for (; isAp(t); t=fun(t)) {
2589 if (!isVar(arg(t))) {
2591 "syntax error in instance head (variable expected)"
2594 if (varIsMember(textOf(arg(t)),tvs)) {
2595 ERRMSG(line) "repeated type variable \"%s\" in instance head",
2596 textToStr(textOf(arg(t)))
2599 tvs = cons(arg(t),tvs);
2603 "syntax error in instance head (constructor expected)"
2608 /* add in the tyvars from the `specifics' so that we don't
2609 prematurely complain about undefined tyvars */
2610 tyvars = typeVarsIn(inst(in).specifics,NIL,NIL,tyvars);
2611 inst(in).head = depPredExp(line,tyvars,inst(in).head);
2614 Type h = getHead(arg(inst(in).head));
2616 ERRMSG(line) "Cannot use type synonym in instance head"
2621 map2Over(depPredExp,line,tyvars,inst(in).specifics);
2623 /* OK, now we start over, and test for ambiguity */
2624 tvts = offsetTyvarsIn(inst(in).head,NIL);
2625 tvps = offsetTyvarsIn(inst(in).specifics,NIL);
2626 fds = calcFunDeps(inst(in).specifics);
2627 tvts = oclose(fds,tvts);
2628 tvts = odiff(tvps,tvts);
2629 if (!isNull(tvts)) {
2630 ERRMSG(line) "Undefined type variable \"%s\"",
2631 textToStr(textOf(nth(offsetOf(hd(tvts)),tyvars)))
2635 h98CheckCtxt(line,"instance definition",FALSE,inst(in).specifics,NIL);
2636 inst(in).numSpecifics = length(inst(in).specifics);
2637 inst(in).c = getHead(inst(in).head);
2638 if (!isClass(inst(in).c)) {
2639 ERRMSG(line) "Illegal predicate in instance declaration"
2643 if (nonNull(cclass(inst(in).c).fds)) {
2644 List fds = cclass(inst(in).c).fds;
2645 for (; nonNull(fds); fds=tl(fds)) {
2646 List as = otvars(inst(in).head, fst(hd(fds)));
2647 List bs = otvars(inst(in).head, snd(hd(fds)));
2648 List fs = calcFunDeps(inst(in).specifics);
2650 if (!osubset(bs,as)) {
2651 ERRMSG(inst(in).line)
2652 "Instance is more general than a dependency allows"
2654 ERRTEXT "\n*** Instance : "
2655 ETHEN ERRPRED(inst(in).head);
2656 ERRTEXT "\n*** For class : "
2657 ETHEN ERRPRED(cclass(inst(in).c).head);
2658 ERRTEXT "\n*** Under dependency : "
2659 ETHEN ERRFD(hd(fds));
2666 kindInst(in,length(tyvars));
2669 if (nonNull(extractSigdecls(inst(in).implements))) {
2671 "Type signature declarations not permitted in instance declaration"
2674 if (nonNull(extractFixdecls(inst(in).implements))) {
2676 "Fixity declarations not permitted in instance declaration"
2679 inst(in).implements = classBindings("instance",
2681 extractBindings(inst(in).implements));
2682 inst(in).builder = newInstImp(in);
2683 if (!preludeLoaded && isNull(nameListMonad) && isAp(inst(in).head)
2684 && fun(inst(in).head)==classMonad && arg(inst(in).head)==typeList) {
2685 nameListMonad = inst(in).builder;
2689 static Void local insertInst(in) /* Insert instance into class */
2691 Class c = inst(in).c;
2692 List ins = cclass(c).instances;
2695 if (nonNull(cclass(c).fds)) { /* Check for conflicts with fds */
2696 List ins1 = cclass(c).instances;
2697 for (; nonNull(ins1); ins1=tl(ins1)) {
2698 List fds = cclass(c).fds;
2699 substitution(RESET);
2700 for (; nonNull(fds); fds=tl(fds)) {
2701 Int alpha = newKindedVars(inst(in).kinds);
2702 Int beta = newKindedVars(inst(hd(ins1)).kinds);
2703 List as = fst(hd(fds));
2705 for (; same && nonNull(as); as=tl(as)) {
2706 Int n = offsetOf(hd(as));
2707 same &= unify(nthArg(n,inst(in).head),alpha,
2708 nthArg(n,inst(hd(ins1)).head),beta);
2710 if (isNull(as) && same) {
2711 for (as=snd(hd(fds)); same && nonNull(as); as=tl(as)) {
2712 Int n = offsetOf(hd(as));
2713 same &= sameType(nthArg(n,inst(in).head),alpha,
2714 nthArg(n,inst(hd(ins1)).head),beta);
2717 ERRMSG(inst(in).line)
2718 "Instances are not consistent with dependencies"
2720 ERRTEXT "\n*** This instance : "
2721 ETHEN ERRPRED(inst(in).head);
2722 ERRTEXT "\n*** Conflicts with : "
2723 ETHEN ERRPRED(inst(hd(ins)).head);
2724 ERRTEXT "\n*** For class : "
2725 ETHEN ERRPRED(cclass(c).head);
2726 ERRTEXT "\n*** Under dependency : "
2727 ETHEN ERRFD(hd(fds));
2737 substitution(RESET);
2738 while (nonNull(ins)) { /* Look for overlap w/ other insts */
2739 Int alpha = newKindedVars(inst(in).kinds);
2740 Int beta = newKindedVars(inst(hd(ins)).kinds);
2741 if (unifyPred(inst(in).head,alpha,inst(hd(ins)).head,beta)) {
2742 Cell pi = copyPred(inst(in).head,alpha);
2743 if (allowOverlap && !haskell98) {
2744 Bool bef = instCompare(in,hd(ins));
2745 Bool aft = instCompare(hd(ins),in);
2746 if (bef && !aft) { /* in comes strictly before hd(ins)*/
2749 if (aft && !bef) { /* in comes strictly after hd(ins) */
2756 if (multiInstRes && nonNull(inst(in).specifics)) {
2760 ERRMSG(inst(in).line) "Overlapping instances for class \"%s\"",
2761 textToStr(cclass(c).text)
2763 ERRTEXT "\n*** This instance : " ETHEN ERRPRED(inst(in).head);
2764 ERRTEXT "\n*** Overlaps with : " ETHEN
2765 ERRPRED(inst(hd(ins)).head);
2766 ERRTEXT "\n*** Common instance : " ETHEN
2774 prev = ins; /* No overlap detected, so move on */
2775 ins = tl(ins); /* to next instance */
2777 substitution(RESET);
2779 if (nonNull(prev)) { /* Insert instance at this point */
2780 tl(prev) = cons(in,ins);
2782 cclass(c).instances = cons(in,ins);
2786 static Bool local instCompare(ia,ib) /* See if ia is an instance of ib */
2788 Int alpha = newKindedVars(inst(ia).kinds);
2789 Int beta = newKindedVars(inst(ib).kinds);
2790 return matchPred(inst(ia).head,alpha,inst(ib).head,beta);
2793 static Name local newInstImp(in) /* Make definition for inst builder*/
2795 Name b = newName(inventText(),in);
2796 name(b).line = inst(in).line;
2797 name(b).arity = inst(in).numSpecifics;
2798 name(b).number = DFUNNAME;
2802 /* --------------------------------------------------------------------------
2803 * Kind checking of instance declaration headers:
2804 * ------------------------------------------------------------------------*/
2806 static Void local kindInst(in,freedom) /* check predicates in instance */
2811 emptySubstitution();
2812 beta = newKindvars(freedom);
2813 kindPred(inst(in).line,beta,freedom,inst(in).head);
2814 if (whatIs(inst(in).specifics)!=DERIVE) {
2815 map3Proc(kindPred,inst(in).line,beta,freedom,inst(in).specifics);
2817 for (inst(in).kinds = NIL; 0<freedom--; ) {
2818 inst(in).kinds = cons(copyKindvar(beta+freedom),inst(in).kinds);
2821 Printf("instance ");
2822 printPred(stdout,inst(in).head);
2824 printKinds(stdout,inst(in).kinds);
2827 emptySubstitution();
2830 /* --------------------------------------------------------------------------
2831 * Process derived instance requests:
2832 * ------------------------------------------------------------------------*/
2834 static List derivedInsts; /* list of derived instances */
2836 static Void local checkDerive(t,p,ts,ct)/* verify derived instance request */
2837 Tycon t; /* for tycon t, with explicit */
2838 List p; /* context p, component types ts */
2839 List ts; /* and named class ct */
2841 Int line = tycon(t).line;
2842 Class c = findQualClass(ct);
2844 ERRMSG(line) "Unknown class \"%s\" in derived instance",
2848 addDerInst(line,c,p,dupList(ts),t,tycon(t).arity);
2851 static Void local addDerInst(line,c,p,cts,t,a) /* Add a derived instance */
2858 Cell head = t; /* Build instance head */
2862 head = ap(head,mkOffset(i));
2868 inst(in).line = line;
2869 inst(in).head = head;
2870 inst(in).specifics = ap(DERIVE,pair(dupList(p),cts));
2871 inst(in).implements = NIL;
2872 inst(in).kinds = mkInt(a);
2873 derivedInsts = cons(in,derivedInsts);
2876 Void addTupInst(c,n) /* Request derived instance of c */
2877 Class c; /* for mkTuple(n) constructor */
2882 cts = cons(mkOffset(m),cts);
2885 addDerInst(0,c,NIL,cts,mkTuple(n),n);
2889 Inst addRecShowInst(c,e) /* Generate instance for ShowRecRow*/
2890 Class c; /* c *must* be ShowRecRow */
2892 Inst in = newInst();
2894 inst(in).head = ap(c,ap2(e,aVar,bVar));
2895 inst(in).kinds = extKind;
2896 inst(in).specifics = cons(ap(classShow,aVar),
2898 cons(ap(c,bVar),NIL)));
2899 inst(in).numSpecifics = 3;
2900 inst(in).builder = implementRecShw(extText(e),in);
2901 cclass(c).instances = appendOnto(cclass(c).instances,singleton(in));
2905 Inst addRecEqInst(c,e) /* Generate instance for EqRecRow */
2906 Class c; /* c *must* be EqRecRow */
2908 Inst in = newInst();
2910 inst(in).head = ap(c,ap2(e,aVar,bVar));
2911 inst(in).kinds = extKind;
2912 inst(in).specifics = cons(ap(classEq,aVar),
2914 cons(ap(c,bVar),NIL)));
2915 inst(in).numSpecifics = 3;
2916 inst(in).builder = implementRecEq(extText(e),in);
2917 cclass(c).instances = appendOnto(cclass(c).instances,singleton(in));
2922 /* --------------------------------------------------------------------------
2923 * Calculation of contexts for derived instances:
2925 * Allowing arbitrary types to appear in contexts makes it rather harder
2926 * to decide what the context for a derived instance should be. For
2929 * data T a = MkT [a] deriving Show,
2931 * we could have either of the following:
2933 * instance (Show [a]) => Show (T a) where ...
2934 * instance (Show a) => Show (T a) where ...
2936 * (assuming, of course, that instance (Show a) => Show [a]). For now, we
2937 * choose to reduce contexts in the hope of detecting errors at an earlier
2938 * stage---in contrast with value definitions, there is no way for a user
2939 * to provide something analogous to a `type signature' by which they might
2940 * be able to control this behaviour themselves. We eliminate tautological
2941 * predicates, but only allow predicates to appear in the final result if
2942 * they have at least one argument with a variable at its head.
2944 * In general, we have to deal with mutually recursive instance declarations.
2945 * We find a solution in the obvious way by iterating to find a fixed point.
2946 * Of course, without restrictions on the form of instance declarations, we
2947 * cannot be sure that this will always terminate!
2949 * For each instance we maintain a pair of the form DERIVE (ctxt,ps).
2950 * Ctxt is a list giving the parts of the context that have been produced
2951 * so far in the form of predicate skeletons. During the calculation of
2952 * derived instances, we attach a dummy NIL value to the end of the list
2953 * which acts as a kind of `variable': other parts of the system maintain
2954 * pointers to this variable, and use it to detect when the context has
2955 * been extended with new elements. Meanwhile, ps is a list containing
2956 * predicates (pi,o) together with (delayed) substitutions of the form
2957 * (o,xs) where o is an offset and xs is one of the context variables
2958 * described above, which may have been partially instantiated.
2959 * ------------------------------------------------------------------------*/
2961 static Bool instsChanged;
2963 static Void local deriveContexts(is) /* Calc contexts for derived insts */
2965 emptySubstitution();
2966 mapProc(initDerInst,is); /* Prepare derived instances */
2968 do { /* Main calculation of contexts */
2969 instsChanged = FALSE;
2970 mapProc(calcInstPreds,is);
2971 } while (instsChanged);
2973 mapProc(tidyDerInst,is); /* Tidy up results */
2976 static Void local initDerInst(in) /* Prepare instance for calculation*/
2977 Inst in; { /* of derived instance context */
2978 Cell spcs = inst(in).specifics;
2979 Int beta = newKindedVars(inst(in).kinds);
2980 if (whatIs(spcs)!=DERIVE) {
2981 internal("initDerInst");
2983 fst(snd(spcs)) = appendOnto(fst(snd(spcs)),singleton(NIL));
2984 for (spcs=snd(snd(spcs)); nonNull(spcs); spcs=tl(spcs)) {
2985 hd(spcs) = ap2(inst(in).c,hd(spcs),mkInt(beta));
2987 inst(in).numSpecifics = beta;
2989 #ifdef DEBUG_DERIVING
2990 Printf("initDerInst: ");
2991 printPred(stdout,inst(in).head);
2993 printContext(stdout,snd(snd(inst(in).specifics)));
2998 static Void local calcInstPreds(in) /* Calculate next approximation */
2999 Inst in; { /* of the context for a derived */
3000 List retain = NIL; /* instance */
3001 List ps = snd(snd(inst(in).specifics));
3002 List spcs = fst(snd(inst(in).specifics));
3003 Int beta = inst(in).numSpecifics;
3005 Int factor = 1+length(ps);
3007 #ifdef DEBUG_DERIVING
3008 Printf("calcInstPreds: ");
3009 printPred(stdout,inst(in).head);
3013 while (nonNull(ps)) {
3016 if (its++ >= factor*cutoff) {
3017 Cell bpi = inst(in).head;
3018 Cell pi = copyPred(fun(p),intOf(snd(p)));
3019 ERRMSG(inst(in).line) "\n*** Cannot derive " ETHEN ERRPRED(bpi);
3020 ERRTEXT " after %d iterations.", its-1 ETHEN
3022 "\n*** This may indicate that the problem is undecidable. However,\n"
3024 "*** you may still try to increase the cutoff limit using the -c\n"
3026 "*** option and then try again. (The current setting is -c%d)\n",
3030 if (isInt(fst(p))) { /* Delayed substitution? */
3032 for (; nonNull(hd(qs)); qs=tl(qs)) {
3033 ps = cons(pair(hd(qs),fst(p)),ps);
3035 retain = cons(pair(fst(p),qs),retain);
3038 else if (isExt(fun(fst(p)))) { /* Lacks predicate */
3039 Text l = extText(fun(fst(p)));
3040 Type t = arg(fst(p));
3041 Int o = intOf(snd(p));
3046 h = getDerefHead(t,o);
3047 while (isExt(h) && argCount==2 && l!=extText(h)) {
3050 h = getDerefHead(t,o);
3052 if (argCount==0 && isOffset(h)) {
3053 maybeAddPred(ap(fun(fun(p)),h),o,beta,spcs);
3054 } else if (argCount!=0 || h!=typeNoRow) {
3055 Cell bpi = inst(in).head;
3056 Cell pi = copyPred(fun(p),intOf(snd(p)));
3057 ERRMSG(inst(in).line) "Cannot derive " ETHEN ERRPRED(bpi);
3058 ERRTEXT " because predicate " ETHEN ERRPRED(pi);
3059 ERRTEXT " does not hold\n"
3064 else { /* Class predicate */
3066 Int o = intOf(snd(p));
3067 Inst in1 = findInstFor(pi,o);
3069 List qs = inst(in1).specifics;
3070 Int off = mkInt(typeOff);
3071 if (whatIs(qs)==DERIVE) { /* Still being derived */
3072 for (qs=fst(snd(qs)); nonNull(hd(qs)); qs=tl(qs)) {
3073 ps = cons(pair(hd(qs),off),ps);
3075 retain = cons(pair(off,qs),retain);
3076 } else { /* Previously def'd inst */
3077 for (; nonNull(qs); qs=tl(qs)) {
3078 ps = cons(pair(hd(qs),off),ps);
3081 } else { /* No matching instance */
3083 while (isAp(qi) && isOffset(getDerefHead(arg(qi),o))) {
3087 Cell bpi = inst(in).head;
3088 pi = copyPred(pi,o);
3089 ERRMSG(inst(in).line) "An instance of " ETHEN ERRPRED(pi);
3090 ERRTEXT " is required to derive " ETHEN ERRPRED(bpi);
3094 maybeAddPred(pi,o,beta,spcs);
3099 snd(snd(inst(in).specifics)) = retain;
3102 static Void local maybeAddPred(pi,o,beta,ps)
3103 Cell pi; /* Add predicate pi to the list ps,*/
3104 Int o; /* setting the instsChanged flag if*/
3105 Int beta; /* pi is not already a member and */
3106 List ps; { /* using beta to adjust vars */
3107 Cell c = getHead(pi);
3108 for (; nonNull(ps); ps=tl(ps)) {
3109 if (isNull(hd(ps))) { /* reached the `dummy' end of list?*/
3110 hd(ps) = copyAdj(pi,o,beta);
3111 tl(ps) = pair(NIL,NIL);
3112 instsChanged = TRUE;
3114 } else if (c==getHead(hd(ps)) && samePred(pi,o,hd(ps),beta)) {
3120 static Cell local copyAdj(c,o,beta) /* Copy (c,o), replacing vars with */
3121 Cell c; /* offsets relative to beta. */
3124 switch (whatIs(c)) {
3125 case AP : { Cell l = copyAdj(fst(c),o,beta);
3126 Cell r = copyAdj(snd(c),o,beta);
3130 case OFFSET : { Int vn = o+offsetOf(c);
3131 Tyvar *tyv = tyvar(vn);
3133 return copyAdj(tyv->bound,tyv->offs,beta);
3136 if (vn<0 || vn>=NUM_OFFSETS) {
3137 internal("copyAdj");
3139 return mkOffset(vn);
3145 static Void local tidyDerInst(in) /* Tidy up results of derived inst */
3146 Inst in; { /* calculations */
3147 Int o = inst(in).numSpecifics;
3148 List ps = tl(rev(fst(snd(inst(in).specifics))));
3150 copyPred(inst(in).head,o);
3151 inst(in).specifics = simpleContext(ps,o);
3152 h98CheckCtxt(inst(in).line,"derived instance",FALSE,inst(in).specifics,in);
3153 inst(in).numSpecifics = length(inst(in).specifics);
3155 #ifdef DEBUG_DERIVING
3156 Printf("Derived instance: ");
3157 printContext(stdout,inst(in).specifics);
3159 printPred(stdout,inst(in).head);
3164 /* --------------------------------------------------------------------------
3165 * Generate code for derived instances:
3166 * ------------------------------------------------------------------------*/
3168 static Void local addDerivImp(in)
3171 Type t = getHead(arg(inst(in).head));
3172 Class c = inst(in).c;
3175 } else if (c==classOrd) {
3177 } else if (c==classEnum) {
3178 imp = deriveEnum(t);
3179 } else if (c==classIx) {
3181 } else if (c==classShow) {
3182 imp = deriveShow(t);
3183 } else if (c==classRead) {
3184 imp = deriveRead(t);
3185 } else if (c==classBounded) {
3186 imp = deriveBounded(t);
3188 ERRMSG(inst(in).line) "Cannot derive instances of class \"%s\"",
3189 textToStr(cclass(inst(in).c).text)
3193 kindInst(in,intOf(inst(in).kinds));
3195 inst(in).builder = newInstImp(in);
3196 inst(in).implements = classBindings("derived instance",
3202 /* --------------------------------------------------------------------------
3203 * Default definitions; only one default definition is permitted in a
3204 * given script file. If no default is supplied, then a standard system
3205 * default will be used where necessary.
3206 * ------------------------------------------------------------------------*/
3208 Void defaultDefn(line,defs) /* Handle default types definition */
3211 if (defaultLine!=0) {
3212 ERRMSG(line) "Multiple default declarations are not permitted in" ETHEN
3213 ERRTEXT "a single script file.\n"
3216 defaultDefns = defs;
3220 static Void local checkDefaultDefns() { /* check that default types are */
3221 List ds = NIL; /* well-kinded instances of Num */
3223 if (defaultLine!=0) {
3224 map2Over(depTypeExp,defaultLine,NIL,defaultDefns);
3225 emptySubstitution();
3227 map2Proc(kindType,defaultLine,"default type",defaultDefns);
3229 emptySubstitution();
3230 mapOver(fullExpand,defaultDefns);
3232 defaultDefns = stdDefaults;
3235 if (isNull(classNum)) {
3236 classNum = findClass(findText("Num"));
3239 for (ds=defaultDefns; nonNull(ds); ds=tl(ds)) {
3240 if (isNull(provePred(NIL,NIL,ap(classNum,hd(ds))))) {
3242 "Default types must be instances of the Num class"
3249 /* --------------------------------------------------------------------------
3250 * Foreign import declarations are Hugs' equivalent of GHC's ccall mechanism.
3251 * They are used to "import" C functions into a module.
3252 * They are usually not written by hand but, rather, generated automatically
3253 * by GreenCard, IDL compilers or whatever. We support foreign import
3254 * (static) and foreign import dynamic. In the latter case, extName==NIL.
3256 * Foreign export declarations generate C wrappers for Hugs functions.
3257 * Hugs only provides "foreign export dynamic" because it's not obvious
3258 * what "foreign export static" would mean in an interactive setting.
3259 * ------------------------------------------------------------------------*/
3261 Void foreignImport(line,callconv,extName,intName,type)
3262 /* Handle foreign imports */
3268 Text t = textOf(intName);
3269 Name n = findName(t);
3270 Int l = intOf(line);
3274 } else if (name(n).defn!=PREDEFINED) {
3275 ERRMSG(l) "Redeclaration of foreign \"%s\"", textToStr(t)
3279 name(n).defn = extName;
3280 name(n).type = type;
3281 name(n).callconv = callconv;
3282 foreignImports = cons(n,foreignImports);
3285 static Void local checkForeignImport(p) /* Check foreign import */
3287 emptySubstitution();
3288 name(p).type = checkSigType(name(p).line,
3289 "foreign import declaration",
3292 /* We don't expand synonyms here because we don't want the IO
3293 * part to be expanded.
3294 * name(p).type = fullExpand(name(p).type);
3296 implementForeignImport(p);
3299 Void foreignExport(line,callconv,extName,intName,type)
3300 /* Handle foreign exports */
3306 Text t = textOf(intName);
3307 Name n = findName(t);
3308 Int l = intOf(line);
3312 } else if (name(n).defn!=PREDEFINED) {
3313 ERRMSG(l) "Redeclaration of foreign \"%s\"", textToStr(t)
3317 name(n).defn = NIL; /* nothing to say */
3318 name(n).type = type;
3319 name(n).callconv = callconv;
3320 foreignExports = cons(n,foreignExports);
3323 static Void local checkForeignExport(p) /* Check foreign export */
3325 emptySubstitution();
3326 name(p).type = checkSigType(name(p).line,
3327 "foreign export declaration",
3330 implementForeignExport(p);
3335 /* --------------------------------------------------------------------------
3336 * Static analysis of patterns:
3338 * Patterns are parsed as ordinary (atomic) expressions. Static analysis
3339 * makes the following checks:
3340 * - Patterns are well formed (according to pattern syntax), including the
3341 * special case of (n+k) patterns.
3342 * - All constructor functions have been defined and are used with the
3343 * correct number of arguments.
3344 * - No variable name is used more than once in a pattern.
3346 * The list of pattern variables occuring in each pattern is accumulated in
3347 * a global list `patVars', which must be initialised to NIL at appropriate
3348 * points before using these routines to check for valid patterns. This
3349 * mechanism enables the pattern checking routine to be mapped over a list
3350 * of patterns, ensuring that no variable occurs more than once in the
3351 * complete pattern list (as is required on the lhs of a function defn).
3352 * ------------------------------------------------------------------------*/
3354 static List patVars; /* List of vars bound in pattern */
3356 static Cell local checkPat(line,p) /* Check valid pattern syntax */
3359 switch (whatIs(p)) {
3361 case VAROPCELL : addToPatVars(line,p);
3364 case INFIX : return checkPat(line,tidyInfix(line,snd(p)));
3366 case AP : return checkMaybeCnkPat(line,p);
3371 case CONOPCELL : return checkApPat(line,0,p);
3376 case FLOATCELL : break;
3377 case INTCELL : break;
3379 case ASPAT : addToPatVars(line,fst(snd(p)));
3380 snd(snd(p)) = checkPat(line,snd(snd(p)));
3383 case LAZYPAT : snd(p) = checkPat(line,snd(p));
3386 case FINLIST : map1Over(checkPat,line,snd(p));
3389 case CONFLDS : depConFlds(line,p,TRUE);
3392 case ESIGN : snd(snd(p)) = checkPatType(line,
3396 fst(snd(p)) = checkPat(line,fst(snd(p)));
3399 default : ERRMSG(line) "Illegal pattern syntax"
3405 static Cell local checkMaybeCnkPat(l,p)/* Check applicative pattern with */
3406 Int l; /* the possibility of n+k pattern */
3409 Cell h = getHead(p);
3411 if (argCount==2 && isVar(h) && textOf(h)==textPlus) { /* n+k */
3412 Cell v = arg(fun(p));
3413 if (!isInt(arg(p))) {
3414 ERRMSG(l) "Second argument in (n+k) pattern must be an integer"
3417 if (intOf(arg(p))<=0) {
3418 ERRMSG(l) "Integer k in (n+k) pattern must be > 0"
3421 fst(fun(p)) = ADDPAT;
3422 intValOf(fun(p)) = intOf(arg(p));
3423 arg(p) = checkPat(l,v);
3427 return checkApPat(l,0,p);
3430 static Cell local checkApPat(line,args,p)
3431 Int line; /* check validity of application */
3432 Int args; /* of constructor to arguments */
3434 switch (whatIs(p)) {
3435 case AP : fun(p) = checkApPat(line,args+1,fun(p));
3436 arg(p) = checkPat(line,arg(p));
3439 case TUPLE : if (tupleOf(p)!=args) {
3440 ERRMSG(line) "Illegal tuple pattern"
3446 case EXT : h98DoesntSupport(line,"extensible records");
3448 ERRMSG(line) "Illegal record pattern"
3454 case QUALIDENT : if (!isQCon(p)) {
3456 "Illegal use of qualified variable in pattern"
3459 /* deliberate fall through */
3461 case CONOPCELL : p = conDefined(line,p);
3462 checkCfunArgs(line,p,args);
3465 case NAME : checkIsCfun(line,p);
3466 checkCfunArgs(line,p,args);
3469 default : ERRMSG(line) "Illegal pattern syntax"
3475 static Void local addToPatVars(line,v) /* Add variable v to list of vars */
3476 Int line; /* in current pattern, checking */
3477 Cell v; { /* for repeated variables. */
3482 for (; nonNull(n); p=n, n=tl(n)) {
3483 if (textOf(hd(n))==t) {
3484 ERRMSG(line) "Repeated variable \"%s\" in pattern",
3491 patVars = cons(v,NIL);
3493 tl(p) = cons(v,NIL);
3497 static Name local conDefined(line,nm) /* check that nm is the name of a */
3498 Int line; /* previously defined constructor */
3499 Cell nm; { /* function. */
3500 Name n = findQualName(nm);
3502 ERRMSG(line) "Undefined constructor function \"%s\"", identToStr(nm)
3505 checkIsCfun(line,n);
3509 static Void local checkIsCfun(line,c) /* Check that c is a constructor fn */
3513 ERRMSG(line) "\"%s\" is not a constructor function",
3514 textToStr(name(c).text)
3519 static Void local checkCfunArgs(line,c,args)
3520 Int line; /* Check constructor applied with */
3521 Cell c; /* correct number of arguments */
3523 Int a = userArity(c);
3526 "Constructor \"%s\" must have exactly %d argument%s in pattern",
3527 textToStr(name(c).text), a, ((a==1)?"":"s")
3532 static Cell local checkPatType(l,wh,e,t)/* Check type appearing in pattern */
3537 List tvs = typeVarsIn(t,NIL,NIL,NIL);
3538 h98DoesntSupport(l,"pattern type annotations");
3539 for (; nonNull(tvs); tvs=tl(tvs)) {
3540 Int beta = newKindvars(1);
3541 hd(btyvars) = cons(pair(hd(tvs),mkInt(beta)), hd(btyvars));
3543 t = checkSigType(l,"pattern type",e,t);
3544 if (isPolyOrQualType(t) || whatIs(t)==RANK2) {
3545 ERRMSG(l) "Illegal syntax in %s type annotation", wh
3551 static Cell local applyBtyvs(pat) /* Record bound type vars in pat */
3553 List bts = hd(btyvars);
3556 pat = ap(BIGLAM,pair(bts,pat));
3557 for (; nonNull(bts); bts=tl(bts)) {
3558 snd(hd(bts)) = copyKindvar(intOf(snd(hd(bts))));
3564 /* --------------------------------------------------------------------------
3565 * Maintaining lists of bound variables and local definitions, for
3566 * dependency and scope analysis.
3567 * ------------------------------------------------------------------------*/
3569 static List bounds; /* list of lists of bound vars */
3570 static List bindings; /* list of lists of binds in scope */
3571 static List depends; /* list of lists of dependents */
3573 /* bounds :: [[Var]] -- var equality used on Vars */
3574 /* bindings :: [[([Var],?)]] -- var equality used on Vars */
3575 /* depends :: [[Var]] -- pointer equality used on Vars */
3577 #define saveBvars() hd(bounds) /* list of bvars in current scope */
3578 #define restoreBvars(bs) hd(bounds)=bs /* restore list of bound variables */
3580 static Cell local bindPat(line,p) /* add new bound vars for pattern */
3584 p = checkPat(line,p);
3585 hd(bounds) = revOnto(patVars,hd(bounds));
3589 static Void local bindPats(line,ps) /* add new bound vars for patterns */
3593 map1Over(checkPat,line,ps);
3594 hd(bounds) = revOnto(patVars,hd(bounds));
3597 /* --------------------------------------------------------------------------
3598 * Before processing value and type signature declarations, all data and
3599 * type definitions have been processed so that:
3600 * - all valid type constructors (with their arities) are known.
3601 * - all valid constructor functions (with their arities and types) are
3604 * The result of parsing a list of value declarations is a list of Eqns:
3605 * Eqn ::= (SIGDECL,(Line,[Var],type))
3606 * | (FIXDECL,(Line,[Op],SyntaxInt))
3608 * The ordering of the equations in this list is the reverse of the original
3609 * ordering in the script parsed. This is a consequence of the structure of
3610 * the parser ... but also turns out to be most convenient for the static
3613 * As the first stage of the static analysis of value declarations, each
3614 * list of Eqns is converted to a list of Bindings. As part of this
3616 * - The ordering of the list of Bindings produced is the same as in the
3618 * - When a variable (function) is defined over a number of lines, all
3619 * of the definitions should appear together and each should give the
3620 * same arity to the variable being defined.
3621 * - No variable can have more than one definition.
3622 * - For pattern bindings:
3623 * - Each lhs is a valid pattern/function lhs, all constructor functions
3624 * have been defined and are used with the correct number of arguments.
3625 * - Each lhs contains no repeated pattern variables.
3626 * - Each equation defines at least one variable (e.g. True = False is
3628 * - Types appearing in type signatures are well formed:
3629 * - Type constructors used are defined and used with correct number
3631 * - type variables are replaced by offsets, type constructor names
3633 * - Every variable named in a type signature declaration is defined by
3634 * one or more equations elsewhere in the script.
3635 * - No variable has more than one type declaration.
3636 * - Similar properties for fixity declarations.
3638 * ------------------------------------------------------------------------*/
3640 #define bindingAttr(b) fst(snd(b)) /* type(s)/fixity(ies) for binding */
3641 #define fbindAlts(b) snd(snd(b)) /* alternatives for function binding*/
3643 static List local extractSigdecls(es) /* Extract the SIGDECLS from list */
3644 List es; { /* of equations */
3645 List sigdecls = NIL; /* :: [(Line,[Var],Type)] */
3647 for(; nonNull(es); es=tl(es)) {
3648 if (fst(hd(es))==SIGDECL) { /* type-declaration? */
3649 Pair sig = snd(hd(es));
3650 Int line = intOf(fst3(sig));
3651 List vs = snd3(sig);
3652 for(; nonNull(vs); vs=tl(vs)) {
3653 if (isQualIdent(hd(vs))) {
3654 ERRMSG(line) "Type signature for qualified variable \"%s\" is not allowed",
3659 sigdecls = cons(sig,sigdecls); /* discard SIGDECL tag*/
3665 static List local extractFixdecls(es) /* Extract the FIXDECLS from list */
3666 List es; { /* of equations */
3667 List fixdecls = NIL; /* :: [(Line,SyntaxInt,[Op])] */
3669 for(; nonNull(es); es=tl(es)) {
3670 if (fst(hd(es))==FIXDECL) { /* fixity declaration?*/
3671 fixdecls = cons(snd(hd(es)),fixdecls); /* discard FIXDECL tag*/
3677 static List local extractBindings(ds) /* extract untyped bindings from */
3678 List ds; { /* given list of equations */
3679 Cell lastVar = NIL; /* = var def'd in last eqn (if any)*/
3680 Int lastArity = 0; /* = number of args in last defn */
3681 List bs = NIL; /* :: [Binding] */
3683 for(; nonNull(ds); ds=tl(ds)) {
3685 if (fst(d)==FUNBIND) { /* Function bindings */
3686 Cell rhs = snd(snd(d));
3687 Int line = rhsLine(rhs);
3688 Cell lhs = fst(snd(d));
3689 Cell v = getHead(lhs);
3690 Cell newAlt = pair(getArgs(lhs),rhs);
3692 internal("FUNBIND");
3694 if (nonNull(lastVar) && textOf(v)==textOf(lastVar)) {
3695 if (argCount!=lastArity) {
3696 ERRMSG(line) "Equations give different arities for \"%s\"",
3697 textToStr(textOf(v))
3700 fbindAlts(hd(bs)) = cons(newAlt,fbindAlts(hd(bs)));
3704 lastArity = argCount;
3705 notDefined(line,bs,v);
3706 bs = cons(pair(v,pair(NIL,singleton(newAlt))),bs);
3709 } else if (fst(d)==PATBIND) { /* Pattern bindings */
3710 Cell rhs = snd(snd(d));
3711 Int line = rhsLine(rhs);
3712 Cell pat = fst(snd(d));
3713 while (whatIs(pat)==ESIGN) {/* Move type annotations to rhs */
3714 Cell p = fst(snd(pat));
3715 fst(snd(pat)) = rhs;
3716 snd(snd(d)) = rhs = pat;
3717 fst(snd(d)) = pat = p;
3720 if (isVar(pat)) { /* Convert simple pattern bind to */
3721 notDefined(line,bs,pat);/* a function binding */
3722 bs = cons(pair(pat,pair(NIL,singleton(pair(NIL,rhs)))),bs);
3724 List vs = getPatVars(line,pat,NIL);
3726 ERRMSG(line) "No variables defined in lhs pattern"
3729 map2Proc(notDefined,line,bs,vs);
3730 bs = cons(pair(vs,pair(NIL,snd(d))),bs);
3738 static List local getPatVars(line,p,vs) /* Find list of variables bound in */
3739 Int line; /* pattern p */
3742 switch (whatIs(p)) {
3744 vs = getPatVars(line,arg(p),vs);
3747 return vs; /* Ignore head of application */
3749 case CONFLDS : { List pfs = snd(snd(p));
3750 for (; nonNull(pfs); pfs=tl(pfs)) {
3751 if (isVar(hd(pfs))) {
3752 vs = addPatVar(line,hd(pfs),vs);
3754 vs = getPatVars(line,snd(hd(pfs)),vs);
3760 case FINLIST : { List ps = snd(p);
3761 for (; nonNull(ps); ps=tl(ps)) {
3762 vs = getPatVars(line,hd(ps),vs);
3767 case ESIGN : return getPatVars(line,fst(snd(p)),vs);
3772 case INFIX : return getPatVars(line,snd(p),vs);
3774 case ASPAT : return addPatVar(line,fst(snd(p)),
3775 getPatVars(line,snd(snd(p)),vs));
3778 case VAROPCELL : return addPatVar(line,p,vs);
3788 case WILDCARD : return vs;
3790 default : internal("getPatVars");
3795 static List local addPatVar(line,v,vs) /* Add var to list of previously */
3796 Int line; /* encountered variables */
3799 if (varIsMember(textOf(v),vs)) {
3800 ERRMSG(line) "Repeated use of variable \"%s\" in pattern binding",
3801 textToStr(textOf(v))
3807 static List local eqnsToBindings(es,ts,cs,ps)
3808 List es; /* Convert list of equations to */
3809 List ts; /* list of typed bindings */
3812 List bs = extractBindings(es);
3813 map1Proc(addSigdecl,bs,extractSigdecls(es));
3814 map4Proc(addFixdecl,bs,ts,cs,ps,extractFixdecls(es));
3818 static Void local notDefined(line,bs,v)/* check if name already defined in */
3819 Int line; /* list of bindings */
3822 if (nonNull(findBinding(textOf(v),bs))) {
3823 ERRMSG(line) "\"%s\" multiply defined", textToStr(textOf(v))
3828 static Cell local findBinding(t,bs) /* look for binding for variable t */
3829 Text t; /* in list of bindings bs */
3831 for (; nonNull(bs); bs=tl(bs)) {
3832 if (isVar(fst(hd(bs)))) { /* function-binding? */
3833 if (textOf(fst(hd(bs)))==t) {
3836 } else if (nonNull(varIsMember(t,fst(hd(bs))))){/* pattern-binding?*/
3843 static Cell local getAttr(bs,v) /* Locate type/fixity attribute */
3844 List bs; /* for variable v in bindings bs */
3847 Cell b = findBinding(t,bs);
3849 if (isNull(b)) { /* No binding */
3851 } else if (isVar(fst(b))) { /* func binding? */
3852 if (isNull(bindingAttr(b))) {
3853 bindingAttr(b) = pair(NIL,NIL);
3855 return bindingAttr(b);
3856 } else { /* pat binding? */
3858 List as = bindingAttr(b);
3861 bindingAttr(b) = as = replicate(length(vs),NIL);
3864 while (nonNull(vs) && t!=textOf(hd(vs))) {
3870 internal("getAttr");
3871 } else if (isNull(hd(as))) {
3872 hd(as) = pair(NIL,NIL);
3878 static Void local addSigdecl(bs,sigdecl)/* add type information to bindings*/
3879 List bs; /* :: [Binding] */
3880 Cell sigdecl; { /* :: (Line,[Var],Type) */
3881 Int l = intOf(fst3(sigdecl));
3882 List vs = snd3(sigdecl);
3883 Type type = checkSigType(l,"type declaration",hd(vs),thd3(sigdecl));
3885 for (; nonNull(vs); vs=tl(vs)) {
3887 Pair attr = getAttr(bs,v);
3889 ERRMSG(l) "Missing binding for variable \"%s\" in type signature",
3890 textToStr(textOf(v))
3892 } else if (nonNull(fst(attr))) {
3893 ERRMSG(l) "Repeated type signature for \"%s\"",
3894 textToStr(textOf(v))
3901 static Void local addFixdecl(bs,ts,cs,ps,fixdecl)
3907 Int line = intOf(fst3(fixdecl));
3908 List ops = snd3(fixdecl);
3909 Cell sy = thd3(fixdecl);
3911 for (; nonNull(ops); ops=tl(ops)) {
3913 Text t = textOf(op);
3914 Cell attr = getAttr(bs,op);
3915 if (nonNull(attr)) { /* Found name in binding? */
3916 if (nonNull(snd(attr))) {
3920 } else { /* Look in tycons, classes, prims */
3925 for (; isNull(n) && nonNull(ts1); ts1=tl(ts1)) { /* tycons */
3927 if (tycon(tc).what==DATATYPE || tycon(tc).what==NEWTYPE) {
3928 n = nameIsMember(t,tycon(tc).defn);
3931 for (; isNull(n) && nonNull(cs1); cs1=tl(cs1)) { /* classes */
3932 n = nameIsMember(t,cclass(hd(cs1)).members);
3934 for (; isNull(n) && nonNull(ps1); ps1=tl(ps1)) { /* prims */
3935 n = nameIsMember(t,hd(ps1));
3940 } else if (name(n).syntax!=NO_SYNTAX) {
3943 name(n).syntax = intOf(sy);
3948 static Void local dupFixity(line,t) /* Report repeated fixity decl */
3952 "Repeated fixity declaration for operator \"%s\"", textToStr(t)
3956 static Void local missFixity(line,t) /* Report missing op for fixity */
3960 "Cannot find binding for operator \"%s\" in fixity declaration",
3965 /* --------------------------------------------------------------------------
3966 * Dealing with infix operators:
3968 * Expressions involving infix operators or unary minus are parsed as
3969 * elements of the following type:
3971 * data InfixExp = Only Exp | Neg InfixExp | Infix InfixExp Op Exp
3973 * (The algorithms here do not assume that negation can be applied only once,
3974 * i.e., that - - x is a syntax error, as required by the Haskell report.
3975 * Instead, that restriction is captured by the grammar itself, given above.)
3977 * There are rules of precedence and grouping, expressed by two functions:
3979 * prec :: Op -> Int; assoc :: Op -> Assoc (Assoc = {L, N, R})
3981 * InfixExp values are rearranged accordingly when a complete expression
3982 * has been read using a simple shift-reduce parser whose result may be taken
3983 * to be a value of the following type:
3985 * data Exp = Atom Int | Negate Exp | Apply Op Exp Exp | Error String
3987 * The machine on which this parser is based can be defined as follows:
3989 * tidy :: InfixExp -> [(Op,Exp)] -> Exp
3990 * tidy (Only a) [] = a
3991 * tidy (Only a) ((o,b):ss) = tidy (Only (Apply o a b)) ss
3992 * tidy (Infix a o b) [] = tidy a [(o,b)]
3993 * tidy (Infix a o b) ((p,c):ss)
3994 * | shift o p = tidy a ((o,b):(p,c):ss)
3995 * | red o p = tidy (Infix a o (Apply p b c)) ss
3996 * | ambig o p = Error "ambiguous use of operators"
3997 * tidy (Neg e) [] = tidy (tidyNeg e) []
3998 * tidy (Neg e) ((o,b):ss)
3999 * | nshift o = tidy (Neg (underNeg o b e)) ss
4000 * | nred o = tidy (tidyNeg e) ((o,b):ss)
4001 * | nambig o = Error "illegal use of negation"
4003 * At each stage, the parser can either shift, reduce, accept, or error.
4004 * The transitions when dealing with juxtaposed operators o and p are
4005 * determined by the following rules:
4007 * shift o p = (prec o > prec p)
4008 * || (prec o == prec p && assoc o == L && assoc p == L)
4010 * red o p = (prec o < prec p)
4011 * || (prec o == prec p && assoc o == R && assoc p == R)
4013 * ambig o p = (prec o == prec p)
4014 * && (assoc o == N || assoc p == N || assoc o /= assoc p)
4016 * The transitions when dealing with juxtaposed unary minus and infix
4017 * operators are as follows. The precedence of unary minus (infixl 6) is
4018 * hardwired in to these definitions, as it is to the definitions of the
4019 * Haskell grammar in the official report.
4021 * nshift o = (prec o > 6)
4022 * nred o = (prec o < 6) || (prec o == 6 && assoc o == L)
4023 * nambig o = prec o == 6 && (assoc o == R || assoc o == N)
4025 * An InfixExp of the form (Neg e) means negate the last thing in
4026 * the InfixExp e; we can force this negation using:
4028 * tidyNeg :: OpExp -> OpExp
4029 * tidyNeg (Only e) = Only (Negate e)
4030 * tidyNeg (Infix a o b) = Infix a o (Negate b)
4031 * tidyNeg (Neg e) = tidyNeg (tidyNeg e)
4033 * On the other hand, if we want to sneak application of an infix operator
4034 * under a negation, then we use:
4036 * underNeg :: Op -> Exp -> OpExp -> OpExp
4037 * underNeg o b (Only e) = Only (Apply o e b)
4038 * underNeg o b (Neg e) = Neg (underNeg o b e)
4039 * underNeg o b (Infix e p f) = Infix e p (Apply o f b)
4041 * As a concession to efficiency, we lower the number of calls to syntaxOf
4042 * by keeping track of the values of sye, sys throughout the process. The
4043 * value APPLIC is used to indicate that the syntax value is unknown.
4044 * ------------------------------------------------------------------------*/
4046 static Cell local tidyInfix(line,e) /* Convert infixExp to Exp */
4048 Cell e; { /* :: OpExp */
4049 Cell s = NIL; /* :: [(Op,Exp)] */
4050 Syntax sye = APPLIC; /* Syntax of op in e (init unknown)*/
4051 Syntax sys = APPLIC; /* Syntax of op in s (init unknown)*/
4054 while (fst(d)!=ONLY) { /* Attach fixities to operators */
4058 fun(fun(d)) = attachFixity(line,fun(fun(d)));
4064 switch (whatIs(e)) {
4065 case ONLY : e = snd(e);
4066 while (nonNull(s)) {
4067 Cell next = arg(fun(s));
4069 fun(fun(s)) = snd(fun(fun(s)));
4075 case NEG : if (nonNull(s)) {
4076 if (sys==APPLIC) { /* calculate sys */
4077 sys = intOf(fst(fun(fun(s))));
4080 if (precOf(sys)==UMINUS_PREC && /* nambig */
4081 assocOf(sys)!=UMINUS_ASSOC) {
4083 "Ambiguous use of unary minus with \""
4084 ETHEN ERREXPR(snd(fun(fun(s))));
4089 if (precOf(sys)>UMINUS_PREC) { /* nshift */
4093 while (whatIs(e1)==NEG)
4095 arg(fun(t)) = arg(e1);
4096 fun(fun(t)) = snd(fun(fun(t)));
4103 /* Intentional fall-thru for nreduce and isNull(s) */
4105 { Cell prev = e; /* e := tidyNeg e */
4106 Cell temp = arg(prev);
4108 for (; whatIs(temp)==NEG; nneg++) {
4109 fun(prev) = nameNegate;
4113 if (isInt(arg(temp))) { /* special cases */
4114 if (nneg&1) /* for literals */
4115 arg(temp) = mkInt(-intOf(arg(temp)));
4117 else if (isFloat(arg(temp))) {
4119 arg(temp) = floatNegate(arg(temp));
4120 //mkFloat(-floatOf(arg(temp)));
4123 fun(prev) = nameNegate;
4124 arg(prev) = arg(temp);
4131 default : if (isNull(s)) {/* Move operation onto empty stack */
4132 Cell next = arg(fun(e));
4139 else { /* deal with pair of operators */
4141 if (sye==APPLIC) { /* calculate sys and sye */
4142 sye = intOf(fst(fun(fun(e))));
4145 sys = intOf(fst(fun(fun(s))));
4148 if (precOf(sye)==precOf(sys) && /* ambig */
4149 (assocOf(sye)!=assocOf(sys) ||
4150 assocOf(sye)==NON_ASS)) {
4151 ERRMSG(line) "Ambiguous use of operator \""
4152 ETHEN ERREXPR(snd(fun(fun(e))));
4153 ERRTEXT "\" with \""
4154 ETHEN ERREXPR(snd(fun(fun(s))));
4159 if (precOf(sye)>precOf(sys) || /* shift */
4160 (precOf(sye)==precOf(sys) &&
4161 assocOf(sye)==LEFT_ASS &&
4162 assocOf(sys)==LEFT_ASS)) {
4163 Cell next = arg(fun(e));
4171 Cell next = arg(fun(s));
4172 arg(fun(s)) = arg(e);
4173 fun(fun(s)) = snd(fun(fun(s)));
4184 static Pair local attachFixity(line,op) /* Attach fixity to operator in an */
4185 Int line; /* infix expression */
4187 Syntax sy = DEF_OPSYNTAX;
4189 switch (whatIs(op)) {
4191 case VARIDCELL : if ((sy=lookupSyntax(textOf(op)))==NO_SYNTAX) {
4192 Name n = findName(textOf(op));
4194 ERRMSG(line) "Undefined variable \"%s\"",
4195 textToStr(textOf(op))
4204 case CONIDCELL : sy = syntaxOf(op = conDefined(line,op));
4207 case QUALIDENT : { Name n = findQualName(op);
4213 "Undefined qualified variable \"%s\"",
4223 return pair(mkInt(sy),op); /* Pair fixity with (possibly) */
4224 /* translated operator */
4227 static Syntax local lookupSyntax(t) /* Try to find fixity for var in */
4228 Text t; { /* enclosing bindings */
4229 List bounds1 = bounds;
4230 List bindings1 = bindings;
4232 while (nonNull(bindings1)) {
4233 if (nonNull(varIsMember(t,hd(bounds1)))) {
4234 return DEF_OPSYNTAX;
4236 Cell b = findBinding(t,hd(bindings1));
4238 Cell a = fst(snd(b));
4239 if (isVar(fst(b))) { /* Function binding */
4240 if (nonNull(a) && nonNull(snd(a))) {
4241 return intOf(snd(a));
4243 } else { /* Pattern binding */
4245 while (nonNull(vs) && nonNull(a)) {
4246 if (t==textOf(hd(vs))) {
4247 if (nonNull(hd(a)) && isInt(snd(hd(a)))) {
4248 return intOf(snd(hd(a)));
4256 return DEF_OPSYNTAX;
4259 bounds1 = tl(bounds1);
4260 bindings1 = tl(bindings1);
4265 /* --------------------------------------------------------------------------
4266 * To facilitate dependency analysis, lists of bindings are temporarily
4267 * augmented with an additional field, which is used in two ways:
4268 * - to build the `adjacency lists' for the dependency graph. Represented by
4269 * a list of pointers to other bindings in the same list of bindings.
4270 * - to hold strictly positive integer values (depth first search numbers) of
4271 * elements `on the stack' during the strongly connected components search
4272 * algorithm, or a special value mkInt(0), once the binding has been added
4273 * to a particular strongly connected component.
4275 * Using this extra field, the type of each list of declarations during
4276 * dependency analysis is [Binding'] where:
4278 * Binding' ::= (Var, (Attr, (Dep, [Alt]))) -- function binding
4279 * | ([Var], ([Attr], (Dep, (Pat,Rhs)))) -- pattern binding
4281 * ------------------------------------------------------------------------*/
4283 #define depVal(d) (fst(snd(snd(d)))) /* Access to dependency information*/
4285 static List local dependencyAnal(bs) /* Separate lists of bindings into */
4286 List bs; { /* mutually recursive groups in */
4287 /* order of dependency */
4288 mapProc(addDepField,bs); /* add extra field for dependents */
4289 mapProc(depBinding,bs); /* find dependents of each binding */
4290 bs = bscc(bs); /* sort to strongly connected comps*/
4291 mapProc(remDepField,bs); /* remove dependency info field */
4295 static List local topDependAnal(bs) /* Like dependencyAnal(), but at */
4296 List bs; { /* top level, reporting on progress*/
4300 setGoal("Dependency analysis",(Target)(length(bs)));
4302 mapProc(addDepField,bs); /* add extra field for dependents */
4303 for (xs=bs; nonNull(xs); xs=tl(xs)) {
4304 emptySubstitution();
4306 soFar((Target)(i++));
4308 bs = bscc(bs); /* sort to strongly connected comps */
4309 mapProc(remDepField,bs); /* remove dependency info field */
4314 static Void local addDepField(b) /* add extra field to binding to */
4315 Cell b; { /* hold list of dependents */
4316 snd(snd(b)) = pair(NIL,snd(snd(b)));
4319 static Void local remDepField(bs) /* remove dependency field from */
4320 List bs; { /* list of bindings */
4321 mapProc(remDepField1,bs);
4324 static Void local remDepField1(b) /* remove dependency field from */
4325 Cell b; { /* single binding */
4326 snd(snd(b)) = snd(snd(snd(b)));
4329 static Void local clearScope() { /* initialise dependency scoping */
4335 static Void local withinScope(bs) /* Enter scope of bindings bs */
4337 bounds = cons(NIL,bounds);
4338 bindings = cons(bs,bindings);
4339 depends = cons(NIL,depends);
4342 static Void local leaveScope() { /* Leave scope of last withinScope */
4343 List bs = hd(bindings); /* Remove fixity info from binds */
4344 Bool toplevel = isNull(tl(bindings));
4345 for (; nonNull(bs); bs=tl(bs)) {
4347 if (isVar(fst(b))) { /* Variable binding */
4348 Cell a = fst(snd(b));
4351 saveSyntax(fst(b),snd(a));
4353 fst(snd(b)) = fst(a);
4355 } else { /* Pattern binding */
4357 List as = fst(snd(b));
4358 while (nonNull(vs) && nonNull(as)) {
4359 if (isPair(hd(as))) {
4361 saveSyntax(hd(vs),snd(hd(as)));
4363 hd(as) = fst(hd(as));
4370 bounds = tl(bounds);
4371 bindings = tl(bindings);
4372 depends = tl(depends);
4375 static Void local saveSyntax(v,sy) /* Save syntax of top-level var */
4376 Cell v; /* in corresponding Name */
4378 Name n = findName(textOf(v));
4379 if (isNull(n) || name(n).syntax!=NO_SYNTAX) {
4380 internal("saveSyntax");
4383 name(n).syntax = intOf(sy);
4387 /* --------------------------------------------------------------------------
4388 * As a side effect of the dependency analysis we also make the following
4390 * - Each lhs is a valid pattern/function lhs, all constructor functions
4391 * have been defined and are used with the correct number of arguments.
4392 * - No lhs contains repeated pattern variables.
4393 * - Expressions used on the rhs of an eqn should be well formed. This
4395 * - Checking for valid patterns (including repeated vars) in lambda,
4396 * case, and list comprehension expressions.
4397 * - Recursively checking local lists of equations.
4398 * - No free (i.e. unbound) variables are used in the declaration list.
4399 * ------------------------------------------------------------------------*/
4401 static Void local depBinding(b) /* find dependents of binding */
4403 Cell defpart = snd(snd(snd(b))); /* definition part of binding */
4407 if (isVar(fst(b))) { /* function-binding? */
4408 mapProc(depAlt,defpart);
4409 if (isNull(fst(snd(b)))) { /* Save dep info if no type sig */
4410 fst(snd(b)) = pair(ap(IMPDEPS,hd(depends)),NIL);
4411 } else if (isNull(fst(fst(snd(b))))) {
4412 fst(fst(snd(b))) = ap(IMPDEPS,hd(depends));
4414 } else { /* pattern-binding? */
4415 Int line = rhsLine(snd(defpart));
4418 fst(defpart) = checkPat(line,fst(defpart));
4419 depRhs(snd(defpart));
4421 if (nonNull(hd(btyvars))) {
4423 "Sorry, no type variables are allowed in pattern binding type annotations"
4427 fst(defpart) = applyBtyvs(fst(defpart));
4429 depVal(b) = hd(depends);
4432 static Void local depDefaults(c) /* dependency analysis on defaults */
4433 Class c; { /* from class definition */
4434 depClassBindings(cclass(c).defaults);
4437 static Void local depInsts(in) /* dependency analysis on instance */
4438 Inst in; { /* bindings */
4439 depClassBindings(inst(in).implements);
4442 static Void local depClassBindings(bs) /* dependency analysis on list of */
4443 List bs; { /* bindings, possibly containing */
4444 for (; nonNull(bs); bs=tl(bs)) { /* NIL bindings ... */
4445 if (nonNull(hd(bs))) { /* No need to add extra field for */
4446 mapProc(depAlt,snd(hd(bs)));/* dependency information... */
4451 static Void local depAlt(a) /* Find dependents of alternative */
4453 List obvs = saveBvars(); /* Save list of bound variables */
4455 bindPats(rhsLine(snd(a)),fst(a)); /* add new bound vars for patterns */
4456 depRhs(snd(a)); /* find dependents of rhs */
4457 fst(a) = applyBtyvs(fst(a));
4458 restoreBvars(obvs); /* restore original list of bvars */
4461 static Void local depRhs(r) /* Find dependents of rhs */
4463 switch (whatIs(r)) {
4464 case GUARDED : mapProc(depGuard,snd(r));
4467 case LETREC : fst(snd(r)) = eqnsToBindings(fst(snd(r)),NIL,NIL,NIL);
4468 withinScope(fst(snd(r)));
4469 fst(snd(r)) = dependencyAnal(fst(snd(r)));
4470 hd(depends) = fst(snd(r));
4471 depRhs(snd(snd(r)));
4475 case RSIGN : snd(snd(r)) = checkPatType(rhsLine(fst(snd(r))),
4477 rhsExpr(fst(snd(r))),
4479 depRhs(fst(snd(r)));
4482 default : snd(r) = depExpr(intOf(fst(r)),snd(r));
4487 static Void local depGuard(g) /* find dependents of single guarded*/
4488 Cell g; { /* expression */
4489 depPair(intOf(fst(g)),snd(g));
4492 static Cell local depExpr(line,e) /* find dependents of expression */
4495 // Printf( "\n\n"); print(e,100); Printf("\n");
4496 //printExp(stdout,e);
4497 switch (whatIs(e)) {
4500 case VAROPCELL : return depVar(line,e);
4503 case CONOPCELL : return conDefined(line,e);
4505 case QUALIDENT : if (isQVar(e)) {
4506 return depQVar(line,e);
4507 } else { /* QConOrConOp */
4508 return conDefined(line,e);
4511 case INFIX : return depExpr(line,tidyInfix(line,snd(e)));
4514 case RECSEL : break;
4516 case AP : if (isAp(e) && isAp(fun(e)) && isExt(fun(fun(e)))) {
4517 return depRecord(line,e);
4523 arg(a) = depExpr(line,arg(a));
4526 fun(a) = depExpr(line,fun(a));
4530 case AP : depPair(line,e);
4544 case INTCELL : break;
4546 case COND : depTriple(line,snd(e));
4549 case FINLIST : map1Over(depExpr,line,snd(e));
4552 case LETREC : fst(snd(e)) = eqnsToBindings(fst(snd(e)),NIL,NIL,NIL);
4553 withinScope(fst(snd(e)));
4554 fst(snd(e)) = dependencyAnal(fst(snd(e)));
4555 hd(depends) = fst(snd(e));
4556 snd(snd(e)) = depExpr(line,snd(snd(e)));
4560 case LAMBDA : depAlt(snd(e));
4563 case DOCOMP : /* fall-thru */
4564 case COMP : depComp(line,snd(e),snd(snd(e)));
4567 case ESIGN : fst(snd(e)) = depExpr(line,fst(snd(e)));
4568 snd(snd(e)) = checkSigType(line,
4574 case CASE : fst(snd(e)) = depExpr(line,fst(snd(e)));
4575 map1Proc(depCaseAlt,line,snd(snd(e)));
4578 case CONFLDS : depConFlds(line,e,FALSE);
4581 case UPDFLDS : depUpdFlds(line,e);
4585 case WITHEXP : depWith(line,e);
4589 case ASPAT : ERRMSG(line) "Illegal `@' in expression"
4592 case LAZYPAT : ERRMSG(line) "Illegal `~' in expression"
4595 case WILDCARD : ERRMSG(line) "Illegal `_' in expression"
4599 case EXT : ERRMSG(line) "Illegal application of record"
4603 default : internal("depExpr");
4608 static Void local depPair(line,e) /* find dependents of pair of exprs*/
4611 fst(e) = depExpr(line,fst(e));
4612 snd(e) = depExpr(line,snd(e));
4615 static Void local depTriple(line,e) /* find dependents of triple exprs */
4618 fst3(e) = depExpr(line,fst3(e));
4619 snd3(e) = depExpr(line,snd3(e));
4620 thd3(e) = depExpr(line,thd3(e));
4623 static Void local depComp(l,e,qs) /* find dependents of comprehension*/
4628 fst(e) = depExpr(l,fst(e));
4632 switch (whatIs(q)) {
4633 case FROMQUAL : { List obvs = saveBvars();
4634 snd(snd(q)) = depExpr(l,snd(snd(q)));
4636 fst(snd(q)) = bindPat(l,fst(snd(q)));
4638 fst(snd(q)) = applyBtyvs(fst(snd(q)));
4643 case QWHERE : snd(q) = eqnsToBindings(snd(q),NIL,NIL,NIL);
4644 withinScope(snd(q));
4645 snd(q) = dependencyAnal(snd(q));
4646 hd(depends) = snd(q);
4651 case DOQUAL : /* fall-thru */
4652 case BOOLQUAL : snd(q) = depExpr(l,snd(q));
4659 static Void local depCaseAlt(line,a) /* Find dependents of case altern. */
4662 List obvs = saveBvars(); /* Save list of bound variables */
4664 fst(a) = bindPat(line,fst(a)); /* Add new bound vars for pats */
4665 depRhs(snd(a)); /* Find dependents of rhs */
4666 fst(a) = applyBtyvs(fst(a));
4667 restoreBvars(obvs); /* Restore original list of bvars */
4670 static Cell local depVar(line,e) /* Register occurrence of variable */
4673 List bounds1 = bounds;
4674 List bindings1 = bindings;
4675 List depends1 = depends;
4679 while (nonNull(bindings1)) {
4680 n = varIsMember(t,hd(bounds1)); /* look for t in bound variables */
4684 n = findBinding(t,hd(bindings1)); /* look for t in var bindings */
4686 if (!cellIsMember(n,hd(depends1))) {
4687 hd(depends1) = cons(n,hd(depends1));
4689 return (isVar(fst(n)) ? fst(n) : e);
4692 bounds1 = tl(bounds1);
4693 bindings1 = tl(bindings1);
4694 depends1 = tl(depends1);
4697 if (isNull(n=findName(t))) { /* check global definitions */
4698 ERRMSG(line) "Undefined variable \"%s\"", textToStr(t)
4702 if (!moduleThisScript(name(n).mod)) {
4705 /* Later phases of the system cannot cope if we resolve references
4706 * to unprocessed objects too early. This is the main reason that
4707 * we cannot cope with recursive modules at the moment.
4712 static Cell local depQVar(line,e)/* register occurrence of qualified variable */
4715 Name n = findQualName(e);
4716 if (isNull(n)) { /* check global definitions */
4717 ERRMSG(line) "Undefined qualified variable \"%s\"", identToStr(e)
4720 if (name(n).mod != currentModule) {
4723 if (fst(e) == VARIDCELL) {
4724 e = mkVar(qtextOf(e));
4726 e = mkVarop(qtextOf(e));
4728 return depVar(line,e);
4731 static Void local depConFlds(line,e,isP)/* check construction using fields */
4735 Name c = conDefined(line,fst(snd(e)));
4736 if (isNull(snd(snd(e))) ||
4737 nonNull(cellIsMember(c,depFields(line,e,snd(snd(e)),isP)))) {
4740 ERRMSG(line) "Constructor \"%s\" does not have selected fields in ",
4741 textToStr(name(c).text)
4746 if (!isP && isPair(name(c).defn)) { /* Check that banged fields defined*/
4747 List scs = fst(name(c).defn); /* List of strict components */
4748 Type t = name(c).type;
4749 Int a = userArity(c);
4750 List fs = snd(snd(e));
4752 if (isPolyType(t)) { /* Find tycon that c belongs to */
4755 if (isQualType(t)) {
4758 if (whatIs(t)==CDICTS) {
4767 for (ss=tycon(t).defn; hasCfun(ss); ss=tl(ss)) {
4769 /* Now we know the tycon t that c belongs to, and the corresponding
4770 * list of selectors for that type, ss. Now we have to check that
4771 * each of the fields identified by scs appears in fs, using ss to
4772 * cross reference, and convert integers to selector names.
4774 for (; nonNull(scs); scs=tl(scs)) {
4775 Int i = intOf(hd(scs));
4777 for (; nonNull(ss1); ss1=tl(ss1)) {
4778 List cns = name(hd(ss1)).defn;
4779 for (; nonNull(cns); cns=tl(cns)) {
4780 if (fst(hd(cns))==c) {
4784 if (nonNull(cns) && intOf(snd(hd(cns)))==i) {
4789 internal("depConFlds");
4793 for (; nonNull(fs1) && s!=fst(hd(fs1)); fs1=tl(fs1)) {
4796 ERRMSG(line) "Construction does not define strict field"
4798 ERRTEXT "\nExpression : " ETHEN ERREXPR(e);
4799 ERRTEXT "\nField : " ETHEN ERREXPR(s);
4808 static Void local depUpdFlds(line,e) /* check update using fields */
4811 if (isNull(thd3(snd(e)))) {
4812 ERRMSG(line) "Empty field list in update"
4815 fst3(snd(e)) = depExpr(line,fst3(snd(e)));
4816 snd3(snd(e)) = depFields(line,e,thd3(snd(e)),FALSE);
4819 static List local depFields(l,e,fs,isP) /* check field binding list */
4827 for (; nonNull(fs); fs=tl(fs)) { /* for each field binding */
4831 if (isVar(fb)) { /* expand var to var = var */
4832 h98DoesntSupport(l,"missing field bindings");
4833 fb = hd(fs) = pair(fb,fb);
4836 s = findQualName(fst(fb)); /* check for selector */
4837 if (nonNull(s) && isSfun(s)) {
4840 ERRMSG(l) "\"%s\" is not a selector function/field name",
4841 textToStr(textOf(fst(fb)))
4845 if (isNull(ss)) { /* for first named selector */
4846 List scs = name(s).defn; /* calculate list of constructors */
4847 for (; nonNull(scs); scs=tl(scs)) {
4848 cs = cons(fst(hd(scs)),cs);
4850 ss = singleton(s); /* initialize selector list */
4851 } else { /* for subsequent selectors */
4852 List ds = cs; /* intersect constructor lists */
4853 for (cs=NIL; nonNull(ds); ) {
4854 List scs = name(s).defn;
4855 while (nonNull(scs) && fst(hd(scs))!=hd(ds)) {
4868 if (cellIsMember(s,ss)) { /* check for repeated uses */
4869 ERRMSG(l) "Repeated field name \"%s\" in field list",
4870 textToStr(name(s).text)
4876 if (isNull(cs)) { /* Are there any matching constrs? */
4877 ERRMSG(l) "No constructor has all of the fields specified in "
4883 snd(fb) = (isP ? checkPat(l,snd(fb)) : depExpr(l,snd(fb)));
4889 static Void local depWith(line,e) /* check with using fields */
4892 fst(snd(e)) = depExpr(line,fst(snd(e)));
4893 snd(snd(e)) = depDwFlds(line,e,snd(snd(e)));
4896 static List local depDwFlds(l,e,fs)/* check field binding list */
4902 for (; nonNull(c); c=tl(c)) { /* for each field binding */
4903 snd(hd(c)) = depExpr(l,snd(hd(c)));
4910 static Cell local depRecord(line,e) /* find dependents of record and */
4911 Int line; /* sort fields into approp. order */
4912 Cell e; { /* to make construction and update */
4913 List exts = NIL; /* more efficient. */
4916 h98DoesntSupport(line,"extensible records");
4917 do { /* build up list of extensions */
4918 Text t = extText(fun(fun(r)));
4919 String s = textToStr(t);
4922 while (nonNull(nx) && strcmp(textToStr(extText(fun(fun(nx)))),s)>0) {
4926 if (nonNull(nx) && t==extText(fun(fun(nx)))) {
4927 ERRMSG(line) "Repeated label \"%s\" in record ", s
4933 exts = cons(fun(r),exts);
4935 tl(prev) = cons(fun(r),nx);
4937 extField(r) = depExpr(line,extField(r));
4939 } while (isAp(r) && isAp(fun(r)) && isExt(fun(fun(r))));
4940 r = depExpr(line,r);
4941 return revOnto(exts,r);
4946 /* --------------------------------------------------------------------------
4947 * Several parts of this program require an algorithm for sorting a list
4948 * of values (with some added dependency information) into a list of strongly
4949 * connected components in which each value appears before its dependents.
4951 * Each of these algorithms is obtained by parameterising a standard
4952 * algorithm in "scc.c" as shown below.
4953 * ------------------------------------------------------------------------*/
4955 #define SCC2 tcscc /* make scc algorithm for Tycons */
4956 #define LOWLINK tclowlink
4957 #define DEPENDS(c) (isTycon(c) ? tycon(c).kind : cclass(c).kinds)
4958 #define SETDEPENDS(c,v) if(isTycon(c)) tycon(c).kind=v; else cclass(c).kinds=v
4965 #define SCC bscc /* make scc algorithm for Bindings */
4966 #define LOWLINK blowlink
4967 #define DEPENDS(t) depVal(t)
4968 #define SETDEPENDS(c,v) depVal(c)=v
4975 /* --------------------------------------------------------------------------
4976 * Main static analysis:
4977 * ------------------------------------------------------------------------*/
4979 Void checkExp() { /* Top level static check on Expr */
4980 staticAnalysis(RESET);
4981 clearScope(); /* Analyse expression in the scope */
4982 withinScope(NIL); /* of no local bindings */
4983 inputExpr = depExpr(0,inputExpr);
4985 staticAnalysis(RESET);
4988 #if EXPLAIN_INSTANCE_RESOLUTION
4989 Void checkContext(void) { /* Top level static check on Expr */
4992 staticAnalysis(RESET);
4993 clearScope(); /* Analyse expression in the scope */
4994 withinScope(NIL); /* of no local bindings */
4996 for (vs = NIL; nonNull(qs); qs=tl(qs)) {
4997 vs = typeVarsIn(hd(qs),NIL,NIL,vs);
4999 map2Proc(depPredExp,0,vs,inputContext);
5001 staticAnalysis(RESET);
5005 Void checkDefns() { /* Top level static analysis */
5006 Module thisModule = lastModule();
5007 staticAnalysis(RESET);
5009 setCurrModule(thisModule);
5011 /* Resolve module references */
5012 mapProc(checkQualImport, module(thisModule).qualImports);
5013 mapProc(checkUnqualImport,unqualImports);
5014 /* Add "import Prelude" if there`s no explicit import */
5015 if (thisModule!=modulePrelude
5016 && isNull(cellAssoc(modulePrelude,unqualImports))
5017 && isNull(cellRevAssoc(modulePrelude,module(thisModule).qualImports))) {
5018 unqualImports = cons(pair(modulePrelude,DOTDOT),unqualImports);
5020 /* Every module (including the Prelude) implicitly contains
5021 * "import qualified Prelude"
5023 module(thisModule).qualImports=cons(pair(mkCon(textPrelude),modulePrelude),
5024 module(thisModule).qualImports);
5026 mapProc(checkImportList, unqualImports);
5028 linkPreludeTC(); /* Get prelude tycons and classes */
5029 mapProc(checkTyconDefn,tyconDefns); /* validate tycon definitions */
5030 checkSynonyms(tyconDefns); /* check synonym definitions */
5031 mapProc(checkClassDefn,classDefns); /* process class definitions */
5032 mapProc(kindTCGroup,tcscc(tyconDefns,classDefns)); /* attach kinds */
5033 mapProc(extendFundeps,classDefns); /* finish class definitions */
5034 mapProc(addMembers,classDefns); /* add definitions for member funs */
5035 mapProc(visitClass,classDefns); /* check class hierarchy */
5036 linkPreludeCM(); /* Get prelude cfuns and mfuns */
5038 instDefns = rev(instDefns); /* process instance definitions */
5039 mapProc(checkInstDefn,instDefns);
5041 setCurrModule(thisModule);
5042 mapProc(addRSsigdecls,typeInDefns); /* add sigdecls for RESTRICTSYN */
5043 valDefns = eqnsToBindings(valDefns,tyconDefns,classDefns,/*primDefns*/NIL);
5044 mapProc(allNoPrevDef,valDefns); /* check against previous defns */
5045 mapProc(addDerivImp,derivedInsts); /* Add impls for derived instances */
5046 deriveContexts(derivedInsts); /* Calculate derived inst contexts */
5047 instDefns = appendOnto(instDefns,derivedInsts);
5048 checkDefaultDefns(); /* validate default definitions */
5050 mapProc(allNoPrevDef,valDefns); /* check against previous defns */
5054 mapProc(checkForeignImport,foreignImports); /* check foreign imports */
5055 mapProc(checkForeignExport,foreignExports); /* check foreign exports */
5056 foreignImports = NIL;
5057 foreignExports = NIL;
5059 /* Every top-level name has now been created - so we can build the */
5060 /* export list. Note that this has to happen before dependency */
5061 /* analysis so that references to Prelude.foo will be resolved */
5062 /* when compiling the prelude. */
5063 module(thisModule).exports = checkExports(module(thisModule).exports);
5065 mapProc(checkTypeIn,typeInDefns); /* check restricted synonym defns */
5068 withinScope(valDefns);
5069 valDefns = topDependAnal(valDefns); /* top level dependency ordering */
5070 mapProc(depDefaults,classDefns); /* dep. analysis on class defaults */
5071 mapProc(depInsts,instDefns); /* dep. analysis on inst defns */
5074 /* ToDo: evalDefaults should match current evaluation module */
5075 evalDefaults = defaultDefns; /* Set defaults for evaluator */
5077 staticAnalysis(RESET);
5083 static Void local addRSsigdecls(pr) /* add sigdecls from TYPE ... IN ..*/
5085 List vs = snd(pr); /* get list of variables */
5086 for (; nonNull(vs); vs=tl(vs)) {
5087 if (fst(hd(vs))==SIGDECL) { /* find a sigdecl */
5088 valDefns = cons(hd(vs),valDefns); /* add to valDefns */
5089 hd(vs) = hd(snd3(snd(hd(vs)))); /* and replace with var */
5094 static Void local allNoPrevDef(b) /* ensure no previous bindings for*/
5095 Cell b; { /* variables in new binding */
5096 if (isVar(fst(b))) {
5097 noPrevDef(rhsLine(snd(hd(snd(snd(b))))),fst(b));
5099 Int line = rhsLine(snd(snd(snd(b))));
5100 map1Proc(noPrevDef,line,fst(b));
5104 static Void local noPrevDef(line,v) /* ensure no previous binding for */
5105 Int line; /* new variable */
5107 Name n = findName(textOf(v));
5110 n = newName(textOf(v),NIL);
5111 name(n).defn = PREDEFINED;
5112 } else if (name(n).defn!=PREDEFINED) {
5113 duplicateError(line,name(n).mod,name(n).text,"variable");
5115 name(n).line = line;
5118 static Void local duplicateErrorAux(line,mod,t,kind)/* report duplicate defn */
5123 if (mod == currentModule) {
5124 ERRMSG(line) "Repeated definition for %s \"%s\"", kind,
5128 ERRMSG(line) "Definition of %s \"%s\" clashes with import", kind,
5134 static Void local checkTypeIn(cvs) /* Check that vars in restricted */
5135 Pair cvs; { /* synonym are defined */
5139 for (; nonNull(vs); vs=tl(vs)) {
5140 if (isNull(findName(textOf(hd(vs))))) {
5141 ERRMSG(tycon(c).line)
5142 "No top level binding of \"%s\" for restricted synonym \"%s\"",
5143 textToStr(textOf(hd(vs))), textToStr(tycon(c).text)
5149 /* --------------------------------------------------------------------------
5150 * Haskell 98 compatibility tests:
5151 * ------------------------------------------------------------------------*/
5153 Bool h98Pred(allowArgs,pi) /* Check syntax of Hask98 predicate*/
5156 return isClass(getHead(pi)) && argCount==1 &&
5157 isOffset(getHead(arg(pi))) && (argCount==0 || allowArgs);
5160 Cell h98Context(allowArgs,ps) /* Check syntax of Hask98 context */
5163 for (; nonNull(ps); ps=tl(ps)) {
5164 if (!h98Pred(allowArgs,hd(ps))) {
5171 Void h98CheckCtxt(line,wh,allowArgs,ps,in)
5172 Int line; /* Report illegal context/predicate*/
5178 Cell pi = h98Context(allowArgs,ps);
5180 ERRMSG(line) "Illegal Haskell 98 class constraint in %s",wh ETHEN
5182 ERRTEXT "\n*** Instance : " ETHEN ERRPRED(inst(in).head);
5184 ERRTEXT "\n*** Constraint : " ETHEN ERRPRED(pi);
5185 if (nonNull(ps) && nonNull(tl(ps))) {
5186 ERRTEXT "\n*** Context : " ETHEN ERRCONTEXT(ps);
5194 Void h98CheckType(line,wh,e,t) /* Check for Haskell 98 type */
5203 if (isQualType(t)) {
5204 Cell pi = h98Context(TRUE,fst(snd(t)));
5206 ERRMSG(line) "Illegal Haskell 98 class constraint in %s",wh
5208 ERRTEXT "\n*** Expression : " ETHEN ERREXPR(e);
5209 ERRTEXT "\n*** Type : " ETHEN ERRTYPE(ty);
5217 Void h98DoesntSupport(line,wh) /* Report feature missing in H98 */
5221 ERRMSG(line) "Haskell 98 does not support %s", wh
5226 /* --------------------------------------------------------------------------
5227 * Static Analysis control:
5228 * ------------------------------------------------------------------------*/
5230 Void staticAnalysis(what)
5233 case RESET : cfunSfuns = NIL;
5246 case MARK : mark(daSccs);
5261 case INSTALL : staticAnalysis(RESET);
5263 extKind = pair(STAR,pair(ROW,ROW));
5269 /*-------------------------------------------------------------------------*/