diff options
Diffstat (limited to 'src/unify.cpp')
-rw-r--r-- | src/unify.cpp | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/src/unify.cpp b/src/unify.cpp index 0383df0..5d4121e 100644 --- a/src/unify.cpp +++ b/src/unify.cpp @@ -40,8 +40,9 @@ substitute(ATuple* tup, const AST* from, AST* to) substitute(tup->at(i)->to<ATuple*>(), from, to); } +/// Compose two substitutions (TAPL 22.1.1) Subst -Subst::compose(const Subst& delta, const Subst& gamma) // TAPL 22.1.1 +Subst::compose(const Subst& delta, const Subst& gamma) { Subst r; for (Subst::const_iterator g = gamma.begin(); g != gamma.end(); ++g) { @@ -55,10 +56,11 @@ Subst::compose(const Subst& delta, const Subst& gamma) // TAPL 22.1.1 return r; } +/// Replace all occurrences of @a s with @a t void -substConstraints(Constraints& constraints, AType* s, AType* t) +Constraints::replace(AType* s, AType* t) { - for (Constraints::iterator c = constraints.begin(); c != constraints.end();) { + for (Constraints::iterator c = begin(); c != end();) { Constraints::iterator next = c; ++next; if (*c->first == *s) c->first = t; if (*c->second == *s) c->second = t; @@ -68,8 +70,9 @@ substConstraints(Constraints& constraints, AType* s, AType* t) } } +/// Unify a type constraint set (TAPL 22.4) Subst -TEnv::unify(const Constraints& constraints) // TAPL 22.4 +unify(const Constraints& constraints) { if (constraints.empty()) return Subst(); AType* s = constraints.begin()->first; @@ -80,10 +83,10 @@ TEnv::unify(const Constraints& constraints) // TAPL 22.4 if (*s == *t) { return unify(cp); } else if (s->var() && !t->contains(s)) { - substConstraints(cp, s, t); + cp.replace(s, t); return Subst::compose(unify(cp), Subst(s, t)); } else if (t->var() && !s->contains(t)) { - substConstraints(cp, t, s); + cp.replace(t, s); return Subst::compose(unify(cp), Subst(t, s)); } else if (s->kind == AType::EXPR && s->kind == t->kind && s->size() == t->size()) { for (size_t i = 0; i < s->size(); ++i) { |