Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,

Slides:



Advertisements
Similar presentations
1. An Overview of Prolog.
Advertisements

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.
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).
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
1 COMP313A Programming Languages Logic Programming (3)
Prolog programming Introduction to Prolog
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
Chapter 12 - Logic Programming
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
Rules Simple rules. Processing rules. Multiple sub-goals. PROLOG syntax. Recursive rules.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog (II) Lecture 22 – Prolog (II), Spring CSE3302 Programming Languages,
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.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Comp 205: Comparative Programming Languages Declarative Programming Languages Unification Backtracking Lecture notes, exercises, etc., can be found at:
School of Computing and Mathematics, University of Huddersfield Computing Science: WEEK 18 LECTURE: Comparative Programming Languages + Prolog PRACTICALS:
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Discussion #20 1/11 Discussion #20 Resolution in Datalog.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
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
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
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.
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.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
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 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CIS2326 Week2: Logic and Prolog Lee McCluskey First term:
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
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.
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.
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Logic Programming Dr. Yasser Ahmed Nada Fall 2010/2011 Lecture 2 1 Logic Programming.
CHA2555 Week2 Practical: Lee McCluskey First term:
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington.
COMP307 Artificial Intelligence Xiaoying Gao Victoria University of Wellington Lecture 2:1 
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.
C. Varela1 Logic Programming (PLP 11, CTM 9.1) Terms, Resolution, Unification, Search, Backtracking (Prolog) Relational Computation Model (Oz) Carlos Varela.
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.
Week 3 Help desks – Mon 4-5 CO238 Tutor: Su – Wed 1-2 CO238 Tutor: Urvesh – Fri CO238 Tutor: Bing Lectures: Mon, Tue, 3-4, HU119 Tutorial: Fri, 3-4,
For Friday No reading Prolog Handout 2. Homework.
1 Knowledge Based Systems (CM0377) Lecture 9 (Last modified 11th April 2002)
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.
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.
From Conventional Languages to Prolog –What we can do in conventional languages but not in Prolog –What we can do in Prolog but not in conventional languages.
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),
Carlos Varela Rennselaer Polytechnic Institute November 11, 2016
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Carlos Varela Rensselaer Polytechnic Institute November 14, 2017
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Chapter 2 Syntax and meaning of prolog programs
CSE 3302 Programming Languages
Presentation transcript:

Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms, lists Tail Recursion & Real Recursion U-bend & rope ladder to collect results Reading: Ch2: How to run Prolog, Ch3: The Prolog Language Ch5: Loading Programs Ch6: Debugging Ch7: Built-In Predicates

Comp 307 Lecture 4:2 min_number(+List, -Min) |?- min_number([3, 2, 6], X). X=2; min_number(, ):- new_min_number(,, ). new_min_number(,, ) :- new_min_number(,, ). new_min_number(,, ) :- new_min_number(,, ).

Comp 307 Lecture 4:3 min_number(+List, -Min) min_number(, ). min_number(, ):- min_number(, ), ………….. min_number(, ):- min_number(, ), …………………. Notes: it should be simplified by defining min2(+number1, +number2, Min) Be careful with “is” Tutorial and help desk details on the Web page.

Comp 307 Lecture 4:4 Prolog and a small rule based system How Prolog works? Matching Backtracking Rule based system

Comp 307 Lecture 4:5 The first program % father(Father, Child) father(bob, liz). father(bob, jim). father(jim, pam). father(jim, pat). mother(ann, jim). mother(pat, tom). parent(X,Y) :- mother(X,Y). parent(X,Y) :- father(X,Y). grandparent(X,Y) :- parent(X,Z), parent(Z,Y). ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).

Comp 307 Lecture 4:6 |?-parent(X, Y). X=ann, Y=jim; X=pat, Y=tom; X=bob, Y=liz; X=bob, Y=jim; X=jim, Y=pam; X=jim, Y=pat; no. | ?- father(bob, X), mother(ann, X). X=jim?; no Queries and Answers

Comp 307 Lecture 4:7 Prolog answers a query by matching the query with FACTs and the HEADs of the rules – If the query matches a fact, then succeed – If the query matches the head of a rule, then the body of the rule is now treated as a query, and prolog tries to solve that – Top-down, left-to-right, Depth-first If Prolog’s search fails, it backtracks to try alternative solutions, if possible. How Prolog answers a query

