Saturday, 2 February 2019

Prolog: Hello World Program


Step 1: Open any text editior and create HelloWorld.pl with below content

HelloWorld.pl
/*Prolog Hello World Program*/
hello :- write('Welcome to prolog programming!!!Have fun!!!').

Step 2: Open the terminal or command prompt. Go to the directory where your prolog file (HelloWorld.pl) file is located.


Step 3: Execute the command ‘swipl’.
C:\Users\Public\prologExamples>swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 7.6.4)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

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

1 ?-


Load the prolog file by executing the statement.

1 ?- ['HelloWorld.pl'].
true.


Execute the statement ‘hello.’ To get the output in console.

2 ?- hello.
Welcome to prolog programming!!!Have fun!!!
true.



Previous                                                 Next                                                 Home

Setting up prolog


Install latest prolog stable version from below location.

After installation set the swi-prolog bin path to your system path.

In case of windows, you should set the path ‘C:\Program Files\swipl\bin’ to system path.

Test prolog installation
Open command prompt or terminal and execute the command ‘swipl’.

If you see below kind of output, your installation is successful.

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

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

1 ?-


Previous                                                 Next                                                 Home

Introduction to prolog


Prolog is a declarative programming language. It is mainly developed for AI (Artificial intelligence) programming.

A prolog application maintains number of rules and facts, and derive the solution using the specified rules and facts.

How to specify the facts?
You can specify the facts like below.

greater(10, 6)
Above statement says 10 is greater than 6

bigger(Elephant, Ant)
Above statement says Elephant is bigger than Ant.

What are the three basic constructs in Prolog?
a.   Facts
b.   Rules
c.    Queries

How a Prolog program looks like?
A prolog program is a set of clauses, where each clause is either a fact (or) rule (A rule define the relationship between two objects).

For example, a rule can be something like below.
a.   Person ‘x’ is taller than person ‘y’, if person x’s height is more than person y’s height.
b.   A person is eligible to vote, if his age is > 18.
c.    Two persons are brothers, if they are male and their parents are same



Previous                                                 Next                                                 Home

Prolog tutorial


Introduction to prolog
Setting up prolog
Hello World Program
Basic constructs in Prolog
How to create facts in prolog?
Prolog: Variables
Unification
Conjunctions
Rules
Prolog program
Prolog: Constants
Prolog: Variables
Prolog: Structures
Unifying operator (=)
Arithmetic Operators
Convert centigrade to farenheit
Prolog: =:= (equal to), =\= (not equal to) operators
Prolog: Example Program with players information
Get the population of cities in a country
Prolog: //: Get integer quotient on division
Prolog: mod: Get the reminder
Lists
Prolog: Check for existence of an element in the list
Get size or length of the list
Prolog: Check whether given element is list or not
Prolog: Why to define facts before rules
Prolog: convert list of characters to list of integers
Prolog: Append elements of a list
Previous                                                 Next                                                 Home