![Page 1: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/1.jpg)
Logic Programming
Lecture 1: Course orientation;
Getting started with Prolog
![Page 2: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/2.jpg)
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
![Page 3: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/3.jpg)
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
![Page 4: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/4.jpg)
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
![Page 5: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/5.jpg)
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
![Page 6: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/6.jpg)
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
![Page 7: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/7.jpg)
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
![Page 8: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/8.jpg)
Organization• Lectures M,Th 15:00
• First two weeks:
• 2 programming, 2 theory
• Remainder of semester: alternate
• Theory Monday, programming Thursday
• No lectures week of Nov 1
• Tutorial/lab sessions: M,Th,F 16:00, from week 3
• Slides, tutorial problems posted on course web page:
• www.inf.ed.ac.uk/teaching/courses/lp/
![Page 9: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/9.jpg)
Evaluation• 10 point UG3 course
• No coursework
• but tutorial participation strongly encouraged
• Tutorial exercises representative of exam problems
• Final exam:
• Programming 50%
• Theory 50%
• UG3 students: pass mark 40%
• MSc students: pass mark 50%
![Page 10: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/10.jpg)
Getting started•We’ll use SICStus Prolog
• Free for UofE students
• Can request through Windows support
•Available on all DICE machines
• Tutorials, exams will be based on this version
•Online documentationhttp://www.sics.se/isl/sicstuswww/site/index.html
![Page 11: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/11.jpg)
Hello World
•Prolog is an interactive language.
$ sicstus
![Page 12: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/12.jpg)
Hello World
•Prolog is an interactive language.
$ sicstus
?- PromptPrompt
![Page 13: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/13.jpg)
Hello World
•Prolog is an interactive language.
$ sicstus
?- print(’hello world’).
GoalGoal
![Page 14: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/14.jpg)
Hello World
•Prolog is an interactive language.
$ sicstus
?- print(’hello world’).
hello world
yes
OutputOutput
responsresponsee
![Page 15: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/15.jpg)
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’
![Page 16: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/16.jpg)
Variables
•A variable is a sequence of alphanumeric characters
•usually starting with an uppercase letter
•Examples:
X Y Z Parent Child Foo
![Page 17: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/17.jpg)
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)
![Page 18: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/18.jpg)
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
![Page 19: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/19.jpg)
Facts
•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).
Punctuation is Punctuation is important!important!
![Page 20: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/20.jpg)
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
![Page 21: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/21.jpg)
Answers•Given a goal, Prolog searches for
answer(s)
• “yes” (possibly with answer substitution)
• “no”
•Substitutions are bindings of variables that make goal true
•Use “;” to see more answers
![Page 22: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/22.jpg)
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
![Page 23: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/23.jpg)
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).
![Page 24: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/24.jpg)
Miscellaneous• Comments
% single line comment
/* multiple
line
comment */
• To quit Sicstus, type
?- exit.
•(or just control-D)
![Page 25: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/25.jpg)
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.pl’).
![Page 26: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/26.jpg)
Consulting (2)
•If the file name ends with '.pl', can just write:
?- consult(simpsons).
•Also, can just write
?- [simpsons].
![Page 27: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/27.jpg)
A complete program
/* hello.pl
* James Cheney
* Sept. 20, 2010
*/
main :- print('hello world').
![Page 28: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/28.jpg)
Tracing
•trace/0 turns on tracing
•notrace/0 turns tracing off
•debugging/0 shows tracing status
![Page 29: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/29.jpg)
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, many library copies
![Page 30: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/30.jpg)
Exercises•Using simpsons.pl, write goal
bodies for:
• classmate(X,Y)
• employer(X)
• parent(X,Y)
• grandparent(X,Y)
•More in tutorial problems handout
![Page 31: Logic Programming Lecture 1: Course orientation; Getting started with Prolog](https://reader035.vdocuments.us/reader035/viewer/2022062223/551ac4eb550346b2288b55c3/html5/thumbnails/31.jpg)
Next time
•Compound terms
•Equality and unification
•How Prolog searches for answers