itec 380 organization of programming languages lecture 8 – prolog

18
ITEC 380 Organization of programming languages Lecture 8 – Prolog

Upload: shana-taylor

Post on 14-Jan-2016

224 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: ITEC 380 Organization of programming languages Lecture 8 – Prolog

ITEC 380

Organization of programming languages

Lecture 8 – Prolog

Page 2: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

Review

• Prolog–What are its 2 major components?

Page 3: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

Objectives

• Prolog– Basic principles– Examples– Example programs

Page 4: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

DeclarativeProgramming

• Instead of focusing on how to accomplish a particular task, focus on what to accomplish

• Declare the intent and let the tool do the rest

• Limits what is possible– Specific language that is limited

compared to say C

• Leaves little room for traditional optimization

• Do you know of any declarative languages?

Page 5: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

Example

• SQL– Insert knowledge where mind =

attentive

• Declare what should happen, not how– B-trees?– Recursive descent algorithms?SELECT * FROM Book WHERE price > 100.00 ORDER BY title;

Page 6: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

Prolog

• Declarative statements in prolog– Facts that are true– Rules that are made up of groups of

facts– Variables that hold a specific value

• Goal– Ask what the value of an expression is– True / False / Numeric

Page 7: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

Facts

• A true statement• Syntax– Factname(identifier that is mapped to

factname)– Factname(ident1, ident2) Means that

ident1 is the Factname of ident2

• Examplemale(Bob)female(Jill)parent(Jill, Bill)parent(Bob, Bill)

Page 8: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

But wait

• Have to store up a “knowledge base” before you can make queries

• Declares a set of facts• Can be used to make queries later on• Difference of what should be done

versus how to go about it

Page 9: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

Loading

• Once you have saved your knowledge base you can load it into prolog– Needs to have a .pl extension

• Synatx– [filename]. %Note the . is the prolog

equivalent to a ;– Do not use the .pl extension

• Once you have done that you can use it

Page 10: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

Example

• View a simple knowledge base• Load it• Make queries about it

male(bob).female(jill).parent(jill,bill).parent(bob, bill).father(TheDad, TheChild) :-

parent(TheDad,TheChild), male(TheDad).mother(TheMom, TheChild) :-

parent(TheMom,TheChild), female(TheMom).

Page 11: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

User I/O

• Need to get input to truly exercise the system

• Expanding the knowledge base – write(‘Data\nNewLine\nData’)– read(Variable)

• Method– Have a rule that uses read, then

combine with a , and then use the new variable in the facts

Page 12: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

Expanded

• Making our previous knowledge base have I/Omale(bob).

female(jill).parent(jill,bill).parent(bob, bill).father(TheDad, TheChild) :-

parent(TheDad,TheChild), male(TheDad).mother(TheMom, TheChild) :-

parent(TheMom,TheChild), female(TheMom).iTester :- write('Type the mother\'s name\n'),

read(Mom), write('Type the child\'s name\n'), read(Child), mother(Mom, Child).

Page 13: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

Issue

• Test out someone not in the DB – Use lower case variables, then upper case

• Why?• Run the test manually…• Variable case matters!– Lower case = constants– Upper case = variables

• Anonymous variables– Use _

Page 14: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

Prolog power

• An example of declarative logic and inference

• The game of clue• What are you declaring in clue?• What are you trying to find out?• How does this relate to RL relevance?

Page 15: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

The miracle of trace

• See exactly what is happening during evaluation

• Turn on with trace.• See what happens when you load a

file?• What happens when you call a

function?

Page 16: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

Numbers

• Working with constants– True/false 8 is 6+2.– Setting a value X is 4.

• Setting up formulas– formula(x,y) :- Y=X*2.

• Note formulas must be placed in knowledge base!– Test with 1) 3,A then 3,a then 3,4

• Why did it get the results it did?

Page 17: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

More capabilities

• Can do relational queries– A is 7, A>5.

• Note: , means and ; means or – What are the implications of using the ; above?– Can also use Rule1 -> Rule2 to only execute

Rule2 if Rule1 is true

• Trivia: What does the, mean in prolog• How could we prompt the user for their

salary and print out what the percentage they pay in tax is (30%)?

Page 18: ITEC 380 Organization of programming languages Lecture 8 – Prolog

Prolog

Next week

• More in-depth with prolog– Lists– Recursion