89
FATIH UNIVERSITY Department of Computer Engineering Prolog Lecture Notes 3rd Week 1. Logical expressions and prolog 2. Operators 3. Lists

Prolog Lecture Notes 3rd Week

  • Upload
    lyle

  • View
    32

  • Download
    1

Embed Size (px)

DESCRIPTION

Prolog Lecture Notes 3rd Week. Logical expressions and prolog Operators Lists. 1. smart(X):-teaches(X,computing). 2. teaches(john,math132). 3. wife(X,john),teaches(X,comp111). 4. mathematics(math132). 5. computing(comp111). 1. likes(X,Y):-mother(X,Y), good(Y). 2. woman(X):- mother(X). - PowerPoint PPT Presentation

Citation preview

Page 1: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Prolog Lecture Notes 3rd Week

1. Logical expressions and prolog 2. Operators3. Lists

Page 2: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

1. smart(X):-teaches(X,computing).

2. teaches(john,math132).

3. wife(X,john),teaches(X,comp111).4. mathematics(math132).

5. computing(comp111).

Page 3: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

1. likes(X,Y):-mother(X,Y), good(Y).

2. woman(X):- mother(X).

3. woman(ann).

4. husband(X,ann),good(X).

Page 4: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 5: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 6: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 7: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 8: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 9: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 10: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 11: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

A Database

• “A car manufacturer uses parts supplied by different companies around the world. Each part has a unique code number, a name, a size, and a price. Different quantities of each part may be ordered from different suppliers.”

Page 12: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

A Database

• The company needs access to this information to answer questions that arise when considering supplier policy. For Example:

• What types of parts are obtained from suppliers in London?

• Where are the suppliers of wheels based?

• How do we turn this into Prolog?

Page 13: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Program Design

• To design suitable data structures we can look for the relevant entities and relationships.

• What are the entities in this situation?• What are the relationships of interest?

Page 14: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Entities

• In this case: parts and suppliers• These have certain attributes

– Each part has: code, name, size, price– Each supplier has: name, address

• Possible Prolog terms:– part(Code, Name, Size, Price)– supplier(SName, Address)

Page 15: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Relationships

• In this case: who-supplies-what(Links supplier to part, with a quantity)

• Possible Prolog terms:– supplier_part(Code, SName, Quantity)– Improvements?

Page 16: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Program (Supplier)

% supplier(SNum, SName, City)supplier(10, leman, newyork).supplier(22, adams, london).supplier(46, fuji, tokyo).::Note: Whatever follows ‘%’ is a comment.

Page 17: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Program (Part)

% part(PNum, PName, Size, Price)part(k10, cam, 6, 20).part(m15, shaft, 14, 50).part(s21, wheel, 25, 132).::

Page 18: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Program (Relationship)

% supplier_part(SNum, PNum, Quantity)supplier_part(10, m15, 300).supplier_part(22, k10, 200).supplier_part(22, m15, 100).supplier_part(46, s21, 500).::

Page 19: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Forming a Query

• How can we find which types of parts are supplied from London?

• First add to the program an extra rule to describe the relationship between part names and cities of suppliers:part_city(PName, City) :-part(PNum, PName, _, _),supplier_part(SNum, PNum, _),supplier(SNum, _, City).

Page 20: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Forming a Query

• Now we can ask for solutions to a specific question by using a query.?- part_city(Part, london).Part = cam ;Part = shaft ;no

Page 21: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Alternativelysname(10, leman).sname(22, adams).sname(46, fuji).:scity(10, newyork).scity(22, london).scity(46, tokyo).:pname(k10, cam).pname(m15, shaft).pname(s21, wheel).:supplier_part(10, m15, 300).supplier_part(22, k10, 200).supplier_part(22, m15, 100).supplier_part(46, s21, 500).:

Page 22: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Forming a Query

• The rule to describe the relationship between part names and cities of suppliers is different in this design:part_city(PName, City) :-pname(PNum, PName),supplier_part(SNum, PNum, _),scity(SNum, City).

Page 23: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Forming a Query

• But the queries can be identical:?- part_city(Part, london).

• (This is ‘data abstraction’ in action)• With this new design it is easy to add

