Sie sind auf Seite 1von 131

Arithmetic

Arithmetic does not fit nicely into the logical scheme of things. The concept of evaluating an arithmetic expression is in contrast to the straight pattern matching we have seen so far. Prolog provides the built-in predicate 'is' that evaluates arithmetic expressions.

7/6/2008

Arithmetic
X is <arithmetic expression> The variable X is set to the value of the arithmetic expression. On backtracking it is unassigned. The arithmetic expression looks like an arithmetic expression in any other programming language.

7/6/2008

Arithmetic
Here is how to use Prolog as a calculator.
7/6/2008

?- X is 2 + 2. X=4 ?- X is 3 * 4 + 2. X = 14
3

Arithmetic
Parentheses clarify precedence. ?- X is 3 * (4 + 2). X = 18 ?- X is (8 / 4) / 2. X=1
4

7/6/2008

Comparison operators
Comparison operators that compare two numbers. These include 'greater than', 'less than', 'greater or equal than', and 'less or equal than.' They behave more logically, and succeed or fail according to whether the comparison is true or false. X>Y X<Y X >= Y X =< Y
7/6/2008 5

Examples


7/6/2008

?- 4 > 3. yes ?- 4 < 3. no ?- X is 2 + 2, X > 3. X=4 ?- X is 2 + 2, 3 >= X. no ?- 3+4 > 3*2. yes
6

Examples
They can be used in rules as well. c_to_f(C,F) :F is C * 9 / 5 + 32. freezing(F) :F =< 32.

7/6/2008

Examples

?- c_to_f(100,X). X = 212 yes ?- freezing(15). yes ?- freezing(45). no


8

7/6/2008

Read & Write


read(X). Prolog waits for some input from the user Prompt > It will read the next term that you type in up to a full stop followed by a return ?- read(V). > ok. V = ok
7/6/2008 9

Read & Write


?- read(X). > healthy(asif). X = healthy(asif) If the argument of read is non-variable, the term typed must match the argument of the read goal ?- read(book). > book. yes

7/6/2008

10

Read & Write


write(X). It will write whatever term X is bound to, on your screen ?- write(salma). salma yes ?- X=5103, write(X). 5103 yes
7/6/2008 11

Read & Write


?- write(Welcome to the). Welcome to the nl next line. The nl goal always succeeds. ?- write(Welcome to the), nl. Write a predicate nls with one argument representing the number of lines to advance.
7/6/2008 12

Built-in Predicates

7/6/2008

13

fail The goal always fails immediately. It is used to initiates backtracking to find all solutions.
flowers_in(tulip, spring). flowers_in(carnation, summer). flowers_in(daffodil, spring). flowers_in(rose, summer). flowers_in(lily, summer).

?- flowers_in(Type, summer), write(Type), nl, fail.

7/6/2008

14

The Cut
Backtracking is a characteristic feature of Prolog But backtracking can lead to inefficiency:
Prolog can waste time exploring possibilities that lead nowhere It would be nice to have some control

The cut predicate !/0 offers a way to control backtracking


Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example of cut
The cut is a Prolog predicate, so we can add it to rules in the body:
Example:

p(X):- b(X), c(X), !, d(X), e(X).

Cut is a goal that always succeeds Commits Prolog to the choices that were made since the parent goal b(X), c(X), was called
Patrick Blackburn, Johan Bos & Kristina Striegnitz

Explaining the cut


In order to explain the cut, we will
Look at a piece of cut-free Prolog code and see what it does in terms of backtracking Add cuts to this Prolog code Examine the same piece of code with added cuts and look how the cuts affect backtracking

Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut-free code


p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3 ?- p(X).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut-free code


p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3 ?- p(X). ?- p(X).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut-free code


p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3 ?- p(X). ?- p(X).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut-free code


p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3 ?- p(X). ?- p(X). ?- a(X).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut-free code


p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3 ?- p(X). X=1 ?- p(X). ?- a(X).
X=1

Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut-free code


p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3 ?- p(X). X=1; ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X).
X=1

Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut-free code


p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3 ?- p(X). X=1; ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X).
X=1

Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut-free code


p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3 ?- p(X). X=1; ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X).
X=1 X=1

?- c(1),d(1),e(1).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut-free code


p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3 ?- p(X). X=1; ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X).
X=1 X=1

?- c(1),d(1),e(1).

?- d(1), e(1).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut-free code


