Logic Programming Dr. Yasser Ahmed Nada Fall 2010/2011 Lecture 2 1 Logic Programming.

Slides:



Advertisements
Similar presentations
Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines
Advertisements

Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
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).
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Comp 205: Comparative Programming Languages Declarative Programming Languages Unification Backtracking Lecture notes, exercises, etc., can be found at:
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 Prolog I. 2 Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
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.
F28PL1 Programming Languages Lecture 16: Prolog 1.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
1.  Provides the ability to access individual assertions. e.g. in Predicate calculus we may say: P denotes “It rained on Tuesday” but in predicate calculus.
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.
CSE S. Tanimoto Horn Clauses and Unification 1 Horn Clauses and Unification Propositional Logic Clauses Resolution Predicate Logic Horn Clauses.
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.
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
Dr. Muhammed Al-Mulhem ICS An Introduction to Logical Programming.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Word Problem Consider the following problem: To register, you will need either a yellow card or a note from the Dean. To obtain a note from the Dean you.
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.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Propositional Logic Predicate Logic
CSE (c) S. Tanimoto, 2008 Predicate Calculus II 1 Predicate Calculus 2 Outline: Unification: definitions, algorithm Formal interpretations and satisfiability.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Dr. Yasser Nada Fall 2010/2011 Lecture 1 1 Logic Programming.
ISBN Chapter 16 Logic Programming Languages.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Artificial Intelligence CS370D
1-1 An Introduction to Logical Programming Sept
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”
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.
Logic Programming Dr. Yasser Nada Fall 2010/2011 Lecture 3 1 Logic Programming.
Logic Programming Lecture 2: Unification and proof search.
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Prolog a declarative language
CSE 341, S. Tanimoto Logic Programming -
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Horn Clauses and Unification
Tests, Backtracking, and Recursion
Prolog programming Introduction to Prolog (part2)
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Carlos Varela Rensselaer Polytechnic Institute November 10, 2017
Horn Clauses and Unification
Horn Clauses and Unification
Chapter Two: Syntax and Meaning of Prolog Programs
Horn Clauses and Unification
Dr. Yasser Nada Fall 2010/2011 Lecture 1
Horn Clauses and Unification
Chapter 2: Prolog (Introduction and Basic Concepts)
PROLOG.
Presentation transcript:

Logic Programming Dr. Yasser Ahmed Nada Fall 2010/2011 Lecture 2 1 Logic Programming

Prolog program consists of: – Clauses: Facts: asserts some property of an object, or relation between two or more objects. – e.g. parent(ali,ahmed). Rules: allow us to infer that a property or relationship holds based on preconditions. – e.g. parent(X,Y) :- father(X,Y). – Goals. 2 Review of Last Lecture Logic Programming

Both facts and rules are predicates definition. – Predicate is the name of the relation between objects that occurs before the parenthesis. parent (ali, ahmed). – Predicate can have 0, 1, 2, 3, … arguments. 3 Review of Last Lecture Logic Programming Predicate name arguments

Arguments consists of terms which are: – Atoms (constants). – Variables. – Compound terms (Explained later). 4 Arguments Logic Programming

Ali like icecreamlikes(ali, icecream). salem is talltall(salem). Ahmed hits ali with a bathit(ahmed, ali, bat). Zero is a numbernumber(0). Ali friend of ahmed if ali like ahmedfriend(ali, ahmed) :- like(ali, ahmed). X is greater than Y if X>Ygreater(X,Y) :- X>Y. Salem is father of ali if salem is parent of ali and ali is male. Father(salem,ali) :- parent(salem, ali), male(ali). 5 Examples: Logic Programming

6 Rules Logic Programming Rules are of the form: – Head :- body. – Head is a predicate. – Body is a conjunction of predicates. – e.g. friend(X,Y) :- like(X,C), like(Y,C). – Comma means and. bodyhead

7 Or Rules Logic Programming different rules for the same head means or: – parent(X,Y) :- father(X,Y). – parent(X,Y) :- mother(X,Y). – X is parent of Y if (X is father of Y) or (X is mother of Y).