extra attributes to some records:sstatus(46, unreliable).

Page 24: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Exercise

• The secretary of a lonely-hearts club wishes to store the following information about clubmembers:Name: Henry LawSex: MaleHeight: 1.78mWeight: 75kgHobbies: jogging, skiing, singing

Page 25: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Exercise (cont.)

• She also wants to be able to answer questions such as:1. How heavy is Mary Jones?2. Who is interested in campanology?

• Design a Prolog database to store this information and answer these questions.

Page 26: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Logic Puzzles

“Three friends of different nationalities, who play

different sports, took the ACSC300 course. Michael plays basketball and did better than the American. Simon, the Israeli, did better than the tennis player.The cricket player did best of all.”1. Who is the Australian?2. What sport does Sarah play?

How do we turn this into Prolog?

Page 27: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Program Design

• To design suitable data structures we can look for the relevant entities and relationships (as in the database design).

• What are the entities in this situation?• What are the relationships of interest?

Page 28: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Entities

• In this case: people– Each with three attributes

• name• nationality• sport

• Prolog term:person(Name, Nation, Sport)

Page 29: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Relationships

• In this case: order• Could be described by:

– adding a score, or ranking attribute– building a sequence, or list

• Prolog term:sequence(P1, P2, P3)

Page 30: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Program

possible_sequence(S) :-did_better(S,person(michael, _, basketball),person(_, america, _)),did_better(S,person(simon, israel, _),person(_, _, tennis)),did_best(S,person(_, _, cricket)).

did_better(sequence(P1, P2, _), P1, P2).did_better(sequence(P1, _, P2), P1, P2).did_better(sequence(_, P1, P2), P1, P2).did_best(sequence(P, _, _), P).

Page 31: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Forming a Query

• First we add rules to describe solutions:solution(Name, Nation, Sport) :-possible_sequence(S),contains(S, person(Name, Nation,

Sport)).contains(sequence(P, _, _), P).contains(sequence(_, P, _), P).contains(sequence(_, _, P), P).

Page 32: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Forming a Query

• Now we can ask for solutions to specific questions:?- solution(Name, australia, _).?- solution(sarah, _, Sport).

• In this way we can use the solution rule to answer any question.

Page 33: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 34: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Operator notation (1)• Since Prolog uses functors for mathematical expressions, an expression like

2×a + b×ccan be represented in Prolog by

+( *(2,a), *(b,c))However, Prolog allows the infix notation for some operators, allowing the expression to be written:

2*a + b*cThe * operator of course binds “”stronger” than the +, which is defined by Prolog’s precedence: the principal functor has the highest precedence. So

a + b * cmeans in Prolog: +(a,*(b,c)), and not

*( +(a,b), c)In the latter case, use parentheses: (a + b) * c

Page 35: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Operator notation (2)

Precedence: number between 0 and 1200 (in Amzi! Prolog), with as rule

• Principal functor has highest precedence

For example: + precedence 500, - precedence 400. Then:a + b * c

highest precedence +, so + is the principal functor, so+(a, *(b,c))

is the Prolog interpretation.

• The precedence of a structure is the precedence of the principal functor of the structure;

• The precedence of a non-structured term is 0.

Page 36: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

bw 0 and 1200

Page 37: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 38: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 39: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Operator notation (3)

• Associativity is one of the following atoms:xfx yfx fx xfxfy yfy fy yf

in which x and y are arguments, and f is the operator. So

?f? indicates an infix operator,?f indicates a postfix operator, and f? indicates a prefix operator.

Further:x: precedence of argument < precedence of f;y: precedence of argument ≤ precedence of f.

Page 40: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Operator notation (4)Defining an operator using op/3. E.g.:

?- op(500, yfx, -).This defines the – operator as an infix operator in such a way that

a – b – cmeans

(a – b) – cand not

a – (b – c)Check:

-

-

a b

c a-

-

a b

precedence 500 precedence 500

precedence 0 precedence 0

precedence 500 precedence 500

OK Not OK

Page 41: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Operator notation (5)An operator may be defined once as an

infix and once as either prefix or postfix, so?- op(500, xfy, +) % + is an infix

