00001 #ifndef TYPESCHEME_HXX 00002 #define TYPESCHEME_HXX 00003 00004 /************************************************************************** 00005 * 00006 * Copyright (C) 2008, Johns Hopkins University. 00007 * All rights reserved. 00008 * 00009 * Redistribution and use in source and binary forms, with or 00010 * without modification, are permitted provided that the following 00011 * conditions are met: 00012 * 00013 * - Redistributions of source code must contain the above 00014 * copyright notice, this list of conditions, and the following 00015 * disclaimer. 00016 * 00017 * - Redistributions in binary form must reproduce the above 00018 * copyright notice, this list of conditions, and the following 00019 * disclaimer in the documentation and/or other materials 00020 * provided with the distribution. 00021 * 00022 * - Neither the names of the copyright holders nor the names of any 00023 * of any contributors may be used to endorse or promote products 00024 * derived from this software without specific prior written 00025 * permission. 00026 * 00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00028 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00029 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00030 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00031 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00032 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00033 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00034 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00035 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00036 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00037 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00038 * 00039 **************************************************************************/ 00040 00041 #include <stdlib.h> 00042 #include <dirent.h> 00043 #include <fstream> 00044 #include <iostream> 00045 #include <string> 00046 00047 #include "AST.hxx" 00048 #include "Type.hxx" 00049 #include "Typeclass.hxx" 00050 00051 enum GeneralizeMode {gen_instance=0, gen_top=1, gen_local=2, 00052 gen_Hinstance=3, gen_Htop=4, gen_Hlocal=5}; 00053 00054 // A type scheme identifies which type variables in a type are to be 00055 // generalized, and what constraints apply to the generalization of 00056 // those type variables. 00057 struct TypeScheme { 00058 00059 boost::shared_ptr<Type> tau; 00060 00061 // Need to maintained the official version here for debugging. 00062 // Type pointers get linked, which leaves them in a state where the 00063 // originating AST becomes ambiguous. Type schemes do not get 00064 // linked, so the originating AST for them remains unambiguous. 00065 boost::shared_ptr<AST> ast; 00066 00067 TypeSet ftvs; 00068 00069 // Type class constraints 00070 // Note: Leave this as a pointer, not an embedded vector. 00071 // This helps tcc sharing for definitions that are 00072 // defined within the same letrec, etc. 00073 boost::shared_ptr<TCConstraints> tcc; 00074 00075 // The constructor 00076 TypeScheme(boost::shared_ptr<Type> _tau, boost::shared_ptr<AST> _ast, 00077 boost::shared_ptr<TCConstraints> _tcc = boost::GC_NULL); 00078 00079 static inline boost::shared_ptr<TypeScheme> 00080 make(boost::shared_ptr<Type> _tau, 00081 boost::shared_ptr<AST> _ast, 00082 boost::shared_ptr<TCConstraints> _tcc = 00083 boost::GC_NULL) { 00084 TypeScheme *tmp = new TypeScheme(_tau, _ast, _tcc); 00085 return boost::shared_ptr<TypeScheme>(tmp); 00086 } 00087 00088 // The generalizer 00089 bool generalize(std::ostream& errStream, 00090 const sherpa::LexLoc &errLoc, 00091 boost::shared_ptr<const TSEnvironment > gamma, 00092 boost::shared_ptr<const InstEnvironment >instEnv, 00093 boost::shared_ptr<const AST> expr, 00094 boost::shared_ptr<TCConstraints >parentTCC, 00095 boost::shared_ptr<Trail> trail, 00096 GeneralizeMode gen); 00097 00098 // The function that actually makes a copy of the 00099 // Produce a type, that is an instace of the current type scheme. 00100 boost::shared_ptr<Type> type_instance(); 00101 00102 // Type scheme's instance (including TC constraints) 00103 boost::shared_ptr<TypeScheme> ts_instance(); 00104 00105 // Read carefully: 00106 // Appends constraints that corrrespond to at least one 00107 // free variable in this scheme's ftvs to _tcc 00108 void addConstraints(boost::shared_ptr<TCConstraints> _tcc) const; 00109 // or ones that correspond to any of the added predicates 00110 void TransAddConstraints(boost::shared_ptr<TCConstraints> _tcc) const; 00111 00112 //boost::shared_ptr<Type> type_copy(); 00113 std::string asString(boost::shared_ptr<TvPrinter> tvP=TvPrinter::make(), 00114 bool norm=false); 00115 00116 void asXML(boost::shared_ptr<TvPrinter> tvP, sherpa::INOstream &out); 00117 std::string asXML(boost::shared_ptr<TvPrinter> tvP = TvPrinter::make()); 00118 00119 // Collect all tvs wrt tau, and tcc->pred, but NOT tcc->fnDeps 00120 void collectAllFtvs(); 00121 void collectftvs(boost::shared_ptr<const TSEnvironment > gamma); 00122 bool removeUnInstFtvs(); 00123 bool normalizeConstruction(boost::shared_ptr<Trail> trail); 00124 00125 bool solvePredicates(std::ostream &errStream, 00126 const sherpa::LexLoc &errLoc, 00127 boost::shared_ptr<const InstEnvironment > instEnv, 00128 boost::shared_ptr<Trail> trail); 00129 00130 bool checkAmbiguity(std::ostream &errStream, const sherpa::LexLoc &errLoc); 00131 bool migratePredicates(boost::shared_ptr<TCConstraints> parentTCC); 00132 00133 // In the presence of PCSTs, the substitution within type schemes is 00134 // not capture avoiding. Therefore, there might be some 00135 // substitutions into the free type-variables. If so, they can be 00136 // removed from the list of type variables. 00137 // Returns true if any changes to the type scheme are made. 00138 // This also removes redundant constraints, or PCSTs that are 00139 // satisfied and no longer need to be present. 00140 bool normalize(); 00141 00142 #if 0 00143 /* FIX: THIS MUST NEVER BE USED IN lhs OF ASSIGNMENT! */ 00144 /* PUBLIC Accessors (Convenience Forms) */ 00145 boost::shared_ptr<Type> & Ftv(size_t i) 00146 { 00147 return (*ftvs)[i]; 00148 } 00149 boost::shared_ptr<Type> Ftv(size_t i) const 00150 { 00151 boost::shared_ptr<Type> t = (*ftvs)[i]; 00152 return t; 00153 } 00154 #endif 00155 }; 00156 00157 #endif /* TYPESCHEME_HXX */ 00158 00159
1.4.7