The cl-prolog2 Reference Manual

Table of Contents

Next: , Previous: , Up: (dir)   [Contents][Index]

The cl-prolog2 Reference Manual

This is the cl-prolog2 Reference Manual, version 0.1, generated automatically by Declt version 2.4 patchlevel 1 "Will Decker" on Mon Jul 29 15:07:51 2019 GMT+0.


Next: , Previous: , Up: Top   [Contents][Index]

1 Introduction

Cl-Prolog2 - Common Interface to the ISO Prolog implementations from Common Lisp

Build Status

This is a realization of Marc Kuo's "modelling approach to OR (operations research)" for Prolog language.

This library provides a transpiler from S-expression to ISO-standardized Prolog language, and the sub-libraries that invoke State-of-the-Art Prolog compilers such as SWI-prolog, XSB, Yap. Choose the one with the best performance for your application.

News We changed the implementation to use the batch-mode only, since the behavior of Prolog top-level loop is not defined in the ISO standard, and it is hard to maintain the compatibility between different interpreters.

News Added support for XSB and GNU Prolog.

News Added a CLI interface (roswell script) which can process a sexp-based prolog file directly from the command line.

News *debug-level* can control the verbosity of the output.

For a practical guide to write a fast, efficient prolog, this page might help.

Related work

It looks like https://github.com/keithj/cl-prolog has a similar idea in mind. However, it is based on a FFI binding written for each interpreter, thus it does not scale to a larger number of prolog systems.

There are already many lisp-to-prolog libraries, including the one mentioned above, but also Allegro Prolog, PAIP prolog and the Prolog in On Lisp are the famous ones. They rather implemented a Prolog system by itself, i.e., programming OR approach. Although these are different approaches, it is quite possible to support those systems from cl-prolog2.

Supported implementations

Recently, many prolog systems contain an interface to constraint/linear programming solvers. Also, with tabling semantics (basically a form of automated memoization), certain programs runs faster and can be conveniently written without much consideration on the termination. I believe there is a good motivation to reevaluate Prolog as a good "middleware language" for expressing and solving complex problems.

Tabling (also known as SLG resolution or Well Founded Semantics) is supported by all implementations above, but with various range of feature support. See the later section for difference.

API

The ASDF system cl-prolog2 does not provide implementations, but merely the API to those implementations. The sub-libraries of cl-prolog2 are in the corresponding sub-directories.

They should implement a method (run-prolog rules prolog-designator &key debug args &allow-other-keys), where prolog-designator is a keyword symbol such as :swi or :yap. The function returns the output of the process as a string.

We don't provide a parser for Prolog output and, therefore, formatting the output should be done by Prolog or you should write a parser from lisp. Also, consider using print-term-sexp below.

Debug level of prolog is controlled by *debug-prolog*.

*DEBUG-PROLOG* names a special variable:
  Declared type: (UNSIGNED-BYTE 2)
  Value: 0
  Documentation:
    Flag (0-3) for debugging the input to the prolog interpreter.
     0  : disabled.
     >=1: print the command line
     >=2: print the prolog output
     3  : most verbose. print misc messages. 

Query format

rule : (:- top-term top-term*) | top-term

