introduction on prolog - programming in logic

29
Presented By Tandel Vishal P. Prolog

Upload: vishal-tandel

Post on 15-Jul-2015

222 views

Category:

Technology


2 download

TRANSCRIPT

Presented By –

Tandel Vishal P.

Prolog

● 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

● 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).

● 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.

● Prolog contains three basic control strategies.

− Forward movement

− Matching (Unification)

− Backward movement (Backtracking)

● Prolog uses depth first search strategy.

● Choose a rule by

− searching sequentially in the program from top to bottom whosehead 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 besolved.

● Repeat until either goal is satisfied or is failed.

● 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

● 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.

● 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 findalternative solutions.

● 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 bindingsof 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

● 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, fromlast place marker of Gi-1. and continue the process.

− If sub goal Gi-1 is satisfied then move forward to Gi, else backtrack toGi-2.

− The process of backtracking continues till we reach G1.

− If G1 fails for all possible definitions, then entire query fails.

● If entire conjunction of sub-goals succeed, then the solution isdisplayed.

● For alternative solution, the sub goal Gn is tried to be satisfied withalternative 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.

● 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.

Ground query:

?- gfather(james, hency).Proof tree: ?- gfather(james, hency).

(1)?- father(james, Z), parent(Z, hency).(4) { Z = robert }?- parent(robert, hency).(2)?- father(robert, hency).(7)

succeeds Answer: Yes

Non-Ground query:

?- gfather(william, X).

Search tree: ?- gfather(william, X).(1)

?- father(william, Z), parent(Z, X).(6) {Z = james}

?- parent(james, X).(2) (3)

?- father(james, X). ?- mother(james, X).(4) {X = robert}

succeeds fails

Answer: X = robert

● Recursion is one of the most important execution control techniques inProlog.

● 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 forholding intermediate results.

● The issues of termination, rule order, goal order and redundancy arealso important.

● Consider a classical example of computing factorial using recursion.

● Recurrence relation or mathematical definition for computing factorial of positiveinteger is:

1, if n = 0

FACTORIAL (n) =

n * FACTORIAL (n-1),otherwise

● In Prolog, program for computing factorial of a positive number is written usingabove definition.

● Factorial predicate contains two arguments; one as input and another as output.

/*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 calculatedby multiplying N with factorial of (N-1).

Goal: ?- factorial(3, R).

Depth first traversal of Prolog has a serious problem. If the search treeof a goal with respect to a program contains an infinite branch, thecomputation will not terminate.

Prolog may fail to find a solution of a goal, even if the goal has a finitecomputation.

Non termination arises because of the following reasons:

− Left recursive rules: The rule having a recursive goal as the first subgoal in the body.

● 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 anothernode.

● 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 .

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).

● A rule order is very important issue in Prolog.

● Change of the order of rules in a program, the branches in any searchtree 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 orderof solutions will be different.

● 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 leftrecursive and all the computations will become non terminating.

● It also affects the amount of search the program does in solving a query .

● Redundancy of the solution to the queries is another important issue in Prologprograms.

● 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 theevent 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 specialcases separately.

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 ofintermediate results, Prolog predicates are augmented with additionalarguments 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 ifpossible.

● 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)