2015-10-01 89 views
1

如何使用'Univ'(=../2)prolog謂詞和Logtalk對象方法作爲參數?以Logtalk對象方法作爲參數使用「univ」Prolog謂詞作爲參數

考慮以下代碼:

baz(foo(X)) :- 
    write(predicate), write(X), nl. 

run :- 
    Term =.. [baz, foo(testfoo)], 
    write(Term), nl, Term,nl,  
    TermLgt =.. [bar::baz, foo(testfoo2)], 
    write(TermLgt), nl, Term,nl.  

:- object(bar). 

    :- public(baz/1). 
    baz(foo(X)) :- 
    write(method), write(X), nl. 

:- end_object. 

:- object(main). 

    :- public(run/0). 
    run :- 
    Term =.. [baz, foo(testfoo)], 
    write(Term), nl, Term,nl,  
    TermLgt =.. [bar::baz, foo(testfoo2)], 
    write(TermLgt), nl, Term,nl. 

:- end_object. 

我將獲得:

?- {myfile}. 
% (0 warnings) 
true. 

?- run. 
baz(foo(testfoo)) 
predicatetestfoo 

ERROR: =../2: Type error: `atom' expected, found `bar::baz' (a compound) 

?- main::run. 
baz(foo(testfoo)) 
ERROR: Undefined procedure: baz/1 
ERROR: However, there are definitions for: 
ERROR:   baz/1 

使用什麼解決辦法了很好的詮釋/編譯?看起來問題與swi-prolog building predicate相似,如predsort/3predsort/3 doc)。

回答

2

標準=../2謂詞預計,從列表構造的術語時,該第一列表參數是一個原子,但bar::baz是與算符::/2(其都定義爲謂詞的化合物術語 - 對於頂級的查詢 - 並在加載Logtalk時作爲操作員)。解決的辦法是不是寫:

baz(foo(X)) :- 
    write(predicate), write(X), nl. 


run :- 
    Term =.. [baz, foo(testfoo)], 
    write(Term), nl, call(Term), nl,  
    TermLgt =.. [::, bar, Term], 
    write(TermLgt), nl, call(Term), nl.  


:- object(bar). 

    :- public(baz/1). 
    baz(foo(X)) :- 
     write(method), write(X), nl. 

:- end_object. 


:- object(main). 

    :- public(run/0). 
    run :- 
     Term =.. [baz, foo(testfoo)], 
     write(Term), nl, Term,nl,  
     TermLgt =.. [::, bar, Term], 
     write(TermLgt), nl, Term,nl. 

:- end_object. 

有了這個變化,你會得到:

?- {univ}. 
% [ /Users/pmoura/Desktop/univ.lgt loaded ] 
% (0 warnings) 
true. 

?- run. 
baz(foo(testfoo)) 
predicatetestfoo 

bar::baz(foo(testfoo)) 
predicatetestfoo 

true.