Presentation on theme: "Getting started with Prolog"— Presentation transcript:
1Getting started with Prolog Logic ProgrammingLecture 1:Course orientation;Getting started with Prolog
2What is Logic Programming? Logic Programming is a programming paradigmcf. object-oriented or functional programmingNot a particular language (like Java or Haskell)Declarative philosophyspecify problem, let computer search for answer
3The declarative dream Slogan: "Algorithm = Logic + Control" Would like to write a program that describes the solutions to the problemComputer then finds answersProgrammer shouldn't have to think about how the system works
4The 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
5Prolog Prolog is the best-known LP language Core based on first-order (predicate) logicAlgorithmic realization via unification, searchMany implementations that make it into a full-fledged programming languageI/O, primitive ops, efficiency issues complicate declarative story
6Why learn LP?LP often great for rapidly prototyping algorithms/search strategies"Declarative" ideas arise in many areas of CSLP concepts very important in AI, databases, PLSAT solvers, model-checking, constraint programmingBecoming important in program analysis, Semantic WebLearning a very different "way to think about problems" makes you a better programmer
7Course objectives Theory Programming first-order logic, semantics of LPunification, resolution proof searchProgrammingbasic LP techniques in Prologhow to use LP to solve interesting problemsAI, parsing, search, symbolic processing
8Organization Lectures M,Th 15:00 First two weeks: 2 programming, 2 theoryRemainder of semester: alternateTheory Monday, programming ThursdayNo lectures week of Nov 1Tutorial/lab sessions: M,Th,F 16:00, from week 3Slides, tutorial problems posted on course web page:
9Evaluation 10 point UG3 course No coursework Final exam: but tutorial participation strongly encouragedTutorial exercises representative of exam problemsFinal exam:Programming 50%Theory 50%UG3 students: pass mark 40%MSc students: pass mark 50%
10Getting started We’ll use SICStus Prolog Free for UofE studentsCan request through Windows supportAvailable on all DICE machinesTutorials, exams will be based on this versionOnline documentation
11Hello WorldProlog is an interactive language.$ sicstus
12Hello WorldProlog is an interactive language.$ sicstus?-Prompt
13Hello World Prolog is an interactive language. $ sicstus ?- print(’hello world’).Goal
14Hello World Prolog is an interactive language. $ sicstus ?- print(’hello world’).hello worldyesOutputresponse
15Atoms An atom is Examples: homer marge lisa bart a sequence of alphanumeric charactersusually starting with lower case letteror, a string enclosed in single quotesExamples:homer marge lisa bart‘Mr. Burns’ ’Principal Skinner’
16Variables A variable is a sequence of alphanumeric characters usually starting with an uppercase letterExamples:X Y Z Parent Child Foo
17Predicates 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)
18Predicates (2) A predicate has a name and an arity = atom p in p(t1,...,tn)and an arity= number of arguments (n)Predicates with same name but different arity are differentWe write foo/1, foo/2, ... to refer to these different predicates
19Punctuation is important! FactsPunctuation 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).
20Goals A goal is a sequence of predicates p(t1,...,tn), ..., q(t1',...,tn').We interpret “,” as conjunctionLogically, read as "p holds of t1...tn and ... and q holds of t1'...tm'"Predicates can be 0-arySome built-ins: true, false, fail
21Answers Given a goal, Prolog searches for answer(s) “yes” (possibly with answer substitution)“no”Substitutions are bindings of variables that make goal trueUse “;” to see more answers
22Examples ?- 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 ;
23Rules 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).
24Miscellaneous Comments To quit Sicstus, type ?- exit. % single line comment/* multiplelinecomment */To quit Sicstus, type?- exit.(or just control-D)
25ConsultingA Prolog program is a collection of facts and rules, or clausesstored in one or more filesThe predicate consult/1 loads the facts/rules in a file?- consult(‘simpsons.pl’).
26Consulting (2) If the file name ends with '.pl', can just write: ?- consult(simpsons).Also, can just write?- [simpsons].
27A complete program /* hello.pl * James Cheney * Sept. 20, 2010 */ main :- print('hello world').
28Tracing trace/0 turns on tracing notrace/0 turns tracing off debugging/0 shows tracing status