logic programming lecture 1: getting started. getting started we’ll use sicstus prolog free for...

21
Logic Programming Lecture 1: Getting started

Upload: ezra-nowland

Post on 01-Apr-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

Logic Programming

Lecture 1: Getting started

Page 2: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

Getting started

•We’ll use SICStus Prolog

•Free for UofE students

•Available on all DICE machineshttp://www.sics.se/isl/sicstuswww/site/index.html

Page 3: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

Hello World

•Prolog is an interactive language.

$ sicstus

Page 4: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

Hello World

•Prolog is an interactive language.

$ sicstus

?- PromptPrompt

Page 5: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

Hello World

•Prolog is an interactive language.

$ sicstus

?- print(’hello world’).

GoalGoal

Page 6: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

Hello World

•Prolog is an interactive language.

$ sicstus

?- print(’hello world’).

hello world

yes

OutputOutput

responsresponsee

Page 7: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

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 8: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

Variables

•A variable is a sequence of alphanumeric characters

•usually starting with an uppercase letter

•Examples:

X Y Z Parent Child Foo

Page 9: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

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 10: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

Predicates (2)• Predicates have 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 11: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

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 12: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

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'"

Page 13: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

Answers

•Given a goal, Prolog searches for answer(s)

• “yes” (possibly with substitution)

• “no”

•Substitutions are bindings of variables that make goal true

•Use “;” to see more answers

Page 14: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

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 15: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

Tracing

•trace/0 turns on tracing

•notrace/0 turns tracing off

•debugging/0 shows tracing status

Page 16: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

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 17: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

Consulting (2)

•If the file name ends with '.pl', can just write:

?- consult(simpsons).

•Also, can just write

?- [foo].

Page 18: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

Simple I/O• read/1: reads a term from input and binds its

argument

• write/1: writes a term to output

• Example:

loop :- read(X),

process(X,Y),

write(Y),

loop.

• For most programming problems you can just work interactively

Page 19: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

Further reading•Quick 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 20: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

Next time

•Compound terms

•Equality and unification

•How Prolog searches for answers

Page 21: Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

Exercises•Using simpsons.pl, write goal

bodies for:

• classmate(X,Y)

• employer(X)

• parent(X,Y)

• grandparent(X,Y)

•More in tutorial problems handout