1 / 18

ITEC 380

ITEC 380. Organization of programming languages Lecture 8 – Prolog. Review. Prolog What are its 2 major components?. Objectives. Prolog Basic principles Examples Example programs. Declarative Programming.

arich
Download Presentation

ITEC 380

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ITEC 380 Organization of programming languages Lecture 8 – Prolog

  2. Review • Prolog • What are its 2 major components?

  3. Objectives • Prolog • Basic principles • Examples • Example programs

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

  5. 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. 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. 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. 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. 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. 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. 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. 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 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. 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. 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. 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. Next week • More in-depth with prolog • Lists • Recursion

More Related