質問

数字の要因を解決したいと思います。私の要因ルールはPrologファイルにあり、C ++ファイルに接続しています。誰かが私のc ++をプロログにインターフェースすることの何が悪いのか教えてもらえますか?

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 );
}

プロログプロンプトを取得します。これが最後のラインが行うことです。しかし、次のような要因計算の結果は得られません。

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

何が足りないの?

ありがとう、

役に立ちましたか?

解決 2

# 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 );
}

他のヒント

私はSWI Prolog – C/C ++ブリッジにそれほど精通しているわけではありませんが、用語を定義するとクエリを開始していないようです。使用する必要があります PL_call また PL_open_query そして、結果を印刷してから、物事をSWI-Prologに引き渡します。

    PL_cons_functor( goal_term, goal_functor, tf, tx );
int nfactorial;
if (PL_call(goal_term, NULL)) {
    PL_get_integer(tx, &nfactorial);
    std::cout << "X = " << nfactorial << " ." << std::endl;
} else {
    PL_fail;
}
    PL_halt( PL_toplevel() ? 0 : 1 );

設定できるかどうかはわかりません goal_term トップレベルのクエリとして。クエリを通話によって実行したい場合 PL_toplevel, 、クエリを文字列として作成し、引数ベクトルの「-t」引数として渡すことができます PL_initialize.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top