×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Factorial in Prolog and C++

Factorial in Prolog and C++

Factorial in Prolog and C++

(OP)

I would like to work out a number's factorial. My factorial rule is in a Prolog file and I am connecting it to a C++ code. Can someone tell me what is wrong with my C++ interface please?


% factorial.pl

factorial( 1, 1 ):-
!.
factorial( X, Fac ):-
X > 1,
Y is X - 1,
factorial( Y, New_Fac ),
Fac is X * New_Fac.


// factorial.cpp

# headerfiles

term_t t1;
term_t t2;
term_t goal_term;
functor_t goal_functor;

int main( int argc, char** argv )
{
argc = 4;

argv[0] = "libpl.dll";
argv[1] = "-G32m";
argv[2] = "-L32m";
argv[3] = "-T32m";

PL_initialise(argc, argv);

if ( !PL_initialise(argc, argv) )
PL_halt(1);

PlCall( "consult(swi('plwin.rc'))" );
PlCall( "consult('factorial.pl')" );

cout << "Enter your factorial number: ";
long n;
cin >> n;

PL_put_integer( t1, n );
t1 = PL_new_term_ref();
t2 = PL_new_term_ref();
goal_term = PL_new_term_ref();
goal_functor = PL_new_functor( PL_new_atom("factorial"), 2 );
PL_put_atom( t1, t2 );
PL_cons_functor( goal_term, goal_functor, t1, t2 );

PL_halt( PL_toplevel() ? 0 : 1 );
}

RE: Factorial in Prolog and C++

(OP)
Here is a simpler version of my problem, if someone could please help me:

I would like to work out a number's factorial. My factorial rule is in a Prolog file and I am connecting it to a C++ file. Can someone please tell me what is wrong with my interfacing C++ to Prolog?

my factorial.pl file:

factorial( 1, 1 ):-
!.
factorial( X, Fac ):-
X > 1,
Y is X - 1,
factorial( Y, New_Fac ),
Fac is X * New_Fac.



my factorial.cpp file:

headerfiles

term_t tf;
term_t tx;
term_t goal_term;
functor_t goal_functor;

int main( int argc, char** argv )
{
argv[0] = "libpl.dll";

PL_initialise(argc, argv);

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

cout << "Enter your factorial number: ";
long nf;
cin >> nf;

tf = PL_new_term_ref();
PL_put_integer( tf, nf );
tx = PL_new_term_ref();

goal_term = PL_new_term_ref();
goal_functor = PL_new_functor( PL_new_atom("factorial"), 2 );
rval = PL_cons_functor( goal_term, goal_functor, tf, tx );

PL_halt( PL_toplevel() ? 0 : 1 );
}

I get the Prolog prompt, which is what the last line does. But I don't get the result of the factorial calculation, such as:

?- factorial( 5, X ).
X = 120
true

What am I missing?

Thanks,

RE: Factorial in Prolog and C++

(OP)


# include files

term_t tf;
term_t tx;
term_t goal_term;
functor_t goal_functor;

int main( int argc, char** argv )
{
argv[0] = "libpl.dll";
PL_initialise( argc, argv );

PlCall( "consult( swi( 'plwin.rc' ) )" );
PlCall( "consult( 'factorial.pl' )" );

cout << " Enter your factorial number: ";
long nf;
cin >> nf;

tf = PL_new_term_ref();
PL_put_integer( tf, nf );
tx = PL_new_term_ref();
goal_term = PL_new_term_ref();
goal_functor = PL_new_functor( PL_new_atom("factorial"), 2 );
PL_cons_functor( goal_term, goal_functor, tf, tx );

int fact;
if( PL_call(goal_term, NULL) )
{
PL_get_integer( tx, &fact );
cout << fact << endl;
}
else
{
PL_fail;
}

PL_halt( PL_toplevel() ? 0 : 1 );
}

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close