How to create prolog scripts that use initialization/2

Hi :slightly_smiling_face:

I’m using: SWI-Prolog 8.4.1.

I’ve created a saved state with a logtalk library of my own and would like to be able to write prolog scripts that can use this library. Something like this :

#!/usr/bin/mylib_savedstate

:- initialization(main, main).

main(_) :- ... .

How may I do that, please ?

Not sure what you want. If you have a working Prolog program using initialization/2 to start it, simply do

swipl -o myprog -c myfile.pl

For example, the scasp executable is created using

swipl --no-pce --undefined=error -O -o scasp -c prolog/scasp/main.pl

Using --no-pce avoid including the graphics stuff into the executable. -O removes debug/3 and assertion/1 and optimizes (notably) arithmetic. --undefined=error causes it to fail if the code contains calls to undefined predicates.

For building as part of a Makefile, I have found these options to be useful:

--stand_alone=true 
--undefined=error 
--on_error=status 
--on_warning=status
--foreign=save
--verbose=false  % --verbose=true is useful for finding some errors
--no-pce         % if not using graphics

The full set of options are on the command line options page.

I don’t want to compile the script. On the contrary, I want my saved state (mylib_savedstate) acts as a new interpreter, interpreting the script. Because I’ve noticed a saved state runs as an interactive interpreter.

Note: mylib_savedstate is obtained like this

?- consult('logtalk.qlf').
true
?- consult('mylib.qlf').
true.
?- qsave_program('mylib_savedstate').
true.

Thanks a lot

If you save the program using the stand_alone(true) option it is a normal native executable, so you should be able to use it as #! interpreter. The normal saved state on POSIX systems is an sh script.

I’ve tried with stand_alone(true) option. The created file is an ELF executable but it doesn’t change the game :
invoked with !#, the saved state is actually initialized but the script of the user is not taken into account. In place, an interactive interpreter runs. :frowning_with_open_mouth:

I’ve tried this too :

swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.4.1)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- qsave_program(dummy, [stand_alone(true)]).
% Disabled autoloading (loaded 45 files)
% Disabled autoloading (loaded 0 files)
true.

?- ^D
% halt
#!/home/didier/collect/dummy

:- initialization(main, main).

main(_) :- writeln("Main execution").

Same thing.

I see. Yes, normal saved states do not process the Prolog commandline arguments, passing all arguments to the application. This means you have two options. Reimplement the commandline processing in the main goal of the saved state or use class(development). From the commandline I used for testing:

swipl -o interp --no-pce --stand-alone --class=development -c interp.pl

That seems to work fine.

1 Like

Thank you a lot.
I’m going to try this. :+1:

Good evening, Jan