180 likes | 372 Views
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.
E N D
ITEC 380 Organization of programming languages Lecture 8 – Prolog
Review • Prolog • What are its 2 major components?
Objectives • Prolog • Basic principles • Examples • Example programs
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?
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;
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
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)
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
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
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).
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
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).
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 _
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?
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?
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?
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%)?
Next week • More in-depth with prolog • Lists • Recursion