CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Prolog Programming (Volume 2) Dr W.F. Clocksin. Lists Lists are the same as other languages (such as ML) in that a list of terms of any length is composed.
CS344: Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 12–Prolog examples: Himalayan club, member, rem_duplicate,
Chapter 11 :: Logic Languages
Prolog.
Declarative Programming Lists in PROLOG Autumn 2014.
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).
A Third Look At Prolog Chapter Twenty-TwoModern Programming Languages, 2nd ed.1.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
Declarative Programming Autumn 2014 Basic syntax and sample programs.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
LING 388 Language and Computers Lecture 2 9/04/03 Sandiway FONG.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 13 Slides Courtesy to: Peter LO.
Formal Models of Computation Part II The Logic Model
CS 321 Programming Languages and Compilers Prolog part 2.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog
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.
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.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/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.
1 CS2136: Paradigms of Computation Class 04: Prolog: Goals Backtracking Syntax Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
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.
Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain).
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
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.
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.
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.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Lecture 2: Unification and proof search.
More on Prolog syntax Already seen program statement types: Already seen program statement types: rules: p(X) :- q(X,Y), r(Y,z). rules: p(X) :- q(X,Y),
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
Matching Matching and unification. Rules for matching terms. Examples of matching. –The most general instantiation. Computation using matching.
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 fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Chapter 11 :: Logic Languages
Artificial Intelligence CS370D
A closer look to prolog San Nabaty
Chapter 12 :: Logic Languages
Programming Paradigms and Languages
Programming Techniques
Chapter 2 Syntax and meaning of prolog programs
Chapter 12 :: Logic Languages
Presentation transcript:

CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21

2 Variables Same database: likes(john, flowers). likes(john, mary). likes(paul, mary). Ask who likes Mary: ?- likes(X, mary). X = john ; X = paul ; no

3 Conjunctions Consider the database: likes(mary, chocolate). likes(mary, wine). likes(john, wine). likes(john, mary). Ask if John and Mary like each other ?- likes(john, mary), likes(mary, john).% comma between two goals represents % their conjunction ("and") no −need to ask if John likes Mary and Mary likes John How does Prolog try to satisfy a goal (or conjunction of goals)? −backtracking (backward-chaining)

4 Backtracking Same database: likes(mary, chocolate). likes(mary, wine). likes(john, wine). likes(john, mary). ?- likes(mary, X), likes(john, X). % scope of X is the entire clause 1. Try to satisfy first goal likes(mary, X), where X is uninstantiated −Succeeds in unifying with likes(mary, chocolate) −X becomes instantiated to chocolate −Mark the place in database where first goal succeeded Ask if there is something that both Mary and John like: 2. Try to satisfy second goal likes(john, chocolate) – it fails

5 Backtracking (cont.) Same database: likes(mary, chocolate). likes(mary, wine). likes(john, wine). likes(john, mary). ?- likes(mary, X), likes(john, X). % scope of X is the entire clause Ask if there is something that both Mary and John like: 3. Try to re-satisfy first goal likes(mary, X), with X again uninstantiated −Start from the marker of first goal – succeeds in unifying with likes(mary, wine) −X becomes instantiated to wine −Mark the place in database where first goal succeeded 4. Try to satisfy second goal likes(john, wine) −Succeeds in unifying with likes(john, wine) −Mark the place in database where second goal succeeded −Now the query is satisfied: X = wine ;% ask for more answers – try to re-satisfy second goal likes(john, wine) from its marker – it fails % try to re-satisfy first goal likes(mary, X) from no its marker – it fails → no more answers

6 Rules Rules – express a predicate that depends on other predicates John likes anyone who likes wine: likes(john, X) :- likes(X, wine). John likes any female who likes wine: likes(john, X) :- female(X), likes(X, wine). A bird is an animal which has feathers: bird(X) :- animal(X), has_feathers(X). A rule has a head (bird(X)) and a body (animal(X), has_feathers(X)) :- is read "if" Scope of variables: −the scope of a variable is only the clause in which it appears −all X should represent the same object in second rule above −X in likes(john, X) and X in bird(X) do not have anything in common

