Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Logic Programming Dr. Yasser Ahmed Nada Fall 2010/2011 Lecture 2 1 Logic Programming."— Presentation transcript:

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

2 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

3 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

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

5 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 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 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 8 Clauses Logic Programming A clause consists of a head. And sometimes a body. – Facts does not have body, they are always true.

9 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 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 11 Prolog Data Objects Logic Programming Data objects Simple objectsstructure Atoms variables numbers constants

12 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 28 Questions Logic Programming


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

Similar presentations


Ads by Google