p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3 ?- p(X). X=1; ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X).
X=1 X=1

?- c(1),d(1),e(1).

?- d(1), e(1).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3).

Example: cut-free code


?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X).
X=1 X=1 X=2

?- c(1),d(1),e(1).

?- p(X). X=1;

?- d(1), e(1).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3). ?- p(X). X=1;

Example: cut-free code


?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X).
X=1 X=1 X=2

?- c(1),d(1),e(1).

?- c(2),d(2),e(2).

?- d(1), e(1).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3). ?- p(X). X=1;

Example: cut-free code


?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X).
X=1 X=1 X=2

?- c(1),d(1),e(1).

?- c(2),d(2),e(2).

?- d(1), e(1).

?- d(2), e(2).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3). ?- p(X). X=1;

Example: cut-free code


?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X).
X=1 X=1 X=2

?- c(1),d(1),e(1).

?- c(2),d(2),e(2).

?- d(1), e(1).

?- d(2), e(2). ?- e(2).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut-free code p(X):- a(X).


p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3). ?- p(X). X=1; X=2
Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X).


X=1 X=1 X=2

?- c(1),d(1),e(1).

?- c(2),d(2),e(2).

?- d(1), e(1).

?- d(2), e(2). ?- e(2).

p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3). ?- p(X). X=1; X=2;
Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut-free code


?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X). ?- f(X).
X=1 X=1 X=2

?- c(1),d(1),e(1).

?- c(2),d(2),e(2).

?- d(1), e(1).

?- d(2), e(2). ?- e(2).

p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3). ?- p(X). X=1; X=2; X=3
Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut-free code


?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X). ?- f(X).
X=1 X=1 X=2 X=3

?- c(1),d(1),e(1).

?- c(2),d(2),e(2).

?- d(1), e(1).

?- d(2), e(2). ?- e(2).

Example: cut-free code p(X):- a(X).


p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3). ?- p(X). X=1; X=2; X=3; no
Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X). ?- f(X).


X=1 X=1 X=2 X=3

?- c(1),d(1),e(1).

?- c(2),d(2),e(2).

?- d(1), e(1).

?- d(2), e(2). ?- e(2).

Adding a cut
Suppose we insert a cut in the second clause:
p(X):- b(X), c(X), !, d(X), e(X).

If we now pose the same query we will get the following response:
?- p(X). X=1; no
Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut
p(X):- a(X). p(X):- b(X), c(X), !d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3). ?- p(X).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

p(X):- a(X). p(X):- b(X), c(X), !d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3). ?- p(X).

Example: cut
?- p(X).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut
p(X):- a(X). p(X):- b(X), c(X), !d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3).

?- p(X).

?- p(X).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

p(X):- a(X). p(X):- b(X), c(X), !d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3).

Example: cut
?- p(X). ?- a(X).
X=1

?- p(X). X=1

Patrick Blackburn, Johan Bos & Kristina Striegnitz

p(X):- a(X). p(X):- b(X), c(X), !d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3).

Example: cut
?- p(X). ?- a(X). ?- b(X),c(X),!,d(X),e(X).
X=1

?- p(X). X=1;

Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut
p(X):- a(X). p(X):- b(X), c(X), !d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3).

?- p(X). ?- a(X). ?- b(X),c(X),!,d(X),e(X).


X=1

?- p(X). X=1;

Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut
p(X):- a(X). p(X):- b(X), c(X), !d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3).

?- p(X). ?- a(X). ?- b(X),c(X),!,d(X),e(X).


X=1 X=1

?- c(1),!,d(1),e(1).

?- p(X). X=1;

Patrick Blackburn, Johan Bos & Kristina Striegnitz

p(X):- a(X). p(X):- b(X), c(X), !d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3).

Example: cut
?- p(X). ?- a(X). ?- b(X),c(X),!,d(X),e(X).
X=1 X=1

?- c(1), !, d(1), e(1). ?- !, d(1), e(1).

?- p(X). X=1;

Patrick Blackburn, Johan Bos & Kristina Striegnitz

p(X):- a(X). p(X):- b(X), c(X), !d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3).

Example: cut
?- p(X).

X
?- a(X). ?- b(X),c(X),!,d(X),e(X).
X=1 X=1

?- c(1), !, d(1), e(1). ?- !, d(1), e(1). ?- d(1), e(1).

