简单的布尔前pression测试 [英] Simple boolean expression testing
问题描述
| ?- [user].
compiling user for byte code...
formula_0(P, Q):- (P; Q), \+ P.
user compiled, 2 lines read - 768 bytes written, 37208 ms
yes
| ?- formula_0(P, Q).
uncaught exception: error(instantiation_error,formula_0/2)
所有我基本上想要做的就是要问的是一组前pressions的{P或Q,〜p}满足的?的
不过,该错误信息不会在这里帮助...
But the error message is not helping here...
PS。答案应该是是,这个公式是满足当p =假,Q =真。
PS. The answer should be "yes", this formula is satisfied when P = false and Q = true.
推荐答案
究其原因,你得到一个实例化的错误就是太知之甚少 P
和<$ C $ ç>问:说在它们被用作目标的时候他们什么。当你问比如你是正确的:
The reason you get an instantiation error is that too little is known about P
and Q
to say anything about them at the time they are used as goals. You are right that when you ask for example:
?- G.
然后 G =真
是使查询成功的解决方案。但这样是例如 G =(A = A)
,因为 A = A
也是如此。因为它是不可能枚举成功的所有目标
then G = true
is a solution that makes the query succeed. But so is for example G = (a=a)
because a=a
is also true. Since it is not possible to enumerate all goals G
that succeed, you get an instantiation error. But notice for example that when you explicitly give a binding, you get the result you expect:
?- G = true, G.
G = true.
所以,你应该提供一套价值你感兴趣的是:
So, you should provide a set of values you are interested in:
?- maplist(boolean, [P,Q]), formula(P, Q).
和定义,例如:
boolean(true).
boolean(false).
要获得具体的解决方案。或者使用的限制的,这让你在使用之前约束变量的域。
to obtain concrete solutions. Or use constraints, which let you constrain the domain of variables before using them.
修改:既然有这个一些讨论,我去到更多的细节。所出现的主要问题是:为什么查询
EDIT: Since there was some discussion about this, I go into more detail. The main question that arose is: Why does the query
?- Q.
与鞋底的解决方案不会成功。
not succeed with the sole solution
Q = true.
因为很明显,真正
成功,因此是一个有效的解决方案?答:还有其他可能的答案为好,因为真正
不是成功的唯一目标。例如,结合(真的,真)
成功也是如此。假设现在 Q =真
是上述查询的唯一解决方案,那么这将是的情况是:
since obviously, true
succeeds and is thus a valid solution? Answer: There are other possible answers as well, since true
is not the only goal that succeeds. For example, the conjunction (true,true)
succeeds as well. Suppose now that Q = true
were the only solution to the above query, then it would be the case that:
?- Q, Q = (true, true).
的失败的(因为 DIF(TRUE(真,真))
本身是真实的),只是简单的交流,比如
fails (since dif(true, (true,true))
is itself true), but simply exchanging the goals like
?- Q = (true,true), Q.
的成功的,否则 - ?真的,真的
将也有失败,并不能如此。这将违反配合可交换已经是最基本的predicate(统一)。
succeeds because otherwise ?- true, true
would also have to fail and that cannot be the case. This would violate commutativity of conjunction already for the most basic predicate (unification).
请注意,虽然真正
和(真的,真)
的两个的执行时成功作为目标,他们显然的不同的术语的,不能在一般的相互取代。同样,术语追加([],[],[])
从长期不同真正
,虽然两者当作为目标执行成功。因此,你得到一个实例化错误,因为太知之甚少问:
在查询像 - ?:Q
给有用的结果。
Note that while true
and (true,true)
both succeed when executed as goals, they are obviously different terms and cannot replace each other in general. Similarly, the term append([], [], [])
is different from the term true
, although both succeed when executed as goals. You therefore get an instantiation error, since too little is known about Q
in a query like ?- Q.
to give useful results.
这篇关于简单的布尔前pression测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!