**1. **Prolog for Dummies Ulf Nilsson
Dept of Computer and Information Science
Link?ping University

**2. **Logic programs

**3. **Relations Adam is a parent of Bill
Paris is the capital of France
5 is greater than 2 plus 2
X times 1 is equal to X
X is a subset of Y
5 is the maximum of 2 and 5
There is an edge from a to b

**4. **Properties Adam is a parent
Adam is male
X plus 1 is non-zero
Paris is a capital
Grass is green
The music was loud

**5. **Queries Who is the father of Bill?
Is there an edge from a to b?
Which town is a capital?
Who is male?

**6. **Language primitives Constantsadam, paris, 5, 3.14, [], ?Adam?, ...
VariablesX, Y, List, _12, _, ...
Function symbolsplus/2, +/2, f/1, ...
Predicate symbolscapital/2, greater/2, non_zero/1, >/2, ...

**7. **Terms Terms represent individuals
Constants
Variables
Compound terms
E.g. paris, X, plus(2,3), plus(2,plus(3,4))
Infix notation: 2+3

**8. **Atomic formulas Atomic formulas describe relations:
If p is a predicate letter of arity n and t1,...,tn are terms then p(t1,...,tn) is an atomic formula.
E.g. capital(paris,france) greater(X,2)
Infix notation: X > 2

**9. **Logic Programs A logic program is a set of clauses:
facts
rules
The program is used to answer queries.

**10. **Facts A fact is an expression of the form:
A.
where A is an atomic formula.
Examples:
edge(a, X).
parent(adam, bill).

**11. **Interpretation Facts Consider a fact A.
Declarative (logical) reading:
For all variables, A is true.
Procedural (operational) reading:
A is solved.

**12. **Rules A rule is an expression of the form:
A0 :- A1, ... , An.
where each Ai is an atomic formula.
Examples:
path(X,Y) :- edge(X,Y).
father(X,Y) :- parent(X,Y), male(X).

**13. **Interpretation Rules Consider a rule A0 :- A1, ... , An.
Declarative (logical) reading:
For all variables, A0 if A1 and...and An.
Procedural (operational) reading:
To solve A0, first solve A1, then A2 etc.

**14. **Example

**15. **Queries A query is an expression of the form:
?- A1, ..., An.
where n=0,1,2,... and A1, ..., An are atomic formulas.
Examples:
?- father(X, bill).
?- parent(X, bill), male(X).

**16. **Interpretation Queries Consider a query ?- A1, ... , An.
Declarative (logical) reading:
Are there variables such that A1 and...and An?
Procedural (operational) reading:
First solve A1, then A2 etc

**17. **Ground SLD-Resolution

**18. **A Derivation

**19. **Another Derivation

**20. **Full SLD-Resolution

**21. **Yet Another Derivation

**22. **And Another One...

**23. **And a Failed One...

**24. **SLD-Tree

**25. **Example

**26. **Database

**27. **Recursion

**28. **List Notation

**29. **More On List Notation The empty list: []
A non-empty list: .(X,Y) or [X|Y]
Syntactic Sugar:
[b] instead of [b|[]] and .(b, [])
[a,b] instead of [a|[b]] and [a|[b|[]]]
[a,b|X] instead of [a|[b|X]]

**30. **List manipulation

**31. **List Manipulation

**32. **Insertion Sort

**33. **Binary Trees

**34. **Built In Predicates setof(X, p(X), S)
~ S is the set of all X such that p(X)
bagof(X, p(X), B)
~ B is the sequence of all X such that p(X)
findall(X, p(X), B)
B is the sequence of all X such that p(X)

**35. **Negation Prolog contains a weak form of negation
called ?negation as failure?.
Written: \+ p(a)
A query ?- \+ p(a) succeeds if the query ?- p(a) fails finitely.
Robust only when the goal contains no variables. (Use only as a test!)

**36. **Example Negation