?- p(X). X=1;

Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example: cut
p(X):- a(X). p(X):- b(X), c(X), !d(X), e(X). p(X):- f(X). a(1). b(1). c(1). b(2). c(2). d(2). e(2). f(3).

?- p(X). ?- a(X). ?- b(X),c(X),!,d(X),e(X).


X=1 X=1

?- c(1), !, d(1), e(1). ?- !, d(1), e(1). ?- d(1), e(1).

?- p(X). X=1; no
Patrick Blackburn, Johan Bos & Kristina Striegnitz

Another build-in predicate: fail/0


As the name suggests, this is a goal that will immediately fail when Prolog tries to proof it That may not sound too useful But remember: when Prolog fails, it tries to backtrack

Vincent and burgers


enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X).

bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

The cut fail combination allows to code exceptions

Vincent and burgers


enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X).

bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

The cut fail combination allows to code exceptions

?- enjoys(vincent,a). yes

Vincent and burgers


enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X).

bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

The cut fail combination allows to code exceptions

?- enjoys(vincent,b). no

Vincent and burgers


enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X).

bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

The cut fail combination allows to code exceptions

?- enjoys(vincent,c). yes

Vincent and burgers


enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X).

bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d).

Patrick Blackburn, Johan Bos & Kristina Striegnitz

The cut fail combination allows to code exceptions

?- enjoys(vincent,d). yes

not If X is a goal, then not(X) succeeds if X fails, and fails if X succeeds.


bachelor(Person):- not(married(Person)).

7/6/2008

54

OPERATORS

7/6/2008

55

OPERATORS
Operators allows you to write down the predicate name and arguments in a different form Operator syntax allows writing predicates without brackets, without commas, and with flexible positioning of the predicate name arshad drinks tea the predicate name drinks placed between its two argument Operator syntax is syntactical sugar
7/6/2008 56

OPERATORS
An expression is a predicate written in operator syntax 4 + 2000 is another way of writing +(4, 2000). In the standard syntax the predicate names + and * have to precede the arguments, while in the operator syntax, they sit between their arguments. In addition to the build-in operators you can declare your own operators.

7/6/2008

57

OPERATORS
The declaration of an operator includes declaring three properties
type precedence associativity

There are three types of operators


infix prefix post fix

7/6/2008

58

OPERATORS
Infix operator has two arguments 5 * 42 arshad drinks tea Prefix operator has only one argument rich sara even 474
Postfix operators also has one argument 120 volt 421 ah
7/6/2008 59

OPERATORS
Precedence
PROLOG uses the precedence number (of an operator) to determine where the brackets go 8+6/2 8+(6/2) which is +(8,/(6,2))
The greater the precedence of an operator, the lower the PROLOG precedence number Valid precedence numbers should be within the range 1 to 999
7/6/2008 60

OPERATORS
drinks 150 and 100 arshad drinks tea and water
drinks(arshad, and(tea, water))

?- arshad drinks Liquids. Liquids = tea and water

7/6/2008

61

OPERATORS
Associativity
heat_rendering and high_temp and rising_temp In these cases precedence alone is insufficient to determine the bracketing Where two operators with equal precedence numbers adjacent, the associativities of the operators determine which of the operators is an argument to the other operator

7/6/2008

62

OPERATORS
Infix:
left, right, non-associative

Prefix:
associative or non-associative prefix

Postfix:
associative or non-associative postfix

weld(Joint) or glue(Joint) or bolt(Joint)

7/6/2008

63

OPERATORS
Op(Precedence, Type_&_associativity, Atom) op(150, xfy, drinks). The f in the symbols represents the operator and its position relative to the argument(s), which are represented by either an x or a y An x symbolizes an argument that may only contain operators of lower precedence A y symbolizes an argument that may contain operators of lower or equal precedence
7/6/2008 64

OPERATORS
Infix
left associative right associative non associative yfx xfy xfx fy fx yf xf

prefix
associative prefix non associative

postfix
associative postfix non associative

7/6/2008

65

OPERATORS
currency(holland, guilder). currency(usa, us_dollar). currency(uk, pound). :- op(200, xfx, exports). usa exports electronics. germany exports cars. uk exports antiques. holland exports tulips.
7/6/2008 66

OPERATORS

7/6/2008

67

OPERATORS

7/6/2008

68

Exercise
Write an operator declaration for an operator is_part_of that make the following procedure valid syntax. sadar is_part_of pindi. pindi is_part_of punjab. punjab is_part_of pakistan. pakistan is_part_of asia. Tree for xfy and yfx ?
7/6/2008 69

