int main() { put_structure("h", 2, 3); /* ?- X3 = h */ set_variable(2); /* (Z, */ set_variable(5); /* W), */ put_structure("f", 1, 4); /* X4 = f */ set_value(5); /* (W), */ put_structure("p", 3, 1); /* X1 = p */ set_value(2); /* (Z, */ set_value(3); /* X3, */ set_value(4); /* X4). */ print_register(1); /* drukowanie X1 */ dump_heap(); fail = false; get_structure("p", 3, 1); /* X1 = p */ unify_variable(2); /* (X2, */ unify_variable(3); /* X3, */ unify_variable(4); /* Y), */ get_structure("f", 1, 2); /* X2 = f */ unify_variable(5); /* (X), */ get_structure("h", 2, 3); /* X3 = h */ unify_value(4); /* (Y, */ unify_variable(6); /* X6), */ get_structure("f", 1, 6); /* X6 = f */ unify_variable(7); /* (X7), */ get_structure("a", 0, 7); /* X7 = a */ printf("fail = %d\n", (int)fail); print_register(1); /* drukowanie X1 */ dump_heap(); return 0; }
static int unify_list_from_span(term_t list, clingo_symbol_t const *syms, size_t slen) { int rc; term_t tail = PL_copy_term_ref(list); term_t head = PL_new_term_ref(); term_t tmp = PL_new_term_ref(); clingo_symbol_t const *it, *ie; for (it = syms, ie = it + slen; it != ie; ++it) { PL_put_variable(tmp); if (!(rc = (unify_value(tmp, *it) && PL_unify_list(tail, head, tail) && PL_unify(head, tmp)))) { goto out; } } if (!(rc = PL_unify_nil(tail))) { goto out; } out: return rc; }
static int unify_value(term_t t, clingo_symbol_t v) { // NOTE: the clingo_symbol_* functions below only fail // if applied to the wrong type // they do not allocate switch (clingo_symbol_type(v)) { case clingo_symbol_type_number: { int number; clingo_symbol_number(v, &number); return PL_unify_integer(t, number); } case clingo_symbol_type_string: { char const *str; clingo_symbol_string(v, &str); return PL_unify_chars(t, PL_STRING | REP_UTF8, (size_t)-1, str); } case clingo_symbol_type_infimum: { return PL_unify_term(t, PL_FUNCTOR, FUNCTOR_hash1, PL_ATOM, ATOM_inf); } case clingo_symbol_type_supremum: { return PL_unify_term(t, PL_FUNCTOR, FUNCTOR_hash1, PL_ATOM, ATOM_sup); } case clingo_symbol_type_function: { // FIXME: functions can have signs represented as -f(x) in gringo char const *str; clingo_symbol_t const *args; size_t size; int rc; clingo_symbol_name(v, &str); clingo_symbol_arguments(v, &args, &size); if (size == 0) { if (!(rc = PL_unify_chars(t, PL_ATOM | REP_UTF8, (size_t)-1, str))) { goto out_function; } } else { clingo_symbol_t const *it, *ie; atom_t name; term_t arg; int i; name = PL_new_atom(str); if (!(rc = PL_unify_functor(t, PL_new_functor(name, size)))) { goto out_function; } PL_unregister_atom(name); arg = PL_new_term_ref(); for (i = 1, it = args, ie = it + size; it != ie; ++it, i++) { _PL_get_arg(i, t, arg); if (!unify_value(arg, *it)) { goto out_function; } } } out_function: return rc; } default: assert(FALSE); return FALSE; } }