0

如何使用带有 Logtalk 对象方法的 ' Univ '( ) prolog 谓词作为参数?=../2

考虑这段代码:

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 构建谓词predsort/3(如predsort/3 doc)相同。

4

1 回答 1

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.
于 2015-10-01T14:04:12.633 回答