Beruflich Dokumente
Kultur Dokumente
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
7/6/2008
7/6/2008
10
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).
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
Example of cut
The cut is a Prolog predicate, so we can add it to rules in the body:
Example:
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
?- c(1),d(1),e(1).
?- c(1),d(1),e(1).
?- d(1), e(1).
?- c(1),d(1),e(1).
?- d(1), e(1).
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).
?- c(1),d(1),e(1).
?- p(X). X=1;
?- d(1), e(1).
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;
?- c(1),d(1),e(1).
?- c(2),d(2),e(2).
?- d(1), e(1).
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;
?- c(1),d(1),e(1).
?- c(2),d(2),e(2).
?- d(1), e(1).
?- d(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;
?- c(1),d(1),e(1).
?- c(2),d(2),e(2).
?- d(1), e(1).
?- c(1),d(1),e(1).
?- c(2),d(2),e(2).
?- d(1), e(1).
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
?- c(1),d(1),e(1).
?- c(2),d(2),e(2).
?- d(1), e(1).
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
?- c(1),d(1),e(1).
?- c(2),d(2),e(2).
?- d(1), e(1).
?- c(1),d(1),e(1).
?- c(2),d(2),e(2).
?- d(1), e(1).
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).
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).
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).
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
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;
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). X=1;
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).
?- c(1),!,d(1),e(1).
?- p(X). X=1;
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
?- p(X). X=1;
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
?- p(X). X=1;
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). X=1; no
Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- enjoys(vincent,a). yes
?- enjoys(vincent,b). no
?- enjoys(vincent,c). yes
?- enjoys(vincent,d). yes
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
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))
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
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).
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
Using assert/1
?- assert(happy(mia)). yes
Using assert/1
happy(mia).
?- assert(happy(mia)). yes ?-
Using assert/1
happy(mia).
?- assert(happy(mia)). yes ?- listing. happy(mia). ?-
Using assert/1
happy(mia).
?- assert(happy(mia)). yes ?- listing. happy(mia). ?- assert(happy(vincent)), assert(happy(marsellus)), assert(happy(butch)), assert(happy(vincent)).
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 ?-
Asserting rules
happy(mia). happy(vincent). happy(marsellus). happy(butch). happy(vincent).
?- assert( (naive(X):- happy(X)).
Asserting rules
happy(mia). happy(vincent). happy(marsellus). happy(butch). happy(vincent). naive(A):- happy(A).
?- assert( (naive(X):- happy(X)). yes ?-
Removing information
Now we know how to add information to the Prolog database
We do this with the assert/1 predicate
Using retract/1
happy(mia). happy(vincent). happy(marsellus). happy(butch). happy(vincent). naive(A):- happy(A).
?- retract(happy(marsellus)).
Using retract/1
happy(mia). happy(vincent). happy(butch). happy(vincent). naive(A):- happy(A).
?- retract(happy(marsellus)). yes ?-
Using retract/1
happy(mia). happy(vincent). happy(butch). happy(vincent). naive(A):- happy(A).
?- retract(happy(marsellus)). yes ?- retract(happy(vincent)).
Using retract/1
happy(mia). happy(butch). happy(vincent).
?- retract(happy(marsellus)). yes ?- retract(happy(vincent)). yes
naive(A):- happy(A).
Using retract/1
happy(mia). happy(butch). happy(vincent).
?- retract(happy(X)).
naive(A):- happy(A).
Using retract/1
naive(A):- happy(A).
?- retract(happy(X)). X=mia; X=butch; X=vincent; no ?-
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
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
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].
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
member/2
member(X,[X|T]). an object X
that list.
Patrick Blackburn, Johan Bos & Kristina Striegnitz
?-
member/2
Member (X,[X|T]). Member (X, [H|T]):- member (X,T).
Patrick Blackburn, Johan Bos & Kristina Striegnitz
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
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 of a list
% base case reverse([ ], [ ]). %Recursive case reverse([H | T], Ans) :reverse(T, RevT), append(RevT, [H], Ans).
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