1 COMP313A Programming Languages Logic Programming (3)

Slides:



Advertisements
Similar presentations
© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
Advertisements

Prolog.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
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.
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.
Introduction to Prolog Language Presented by San Myint.
Prolog programming Introduction to Prolog
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
Logic Programming.
4. PROLOG Data Objects And PROLOG Arithmetic
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog (II) Lecture 22 – Prolog (II), Spring CSE3302 Programming Languages,
Chapter 8: The Logical Paradigm Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
© 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 Syntax and Meaning of Prolog Programs Notes for Ch.2 of Bratko For CSCE 580.
1 Artificial Intelligence CSC 361 Prof. Mohamed Batouche Department of Computer Science CCIS – King Saud University Riyadh, Saudi Arabia
(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
CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.
LING 388: Language and Computers Sandiway Fong Lecture 4.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
For Wednesday No new reading Prolog handout 2 Chapter 9, exercise 4.
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.
MB: 5 March 2001CS360 Lecture 41 Programming in Logic: Prolog Lists and List Operations Readings: Sections 3.1 & 3.2.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
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.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
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.
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.
Introduction to Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
ISBN Chapter 16 Logic Programming Languages.
Artificial Intelligence CS370D
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.
For Wednesday No reading Prolog handout 2 Chapter 9, exercise 4.
C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
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),
Section 16.5, 16.6 plus other references
A Conjunction is a logical operator that connects two logical terms.
Prolog a declarative language
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog programming Introduction to Prolog (part2)
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Prolog a declarative language
Prolog syntax + Unification
Prolog a declarative language
Prolog a declarative language
Prolog programming Introduction to Prolog (part3)
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Chapter Two: Syntax and Meaning of Prolog Programs
Prolog programming Introduction to Prolog (part4)
How To Think Like a Prolog ?
PROLOG.
Presentation transcript:

1 COMP313A Programming Languages Logic Programming (3)

2 Lecture Outline Some Prolog Unification

3 Some more prolog Recursive rules example predecessor relation predecessor(X,Z) :- parent(X,Z). predecessor(X,Z) :- parent(X,Y), parent(Y,Z) And so on… predecessor(X,Z) :- parent(X,Y), predecessor(Y,Z).

4 Some more prolog parent(pam,bob). parent(tom,bob). parent(tom,liz). parent(bob, ann). parent(bob, pat). parent(pat, jim). ? predecessor(pam,bob). ? predecessor(pam, ann). ? predecessor(pam, liz). ? predecessor(pam, X).

5 A prolog program comprises clauses - facts, rules and queries PROGRAM big(bear).%clause 1 big(elephant).%clause 2 small(cat).%clause 3 brown(bear).%clause 4 black(cat).%clause 5 grey(elephant).%clause 6 dark(Z) :- black(Z).%clause 7 dark(Z) :- brown(Z).%clause 8 ?dark(X), big(X).

6 Data Objects Atoms versus numbers versus Variables Atoms –strings of letters, digits, and the underscore character beginning with a lower case letter –some strings of special characters –can enclose strings of characters in single quotes e.g. ‘Fred’ Numbers –integers and floats

7 Data Objects Atoms versus Variables Variables are strings of characters, digits and underscores that begin with an uppercase letter or an underscore Can use the anonymous underscore hasachild(X) :- parent(X,Y) hasachild(X) :- parent(X,_) ? parent(X, _)

8 Data Objects Structured objects objects that have several components location(X, Y, Orientation). location(156, 786, 25). location(156, 786, Orientation). location is the functor

9 Structures date(X,Y,Z). date(20, june, 2005). date(Day, june, 2005). date(Day, Month, Year) :- Day > 0, Day <= 31,…….

10 data objects simple objects structures constantsvariables atomsnumbers These are all terms

11 Operations on lists Membership The member relation member(X,L) ? member(d, [a, b, h, d]). ? ? member(d, [a,b,[d h], e]). ? ?member([d, h], [a, [d,h], e f]). ?

12 Membership X is a member of L if –X is the head of L, or –X is a member of the tail of L. member(X, [X|Tail]). member(X, [Head | Tail]) :- member(X,Tail). Note two separate clauses

13 Membership X is a member of L if –X is the head of L, or –X is a member of the tail of L, or –X is a member of a sublist of L member(X, [X|Tail]). member(X, [Head | Tail]) :- member(X,Tail). plus one more clause……

14 Concatenation The concatenation relation – conc(L1, L2, L3) ? conc([a,b], [c,d], [a,b,c,d]) yes ? conc([a,b], [c,d], [a, b, [c,d]]) no ?conc([a,b], [c,d], X). X = [a,b,c,d]

15 Concatentation conc(L1, L2, Result) If L1 is the empty list –then L2 and Result must be equal If L1 is nonempty –then have [X|L1tail] –recursively X becomes the head of Result and we use conc to find the tail of result [X|TailResult] –Eventually will have exhausted L1 and TailResult will be L2.

16 Concatentation conc([], L, L). conc([X | L1], L2, [X | L3]) :- conc(L1, L2, L3).

17 Unification Matching clauses with variables Have to find the appropriate substitutions for variables so that the clauses match Process is called unification –Process by which variables are instantiated

18 GCD example gcd(u,0,u)  not zero(v), gcd(v, u mod v, w) gcd(u,v,w)  not zero(v), gcd(v, u mod v, w) Using resolution the goal  gcd(15, 10, x)  gcd(15, 10, x)

19 Unification in Prolog A constant unifies only with itself ? me = me. Yes ?me = you. No  Gcd(5, 0, 5)  Gcd(5, 10, w)  Gcd(5, 0, w)

20 Unification in Prolog… A variable that is uninstantiated unifies with anything and becomes instantiated with that thing ? me = X. X = me ? f(a,X) = f(Y, b). X = b Y = a ? f(X) = f(Y)  not zero(v), gcd(v, u mod v, w), gcd(15, 10, x). gcd(u,v,w)  not zero(v), gcd(v, u mod v, w), gcd(15, 10, x). gcd(15, 10, x)  not zero(10), gcd(10, 15 mod 10, x), gcd(15, 10, x).

21 Unification in Prolog A structured term (predicate or function applied to arguments requires –Same predicate/function name –Same number of arguments –Arguments can be unified recursively ? f(X) = g(X) ? f(X) = f(a,b) ? f(a, g(X)) = f(Y, b) ? f(a, g(X)) = f(Y, g(b))

22 Unification examples Unify the following : p(X,Y) and p(a, Z) p(X,X) and p(a,b) ancestor(X,Y) and ancestor(bill, W) p(X,a,Y) and p(Z,Z,b) p(Marcus, g(X,Y)) and f(x, g(Caesar, Marcus)) g(X, X) and g(f(X), f(X))