1 / 24

Logic Programming

Logic Programming. Lecture 1: Course orientation; Getting started with Prolog Assoc. Prof. Zeki Bayram Based on slides by James Cheney. What is Logic Programming?. Logic Programming is a programming paradigm cf. object-oriented or functional programming

emele
Download Presentation

Logic Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Logic Programming • Lecture 1: • Course orientation; • Getting started with Prolog • Assoc. Prof. Zeki Bayram • Based on slides by James Cheney

  2. What is Logic Programming? • Logic Programming is a programming paradigm • cf. object-oriented or functional programming • Not a particular language (like Java or Haskell) • Declarative philosophy • specify problem, let computer search for answer

  3. The declarative dream • Slogan: "Algorithm = Logic + Control" • Would like to write a program that describes the solutions to the problem • Computer then finds answers • Programmer shouldn't have to think about how the system works

  4. The reality • Purely declarative programming can only get you so far • I/O, interaction with outside world, seem inherently "imperative" • For efficiency/termination, sometimes need finer-grained control over search

  5. Prolog • Prolog is the best-known LP language • Core based on first-order (predicate) logic • Algorithmic realization via unification, search • Many implementations that make it into a full-fledged programming language • I/O, primitive ops, efficiency issues complicate declarative story

  6. Why learn LP? • LP often great for rapidly prototyping algorithms/search strategies • "Declarative" ideas arise in many areas of CS • LP concepts very important in AI, databases, PL • SAT solvers, model-checking, constraint programming • Becoming important in program analysis, Semantic Web • Learning a very different "way to think about problems" makes you a better programmer

  7. Course objectives • Theory • first-order logic, semantics of LP • unification, resolution proof search • Programming • basic LP techniques in Prolog • how to use LP to solve interesting problems • AI, parsing, search, symbolic processing

  8. Atoms • An atom is • a sequence of alphanumeric characters • usually starting with lower case letter • or, a string enclosed in single quotes • Examples: • homer marge lisa bart • ‘Mr. Burns’ ’Principal Skinner’

  9. Variables • A variable is a sequence of alphanumeric characters • usually starting with an uppercase letter • Examples: • X Y Z Parent Child Foo

  10. Predicates • A predicate has the form • p(t1,...,tn) • where p is an atom and t1...tn are terms • (For now a term is just an atom or variable) • Examples: • father(homer, bart) • mother(marge, bart)

  11. Predicates (2) • A predicate has a name • = atom p in p(t1,...,tn) • and an arity • = number of arguments (n) • Predicates with same name but different arity are different • We write foo/1, foo/2, ... to refer to these different predicates

  12. Facts Punctuation is important! • A fact is an assertion that a predicate is true: • father(homer, bart). • mother(marge, bart). • A collection of facts is sometimes called a knowledge base (or database).

  13. Goals • A goal is a sequence of predicates • p(t1,...,tn), ..., q(t1',...,tn'). • We interpret “,” as conjunction • Logically, read as "p holds of t1...tn and ... and q holds of t1'...tm'" • Predicates can be 0-ary • Some built-ins: true, false, fail

  14. Answers • Given a goal, Prolog searches for answers • “yes” (possibly with answer substitution) • “no” • Substitutions are bindings of variables that make goal true • Use “;” to see more answers

  15. Examples • ?- father(X,bart). • X = homer ; • no • ?- father(X,Z), mother(Y,Z). • X = homer, Y = marge, Z = bart ; • X = homer, Y = marge, Z = lisa ; • X = homer, Y = marge, Z = maggie ; • no

  16. Rules • A rule is an assertion of the form • p(ts) :- q(ts’), ..., r(ts’’). • where ts, ts’, ts’’ are sequences of terms • “p(ts) holds if q(ts’) holds and ... and r(ts’’) holds” • Example: • sibling(X,Y) :- parent(Z,X), • parent(Z,Y).

  17. Miscellaneous • Comments • % single line comment • /* multiple • line • comment */

  18. Consulting • A Prolog program is a collection of facts and rules, or clauses • stored in one or more files • The predicate consult/1 loads the facts/rules in a file • ?- consult(‘simpsons.pro’).

  19. Consulting (2) • If the file name ends with '.pl', can just write: • ?- consult(simpsons). • Also, can just write • ?- [simpsons].

  20. A complete program • /* hello.pl • * James Cheney • * Sept. 20, 2010 • */ • main :- write('hello world').

  21. Tracing • trace/0 turns on tracing • notrace/0 turns tracing off • debugging/0 shows tracing status • But: better to use Amzi Prolog’s built-in debugger

  22. Further reading • Quick Start Prolog notes (Dave Robertson) • http://www.inf.ed.ac.uk/teaching/courses/lp/prolognotes.pdf • Learn Prolog Now! (Blackburn, Bos, Striegnitz) • online, free • http://www.learnprolognow.org/ • Programming in Prolog (Clocksin & Mellish) • a standard/classic text

  23. Exercises • Using simpsons.pro, write goal bodies for: • classmate(X,Y) • employer(X) • parent(X,Y) • grandparent(X,Y)

  24. Next time • Compound terms • Equality and unification • How Prolog searches for answers

More Related