+ All Categories
Home > Documents > Ct006!3!3 Adplc 09 Prolog Rulesandstructures

Ct006!3!3 Adplc 09 Prolog Rulesandstructures

Date post: 28-Nov-2015
Category:
Upload: suraj-chimchim
View: 28 times
Download: 2 times
Share this document with a friend
Description:
aur
23
PROLOG: Rules and Structures Week 9 CT006-3-3: Advanced Programming Language Concepts 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
Transcript

PROLOG: Rules and Structures

Week 9

CT006-3-3: Advanced Programming Language Concepts

Prepared by: JRK First Prepared on: 09-10-06 Last Modified on: 09-10-06Quality checked by:

Copyright 2006 Asia Pacific Institute of Information Technology

Module Code and Module Title Title of SlidesCE0903313-3 APLC

Topic and Structure of the Lesson

1. Rules in PROLOG

2. Example queries

3. Data structure

4. Structure

5. Pattern Matching

6. Example

Slide 2 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

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

Slide 3 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

Key Terms1. Rules

2. Facts

3. Structures

4. Data structure

5. Pattern Matching

Slide 4 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

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

Slide 5 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

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.

Slide 6 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

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, thereforethe condition father(george,mary) is true

therefore the conclusion parent(george,mary) is trueand result displayed is

YES

Slide 7 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

Example queries:

?-parent(mary,W).

Try 1st ruleparent(X,Y) :- father(X,Y).

Instantiate X=mary & Y=W

New goal is father(mary,W)no match therefore fails

Slide 8 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

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

Slide 9 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

DATA STRUCTURE

DATA STRUCTURE

List Term

ConstantVariable Structure

Integer Atom

Slide 10 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

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) is a structure2342 is an integer constant

?-departs(last_train(stafford,X),2342).

GivesX=london

Slide 11 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

Structures

Example:

last_train(stafford,london)

each argument in a structure must be a term or a list.

Slide 12 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

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.

Slide 13 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

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.

Slide 14 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

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

Slide 15 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

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.

Slide 16 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

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.

Slide 17 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

Consider a query:

?-member (tom, [jim,mary,tom,sue]).

X=tom, T=[mary,tom,sue] matches rule - member (X, [_|T]) :- member (X, T).

Try subgoalmember (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 YES

Slide 18 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

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

Slide 19 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

DEMO

Slide 20 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

Exercises

Slide 21 (of 23)

1. Using a predicate married_couple(Wife, Husband), define the relationshiops mother_in_law, brother_in_law, and son_in_law.

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

Module Code and Module Title Title of SlidesCE0903313-3 APLC

Q & A

Any Questions?

Slide 22 (of 23)

Module Code and Module Title Title of SlidesCE0903313-3 APLC

Next Session

• PROLOG: Lists and Rules

• Prolog Examples

• Simple exercises

Slide 23 (of 23)


Recommended