7 Backtracking with Rules Consider the database: male(albert). male(edward). female(alice). female(victoria). parents(edward, victoria, albert).% victoria and albert are parents of edward parents(alice, victoria, albert). sister_of(X, Y) :- female(X), parents(X, M, F), parents(Y, M, F). ?- sister_of(alice, edward). 1. Query unifies with sister_of(X, Y), so X is alice, and Y is edward Now try to satisfy goals in the body, one by one 2. Try to satisfy female(alice) - succeeds Place a marker for this goal (at 3 rd clause)

8 Backtracking with Rules (cont.) Consider the database: male(albert). male(edward). female(alice). female(victoria). parents(edward, victoria, albert).% victoria and albert are parents of edward parents(alice, victoria, albert). sister_of(X, Y) :- female(X), parents(X, M, F), parents(Y, M, F). ?- sister_of(alice, edward). 3. Try to satisfy parents(alice, M, F) – succeeds, M is victoria, and F is albert Place a marker for this goal (at 6 th clause) 4. Try to satisfy parents(edward, victoria, albert) – succeeds Place a marker for this goal (at 5 th clause) Now the entire query succeeds: yes

9 Rules – Shared Variables Same database: male(albert). male(edward). female(alice). female(victoria). parents(edward, victoria, albert).% victoria and albert are parents of edward parents(alice, victoria, albert). sister_of(X, Y) :- female(X), parents(X, M, F), parents(Y, M, F).% clause 7 ?- sister_of(alice, X). X = edward ; no X in the query sister_of(alice, X) is not the same as X in clause 7 When unifying query with head of clause 7: −X in clause 7 will instantiate with alice −Y in clause 7 and X in query remain uninstantiated, but they are shared (co- refererences) −when one becomes instantiated, the other will be instantiated to the same object

10 Anonymous Variables male(albert). male(edward). male(jimmy). female(alice). female(victoria). parents(edward, victoria, albert).% victoria and albert are parents of edward parents(alice, victoria, albert). parents(jimmy, lisa, albert). sister_of(X, Y) :- female(X), parents(X, M, F), parents(Y, M, F). Is Alice anyone's sister? ?- sister_of(alice, _).% _ is a placeholder for an anonymous variable % don't need to know who it is yes Do Alice and Jimmy have the same father? ?- parents(alice, _, X), parents(jimmy, _, X). X = albert ;% anonymous variables do not co-refer with any other % variable or anonymous variable no

11 Structures Structures can also appear in predicates: owns(john, book(wuthering_heights, author(emily, bronte))). ?- owns(john, book(X, author(Y, bronte))). X = wuthering_heights Y = emily yes Can have queries about components of structures – ask if John owns a book by any of the Bronte sisters: ?- owns(john, book(_, author(_, bronte))). yes Why do predicates and structures have the same syntax? Or, with anonymous variables: −convenient to represent a Prolog program as a set of structures −can add/remove clauses dynamically, at run-time

12 Unification Prolog rules for unification: −if the object has a value (is a constant or an instantiated variable), the first variable becomes instantiated to that object A structure will unify with another structure if they have the same functor and arity (number of arguments), and the corresponding arguments also unify recursively; exactly the same rule also applies to predicates An uninstantiated variable unifies with any object −if the object is an uninstantiated variable, the two variables will remain uninstantiated, but will co-refer A constant unifies only with itself or with an uninstantiated variable

13 The Occurs Check Consider the following query: ?- p(X) = X. Should p(X) and X unify? −Conceptually, no – for any constant a, it's obvious that p(a) is not the same as a −The answer is: X = p(p(p(p(p(p(p(p(p(p(...)))))))))) yes −However, checking for such occurrences would be potentially time- consuming −Therefore, this occurs check is not performed in Prolog −An uninstantiated variable unifies with anything

14 Programming with Matching Example (horizontal and vertical lines): vertical(line(point(X,Y), point(X,Z))). horizontal(line(point(X,Y), point(Z,Y))). ?- vertical(line(point(1, 1), point(1, 3))). yes ?- horizontal(line(point(1, 1), point(2, Y))). Y = 1 ; no ?- horizontal(line(point(2, 3), P)). P = point(_G405,3) ;% any x-coordinate will do no