if-then-else
Condition -> Goal1 ; Goal2 abs(X, Abs) :X >= 0 -> Abs is X ; Abs is X.

7/6/2008

70

integer(X)
True if X is an integer.

real(X)
True if X is a real number.

var(X)
True if X is unbound variable.

nonvar(X)
True if X is bound to a constant, a compound term, or a list.

7/6/2008

71

tab(X)
Writes X blank spaces onto the screen.

put(X)
Writes a single character corresponding to the ASCII number in the argument X onto the screen.

7/6/2008

72

bagof(X, G, L)
returns a list L of Xs for which the goal G succeed. It includes duplicates

Example
qualify(pakistan). qualify(england). qualify(korea).

bagof(Country, qualify(Country), L).

L = [pakistan, england, korea]

7/6/2008

73

Database Manipulation
Prolog has five basic database manipulation commands:
assert/1 asserta/1 assertz/1 retract/1 retractall/1
Patrick Blackburn, Johan Bos & Kristina

Database Manipulation
Prolog has five basic database manipulation commands:
assert/1 asserta/1 assertz/1 retract/1 retractall/1
Patrick Blackburn, Johan Bos & Kristina

Adding information

Removing information

Start with an empty database

Patrick Blackburn, Johan Bos & Kristina

Start with an empty database


?- listing. yes

Patrick Blackburn, Johan Bos & Kristina

Using assert/1
?- assert(happy(mia)). yes

Patrick Blackburn, Johan Bos & Kristina

Using assert/1
happy(mia).
?- assert(happy(mia)). yes ?-

Patrick Blackburn, Johan Bos & Kristina

Using assert/1
happy(mia).
?- assert(happy(mia)). yes ?- listing. happy(mia). ?-

Patrick Blackburn, Johan Bos & Kristina

Using assert/1
happy(mia).
?- assert(happy(mia)). yes ?- listing. happy(mia). ?- assert(happy(vincent)), assert(happy(marsellus)), assert(happy(butch)), assert(happy(vincent)).

Patrick Blackburn, Johan Bos & Kristina

Using assert/1
happy(mia). happy(vincent). happy(marsellus). happy(butch). happy(vincent).
?- assert(happy(mia)). yes ?- listing. happy(mia). ?- assert(happy(vincent)), assert(happy(marsellus)), assert(happy(butch)), assert(happy(vincent)). yes ?-

Patrick Blackburn, Johan Bos & Kristina

Changing meaning of predicates


The database manipulations have changed the meaning of the predicate happy/1 More generally:
database manipulation commands give us the ability to change the meaning of predicates during runtime

Patrick Blackburn, Johan Bos & Kristina

Dynamic and Static Predicates


Predicates which meaning changing during runtime are called dynamic predicates
happy/1 is a dynamic predicate Some Prolog interpreters require a declaration of dynamic predicates

Ordinary predicates are sometimes referred to as static predicates


Patrick Blackburn, Johan Bos & Kristina

