Rule-based Programming, Logic Programming and Prolog

Slides:



Advertisements
Similar presentations
First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
1. An Overview of Prolog.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
Prolog.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
Chapter 12 - Logic Programming
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
ISBN Chapter 16 Logic Programming Languages.
Formal Models of Computation Part II The Logic Model
1 Prolog I. 2 Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
Declarative vs Procedural Programming  Procedural programming requires that – the programmer tell the computer what to do. That is, how to get the output.
F28PL1 Programming Languages Lecture 16: Prolog 1.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
Rule-based Programming, Logic Programming and Prolog.
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
Dr. Muhammed Al-Mulhem ICS An Introduction to Logical Programming.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Logic Programming. Formal Logics- Recap Formulas – w/out quantifiers Free Variables Bound Variables Assignments and satisfaction Validity and satisfiability.
ISBN Chapter 16 Logic Programming Languages.
Knowledge Based Information System
1-1 An Introduction to Logical Programming Sept
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
For Friday No reading Prolog Handout 2. Homework.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Section 16.5, 16.6 plus other references
CSE (c) S. Tanimoto, 2001 Logic Programming
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Introduction to Logic for Artificial Intelligence Lecture 2
Prolog a declarative language
Prolog Concepts.
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog and Logic Programming
Logical Inference 2 Rule-based reasoning
Logic Programming Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Horn Clauses and Unification
Programming Paradigms and Languages
Horn Clauses and Unification
Logic Programming Language
Programming Techniques
CSE (c) S. Tanimoto, 2004 Logic Programming
CSE (c) S. Tanimoto, 2002 Logic Programming
Horn Clauses and Unification
Prolog Concepts.
Predicate Logic: Syntax.
Chapter 2: Prolog (Introduction and Basic Concepts)
Prolog Concepts.
PROLOG.
Presentation transcript:

Rule-based Programming, Logic Programming and Prolog

Rule Based Programming Paradigm An important programming paradigm is to express a program as a set of rules The rules are independent and often unordered CFGs can be thought of as a rule based system We’ll take a brief look at a particular sub-paradigm, Logic Programming And at Prolog, the most successful of the logic programming languages

History Logic Programming has roots going back to early AI researchers in the 50s & 60s Alain Colmerauer (France) designed Prolog as the first LP language in the early 1970s Prolog = Programming in logic Bob Kowalski and colleagues in the UK evolved the language to its current form in the late 70s It’s widely used for AI & systems needing a fast, efficient & clean rule based engine The Prolog model has also influenced the database community – see datalog

Computation as Deduction Logic programming paradigm for computation is computation is logical deduction It uses the language of logic to express data and programs Forall X, Y: X is father of Y if X is parent of Y and X is male father(X,Y) :- parent(X,Y), male(X). Current logic programming languages use first order logic (FOL) which is often referred to as first order predicate calculus (FOPC) The first order refers to the constraint that we can quantify (i.e. generalize) over objects, but not over functions or relations

Theorem Proving Logic Programming uses the notion of an automatic theorem prover as an interpreter The theorem prover derives a desired solution from an initial set of axioms parent(adam,able). male(adam). father(X,Y) :- parent(X,Y), male(X). The proof must be a "constructive" one so that more than a true/false answer can be obtained ?- father(Who, able) % Who is a variable since it’s capitalized Who = adam. % query is true for Who = adam ?- father(Who, adam). false. % query is false: can’t find adam’s father

Non-procedural Programming Logic Programming languages are non-procedural programming languages One specifies what needs to be computed but not how it is to be done, e.g. Set of objects involved in the computation Relationships which hold between them Constraints which must hold in a solution The interpreter or compiler decides how to satisfy the constraints, typically using one or more general strategies

Example Problem: find the smallest element in a list L What are the objects involved? What statements must be true in a solution?

Example Problem: find the smallest element in a list L What are the objects involved? The list L and the smallest element X What statements must be true in a solution?

