Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2003 Bolton Institute Logical Analysis and Problem Solving, (LAPS) Programming in Prolog.

Similar presentations


Presentation on theme: "Copyright © 2003 Bolton Institute Logical Analysis and Problem Solving, (LAPS) Programming in Prolog."— Presentation transcript:

1 Copyright © 2003 Bolton Institute Logical Analysis and Problem Solving, (LAPS) Programming in Prolog

2 Copyright © 2003 Bolton Institute Recap Prolog –Relations can be created between objects. –Specific objects must be in lower-case. –Variables in upper-case. –Queries can be written to interrogate data. –Multiple output can be achieved with OR symbol. –Multiple statements can be connected using the AND connective. –Rules can be defined in Prolog to ease use and development.

3 Copyright © 2003 Bolton Institute Example The following facts are provided: –Mary works hard, Adi works hard and Jack doesn’t work hard. Mary attends all lessons, Adi attends some lessons and Jack hardly attends. –The following rules are given: –IF a student works hard and attends all lessons THEN the student will pass well. –IF a student works hard and attends some lessons THEN the student will pass –IF a student doesn’t work hard OR a student hardly attends lessons THEN student will fail Write Prolog statements representing the above information. What will happen to Mary, Adi and Joe?

4 Copyright © 2003 Bolton Institute Recursion Sometimes when relations are defined for objects, there is an indirect relationship between the objects. For example, within a family tree, a person is an ancestor of another person if they are their parent, or grandparent, great grandparent etc. What if we wanted to know whether a person was an ancestor - without knowing the precise relationship, such as grandparent or great grandparent? How could we find this in Prolog? We use recursion.

5 Copyright © 2003 Bolton Institute Grandparent Rule A Grandparent rule for a family tree could be defined as: /* Rule that defines grandparent relation */ grandparent(Grandparent,Grandchild) :- parent(Grandparent,Parent), parent(Parent,Grandchild). Note: how a grandparent is the parent of the grandchild’s parent!

6 Copyright © 2003 Bolton Institute Ancestor rule We can state an ancestor rule, as a specific rule and a general rule, as outlined below. ancestor(Ancestor, Descendant) :- parent(Ancestor,Descendant). ancestor(Ancestor, Descendant) :- parent(Ancestor,Parent), ancestor(Parent, Descendant). The first rule is true when the Ancestor variable is the parent of the Descendant. If the first rule is NOT true, the second rule is executed! The second rule finds who the Ancestor is the parent of and then requests that the ancestor rule is tried again - the first rule will be attempted again! See example on board.

7 Copyright © 2003 Bolton Institute Backtracking Prolog works through rules until a false relation is found. If a false relation is found, Prolog backtracks and tries another fact or rule, until, all relevant facts / rules have been attempted. Backtracking is one of the most powerful aspects of Prolog. Other programming languages require the developer to write code that does backtracking!

8 Copyright © 2003 Bolton Institute Exercises How could you query whether Jan is an ancestor of Lucy? How could you show all the ancestors of Jack? How could you show all the descendants of Jim? Create Prolog queries for the above questions using the ancestor rules previously defined.

9 Copyright © 2003 Bolton Institute Recap Review notes and worksheets.


Download ppt "Copyright © 2003 Bolton Institute Logical Analysis and Problem Solving, (LAPS) Programming in Prolog."

Similar presentations


Ads by Google