/* --------------------------------------------------------------------------
* Deriving
*
- * Copyright (c) The University of Nottingham and Yale University, 1994-1997.
- * All rights reserved. See NOTICE for details and conditions of use etc...
- * Hugs version 1.4, December 1997
+ * The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
+ * Yale Haskell Group, and the Oregon Graduate Institute of Science and
+ * Technology, 1994-1999, All rights reserved. It is distributed as
+ * free software under the license in the file "License", which is
+ * included in the distribution.
*
* $RCSfile: derive.c,v $
- * $Revision: 1.4 $
- * $Date: 1999/03/01 14:46:44 $
+ * $Revision: 1.14 $
+ * $Date: 2000/03/23 14:54:20 $
* ------------------------------------------------------------------------*/
-#include "prelude.h"
+#include "hugsbasictypes.h"
#include "storage.h"
-#include "backend.h"
#include "connect.h"
#include "errors.h"
#include "Assembler.h"
-#include "link.h"
-
-static Cell varTrue;
-static Cell varFalse;
-#if DERIVE_ORD
-static Cell varCompAux; /* auxiliary function for compares */
-static Cell varCompare;
-static Cell varEQ;
-#endif
-#if DERIVE_IX
-static Cell varRangeSize; /* calculate size of index range */
-static Cell varInRange;
-static Cell varRange;
-static Cell varIndex;
-static Cell varMult;
-static Cell qvarPlus;
-static Cell varMap;
-static Cell qvarMinus;
-static Cell varError;
-#endif
-#if DERIVE_ENUM
-static Cell varToEnum;
-static Cell varFromEnum;
-static Cell varEnumFromTo;
-static Cell varEnumFromThenTo;
-#endif
-#if DERIVE_BOUNDED
-static Cell varMinBound;
-static Cell varMaxBound;
-#endif
-#if DERIVE_SHOW
- Cell conCons;
-static Cell varShowField; /* display single field */
-static Cell varShowParen; /* wrap with parens */
-static Cell varCompose; /* function composition */
-static Cell varShowsPrec;
-static Cell varLe;
-#endif
-#if DERIVE_READ
-static Cell varReadField; /* read single field */
-static Cell varReadParen; /* unwrap from parens */
-static Cell varLex; /* lexer */
-static Cell varReadsPrec;
-static Cell varGt;
-#endif
-#if DERIVE_SHOW || DERIVE_READ
-static Cell varAppend; /* list append */
-List cfunSfuns; /* List of (Cfun,[SelectorVar]) */
-#endif
-#if DERIVE_EQ || DERIVE_IX
-static Cell varAnd; /* built-in logical connectives */
-#endif
-#if DERIVE_EQ || DERIVE_ORD
-static Cell varEq;
-#endif
+List cfunSfuns; /* List of (Cfun,[SelectorVar]) */
/* --------------------------------------------------------------------------
* local function prototypes:
* ------------------------------------------------------------------------*/
-static List local getDiVars Args((Int));
-static Cell local mkBind Args((String,List));
-static Cell local mkVarAlts Args((Int,Cell));
-
-#if DERIVE_EQ || DERIVE_ORD
-static List local makeDPats2 Args((Cell,Int));
-#endif
-#if DERIVE_ORD || DERIVE_ENUM || DERIVE_IX || DERIVE_BOUNDED
-static Bool local isEnumType Args((Tycon));
-#endif
-
-static Pair local mkAltEq Args((Int,List));
-static Pair local mkAltOrd Args((Int,List));
-static Cell local prodRange Args((Int,List,Cell,Cell,Cell));
-static Cell local prodIndex Args((Int,List,Cell,Cell,Cell));
-static Cell local prodInRange Args((Int,List,Cell,Cell,Cell));
-static List local mkIxBinds Args((Int,Cell,Int));
-static Cell local mkAltShow Args((Int,Cell,Int));
-static Cell local showsPrecRhs Args((Cell,Cell,Int));
-static Cell local mkReadCon Args((Name,Cell,Cell));
-static Cell local mkReadPrefix Args((Cell));
-static Cell local mkReadInfix Args((Cell));
-static Cell local mkReadTuple Args((Cell));
-static Cell local mkReadRecord Args((Cell,List));
-static List local mkBndBinds Args((Int,Cell,Int));
-
+static List local getDiVars ( Int );
+static Cell local mkBind ( String,List );
+static Cell local mkVarAlts ( Int,Cell );
+static List local makeDPats2 ( Cell,Int );
+static Bool local isEnumType ( Tycon );
+static Pair local mkAltEq ( Int,List );
+static Pair local mkAltOrd ( Int,List );
+static Cell local prodRange ( Int,List,Cell,Cell,Cell );
+static Cell local prodIndex ( Int,List,Cell,Cell,Cell );
+static Cell local prodInRange ( Int,List,Cell,Cell,Cell );
+static List local mkIxBinds ( Int,Cell,Int );
+static Cell local mkAltShow ( Int,Cell,Int );
+static Cell local showsPrecRhs ( Cell,Cell,Int );
+static Cell local mkReadCon ( Name,Cell,Cell );
+static Cell local mkReadPrefix ( Cell );
+static Cell local mkReadInfix ( Cell );
+static Cell local mkReadTuple ( Cell );
+static Cell local mkReadRecord ( Cell,List );
+static List local mkBndBinds ( Int,Cell,Int );
/* --------------------------------------------------------------------------
return singleton(pair(NIL,pair(mkInt(line),r)));
}
-#if DERIVE_EQ || DERIVE_ORD
static List local makeDPats2(h,n) /* generate pattern list */
Cell h; /* by putting two new patterns with*/
Int n; { /* head h and new var components */
}
return cons(p,vs);
}
-#endif
-#if DERIVE_ORD || DERIVE_ENUM || DERIVE_IX || DERIVE_BOUNDED
static Bool local isEnumType(t) /* Determine whether t is an enumeration */
Tycon t; { /* type (i.e. all constructors arity == 0) */
if (isTycon(t) && (tycon(t).what==DATATYPE || tycon(t).what==NEWTYPE)) {
}
return FALSE;
}
-#endif
+
/* --------------------------------------------------------------------------
* Given a datatype: data T a b = A a b | B Int | C deriving (Eq, Ord)
* constructors in the datatype definition.
* ------------------------------------------------------------------------*/
-#if DERIVE_EQ
-
-static Pair local mkAltEq Args((Int,List));
+static Pair local mkAltEq ( Int,List );
List deriveEq(t) /* generate binding for derived == */
Type t; { /* for some TUPLE or DATATYPE t */
List cs = tycon(t).defn;
for (; hasCfun(cs); cs=tl(cs)) {
alts = cons(mkAltEq(tycon(t).line,
- makeDPats2(hd(cs),name(hd(cs)).arity)),
+ makeDPats2(hd(cs),userArity(hd(cs)))),
alts);
}
if (cfunOf(hd(tycon(t).defn))!=0) {
alts = cons(pair(cons(WILDCARD,cons(WILDCARD,NIL)),
- pair(mkInt(tycon(t).line),varFalse)),alts);
+ pair(mkInt(tycon(t).line),nameFalse)),alts);
}
alts = rev(alts);
} else { /* special case for tuples */
List pats; { /* arguments */
Cell p = hd(pats);
Cell q = hd(tl(pats));
- Cell e = varTrue;
+ Cell e = nameTrue;
if (isAp(p)) {
- e = ap2(varEq,arg(p),arg(q));
+ e = ap2(nameEq,arg(p),arg(q));
for (p=fun(p), q=fun(q); isAp(p); p=fun(p), q=fun(q)) {
- e = ap2(varAnd,ap2(varEq,arg(p),arg(q)),e);
+ e = ap2(nameAnd,ap2(nameEq,arg(p),arg(q)),e);
}
}
return pair(pats,pair(mkInt(line),e));
}
-#endif /* DERIVE_EQ */
-#if DERIVE_ORD
-static Pair local mkAltOrd Args((Int,List));
+static Pair local mkAltOrd ( Int,List );
List deriveOrd(t) /* make binding for derived compare*/
Type t; { /* for some TUPLE or DATATYPE t */
Cell rhs = NIL;
if (cfunOf(hd(tycon(t).defn))!=0) {
implementConToTag(t);
- rhs = ap2(varCompare,
+ rhs = ap2(nameCompare,
ap(tycon(t).conToTag,u),
ap(tycon(t).conToTag,w));
} else {
- rhs = varEQ;
+ rhs = nameEQ;
}
alts = singleton(pair(doubleton(u,w),pair(mkInt(tycon(t).line),rhs)));
} else if (isTycon(t)) { /* deal with type constrs */
List cs = tycon(t).defn;
for (; hasCfun(cs); cs=tl(cs)) {
alts = cons(mkAltOrd(tycon(t).line,
- makeDPats2(hd(cs),name(hd(cs)).arity)),
+ makeDPats2(hd(cs),userArity(hd(cs)))),
alts);
}
if (cfunOf(hd(tycon(t).defn))!=0) {
implementConToTag(t);
alts = cons(pair(doubleton(u,w),
pair(mkInt(tycon(t).line),
- ap2(varCompare,
+ ap2(nameCompare,
ap(tycon(t).conToTag,u),
ap(tycon(t).conToTag,w)))),
alts);
List pats; { /* arguments */
Cell p = hd(pats);
Cell q = hd(tl(pats));
- Cell e = varEQ;
+ Cell e = nameEQ;
if (isAp(p)) {
- e = ap2(varCompare,arg(p),arg(q));
+ e = ap2(nameCompare,arg(p),arg(q));
for (p=fun(p), q=fun(q); isAp(p); p=fun(p), q=fun(q)) {
- e = ap3(varCompAux,arg(p),arg(q),e);
+ e = ap3(nameCompAux,arg(p),arg(q),e);
}
}
return pair(pats,pair(mkInt(line),e));
}
-#endif /* DERIVE_ORD */
/* --------------------------------------------------------------------------
* Deriving Ix and Enum:
* ------------------------------------------------------------------------*/
-#if DERIVE_ENUM
List deriveEnum(t) /* Construct definition of enumeration */
Tycon t; {
- Int l = tycon(t).line;
- Cell x = inventVar();
- Cell y = inventVar();
+ Int l = tycon(t).line;
+ Cell x = inventVar();
+ Cell y = inventVar();
Cell first = hd(tycon(t).defn);
- Cell last = tycon(t).defn;
+ Cell last = tycon(t).defn;
if (!isEnumType(t)) {
ERRMSG(l) "Can only derive instances of Enum for enumeration types"
implementTagToCon(t);
return cons(mkBind("toEnum", mkVarAlts(l,tycon(t).tagToCon)),
cons(mkBind("fromEnum", mkVarAlts(l,tycon(t).conToTag)),
- cons(mkBind("enumFrom", singleton(pair(singleton(x),
- pair(mkInt(l),
- ap2(varEnumFromTo,x,last))))),
- /* default instance of enumFromTo is good */
- cons(mkBind("enumFromThen",singleton(pair(doubleton(x,y),
- pair(mkInt(l),
- ap3(varEnumFromThenTo,x,y,
- ap(COND,triple(ap2(varLe,x,y),
- last,first))))))),
- /* default instance of enumFromThenTo is good */
- NIL))));
+ NIL));
}
-#endif /* DERIVE_ENUM */
-#if DERIVE_IX
-static List local mkIxBindsEnum Args((Tycon));
-static List local mkIxBinds Args((Int,Cell,Int));
-static Cell local prodRange Args((Int,List,Cell,Cell,Cell));
-static Cell local prodIndex Args((Int,List,Cell,Cell,Cell));
-static Cell local prodInRange Args((Int,List,Cell,Cell,Cell));
+
+static List local mkIxBindsEnum ( Tycon );
+static List local mkIxBinds ( Int,Cell,Int );
+static Cell local prodRange ( Int,List,Cell,Cell,Cell );
+static Cell local prodIndex ( Int,List,Cell,Cell,Cell );
+static Cell local prodInRange ( Int,List,Cell,Cell,Cell );
List deriveIx(t) /* Construct definition of indexing */
Tycon t; {
} else if (isTycon(t) && cfunOf(hd(tycon(t).defn))==0) {
return mkIxBinds(tycon(t).line,
hd(tycon(t).defn),
- name(hd(tycon(t).defn)).arity);
+ userArity(hd(tycon(t).defn)));
}
ERRMSG(tycon(t).line)
"Can only derive instances of Ix for enumeration or product types"
Cell c2 = inventVar();
Cell ci = inventVar();
return cons(mkBind("range", singleton(pair(singleton(ap2(mkTuple(2),
- c1,c2)), pair(mkInt(l),ap2(varMap,tagToCon,
- ap2(varEnumFromTo,ap(conToTag,c1),
+ c1,c2)), pair(mkInt(l),ap2(nameMap,tagToCon,
+ ap2(nameFromTo,ap(conToTag,c1),
ap(conToTag,c2))))))),
cons(mkBind("index", singleton(pair(doubleton(ap(ASPAT,pair(b,
ap2(mkTuple(2),c1,c2))),ci),
pair(mkInt(l),ap(COND,
- triple(ap2(varInRange,b,ci),
- ap2(qvarMinus,ap(conToTag,ci),
+ triple(ap2(nameInRange,b,ci),
+ ap2(nameMinus,ap(conToTag,ci),
ap(conToTag,c1)),
- ap(varError,mkStr(findText(
+ ap(nameError,mkStr(findText(
"Ix.index: Index out of range"))))))))),
cons(mkBind("inRange",singleton(pair(doubleton(ap2(mkTuple(2),
- c1,c2),ci), pair(mkInt(l),ap2(varAnd,
- ap2(varLe,ap(conToTag,c1),ap(conToTag,ci)),
- ap2(varLe,ap(conToTag,ci),
+ c1,c2),ci), pair(mkInt(l),ap2(nameAnd,
+ ap2(nameLe,ap(conToTag,c1),ap(conToTag,ci)),
+ ap2(nameLe,ap(conToTag,ci),
ap(conToTag,c2))))))),
/* ToDo: share conToTag ci */
NIL)));
Cell ls = h;
Cell us = h;
Cell is = h;
+ Cell js = h;
Cell pr = NIL;
Cell pats = NIL;
+
Int i;
for (i=0; i<n; ++i, vs=tl(vs)) { /* build three patterns for values */
ls = ap(ls,hd(vs)); /* of the datatype concerned */
us = ap(us,hd(vs=tl(vs)));
is = ap(is,hd(vs=tl(vs)));
+ js = ap(js,hd(vs)); /* ... and one expression */
}
pr = ap2(mkTuple(2),ls,us); /* Build (ls,us) */
pats = cons(pr,cons(is,NIL)); /* Build [(ls,us),is] */
- return cons(prodRange(line,singleton(pr),ls,us,is),
+ return cons(prodRange(line,singleton(pr),ls,us,js),
cons(prodIndex(line,pats,ls,us,is),
cons(prodInRange(line,pats,ls,us,is),
NIL)));
List e = NIL;
for (; isAp(ls); ls=fun(ls), us=fun(us), is=fun(is)) {
e = cons(ap(FROMQUAL,pair(arg(is),
- ap(varRange,ap2(mkTuple(2),
+ ap(nameRange,ap2(mkTuple(2),
arg(ls),
arg(us))))),e);
}
List xs = NIL;
Cell e = NIL;
for (; isAp(ls); ls=fun(ls), us=fun(us), is=fun(is)) {
- xs = cons(ap2(varIndex,ap2(mkTuple(2),arg(ls),arg(us)),arg(is)),xs);
+ xs = cons(ap2(nameIndex,ap2(mkTuple(2),arg(ls),arg(us)),arg(is)),xs);
}
for (e=hd(xs); nonNull(xs=tl(xs));) {
Cell x = hd(xs);
- e = ap2(qvarPlus,x,ap2(varMult,ap(varRangeSize,arg(fun(x))),e));
+ e = ap2(namePlus,x,ap2(nameMult,ap(nameRangeSize,arg(fun(x))),e));
}
e = singleton(pair(pats,pair(mkInt(line),e)));
return mkBind("index",e);
* inRange (X a b c, X p q r) (X x y z)
* = inRange (a,p) x && inRange (b,q) y && inRange (c,r) z
*/
- Cell e = ap2(varInRange,ap2(mkTuple(2),arg(ls),arg(us)),arg(is));
+ Cell e = ap2(nameInRange,ap2(mkTuple(2),arg(ls),arg(us)),arg(is));
while (ls=fun(ls), us=fun(us), is=fun(is), isAp(ls)) {
- e = ap2(varAnd,
- ap2(varInRange,ap2(mkTuple(2),arg(ls),arg(us)),arg(is)),
+ e = ap2(nameAnd,
+ ap2(nameInRange,ap2(mkTuple(2),arg(ls),arg(us)),arg(is)),
e);
}
e = singleton(pair(pats,pair(mkInt(line),e)));
return mkBind("inRange",e);
}
-#endif /* DERIVE_IX */
/* --------------------------------------------------------------------------
if (defaultSyntax(name(h).text)==APPLIC) {
rhs = ap(showsBQ,
ap2(nameComp,
- ap(nameApp,mkStr(name(h).text)),
+ ap(nameApp,mkStr(fixLitText(name(h).text))),
ap(showsBQ,rhs)));
} else {
- rhs = ap2(nameComp,ap(nameApp,mkStr(name(h).text)),rhs);
+ rhs = ap2(nameComp,
+ ap(nameApp,mkStr(fixLitText(name(h).text))),rhs);
}
rhs = ap2(nameComp,
* Deriving Bounded:
* ------------------------------------------------------------------------*/
-#if DERIVE_BOUNDED
-
List deriveBounded(t) /* construct definition of bounds */
Tycon t; {
if (isEnumType(t)) {
cons(mkBind("maxBound",mkVarAlts(line,maxB)),
NIL));
}
-#endif /* DERIVE_BOUNDED */
-
/* --------------------------------------------------------------------------
/* \ v -> case v of { ...; i -> Ci; ... } */
Void implementTagToCon(t)
-Tycon t; {
+Tycon t; {
if (isNull(tycon(t).tagToCon)) {
- String etxt;
String tyconname;
List cs;
Name nm;
StgVar bind2;
StgVar bind3;
List alts;
+ char etxt[200];
assert(nameMkA);
assert(nameUnpackString);
|| tycon(t).what==NEWTYPE));
tyconname = textToStr(tycon(t).text);
- etxt = malloc(100+strlen(tyconname));
- assert(etxt);
+ if (strlen(tyconname) > 100)
+ internal("implementTagToCon: tycon name too long");
+
sprintf(etxt,
"out-of-range arg for `toEnum' "
"in derived `instance Enum %s'",
tycon(t).tagToCon = nm;
/* hack to make it print out */
stgGlobals = cons(pair(nm,name(nm).stgVar),stgGlobals);
- if (etxt) free(etxt);
}
}
Void deriveControl(what)
Int what; {
- Text textPrelude = findText("Prelude");
switch (what) {
- case INSTALL :
- varTrue = mkQVar(textPrelude,findText("True"));
- varFalse = mkQVar(textPrelude,findText("False"));
-#if DERIVE_ORD
- varCompAux = mkQVar(textPrelude,findText("primCompAux"));
- varCompare = mkQVar(textPrelude,findText("compare"));
- varEQ = mkQVar(textPrelude,findText("EQ"));
-#endif
-#if DERIVE_IX
- varRangeSize = mkQVar(textPrelude,findText("rangeSize"));
- varInRange = mkQVar(textPrelude,findText("inRange"));
- varRange = mkQVar(textPrelude,findText("range"));
- varIndex = mkQVar(textPrelude,findText("index"));
- varMult = mkQVar(textPrelude,findText("*"));
- qvarPlus = mkQVar(textPrelude,findText("+"));
- varMap = mkQVar(textPrelude,findText("map"));
- qvarMinus = mkQVar(textPrelude,findText("-"));
- varError = mkQVar(textPrelude,findText("error"));
-#endif
-#if DERIVE_ENUM
- varToEnum = mkQVar(textPrelude,findText("toEnum"));
- varFromEnum = mkQVar(textPrelude,findText("fromEnum"));
- varEnumFromTo = mkQVar(textPrelude,findText("enumFromTo"));
- varEnumFromThenTo = mkQVar(textPrelude,findText("enumFromThenTo"));
-#endif
-#if DERIVE_BOUNDED
- varMinBound = mkQVar(textPrelude,findText("minBound"));
- varMaxBound = mkQVar(textPrelude,findText("maxBound"));
-#endif
-#if DERIVE_SHOW
- conCons = mkQCon(textPrelude,findText(":"));
- varShowField = mkQVar(textPrelude,findText("primShowField"));
- varShowParen = mkQVar(textPrelude,findText("showParen"));
- varCompose = mkQVar(textPrelude,findText("."));
- varShowsPrec = mkQVar(textPrelude,findText("showsPrec"));
- varLe = mkQVar(textPrelude,findText("<="));
-#endif
-#if DERIVE_READ
- varReadField = mkQVar(textPrelude,findText("primReadField"));
- varReadParen = mkQVar(textPrelude,findText("readParen"));
- varLex = mkQVar(textPrelude,findText("lex"));
- varReadsPrec = mkQVar(textPrelude,findText("readsPrec"));
- varGt = mkQVar(textPrelude,findText(">"));
-#endif
-#if DERIVE_SHOW || DERIVE_READ
- varAppend = mkQVar(textPrelude,findText("++"));
-#endif
-#if DERIVE_EQ || DERIVE_IX
- varAnd = mkQVar(textPrelude,findText("&&"));
-#endif
-#if DERIVE_EQ || DERIVE_ORD
- varEq = mkQVar(textPrelude,findText("=="));
-#endif
- /* deliberate fall through */
+ case PREPREL :
case RESET :
diVars = NIL;
diNum = 0;
-#if DERIVE_SHOW | DERIVE_READ
cfunSfuns = NIL;
-#endif
break;
case MARK :
mark(diVars);
-#if DERIVE_SHOW | DERIVE_READ
mark(cfunSfuns);
-#endif
- mark(varTrue);
- mark(varFalse);
-#if DERIVE_ORD
- mark(varCompAux);
- mark(varCompare);
- mark(varEQ);
-#endif
-#if DERIVE_IX
- mark(varRangeSize);
- mark(varInRange);
- mark(varRange);
- mark(varIndex);
- mark(varMult);
- mark(qvarPlus);
- mark(varMap);
- mark(qvarMinus);
- mark(varError);
-#endif
-#if DERIVE_ENUM
- mark(varToEnum);
- mark(varFromEnum);
- mark(varEnumFromTo);
- mark(varEnumFromThenTo);
-#endif
-#if DERIVE_BOUNDED
- mark(varMinBound);
- mark(varMaxBound);
-#endif
-#if DERIVE_SHOW
- mark(conCons);
- mark(varShowField);
- mark(varShowParen);
- mark(varCompose);
- mark(varShowsPrec);
- mark(varLe);
-#endif
-#if DERIVE_READ
- mark(varReadField);
- mark(varReadParen);
- mark(varLex);
- mark(varReadsPrec);
- mark(varGt);
-#endif
-#if DERIVE_SHOW || DERIVE_READ
- mark(varAppend);
-#endif
-#if DERIVE_EQ || DERIVE_IX
- mark(varAnd);
-#endif
-#if DERIVE_EQ || DERIVE_ORD
- mark(varEq);
-#endif
break;
+
+ case POSTPREL: break;
}
}