swi-prolog 中的逻辑门与

Logic gate AND in swi-prolog

如何获取以下 Prolog 程序

and(1,1,1).
and(1,0,0).
and(0,1,0).
and(0,0,0).

对应以下答案

?- and(A,B,C).
A=1, B=1, C=1;
A=1, B=0, C=0;
A=0, B=1, C=0;
A=0, B=0, C=0.

当我尝试运行上面的程序时,我得到以下结果

?- and(A,B,C).
A = B, B = C, C = 0 ;
A = C, C = 0,
B = 1 ;
A = 1,
B = C, C = 0 ;
A = B, B = C, C = 1.

这似乎是正确的,但我不想在我的答案中有变量,这简化了我的预期答案。

如果我 运行 GNU Prolog 的示例,我只会得到原子作为变量的答案,而不是对变量本身的引用。这也是我想要的 swi-prolog:

GNU Prolog 1.4.5 (64 bits)
Compiled Feb  5 2017, 10:30:08 with gcc
By Daniel Diaz
Copyright (C) 1999-2016 Daniel Diaz
| ?- und(A,B,C).

A = 1
B = 1
C = 1 ? ;

A = 1
B = 0
C = 0 ? ;

A = 0
B = 1
C = 0 ? ;

A = 0
B = 0
C = 0

此示例也在第 10 页的 PDF file 中。

我正在 运行在 Ubuntu 17.10

上为 amd64 安装 SWI-Prolog 7.4.2 版

谢谢!

//编辑:逻辑与的更正结果。 //edit2: 添加了来自 GNU Prolog 的示例,结果应该如何。

首先,正如评论中已经提到的,您的谓词 and/3 没有像您引用的 PDF 中那样描述逻辑 AND 。第10页的定义是:

and(0,0,0).
and(0,1,0).
and(1,0,0).
and(1,1,1).

其次,如果它只是关于最一般查询的输出,您可以编写一个 arity 1 的包装谓词,将两个参数和结果显示为三元组:

and(A-B-C) :-
   and(A,B,C).

如果您使用单个变量查询 and/1,您会得到类似于 post:

中的输出
?- and(X).
X = 0-0-0 ;
X = 0-1-0 ;
X = 1-0-0 ;
X = 1-1-1.

如果您使用三个变量查询 and/1,您会得到与 and/3:

的最一般查询相同的答案
?- and(A-B-C).
A = B, B = C, C = 0 ;
A = C, C = 0,
B = 1 ;
A = 1,
B = C, C = 0 ;
A = B, B = C, C = 1.

?- and(A,B,C).
A = B, B = C, C = 0 ;
A = C, C = 0,
B = 1 ;
A = 1,
B = C, C = 0 ;
A = B, B = C, C = 1.

编辑

在上面的示例中,您可以观察到 Prolog 提供的每个答案如何包含对查询中出现的每个变量的替换,以便这些替换满足关系。这是在使用参数 X 查询 and/1 时在上面的 "trick" 中使用的 属性:只有一个变量可以提供答案替换。您可以通过定义元数为 0 的输出谓词来更进一步。然后 Prolog 只能在成功的情况下回答 true,因为查询中没有变量可以提供替代,您可以使用像 format/2 根据您的喜好创建输出。例如:

andoutput :-
   and(A,B,C),
   format('A = ~d, B = ~d, C = ~d~n', [A,B,C]).

查询此谓词会产生所需的输出:

?- andoutput.
A = 0, B = 0, C = 0    % <- output by format/2
true ;                 % <- Prolog's answer
A = 0, B = 1, C = 0    % <- output by format/2
true ;                 % <- Prolog's answer
A = 1, B = 0, C = 0    % <- output by format/2
true ;                 % <- Prolog's answer
A = 1, B = 1, C = 1    % <- output by format/2
true.                  % <- Prolog's answer

注意谓词生成的输出与 Prolog 提供的答案之间的区别。如果您更喜欢与 GNU-Prolog 的答案更相似的输出,您可以定义如下内容:

andoutput2 :-
   and(A,B,C),
   format('~nA = ~d~nB = ~d~nC = ~d~n', [A,B,C]).

?- andoutput2.
         % <- output by format/2
A = 0    % <- output by format/2
B = 0    % <- output by format/2
C = 0    % <- output by format/2
true ;   % <- Prolog's answer
         % <- output by format/2
A = 0    % <- output by format/2
B = 1    % <- output by format/2
C = 0    % <- output by format/2
true ;   % <- Prolog's answer
         % <- output by format/2
A = 1    % <- output by format/2
B = 0    % <- output by format/2
C = 0    % <- output by format/2
true ;   % <- Prolog's answer
         % <- output by format/2
A = 1    % <- output by format/2
B = 1    % <- output by format/2
C = 1    % <- output by format/2
true.    % <- Prolog's answer

但是,请记住,这只是格式化输出,绝不会改变 Prolog 提供答案的方式。因此,对于您想以个性化方式回答的每个谓词,您必须提供一个输出谓词。要查看更多用于生成输出的选项,请查看 formatted write.

上的文档