Example Problem: find the smallest element in a list L What are the objects involved? The list L and the smallest element X What statements must be true in a solution? X must be a member of L There is no other member, X2, such that X2 < X

Example Problem: find the smallest element in a list L What are the objects involved? The list L and the smallest element X What statements must be true in a solution? X must me a member of L There is no other member, X2, such that X2 < X

Example Problem: find the smallest element in a list L What are the objects involved? The list L and the smallest element X What statements must be true in a solution? X must me a member of L There is no other member, X2, such that X2 < X How do we find a member of a list? No X is a member of the empty list X is a member of list L if it’s equal to L’s head X is a member of list L if it’s a member of L’s tail

Actual code mymin(L, X) :- memberOf(L, X), % X is the smallest element of L if X is a % member of L and there is no X2 that is also % a member and is smaller mymin(L, X) :- memberOf(L, X), \+ (memberOf(X2, L), X2 < X). % X is a member of a list if it is the first element % of the list memberOf(X, [X | Tail] ). % X is a member of a list if it a member of the of % the tail of the list memberOf(X, [_ | Tail] ) :- memberOf(X, Tail).

Note Prolog’s is organized around a database of database of relations between objects, e.g., mymin([2,1,3], 1) is true memberOf(3, [1,2,3]) is true We interact with it by asking if a relation holds Is 13 the smallest element of list L? e.g., is mymin(13, L) true Our query can have variables, e.g. mymin(X, [2,1,3]) proving this produces bindings for the variables

A Simple Prolog Model Think of Prolog as a system which has a database composed of two components: facts: statements about true relations which hold between particular objects in the world. For example: parent(adam, able). % adam is a parent of able parent(eve, able). % eve is a parent of able male(adam). % adam is male. rules: statements about relations between objects in the world which use variables to express generalizations % X is the father of Y if X is a parent of Y and X is male father(X,Y) :- parent(X, Y), male(X). % X is a sibling of Y if X and Y share a parent sibling(X,Y) :- parent(P,X), parent(P,Y), X <> Y.

Nomenclature and Syntax A prolog rule is called a clause A clause has a head, a neck and a body: father(X,Y) :- parent(X,Y) , male(X) . head neck body the head is a single predicate: the rule's conclusion The body is a a sequence of zero or more predicates that are the rule's premise or condition An empty body means the rule’s head is a fact note: read :- as IF read , as AND between predicates a . marks the end of input

Prolog Database parent(adam,able) parent(adam,cain) male(adam) ... Facts comprising the “extensional database” father(X,Y) :- parent(X,Y), male(X). sibling(X,Y) :- ... Rules comprising the “intensional database”

Queries We also have queries in addition to having facts and rules The Prolog REPL interprets input as queries A simple query is just a predicate that might have variables in it: parent(adam, cain) parent(adam, X) Symbol that begin with an uppercase letter or a _ are variables

Running prolog A good free version of prolog is swi-prolog GL has a commercial version (sicstus prolog) you can invoke with the command “sicstus” [finin@linux2 ~]$ sicstus SICStus 3.7.1 (Linux-2.2.5-15-i686): Wed Aug 11 16:30:39 CEST 1999 Licensed to umbc.edu | ?- assert(parent(adam,able)). yes | ?- parent(adam,P). P = able ? | ?-

A Simple Prolog Session | ?- assert(parent(adam,able)). yes | ?- assert(parent(eve,able)). | ?- assert(male(adam)). | ?- parent(adam,able). | ?- parent(adam,X). X = able | ?- parent(X,able). X = adam ; X = eve ; no | ?- parent(X,able) , male(X).

A Prolog Session | ?- [user]. | female(eve). | parent(adam,cain). | parent(eve,cain). | father(X,Y) :- parent(X,Y), male(X). | mother(X,Y) :- parent(X,Y), female(X). | ^Zuser consulted 356 bytes 0.0666673 sec. yes | ?- mother(Who,cain). Who = eve | ?- mother(eve,Who). Who = cain yes | ?- trace, mother(Who,cain). (2) 1 Call: mother(_0,cain) ? (3) 2 Call: parent(_0,cain) ? (3) 2 Exit: parent(adam,cain) (4) 2 Call: female(adam) ? (4) 2 Fail: female(adam) (3) 2 Back to: parent(_0,cain) ? (3) 2 Exit: parent(eve,cain) (5) 2 Call: female(eve) ? (5) 2 Exit: female(eve) (2) 1 Exit: mother(eve,cain) Who = eve