8 Clauses Logic Programming A clause consists of a head. And sometimes a body. – Facts does not have body, they are always true.

9 Ex. 2.6 Logic Programming Convert each proposition into prolog clauses: – a  b. – a  b  c. – a  b  c. – a  (b  c)  d –  a  b. b :- a. c :- a. c :- b. c :- a, b. d :- a,b. d :- a,c. b :- a.

10 Terms Logic Programming Atoms: constants: – Number Integer: 0, 1, 2, … Float: 1.3, 1E5, … – Symbolic: Starts with lower case letter: ali, ahmed, sun, … – String: between single quote: ‘ali salem’ Variables: starts with capital letter: X, Y, …

11 Prolog Data Objects Logic Programming Data objects Simple objectsstructure Atoms variables numbers constants

12 Structure Logic Programming They are compound objects: – date(10,oct,2010). Structure consists of functor and argument. – e.g. structure date(10, oct, 2010) Functor: date. Arguments: 10 oct 2010.

13 Structure Example Logic Programming book(‘prolog programming’, brna, 2000). book(‘C++ programming’, john, 2005). book(‘Data Structure’, mike, 2007). findBook(Name, Author, Year) :- book(Name,Author,Year). ? findBook(X, Y, 2000). X=‘prolog programming’ Y=‘brna’ ? ; No ?

14 Multiple Clauses Logic Programming Clauses that have same predicate name in the head. wealthy(ali). healthy(ahmed). wise(salem). happy(P) :- wealthy(P). happy(P) :- healthy(P). happy(P) :- wise(P). ? happy(P). P=ali ? ; P=ahmed ? ; P=salem ? ; no ?

15 Ordering of Clause Logic Programming Prolog tries clauses from top to bottom and from left to right. ? happy(P). P=ali ? ; P=ahmed ? ; P=salem ? ; no ? wealthy(ali). healthy(ahmed). wise(salem). happy(P) :- wealthy(P). happy(P) :- healthy(P). happy(P) :- wise(P). wealthy(ali). healthy(ahmed). wise(salem). happy(P) :- healthy(P). happy(P) :- wealthy(P). happy(P) :- wise(P). ? happy(P ). P=ahmed ? ; P=ali ? ; P=salem ? ; no ?

16 Example Logic Programming uses(ali, compiler, sun). uses(ali, compiler, pc). uses(ali, compiler,mac). uses(ali, editor, sun). uses(ali, editor, pc). uses(ali, diary, pc). uses(ahmed, editor,mac). uses(ahmed, spreadsheet,mac). uses(salem, database, pc). uses(salem, compiler, pc). uses(salem, editor, pc). needs(compiler, 128). needs(editor, 512). needs(diary, 64). needs(spreadsheet, 640). needs(database, 8192). answer (Program, Memory ) :− uses(Person, Program, mac), needs(Program, Memory ). Program=compiler Memory=128 Program=editor Memory=512 Program=spreadsheet Memory=640. Find all programs and Memory usage that works in mac computers? Database

17 Example Logic Programming uses(ali, compiler, sun). uses(ali, compiler, pc). uses(ali, compiler,mac). uses(ali, editor, sun). uses(ali, editor, pc). uses(ali, diary, pc). uses(ahmed, editor,mac). uses(ahmed, spreadsheet,mac). uses(salem, database, pc). uses(salem, compiler, pc). uses(salem, editor, pc). needs(compiler, 128). needs(editor, 512). needs(diary, 64). needs(spreadsheet, 640). needs(database, 8192). What the programs that requires more than 256KB? prg(P) :- needs(P,M), M>256. P=editor P=spreadsheet P=database Selection Database

18 Example Logic Programming uses(ali, compiler, sun). uses(ali, compiler, pc). uses(ali, compiler,mac). uses(ali, editor, sun). uses(ali, editor, pc). uses(ali, diary, pc). uses(ahmed, editor,mac). uses(ahmed, spreadsheet,mac). uses(salem, database, pc). uses(salem, compiler, pc). uses(salem, editor, pc). needs(compiler, 128). needs(editor, 512). needs(diary, 64). needs(spreadsheet, 640). needs(database, 8192). personProgram(Person, Program) :- uses(Person, Program, M). Person=ali Program=compiler … Database Projection What programs does each person use?

