Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines"— Presentation transcript:

1 Logic Programming Lecture 1: Getting started

2 Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines

3 Hello World Prolog is an interactive language. $ sicstus

4 Hello World Prolog is an interactive language. $ sicstus ?- Prompt

5 Hello World Prolog is an interactive language. $ sicstus ?- print(’hello world’). Goal

6 Hello World Prolog is an interactive language. $ sicstus ?- print(’hello world’). hello world yes Output response

7 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’

8 Variables A variable is a sequence of alphanumeric characters usually starting with an uppercase letter Examples: X Y Z Parent Child Foo

9 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)

10 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

11 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 important!

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

13 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

14 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).

15 Tracing trace/0 turns on tracing notrace/0 turns tracing off debugging/0 shows tracing status

16 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’).

17 Consulting (2) If the file name ends with '.pl', can just write: ?- consult(simpsons). Also, can just write ?- [foo].

18 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

19 Further reading Quick Prolog notes (Dave Robertson) Learn Prolog Now! (Blackburn, Bos, Striegnitz) online, free Programming in Prolog (Clocksin & Mellish) a standard/classic text, many library copies

20 Next time Compound terms Equality and unification How Prolog searches for answers

21 Exercises Using simpsons.pl, write goal bodies for: classmate(X,Y) employer(X) parent(X,Y) grandparent(X,Y) More in tutorial problems handout


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

Similar presentations


Ads by Google