Presentation is loading. Please wait.

Presentation is loading. Please wait.

Artificial Intelligence

Similar presentations


Presentation on theme: "Artificial Intelligence"— Presentation transcript:

1 Artificial Intelligence
Lecture 5

2 more Prolog test vs. find built-in predicates user-defined operators
list operations: member, append, nth0, reverse, … not, default vs. logical negation comparison operators, arithmetic user-defined operators position, precedence, associativity

3 Test vs. find Prolog programs define relations:
query w/ constants: "test" whether relation holds between the constants query w/ variables: "find" values for which the relation holds ?- member(X, [a, b, c]). X = a ; X = b ; X = c ; No ?- member(a, X). X = [a|_G260] ; X = [_G259,a|_G262] Yes ?- member(X, Y). X = _G209 Y = [_G209|_G270] ; Y = [_G275,_G209|_G278] with a variable as first argument, the query is used to "find" members of the given list with a variable as the second argument, the query is used to "find" lists that have the given item as member with both variables, the query is used to "find" general schemas for list membership

4 Append predicate another useful predefined predicate for list manipulation append(L1,L2,L3): L3 is the result of placing the items in L1 at the front of L2 can be used in reverse to partition a list ?- append([a,b], [c,d], L). L = [a,b,c,d] Yes ?- append(L1, L2, [a,b,c,d]). L1 = [] L2 = [a,b,c,d] ; L1 = [a] L2 = [b,c,d] ; L1 = [a,b] L2 = [c,d] ; L1 = [a,b,c] L2 = [d] ; L1 = [a,b,c,d] L2 = [] ; No

5 Other list predicates ?- is_list([]). Yes
is_list(Term): succeeds if Term is a list length(List,Len): Len is the number of items in List nth0(Index,List,Item): Item is the item at index Index of List(starting at 0) nth1(Index,List,Item): Item is the item at index Index of List(starting at 1) reverse(List,RevList): RevList is List with the items in reverse order delete(List,Item,NewList): NewList is the result of deleting every occurrence of Item from List select(Item,List,Remain): Remain is the List with an occurrence of Item removed ?- is_list([]). Yes ?- L = [a,b,c,d], length(L, Len), nth1(Len, L, X). L = [a,b,c,d] Len = 4 X = d ?- reverse([a,b,a,c], Rev), delete(Rev, a, Del). Rev = [c,a,b,a] Del = [c,b] ?- select(a, [a,b,a,c], R). R = [b,a,c] ; R = [a,b,c] ; No

6 not predicate anything Prolog can't prove true it assumes to be false!
?- not(is_list([])). No ?- member(d, [a,b,c]). ?- not(member(d, [a,b,c])). Yes ?- member(X, [a,b,c]). X = a ?- not(member(X, [a,b,c])). ?- X = d, not(member(X, [a,b,c])). X = d ?- not(member(X, [a,b,c])), X = d. not defines the (default) negation of conditional statements when applied to relations with no variables, it is equivalent to logical negation () in reality, it returns the opposite of whatever its argument would return if X would succeed as a query, then not(X) fails if X would fail as a query, then not(X) succeeds anything Prolog can't prove true it assumes to be false!

7 Programming exercises
suppose we want to define a relation to test if a list is palindromic palindrome(List): succeeds if List is a list whose elements are the same backwards & forwards

8 Programming exercises
palindrome([]). palindrome(List) :- reverse(List, Rev), List = Rev. suppose we want to define relation to see if a list has duplicates has_dupes(List): succeeds if List has at least one duplicate element has_dupes([H|T]) :- member(H, T). has_dupes([_|T]) :- has_dupes(T). suppose we want the opposite relation, that a list has no dupes no_dupes(List): succeeds if List has no duplicate elements no_dupes(List) :- not( has_dupes(List) ).

9 Built-in comparison operators
?- foo = foo. Yes ?- X = foo. X = foo ?- [H|T] = [a,b,c]. H = a T = [b,c] ?- foo \= bar. ?- X \= foo. No ?- 4+2 = 6. X = Y does more than test equality, it matches X with Y, instantiating variables if necessary X \= Y determines whether X & Y are not unifiable for ground terms, this means inequality can think of as: not(X = Y) again, doesn't make a lot of sense for variables Note: arithmetic operators (+, -, *, /) are not evaluated  +(4, 2) can force evaluation using 'is' ?- X = ?- X is X = X = 6 Yes Yes

10 Programming exercise suppose we want to define relation to see how many times an item occurs in a list num_occur(Item,List,N): Item occurs N times in List num_occur(_,[],0). num_occur(H, [H|T], N) :- num_occur(H, T, TailN), N is TailN+1. num_occur(H, [_|T], N) :- num_occur(H, T, TailN), N is TailN. is the first answer supplied by this relation correct? are subsequent answers obtained via backtracking correct?

11 User-defined operators
it is sometimes convenient to write functors/predicates as operators predefined: +(2, 3)  user defined? likes(dave, cubs)  dave likes cubs operators have the following characteristics position of appearance prefix e.g., -3 infix e.g., postfix e.g., 5! precedence 2 + 3 * 4  2 + (3 * 4) associativity 8 –  8 – (5 – 2)

12 op new operators may be defined as follows
:- op(Prec, PosAssoc, Name). Name is a constant Prec is an integer in range 0 – 1200 (lower number binds tighter) PosAssoc is a constant of the form xf, yf (postfix) fx, fy (prefix) xfx, xfy, yfx, yfy (infix) the location of f denotes the operator position x means only operators of lower precedence may appear here y allows operators of lower or equal precedence Example: :- op(300, xfx, likes).

13 Operator example %%% likes.pro likes(dave, cubs). likes(kelly, and(java, and(scheme,prolog))). %%% likes.pro :- op(300, xfx, likes). :- op(250, xfy, and). dave likes cubs. kelly likes java and scheme and prolog. ?- likes(dave, X). X = cubs Yes ?- likes(Who, What). Who = dave What = cubs ; Who = kelly What = and(java, and(scheme,prolog)) ; No ?- dave likes X. X = cubs Yes ?- Who likes What. Who = dave What = cubs ; Who = kelly What = java and scheme and prolog ; No by defining functors/predicates as operators, can make code more English-like

14 IQ Test choose the most likely answer by analogy
Question 1: IQ Test choose the most likely answer by analogy Question 3: Question 2:

15 Next week… AI as search Be prepared for a quiz on
problem states, state spaces uninformed search strategies depth first search breadth first search iterative deepening Be prepared for a quiz on this week’s lecture (moderately thorough) the reading (superficial)

16 Questions & Answers ???????


Download ppt "Artificial Intelligence"

Similar presentations


Ads by Google