Visual studio 2008 回溯问题

Visual studio 2008 回溯问题,visual-studio-2008,swi-prolog,backtracking,Visual Studio 2008,Swi Prolog,Backtracking,假设我有以下Prolog知识库: likes( john, mary ). likes( john, emma ). likes( john, ashley ). 如果我编写以下C代码: #include... term_t tv; term_t tu; term_t goal_term; functor_t goal_functor; int main( int argc, char** argv ) { argv[ 0 ] = "libpl.dll"; PL_initi

假设我有以下Prolog知识库:

likes( john, mary ).
likes( john, emma ).
likes( john, ashley ).
如果我编写以下C代码:

#include...

term_t tv;
term_t tu;
term_t goal_term;
functor_t goal_functor;

int main( int argc, char** argv )
{
    argv[ 0 ] = "libpl.dll";
    PL_initialise( 1, argv );
    PlCall( "consult( 'likes.pl' )" );

    tv = PL_new_term_ref( );
    PL_put_atom_chars( tv, "john" );
    tu = PL_new_term_ref( );
    goal_term = PL_new_term_ref( );
    goal_functor = PL_new_functor( PL_new_atom( "likes" ), 2 );
    PL_cons_functor( goal_term, goal_functor, tv, tu );

    if ( PL_call( goal_term, NULL ) );
     {
       char* solution;
       PL_get_atom_chars( tu, &solution );
       cout << solution << endl;
     }

    PL_halt( PL_toplevel( ) ? 0 : 1 );
}
#包括。。。
电视术语;
任期;;
期限\目标\期限;
函子\u t目标\u函子;
int main(int argc,字符**argv)
{
argv[0]=“libpl.dll”;
PL_初始化(1,argv);
PlCall(“consult('likes.pl')”);
tv=PL_new_term_ref();
电视,约翰;
tu=PL_new_term_ref();
目标条款=新条款条款参考();
目标函数=PL新函数(PL新原子(“likes”),2);
PL_cons_函子(目标项,目标函子,tv,tu);
if(PL_调用(目标项,NULL));
{
char*溶液;
获得原子电荷(tu和溶液);

CUT

我已经把完整的河内程序、阶乘和回溯都包含在下面,希望初学者能从中受益。如果有人不确定VS2008的C++接口和Prolog接口,我会很乐意提供它们。

#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdafx.h>
using namespace std;
#include "Windows.h"
#include "ctype.h"
#include "SWI-cpp.h"
#include "SWI-Prolog.h"
#include "SWI-Stream.h"

term_t t;
predicate_t p;

term_t tf;
term_t tx;

term_t tv;
term_t tu;
predicate_t pred;

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( 'myPrologFile.pl' )" );


    cout << "Enter your hanoi number: ";
    int n;
    cin >> n;
    cout << "Calculating hanoi of " << n << endl;
    PL_put_integer( t, n );
    p = PL_predicate( "hanoi", 1, NULL );
    PL_call_predicate( NULL, PL_Q_NORMAL, p, t );


    cout << "Enter your factorial number: ";
    int nf;
    cin >> nf;
    cout << "Calculating factorial of " << nf << endl;
    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;
    }


    cout << "Backtracking . . .";
    tv = PL_new_term_ref( );
    PL_put_atom_chars( tv, "john" );
    pred = PL_predicate( "likes", 2, NULL );
    tu = PL_new_term_ref( );
    qid_t qid = PL_open_query( NULL, PL_Q_NODEBUG, pred, tv );
    while ( int i = PL_next_solution( qid ) )
    {
        char* solution;
        PL_get_atom_chars( tu, &solution );
        cout << solution << endl;
    }
    PL_close_query( qid );


    PL_halt( PL_toplevel( ) ? 0 : 1 );
}
:- use_module( library( shlib ) ).
:- use_module( library( lists ) ).


hanoi( N ):-
    move( N, left, center, right ).

move( 0, _, _, _ ):-
    !.
move( N, A, B, C ):-
    M is N-1,
    move( M, A, C, B ),
    inform( A, B ),
    move( M, C, B, A ).

inform( X, Y ):-
    write( 'move a disk from ' ),
    write( X ),
    write( ' to ' ),
    write( Y ),
    nl.


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


likes( john, mary ).
likes( john, emma ).
likes( john, ashley ).