Prolog Notes Links to tutorials on course webpage Example programs on csserver in /home/hwang/cs430/prolog Monday, April 7 CS 430 Artificial Intelligence 1 Prolog PROgramming LOGic language, uses firstorder logic back-chaining to drive computations Will use GNU Prolog (gprolog) for this course, most other Prolog implementations have similar syntax. Links available on course webpage Identifier syntax convention is opposite FOL Variables start with uppercase letters Constants and predicate names start with lowercase Comments: % to end of line or /* */ pairs Monday, April 7 CS 430 Artificial Intelligence 2 Basic Syntax - Terms Atoms: symbols, single-quoted sequence, builtin operators Numbers: the usual Variables: the usual, one special "anonymous" variable underscore ( _ ) Structures: predicates and nested predicates, names can be overloaded with different arity Terms cannot have space between predicate and '(' Monday, April 7 CS 430 Artificial Intelligence 3 Basic Syntax - Built-Ins Operators :­ left implication () = unification , conjunction \= non-unification ; disjunction is expression evaluation & assignment Predicates exit the interpreter halt write display argument nl Monday, April 7 display newline CS 430 Artificial Intelligence 4 Interpreter Read-execute-write loop against a KB. Prompt is: ?­ Given a query sentence, responds "yes" if can be proved and "no" if it cannot. All interaction is via query sentences, so "command" to exit interpreter is: ?­ halt. (note the period at the end of the sentence) Monday, April 7 CS 430 Artificial Intelligence 5 Consulting Reading in a file of facts and rules is called consulting. Query to do this: [<filename>]. E.g., ?­ [criminal]. Note: no quotation marks around the file name. Also, will assume extension of ".pl" To read from keyboard, consult the user with [user]. Ctrl-D indicates end of input. Note: there are no retained sentences from previous consults. Monday, April 7 CS 430 Artificial Intelligence 6 Knowledge Bases KB is a set of sentences. <head> :- <term1>, <term2>, ..., <termn>. means <term1> ∧ <term2> ∧...∧ <termn> <head> sibling(X, Y) :- parent(P, X), parent(P, Y), X \= Y. If no variables, sentence is called a fact female(mary). parent(mary, george). Monday, April 7 CS 430 Artificial Intelligence 7 Knowledge Bases Sentences with variables are called rules. Equivalent of a function. May consist only of a head. vertical(line(point(X,Y)), line(point(X,Z))). Often, rules are "recursive" Base case must be listed first. Recursive case is a rule of the same name that uses the rule on the right-hand side. Monday, April 7 CS 430 Artificial Intelligence 8 Structures Structures are compound terms used to group data. Examples: owns(john, 'Wuthering Heights'). owns(john, book('Wuthering Heights', bronte)). owns(john, book('Wuthering Heights', author(bronte, emily))). owns(john, book('Jane Eyre', author(bronte, charlotte))). Write a query that asks: "Does John own any books by any author whose last name is Bronte?" Monday, April 7 CS 430 Artificial Intelligence 9 Lists Lists are enclosed in [ ] and separated by commas. Empty list is [ ]. May be nested. E.g., [a, [b,c], d] Built-in list functions like member(Elem,List) and append(List1,List2,Result) Manipulated by matching head (first element) and tail (rest of elements) using [H|T] notation Write function rules to answer queries: "What is the last element of a list?" "What is the length of a list?" Monday, April 7 CS 430 Artificial Intelligence 10 Arithmetic Operators X =:= Y X =\= Y X =< Y <, >, >= +, -, *, / // mod X and Y stand for the same number X and Y stand for different numbers X less than or equal to usual meanings usual meanings integer quotient integer remainder Write function rules for Minimum. Write Population and Area facts, and a function to compute Density Monday, April 7 CS 430 Artificial Intelligence 11 Debugging Can turn on the debugger for tracing using Default is single-stepping using command 'c' (for creep). Command 'a' to abort run. Turn off tracing using ?­ trace. ?­ notrace. See gprolog manual for setting breakpoints, etc. Monday, April 7 CS 430 Artificial Intelligence 12 Backtracking When a goal fails (or user requests alternative answers), Prolog backtracks up proof tree to consider other possibilities for unification Sometimes don't want this to happen Know there's only one match Know if get to a particular place in proof tree, query will fail Only want one result Control backtracking with cut (!) operator Monday, April 7 CS 430 Artificial Intelligence 13 Cut (!) Cut (!) operator is a built-in term Effect of cut is: Any variables that are bound to values at this point cannot take on other values No other versions of predicates called before the cut will be considered No other subsequent version of the predicate at the head of the current rule will be considered The cut always succeeds. Monday, April 7 CS 430 Artificial Intelligence 14 Prolog Problems Compute letter grades Compute sum of 1 to N Use cut for efficiency Use cut to produce one answer; prevents infinite recursion when there is a request for backtracking. Display instructions for Tower of Hanoi Monday, April 7 CS 430 Artificial Intelligence 15