Lecture 5-Logic programming and prolog
Lecture 5-Logic programming and prolog
LECTURE 5
Logic programming and prolog
Contents
• Summary
5.1. Introduction
1
CCS 3400 Knowledge Based Systems Kaje David Murithi
5.2. Prolog
•.6. Prolog is the most widely used language to have been inspired by logic
pro- gramming research. Some features:
•.7. Prolog uses logical variables. These are not the same as variables in
other lan- guages. Programmers can use them as ‘holes’ in data structures
that are gradually filled in as computation proceeds.
2
CCS 3400 Knowledge Based Systems Kaje David Murithi
•.13. Sometimes it is necessary to use control features that are not part of ‘logic’.
•.15. Clauses are statements about what is true about a problem, instead
of instruc- tions how to accomplish the solution.
•.16. The Prolog system uses the clauses to work out how to accomplish
the solu- tion by searching through the space of possible solutions.
3
CCS 3400 Knowledge Based Systems Kaje David Murithi
4
CCS 3400 Knowledge Based Systems Kaje David Murithi
•.19. example
• A Fact
• A fact has the format:
– predicatename(arguments)
• For instance:
5
CCS 3400 Knowledge Based Systems Kaje David Murithi
– female(monica).
• A Rule
• A rule has the following form:
• For instance:
• mother(X, Y) :- parent(X, Y), female(X).
•.4. Once a prolog program is written, the user may ask different questions.
• ?- parent(X, rachael).
6
CCS 3400 Knowledge Based Systems Kaje David Murithi
• ?- predecessor(monica, X).
Example 3
7
CCS 3400 Knowledge Based Systems Kaje David Murithi
•.6. If the question consists of a predicate with no variables like, for instance
• Prolog tries to demonstrate that this fact logically follows from the facts and
the rules in the program.
8
CCS 3400 Knowledge Based Systems Kaje David Murithi
•.8. Prolog uses the closed world assumption which states that all
relevant, true assertions are explicitly represented or are derivable from
those explicitly repre- sented.
• Based on CWA, any assertion that is neither explicitly represented nor deriv-
able, is considered to be false.
•.9. If the question consists of one or more predicates with variables such as:
•.10. Prolog will look for all the instances of the variables from the question
(X and Y in the above example) such that the predicates in the question
logically follow from the facts and the rules in the program.
•.11. One says that Prolog tries to satisfy the goals "parent(Y, kenny)" and
"par- ent(X, Y)".
• X = bob
• Y = pat
•.13. In all the queries the goals are satisfied by matching the questions
with the facts.
•.14. If a goal cannot be satisfied in such a way, Prolog will try to use the
rules from the program
9
CCS 3400 Knowledge Based Systems Kaje David Murithi
•.15. Prolog backtracks and tries any applicable rules if one goal fails
10
CCS 3400 Knowledge Based Systems Kaje David Murithi
5.3. Unification
• Two terms unify if substitutions can be made for any variables in the terms
so that the terms are made identical. If no such substitution exists, the terms
do not unify.
When writing Prolog programs one should use the following problem
solving heuris- tic:
• try the simplest idea first.
11
CCS 3400 Knowledge Based Systems Kaje David Murithi
Complete Syntax of Terms
12
CCS 3400 Knowledge Based Systems Kaje David Murithi
5.3.3. Variables
• Variables are strings of letters, digits and underscore characters.
• If the anonymous variable appears in a question clause then its value is not
output when Prolog answers the question:
5.3.5. Structures
•.19. Structured objects (structures) are objects that have several components.
•.21. All structured objects in Prolog are trees, represented in the program by terms.
• They are not functions applied to arguments to denote the corresponding re-
sult.
5.3.6. Lists
•.27. For improved readability Prolog provides a special notation for lists:
• [ Head | Tail] or
14
CCS 3400 Knowledge Based Systems Kaje David Murithi
15
CCS 3400 Knowledge Based Systems Kaje David Murithi
• X is a member of L if either
– member( X, [X | Tail] ).
– member( X, [Head | Tail] ) :-member( X, Tail).
5.3.8. Concatenation
•.28. We define the relation conc( L1, L2, L3) where L1 and L2 are two
lists and L3 is their concatenation.
•.29. conc( [], L, L). % the concatenation of an empty list [] %with a list L is L
•.30. conc([X | L1], L2, [X | L3] ) :- % a non-empty list has the form [X | L1]
•.31. conc( L1, L2, L3). % the concatenation of [X | L1] with a list L2 is
% the list [X | L3],where L3 is the concatenation of L1 and L2
• L = [a,b,c,1,2,3]
•.33. conc could also be used in the inverse direction for decomposing a
given list into two lists:
• ?- conc(U, V, [a,b,c]).
– U = []
16
CCS 3400 Knowledge Based Systems Kaje David Murithi
– V = [a,b,c];
– U = [a]
17
CCS 3400 Knowledge Based Systems Kaje David Murithi
– V = [b,c];
– U = [a,b]
– V = [c];
– U = [a,b,c]
– V = [];
– no
Add
Delete
– del ( X, L, L1) where L1 is equal to the list L with the item X removed.
– del( X, [X | Tail], Tail). % if X is the head of the %list then the result of
deletion of X % is the tail of the list.
– del( X, [Y | Tail], [Y | Tail1]) :- % if X is in the tail, then it %should be
deleted from there. del( X, Tail, Tail1)
Sublist
5.3.9. Operators
19
CCS 3400 Knowledge Based Systems Kaje David Murithi
Revision questions
E XERCISE 8. ....
20