Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC 380 Organization of programming languages Lecture 8 – Prolog.

Similar presentations


Presentation on theme: "ITEC 380 Organization of programming languages Lecture 8 – Prolog."— Presentation transcript:

1 ITEC 380 Organization of programming languages Lecture 8 – Prolog

2 Prolog Review Prolog –What are its 2 major components?

3 Prolog Objectives Prolog –Basic principles –Examples –Example programs

4 Prolog Declarative Programmin g 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?

5 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;

6 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

7 Prolog Facts A true statement Syntax –Factname(identifier that is mapped to factname) –Factname(ident1, ident2) Means that ident1 is the Factname of ident2 Example male(Bob) female(Jill) parent(Jill, Bill) parent(Bob, Bill)

8 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

9 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

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

11 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

12 Prolog Expanded Making our previous knowledge base have I/O 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). iTester :- write('Type the mother\'s name\n'), read(Mom), write('Type the child\'s name\n'), read(Child), mother(Mom, Child).

13 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 _

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

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

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

17 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%)?

18 Prolog Next week More in-depth with prolog –Lists –Recursion


Download ppt "ITEC 380 Organization of programming languages Lecture 8 – Prolog."

Similar presentations


Ads by Google