1 CS2136: Paradigms of Computation Class 04: Prolog: Goals Backtracking Syntax Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel.

Slides:



Advertisements
Similar presentations
CS344: Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 12–Prolog examples: Himalayan club, member, rem_duplicate,
Advertisements

Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.
Chapter 11 :: Logic Languages
Prolog for Dummies Ulf Nilsson Dept of Computer and Information Science Linköping University.
Prolog.
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 The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 16- Prolog.
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).
2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and.
4. PROLOG Data Objects And PROLOG Arithmetic
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
COP4020 Programming Languages Logic programming and Prolog Prof. Xin Yuan.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Prolog Ross (Tate). Filling in the Blanks Rather than reverse((a,b)) returns (b,a) Rather than reverse((a,b)) returns (b,a) What X makes reverse((a,b),
CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 22, 23- Prolog.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 13 Slides Courtesy to: Peter LO.
P-Phunck - Prolog1 Introduction to Prolog Brian Paden.
Formal Models of Computation Part II The Logic Model
1 CS2136: Paradigms of Computation Class 03: Logic Programming Prolog Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog
F28PL1 Programming Languages Lecture 16: Prolog 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
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.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
By: Cory Canter CSC 415 Programming Languages. History  Created by Alain Colmerauer, Phillipe Roussel and Robert Kowalski in 1971  Started as a natural.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
Logic Programming Dr. Yasser Ahmed Nada Fall 2010/2011 Lecture 2 1 Logic Programming.
Lecture course: Heuristics & Prolog for Artificial Intelligence Applications Notes on Prolog from Professor Bruce Batchelor, DSc.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Knowledge Based Information System
Artificial Intelligence CS370D
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
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.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
Chapter 2 Syntax and meaning of prolog programs Part 1.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Section 16.5, 16.6 plus other references
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
Prolog programming Introduction to Prolog (part2)
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Chapter 2 - Introduction to C Programming
A closer look to prolog San Nabaty
Chapter Two: Syntax and Meaning of Prolog Programs
Chapter 2: Prolog (Introduction and Basic Concepts)
PROLOG.
Presentation transcript:

1 CS2136: Paradigms of Computation Class 04: Prolog: Goals Backtracking Syntax Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel

2 Satisfying Goals

3 Variables (same as last time) zThey can stand for objects. zA variable is y“Instantiated” if it currently stands for an object. y“Not instantiated” if it doesn’t. zStart with an upper-case letter.

4 Questions with Variables (same as last time) zAlso known as “Goals”. zProlog looks for a match. zlikes(john,X). yLogically means: “Is there anything John likes?” yReally means: “Is there an X such that this is true?”

5 What It Means to Prolog z“Now if an uninstantiated variable appears in an argument, Prolog will allow that argument to match any other argument in the same position in the fact.” – C&M p. 9

6 Example zFile ‘likes.pl’: likes(john,mary). likes(john,gold). likes(mary,john). likes( john, football). likes(john,mary). zQuestions & Answers: ?- likes(john,X). X = mary [I hit RETURN here] Yes ?-

7 How It Works zProlog searches top-to-bottom through the database. zWhen it finds a match (“satisfies the goal”), X is instantiated to mary. zProlog marks the place in the database. zProlog prints all objects that variables now stand for. zYou hit RETURN—you’re done!

8 Continuing Instead ?- likes(john,X). [I type this] X = mary [I type ; ] X = gold [I type ; ] X = football [I type ; ] X = mary [I type ; ] No [No more matches] ?-

9 How This Case Works zProlog searches top-to-bottom through the database. zWhen it finds a match, X is instantiated to mary. zProlog marks the place in the database. zProlog prints all objects that variables now stand for. zYou hit semicolon—Prolog attempts to re-satisfy the goal, starting where it left off (not from the beginning).

10 What’s Happening ?- likes(john,X). [At this point, X is not instantiated] X = mary ; [X is instantiated to mary] X = gold ; [X is instantiated to gold] X = football ; [etc.] X = mary ; [etc.] No [No more matches] ?-

11 One More Time: Satisfy vs. Re-Satisfy zWhen Prolog tries to satisfy a goal, it starts from the top of the database, looking for a match. zWhen Prolog tries to re-satisfy a goal, it starts looking in the database from where it left off (i.e. just after the last match of that goal).

12 Conjunctions zProlog uses a comma to indicate AND. zTo get OR, for now just use separate goals or rules. yCould also use semicolon.

13 Conjunctions: AND zDo John and Mary like each other? ?- likes(john,mary), likes(mary,john). Yes ?- likes(john,gold), likes(gold,john). No zProlog tries to satisfy each goal. yEach of these examples has two goals.

14 Satisfying Multiple Goals I zWhat if the multiple goals involve variables? y?- likes( mary, X), likes(john, X). yFor the goal to be satisfied, the variable X must be instantiated to the same value in each goal. yIn other words, is there one object X that is liked by both Mary and John?

15 Satisfying Multiple Goals II zProlog tries to satisfy the first goal. zIf first goal is satisfied, Prolog marks the place and tries to satisfy the second goal. zIf second goal becomes satisfied, Prolog also marks that place. yEach goal has its own place marker. zWhat if the second goal is not satisfied? yNot satisfied = no match OR user says to try again. yMust “backtrack”.

16 Backtracking

17 Backtracking zWhen the second goal fails, uninstantiate X, then try to re-satisfy the first goal. yStart where you left off on the first goal. zIf the first goal gets re-satisfied, try to satisfy the second goal. yNot re-satisfy. yStart matching second goal from the beginning. zOrder is very important. yProlog always tries to satisfy goals left-to-right.

18 Backtracking Exercise

19 Rules

20 Rules zRules are general statements about objects and their relationships. zExample: yJohn likes everyone ySame as: John likes every object, if the object is a person. zAnother example: yJohn likes any woman who likes wine. ySame as: John likes X if X is a woman and X likes wine.

21 Rule Syntax zHead :- Body. z“:-” (colon, hyphen) pronounced “if”. zHead is the thing that is true if the Body is true.

22 Rule Example zSame example: yJohn likes any woman who likes wine. ySame as: John likes X if X is a woman and X likes wine. ylikes(john,X) :- likes(X,wine),female(X). yDon’t forget the dot!

23 Scope zlikes(john,X) :- likes(X,wine),female(X). zWhen X becomes instantiated, all X’s are instantiated within the scope of X. yIn this case, within the same rule.

24 Another Example male(albert). male(edward). female(alice). female(victoria). parents(edward, albert, victoria). parents(alice, albert, victoria). zNote: parents(child, father, mother). z Rule to test if X is the sister of Y: sister_of(X,Y) :- female(X), parents(X,M,F), parents(Y,M,F). z How to write are_sisters() ?

25 Clauses z2 ways to give Prolog information about predicates: yFacts yRules zThese are called clauses.

26 Prolog Syntax: Comments and Integers zComments like C or PL/I: y/* your comment here! */ zIntegers ySome versions of Prolog do not allow negative numbers. xSWI-Prolog does. xIt even allows non-integers.

27 Prolog Syntax: Atoms zAtoms (objects and predicates) must be lower case. yCan include digits and underscores (but cannot start with them). yObjects can be enclosed in single quotes.

28 Prolog Syntax: Variables zVariables must start with upper case or underscore (“_”). zSingle underscore is the anonymous variable. yDo not have to match each other, even in the same clause.

29 Structures a.k.a. Compound Terms zMade up of “functor” and “components”. zEach component acts as an object. zExamples: yowns(john,book(wuthering_heights,bronte)). yowns(john,book(wuthering_heights, author(emily,bronte))).

30 File ‘books.pl’ owns(john, book(wuthering_heights, author(emily, bronte))). owns(john, book(jane_eyre, author(charlotte, bronte))). owns(mary, book(wuthering_heights, author(emily, bronte))). owns(john, book(the_professor, author(charlotte, bronte))). owns(john, book(the_invisible_man, author(h_g, wells))). owns(mary, book(invisible_man, author(ralph, ellison))).

31 Structures and Goals zWhat do these test for? yowns(john,X). yowns(john,book(X, author(Y,bronte))). yowns(X,Y),owns(Z,Y).

32 Equality zEquals: “=“ zNot Equals: “\=“ zCan we eliminate duplicate owners from the last slide?

33 Next Time zMore Prolog yStructures yOperators yComplicated rules