0% found this document useful (0 votes)
8 views15 pages

Prolog Programming Basics and Examples

The document provides an introduction to Prolog, highlighting its differences from procedural programming and its applications in fields such as AI, expert systems, and natural language processing. It discusses the features and limitations of Turbo Prolog, including its advantages as a true compiler and its support for various data types and structures. Additionally, it includes examples of Prolog programming concepts such as lists, unification, and common predicates.

Uploaded by

kageles706
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views15 pages

Prolog Programming Basics and Examples

The document provides an introduction to Prolog, highlighting its differences from procedural programming and its applications in fields such as AI, expert systems, and natural language processing. It discusses the features and limitations of Turbo Prolog, including its advantages as a true compiler and its support for various data types and structures. Additionally, it includes examples of Prolog programming concepts such as lists, unification, and common predicates.

Uploaded by

kageles706
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Prolog

Reference Book :
“Introduction to Turbo Prolog” by Carl
Townsend
Some facts about the language
• The name Prolog is taken from the phrase
“Programming in logic”

• How is Prolog different from procedural


programming ?
– Combines logic , data and relationships between data
in its program
– Suitable for recursion intensive, AI applications
– Prolog programs are created by Knowledge Engineers
rather than just programmers
Applications of Prolog
• Expert Systems
• Natural Language Processing
• Robotics
• Gaming
• Simulation
Features of Turbo Prolog
Advantages of using Prolog :
• It is a true compiler. It can generate stand-alone programs that can
execute on a machine not running Turbo Prolog.
• Standard predicates for many functions such as string operations, random
file access, cursor control, graphics, windows and sound is available.
• Procedural language support can be added to a Prolog system through
functional interface
• Both integer and real arithmetic is supported.
• An integrated editor is provided, so that program development, debugging
and compilation is easy
Limitations of Prolog :
• Not very efficient for numerical processing
• All data structures used in conventional procedural programming are not
available with Prolog
Domain types in Prolog
char : single character (enclosed in ‘ ’)
integer : integer from -32768 to 32767
real : floating point number
string : character sequence (enclosed in “ “)
symbol : character sequence of letters, numbers and underscores with the
first character a lowercase letter
file : symbolic file name
An Example
domains
disease, indication=symbol
predicates
symptom(disease, indication)
clauses
symptom(malaria, high_fever).
symptom(malaria, body_ache).
symptom(flu, runny_nose).
symptom(flu, mild_fever).
symptom(flu, body_ache).
Variable in Prolog
• A variable name in Prolog must begin with a
capital letter and may be from 1 to 250
characters long.

• Except for the first character in the variable


name, the rest may be uppercase or
lowercase letters, digits or the underscore.
Lists in Prolog
• A List is a powerful data structure in Prolog
• A list is an ordered sequence of terms.
• Components of a list should be of the same domain type.
The components could be integer, real, string, symbol or
other lists.

• Example of a list :
[Juhi, Ekta, Mohit, Priyanka, Dhruv]
The above list is different from the following, since order of
terms is not the same.
[Ekta, Juhi, Mohit, Dhruv, Priyanka]
Unification
• Prolog divides each list into a Head and a Tail.
• A Head is the first element of a list and Tail is the rest of the elements.

• For example,

stud_list([Juhi, Ekta, Mohit, Priyanka, Dhruv])

is the same as :

stud_list([Juhi |[Ekta, Mohit, Priyanka, Dhruv]])


in which Prolog binds Head to ‘Juhi’ and
Tail to ‘’ [Ekta, Mohit, Priyanka, Dhruv]”
Example of Unification using Lists
domains
sportlist = sports*
sport = symbol
predicates
sport_names (sportlist)
clauses
sport_names([cricket, hockey, baseball, football, tennis])

Now,
Goal : sport_names(A) returns A= [“cricket”, “hockey”, “baseball”, “football”, “tennis”]

Goal : sport_names(A|B) returns A= cricket


B=[“hockey”, “baseball”, “football”, “tennis”]

Goal :sport_names(A, B|C) returns


A=cricket
B=hockey
C= [“baseball”, “football”, “tennis”]
Example : Appending a list
program “[Link]”
domains
list=integer *
predicates
append(list,list,list)
clauses
append([],L,L).
append([H|T1],L2, [H|T3]) :-
append(T1, L2, T3).
Example : Reversing a list
Program: “[Link]”
Include “c:\[Link]”
predicates
reverse(list, list)
clauses
reverse([],[]).
reverse([X|Y],Z):-
reverse(Y,W) ,
append(W,[X],Z).
Find the last element of a list
program : “[Link]”
domains
list=integer*
predicates
findlast(list,list)
clauses
findlast([H],X):-
X=[H].
findlast([_|T], X):-
findlast(T,X).
Finding the nth element of a list
Program : “[Link]”
domains
list=integer*
predicates
nele(list,integer,list)
clauses
nele([H|_],1,X):-
X=[H].
nele([_|T],N,X):-
N1=N-1,
nele(T,N1,X).

You might also like