2. ● What is Prolog?
● Syntax of Prolog
● Prolog Control Strategy
● Interface architecture
● Prolog Query
● Programming Techniques
● Termination
● Rule Order
● Goal Order
● Redundancy
● Iteration in Prolog
● conclusion
3. ● Prolog is acronym of PROgramming in LOGic.
● Prolog program is sequence of rules and facts.
● Prolog Rule for grandfather is defined as:
gfather(X, Y) :- father(X, Z), parent(Z, Y).
father(james, robert).
4. ● The rules and facts in Prolog are terminated by full stop (.).
● Goal can be included in the program or given at the prompt.
● Goal can be single or conjunction of sub-goal(s) terminated by full stop.
● Constants are numerals and symbols such as, 4, mary, etc.
● String is a sequence of characters and is enclosed within single quotes e.g., 'This is
a string'.
● Predicate names must start with alphabet and are formed by using lower case
letters, numerals and underscore ( _ ).
● Variable names are formed similar to predicate names but it must start with
upper case letter. Normally, we use X, Y, Z, ... for variables.
5. ● Prolog contains three basic control strategies.
− Forward movement
− Matching (Unification)
− Backward movement (Backtracking)
● Prolog uses depth first search strategy.
6. ● Choose a rule by
− searching sequentially in the program from top to bottom whose
head matches with the goal with possible unifier.
− Remember the position of the matched rule.
− Join the rule body in front of the sequence of sub goals to be
solved.
● Repeat until either goal is satisfied or is failed.
7. ● Consider the following query consisting of n sub-goals.
?- G1, ... , Gi-1, Gi, ... , Gn
● Starts executing or satisfying G1
− If G1 is satisfied using some rule, put a place marker so that next rule for G1 will
be searched after the marker.
− Continue in forward direction to satisfy G2 and if it is satisfied then continue with
common bindings of the variables in sub-goals so far satisfied.
− Such a movement is called forward
8. ● Unification is a process of matching or finding the most general unifier.
− Constant matches with the same constant.
− A variable can match with any constant or any another variable.
● Examples
− love(X, sita) unifies with love(ram, sita) with mgu as {X / ram}.
− love(X, Y) unify with love(ram, sita) with mgu as {X/ram, Y/sita} respectively.
9. ● Backtracking refers to backtrack in search process for a solution.
● In Prolog, backtracking takes place in two situations.
− First when a sub goal fails and
− Other when the last sub goal succeeds, it backtracks to find
alternative solutions.
10. ● Prolog has two kinds of backtracking.
− Shallow and
− Deep backtracking
● Both backtrackings take place in conjunction with each other.
● Shallow backtracking occurs
− when alternative definition of the goal is tried.
− Suppose G1,.., Gi-1 have been satisfied with some common bindings
of the variables in sub-goals.
− While satisfying Gi., if it fails for some rule then try another rule of Gi
− This is called shallow backtracking to rules of the same sub-goal
11. ● Deep backtracking occurs
− When sub-goal Gi fails for all rules of Gi then backtrack to Gi-1.
− This backtracking is called deep backtracking.
− Remove the bindings created earlier to satisfy Gi-1.
− Try to satisfy Gi-1 again by using an alternative rule, if it exists, from
last place marker of Gi-1. and continue the process.
− If sub goal Gi-1 is satisfied then move forward to Gi, else backtrack to
Gi-2.
− The process of backtracking continues till we reach G1.
− If G1 fails for all possible definitions, then entire query fails.
12. ● If entire conjunction of sub-goals succeed, then the solution is
displayed.
● For alternative solution, the sub goal Gn is tried to be satisfied with
alternative rule, if it exist, after last place marker (Shallow backtracking)
of Gn .
● Otherwise it backtracks to previous sub goal Gn-1 (Deep backtracking).
● Process is repeated till all possible solutions are generated.
13.
14. ● Two types of query
− Ground query
No variable in the argument
Example:
? gfather(ram, shyam) – Is ram a grand father of shyam?
Answer to ground queries are Yes or No
Proof tree is generated while solving the query.
− Non ground query
Atleast one argument is a variable
Example:
? gfather(X, shyam) – Who is a grand father of shyam?
Answer to such queries are the values bound to the variables in the query.
Search tree is generated using DFS to find all possible solutions.
17. ● Recursion is one of the most important execution control techniques in
Prolog.
● Iteration is another important programming technique.
● Prolog does not support iteration.
● It can be achieved by
− making recursive call to be the last sub goal in the rule
− using the concept of accumulators which are special types of arguments used for
holding intermediate results.
● The issues of termination, rule order, goal order and redundancy are
also important.
18. ● Consider a classical example of computing factorial using recursion.
● Recurrence relation or mathematical definition for computing factorial of positive
integer is:
1, if n = 0
FACTORIAL (n) =
n * FACTORIAL (n-1),
otherwise
● In Prolog, program for computing factorial of a positive number is written using
above definition.
● Factorial predicate contains two arguments; one as input and another as output.
19. /*fact(N, R) – succeeds if R is unified with the factorial of N */
factorial(0, 1). (1)
factorial(N, R) :- N1 is N–1, factorial(N1, R1), R is N * R1. (2)
● Rule (1) states that factorial of 0 is 1 which is a terminating clause.
● Rule (2) is a recursive rule which states that factorial of N is calculated
by multiplying N with factorial of (N-1).
Goal: ?- factorial(3, R).
20. Depth first traversal of Prolog has a serious problem. If the search tree
of a goal with respect to a program contains an infinite branch, the
computation will not terminate.
Prolog may fail to find a solution of a goal, even if the goal has a finite
computation.
Non termination arises because of the following reasons:
− Left recursive rules: The rule having a recursive goal as the first sub
goal in the body.
21. ● Consider acyclic directed graph
p
q
r s
t
● Graph G is represented by set of connected edges.
G = { edge(p, q), edge(q, r), edge(q, s), edge(s, t)}
● Write Prolog Program to check whether there is a route from one node to another
node.
22. ● Route from node A to B is defined as follows:
− route from A to B if there is a root from A some node C and an edge from C to B.
− route from A to B if there is a direct edge from A to B.
● Prolog Program
route(A, B) :- route(A, C), edge(C, B). (1)
route(A, B) :- edge(A, B). (2)
edge(p, q).
edge(q, r).
edge(q, s).
edge(s, t).
Query: Check whether there exist a route between nodes p and t .
23. Goal: ?- route(p, t).
Proof tree:?- route(p, t).
(1)
?- route(p, X), edge(X, t)
(1)
?- route(p, Y), edge(Y, X), edge(X, t).
Non terminating
● Non terminating program because of left recursion.
● Better way of writing a rule is to write recursive call as the last call.
route(A, B) :- edge(A, C), route(C, B).
24. ● A rule order is very important issue in Prolog.
● Change of the order of rules in a program, the branches in any search
tree for a goal to be solved with that program get permuted.
● It may affect the efficiency of executing goal.
● Since the search tree is traversed using depth first approach, the order
of solutions will be different.
25. ● Goal order in Prolog is more significant than rule order.
● It may affect the termination because
− subsequent sub goals have different bindings and hence different search trees.
● Consider the following recursive rules.
ancester(X, Y) :- parent(X, Z), ancester(Z, Y).
route(X, Y) :- edge(X, Z), route(Z, Y).
● If the sub goals in the body of above rules are swapped, then the rules becomes left
recursive and all the computations will become non terminating.
● It also affects the amount of search the program does in solving a query .
26. ● Redundancy of the solution to the queries is another important issue in Prolog
programs.
● In Prolog, each possible deduction means an extra branch in the search tree.
● The bigger the search tree, higher the computation time.
● It is desirable in general to keep the size of the search tree as small as possible.
● Redundant program at times may cause exponential increase in runtime in the
event of backtracking.
● There are various reasons by which redundancy gets introduced.
− The same case is covered by several rules
− Redundancy in computation appears in the program by not handling special
cases separately.
27. Prolog does not have storage variables that can
− hold intermediate results of the computation
− be modified as the computation progress.
To implement iterative algorithm one requires the storage of
intermediate results, Prolog predicates are augmented with additional
arguments called accumulators.
These are similar to local variables in procedural languages.
The programs written using accumulators are iterative in nature.
In iterative form of a program, make the recursive call as the last call if
possible.
28. ● Prolog has helped more than 6,000 organization reduce construction project risk, control project costs, and
provide complete transparency to project stakeholders, becoming the industry standard for construction project
control and visibility.
Prolog Enables you to Your Benefit
Automate Task and process Complete automation from project design to close-out
Streamline project Delivery Manage all construction projects in one system
Control cost Real-time cost management tools and budget tracking
Increase Productivity Real-time collaboration with geographically dispersed project teams
Reduce Legal Risk Audited access to data and documents in one central location
Monitor Project
performance
Access to real time metrics tracking and dashboards
Extend The value of your
Software investment
Integrating your construction project data with other technology system
and devices (accounting , Microsoft Office, BIM models, and handheld
devices)