70 likes | 166 Views
Programming Language Paradigms. LOGIC PROGRAMMING. A Logic program is expressed as a set of atomic sentences (FACTS) and HORN clauses(RULES) We can ask questions in the form of conjectures (like FACTS but with variables) to find out what values will make the conjecture true. Eg.
E N D
LOGIC PROGRAMMING A Logic program is expressed as a set of atomic sentences (FACTS) and HORN clauses(RULES) We can ask questions in the form of conjectures (like FACTS but with variables) to find out what values will make the conjecture true. Eg. Ramu is a boy → FACT Ramu's father is Kicha → Relationship Ramu eats choclates if chocolates have nuts and chocolates are available → HORN CLAUSE
LOGIC PROGRAMMING Computation is done by a separate inference engine. (Hidden from programmer). LIPS : Logical Inferences Per Second.
PROLOG OVERVIEW PROLOG – Programming in Logic A Prolog program consists of a database of predicates composed of FACTS and RULES involving CONSTANTS and VARIABLES Constants (ATOMS) begin with lowercase letters Eg: tom, bill, a1, x, y, 217, -32, 2.76 Variables begin with uppercase letter or underscore. Eg: X, U, _x1, Tom, A1 A Fact is true about some constant. A Predicate is a function result is either TRUE or FALSE.
PROLOG OVERVIEW Consider the following relationship in Prolog. Mother (peggy, george). Peggy is the mother of george Rules in Prolog grandmother(x,z) :- mother(x,y), parent(y,z) :- refer to “provided that” = “if” part in rule , refers to “and” in the rule. ; referes to “or” in the rule. If x is the mother of y and y is the parent of z then x is the grandmother of z Goals in Prolog are entered by the user following the ?- prompt
SWI PROLOG TUTORIAL $vi ancestor.pro ancestor(bob, susan). ancestor(A, X) :- parent(A, X). ancestor(A, X) :- parent(A, C), ancestor(C, X). parent(fred, sally). parent(tina, sally). parent(sally, john). parent(sally, diana). parent(sam, bill).
SWI PROLOG TUTORIAL $pl ?- help. ?- [’ancestor.pro’]. ?- listing. ?- halt. $pl –o ancestor –c ancestor.pro $ancestor ?- ancestor(bv, bv). No ?- ancestor(fred, diana). Yes ?- parent(tina, sally). Yes ?- parent(bill, sally). No