Example .exe compiled by swi-prolog that can take arguments
I would be grateful for your help in the fight in 4 hours:
I need to create an exe file (in windows) from a prologue script. For example, main.pl has inside:
day(monday).
day(tuesday).
day(wednesday).
day(thursday).
day(friday). % let stop here
I would like to compile this script, produce prog.exe and then do the following runs:
$ prog.exe --term sunday
false
$ prog.exe --term monday
true
$ prog.exe --goal day(friday)
true
$ prog.exe --goal fun(foo)
false
If the flags are complex no flag version with input targets will be very helpful for me as well.
I tried to read the compilation of the pages on the swi-proog page but got confused. I can't print anything to stdout. Also I didn't understand how flags work.
tried the example they have on the swi-prolog site, but I don't understand why nothing is printed. With below script I can create exe file with save (prog) command but then while running prog.exe nothing is printed.
:- ['main'].
main :-
pce_main_loop(main).
main(Argv) :-
write('hello word').
save(Exe) :-
pce_autoload_all,
pce_autoload_all,
qsave_program(Exe,
[ emulator(swi('bin/xpce-stub.exe')),
stand_alone(true),
goal(main)
]).
source to share
I will link to eighth_puzzle.pl, a module I posted for another answer as a test case. Then I write a new file (say p8.pl) using the test argument string, compile and run
:- use_module(eight_puzzle).
go :-
current_prolog_flag(argv, Argv),
writeln(argv:Argv),
( nth1(Test_id_flag, Argv, '--test_id'),
Test_id_pos is Test_id_flag+1,
nth1(Test_id_pos, Argv, Id)
-> atom_concat(test, Id, Func)
; Func = test1
),
forall(time(call(eight_puzzle:Func, R)), writeln(R)).
for compilation I used documentation section 2.10.2.4 of 2.10 Compilation
swipl -O --goal=go --stand_alone=true -o p8 -c p8.pl
and to run with the specified option:
./p8 --test_id 0
I am running Ubuntu, but there should be no difference on Windows.
argv:[./p8,--test_id,0]
% 4,757 inferences, 0.003 CPU in 0.003 seconds (100% CPU, 1865842 Lips)
[4,3,6,7,8]
% 9,970 inferences, 0.005 CPU in 0.005 seconds (100% CPU, 2065656 Lips)
[4,3,6,7,4,5,8,7,4,5,8,7,4,5,8]
...
NTN
source to share
SWI-Prolog contains a library optparse
that you can use to parse command line arguments.
opts_spec(
[ [opt(day), type(atom),
shortflags([d]), longflags(['term', 'day']),
help('name of day')]
, [opt(goal),
shortflags([g]), longflags([goal]),
help('goal to be called')]
]
).
% days
day(monday).
day(tuesday).
day(wednesday).
day(thursday).
day(friday).
main :-
opts_spec(OptsSpec),
opt_arguments(OptsSpec, Opts, _PositionalArgs),
writeln(Opts),
memberchk(day(Day), Opts),
memberchk(goal(Goal), Opts),
(nonvar(Day) -> call_call(day(Day), Result), writeln(Result) ; true),
(nonvar(Goal) -> call_call(Goal, Result), writeln(Result) ; true),
halt.
call_call(Goal, true) :-
call(Goal), !.
call_call(_Goal, false).
You can compile and use the above code as follows. (Tested on Ubuntu only, sorry, I can't help with regards to Windows.)
$ swipl -o day.exe -g main -c day.pl
$ ./day.exe --term monday
[goal(_G997),day(monday)]
true
$ ./day.exe --goal "day(sunday)"
[day(_G1009),goal(day(sunday))]
false
source to share