• Places
    • Home
    • Graphs
    • Prefixes
  • Admin
    • Users
    • Settings
    • Plugins
    • Statistics
  • CPACK
    • Home
    • List packs
    • Submit pack
  • Repository
    • Load local file
    • Load from HTTP
    • Load from library
    • Remove triples
    • Clear repository
  • Query
    • YASGUI SPARQL Editor
    • Simple Form
    • SWISH Prolog shell
  • Help
    • Documentation
    • Tutorial
    • Roadmap
    • HTTP Services
  • Login

1 A C++ interface to SWI-Prolog
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • A C++ interface to SWI-Prolog
        • A C++ interface to SWI-Prolog
          • Summary of changes between Versions 1 and 2
          • A simple example
          • Sample code
          • Introduction
          • The life of a PREDICATE
          • Overview
          • Examples
          • Rationale for changes from version 1
          • Porting from version 1 to version 2
          • The class PlFail
          • Overview of accessing and changing values
          • The class PlRegister
          • The class PlQuery
          • The PREDICATE and PREDICATE_NONDET macros
          • Exceptions
          • Embedded applications
          • Considerations
          • Conclusions

1.3 Sample code

The file test_cpp.cpp contains examples of Prolog predicates written in C++. This file is used for testing (called from test_cpp.pl). Notable examples:

  • add_num/3 - same as A3 is A1+A2, converting the sum to an integer if possible.
  • name_arity/3 - C++ implementation of functor/3.
  • average/3 - computes the average of all the solutions to Goal
  • can_unify/2 - tests whether the two arguments can unify with each other, without instantiating anything (similar to unifiable/3).
  • eq1/1, eq2/2, eq3/2 - three different ways of implementing =/2.
  • write_list/1 - outputs the elements of a list, each on a new line.
  • cappend/3 - appends two lists (requires that the two lists are instantiated).
  • square_roots/2 - same as bagof(Sqrt, X^(between(0,4,X), Sqrt is sqrt(X)), A2).
  • range_cpp/3 - on backtracking, generates all integers starting at A1 and less than A2 (that is, one less than between/3).
  • int_info/2 - on backtracking generates all the integral types with their minimum and maximum values.

The file likes.cpp contains a simple program that calls the Prolog predicate likes/2 and happy/1 (these predicates are defined in likes.pl. The usage and how to compile the code are in comments in likes.cpp

ClioPatria (version V3.1.1-51-ga0b30a5)