== name == Allegro Prolog == category == AI -> Logic Languages -> Prolog -> Allegro Prolog == author == Steven M. Haflich (mailto(smh@franz.com), mailto(support@franz.com)) adapted Peter Norvig's Common Lisp implementation. == author-image == == short-description == Prolog is a logic programming language built around a backward-chaining inference engine. Allegro Prolog is a highly efficient implementation of Prolog within Common Lisp. There are extensions for interfacing in both directions with Lisp code, for unifying against Lisp object slots, and for interfacing the inference engine to Lisp databases. == long-description == Prolog is a logic programming language based on first-order logic and horn clauses. Prolog is a back-chaining system, that is, it starts with a desired result and attempts to derive a path to that result using truth rules it has been given. Prolog is a mature and complete language with its own ISO standard. There exist quality implementations both commercial and free. As a self-contained programming language, ISO Prolog necessarily contains functionality in all the usual places necessary for general-purpose for a computer languages such as reading and printing, file manipulation, and operating system interface. It isn't the purpose of Allegro Prolog to compete with full Prolog implementations -- rather, it intends only to provide Lisp programs with the logic inference engine of Prolog, efficiently implemented and tightly integrated with Lisp computation. It includes a number of the usual Prolog operators, but not those concerned with capabilities such as I/O, system interface, or filesystem operations. Lisp programmers using Prolog for its inferencing capabilities would probably prefer to keep these other system areas in Lisp code. This makes the Prolog system smaller and easier for Lisp programmers to learn. There are two equivalent syntaxes traditionally used for expressing Prolog code. Edinburgh syntax, the more common of the two, is similar to C syntax, while Lisp sexpr syntax is more-often used by systems implemented in and interfacing with Lisp. Allegro Prolog uses sexpr syntax. There is more info and additional links on the Franz Inc. href(Tech Corner article about Allegro Prolog, http://www.franz.com/support/tech_corner/prolog-071504.lhtml). == examples == Here is a solution for the "Who owns the zebra?" puzzle in Allegro Prolog: (<-- (nextto ?x ?y ?list) (iright ?x ?y ?list)) (<- (nextto ?x ?y ?list) (iright ?y ?x ?list)) (<-- (iright ?left ?right (?left ?right . ?rest))) (<- (iright ?left ?right (?x . ?rest)) (iright ?left ?right ?rest)) (<-- (zebra ?h ?w ?z) ;; Each house is of the form: ;; (house nationality pet cigarette drink house-color) (= ?h ((house norwegian ? ? ? ?) ? (house ? ? ? milk ?) ? ?)) (member (house englishman ? ? ? red) ?h) (member (house spaniard dog ? ? ?) ?h) (member (house ? ? ? coffee green) ?h) (member (house ukrainian ? ? tea ?) ?h) (iright (house ? ? ? ? ivory) (house ? ? ? ? green) ?h) (member (house ? snails winston ? ?) ?h) (member (house ? ? kools ? yellow) ?h) (nextto (house ? ? chesterfield ? ?) (house ? fox ? ? ?) ?h) (nextto (house ? ? kools ? ?) (house ? horse ? ? ?) ?h) (member (house ? ? luckystrike oj ?) ?h) (member (house japanese ? parliaments ? ?) ?h) (nextto (house norwegian ? ? ? ?) (house ? ? ? ? blue) ?h) (member (house ?w ? ? water ?) ?h) (member (house ?z zebra ? ? ?) ?h) ) ;; This runs the query: (?- (zebra ?houses ?water-drinker ?zebra-owner)) For comparison, here is how the first few rules above would be expressed in Edinburgh syntax: nextto(X, Y, List) :- iright(X, Y, List). nextto(X, Y, List) :- iright(Y, X, List). iright(Left, Right, [Left, Right | _]). iright(Left, Right, [_ | Rest]) :- iright(Left, Right, Rest). == instructions == 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 bold(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. == tutorial == Computation done by a Prolog engine is fundamentally different from computation done by functional languages like Lisp, C, and Java. Programmers unfamiliar with Prolog would do well to consult one of several Prolog tutorials on the web. Although these will cover aspects of Prolog programming that won't be used in Allegro Prolog (e.g. reading and printing) but they will introduce the standard Prolog operators and acquaint the programmer with the Prolog way of thinking. The Allegro Prolog tutorial touches on Prolog programming itself, but concentrates more on issues specific to Lisp integration. Programmers without sufficient background in Prolog are encouraged to read one of the several available Prolog tutorials. In addition to the classic Clocksyn and Mellish cited under book links, several free tutorials are available via the www.swi-prolog.org site from the "links" navigation menu. The recent _Learn Prolog Now!_ by Blackburn, Bos, and Striegnitz seems particularly strong on the issues of Prolog programming, rather than Prolog theory and implementation. == home-url == == doc-url == http://www.franz.com/support/documentation/7.0/doc/prolog.html == license == Allegro Prolog requires no additional license beyond the license to use Allegro CL 6.2 or later. Supported (including Trial) users of 6.2 can download Allego Prolog as a normal patch file with sys:update-allegro. == book == href(Paradigms of Artificial Intelligence Programming,http://www.amazon.com/exec/obidos/ASIN/1558601910/) by Peter Norvig. Allegro Prolog is an extended, optimized version of the original Prolog implementation developed by Norvig in this volume. The development and explanation are excellent for anyone who wants both to understand how to use Prolog and what the Prolog engine is doing inside. href(Programming in Prolog,http://www.amazon.com/exec/obidos/tg/detail/-/3540583505) by W.F. Clocksyn, C.S. Mellish. "Clocksin and Mellish" through its several versions has been to Prolog what "K&R" has been to C. It is the standard Prolog tutorial and reference. == references == Anyone who wants to play with a standalone Prolog implementation is encouraged to check out href(SWI Prolog,http://www.swi-prolog.org/). It is an excellent, mature, open-souce Prolog implementation. The web site also has numerous links to other Prolog resources. Allegro Prolog is not in competition with standalone Prologs. Allegro Prolog exists to make the Prolog inference engine and standard predefined functors -- the core of the Prolog language model -- available from a Common Lisp program. == source-fooball == Currently sources are not available. Meanwhile, parties interested in obtaining the source may contact Franz Inc. at mailto(info@franz.com). == release-date == 15 July, 2004 == release-version == 0.92 == status == Stable. Allegro Prolog is a supported product, but it is also a new product and still evolving. Integration of Prolog, Comon Lisp, and large database support is somewhat an experimental project, and this experimentation will drive evolution. So much as possible, of course, such evolution will be backward compatible. Updated patch versions for Allegro 6.2 and 7.0 will be released from time to time through the regular Franz Inc support site. The usual update-allegro procedure will retrieve the latest version. == history == Initial release as an Allegro CL 6.2 patch was 15 July, 2004. == acl-dependencies == Should run in any Allegro CL 6.2 or later. 7.0 or later preferred for slight performance gain. == other-dependencies == None. == platform == Any Allegro CL supported platform. == ad == aiprogramming