Search code examples
moduleprologoperator-keyword

SWI-Prolog: Use operators from modules?


I have a prolog-file (name definitions.pl) of this kind of type:

:- module(definitions,[]).

:-op(699, xfx, :=).
:-op(599, xfy, ∪).



C := (A ∪ B) :- union(A, B, C).

My aim is to use this file as module in another file test.pl. To integrate it I tried:

:-use_module(definitions). 

But for some reason its not possible to make statements like:

X:=[1,2]∪[3,4]. 

after loading test.pl into swipl. I also tried:

:- module(definitions,[:=/2, ∪/2]).

:-op(699, xfx, :=).
:-op(599, xfy, ∪).

but this gives some operator exprected error. What is the correct way to use operators in prolog-modules?


Solution

  • Move the operator definitions into the module export list:

    :- module(definitions, [
        (:=)/2, op(699, xfx, :=),
        op(599, xfy, ∪)
    ]).
    
    :- use_module(library(lists), [union/3]).
    
    C := (A ∪ B) :-
        union(A, B, C).
    

    Usage example (assuming a definitions.pl file with the above contents in the current working directory):

    ?- use_module(definitions).
    true.
    
    ?- X:=[1,2]∪[3,4].
    X = [1, 2, 3, 4].