160 likes | 263 Views
Logic Programming March 27, 2012 Tom Lever. What is Logic Programming?. Roots in automatic theorem proving Describe the problem, not the solution Let the computer decide how to solve it Describe problem in language native to the field Write declarative sentences in a dialect of FOL
E N D
What is Logic Programming? • Roots in automatic theorem proving • Describe the problem, not the solution • Let the computer decide how to solve it • Describe problem in language native to the field • Write declarative sentences in a dialect of FOL • Computer follows simple rules to analyze • Correct description correct solution (?!)
What is PROLOG? • PROgramming en LOGique • Early 1970s work on natural language processing • Alain Colmerauer – Montreal and Marseille • Robert Kowalski – Imperial College, London • David H D Warren – University of Edinburgh • Warren Abstract Machine (WAM) interpreted code • Declarations are in the form of Horn clauses (see section 17.3 of our textbook - clauses in CNF with at most one positive literal in each disjunction)
What do declarations look like? • Material conditionals (implications) facing left () • Literal :- Literal ^ Literal ^…^ Literal • Each literal takes the form of Predicate(arguments) • Each declaration has the form Head :- Body. • A declaration with only a Head is a Fact owns(max,pris). • A declaration without a head is a Query ? – owns(max,Who). • A complete declaration is a Rule ownsCat(Who, Pet) :- owns(Who, Pet), animal(Pet, cat).
Basic Housekeeping • Predicates and constants begin with lower-case • Variables begin with upper-case • A comma between predicates means AND • Variables are instantiated with values when a predicate is examined (executes) • Predicates in the same declaration are examined left to right. • Facts and rules are preloaded from files.
Fact = an assertion or premise • owns(max, carl). • owns(max, pris). • owns(claire, scruffy). • owns(claire, folly). • animal(carl, dog). • animal(scruffy, cat). • animal(folly, dog). • animal(pris, cat).
Query = interactive way to access data • Queries continue executing until the entire database has been examined. Continue after pauses with Tab or terminate with Enter. • ? – owns(max, folly). • ? – owns(Who, folly). • ? – owns(Who, Pet). • ? – animal(Pet, dog). • ? – ownsCat(Person, Pet). • ? – owns(Person, Pet), animal(Pet, What).
Definite Programs and Goals • Definite program – a set of definite clauses (facts & rules) • Definite goal – a query consisting of atomic formulas as subgoals which must each be satisfied • Query – analogous to relational database query, posing an existential question whether all subgoals can be satisfied • Rules containing variables imply universal quantification • Rules defined recursively can give a model infinite depth • PROLOG code specifies “what” exists but not “how” to find the solution
Backtracking? – animal(Pet, _), owns(Person, Pet). • When a predicate succeeds, it instantiates its variables and control passes to the right • The rightmost predicate pauses after it succeeds, then continues where it left off • When the rightmost predicate fails, control passes back to its predecessor • If it succeeds again, it re-instantiates its variables and control passes to the right again • The rightmost predicate starts from the top
Unification • The process of matching up references and definitions of predicates (argument passing) • Sophisticated pattern matching to instantiate variables in two directions. For example: T = p( X, f(a,b), g(Z,F)) U = p(t(e), W, g(3,C)) • Combining these changes both to this: T = U = p(t(e), f(a,b), g(3,C) where only variable C (or F) remains uninstantiated
Recursion • Used in list processing (similar to LISP) • Defining something in terms of itself • Must define a stop condition append([], List, List). append([Head|Tail], List, [Head|NewTail]) :- append(Tail, List, NewTail). • Sample uses ? – append([a,b,c], [d,e,f], What). ? – append(First, Second, [mary, anne, john]).
Quicksort Pseudocode function quicksort('array') if length('array') ≤ 1 return 'array' // 0 or 1 elements already sorted select and remove a pivot value 'pivot' from 'array' create empty lists 'less' and 'greater' for each 'x' in 'array' if 'x' ≤ 'pivot' then append 'x' to 'less' else append 'x' to 'greater' return concatenate(quicksort('less'), 'pivot', quicksort('greater')) // two recursive calls
Quicksort in PROLOG partition([], _, [], []). partition([X|Xs], Pivot, Smalls, Bigs) :- ( X @< Pivot -> Smalls = [X|Rest], partition(Xs, Pivot, Rest, Bigs) ; Bigs = [X|Rest], partition(Xs, Pivot, Smalls, Rest) ). quicksort([]) --> []. quicksort([X|Xs]) --> { partition(Xs, X, Smaller, Bigger) }, quicksort(Smaller), [X], quicksort(Bigger).
Applications • Natural language processing • Artificial intelligence • Theorem proving • Ontologies (information frameworks) • Expert systems • Automated answering systems • Control systems • Games
References • Bharath, R. An Introduction to PROLOG, Tab Books 1986 • Saint-Dizier, P. An Introduction to Programming in PROLOG, Springer-Verlag, 1990 • http://www.learnprolognow.org/ - a free online introductory course to PROLOG, with links to free downloadable implementations • http://en.wikipedia.org/wiki/Prolog- overview with abundant links leading in many directions • http://www.cs.bham.ac.uk/~pjh/prolog_course/sem223_se.html - Logic Programming with Prolog – An Introduction for Software Engineers - course overview