Define predicate parts on multiple modules

I am trying to write a predicate move/3that processes several types of terms, each of which is defined in a separate file. I am trying to use modules for this because the files contain other predicates that must match the names accordingly.

So, I created a module cat.prologwith the contents:

:- module(cat, [move/3]).
:- multifile(move/3).

move(cat(C), P, cat(C2)) :-
   ...

Similarly for dog.prolog.

And main.prologwith the help of:

:- use_module(['cat.prolog'], [move/3]).
:- use_module(['dog.prolog'], [move/3]).

(various predicates that use move/3 and expecting the clauses from all imported modules to be applicable.)

Trying to run this in SWI-Prolog:

?- ['main.prolog'].
%  cat.prolog compiled into cat 0.00 sec, 4,800 bytes
ERROR: Cannot import dog:move/3 into module user: already imported from cat
Warning: /home/edmund/main.prolog:2:
        Goal (directive) failed: user:use_module([dog.prolog],[move/3])
% main.prolog compiled 0.00 sec, 10,176 bytes
true.

At this moment I can use dog:move/3and cat:move/3, but not move/3. It works for a case cat, but not for a case dog.

, . ...

+4
3

multifile/1 , ...

3 : pets.pl, cat.pl, dog.pl.

:- module(pets, [test/0, move/3]).
:- multifile move/3.
move(A,B,C) :- writeln(pets-move(A,B,C)).
test :- forall(move(A,B,C), writeln(move(A,B,C))).

:- module(cat, []).
:- use_module(pets).
pets:move(A,B,C) :- writeln(cat-move(A,B,C)).

:- module(dog, []).
:- use_module(pets).
pets:move(A,B,C) :- writeln(dog-move(A,B,C)).

Module:Pred :- ...

?- [cat,dog].
%  pets compiled into pets 0.00 sec, 3 clauses
% cat compiled into cat 0.01 sec, 7 clauses
% dog compiled into dog 0.00 sec, 3 clauses
true.

?- test.
Correct to: "pets:test"? yes
pets-move(_G41,_G42,_G43)
move(_G41,_G42,_G43)
cat-move(_G41,_G42,_G43)
move(_G41,_G42,_G43)
dog-move(_G41,_G42,_G43)
move(_G41,_G42,_G43)
true.

?- 
+5

, Logtalk Prolog, include/1:

----- common.pl -----
:- export(move/1).
---------------------

----- cat.pl -----
:- module(cat, []).
:- include(common).

move(cat).
---------------------

----- dog.pl -----
:- module(dog, []).
:- include(common).

move(dog).
---------------------

$ swipl
...
?- use_module(cat, []), use_module(dog, []).
% cat compiled into cat 0.00 sec, 4 clauses
% dog compiled into dog 0.00 sec, 4 clauses
true.

?- cat:move(X).
X = cat.

?- dog:move(X).
X = dog.

?- module_property(cat, exports(Exports)).
Exports = [move/1].

?- module_property(dog, exports(Exports)).
Exports = [move/1].

Logtalk, .

, Prolog, SWI-Prolog, , . : (1) ; (2) , , . , - , . , , , , consult/1 use_module/1 :

?- [cat, dog].
% cat compiled into cat 0.00 sec, 4 clauses
ERROR: import/1: No permission to import dog:move/1 into user (already imported from cat)
% dog compiled into dog 0.00 sec, 4 clauses
true.

, .

+4

, Logtalk (.. ) , move/3 , . Logtalk, Prolog, SWI-Prolog. :

:- protocol(pets).

    :- public(move/3).
    ...

:- end_protocol.


:- object(cat, implements(pets)).

    move(A, B, C) :-
        ...

:- end_object.


:- object(dog, implements(pets)).

    move(A, B, C) :-
        ...

:- end_object.

, , pets.lgt, cat.lgt dog.lgt :

$ swilgt
...
?- {pets, cat, dog}.
...
? - cat::move(A, B, C).
...

Logtalk , /, / , . , , , , .

+2
source

Source: https://habr.com/ru/post/1526284/


All Articles