Programming Logic Prolog
Programming Logic Prolog
LESSON 5
Logic programming and prolog
Contents
• Summary
5.1. Introduction
• Logic programming refers to a family of languages and an associated pro-
gramming style based on writing programs as a set of assertions (facts and
inference rules).
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.
38
ICS 2405 Knowledge Based Systems
•.11. The relational form of procedures makes it possible to define ‘reversible’ pro-
cedures.
•.12. Clauses provide a convenient way to express case analysis and nondetermin-
ism.
•.13. Sometimes it is necessary to use control features that are not part of ‘logic’.
•.14. A Prolog program can also be seen as a relational database containing rules
as well as facts.
•.16. The Prolog system uses the clauses to work out how to accomplish the solu-
tion by searching through the space of possible solutions.
•.17. Not all problems have pure declarative specifications. Sometimes extralogical
statements are needed.
39
ICS 2405 Knowledge Based Systems
•.19. example
• A Fact
• A fact has the format:
– predicatename(arguments)
• For instance:
40
ICS 2405 Knowledge Based Systems
– female(monica).
• A Rule
• A rule has the following form:
• X :- Y1, Y2, ... ,Yn.
• For instance:
• mother(X, Y) :- parent(X, Y), female(X).
• ?- parent(X, rachael).
41
ICS 2405 Knowledge Based Systems
• ?- predecessor(monica, X).
Example 3
42
ICS 2405 Knowledge Based Systems
• Prolog tries to demonstrate that this fact logically follows from the facts and
the rules in the program.
43
ICS 2405 Knowledge Based Systems
•.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)".
•.12. Prolog returns the found pairs of the values of X and Y, or No if no such pair
is found.
• 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
•.15. Prolog backtracks and tries any applicable rules if one goal fails
44
ICS 2405 Knowledge Based Systems
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.
45
ICS 2405 Knowledge Based Systems
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:
46
ICS 2405 Knowledge Based Systems
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.
•.22. It should be noted that the functions of Prolog are seen as compound data
structures similar to the records of Pascal: they can be constructed and analyzed.
• They are not functions applied to arguments to denote the corresponding re-
sult.
5.3.6. Lists
•.23. A list is a data structure that is either empty or consists of two parts: a head
and a tail. The tail itself has to be a list.
•.27. For improved readability Prolog provides a special notation for lists:
• [ Head | Tail] or
47
ICS 2405 Knowledge Based Systems
• 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 = []
– V = [a,b,c];
– U = [a]
48
ICS 2405 Knowledge Based Systems
– V = [b,c];
– U = [a,b]
– V = [c];
– U = [a,b,c]
– V = [];
– no
Add
Adding an item X to the beginning of a list L can be programmed as a relation
Delete
• Deleting an item X from a list L can be programmed as a relation
– 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
sublist( S, L) :- % S is a sublist of L if conc( L1, L2, L), % L is composed of two
lists, L1 and L2, and conc( S, L3, L2). % L2 is composed of S and some L3.
5.3.9. Operators
Any atom may be designated an operator. The only purpose is for convenience; the
only effect is how the term containing the atom is parsed. Operators are ‘syntactic
sugar’. Operators have three properties: position, precedence and associativity.
Examples of operator properties
49
ICS 2405 Knowledge Based Systems
Revision questions
Solution:
E XERCISE 8. ....
50