Comp 307 Lecture 4:8 How Prolog answers a query |?- grandparent(bob, Y).

Comp 307 Lecture 4:9 When – Arguments at the same place – The same variable in one clause – X = Y How – Propagate variable bindings forward – Once a variable has a binding, it cannot be changed -- there is no destructive assignment Matching

Comp 307 Lecture 4:10 Backtracking When – When a sub goal failed – Multiple answers, using “;” – ……., fail. How: – Undo variable bindings, try alternative clauses

Comp 307 Lecture 4:11 How Prolog answers a query |?-ancestor(bob, pat).

Comp 307 Simple Rule-based System in Prolog mammal:-f(hair). mammal:-f(milk). bird:-f(feathers). bird:-f(eggs), f(flies). carnivore:-mammal, f(meat). classify(tiger):-carnivore, f(tawny), f(striped). classify(puma):-carnivore, f(black). f(hair). f(black). f(meat). |?-classify(X). X=puma.

Comp 307 Lecture 4:13 Term Term Possible Instantiation a 6 no a Z Z=a A C A=C date(2, 3, 2000) Z Z=date(2, 3, 2000) date(D, M, Y) Z Z=date(D, M, Y) date(X, Y, Z) date(Y, X, Z) X=Y date(D, 5, Y) date(D1, M, 2000) D=D1, M=5, Y=2000 date(6, 5, 2000) date(6, may, 2000) no date(D, M, Y) date1(D, M, Y) no Matching (Unification)

Comp 307 Lecture 4:14 Term Term Possible Instantiation [] [] yes [a, b, c] [X, Y, c] X=a, Y=b [1, [2, 3], 4] [X, Y, Z] X=1, Y=[2, 3], Z=4 [X,Y] [a,b,c] no [H|T] [a,b] H=a, T=[b] [H|T] [1,[2,3],4] H=1, T=[[2,3],4] Matching (Unification)

Comp 307 Lecture 4:15 Matching (Unification) If S and T are constants, then S and T match only if they are the same object If S is a variable and T is anything, then they match, and S is instantiated to T (and vice-versa) If S and T are structures, then they match only if – S and T have the same principal functor, and – all their corresponding components match (the recursive part) If there are more than one bindings, we always compute the most general unifier: e.g. f(X,Y)=f(a,Z) X=a,Y=Z or X=a,Z=Y (Not X=a, Y=b, Z=b)

Comp 307 Lecture 4:16 terms: To match = or to compare == To match X=Y To compare X==Y, X\==Y e.g: [a,b] == [a,b] yes [a,X] == [a,b] no [a,X] = [a,b] X=b Note: X\==Y is always true if there are free variables (X, Y must not be free variable)

Comp 307 Lecture 4:17 Arithmetic: =:=, =\=,, >=, =<, is To compare the values: =:=, =\=, >=, =< – 3 =:= 1+2 yes To evaluate and assign: is – |?- 3 is 2+1. Yes – At the time that evaluation is carried out, all arguments must be already instantiated to numbers. |?-X is 2+Y. error Arithmetic: =:=, =/=, is

Comp 307 Lecture 4:18 Common Errors – 3=1+2. – 3= =1+2. – X=1+2. – X = X+1 – X is X+1

Comp 307 Lecture 4:19 Exercises |?- 1+A = B+2. A=2, B=1. |?- 1+2 = 2+1. no |?- 1+2 =:= 2+1. Yes |?- 1+2 = = 2+1. no |?-X=2+3. X=2+3 |?-X is 2+3. X=5 |?-X=2, X is X+3. no

Lecture 5:20 Comp 307 Caching facts in Prolog All variables are local variables. Longer memories: caching facts retractall/1, retract/1, assert/1 :-dynamic rectangle/2. rectangle_example3:- retractall(rectangle(_,_)), write('Enter the width: '), read(W), write('Enter the height: '), read(H), assert(rectangle(W, H)), calculate_area(Area), format("The area is ~w~n", [Area]). calculate_area(X):- rectangle(W, H), X is 2*W*H.

Lecture 5:21 Comp 307 Build in predicates: findall findall(X, P, XList) produces a list of object X that satisfy P. age(ann, 5). age(tom,5). age(peter,7). age(pat,8). ?-findall(Child, age(Child,5), List). List=[ann, tom]. ?-findall(Child, age(Child, Age), List). List=[ann, tom, peter, pat].