2013-05-04 52 views
18

我有這樣一段代碼:序言:第不在一起的源文件

% Family tree 
female(pen). 
male(tom). 
male(bob). 
female(liz). 
female(pat). 
female(ann). 
male(jim). 

parent(pam, bob). 
parent(tom, bob). 
parent(tom, liz). 
parent(bob, ann). 
parent(bob, pat). 
parent(pat, jim). 

我得到這個錯誤:

Warning: Clauses of female/1 are not together in source-file 
Warning: Clauses of male/1 are not together in source-file 

這是什麼錯誤的目的是什麼?
我的意思是,文件編譯和運行很好,我意識到錯誤的含義。但爲什麼?
這僅僅是一個強制執行最佳實踐的通知?

我對邏輯編程非常陌生。
謝謝!

+0

對於那些不知道如何解決它的人:按關係的名稱將關係(在這種情況下是男性和女性)進行分組。 – 2014-03-04 17:06:44

回答

12

正確,這是強制執行最佳實踐的警告,即將所有相關子句放在一起放在源文件中。除此之外,只要它們的相對順序沒有改變,源文件中子句彼此的接近就不重要了。

+0

有沒有辦法關閉它?有時候我不能以這種方式編寫代碼。 – intelis 2013-05-04 12:29:09

+1

@intelis有一種方法可以告訴Prolog你打算散佈子句([鏈接到解釋如何做的答案](http://stackoverflow.com/q/2482101/335858))。 – dasblinkenlight 2013-05-04 12:34:50

+0

謝謝,但它不僅僅是子句。我得到一堆不同的警告,比如Singleton變量等。 – intelis 2013-05-04 12:42:23

5

該警告鼓勵最佳做法並幫助識別錯別字。這裏有一個錯字例如:

small(ant). 
small(fly). 
small(molecule). 

smell(sweet). 
smell(pungent). 
small(floral). 

的錯誤是很難發現的,但幸運的是編譯器警告:

Warning: /tmp/test.pl:7: 
Clauses of small/1 are not together in the source-file 

與警告和線路故障,可以發現並更快地糾正這個錯誤。

ISO Prolog提供了discontiguous/1指令以針對特定謂詞使此警告無聲。參見規範的第7.4.2.3節。它的使用是這樣的:

:- discontiguous small/1. 
相關問題