Lecture 1:
Course orientation;
Getting started with Prolog
•
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
James Cheney Logic Programming September 19, 2011 answer
•
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
James Cheney Logic Programming September 19, 2011
•
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
James Cheney Logic Programming September 19, 2011
•
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
James Cheney complicate declarative story
Logic Programming September 19, 2011
•
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
James Cheney Logic Programming September 19, 2011
•
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
•
Lectures M,Th 15:00
•
Programming Monday (James Cheney)
•
Theory Thursday (Alex Simpson)
•
No lectures week of October 17-21
•
Tutorial/lab sessions: TBA , from week 3
•
Assignment 1 (programming) due: October 24
•
Assignment 2 (theory) due: November 14
•
Slides, tutorial problems posted on course web page:
www.inf.ed.ac.uk/teaching/courses/lp/
James Cheney Logic Programming September 19, 2011
•
10 point UG3 course
•
Coursework
•
There will be two formatively assessed assignments
•
Marking/feedback provided but not part of final grade
•
Tutorial participation strongly encouraged
•
Final exam:
•
Programming 50%
•
Theory 50%
•
Tutorial, coursework exercises representative of exam problems
•
UG3 students: pass mark 40%
•
MSc students: pass mark 50%
James Cheney Logic Programming September 19, 2011
•
We’ll use SICStus Prolog
•
Available on all DICE machines
•
Tutorials, exams will be based on this version
•
Windows, Mac version free for UofE students
•
Can request through Computing Support
•
Online documentation
James Cheney Logic Programming September 19, 2011
• http://www.sics.se/isl/sicstuswww/site/index.html
•
Prolog is an interactive language.
•
$ sicstus
James Cheney Logic Programming September 19, 2011
•
Prolog is an interactive language.
•
$ sicstus
•
?-
Prompt
James Cheney Logic Programming September 19, 2011
•
Prolog is an interactive language.
•
$ sicstus
•
?print(’hello world’).
Goal
James Cheney Logic Programming September 19, 2011
•
Prolog is an interactive language.
•
$ sicstus
•
?print(’hello world’).
• hello world
• yes
Output response
James Cheney Logic Programming September 19, 2011
•
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’
James Cheney Logic Programming September 19, 2011
•
A variable is a sequence of alphanumeric characters
• usually starting with an uppercase letter
•
Examples:
•
X Y Z Parent Child Foo
James Cheney Logic Programming September 19, 2011
•
A predicate has the form
• p(t
1
,...,t n
)
• where p is an atom and t
1
...t
n are terms
•
(For now a term is just an atom or variable)
•
Examples:
• father(homer, bart)
• mother(marge, bart)
James Cheney Logic Programming September 19, 2011
•
A predicate has a name
= atom p in p(t
1
,...,t n
)
• 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
James Cheney Logic Programming September 19, 2011
•
A fact is an assertion that a predicate is true:
• father(homer, bart).
important!
• mother(marge, bart).
•
A collection of facts is sometimes called a knowledge base (or database ) .
James Cheney Logic Programming September 19, 2011
•
A goal is a sequence of predicates
p(t
1
,...,t n
), ..., q(t
1
',...,t n
').
•
We interpret “ , ” as conjunction
•
Logically, read as " p holds of t
1
...t
n and ... and q holds of t
1
'...t
m
' "
•
Predicates can be 0-ary
•
Some built-ins: true , false , fail
James Cheney Logic Programming September 19, 2011
•
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
James Cheney Logic Programming September 19, 2011
•
?- 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
James Cheney Logic Programming September 19, 2011
•
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).
James Cheney Logic Programming September 19, 2011
•
Comments
•
% single line comment
•
/* multiple
• line
• comment */
•
To quit Sicstus, type
•
?exit.
•
(or just control-D)
James Cheney Logic Programming September 19, 2011
•
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’).
James Cheney Logic Programming September 19, 2011
•
If the file name ends with '.pl', can just write:
•
?consult(simpsons).
•
Also, can just write
•
?[simpsons].
James Cheney Logic Programming September 19, 2011
•
/* hello.pl
•
* James Cheney
•
* Sept. 20, 2010
•
*/
• main :- print('hello world').
James Cheney Logic Programming September 19, 2011
• trace/0 turns on tracing
• notrace/0 turns tracing off
• debugging/0 shows tracing status
James Cheney Logic Programming September 19, 2011
•
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)
James Cheney Logic Programming September 19, 2011 • a standard/classic text, many library copies
•
Using simpsons.pl
, write goal bodies for:
• classmate(X,Y)
• employer(X)
• parent(X,Y)
• grandparent(X,Y)
•
More in tutorial problems handout
James Cheney Logic Programming September 19, 2011
•
Compound terms
•
Equality and unification
•
How Prolog searches for answers
James Cheney Logic Programming September 19, 2011