19 Example Logic Programming uses(ali, compiler, sun). uses(ali, compiler, pc). uses(ali, compiler,mac). uses(ali, editor, sun). uses(ali, editor, pc). uses(ali, diary, pc). uses(ahmed, editor,mac). uses(ahmed, spreadsheet,mac). uses(salem, database, pc). uses(salem, compiler, pc). uses(salem, editor, pc). needs(compiler, 128). needs(editor, 512). needs(diary, 64). needs(spreadsheet, 640). needs(database, 8192). How much memory does the editor need? editorMem(editor, M) :- needs(editor,M). M=512 Database

20 Example Logic Programming uses(ali, compiler, sun). uses(ali, compiler, pc). uses(ali, compiler, mac). uses(ali, editor, sun). uses(ali, editor, pc). uses(ali, diary, pc). uses(ahmed, editor,mac). uses(ahmed, spreadsheet,mac). uses(salem, database, pc). uses(salem, compiler, pc). uses(salem, editor, pc). needs(compiler, 128). needs(editor, 512). needs(diary, 64). needs(spreadsheet, 640). needs(database, 8192). Which programs are used by two different people on the same machine? twoDiff(R) :- uses(P1, R, M1), uses(P2, R, M1), P1 \= P2. R=compiler R= editor Database

21 Example Logic Programming uses(ali, compiler, sun). uses(ali, compiler, pc). uses(ali, compiler, mac). uses(ali, editor, sun). uses(ali, editor, pc). uses(ali, diary, pc). uses(ahmed, editor,mac). uses(ahmed, spreadsheet,mac). uses(salem, database, pc). uses(salem, compiler, pc). uses(salem, editor, pc). needs(compiler, 128). needs(editor, 512). needs(diary, 64). needs(spreadsheet, 640). needs(database, 8192). What programs are used by Ali but not by Ahmed? prgUsed(P) :- uses(ali, P, M1), not uses(ahmed, P, M2). P=compiler P=diary Database

22 Unification Logic Programming A substitution is a mapping from variables to terms. Given two terms t and u. – A unifier is a substitution that make t and u equal.

23 Unification Logic Programming Find a unifier for – p(X, g(X,Z)) and p(c, g(X,Y)). – Match predicate names: p. – Match first arguments: X=c. – Match second argument: g(c,Z) and g(c,Y). Match predicate name: g Match first argument: c. Match second argument: Z = Y. – Unifier is {X/c, Z/Y}.

24 Unification Algorithm Logic Programming Function Unify(s,t): – If s and t are constants then If s=t then return success Else return fail. – If s is a variable: If s is in t then return fail. Else substitute t with s, return success. – If t is a variable: If t is in s then return fail. Else substitute t with s, return success. – If s is in the form p(a1, a2, …, an) and t is in the form p(b1,b2, …, bn) then For each argument ai and bi, return unify(ai, bi). – Else fail.

25 Unification Logic Programming Find a unifier for – p(a, g(a,Z)) and p(X, g(c,Y)). – Match predicate names: p. – Match first arguments: X=a. – Match second argument: g(a,Z) and g(c,Y). Match predicate name: g Match first argument: a  c, fail. – Can not be unified.

26 Unification Logic Programming Find a unifier for – point(A,B) = point(1,2). – point(A,B)=point(X,Y,Z). – plus(2,2) = 4. – Book(name(‘a’), auth(‘b’), year(2000)) = book(A, auth(A), year(Y)). A=1, B=2 Does not unify

27 Homework Logic Programming What is the unifier in the following: – c=letter(c). – paris=X. – foo(S)=foo(see). – like(ali, book(‘c++’,’john’,2010))=like(W,B). – f(1)=F. – name(Family)=ali. – times(2,2)=Four. – 5*3=15.

28 Questions Logic Programming