15 Equality Equality - defined in terms of "unifiability" The goal =(X, Y) which can also be written as X = Y, succeeds if and only if X and Y can unify ?- a = a. ?- a = b. yes% constant unifies with itself ?- foo(a, b) = foo(a, b). no% but not with another constant yes% structures are recursively identical ?- X = a. X = a ;% variable unifies with constant, also becomes instantiated no% only once Examples:

16 Equality ?- foo(a, b) = foo(X, b). ?- foo(a, b) = X. X = a ;% arguments must unify no% only one possibility ?- X = Y. X = foo(a, b) ;% variable unifies with anything no% only once X = _G204% variables remain uninstantiated, but will co-refer Y = _G204 ;% _G204 is some implementation tag that represents their (shared) location no More examples:

17 Arithmetic −+(2, 3) which can also be written as 2 + 3, is a two-argument structure, not a function call – it will not unify with 5: −To actually compute the value of an expression – use the infix predicate is: −succeeds if it can unify its first argument (must be a variable) with the arithmetic value of its second argument The usual arithmetic operators are available −however, they are functors, not functions ?- X is 1+2. X = 3% evaluates the arithmetic expression ?- (2 + 3) = 5. no ?- X = X = 2 + 3

18 Arithmetic ?- 1+2 is 4-1. More examples: no% first argument is not a variable ?- X is Y. ERROR% second argument must be instantiated ?- Y is 1+2, X is Y. X = 3 Y = 3% Y is instantiated by the time it is needed

19 Arithmetic Predicates for comparing numbers: X =:= Ysame number X =\= Ydifferent numbers X < Y X > Y X =< Yless than or equal (not the usual <= notation) X >= Y Both arguments must be instantiated All these predicates also evaluate expressions: ?- 3 =:= 2+1. yes ?- 3*2 < 7+1. yes

20 Arithmetic Example (reigns of Princes of Wales in 9th and 10th centuries): reigns(rhodri, 844, 878). reigns(anarawd, 878, 916). reigns(hywel_dda, 916, 950). reigns(lago_ap_idwal, 950, 979). reigns(hywel_ap_ieuaf, 979, 985). reigns(cadwallon, 985, 986). reigns(maredudd, 986, 999). prince(X, Y) :- reigns(X, A, B), Y >= A, Y =< B.% X was a prince during year Y ?- prince(cadwallon, 986). yes ?- prince(X, 979). X = lago-ap_idwal ; X = hywel_ap_ieuaf ; no

21 Arithmetic Example (compute the population density): pop(india, 548).% in millions (old data) pop(china, 800). pop(brazil, 108). area(india, 1).% in millions of square miles area(china, 4). area(brazil, 3). density(X, Y) :- pop(X, P), area(X, A), Y is P/A. ?- density(china, X). X = 200 yes ?- density(turkey, X). no

22 Lists List – non-homogeneous collection of elements −Defined recursively.(a,.(b,.(c, []))). is a functor (similar to cons) that builds a list from head and tail [] is the empty list −Shorthand notation: [a, b, c] −Can also specify a list by its first elements (not only head), and the rest (tail): [a | [b, c]] [a, b | [c]] [a, b, c | []]

23 Lists Instantiations with lists: p([1, 2, 3]). p([the, cat, sat, [on, the, mat]]). ?- p([X|Y]). X = 1 Y = [2, 3] ; X = the Y = [cat, sat, [on, the, mat]] ; no ?- p([_, _, _, [_|X]]). X = [the, mat] ; no

24 Lists Instantiations with lists: List 1List 2Instantiations [X, Y, Z][john, likes, fish] [cat][X|Y] [X, Y|Z][mary, likes, wine] [[the, Y]|Z][[X, hare], [is, here]] X = john Y = likes Z = fish X = cat Y = [] X = mary Y = likes Z = [wine] X = the Y = hare Z = [[is, here]]

25 Lists Instantiations with lists (cont.): List 1List 2Instantiations [golden|T][golden, norfolk] [black, horse][horse, X] [white|Q][P|horse] P = white Q = horse (none) T = [norfolk] Proper and improper lists: ?- [white|[horse]] =.(white,.(horse, [])). yes% proper list ?- [white|horse] =.(white,.(horse, [])). no ?- [white|horse] =.(white, horse). yes% improper list (tail is not a list)

26 Announcements Readings −Prolog resources