0% found this document useful (0 votes)
120 views23 pages

Ct006!3!3 Adplc 09 Prolog Rulesandstructures

aur

Uploaded by

suraj chimchim
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views23 pages

Ct006!3!3 Adplc 09 Prolog Rulesandstructures

aur

Uploaded by

suraj chimchim
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

CT006-3-3: Advanced Programming Language Concepts

PROLOG: Rules and Structures


Week 9

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

Topic and Structure of the Lesson


1. 2. 3. 4. 5. 6. Rules in PROLOG Example queries Data structure Structure Pattern Matching Example

Module Code and CE0903313-3 APLC Module Title

Title of Slides

Slide 2 (of 23)

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

Module Code and CE0903313-3 APLC Module Title

Title of Slides

Slide 4 (of 23)

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

Module Code and CE0903313-3 APLC Module Title

Title of Slides

Slide 8 (of 23)

Example queries (cont..):


Try 2nd rule

parent(X,Y) :- mother(X,Y). Instantiate X=mary & Y=W


New goal is mother(mary,W) which matches the fact: mother(mary,sue) and Instantiates W=Y=sue and displays result (W is the only variable in the query) W=sue
Module Code and CE0903313-3 APLC Module Title Title of Slides Slide 9 (of 23)

DATA STRUCTURE
DATA STRUCTURE List Constant Integer Atom Term Variable Structure

Module Code and CE0903313-3 APLC Module Title

Title of Slides

Slide 10 (of 23)

An argument of a predicate must be


A term or a list

Example fact: departs(last_train(stafford,london),2342).


the predicate, departs, has 2 arguments: last_train(stafford,london) 2342 is a structure is an integer constant

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

Module Code and CE0903313-3 APLC Module Title

Title of Slides

Slide 12 (of 23)

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)

Rules for Pattern Matching:

A variable (eg X, Who or _ ) matches anything.


A constant (eg sue or 123) matches a variable or itself.

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

Slide 14 (of 23)

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

Module Code and CE0903313-3 APLC Module Title

Title of Slides

Slide 15 (of 23)

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.

Module Code and CE0903313-3 APLC Module Title

Title of Slides

Slide 16 (of 23)

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

Slide 17 (of 23)

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

Return true through subgoals to query and display


Module Code and CE0903313-3 APLC Module Title Title of Slides Slide 18 (of 23)

Consider another query:

?-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|_]).

Returns true to subgoal and query displays


Who = tom

Module Code and CE0903313-3 APLC Module Title

Title of Slides

Slide 19 (of 23)

DEMO

Module Code and CE0903313-3 APLC Module Title

Title of Slides

Slide 20 (of 23)

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.

Module Code and CE0903313-3 APLC Module Title

Title of Slides

Slide 21 (of 23)

Q&A

Any Questions?

Module Code and CE0903313-3 APLC Module Title

Title of Slides

Slide 22 (of 23)

Next Session
PROLOG: Lists and Rules Prolog Examples Simple exercises

Module Code and CE0903313-3 APLC Module Title

Title of Slides

Slide 23 (of 23)

You might also like