Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hello world in Prolog

Tags:

I'm tearing my hair out trying to find how to just write a Hello World program in Prolog. I just want to create a program that runs like so:

> ./hw Hello, world! > 

The problem is that every single example I can find works in a REPL, like so:

?- consult(hello_world). % hello compiled 0.00 sec, 612 bytes  Yes ?- hello_world. Hello World!  Yes 

This is the same even with examples of compiled Prolog: the program still just drops into a REPL. This is obviously not much use for a "general-purpose" language. So, how do I write the traditional Hello World?

like image 299
jameshfisher Avatar asked Aug 26 '10 14:08

jameshfisher


People also ask

How do you program a Prolog?

To create a program in Prolog, the simple way is to type it into the text editor and then save it as a text file like prolog1.pl. The following example shows a simple program of Prolog. The program contains three components, which are known as clauses. Each clause is terminated using a full stop.

What is the full form of Swipl?

Its main author is Jan Wielemaker. The name SWI is derived from Sociaal-Wetenschappelijke Informatica ("Social Science Informatics"), the former name of the group at the University of Amsterdam, where Wielemaker is employed.


2 Answers

Using GNU Prolog:

 $ cat hello.pl  :- initialization(main). main :- write('Hello World!'), nl, halt.

$ gplc hello.pl $ ./hello
Hello World!

like image 50
Greg Buchholz Avatar answered Oct 08 '22 22:10

Greg Buchholz


You can write your source file to both launch the Prolog interpreter and to quit it when your code is done running. Here is an example using SWI-Prolog:

#!/usr/bin/swipl -q -t hello_world -f  hello_world :- write('Hello World'), nl,                 halt. 

Assuming you put this in a file named 'hw', and set the executable permission, you can call it like you want to:

$ ./hw Hello World $ 
like image 25
Jeff Dallien Avatar answered Oct 09 '22 00:10

Jeff Dallien