Simple c++ tutorial. How to call swi-prolog from c++

728 views
Skip to first unread message

Janez Bindas

unread,
Feb 2, 2018, 12:27:10 PM2/2/18
to SWI-Prolog
I would like to call swi-prolog from c++, but I can't find  good tutorial. I started with this program on https://stackoverflow.com/questions/45641606/how-to-consult-existing-prolog-file-into-c-and-its-module.
But when I put my own prolog program I couldn't get any result. My prolog program is this:


==================d.pl=================
 
d( X, X, 1 ):- !.                  /* d(X) w.r.t. X is 1      */
 
d( C, X, 0 ):- atomic(C).          /* If C is a constant then */
                                   /* d(C)/dX is 0            */
 
d( U+V, X, R ):-                 /* d(U+V)/dX = A+B where   */
   d( U, X, A ),                   /* A = d(U)/dX and         */
   d( V, X, B ),
   R = A + B.
 
d( U-V, X, R ):-
   d( U, X, A ),
   d( V, X, B ),
   R = A - B.
 
d( C*U, X, R ):-
   atomic(C),
   C \= X,
   d( U, X, A ),
   R = C * A,
   !.
 
d( U*V, X, U*B+V*A ):-           /* d(U*V)/dX = B*U+A*V where */
   d( U, X, A ),                 /* A = d(U)/dX and           */
   d( V, X, B ).                 /* B = d(V)/dX               */
 
d( U/V, X, (A*V-B*U)/(V^2) ):- /* d(U/V)/dX = (A*V-B*U)/(V*V) */
   d( U, X, A),                /* where A = d(U)/dX and       */
   d( V, X, B).                /*       B = d(V)/dX           */
 
d( U^C, X, R ):-       /* d(U^C)/dX = C*A*U^(C-1)   */
   atomic(C),                    /* where C is a number or    */
   C\=X,
   d( U, X, A ),
   R = C * A * U ^ ( C - 1 ).
====================end d.pl====================

and my c++ program look like this :
 
int main(int argc, char **argv)
{
    int n;
    cout << "Please enter a number: ";
    cin >> n;

    PL_initialise(argc, argv);

    PlCall("consult('d.pl')");

    term_t a, b, ans;
    functor_t func;

    a = PL_new_term_ref();
    PL_put_integer(a, n);
    b = PL_new_term_ref();
    ans = PL_new_term_ref();
    func = PL_new_functor(PL_new_atom("fact"), 2);  <--  here on I have to change something
    PL_cons_functor(ans, func, a, b);

    int fact;

    if (PL_call(ans, NULL))
    {
        PL_get_integer(b, &fact);
        cout << "Result is: " << fact << endl;
    }

    cin.ignore(2);
    return 0;
}

Can anybody help me to write a good tutorial about calling prolog from c++?

Regards Janez

Grzegorz Jaśkiewicz

unread,
Feb 2, 2018, 6:42:03 PM2/2/18
to Janez Bindas, SWI-Prolog
Its not really a tutorial, but i got some working example:

It comes from Counter-Strike bots controlled with Prolog scripts. I used there invocations of Prolog from C++ and C++ from Prolog.

--
You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/swi-prolog.
For more options, visit https://groups.google.com/d/optout.

Janez Bindas

unread,
Feb 3, 2018, 6:22:11 AM2/3/18
to Grzegorz Jaśkiewicz, SWI-Prolog
I looked at your code example and i rewrite my program like this:

#include <iostream>

#include <SWI-Prolog.h>
#include <SWI-Stream.h>



int main(int argc, char** argv) {

    PL_initialise(argc, argv);

    PlTermv args(1);
    args[0] = "d.pl";
    PlQuery pq("consult", args);
    if (pq.next_solution()) {
        std::cout << "Command OK\n";
    } else {
        std::cout << "Command failed\n";
    }

    PlTermv inputs(3);
    inputs[0] = "sin(x)";
    inputs[1] = "x";
    inputs[2] = "R";


   
    try {
        PlQuery pq_sin("d", inputs);

        while (pq_sin.next_solution()) {
            std::cout << (char*) inputs[0] << std::endl;
        }
        std::cout << "Query OK\n";
    } catch (PlException& e) {
        std::cout << (char*) e << std::endl;
        std::cout << "Query error\n";
    }


    return 1;
}
result is :


Command OK
Query OK

How I get the result from query?

Regards Janez


Reply all
Reply to author
Forward
0 new messages