^Zuser consulted 152 bytes 0.0500008 sec. yes | ?- sibling(X,Y). trace,sibling(X,Y). (2) 1 Call: sibling(_0,_1) ? (3) 2 Call: father(_65643,_0) ? (4) 3 Call: parent(_65643,_0) ? (4) 3 Exit: parent(adam,able) (5) 3 Call: male(adam) ? (5) 3 Exit: male(adam) (3) 2 Exit: father(adam,able) (6) 2 Call: father(adam,_1) ? (7) 3 Call: parent(adam,_1) ? (7) 3 Exit: parent(adam,able) (8) 3 Call: male(adam) ? (8) 3 Exit: male(adam) (6) 2 Exit: father(adam,able) (9) 2 Call: mother(_65644,able) ? (10) 3 Call: parent(_65644,able) ? (10) 3 Exit: parent(adam,able) (11) 3 Call: female(adam) ? (11) 3 Fail: female(adam) (10) 3 Back to: parent(_65644,able) ? (10) 3 Exit: parent(eve,able) (12) 3 Call: female(eve) ? (12) 3 Exit: female(eve) (9) 2 Exit: mother(eve,able) (13) 2 Call: mother(eve,able) ? (14) 3 Call: parent(eve,able) ? (14) 3 Exit: parent(eve,able) (15) 3 Call: female(eve) ? (15) 3 Exit: female(eve) (13) 2 Exit: mother(eve,able) (16) 2 Call: not able=able ? (17) 3 Call: able=able ? (17) 3 exit: able=able (16) 2 Back to: not able=able ? (16) 2 Fail: not able=able (15) 3 Back to: female(eve) ? (15) 3 Fail: female(eve) (14) 3 Back to: parent(eve,able) ? (14) 3 Fail: parent(eve,able) (13) 2 Back to: mother(eve,able) ? (13) 2 Fail: mother(eve,able) (12) 3 Back to: female(eve) ? (12) 3 Fail: female(eve) (10) 3 Back to: parent(_65644,able) ? (10) 3 Fail: parent(_65644,able) (9) 2 Back to: mother(_65644,able) ? (9) 2 Fail: mother(_65644,able) (8) 3 Back to: male(adam) ? (8) 3 Fail: male(adam) (7) 3 Back to: parent(adam,_1) ? (7) 3 Exit: parent(adam,cain) (18) 3 Call: male(adam) ? (18) 3 Exit: male(adam) (6) 2 Exit: father(adam,cain) (19) 2 Call: mother(_65644,able) ? (20) 3 Call: parent(_65644,able) ? (20) 3 Exit: parent(adam,able) (21) 3 Call: female(adam) ? (21) 3 Fail: female(adam) (20) 3 Back to: parent(_65644,able) ? (20) 3 Exit: parent(eve,able) (22) 3 Call: female(eve) ? (22) 3 Exit: female(eve) (19) 2 Exit: mother(eve,able) (23) 2 Call: mother(eve,cain) ? (24) 3 Call: parent(eve,cain) ? (24) 3 Exit: parent(eve,cain) (25) 3 Call: female(eve) ? (25) 3 Exit: female(eve) (23) 2 Exit: mother(eve,cain) (26) 2 Call: not able=cain ? (27) 3 Call: able=cain ? (27) 3 Fail: able=cain (26) 2 Exit: not able=cain (2) 1 Exit: sibling(able,cain) X = able Y = cain yes no | ?- | ?- [user]. | sibling(X,Y) :- | father(Pa,X), | father(Pa,Y), | mother(Ma,X), | mother(Ma,Y), | not(X=Y). ^Zuser consulted 152 bytes 0.0500008 sec. yes | ?- sibling(X,Y). X = able Y = cain ; X = cain Y = able ;

