I have just written the following "hello world" program in Prolog. Although it is as simple as it can be, I would like your feedback as Prolog is very new to me.

#!/usr/bin/swipl -q -t main -f

main :-
    write("Hello world!"), nl, fail.


  1. Shebang: I am used to use #!/usr/bin/env python in Python. This is the prefered method, because it does not rely (so much) on a specific system.
  2. write: it seems as if I could also use format. Are there other options?
  3. Style guide: I have not found any style guide regarding indentation or spaces after ,.
  4. Is quitting with fail to usual way? I have seen halt, too.
  5. File ending: I saved it as helloworld.pro. What file extensions are common?

As the code is short and I have difficulties with even getting that to work with Prolog, I would admire it if you could add the complete code when you make suggestions.

  • \$\begingroup\$ regarding prolog file extension: .pl is commonly used \$\endgroup\$
    – tim
    Aug 6, 2014 at 17:30
  • \$\begingroup\$ IF you would like to execute main in the prolog file, simply put ?- main. at the end. \$\endgroup\$
    – C.B.
    Aug 6, 2014 at 18:01
  • \$\begingroup\$ Your edits should not be part of your question. They are responses to other answers and should be comments on those answers. It has caused someone to mistakenly think your code does not work. \$\endgroup\$
    – RubberDuck
    Aug 11, 2014 at 22:06

1 Answer 1


Why fail at all? Isn't success better than failure? A shorter version is thus:

main :- writeln('Hello world!').

Also, why even have side effects? Isn't a declarative solution much better:

message('Hello World').

This way, you can also reason about the message within the program itself. This is not possible if it only appears as output on the terminal. You can ask on the toplevel whether there is any message:

?- message(M).
M = 'Hello World'.

File endings and shebang-lines are technicalities that are pretty unrelated to the program itself. A Prolog program that is intended as a shell script will often have an initialization/1 directive somewhere in the file, like:

:- initialization(run).

This is a standardised feature, whereas placing arbitrary queries in the file is not.

  • 1
    \$\begingroup\$ I've edited my answer to use single quotes instead of double quotes, so that you get the atom 'Hello World' instead of a list of character codes. \$\endgroup\$
    – mat
    Aug 11, 2014 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.