Prolog
• Prolog is a ‘non numeric’ or ‘symbolic
language’ and is also a ‘declarative’ language, .
• Contains Clauses
These are statements about what is true about a
problem, instead of instructions how to accomplish the
solution.
• The Prolog system uses the clauses to work out how to
accomplish the solution by searching through the space
of possible solutions.
• Not all problems have pure declarative specifications.
Sometimes extralogical statements are needed.
• A PROLOG program is a set of specifications in the
first-order predicate calculus describing the objects and
relations in a program domain.
• The PROLOG interpreter processes (controls) queries,
searching the database to find out whether the query is a
logical consequence of the database of specifications.
• Prolog has data objects classified as below:
Atoms
• Atoms can be constructed in three ways:
1. Strings of letters, digits, and the underscore character,
starting with a lowercase letter.
• Example abc, ab_cd, x123, _123.
2. Strings of special characters.
3. Strings of characters enclosed in single quotes.
• Example ‘Tom’, ‘Hello’.
Numbers
Numbers used in PROLOG included integer
numbers and real numbers.
Integer numbers: 1, 100, 123
Real numbers: 100.0, 0.001
Variables
• These are strings of letters, digits and underscore characters.
• They start with an uppercase letter or an underscore character.
Example: X, Result, Tom, _123, X_y and
_ (the so-called anonymous variable)
Example haschild(X) :- parent(X,_)
• ?-likes(george,_).
 Is there anything that George likes?
 We are interested in whether George likes something
