为什么这个简单的代码在Prolog中无法正常运行?

时间:2016-10-20 14:54:39

标签: prolog

我已经编写了一段代码,根据用户输入确定了刺激情况,但问题是它总是提供相同的输出,并且根据规则下调不正确。 这是我的代码:

 start:- readenviroment(X),check(X,SS),environment(X,SS),write(SS).


readenviroment(X):-
write("What sort of environment is a trainee dealing with on the job?"),nl,
read(X).

environment(X,SS):-
    visual(X,SS) ; verbal(X,SS).

visual(X,SS):-
((X == pictures ; X == illustrations ; X == photographs ; X== diagrams)->assert(yes(SS,cars))).

verbal(X,SS):-
((X == papers ; X == manuals ; X == documents ; X == textbooks )->assert(yes(SS,verbal))).


:- dynamic yes/2.
check(XX,SS):-
verify(XX,SS),!.

verify(XX,SS):-
yes(XX,SS)->true.

这是输出:

     2 ?- start.
What sort of environment is a trainee dealing with on the job?
|: textbook.
verbal
true.

3 ?- start.
What sort of environment is a trainee dealing with on the job?
|: diagram.

false.

4 ?- start.
What sort of environment is a trainee dealing with on the job?
|: diagrams.
verbal
true.

根据这两条规则。

Rule 1:
if the environment is papers
or the environment is manuals
or the environment is documents
or the environment is textbooks
then stimulus_situation is verbal
Rule 2:
if the environment is pictures
if the environment is illustrations
if the environment is photographs
if the environment is diagrams
then stimulus_situation is visual

有人可以帮我吗?!还要提前感谢。

1 个答案:

答案 0 :(得分:0)

您的代码存在许多问题。直接问题是在check/2之前调用environment/2

但在我看来,你并不了解Prolog翻译工作。例如,变量SS永远不会绑定到代码中的任何位置。

您可以按照以下方式对规则进行建模:

env_stim(Env, verbal) :- member(Env, [paper, manuals, documents, textbooks])
env_stim(Env, visual) :- member(Env, <etc>).
<etc>.

然后,您只需询问用户Env变量,调用env_stim(Env, Stim)并写出回复:

start :- 
  writeln('What is the environment?'), 
  read(Env), 
  env_stim(Env, Stim), 
  writeln(Stim).