0

我刚开始学习序言。我正在为我的 CS 2300 课程学习它。我现在正在努力学习基础知识。如何向我的 .pl 文件添加谓词?我只是想在我的 .pl 文件中添加一个谓词。我正在这样做:

married(X,Y) :- married(Y,X).

我得到了错误:

ERROR: Undefined procedure: (:-)/2
ERROR: Rules must be loaded from a file
ERROR: See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.tx

我应该以某种方式启用它吗?

这是给定的.pl文件:

% File FAMILY.PL
% Part of a family tree expressed in Prolog
% In father/2, mother/2, and parent/2,
% first arg. is parent and second arg. is child.

father(michael,cathy).
father(michael,sharon).
father(charles_gordon,michael).
father(charles_gordon,julie).
father(charles,charles_gordon).
father(jim,melody).
father(jim,crystal).
father(elmo,jim).
father(greg,stephanie).
father(greg,danielle).

mother(melody,cathy).
mother(melody,sharon).
mother(hazel,michael).
mother(hazel,julie).
mother(eleanor,melody).
mother(eleanor,crystal).
mother(crystal,stephanie).
mother(crystal,danielle).

parent(X,Y) :- father(X,Y).
parent(X,Y) :- mother(X,Y).
4

2 回答 2

3

您必须在所有谓词之前使用单词谓词来编写谓词。

谓词,子句是序言中的关键字。你也必须使用那个关键词。

您可以参考此链接了解家庭关系计划。

http://www.dailyfreecode.com/Code/prolog-find-relations-family-3025.aspx

如果您是 prolog 的新手。

predicates 
father (symbol,symbol).
clauses 
father(michael,cathy).

试试这个代码。

于 2013-09-13T04:30:59.040 回答
2

这里发生的是您尝试在查询提示符处输入规则。这是你正在经历的:

Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 6.2.6)
Copyright (c) 1990-2012 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).

?- parent(X,Y) :- father(X,Y).
ERROR: Undefined procedure: (:-)/2
ERROR:   Rules must be loaded from a file
ERROR:   See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.txt
?- 

请注意,我那里的错误消息与您完全相同。Prolog 区分查询和咨询数据库。你想做的就是咨询。把你所有的东西放到一个文件中并命名它family.pl,然后这样做:

?- [family].

你应该看到这个结果:

% family compiled 0.00 sec, 21 clauses
true.

?- 

如果您想以交互方式输入规则和事实,请咨询“用户”,如下所示:

?- [user].
|: foo(X) :- bar(X).
|: <Ctrl-D>
% user://1 compiled 0.00 sec, 2 clauses
true.

请注意,<Ctrl-D>在按住 Control 键的同时键入 D,而不是按字面意思键入该文本。

至于另一个答案,它适用于 Visual Prolog ,因此与您的问题无关。许多 Prolog 实现了 ISO 标准,您可以期望它们根据输入的行为相似或相同。SWI 和 GNU 是一些比较流行的 ISO Prolog 实现。但是,Visual Prolog 是一种完全不同的语言,不应顺便称其为“Prolog”。

于 2013-09-13T19:14:25.367 回答