More Prolog test vs. find built-in predicates list operations: member, append, nth0, reverse, … not, default vs. logical negation comparison operators,

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Formal Models of Computation Part II The Logic Model
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
Part 1 The Prolog Language Chapter 3 Lists, Operators, Arithmetic
3. Lists, Operators, Arithmetic. Contents Representation of lists Some operations on lists Operator notation Arithmetic.
Logic Programming Lecture 9: Constraint logic programming.
Cs7120 (Prasad)L22-MetaPgm1 Meta-Programming
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Logic Programming (cont’d): Lists Lists in Prolog are represented by a functor (similar to cons in Scheme): 1.The empty list is represented by the constant.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
A Third Look At Prolog Chapter Twenty-TwoModern Programming Languages, 2nd ed.1.
TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University.
COEN Expressions and Assignment
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
1 Lecture Expert Systems &. 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses,
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Chapter 1, Part II: Predicate Logic With Question/Answer Animations.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Copyright © Curt Hill Quantifiers. Copyright © Curt Hill Introduction What we have seen is called propositional logic It includes.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate.
Artificial Intelligence
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Cs774 (Prasad)L4ListProcessing1 List processing in Prolog
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
1 Artificial Intelligence CS370D Prolog programming List operations.
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Chapter 2 Syntax and meaning of prolog programs Part 1.
Logic Programming Lecture 9: Constraint logic programming.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Section 16.5, 16.6 plus other references
Polymorphic Functions
Operators and Expressions
Expressions and Assignment
CS 100: Roadmap to Computing
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Tests, Backtracking, and Recursion
Artificial Intelligence CS370D
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Announcements Quiz 5 HW6 due October 23
Chapter 2 Syntax and meaning of prolog programs
Programming Languages
Presentation transcript:

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 Dave Reed

Test vs. find ?- 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 Prolog programs define relations: query w/ constants: "test" whether relation holds between the constants query w/ variables: "find" values for which the relation holds 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

Append predicate ?- 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 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 again, we could define append ourselves: append([], L, L). append([H|T], L, [H|A]) :- append(T, L, A). as we saw with ancestor example, clause ordering is important with recursive definitions if put rule first, infinite loop possible

Other list predicates ?- 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 ?- select(a, L, [b,c]). L = [a,b,c] ; L = [b,a,c] ; L = [b,c,a] ; No 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

not predicate 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! ?- is_list([]). Yes ?- 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

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 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) ).

Built-in comparison operators 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 = 4+2X = 6Yes ?- 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

Arithmetic operations arithmetic comparisons automatically evaluate expressions X =:= Y X and Y must both be arithmetic expressions (no variables) X =\= Y X > Y ?- 12 =:= 6+6. X >= Y Yes X < Y X =< Y ?- X =:= 6+6. ERROR: Arguments are not sufficiently instantiated Example: sum_of_list(ListOfNums, Sum) : Sum is the sum of numbers in ListOfNums sum_of_list([], 0). sum_of_list([H|T], Sum) :- sum_of_list(T,TailSum), Sum is H + TailSum.

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?

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 prefixe.g., -3 infixe.g., postfixe.g., 5! precedence * (3 * 4) associativity 8 – – (5 – 2)

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).

Operator example %% 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 %% likes.pro :- op(300, xfx, likes). :- op(250, xfy, and). dave likes cubs. kelly likes java and scheme and prolog. ?- 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

SWI-Prolog operators the following standard operators are predefined in SWI-Prolog to define new operators Name & Type are easy Precedence is tricky, must determine place in hierarchy Note: always define ops at top of the program (before use) 1200xfx -->, :- 1200fx :-, ?- 1150fx dynamic, multifile, module_transparent, discontiguous, volatile, initialization 1100xfy ;, | 1050xfy -> 1000xfy, 954xfy \ 900fy \+ 900fx ~ \=, \==, is 600xfy : 500yfx +, -, /\, \/, xor 500fx +, -, ?, \ 400yfx *, /, //, >, mod, rem 200xfx ** 200xfy ^

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

Analogy reasoner want to write a Prolog program for reasoning by analogy (Evans, 1968) need to decide on a representation of pictures constants: small, large, triangle, square, circle functor/operator: sized sized(small, triangle) OR small sized triangle sized(large, square) OR large sized square functors: inside, above inside(small sized circle, large sized square). above(large sized triangle, small sized square). need to represent questions as well operators: is_to, as (bind looser than sized, so higher prec #) predicate: question question(Name, F1 is_to F2 as F3 is_to [A1,A2,A3]).

IQ test questions :- op(200, xfy, is_to). :- op(200, xfy, as). :- op(180, xfy, sized). question(q1, inside(small sized square, large sized triangle) is_to inside(small sized triangle, large sized square) as inside(small sized circle, large sized square) is_to [inside(small sized circle, large sized triangle), inside(small sized square, large sized circle), inside(small sized triangle, large sized square)]). question(q2, inside(small sized circle, large sized square) is_to inside(small sized square, large sized circle) as above(small sized triangle, large sized triangle) is_to [above(small sized circle, large sized circle), inside(small sized triangle, large sized triangle), above(large sized triangle, small sized triangle)]). question(q3, above(small sized square, large sized circle) is_to above(large sized square, small sized circle) as above(small sized circle, large sized triangle) is_to [above(large sized circle, small sized triangle), inside(small sized circle, large sized triangle), above(large sized triangle, small sized square)]).

Analogy transformations transform(invertPosition, inside(small sized Figure1, large sized Figure2) is_to inside(small sized Figure2, large sized Figure1)). transform(invertPosition, above(Size1 sized Figure1, Size2 sized Figure2) is_to above(Size2 sized Figure2, Size1 sized Figure1)). transform(invertSizes, inside(small sized Figure1, large sized Figure2) is_to inside(small sized Figure2, large sized Figure1)). transform(invertSizes, above(Size1 sized Figure1, Size2 sized Figure2) is_to above(Size2 sized Figure1, Size1 sized Figure2)). also need to represent transformations predicate: transform transform(Name, F1 is_to F2). Note: different but related transformations can have the same name

Analogy reasoner %%%%%%%%%%%%%%%%%%%%%%%%%%% %% analogy.pro Dave Reed 1/23/02 %% %% A program based on Evans' analogy reasoner. %%%%%%%%%%%%%%%%%%%%%%%%%%% :- op(200, xfy, is_to). :- op(200, xfy, as). :- op(180, xfy, sized). analogy(Question, Solution) :- question(Question, F1 is_to F2 as F3 is_to Answers), transform(Rule, F1 is_to F2), transform(Rule, F3 is_to Solution), member(Solution, Answers). %% questions (as before) %% transformations (as before) to find an answer: 1.look up the question based on its name 2.find a transformation that takes F1 to F2 3.apply that rule to F3 to obtain a potential solution 4.test to see if that solution is among the answers to choose from

Analogy reasoner ?- analogy(q1, Answer). Answer = inside(small sized square, large sized circle) ; No ?- analogy(q2, Answer). Answer = above(large sized triangle, small sized triangle) ; No ?- analogy(q3, Answer). Answer = above(large sized circle, small sized triangle) ; No Note: Questions 1 & 2 yield the same answer twice. WHY? Is it possible for a questions to have different answers?

Handling ambiguities Question 4: ?- analogy(q4, Answer). Answer = above(small sized triangle, large sized circle) ; Answer = above(small sized circle, large sized triangle) ; No it is possible for 2 different transformations to produce different answers must either 1.refine the transformations (difficult) 2.use heuristics to pick most likely answer (approach taken by Green)