不寻求完整的解决方案,但这里有一些提示。
基本方法
% fact: truth value "v" is satisfiable in all environments.
sat(v,_).
% rule: and(X,Y) is satisfiable in environment E iff both X and Y are sat in E
sat(and(X,Y),E) :- sat(X,E), sat(Y,E).
绑定
sat(Var, E) :-
(member(Var:Value,E) ->
Value = v
; throw(unknown_variable(Var,E))).
例子:
[eclipse 6]: sat(o,[o:v]).
Yes (0.00s cpu)
[eclipse 7]: sat(o,[o:f]).
No (0.00s cpu)
[eclipse 8]: sat(o,[u:v]).
uncaught exception in throw(unknown_variable(o, [u : v]))
Abort
枚举
定义一个规则(binding)将一个变量绑定到一个不确定的值,另一个规则(bindings)将一个符号(原子)列表绑定到绑定列表。
% Two different solution possible when binding Var
binding(Var, Var:v).
binding(Var, Var:f).
% Lists of bindings
bindings([],[]).
bindings([V|VL],[B|BL]) :-
binding(V,B),
bindings(VL,BL).
例如:
[eclipse 9]: bindings([a,b,c],L).
L = [a : v, b : v, c : v]
Yes (0.00s cpu, solution 1, maybe more) ? ;
L = [a : v, b : v, c : f]
Yes (0.00s cpu, solution 2, maybe more) ? ;
L = [a : v, b : f, c : v]
Yes (0.00s cpu, solution 3, maybe more) ? ;
L = [a : v, b : f, c : f]
Yes (0.00s cpu, solution 4, maybe more) ? ;
L = [a : f, b : v, c : v]
Yes (0.00s cpu, solution 5, maybe more) ? ;
L = [a : f, b : v, c : f]
Yes (0.00s cpu, solution 6, maybe more) ? ;
L = [a : f, b : f, c : v]
Yes (0.00s cpu, solution 7, maybe more) ? ;
L = [a : f, b : f, c : f]
Yes (0.00s cpu, solution 8)
运营商
首先,您可以声明以下and 谓词:
and(0,0,0).
and(1,0,0).
and(0,1,0).
and(1,1,1).
规则可以应用为and(X,Y,R),R 是and 操作的结果。 or等也可以这样做。
您的声明:
:- op(100,xfy,and).
... 允许写X and Y 而不是and(X,Y),但请注意这里没有第三个参数。在ECLiPSe 环境中,运算符表示法还与is/2 一起用于计算算术表达式。由于上述add 谓词处理数字,因此以下工作:
X is 0 and 1.
以上将X与0统一。