<<

What is Programming?

There are many (overlapping) perspectives on logic programming – Computations as Deduction – Theorem Proving – Non- minus Control – A Very High Level – A Procedural Interpretation of Declarative Specifications

The Paradigm History • An important 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 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 logic programming languages community – see

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 . • 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 n0 then there exists an x such that n

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

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), Y = cain ; (14) 3 Call: parent(eve,able) ? (22) 3 Exit: female(eve) P1 = cain, male(X). (14) 3 Exit: parent(eve,able) (19) 2 Exit: mother(eve,able) P2 = cain ? ; (15) 3 Call: female(eve) ? (23) 2 Call: mother(eve,cain) ? X = cain P1 = cain, (15) 3 Exit: female(eve) (24) 3 Call: parent(eve,cain) ? mother(X,Y) :- Y = able ; (13) 2 Exit: mother(eve,able) (24) 3 Exit: parent(eve,cain) P2 = able ? ; parent(X,Y), (16) 2 Call: not able=able ? (25) 3 Call: female(eve) ? P1 = able, (17) 3 Call: able=able ? (25) 3 Exit: female(eve) P2 = cain ? ; female(X). (17) 3 exit: able=able (23) 2 Exit: mother(eve,cain) P1 = able, (16) 2 Back to: not able=able ? (26) 2 Call: not able=cain ? sibling(X,Y) :- (16) 2 Fail: not able=able (27) 3 Call: able=cain ? P2 = able ? ; (15) 3 Back to: female(eve) ? (27) 3 Fail: able=cain P1 = able, parent(P, X), (15) 3 Fail: female(eve) (26) 2 Exit: not able=cain P2 = cain ? ; (2) 1 Exit: sibling(able,cain) parent(P, Y). P1 = able, X = able Y = cain P2 = able ? ; child(X, Y) :- parent(Y, X). yes no no | ?- | ?-

How to Satisfy a Goal Note Here is an informal description of how Prolog • Two basic conditions are true, which always satisfies a goal (like father(adam,X)). Suppose succeeds, and fail, which always fails. the goal is G: • Comma (,) represents conjunction (i.e. and). – if G = P,Q then first satisfy P, carry any variable • Semi-colon represents disjunction (i.e. or): bindings forward to Q, and then satiety Q. grandParent(X,Y) :- – if G = P;Q then satisfy P. If that fails, then try to grandFather(X,Y); satisfy Q. grandMother(X,Y). – if G = not(P) then try to satisfy P. If this succeeds, then • No real distinction between rules and facts. A fail and if it fails, then succeed. fact is just a rule whose body is the trivial – if G is a simple goal, then look for a fact in the DB that condition true. These are equivalent: unifies with G look for a rule whose conclusion unifies – parent(adam,cain). with G and try to satisfy its body – parent(adam,cain) :- true.

5 Note Terms • Goals can usually be posed with any of several • The term is the basic data structure in combination of variables and constants: Prolog. – parent(cain,able) - is Cain Able's parent? • The term is to Prolog what the s-expression – parent(cain,X) - Who is a child of Cain? is to Lisp. – parent(X,cain) - Who is Cain a child of? • A term is either: – parent(X,Y) - What two people have a – a constant - e.g. parent/child relationship? • john , 13, 3.1415, +, 'a constant' – a variable - e.g. • X, Var, _, _foo – a compound term - e.g. • part(arm,body) • part(arm(john),body(john))

Compound Terms Lists • A compound term can be thought of as a relation • Lists are so useful there is special syntax to between one or more terms: support them, tho they are just terms – part_of(finger,hand) and is written as: • It’s like Python: [1, [2, 3], 4, foo] – the relation name (called the principle functor) which • But matching is special must be a constant. – If L = [1,2,3,4] then L = [Head | Tail] results in – An open parenthesis Head being bound to 1 and Tail to [2,3,4] – The arguments - one or more terms separated by – If L = [4] then L = [Head | Tail] results in Head commas. being bound to 4 and Tail to [] – A closing parenthesis. Term arity f 0 • The number of arguments of a compound f(a) 1 f(a,b) 2 terms is called its arity. f(g(a),b) 2

6 member min

% member(X,L) is true if X is a member of list L. % min(X, L) is true if X is the smallest member % of a list of numbers L • \+ is Prolog’s negation member(X, [X|Tail]). min(X, L) :- operator member(X, [Head|Tail]) :- member(X, Tail). • It’s really “negation as member(X, L), failure” • \+ G is false if goal G can \+ (member(Y,L), Y>X). be proven • \+ G is true if G can not be proven • i.e., assume its false if you can not prove it to be true

Computations From Functions to Relations • Numerical computations can be done in logic, but • Prolog facts and rules define relations, not its messy and inefficient functions • Prolog provides a simple limited way to do • Consider age as: computations – A function: calling age(john) returns 22 • is succeeds if can be unified with the value produced by – As a relation: querying age(john, 22) returns true, age(john, X) binds X to 22, and age(john, X) is ?- X=2, Y=4, Z is X+Y. false for every X ≠ 22 X = 2, • Relations are more general than functions Y = 4, Z = 6. • The typical way to define a function f with

?- X=2, Y=4, X is X+Y. inputs i1…in and output o is as: f(i1,i2,…in,o) false.

7 A numerical example Prolog = PROgramming in LOGic • Here’s how we might define the factorial relation • Prolog is as much a programming language as in Prolog. def fact(n): it is a theorem prover if n==1: • It has a simple, well defined and controllable fact(1,1). return 1 else: reasoning strategy that programmers can fact(N,M) :- n1 = n-1 exploit for efficiency and predictability N > 1, m1 = fact(n1) • It has basic data structures (e.g., Lists) and N1 is N-1, m = m1 * n return m can link to routines in other languages fact(N1,M1), • It’s a great tool for many problems M is M1*N. Another example: square(X,Y) :- Y is X*X.

8