Title: Prolog Programming (Volume 3)
1Prolog Programming(Volume 3)
2Mapping The Full Map
- sqlist( , )
- sqlist(, ).
- sqlist(XT, YL) - Y is X X, sqlist(T, L).
List of numbers
List of squares of numbers
3Mapping The Full Map (contd)
- Here is an exercise in compound terms. Map each
list element (a number) to a term s(A,B) where A
is the number and B is its square. - Input 1, 9, 15
- Output s(1,1), s(9, 81), s(15, 225)
- sqterm(, ).
- sqterm(XT, s(X,Y)L) -
- Y is X X,
- sqterm(T, L).
4General Scheme for Full Map
- / fullmap(In, Out) /
- fullmap(, ).
- fullmap(XT, YL) -
- transform(X,Y), fullmap(T, L).
- Here is a typical transformation table
5Simple Transformation
- transform(cat, gatto).
- transform(dog, cane).
- transform(hamster, criceto).
- transform(X, X).
- ?- fullmap(cat, dog, goat, Z).
- Z gatto, cane, goat
A catchall rule
6Worksheet 11 Multiple Choices
- Sometimes the map needs to be sensitive to the
input data - Input 1, 3, w, 5, goat
- Output 1, 9, w, 25, goat
- squint(, ).
- squint(XT, YL) -
- integer(X),
- Y is X X, squint(T, L).
- squint(XT, XL) - squint(T,L).
Just use a separate clause for each choice
7Multiple Choices
- Using the infix binary compound term , it is
easy enough to give some mathematical reality to
the map - Input 1, 3, w, 5, goat
- Output 1, 9, ww, 25, goatgoat
- squint(, ).
- squint(XT, YL) -
- integer(X),
- Y is X X, squint(T, L).
- squint(XT, XXL) - squint(T,L).
8Worksheet 12 Partial Maps
- Given an input list, partially map it to an
output list. - evens(, ).
- evens(XT, XL) - 0 is X mod 2, evens(T, L)
- evens(XT, L) - 1 is X mod 2, evens(T, L).
- ?- evens(1, 2, 3, 4, 5, 6, Q),
- Q 2, 4, 6.
9General Scheme for Partial Maps
- partial(, ).
- partial(XT, XL) - include(X), partial(T,
L) - partial(XT, L) - partial(T, L).
- For example,
- include(X) - X gt 0.
- ?- partial(-1, 0, 1, -2, 2, X),
- X 0, 1, 2.
10Partial Maps
- Exercise Write a program that censors and
input list, by making a new list in which certain
prohibited words do not appear. To do this,
define a predicate prohibit such that prohibit(X)
succeeds if X is a censored word. For example, - prohibit(bother).
- prohibit(blast).
- prohibit(drat).
- prohibit(fiddlesticks).
11Worked example
- censor(, ).
- censor(HT, T1) - prohibit(H), censor(T, T1).
- censor(HT, HT1) - censor(T, T1).
12Worksheet 13 Removing Duplicates
- setify(, ).
- setify(XT, L) - member(X,T), setify(T, L).
- setify(XT, XL) - setify(T, L).
13WS14 Partial Maps with a Parameter
- Before, we saw how to prevent loops (when
searching a graph) by keeping a trail of the
nodes visited so far. Here is another way to
think about the problem. - Keep a list of all the visitable nodes. As each
node is visited, strike it off the list and pass
the reduced list to the recursive call.
Backtracking restores the old list, so
alternative paths can be searched.
14WS14 Partial Maps with a Parameter
- First, a predicate to reduce the list.
- Goal reduce(L,X,M) succeeds for input list L,
term X and output list M, when M contains the
elements of L except for the first occurrence of
X. Thus, X is a parameter that controls which
element will be omitted from the output list. - reduce(XT, X, T).
- reduce(HT, X, HL) - reduce(T, X, L).
15Use this for searching as follows
- path(X, X, L).
- path(X, Y, L) -
- a(X, Z),
- reduce(L, Z, L1),
- path(Z, Y, L1).
- Using the arc relation on Worksheet 9,
- ?- path(a, b, a, b, c, d, e, f, g, h).
- yes.
16WS 15 Multiple Disjoint Partial Maps
- Maps a list into several disjoint lists.
- Separating sheep from goats. Define the predicate
herd, such that the goal herd(L, S, G) succeeds
if S is a list of all the sheep in L and G is a
list of all the goats in L. - herd(, , ).
- herd(sheepT, sheepS, G) - herd(T, S, G).
- herd(goatT, S, goatG) - herd(T, S, G).
17What do the following goals do?
- ?- herd(sheep, goat, goat, sheep, goat, X, Y).
- ?- herd(goat, sheep, stone, goat, tree, X, Y).
- ?- herd(X, sheep, sheep, goat, goat).
18How to deal with other objects?
- ?- herd(goat, sheep, stone, goat, tree, X, Y).
- The above goal fails. Instead, we could ignore
other objects by having a catchall - herd(XT, S, G) - herd(T, S, G).
19Or, collect them in a list also
input sheep goats extras herd(, , ,
). herd(sheepT, sheepS, G, E) - herd(T,
S, G, E). herd(goatT, S, goatG, E) -
herd(T, S, G, E). herd(XT, S, G, XE) -
herd(T, S, G, E).
20Example Alternating a list
Maps an input list into a pair of lists,
alternating the elements. alternate(a, b, c, d,
e, f, a, c, e, b, d, f) Alternating by
index (which element of the input list it
is) altx(, , ). altx(A, B T, A T1,
B T2) - altx(T, T1, T2).
21Or, alternating by value
Maps an input list into a pair of lists,
alternating the elements. altv(1, 2, 3, 4, 2,
4, 1, 3) altv(, , ). altv(A T, A
T1, B) - 0 is A mod 2, altv(T, T1,
B). altv(B T, A, B T2) - 1 is B mod 2,
altv(T, A, T2). See how this is the same as sheep
and goats?
22WS 17 Full maps with state
/ mapsum(list of integers, cumulative sum)
/ ?- mapsum(1, 3, 2, 5, 4, X).
X 1, 4, 6, 11, 15.
0
23mapsum
Use an accumulator as a state variable that helps
to determine the value of each element of the
output list. / ms(input list, accumulator,
output list) / mapsum(A, B) - ms(A, 0,
B) ms(, _, ). ms(HT, N, CL) - C is H
N, ms(T, C, L).
initialise accumulator
anonymous variable (dont care about accumulator
if end of list)
24WS 18 Sequential Partial Map with State
Example Run length encoding is a useful data
representation and compression technique. Represe
nt sequential runs of N identical terms T as
the term NT, for example 12, 2, 2, w, 3, 3, s,
s, s maps to 112, 2 2, 1 w, 2 3, 3 s.
25WS 18 Sequential Partial Map with State
12, 2, 2, w, 3, 3, s, s, s
112, 2 2, 1 w, 2 3, 3
s runcode(input, current term, current count,
output) runcode(, C, N, NC). runcode(HT,
H, N, Z) - N1 is N1, runcode(T, H, N1,
Z). runcode(HT, C, N, NCZ) - H \ C,
runcode(T, H, 1, Z).
26Sequential Partial Map with State
runcode(, C, N, NC). runcode(HT, H,
N, Z) - N1 is N1, runcode(T, H, N1,
Z). runcode(HT, C, N, NCZ) - H \ C,
runcode(T, H, 1, Z).
End of list? Deal withleftover accumulator
values
Have seen this before...
Otherwise, discharge the current run...
...and start a new run