1

我已获得用于验证 2 位全加器电路的功能和谓词。这是我到目前为止所理解的:-

一个函数 signal(t),其中 t 是终端,信号取值为 1 或 0。

signal(T, V) :- V is 1; V is 0.

函数 type(x),其中 type 是一个电路元素, type(x) 可以取值 xor,并且,或

type(X, T) :- T is and; T is or; T is xor.

函数 out(x) 表示门 x 的输出,而 in(n, x) n 是输入端。

out(X, O) :- type(X, and), O is I1+I2, in(1, X, I1), in(1, X, I2). 
%%and so on for other gates

对于门谓词给出为

forall X[{type(X) == OR} ----> {(out(X) =1)}-----> for some y(in(y, X) = 1)]

对于这些,我不确定如何将它们转换为 prolog 代码,也不了解 prolog 函数如何返回某些东西(我认为结果也必须作为参数提供)。

任何帮助都会非常有用!

4

1 回答 1

1

首先,您应该为您的问题选择更好的表示。对于这只海豚来说,事实已经足够了。电路可以表示为:

in/1 --> 输入信号(0 或 1)。这是避免无限循环所必需的(见下文)。

xor/2, and/2, or/2 --> 对电路A 和 B 的逻辑运算。 not/1 --> 等等。

这样,电路将采用以下形状:

xor(and(in(In1),in(In2)),or(in(In1),not(in(In3))))

现在,让我们定义一个谓词 output/1,它计算电路的状态:

output(xor(In1,In2),1) :-
    output(In1,Out1),
    output(In2,Out2),
    Out1 \== Out2.
output(xor(In1,In2),0) :-
    output(In1,Out1),
    output(In2,Out2),
    Out1 == Out2.

output(and(In1,In2),1) :-
    output(In1,1),
    output(In2,1).
output(and(In1,In2),0) :-
    output(In1,Out1),
    output(In2,Out2),
    (Out1==0 ; Out2==0).

output(or(In1,In2),1) :-
    (output(In1,1); output(In2,1)).
output(or(In1,In2,0),0) :-
    output(In1,0),
    output(In2,0).

output(not(In),0) :-
    output(In,1).
output(not(In),1) :-
    output(In,0).
output(in(0),0).
output(in(1),1).

这个 predeciate 在 porpouse 上是可逆的,因此输入可以保留为自由变量以获得所有输出:

?- output(xor(and(in(In1),in(In2)),or(in(In1),not(in(In3)))),Result).
In1 = In2, In2 = In3, In3 = 0,
Result = 1 ;
In1 = In2, In2 = In3, In3 = 0,
Result = 1 ;
In1 = In3, In3 = 0,
In2 = Result, Result = 1 ;
In1 = Result, Result = 1,
In2 = 0 ;
In1 = Result, Result = 1,
In2 = In3, In3 = 0 ;
In1 = In2, In2 = 1,
Result = 0 ;
In1 = In2, In2 = 1,
In3 = Result, Result = 0 ;
false.

output(A,B)不应与output(in(A),B). 第一个可以实例化为任何电路(无限循环),而第二个可以实例化为 0 或 1。

它似乎适用于递归电路:

?- output(or(in(Input),in(InOut)),InOut).
Input = InOut, InOut = 1 ;
InOut = 1.
于 2016-09-20T08:23:37.757 回答