Prolog语句之间的区别
Prolog difference between statements
谁能给我解释一下原因是:
notmarried(P) :- \+(married(P)), male(P).
不同于:
notmarried(P) :- male(P), \+(married(P)).
问得好!
答案与逻辑纯度有关:在 Prolog 中,否定被实现为失败否定。一般来说,目标 \+ G
声明 G
目前无法 证明 ——并不是说 G
在逻辑上是错误的。
因此,您写的连词可能不是可交换的。
主题是 prolog 中的变量可以绑定到某个值 (X=foo) 或未绑定(未知值)。
现在,假设以下事实:
married(tom).
married(john).
中的 "not married" 之后必须是什么
\+married(P), male(P)
?
P 可以是除 "tom" 或 "john" 之外的任何值。但是 prolog 没有办法将这个事实存储在 "P" 中(不是使用基本语句)。因此,"not married" 的结果是 "yes, it is possible there some people not married" 并且 P 未绑定。 P未绑定,male(P)取第一个雄性,我们得到第一个答案。
现在,第二个查询:
male(P), \+married(P).
在 male 之后,prolog 会将 P 绑定到其中一个男性。现在,它会检查这个男性是否已婚,回答 yes/not。如果不是,它会退缩到另一只雄性身上,依此类推。
谁能给我解释一下原因是:
notmarried(P) :- \+(married(P)), male(P).
不同于:
notmarried(P) :- male(P), \+(married(P)).
问得好!
答案与逻辑纯度有关:在 Prolog 中,否定被实现为失败否定。一般来说,目标 \+ G
声明 G
目前无法 证明 ——并不是说 G
在逻辑上是错误的。
因此,您写的连词可能不是可交换的。
主题是 prolog 中的变量可以绑定到某个值 (X=foo) 或未绑定(未知值)。
现在,假设以下事实:
married(tom).
married(john).
中的 "not married" 之后必须是什么
\+married(P), male(P)
?
P 可以是除 "tom" 或 "john" 之外的任何值。但是 prolog 没有办法将这个事实存储在 "P" 中(不是使用基本语句)。因此,"not married" 的结果是 "yes, it is possible there some people not married" 并且 P 未绑定。 P未绑定,male(P)取第一个雄性,我们得到第一个答案。
现在,第二个查询:
male(P), \+married(P).
在 male 之后,prolog 会将 P 绑定到其中一个男性。现在,它会检查这个男性是否已婚,回答 yes/not。如果不是,它会退缩到另一只雄性身上,依此类推。