P. 1
Prolog Notes

# Prolog Notes

|Views: 75|Likes:
Prolog Notes
Prolog Notes

Published by: Andreas Michael on Feb 18, 2013

### Availability:

Read on Scribd mobile: iPhone, iPad and Android.
See more
See less

02/18/2013

pdf

text

original

## Sections

• Facts, Rules, and Queries
• 1.1 Some simple examples
• 1.1.1 Knowledge Base 1
• 1.1.2 Knowledge Base 2
• 1.1.3 Knowledge Base 3
• 1.1.4 Knowledge Base 4
• 1.1.5 Knowledge Base 5
• 1.2 Prolog Syntax
• 1.2.1 Atoms
• 1.2.2 Numbers
• 1.2.3 Variables
• 1.2.4 Complex terms
• 1.3 Exercises
• 1.4 Practical Session 1
• Matching and Proof Search
• 2.1 Matching
• 2.1.1 Examples
• 2.1.2 The occurs check
• 2.1.3 Programming with matching
• 2.2 Proof Search
• 2.3 Exercises
• 2.4 Practical Session 2
• Recursion
• 3.1 Recursive deﬁnitions
• 3.1.1 Example 1: Eating
• 3.1.2 Example 2: Descendant
• 3.1.3 Example 3: Successor
• 3.1.4 Example 3: Addition
• 3.2 Clause ordering, goal ordering, and termination
• 3.3 Exercises
• 3.4 Practical Session 3
• 4.1 Lists
• 4.2 Member
• 4.3 Recursing down lists
• 4.4 Exercises
• 4.5 Practical Session 4
• Arithmetic
• 5.1 Arithmetic in Prolog
• 5.2 A closer look
• 5.3 Arithmetic and lists
• 5.4 Comparing integers
• 5.5 Exercises
• 5.6 Practical Session 5
• More Lists
• 6.1 Append
• 6.1.1 Deﬁning append
• 6.1.2 Using append
• 6.2 Reversing a list
• 6.2.1 Naive reverse using append
• 6.2.2 Reverse using an accumulator
• 6.3 Exercises
• 6.4 Practical Session 6
• Deﬁnite Clause Grammars
• 7.1 Context free grammars
• 7.1.1 CFG recognition using append
• 7.1.2 CFG recognition using difference lists
• 7.2 Deﬁnite clause grammars
• 7.2.1 A ﬁrst example
• 7.2.2 Adding recursive rules
• 7.2.3 A DCG for a simple formal language
• 7.3 Exercises
• 7.4 Practical Session 7
• More Deﬁnite Clause Grammars
• 8.1 Extra arguments
• 8.1.1 Context free grammars with features
• 8.1.2 Building parse trees
• 8.1.3 Beyond context free languages
• 8.2 Extra goals
• 8.2.1 Separating rules and lexicon
• 8.3 Concluding remarks
• 8.4 Exercises
• 8.5 Practical Session 8
• A Closer Look at Terms
• 9.1 Comparing terms
• 9.2 Terms with a special notation
• 9.2.1 Arithmetic terms
• 9.2.2 Lists as terms
• 9.3 Examining Terms
• 9.3.1 Types of Terms
• 9.3.2 The Structure of Terms
• 9.4 Operators
• 9.4.1 Properties of operators
• 9.4.2 Deﬁning operators
• 9.5 Exercises
• 9.6 Practical Session
• 10.1 The cut
• 10.2 If-then-else
• 10.3 Negation as failure
• 10.4 Exercises
• 10.5 Practical Session 10
• Database Manipulation and Collecting Solutions
• 11.1 Database manipulation
• 11.2 Collecting solutions
• 11.2.1 findall/3
• 11.2.2 bagof/3
• 11.2.3 setof/3
• 11.3 Exercises
• 11.4 Practical Session 11
• 12.1 Splitting Programs Over Files
• 12.1.1 Reading in Programs
• 12.1.2 Modules
• 12.1.3 Libraries
• 12.2 Writing To and Reading From Files
• 12.3 Practical Session
• 12.3.1 Step 1
• 12.3.2 Step 2
• 12.3.3 Step 3
• 12.3.4 Step 4
• 12.3.5 Step 5
• 12.3.6 Step 6

Exercise 3.1 Do you know these wooden Russian dolls, where smaller ones are con-
tained in bigger ones? Here is schematic picture of such dolls.

50

Chapter 3. Recursion

Deﬁne a predicate in/2, that tells us which doll is (directly or indirectly) contained
in which other doll. E.g. the query
in(katarina,natasha) should evaluate to true,
while
in(olga, katarina) should fail.

Exercise 3.2 Deﬁne a predicate greater_than/2 that takes two numerals in the no-
tation that we introduced in this lecture (i.e. 0, succ(0), succ(succ(0)) ...) as arguments
and decides whether the ﬁrst one is greater than the second one. E.g:

?- greater_than(succ(succ(succ(0))),succ(0)).

yes

?- greater_than(succ(succ(0)),succ(succ(succ(0)))).

no

Exercise 3.3 We have the following knowledge base:

directTrain(forbach,saarbruecken).

directTrain(freyming,forbach).

directTrain(fahlquemont,stAvold).

directTrain(stAvold,forbach).

directTrain(saarbruecken,dudweiler).

directTrain(metz,fahlquemont).

directTrain(nancy,metz).

That is, this knowledge base holds facts about towns it is possible to travel between by
taking a
direct train. But of course, we can travel further by ‘chaining together’ direct
train journeys. Write a recursive predicate
travelBetween/2 that tells us when we
can travel by train between two towns. For example, when given the query

travelBetween(nancy,saarbruecken).

3.4. Practical Session 3

51

it should reply ‘yes’.

It is, furthermore, plausible to assume that whenever it is possible to take a direct train
from A to B, it is also possible to take a direct train from B to A. Can you encode this
in Prolog? You program should e.g. answer ‘yes’ to the following query:

travelBetween(saarbruecken,nancy).

Do you see any problems you program may run into?

scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->