Prolog Programming (Volume 3) - PowerPoint PPT Presentation

About This Presentation
Title:

Prolog Programming (Volume 3)

Description:

fullmap([X|T], [Y|L]) :- transform(X,Y), fullmap(T, L). Here is a typical ... transform(hamster, criceto). transform(X, X). ?- fullmap([cat, dog, goat], Z) ... – PowerPoint PPT presentation

Number of Views:109
Avg rating:3.0/5.0
Slides: 27
Provided by: williamc4
Category:

less

Transcript and Presenter's Notes

Title: Prolog Programming (Volume 3)


1
Prolog Programming(Volume 3)
  • Dr W.F. Clocksin

2
Mapping The Full Map
  • sqlist( , )
  • sqlist(, ).
  • sqlist(XT, YL) - Y is X X, sqlist(T, L).

List of numbers
List of squares of numbers
3
Mapping 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).

4
General Scheme for Full Map
  • / fullmap(In, Out) /
  • fullmap(, ).
  • fullmap(XT, YL) -
  • transform(X,Y), fullmap(T, L).
  • Here is a typical transformation table

5
Simple 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
6
Worksheet 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
7
Multiple 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).

8
Worksheet 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.

9
General 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.

10
Partial 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).

11
Worked example
  • censor(, ).
  • censor(HT, T1) - prohibit(H), censor(T, T1).
  • censor(HT, HT1) - censor(T, T1).

12
Worksheet 13 Removing Duplicates
  • setify(, ).
  • setify(XT, L) - member(X,T), setify(T, L).
  • setify(XT, XL) - setify(T, L).

13
WS14 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.

14
WS14 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).

15
Use 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.

16
WS 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).

17
What 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).

18
How 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).

19
Or, 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).
20
Example 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).
21
Or, 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?
22
WS 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
23
mapsum
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)
24
WS 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.
25
WS 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).
26
Sequential 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
Write a Comment
User Comments (0)
About PowerShow.com