警告:单例变量:序言中的[Alice,Ben]

maz*_*zix 1 prolog

我在prolog中写了一个程序.

parent(Amy,John).
parent(Bob,John).
parent(John,Ben).
parent(Alice,Ben).
Run Code Online (Sandbox Code Playgroud)

我在Ubuntu 12.04上使用SWI-Prolog.当我在swi-prolog解释器中插入我的文件时:

['example.pl']
Run Code Online (Sandbox Code Playgroud)

我收到警告:

Warning: /home/mazix/example1.pl:1:
        Singleton variables: [Amy,John]
Warning: /home/mazix/example1.pl:2:
        Singleton variables: [Bob,John]
Warning: /home/mazix/example1.pl:3:
        Singleton variables: [John,Ben]
Warning: /home/mazix/example1.pl:4:
        Singleton variables: [Alice,Ben]
% example1.pl compiled 0.00 sec, 4 clauses
true.
Run Code Online (Sandbox Code Playgroud)

这些是什么意思?又是什么的true底部是什么意思?我应该如何摆脱这种警告?

小智 6

以大写字母开头的标识符是变量.如果你想要原子,用单引号括起来:

parent('Amy', 'John').
Run Code Online (Sandbox Code Playgroud)

或者用小写字母开头:

parent(amy, john).
Run Code Online (Sandbox Code Playgroud)

"单例变量"是一个命名变量,在其词法范围内只出现一次.在实践中,这意味着你要命名它,但是你没有对它做任何有用的事情,因此编译器警告.