How to use a predicate using the same name from multiple modules in Prolog

I am new to Prolog and I am struggling with the following issue using SWI-Prolog. I have several files dataBase1.pl, dataBase2.pl... with the same structure (on the basis of the flow )

:- module(dataBase1,[]).

:- use_module(library(persistency)).

:- persistent 
       predicate1(A:any, B:any),
       predicate2(A:any, B:any).

:- initialization(init).

init :-
        absolute_file_name('dataBase1.db', File, [access(write)]),
        db_attach(File, []).

predicate1/2, predicate2/2 are common to all database files.

Then I defined predicates.plseveral sentences in the third file that used sentences in previous databases, such astestPredicate(A,B) :- predicate1(A,B), predicate2(A,B).

My problem is that I would like the sentence above to use predicate1/2, predicate2/2from all the modules corresponding to the database files. In the current state, I need to clarify the context module in order to use predicate1/2, predicate2/2(i.e dataBase1:predicate1/2, dataBase2:predicate1/2,.....)

use_module/1, / .

!

: , head(X,Y) :- body() ?

+7
2

Prolog , . " ". , Logtalk ( Logtalk Prolog, SWI-Prolog).

, :

:- object(database).

    :- public([predicate1/2, predicate2/2]).
    :- dynamic([predicate1/2, predicate2/2]).

:- end_object.

, . /, include/1 , . :

:- object(db1, extends(database)).

    :- include('db1.db').

:- end_object.


:- object(db2, extends(database)).

    :- include('db2.db').

:- end_object.

:

...,
% ensure the corresponding file exists
open(write, 'db42.db', Stream),
close(Stream),
% create the dynamic database object    
create_object(db42, [extends(database)] [include('db42.db')], []),
...

, :

.pl , , testPredicate (A, B): - 1 (, ), 2 (, ).

, testPredicate/2 , :

:- object(database).

    :- public([predicate1/2, predicate2/2]).

    :- public(testPredicate/2).
    testPredicate(A,B) :-
        ::predicate1(A,B),
        ::predicate2(A,B).

:- end_object.

:: /1 - Logtalk . , , :

?- db1::testPredicate(A,B).

predicate1/2 predicate2/2 db1 :

?- db2::testPredicate(A,B).

predicate1/2 predicate2/2 db2. , assert retract . :

?- db42::assertz(predicate1(foo,bar)).
...

, . , , . ( , ):

    :- public(save/0).
    save :-
        this(This),
        forall(
            extends_object(Database, This),
            save(This)
        ).

    save(Database) :-
        atom_concat(Database, '.db', File),
        open(File, write, Stream),
        save_predicates(Database, Stream),
        close(Stream).

    save_predicates(Database, Stream) :-
        current_predicate(Functor/Arity),
        functor(Template, Functor, Arity),
        predicate_property(Template, (dynamic)),
        write_canonical(Stream, (:- dynamic(Functor/Arity))), write('.\n),
        Database::clause(Template, true),
        write_canonical(Stream, Template), write('.\n),
        fail.
    save_predicates(_, _).

, database::save. , . Logtalk Prolog.

0

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


All Articles