Lab 11: Introduction to Prolog
Objectives
Introducing Prolog and designing and implementing Prolog programs that deal with:
Facts
Rules
Constants & Variables
Sample Programs
1.
What is Prolog?
Prolog (programming in logic) is one of the most widely used programming languages in artificial intelligence research.
High-level interactive language.
Logic programming language.
Prolog is a declarative language.
This means that given the necessary facts and rules, prolog will use deductive reasoning to solve your programming problems.
Prolog is “intelligent.”
Prolog has a built-in inference engine performing a top-down, left to right search in fact and rules to find solutions.
2.
What is Prolog used for?
Good at
Grammars and Language processing,
Artificial intelligence,
Knowledge representation and reasoning,
Expert System,
Pattern matching,
Smart information management systems.
Problem solving
Poor at
Numerical algorithms.
Representing complex data structures,
Inpu t/Output (interfaces).
3.
Prolog in English
(Facts, Rules, Questions)Example Database:
[Facts]
Tom is a male. John is a male. Ann is a female. Mary is a female. Tom is a parent of John. Tom is a parent of Mary. Ann is a parent of John. Ann is a parent of Mary. [Rules]
Someone is the father of a person if that someone is a parent of the person and that someone is a male.
Someone is the mother of a person if that someone is a parent of the person and that someone is a female
Example questions:
Who is John's father? Is Tom the parent of Marry? Is Ann is the mother Marry?
4.
Prolog in Prolog
PREDICATES
nondeterm male(symbol) nondeterm female(symbol)
nondeterm parent(symbol,symbol) nondeterm father(symbol,symbol) nondeterm mother(symbol,symbol) CLAUSES
parent(X,Y),male(X).
parent(X,Y),female(X). GOAL
father(X,john), parent(tom,mary), mother(ann,mary).
5.
Constants and Variables
Constants can either be: Numbers:
integers are the usual form (e.g. 1, 0, -1, etc), but floating-point numbers can also be used (e.g. 3.0E7)
Symbolic (non-numeric) constants:
always start with a lower case alphabetic character and contain any mixture of letters, digits, and underscores (but no spaces, punctuation, or an initial capital).
e.g. abc, big_long_constant, x4_3t).
String constants:
are anything between single quotes e.g. ‘Like this’.
Variables:
Variables always start with an upper case alphabetic character or an underscore.
Other than the first character they can be made up of any mixture of letters, digits, and underscores.
e.g. X, ABC, _89two5, _very_long_variable
There are no “types” for variables (or constants) – a variable can take any value. All Prolog variables have a “local” scope:
6.
Visual Prolog
Program StructureA Visual Prolog program has the following basic structure: DOMAINS
/* ...
domain declarations ... */
PREDICATES /* ...
predicate declarations ... */
CLAUSES /* ...
clauses (rules and facts) ... */
GOAL /* ... subgoal_1, subgoal_2, etc. */
7.
Clauses
The clauses section is where you put the facts and rules that Visual Prolog will operate on when trying to satisfy the program's goal.
8.
Predicate Declaration
The predicates section is where you declare your predicates and the domains (types) of the arguments to your predicates. Predicate names must begin with a letter (preferably lower-case), followed by a sequence of letters, digits, and underscores, up to 250 characters long. You can't use spaces, the minus sign, asterisks, or slashes in predicate names. Predicate declarations are of the form
PREDICATES
predicateName(argumentType1, argumentType2, ..., argumentTypeN)
argumentType1,..., argumentTypeN are either standard domains or domains that you've declared in the domains section. Declaring the domain of an argument and defining the argument's type are the same things.
The domains section is where you declare any nonstandard domains you're using for the arguments to your predicates. Domains in Prolog are like types in other languages. Visual Prolog's basic standard domains are char, byte, short, ushort, word, integer, unsigned, long, ulong, dword, real,
string, and symbol; the more specialized standard domains are covered in other chapters. The basic domain declarations are of the form
DOMAINS
argumentType1,..., argumentTypeN = <standardDomain> Compound domain declarations are of the form:
argumentType_1,..., argumentType_N = <compoundDomain_1>; <compoundDomain_2>;
< ... >;
<compoundDomain_M>;
10.
Goal
The goal section is where you put your program's goal (in PDC1 Prolog we also used here term internal goal); this allows the program to be compiled, built and run as standalone executable
independent of the Visual Development Environment. In standalone executables, Visual Prolog only searches for the first solution for the program goal, and the values to which goal variables are bound are not displayed.
Some Prolog environments (for instance, the old PDC Prolog environment) support, so called
external goals (as counterpart to term internal goal). When the PDC Prolog environment runs a program that does not contain an internal goal, then the environment displays the special dialog in which you can enter an external goal at run time. With an external goal, Prolog searches for all goal solutions, and displays the values to which goal variables are bound. Visual Prolog's Visual
Development Environment does not support external goals. However, for simple programs (like most examples in this Language Tutorial) you can use the special Visual Development
Environment's utility Test Goal. The Test Goal searches for all solutions for the goal, and displays the values to which the goal variables are bound.
11.
Arity
The arity of a predicate is the number of arguments that it takes; two predicates can have the same name but different arity. You must group a predicate's different arity versions together in both the
predicates and clauses sections, but different arities are treated as completely different predicates.
NOTE: The :- ("if") in Prolog should not be confused with the IF used in other languages; a Prolog rule is in the form of a then/if conditional, while IF statements in other languages are in the form of an if/then conditional.
Prolog Program 1
PREDICATES
nondeterm likes(symbol,symbol) CLAUSES
likes(ellen,tennis). likes(john,football). likes(tom,baseball). likes(eric,swimming). likes(mark,tennis). likes(bill,Activity):-
likes(tom, Activity). GOAL
likes(bill, baseball)
Prolog Program 2
PREDICATES
phone_number(symbol,symbol) CLAUSES
phone_number("Albert","EZY-3665"). phone_number("Betty","555-5233"). phone_number("Carol","909-1010"). phone_number("Dorothy","438-8400"). Goal:
phone_number("Carol", Number). %phone_number(Who, "438-8400"). %phone_number("Albert", Number). %phone_number(Who, Number).
Prolog Program 3
PREDICATES
car(symbol,long,integer,symbol,long) truck(symbol,long,integer,symbol,long)
CLAUSES
car(chrysler,130000,3,red,12000). car(ford,90000,4,gray,25000). car(datsun,8000,1,red,30000). truck(ford,80000,6,blue,8000). truck(datsun,50000,5,orange,20000). truck(toyota,25000,2,black,25000).
vehicle(Make,Odometer,Age,Color,Price):-car(Make,Odometer,Age,Color,Price); truck(Make,Odometer,Age,Color,Price). GOAL
car(Make, Odometer, Years_on_road, Body, 25000).
Prolog Program 4
PREDICATES
nondeterm can_buy(symbol, symbol) nondeterm person(symbol)
nondeterm car(symbol) likes(symbol, symbol) for_sale(symbol) CLAUSES
can_buy(X,Y):- person(X), car(Y), likes(X,Y), for_sale(Y). person(kelly).
person(judy). person(ellen). person(mark). car(lemon). car(hot_rod).
for_sale(lemon). for_sale(hot_rod). GOAL
can_buy(Who,What).
Exercises
Exercise 2 (pet.pro)
Write Prolog Program to Create following database.
Name Age Hobby Pet
Rahim 15 Football Dog Mohsan 11 Volleyball Cat Sohail 25 Card Cow Kamal 30 Swimming Dog Haseeb 11 Football Goat Shakeel 25 Volleyball Cat Abrar 15 Swimming Dog Raju 30 Swimming Dog Javed 40 Football Cow Waleed 30 Volleyball Cat
a) Write Query to display P_name and age, P_Name and hobby.
b) Find How many of them are child if age <=15 is child.
Exercise 3 (solar.pro)
Translate the following sentences into a Prolog program:
The sun is a star. Venus is a planet. The earth is a planet.
The moon orbits around the earth. Every planet orbits the sun.
Anything is a heavenly body if it is a star or a planet. The solar system is geocentric if the sun orbits the earth. The solar system is heliocentric if the earth orbits the sun.
Do the following Goals:
(a) Is the Sun is a star ?
(b) Is moon orbit around the earth ?
(c) Write a Prolog query to ask if the solar system is heliocentric.