Prolog as a Unifying Technology


Prolog can be a key technology for the Digital Humanities: It makes the processing of knowledge accessible to the researcher by providing a smooth transition from data representation to processing, querying, data manipulation, data-centered programming and logic-based knowledge processing. For the KBSET environment, Prolog, and in particular SWI-Prolog, is used in many roles:

This is discussed in more detail in Section 4 of KBSET – Knowledge-Based Support for Scholarly Editing and Text Processing with Declarative LaTeX Markup and a Core Written in SWI-Prolog, Jana Kittelmann and Christoph Wernhard. In: Declarative Programming and Knowledge Management (DECLARE 2019), Revised Selected Papers LNCS (LNAI), 2020, to appear.

A scholarly edition project involves, more or less explicitly, the creation of data. In accord with identifying Prolog as a potential key technology for the Digital Humanities, KBSET provides support for making these data accessible primarily as Prolog fact bases that can be accessed via Web URLs. This page gives introductory examples for using such documents.

First Steps with the Tau Prolog Sandbox

Tau Prolog is a small Prolog system that runs directly in the Web browser and is currently under active development. It permits to load documents with Prolog programs (with fact bases a special case) in a Semantic Web fashion by “consulting” (as loading is called in Prolog) them via an HTTP URL. Here is an example session.

Open the Tau Prolog Sandbox. Paste the following tiny program into the program editor on the left side:

init :-

Press Consult program (or Reconsult program, respectively).

The following query when entered in the goal field on the right (with Text “Type a Prolog goal in here and press ENTER”) now effects that the example fact base is loaded from the KBSET site directly into the Tau Prolog sandbox in your browser. (Note: Queries are displayed here always with the Prolog prompt “?- ”, but should be inserted without the prompt into Tau Prolog's goal field.)

?- init.

You can now evaluate queries against the loaded fact base by entering them into the query field. Pressing Enter repeatedly effects that alternative solutions are enumerated. Here are some examples (a brief explanation of Prolog's syntax is below).

Enumerating all instances of the letter relation:

?- letter(L, A, V).

Enumerating all attributes and values a specific letter:

?- letter('b:sv:1767-03-27', A, V).

Enumerating all letters whose date is in a given year:

?- letter(L, date_year, 1767).

Enumerating all letters whose date is in a given year along with the ID of their author:

?- letter(L, date_year, 1767), letter(L, from, P).

Enumerating all letters whose date is in a given year along with the ID and name string of their author:

?- letter(L, date_year, 1767), letter(L, from, P), defperson(P, N).

For a more complex query, it can be useful to define a dedicated predicate:

wrote_letter_in_year(Y, N) :-
        letter(L, date_year, Y),
        letter(L, from, P),
        defperson(P, N).

The predicate definition can be entered into the program editor on the left side and is activated by pressing Reconsult program. The newly defined predicate can now be used in queries. For example, to enumerate all its instances:

?- wrote_letter_in_year(Y, N).

Or to enumerate name strings of persons who wrote a letter in a given year:

?- wrote_letter_in_year(1767, N).

Prolog enumerates solutions in a certain order corresponding to its underlying evaluation regime, possibly returning the same answer multiple times. This may or may not be sufficient for applications. Standard predicates that allow to write advanced queries and to control the handling of multiple solutions include the aggregation predicates findall/3, bagof/3 and setof/3 as well as the sorting predicates sort/2, msort/2 and keysort/2.

Note on Prolog's Syntax

Symbols starting with a capital letter are considered as logical variables by Prolog. Examples are L, A, V, Letter.

Non-variable terms include atoms and numbers. An atom can contain arbitrary characters, but, if the first character is not a lowercase letter or if it contains a character that is not alphanumeric (with exception of the underscore “_”), then it needs to be placed in single quotes. Examples are date_year, from, 'b:sv:1767-03-27', and 'Letter'.

Solutions of a Prolog query are represented by variable bindings, that is, variable substitutions under which the query follows from the program, which, in our case, is the imported fact base. The Prolog interpreter enumerates these substitutions and prints them, one by one, as long as the user asks for more solutions.

Note on Web Server Configuration for Making Prolog Fact Bases Available

Tau Prolog is implemented in JavaScript and is executed in the browser on the client side. The loading of a Prolog fact base via an invocation of consult with an HTTP URL as in the example is implemented in Tau Prolog with an XMLHttpRequest object. This is subject to the Cross-Origin Resource Sharing (CORS) mechanism, which requires that the Web server that provides the Prolog document is configured to deliver it together with the response Access-Control-Origin: *.

Further Systems


Answer Set Programming, Model Computation

Prolog, as a general purpose programming language, associates a specific top-down processing model with logic formulas. Other approaches to logic programming proceed in a bottom-up fashion. Programming is understood there rather in the sense of linear programming. Many of these systems, including model computation systems, answer set programming systems and deductive database systems can process Prolog fact bases. Here are some examples:

KBSET Main Page | Christoph Wernhard