P ROLOG. Lists in P ROLOG. Operations and Predicates. Lists as Sequences, Sets, Bags. Meta Predicates.
Antoni Lig za e
Katedra Automatyki, AGH w Krakowie
2011
Antoni Lig za e
Prolog
1/17
References
[1] Ulf Nilsson, Jan Maluszy ski: Logic, Programming and Prolog, John Wiley & n Sons Ltd., pdf, http://www.ida.liu.se/ ulfni/lpp [2] Dennis Merritt: Adventure in Prolog, Amzi, 2004 http://www.amzi.com/AdventureInProlog [3] Quick Prolog: http://www.dai.ed.ac.uk/groups/ssp/bookpages/quickprolog/quickprolog.html [4] W. F. Clocksin, C. S. Mellish: Prolog. Programowanie. Helion, 2003 [5] SWI-Prologs home: http://www.swi-prolog.org [6] Learn Prolog Now!: http://www.learnprolognow.org [7] http://home.agh.edu.pl/ ligeza/wiki/prolog [8] http://www.im.pwr.wroc.pl/ przemko/prolog
Antoni Lig za e
Prolog
2/17
Introduction to Lists in Prolog
Lists - basic concepts Lists are one of the most important structures in symbolic languages. In most of the implementations of P ROLOG lists are standard, build-in structures and there are numerous operations on them provided as routine predicates. Lists can be used to represent
1 2 3 4
sets, sequences, multi-sets (bags), and more complex structures, such as trees, records, nested lists, etc.
Lists - basic notation A list in P ROLOG is a structure of the form [t1 , t2 , . . . , tn ] The order of elements of a list is important; the direct access is only to the rst element called the Head, while the rest forms the list called the Tail. [Head|Tail] where Head is a single element, while Tail is a list.
Antoni Lig za e Prolog 3/17
Denition of Lists. Lists as Terms
Lists as Terms Lists in fact are also terms. A list: [t1 , t2 , . . . , tn ] is equivalent to a term dened as follows: l(t1 , l(t2 , . . . l(tn , nil) . . .)) l/2 is the list constructor symbol and nil is symbolic denotation of empty list. Lists: Head and Tail In practical programming it is convenient to use the bracket notation. In order to distinguish the head and the tail of a list the following notation is used [H|T]. An example of list matching
1 2
[H|T] = [a,b,c,d,e] H=a, T = [b,c,d,e]
Antoni Lig za e
Prolog
4/17
Some Notes on lists. Unication Variants
List properties A list can have as many elements as necessary. A list can be empty; an empty list is denoted as [ ]. A list can have arguments being of:
1 2 3
mixed types, complex structures, i.e. terms, lists, etc., and as a consequence a list can have nested lists (to an arbitrary depth)
a list of k elements can be matched directly against these elements, i.e.
1 2
[X,Y,Z,U,V] = [a,b,c,d,e] X=a, Y=b, Z=c, U=d, V=e
rst k elements of any list can be matched directly
1 2
[X,Y,Z|T] = [a,b,c,d,e] X=a, Y=b, Z=c, T=[d,e]
Single-element list A single-element list is different from its content-element! foo = [foo]
Antoni Lig za e Prolog 5/17
First k elements. The n-th element. Propagation of Substitutions
First k-elements: k = 1, 2, 3
1 2 3 4 5 6 7 8
[X|_] = [a,b,c,d,e]. X=a [_,X|_] = [a,b,c,d,e]. X=b [_,_,X|_] = [a,b,c,d,e]. X=c
Take the n-th element
1 2
take(1,[H|_],H):- !. take(N,[_|T],X):- N1 is N-1, take(N1,T,X).
Propagation of substitutions
1 2 3
[X,Y,Z,U] = [a,b,c,d] ? [X,Y,Z,X] = [a,b,c,d] ? [X,Y,Y,X] = [a,U,Q,U] ?
Antoni Lig za e Prolog 6/17
Applications of Lists: Examples
List understanding: three basic possibilities as sequences, as sets, as sets with repeated elements, When thinking of lists as sets, the order of elements is (read: must be made) unimportant. Lists as sets
1 2 3
[a,b,c,d,e] [1,2,3,4,5,6,7,8,9] [1,a,2,b,f(a),g(b,c)]
Lists as multi-sets (bags, collections) or sequences
1 2 3
[a,b,c,d,e,a,c,e] [1,1,2,3,4,5,6,7,8,9,2,7,1] [1,a,2,b,f(a),g(b,c),b,1,f(a)]
Repeated elements can occur.
Antoni Lig za e Prolog 7/17
Member/2 and Select/3 Predicates
Member/2 Checking if an item occurs within a list; deterministic version.
1 2 3
member(Element,[Element|_):- !. member(Element,[_|Tail]):member(Element,Tail).
Member/2 Checking if an item occurs within a list; indeterministic version.
1 2 3
member(Element,[Element|_). member(Element,[_|Tail]):member(Element,Tail).
Select/3 Selecting and item from a list indeterministic.
1 2 3
select(Element,[Element|Tail],Tail). select(Element,[Head|Tail],[Head|TaiE]):select(Element,Tail,TaiE).
Antoni Lig za e
Prolog
8/17
Lists as Sequences: the Beauty of the Append/3 Predicate
Append/3 The basic use of the append/3 predicate is to concatenate two lists.
1 2
append([],L,L). append([H|T],L,[H|TL]) :- append(T,L,TL).
Concatenation Test
1
append([a,b],[c,d,e],[a,b,c,d,e]).
Finding Front List
1 2
append(FL,[c,d,e],[a,b,c,d,e]). FL = [a,b]
Finding Back List
1 2
append([a,b],BL,[a,b,c,d,e]). BL = [c,d,e]
Antoni Lig za e
Prolog
9/17
Append/3 Indeterministic List Decomposition
Indeterministic List Decomposition
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
append(FL,BL,[a,b,c,d,e]) FL = [], BL = [a,b,c,d,e]; FL = [a], BL = [b,c,d,e]; FL = [a,b], BL = [c,d,e]; FL = [a,b,c], BL = [d,e]; FL = [a,b,c,d], BL = [e]; FL = [a,b,c,d,e], BL = []; false.
Antoni Lig za e
Prolog
10/17
Basic Recurrent Operations: length, sum, writing a list
Length of a list
1 2 3 4
len([],0). len([_|T],L):len(T,LT), L is LT+1.
Sum of a list
1 2 3 4
sum([],0). sum([H|T],S):sum(T,ST), S is ST+H.
Write a list
1 2 3 4
writelist([]):- nl. writelist([H|T]):write(H),nl, writelist(T).
Antoni Lig za e
Prolog
11/17
Putting and Deleting Elements to/form a List
Put X as the rst element to L
1
XL = [X|L].
Put X as the k-th element to L
1 2
putk(X,1,L,[X|L]):- !. putk(X,K,[F|L],[F|LX]):- K1 is K-1, putk(X,K1,L,LX).
Delete one X from L (indeterministic!)
1 2 3
del(X,[X|L],L). del(X,[Y|L],[Y|L1]):del(X,L,L1).
Delete all X from L
1 2 3
delall(_,[],[]):- !. delall(X,[H|L],[H|LL]):- X \= H,!, delall(X,L,LL). delall(X,[X|L],LL):- delall(X,L,LL).
Antoni Lig za e Prolog 12/17
Lists and sublists. Nested Lists. Flatten List
A list and a sublist [1,2,3,4,5,6,7,8,9] [3,4,5,6] Checking for a sublist
1
sublist(S,FSL,F,L):- append(F,SL,FSL),append(S,L,SL).
A list and a subsequence [1,2,3,4,5,6,7,8,9] [3,5,8] Checking for subsequence
1 2
subseq([],_):- !. subseq([H|S],L):- append(_,[H|SL],L),!, subseq(S,SL).
Nested lists. Flatten a list [1,[2,3],4,[5,[6,7],8],9] [1,2,3,4,5,6,7,8,9]
Antoni Lig za e Prolog 13/17
Lists: some small challenges
Think!
1 2 3 4 5 6 7 8 9 10 11
[1,2,3,...,N-1,N], all permutations, K-element comobinations, all subsets, [7,2,3,4,5,6,1], [2,3,4,5,6,7,1], [7,1,2,3,4,5,6,7],
List: [1,2,3,4,5,6,7] K, [1,2,3,4,5,6,7] Set: [1,2,3,4,5,6,7]
ExchangeFL: [1,2,3,4,5,6,7] ShiftLCircular: [1,2,3,4,5,6,7] ShiftRCircular: [1,2,3,4,5,6,7] Split: [1,2,3,4,5,6,7] Merge: [1,3,5,7], [2,4,6] Split C=4: [1,2,3,4,5,6,7] p1. p2. ... pK.
[1,3,5,7], [2,4,6], [1,2,3,4,5,6,7], [1,2,3],[4],[5,6,7],
[p1,p2,...,pK].
Think! Recursion Recursion Iterations, repeat-fail.
Antoni Lig za e Prolog 14/17
Inserting List Element. Permutations.
Insert (indeterministic!). Permutations: insert
1 2 3 4 5 6
insert(X,L,LX):-
del(X,LX,L).
perm([],[]). perm([H|T],P):perm(T,T1), insert(H,T1,P).
Sorted List Denition
1 2
sorted([]):- !. sorted([_]):- !. sorted([X,Y|T]) :- X =< Y, sorted([Y|T]).
Slow Sort
1 2 3
slowsort(L,S):perm(L,S), sorted(S).
Antoni Lig za e
Prolog
15/17
Reverse List. Inverse List
Naive List Reverse
1 2 3 4
reverse([],[]). reverse([X|L],R):reverse(L,RL), append(RL,[X],R).
Iterative List Inverting: Accumulator
1 2 3 4 5
inverse(L,R):do([],L,R). do(L,[],L):-!. do(L,[X|T],S):do([X|L],T,S).
Accumulator [a,b,c], [d,e,f,g] [d,c,b,a], [e,f,g]
Antoni Lig za e
Prolog
16/17
Lists as Sets: Basic Operations
Set Algebra Operations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
subset([],_). subset([X|L],Set):member(X,Set), subset(L,Set). intersect([],_,[]). intersect([X|L],Set,[X|Z]):member(X,Set),!, intersect(L,Set,Z). intersect([X|L],Set,Z):not(member(X,Set)), intersect(L,Set,Z). union([],Set,Set). union([X|L],Set,Z):member(X,Set),!, union(L,Set,Z). union([X|L],Set,[X|Z]):not(member(X,Set)),!, union(L,Set,Z). difference([],_,[]). difference([X|L],Set,[X|Z]):not(member(X,Set)),!, difference(L,Set,Z). difference([_|L],Set,Z):- difference(L,Set,Z).
Antoni Lig za e Prolog 17/17