Logic Programming
-
Upload
arsenio-sutton -
Category
Documents
-
view
50 -
download
0
description
Transcript of Logic Programming
![Page 1: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/1.jpg)
Logic Programming
Programming Lecture 1: Getting started with Prolog
![Page 2: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/2.jpg)
James Cheney Logic Programming September 18, 2014
Recap•Logic programming is a form of declarative programming
• "Algorithm = logic + control"
• Specify a problem, let computer find solution
• This does not always work out as well as we would wish
• Writing effective logic programs generally still requires pragmatic knowledge
![Page 3: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/3.jpg)
James Cheney Logic Programming September 18, 2014
Why learn LP?• Learning a very different "way to think about
problems" makes you a better programmer
• LP works well for rapidly prototyping algorithms/search strategies, which can be transferred to mainstream language
• "Declarative" ideas arise in many areas of CS and AI
• LP concepts very important in AI, databases, PL
• SAT solvers, model-checking, constraint programming
• Becoming important in program analysis, Semantic Web
• Learning how logic provides a foundation for computation can improve your understanding of both
![Page 4: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/4.jpg)
James Cheney Logic Programming September 18, 2014
Getting started• There are many Prolog implementations & dialects
• We’ll use SICStus Prolog
• Free for UofE students
• Can request through Windows support
• Available on all DICE machines
• You should become familiar with running on DICE
• Tutorials, assignments, exams will be based on this version
• Online documentationhttp://sicstus.sics.se/documentation.html
![Page 5: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/5.jpg)
James Cheney Logic Programming September 18, 2014
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 6: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/6.jpg)
James Cheney Logic Programming September 18, 2014
Hello World
•Prolog is an interactive language.
$ sicstus
![Page 7: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/7.jpg)
James Cheney Logic Programming September 18, 2014
Hello World
•Prolog is an interactive language.
$ sicstus
?- PromptPrompt
![Page 8: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/8.jpg)
James Cheney Logic Programming September 18, 2014
Hello World
•Prolog is an interactive language.
$ sicstus
?- print(’hello world’).
GoalGoal
![Page 9: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/9.jpg)
James Cheney Logic Programming September 18, 2014
Hello World
•Prolog is an interactive language.
$ sicstus
?- print(’hello world’).
hello world
yes
OutputOutput
responsresponsee
![Page 10: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/10.jpg)
James Cheney Logic Programming September 18, 2014
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 11: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/11.jpg)
James Cheney Logic Programming September 18, 2014
Variables
•A variable is a sequence of alphanumeric characters
•usually starting with an uppercase letter
•Examples:
X Y Z Parent Child Foo _Bar
![Page 12: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/12.jpg)
James Cheney Logic Programming September 18, 2014
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 13: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/13.jpg)
James Cheney Logic Programming September 18, 2014
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 14: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/14.jpg)
James Cheney Logic Programming September 18, 2014
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 15: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/15.jpg)
James Cheney Logic Programming September 18, 2014
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 16: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/16.jpg)
James Cheney Logic Programming September 18, 2014
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 17: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/17.jpg)
James Cheney Logic Programming September 18, 2014
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 18: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/18.jpg)
James Cheney Logic Programming September 18, 2014
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 19: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/19.jpg)
James Cheney Logic Programming September 18, 2014
Miscellaneous• Comments
% single line comment
/* multiple
line
comment */
• To quit Sicstus, type
?- halt.
•(or just control-D)
![Page 20: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/20.jpg)
James Cheney Logic Programming September 18, 2014
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 21: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/21.jpg)
James Cheney Logic Programming September 18, 2014
Consulting (2)
•If the file name ends with '.pl', can just write:
?- consult(simpsons).
•Also, can just write
?- [simpsons].
![Page 22: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/22.jpg)
James Cheney Logic Programming September 18, 2014
A complete program
/* hello.pl
* James Cheney
* Sept. 18, 2014
*/
main :- print('hello world').
![Page 23: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/23.jpg)
James Cheney Logic Programming September 18, 2014
Tracing
•trace/0 turns on tracing
•notrace/0 turns tracing off
•debugging/0 shows tracing status
![Page 24: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/24.jpg)
James Cheney Logic Programming September 18, 2014
More kinds of terms
•Story so far...
• Atoms: homer marge 'Mr. Burns'
• Variables: X Y Z MR_BURNS
•Also have...
• Numbers: 1 2 3 42 -0.12345
• Lists [1,2,3] and other complex terms
• Additional constants and infix operators
![Page 25: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/25.jpg)
James Cheney Logic Programming September 18, 2014
Complex terms•A complex term is of the form
f(t1,...,tn)
•where f is an atom and t1...tn are terms
•Examples:
f(1,2) node(leaf,leaf) cons(42,nil)
household(homer, marge, bart, lisa, maggie)
![Page 26: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/26.jpg)
James Cheney Logic Programming September 18, 2014
More about lists•Lists are built-in (and very useful)
data structures
•Syntax:
[1,2,3,4]
[a,[1,2,3],42,'forty-two']
[a,b,c|Xs]
•(Lots) More next week
![Page 27: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/27.jpg)
James Cheney Logic Programming September 18, 2014
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 28: Logic Programming](https://reader031.fdocuments.us/reader031/viewer/2022012922/568134e7550346895d9c1e96/html5/thumbnails/28.jpg)
James Cheney Logic Programming September 18, 2014
Next time
•Equality and unification
•How Prolog searches for answers
•Reminder: Tutorials start in week 3