12.1. A brief introduction to clausal logic#
There are six answers to this query:
{ W→green_park }
{ W→piccadilly_circus }
{ W→leicester_square }
{ W→bond_street }
{ W→oxford_circus }
{ W→tottenham_court_road }
The proof trees for the first three answers are analogous to Figure 1.2. The proof tree for the fourth answer is given below (the two remaining proof trees are similar):
A list is either the empty list []
, or a non-empty list [First|Rest]
where Rest
is a list. Define a predicate list(L)
, which checks whether L
is a list. Adapt it such that it succeeds only for lists of (1) even length and (2) odd length.
The first specification can immediately be translated to Prolog:
list([]).
list([_First|Rest]):-list(Rest).
A list of even length is either the empty list, or a non-empty list with two more elements than the next shorter list of even length:
evenlist([]).
evenlist([_First,_Second|Rest]):-evenlist(Rest).
In order to adapt this definition for lists of odd length, only the non-recursive clause needs to be changed:
oddlist([_One]).
oddlist([_First,_Second|Rest]):-oddlist(Rest).
Notice that oddlist
can also be defined in terms of evenlist
(or vice versa):
oddlist([_First|Rest]):-evenlist(Rest).
Construct a query asking for a route from Bond Street to Piccadilly Circus with at least two intermediate stations.
?-reachable(bond_street,piccadilly_circus,[S1,S2|Rest]).