top-term : ('not term) | ( atom term* )

term : builtins | ( atom term* ) | atom | variable | number | string

variable : symbols starting with ? or _

atom : other symbols

builtins : ('list term*) | ('list* term*) | ('not term) | ('or term*) | ('and term*)

Predicates and atoms are wrapped in single quotes.

Variables are given to the Prolog interpreter as underscored symbols, and note that all non-alphanumeric characters are converted to underscores. This is more than enough for most use cases because variables are converted to gensyms by the prolog interpreter anyways, and we cannot expect the variable names are maintained. Care must be taken when two variables ends up in the same name, e.g. both ?a-b-c and ?a_b_c ends up in _a_b_c.

Symbol _ and ? are both converted to symbol _, a wildcard symbol.

CLI interface (roswell script)

To use the CLI interface, install roswell and run ros install cl-prolog2. (note: until it is included in Quicklisp, use ros install guicho271828/cl-prolog2 instead.) A usage example is in examples/ directory.

Usage:
cl-prolog2 [-E] [-d] [-s] [-b BACKEND] FILE ARGS...

This program takes a file written in cl-prolog2 sexp format and process it.
Normally it converts the input to a regular prolog file and invokes the specified prolog interpreter/compiler.

Options:

-E : Preprocessing-only mode. It writes the regular prolog file to the standard output.
-d : debug mode, the preprocessed prolog file is echoed in the error output.
-s : it appends the print-sexp code to the prolog output.
-b BACKEND : change the backend, e.g. swi, yap, xsb, gprolog, bprolog. [default: swi]
ARGS... : ARGS is a set of additional parameters given to each prolog processor.

Utility Functions

cl-prolog2:sort-clauses (rules)

Destructively sort the rules by dictionary ordering.
Useful for avoiding noncontiguous-clauses errors (rules for the same clauses are not written together).
However, since it alters the ordering of the rules and Prolog checks the rules from top-to-bottom,
it may corrupt the program especially when cut operator (!) is involved.

Facts (rules without conditions) are given precedence to the standard rules.

cl-prolog2:print-sexp ()

This function returns a cl-prolog2 program for a prolog rule print-sexp/1,
which prints a prolog term in a SEXP form.

print-sexp prints atoms/numbers as atoms/numbers, a term as a list, and a list as a list.

To be used with SWI, SWI should be non-nil due to the implementation-specific matter.
(It uses compound_name_arguments/2 instead of =../2 for printing a term.)

usage:

(run-prolog `((:- main
                  (print-sexp (parent-of luke anakin))
                  halt)
              (:- (initialization main))
              ,@(print-sexp :swi t))
            :swi :output :string)

;; -> "(parent-of luke anakin)", NIL, 0

To what extent prolog systems are compatible to each other?

No implementations are complete. Each system has its own problem. Below we list some minor/major incompatibility and advantage/disadvantage that I encountered:

Dependencies

This library is at least tested on implementation listed below:

To test the library, you need swi-prolog, gprolog, xsb, bprolog or yap prolog. Specific notes for implementations:

Author

Copyright

Copyright (c) 2017 Masataro Asai (guicho2.71828@gmail.com)

License

Copyright 2017 Masataro Asai Released under the MIT license http://opensource.org/licenses/mit-license.php


Next: , Previous: , Up: Top   [Contents][Index]

2 Systems

The main system appears first, followed by any subsystem dependency.


Previous: , Up: Systems   [Contents][Index]

2.1 cl-prolog2

Author

Masataro Asai

Contact

guicho2.71828@gmail.com

License

MIT

Description

Common Interface to the ISO prolog implementations from Common Lisp

Version

0.1

Dependencies
Source

cl-prolog2.asd (file)

Components

Next: , Previous: , Up: Top   [Contents][Index]

3 Files

Files are sorted by type and then listed depth-first from the systems components trees.


Previous: , Up: Files   [Contents][Index]

3.1 Lisp


Next: , Previous: , Up: Lisp files   [Contents][Index]

3.1.1 cl-prolog2.asd

Location

/home/quickref/quicklisp/dists/quicklisp/software/cl-prolog2-20181018-git/cl-prolog2.asd

Systems

cl-prolog2 (system)


Next: , Previous: , Up: Lisp files   [Contents][Index]

3.1.2 cl-prolog2/package.lisp

Parent

cl-prolog2 (system)

Location

package.lisp

Packages

Next: , Previous: , Up: Lisp files   [Contents][Index]

3.1.3 cl-prolog2/compiler-macro.lisp

Dependency

package.lisp (file)

Parent

cl-prolog2 (system)

Location

compiler-macro.lisp

Internal Definitions

Next: , Previous: , Up: Lisp files   [Contents][Index]

3.1.4 cl-prolog2/printers.lisp

Dependency

compiler-macro.lisp (file)

Parent

cl-prolog2 (system)

Location

printers.lisp

Internal Definitions

Next: , Previous: , Up: Lisp files   [Contents][Index]

3.1.5 cl-prolog2/interpreter.lisp

Dependency

printers.lisp (file)

Parent

cl-prolog2 (system)

Location

interpreter.lisp

Exported Definitions

Previous: , Up: Lisp files   [Contents][Index]

3.1.6 cl-prolog2/util.lisp

Dependency

interpreter.lisp (file)

Parent

cl-prolog2 (system)

Location

util.lisp

Exported Definitions

Next: , Previous: , Up: Top   [Contents][Index]

4 Packages

Packages are listed by definition order.


Next: , Previous: , Up: Packages   [Contents][Index]

4.1 cl-prolog2

Source

package.lisp (file)

Used By List

cl-prolog2.impl

Exported Definitions

Previous: , Up: Packages   [Contents][Index]

4.2 cl-prolog2.impl

Source

package.lisp (file)

Use List
Exported Definitions
Internal Definitions

Next: , Previous: , Up: Top   [Contents][Index]

5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


Next: , Previous: , Up: Definitions   [Contents][Index]

5.1 Exported definitions


Next: , Previous: , Up: Exported definitions   [Contents][Index]

5.1.1 Special variables

Special Variable: *debug-prolog*

Flag (0-3) for debugging the input to the prolog interpreter. 0 : disabled.
>=1: print the command line
>=2: print the prolog output
3 : most verbose. print misc messages.

Package

cl-prolog2

Source

interpreter.lisp (file)


Next: , Previous: , Up: Exported definitions   [Contents][Index]

5.1.2 Macros

Macro: with-temp (VAR &key DIRECTORY TEMPLATE TMPDIR DEBUG) &body BODY

Create a temporary file, then remove the file by unwind-protect.
Most arguments are analogous to mktemp.
When DIRECTORY is non-nil, creates a directory instead.
DEBUG is a form.
When evaluated to non-nil, it does not remove the directory so that you can investigate what happened inside the directory. It may be evaluated multiple times.

Package

cl-prolog2.impl

Source

interpreter.lisp (file)


Next: , Previous: , Up: Exported definitions   [Contents][Index]

5.1.3 Functions

Function: print-rule STREAM RULE
Package

cl-prolog2.impl

Source

interpreter.lisp (file)

Function: print-sexp &key SWI

This function returns a cl-prolog2 program for a prolog rule print-sexp/1,
which prints a prolog term in a SEXP form.

print-sexp prints atoms/numbers as atoms/numbers, a term as a list, and a list as a list.

To be used with SWI, SWI should be non-nil due to the implementation-specific matter. (It uses compound_name_arguments/2 instead of =../2 for printing a term.)

usage:

(run-prolog ‘((:- main
(print-sexp (parent-of luke anakin))
halt)
(:- (initialization main))
,@(print-sexp :swi t))
:swi :output :string)

;; -> "(parent-of luke anakin)", NIL, 0

Package

cl-prolog2

Source

util.lisp (file)

Function: run-command-with-debug-print COMMAND &rest ARGS
Package

cl-prolog2.impl

Source

interpreter.lisp (file)

Function: sort-clauses RULES

Destructively sort the rules by dictionary ordering.
Useful for avoiding noncontiguous-clauses errors (rules for the same clauses are not written adjacently). Facts (rules without conditions) are given precedence to the standard rules.
Sorting is stable.
However, since it alters the ordering of the rules and Prolog checks the rules from top-to-bottom,
it may corrupt the program especially when cut operator (!) is involved.

Package

cl-prolog2

Source

util.lisp (file)


Previous: , Up: Exported definitions   [Contents][Index]

5.1.4 Generic functions

Generic Function: run-prolog RULES PROLOG-DESIGNATOR &key DEBUG ARGS INPUT OUTPUT ERROR &allow-other-keys
Package

cl-prolog2

Source

interpreter.lisp (file)


Previous: , Up: Definitions   [Contents][Index]

5.2 Internal definitions


Next: , Previous: , Up: Internal definitions   [Contents][Index]

5.2.1 Compiler macros

Compiler Macro: %print-rule STREAM OBJ COLON AT
Package

cl-prolog2.impl

Source

compiler-macro.lisp (file)

Compiler Macro: print-commas STREAM OBJ COLON AT
Package

cl-prolog2.impl

Source

compiler-macro.lisp (file)

Compiler Macro: print-semicolons STREAM OBJ COLON AT
Package

cl-prolog2.impl

Source

compiler-macro.lisp (file)

Compiler Macro: print-term STREAM OBJ COLON AT
Package

cl-prolog2.impl

Source

compiler-macro.lisp (file)


Previous: , Up: Internal definitions   [Contents][Index]

5.2.2 Functions

Function: %print-rule STREAM LIST COLON AT
Package

cl-prolog2.impl

Source

printers.lisp (file)

Function: constant-fold-printer ENV WHOLE
Package

cl-prolog2.impl

Source

compiler-macro.lisp (file)

Function: print-commas STREAM LIST COLON AT
Package

cl-prolog2.impl

Source

printers.lisp (file)

Function: print-semicolons STREAM LIST COLON AT
Package

cl-prolog2.impl

Source

printers.lisp (file)

Function: print-term STREAM TERM COLON AT
Package

cl-prolog2.impl

Source

printers.lisp (file)


Previous: , Up: Top   [Contents][Index]

Appendix A Indexes


Next: , Previous: , Up: Indexes   [Contents][Index]

A.1 Concepts

Jump to:   C   F   L  
Index Entry  Section

C
cl-prolog2.asd: The cl-prolog2<dot>asd file
cl-prolog2/compiler-macro.lisp: The cl-prolog2/compiler-macro<dot>lisp file
cl-prolog2/interpreter.lisp: The cl-prolog2/interpreter<dot>lisp file
cl-prolog2/package.lisp: The cl-prolog2/package<dot>lisp file
cl-prolog2/printers.lisp: The cl-prolog2/printers<dot>lisp file
cl-prolog2/util.lisp: The cl-prolog2/util<dot>lisp file

F
File, Lisp, cl-prolog2.asd: The cl-prolog2<dot>asd file
File, Lisp, cl-prolog2/compiler-macro.lisp: The cl-prolog2/compiler-macro<dot>lisp file
File, Lisp, cl-prolog2/interpreter.lisp: The cl-prolog2/interpreter<dot>lisp file
File, Lisp, cl-prolog2/package.lisp: The cl-prolog2/package<dot>lisp file
File, Lisp, cl-prolog2/printers.lisp: The cl-prolog2/printers<dot>lisp file
File, Lisp, cl-prolog2/util.lisp: The cl-prolog2/util<dot>lisp file

L
Lisp File, cl-prolog2.asd: The cl-prolog2<dot>asd file
Lisp File, cl-prolog2/compiler-macro.lisp: The cl-prolog2/compiler-macro<dot>lisp file
Lisp File, cl-prolog2/interpreter.lisp: The cl-prolog2/interpreter<dot>lisp file
Lisp File, cl-prolog2/package.lisp: The cl-prolog2/package<dot>lisp file
Lisp File, cl-prolog2/printers.lisp: The cl-prolog2/printers<dot>lisp file
Lisp File, cl-prolog2/util.lisp: The cl-prolog2/util<dot>lisp file

Jump to:   C   F   L  

Next: , Previous: , Up: Indexes   [Contents][Index]

A.2 Functions

Jump to:   %  
C   F   G   M   P   R   S   W  
Index Entry  Section

%
%print-rule: Internal compiler macros
%print-rule: Internal functions

C
Compiler Macro, %print-rule: Internal compiler macros
Compiler Macro, print-commas: Internal compiler macros
Compiler Macro, print-semicolons: Internal compiler macros
Compiler Macro, print-term: Internal compiler macros
constant-fold-printer: Internal functions

F
Function, %print-rule: Internal functions
Function, constant-fold-printer: Internal functions
Function, print-commas: Internal functions
Function, print-rule: Exported functions
Function, print-semicolons: Internal functions
Function, print-sexp: Exported functions
Function, print-term: Internal functions
Function, run-command-with-debug-print: Exported functions
Function, sort-clauses: Exported functions

G
Generic Function, run-prolog: Exported generic functions

M
Macro, with-temp: Exported macros

P
print-commas: Internal compiler macros
print-commas: Internal functions
print-rule: Exported functions
print-semicolons: Internal compiler macros
print-semicolons: Internal functions
print-sexp: Exported functions
print-term: Internal compiler macros
print-term: Internal functions

R
run-command-with-debug-print: Exported functions
run-prolog: Exported generic functions

S
sort-clauses: Exported functions

W
with-temp: Exported macros

Jump to:   %  
C   F   G   M   P   R   S   W  

Next: , Previous: , Up: Indexes   [Contents][Index]

A.3 Variables

Jump to:   *  
S  
Index Entry  Section

*
*debug-prolog*: Exported special variables

S
Special Variable, *debug-prolog*: Exported special variables

Jump to:   *  
S  

Previous: , Up: Indexes   [Contents][Index]

A.4 Data types

Jump to:   C   P   S  
Index Entry  Section

C
cl-prolog2: The cl-prolog2 system
cl-prolog2: The cl-prolog2 package
cl-prolog2.impl: The cl-prolog2<dot>impl package

P
Package, cl-prolog2: The cl-prolog2 package
Package, cl-prolog2.impl: The cl-prolog2<dot>impl package

S
System, cl-prolog2: The cl-prolog2 system

Jump to:   C   P   S