but not in the thing that George likes.
• Structured objects are objects that have several
components.
• The components themselves can be structures.
Tree Representation
• Consider (a+b)*(c-5) *(+(a,b),-(c,5).
• Infix notation preferred in Prolog.
Example: monkey and banana
• Problem. There is a monkey at the door into a room. In the middle
of the room a banana is hanging from the ceiling. The monkey is
hungry and wants to get the banana but it cannot stretch high
enough from the floor. At the window of the room there is a box
the monkey may use. The monkey can perform the following
actions: walk on the floor, climb the box, push the box around, and
grasp the banana if standing on box directly under the banana. Can
the monkey get the banana?
Question: Can the monkey in some initial state get the
banana?
State1
canget canget
Move
State2
State3
canget canget
Move
State4 statem
has
List
• A list is a sequence of any number of items.
 The empty list: [].
 A list of three items: [item1, item2, item3].
 A list in PROLOG can be viewed as consisting of
two parts:
1. The first item, called the head of the list;
 The head can be any PROLOG object.
2. The remaining items, called the tail of the list.
 The tail is a list.
• Lists are handled in PROLOG as a special case of
binary trees.
Concatenate two lists.
conc([],L2,L2).
conc([X|T1],L2,[X|L3]):- conc(T1,L2,L3).
If the first argument is the empty list then the second and the third
Arguments must be the same list.
If the first argument of conc is a non-empty list then it has a head and a tail
and must look like :I[X|L]
• Determine whether or not an item is in a list.
member(X,[]):-!,fail.
member(X, [X|L]).
member(X,[H|L]:- member(X,L).
X is a member of L if either
(1) X is the head of L, or
(2) X is a member of the tail of L.
• Add an item to a list.
add(X,L,[X|L]).
• Delete an item from a list.
del(X,[X|L],L).
del(X,[Y|L1],[Y|L2]):- del(X,L1,L2).
Decomposition of list.
If X is the head of the list then the result after the deletion is the tail of the
list.
If X is in the tail then it is deleted from there
• Insert an item into a list.
insert(X,L1,[X|L1]).
• Insert an item into a list at any place.
insert(X,L1,L2):- del(X,L2,L1).
Sublist
• sublist([b,c,d],[a,b,c,d,e]) is true, but
• sublist([b,d],[a,b,c,d,e]) is false.
sublist( S, L) :- conc( Ll, L2,L),conc( S, L3,L2).
S is a sublist of L if
(1) L can be decomposed in to two lists, L1 and L2, and
(2) L2 can be decomposed in to two lists, S and some L3.
Arithmetic
• Prolog is mainly a language for symbolic computation where the
need for numerical calculation is comparatively modest
• Some of the predefined operators can be used for basic arithmetic
operations
• The comparison operators are
GCD
PROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOG

PROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOG

  • 1.
  • 2.
    • Prolog isa ‘non numeric’ or ‘symbolic language’ and is also a ‘declarative’ language, . • Contains Clauses These are statements about what is true about a problem, instead of instructions how to accomplish the solution. • The Prolog system uses the clauses to work out how to accomplish the solution by searching through the space of possible solutions. • Not all problems have pure declarative specifications. Sometimes extralogical statements are needed.
  • 3.
    • A PROLOGprogram is a set of specifications in the first-order predicate calculus describing the objects and relations in a program domain. • The PROLOG interpreter processes (controls) queries, searching the database to find out whether the query is a logical consequence of the database of specifications.
  • 4.
    • Prolog hasdata objects classified as below:
  • 5.
    Atoms • Atoms canbe constructed in three ways: 1. Strings of letters, digits, and the underscore character, starting with a lowercase letter. • Example abc, ab_cd, x123, _123. 2. Strings of special characters. 3. Strings of characters enclosed in single quotes. • Example ‘Tom’, ‘Hello’.
  • 6.
    Numbers Numbers used inPROLOG included integer numbers and real numbers. Integer numbers: 1, 100, 123 Real numbers: 100.0, 0.001
  • 7.
    Variables • These arestrings of letters, digits and underscore characters. • They start with an uppercase letter or an underscore character. Example: X, Result, Tom, _123, X_y and _ (the so-called anonymous variable) Example haschild(X) :- parent(X,_) • ?-likes(george,_).  Is there anything that George likes?  We are interested in whether George likes something but not in the thing that George likes.
  • 8.
    • Structured objectsare objects that have several components. • The components themselves can be structures. Tree Representation
  • 9.
    • Consider (a+b)*(c-5)*(+(a,b),-(c,5). • Infix notation preferred in Prolog.
  • 10.
    Example: monkey andbanana • Problem. There is a monkey at the door into a room. In the middle of the room a banana is hanging from the ceiling. The monkey is hungry and wants to get the banana but it cannot stretch high enough from the floor. At the window of the room there is a box the monkey may use. The monkey can perform the following actions: walk on the floor, climb the box, push the box around, and grasp the banana if standing on box directly under the banana. Can the monkey get the banana?
  • 12.
    Question: Can themonkey in some initial state get the banana? State1 canget canget Move State2 State3 canget canget Move State4 statem has
  • 14.
    List • A listis a sequence of any number of items.  The empty list: [].  A list of three items: [item1, item2, item3].  A list in PROLOG can be viewed as consisting of two parts: 1. The first item, called the head of the list;  The head can be any PROLOG object. 2. The remaining items, called the tail of the list.  The tail is a list. • Lists are handled in PROLOG as a special case of binary trees.
  • 15.
    Concatenate two lists. conc([],L2,L2). conc([X|T1],L2,[X|L3]):-conc(T1,L2,L3). If the first argument is the empty list then the second and the third Arguments must be the same list. If the first argument of conc is a non-empty list then it has a head and a tail and must look like :I[X|L]
  • 16.
    • Determine whetheror not an item is in a list. member(X,[]):-!,fail. member(X, [X|L]). member(X,[H|L]:- member(X,L). X is a member of L if either (1) X is the head of L, or (2) X is a member of the tail of L.
  • 17.
    • Add anitem to a list. add(X,L,[X|L]). • Delete an item from a list. del(X,[X|L],L). del(X,[Y|L1],[Y|L2]):- del(X,L1,L2). Decomposition of list. If X is the head of the list then the result after the deletion is the tail of the list. If X is in the tail then it is deleted from there
  • 18.
    • Insert anitem into a list. insert(X,L1,[X|L1]). • Insert an item into a list at any place. insert(X,L1,L2):- del(X,L2,L1).
  • 19.
    Sublist • sublist([b,c,d],[a,b,c,d,e]) istrue, but • sublist([b,d],[a,b,c,d,e]) is false. sublist( S, L) :- conc( Ll, L2,L),conc( S, L3,L2). S is a sublist of L if (1) L can be decomposed in to two lists, L1 and L2, and (2) L2 can be decomposed in to two lists, S and some L3.
  • 20.
    Arithmetic • Prolog ismainly a language for symbolic computation where the need for numerical calculation is comparatively modest • Some of the predefined operators can be used for basic arithmetic operations
  • 21.
    • The comparisonoperators are
  • 23.