00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <assert.h>
00039 #include <stdint.h>
00040 #include <stdlib.h>
00041 #include <dirent.h>
00042 #include <fstream>
00043 #include <iostream>
00044 #include <string>
00045 #include <sstream>
00046
00047 #include <libsherpa/UExcept.hxx>
00048
00049 #include "Options.hxx"
00050 #include "UocInfo.hxx"
00051 #include "AST.hxx"
00052 #include "Type.hxx"
00053 #include "TypeInfer.hxx"
00054 #include "TypeScheme.hxx"
00055 #include "TypeMut.hxx"
00056 #include "Typeclass.hxx"
00057 #include "inter-pass.hxx"
00058 #include "Unify.hxx"
00059
00060 using namespace boost;
00061 using namespace sherpa;
00062 using namespace std;
00063
00064 TypeScheme::TypeScheme(shared_ptr<Type> _tau, shared_ptr<AST> _ast, shared_ptr<TCConstraints> _tcc)
00065 {
00066 tau = _tau;
00067 ast = _ast;
00068 tcc = _tcc;
00069 }
00070
00071 shared_ptr<Type>
00072 TypeScheme::type_instance()
00073 {
00074 normalize();
00075
00076
00077
00078 vector<shared_ptr<Type> > cnftvs;
00079 vector<shared_ptr<Type> > cftvs;
00080
00081 for (TypeSet::iterator itr_i = ftvs.begin();
00082 itr_i != ftvs.end(); ++itr_i) {
00083 cftvs.push_back(*itr_i);
00084 cnftvs.push_back(newTvar());
00085 }
00086
00087 shared_ptr<Type> t = tau->TypeSpecialize(cftvs, cnftvs);
00088
00089
00090 return t;
00091 }
00092
00093 shared_ptr<TypeScheme>
00094 TypeScheme::ts_instance()
00095 {
00096 normalize();
00097
00098 shared_ptr<TypeScheme> ts = TypeScheme::make(tau, ast);
00099 ts->tau = GC_NULL;
00100
00101 vector<shared_ptr<Type> > cnftvs;
00102 vector<shared_ptr<Type> > cftvs;
00103
00104 for (TypeSet::iterator itr_i = ftvs.begin();
00105 itr_i != ftvs.end(); ++itr_i) {
00106 shared_ptr<Type> tv = newTvar();
00107 ts->ftvs.insert(tv);
00108 cftvs.push_back(*itr_i);
00109 cnftvs.push_back(tv);
00110 }
00111
00112 ts->tau = tau->TypeSpecializeReal(cftvs, cnftvs);
00113
00114 if (tcc) {
00115 shared_ptr<TCConstraints> _tcc = TCConstraints::make();
00116 addConstraints(_tcc);
00117
00118 ts->tcc = TCConstraints::make();
00119 for (TypeSet::iterator itr = _tcc->begin();
00120 itr != _tcc->end(); ++itr) {
00121 shared_ptr<Typeclass> pred;
00122
00123 pred = (*itr)->TypeSpecializeReal(cftvs, cnftvs);
00124 ts->tcc->addPred(pred);
00125 }
00126 }
00127
00128 tau->clear_sp();
00129
00130 if (tcc)
00131 for (TypeSet::iterator itr = tcc->begin();
00132 itr != tcc->end(); ++itr)
00133 (*itr)->clear_sp();
00134
00135 return ts;
00136 }
00137
00138 void
00139 TypeScheme::addConstraints(shared_ptr<TCConstraints> _tcc) const
00140 {
00141 if (!tcc)
00142 return;
00143
00144 TypeSet allFtvs;
00145 tau->collectAllftvs(allFtvs);
00146
00147 for (TypeSet::iterator itr = tcc->begin();
00148 itr != tcc->end(); ++itr) {
00149 for (TypeSet::iterator itr_j = allFtvs.begin();
00150 itr_j != allFtvs.end(); ++itr_j)
00151 if ((*itr)->boundInType(*itr_j)) {
00152 _tcc->addPred(*itr);
00153 break;
00154 }
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 }
00169
00170 bool
00171 TypeScheme::normalize()
00172 {
00173 bool changed = false;
00174
00175 DEBUG(TS_NORM)
00176 std::cerr << "Considering: "
00177 << asString(Options::debugTvP, false)
00178 << std::endl;
00179
00180
00181 TypeSet newTvs;
00182 for (TypeSet::iterator itr_c = ftvs.begin();
00183 itr_c != ftvs.end(); ++itr_c) {
00184 shared_ptr<Type> ftv = (*itr_c)->getType();
00185
00186 if (ftv->typeTag == ty_tvar)
00187 newTvs.insert(ftv);
00188 else
00189 changed = true;
00190 }
00191 ftvs = newTvs;
00192
00193 if (tcc) {
00194 ConstraintSet allPreds = tcc->pred;
00195 tcc->pred.clear();
00196
00197 for (TypeSet::iterator itr = allPreds.begin();
00198 itr != allPreds.end(); ++itr) {
00199 shared_ptr<Constraint> ct = (*itr)->getType();
00200 if (!ct->isPcst()) {
00201 tcc->addPred(ct);
00202 continue;
00203 }
00204
00205 shared_ptr<Type> k = ct->CompType(0)->getType();
00206 shared_ptr<Type> tg = ct->CompType(1)->getType();
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 if ((k == Type::Kmono) ||
00220 ((k == Type::Kpoly) && tg->isConcretizable())) {
00221 changed = true;
00222 }
00223 else {
00224 tcc->addPred(ct);
00225 }
00226 }
00227 }
00228
00229 DEBUG(TS_NORM)
00230 if (changed)
00231 std::cerr << "\t\tNormalized to "
00232 << asString(Options::debugTvP, false)
00233 << std::endl;
00234
00235 return changed;
00236 }