Search code examples
prolog

How to make the directive "initialization" use ; in Prolog?


So I have a predicate named confirm.

This predicate has 3 possible shapes:

confirm([ ]):-!.
confirm():-
confirm([arc(X,Y)|T]) :-

I want this predicate to auto-activate when I execute my file. I have read that in order to achieve this, I need to use the directive initialization.

So, first I execute my file with the interpreter:

myName@myName-VirtualBox:~/dir/$ swipl fileONE.pl

Then inside my code, I have this:

:- initialization(confirm).

The thing is it only works partially... Let me explain.

I have some graph structure in ANOTHER file - let's call it fileTWO.pl. The graphs are of the form:

:- dynamic arc/2.
:- dynamic graphe/2.

graph(g1,[arc(x,y),arc(x2,y2),arc(x3,y3)...]).
graph(g2,[arc(a,b),arc(a2,b2),arc(a3,b3)...]).
graph(g3,[arc(a,b),arc(a2,b2),arc(a3,b3)...]).
...

Basically, the confirm predicate asserts the arcs into my "database". If I have 3 graphs, like in the example above (g1, g2, g3), then I need to use ; when using confirm in order to add all the graphs.

So here is how I would use my predicate confirm manually:

?- confirm().
true ;
true ;
true .

And that's exactly what I want.

So here is my problem. When I use the directive initialization, it only does the first true, if I can say. So only my first graph would be asserted.

Therefore my question is how to get a predicate to execute at the launch of my file and execute every "instance" or every "cycle" (I am not sure how to say).


Solution

  • One solution is to use instead:

    :- initialization((confirm,fail;true)).
    

    I.e. a failure-driven loop as the initialization goal.