The document describes a Prolog program for planning flight routes between locations on a given day. It defines predicates for finding direct and indirect flight routes based on a timetable database. The timetable stores flights between locations with departure and arrival times, flight numbers, and valid days. The route predicate uses the flight predicate to recursively find valid multi-segment routes that ensure a minimum transfer time between connections. Sample queries and timetable facts are provided to demonstrate the program's operation.
2. Concatenation- Review
Recursive definition
Base clause: conc the empty list to any list
produces that same list
The recursive step says that when concatenating a
non-empty list [H|T] with a list L, the result is a list
with head H and the result of concatenating
T and L
conc([], L, L).
conc([H|L1], L2, [H|L3]):-
conc(L1, L2, L3).
3. How conc/3 works
Two ways to find out:
Use trace/0 on some examples
Draw a search tree!
Let us consider a simple example
?- conc([a,b,c],[1,2,3], R).
4. Search tree example
?- conc([a,b,c],[1,2,3], R).
conc([], L, L).
conc([H|L1], L2, [H|L3]):- conc(L1, L2, L3).
5. How conc works
Two ways to find out:
Use trace/0 on some examples
Draw a search tree!
Let us consider a simple example
?- conc([a,b,c],[1,2,3], R).
6. Search tree example
?- conc([a,b,c],[1,2,3], R). conc([], L, L).
conc([H|L1], L2, [H|L3]):-
conc(L1, L2, L3).
7. Search tree example
?- conc([a,b,c],[1,2,3], R). conc([], L, L).
conc([H|L1], L2, [H|L3]):-
conc(L1, L2, L3).
8. Search tree example
?- conc([a,b,c],[1,2,3], R).
/
conc([], L, L).
conc([H|L1], L2, [H|L3]):-
conc(L1, L2, L3).
9. Search tree example
?- conc([a,b,c],[1,2,3], R).
/
† R = [a|L0]
?- conc([b,c],[1,2,3],L0)
conc([], L, L).
conc([H|L1], L2, [H|L3]):-
conc(L1, L2, L3).
10. Search tree example
?- conc([a,b,c],[1,2,3], R).
/
† R = [a|L0]
?- conc([b,c],[1,2,3],L0)
/
conc([], L, L).
conc([H|L1], L2, [H|L3]):-
conc(L1, L2, L3).
11. Search tree example
?- conc([a,b,c],[1,2,3], R).
/
† R = [a|L0]
?- conc([b,c],[1,2,3],L0)
/
† L0=[b|L1]
?- conc([c],[1,2,3],L1)
conc([], L, L).
conc([H|L1], L2, [H|L3]):-
conc(L1, L2, L3).
12. Search tree example
?- conc([a,b,c],[1,2,3], R).
/
† R = [a|L0]
?- conc([b,c],[1,2,3],L0)
/
† L0=[b|L1]
?- conc([c],[1,2,3],L1)
/
conc([], L, L).
conc([H|L1], L2, [H|L3]):-
conc(L1, L2, L3).
17. Small exercise
Consider the following fact.
p([H|T], H, T).
Lets see what happens when we ask some simple queries.
?- p([a,b,c], X, Y).
X=a
Y=[b,c]
true
?- p([a], X, Y).
X=a
Y=[]
true
?- p([], X, Y).
false
18. Print a list with space
print_a_list([]).
print_a_list([H|T]):- write(H),
write(‘ ‘),
print_a_list(T).
?- print_a_list([a,b,c,d]).
a b c d
.
19.
20. Add a member to a List
Code can be written as below:
add(X,L,[X|L]).
Example:
add(a,[1,2,3],L).
21. Delete a member from a List
Consider del(X,L,L1), deletes X from L and puts
the new list in L1.
Code can be written as below:
del(X,[X|Tail],Tail). % if X is head of the List L
del(X,[Y|Tail], [Y|Tail1]):-
del(X,Tail,Tail1).
Example:
del(1,[1,2,3],L).
22. Sublist
S is sublist of L if:
(1) L can be decomposed into two lists L1, L2 and
(2) L2 can be decomposed into two lists, S and
some L3
L1 L2X member(X,L)
L1 L3S
L2
L
L
[X|L2]
sublist(S,L)
25. Permutations
The permutation relation with 2 arguments of 2 lists such
that one is a permutation of the other.
?- permutation( [a,b,c], P ).
P = [a,b,c];
P = [a,c,b];
P = [b,a,c];
…
Example:
?- permutation( [red,blue,green], P ).
P = [red, blue, green];
P = [red, green, blue];
P = [blue, red, green];
P = [blue, green, red];
P = [green, red, blue];
P = [green, blue, red];
no
X L
L1
Insert X obtain a
permutation of [X | L]
L1 is a permutation of
L
26. 1.If the list is empty, new permutation must be
empty
permutation([],[]).
2. It the list is not empty it has a form [X|L].
permutation([X|L],P):-
permutation(L,L1),
insert(X,L1,P).
28. Predefined operators for basic arithmetic operations:
Addition +
Subtraction -
Multiplication *
Division /
Power **
Integer division //
Modula, the remainder of integer division mod
Example:
?- X is 1 + 2.
X = 3
Comparing numerical values e.g.
?- 300 * 35 > 10000.
yes
29. CPT114
Operator is force evaluation
?- X is 5/2, Y is 6//2, Z is 5 mod 2.
X = 2.5
Y = 2
Z = 1
Comparison operator
> < >= =< =:= ==
?- born (Name, Year), Year >= 1990, Year =< 2000.
30. Greatest common divisor
(1) If X and Y are equal, then D is sequal to X.
(2) If X < Y, then D is equal to gcd of X and the difference Y
– X.
(3) If Y < X, then do the same as in case (2) with X and Y
interchanged.
gcd(X, X, X).
gcd(X, Y, D) :- X < Y, Y1 is Y –X, gcd(X, Y1, D).
gcd(X, Y, D) :- Y < X, gcd(Y, X, D).
Exercise For Arithmetic
31. Length of list – counting the item in the list.
(1) If the list is empty, then length is 0.
(2) If list not empty, then List = [Head | Tail]; then its length is equal to
1 plus the length of the tail Tail.
len([], 0).
len([_|Tail], N) :-
len(Tail, N1),
N is 1 + N1.
?- len([a,b,[c,d],e],N).
N = 4
37. Matching
?- data(D,M,83) = date(D1,may,Y1) .
Matching Rules:
if S and T constants, then S & T match if they are the same object.
if S is a variable and T is anything, then they match & S is instantiated to T. if T is a var then T is
instantiated to S.
if S and T are structures, then they match if :
S and T have the same functor
all their correspondinf components match.
Example:
ver(seg(pt(X,Y),pt(X,Y1))). hor(seg(pt(X,Y),pt(X1,Y))).
?- ver(seg(pt(1,1),pt(1,2))).
yes
?- ver(seg(pt(1,1),pt(2,Y))).
no
?- hor(seg(pt(1,1),pt(2,Y))).
Y=1
?- ver(seg(pt(2,3),P)).
P=pt(2,Y)
39. Trace the process
1. Initial goal: dark(X),big(X)
2. dark(X) := black(X)
3. new goal: black(X),big(X)
4. found black(cat)
. check big(cat)
no clause found
backtrack to step 3
no other black
backtrack to step 2
dark(X) :- brown(X),big(X).
5. new goal: brown(X),big(X).
6. found brown(bear)
check big(bear)
both satisfied
41. Travel Planning
Querying a database of flight information
timeTable(Place1, Place2, ListOfFlights).
Form of flight item
depTime / arrTime / fltNo / days
One day travels
?- route(Place1, Place2, Day, Route).
41
operato
r
42. Examples of Fact and Query
timetable(edinburgh, london,
[ 9:40 / 10:50 / ba4733 / alldays,
13:40 / 14:50 / ba4773 / alldays,
19:40 / 20:50 / ba4833 / [mo,we,fr]]).
Note that ‘:’ should bind stronger than ‘/’.
?- route(london, paris, tu, Route)
Form of items in the route
from / to / fltNo / depTime
42
43. Prolog Code
% A Flight Route Planner
:- op(50, xfy, :).
% route(Place1, Place2, Day, Route).
% Route is a sequence of flights on Day, starting at Place1 and ending at
Place2
route(P1, P2, Day, [P1 / P2 / Fnum / Deptime]) :-
flight(P1, P2, Day, Fnum, Deptime, _).
% direct flight
route(P1, P2, Day, [(P1 / P3 / Fnum1 / Dep1) | RestRoute]) :-
flight(P1, P3, Day, Fnum1, Dep1, Arr1),
route(P3, P2, Day, RestRoute),
deptime(RestRoute, Dep2),
transfer(Arr1, Dep2).
% Indirect flight - ensure enough transfer time
% optimize by propagating that constraint
43
48. Queries
What days of the week is there is a direct
evening flight from Ljubljana to London?
?- flight(ljubljana, london, Day, _,
DepHour:_,_), DepHour >= 18.
How can I get from Ljubljana to London on
Thursday?
?- route(ljubljana, london, th, R).
Queries can result in infinite loops or stack
overflow on Prolog.
48
49. SWI-Prolog vs XSB
XSB: table route/4.
How can I get from Ljubljana to Edinburgh
on Thursday?
?- route(ljubljana, edinburgh, th, R).
SWI-Prolog: Out of local stack
XSB: [ ljubljana / zurich / jp322 / 11 : 30,
zurich / london / sr806 / 16 : 10,
london / edinburgh / ba4822 / 18 :
40];
49
Editor's Notes
Numeric comparison operators: <, >, =<, >=, =:=, =\=
To solve a numeric comparison goal, Prolog evaluates both sides and compares the results numerically
So both sides must be fully instantiated
?- A + 1 =:= 2 + B.
ERROR: =:=/2: Arguments are not sufficiently instantiated
captures logic and database like querying
for more sophisticated control for optimal routing problem:
need additional constructs
enable hr:min:sec
member built-in in SWI-Prolog but requires explicit definition in XSB.