Importing two (or more) modules which have some predicates with the same name

View: New views
6 Messages — Rating Filter:   Alert me  

Importing two (or more) modules which have some predicates with the same name

by leledumbo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Either I don't understand the explanation in the docs or what, one thing for sure I can't do it correctly. I have:

% module queue
:- module(queue,[empty/1,enqueue/3,dequeue/3,member/2,add_list/3]).

% module set
:- module(set,[empty/1,member/3,member2/3,delete_if_exists/3,add_if_not_exists/3,
          union/3,subset/2,intersection/3,diff/3,equal/2]).

% main module
:- use_module([queue,set]).

when the main module is consulted, swi-prolog says:
ERROR: Cannot import set:empty/1 into module user: already imported from queue

The definition are equal (i.e.: empty([]).), could that be the problem? Should it be possible to call each with a qualified name?

Re: Importing two (or more) modules which have some predicates with the same name

by Eva Stoewe :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


> Either I don't understand the explanation in the docs or what, one thing
> for
> sure I can't do it correctly. I have:
>
> % module queue
> :- module(queue,[empty/1,enqueue/3,dequeue/3,member/2,add_list/3]).
>
> % module set
> :-
> module(set,[empty/1,member/3,member2/3,delete_if_exists/3,add_if_not_exists/3,
>           union/3,subset/2,intersection/3,diff/3,equal/2]).
>
> % main module
> :- use_module([queue,set]).
>
> when the main module is consulted, swi-prolog says:
> ERROR: Cannot import set:empty/1 into module user: already imported from
> queue
>
> The definition are equal (i.e.: empty([]).), could that be the problem?
> Should it be possible to call each with a qualified name?

You are not allowed to have two different predicates with the same name in one module. When you use use_module/1 every predicate from those modules (queue, set) that are exported by them become also predicates of the loading module. It does not matter if the definitions are the same.

But there is a way to help you here. You can rename the predicates while loading with use_module(+File, +ImportList).

A completely other way is to make a predicate multifile but I do not think that that is your intention.

Cheers,
  Eva Stöwe.
--
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser
_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: Importing two (or more) modules which have some predicates with the same name

by Paulo Moura :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 2009/10/13, at 14:45, leledumbo wrote:

> Either I don't understand the explanation in the docs or what, one  
> thing for
> sure I can't do it correctly. I have:
>
> % module queue
> :- module(queue,[empty/1,enqueue/3,dequeue/3,member/2,add_list/3]).
>
> % module set
> :-
> module(set,[empty/1,member/3,member2/3,delete_if_exists/
> 3,add_if_not_exists/3,
>          union/3,subset/2,intersection/3,diff/3,equal/2]).
>
> % main module
> :- use_module([queue,set]).
>
> when the main module is consulted, swi-prolog says:
> ERROR: Cannot import set:empty/1 into module user: already imported  
> from
> queue
>
> The definition are equal (i.e.: empty([]).), could that be the  
> problem?

No. You're getting an error because the use_module/1 directive both  
loads the module files and imports their exported predicates. The  
predicates are imported into the current module. For example, if you  
type the query "use_module([queue,set])." at the Prolog prompt, the  
current module is usually the module "user". Thus, the error results  
from trying to import predicates with the same name into the same  
module.

> Should it be possible to call each with a qualified name?


Depends. Do you want instead to load the two module files without auto-
importing their exported predicates?

Cheers,

Paulo


-----------------------------------------------------------------
Paulo Jorge Lopes de Moura, PhD
Assistant Professor
Dep. of Computer Science, University of Beira Interior
6201-001 Covilhã, Portugal

Office 3.18  Ext. 3276
Phone: +351 275319891 Fax: +351 275319899
Email: <mailto:pmoura@...>

Home page: <http://www.di.ubi.pt/~pmoura>
Research:  <http://logtalk.org/> Blog: <http://blog.logtalk.org/>
-----------------------------------------------------------------







_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: Importing two (or more) modules which have some predicates with the same name

by leledumbo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Depends. Do you want instead to load the two module files without auto-
> importing their exported predicates?

Yes, I want to call all the predicates with qualified names, treating them as singleton objects.

Re: Importing two (or more) modules which have some predicates with the same name

by Jan Wielemaker-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wednesday 14 October 2009 03:25:24 pm leledumbo wrote:
> > Depends. Do you want instead to load the two module files without auto-
> > importing their exported predicates?
>
> Yes, I want to call all the predicates with qualified names, treating them
> as singleton objects.

In that case the fix is easy:

:- use_module(m1, []).
:- use_module(m2, []).

        Cheers --- Jan

_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: Importing two (or more) modules which have some predicates with the same name

by leledumbo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> In that case the fix is easy:
>
> :- use_module(m1, []).
> :- use_module(m2, []).

Thanks. I should've read use_module/2 more carefully.