operator?- op(700, fx, +) % + is now an

infix and a prefix operator?- op(700, xf, +) % + is now an

infix and a postfix operator

Page 42: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Operator notation (6)• Some Predefined Prolog operators:

:- (op(1200, xfx, [:-, -->])). :- (op(1200, fx, [?-, :-])). :- (op(1100, xfy, ';')). :- (op(1050, xfy, ->)). :- (op(1000, xfy, ',')). :- (op(900, fy, [not])). :- (op(700, xfx, [=, \=, is, =.., ==, \==, =:=, ~=, =\=, <, >, =<, >=])). :- (op(600, xfy, :)). :- (op(500, yfx, [+, -, /\, \/, xor])). :- (op(400, yfx, [/, //, *, >>, <<])). :- (op(200, xfx, **)). :- (op(200, xfy, ^)). :- (op(200, fy, [+, -, \])).

Page 43: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Operator notation (7)• Sometimes it is useful to define your own operators. For

instance, de Morgan’s theorem ~(A & B) ~A | ~B could be defined in Prolog as: equiv( not( and(A,B), or( not(A), not(B))).

Better is to introduce the following operators::- op(800, xfx, <===>).

:- op(700, xfy, v).

:- op(600, xfy, &).

:- op(500, fy, ~).

Now de Morgan’s theorem can be written as:~(A & B) <==> ~A v ~B.

Page 44: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Operator notation (8)

Some remarks on operators:• Special operator notation can be useful, especially when

the infix, prefix and postfix notation are used in a “natural” way.

• Operators are just functors! So they are not necessarily connected to actions. However, for predefined actions (e.g., arithmetic) see the next slides.

• Besides predefined operators it can be useful to define your own operators, specifying the precedence, associativity and the name of the operator. Moreover, you have to define their meaning!

Page 45: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 46: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 47: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Operator example

• a-b-c how do we interpret this?(a-b)-c OR a-(b-c)

• Normally the first• Since – is defined as yfx• not not pnot(not p)

– is legal if not is defined as fy– not legal if it is fx

• See Figure 3.8 for more predefined operators.

Page 48: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 49: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Operations on Lists

• Lists vs Sets: analoguous except– The order is important in lists– The element repetition is allowed in

lists• Common operations on lists are similiar to

set operations– Membership check– Concatenation– Adding/deleting elements to/from

Page 50: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 51: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Representation of lists[coffee, tea, water, beer, juice]

• Internal representation as a binary tree with as functor ‘.’:.(Head, Tail)

in which Head is a Prolog object (atom, number, variable, structure, possibly a list) and Tail a list.?- display([coffee, tea, water, beer, juice])..(coffee,.(tea,.(water,.(beer,.(juice,[])))))yes

The empty list is indicated by [].

• Second (cosmetic) notation:[Head | Tail]

in which Head is/are one or more elements and Tail a list.

Page 52: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 53: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 54: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Representation of lists

• Alternative notations for previous list:[coffee,tea,water,beer,juice]

[coffee | [tea,water,beer,juice]]

[coffee, tea | [water,beer,juice]]

[coffee, tea, water | [beer,juice]]

[coffee, tea, water, beer | [juice]]

but also for example:[coffee | [tea,water | [beer,juice]]]

Page 55: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

List operations• Prolog hardly has primitive (pre-defined)

functions. However, we can define them themselves and save them as a kind of library. The most important list operations:– Checking for membership– Concatenation of two lists– Adding an object to a list– Deleting an object from a list– Determining sublists of a list– Obtaining permutations of a list

• These are defined in the following sheets.

Page 56: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 57: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 58: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Membership

• X is a member of a list L if either– X is the head of the list– Or X is a member of the tail of L

Page 59: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 60: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 61: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

List operations• Checking list membership

?- member(b, [a,b,c]).yes?- member(b, [a, [b,c]]).no?- member([b,c], [a, [b,c]]).yes

Recursive definition of member/2:member(X, [X | Tail]).member(X, [Head | Tail]) :-member(X, Tail).

Page 62: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 63: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

List Concatenation

• If the first argument is the empty list then the second and the third arguments must be the same lists

• If the first argument is a non-empty list then the head of the first argument will be the head of the third list and the tail of the first list will be concatenated to the second list

Page 64: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 65: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 66: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Page 67: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

List operations• Concatenation of two lists

?- conc([a,b], [c,d], [a,b,c,d]).yes?- conc([a,b], [c,d], [a,b,a,c,d]).no

• Recursive definition of conc/3:conc([], L, L).conc([X | L1], L2, [X | L3]) :-

conc(L1, L2, L3).• Some applications of conc/3:

?- conc([a,b,c], [1,2,3], L).L = [a,b,c,1,2,3]?- conc([a,[b,c],d], [a,[],b], L).L = [a,[b,c],d,a,[],b]

Page 68: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

List operations• “Inverse” applications = decomposing a list:

?- conc(L1, L2, [a,b,c]).

L1 = []

L2 = [a,b,c];

L1 = [a]

L2 = [b,c];

L1 = [a,b]

L2 = [c];

L1 = [a,b,c]

L2 = [];

no

Page 69: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

• Write member relation by using concmember(X,L):-conc(L1,[X|L2]).

member(X,L):-conc(_,[X|_]).

Membership relation with concatenation

Page 70: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

List operations• Searching for patterns in lists:

?- conc(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]?- conc(_, [Pred,may,Succ | _], [jan,feb,mar,

apr,may,jun,jul,aug,sep,oct,nov,dec]).Pred = aprSucc = jun

Page 71: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

• We want to insert an item in a list• Decide where to insert the • Easiets way is inserting as the first

element• Insert X to list L

– Resulting list L’=[X|L]– add(X,L, [X|L]).

• What about inserting to a specific position?

Adding an item

Page 72: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

• We want to delete an item from a list• If X is the head of the list then te result after

the deletion is the tail of the lsit• If X is in tha tail then delete it from the tail.

HOW? • What about deleting from a specific position?

Deleting an item

Page 73: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

List operations• Adding an object to a list with add/3:

add(X, L, [X | L]).• Deleting an object from a list with del/3:

del(X, [X | Tail], Tail).del(X, [Y | Tail], [Y | Tail1]) :-del(X, Tail, Tail1).

• Example: del(a, [a,b,a,a], L).L = [b,a,a];L = [a,b,a];L = [a,b,a];no

Page 74: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

List operations• Inverse:

del(a, L, [1,2,3]).L = [a,1,2,3];L = [1,a,2,3];L = [1,2,a,3];L = [1,2,3,a];no

• More general than add/3 is insert/3:insert(X, List, Biggerlist) :-del(X, Biggerlist, List).

• Another alternative definition of member/2:member2(X, L) :-del(X, L, _).

Page 75: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

List operations• Determining sublists of a list

?- sublist([c,d,e], [a,b,c,d,e,f]).

yes

?- sublist([c,e], [a,b,c,d,e,f]).

no• Definition of sublist/2:

sublist(S, L) :-

conc(L1, L2, L),

conc(S, L3, L2).

Page 76: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

List operations• Application:

?- sublist(S, [a,b,c]).

S = [];

S = [a];

S = [a,b];

S = [a,b,c];

S = [];

S = [b];

S = [b,c];

S = [];

S = [c];

S = [];

no

Page 77: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

List operations• Obtaining permutations of a list:

?- permutation([1,2,3], [2,1,3]).

yes

?- permutation([1,2,3], [1,2,4]).

no• Recursive definition of permutation/2:

permutation([], []).

permutation([X | L], P) :-

permutation(L, L1),

insert(X, L1, P).

Page 78: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

List operations• Obtaining all permutations of a list:

?- permutation([1,2,3], L).L = [1,2,3];L = [2,1,3];L = [2,3,1];L = [1,3,2];L = [3,1,2];L = [3,2,1];no

• Alternative definition: permutation2([], []).permutation2(L, [X | P]) :-del(X, L, L1),permutation2(L1, P).

Page 79: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

List operations• All together in a library for lists:

% membership functionmember( X, [X | Tail] ).member( X, [Head | Tail] ) :- member( X, Tail).

% concatenation of listsconc( [], L, L).conc( [X | L1], L2, [X | L3]) :- conc( L1, L2, L3).

% membership using concatenationmember1( X, L) :- conc( _, [X | _], L).

% adding an itemadd( X, L, [X | L] ).

Page 80: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

List operations% deleting an itemdel( X, [X | Tail], Tail).del( X, [Y | Tail], [Y | Tail1]) :- del( X, Tail, Tail1).% inserting an iteminsert( X, List, BiggerList) :- del( X, BiggerList, List).% sublistssublist( S, L) :- conc( L1, L2, L), conc( S, L3, L2).% permutationspermutation( [], []).permutation( [X | L], P) :- permutation( L, L1), insert( X, L1, P).

Page 81: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

Length of a List• Length of a list: length/2. Two cases:

1. If the list is empty, then its length is 0;2. If the list is not empty then List = [Head |

Tail]; then its length is equal to 1 plus the length of its tail Tail.

length( [], 0).

length( [_ | Tail], N) :- length( Tail, N1), N is 1 + N1.

In this clause the latter two goals may not be switched, since N1 has to be instantiated before ‘is’ evaluates its right-hand side. So the operator ‘is’ introduces order-sensitivity!

Page 82: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

The automaton example• Simulation of a non-deterministic

automaton

s1s2

s3s4

a

b

b

a

b

null

null

Page 83: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

The automaton example• An automaton is an abstract machine, accepting

or rejecting an input string. It is always in some state si.

• An arc denotes a transition, if the corresponding symbol is read. The transition may be non-deterministic.

• There can also be “silent” moves, i.e., transitions without reading (denoted null).

• The example machine has s3 as final state.• A string is accepted if there is a path from the

start state to a final state reading the input, otherwise the string is rejected.

• The example machine accepts all strings terminating with ‘ab’.

Page 84: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

The automaton example• An automaton in Prolog:

– A relation final/1 for the terminal state– A relation trans/3 for state transitions:

trans(S1, X, S2)

denotes a transition from state s1 to s2 when reading symbol X

– A relation silent/2 for silent transitions:

silent(S1, S2)

if a silent transition from state s1 to s2 is possible

Page 85: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

The automaton example• The automaton from the example: final( s3).

trans( s1, a, s1).

trans( s1, a, s2).

trans( s1, b, s1).

trans( s2, b, s3).

trans( s3, b, s4).

silent( s2, s4).

silent( s3, s1).

Page 86: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

The automaton example• A string is represented by a list, e.g., aab =

[a,a,b]• Acceptance of a string:

accepts(State, String)• Three cases:

1. The empty string [] is accepted if State is final;2. Otherwise a string is accepted if the first symbol

can cause a transition to a state, from which the remainder of the string is accepted; or

3. A string is accepted if the automaton by a silent transition can reach a state, from which the string is accepted.

Page 87: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

The automaton exampleIn Prolog:

accepts( State, []) :- final( State).accepts( State, [X | Rest]) :- trans( State, X, State1), accepts( State1, Rest).accepts( State, String) :- silent( State, State1), accepts( State1, String).

• Examples?- accepts(s1, [a,a,a,b]).yes?- accepts(s1, [a,a,a,b,a]).no

Page 88: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

The automaton example• But also:

?- accepts(S, [a,b]).S = s1;S = s3;no

or even?- accepts(s1, [X1,X2,X3]).X1 = aX2 = aX3 = b;X1 = bX2 = aX3 = b;no

Page 89: Prolog Lecture Notes 3rd Week

FATIH UNIVERSITY FATIH UNIVERSITY Department of Computer EngineeringDepartment of Computer Engineering

The automaton examplefinal( s3).trans( s1, a, s1).trans( s1, a, s2).trans( s1, b, s1).trans( s2, b, s3).trans( s3, b, s4).silent( s2, s4).silent( s3, s1).accepts( State, []) :- % Accept empty string final( State).accepts( State, [X | Rest]) :- % Accept by reading first

symbol trans( State, X, State1), accepts( State1, Rest).accepts( State, String) :- % Accept by making silent

move silent( State, State1), accepts( State1, String).