Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export dynamically created predicate?

Consider the following code:

:- module(my_export, [create/2]).

create(Predicate, Id) :-
    assertz(Predicate),
    export(Id).

Assuming that the predicated and the identifier match, I'd expect the newly asserted predicate to be available outside the module. However, I get this output:

?- create(c(A,B) :- A > B, c/2).
true.

?- c(1,2).
Correct to: "my_export:c(1,2)"? 

How come the predicate isn't exported? What is the correct way to do this?

like image 807
Steven Avatar asked Mar 05 '26 12:03

Steven


1 Answers

You have to import module by using use_module/1.

For example, if this is sample_module.pl:

:- module(my_export, [create/2]).

create(Predicate, Id) :-
    assertz(Predicate),
    export(Id).

Then this input and output is true (observe closely what's going on):

?- create(tmp(A,B) :- A > B, tmp/2).
ERROR: toplevel: Undefined procedure: create/2 (DWIM could not correct goal)

?- consult('c:\\Prolog\\pl\\bin\\sample_module.pl').
% c:\Prolog\pl\bin\sample_module.pl compiled into my_export 0.00 sec, 2 clauses
true.

?- create(tmp(A,B) :- A > B, tmp/2).
true.

?- tmp(1,2).
Correct to: "my_export:tmp(1,2)"? yes
false.

?- use_module('c:\\Prolog\\pl\\bin\\sample_module.pl').
true.

?- tmp(1,2).
false.

?- tmp(5,4).
true.

Now, when you "compile buffer" in SWI-Prolog what really happens is consult/1. You need to import your module manually.

like image 134
Grzegorz Adam Kowalski Avatar answered Mar 07 '26 03:03

Grzegorz Adam Kowalski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!