Program files [finin@linux2 ~]$ more genesis.pl % prolog example % facts male(adam). female(eve). parent(adam,cain). parent(eve,cain). parent(adam,able). parent(eve,able). % rules father(X,Y) :- parent(X,Y), male(X). mother(X,Y) :- female(X). sibling(X,Y) :- parent(P, X), parent(P, Y). child(X, Y) :- parent(Y, X). Typically you put your assertions (fact and rules) into a file and load it | ?- [genesis]. {consulting /afs/umbc.edu/users/f/i/finin/home/genesis.pl...} {/afs/umbc.edu/users/f/i/finin/home/genesis.pl consulted, 0 msec 2720 bytes} yes | ?- male(adam). | ?- sibling(P1, P2). P1 = cain, P2 = cain ? ; P2 = able ? ; P1 = able, no | ?-

Listing all clauses ?- [genesis]. % genesis compiled 0.00 sec, 12 clauses True. ?- listing. isParent(A) :- parentOf(A, _). :- dynamic parentOf/2. parentOf(adam, able). parentOf(eve, able). parentOf(adam, cain). parentOf(eve, cain). childLess(A) :- \+ isParent(A). motherOf(A, B) :- parentOf(A, B), female(A). fatherOf(A, B) :- parentOf(A, B), male(A). :- dynamic male/1. male(adam). siblings(A, C) :- parentOf(B, A), parentOf(B, C), A\=C. :- dynamic female/1. female(eve). True. ?-

A rules has two readings Consider: father(X,Y) :- parent(X,Y), male(X) Logical or declarative reading The father(X,Y) relation is true if two conditions are true: parent(X,Y) and male(X) Procedural reading To find an X & Y for which father(X,Y) is true, first find values for which parent(X,Y) is true and then check if male(X) is true Consider: father(X,Y) :- male(X), parent(X,Y). Same declarative reading, different procedural reading

How to Satisfy a Goal Here is an informal description of how Prolog satisfies a goal (like father(adam,X)). Suppose the goal is G: if G = P,Q then first satisfy P, carry any variable bindings forward to Q, and then satisfy Q. if G = P;Q then satisfy P. If that fails, then try to satisfy Q. if G = not(P) then try to satisfy P. If this succeeds, then fail and if it fails, then succeed. if G is a simple goal, then look for a fact in the DB that unifies with G look for a rule whose conclusion unifies with G and try to satisfy its body

Note Two basic conditions are true, which always succeeds, and fail, which always fails. Comma (,) represents conjunction (i.e. and). Semi-colon represents disjunction (i.e. or): grandParent(X,Y) :- grandFather(X,Y) ; grandMother(X,Y). No real distinction between rules and facts. A fact is just a rule whose body is the trivial condition true. These are equivalent: parent(adam,cain). parent(adam,cain) :- true.

Note Goals can usually be posed with any of several combination of variables and constants: parent(cain,able) - is Cain Able's parent? parent(cain,X) - Who is a child of Cain? parent(X,cain) - Who is Cain a child of? parent(X,Y) - What two people have a parent/child relationship?

Terms The term is Prolog’s basic data structure The term is to Prolog what the s-expression is to Lisp A term is either: a constant - e.g. 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))

Variables Prolog has logical variables: they can only take on one value Their scope is just one clause or query The Xs and Ys in these rules are independent father(X,Y) :- parent(X,Y), male(X). mother(X,Y) :- parent(X,Y), female(X). Assignment is done via unification ?- foo(X,1) = foo(100,Y). X = 100, Y = 1.

Compound Terms A compound term can be thought of as a relation between one or more terms: part_of(finger,hand) and is written as: the relation name (called the principle functor) which must be a constant. An open parenthesis The arguments - one or more terms separated by commas A closing parenthesis The number of arguments of a compound terms is called its arity. Term arity f 0 f(a) 1 f(a,b) 2 f(g(a),b) 2

