Is there a way to just create a prolog script called hello.pl
like this:
#!/usr/local/bin/swipl -q -s -t main
main:-
write('Hello World\n').
And be able to run it from the terminal like this?
$ hello.pl
Hello World
$
When I do that it gives me this:
hello.pl: line 3: main:-: command not found
hello.pl: line 4: syntax error near unexpected token `'Hello World\n''
hello.pl: line 4: ` write('Hello World\n').'
I am able to get it working by writing this on the command line:
$ swipl -q -f hello.pl -t main
Hello World
$
But is there a way to just run the straight script as an executable instead?
Edit
Haven't yet been able to get this to work. Here is the output from the commands @Boris asked in the comments in his answer:
$ ls -l
total 8
-rwxr-xr-x 1 viatropos staff 235 Aug 26 20:28 example.pl
$ cat example.pl
#!/usr/local/bin/swipl
:- set_prolog_flag(verbose, silent).
:- initialization main.
main :-
format('Example script~n'),
current_prolog_flag(argv, Argv),
format('Called with ~q~n', [Argv]),
halt.
main :-
halt(1).
$ which swipl
/usr/local/bin/swipl
$ swipl --version
SWI-Prolog version 6.6.6 for x86_64-darwin13.1.0
$ ./example.pl
./example.pl: line 3: syntax error near unexpected token `('
./example.pl: line 3: `:- set_prolog_flag(verbose, silent).'
$
I am on Mac OSX 10.9.2, and installed swipl with homebrew via brew install swi-prolog --with-libarchive
On Windows, the . pl extension is associated with swipl-win.exe and most comfortable way is to double-click the . pl file you want to load in the explorer. This will start SWI-Prolog, which changes directory to the directory holding the file and then loads the clicked file.
Follow the below steps to install SWI Prolog on Windows: Step 1: Visit swi-prolog.org website using any web browser. Step 2: Click on Download which is adjacent to Home, dropdown list will appear then click on SWI-Prolog. Step 3: New webpage will open, click on Stable release.
To run tests from the Prolog prompt, first load the program and then run run_tests/0 or run_tests(+Unit) . Run all test-units. Run only the specified tests. Spec can be a list to run multiple tests.
Then check your installation, call SWI-Prolog from a terminal shell as follows: - In the shell, type in …> ./swipl which should open the interpreter, i.e., you should see something like … ?- If you get an error that the command swipl is not known, check that your PATH variable includes a path to SWI-Prolog.
ISO directive: initialization. This should work.
:- initialization main.
main :-
write('Hello World\n').
edit sorry, I skipped over most interesting details. Here is a sample script, let's say saved in ~/test/main.pl
#!/home/carlo/bin/swipl -f -q
:- initialization main.
main :-
current_prolog_flag(argv, Argv),
format('Hello World, argv:~w\n', [Argv]),
halt(0).
and made executable with
chmod +x ~/test/main.pl
then I get
~$ ~/test/main.pl
Hello World, argv:[]
~$ ~/test/main.pl as,dnj asdl
Hello World, argv:[as,dnj,asdl]
In script main.pl
, I used the swipl path that results from building from source without admin privileges. The SWI-Prolog build process put bin and lib under ~/bin and ~/lib
Note: the -f flag disables loading the initialization ~/.plrc, and this could be necessary to get more 'strict control' over execution...
I'm currently unsure if the documentation page is up-to-date with current SW status. From some mailing list message, and my own efforts to reuse thea, seems that command line flags changed recently...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With