290 likes | 540 Views
Logic Programming. PROLOG. Dr. Yasser Ahmed Nada Fall 2010/2011 Lecture 2. Logic Programming. Review of Last Lecture. Prolog program consists of: Clauses: Facts: asserts some property of an object, or relation between two or more objects . e.g. parent(ali,ahmed).
E N D
Logic Programming PROLOG Dr. Yasser Ahmed Nada Fall 2010/2011 Lecture 2 Logic Programming
Review of Last Lecture • 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. PROLOG Logic Programming
Review of Last Lecture • 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. PROLOG Predicate name arguments Logic Programming
Arguments • Arguments consists of terms which are: • Atoms (constants). • Variables. • Compound terms (Explained later). PROLOG Logic Programming
Examples: PROLOG Logic Programming
Rules • 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. PROLOG head body Logic Programming
Or Rules • 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). PROLOG Logic Programming
Clauses • A clause consists of a head. • And sometimes a body. • Facts does not have body, they are always true. PROLOG Logic Programming
Ex. 2.6 • Convert each proposition into prolog clauses: • a b. • a b c. • a b c. • a (b c) d • a b. b :- a. PROLOG c :- a. c :- b. c :- a, b. d :- a,b. d :- a,c. b :- a. Logic Programming
Terms • 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, … PROLOG Logic Programming
Prolog Data Objects Data objects Simple objects structure PROLOG constants variables Atoms numbers Logic Programming
Structure • 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. PROLOG Logic Programming
Structure Example • book(‘prolog programming’, brna, 2000). • book(‘C++ programming’, john, 2005). • book(‘Data Structure’, mike, 2007). • findBook(Name, Author, Year) :- book(Name,Author,Year). PROLOG ? findBook(X, Y, 2000). X=‘prolog programming’ Y=‘brna’ ? ; No ? Logic Programming
Multiple Clauses • 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). PROLOG ? happy(P). P=ali ? ; P=ahmed ? ; P=salem ? ; no ? Logic Programming
Ordering of Clause • Prolog tries clauses from top to bottom and from left to right. • 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). PROLOG ? happy(P). P=ali ? ; P=ahmed ? ; P=salem ? ; no ? ? happy(P). P=ahmed ? ; P=ali ? ; P=salem ? ; no ? Logic Programming
Example Database • 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) . PROLOG Find all programs and Memory usage that works in mac computers? answer (Program, Memory ) :− uses(Person, Program, mac), needs(Program, Memory ). Program=compiler Memory=128 Program=editor Memory=512 Program=spreadsheet Memory=640. Logic Programming
Example Database • 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) . PROLOG What the programs that requires more than 256KB? Selection prg(P) :- needs(P,M), M>256. P=editor P=spreadsheet P=database Logic Programming
Example Database • 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) . PROLOG What programs does each person use? personProgram(Person, Program) :- uses(Person, Program, M). Person=ali Program=compiler Person=ali Program=compiler … Projection Logic Programming
Example Database • 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) . PROLOG How much memory does the editor need? editorMem(editor, M) :- needs(editor,M). M=512 Logic Programming
Example Database • 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) . PROLOG 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 Logic Programming
Example Database • 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) . PROLOG What programs are used by Ali but not by Ahmed? prgUsed(P) :- uses(ali, P, M1), not uses(ahmed, P, M2). P=compiler P= compiler P=compiler P=diary Logic Programming
Unification • 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. PROLOG Logic Programming
Unification • 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}. PROLOG Logic Programming
Unification Algorithm • 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. PROLOG Logic Programming
Unification • 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. PROLOG Logic Programming
Unification • 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 PROLOG Does not unify Does not unify Logic Programming
Homework • 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. PROLOG Logic Programming
Questions PROLOG Logic Programming 28