diff options
Diffstat (limited to 'src/unify.cpp')
-rw-r--r-- | src/unify.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/unify.cpp b/src/unify.cpp index 74e35fd..eebd75c 100644 --- a/src/unify.cpp +++ b/src/unify.cpp @@ -22,6 +22,40 @@ #include <set> #include "tuplr.hpp" +/** Build a type substitution for calling a generic function type + * with a specific set of argument types + */ +Subst +TEnv::buildSubst(AType* genericT, const AType& argsT) +{ + Subst subst; + + // Build substitution to apply to generic type + const ATuple* genericProtT = genericT->at(1)->as<const ATuple*>(); + assert(argsT.size() == genericProtT->size()); + ATuple::const_iterator g = genericProtT->begin(); + AType::const_iterator a = argsT.begin(); + for (; a != argsT.end(); ++a, ++g) { + const AType* genericArgT = (*g)->to<const AType*>(); + AType* callArgT = (*a)->to<AType*>(); + if (callArgT->kind == AType::EXPR) { + assert(genericArgT->kind == AType::EXPR); + assert(callArgT->size() == genericArgT->size()); + for (size_t i = 0; i < callArgT->size(); ++i) { + const AType* gT = genericArgT->at(i)->to<const AType*>(); + AType* aT = callArgT->at(i)->to<AType*>(); + if (gT && aT) + subst.add(gT, aT); + } + } else { + subst.add(genericArgT, callArgT); + } + } + + return subst; +} + + void Constraints::constrain(TEnv& tenv, const AST* o, AType* t) { |