diff --git a/opencog/unify/Unify.cc b/opencog/unify/Unify.cc index 7cf2758635..b2841071b7 100644 --- a/opencog/unify/Unify.cc +++ b/opencog/unify/Unify.cc @@ -686,8 +686,8 @@ Unify::SolutionSet Unify::ordered_unify(const HandleSeq& lhs, if (lhs.empty() and rhs.empty()) return SolutionSet(true); -#define is_lh_glob lhs[0]->get_type() == GLOB_NODE -#define is_rh_glob rhs[0]->get_type() == GLOB_NODE +#define is_lh_glob lhs[0]->get_type() == GLOB_NODE and is_declared_variable(lhs[0]) +#define is_rh_glob rhs[0]->get_type() == GLOB_NODE and is_declared_variable(rhs[0]) if (!lhs.empty() and !rhs.empty() and !(is_lh_glob) and !(is_rh_glob)){ const auto head_sol = unify(lhs[0], rhs[0], lc, rc); @@ -720,8 +720,20 @@ void Unify::ordered_unify_glob(const HandleSeq &lhs, const auto inter = _variables.get_interval(lhs[0]); for (size_t i = inter.first; (i <= inter.second and i <= rhs.size()); i++) { - const Handle r_h = - createLink(HandleSeq(rhs.begin(), rhs.begin() + i), LIST_LINK); + // The condition is to avoid extra complexity when calculating + // type-intersection for glob. Should be fixed from the atomspace + // Variables::is_type. + Handle r_h; + if (i == 1) { + Type rtype = (*rhs.begin())->get_type(); + if (GLOB_NODE == rtype) + r_h = *rhs.begin(); + else if (QUOTE_LINK == rtype or UNQUOTE_LINK == rtype) + r_h = createLink((*rhs.begin())->getOutgoingSet(), LIST_LINK); + else r_h = createLink(HandleSeq(rhs.begin(), rhs.begin() + i), LIST_LINK); + } + else r_h = createLink(HandleSeq(rhs.begin(), rhs.begin() + i), LIST_LINK); + auto head_sol = flip ? unify(r_h, lhs[0], rc, lc) : unify(lhs[0], r_h, lc, rc); diff --git a/tests/unify/UnifyGlobUTest.cxxtest b/tests/unify/UnifyGlobUTest.cxxtest index 19a10a567f..765a66754c 100644 --- a/tests/unify/UnifyGlobUTest.cxxtest +++ b/tests/unify/UnifyGlobUTest.cxxtest @@ -500,8 +500,6 @@ void UnifyGlobUTest::test_unify_typed_3() Unify::SolutionSet( {{{{U, al(LIST_LINK, {X, Y})}, al(LIST_LINK, {X, Y})}, {{Z, al(LIST_LINK, HandleSeq{})}, al(LIST_LINK, HandleSeq{})}}, - {{{U, X}, U}, - {{Y, Z, al(LIST_LINK, HandleSeq{})}, al(LIST_LINK, HandleSeq{})}}, {{{U, X}, X}, {{Y, Z, al(LIST_LINK, HandleSeq{})}, al(LIST_LINK, HandleSeq{})}}}); std::cout << "\n expected\n" << oc_to_string(expected) << std::endl; @@ -557,16 +555,10 @@ void UnifyGlobUTest::test_unify_typed_4() {{Z, al(LIST_LINK, HandleSeq{})}, al(LIST_LINK, HandleSeq{})}}, {{{U, X}, U}, {{Y, Z, al(LIST_LINK, HandleSeq{})}, al(LIST_LINK, HandleSeq{})}}, - {{{U, X}, X}, - {{Y, Z, al(LIST_LINK, HandleSeq{})}, al(LIST_LINK, HandleSeq{})}}, {{{U, Z}, U}, {{Y, X, al(LIST_LINK, HandleSeq{})}, al(LIST_LINK, HandleSeq{})}}, - {{{U, Z}, Z}, - {{Y, X, al(LIST_LINK, HandleSeq{})}, al(LIST_LINK, HandleSeq{})}}, {{{U, Y}, U}, {{Z, X, al(LIST_LINK, HandleSeq{})}, al(LIST_LINK, HandleSeq{})}}, - {{{U, Y}, Y}, - {{Z, X, al(LIST_LINK, HandleSeq{})}, al(LIST_LINK, HandleSeq{})}}, {{{U, al(LIST_LINK, {X, Y, Z})}, al(LIST_LINK, {X, Y, Z})}}, {{{U, X, Y, Z, al(LIST_LINK, HandleSeq{})}, al(LIST_LINK, HandleSeq{})}}}); std::cout << "\n expected\n" << oc_to_string(expected) << std::endl;