注册 | 登录


Jammg 分享于



推荐:Qt Model/View 学习笔记 (一)

本文来源 :清源游民 介绍 Qt 4推出了一组新的item view类,它们使用model/view结构来管理数据与表示层的关系。这种结构带来的 功能上的分离给了开发人员更大的

up vote 2 down vote favorite I have this hypothetical program to check if a path exists from point A to B. /*city rules*/edge(phx, tuc).edge(tuc, ccg).edge(ccg, sf).connected(C1, C2) :-

edge(C1, C2).connected(C1, C2) :-

edge(C1, X),

connected(X, C2). the problem is it returns true, then false. Where is the false coming from? prolog logic prolog-toplevel transitive-closure share | improve this question edited Dec 2 '14 at 17:45 false 10.1k 6 56 113 asked Dec 2 '14 at 17:26 ryan 128 2 14 add a comment


1 Answer 1 active oldest votes up vote 2 down vote Let's look at the exact reply you got from Prolog! First you got a single true and on pressing SPACE or ; you eventually got: ?- connected(phx,sf).true ;false. So you got true ; false. as the complete answer. The ; means "or". So Prolog essentially says: Your query connected(phx,sf). is the same as true ; false. So you need to look at the entire answer to understand what it means. Of course that is a bit odd, when true. would be good enough. But first let's have another example: ?- connected(phx,X).X = tuc ;X = ccg ;X = sf ;false. Here Prolog's complete answer is: connected(phx,X). describes the same set of solutions as X = tuc ; X = ccg ; X = fg ; false. which again would be shorter if false would be omitted. So why does Prolog write out this false? Prolog computes the answers incrementally. It first shows you X = tuc and waits what you would do. In a sense, it is a bit lazy not to show you everything at once. Sometimes, Prolog does know that there will be no further answer, in that case, it writes a dot directly: ?- member(X,[1,2]).X = 1 ;X = 2. Here Prolog says: dixi! I have spoken. But sometimes, it is not that sure: ?- member(1,[1,2]).true ;false. After proving that 1 is a member, it stops, otherwise it would have to explore the list further. So this ; false. means: After the last answer/solution, Prolog was not yet sure that everything has been explored. This might be an inefficiency, it might be an indication that something can be improved. However, never, ever take this as pretext to insert a cut into your program. The cut in another answer is completely malaprop. It is the source for many, many errors. Before starting to use cuts you really should learn the other, pure part of Prolog, first. BTW: Here is a better definition for connected/2 which avoids infinite loops by using closure/3 (click on it to get the definition). connected(C0,C) :-


closure0(edge,C1,C). share | improve this answer answered Dec 2 '14 at 18:20 false 10.1k 6 56 113 add a comment


Your Answer

draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged prolog logic prolog-toplevel transitive-closure or ask your own question.

推荐:Qt Model/View 学习笔记

介绍 Qt 4推出了一组新的item view类,它们使用model/view结构来管理数据与表示层的关系。这种结构带来的功能上的分离给了开发人员更大的弹性来定制数据项的表示

up vote 2 down vote favorite I have this hypothetical program to check if a path exists from point A to B. /*city rules*/edge(phx, tuc).edge(tuc, ccg).edge(ccg, sf).connected








您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