Typeclass.hxx

Go to the documentation of this file.
00001 #ifndef TYPECLASS_HXX
00002 #define TYPECLASS_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 #include <set>
00047 
00048 #include "AST.hxx"
00049 #include "Type.hxx"
00050 
00051 struct TypeScheme;
00052 struct TCConstraints;
00053 
00054 typedef Type Typeclass;
00055 typedef Typeclass Constraint; 
00056 typedef TCConstraints Constraints; 
00057 
00058 typedef TypeSet TypeclassSet;
00059 typedef TypeSet ConstraintSet;
00060 
00061 struct Instance {
00062   boost::shared_ptr<TypeScheme> ts;
00063   boost::shared_ptr<AST> ast;
00064   
00065   Instance(boost::shared_ptr<TypeScheme> _ts, boost::shared_ptr<AST>_ins)
00066   {
00067     ts = _ts;
00068     ast = _ins;
00069   }
00070   
00071   static inline boost::shared_ptr<Instance>
00072   make(boost::shared_ptr<TypeScheme> _ts, boost::shared_ptr<AST>_ins) {
00073     Instance *tmp = new Instance(_ts, _ins);
00074     return boost::shared_ptr<Instance>(tmp);
00075   }
00076 
00077   bool equals(boost::shared_ptr<Instance> ins, 
00078               boost::shared_ptr<const InstEnvironment > instEnv) const;
00079   bool overlaps(boost::shared_ptr<Instance> ins) const;
00080   bool satisfies(boost::shared_ptr<Typeclass> pred, 
00081                  boost::shared_ptr<const InstEnvironment > instEnv) const;
00082   std::string asString() const;
00083 
00084   std::string asXML();
00085   void asXML(sherpa::INOstream &out);
00086 
00087   int operator<(const struct Instance& rhs) const {
00088     return (asString() < rhs.asString());
00089   }
00090 };
00091 
00092 // Impose a canonical sort order on pointers to Instance, so that
00093 // operations involving sets of Instance pointers will procede in a
00094 // deterministic order
00095 static int operator<(const boost::shared_ptr<Instance>& lhs, 
00096                      const boost::shared_ptr<Instance>& rhs)
00097 {
00098   return (*lhs) < (*rhs);
00099 }
00100 
00101 
00102 
00103 /* Type class constraints */
00104  
00105 struct TCConstraints {
00106   // Type class predicates (which are, in turn, instances of Type
00107   TypeSet pred;
00108   typedef TypeSet::iterator iterator;
00109   
00110   TCConstraints()
00111   {
00112   }
00113 
00114   bool empty() const {
00115     return pred.empty();
00116   }
00117 
00118   size_t size() const {
00119     return pred.size();
00120   }
00121 
00122   void addPred(boost::shared_ptr<Typeclass> tc);
00123   void clearPred(boost::shared_ptr<Constraint> ct);
00124 
00125   void normalize();
00126   
00127   bool contains(boost::shared_ptr<Typeclass> tc);
00128 
00129   void collectAllFnDeps(TypeSet& fnDeps);
00130   
00131   // Compute the closure of all functional dependencies 
00132   // supplied in the vector
00133   static void close(TypeSet& closure,
00134                     const TypeSet& fnDeps);
00135   void clearHintsOnPreds(boost::shared_ptr<Trail> trail);  
00136 
00137   iterator begin() {
00138     return pred.begin();
00139   }
00140   iterator end() {
00141     return pred.end();
00142   }
00143 
00144   static inline boost::shared_ptr<TCConstraints>
00145   make() {
00146     TCConstraints *tmp = new TCConstraints();
00147     return boost::shared_ptr<TCConstraints>(tmp);
00148   }
00149 };
00150 
00151 
00152 #endif /* TYPECLASS_HXX */
00153 

Generated on Thu May 17 23:59:16 2012 for BitC Compiler by  doxygen 1.4.7