如何在Prolog中获得变量?

时间:2012-09-08 12:43:47

标签: prolog swi-prolog

我有问题。几个星期以来,我无法弄清楚这个问题。 例如:

parent(john,paul). 
parent(paul,tom).  
parent(tom,mary).  
ancestor(X,Y) :- parent(X,Y). 
ancestor(X,Y) :- parent(X,Z),
                 ancestor(Z,Y).

查询是:

swipl -s /home/alikoyuncu/pl/ples.pl -g "ancestor(X,'tom')" -t halt.

输出:

% library(swi_hooks) compiled into pce_swi_hooks 0.00 sec, 3,856 bytes
% /home/alikoyuncu/pl/ples.pl compiled 0.01 sec, 119,096 bytes

alikoyuncu@alikoyuncu-EasyNote-TM98:/var/www/nlp$

我应该怎么做变量X?


我正在从php打电话。我的PHP代码:

<?php

try
{

    $cmd ="swipl --quiet -s /home/alikoyuncu/pl/ples.pl -g \"forall(f(X,gel),writeln(X))\" -t halt.";
    $cmd2="/var/www/nlp/betik.sh";
    exec( $cmd, $output );


if($output==null)
{
    echo "null";
}
else
{
    foreach( $output as $tampon ) { echo "$tampon .nci satir <br>"; };
}

}
catch(Exception $ex)
{
    echo "Error";
}
?>

2 个答案:

答案 0 :(得分:2)

请注意,查询文件的常用方法是

$ prolog

允许您输入swi prolog解释器:

Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.10.4)
Copyright (c) 1990-2011 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

然后您可以在哪里查询:

?- consult(ples.pl).

(或[ples].)加载文件然后

?- ancestor(X, tom).

获得理想的结果。

否则,要从命令行调用它,我建议:

swipl --quiet -s ples.pl -g "forall(ancestor(X, tom), writeln(X)), halt."

答案 1 :(得分:0)

谢谢,这个代码运行到祖先procudure但是当我使用f(X,'gel')procudere时,它没有运行.f procudure是:

f(Morphemes,String) :- trav(Morphemes,String,b).

trav([],'',CurrentState) :-
    son(CurrentState).

trav([Morph|Morphs],String,CurrentState):-
    (nonvar(String) ->
         (concat(Phon,Rest,String),
          not(Phon == ''));
          true),
    t(CurrentState,Morph:Phon,NextState),
    trav(Morphs,Rest,NextState),
    (var(String) ->
              concat(Phon,Rest,String);true).

所有代码:https://docs.google.com/open?id=0B0itKSwxT0gTX0dNUmFobTJqaG8

相关问题