What Is Logic Programming? the Paradigm History
Total Page:16
File Type:pdf, Size:1020Kb
What is Logic Programming? There are many (overlapping) perspectives on logic programming – Computations as Deduction – Theorem Proving – Non-procedural Programming – Algorithms minus Control – A Very High Level Programming Language – A Procedural Interpretation of Declarative Specifications The Paradigm History • An important programming paradigm is to • Logic Programming has roots going back to early express a program as a set of rules AI researchers like John McCarthy in the 50s & 60s • The rules are independent and often • Alain Colmerauer (France) designed Prolog as the first LP language in the early 1970s unordered • Bob Kowalski and colleagues in the UK evolved the • CFGs can be thought of as a rule based language to its current form in the late 70s system • It’s been widely used for many AI systems, but also • We’ll take a brief look at a particular sub- for systems that need a fast, efficient and clean rule paradigm, Logic Programming based engine • And at Prolog, the most successful of the • The prolog model has also influenced the database logic programming languages community – see datalog 1 Computation as Deduction Theorem Proving • Logic programming offers a slightly different paradigm for • Logic Programming uses the notion of an automatic computation: computation is logical deduction theorem prover as an interpreter. • It uses the language of logic to express data and programs. Forall X, Y: X is the father of Y if X is a parent of Y and X is male • The theorem prover derives a desired solution from • Current logic programming languages use first order logic an initial set of axioms. (FOL) which is often referred to as first order predicate • The proof must be a "constructive" one so that more calculus (FOPC). than a true/false answer can be obtained • The first order refers to the constraint that we can quantify • E.G. The answer to (i.e. generalize) over objects, but not over functions or exists x such that x = sqrt(16) relations. We can express "All elephants are mammals” but not • should be "for every continuous function f, if n<m and f(n)<0 and f(m) x = 4 or x = -4 >0 then there exists an x such that n<x<m and f(x)=0" • rather than true Non-procedural Programming A Declarative Example • Logic Programming languages are non-procedural • Here’s a simple way to specify what programming languages has to be true if X is the smallest number in a • A non-procedural language one in which one list of numbers L specifies what needs to be computed but not how it 1. X has to be a member of the list L is to be done 2. There can be list member X2 such that X2<X • That is, one specifies: • We need to say how we determine that some – the set of objects involved in the computation X is a member of a list – the relationships which hold between them – the constraints which must hold for the problem to be 1. No X is a member of the empty list solved 2. X is a member of list L if it is equal to L’s head • and leaves it up the the language interpreter or 3. X is a member of list L if it is a member of L’s compiler to decide how to satisfy the constraints tail. 2 A Simple Prolog Model Nomenclature and Syntax Think of Prolog as a system which has a database • A prolog rule is called a clause composed of two components: • A clause has a head, a neck and a body: • facts: statements about true relations which hold between father(X,Y) :- parent(X,Y) , male(X) . particular objects in the world. For example: parent(adam, able). % adam is a parent of able head neck body parent(eve, able). % eve is a parent of able • the head is a single predicate -- the rule's conclusion male(adam). % adam is male. • The body is a a sequence of zero or more predicates • rules: statements about relations between objects in the that are the rule's premise or condition world which use variables to express generalizations % X is the father of Y if X is a parent of Y and X is male • An empty body means the rule’s head is a fact. father(X,Y) :- parent(X, Y), male(X). • note: % X is a sibling of Y if X and Y share a parent – read :- as IF sibling(X,Y) :- parent(P,X), parent(P,Y) – read , as AND between predicates – a . marks the end of input Prolog Database Queries • We also have queries in addition to having facts and rules parent(adam,able) • The Prolog REPL interprets input as queries parent(adam,cain) Facts comprising the male(adam) “extensional database” • A simple query is just a predicate that might ... have variables in it: – parent(adam, cain) father(X,Y) :- parent(X,Y), – parent(adam, X) male(X). Rules comprising the sibling(X,Y) :- ... “intensional database” 3 Extensional vs. Intensional Running prolog The terms extensional and Prolog Database • A good free version of prolog is swi-prolog intensional are borrowed from the language philosophers use parent(adam,able) Facts comprising the • GL has a commercial version (sicstus prolog) for epistemology. parent(adam,cain) male(adam) “extensional database” you can invoke with the command “sicstus” • Extension refers to whatever extends, i.e., “is ... [finin@linux2 ~]$ sicstus quantifiable in space as well as in time”. father(X,Y) :- parent(X,Y), SICStus 3.7.1 (Linux-2.2.5-15-i686): Wed Aug 11 16:30:39 CEST 1999 • Intension is an antonym of extension, Rules comprising the male(X). “intensional database” Licensed to umbc.edu referring to “that class of existence which may sibling(X,Y) :- ... be quantifiable in time but not in space.” | ?- assert(parent(adam,able)). • NOT intentional with a “t”, which has to do yes with “will, volition, desire, plan, …” | ?- parent(adam,P). For KBs and DBs we use Epistemology is “a branch of philosophy P = able ? • extensional to refer to that which is explicitly that investigates the origin, nature, represented (e.g., a fact), and methods, and limits of knowledge” yes • intensional to refer to that which is represented abstractly, e.g., by a rule of | ?- inference. A Simple Prolog Session A Prolog Session | ?- [user]. | ?- mother(eve,Who). | female(eve). Who = cain | ?- assert(parent(adam,able)). | parent(adam,cain). yes | ?- parent(X,able). | parent(eve,cain). | ?- trace, mother(Who,cain). yes X = adam ; | father(X,Y) :- parent(X,Y), male(X). (2) 1 Call: mother(_0,cain) ? | ?- assert(parent(eve,able)). X = eve ; | mother(X,Y) :- parent(X,Y), female(X). (3) 2 Call: parent(_0,cain) ? yes no | ^Zuser consulted 356 bytes 0.0666673 (3) 2 Exit: parent(adam,cain) | ?- assert(male(adam)). | ?- parent(X,able) , male(X). sec. (4) 2 Call: female(adam) ? yes X = adam ; yes (4) 2 Fail: female(adam) | ?- mother(Who,cain). | ?- parent(adam,able). no (3) 2 Back to: parent(_0,cain) ? yes Who = eve (3) 2 Exit: parent(eve,cain) yes | ?- parent(adam,X). (5) 2 Call: female(eve) ? X = able (5) 2 Exit: female(eve) (2) 1 Exit: mother(eve,cain) yes Who = eve yes 4 trace,sibling(X,Y). (14) 3 Back to: parent(eve,able) ? (2) 1 Call: sibling(_0,_1) ? (14) 3 Fail: parent(eve,able) [finin@linux2 ~]$ more genesis.pl (3) 2 Call: father(_65643,_0) ? (13) 2 Back to: mother(eve,able) ? Program files | ?- [user]. (4) 3 Call: parent(_65643,_0) ? (13) 2 Fail: mother(eve,able) % prolog example (4) 3 Exit: parent(adam,able) (12) 3 Back to: female(eve) ? | sibling(X,Y) :- (5) 3 Call: male(adam) ? (12) 3 Fail: female(eve) (5) 3 Exit: male(adam) (10) 3 Back to: parent(_65644,able) ? % facts | father(Pa,X), (3) 2 Exit: father(adam,able) (10) 3 Fail: parent(_65644,able) Typically you put your assertions (fact (6) 2 Call: father(adam,_1) ? (9) 2 Back to: mother(_65644,able) ? male(adam). | father(Pa,Y), (7) 3 Call: parent(adam,_1) ? (9) 2 Fail: mother(_65644,able) and rules) into a file and load it (7) 3 Exit: parent(adam,able) (8) 3 Back to: male(adam) ? | ?- [genesis]. female(eve). | mother(Ma,X), (8) 3 Call: male(adam) ? (8) 3 Fail: male(adam) {consulting /afs/umbc.edu/users/f/i/finin/home/genesis.pl...} (8) 3 Exit: male(adam) (7) 3 Back to: parent(adam,_1) ? parent(adam,cain). | mother(Ma,Y), (6) 2 Exit: father(adam,able) (7) 3 Exit: parent(adam,cain) {/afs/umbc.edu/users/f/i/finin/home/genesis.pl consulted, 0 msec 2720 (9) 2 Call: mother(_65644,able) ? (18) 3 Call: male(adam) ? bytes} parent(eve,cain). | not(X=Y). (10) 3 Call: parent(_65644,able) ? (18) 3 Exit: male(adam) yes parent(adam,able). (10) 3 Exit: parent(adam,able) (6) 2 Exit: father(adam,cain) | ?- male(adam). (11) 3 Call: female(adam) ? (19) 2 Call: mother(_65644,able) ? ^Zuser consulted 152 bytes 0.0500008 sec. yes parent(eve,able). (11) 3 Fail: female(adam) (20) 3 Call: parent(_65644,able) ? yes (10) 3 Back to: parent(_65644,able) ? (20) 3 Exit: parent(adam,able) | ?- sibling(P1, P2). (10) 3 Exit: parent(eve,able) (21) 3 Call: female(adam) ? P1 = cain, % rules | ?- sibling(X,Y). (12) 3 Call: female(eve) ? (21) 3 Fail: female(adam) P2 = cain ? ; (12) 3 Exit: female(eve) (20) 3 Back to: parent(_65644,able) ? father(X,Y) :- P1 = cain, X = able (9) 2 Exit: mother(eve,able) (20) 3 Exit: parent(eve,able) (13) 2 Call: mother(eve,able) ? (22) 3 Call: female(eve) ? P2 = able ? ; parent(X,Y), (14) 3 Call: parent(eve,able) ? (22) 3 Exit: female(eve) P1 = cain, Y = cain ; male(X).