1 / 16

Artificial Intelligence

This lecture covers the concepts of test vs find operators, built-in predicates, list operations, and default vs logical negation in Prolog programming.

soliss
Download Presentation

Artificial Intelligence

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. Artificial Intelligence Lecture 5

  2. more Prolog • test vs. find • built-in predicates 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] ; X = _G209 Y = [_G275,_G209|_G278] Yes 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(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 Yes ?- reverse([a,b,a,c], Rev), delete(Rev, a, Del). Rev = [c,a,b,a] Del = [c,b] Yes ?- select(a, [a,b,a,c], R). R = [b,a,c] ; R = [a,b,c] ; No

  6. not predicate ?- not(is_list([])). No ?- member(d, [a,b,c]). No ?- not(member(d, [a,b,c])). Yes ?- member(X, [a,b,c]). X = a Yes ?- not(member(X, [a,b,c])). No ?- X = d, not(member(X, [a,b,c])). X = d Yes ?- not(member(X, [a,b,c])), X = d. No • 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 Yes ?- [H|T] = [a,b,c]. H = a T = [b,c] Yes ?- foo \= bar. Yes ?- X \= foo. No ?- 4+2 = 6. No • X = Ydoes more than test equality, it matches X with Y, instantiating variables if necessary • X \= Ydetermines 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  +(4, 2) • can force evaluation using 'is' • ?- X = 4 + 2. ?- X is 4 + 2. • X = 4+2 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) 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., 2 + 3 postfix e.g., 5! • precedence 2 + 3 * 4  2 + (3 * 4) • associativity 8 – 5 - 2  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 :- op(300, xfx, likes). :- op(250, xfy, and). dave likes cubs. kelly likes java and scheme and prolog. • %%% likes.pro • likes(dave, cubs). • likes(kelly, and(java, and(scheme,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 Testchoose the most likely answer by analogy Question 1: Question 3: Question 2:

  15. Next week… • AI as search • 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 ???????

More Related