Home Contents Index Summary Previous Next

2.7 Query Substitutions

SWI-Prolog offers a query substitution mechanism similar to that of Unix csh (csh(1)), called `history'. The availability of this feature is controlled by set_feature/2, using the history feature. By default, history is available if the feature readline is false. To enable this feature, remembering the last 50 commands, put the following into your startup file (see section 2.2:


:- set_feature(history, 50).

The history system allows the user to compose new queries from those typed before and remembered by the system. It also allows to correct queries and syntax errors. SWI-Prolog does not offer the Unix csh capabilities to include arguments. This is omitted as it is unclear how the first, second, etc. argument should be defined. (3)

The available history commands are shown in table 1. Figure 1 gives some examples.

!!. Repeat last query
!nr. Repeat query numbered <nr>
!str. Repeat last query starting with <str>
!?str. Repeat last query holding <str>
^old^new. Substitute <old> into <new> in last query
!nr^old^new. Substitute in query numbered <nr>
!str^old^new. Substitute in query starting with <str>
!?str^old^new. Substitute in query holding <str>
h. Show history list
!h. Show this list
Table 1 : History commands


/staff/jan/.plrc consulted, 0.066667 seconds, 591 bytes
Welcome to SWI-Prolog (Version \plversion)
Copyright (c) 1993-1996 University of Amsterdam.  All rights reserved.

For help, use ?- help(Topic). or ?- apropos(Word).

1 ?- append("Hello ", "World", L).

L = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100] 

Yes
2 ?- !!, writef('L = %s\n', [L]).
append("Hello ", "World", L), writef('L = %s\n', [L]).
L = Hello World

L = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100] 

Yes
3 ?- sublist(integer, [3, f, 3.4], L).

L = [3] 

Yes
4 ?- ^integer^number.
sublist(number, [3, f, 3.4], L).

L = [3, 3.400000] 

Yes
5 ?- h.
    1   append("Hello ", "World", L).
    2   append("Hello ", "World", L), writef('L = %s\n', [L]).
    3   sublist(integer, [3, f, 3.4], L).
    4   sublist(number, [3, f, 3.4], L).

5 ?- !2^World^Universe.
append("Hello ", "Universe", L), writef('L = %s\n', [L]).
L = Hello Universe

L = [72, 101, 108, 108, 111, 32, 85, 110, 105, 118, 101, 114, 115, 101] 

Yes
6 ?- halt.

Figure 1 : Some examples of the history facility

2.7.1 Limitations of the History System

When in top level SWI-Prolog reads the user's queries using read_history/6 rather than read/1. This predicate first reads the current input stream up to a full stop. While doing so it maps all contiguous blank space onto a single space and deletes /* ... */ and % ...<cr> comments. Parts between double quotes (") or single quotes (') are left unaltered. Note that a Prolog full stop consists of a `non-symbol' character, followed by a period (.), followed by a blank character. `Symbol' characters are: #$&*+-./:<=>?@^`~. A single quote immediately preceded by a digit (0-9) is considered part of the <digit>'<digit>... (e.g. 2'101; binary number 101) sequence.

After this initial parsing the result is first checked for the special ^<old>^<new>. construction. If this fails the string is checked for all occurrences of the !, followed by a !, ?, a digit, a letter or an underscore. These special sequences are analysed and the appropriate substitution from the history list is made.

From the above it follows that it is hard or impossible to correct quotation with single or double quotes, comment delimiters and spacing.