Ct006!3!3 Adplc 09 Prolog Rulesandstructures
Ct006!3!3 Adplc 09 Prolog Rulesandstructures
Prepared by: JRK First Prepared on: 09-10-06 Last Modified on: 09-10-06 Quality checked by: Copyright 2006 Asia Pacific Institute of Information Technology
Title of Slides
Learning Outcomes
At the end of this chapter, you should be able to Explain the concept Logic Programming with Prolog Explain the concept of Rules, Structures and Pattern Matching Write Simple Prolog database with rules and structures
Module Code and CE0903313-3 APLC Module Title Title of Slides Slide 3 (of 23)
Key Terms
1. 2. 3. 4. 5. Rules Facts Structures Data structure Pattern Matching
Title of Slides
RULES
Consider a database with some facts: father(george,mary). father(george,john). father(fred,jim). mother(mary,sue). mother(mary,john). and the database also has some rules: parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y).
Module Code and CE0903313-3 APLC Module Title Title of Slides Slide 5 (of 23)
RULES
which mean: X is the parent of Y if X is the father or mother of Y alternatively: parent(X,Y) is true if father(X,Y) is true or if mother(X,Y) is true.
In a rule, on the left is the conclusion, eg parent(X,Y) on the right is the condition, eg father(X,Y) Thus if the condition is true then the conclusion is true.
Module Code and CE0903313-3 APLC Module Title Title of Slides Slide 6 (of 23)
Example queries:
?-parent(george,mary). Try 1st rule: parent(X,Y) :- father(X,Y). Instantiate X=george & Y=mary New goal is father(george,mary) which matches a fact, therefore the condition father(george,mary) is true therefore the conclusion parent(george,mary) is true and result displayed is
YES
Module Code and CE0903313-3 APLC Module Title Title of Slides Slide 7 (of 23)
Example queries:
?-parent(mary,W). Try 1st rule parent(X,Y) :- father(X,Y). Instantiate X=mary & Y=W New goal is father(mary,W) no match therefore fails
Title of Slides
DATA STRUCTURE
DATA STRUCTURE List Constant Integer Atom Term Variable Structure
Title of Slides
?-departs(last_train(stafford,X),2342).
Gives X=london
Module Code and CE0903313-3 APLC Module Title Title of Slides Slide 11 (of 23)
Structures
Example: last_train(stafford,london) each argument in a structure must be a term or a list.
Title of Slides
Structures
Another example of structure in a fact: book(prolog,author(ivan,bratko)). ?-book(prolog,Who). gives Who = author(ivan,bratko) ?-book(prolog,author(_,Surname)). gives Surname = bratko ?-book(_,author(_,Surname)). Each solution would be a surname for all authors in database.
Module Code and CE0903313-3 APLC Module Title Title of Slides Slide 13 (of 23)
Pattern matching
Arguments must be matched between predicates (ie queries, rules & facts)
A structure (eg last_train(stafford,london)) matches a variable or a structure with same name and all arguments match.
Module Code and CE0903313-3 APLC Module Title Title of Slides
Example:
A readers database contains facts such as: owns(john,book(prolog,author(ivan,bratko))). owns(jim,magazine(stern,date(13,3,94),language (german))). Etc
Title of Slides
Example (cont ):
?-owns(john,book(Topic,author(_,Surname))). Topic = prolog, Surname = bratko
?-owns(john,Book). Book = book(prolog,author(ivan,bratko)) ?-owns(_,magazine(Title,date(_,3,94),_)). Title = stern ?-owns(Who,magazine(_,_,_)). Each solution is somebody who owns a magazine.
Title of Slides
Example (cont ):
?-owns(Who,magazine(_,_,language(german))), owns(Who,magazine(_,_,language(french))). Each solution gives the name of somebody who owns a French and a German magazine! If the query has many solutions we have to press ; to get each one. May be we would want a list produced in one go as result of a query.
Module Code and CE0903313-3 APLC Module Title Title of Slides
Consider a query:
?-member (tom, [jim,mary,tom,sue]). X=tom, T=[mary,tom,sue] matches rule - member (X, [_|T]) :- member (X, T).
Try subgoal member (tom, [mary,tom,sue]) X=tom, T=[tom,sue] matches rule - member (X, [_|T]) :- member (X, T).
Try subgoal member (tom, [tom,sue]) X=tom matches fact - member (X, [X|_]).
?-member(tom, [jim,Who,sue]).
X=tom, T=[Who,sue] matches rule - member (X, [_|T]) :- member (X, T). Try subgoal member (tom, [Who,sue]) X=tom=Who matches fact - member (X, [X|_]).
Title of Slides
DEMO
Title of Slides
Exercises
1. Using a predicate married_couple(Wife, Husband), define the relationshiops mother_in_law, brother_in_law, and son_in_law.
Write a PROLOG program to check if a student has met the requirements for a college degree. Facts will be used to represent the courses that the student has taken and the grades obtained, and rules will be used to enforce the college requirements.
2.
Title of Slides
Q&A
Any Questions?
Title of Slides
Next Session
PROLOG: Lists and Rules Prolog Examples Simple exercises
Title of Slides