2.9 The class PlTerm (version 2)
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • A C++ interface to SWI-Prolog
        • A C++ interface to SWI-Prolog (Version 2)
          • The class PlTerm (version 2)
            • Constructors (version 2)
            • Overview of accessing and changing values (version 2)
            • Converting PlTerm to native C and C++ types (version 2)
            • Unification (version 2)
            • Comparison (version 2)
            • Analysing compound terms (version 2)
            • Miscellaneous (version 2)
            • The class PlTermString (version 2)
            • The class PlCodeList (version 2)
            • The class PlCharList (version 2)
            • The class PlCompound (version 2)
            • The class PlTail (version 2)

2.9.2 Overview of accessing and changing values (version 2)

The SWI-Prolog.h header provides various functions for accessing, setting, and unifying terms, atoms and other types. Typically, these functions return a 0 (false) or 1 (true) value for whether they succeeded or not. For failure, there might also be an exception created - this can be tested by calling PL_excpetion(0).

There are three major groups of methods:

  • Put (set) a value, corresponding to the PL_put_*() functions.
  • Get a value, corresponding to the PL_get_*() and PL_get_*_ex() functions.
  • Unify a value, corresponding to the PL_unify_*() and PL_unify_*_ex() functions.

The "put" operations are typically done on an uninstantiated term (see the PlTerm_var() constructor). These are expected to succeed, and typically raise an exception failure (e.g., resource exception) - for details, see the corresponding PL_put_*() functions in Constructing Terms.

For the "get" and "unify" operations, there are three possible failures:

  • false return code
  • unification failure
  • exception (value of unexpected type or out of resources)

Each of these is communicated to Prolog by returning false from the top level; exceptions also set a "global" exception term (using PL_raise_exception()). The C++ programmer usually doesn't have to worry about this; instead they can throw PlFail() for failure or throw PlException() (or one of PlException’s subclasses) and the C++ API will take care of everything.