12.4.5 Unifying data
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • Foreign Language Interface
        • The Foreign Include File
          • Unifying data
            • PL_unify()
            • PL_unify_atom()
            • PL_unify_bool()
            • PL_unify_chars()
            • PL_unify_atom_chars()
            • PL_unify_list_chars()
            • PL_unify_string_chars()
            • PL_unify_integer()
            • PL_unify_int64()
            • PL_unify_uint64()
            • PL_unify_float()
            • PL_unify_pointer()
            • PL_unify_functor()
            • PL_unify_compound()
            • PL_unify_list()
            • PL_unify_nil()
            • PL_unify_arg()
            • PL_unify_term()
            • PL_chars_to_term()
            • PL_wchars_to_term()
            • PL_quote()
    • Packages
Availability:C-language interface function
int PL_unify(term_t ?t1, term_t ?t2)
Unify two Prolog terms and return TRUE on success.

Care is needed if PL_unify() returns FAIL and the foreign function does not immediately return to Prolog with FAIL. Unification may perform multiple changes to either t1 or t2. A failing unification may have created bindings before failure is detected. Already created bindings are not undone. For example, calling PL_unify() on a(X, a) and a(c,b) binds X to c and fails when trying to unify a to b. If control remains in C or even if we want to return success to Prolog, we must undo such bindings. This is achieved using PL_open_foreign_frame() and PL_rewind_foreign_frame(), as shown in the snippet below.

    { fid_t fid = PL_open_foreign_frame();

      ...
      if ( !PL_unify(t1, t2) )
        PL_rewind_foreign_frame(fid);
      ...

      PL_close_foreign_frame(fid);
    }

In addition, PL_unify() may have failed on an exception, typically a resource (stack) overflow. This can be tested using PL_exception(), passing 0 (zero) for the query-id argument. Foreign functions that encounter an exception must return FAIL to Prolog as soon as possible or call PL_clear_exception() if they wish to ignore the exception.