|
Allegro Prolog is included with Allegro CL 7.0 and available as a patch for Allegro CL 6.2.
Before using Allegro Prolog it must be loaded into the Lisp image, and it is convenient to use the symbols exported by the prolog package.. This can be done interactively with
(require :prolog)
(use-package :prolog)
In a source file these forms would typically be expressed this way
(eval-when (compile load eval)
(require :prolog)
(use-package :prolog))
Once loaded, interactive use of prolog is mostly through two Lisp macros. The <- macro adds rules to the internal Prolog database. The ?- macro invokes the prolog engine to try to find a solution to a series of clauses. Here is an example using the built-in Prolog append functor. It has three arguments and succeeds if appending first and second arguments results in the same same list as the third argument:
cl-user(11): (?- (append (a b) (c d) (a b c d)))
Yes
No.
cl-user(12): (?- (append (a b) (c d) (a c d)))
No.
This isn't so interesting, but the neat thing about append is that it also computes correctly if any two of the three list arguments are given!
cl-user(13): (?- (append ?x (b c d) (a b c d)))
?x = (a)
No.
Here ?x is a variable, and Prolog tells us the values of any variables when it finds a solution. It is necessary to explain how the programmer interacts with ?- macro, which is copied exactly from traditional Prolog top-level loops.
A succession of Prolog clauses like the calls to append above might have zero, one, or more solutions. For each solution, ?- prints the varues of any variables, or if there are no variables, prints the word "Yes". It then waits for input: if the programmer tells ?- to continue by typing Return (or Enter, or a semicolon, or whatever passes for an empty line on his particular system) ?- then searches for another solution. If there are no more solutions, it prints the word "No". The programmer can instruct Prolog not to look for more solutions by typing the period character.
cl-user(14): (?- (append (a b) ?x (a b c d)))
?x = (c d)
No.
So in the example above, the programmer typed Return after Prolog printed the ?x line, and then Prolog reported that there were no more solutions and returned.
Finally, Prolog append also computes results when both of the first two arguments are variables.
cl-user(15): (?- (append ?x ?y (a b c d)))
?x = ()
?y = (a b c d)
?x = (a)
?y = (b c d)
?x = (a b)
?y = (c d)
?x = (a b c)
?y = (d)
?x = (a b c d)
?y = ()
No.
In this example Prolog found five solutions, and in each case the programmer typed Return to find the next solution. The fifth time Prolog reported no more solutions.
You usually don't want to use Prolog append if the third argument and at least one of the first two arguments are variables. Prolog will find an infinite number of solutions, all of which contain additional unbound variables, but discussion of this issue is beyond the scope of this simple example.
The <-- macro used in some places above is a variant of the <- macro. It first removes any existing clauses for the same functor and arity (number of arguments), and is useful when interactively entering and reentering Prolog code. A Prolog functor is defined as a combination of all the clauses that have been entered into its database. When correcting an earlier definition it is generally necessary to remove previously-defined clauses rather than adding to them. <-- is a convenience macro that does this.
|