Lists Lists are useful., so there’s special syntax to support them, tho they are just terms It’s like Python: [1, [2, 3], 4, foo] But matching is special If L = [1,2,3,4] then L = [Head | Tail] results in Head being bound to 1 and Tail to [2,3,4] If L = [4] then L = [Head | Tail] results in Head being bound to 4 and Tail to []

member member(X, [X|Tail]). member(X, [Head|Tail]) :- member(X, Tail). % member(X,L) is true if X is a member of list L. member(X, [X|Tail]). member(X, [Head|Tail]) :- member(X, Tail).

min min(X, L) :- member(X, L), \+ (member(Y,L), Y>X). % min(X, L) is true if X is the smallest member % of a list of numbers L min(X, L) :- member(X, L), \+ (member(Y,L), Y>X). \+ is Prolog’s negation operator It’s really “negation as failure” \+ G is false if goal G can 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 Numerical computations can be done in logic, but it’s messy and inefficient Prolog provides a simple limited way to do computations <variable> is <expression> succeeds if <variable> can be unified with the value produced by <expression> ?- X=2, Y=4, Z is X+Y. X = 2, Y = 4, Z = 6.

Computations Computations succeed, fail or raise an exception ?- X=1, Y=2, Z=X+Y | . X = 1, Y = 2, Z = 1+2. ?- X=1, X is X+1. false. ?- X is Y+Z. ERROR: is/2: Arguments are not sufficiently instantiated ?- All variables in expression must be instantiated

Computation Example Computation variables usually come from data :- dynamic age/2. age(alice, 40). age(bob, 20). age(carol, 18). older(P1,P2) :- age(P1,A1), age(P2,A2), A1>A2. wayolder(P1,P2) :- TwiceA2 is A2*2, A1 > TwiceA2. ?- [age]. % age … 2 clauses true. ?- older(X,Y). X = alice, Y = bob ; Y = carol . ?- wayolder(X,Y). Y = carol ; False.

From Functions to Relations Prolog facts and rules define relations, not functions Consider age as: A function: calling age(john) returns 22 As a relation: querying age(john, 22) returns true, age(john, X) binds X to 22, and age(john, X) is false for every X ≠ 22 Relations are more general than functions The typical way to define a function f with inputs i1…in and output o is as: f(i1,i2,…in,o)

A numerical example Another example: square(X,Y) :- Y is X*X. Here’s how we might define factorial in Prolog fact(1,1). fact(N,M) :- N > 1, N1 is N-1, fact(N1,M1), M is M1*N. def fact(n): if n==1: return 1 else: n1 = n-1 m1 = fact(n1) m = m1 * n return m Another example: square(X,Y) :- Y is X*X.

Sorting mysort(List,Sorted) :- permute(List,Sorted), is_sorted(Sorted). is_sorted([]). is_sorted)[_]). is_sorted([X,Y|T]) :- X=<Y, is_sorted([Y|T]). permute([], []). permute([X|Rest], L) :- permute(Rest, L1), select(X, L, L1). % true if removing 1 X from L1 yields L

Sorting prolog> swipl ?- [sort]. % sort compiled 0.00 sec, 7 clauses true. ?- select(100, L, [1,2,3]). L = [100, 1, 2, 3] ; L = [1, 100, 2, 3] ; L = [1, 2, 100, 3] ; L = [1, 2, 3, 100] ; false. ?- mysort([9,3,1,8],S). S = [1, 3, 8, 9] ; false. ?- mysort([8,3,1,8],S). S = [1, 3, 8, 8] ;

Prolog = PROgramming in LOGic Prolog is as much a programming language as it is a theorem prover It has a simple, well defined and controllable reasoning strategy that programmers can exploit for efficiency and predictability It has basic data structures (e.g., Lists) and can link to routines in other languages It’s a great programming language for many problems It’s design elements are used in many other systems, e.g., databases with rules.