-
Notifications
You must be signed in to change notification settings - Fork 1
/
chapter1.pl
127 lines (110 loc) · 3.89 KB
/
chapter1.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% Prolog programs from Chapter 1 of the book %
% SIMPLY LOGICAL: Intelligent reasoning by example %
% (c) Peter A. Flach/John Wiley & Sons, 1994. %
% %
% Predicates: connected/3 %
% nearby/2 (several versions) %
% reachable/2 (several versions) %
% reachable/3 (several versions) %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% The London Underground example %%%
connected(bond_street,oxford_circus,central).
connected(oxford_circus,tottenham_court_road,central).
connected(bond_street,green_park,jubilee).
connected(green_park,charing_cross,jubilee).
connected(green_park,piccadilly_circus,piccadilly).
connected(piccadilly_circus,leicester_square,piccadilly).
connected(green_park,oxford_circus,victoria).
connected(oxford_circus,piccadilly_circus,bakerloo).
connected(piccadilly_circus,charing_cross,bakerloo).
connected(tottenham_court_road,leicester_square,northern).
connected(leicester_square,charing_cross,northern).
/*
nearby(bond_street,oxford_circus).
nearby(oxford_circus,tottenham_court_road).
nearby(bond_street,tottenham_court_road).
nearby(bond_street,green_park).
nearby(green_park,charing_cross).
nearby(bond_street,charing_cross).
nearby(green_park,piccadilly_circus).
nearby(piccadilly_circus,leicester_square).
nearby(green_park,leicester_square).
nearby(green_park,oxford_circus).
nearby(oxford_circus,piccadilly_circus).
nearby(piccadilly_circus,charing_cross).
nearby(oxford_circus,charing_cross).
nearby(tottenham_court_road,leicester_square).
nearby(leicester_square,charing_cross).
nearby(tottenham_court_road,charing_cross).
*/
nearby(X,Y):-connected(X,Y,L).
nearby(X,Y):-connected(X,Z,L),connected(Z,Y,L).
%%% 1.2 Recursion %%%
/*
reachable(bond_street,charing_cross).
reachable(bond_street,green_park).
reachable(bond_street,leicester_square).
reachable(bond_street,oxford_circus).
reachable(bond_street,piccadilly_circus).
reachable(bond_street,tottenham_court_road).
reachable(green_park,charing_cross).
reachable(green_park,leicester_square).
reachable(green_park,oxford_circus).
reachable(green_park,piccadilly_circus).
reachable(green_park,tottenham_court_road).
reachable(leicester_square,charing_cross).
reachable(oxford_circus,charing_cross).
reachable(oxford_circus,leicester_square).
reachable(oxford_circus,piccadilly_circus).
reachable(oxford_circus,tottenham_court_road).
reachable(piccadilly_circus,charing_cross).
reachable(piccadilly_circus,leicester_square).
reachable(tottenham_court_road,charing_cross).
reachable(tottenham_court_road,leicester_square).
% non-recursive version
reachable(X,Y):- connected(X,Y,L).
reachable(X,Y):- connected(X,Z,L1),connected(Z,Y,L2).
reachable(X,Y):- connected(X,Z1,L1),connected(Z1,Z2,L2),
connected(Z2,Y,L3).
*/
% recursive version
reachable(X,Y):-connected(X,Y,L).
reachable(X,Y):-connected(X,Z,L),reachable(Z,Y).
%%% 1.3 Structured terms %%%
/*
% non-recursive version with route
reachable0(X,Y):-
connected(X,Y,L).
reachable1(X,Y,Z):-
connected(X,Z,L1),
connected(Z,Y,L2).
reachable2(X,Y,Z1,Z2):-
connected(X,Z1,L1),
connected(Z1,Z2,L2),
connected(Z2,Y,L3).
% non-recursive version with route and functor
reachable(X,Y,noroute):-
connected(X,Y,L).
reachable(X,Y,route(Z)):-
connected(X,Z,L1),
connected(Z,Y,L2).
reachable(X,Y,route(Z1,Z2)):-
connected(X,Z1,L1),
connected(Z1,Z2,L2),
connected(Z2,Y,L3).
% recursive version with route and functor
reachable(X,Y,noroute):-
connected(X,Y,L).
reachable(X,Y,route(Z,R)):-
connected(X,Z,L),
reachable(Z,Y,R).
*/
% recursive version with route and list
reachable(X,Y,[]):-
connected(X,Y,L).
reachable(X,Y,[Z|R]):-
connected(X,Z,L),
reachable(Z,Y,R).