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.

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.
AI/ES (Artificial Intelligence / Expert System) Visual Prolog: Part 2
Introduction to Recursion and Recursive Algorithms
1. An Overview of Prolog.
1 Part 1 The Prolog Language Chapter 2 Syntax and Meaning of Prolog programs.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
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.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
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).
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Prolog OR (disjunction) “;” is same as a logical OR “;” is same as a logical OR It is also equivalent to using separate clauses... It is also equivalent.
2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and.
Prolog Programming for Artificial Intelligence Three edition 2001
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
Programming Logic and Design Sixth Edition
Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 2 - Algorithms and Design
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
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.
Flow of Control Chapter 5. Flow of Control n What order computer uses to get answers –sub-goal ordering –clause ordering n Prolog flow-of-control –sequence.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
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.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping with.
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Chapter 12. Recursion Basics of Recursion Programming with Recursion Computer Programming with JAVA.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Cs774 (Prasad)L6Backtracking1 Controlling Backtracking : Cuts
Lists in Prolog Sections 3.1, 3.2. Lists n List = sequence of values –[1, 2, 3, 4, 5] –[bob, brian, cathy, mark, david, loretta] –[birds(4, calling),
Prolog Unification and clause order. The notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
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.
Computer Science 1000 Algorithms III. Multiple Inputs suppose I ask you to write a program that computes the area of a rectangle area = length * width.
1 TP #2: How does Prolog answer questions? n Miscellaneous info; n Last TP exercises solved; n How does Prolog answer questions? n Recursive Prolog programs;
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures (predicates),
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog Primarily for Symbolic (nonnumeric) Computation
Presentation transcript:

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 –\+/1this is a prefix operator n \+ is the preferred way to go n not is the traditional way to go

Non-Members n a is not a member of [q, w, e, r, t, y] ?- \+ member(a, [q,w,e,r,t,y]). Yes n Admission prices revised admission(Member, 2) :- club_member(Member). admission(NonMember, 5) :- \+ club_member(NonMember).

How Not Works n “Negation by failure” –not tries to prove its argument –if the argument fails, not succeeds n \+ club_member(mark). –tries to prove club_member(mark). –if club_member(mark) succeeds, \+ fails –if club_member(mark) fails, \+ succeeds

Definition of Not \+ G :- G, !, fail ;true. n Some dialects require \+ G :- call(G), !, fail ;true.

Not versus Cut n Not is generally less efficient than cut –club_member called twice for non-members n Not is more logical than cut –can rearrange clauses so not-clause comes first n Exercise: rewrite costToBuy/3 using \+ instead of !

Variables Under the Not n Handled by the predicate call ?- \+ parent(jim, X). X = _G301 Yes n Tried to find an X that jim was parent of –that failed –\+ succeeded –X remains unbound (OK – no child of jim)

Variables Under the Not n Failure results in no variable bindings ?- \+ parent(bob, X). No n Tried to find an X that bob was parent of –that succeeded (X = ann) –so \+ failed –answer was No, so no bindings printed

Not Never Binds Variables n No bindings printed for \+ questions –success means no values were found –failure means no bindings survive n Can’t use \+ or not to generate counter- examples –can’t say “find an X such that bob is not the parent of X”

Finding Bob’s Non-Children n Need to make X into someone first person(X) :- parent(X, _). person(X) :- parent(_, X). nonparent(X, Y) :- person(X), person(Y), \+ parent(X, Y). n Now can find nonparents ?- nonparent(bob, X). X = pam

Not Does Not Commute n \+’s variables should be as instantiated as necessary when called –otherwise it may prevent solutions being found ?- \+ parent(X, Y), X = jim, Y = bob. No ?- X = jim, Y = bob, \+ parent(X, Y). X = jim, Y = bob Yes

Exercise n Write clauses for likes/2 for Michele –Michele likes animals –except spiders and snakes –except she likes Sammy (who is a snake) n Use \+ instead of cut, fail and disjunction likes(michele, X) :- …

Using Negation to Block Cycles n Monkey example very “fragile” –change order of actions – get infinite recursion –put walking first, monkey just tries walking all over the place, never climbs on or pushes box n Want to avoid monkey going into a loop –getting into a state he was in before –not making progress toward the goal

Avoiding Cycles n Whenever monkey makes a move… n …he checks to see if he’s been in the new state before –if so, reject that move –if not, carry on looking for other moves n Need to keep track of previous states –an extra argument –add previous states as we go along

Keeping Track of Old States % canget(State, OldStates): % monkey can get banana from State % without repeating any of his OldStates canget(state( _, _, _, has), _). canget(State1, OldStates) :- move(State1, Move, State2), move(State1, Move, State2), \+ member(State2, OldStates), \+ member(State2, OldStates), canget(State2, [State1|OldStates]). canget(State2, [State1|OldStates]).

Passing Down vs Passing Up n Compare above with building list of moves canget(…, []). vs canget(…, _). and canget(…, [Move|Moves]) :- … canget(…, Moves). vs canget(Old, Olds) :- … canget(…, [Old|Olds]). Head passes up Body passes down

Combining Down & Up n Can do it all in Prolog canget(state(_,_,_,has), _, []). canget(Old, Olds, [Move|Moves]) :- move(Old, Move, New), \+ member(New, Olds), canget(New, [Old|Olds], Moves).

Hiding Arguments n Can have different number of arguments with same predicate name –hide arguments that always start out the same canget(StartState, Moves) :- canget(StartState, [], Moves). canget(Moves) :- canget(state(atdoor,onfloor,atwindow,hasnot),Moves).

Compound Nots n Suppose we want to define none_parents/1 –argument is a list with no parents in it n Could go down list & check each element –just like we did for all_male n But could just say: no member of this list is a parent –how do we say that?

Wrong Answers none_parents_1(List) :- member(P, List), \+ parent(P). none_parents_2(List) :- parent(P), \+ member(P, List). some member is not a parent [pam, bob, ann] some parent is not a member [pam, bob, liz] tom not there

Correct Answer n Not the case that a member is a parent none_parents(List) :- \+ (member(P, List), parent(P)). n Parentheses around the compound argument –\+ binds closer than, –use parentheses to change order n If there is a member who is a parent, fail –otherwise, you’re OK

Exercise n Write a predicate no_brothers/1 –no one in the argument is brother to anyone else in the argument % no_brothers( +ListOfPeople )

First Midterm Test Prolog

First Test n Next Thursday (February 7) n In class (HSH 206) n Topics –chapters 1 to 5 (in-class material only) n Closed book n Closed computer n More doing than saying

Doing n Writing Prolog code –facts –rules –queries n Reading & interpreting Prolog code –saying what Prolog’s response would be –“tracing execution”

Next Time n Input and Output –Chapter 6