Asserting rules
happy(mia). happy(vincent). happy(marsellus). happy(butch). happy(vincent).
?- assert( (naive(X):- happy(X)).

Patrick Blackburn, Johan Bos & Kristina

Asserting rules
happy(mia). happy(vincent). happy(marsellus). happy(butch). happy(vincent). naive(A):- happy(A).
?- assert( (naive(X):- happy(X)). yes ?-

Patrick Blackburn, Johan Bos & Kristina

Removing information
Now we know how to add information to the Prolog database
We do this with the assert/1 predicate

How do we remove information?


We do this with the retract/1 predicate, this will remove one clause We can remove several clauses simultaneously with the retractall/1 predicate
Patrick Blackburn, Johan Bos & Kristina

Using retract/1
happy(mia). happy(vincent). happy(marsellus). happy(butch). happy(vincent). naive(A):- happy(A).
?- retract(happy(marsellus)).

Patrick Blackburn, Johan Bos & Kristina

Using retract/1
happy(mia). happy(vincent). happy(butch). happy(vincent). naive(A):- happy(A).
?- retract(happy(marsellus)). yes ?-

Patrick Blackburn, Johan Bos & Kristina

Using retract/1
happy(mia). happy(vincent). happy(butch). happy(vincent). naive(A):- happy(A).
?- retract(happy(marsellus)). yes ?- retract(happy(vincent)).

Patrick Blackburn, Johan Bos & Kristina

Using retract/1
happy(mia). happy(butch). happy(vincent).
?- retract(happy(marsellus)). yes ?- retract(happy(vincent)). yes

naive(A):- happy(A).

Patrick Blackburn, Johan Bos & Kristina

Using retract/1
happy(mia). happy(butch). happy(vincent).
?- retract(happy(X)).

naive(A):- happy(A).

Patrick Blackburn, Johan Bos & Kristina

Using retract/1
naive(A):- happy(A).
?- retract(happy(X)). X=mia; X=butch; X=vincent; no ?-

Patrick Blackburn, Johan Bos & Kristina

Using asserta/1 and assertz/1


If we want more control over where the asserted material is placed we can use the variants of assert/1:
asserta/1 places asserted matieral at the beginning of the database assertz/1 places asserted material at the end of the database
Patrick Blackburn, Johan Bos & Kristina

List in Prolog

LISTS
A shopping list eggs, tea, milk, bread In PROLOG we can write this as [eggs, tea, milk, bread] Each item in the list is called element The elements may also be variables or predicates

LISTS
A list without any element is called empty list and is written as [ ] A list can be split in to a head & a tail Head is the first element in a list Tail is a list containing the rest of the element(s).

LISTS
[eggs, tea, milk, bread] can be split into eggs as head [tea, milk, bread] as a tail The tail itself is a list this new list has tea as a head [milk, bread] as a tail

LISTS
The tail of a list is in itself a list A list containing just one element [bread] is in fact a list with a head - bread a tail - [ ] an empty list

LISTS
Two notations for lists:
The standard notation for lists enumerates all the elements, separated by commas, and enclosed in a square brackets The head-tail notation uses the vertical bar | as a separator between the head and the tail of the list

A list in the head-tail notation is written as [H | T]

LISTS
Head and tails can have arbitrary name just like any other variable Thus [eggs, tea, milk, bread] and [eggs | tea, milk, bread] represent the same list Lists can be used as arguments in queries, rules, and facts

LISTS
PROLOG uses different internal representation for lists Internally, it has no such thing as a list. It uses predicate with the predicate name full stop (.) to represent lists [a, b, c] is represented as .(a, .(b, .(c, [])))

Exercise
Which of the following pairs of list match?
[a, b, c, d, e] No [a, X] [a, b, c, d, e] Yes [a | X] [[a, b, c, d, e]] Yes [X, Y] [a, b, c, d, e] [[X] | [b, c, d, e]] No

Destruction and Construction


List Destruction
how to remove the first element from a list [X|Y] = [f,r,e,d] will result in X=f Y=[r,e,d]

List Construction
take a variable bound to any old list say, X=[r, e, d] and add the element, say, b at the front with Result_wanted = [b|X].

Destruction and Construction


it is possible to add (or take away) bigger chunks onto (from) the front of a list than one element at a time. To stick the elements a, b and c onto the front of the list X to make a new list Y.
Y=[a,b,c|X].

Conversely, to take three elements off the front of a list X in such a way that the remaining list, Y, is available for use.
X=[A,B,C|Y]

LISTS as arguments
origion(england, [austin, landrover, ford]). origion(usa, [buick, ford]). origion(germany, [audi, volkswagen, porsche, mercedes]). origion(japan, [toyota, nissan, suzuki, honda]). ? - origion(usa, brands). No

Member
One of the most basic things we would like to know is whether something is an element of a list or not So let`s write a predicate that when given a term X and a list L, tells us whether or not X belongs to L This predicate is usually called member/2

Patrick Blackburn, Johan Bos & Kristina Striegnitz

member/2
member(X,[X|T]). an object X
that list.
Patrick Blackburn, Johan Bos & Kristina Striegnitz

is a member of a list if it is the head of is member of a list

member(X,[H|T]):- member(X,T). an object X

if it is a member of the tail of the list.

?-

member/2
Member (X,[X|T]). Member (X, [H|T]):- member (X,T).
Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- member (yolanda, [yolanda, trudy,vincent,jules]).

member/2
member(X,[X|T]). member(X,[H|T]):- member(X,T).
Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- member(yolanda,[yolanda,trudy,vincent,jules]). yes ?-

member/2
member(X,[X|T]). member(X,[H|T]):- member(X,T).
Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- member(vincent,[yolanda,trudy,vincent,jules]).

member/2
member(X,[X|T]). member(X,[H|T]):- member(X,T).
Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- member(vincent,[yolanda,trudy,vincent,jules]). yes ?-

member/2
member(X,[X|T]). member(X,[H|T]):- member(X,T).
Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- member(zed,[yolanda,trudy,vincent,jules]).

member/2
member(X,[X|T]). member(X,[H|T]):- member(X,T).
Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- member(zed,[yolanda,trudy,vincent,jules]). no ?-

member/2
member(X,[X|T]). member(X,[H|T]):- member(X,T).
Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- member(X,[yolanda,trudy,vincent,jules]).

member/2
member(X,[X|T]). member(X,[H|T]):- member(X,T).
Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- member(X,[yolanda,trudy,vincent,jules]). X = yolanda; X = trudy; X = vincent; X = jules; no

Example
Given are the dispatch lists for the coming days of the week, and a list of lots that contain fragile goods.
dispatch(monday, [lot501, lot601, lot751]). dispatch(tuesday, [lot711, lot523]). fragile([lot601, lot711]).

All lots that contain fragile goods require special packaging before dispatch. Write a rule that specifies which lots require special packaging on a certain day.

Example
An object will require special packaging if it is a member of the dispatch list for that day. And if it is a member of the list of fragile lots. Special_pack(Day, Object):dispatch(Day, Dispatchlist), fragile(Fragilelist), member(Object, Dispatchlist), member(Object, Fragilelist).

Exercise - Timetable
Write relevant facts and rules for your lectures timetable. You should be able to find out what lectures you have on a particular day. Also, you should be able to ask on which days you have lecture of a subject.

APPENDING LISTS
When we append the list [rome, paris, madrid] to the list [tokyo, peking, hong_kong] the result is the new list [rome, paris, madrid, tokyo, peking, hong_kong] A Prolog query like ?- append([rome, paris, madrid], [tokyo, peking, hong_kong], X). Will give X= [rome, paris, madrid, tokyo, peking, hong_kong]

APPENDING LISTS
The base case? append([ ], List, List). The recursive rule? The result of appending List1 and List2 is Result, if List1 consists of a head H and a tail T; the result of appending T and List2 is Temp;and Result is the list with head H and tail Temp.

APPENDING LISTS
So we have base case append(List1, List2, Result):List1 = [ ], Result = List2. The recursive rule append(List1, List2, Result):List1 = [H | T], append(T, List2, Temp), Result = [H | Temp].

practice
?- append([a,b,c,d],[1,2,3,4,5], X). X=[a,b,c,d,1,2,3,4,5] yes Work out the append code given in the previous slides to figure out how the two lists are appended in the above example.
7/6/2008 124

Reversing a list can be done with reverse([X|Y],Z,W) :- reverse(Y,[X|Z],W). reverse([],X,X). This program illustrates Prolog's approach to an important strategy -- using an accumulating parameter (the middle variable) -- to accumulate a list answer until the computation is finished. For example, consider the following (partial) derivation tree ?-

?- reverse([1,2,3],[],A) reverse([2,3],[1],A) reverse([3],[2,1],A) reverse([],[3,2,1],A) true A = [3,2,1]

reverse([X|Y],Z,W) :reverse(Y,[X|Z],W). reverse([],X,X).

REVERSE the list


?- reverse([a,b,c], X). X = [c, b, a] L = [a,b,c] Head = a and Tail = [b,c] reverse [b,c] (which is T) gives [c, b] return [c, b] (let us say ReverseT) followed by a i.e., append a to ReveseT reverse of [ ] is [ ]

REVERSE of a list
% base case reverse([ ], [ ]). %Recursive case reverse([H | T], Ans) :reverse(T, RevT), append(RevT, [H], Ans).

Do think and practice.


Find the months that proceed and the months that follow a given month. append(Before, [may|After], [jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec] . Before = [jan,feb,mar,apr] After = [jun,jul,aug,sep,oct,nov,dec].

Closed world assumption


The Close World Assumption is a mechanism that allows us to draw negative conclusions based on the lack of positive information. Prolog assumes that everything that is stated in the program or that can be derived from it is true and everything else is false. The answer to the question: ?- not(clever(user)). yes although we (and the program) know nothing about the user.
7/6/2008 130

Quiz(MCS-3(M)
Write down the prolog Control-strategy and search strategy Write a function which can calculate the sum of numbers from 10 40. Append function

7/6/2008

131

Das könnte Ihnen auch gefallen