Prolog - David S Warren
Prolog - David S Warren
Series Editors
Randy Goebel, University of Alberta, Edmonton, Canada
Wolfgang Wahlster, DFKI, Berlin, Germany
Zhi-Hua Zhou, Nanjing University, Nanjing, China
The series Lecture Notes in Artificial Intelligence (LNAI) was established in 1988 as a
topical subseries of LNCS devoted to artificial intelligence.
The series publishes state-of-the-art research results at a high level. As with the LNCS
mother series, the mission of the series is to serve the international R & D community
by providing an invaluable service, mainly focused on the publication of conference and
workshop proceedings and postproceedings.
David S. Warren · Veronica Dahl · Thomas Eiter ·
Manuel V. Hermenegildo · Robert Kowalski ·
Francesca Rossi
Editors
© The Editor(s) (if applicable) and The Author(s), under exclusive license
to Springer Nature Switzerland AG 2023
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of
the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication
does not imply, even in the absence of a specific statement, that such names are exempt from the relevant
protective laws and regulations and therefore free for general use.
The publisher, the authors, and the editors are safe to assume that the advice and information in this book
are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the
editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors
or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in
published maps and institutional affiliations.
This Springer imprint is published by the registered company Springer Nature Switzerland AG
The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland
Preface
1 https://prologyear.logicprogramming.org.
2 https://prologyear.logicprogramming.org/PrologDay.html.
vi Preface
Book, progress on the education initiative, the online Prolog community, and other
related activities.
The authors present in a tutorial way some ideas from Ciao Prolog that they
believe could be useful for the future evolution of Prolog, including the use of asser-
tions with types, modes, and other properties, and other extensions to the expres-
siveness and functionality of the language. They also argue that the unique char-
acteristics of Prolog facilitated many advances in the area of combining static and
dynamic language features.
(b) Prolog as a Knowledge Representation Language the Nature and Importance
of Prolog by Michael Genesereth
In this paper Michael Genesereth takes the viewpoint that classifying Prolog
primarily as a programming language is a disservice to it, and argues for its use
as an excellent language for knowledge representation, which may be even one of
its main assets. He compares it to First-Order Logic as another popular language
for knowledge representation, and points out the versatility of Prolog for enabling a
multiplicity of uses from a single representation.
(c) Prolog: Past, Present, and Future by Gopal Gupta, Elmer Salazar, Farhad Shakerin,
Joaquín Arias, Sarat Chandra Varanasi, Kinjal Basu, Huaduo Wang, Fang Li, Serdar
Erbatur, Parth Padalkar, Abhiramon Rajasekharan, Yankai Zeng, and Manuel Carro
This paper discusses a variety of additions and extensions to Prolog made over
the years to extend its applicability. It describes a system, s(CASP), which combines
many of these features, and then proposes how it can be used to attack the problem
of general Artificial Intelligence.
(d) Writing Correct Prolog Programs by David S. Warren
Warren presents, by example, a methodology for writing Prolog programs that
proposes the primary use of bottom-up thinking. He argues that this methodology
will more likely result in correct programs due to the fact that most proofs of logical
correctness involve bottom-up thinking.
(e) Demonstrating Multiple Prolog Programming Techniques Through a Single
Operation by Nick Bassiliades, Ilias Sakellariou and Petros Kefalas
Bassiliades, Sakellariou and Kefalas illustrate the power of the full Prolog lan-
guage, by showing alternative implementations of a single list operation, thus demon-
strating a good number of Prolog programming aspects and techniques, and some
related issues such as efficiency or readability of the code.
(f) A Better Logical Semantics for Prolog by David S. Warren and Marc Denecker
This paper proposes that positive Prolog programs can be productively under-
stood as inductively defining predicates. It informally develops a compositional
semantics that gives a precise meaning to components of full programs.
(g) The Janus System: A Bridge to New Prolog Applications by Carl Andersen and
Theresa Swift
This paper presents the Janus system, an integration of Prolog and Python, with
the goal of making Prolog more accessible to large-scale industrial applications.
Janus makes the large number of libraries present in Python available within Prolog.
The paper also discusses the use of the Janus system for several large, real-world
applications.
3. Teaching Prolog
These papers explore various ideas and experiences of teaching Prolog programming.
(a) Some Thoughts on How to Teach Prolog by Manuel V. Hermenegildo, Jose F.
Morales and Pedro Lopez-Garcia
viii Preface
These two papers discuss technology that has been developed for help in teaching
Prolog.
(a) Teaching Prolog with Active Logic Documents by José Morales, Salvador Abreu,
Daniela Ferreiro and Manuel V. Hermenegildo
Morales, Abreu, Ferreiro and Hermenegildo present their Active Logic Doc-
uments approach and tools, for easily developing Prolog teaching materials with
Preface ix
embedded runnable code and interactive components. In this approach, the materi-
als are self-contained, can be developed with standard tools, and run locally on the
student’s browser, not relying on a server infrastructure or notebook facility. They
argue that this offers advantages in scalability, ease of maintenance, security, etc.
(b) Simply Logical—The First Three Decades by Peter Flach, Kacper Sokol and Jan
Wielemaker
This paper traces the evolution of the Prolog textbook “Simply Logical – Intel-
ligent Reasoning by Example” from print (with a 3.5 diskette containing runnable
programs) published in 1994 to the fully interactive online edition based on SWI Pro-
log’s SWISH interface available today. The authors describe the philosophy behind
the original book, along with how contemporary web programming eventually
enabled a versatile authoring toolkit that underlies the book’s interactive edition.
5. Prolog-Based Languages and Systems
These papers describe new languages firmly based on Prolog which show future
directions for logic programming.
(a) Dynamic Logic Programming by Michael Genesereth
Genesereth describes an elegant extension to a subset of Prolog aimed at support-
ing the representation of knowledge about dynamic worlds. The resulting language
allows the definition of operators that update the state of the extensional database
(by moving between worlds which are such extensional database states), as well as
reasoning about the evolution of such worlds.
(b) Combining Logic Programming and Imperative Programming in LPS by Robert
Kowalski, Fariba Sadri, Miguel Calejo and Jacinto Dávila Quintero
The language LPS (Logic Production Systems) combines the logic programming
and imperative programming notions of computing, by using logic programs to
represent an agent’s beliefs, and using reactive rules and constraints to represent
the agent’s goals. An agent program in LPS computes by generating actions, to
satisfy its goals in a model that is defined by its beliefs extended by its actions. The
paper describes a Prolog implementation of LPS, which displays the model that is
computed, either as a timeline or as an animation of the destructively changing state
of computation.
(c) Ergo: A Quest for Declarativity in Logic Programming by Benjamin Grosof,
Michael Kifer, Theresa Swift, Paul Fodor and Janine Bloomfield
This paper describes the Ergo system, a declarative, logic-based, object-oriented
system based on the well-founded semantics that includes defeasible rules, fully
logical updates, and explanations. Applications that represent tax laws and banking
regulations are presented.
6. Prolog Applications: Finalists for the Colmerauer Prize
These next five papers describe the applications that were the finalists for the 2022
Colmerauer Prize. The first one, by Michael Leuschel on ProB, was the winner of the
prize.
(a) ProB: Harnessing the Power of Prolog to Bring Formal Models and Mathemat-
ics to Life by Michael Leuschel
x Preface
Leuschel presents the ProB system, an animator, model checker and constraint
solver for high-level formal models, implemented in Prolog, making significant use
of constraints. It has been developed for over 20 years and has been used extensively
in both academic and industrial applications, e.g., by several companies (Siemens,
Alstom, ClearSy, Thales) to validate train system configurations worldwide. ProB
was the winner of the first edition of the Colmerauer Prize.
(b) Pacioli: A PROLOG System for Financial Report Processing by Miguel Calejo
and Charles Hoffman
Pacioli is a logic and rules engine toolkit implemented in Prolog, which vali-
dates financial information reported by public companies to regulators worldwide
using a standard, rich structured, data format, the “Extensible Business Reporting
Language”, or XBRL. At the time of writing, a dozen Pacioli instances, operated by
different entities around the world, have validated thousands of financial reports in
different jurisdictions. The authors argue that the wide range of capabilities needed
for Pacioli would be hard to implement in a mainstream language such as Python or
Javascript.
(c) Logic Model Processing by Pierre Dissaux
This paper describes the application of Prolog to Model Driven Engineering, that
refers to the use of software models to standardize and ease industrial engineering
processes. It includes the description of the methodology, the implementation, the
tools, and the Prolog libraries that have been developed over many years and deployed
worldwide for industrial usages, with examples of its practical use and its most recent
developments.
(d) Symbium: Using Logic Programming to Streamline Citizen-to-Government
Interactions by Tristan Krueger, Abhijeet Mohapatra and Michael Genesereth
This paper describes an interesting, useful real world application, offered by the
US company Symbium, that helps homeowners, architects, and contractors comply
with the regulatory aspects of residential construction. Logic Programming is used
to facilitate and, in some cases, automate regulatory processes involving permits,
inspections, and rebates. Interestingly, uses of this application in interaction with
municipalities have uncovered contradictions and omissions in the law, suggesting
that further applications in this regard might also bear good fruit.
(e) PROLEG: Practical Legal Reasoning System by Ken Satoh
The PROLEG system is possibly the largest legal rule base in the world, having
been used since 2009 to build a rule base of approximately 2500 rules and exceptions
consisting of civil code and supreme court case rules in Japan. In this paper, Ken
Satoh presents the PROLEG system, which is based on Prolog, but which represents
rules and exceptions without negation as failure. He argues that the PROLEG repre-
sentation of rules and exceptions corresponds better than negation as failure to the
way that lawyers reason with such concepts as “burden of proof”.
7. Contributed Prolog Applications
These papers describe some additional, contributed applications developed using the
Prolog language. They illustrate further the range of applications for which Prolog is
well-suited.
(a) Logical English for Law and Education by Robert Kowalski, Jacinto Dávila
Quintero, Galileo Sartor and Miguel Calejo
Preface xi
Background
Introduction to Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
David S. Warren
Teaching Prolog
David S. Warren(B)
1 What is Prolog?
Prolog is a logical language for making true statements about a world, whose
statements can be interpreted as a program and thus evaluated to conclude an
answer to a question. Functional programs are understood as defining functions
whereas Prolog programs are generally understood as defining relations.
Various family relationships can easily be defined in Prolog. For example,
a grandparent is the parent of a parent. We can define a child of relation
by providing a set of facts giving child-parent pairs, for example, some recent
relationships of the House of Windsor, as follows:
childOf(charles,elizabeth).
childOf(william,charles).
childOf(harry,charles).
childOf(george,william).
childOf(charlotte,william).
childOf(louis,william).
childOf(archie,harry).
childOf(lilibet,harry).
And we can define the grandparent relation, containing person-grandparent
pairs, using the childOf definition by the rule:
hasGrandparent(X,GPofX) :- childOf(X,ParX), childOf(ParX,GPofX).
This rule states that X has grandparent GPofX if X is a child of ParX and ParX is
a child of GPofX. Prolog uses the token :- to mean if, and the comma to mean
c The Author(s), under exclusive license to Springer Nature Switzerland AG 2023
D. S. Warren et al. (Eds.): Prolog: The Next 50 Years, LNAI 13900, pp. 3–19, 2023.
https://doi.org/10.1007/978-3-031-35254-6_1
4 D. S. Warren
and. Symbols starting with uppercase letters are variables, which can take on
any value, and those starting with lowercase letters are constants. The atomic
formula before the :- is known as the head of the rule; the sequence of formulas
following that token form the body of the rule. For every assignment of values to
the variables of a rule, if its body is true, then its head is true.
If we consistently substitute constants for variables into a rule, we obtain an
instance of this rule. For example, substituting harry for X, charles for ParX
and elizabeth for GPofX, we get the rule instance:
hasGrandparent(harry,elizabeth) :-
childOf(harry,charles), childOf(charles,elizabeth).
Since our database of childOf facts includes the two facts in the body of the rule,
then we (and Prolog) can conclude that hasGrandparent(harry,elizabeth) is
true, i.e., that harry has a grandparent elizabeth. Similarly we can conclude
that archie has grandparent charles, george has grandparent charles, and a
number of other grandparent pairs.
Prolog accepts such rules and facts and then answers certain queries about
them. For example, given the rules and facts above for hasGrandparent, we can
ask Prolog who are the grandparents of Lilibet:
?- hasGrandparent(lilibet,GP).
GP = charles;
no
Here (after loading the childOf facts and the hasGrandparent rule to Pro-
log’s memory) we have asked at Prolog’s prompt “?-” for the variable GP to
be given values that make the hasGrandparent(lilibet,GP) atomic formula
true. Prolog responds that such a value is charles, to which we respond with a
“;”, requesting another answer. And Prolog responds with “no” meaning that it
failed to find any (more) answers. If a query has no answers at all, its execution is
said to fail and “no” printed. We could ask for all of Elizabeth’s grandchildren
with the query: hasGrandparent(GC,elizabeth).
We can then use hasGrandparrent to define hasGreatGrandparent with the
rule:
hasGreatGrandparent(X,GGPofX) :-
hasGrandparent(X,GPofX), childOf(GPofX,GGPofX).
hasAncestor(X,AncX) :- childOf(X,AncX).
hasAncestor(X,AncX) :- childOf(X,ParX), hasAncestor(ParX,AncX).
Introduction to Prolog 5
This relation requires two rules for its definition. The first says X has ancestor
AncX if X is a child of AncX. The second says X has ancestor AncX if X is a child of
ParX and ParX has ancestor AncX. I.e., a parent is an ancestor, and an ancestor
of a parent is an ancestor. With this definition, Prolog will infer many ancestors
from our definition of childOf, including hasAncestor(charles,elizabeth)
and hasAncestor(lilibet,elizabeth).
In addition to defining relations over constants, Prolog can define relations
over complex data structures, for example lists. A list is a sequence of terms
(constants or lists)1 , and is written within square brackets with its elements
separated by commas. For example, [charles,harry,charlotte] is a list, and
so is [], the empty list. The head of a (nonempty) list is the first element of
the list; the tail of a (nonempty) list is the list that remains when the head
is removed. We can write a pattern [H|T] to match a list, in which case H is
matched to the head of the list and T is matched to the tail. So for example,
matching [H|T] to the list [charles,harry,charlotte] results in variable H
getting the value charles and T getting the value [harry,charlotte]. With
this understanding, we can now write a relation of pairs of elements and lists for
which the element appears in the list, which we call member:
member(E,[E|L]).
member(E,[X|L]) :- member(E,L).
The first clause says that a item E is a member of any list that has E as head.
The second clause says that E is a member of a list, if it’s a member of the tail
of the list.
2 Procedural Interpretation
Now we turn to considering how Prolog actually carries out the necessary infer-
encing to be able to answer queries using a process formally called resolution.
Each clause in a Prolog program can be understood as defining a procedure:
the head of a rule provides the name of the procedure and the body of the rule
provides the procedures to call, in order, to carry out the execution of the head
procedure. For example, the rule:
does not have a value. I.e., the call to hasGrandparent gives a person and asks
for a grandparent of that person. Then childOf is called passing the indicated
arguments. The variable P will not have a value when the first childOf is called,
but will be assigned a value during the execution of the childOf procedure, say
harry, so when P is passed to the second call to childOf as its first argument, it
will have that value. This call to childOf will bind variable GP, say to charles,
which will be returned as the value of the second parameter to the original
hasGrandparent call. In this way every rule can be understood as defining a
procedure whose execution will take some variables and further bind them (i.e.,
give them values) as it executes the procedures in the body of the rule in order.
Facts, like those for childOf just bind variables in their call and directly return.
Thus, if we assume only one rule per relation, we can think of a call to a relation
definition as being carried out by a machine that carries out that sequence of
procedure invocations and returns.
the order their clauses appear in the program) and then executes the top (first)
one. When a machine fails, the machine on the top of the stack is removed and
executed. In this way, Prolog uses a depth-first, backtracking (to restore the
state to that of the top machine on the stack) strategy.
The metaphor of “deterministic machines” is used here for didactic purposes.
In practice Prolog systems implement this strategy extremely efficiently, using
techniques very similar to traditional procedural languages for forward machine
execution, and essentially constant time backtracking techniques for restoring a
previous state.
Alternative strategies are possible, but this depth-first strategy is generally
the most efficient, and it is the one adopted, at least by default, by Prolog
engines.
3 Modes
Procedures, as noted, can be called with some arguments having values and
with other arguments being variables without values. Variables that have values
are said to be bound2 , and variables that don’t (yet) have values are said to
be unbound or free. In our first example, evaluation for the hasGrandparent
procedure we assumed the first argument to hasGrandparent was bound to
2
Note this is a different meaning for a bound variable than in first-order logic.
8 D. S. Warren
lilibet and the second argument was free. This particular “value pattern”
(or “instantiation pattern”) is known as the mode (+,-). A mode is a sequence
of the symbols such as: +, -, and ?, and indicates the binding pattern of a
sequence of arguments, for example, + meaning bound, - meaning free, and ?
meaning either (or other). In our first hasGrandparent example, finding the
grandparents of Lilibet, the mode for hasGrandparent goal was (+,-), and the
mode for (both calls to) childOf was also (+,-). The modes at success of all three
of those calls was (+,+). In the example to find the grandchildren of Elizabeth,
the corresponding modes were (-,+) for hasGrandparent, then (-,-) for the first
childOf subquery, and (+,+) for the second.
Because variables get values by unification, a generalized matching opera-
tion, many procedures can be called in multiple different modes, as we see for
hasGrandparent and childOf. Indeed the hasGrandparent procedure may also
be called in (+,+) modes, in which case Prolog will check whether the names
provided indeed stand in the grandparent relation. It may be called in (-,-) mode,
in which case Prolog will generate all pairs of names in the grandparent relation.
And as we saw before, member can be called in modes (+,+) to check membership
and in mode (-,+) to generate all members.
It is useful to know the modes of calls (and returns) since many Prolog
predicates can be called effectively only in certain modes. For example, in other
modes the computation may loop infinitely or may generate infinitely many
answers, as would member called in (+,-) mofe, and so calls in such modes to
those procedures should generally be avoided.
Sometimes modes are combined with type information, so that, e.g., mode
(-,+list) for member/2 means that it is called with the second argument bound
to a list.
Modes are used in most Prolog systems as program documentation, and as
an aid for discussing the uses of a predicate, as we do here. There are however
some Prolog systems that can check the modes and other properties declared
for a given predicate and detect if such modes and properties are violated. This
can be done either at compile time (when loading the program) or at run time
(when executing a query).3
4 Bottom-Up Evaluation
The procedural interpretation of Prolog programs presented before is known
as top-down evaluation, since it starts at the top with a query (or goal) and
computes down to the facts. Prolog programs may also be evaluated bottom up,
starting with the facts and generating implied atoms.
For example, consider the hasGrandparent and childOf definitions above.
We can start by seeing that we know that all the facts are true, so we can
initialize our set of atoms we know to contain these facts:
3
See Chapter “Types, modes and so much more – the Prolog way,” in this volume.
Introduction to Prolog 9
childOf(charles,elizabeth). childOf(william,charles).
childOf(harry,charles). childOf(george,william).
childOf(charlotte,william). childOf(louis,william).
childOf(archie,harry). childOf(lilibet,harry).
Next we can look at each rule, here
hasGrandparent(X,GPofX) :- childOf(X,ParX), childOf(ParX,GPofX)
and see what instances of the body of the rule are already in our set of known
atoms. And we add them to our set. So here, we see that since
childOf(william,charles) and childOf(charles,elizabeth) are both in
our known set, and these facts make up an instance of the body of our rule,
then we can add hasGrandparent(william,elizabeth), the rule’s correspond-
ing head instance, to our known set. And there are many more we can add, such
as hasGrandparent(archie,charles) because we already have
childOf(archie,harry) and childOf(harry,charles), and
hasGrandparent(harry,elizabeth) since we have childOf(harry,charles)
and childOf(charles,elizabeth), and so on. For this definition, we need only
two steps to get all the known atoms: one for the known childOf facts, and
then one more for all the known hasGrandparent facts. After two rounds,
we will never get anything new, so we can stop. If we include our rule for
hasGreatGrandparent, then we would have needed three rounds in our exam-
ple. This is because the first two rounds would be identical to above, and then
hasGreatGrandparent facts can be added only at the third round.
This bottom-up computation, starting with facts and then iteratively adding
more facts by using the rule instances, is also known as forward reasoning. For
some rule sets it may take many rounds to infer all facts that can be known;
in fact, there may be infinitely many rounds, and so this computation may not
terminate. Also note that bottom-up computation computes, for example, all
hasGrandparent facts, not just those, say related to being grandparents of Lili-
bet, as top-down evaluation can. On the other hand, for rules that contain only
constants (i.e., no lists), bottom up is guaranteed to terminate, whereas (simple)
top-down is not. So, bottom-up evaluation is more used in database contexts,
where rules contain only constants (and variables, of course.) Such Prolog pro-
grams, those with only constants, are called Datalog programs, and they are
traditionally implemented using bottom-up methods. (But see the discussion of
tabling below.)
We note that, in theory, top-down and bottom-up evaluation of a query pro-
duce the same results, when infinite loops in each are considered appropriately.
In practical computations of a given query, however, they may encounter infi-
nite loops in different ways. So while they are logically equivalent, they are not
computationally equivalent.
male(walter). married(walter).
male(bill). married(sarah).
The intuitive meaning of this is clear. We define bachelor as a person who is male
and is not married. When the subgoal not married(p), for some constant p, is
encountered in procedural evaluation, the married(p) subgoal is called and if it
succeeds, then the not married(p) subgoal fails, and if it fails the not subgoal
succeeds. And we can see Prolog’s evaluation:
?- bachelor(Person).
Person = bill;
no
which gives the expected answer that bill is a bachelor, but walter, who is
married, is not.
It turns out that, for this treatment of negative subgoals, the mode of the
subgoal is important; it must not have variables in it. In our example the first
subgoal male(P) in the body of the bachelor definition will always bind P to a
value, so married(P) will always be called in mode (+). Notice that if the body
literals in bachelor were reversed, then our query to bachelor(Person) would
invoke the subgoal married(Person) (with Person still a variable), and this
would succeed, since there are two married persons, so not married(Person)
would fail, as would the original query; this is clearly is wrong. The moral is that
the mode of a negative subgoal when called is critical for Prolog’s procedural
interpretation of not to be intuitively correct. If a negative subgoal is called with
a variable, the computation is said to flounder.
This issue arises when a subgoal depends, in some way, on its own negation. For
example, shaves(barber,barber) depends directly on
not shaves(barber,barber). There have been many suggested approaches for
resolving this issue. We can simply avoid this possibility by allowing only pro-
grams that are “stratified.” A program is stratified if we can assign each predicate
to a stratum (a natural number) where for every rule, (1) each predicate of a
positive body subgoal has stratum the same or lower than the stratum of the
head predicate, and (2) each predicate of a negative body subgoal has a stratum
that is strictly less than the stratum of the head predicate. This ensures that no
subgoal can depend on its own negation. Stratified programs have their expected
meanings.
But what about non-stratified programs? After much work and discussion, the
Prolog community has (mostly) come to the conclusion that there are two dis-
tinct, and reasonable, ways to give meanings to non-stratified programs. They go
by the names of the Well-Founded Semantics (WFS) and the Stable Model
Semantics (SMS). We note that both semantics give the same meaning to strat-
ified programs; they differ only in their treatment of non-stratified programs. We
give only a cursory introduction, by example, to these two approaches.
Consider our shaves example above. The WFS uses a 3-valued logic with
truth values of true, false, and undefined. It gives the value true to
shaves(barber,baker), the value false to shaves(barber,butcher) and the
value undefined to shaves(barber,barber). The WFS always associates a
unique (3-vaued) model to a program. The SMS uses multiple 2-valued mod-
els to give meanings to (non-stratified) programs. The shaves program has no
such stable model, so that program is said to be inconsistent in SMS.
Consider another unstratified program, describing how the “Programming
Languages” course is taught in our department:
6 Tabling
Tabling is an alternative evaluation strategy for Prolog, based on the idea of
saving calls and results from procedure invocations in a table, and then using
those results for future identical (or subsumed) calls. Implemented correctly, it
is easy to see that with tabling Prolog programs that use only constants and
variables (no lists or other data structures), the so-called Datalog programs, will
always terminate. Such programs have finitely many possible calls and finitely
many possible returns, so if none is done more than once, their processing must
eventually terminate.
This has far-reaching consequences for Prolog programming. Consider for
example, the rules that define transitive closure for an arbitrary directed graph,
edge:
transclose(X,Y) :- edge(X,Y).
transclose(X,Y) :- transclose(X,Z), edge(Z,Y).
4
See chapter “Ergo: A Quest for Declarativity in Logic Programming” for more on
unstratified programs, and chapter “Prolog: Past, Present, and Future” for more on
ASP and SMS.
Introduction to Prolog 13
answer currently in the table and suspends on that table entry. Whenever a new
answer shows up for an entry, the suspended machine forks off another machine
to continue with that answer. The implementation is complicated by the fact
that full asynchronicity of returning answers to suspended calls is indeed required
because there may be two distinct calls with each one alternately producing a
new answer for the other to consume.
Tabling does, of course, take time, to add entries to a table, and space, to
store it. In many, but not all, cases that time and space is more than recovered by
the savings from redundant computation that is avoided. For this reason, tabling
is generally optional and can be turned on, or off, for any given predicate.
A number of Prolog systems now include the tabling options. Some of them
use tabling to implement the WFS for non-stratified programs. They main-
tain information to distinguish between successful derivations that indicate true
answers and those that indicate undefined answers.
7 Operators
Prolog has a flexible syntax. We’ve mentioned lists, but Prolog actually supports
a more general structured data type, called the term. (A list is just a special
kind of term.) A term is a labeled, ordered, tree. The fundamental type is the
labeled record. A record is written as, e.g., employee(david,2020,programmer)
which indicates a record with label employee and three fields, the first and last
populated by string constants and the second by an integer constant. Records
may be nested. For example, cons(a,cons(b,cons(c,[]))) is a nested record
structure, made up of binary cons records, and the constant symbol []. Another
example is +(1,*(2,3)) where we have two binary records, one a +-record and
one a *-record nested in the first. Such record structures are heavily used in
Prolog programming.
This last record structure can also be seen as representing an arithmetic
expression. In fact, it is used in Prolog (in some contexts) for precisely this. But
this record notation for expressions is not what is normally used in mathematics.
One would rather represent it as 1 + 2 * 3, in what is known as infix notation.
Here the record symbols + and * are infix operator symbols and thus are placed
between their operands. Prolog supports such infix operators for binary record
symbols, as well as prefix and postfix operators for unary record symbols. For
example, - is both an infix operator (normally interpreted as subtraction) and
a prefix operator (normally interpreted as the additive inverse). While the basic
nested record notation, known as canonical form, is unambiguous, infix notation
can be ambiguous. E.g., does 1+2*3 mean +(1,*(2,3)) or *(+(1,2),3))? The
convention we learn early on is that it means the first, but the Prolog parser
must be told that. This is determined by the relative precedence of the operators.
In fact, a Prolog rule itself is actually a record structure. It is clear that sub-
goals can be directly seen as record structures. And the conditional symbol “:-”
and the conjunction symbol “,” of Prolog rules are infix operators (with appro-
priate precedences) so that a rule is read as a set of nested records. For example,
the rule p :- q,r. is in fact a record structure of the form :-(p,‘,’(q,e)).
14 D. S. Warren
8 Meta-interpretation
One strength of Prolog is that, as we saw, its programs are expressible as simple
data structures in the language. This allows Prolog to easily manipulate its own
programs.
A subgoal looks exactly the same as a record structure. Prolog has a builtin
predicate, named call, that allows a program to treat a record structure as a
subgoal. Consider the situation in which the Prolog programmer wishes to make
it easier for non-experts to invoke specific complex Prolog queries by simply
entering the query name. The programmer creates a relation that associates a
name with a query. E.g.:
queryName(allGrandparents, hasGrandparent(X,Y)).
queryName(memberTest, member(X,[1,2,3,4])).
query(Name,Res) :- queryName(Name,Res),call(Res).
?- query(allGrandparents,Res).
Res = hasGrandparent(william,elizabeth);
Res = hasGrandparent(harry,elizabeth);
Res = hasGrandparent(george,charles);
Res = hasGrandparent(charlotte,charles)
yes
Prolog prints out the query with the variables instantiated to child-grandparent
pairs. Since “;” was not entered after the fourth answer, Prolog stops generating
answers (and just prints “yes”).
This simple example shows how Prolog can build a goal as a record structure
during execution and then invoke it. This call facility supports higher-order
programming in Prolog.
Another implication of the easy reflection of Prolog programs as data struc-
tures is that one can write a “meta-interpreter” in Prolog. A meta-interpreter
is a program that takes another program and goal as input and evaluates that
Introduction to Prolog 15
query with respect to the program. For example, we could store the clauses of a
program as facts of a binary predicate we call rule, with the head as the first
argument and the body as the second. For example,
rule(member(X,[X|L]),true).
rule(member(X,[Y|L]),member(X,L)).
is our member program from back in the first section. Now we can define a
predicate, say, interp that takes any subgoal and determine its instances that
follow from the rules defined in rule:
interp(true).
interp((X,Y)) :- interp(X), interp(Y).
interp(X) :- rule(X,B), interp(B).
The first rule (a fact) says that if the query is true we should just succeed. The
second rule says that if we have a query that is a conjunction, we just need to
interpret each conjunct in turn. The unusual double parentheses in the head of
that rule are to indicate that interp takes a single argument, and that argument
is a record structure with two subfields that has the record name of “,” (comma)
and is represented in infix notation (with the comma between the two operands).
The final rule says what to do if the query is a subgoal: see if there is a rule with
the query as its head, and if so, interpret the body of that rule. (Actually, do
this, nondeterministically, for every rule that has a matching head.)
And evaluating interp goals, we see:
?- interp(member(0,[3,2,1])).
no
?- interp(member(X,[3,2,1]).
X = 3;
X = 2;
X = 1;
no
These three clauses are all that are needed to execute many Prolog programs,
e.g., almost all we have seen so far. (What rule should be added to handle
negative subgoals?) This simplicity never ceases to amaze. Compare this with
the complexity of a metainterpreter for Java written in Java, for example.
The importance of this simple metainterpreter is not that it can execute
Prolog programs; we have the Prolog engine itself to do that. The importance is
that we can add arguments to this metainterpreter to carry along debugging or
other analysis information; we can add logging subgoals (built-ins of Prolog) to
provide a trace; we can modify it to perform different kinds of evaluation.
For example, we can write a Prolog meta-interpreter that will only invoke
procedures up to some fixed depth, and thus will always terminate, albeit some-
times incorrectly:
16 D. S. Warren
interp(true,Depth,Max).
interp((X,Y),Depth,Max) :-
interp(X,Depth,Max), interp(Y,Depth,Max).
interp(X,Depth,Max) :-
Depth < Max,
NewDepth is Depth + 1,
rule(X,B),
interp(B,NewDepth,Max).
Here Max is the maximum predicate nesting depth to be searched; Depth is the
current nesting depth. The first subgoal of the final rule is a Prolog builtin,
the infix operator “<”, that compares the two integers, and so only succeeds
if the limit is not reached. The second subgoal, using builtin infix operator
“is”, computes the new depth when a rule is used. So, for example, a query
of interp(member(5,[1,2,3,4,5]),0,3) will fail since finding 5 in the list
requires making more than 4 nested subgoal invocations.
Of course we can extend this interpreter to indicate “depth limit exceeded”
instead of simply failing. Similar techniques can be used to implement other
forms of search through the tree of executing virtual deterministic machines.
For example, breadth-first search, or iterative deepening strategies can be imple-
mented when useful in educational and other contexts.
This is a grammar that represents a small fragment of English. The syntax we’ve
used is similar to BNF, with nonterminals represented by constant symbols,
and terminals represented by the symbol surrounded by square brackets. We’ve
separated symbols in the right-hand-sides of rules by commas, and alternate
right-hand-sides by semicolons. The language of this grammar includes such
sentences as “sarah walks” and “the man loves a woman.”
We can represent this grammar in Prolog by introducing for each non-
terminal a Prolog predicate with two arguments; the first argument is a list
Introduction to Prolog 17
of tokens and the second a tail of the first list that remains after removing a
sequence of tokens generated by the non-terminal. So, for example, the predi-
cate noun phrase should take the list [‘a’,‘man’,‘walks’] and, removing the
noun phrase, “a man”, return the list [‘walks’]. The following Prolog program
represents this grammar:
sentence(S0,S) :- subject(S0,S1), verb_phrase(S1,S).
subject(S0,S) :- noun_phrase(S0,S).
noun_phrase(S0,S) :- det(S0,S1), noun(S1,S) ;
S0 = [john|S] ; S0 = [sarah|S].
det(S0,S) :- S0 = [ a |S] ; S0 = [the|S].
noun(S0,S) :- S0 = [woman|S] ; S0 = [man|S].
verb_phrase(S0,S) :- transitive_verb(S0,S1), noun_phrase(S1,S) ;
intransitive_verb(S0,S).
transitive_verb(S0,S) :- S0 = [ loves |S].
intransitive_verb(S0,S) :- S0 = [ walks |S].
Here we have used the Prolog “or” operator, represented by the semi-colon (;),
which we haven’t seen before. The translation of the context-free grammar to a
Prolog program is clear. Each rule has a clear meaning: if the right-hand-side
non-terminals (and terminals) of a grammar rule generate a sequence of strings,
then their concatenation is a string generated by the left-hand side of the rule.
We can query these predicates to recognize sentences in the grammar.
?- sentence([the,man,loves,sarah],[]).
yes
?- sentence([the,man,loves,walks],[]).
no
as expected. Looking at the execution of this program, we see that Prolog is
carrying out a recursive descent recognition algorithm. We note that if all the
predicates are tabled, then Prolog would carry out a variation of the efficient
Earley recognition algorithm.
We now point out that the syntax for the context-free grammar we used
to introduce it above is actually accepted by Prolog and converted into the
Prolog program we just presented. This is done automatically by the Prolog
reader using a built-in Prolog facility called term expansion which supports very
powerful macro capabilities5 . Grammars in this syntax are called Definite Clause
Grammars or DCGs. So, DCG syntax uses the “-->” symbol instead of the “:-”
symbol to separate head and body of rules. It requires terminal symbols of the
grammar to be placed in lists. Then the DCG processing of Prolog translates such
rules to predicate definitions by adding two extra arguments to each nonterminal
symbol, and threading the variables through the body subgoals, including the
terminal lists, as illustrated above.
5
Term expansion and other related facilities such as “attributed variables” have been
used to add many other syntactic and semantic extensions to Prolog, such as func-
tional programming and constraints. We discuss the latter in the following section.
18 D. S. Warren
10 Constraints
As we have seen, Prolog has basic arithmetic builtin predicates, such as is for
evaluating expressions, e.g., X is Y + 1. This builtin requires that the expres-
sion has no variables, i.e., in this case that Y must have a value. But it may
be the case that Y doesn’t have a value at the point of evaluation, but will be
given a value by the evaluation of a later subgoal. So, waiting until Y has its
value would allow the builtin is to correctly evaluate to give X a value. For this
situation, many Prologs have a wait operation, which allows the programmer
to specify that a particular subgoal, say X is Y + 1, should be delayed until
certain variables, say Y, are given values, at which point it will be evaluated.
The wait facility allows more flexible evaluation strategies, or what gets done
when, driven by various modes.
But there are cases for which waiting for variables to get values may still not
allow some seemingly reasonable programs to be evaluated. Consider a simple
example of a compound goal: X is Y + 2, Y is X * 3, where neither X nor Y
have values. But we can solve these two simple equations in two unknowns and
determine that X must be −1 and Y must be −3. Many Prolog systems support
such constraint solving.
The general idea is for the Prolog system to accumulate the set of con-
straints imposed by evaluated constraint subgoals, and then to solve those con-
straints, using some solver methodology, to bind variables to values that satisfy
the constraints. In many Prolog implementations the constraints are stored in
the attributes of attributed variables and incrementally solved as soon as enough
of the variables involved are known. This improves efficiency by both detecting
inconsistent constraints early and binding variables to constants as soon as pos-
sible, thus avoiding unnecessary search. For example, if the constraints are linear
constraints over the real numbers, one could use an incremental version of the
method of Gaussian elimination to solve them.
This constraint framework is known as Constraint Logic Programming
(CLP). CLP systems may support constraints over a variety of domains, such as
real numbers (as we’ve seen here), rational numbers, booleans, or finite domains.
6
See chapter “Introducing Prolog in Language-Informed Ways” for more uses of
DCG’s.
Introduction to Prolog 19
Each domain will have a specific solver that solves sets of constraints of some
specified forms. And there may be multiple solvers for some specific domains,
depending on the form of the constraints. There has been significant research into
CLP, and many powerful constraint systems have been developed. For example,
see the chapter “ProB: Harnessing the Power of Prolog to Bring Formal Models
and Mathematics to Life.”
11 Conclusion
We hope that this brief introduction to Prolog has provided some insight into
Prolog’s capabilities. But we have only scratched the surface of what Prolog
can do. Fifty years of research and language extensions have created extremely
powerful systems.7 Perhaps now you can begin to see that Prolog is a logic
language, and how Prolog can be understood as a “declarative language” in
which programmers describe what they want, and not so much how to compute
it; yet also write very efficient recursive algorithms if desired. The rest of this
book will further explore what has been done, and what can be done, with Prolog
programs.
7
See Fifty Years of Prolog and Beyond by Korner, Leuschel, Barbosa, Santos Costa,
Dahl, Hermenegildo, Morales, Wielemaker, Diaz, and Abreu in TPLP 22(6).
About Prolog, Present and Future
Types, Modes and so Much More
– The Prolog Way
Prolog is a dynamically-typed language and this aspect, combined with the intrin-
sic power of the language, has arguably contributed to its continued relevance and
use in many applications. In fact, the environment in which much software is devel-
oped nowadays, aligns well with the classical arguments for dynamic languages,
and many of the currently most popular languages, such as Python, JavaScript,
Ruby, etc. (with Scheme and Prolog also in this class) are dynamic.
At the same time, detecting errors as early as possible at compile time, and
inferring properties required to optimize and parallelize programs are clearly
important issues in real-world applications, and thus, strong arguments can also
be made for static languages. For example, statically-typed logic and functional
languages (such as, e.g., Mercury [37] or Haskell [17]) impose strong type-related
Fig. 1. With no entry information, the system warns that it cannot verify that the
call to =</2 will not generate a run-time error.
requirements such as that all types (and, when relevant, modes) have to be
defined explicitly or that all procedures have to be well-typed and well-moded.
An important argument supporting this approach is that types clarify interfaces
and meanings and facilitate programming in the large by making large programs
more maintainable and better documented. Also, the compiler can use the static
information to generate more specialized code, which can be better in several
ways (e.g., performance-wise).
In the design of Ciao Prolog we certainly had the latter arguments in mind,
but we also wanted to retain the usefulness of standard Prolog for highly dynamic
scenarios, programming in the small, prototyping, developing simple scripts, or
simply for experimenting with the solution to a problem. We felt that strong typ-
ing and other related restrictions of statically-typed logic languages can some-
times get in the way in these contexts.
The solution we came up with –the Ciao assertions model – involves the
combination of a rich assertion language, allowing a very general class of (pos-
sibly undecidable) properties, and a novel methodology for dealing with such
assertions [3,13,14,29,30], based on making a best effort to infer and check
assertions statically, using rigorous static analysis tools based on safe approx-
imations, in particular via abstract interpretation [7]. This implies accepting
that complete verification or error detection may not always be possible and
run-time checks may be needed. This approach allows dealing in a uniform way
with a wide variety of properties which includes types [33,41], but also, e.g., rich
modes [24,25], determinacy [19], non-failure [4,9], sharing/aliasing, term linear-
ity, cost [20,26,35], etc., while at the same time allowing assertions to be optional.
The Ciao model and language design also allows for a smooth integration with
testing [21]. Moreover, as (parts of) tests that can be verified at compile time
are eliminated, some tests can be passed without ever running them. Finally, the
model supports naturally assertion-based test case generation. In the following
we illustrate these aspects of the model through examples run on the system.1
1
The examples are runnable in the Ciao playground ; they have been developed with
version 1.22 of the system. Screenshots are from the Ciao Prolog Emacs interface.
Types, Modes and So Much More - The Prolog Way 25
Fig. 2. Hovering over the clause the system shows a popup saying that it cannot verify
the assertions for =</2 (present in the library!).
Fig. 3. Adding information on how the exported predicate should be called the system
can infer that =</2 will be called correctly, and no warnings are flagged.
also be called with arbitrary terms and thus run-time errors are possible, since
=</2 and >/2 require their arguments to be bound to arithmetic expressions
when called. Even though there are no assertions in the program itself, the
system is able to warn that it cannot verify that the calls to =</2 and >/2
will not generate a run-time error (note >> symbol and code underlining in
orange). This is the result of a modular global analysis and a comparison of the
information inferred for the program points before the calls to =</2 and >/2
with the assertions that express the calling restrictions for =</2 and >/2. Such
assertions live in the libraries that provide these standard predicates. Further
details can be obtained by hovering over the literal (Fig. 2).
In Fig. 3 we have added an assertion for the exported predicate qsort/2
expressing that it should be called with its first argument bound to a list of
numbers.2 Assuming this “entry” information, the system can verify that all
the calls to =</2 and >/2 are now correct (with their arguments bound to
numbers in this case), and thus no warnings are flagged. Note that in practice
this assertion may not be necessary since this information could be obtained
from the analysis of the caller(s) to this module.
Let us now add more assertions to the program, stating properties that
we want checked, as shown in Fig. 4. The assertion for predicate partition/4
(eighth line of Fig. 4) expresses, using modes,3 that the first argument should be
bound to a list of numbers, and the second to a number, and that, for any termi-
nating call meeting this call pattern: a) if the call succeeds, then the third and
fourth arguments will be bound to lists of numbers; and b) the call is determin-
istic, i.e., it will produce one solution exactly, property det in the + field (as in
Mercury [37]), which is inferred in CiaoPP as the conjunction of two properties:
1) the call does not (finitely) fail (property not_fails as in [4,9]) and 2) the
2
Due to space limitations we present the assertion language through –hopefully
intuitive– examples. More complete descriptions of the assertion language can be
found in [2, 12, 29].
3
See, e.g., [43] in this same volume for an introduction to modes.
Types, Modes and So Much More - The Prolog Way 27
call will produce one solution at most (property is_det as in [19]). Similarly,
the assertion for qsort/2 expresses the expected calling pattern, and that the
call can have at most one answer, property semidet.
In the assertion model, modes are macros that serve as a shorthand for
assertions, in particular predicate-level assertions. These are in general of the
form:
where Head denotes the predicate that the assertion applies to, and Pre and
Post are conjunctions of state property literals. Pre expresses properties that
hold when Head is called. Post states properties that hold if Head is called in a
state compatible with Pre and the call succeeds. Comp describes properties of
the whole computation such as determinism, non-failure, resource usage, termi-
nation, etc., aso for calls that meet Pre. In particular, the modes for qsort/2 in
Fig. 4 are expanded by the modes package (see module declaration in Fig. 3) to:
:- pred qsort (X,Y) : list(num ,X) => list(num ,Y) + semidet .
All the assertions in Fig. 4 indeed get verified by the system, which is
shown by underlying the assertions in green (Fig. 5), and again further infor-
mation can be obtained in a popup (Fig. 6).4 Figure 7, shows again qsort/2
but now the assertions are written as machine readable comments enabled by
the doccomments package. Such comments can contain embedded assertions,
which are also verified. Here we use again modes and determinacy. This for-
mat is familiar to Prolog programmers and compatible with any Prolog system
without having to define any operators for the assertion syntax.
In Fig. 8, we have replaced =</2 with </2 in the second clause of
partition/4, and the system warns that this predicate may fail. This is because
the case where X=F is not “covered” by the “tests” of partition/4 [4,9]. Con-
versely, if we replace >/2 with >=/2 in the second clause of the original def-
Fig. 6. The popup shows that calls, computational/global properties, and success pat-
tern for partition/ are verified.
4
Note that while, as mentioned before, the assertions in Fig. 4 use modes they are
represented internally in normal form and the popup message uses syntax close to
this form, where the computational properties and the state properties that must
hold upon success are split into separate (comp and success assertions respectively).
28 M. V. Hermenegildo et al.
Fig. 8. If we replace =</2 with </2 the system warns that partition/4 may fail.
inition of partition/4, Fig. 9, the system warns that the predicate may not
be deterministic. This is because the analyzer infers that not all the clauses of
partition/4 are pairwise mutually exclusive (in particular the second and third
clauses are not), and thus, multiple solutions may be obtained [19].
Defining Properties. The reader may be wondering at this point where the
properties that are used in assertions (such as list(num)) come from. As men-
tioned before, such properties are typically written in Prolog and its extensions;
and they can also be built-in and/or defined and imported from system libraries
or in user code. Visibility is controlled by the module system as for any other
predicate. Figure 10 shows some examples of definitions of properties. Two of
them are marked as regular types (regtype directive): color/1, defined as the
set of values {red, green, blue}, and colorlist/1, representing the infinite set
of lists whose elements are of color type. The third property is not a regular
type, but an arbitrary property (prop directive), representing the infinite set of
lists of numeric elements in descending order. Marking predicates as properties
allows them to be used in assertions, but they remain regular predicates, and
can be called as any other, and also used as run-time tests, to generate examples
(test cases), etc. For example:
Types, Modes and So Much More - The Prolog Way 29
Fig. 9. If we replace >/2 with >=/2 the system warns that both partition/4 and
qsosrt/2 may not be deterministic.
Fig. 10. Defining some properties which can then be used in assertions.
?- colorlist (X).
X = [] ? ;
X = [red] ? ;
X = [red ,red] ? ...
Figure 11 shows the same properties of Fig. 10 but written using functional
notation. The definitions are equivalent, functional syntax being just syntactic
sugar.
Fig. 13. Success and inferred properties (sorted/1 and red) are incompatible.
Fig. 15. Just a warning: sorted could not be verified (with selected domains).
Fig. 16. An example with more complex properties, a cost error is flagged.
Types, Modes and So Much More - The Prolog Way 31
Fig. 17. The system reminds us that nrev/2 is of course quadratic, not linear.
Fig. 18. With the cost expression fixed all properties are now verified.
In Fig. 12 we add some simple definitions for p/1 and q/1, and a pred asser-
tion for q/1, meaning “in all calls q(X) that succeed, X is instantiated on success
to a term of color type.” This is verified by the system. We have also added
an assertion for p/1 meaning “in all calls p(X) that succeed, X gets instanti-
ated to a term meeting the sorted property.” The system detects that such
assertion is false and shows the reason (Fig. 13): the analyzer (with the eterms
abstract domain [41]) infers that on success X gets bound to red, expressed as the
automatically inferred regular type rt27/1, and the system finds that rt27(X)
and sorted(X) are incompatible (empty intersection of the set of terms they
represent). In Fig. 14, we have changed the definition of q/1 so that there is no
incompatibility, and now the system simply warns (Fig. 15) that it cannot verify
the assertion for p/1. The success type rt27(X) inferred for p/1 (lists of three
arbitrary terms) and sorted(X) are now compatible, and thus no error is flagged.
However, rt27(X) does not imply sorted(X) for all X’s, and thus sorted(X) is
not verified (with the default set of abstract domains). In this case the system
will (optionally) introduce a run-time check so that sorted(X) is tested when
p/1 is called. Furthermore, the system can run unit tests or generate test cases
(in this case arbitrary terms) automatically to exercise such run-time tests.
An example with more complex properties (also using the functional syn-
tax package) is shown in Fig. 16. It includes a user-provided assertion stating
(among other properties) that the cost of nrev/2 in resolution steps, for calls to
nrev(A, B) with A a ground list and B a free variable, should be linear in the
32 M. V. Hermenegildo et al.
Fig. 19. If we change the assertion for conc/3 from complexity order (_o) to upper
bound (_ub) then the system flags that length(A) is not a correct upper bound.
Fig. 20. With the cost expression fixed all properties are now verified.
1.2 Discussion
We argue that this assertion model greatly enhances the power of Prolog for pro-
gramming both in the small and in the large, combining effectively the advan-
tages of the of dynamically- and statically-typed languages. It preserves the
dynamic language features while at the same time providing safety guaran-
tees and the capability of achieving the performance and efficiency of static
systems [6]. The novel combination of assertion language, properties, run-time
checking, testing, etc. generates many new synergies.
We believe that a good part of the power of the approach (and perhaps why
this approach was first proposed in the context of Prolog) arises from character-
istics of the logic programming paradigm and the Prolog language in particular.
5
An upper bound [26, 35] is also inferred, equal to the lower bound (Fig. 17).
Types, Modes and So Much More - The Prolog Way 33
For example, as we have seen, the fact that Prolog allows writing many properties
(including types) in the source language is instrumental in allowing assertions
which cannot be statically verified to be easily used as run-time checks, allowing
users to obtain benefits even if a certain property cannot be verified at compile
time. As another example, the reversibility of properties written in Prolog allows
generating test cases automatically from assertions, without having to invent new
concepts or to implement any new functionality, since “property-based testing”
comes for free in this approach and thus did not need to be invented. Another
contributing factor is that it was in the Prolog community that formal static
analysis techniques, in particular abstract interpretation, flourished first, during
the 80’s and 90’s [10], leading quite naturally to the development in the mid-90’s
of the Ciao model.
The practical relevance of the combination of static and dynamic features
brought about by this approach is illustrated by the many other languages
and frameworks which have been proposed more recently, aiming at bringing
together both worlds, using similar ideas. This includes, e.g., the work on grad-
ual typing [31,36,40] and liquid types [32,42]. Pfenning’s et al.’s early work on
refinement types [28] and practical dependent types [44] was seminal in this con-
text and also uses abstract interpretation or constraint solving, but stays on
the decidable side and is thus not combined with run-time checking or testing.
Another example is the recent work on verifying contracts [18,23,27,39]. Prolog
pioneered and is continuing to push the state of the art in this area. However,
although some Prolog systems have introduced run-time checks or testing, there
is still much work in this area that could become more widely adopted.
References
1. Arias, J., Carro, M., Salazar, E., Marple, K., Gupta, G.: Constraint answer set
programming without grounding. Theory Pract. Logic Program. 18(3–4), 337–354
(2018). https://doi.org/10.1017/S1471068418000285
2. Bueno, F., Carro, M., Hermenegildo, M.V., Lopez-Garcia, P., Morales, J. (eds.) The
Ciao System. Reference Manual (v1.22). Techncial report, April 2023. https://ciao-
lang.org
6
See again [43] for in introduction to term expansion in Prolog.
Types, Modes and So Much More - The Prolog Way 35
3. Bueno, F., et al.: On the role of semantic approximations in validation and diag-
nosis of constraint logic programs. In: Proceedings of the 3rd International WS on
Automated Debugging-AADEBUG, pp. 155–170. U. Linköping Press, May 1997
4. Bueno, F., López-García, P., Hermenegildo, M.: Multivariant non-failure analy-
sis via standard abstract interpretation. In: Kameyama, Y., Stuckey, P.J. (eds.)
FLOPS 2004. LNCS, vol. 2998, pp. 100–116. Springer, Heidelberg (2004). https://
doi.org/10.1007/978-3-540-24754-8_9
5. Cabeza, D., Hermenegildo, M.: A new module system for prolog. In: Lloyd, J.,
et al. (eds.) CL 2000. LNCS (LNAI), vol. 1861, pp. 131–148. Springer, Heidelberg
(2000). https://doi.org/10.1007/3-540-44957-4_9
6. Carro, M., Morales, J., Muller, H., Puebla, G., Hermenegildo, M.V.: High-level
languages for small devices: a case study. In: Flautner, K., Kim, T. (eds.) Compil-
ers, Architecture, and Synthesis for Embedded Systems, pp. 271–281. ACM Press
/ Sheridan, October 2006
7. Cousot, P., Cousot, R.: Abstract interpretation: a unified lattice model for static
analysis of programs by construction or approximation of fixpoints. In: ACM Sym-
posium on Principles of Programming Languages (POPL’77), pp. 238–252. ACM
Press (1977). https://doi.org/10.1145/512950.512973
8. Debray, S.K., Lopez-Garcia, P., Hermenegildo, M.V., Lin, N.W.: Lower bound cost
estimation for logic programs. In: ILPS’97, pp. 291–305. MIT Press (1997)
9. Debray, S., Lopez-Garcia, P., Hermenegildo, M.V.: Non-failure analysis for logic
programs. In: ICLP’97, pp. 48–62. MIT Press (1997)
10. Giacobazzi, R., Ranzato, F.: History of abstract interpretation. IEEE Ann. Hist.
Comput. 44(2), 33–43 (2022). https://doi.org/10.1109/MAHC.2021.3133136
11. Gupta, G., Salazar, E., Arias, J., Basu, K., Varanasi, S., Carro, M.: Prolog: past,
present, and future. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowal-
ski, R., Rossi, F. (eds.) Prolog: The Next 50 Years. LNCS (LNAI), vol. 13900, pp.
48–61. Springer, Cham (2023). https://doi.org/10.1007/978-3-031-35254-6_4
12. Hermenegildo, M.V., et al.: An overview of ciao and its design philosophy. Theory
Pract. Logic Program. 12(1–2), 219–252 (2012)
13. Hermenegildo, M.V., Puebla, G., Bueno, F.: Using global analysis, partial specifica-
tions, and an extensible assertion language for program validation and debugging.
In: Apt, K.R., Marek, V.W., Truszczynski, M., Warren, D.S. (eds.) The Logic
Programming Paradigm: a 25-Year Perspective, pp. 161–192. Springer, Heidelberg
(1999). https://doi.org/10.1007/978-3-642-60085-2_7
14. Hermenegildo, M.V., Puebla, G., Bueno, F., Lopez-Garcia, P.: Integrated pro-
gram debugging, verification, and optimization using abstract interpretation (and
the Ciao system preprocessor). Sci. Comput. Program. 58(1–2), 115–140 (2005).
https://doi.org/10.1016/j.scico.2005.02.006
15. Hermenegildo, M., Morales, J., Lopez-Garcia, P.: Some thoughts on how to teach
prolog. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R.,
Rossi, F. (eds.) Prolog: The Next 50 Years. LNCS (LNAI), vol. 13900, pp. 107–
123. Springer, Cham (2023). https://doi.org/10.1007/978-3-031-35254-6_9
16. Holzbaur, C.: Metastructures vs. attributed variables in the context of extensible
unification. In: Bruynooghe, M., Wirsing, M. (eds.) PLILP 1992. LNCS, vol. 631,
pp. 260–268. Springer, Heidelberg (1992). https://doi.org/10.1007/3-540-55844-
6_141
17. Hudak, P., et al.: Report on the programming language Haskell. Haskell Spec.
Issue, ACM SIGPLAN Not. 27(5), 1–164 (1992)
18. Logozzo, F., et al.: Clousot. https://msdn.microsoft.com/en-us/devlabs/dd491992.
aspx. Accessed 2018
36 M. V. Hermenegildo et al.
19. Lopez-Garcia, P., Bueno, F., Hermenegildo, M.V.: Automatic inference of deter-
minacy and mutual exclusion for logic programs using mode and type analyses. N.
Gener. Comput. 28(2), 117–206 (2010)
20. Lopez-Garcia, P., Klemen, M., Liqat, U., Hermenegildo, M.V.: A general framework
for static profiling of parametric resource usage. TPLP (ICLP’16 Spec. Issue) 16(5–
6), 849–865 (2016). https://doi.org/10.1017/S1471068416000442
21. Mera, E., Lopez-García, P., Hermenegildo, M.: Integrating software testing and
run-time checking in an assertion verification framework. In: Hill, P.M., Warren,
D.S. (eds.) ICLP 2009. LNCS, vol. 5649, pp. 281–295. Springer, Heidelberg (2009).
https://doi.org/10.1007/978-3-642-02846-5_25
22. Morales, J., Carro, M., Hermenegildo, M.V.: Description and optimization of
abstract machines in a dialect of prolog. Theory Pract. Logic Program. 16(1),
1–58 (2016). https://doi.org/10.1017/S1471068414000672
23. MSR: Code contracts. https://research.microsoft.com/en-us/projects/contracts/.
Accessed 2018
24. Muthukumar, K., Hermenegildo, M.: Combined determination of sharing and free-
ness of program variables through abstract interpretation. In: ICLP’91, pp. 49–63.
MIT Press, June 1991
25. Muthukumar, K., Hermenegildo, M.: Compile-time derivation of variable depen-
dency using abstract interpretation. JLP 13(2/3), 315–347 (1992)
26. Navas, J., Mera, E., López-García, P., Hermenegildo, M.V.: User-definable resource
bounds analysis for logic programs. In: Dahl, V., Niemelä, I. (eds.) ICLP 2007.
LNCS, vol. 4670, pp. 348–363. Springer, Heidelberg (2007). https://doi.org/10.
1007/978-3-540-74610-2_24
27. Nguyen, P.C., Tobin-Hochstadt, S., Van Horn, D.: Soft contract verification. In:
Proceedings of the 19th ACM SIGPLAN International Conference on Functional
Programming. ICFP ’14, pp. 139–152. ACM, New York, NY, USA (2014). https://
doi.org/10.1145/2628136.2628156
28. Pfenning, F.: Dependent types in logic programming. In: Pfenning, F. (ed.) Types
in Logic Programming, pp. 285–311. The MIT Press (1992)
29. Puebla, G., Bueno, F., Hermenegildo, M.: An assertion language for constraint logic
programs. In: Deransart, P., Hermenegildo, M.V., Małuszynski, J. (eds.) Analysis
and Visualization Tools for Constraint Programming. LNCS, vol. 1870, pp. 23–61.
Springer, Heidelberg (2000). https://doi.org/10.1007/10722311_2
30. Puebla, G., Bueno, F., Hermenegildo, M.: Combined static and dynamic assertion-
based debugging of constraint logic programs. In: Bossi, A. (ed.) LOPSTR 1999.
LNCS, vol. 1817, pp. 273–292. Springer, Heidelberg (2000). https://doi.org/10.
1007/10720327_16
31. Rastogi, A., Swamy, N., Fournet, C., Bierman, G., Vekris, P.: Safe & efficient
gradual typing for typescript. In: 42nd POPL, pp. 167–180. ACM, January 2015
32. Rondon, P.M., Kawaguchi, M., Jhala, R.: Liquid types. In: Gupta, R., Amaras-
inghe, S.P. (eds.) Proceedings of the ACM SIGPLAN 2008 Conference on Pro-
gramming Language Design and Implementation, Tucson, AZ, USA, 7–13 June
2008, pp. 159–169. ACM (2008). https://doi.org/10.1145/1375581.1375602
33. Saglam, H., Gallagher, J.: Approximating constraint logic programs using polymor-
phic types and regular descriptions. Technical report CSTR-95-17, Department of
Computer Science, U. of Bristol, Bristol BS8 1TR (1995)
34. Sanchez-Ordaz, M., Garcia-Contreras, I., Perez-Carrasco, V., Morales, J.F., Lopez-
Garcia, P., Hermenegildo, M.V.: Verifly: on-the-fly assertion checking via incremen-
tality. Theory Pract. Logic Program. 21(6), 768–784 (2021)
Types, Modes and So Much More - The Prolog Way 37
35. Serrano, A., Lopez-Garcia, P., Hermenegildo, M.V.: Resource usage analysis of
logic programs via abstract interpretation using sized types. TPLP, ICLP’14 Spec.
Issue 14(4–5), 739–754 (2014). https://doi.org/10.1017/S147106841400057X
36. Siek, J.G., Taha, W.: Gradual typing for functional languages. In: Scheme and
Functional Programming Workshop, pp. 81–92 (2006)
37. Somogyi, Z., Henderson, F., Conway, T.: The execution algorithm of mercury: an
efficient purely declarative logic programming language. JLP 29(1–3), 17–64 (1996)
38. Swift, T., Warren, D.S.: XSB: extending prolog with tabled logic programming.
Theory Pract. Logic Program. 12(1–2), 157–187 (2012). https://doi.org/10.1017/
S1471068411000500
39. Takikawa, A., et al.: Towards practical gradual typing. In: Boyland, J.T. (ed.)
29th European Conference on Object-Oriented Programming, ECOOP 2015, 5–10
July 2015, Prague, Czech Republic. LIPIcs, vol. 37, pp. 4–27. Schloss Dagstuhl -
Leibniz-Zentrum fuer Informatik (2015). https://doi.org/10.4230/LIPIcs.ECOOP.
2015.4
40. Tobin-Hochstadt, S., Felleisen, M.: The design and implementation of typed
scheme. In: POPL, pp. 395–406. ACM (2008)
41. Vaucheret, C., Bueno, F.: More precise yet efficient type inference for logic pro-
grams. In: Hermenegildo, M.V., Puebla, G. (eds.) SAS 2002. LNCS, vol. 2477, pp.
102–116. Springer, Heidelberg (2002). https://doi.org/10.1007/3-540-45789-5_10
42. Vazou, N., Tanter, É., Horn, D.V.: Gradual liquid type inference. Proc. ACM Pro-
gram. Lang. 2(OOPSLA), 132:1–132:25 (2018). https://doi.org/10.1145/3276502
43. Warren, D.S.: Introduction to prolog. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog: The Next 50 Years. LNCS
(LNAI), vol. 13900, pp. 3–19. Springer, Cham (2023). https://doi.org/10.1007/978-
3-031-35254-6_1
44. Xi, H., Pfenning, F.: Dependent types in practical programming. In: Appel, A.W.,
Aiken, A. (eds.) POPL ’99, Proceedings of the 26th ACM SIGPLAN-SIGACT
Symposium on Principles of Programming Languages, San Antonio, TX, USA,
20–22 January 1999. pp. 214–227. ACM (1999). https://doi.org/10.1145/292540.
292560
Prolog as a Knowledge Representation
Language the Nature and Importance
of Prolog
Michael Genesereth(B)
1 Introduction
data integration). And so forth. Each of these tasks requires a different “inter-
preter”, but the rules are the same in all cases. (And, incidentally, few of these
tasks can be performed easily with programs written in traditional imperative
programming languages.)
The point is that, by focussing on Prolog as a programming language rather
than a knowledge representation language, we may be doing it a disservice. In
fact, it can be argued that Prolog’s main value lies not so much in programming
as in knowledge representation. In this article, I make this argument in three
stages. First of all, I explain why I think Prolog is superior to other knowledge
representation formalisms, such as First-Order Logic. I then discuss the merits
of having multiple interpreters for Prolog. Finally, I talk about the prospects for
automatically transforming logic programs from natural but potentially expen-
sive form into versions that execute more efficiently for specific interpreters.
In an early paper [20], John McCarthy extolled the benefits of First Order Logic
(FOL) as a framework for knowledge representation. The language of FOL pro-
vides a variety of useful linguistic features, e.g. logical operators, variables, and
quantifiers. Moreover, being domain-independent, the language of FOL has no
built in assumptions and thus is more general than domain-specific languages.
Unfortunately, FOL has some properties that limit its usefulness. For exam-
ple, in FOL, it is not possible to define the notion of transitive closure in a way
that precludes non-standard models (at least without Herbrand semantics [7]).
Moreover, in defining relations, it is usually necessary to write “negation axioms”
to say when those relations do not hold as well as positive axioms that say when
those relations do hold.
One nice feature of Prolog is that it deals with these limitations in a graceful
way. If one abides by a few restrictions in writing Prolog programs (e.g. safety
and stratified negation) and if one uses minimal model semantics for the language
(i.e. negation as failure), transitive closure can be defined precisely, and negation
axioms become unnecessary.
Some might argue that this feature of Prolog is also a disadvantage. Under the
conditions just mentioned, every Prolog program has a unique minimal model.
This effectively prevents one from encoding incomplete information. In order to
deal with this disadvantage, it might be nice to allow programmers to write rules
with negations or disjunctions or existential heads. And Answer-Set Program-
ming (ASP) [19] provides a way for programmers to blend classical negation
with negation as failure.
On the other hand, in many circumstances it is often desirable to strive
for complete knowledge about the application area of a program. For example,
in writing specifications for runnable programs, it is desirable to know what
is acceptable behavior and what is not acceptable so that a system executing
the program can act with confidence. While it is possible to write complete
theories in FOL, it is not always easy to determine whether or not a given
40 M. Genesereth
3 Multiple Interpreters
Query Evaluation is the simplest way in which a Prolog program can be used.
We start with a ruleset and a dataset and apply a query evaluation procedure to
compute answers to queries. The interpreter could be a bottom-up interpreter
or a top-down evaluator (like the standard Prolog interpreter [4]) alone or in
combination with optimization refinements such as conjunct ordering and/or
tabling [26].
Suppose, for example, we have a dataset of kinship information like the one
below. The person named art is a parent of a person bob and another person
bea; bob is the parent of both cal and cam; and bea is the parent of both cat
and coe.
parent(art,bob)
parent(art,bea)
parent(bob,cal)
parent(bob,cam)
parent(bea,cat)
parent(bea,coe)
Prolog as a Knowledge Representation Language 41
The following Prolog rule defines the grandparent relation in terms of parent. A
person x is the grandparent of a person z if x is the parent of a person y and
y is the parent of z.
Given this dataset and ruleset, we can apply a query evaluation procedure to
compute the corresponding instance of the grandparent relation.
grandparent(art,cal)
grandparent(art,cam)
grandparent(art,cat)
grandparent(art,coe)
Query evaluation is the usual way in which Prolog is used. The answers are
logically entailed by the data and rules, and the standard Prolog interpreter
produces these answers by some form of deduction (typically SLD-resolution
[13]).
Now, consider the problem of coloring planar maps using only four colors, the
idea being to assign each region a color so that no two adjacent regions are
assigned the same color. A typical map is shown below. In this case, we have six
regions. Some are adjacent to each other, meaning that they cannot be assigned
the same color. Others are not adjacent, meaning that they can be assigned the
same color.
We can represent the basic facts of this problem as a set of ground atoms like the
ones below. We use the unary relation region to enumerate regions. We use hue
to enumerate possible colors of regions. And we use the binary relation next to
42 M. Genesereth
capture contiguity between regions. (Note that the sentences here capture conti-
guity in one direction. One might wish to include the sentences with arguments
reversed to capture contiguity in the other direction as well.)
One way to codify the constraints in this problem is to define a relation illegal,
which is true for any assignment that violates those constraints. For example,
the first rule below states that no two adjacent regions can have the same color.
The second rule states that no region can have more than one color. The last
two rules state that every region must have at least one color.
Our goal in this problem is to infer a set of ground atomic sentences that charac-
terize the color relation. Given these definitions, it is possible to determine that
the dataset below is one solution to the problem. Of course, this is not the only
solution. It is possible to permute the colors in various ways and still satisfy the
constraints.
color(r1,red)
color(r2,green)
color(r3,blue)
color(r4,red)
color(r5,blue)
color(r6,purple)
The point of this example is that none of these solutions is logically entailed
by the definitions in the problem, and the standard Prolog interpreter will not
produce any answers to questions about the colors of regions (given the rules
as written). However, an interpreter that is capable of abduction (as opposed to
deduction) or constraint satisfaction can produce answers like the one above.
Consider the example below. Here we see the preferences of an insuree joe.
In particular, he wants a portfolio of policies that covers him for hospitalizations
in Japan or Korea. The binary relation patient here relates a hospitalization
and the patient. The relation hospital relates a hospitalization and a hospital.
And the country relation relates a hospital and a country.
covered(Z) :-
patient(Z,P) & related(P,joe) &
hospital(Z,H) & country(H,C) & continent(C,asia)
related(I,I)
related(P,I) :- spouse(P,I)
related(P,I) :- parent(I,P)
continent(japan,asia)
continent(korea,asia)
Given this information, it is easy for us to see that the policy covers the pref-
erences of the insuree. Like the preceding examples, we have definitions of view
relations. Unlike the case of query evaluation, we do not have a complete dataset.
And, unlike the case of constraint satisfaction, we are not looking for a complete
dataset. Instead, we are trying to determine whether a policy covers his needs
for every complete dataset. The key to automating this determination is to use
an interpreter capable of containment testing [1,24] to determine whether one
program produces all of the results of another program for any dataset, not just
one particular dataset.
4 Program Transformation
In the case of the map shown above, our goal is to find six hues (one for each
region of the map) such that no two adjacent regions have the same hue. We can
express this goal by writing the query shown below.
goal(C1,C2,C3,C4,C5,C6) :-
ok(C1,C2) & ok(C1,C3) & ok(C1,C5) & ok(C1,C6) &
ok(C2,C3) & ok(C2,C4) & ok(C2,C5) & ok(C2,C6) &
ok(C3,C4) & ok(C3,C6) & ok(C5,C6)
Given this version, we can use the standard Prolog interpreter to produce 6-
tuples of hues that ensure that no two adjacent regions have the same color. Of
course, in problems like this one, we usually want only one solution rather than
all solutions. However, finding even one solution is such cases can be costly.
The good news it is possible to convert the formulation described earlier into
this form; and in many cases this conversion can be done automatically [11].
The benefit of doing things this way is that the programmer can formalize the
problem in its most natural form - as a constraint satisfaction problem - and the
system can then transform into a version that can be executed by the standard
Prolog interpreter.
But, wait, there’s more! Given a program’s declarative semantics, it is possi-
ble to rewrite the program into a form that runs even more rapidly. In our exam-
ple, the program can be improved by reordering the conjuncts in the definition
of goal, based on size heuristics and/or the Kempe transformation described by
McCarthy [21], resulting in the version shown below.
goal(C1,C2,C3,C4,C5,C6) :-
ok(C1,C2) & ok(C1,C3) & ok(C1,C6) &
ok(C2,C3) & ok(C2,C6) & ok(C3,C6) &
ok(C2,C4) & ok(C3,C4) & ok(C1,C5) & ok(C2,C5) &
ok(C5,C6)
Prolog as a Knowledge Representation Language 45
This version looks the same but runs faster due to the better ordering of subgoals.
This is not something that necessarily matters to the programmer; but it does
matter to the interpreter. If this transformation is automated, the programmer
does not need to worry these details.
Similar examples can be found in the formulation of computationally com-
plex problems such as the fibonacci function. The programmer can write the
definitions in their most natural but computationally inefficient form, and the
program can be automatically written in a form that can be executed efficiently,
either by rewriting the fibonacci definition with a single recursion or by using a
tabling interpreter [23].
The computer programming community has long recognized the value of
Interactive Development Environments (IDEs) to help in developing and main-
taining programs. Logical Interactive Development Environments (LIDEs) have
similar value for Logic Programming. These systems can save authors work pro-
viding pre-existing ontologies and databases and knowledge bases. They provide
tools for the verification, analysis, and debugging of legal codes. And they can
provide technology for automatically translating to and from other formal lan-
guages. They can support languages that are more expressive than Logic Pro-
gramming, e.g. FOL and beyond. They can support languages that are more
human-friendly, e.g. controlled English, such as Kowalski’s Logical English [18],
thus making possible pseudo-natural language authoring without the drawbacks
of general natural language.
More importantly, it is possible in many cases for such environments to
transform programs into computationally more efficient versions, allowing the
programmer to encode knowledge in its most natural form while the computer
gets to execute a more efficient version. The cost of these transformations can be
paid once, and the cost can be amortized over multiple uses of the transformed
programs.
5 Conclusion
The facts and rules in the examples described above are all Prolog programs
written using simple declarative semantics; but, in the various examples, the
programs are processed in completely different ways. This multiplicity of uses
illustrates the value of using Prolog to encode the knowledge relevant to appli-
cations rather than thinking of sets of Prolog facts and rules as programs for pro-
cessing that knowledge in one specific way for all applications. Transformations
to enhance efficiency can be applied by an interactive development environment.
The upshot is that the user can code the knowledge in its most natural form and
use the LIDE to find a suitable interpreter or a computationally tractable form
for the program that can be executed by the standard interpreter or a variant
like XSB [23].
46 M. Genesereth
References
1. Carlson, P., Genesereth, M.: Insurance portfolio management as containment test-
ing. In: ICAIL (2023)
2. Chandra, A.K., Merlin, P.M.: Optimal implementation of conjunctive queries in
relational databases. In: Proceeding of the 9th Annual ACM Symposium on the
Theory of Computing, pp. 77–90 (1977)
3. Chen, W., Swift, T., Warren, D.: Efficient top-down computation of queries under
the well-founded semantics. J. Logic Program. 24(3), 161–201 (1995)
4. Clocksin, W.F., Mellish, C.S.: Programming in Prolog, 4th edn. Springer, New
York (1994). https://doi.org/10.1007/978-3-642-97596-7
5. De Cat, B., Bogaerts, B., Bruynooghe, M., Janssens, G., Denecker, M.: Predicate
logic as a modeling language: the IDP system. Declarative Logic Program. 279–323
(2018)
6. Gelfond, M., Leone, N.: Logic programming and knowledge representation. Artif.
Intell. 138(1–2), 1 (2002)
7. Bassiliades, N., Gottlob, G., Sadri, F., Paschke, A., Roman, D. (eds.): RuleML
2015. LNCS, vol. 9202. Springer, Cham (2015). https://doi.org/10.1007/978-3-
319-21542-6
8. Genesereth, M., Chaudhri, V.: Logic programming. Synth. Lect. Artif. Intell. Mach.
Learn. (2020). https://doi.org/10.2200/S00966ED1V01Y201911AIM044
9. Genesereth, M.: Insurance portfolio management. Complaw Corner, Codex: The
Stanford Center for Legal Informatics (2022). https://law.stanford.edu/2022/07/
30/insurance-portfolio-management/
10. Hayes, P.: Computation and deduction. In: Proceedings Second Symposium on
Mathematical Foundations of Computer Science, Czechoslovakian Academy of Sci-
ences, Czechoslovakia, pp. 105–118 (1973)
11. Hinrichs, T.: Extensional reasoning. Ph.D. thesis, Computer Science Department,
Stanford University (2007)
12. Kakas, A.C., Kowalski, R., Toni, F.: The role of abduction in logic programming. In:
Gabbay, D.M., Hogger, C.J., Robinson, J.A. (eds.) Handbook of Logic in Artificial
Intelligence and Logic Programming, vol. 5, pp. 235–324. Oxford University Press,
Oxford (1998)
13. Kowalski, R., Kuehner, D.: Linear resolution with selection function. Artif. Intell.
2, 227–60 (1971)
14. Kowalski, R.: Predicate logic as a programming language. In: Proceedings of IFIP
1974, North Holland Publishing Company, Amsterdam, pp. 569–574 (1974)
15. Kowalski, R.: Algorithm = Logic + Control. Commun. ACM 22(7) (1979)
16. Kowalski, R., Sadri, F.: LPS - a logic-based production system framework (2009)
17. Kowalski, R., Sadri, F.: Integrating Logic Programming and Production Systems
in Abductive Logic Programming Agents (2009)
18. Logical English as a Programming Language for the Law. ProLALA 22 (2022)
19. Lifschitz, V.: What is answer set programming? (PDF). In: Proceedings of the 23rd
National Conference on Artificial Intelligence, vol. 3, pp. 1594–1597. AAAI Press,
13 July 2008
20. McCarthy, J.: Programs with common sense. In: Proceedings of the Teddington
Conference on the Mechanization of Thought Processes, pp. 75–91. Her Majesty’s
Stationary Office, London (1959)
21. McCarthy, J.: Coloring maps and the kowalski doctrine. In: Lifschitz, V. (ed.)
Formalizing Common Sense: Papers by John McCarthy, pp. 167–174 (1998)
Prolog as a Knowledge Representation Language 47
22. Pereira, L.M., Porto, A.: Selective Backtracking for Logic Programs. Departamento
de Informatica, Faculdade de Ciencias e Tecnologia, Universidade Nova de Lisboa,
Lisboa, Portugal (1980)
23. Sagonas, K., Swift, T.: Warren, D.S.: XSB as an efficient deductive database engine.
In: Proceedings of the ACM SIGMOD International Conference on the Manage-
ment of Data (1994)
24. Ullman, J.D.: Information integration using logical views. Theor. Comput. Sci.
239(2), 189–210 (2000). https://doi.org/10.1016/S0304-3975(99)00219-4
25. van Emden, M., Kowalski, R.: The semantics of predicate logic as a programming
language. J. Assoc. Comput. Mach. 23(4), 733–774 (1976)
26. Warren, D.S.: Programming in Tabled Prolog. https://citeseerx.ist.psu.edu/
viewdoc/download?doi=10.1.1.49.4635
Prolog: Past, Present, and Future
1 Introduction
The year 2022 was celebrated as the 50th anniversary of the founding of logic pro-
gramming (LP) and Prolog [16]. Prolog harnesses the power of logic and provides
a new declarative paradigm for computing. Initially, Prolog was based on Horn
clauses with some built-ins added. Over time more features were added to make
the language more powerful as well as efficient. These features include constraints
over various types of domains (reals, booleans, finite domains, etc.), negation-as-
failure, coroutining, tabling, parallelism, etc. Prolog has been applied to many
(innovative) applications. Today, Prolog is a highly sophisticated language [34]
with a large user base. Over the last fifty years, as one would expect, research
in logic programming has flourished in three main areas: making Prolog more
efficient, making Prolog more expressive, and developing applications that make
use of logic programming technology. We will focus on the first two issues, as
any discussion of applications of logic programming will take significantly more
space. However, one of the applications of LP that we will discuss in this paper is
automating commonsense reasoning with the aim of building systems that can
emulate the thought process of an (unerring) human [18,25,37,47]. We believe
that logic programming is indispensable for this purpose. We assume that the
reader is familiar with Prolog, logic programming, and answer set programming.
An excellent, brief exposition is given in the introductory chapter of this book
[51]. More detailed expositions can be found elsewhere [23,39,48].
of the clause are executed. The strategy for rule selection and subgoal selection
is called the computation rule [39]. Prolog uses a computation rule that tries
clauses in textual order, while subgoals in a clause are tried left to right. A
considerable amount of research has been undertaken over the past decades to
improve the rule selection process. Executing Prolog programs in or-parallel—
trying multiple matching clauses simultaneously on multiple processors—can be
regarded as a strategy to improve rule-selection. Tabled logic programming can
also be viewed as a rule-selection strategy, where rules are (optionally) selected in
a non-textual order to ensure termination of left-recursive programs, for example.
Research has also been undertaken over the past several decades to improve the
subgoal selection process. Research in goal selection strategies includes constraint
logic programming (where the generate and test strategy is flipped into test
and generate), coroutining, concurrent logic programming, and and-parallelism.
These efforts in improving rule selection and subgoal selection have resulted in
Prolog systems that are highly effective [34].
Unfortunately, various strategies developed to make execution more efficient
are not all available in a single Prolog system where they work seamlessly with
each other. We believe that future research in Prolog must focus on building a
unified system that supports tabling, constraints, coroutining, parallelism, and
concurrency [27]. These enhancements must also work seamlessly with each
other. Many groups have been steadfastly working towards this goal. These
include, among others, efforts by Arias and Carro to integrate constraints in
tabled logic programming [6] and by Rocha and Santos Costa to combine tabling
and or-parallelism [44]. Research has also been conducted on adding concurrency
at the user level to Prolog [3,12,17]. Some of these ideas have been incorporated
in systems such as SWI-Prolog [52] and Ciao-Prolog [31], nevertheless, research
to realize a system where all these advanced features are efficiently and seam-
lessly integrated and available in a single system must continue. Our hope is
that such a logic programming system will be realized in the future.
lated in ASP [23,42]. However, a problem faced by ASP is the following: how
to execute answer set programs in the presence of predicates? ASP researchers
resorted to allowing only propositional programs. Thus, ASP programs contain-
ing predicates have to be grounded first so that they become propositional and
then a SAT-solver is used to find its (multiple) models. This leads to a number
of restrictions including: (i) programs have to be finitely groundable, (ii) data
structures such as lists are not permitted, (iii) program size blows up exponen-
tially during grounding, (iv) real numbers cannot be faithfully represented. Thus,
while ASP enhances Prolog, it also restricts it due to the choice of implementa-
tion mechanism used. While extremely efficient propositional ASP systems such
as CLINGO [22] have been developed, restriction to propositions-only programs
make them hard to use for knowledge representation applications, in particular,
modeling commonsense reasoning, which is often query-driven or goal-directed.
To overcome this, stable model semantics-based NAF must be incorporated in
Prolog. The discovery of coinductive logic programming [46] led to development
of query-driven implementations of ASP called s(ASP) [40] and s(CASP) [4].
The s(ASP) and s(CASP) systems allow predicates, do not require grounding,
and can be thought of as extending Prolog with stable model semantics-based
negation. Thus, default rules with exceptions, integrity constraints, and cyclical
reasoning through negation can be elegantly supported in this extended Pro-
log, thereby supporting automation of commonsense reasoning. The s(ASP) and
s(CASP) systems rely on many advanced techniques such as constructive nega-
tion, dual rule generation, coinduction, etc., and are scalable.
senses (sight, sound, smell, taste, and touch) to acquire information via pat-
tern matching, but to draw conclusions based on this information, humans use
(commonsense) reasoning. Note that:
1. Machine learning technologies are akin to human sensing and pattern match-
ing (humans learn by observing patterns through use of various senses).
Machine learning technologies have greatly advanced in the last few years.
2. Commonsense reasoning relates to human thinking. ASP and systems such
as s(CASP) provide the wherewithal to elegantly automate commonsense
reasoning.
As stated earlier, a large portion of the human thought process can be largely
emulated by supporting (i) default rules with exceptions and preferences, (ii)
integrity constraints, and (iii) multiple possible worlds. As explained earlier,
default rules with exceptions express inductive generalizations, and are used by
humans for making deductions. Similarly, multiple possible worlds help in abduc-
tion, or assumption-based reasoning. Integrity constraints allow us to prune
unfruitful paths in our abductive reasoning process. Unfruitful paths in the
deductive reasoning process are pruned by adding conditions to rule bodies.
Default Reasoning with Exceptions and Preferences: Humans use default
reasoning [23] to jump to conclusions. These conclusions may be revised later
in light of new knowledge. For example, if we are told that Tweety is a bird,
and then asked whether Tweety flies, we will immediately answer, yes, it does.
However, later if we are told that Tweety is a penguin, we will withdraw the
conclusion about Tweety’s flying ability, labeling Tweety as an exception. Thus,
human reasoning is non-monotonic in nature, meaning that conclusions may be
withdrawn as new knowledge becomes available. Humans use this sort of default
reasoning to jump to conclusions all the time, and if they find the assumptions
made to jump to this conclusion to be incorrect, they revise their conclusion.
Multiple default conclusions can be drawn in some situations, and humans will
use additional reasoning to prefer one default over another. Thus, default rules
with exceptions and preferences capture most of the deductive reasoning we
perform. (More details on default reasoning can be found elsewhere [23,37]). It
should be noted that expert knowledge is nothing but a set of default rules about
a specialized topic [23].
Classical logic is unable to model default reasoning and non-monotonicity in
an elegant way. We need a formalism that is non-monotonic and can support
defaults to model commonsense reasoning. ASP is such a formalism. ASP sup-
ports both NAF (not p) as well as strong negation (-p), where p is a proposition
or a predicate. A strongly negated predicate has to be explicitly defined, just
as positive predicates are. Combining these two forms of negations results in
nuanced reasoning closer to how humans reason:
The above insight can be used, for example, to model the exceptions to Tweety’s
ability to fly in two possible ways. Consider the rules:
flies(X):- bird(X), not abnormal bird(X). % default
abnormal bird(X):- penguin(X). % exception
which state that if we know nothing about a bird, X, we conclude that it flies.
This is in contrast to the rules:
flies(X):- bird(X), not abnormal bird(X). % default
abnormal bird(X):- not -penguin(X). % exception
which states that a bird can fly only if we can explicitly rule out that it is a
penguin. So in the latter case, if we know nothing about a bird, we will conclude
that it does not fly. Which of the two rules one will use depends on how conser-
vative or aggressive one wants to be in jumping to the (default) conclusion. Note
that exceptions can have exceptions, which in turn can have their own excep-
tions, and so on. For example, animals normally don’t fly, unless they are birds.
Thus, birds are exception to the default of not flying. Birds, in turn, normally
fly, unless they are penguins. Thus, a penguin is an exception to the exception
for not flying. Defaults, exceptions, exceptions to exceptions, and so on, allow
humans to perform reasoning elegantly in an elaboration tolerant manner [11,23].
Integrity Constraints: ASP can also model integrity constraints elegantly. An
integrity constraint is a rule of the form:
false:- p1 , p2 , . . . , pn .
which states that the conjunction of p1 , p2 , through pn is false (the keyword
false is often omitted). Integrity constraints elegantly model global invariants
or restrictions that our knowledge must satisfy, e.g., p and -p cannot be true at
the same time, denoted
false:- p, -p.
Humans indeed use integrity constraints in their everyday reasoning: as restric-
tions (two humans cannot occupy the same spot) and invariants (a human must
breath to stay alive). Note that integrity constraints are global constraints, in
that they eliminate possible worlds. Unfruitful paths during deductive reasoning
are eliminated by adding appropriate conditions to the rule-bodies. Note that in
ASP, integrity constraints may also arise due to odd loops over negation (OLON),
i.e., rules of the form:
p(t̄) :- G, not p(t̄).
where p(t̄) is a predicate and G is a conjunction of goals. In absence of an alter-
native proof for p(t̄), the only admissible model for the above rule is p(t̄) =
false, G = false, which amounts to the global constraint that G must be false.
Possible Worlds: Humans can represent multiple possible worlds in parallel in
their minds and reason over each. For example, in the real world, birds do not
talk like humans, while in a cartoon world, birds (cartoon characters) can talk.
Prolog: Past, Present, and Future 55
Humans can maintain the distinction between various worlds in their minds and
reason within each one of them. These multiple worlds may have aspects that are
common (birds can fly in both the real and cartoon worlds) and aspects that are
disjoint (birds can talk only in the cartoon world). Unlike Prolog, ASP/s(CASP)
support multiple possible worlds. (See also the example about Annie and David
teaching programming languages in the introductory paper of this volume [51]).
are suitably transformed and appended to each top level query to ensure that
each constraint is enforced. Global constraints can also implicitly arise due to
OLON rules. These are analyzed at compile time and the appropriate constraint
generated and appended to a top-level query [4,40].
Constructive Negation: Since s(CASP) allows general predicates that could
be negated, support for constructive negation becomes essential. Consider a pro-
gram consisting of the simple fact:
p(a).
If we pose the query ?-not p(X), it should succeed with answer X = a. Intu-
itively, X = a means that X can be any term not unifiable with a. To support
constructive negation, the implementation has to keep track of values that a
variable cannot take. The unification algorithm has to be extended, therefore,
to account for such disequality-constrained values. The s(CASP) system incor-
porates [4,40] constructive negation.
Dual Rules: ASP assumes that programs have been completed [23,39]. To com-
plete a program, the s(CASP) system will add dual rules [2] to the program. The
procedure to add the dual rules is relatively simple and can be found elsewhere
[4]. An additional complication in computing the dual rules is the need to handle
existential variables. Consider the following very simple rule:
p(X):- not q(X,Y).
This rule corresponds to the Horn clause:
∀X(p(X) ⇐ ∃Y not q(X, Y ))
Its dual will be:
∀X(not p(X) ⇐ ∀Y q(X, Y ))
which, in s(CASP), will be represented as:
not p(X):- forall(Y, q1(X,Y)). q1(X,Y):- q(X,Y).
Universal quantification in the body of the dual rule is needed because, for
example, for the goal not p(a) to succeed, we must prove that q(a, Y) holds
for every possible value of Y. The s(CASP) system handles all these issues and
produces dual rules for arbitrary programs. The execution of the forall, however,
is non-trivial, as often times the foralls are nested.
4 Applications
Several advanced applications that automate commonsense reasoning using ASP
and s(CASP) have been developed. Most prominent is the CHeF system [15]
which emulates the expertise of a cardiologist to automatically generate treat-
ment for congestive heart failure. Our studies show that the CHeF system per-
forms on par with a cardiologist. The s(CASP) system has also been used by
Prolog: Past, Present, and Future 57
5 Conclusion
References
1. Aggarwal, C.C.: Neural Networks and Deep Learning. Springer, Cham (2018).
https://doi.org/10.1007/978-3-319-94463-0
2. Alferes, J.J., Pereira, L.M., Swift, T.: Abduction in well-founded semantics and
generalized stable models via tabled dual programs. TPLP 4(4), 383–428 (2004)
3. Areias, M., Rocha, R.: Multi-dimensional lock-free arrays for multithreaded mode-
directed tabling in prolog. Concurr. Comput. Pract. Exp. 31(5), 1–16 (2019)
4. Arias, J., Carro, M., Salazar, E., Marple, K., Gupta, G.: Constraint answer set
programming without grounding. TPLP 18(3–4), 337–354 (2018)
5. Arias, J., Carro, M., Chen, Z., Gupta, G.: Modeling and reasoning in event calculus
using goal-directed constraint answer set programming. TPLP 22(1), 51–80 (2022)
6. Arias, J., Carro, M.: Description, implementation, and evaluation of a generic
design for tabled CLP. TPLP 19(3), 412–448 (2019)
7. Basu, K., Shakerin, F., Gupta, G.: AQuA: ASP-Based Visual Question Answering.
In: Komendantskaya, E., Liu, Y.A. (eds.) PADL 2020. LNCS, vol. 12007, pp. 57–72.
Springer, Cham (2020). https://doi.org/10.1007/978-3-030-39197-3 4
Prolog: Past, Present, and Future 59
30. Hermenegildo, M.V., Morales, J.F., Lopez-Garcia, P., Carro, M.: Types, modes and
so much more - the prolog way. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo,
M., Kowalski, R., Rossi, F. (eds.) Prolog: The Next 50 Years. LNCS (LNAI),
vol. 13900, pp. 23–37. Springer, Cham (2023). https://doi.org/10.1007/978-3-031-
35254-6 2
31. Hermenegildo, M.V., et al.: An overview of ciao and its design philosophy. Theory
Pract. Log. Program. 12(1–2), 219–252 (2012)
32. Hutter, M.: Universal Artificial Intellegence. TTCSAES, Springer, Heidelberg
(2005). https://doi.org/10.1007/b138233
33. Kahneman, D.: Thinking. Fast and Slow. Farrar, Straus and Giroux (2011)
34. Körner, P., et al.: Fifty Years of Prolog and Beyond. Theory and Practice of Logic
Programming, pp. 1–83 (2022)
35. Kothawade, S., Khandelwal, V., Basu, K., Wang, H., Gupta, G.: AUTO-DISCERN:
autonomous driving using common sense reasoning. In: Proceedings of the ICLP
Workshops: GDE 2021, vol. 2970, CEUR Workshop Proceedings. CEUR-WS.org
(2021)
36. Kowalski, R., Davila, J., Sartor, G., Calejo, M.: Logical English for law and educa-
tion. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi,
F. (eds.) Prolog: The Next 50 Years. LNCS (LNAI), vol. 13900, pp. 287–299.
Springer, Cham (2023). https://doi.org/10.1007/978-3-031-35254-6 24
37. Kowalski, R.A.: Computational Logic and Human Thinking. Cambridge University
Press, Cambridge (2011)
38. Kowalski, R.A.: Logic for Problem Solving. North Holland (1979)
39. Lloyd, J.W.: Foundations of Logic Programming, 2nd edn. Springer, Heidelberg
(1987). https://doi.org/10.1007/978-3-642-83189-8
40. Marple, K., et al.: Computing stable models of normal logic programs without
grounding. Preprint arXiv:1709.00501 (2017)
41. Morris, J.: Blawx: user-friendly goal-directed answer set programming for rules
as code. In: Proceedings of the Programming Language and the Law (ProLaLa)
(2023)
42. Mueller, E.T.: Commonsense Reasoning: An Event Calculus Based Approach. Mor-
gan Kaufmann, San Francisco (2014)
43. Rajasekharan, A., Zeng, Y., Padalkar, P., Gupta, G.: Reliable natural language
understanding with large language models and answer set programming (2023).
Preprint arXiv:2302.03780; in Proc. ICLP’23 (Tech. Comm.) (2023, to appear)
44. Rocha, R., Silva, F.M.A., Costa, V.S.: On applying or-parallelism and tabling to
logic programs. Theory Pract. Log. Program. 5(1–2), 161–205 (2005)
45. Salazar, E.: Proof-theoretic Foundations of Normal Logic Programs. Ph.D. thesis,
Department of Computer Science, Univ. of Texas at Dallas (2019)
46. Simon, L., Bansal, A., Mallya, A., Gupta, G.: Co-logic programming: extending
logic programming with coinduction. In: Arge, L., Cachin, C., Jurdziński, T., Tar-
lecki, A. (eds.) ICALP 2007. LNCS, vol. 4596, pp. 472–483. Springer, Heidelberg
(2007). https://doi.org/10.1007/978-3-540-73420-8 42
47. Stenning, K., van Lambalgen, M.: Human Reasoning and Cognitive Science. MIT
Press, Boston (2008)
48. Sterling, L., Shapiro, E.: The Art of Prolog. MITPress, Cambridge (1994)
49. Wang, H., Gupta, G.: FOLD-R++: a scalable toolset for automated inductive
learning of default theories from mixed data. In: Hanus, M., Igarashi, A. (eds.)
Functional and Logic Programming. FLOPS 2022. LNCS, vol. 13215, pp. 224–242.
Springer, Cham (2022). https://doi.org/10.1007/978-3-030-99461-7 13
Prolog: Past, Present, and Future 61
50. Wang, H., Shakerin, F., Gupta, G.: FOLD-RM: efficient scalable explainable AI.
TPLP 22(5), 658–677 (2022)
51. Warren, D.S.: Introduction to Prolog. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog: The Next 50 Years. LNCS
(LNAI), vol. 13900, pp. 3–19. Springer, Cham (2023). https://doi.org/10.1007/978-
3-031-35254-6 1
52. Wielemaker, J., Schrijvers, T., Triska, M., Lager, T.: Swi-prolog. Theory Pract.
Log. Program. 12(1–2), 67–96 (2012)
53. Wikipedia. Neurosymbolic AI. https://en.wikipedia.org/wiki/Neuro-symbolic
AI#. Accessed Feb 2022
54. Wikipedia contributors. Artificial general intelligence - Wikipedia, the
free encyclopedia, 2023. https://en.wikipedia.org/w/index.php?title=Artificial
general intelligence&oldid=1148436187. Accessed 8 Apr 2023
55. Zeng, Y., Rajasekharan, A., et al.: Automated interactive domain-specific conver-
sational agents that understand human dialogs. Preprint arXiv:2302.08941 (2023)
Writing Correct Prolog Programs
David S. Warren(B)
1 Inductive Definitions
n! = 1 if n=0
n! = n*(n-1)! if n>0
The first clause specifies the value of factorial of 0 directly; the second clause
specifies the values for all natural numbers greater than 0. It’s clear how one can
use this definition to find the value of n! for any n. For example, say we want
the value of 4!. We know 0! = 1 from the first clause; from the second clause, we
know 1! = 1∗0!, and since we’ve established that 0! = 1, then 1! = 1∗1 = 1; again
from the second clause 2! = 2 ∗ 1! = 2 ∗ 1 = 2; again 3! = 3 ∗ 2! = 3 ∗ 2 = 6; and
finally 4! = 4 ∗ 3! = 4 ∗ 6 = 24. In the same way we can find the value of factorial
for any natural number by starting with 0 and computing the values of factorial
c The Author(s), under exclusive license to Springer Nature Switzerland AG 2023
D. S. Warren et al. (Eds.): Prolog: The Next 50 Years, LNAI 13900, pp. 62–70, 2023.
https://doi.org/10.1007/978-3-031-35254-6_5
Writing Correct Prolog Programs 63
for all numbers up to and including the one of interest. This is ensured by the
fact that all natural numbers can be reached by starting with 0 and adding 1
some (finite) number of times.
Here we have defined a function inductively. We can also (or more generally)
defines sets inductively. To define a set inductively, one first specifies a universe
of elements. Then one explicitly gives some subset of them as members of the
desired set, and provides a set of rules. Each rule says that if certain elements
of the universe are in the desired set, then some other element(s) must be in
the set. This defines a subset of the universe: the smallest set that contains the
explicit elements and is closed under the rules.
As another, perhaps slightly more interesting, example of an inductive defi-
nition, we consider GCD, the Greatest Common Divisor relation. The GCD of
two non-zero natural numbers is the largest natural number that evenly divides
them both. E.g., the GCD of 18 and 24 is 6. We’ll want to define gcd(n, m, d) to
mean that d is the GCD of the non-zero natural numbers n and m. An inductive
definition of this set is:
gcd(n,n,n) for n > 0
gcd(n+m,m,d) if gcd(n,m,d)
gcd(n,n+m,d) if gcd(n,m,d)
The first (base) clause says that the GCD of a number and itself is that number.
The second clause, a rule, says that if d is the GCD of n and m, then it is also
the GCD of n + m and m. And the third is similar. We’ll leave it to the reader
to compute a few of these triples. For example, starting from the single basic
element gcd(1, 1, 1) we see that it generates pairs with GCD of 1, i.e., pairs that
are relatively prime.
There are two nice properties of inductive definitions that use well-defined
and computable conditions:
1. They come with a mechanism to compute their values, as we have seen,
by starting with the base clauses, which give the first (unconditional) set
members; and then continuing to apply the other clauses until we get the
answer we want (or maybe all the answers.) This iterative process can serve
as a computational model for inductive definitions.
2. They provide a ready-made structure for proving properties that hold of all
members of the inductively defined sets: we just need to show that the desired
property holds for the unconditional members defined by the base clauses, and
that if the property holds for the set elements used in the conditions of an
inductive clause and the condition itself holds, then the property holds for
the newly specified member. Intuitively our reasoning for why this is true can
follow exactly the process we used to add members to the set: we see that each
initial member and each added member must have the desired property, so
that all members of the set must have it. The Induction Principle guarantees
that the property holds for every member of the defined set.
As an example of a proof, say we want to prove that if gcd(n, m, d) is in the
set defined inductively above, then d is indeed the greatest common divisor of n
64 D. S. Warren
and m. For the base clause, clearly the greatest common divisor of two identical
numbers is that number itself. For the inductive clauses, if d divides n and m,
then it clearly divides n + m. And if there were a greater divisor of n + m and m,
that greater divisor would have to divide n, contradicting the assumption that
d is the GCD of n and m. And similarly, for the other inductive clause. So. we
have proved the desired property. In fact our (perhaps implicit) recognition of
this property was what led us to write this definition in the first place.
The computational model and the proof method are fundamentally inter-
twined. When we wrote the inductive definition we had in mind the property we
wanted the set to have, and ensured that each rule preserved that property. I.e.,
we had the proof of correctness directly in mind when we wrote the definition.
We now have an inductive definition of the GCD relation, which has been proved
to be correct. But we want a Prolog program for finding GCD. How do we turn
this inductive definition into a correctly running Prolog program?
We take the inductive definition that was written in English using conven-
tional mathematical notation:
and we directly convert it to a Prolog program. We note that there have been
many extensions to the Prolog language, so it makes a difference which dialect
of Prolog we are working with. For our purposes here, we will assume a rela-
tively primitive Prolog, essentially ISO Prolog. But there are Prolog systems that
support functional notation and general numeric constraints. In these Prologs
different transformations, perhaps including none, would be necessary.
ISO Prolog doesn’t support functional notation, so we need to introduce
new variables (identifiers starting with upper-case letters) for the sums in the
atomic formulas. And we can convert the sums to use Prolog’s general arithmetic
construct, is/2:
gcd(N,N,N) :- N > 0.
gcd(NpM,M,D)) :- gcd(N,M,D), NpM is N + M.
gcd(N,NpM,D)) :- gcd(N,M,D), NpM is N + M.
This form now is a Prolog program in that it satisfies Prolog syntax. However, to
determine if it will correctly execute to solve a problem, we have to consider the
queries we will ask. In our case, we want to provide two integers and have Prolog
determine the GCD of those two integers. This mode of the queries we will ask
is denoted by gcd(+,+,-), where + indicates a value is given and - indicates
a value is to be returned by the computation. Here we intend to give the first
two arguments and expect the have the third one returned. And we also need to
Writing Correct Prolog Programs 65
know the mode of each subquery in order to determine if the definition can be
executed by the Prolog processor.
Correct Prolog evaluation depends on subquery modes in two ways: 1) sub-
queries must have only finitely many answers, and 2) Prolog’s predefined predi-
cates (like is/2) often work only for particular modes and so the modes for calls
to those predicates must be acceptable.
We must check that the modes of our Prolog program for GCD are correct.
The first requirement for correct evaluation of a query (or subgoal) is that it
must have only finitely many answers. Since Prolog uses backward-chaining, it
poses a number of subgoals during its computation starting from an initial goal.
We must be sure that every one of those subgoals has only finitely many answers.
Otherwise, Prolog would go into an infinite loop trying to generate all infinitely
many answers to such a subgoal. For example, the goal gcd(27,63,D) has only
finitely many instances in the set defined by the gcd program; actually only one
instance, with D=9. But the goal gcd(N,63,9) has infinitely many instances in
the defined set; including all those with N being a multiple of 9. The lesson here
is that we need to understand the modes of all subgoals generated by a Prolog
computation. The mode of a subgoal describes where variables appear in it, and
this affects whether it matches infinitely many set members or not.
We note that since Prolog calls goals in the body of a rule in a left-to-right
order, the mode of a subgoal is determined by the success of goals to its left in
the rule body, as well as by the mode of this initial call. We assume that the
initial goal that we pose to this program will have the first two fields as numbers
and the third field as a variable. I.e., we’ll be asking to find the GCD of two
positive integers. This mode is expressed as gcd(+,+,-).
We next explore the modes of the subgoals in our Prolog program above.
Under this mode assumption, since any call to gcd will have its first two argu-
ments bound to values, the variable N in the subgoal N > 0 in the first clause
will have a value as required by Prolog so this condition can be checked.
In the second clause NpM and M will be integers (by the mode assumption),
so in the first subgoal, NpM is N + M, NpM and M will have values, but N will be
a variable. That means that the call to gcd(N,M,D) will have mode (-,+,-),
and this subgoal will have infinitely many answers. So, this Prolog program will
not execute correctly. (The third rule suffers from a similar problem.) We need
N to get a value before the call to gcd(N,M,D). N appears in the second subgoal
of that condition, so let’s try evaluating that subgoal first. To this end we move
the is/2 goals earlier and get a new program:
gcd(N,N,N) :- N > 0.
gcd(NpM,M,D)) :- NpM is N + M, gcd(N,M,D).
gcd(N,NpM,D)) :- NpM is N + M, gcd(N,M,D).
Now in the call to is/2, NpM and M will have values (because of the mode
assumption for calls to gcd/3). Prolog, however, requires is to have the mode
of is(?,+). (The “?” indicates either “+” or “-” is allowed.) Since N will not
have a value, the second argument to is/2 will not have a value and this rule will
66 D. S. Warren
result in an error when evaluated with the expected values. But we can change
the is/2 to compute N from MpN and M by writing N is MpN - M. This imposes
the equivalent constraint among N, M and MpN and is correctly modded for Prolog
to evaluate it. Similarly fixing the third clause gives us a program:
gcd(N,N,N) :- N > 0.
gcd(NpM,M,D)) :- N is NpM - M, gcd(N,M,D).
gcd(N,NpM,D)) :- M is NpM - N, gcd(N,M,D).
In the second clause now, the first body subgoal causes N to get a value, so the
second subgoal is called with N and M with values, and thus in the same mode as
the original mode, that is gcd(+,+,-). Similarly for the third clause. Thus all
calls to gcd/3 (and is/2) will be correctly moded.
However, there is still an issue with this program: Prolog computes with
integers, not natural numbers, and so the subtraction operations might gener-
ate negative integers. But we want only positive integers. So we must add this
constraint explicitly as follows:
gcd(N,N,N) :- N > 0.
gcd(NpM,M,D)) :- N is NpM - M, N > 0, gcd(N,M,D).
gcd(N,NpM,D)) :- M is NpM - N, M > 0, gcd(N,M,D).
Only when we generate a new integer do we need to check that it is positive.
And we must do the check after the number variable gets a value to satisfy the
mode requirement of </2; immediately after it gets that value is best. This is
now a good Prolog program for computing the GCD of two integers. You might
recognize this as the Euclidean algorithm for GCD. I.e., the Euclidean algorithm
is the top-down evaluation (i.e., Prolog evaluation) of this inductive definition.
Actually, we can make this algorithm slightly more efficient, and maybe make
it look a bit more like Euclid’s algorithm by noting in the second clause (and
analogously in the third) that N will be greater than 0 only if NpM is greater than
M, so we can make that check before taking the difference, getting:
gcd(N,N,N) :- N > 0.
gcd(NpM,M,D)) :- NpM > M, N is NpM - M, gcd(N,M,D).
gcd(N,NpM,D)) :- NpM > N, M is NpM - N, gcd(N,M,D).
(Renaming the variables in the third clause might make it look even more famil-
iar.) Now we can see that only one of the three clauses can ever satisfy its
comparison condition for a (correctly moded) subgoal, and so the Prolog com-
putation is deterministic.
See Kowalski et al. [5] for another development of Euclid’s algorithm, there
in a more English-like dialect of Prolog.
Let’s recap how we approached Prolog programming. We followed a sequence
of steps, which we will describe in some generality here. They are a generalization
of the steps we just used in our development of the gcd Prolog program.
1. Use inductive clauses to define the relation, a set of tuples, that characterizes
the solution to the problem of interest. Use names for the relations to organize
Writing Correct Prolog Programs 67
and name sets of tuples. Above we used the name gcd as the predicate symbol
to remind us that it is a set of triples that define the Greatest Common Divisor
function. Use whatever mathematical notation is convenient. Similarly, define
and use whatever sets of tuples are useful as subsidiary definitions.
2. Convert this mathematical definition into Prolog clauses, using the necessary
Prolog built-ins.
3. Consider the mode of each subgoal that will be invoked during top-down
evaluation of the clauses. Ensure that the subset of each relation required
to answer each moded subgoal is finite. Ensure that all built-ins are invoked
in modes that they support. Ensure that defined subgoals are all invoked in
desired modes, by ordering the subgoals of rule bodies so their left-to-right
evaluation is well-moded and efficient.
Notice that in developing this program, we did not consciously think about
recursive programming. We thought about an inductive definition. Recursion is
the procedural mechanism used to evaluate inductive definitions top-down. The
procedural mechanism to evaluate inductive definitions bottom-up is iteration.
To understand the correctness of an inductive definition we can think iter-
atively; just iterate the application of the inductive rules, starting from empty
relations. Intuitively, this is easier to understand than recursive programming.
The recursion showed up in the final program because of the form of the rules
of the inductive definition. We know the recursive evaluation will give the cor-
rect answer because top-down evaluation computes the same answer (when it
terminates) as the bottom-up iterative evaluation.
Many others have noted the importance of bottom-up thinking. The deduc-
tive database community (see, e.g., [7]) looks at programs with only constant
variable values, so-called Datalog programs, exclusively bottom up. And teach-
ers of Prolog teach bottom-up evaluation, and sometimes provide bottom-up
evaluators for students to use to understand the programs they write, [4].
3 The Claim
Let’s look at what needs to be done to write a correct program. First we have to
determine what it means for a correct program to be correct. For that we need
to have an idea in our heads of what our program should do, i.e., a property
the program should have, i.e., for a logic program a property that must hold of
every tuple in the defined relation. And we must ensure that it contains every
tuple it should. Then we have to write a program that defines a relation that
has that property.
We could require that this whole process be done formally, i.e., do a formal
verification of our program. In that case, we would specify the correctness prop-
erty, a.k.a. the program specification, in some formal language. Then we would
generate a formal proof that the program we created satisfied that specifica-
tion. This turns out to be rather complicated and messy, and for large practical
programs essentially undoable. Almost no large programs in practice are ever
proved correct in this way. (See [2] for a discussion of relevant issues.)
68 D. S. Warren
In lieu of formal verification we might use informal methods that won’t guar-
antee exact program correctness but might provide some confidence that our
programs do what we want them to do. We argue that the informal program
development strategy that we have described above does just that.
When we develop an inductive definition of a relation that satisfies our intu-
itive correctness criteria (what we want the program to do), we are thinking
of bottom-up generation of a satisfying relation. And the bottom-up generation
must generate correct tuples at every step. And seeing that rules always gen-
erate correct tuples is exactly what a proof of correctness requires. Indeed, the
name given to the predicate is naturally a shorthand for its correctness property.
We named our predicate gcd because of the Greatest Common Divisor property
that we wanted its tuples to have. So, when generating an inductive definition
of a relation, one has directly in mind the property all its tuples must have,
and so writes rules that guarantee that property. This is exactly the thinking
necessary to formulate a proof of correctness. In this way the thinking required
to formulate the program is exactly the thinking require to formulate a proof of
its correctness. Even if the proof is not formally carried out, the intuitive ideas
of how it would be created have already been thought through.
Note, however, that such an inductive proof is not a formal proof of correct-
ness for a Prolog program. That would require formal consideration of modes,
occur-check, termination, and other important details of actual Prolog evalua-
tion. Discussions of formal proofs of total correctness of Prolog programs in the
literature tend to focus on these issues, e.g., [1].
Of course, most Prolog programs are more complicated than a single induc-
tive definition. Most require multiple subsidiary relations to be defined and then
used in more complex definitions. But each such subsidiary relation is also induc-
tively defined and a similar methodology can be used for them. For procedural
programs with while and for constructs, one needs to generate an invariant
for each loop; the corresponding logic program requires a relation, with its own
name and inductive definition, for each loop, thus laying bare the correctness
criteria that may be hidden for iterative programs with loops.
4 Caveats
What is proposed here is an idealized methodology for developing Prolog pro-
grams. Of course, it won’t always work this way. Prolog programmers learn (and
are encouraged) to think about top-down execution and recursion in a procedural
way. Indeed, to develop definitions that evaluate efficiently top-down, it is often
necessary to think in this way. So almost always an experienced Prolog program-
mer will develop a program without ever thinking about how it would evaluate
bottom up. My suggestion is that programmers should initially be taught this
bottom-up-first methodology, and then as they advance and develop their top-
down intuitions, they should always go back and look at the bottom-up meanings
of their programs. As a practicing top-down Prolog programmer, I’ve found it
often enlightening to think of my programs in a bottom-up way. Sometimes effi-
cient top-down programs are infinite, or ridiculously explosive, as bottom-up
Writing Correct Prolog Programs 69
programs. But experience can make them intuitively understandable, and think-
ing that way provides insight. It deepens an understanding of what the program
defines and can sometimes uncover problems with it. It is also worth noting that
bottom-up evaluation is independent of problem decomposition, which is a good
development strategy, independent of any evaluation strategy.
Bottom-up evaluation is generally easier to understand not only because it
is based on iteration instead of recursion as is top-down. Every state reachable
in a bottom-up construction satisfies the desired correctness property. But in
top-down, many reachable states may not satisfy the correctness property; they
may be states only on a failing derivation path. This means that for top-down
one must distinguish between states that satisfy the desired property and those
encountered along a failing path towards a hoped-for justification. It’s more to
keep track of in one’s head. Perhaps another way to say it is that bottom-up
evaluation is intuitively simpler in part because it needs no concept of failure.
5 Conclusion
I would claim that mathematical induction provides the formal foundation of all
algorithmic computation, i.e., computation intended to terminate1 . Prolog asks
programmers to give inductive definitions directly. The form of definition is par-
ticularly simple, being straightforward rules for adding members to sets. Since
the programmer creates definitions thinking directly in terms of the mathemati-
cal foundations of computation, there is less of a distance between programming
and proving. This makes for programs more likely to do what the programmer
intends.
References
1. Apt, K.R.: Program verification and Prolog. In: Börger, E. (ed.) Specification and
Validation Methods, pp. 55–95. Oxford University Press, Oxford (1993)
2. DeMillo, R., Lipton, R., Perlis, A.: Social processes and proofs of theorems and
programs. In: Tymoczko, T. (ed.) New Directions in the Philosophy of Mathematics:
An Anghology, pp. 237–277. Birkhauser Boston Inc., Boston (1986)
3. Denecker, M., Warren, D.S.: The logic of logic programming. CoRR,
cs.LO/2304.13430, arXiv:2304.13430 (2023)
4. Hermenegildo, M.V., Morales, J.F.: Some thoughts on teaching (and preaching)
Prolog. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi,
F. (eds.) Prolog - The Next 50 Years. LNCS, vol. 13900, pp. 107–123. Springer, Cham
(2023)
5. Kowalski, R., Quintero, J.D., Sartor, G., Calejo, M.: Logical English for law and
education. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R.,
Rossi, F. (eds.) Prolog - The Next 50 Years. LNCS, vol. 13900, pp. 287–299. Springer,
Cham (2023)
1
Clearly quantum computation has a different foundation. And infinite computations
have their foundations in co-induction.
70 D. S. Warren
What was missing was a standard reference to a single operation that could
be implemented in different ways, thus demonstrating the flexibility of the lan-
guage which, however, may raise issues for discussion around declarative versus
procedural approach, readability versus writability of code and simplicity versus
efficiency. We found that list_min predicate could serve this purpose. We fol-
lowed different approaches in our own institutions; either to present this example
throughout the semester as we introduce concepts together with other examples,
or to devote a revision session towards the end of the semester to summarise
the language potential before the final exam. Results were encouraging in both
approaches.
Therefore, we demonstrate how a single list operation, i.e. the minimum of
a list, can prove to be a rather helpful tool to refer to a plethora of Prolog pro-
gramming aspects and techniques. The benefits are numerous: (a) it is a simple
operation, with which students are familiar with and thus presents no barriers
in understanding its specification, (b) it has a surprising number of alterna-
tive implementations, (c) benchmarking of the alternatives is a straightforward
task, allowing discussion of complexity issues that arise. The current paper con-
tributes towards demonstrating Prolog flexibility, especially in audiences just
getting familiar with Prolog, by:
– discussing how a single simple list operation can demonstrate a number of
programming techniques, through alternative implementations,
– presenting how such simple code can expose a number of lower level issues of
some techniques, such as garbage collection and tail recursion optimization.
The paper is not aimed to be a tutorial in Prolog, as this is presented earlier
in this book [14]. It is assumed that the reader has some knowledge of the
language. Additionally, our audience includes educators who would like to adopt
this example throughout their courses on top of all existing material they teach
as a single point of reference suitable for discussing the flexibility of the language.
The term “programming technique” has been used in different contexts: we adopt
the definition that a technique is a pattern, applied to a wide range of problems
[2], as for example failure driven loops, generate-and-test and accumulator pairs,
among many others.
A schema is a representation of a technique that allows developers to repre-
sent a general structure solution code to a problem, so that, given the specific
context, the schema may be instantiated to executable code that solves the par-
ticular problem. The schema approach to teaching Prolog was introduced rather
early, as for instance in [5], where also a single example was used. Although we
follow the same approach, our focus is different, since we contend that the single
task used can serve to discuss a wider number of issues, as for example efficiency
and non-recursive techniques.
Probably the most widely used approach for logic programs is recursion. It is
derived through the mathematical concept of induction [15] and assumes that a
problem of a certain size, say N, can be solved if the same problem of a smaller
size, say N-1, is solvable. Although a powerful approach, that provides for sim-
ple solutions of rather complex problems even when considered in a procedural
74 N. Bassiliades et al.
mindset, it is still a skill that most novice programmers lack, or have not been
exposed and exercised to a great extent.
min(X,Y,X) :- X=<Y.
min(X,Y,Y) :- X>Y.
Although, arithmetic checks are delayed and placed “after” the recursive call,
committing to the min value is delayed, i.e. variable Min is instantiated at the
last call, after the arithmetic check. This leads to a linear complexity, albeit some
memory inefficiencies. A puzzling point to novice learners is that comparisons
take actually place backwards, from the last element of the list to the first,
usually referred to as building the solution “on the way up”.
This version offers itself to talk about the if-then-else construct (without
explicit reference to the hidden cut) by rewriting the code for min/2 as follows:
list_min21([Min],Min).
list_min21([H|Tail],Min) :-
list_min21(Tail,MinTail),
(H>MinTail -> Min=MinTail; Min=H).
Although, some may find that the above presents a more readable implemen-
tation, it does contain explicit unification, i.e. the use of the “=” operator, which
can be easily (mis)taken for assignment.
list_min3([H|T],Min) :-
list_min3_aux(T,H,Min).
list_min3_aux([],Min,Min).
list_min3_aux([H|T],TempMin,Min) :-
min(H,TempMin,NextMin),
list_min3_aux(T,NextMin,Min).
Alternatively, since now the comparison is done before the recursive call,
we could avoid the use of the min/3 predicate and have instead two recur-
sive calls, without causing so much inefficiency this time, as shown in predicate
list_min4/2). However, to maintain efficiency, it is needed to explicitly insert
the cut operator leading to a check-and-commit technique.
list_min4([H|T],Min) :-
list_min4_aux(T,H,Min).
list_min4_aux([],MSF,MSF).
list_min4_aux([H|T],MSF,Min):-
H < MSF, !,
list_min4_aux(T,H,Min).
list_min4_aux([H|T],MSF,Min):-
H >= MSF, !,
list_min4_aux(T,MSF,Min).
Although the cut in the code above is inserted to take advantage of the
tail recursion optimization, since checks are mutually exclusive, the second
check could be eliminated, leading to a reduced number of checks in the code.
Alternatively, as mentioned above, cut can be implicitly replaced by the “more
declarative” if-then-else construct.
list_min5([M],M).
list_min5([H1,H2|T],Min):-
H1 > H2, !,
list_min5([H2|T],Min).
list_min5([H1,H2|T],Min):-
Multiple Prolog Programming Techniques Through a Single Operation 77
H1 =< H2, !,
list_min5([H1|T],Min).
Selecting two elements instead of a single from the list supports an early arith-
metic comparison, leading to an immediate pruning of the non-successful
branch. What is left to be decided is the repeated execution of the operation
for the rest of the elements, achieved by “pushing” the result (i.e. the minimum
between the two) elements to the top of the list for the next recursive call.
Alternatively, an even more compact version of the predicate relies on the
min/3 predicate mentioned previously (predicate list_min6/2).
list_min6([Min],Min).
list_min6([H1,H2|T],Min):-
min(H1,H2,M),
list_min6([M|T],Min).
list_reduce([Value],_,Value).
list_reduce([H1,H2|T],Operation,Value):-
C =.. [Operation,H1,H2,Next],
call(C),
list_reduce([Next|T],Operation,Value).
list_min7(List,Min):-
member(Min,List),
not((member(X,List), X < Min)).
78 N. Bassiliades et al.
Another version (list_min8/2) mainly used for illustrating the operation of the
setof solution gathering predicate, stressing that it is a clever trick, but with
higher-than-needed computational cost.
list_min8(List,Min) :-
setof(X,member(X,List),[Min|_]).
This version exploits the builtin predicate setof and follows the naive algo-
rithmic thinking of sorting a list in ascending order to return its first element.
However, one needs to realize that sorting has a larger average complexity
O(n log n) than finding the minimum O(n), so in general it should be avoided.
No matter how simple a programming language can be, some of its features may
be used to create the “unthinkable”. The same happens with Prolog’s ability to
alter its program while the program is executed, i.e. asserting and retracting
clauses on the fly. We refer to this version (predicate list_min9) as the “one
to avoid”, since it relies on a “global” variable implemented as a separate
dynamic predicate, to simulate destructive assignment. It offers the opportunity
to present a number of issues regarding assert/retract, as well as the necessity
of side-effects inside a failure-driven loop.
list_min9([H|_T],_Min) :-
assert(temp_min(H)),
fail.
list_min9(List,_Min) :-
member(X,List),
retract(temp_min(TmpMin)),
min(X,TmpMin,NextMin),
assert(temp_min(NextMin)),
Multiple Prolog Programming Techniques Through a Single Operation 79
fail.
list_min9(_List,Min) :-
retract(temp_min(Min)).
We do not present this as a technique that someone should adopt. We just
mention it as an extreme example of how flexible and “dirty” Prolog programming
can get!
All predicates we tested (most of them recursive) scan the whole list once
no matter the type of list. So, with minor deviations in any of the ascending,
descending, or random order, the number of inferences is more or less the same
(the extra lines from one predicate to the other cause the extra inferences but
play a minor role). Thus, performance is independent of the type of list.
Minor differences among predicates in the figure are attributed to the order
of checks. For instance, in list_min2 arithmetic comparisons occur “backwards”,
whereas in list_min3 occur on the “way down” to the base recursive case, thus
showing slightly different behaviour on the extreme cases (ascending/descending
order). It should be noted that the assert/retract version seems to be unaf-
fected by the order of elements in the list and yields the higher number of
inferences, due to constantly accessing Prolog memory.
Regarding student perception, the definitions of list_min8, list_min2 and
list_min7 were among the first three preferences, gathering 72%, 48% and
40% respectively of the students who preferred them in their top-three choices,
although the last one requires a considerable number of inferences compared to
all the rest. All other versions were roughly equally preferred. It was surprising
that 16% of the students declared as a top-three choice list_min1; it takes an
enormous amount of cputime to complete which makes it practically useless but
it was preferred for its readability. Even more surprising is that 20% included
list_min9 in their best three choices; it is extremely complex and far from
purity but it may match the programming style that learners have been exposed
to in previous courses.
6 Conclusions
We presented the flexibility of Prolog by using a single operation and multiple
programming techniques that result in different implementations. Each version of
the predicate list_min allows space to discuss all interesting features of Prolog.
The code variations gave us the opportunity to discuss declarativeness versus
efficiency issues as well as readability, purity and dirty characteristics of the
Multiple Prolog Programming Techniques Through a Single Operation 81
References
1. Bratko, I.: PROLOG Programming for Artificial Intelligence, 4th edn. Addison-
Wesley Longman Publishing Co., Inc, USA (2012)
2. Brna, P., et al.: Prolog programming techniques. Instr. Sci. 20(2), 111–133 (1991).
https://doi.org/10.1007/BF00120879
3. Clocksin, W.F., Mellish, C.S.: Programming in Prolog, 5 edn.. Springer, Berlin
(2003). https://doi.org/10.1007/978-3-642-55481-0
4. Flach, P., Sokol, K., Wielemaker, J.: Simply logical - the first three decades. In:
Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.)
Prolog: 50 Years of Future, LNAI 13900, pp. 184–193. Springer, Cham (2023)
5. Gegg-Harrison, T.S.: Learning prolog in a schema-based environment. Inst. Sci.
20(2), 173–192 (1991). https://doi.org/10.1007/BF00120881
6. Hermenegildo, M.V., Morales, J.F., Lopez-Garcia, P.: Some thoughts on how to
teach prolog. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski,
R., Rossi, F. (eds.) Prolog: 50 Years of Future, LNAI 13900, pp. 107–123. Springer,
Cham (2023)
7. Hogan, A., Blomqvist, E., Cochez, M., D’amato, C., Melo, G.D., Gutierrez, C.,
Kirrane, S., Gayo, J.E.L., Navigli, R., Neumaier, S., Ngomo, A.C.N., Polleres, A.,
Rashid, S.M., Rula, A., Schmelzeisen, L., Sequeda, J., Staab, S., Zimmermann, A.:
Knowledge graphs. ACM Comput. Surv. 54(4), 1–37 (2022). https://doi.org/10.
1145/3447772
8. Morales, J.F., Abreu, S., Hermenegildo, M.V.: Teaching prolog with active logic
documents. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R.,
Rossi, F. (eds.) Prolog: 50 Years of Future, LNAI 13900, pp. 171–183. Springer,
Cham (2023)
9. O’Keefe, R.A.: The Craft of Prolog. MIT Press, Cambridge (1990)
10. Ross, P.: Advanced Prolog: Techniques and Examples. Addison-Wesley (1989)
11. Sekovanić, V., Lovrenčić, S.: Challenges in teaching logic programming. In:
2022 45th Jubilee International Convention on Information, Communication and
Electronic Technology (MIPRO), pp. 594–598 (2022). https://doi.org/10.23919/
MIPRO55190.2022.9803530
12. Sterling, L., Shapiro, E.: The Art of Prolog (2nd Ed.): Advanced Programming
Techniques. MIT Press, Cambridge (1994)
13. Van Someren, M.W.: What’s wrong? Understanding beginners’ problems with Pro-
log. Instr. Sci. 19(4), 257–282 (1990). https://doi.org/10.1007/BF00116441
14. Warren, D.S.: Introduction to Prolog. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog: 50 Years of Future, LNAI
13900, pp. 3–19. Springer, Cham (2023)
15. Warren, D.S.: Writing correct prolog programs. In: Warren, D.S., Dahl, V., Eiter,
T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog: 50 Years of Future,
LNAI 13900, pp. 62–70. Springer, Cham (2023)
A Better Logical Semantics for Prolog
1 Introduction
This paper describes a Logic of Definitions, which provides a semantics for the
Prolog programming language [13]. All the ideas here are more fully presented
in [6],1 which is itself an explication of earlier ideas developed by Denecker and
his group in [5]. The primary differences from the presentation in that paper
are that here we restrict our models to be Herbrand models and we use the
Clark completion semantics to motivate this approach to improved Prolog logical
semantics. We eliminate any formalism and focus on the informal motivation and
discussion. There is nothing in this paper that is not in that original paper. The
hope is that this paper may be more accessible to a wider audience of Prolog
programmers, and perhaps provide intuitions that will help in understanding
that paper.
Prolog evaluators answered “yes” to ground queries for which it could find an
SLD proof and “no” when no proof was found. Early Prolog evaluators included
an operation called not, which was described as not provable.2 This shows the
recognition that the failure of a query indicates that it is not provable, and not
that it is logically false. But the “yes” response for true queries suggests that
the “no” response might be for queries that are false, and many programmers
productively interpreted that “no” as logically false. The attempt to make this
interpretation logically correct led to a search for a new logic-based semantics
for positive Prolog that would indeed imply failing queries are logically false.
Seminal papers in this research effort include:
1. In 1977 van Emden and Kowalski [11] provide a definition of the least Her-
brand model (LHM) and its construction, for positive Prolog programs, and
showed that the atoms true in the LHM were exactly the atoms for which
there is an SLD proof.
2. In 1977 Clark [4] defines the completion of a positive Prolog program (by
essentially turning the Horn clause implications into biconditionals) and
shows that a (slightly modified) SLD resolution finitely fails for a (ground)
query if and only if the program completion logically implies that the query
is false.
3. In the mid-eighties, work by Chandra and Harel [3], Apt, Blair, and Walker
[2] and Przymusinski [10] built on earlier work on fixpoint logic [9] and con-
tributed to the definition of the perfect model as the generally accepted mean-
ing for stratified programs. The (unique) perfect model of a positive program
is its Least Herbrand Model. Since according to this semantics, a positive
program has only a single model, an atom false in this model is false in all
models and thus is logically false.
This idea of using a single model to provide the semantics for a Prolog pro-
gram seems to have taken hold when the database community became inter-
ested in using the function-free subset of positive Prolog as a logic of deductive
databases, later called Datalog.
This semantics, known as the LHM semantics, has become widely accepted
by the logic programming community.
A Herbrand structure (see [7]) is a first-order structure for a language in
which every ground term of the language names a distinct object (the unique
names assumption) and all objects are named (the domain closure assumption).
Restricting to Herbrand structures allows programmers to use functions to store
and later retrieve data, as it allows arguments to functions to be recovered from
their applications. For the remainder of this paper we assume that all structures
are Herbrand structures.
For positive Prolog programs (as we consider here), Tarski’s fixpoint theorem
guarantees that any set of Horn clauses, i.e., any Prolog program, has a unique
minimal Herbrand model, its LHM. As shown in [11], it can be constructed in a
2
The form of the not operator in most Prologs is \+, which is intended to approximate
in ASCII the logical proves symbol with a slash / through it.
84 D. S. Warren and M. Denecker
bottom-up fashion by starting with the empty set, and then iteratively adding
ground atomic formulas that are heads of ground rule instances that have all
body literals in the current set. This continues until nothing more can be added
(perhaps to ω). The resulting set of atomic formulas determines the Herbrand
structure in which the atomic formulas derived in this process are true, and all
others are false. And this structure is a model of the program Horn clauses,
satisfying them all. Under this semantics, every (positive) Prolog program has a
unique model, so a query is true if it is true in all models, i.e., true in the single
LHM, and it is false if it is false in all models, i.e., false in the single LHM.
In [11] it was proved that the atoms true in the LHM of a program are exactly
the atoms that SLD resolution proves true. This means that if SLD succeeds on
a ground query, it is true in the LHM and thus logically true. If SLD fails on a
ground query, it is false in the LHM and thus logically false. Thus a “no” answer
to a Prolog query demonstrates that the query is logically false.
But the claim of this paper is that there are problems with the LHM seman-
tics. Specifically, the LHM gives meanings only to full programs, but not to pro-
gram components. No meanings are given to subcomponents of full programs.
And this clearly violates the intuitions of Prolog programmers. Prolog program-
mers undeniably give meaning to program components.
Consider the example of a teacher of introductory Prolog who gives the class
an assignment to write a predicate defining “sibling” in terms of a base “childOf”
predicate that will work for any definition of “childOf”, and then to exemplify
it with their own nuclear family. One student submits the program:
childOf(tessa,david).
childOf(jonah,david).
sibling(tessa,jonah).
sibling(jonah,tessa).
The teacher marks this wrong, so the student responds by asking what a correct
answer would be. And the teacher replies:
childOf(tessa,david).
childOf(jonah,david).
sibling(X,Y) :- childOf(X,P), childOf(Y,P), X \== Y.
To which the student replies that his program has exactly the same meaning,
i.e. LHM, as the teacher’s program, so it should be accepted as a reasonable
alternative correct answer to the problem. The teacher reminded the student
that she asked for a definition that would work for all childOf relations, as does
the rule above. But, replies the student, the meaning of that rule alone, under
the LHM, is that sibling is the empty relation, since that is the LHM of that
rule alone. Clearly there is a problem.
The problem is that the teacher wants a definition of sibling, that holds for
every value of the childOf relation, not only for the family of the student. But
the LHM semantics has nothing like this to say. It can give meaning only to
complete programs, here when sibling and childOf are both defined. But the
A Better Logical Semantics for Prolog 85
teacher gives a meaning, and wants the student to give that same meaning, to
a component of the program, the component consisting of the single rule for
sibling.
3 Clark Completion
An early proposal to provide a semantics for positive Prolog programs that
accounted for queries being false was made by Keith Clark in [4]. He said that
programs were not made up of individual Prolog clauses, but of Prolog defini-
tions. That is, the set of clauses that have the same predicate symbol in the head
together make up a definition of a relation. And that definition for a predicate
can be constructed from the rules for that predicate by putting them together
into one conditional formula whose head is the predicate with all distinct vari-
ables as arguments, and whose body comes from the disjunction of the bodies of
the rules. We introduce explicit existential quantifiers and equalities to ensure
that this single implication is equivalent to the set of original implications for the
predicate. And now, in this single formula, we change the if into an if-and-only-if,
which is intended to turn it into a definition.
The sibling rule becomes the definition:
Here, in this program, the singleton set of rules defining the childOf rela-
tion expresses the correct definition of the concept of sibling in terms of binary
relation of being a child of. A Herbrand structure, whose language includes the
two predicate symbols: sibling/2 and childOf/2, is a model of that formula if
it makes that formula true. A structure makes it true if for whatever relation it
assigns to childOf, it assigns the right sibling relation to the sibling predicate,
i.e., the sibling relation in the family represented by the relation for childOf.
So this semantics does provide a meaning to individual predicate definitions and
indeed exactly the meaning we (and our poor teacher) want; that the sibling
rule gives the correct definition for any possible family.
Since the Clark completion semantics does the right thing for this definition
and many, many others, why is it not the dominant semantics for Prolog?
But this formula does not define transitive closure as required. Actually, given
an edge relation, this may not define a relation at all. To define the tc relation,
it must be the case that for any particular edge relation, this formula uniquely
determines the set of tuples that tc is true of. But this formula doesn’t do that.
Consider the edge relation:
edge(a,a). edge(b,a).
Consider the two Herbrand structures, described by the set of ground formulas
true in them:
M1 = {edge(a,a), edge(b,a), tc(a,a), tc(b,a)}.
M2 = {edge(a,a), edge(b,a), tc(a,a), tc(b,a), tc(a,b)}.
These are both models of the tc if-and-only-if formula above and they have the
same interpretation for edge. M1 is clearly the transitive closure as intended. So
how can the tc(a,b) fact be true in M2 and it still be a model of (3)? Consider
the instance of (3):
The left-hand is true in M2, but also the right hand is true: take Z=a! Hence
M2 is a model. Therefore, (3) does not (always) define a relation.
For those knowledgeable about FOL this does not come as a surprise. In
fact, there is no first-order (FO) formula that defines transitive closure. This is
a well-known fact (and limitation) of FOL.3
p(X) ⇐⇒ Ψ (X, p, q)
3
See a footnote in [6] for a proof.
A Better Logical Semantics for Prolog 87
M 3 = {edge(a, a), edge(b, a), edge(c, b), tc(a, a), tc(b, a), tc(c, b), tc(c, a), q(d)}
88 D. S. Warren and M. Denecker
is true in M3. So we compute the least fixpoint, starting with the parameter
predicate facts, those for edge, that are true in the structure:
{edge(a, a), edge(b, a), edge(c, b), tc(a, a), tc(b, a), tc(c, b)}
{edge(a, a), edge(b, a), edge(c, b), tc(a, a), tc(b, a), tc(c, b), tc(c, a)}
{edge(a, a), edge(b, a), edge(c, b), tc(a, a), tc(b, a), tc(c, b), tc(c, a)}
The set is unchanged on this last iteration, so we have reached the fixpoint. Now
we check if the four tc facts in the fixpoint are exactly those in the structure
M3. In fact, they are so M3 makes this formula true and is thus a model of
this formula. Notice that this formula remains true when we have any other
additional facts for any predicates other than tc and edge. For example, M 3 has
q(d) which had no effect on the truth or falsity of the tc formula.
And we can see, by a similar (actually the same) construction, that:
{edge(a, a), edge(b, a), edge(c, b), tc(a, a), tc(b, a), tc(c, b), tc(c, a), t(a, c)}
7 Negation
For 50 years now, the general conviction is that the negation not in Prolog can-
not be classical negation ¬. This belief stems from the fact that Horn theories
do not entail the falsity of atoms. But the definitional view sheds a completely
different light on the issue. Indeed, definitions do semantically entail the falsity
of any defined atomic formula A that is false in the LHM. Therefore, any pro-
gram interpreted as an inductive definition semantically entails the truth of the
classically negated atomic formula ¬A. As such the finite failure inference rule
can be understood to infer the falsity of A, and hence, the truth of the classically
negated ¬A. As such, negation is here classical negation, not negation-by-failure.
It is the meaning of the “:-” symbol that has changed; it is not implication but
indicates an inductive rule. To conclude, the definitional view on logic programs
sheds a different light on the nature of language constructs: negation as failure
is indeed classical negation! It is the rule operator that is non-classical: much
stronger than a material implication, it is a definitional operator of inductive
definitions.
8 Discussion
As a final example of the importance of providing meaning for program compo-
nents, consider Datalog, a sublanguage of Prolog in which there are no complex
data structures, only constants. In Datalog one has a set of stored relations, the
extensional database, and then one writes queries (or views) to ask questions of
the database. Datalog programmers must understand their view definitions and
queries independent of any specific extensional database state. The whole point
of writing a query is to find out something unknown about the current database
state. For example, if we define a view and write a query to it, such as:
rich(Emp) :- employee_sal(Emp,Salary), Salary > 300000.
| ?- rich(david).
we are trying to get information about an unknown extensional relation
employee sal. We want to know if, in that unknown relation, David’s salary
is greater than $300,000. That is why we wrote this query. It doesn’t make sense
to think that the extensional database state, i.e., the contents of the relation
employee sal must be known before we can understand this query, as is done
in the original LHM program semantics. That is exactly backwards: we under-
stand the view and query and use that understanding to find out something
we don’t know about the extensional database. The semantics provided in this
paper explains how this actually works.
A Better Logical Semantics for Prolog 91
9 Conclusion
We have extended first-order logic by adding a new kind of “formula,” the induc-
tive definition using the new connective . And we have described how these
inductive definition formulas are true or false in Herbrand models. The only
(but it is a big only) change to Clark’s completion semantics is that we have
changed the idea of definition from first-order definition to inductive definition.
One way to think about it is that this semantics now chooses only the least
fixpoint as a model of the definition, not any fixpoint as Clark’s FO definitions
do.
This has allowed us to construct a semantics for (positive) Prolog that is
compositional, i.e., in which the meaning of a program is a function (here con-
junction) of the meanings of its components. This is not only theoretically ele-
gant, it is fundamentally practical. It correctly reflects and models how people
write and understand large programs, one piece at a time.
References
1. Aczel, P.: An introduction to inductive definitions. In: Barwise, J. (ed.) Handbook
of Mathematical Logic, pp. 739–782. North-Holland Publishing Company (1977)
2. Apt, K.R., Blair, H.A., Walker, A.: Towards a theory of declarative knowledge. In:
Minker, J. (ed.) Foundations of Deductive Databases and Logic Programming, pp.
89–148. Morgan Kaufmann (1988)
3. Chandra, A.K., Harel, D.: Horn clauses queries and generalizations. J. Log. Pro-
gram. 2(1), 1–15 (1985)
4. Clark, K.L.: Negation as failure. In: Gallaire, H., Minker, J. (eds.) Logic and Data
Bases, Symposium on Logic and Data Bases, Centre d’études et de recherches de
Toulouse, France, 1977, Advances in Data Base Theory, pp. 293–322, New York
(1977). Plemum Press
5. Denecker, M., Ternovska, E.: A logic of nonmonotone inductive definitions. ACM
Trans. Comput. Log. 9(2), 14:1–14:52 (2008)
92 D. S. Warren and M. Denecker
1 Introduction
After 50 years, the state of Prolog exhibits both strengths and weaknesses. Prolog
is widely acknowledged as the leading framework for employing logical reasoning
in general programming. Even its detractors admit that Prolog represents an
important alternative to imperative and functional programming and that many
of its features (e.g., unification, representation of object structure via terms,
query-based computation) are intriguing. Additionally, Prolog is still a living
language, with many actively maintained systems that support extensions to
vanilla Prolog such as constraint-based reasoning, tabling, program analysis,
multi-threading and probabilistic deduction.
Indeed, one sign of Prolog’s health is that, as of this writing, there are
numerous actively-maintained Prologs that are (usually) ISO-compliant includ-
ing SWI [17], SICStus [3], Ciao [8], YAP [12], ECLIPSe [13], GNU Prolog [6],
Picat1 [18], Trealla2 , Tau3 and XSB [15]. q Several of these Prologs are the
results of over a decade of effort, and all have differing strengths. For instance,
Picat, SICStus, and Eclipse are excellent for constraint-based reasoning, Ciao is
1
Picat has a non-ISO syntax, but it is based on the B-Prolog engine [19].
2
https://github.com/trealla-prolog/trealla.
3
http://tau-prolog.org.
This research was developed with funding from the Defense Advanced Research
Projects Agency (DARPA) under contract number FA8750-18-C-0001. The views, opin-
ions and/or findings expressed are those of the authors and should not be interpreted
as representing the official views or policies of the Department of Defense or the U.S.
Government. Distribution Statement “A” (Approved for Public Release, Distribution
Unlimited).
c The Author(s), under exclusive license to Springer Nature Switzerland AG 2023
D. S. Warren et al. (Eds.): Prolog: The Next 50 Years, LNAI 13900, pp. 93–104, 2023.
https://doi.org/10.1007/978-3-031-35254-6_8
94 C. Andersen and T. Swift
particularly strong for program analysis and flexible syntax, YAP is especially
fast for single-threaded SLDNF, SWI offers a fast and stable multi-threaded
engine, and XSB has pioneered a wide array of tabled deduction strategies.
Nonetheless, the authors, who have over 50 years of combined experience in
industry, have seldom seen Prolog adopted for projects except at our instiga-
tion. One immediate reason for this is developer unfamiliarity: in the U.S. few
developers learn Prolog in Computer Science courses, which instead emphasize
popular imperative languages. Many developers are reluctant to confront the
learning curve associated with resolution, backtracking, unification, and build-
ing complex objects from terms and lists.
One avenue for coaxing developers to learn and use logic programming is to
make Prolog more powerful and easier to use. We believe that addressing the
following issues, although they are not exhaustive, will help Prolog become more
widely used in industry. They are listed in what we believe is their increasing
importance.
Our system, Janus, addresses these issues by integrating Prolog and Python
into a single system, putting Prolog at a Python programmer’s fingertips and
4
Here again, SWI’s xpce-based debugger is an exception.
The Janus System: A Bridge to New Prolog Applications 95
vice-versa. Both languages execute within a singe process: this allows the over-
head of calling one language from another to be extremely low, and supports
Janus’s fast bi-translation of large, complex data structures between languages.5
Janus is automatically configured and available when versions 5.0 or later
of XSB are installed. Alternately, Janus also supports configuring and installing
XSB for Python programmers via the Python package manager pip.6 The Janus
code is compact and has already been ported to lvm Prolog developed by Graph-
Stax, Inc., where it is being used in internal applications.
Although Janus is still new, we show that in several projects, it has directly
addressed the package problem by making the huge ecosystem of Python pack-
ages usable in Prolog code with little effort. These include a government research
project (DARPA AIDA), along with several industrial research projects (cf.
Sect. 4). The application areas include natural language processing, visual query
answering, geospatial reasoning, and handling semantic web data. Janus has
also addressed the embeddability problem by enabling the use of XSB within a
Python-based robotics application (Sect. 4.2). Less progress has yet been made
using Janus in IDEs and graphical interfaces, although it has supported XSB use
in Jupyter notebooks and we believe that Janus provides a natural foundation
for such work.
This paper focuses on the impact of Janus for applications, while a companion
paper [14] covers fully its implementation and performance. Accordingly, our
paper is structured as follows. After presenting related work, we briefly outline
the architecture of Janus in Sect. 3.
2 Related Work
Table 1 provides a summary of foreign language interfaces for various Prologs.
Most of the Prologs mentioned in Sect. 1 are written largely in C and, so offer
bi-directional interfaces with C/C++ . Exceptions are Tau Prolog written in
Javascript, and Scryer written in Rust. Many Prologs offer two-way Java inter-
faces, while a few offer interfaces to Python or Javascript, or support for .NET.7
Some of these interfaces have proven quite useful: for example in the U.S. Cus-
toms Automated Targeting System8 Java calls SICStus Prolog millions of times
per day, every day, for name-address standardization [5].
Many interfaces are implemented (e.g. SICStus to .NET, XSB to Java) via
socket connections to a separate process running the other language. This app-
roach is workable for many use cases, but introduces unacceptable latencies for
tightly coupled applications. Also, some interfaces can be non-trivial to set up
5
A separate approach to cobinding Python and Prolog functionality is the natlog
interpreter, written in Python [16].
6
A beta version of the pip interface is available through https://test.pypi.org/project/
python-xsb-installer.
7
SICStus also offers a general-purpose SON-RPC library.
8
www.dhs.gov/sites/default/files/2022-05/privacy-pia-cbp006%28e%29-ats-
may2022.pdf.
96 C. Andersen and T. Swift
Table 1. Foreign Language Interfaces between Prologs/ Python and other languages.
Here, ‘f’ = ‘from’, ‘t’ = ‘to’.
(e.g. XSB to C, which requires file wrappers) and to use (e.g. Interprolog, which
uses a complex type syntax). Finally, many of the interfaces are third-party (not
written by the system developers) and so their quality may vary.
Three factors taken together distinguish Janus and its use. First, Janus
tightly combines Prolog and Python into a single process. Second, its auto-
matic bi-translation (Sect. 3) allows large and complex data structures to be
communicated from one language to another without requiring special declara-
tions. As will be discussed in subsequent sections, the result of these two factors
is that Janus is extremely fast. Third, specialized expertise about compilation
and linking is handled by the Janus configuration process, allowing transparent,
on-demand compilation for Prolog and Python modules, and on-demand loading
of Python modules when called by Prolog. These features are fully supported
for Python modules even when they call other languages, such as C.
Prolog and Python Data Structures. In part because of its dynamic typing,
Prolog’s data structures are remarkably simple: in addition to base datatypes
such as atomic constants, integers and floats, all data structures are logical terms.
The base datatypes of Python are similar to those of Prolog, while its constructed
types are also simple: lists, sets, tuples, and dictionaries.
The simplicity of data structures in each language enables Janus to bi-
translate Prolog and Python data structures in a transparent manner. For
The Janus System: A Bridge to New Prolog Applications 97
instance, Python and Prolog lists have the same syntax, while a Python tuple
such as (a,1,[b,c]) is translated to a Prolog term "(a,1,[b,c]) – i.e., a term
with the same arity but whose functor is the empty string. A Python dictionary
is recursively translated to a pyDict/1 term whose argument is a list of 2-ary
terms, each of which represent a key-value pair. For instance, the dictionary
{"name": "Bob", "languages": ["English", "GERMAN"]}
is translated to the Prolog term
pyDict([’’(name,’Bob’),’’(languages,[’English’,’GERMAN’]) ])
Prolog Calling Python. The advantage of bi-translation can be seen from the
following Janus library example.
Example 1. The Python wikidataIntegrator package9 provides a wrapper to
access Wikidata via the MediaWiki API. It returns (sometimes copious) infor-
mation about a Wikidata concept (Qnode or Pnode) as a JSON structure or
Python dictionary. Within Janus the XSB goal:
pyfunc(xp_wdi,get_wd_entity(Qnode),Json)
executes several steps. First, Janus automatically loads if needed the Python
xp_wdi andwikidataintegrator modules along with their supporting Python
packages. Once loaded, wikidataintegrator calls the Wikidata REST server,
and creates a large Python dictionary out of the Json returned from the server.
Janus then translates the Python dictionary to a Prolog term that can be
searched in a manner analogous to how Python dictionaries are searched.
Although the xp_wdi package uses a small amount of Python for the interface,
the code is included simply for ease of use. In fact, the wikidataintegrator
package could be used without writing any Python code, although this would
require slightly more sophistication on the Prolog side.
3.1 Performance
In the implementation of Janus, low-level C-APIs are used both for Python
and XSB, so that a terms on Prolog’s heap are quickly translated to objects
on Python’s heap and vice-versa. The translation is very fast – an element in a
list, set, tuple or dictionary is translated and created in 30–90 ns on a moderate-
speed server. Tests show that translation scales linearly with data structures
containing millions of elements translated and copied in under a second. The
use of bi-translation makes the C code for calls simple and their overhead low.
When only a small amount of data is transferred between systems close to a
million round-trip calls can be made from Prolog to Python. Round-trip calls
from Python to Prolog are more expensive, with around a hundred round-trip
calls possible per second.
BBN used XSB and Janus extensively in its work on DARPA’s Active Interpre-
tation of Disparate Alternatives (AIDA) project. AIDA focused on two Natural
Language automation tasks: Question Answering and Answer Summarization.
After extracting semantic knowledge graph content from thousands of textual
and video news reports, AIDA performers assembled hypotheses from the graph.
Each hypothesis explained some real-world event of interest by answering stan-
dard (who, where, when, why, and how) questions. Here, the AIDA program
addressed contemporary themes of information conflict by asking performers to
identify conflicting hypotheses. For example, in explaining the 2014 crash of an
airliner over Ukraine, systems often identified two competing hypotheses (among
many) in the data. The first was that Ukrainian separatists mistakenly shot down
The Janus System: A Bridge to New Prolog Applications 99
the airliner using Russian-supplied missiles. The second was that the plane was
destroyed by the Ukrainian Air Force.
The BBN team used Prolog as its primary programming language for Deep-
Content, its data analysis and hypothesis assembly pipeline. Despite the size
of AIDA’s knowledge graphs (hundreds of millions of triples), the graphs were
maintained in XSB, which executed a series of complex analytics scalably and
in comparable runtime (1–2 h) to imperative approaches used by other perform-
ers. A full description of the pipeline is found in [1,2]; we focus here on Janus’
contributions.
Prolog was the choice for AIDA’s semantic search and analytics because of
Prolog’s integration of backtracking search (made efficient by tabling), query-
oriented processing, and imperative (side-effect) functions. Our experience is that
Prolog is far more agile and requires many fewer lines of code than, for example,
a Java+SPARQL approach to writing software processing Semantic Web data.
This is particularly true when an application requires advanced reasoning. For
AIDA, BBN quickly implemented a Prolog meta-interpreter performing fuzzy
query processing that we estimate would have required 5X effort and code lines
in an imperative language.
Janus enabled key capabilities at several points in BBN’s AIDA pipeline.
Janus’ first use was in inter-document co-reference resolution of the many enti-
ties mentioned in AIDA documents. Here, Janus enabled low-latency direct calls
to a variety of Python-based NL packages and interfaces. These included Elas-
ticsearch (for text indexing and geo-queries), fastText, MUSE, BERT, and Faiss
(for text embeddings), and SpaCy (for parse trees), among others.
All these packages have sophisticated capabilities that our team could not
have efficiently replicated in Prolog (or in Python!). Integration of these capa-
bilities significantly boosted the accuracy of our co-reference code. Importantly,
Janus enabled tight integration of these Python calls within larger Prolog-based
query processing routines executing thousands of times per second. In this con-
text, a loose Prolog-Python integration via shell calls or sockets would have
slowed the overall pipeline unacceptably.
Additionally, Janus’ ease of use, requiring only an import and the use of spe-
cialized calls, made it easy to develop in both languages and generally preserved
Prolog’s conciseness and readability. Usually, the time required to integrate some
new Python package was dominated by learning and understanding the package
itself. Developing and testing associated Janus code often took only 1–2 h.10
Later in the pipeline, our team required a simple priority queue support-
ing insertion/deletion of arbitrary keys as part of the meta-reasoner mentioned
earlier. In our experience, one weakness of Prolog is the difficulty of imple-
menting standard data structures requiring indexing and destructive assignment
so that they are scalable and efficient. In this case, we explored a series of
Prolog-based priority queue implementations. SWI’s heaps.pl (implementing a
classic pointer-oriented algorithm) required logarithmic to linear time for arbi-
trary inserts/deletes, causing unacceptable slowdown of the larger application.
10
About a dozen Janus package libraries are included in the XSB distribution.
100 C. Andersen and T. Swift
We then augmented the heaps package with XSB’s trie package, which achieved
efficient indexing required for inserts/deletes, but also introduced unacceptable
copying of the large terms we needed to index. When further Prolog elaborations
failed to achieve acceptable speeds, we very quickly stood up a Janus call to a
Python priority queue package, which performed acceptably. Team members still
disagree about whether a scalable priority queue is possible in Prolog, but the
team is in agreement that using Janus can save time in solving prosaic problems.
In the late stages of AIDA, DARPA added a requirement that users leverage
Wikidata as a background knowledge base, and we describe the Janus library
for this as a case study. Wikidata is an enormous knowledge base (>13B triples
in 2021) that is challenging to load, even on capable servers with terabytes of
RAM. After failing to load Wikidata using more conventional relational database
methods, BBN used Janus to solve the problem. We successfully loaded Wikidata
using rdflib_HDT, an addon to the popular Python-based rdflib11 triple store.
The addon leverages the HDT compression system, which in turn allows query
access to RDF knowledge bases compressed to a 10X factor. The library jns_wd
allowed our Prolog application to efficiently call Wikidata through rdflib_HDT
at a negligible performance penalty.12
The basic jns_wd query is: wd_query(?Arg1,?Arg2,?Arg3,?Lang). This
query allows Prolog to backtrack through Wikidata triples using various argu-
ment instantiation patterns, potentially filtering the Arg3 results using a lan-
guage designator, Lang. Internally, Wikidata uses URLs for non-literals, but
ensuring a query uses the correct URL is tedious and error-prone. Accordingly,
the library performs transformations that enable queries to use short abbrevia-
tions of the URLs (e.g., Q144 for http://www.wikidata.org/entity/Q144).
DeepContent’s architecture was unusual in that XSB not only performed rea-
soning, but orchestrated a variety of Python libraries. Users more familiar with
Python may choose to employ the reverse architecture, in which XSB performs
an inference function within a larger Python-based system. The recent addition
to Janus for supporting Python-to-Prolog calls, made this possible.
BBN used such an architecture in Resource Aware Autonomy for Collabora-
tive Effects (RAACE), a recent internal research project exploring autonomous
agents. Here, Prolog was used to perform Situational Awareness (SA), plan-
ning and plan monitoring functions within a larger agent autonomy architec-
ture written in Python. This architecture builds upon Robot Operating System
(ROS) [11], a widely used autonomy framework supporting both Python and
C++ development and offering many capabilities, including sensing, navigation,
simulation, and testing, among others. Here, the advantages of conforming to
existing Python autonomy code bases mandate that Prolog be a supporting
player.
11
https://rdflib.readthedocs.io/en/stable.
12
WikidataInegrator, mentioned in Sect. 3 augmented the Wikidata-HDT snapshot.
The Janus System: A Bridge to New Prolog Applications 101
A smaller R&D project funded by a large defense contractor used the Ergo sys-
tem (based on XSB) [7] and Python together to 1) interpret simple natural
language statements about changing situations; 2) understand the significance
of the changes to concrete plans to achieve a high-level goal; and 3) use geospa-
tial data to reason about how to achieve the goal in an altered situation. For
this, Ergo used Janus to interpret the output of SpaCy on the natural language
statements and to query Google Maps for directions and other information. Ergo
then devised new concrete plans to achieve the high-level goal.
5 Discussion
The preceding sections show how similarities between Python and Prolog – their
implementation in C, dynamic typing, and the simple recursive nature of their
data structures – support a fast, powerful interface resulting in new applications
and use cases for Prolog. In this section, we revisit the three issues introduced
in Sect. 1 and review the extent to which Janus mitigates each.
Janus has the most impact upon the Packages problem, by enabling easy use
of the vast Python package ecosystem in Prolog. Over a dozen Python packages
for natural language analysis, vector similarity search, geospatial information,
access to Wikidata, and interfaces to semantic web data all proved key to the
Prolog applications described in Sect. 4; libraries for most of these packages are
now included in the XSB distribution and repo. In addition to XSB applica-
tions, the very recent port of Janus to a commercial Prolog is being used for
customer demos. Large package bases are commonly found in languages with
robust frameworks for classes and complex object definition. It is surprising that
the availability of LogTalk across many different Prologs has not, to our knowl-
edge, led to a vibrant Prolog class and package ecosystem.
Mitigation of the Embeddability problem was shown in Sect. 4.2, where Pro-
log is used for specialized reasoning within a larger autonomy application. In
addition, the Python-Prolog interface is planned to support the multi-language
development approach to VQA discussed in Sect. 4.3. However, Janus has not
yet been used to construct an interface from a language such as Rust or Julia.
Janus has had less impact upon the problems of Graphical Interfaces and
IDEs thus far. We have enabled Jupyter notebooks to directly call XSB, a
functionality available in the XSB distribution. However, while XSB calls to
Python graphics libraries like TkInter and plotty are occasionally found in
the projects of Sect. 4, we have not yet developed a major application fronted
by Python graphics or developed Python graphics-centric XSB libraries. We are
hopeful that such libraries will be developed over the next year. We believe these
The Janus System: A Bridge to New Prolog Applications 103
libraries can in turn be leveraged to create a powerful Prolog IDE. The need for a
graphical IDE is particularly pressing for Prolog due to the well-known learning
curve imposed by core Prolog features such as backtracking, unification, and cuts
– not to mention tabling, constraint satisfaction and other advanced features.
In summary, Janus provides a viable solution to the package problem, has
begun to address the embeddability problem, but has so far had less impact on
the problem of graphical interfaces and IDEs. Although work remains to address
these problems, without Janus the applications of Sect. 4 either would have been
less reliant on Prolog or would not have used Prolog at all.
Disincentives for using Prolog in industry—lack of native packages, lack of
IDEs, lack of proficient programmers – hamper all but the most popular lan-
guages. However Prolog’s strengths are unique, and steadily accumulate as new
research results move into Prolog systems year after year. A recent panel at a
logic programming conference had the title “No Logic is an Island.” We believe
that building bridges from Prolog to hugely popular languages like Python will
contribute to Prolog still thriving in its hundredth year.
References
1. Andersen, C., Swift, T., Ki, A., Gerken, M., Carroll, D., Harless, C.: Improved
entity resolution and relaxed query hypothesis generation using SAMSON-2020.
In: 2020 NIST Text Analysis Conference on Streaming Multimedia Knowledge
Base Population (SM-KBP) (2020)
2. Andersen, C., et al.: KB construction and hypothesis generation using SAMSON.
In: 2019 NIST Text Analysis Conference on Streaming Multimedia Knowledge
Base Population (SM-KBP) (2019)
3. Carlsson, M., Mildner, P.: SICStus Prolog - the first 25 years. Theory Pract. Logic
Program. 12(1–2), 35–66 (2012)
4. Chen, V., Varma, P., Krishna, R., Bernstein, M., Re, C., Fei-Fei, L.: Scene graph
prediction with limited labels. In: International Conference on Computer Vision,
pp. 2580–2590 (2019)
5. Cui, B., Swift, T.: Preference logic grammars: fixed-point semantics and application
to data standardization. Artif. Intell. 138, 117–147 (2002)
6. Diaz, D., Abreu, S., Codognet, P.: On the implementation of GNU Prolog. Theory
Pract. Logic Program. 12(1–2), 253–282 (2012)
7. Grosof, B., Kifer, M., Swift, T., Fodor, P., Bloomfield, J.: Ergo: a quest for declar-
ativity in logic programming. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo,
M., Kowalski, R., Rossi, F. (eds.) Prolog: The Next 50 Years. LNCS (LNAI),
vol. 13900, pp. xx–yy. Springer, Cham (2023). https://doi.org/10.1007/978-3-031-
35254-6_18
8. Hermenegildo, M.V., et al.: An overview of Ciao and its design philosophy. Theory
Pract. Logic Program. 12(1–2), 219–252 (2012)
9. Ingrand, F., Ghallab, M.: Deliberation for autonomous robots: a survey. Artif.
Intell. 247, 10–44 (2017)
10. Johnson, J., Krishnan, R., Stark, M., Li, L., Shamma, D., Bernstein, M., Fei-Fei,
L.: Image retrieval using scene graphs. In: International Conference on Computer
Vision, pp. 3668–3678 (2015)
104 C. Andersen and T. Swift
11. Quigley, M., et al.: Ros: an open-source robot operating system. In: ICRA Work-
shop on Open Source Software (2009)
12. Santos Costa, V., Damas, L., Rocha, R.: The YAP Prolog system. Theory Pract.
Logic Program. 12(1–2), 5–34 (2012)
13. Schimpf, J., Shen, K.: ECLiPSe - from LP to CLP. Theory Pract. Logic Program.
12(1–2), 127–156 (2012)
14. Swift, T., Andersen, C.: The Janus system: multi-paradigm programming in Prolog
and Python. In: Proceedings of the International Conference on Logic Program-
ming. EPTCS (2023)
15. Swift, T., Warren, D.S.: XSB: extending the power of Prolog using tabling. Theory
Pract. Logic Program. 12(1–2), 157–187 (2012)
16. Tarau, P.: Natlog: a lightweight logic programming language with a neuro-symbolic
touch. In: ICLP (2021)
17. Wielemaker, J., Schrijvers, T., Triska, M., Lager, T.: SWI-Prolog. Theory Pract.
Logic Program. 12(1–2), 67–96 (2012)
18. Zhou, N., Kjellerstrand, H., Fruhman, J.: Constraint Solving and Planning with
Picat. Springer Briefs in Intelligent Systems. Springer, Cham (2015). https://doi.
org/10.1007/978-3-319-25883-6
19. Zhou, N.F.: The language features and architecture of B-Prolog. Theory Pract.
Logic Program. 12(1–2), 189–218 (2012)
Teaching Prolog
Some Thoughts on How to Teach Prolog
1 Introduction
(Constraint) Logic Programming, (C)LP, and Prolog in particular, represent a
unique programming paradigm with many characteristics that are not present in
other styles of programming, such as imperative, object-oriented, or functional
programming. Most notably the paradigm is based on logic and includes search
as an intrinsic component, as well as the use of unification, generalizing pattern
matching. This brings about other interesting and also different aspects, such as
for example reversibility of programs or being able to represent knowledge and
reason about it, including formulating specifications and algorithms within the
same formalism. It is thus not only a unique programming paradigm, but also a
modeling and reasoning tool.
Partially funded by MICINN projects PID2019-108528RB-C21 ProCode, TED2021-
132464B-I00 PRODIGY, and FJC2021-047102-I, by the Comunidad de Madrid pro-
gram P2018/TCS-4339 BLOQUES-CM, and by the Tezos foundation. The authors
would also like to thank the anonymous reviewers for very useful feedback on previous
drafts of this paper.
c The Author(s), under exclusive license to Springer Nature Switzerland AG 2023
D. S. Warren et al. (Eds.): Prolog: The Next 50 Years, LNAI 13900, pp. 107–123, 2023.
https://doi.org/10.1007/978-3-031-35254-6_9
108 M. V. Hermenegildo et al.
natural (0).
natural (s(X)) :- natural (X).
Fig. 2. Horn-clause specification (and program) for squares of naturals < 5 (click on
run to load).
7
And even some LP languages have been proposed that explicitly did not have an
operational semantics, such as, e.g., the Goedel language.
112 M. V. Hermenegildo et al.
illustrate that logical variables can be seen as “declarative pointers” [9]. I.e., in
the same way a set of Prolog clauses constitute at the same time statements
in logic and a program, the data structures of Prolog can be seen at the same
time as (Herbrand) terms of the logic and as traditional data structures with
(declarative, i.e., single assignment) pointers. We have found that explaining
this duality is also very enlightening.
There are of course many other beautiful and elegant aspects to show (e.g.,
higher-order, meta-interpretation, or types and program properties in general,
all of which in (C)LP once more can be covered within the same language),
but our space here is limited. As a compelling example, in (C)LP, types (and
properties in general) can be defined as (runnable) predicates. E.g., the type
natlist can be defined as:
In addition to showing the beauty of the language, another aspect that we believe
is important to cover during the course is to dispel the many unfounded myths
and misconceptions that still circulate about Prolog and the (C)LP paradigm
in general, and to which students may be exposed. While some of these views
may have been at least partially true of early implementations of Prolog, both
the language and its implementations have come a long way over many years
(actually decades) of evolution, and it is easy to show how the shortcomings
of early Prologs have been addressed in most current systems. The following is
an incomplete list of some of these perceived shortcomings and some suggested
dispelling facts or actions:
Explaining Termination. As mentioned in the previous section, it is certainly
a good idea to start teaching the declarative view, i.e., using the logical reading
114 M. V. Hermenegildo et al.
solution
fail
fail fail
solution
solution
infinite failure
when writing and reading clauses. In this view of the language, if the logic
that we write is correct the system will answer any question. As mentioned
before, here examples with bounded search tree are great starters (e.g., using only
constants or avoiding recursion). However, trouble begins at the very beginning
once structures and recursion are introduced: students soon run into termination
problems. This is true of course of any programming language or proof system.
However, non-terminating executions are likely to discourage beginners if their
origins are not explained well and no remedies provided.
For example, let us define a pair of natural numbers in Peano representation:
solution solution
fail fail
fail fail fail fail
solution solution
solution solution
2. Without giving too many details, start by running all predicates in breadth-
first mode - all examples work great! This will allow students to gather con-
fidence with recursion, search, and the power of a logic-based programming
language (specially if they have already taken an introductory logic course),
by simply thinking logically and/or inductively.
3. After students have been exposed to and written a few examples, we have
found figures such as Figs. 4–5 useful to introduce them in a graphical way
to the basic theoretical results at play, i.e., the soundness and (refutation-
)completeness of the SLD(NF)-resolution proof procedure used by Prolog.
The practical implication to convey is that the search tree has the shape
of Fig. 4, i.e., that all solutions and some failures are at finite depth, but
that there are branches leading to failure that may be infinite and that it is
not always possible to detect this. This summary depiction makes it easy to
explain why breadth-first (or iterative deepening, or any other fair search rule)
is guaranteed to produce all solutions if they exist in finite time (Fig. 5, left),
and why depth-first sometimes may not (Fig. 5, right). Of course, neither one
of them is guaranteed to always finish after producing the positive answers.
4. At the same time, one should discuss the advantages and disadvantages of
these search rules in terms of time, memory, etc. I.e., that the price to pay
for breadth-first execution’s advantages is very large (potentially exponential)
memory and time consumption, while depth-first can be implemented very
efficiently with a stack, with iterative deepening representing an interesting
middle ground. This can be shown very practically by benchmarking actual
examples, motivating the practical choices made for Prolog, which bring in
great efficiency at the (reasonable) price of having to think about goal and
clause order.
5. For example, simply changing the goal order (in this case in the query) to
?- X=s(0),pair(X,Y). modifies the search space and the program can pro-
duce all the answers with the standard Prolog search. This leads naturally to
discussing how one needs to reason about the behavior (cost and termination)
116 M. V. Hermenegildo et al.
of predicates depending on the different modes (see [20]) in which they will
be called.
6. And it can also be pointed out that there exist other techniques like delays
and, specially, tabling, which helps detect some of the infinite failures in finite
time, and avoid repeated answers (even infinite ones), in addition to changing
dynamically the goal order.
7. More generally, while using small examples, sophisticated Prolog implemen-
tations, and modern fast computers can create the misconception that Prolog
provides solutions effortlessly, it should be stressed that, as a Turing complete
language, also Prolog programmers eventually need to care about algorithmic
time and memory complexity of both their programs and the libraries/fea-
tures they utilize, and, at the limit, termination.
8. Thus, this is also a good moment to introduce informally the notion of unde-
cidability and the halting problem and relate them to the graphical depictions
of the search tree. Some students may not be aware that there exist deci-
sion problems that admit no algorithmic solution. One should underline that
this is of course not a particular problem of Prolog, but rather the essence
of computability, and no language or system (Prolog, logic, nor any other
Turing-complete programming language) can provide a magic formula that
guarantees termination.
9. Needless to say it is also important to explain how to control search, via
clause order and literal order, as well as pruning (see cut later). And, if time
permits, it is important to discuss the use of the constraint systems built into
most current Prolog systems (Q, R, fd, . . . ), and how they can bring about
many other improvements to search (e.g., generate and test vs. constrain and
generate), arithmetic (see below), etc.
for the student to fully understand what is really going on one should really
discuss the workings of the constraint solver. In that sense Peano is simpler
because with only unification and Horn clauses all operations are fully defined.
Still, both approaches are fine.
1. First, it is important to explain why the decision to leave out the occurs
check was made in the first place: in addition to reducing the cost of general
unification, removing the occurs check allows the complexity of the variable-
value unification case to be constant time (instead of linear, since there is no
need to check that the left hand side does not occur in the value), which is
arguably a basic requirement for a practical programming language.
2. It is also important to point out that the lack of occurs check is rarely an issue
in actual applications, and that, furthermore, there is in any case a built-in for
unification with occurs check. In most cases, including e.g., implementation
of theorem provers, one can selectively use the unification with occurs check
built-in only when needed. It can also be useful to provide a package (easy
to implement in most Prologs with a term expansion) that calls unification
with occurs check by default for all unifications. This can also be provided in
some systems via an engine/compiler flag.
3. Furthermore, it should be mentioned that many Prolog systems in fact now
support infinite tree unification (stemming all the way back to Prolog II) as
the default. In this extension to unification (actually, a constraint domain),
infinite terms are actually supported and the unifications that would be the
subject of the occurs check produce such terms. For example, the answer to
?- X = f(X). is simply X = f(X) , and the system does not go into a loop
when performing such unifications or printing such infinite terms.
1. In the same way that we start with a powerful yet not necessary efficient
search rule such as breath-first, it is convenient to restrict the discussion
initially to pure fragments of Prolog without side-effects, cuts, or the dynamic
manipulation of database.
2. In this phase it can be convenient to have a mechanism to enable a pure mode
in the Prolog implementation where impure built-ins simply are not acces-
sible. However, this is not strictly necessary and the objective can also be
achieved by simply not allowing the use of impure built-ins, or, in fact, any
built-ins, initially. Peano arithmetic is again handy here.
3. Later, the ISO-Prolog built-ins can be introduced. Here, it is convenient to
treat each kind of impurity separately:
(a) Cuts: a first consideration is that using if-then-else is often preferable.
Then, regarding cuts, it is important to explain at least the difference
between (green) cuts added as optimizations to discard unnecessary choice
118 M. V. Hermenegildo et al.
points (but which do not alter the declarative semantics) from (red) cuts
that are only meaningful for some calling modes and whose removal would
make the program incorrect. Explain that argument indexing makes many
green cuts unnecessary.
(b) Assert/retract: some programming patterns using dynamic program
manipulation, like explicit memoization or algorithms that explicitly
interleave phases of database modifications with pure deductions, do not
sacrifice declarativeness. The classic Fibonacci program with a double
recursion, implemented with and without memoing, is a good example
here. Assert and retract in modern Prolog systems are module-aware,
which makes it easier to encapsulate their effects. Also, it should be
noted that there are other approaches, such as mutables or condition-
action rules, that offer more structured ways to express state change; see,
e.g., [6,13].
4. It is also useful to develop pure libraries (e.g., implicit states ala DCGs,
monad-like styles), built-ins, or semantics where effects and actions are prop-
erly captured.
5. Finally, it is also important to point out that sometimes impurity is just
necessary, but one should strive to keep it encapsulated as much as possible
as libraries or program expansions.
means. (And that this idea is very useful in practice for analysis of other
languages using Horn clauses!)
In practice, translating functional or imperative constructs to Prolog is rela-
tively easy, specially when using special syntactic extensions (such as logical
loops or functional notation). Performance-wise, most Prolog implementa-
tions are optimized enough to execute recursions as efficiently as loops (e.g.,
last-call optimization), use logical mutable variables (equivalent to implicit
arguments), or as a last resort store mutable states via the dynamic database.
3. For students that have some notions of programming language implementa-
tion, it helps to explain that, when running “forward”, Prolog uses a stack
of activation records for local variables, and return addresses (forward con-
tinuations), as every language, that allow knowing where to return when a
procedure returns (succeeds). Then, Prolog also has a stack of backwards con-
tinuations, to know where to go if there is a failure (previous choice point),
and this (coupled with other memory and variable binding management tech-
niques) implements Prolog’s backtracking semantics very efficiently.
4. College students that are already familiar with younger languages (Erlang,
Haskell, or even Rust) often recognize striking similarities in syntax and
semantics with Prolog (e.g., “pattern matching”). Most of them tend to be
amazed by Prolog simplicity and expressive power and recognize that Prolog
is still unique.
1. Show some of the many examples of great applications that Prolog has. There
are great collections on line, and some new ones have been gathered for the
Prolog 50th anniversary, but in particular there are some really excellent
examples in the volume in which this paper appears. An excellent example is
ProB, winner of the first Colmerauer Prize [15]. See also the excellent related
discussion11 on the advantages of using Prolog.
2. Give the students as homework real, challenging, and interesting projects
where they can experience the power and elegance of the language.
3. Another thing to perhaps point out in this context is that modern applications
are almost never written in a single language and some Prolog implementa-
tions can be easily embedded as part of more complex systems, and this is
done routinely.
Prolog Can also Have “Types” (If Needed). Prolog is in principle a dynam-
ically typed language, and this is not without its advantages –the success of
Python attests to this. Second, as mentioned before, types and many other
properties can indeed be defined very elegantly within the same language of
predicates, and used as both run-time tests and generators. Furthermore, there
are Prolog systems that also check these types and properties statically (again,
see [10] in this volume).
11
https://prob.hhu.de/w/index.php?title=Why_Prolog%3F.
120 M. V. Hermenegildo et al.
And, to end this section on myths and misconceptions, a final mention is due
to the Fifth Generation (FG) project. It is probably unlikely for current
students to be aware of this project, but since the subject of its success or
failure does comes up with some periodicity, e.g., in online forums, it suffices to
say that one hand there were many interesting outcomes of this project (and
there is ample literature on the subject) and on the other the fate of the FG
Project is in any case quite unrelated to Prolog and (C)LP, simply because,
contrary to widespread belief, the FG did not use Prolog or “real LP” anyway!
It used flat committed choice languages,12 which arguably did not contribute to
make the FG as successful as it could have been.
Some thoughts regarding the different types of Prolog systems that we fortu-
nately have currently freely available for teaching. This includes:
1. The classical systems with traditional installation, which in general are most
appropriate for more advanced users and intensive use. In the context of
teaching, they have the advantage that the best implementations portray a
serious language which is competitive with the most popular languages in per-
formance, features, environment, libraries, embeddability, small executables,
etc. The drawback is of course that this type of system requires installation,
but that should not be any hurdle for college students (or at least not for
those in CS).
2. At the same time, there are now fortunately also very good Prolog play-
grounds and notebooks that require no installation. Examples are, e.g., the
Ciao Playgrounds and Active Logic Documents, SWISH, τ -Prolog, s(CASP)
playground, etc. These no-installation alternatives can be very attractive for
beginners, young students, or simply casual users, and they are in all cases
very useful for supporting executable examples in manuals and tutorials, as
done here. These systems can be server-based or browser-based, each having
advantages and disadvantages. A good discussion on this topic can be found
in [16] and [5], within this same volume.
3. As far as functionality, ideally the system (or systems) to be used should
allow covering ISO-Prolog and some constraint domains –most current Prolog
systems are capable of this. Other desirable features in our opinion are the
possibility of restricting programs to pure LP (and supporting several search
rules and tabling), modern forms of negation (such as, e.g., ASP/s(CASP)),
functional syntax, extended support for higher-order and libraries, etc. Again,
many current Prolog systems provide at least some of these characteristics.
12
An interesting topic that is however out of our scope here –let’s just say for this
discussion that they used “something similar to Erlang.”
Some Thoughts on How to Teach Prolog 121
6 Conclusions
We have presented some lessons learned from our experience teaching Prolog,
and (C)LP in general, over the years. We have covered some methodological
issues that we feel are important, such as how to show the beauty and usefulness
of the language, and also to how to avoid some common pitfalls, misconceptions,
and myths about it. However, teaching Prolog and (C)LP is an extremely rich
subject and there are of course many other aspects that we have not been able
to address for lack of space. We still hope that at least some of our suggestions
are of use to other instructors that are teaching or plan to teach Prolog. For a
more complete picture, as mentioned before, much of our experience over the
years is materialized in a) the courses that we have developed, for which, as
pointed out before, the material is publicly available13 , and b) the many special
features that we have incorporated over time in our own Ciao Prolog system in
order to aid in this important task of teaching logic programming. Again, we
have touched upon some of them, such as being able to choose different search
13
https://cliplab.org/logalg.
122 M. V. Hermenegildo et al.
rules, the playground, or active logic documents, but there are many others. We
hope that all the ideas present in these materials and systems are helpful and
inspiring to both Prolog instructors and students.
References
1. Bassiliades, N., Sakellariou, I., Kefalas, P.: Demonstrating multiple prolog pro-
gramming techniques through a single operation. In: Warren, D.S., Dahl, V., Eiter,
T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog: The Next 50 Years.
LNCS (LNAI), vol. 13900, pp. 71–81. Springer, Cham (2023). https://doi.org/10.
1007/978-3-031-35254-6_6
2. Cecchi, L.A., Rodríguez, J.P., Dahl, V.: Logic Programming at Elementary School:
why, what and how should we teach Logic Programming to children. In: Warren,
D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog:
The Next 50 Years. LNCS (LNAI), vol. 13900, pp. 131–143. Springer, Cham (2023).
https://doi.org/10.1007/978-3-031-35254-6_11
3. Cervoni, L., Brasseur, J., Rohmer, J.: Simultaneously teaching mathematics and
prolog in school curricula: a mutual benefit. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog: The Next 50 Years. LNCS
(LNAI), vol. 13900, pp. 124–130. Springer, Cham (2023). https://doi.org/10.1007/
978-3-031-35254-6_10
4. Colmerauer, A.: The birth of prolog. In: Second History of Programming Languages
Conference, pp. 37–52. ACM SIGPLAN Notices (1993)
5. Flach, P., Sokol, K., Wielemaker, J.: Simply logical - the first three decades. In:
Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.)
Prolog: The Next 50 Years. LNCS (LNAI), vol. 13900, pp. 184–193. Springer, Cham
(2023). https://doi.org/10.1007/978-3-031-35254-6_15
6. Genesereth, M.: Dynamic logic programming. In: Warren, D.S., Dahl, V., Eiter,
T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog: The Next 50 Years.
LNCS (LNAI), vol. 13900, pp. 197–209. Springer, Cham (2023). https://doi.org/
10.1007/978-3-031-35254-6_16
7. Green, C.C.: Application of Theorem Proving to Problem Solving. In: Walker,
D.E., Norton, L.M. (eds.) Proceedings IJCAI, pp. 219–240. William Kaufmann
(1969)
8. Gupta, G., Salazar, E., Arias, J., Basu, K., Varanasi, S., Carro, M.: Prolog: past,
present, and future. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowal-
ski, R., Rossi, F. (eds.) Prolog: The Next 50 Years. LNCS (LNAI), vol. 13900, pp.
48–61. Springer, Cham (2023). https://doi.org/10.1007/978-3-031-35254-6_4
9. Hermenegildo, M.: Parallelizing irregular and pointer-based computations auto-
matically: perspectives from logic and constraint programming. Parallel Comput.
26(13–14), 1685–1708 (2000)
10. Hermenegildo, M., Morales, J., Lopez-Garcia, P., Carro, M.: Types, modes and so
much more - the prolog way. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo,
M., Kowalski, R., Rossi, F. (eds.) Prolog: The Next 50 Years. LNCS (LNAI),
vol. 13900, pp. 23–37. Springer, Cham (2023). https://doi.org/10.1007/978-3-031-
35254-6_2
11. Kowalski, R.A.: Predicate logic as a programming language. In: Proceedings IFIPS,
pp. 569–574 (1974)
Some Thoughts on How to Teach Prolog 123
12. Kowalski, R., Kuehner, D.: Linear resolution with selection function. Artif. Intell.
2(3), 227–260 (1971)
13. Kowalski, R., Sadri, F., Calejo, M., Dávila-Quintero, J.: Combining prolog and
imperative computing in LPS. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo,
M., Kowalski, R., Rossi, F. (eds.) Prolog: The Next 50 Years. LNCS (LNAI), vol.
13900, pp. 210–223. Springer, Cham (2023). https://doi.org/10.1007/978-3-031-
35254-6_17
14. Kowalski, R.A.: The early years of logic programming. Commun. ACM 31(1), 38–
43 (1988)
15. Leuschel, M.: ProB: harnessing the power of prolog to bring formal models and
mathematics to life. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M.,
Kowalski, R., Rossi, F. (eds.) Prolog: The Next 50 Years. LNCS (LNAI), vol.
13900, pp. 239–247. Springer, Cham (2023). https://doi.org/10.1007/978-3-031-
35254-6_19
16. Morales, J., Abreu, S., Hermenegildo, M.: Teaching prolog with active logic doc-
uments. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R.,
Rossi, F. (eds.) Prolog: The Next 50 Years. LNCS (LNAI), vol. 13900, pp. 171–
183. Springer, Cham (2023). https://doi.org/10.1007/978-3-031-35254-6_14
17. Pereira, L., Pereira, F., Warren, D.: User’s Guide to DECsystem-10 Prolog. Dept.
of Artificial Intelligence, Univ. of Edinburgh (1978)
18. Robinson, J.A.: A machine oriented logic based on the resolution principle. J. ACM
12(23), 23–41 (1965)
19. Tabakova-Komsalova, V., Stoyanov, S., Stoyanova-Doycheva, A., Doukovska, L.:
Prolog education in selected high schools in Bulgaria. In: Warren, D.S., Dahl, V.,
Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog: The Next 50
Years. LNCS (LNAI), vol. 13900, pp. 144–153. Springer, Cham (2023). https://
doi.org/10.1007/978-3-031-35254-6_12
20. Warren, D.S.: Introduction to prolog. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog: The Next 50 Years. LNCS
(LNAI), vol. 13900, pp. 3–19. Springer, Cham (2023). https://doi.org/10.1007/978-
3-031-35254-6_1
21. Warren, D.S.: Writing correct prolog programs. In: Warren, D.S., Dahl, V., Eiter,
T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog: The Next 50 Years.
LNCS (LNAI), vol. 13900, pp. 62–70. Springer, Cham (2023). https://doi.org/10.
1007/978-3-031-35254-6_5
22. Warren, D.S., Denecker, M.: A better logical semantics for prolog. In: Warren,
D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog:
The Next 50 Years. LNCS (LNAI), vol. 13900, pp. 82–92. Springer, Cham (2023).
https://doi.org/10.1007/978-3-031-35254-6_7
23. Warren, D.: Applied logic-its use and implementation as programming tool, Ph. D.
thesis, University of Edinburgh (1977), also available as SRI Technical Note 290
Simultaneously Teaching Mathematics
and Prolog in School Curricula: A Mutual
Benefit
1 Introduction
Based on predicate logic, Prolog is particularly well suited for expressing relation-
ships between objects, checking properties about these relationships, validating
the consistency of logical rules or for its intrinsic database capabilities.
However, the (re)introduction of Prolog into the educational system at the
secondary school level, and its use as a complement to traditional school subjects,
provides a new and enriching form of learning that may prove to facilitate the
acquisition of certain knowledge.
There have been many examples in the past of the use of Prolog in mathe-
matical analysis (derivative), geometry (Géometrix [6]) or chemistry [7,8]. Var-
ious papers [1–4], some going back to the 1980s, have illustrated, through a few
examples, the pedagogical interest of Prolog in the teaching of elementary math-
ematics, or more advanced mathematics (such as the learning of recursion via
fractals [5], or related to graphs [9,10]). Its educational potential in the history
classroom [11] has also been considered.
This collection of work shows the interest of exploring the usefulness of Prolog
in a pedagogical context. The experiments we have conducted in 2022 with senior
High School students seem to confirm its relevance. In this paper, we gather some
of the case studies which were proposed to the students.
3 Some Examples
The intrinsic qualities of Prolog justify its implementation in high schools (not
an intention to apply it artificially out of context). As we will illustrate with
a few examples, its use has the potential to allow students to assimilate the
elements of the course and to manipulate them and exploit them easily during
exercises.
As we shall illustrate, its use has the potential to allow students to assimilate
the elements of the course and to manipulate them easily during the exercises.
When solving problems or doing exercises, Prolog may give students a way to
understand the flow and application of certain theorems. Let us now consider a
few examples.
line_point(ab,a).
line_point(ab,b).
line_point(ef,a).
triangle([A,B,C]):-
line_point(AB,A),
line_point(AB,B),
line_point(BC,B),
line_point(BC,C),
line_point(CA,C),
line_point(CA,A).
One can now experiment with this first try in Prolog on a case study, and
see that it is not totally satisfactory: Prolog will tell us that a point is a triangle
(the three vertices being in coincidence), that a segment line is a triangle (the
three vertices being aligned), and, finally, it will find that with three vertices, we
can name six triangles [A, B, C], [A, C, B], ..., which is not of much relevance, as
these “six” triangles are nothing but one.
1
See, for example: https://www.rd.com/article/triangle-puzzle/.
Simultaneously Teaching Mathematics and Prolog 127
These experiments, and the modifications to our Prolog program that they
may require (specifying that the vertices and segments must be distinct, e.g. by
defining a new ad hoc predicate), are an excellent ground to encourage careful,
precise exploration of the relationships between concepts, models, experiences,
real world, and human perception.2
3.2 Polynomials
By writing these three clauses, the pupil becomes aware of different, more
or less complete forms of polynomials (the teacher then guides him/her in this).
They may start with only the first rule and find that it is not very precise. They
can then move on to the standard application exercises of the course, where
the aim is to check whether a function is a second degree trinomial or not, for
example:
quadPolynomial(7). FALSE
quadPolynomial(12*x^2+1*x+0). TRUE
The advantage of using Prolog is that the student can transcribe the course
definition exactly with a minimum of learning. In the same way, if he has to
represent the solution of a second degree polynomial, the course is also written
directly in Prolog. Introducing the concept of the undefined variable “_”, this
can be written as follows:
solvePoly(A*x^2+B*x+C, Discriminant, _, _) :-
Discriminant is B*B - 4*A*C,
Discriminant $<$ 0.
/* The discriminant is negative, no solutions to display */
solvePoly(A*x^2+B*x+C, Discriminant, X1, X1) :-
Discriminant is B*B - 4*A*C,
Discriminant = 0,
A =\= 0,
2
More details can be found at: https://fr.slideshare.net/Jean Rohmer/compter-les-
triangles-en-prolog.
128 L. Cervoni et al.
X1 is -(B/(2*A)).
/* The discriminant is zero, X1 is the only solution */
solvePoly(A*x^2+B*x+C, Discriminant, X1, X2) :-
Discriminant is B*B - 4*A*C,
Discriminant > 0,
A =\= 0,
X1 is (-B-sqrt(Discriminant))/(2*A),
X2 is (-B+sqrt(Discriminant))/(2*A).
diameter([a,e],c1).
diameter([a,f],c2).
Simultaneously Teaching Mathematics and Prolog 129
inscribed([a,b,e],c1).
inscribed([a,b,f],c2).
For example, the student can easily check that [B, A] and [E, F ] are perpen-
dicular by asking whether “perpendicular([a,b],[e,f]).” is true, and Prolog
will return “TRUE”.
Although Prolog is not a substitute for learning how to solve a problem,
it is a valuable tool for learning how to understand and structure a problem,
which is the first step towards solving it. Learning to pose a problem correctly
is sometimes enough to trivialise its solution.
Pythagoras, Thales or midpoint theorems can be expressed just as simply
in the form of Prolog clauses and, with the use of the traditional trace when
executing a Prolog program, the student can see the solution of an exercise step
by step.
In a first step, the teacher can show the students how to express theorems
in Prolog, with the learners having to describe the exercises as facts (as in the
example above). Then, in a second step, the students write all the case studies
(theorems or propositions and descriptions of the exercises) themselves.
4 Conclusion
Solving a problem in Prolog means describing it. As we have seen from a few
examples, no matter how you describe the problem, no matter how diverse the
description, Prolog will always return an answer. The main interest of Prolog
for mathematics education is that it learns to understand, express and structure
a problem. This step alone often makes it possible, if not to solve the problem,
at least to identify possible lines of attack opening the way to its resolution. It
seems to us that this step is fundamental in the problem solving process and
that Prolog, by its nature, is adapted to its learning.
The interrelationship between the modelling of theorems or course principles
in Prolog and the progressive learning of the language seems to us to be more
relevant than in traditional imperative languages where an algorithm must first
be imagined. With Prolog’s native solving principle, the student discovers the
mechanisms that allow him to solve an exercise. The trace helps him to better
understand which theorems apply or why some descriptions he may have made
are incorrect or incomplete.
However, by calling the Prolog interpreter, the student will get the informa-
tion that the property can be derived, but not how. The sequence of predicates is
essential, and can be obtained by using the trace which, beyond debugging, also
allows to understand the “reasoning” implemented and its relevance. Moreover,
the solution obtained is relative to the known and described knowledge. A Prolog
answer confirms that the knowledge domain is sufficient to reach a satisfactory
conclusion, whereas a failure does not show that the requested objective cannot
be reached. It does not show that the objective cannot be reached, but rather
that the knowledge expressed (in the form of rules and facts) is sufficient.
130 L. Cervoni et al.
Finally, it is important to note that languages evolve rapidly and are likely to
move from widespread use to more moderate use; therefore, while it is good to
introduce students to a computer language, it should not necessarily be chosen
on the basis of its popularity, but rather its educational potential.
References
1. Ball, D.: PROLOG and mathematics teaching. Educ. Rev. 39(2), 155–161 (1987)
2. Bensky, T.: Teaching and learning mathematics with Prolog. arXiv preprint
arXiv:2108.09893 (2021)
3. Buscaroli, R., Chesani, F., Giuliani, G., Loreti, D., Mello, P.: A Prolog application
for reasoning on maths puzzles with diagrams. J. Exp. Theor. Artif. Intel. 1–21
(2022)
4. Connes, A.: Micro-Prolog et géométrie élémentaire. Bulletin de l’EPI (Enseigne-
ment Public et Informatique) 44, 125–137 (1986)
5. Elenbogen, B. S., O’Kennon, M. R.: Teaching recursion using fractals in Prolog. In
Proceedings of the nineteenth SIGCSE technical symposium on Computer science
education, 263–266 (1988)
6. Géométrix website, http://geometrix.free.fr/site. Accessed 9 Feb 2023
7. Kleywegt, G. J., Luinge, H. J., Schuman, B. J. P.: PROLOG for chemists. Part 1.
Chemom. Intel. Lab. Syst. 4(4), 273–297 (1988)
8. Kleywegt, G. J., Luinge, H. J., Schuman, B. J. P.: PROLOG for chemists. Part 2.
Chemom. Intel. Lab. Syst. 5(2), 117–128 (1989)
9. McGrail, R. W., Nguyen, T. T., Granda, M. S.: Knot Coloring as Verification.
In: 2020 22nd International Symposium on Symbolic and Numeric Algorithms for
Scientific Computing (SYNASC). pp. 24–31. IEEE (2020)
10. Volk, A. C.: Graph Algorithms in PROLOG, CPS 499/592 Emerging Languages,
University of Dayton, Spring (2016)
11. Weissberg, D.: Micro-prolog en classe d’histoire: Montségur au risque de
l’informatique. Bulletin de l’EPI (Enseignement Public et Informatique) 39, 115–
120 (1985)
Logic Programming at Elementary
School: Why, What and How Should We
Teach Logic Programming to Children?
1 Introduction
At present, there is an international consensus on the importance of Computa-
tional Thinking (CT) [30] in developing general-purpose problem solving skills.
Hence, countries have begun to incorporate CT into their curriculum since ele-
mentary school. However, there is no complete agreement regarding the concepts
that should be addressed. In this respect, some works [4,24,27] recognise that the
following concepts should be covered: algorithm design, abstraction, decompo-
sition, generalisation and evaluation. Elementary school curricula include some
or all of these concepts, in different complexity levels selected according to chil-
dren’s ages [3,22].
There is much research explaining how to introduce CT to children through
Imperative Programming, supported by a variety of teaching resources that
c The Author(s), under exclusive license to Springer Nature Switzerland AG 2023
D. S. Warren et al. (Eds.): Prolog: The Next 50 Years, LNAI 13900, pp. 131–143, 2023.
https://doi.org/10.1007/978-3-031-35254-6_11
132 L. A. Cecchi et al.
collaborate in this process. The majority of these initiatives use visual pro-
gramming languages, where students build programs using graphical objects
and drag-and-drop interfaces. Examples of these tools are Scratch, Alice and
Open Roberta. Compared to text-based programming, these visual languages
significantly reduce the challenges for students to learn syntax and avoid the
inconvenience of syntax errors.
The Logic Programming paradigm is not represented enough within the
paradigms habitually covered in introductory Computer Science (CS) courses
and developing children’s CT skills.
Similarly, in the literature there is no consensus regarding how to define
logical thinking (LT) [32]. In this article we consider that LT mainly focuses on
the abilities needed to identify entities (e.g. objects, concepts) and relationships
between them, and to understand, incorporate and soundly use the rules of logical
inference relevant to deriving new, implicit ideas in everyday activities, and to
reason about and judiciously choose among different logical formulations of the
same problem.
In this paper, we argue that Logic Programming (LP) is a viable and desir-
able paradigm choice for elementary school children, even for those with no
previous programming knowledge. Some research has been done to introduce
CS and to develop CT skills through LP for high school children [2,29,31,33].
Similarly, we consider we can fruitfully introduce CS and develop CT and LT
skills through LP to children. In addition to this discussion, in this paper we also
address the problem of determining what and how we should teach LP, analysing
teaching activities we consider suitable to face this challenge. For our proposals
we describe some game-based, unplugged and social good projects. Finally, we
describe an experience carried out in Argentina with children from 8 to 10 years
old, detailing what results are being obtained in the pilot project.
1
Blockly for Prolog Homepage https://guppy.eng.kagawa-u.ac.jp/∼kagawa/Member
s/Sano/prolog.html.
2
Blockly Prolog Homepage http://www.programmierkurs-java.de/blocklyprolog/.
Logic Programming at Elementary School 135
Note that natural language phrases representing conditions are written so that
its translation to a rule (Head, Body) is unambiguous and understandable for
children.
– Recursive rules: we present recursion (Clue 8) through an example inspired
by Kowalski in [13]: A José le gusta todo lo que le gusta a Rosa (José likes
everything Rosa likes).
gusta(jose,X):-gusta(rosa,X).
During the game, students receive four rewards according to what they have
learnt: constants and facts; variables and queries; rules; and finally, recursive
rules. We address the following CT and LT dimensions all over the game: algo-
rithm design, generalisation, abstraction and decomposition in human commu-
nication in order to formalise knowledge; logical deduction when new implicit
conclusions are obtained from the rules useful to explain this conclusion; and
decomposition when facing rule formalisation with and without recursion.
It is interesting to remark that students must build a large knowledge base
with several facts. The goal is to show that even if they can find out the thief
without a computer, they can do it faster with Prolog. This idea is to emphasise
the point of LP.
Moreover, in several stages the clues seem to be repetitive, in order to rein-
force the concept, so that students build a model for the construction of new
facts (generalisation).
As an alternative to the way in which the experience was presented and in
order to promote collaborative learning, student groups could be provided with
pieces of information that, when put all together, form a large knowledge base.
4 Experience
3
https://drive.google.com/file/d/1X0vtYx7PUfJK5DH1pvUDqLdbxviH1MEZ/
view?usp=sharing.
140 L. A. Cecchi et al.
and girls had remained attentive for longer than expected and that this had been
a great achievement of the activity and, at the same time, it was interesting to
observe some aspects of the group dynamics.
The teaching team considered it to be a beginner-friendly initial course to
CS for the children. Soon after, they started talking with a relevant lexicon and,
without feeling the pressure of being scrutinised, enjoyed their detective roles.
5 Conclusion
Logic Programming is a suitable programming paradigm choice for elementary
school children. We consider the development of CT skills as one strategy dimen-
sion, in order to bring LP close to educators during introductory courses for
children. However, in the context of our work we are interested in teaching CS
through LP, since our end goal is to encourage the inclusion of LP in the ele-
mentary school curriculum all over the world.
In this direction, we analysed the need of teaching resources and educational
methodological approaches and we presented different proposals for children. We
carried out a pilot project and our results show that children were able to solve
problems in a natural way with LP, focusing on key concepts of CS including
abstraction, knowledge representation and reasoning. We highlight how early in
the education process we can introduce CS, solving problems through declarative
programming paradigm.
Regarding “Why”, “What” and “How” LP should be taught to children, this
work provides, as a starting point, a framework that set guidelines for elementary
school curriculum design and implementation, showing a new possible way to
engage children in STEM.
Even though the preliminary results are encouraging, further efforts should
be done to set how elementary school teachers can be trained to provide LP
literacy education. An educational agenda that includes introductory courses
for teachers to fill this gap will complete the learning progression and will make
the framework sustainable over time.
It is also necessary to make sustained efforts to develop new technological
environments that allow the consolidation of LP in primary school increasing
and strengthening the initiatives carried out. In this direction, some educational
resources are currently being developed to engage children, create customised
lessons and increase classroom participation, e.g. in [21] the authors present an
interactive web environment based on browser-side Ciao Playground to guide
students in learning to program by way of Prolog.
Acknowledgements. The authors would like to thank the anonymous reviewers for
their very useful feedback on previous drafts of this paper. We are also grateful to all
the participants of Prolog’50’s Education Committee meetings for their fruitful, encour-
aging and inspiring discussions on teaching Prolog to students at all levels. Support
from Veronica Dahl’s NSERC grant 31611021 is also gratefully acknowledged.
Logic Programming at Elementary School 141
A Appendix
Run on or on
1 Prolog for Kids : Playing Detective
2 Code in textual Prolog corresponding to the block - based
3 Prolog code developed by 8 to 11 years old children
4 October 2022
5
6 %% Clue 1: Estela nos indica que en el lugar se encontraban 7 personas
involucradas en el hecho : Juan , Ana , Romualdo , Alicia , Jose , Rosa y
Pedro .
7 involucrado ( juan ) . involucrado ( ana ) . involucrado ( romualdo ) .
8 involucrado ( alicia ) . involucrado ( jose ) . involucrado ( rosa ) .
9 involucrado ( pedro ) .
10
11 %% Clue 2: Laura nos indica que en ese momento , estaban en el salon
principal tres personas que usan sombrero Juan , Ana y Pedro .
12 usa ( juan , sombrero ) . usa ( ana , sombrero ) . usa ( pedro , sombrero ) .
13
14 %% Clue 3: Roberto nos cuenta que ese dia habia dos personas mas que usaban
sombrero : Romualdo y Alicia .
15 usa ( romualdo , sombrero ) . usa ( alicia , sombrero ) .
16
17 %% Clue 4: Un informante anonimo mientras esperaba su vuelo pudo ver que
Juan , Jose , Rosa , Ana y Romualdo usaban lentes .
18 usa ( juan , lentes ) . usa ( jose , lentes ) . usa ( rosa , lentes ) .
19 usa ( ana , lentes ) . usa ( romualdo , lentes ) .
20
21 %% Clue 5: Cristian nos cuenta que a Rosa le gusta nadar ; a Alicia le gusta
bailar y pescar ; a Juan le gusta ver TV y bailar ; y a Romualdo le gusta
pescar , ver TV y bailar .
22
23 gusta ( alicia , bailar ) . gusta ( alicia , pescar ) . gusta ( rosa , nadar ) .
24 gusta ( juan , verTV ) . gusta ( juan , bailar ) . gusta ( romualdo , verTV ) .
25 gusta ( romualdo , bailar ) . gusta ( romualdo , pescar ) .
26
27 %% Clue 6: De los formularios que anoto Veronica podemos conocer que Ana y
Juan saben leer . Por otra parte , Ana sabe soldar y cantar . Tambien
sabemos que Romualdo sabe cocinar y Alicia sabe dibujar .
28 sabe ( juan , leer ) . sabe ( ana , leer ) . sabe ( ana , soldar ) .
29 sabe ( ana , cantar ) . sabe ( romualdo , cocinar ) . sabe ( alicia , dibujar ) .
30
31 %% Clue 7: Julio , basado en su enorme experiencia , nos dice que debemos
dudar de algunas personas . Una persona es dudosa si usa lentes . Por otra
parte , tambien nos informa que una persona es dudosa si sabe
dibujar .
32 dudosa ( X ) : - usa (X , lentes ) .
33 dudosa ( X ) : - sabe (X , dibujar ) .
34
35 %% Clue 8: Emma , nos aclara que a Jose le gusta hacer algo si eso le que
gusta hacer a Rosa . Ademas , nos informa que resulta misteriosa una
persona si sabe leer y le gusta nadar . Tambien resulta misteriosa una
persona si dudamos de ella y ademas esa persona usa sombrero .
36 gusta ( jose , X ) : - gusta ( rosa , X ) . % % Recursive Rule
37 misteriosa ( X ) : - sabe (X , leer ) , gusta (X , nadar ) .
38 misteriosa ( X ) : - dudosa ( X ) , usa (X , sombrero ) .
39
40 %% Clue 9: Nahuel , nos indica que una persona es sospechosa si es
misteriosa y le gusta pescar . Tambien nos informa que una persona es
sospechosa si es misteriosa , sabe soldar y le gusta nadar .
41 sospechosa ( X ) : - misteriosa ( X ) , gusta (X , pescar ) .
42 sospechosa ( X ) : - misteriosa ( X ) , sabe (X , soldar ) , gusta (X , nadar ) .
43
44 %% Clue 10: Ayelen , nos indica que la persona culpable es una persona que
esta involucrada , que es sospechosa y que sabe cocinar .
45 culpable ( X ) : - involucrado ( X ) , sospechosa ( X ) , sabe (X , cocinar ) .
142 L. A. Cecchi et al.
References
1. Bell, T.C., Witten, I.H., Fellows, M.: Computer Science Unplugged: off-line activ-
ities and games for all ages. Computer Science Unplugged (2015)
2. Beux, S., et al.: Computational thinking for beginners: a successful experience
using prolog. In: CILC, pp. 31–45 (2015)
3. Bocconi, S., et al.: Reviewing computational thinking in compulsory education.
Tech. rep, Joint Research Centre (Seville site) (2022)
4. Bubnic, B., Kosar, T.: Towards a consensus about computational thinking skills:
identifying agreed relevant dimensions. In: Psychology of Programming Interest
Group (2019)
5. Curzon, P., Dorling, M., Ng, T., Selby, C., Woollard, J.: Developing computational
thinking in the classroom: a framework. Project report, Computing at School (June
(2014)
6. Dahl, V., Cecchi, L.A.: Introducing prolog in language-informed ways. In: Warren,
D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog
- The Next 50 Years. No. 13900 in LNCS, Springer (2023)
7. Dahl, V., Moreno-Navarro, J.J.: Doughnut computing in city planning for achiev-
ing human and planetary rights. In: Ferrández Vicente, J.M., Álvarez-Sánchez,
J.R., de la Paz López, F., Adeli, H. (eds.) Bio-inspired Systems and Applications:
from Robotics to Ambient Intelligence. IWINAC 2022. Lecture Notes in Computer
Science, vol. 13259, pp. 562–572. Springer, Cham (2022). https://doi.org/10.1007/
978-3-031-06527-9 56
8. Ennals, R.: Logic as a computer language for children: core materials. Tech. rep,
Imperial College of Science and Technology Department of Computing (1982)
9. Grover, S., Pea, R.: Computational thinking in k-12: a review of the state of the
field. Educ. Res. 42(1), 38–43 (2013)
10. Guzdial, M.: Programming environments for novices. In: Fincher, S., Petre, M.
(eds.) Computer Science Education Research, pp. 137–164. Taylor & Francis (2005)
11. Hallström, J., Elvstrand, H., Hellberg, K.: Gender and technology in free play in
Swedish early childhood education. Int. J. Technol. Des. Educ. 25, 137–149 (2015)
12. Huang, W., Looi, C.K.: A critical review of literature on “unplugged” pedagogies in
K-12 computer science and computational thinking education. Comput. Sci. Educ.
31(1), 83–111 (2021)
13. Kowalski, R.: Logic for problem solving. Edinburgh University, Department of
Computational Logic (1974)
14. Kowalski, R.: Algorithm= logic+ control. Commun. ACM 22(7), 424–436 (1979)
15. Kowalski, R.: Computational logic and human thinking: how to be artificially intel-
ligent. Cambridge University Press (2011)
16. Kowalski, R., Datoo, A.: Logical English meets legal English for swaps and deriva-
tives. Artif. Intell. Law 30(2), 163–197 (2022)
17. Kowalski, R., Dávila, J., Sator, G., Calejo, M.: Logical english for law and educa-
tion. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi,
F. (eds.) Prolog - The Next 50 Years. No. 13900 in LNCS, Springer (2023)
18. Kowalski, R.A.: Logic as a computer language for children. In: ECAI, pp. 2–10
(1982)
19. Levesque, H.: Thinking as computation: a first course. The MIT Press (2012)
20. Lodi, M., Martini, S.: Computational Thinking, Between Papert and Wing. Sci.
Educ. 30(4), 883–908 (2021)
Logic Programming at Elementary School 143
21. Morales, J.F., Abreu, S., Hermenegildo, M.V.: Teaching prolog with active logic
documents. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R.,
Rossi, F. (eds.) Prolog - The Next 50 Years. No. 13900 in LNCS, Springer (2023)
22. Ottestad, G., Gudmundsdottir, G.B.: Information and communication technology
policy in primary and secondary education in Europe. Second Handbook of Infor-
mation Technology in Primary and Secondary Education, pp. 1–21 (2018)
23. Papert, S.A.: Mindstorms: children, computers, and powerful ideas. Basic books
(1980)
24. Prottsman, K.: Computational thinking meets student learning: extending the
ISTE standards. International Society for Technology in Education (2022)
25. Resnick, M., Maloney, J., Monroy-Hernández, A., Rusk, N., Eastmond, E., Bren-
nan, K., Millner, A., Rosenbaum, E., Silver, J., Silverman, B., et al.: Scratch:
programming for all. Commun. ACM 52(11), 60–67 (2009)
26. Saad, A., Zainudin, S.: A review of project-based learning (pbl) and computational
thinking (ct) in teaching and learning. Learn. Motiv. 78, 101802 (2022)
27. Shute, V.J., Sun, C., Asbell-Clarke, J.: Demystifying computational thinking.
Educ. Res. Rev. 22, 142–158 (2017)
28. Stables, K., et al.: Critical issues to consider when introducing technology education
into the curriculum of young learners, vol. 8(2) (spring 1997) (1997)
29. Tabakova-Komsalova, V., Stoyanov, S., Stoyanova-Doycheva, A., Doukovska, L.:
Prolog education in selected high schools in bulgaria. In: Warren, D.S., Dahl, V.,
Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50
Years. No. 13900 in LNCS, Springer (2023)
30. Wing, J.M.: Computational thinking. Commun. ACM 49(3), 33–35 (2006)
31. Yuen, T.T., Reyes, M., Zhang, Y.: Introducing computer science to high school
students through logic programming. Theory Pract. Logic Program. 19(2), 204–
228 (2019)
32. Yunus, Y.S.: Features of logical thinking of junior schoolchildren. Middle European
Scientific Bulletin 10 (2021)
33. Zhang, Y., Wang, J., Bolduc, F., Murray, W.G.: LP based integration of computing
and science education in middle schools. In: Proceedings of the ACM Conference
on Global Computing Education, pp. 44–50 (2019)
34. Zhang, Y., Wang, J., Bolduc, F., Murray, W.G., Staffen, W.: A preliminary report
of integrating science and computing teaching using logic programming. Proceed.
AAAI Conf. Artif. Intell. 33(01), 9737–9744 (2019)
Prolog Education in Selected Secondary Schools
in Bulgaria
Abstract. This article presents our activities for introducing the training of Pro-
log programming to the secondary school. The beginning was the development of
an appropriate curriculum. The results of an experiment conducted with a group
of selected students are summarized in this paper. A project is briefly presented,
the purpose of which is to provide an opportunity to share knowledge and experi-
ence about Prolog programming and at the same time help build a community of
interested students. In the conclusion, we have tried to summarize our experience
which would possibly help to introduce Prolog programming in secondary school
in other conditions.
1 Introduction
Recently, more and more efforts have been made worldwide to introduce the study of
artificial intelligence (AI) in secondary schools. European countries strive to take a lead-
ing position in the technological development in the field of AI and take care of the
rapid and comprehensive adoption of AI in their economy. Bulgaria is no exception to
this trend. A strategy for the development of artificial intelligence in Bulgaria until 2030
was published, including artificial intelligence in education and science. In 2019, Bul-
garia’s Ministry of Education and Science prepared a National Program for Innovations
in Secondary Education. The program addresses three types of innovation: innovative
curricula involving new subjects, new teaching methods, and innovative learning spaces
(STEM centres).
Implementing the program, the heads of schools from the region of the city of Plovdiv
contacted our team for the joint development of a curriculum for the introduction of
the discipline “artificial intelligence” in their schools. We have proposed a curriculum
including logical programming based on the Prolog language [1] as one of the main
topics (Fig. 1).
One of the ideas embedded in the program is to emphasize and demonstrate that
the Prolog language is a convenient means of representing and processing knowledge
in various subjects, studied in secondary school. For this reason, the course included
examples from subjects such as history, geography, literature, cultural and historical
heritage, etc. In this sense, one of the main approaches in the conducted experimental
training is the creation of a system of learning tasks related to the students’ knowledge
both in the other school subjects and in their everyday life [2]. The conclusion that can
be drawn from our observations is that students like this type of learning. Moreover,
there is an increase in interest in the subjects covered by the examples.
A total of 115 students participated in the training during the two years. In the course
of the training, we carried out several surveys related to the attitude of the students toward
the training being conducted. Survey results [3] show that over 70% of students believe
that studying AI is useful for their future development and are willing to study it in the
future. Only 8% of the surveyed students answered that this training has no relevance to
their future life. The results show that the students have mastered the learning material
at a very good level. However, a number of difficulties are also observed. According to
the students, the use of the Prolog language is appropriate and motivating.
Our observations show that Prolog programming can be efficiently studied in dif-
ferent forms and with different age groups of students [4]. Our secondary education
legislation allows for the use of different forms of learning. We mainly used one of
the eligible forms of training known as “interest-based activities” which means extra-
curricular activities that take place outside the normal curriculum and are voluntary.
In this way, interest-based education supports the development of students’ key com-
petencies in the fields of mathematics, informatics, natural sciences, and technology.
The experience accumulated so far allows us to say that the interest and motivation of
students are constantly growing and logic and Prolog programming can be successfully
introduced in school education to different degrees, in various forms, and in different
volumes. The approved curriculum and the associated teaching material can be used
146 V. Tabakova-Komsalova et al.
3 An Experiment
The regular education of students from selected schools will start the next academic
year with the curriculum proposed in Fig. 1. We are currently conducting training with
teachers who will be involved in teaching the students. At the same time, we are carrying
out an experiment with a group of students, the purpose of which is to check how
successful our approach is by implementing conditions close to real teaching. In this
section, we would like to demonstrate the results of the experiment with three examples
from different school subjects. The following examples were prepared by secondary
school students who studied imperative programming as a mandatory form of education.
The first example deals with kinship relationships, one of the most exploited intro-
ductory examples in Prolog programming books. Figure 2 shows the solutions of two
students: the first one created a Prolog program by changing the school subject (the Krum
dynasty from the history of Bulgaria), and the second presented the genealogical rela-
tionships between the characters of a large patriarchal family from a famous Bulgarian
novel, studied in the subject of Bulgarian literature.
In Student1’s solution, the template was adapted for the royal dynasty from Bul-
garian history while Student2’s solution adapted the template for family relationships
described in a famous Bulgarian novel. In both cases, the students used one-position
axioms man/1 and woman/1. The first student used additional two-position axioms for
son/2 and daughter/2 by which (s)he defined a predicate for a parent. The second student
introduced the parent relationship as a two-position axiom parent/2. The learners used
a different representation of the constants. Students easily learn to construct rules by
describing kinship relationships such as father/2 and mother/2. In this case, the teacher
set a template without a sample code and the students gave different definitions. They
used variables in the rules to describe generally valid definitions. After that, they moved
on to building more complex logical kinship relationships (grandfather/2, brother/2,
uncle/2, etc.), as described in [10]. In this sense, if the logic we write is correct, Prolog
will answer any question related to the described knowledge base. Our experience shows
that after mastering this basic example, students easily adapt it to similar tasks from other
disciplines (history, geography, biology, chemistry, etc.).
The second example (Fig. 3) demonstrates a game template described in its Artefact
Description section. A student presented an adaptation for the subject of geography in
the form of a game named “Guess the city”.
Various exercises presented as games are used to increase the interest of students.
This example is also interesting because we believe that games are an effective way
to introduce children to computers. This view can also be seen for example in [11]. In
this case, the students have to create a game that tests their knowledge of characteristic
sights of Bulgarian cities. The program in the above example initiates a dialog with
the user. Depending on the answers, it tries to guess the city the player has in mind. If
confirmation of a given hypothesis is found, the program should not continue looking
for another solution. The user dialog uses the control predicate -> /2 from SWI-Prolog
to mark positive and negative answers.
We would like to briefly comment on the solutions given in Fig. 3. We are of the
opinion that the problem of declarative and imperative style is a sensitive issue. Our
experience shows that we should approach it very carefully. On the one hand, students
Prolog Education in Selected Secondary Schools in Bulgaria 149
and much shorter code. This teacher is now our biggest supporter and enjoys teaching
students in Prolog. Our findings confirm those detailed in [12]. A combination of logic
programming and imperative programming is discussed in [13].
The third example is from Bulgarian folklore and more specifically, embroideries.
The template is prepared as a logical problem. The task to solve is given in the Artefact
Description part of the template. Figure 4 presents one of the solutions provided by the
students.
The program in Fig. 4 is relatively advanced for beginners. It is a constraint problem
to find a linear arrangement of four coloured shapes that satisfy a set of constraints on
how they can be arranged. The student uses a generate and test strategy. Shape_color/1 is
true for satisfying a condition represented as a list of shape-colour pairs, where the shapes
and colours are given in the shape/1 and color /1 predicates. Five specified constraints
are identified by integers 1–5. The first 6 subgoals in the shape_color/1 definition non-
deterministically generate all possible states, essentially using permutation1/2. The last
subgoal, check_rules/2, checks that the generated state satisfies all 5 constraints. The
constraint is checked by the r/2 clause. Each constraint is checked using select/3 and
append/3 to verify that the positions of the coloured figures in the state list do indeed
satisfy the specified requirement.
The solution to the third example was provided by a student with a very good
mathematical background. Unlike most students, he demonstrates a good understand-
ing and use of list structures and recursion. Also, the solution uses its own predicates
(permutation, select, member, append) instead of the built-in ones.
Prolog Education in Selected Secondary Schools in Bulgaria 151
4 Conclusion
This article presents our activities for introducing the training of Prolog programming
to the secondary school. Our two-year relatively successful experience motivates us to
improve the training approach and look for new educational forms. For this purpose,
we conducted an experiment which is presented in this article. From this experiment,
we have selected three distinctive examples. The first one shows the type of examples
that students do well and enjoy working on. The second one demonstrates a blending of
imperative and declarative programming styles. The third one shows that students with
a good mathematical background can also cope with more difficult tasks for beginners.
One main conclusion we can draw is that it is of utmost importance that computer
science teachers are consistently convinced step by step that the “peaceful coexistence” of
declarative and imperative styles is not only possible but even useful and can bring many
152 V. Tabakova-Komsalova et al.
References
1. Kowalski, R.: Predicate logic as programming language. In: Proceedings IFIP Congress,
pp. 569–574. North-Holland Publishing Co., Stockholm (1974)
2. Glushkova, T., Stoyanov, S., Tabakova-Komsalova, V., Grancharova-Hristova, M., Krasteva,
I.: An approach to teaching artificial intelligence in School. In: Smyrnova-Trybulska, E.
(ed.) Innovative Educational Technologies, Tools and Methods for E-learning, 12, Katowice–
Cieszyn 2020, pp. 257–267. https://doi.org/10.34916/el.2020.12.22
3. Tabakova-Komsalova, V., Glushkova, T., Krasteva, I., Stoyanov, S.: AI training – approaches,
results, analyses and conclusions. In: Smyrnova-Trybulska, E. (ed.) E-learning in the Time
of COVID-19 , 13, Katowice–Cieszyn 2021, pp. 176–186. https://doi.org/10.34916/el.2021.
13.15
4. Tabakova-Komsalova, V., Glushkova, T., Grancharova-Hristova, M., Krasteva, I.: Learning
tasks in artificial intelligence education. Educ. Technol. 11/2020(1), 15–22, 233–240. ISSN
1314–1791 (PRINT), ISSN 2535–1214 (ONLINE) (2020) https://doi.org/10.26883/2010.
201.2292
Prolog Education in Selected Secondary Schools in Bulgaria 153
5. Cecchi, L., Rodrıguez, J., Dahl, V.: Logic programming at Elementary School: why, what and
how should we teach logic programming to children?. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years. No. 13900 in
LNCS, Springer, July 2023
6. Dahl, V., Cecchi, L.: Introducing prolog in language-informed ways. In: Warren, D.S., Dahl,
V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years.
No. 13900 in LNCS. Springer, July 2023
7. Cervoni, L., Brasseur, J., Rohmer, J.: Simultaneously teaching Mathematics and Prolog in
school curricula: a mutual benefit. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M.,
Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years. No. 13900 in LNCS, Springer, July
2023
8. Kowalski, R., Dávila, J., Sartor, J., Calejo, M.: Logical English for Law and Education, In:
Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog -
The Next 50 Years. No. 13900 in LNCS, Springer (July 2023)
9. SWI-Prolog. https://www.swi-prolog.org/
10. Warren, D.S.: Introduction to Prolog. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M.,
Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years. No. 13900 in LNCS, Springer, July
2023
11. Genesereth, M.: Dynamic Logic Programming. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years. No. 13900
in LNCS, Springer, July 2023
12. Hermenegildo, M.V., Morales, J.F., Lopez-Garcia, P.: Some thoughts on how to teach Prolog.
In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog
- The Next 50 Years. No. 13900 in LNCS, Springer, July 2023
13. Kowalski, R., Sadri, F., Calejo, M., Dávila, J.: Combining logic programming and imperative
programming in LPS. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R.,
Rossi, F. (eds.) Prolog - The Next 50 Years. No. 13900 in LNCS, Springer, July 2023
Introducing Prolog in Language-Informed
Ways
1 Introduction
In acquiring language, humans unconsciously acquire many of the skills and
notions that are also needed in general for logical thinking and for computa-
tional thinking, and also for understanding, using, and creating Prolog programs
in particular1 . For instance, the concept of a list is implicit in the linguistic
competence of even toddlers, owing to their naturally acquired proficiency with
lists of sounds. Of course, this competence rests on their also naturally acquired
understanding of symbols: sequences of sounds in a given order are recognized
as pointing the mind to something other than themselves, just from humans
becoming exposed to language. Typically, children younger than 2 years old
already recognize the meaning of many sentences, even if they reproduce them
in ways only grammatical to their own personal dialect. For instance, they might
omit the determiner “the” when asking for a door to be opened, or pronounce
“opi” rather than “open”.
We argue that using students’ conscious and unconscious language and gram-
mar proficiency, plus applying what we know about language acquisition, can
make Prolog much more accessible, consequently maximizing our potential to
1
In the remainder of this article, we highlight sample notions in bold type as we relate
them to language.
c The Author(s), under exclusive license to Springer Nature Switzerland AG 2023
D. S. Warren et al. (Eds.): Prolog: The Next 50 Years, LNAI 13900, pp. 154–167, 2023.
https://doi.org/10.1007/978-3-031-35254-6_13
Introducing Prolog in Language-Informed Ways 155
2 Motivation
Human language is, arguably, one of the most complex, intricate and subtle
abstract systems ever invented. Yet natural language skills and notions are
among the first, best learned and most universally and assiduously practised
human skills and notions.
At the same time, much of the (largely unconscious) linguistic competence
of even small children relates directly to concepts also needed to learn Prolog,
logic thinking [28] and computational thinking [35].
This motivates us to explore the reaches of a) taking language anchored
approaches to learn or solidify Prolog concepts as our pedagogical pivot, and
b) counting Prolog grammars among our main tools. We shall substantiate this
position through a series of discussed examples.
Preliminary empirical evidence for our position was provided by several initial
experiences in teaching Prolog to children [6]. This approach was also successful
with university students of humanistic careers taught by Dahl over the years2 .
We next discuss and exemplify how logic grammars, owing to their close
relationship to language, are particularly well-suited to make those unconscious
concepts more explicit and easily relatable to programming concepts.
say Something.
Such drills will familiarize students with the versatility of Prolog/Prolog gram-
mars, and naturally elicit further concepts such as that of variables, such
as “Something” above; that of constants, exemplified by object words, that
of data structures, exemplified through lists (of words) and through terms
(such as those represented by drawing derivation trees, and later, through terms
constructed to represent meaning); the notion that declarative specifica-
tions have procedural interpretations, through showing that languages can
be described or specified through (sequence-sensitive) rewriting rules and that
querying a grammar makes it directly executable; the notion of a (logic) gram-
mar itself, as describing a language using its words (called terminal symbols),
special words about them serving to group them (called meta-, or non-terminal
symbols), and rewriting rules that do so; the notion of existential variables as
those in queries, which “ask” for desired values for them that satisfy the query;
4
Whether a language has a noun or verb bias appears to be culture and activity
dependent [33], but our same argument applies transposed to any other first-learned
constituents.
5
We freely use syntactic sugar throughout the paper for readability because it is easy
to guarantee through compilation into more cumbersome but “legal” forms, e.g.
by automatically adding square brackets around terminal symbols. Full notational
details are explained in [34], pp. 13–14.
Introducing Prolog in Language-Informed Ways 157
Teachable moments here, such as finding out why reversing the order of the last
two rules might leave our sheep speechless, could alert students about the need to
judiciously place end-of-recursion rules (and clauses) first, and generally become
aware of the practical implications of left-to-right, top-down orderings in
Prolog as well as in (logic) grammars. Since Prolog’s strategy is already second-
nature to creatures whose language and life events, similarly, follow sequential
temporal orderings, we feel such an awareness could productively be elicited as
soon as the opportunity arises.
How explicitly we present the procedural aspects, how “proper” a terminol-
ogy we introduce, and how much syntactic sugar we make available is, in our
opinion, the teacher’s choice, to be made in function of their audience’s needs
and background. The wonderful thing is that Prolog’s modularity and versatility
allow us, whenever desired, to focus only the declarative parts of a grammar or
program. For instance, students of literature with no computer background need
not scrutinize any of the utilites that handle input/output, and are thus free to
only learn (at first) how to consult it for their various specific purposes.
non-terminal symbols, grammars can also morph into producers and con-
sumers of structure, be it semantic or other, related to the target language.
For instance, to obtain the term command(open(door)) as the meaning rep-
resentation for the utterance “open door” (e.g. because we want to command a
robot through natural language, as in [12]), all we have to do is complete the
grammar of Example 1 with semantic structure building arguments6 , and call it
in analysis mode (i.e., by specifying a concrete sentence as input and a variable
as output).
6
We abuse notation here again, for readability: variables are not legal, in most Prologs,
as function names. Again we assume a compiler that “legalizes” our more human-
readable notations.
Introducing Prolog in Language-Informed Ways 159
resulting information also through NL, for further drilling. Sample output for a
knowledge base creation grammar that admits a larger coverage of English [14]
is shown in SampleOutput.pdf7 .
7
https://drive.google.com/file/d/1miCb6A21qZbO2wXKqu-QB4WpaCcoXPS7/vie
w?usp=sharing.
8
Natlog modifies the syntax of Prolog’s clauses to make them closer to NL.
160 V. Dahl and L. A. Cecchi
An interesting and fairly general approach for leading from language into
Prolog is that of Logical English [26], where users express sentences as close
paraphrases of Prolog clauses, to in particular minimize ambiguities, e.g. for
creating legal and educational databases.
It may not always be practical to use NL for generating more involved Pro-
log programs than those defining moderately straightforward knowledge bases,
but interesting subsets of NL will often be useful for consulting more complex
systems implemented in Prolog, such as those in robotics or planning. For such
applications, we favour developing parsers tailored to application-appropriate
NL subsets.
Query languages for Prolog systems are, appropriately, quite varied, since
they are designed so that a query’s evaluation with respect to the underlying
logic system directly produces its answer, even when the logic system involved
is richer than Prolog, e.g. multi-valued.
In [12], for instance, the NL subset we chose for queries mostly comprises
imperative sentences of a certain form, whose intended recipient (a given robot)
is often implicit, and whose “meaning” is evaluated into instructions necessary
to command mini-robots at Sophia Antipolis University, from natural language
commands such as “Go to point P taking care that the temperature remains
between X and Y degrees.”. In [13], instead, we evaluate the meaning of a user’s
query (e.g., “A system that has the optional processor MTS05 and the memory
module SMX08, and whose memory size is different from 12 K words”) through
first mapping it into a first-order-logic meaning representation of it, whose (co-
routined) execution yields descriptions of a computer system satisfying the user’s
query. In another application, e.g. if we want students to learn/drill on first-order
logic, just the first-order logic expression of an input phrase might be considered
to be its meaning.
Such targeted parsers, while giving up generality, can provide natural-
sounding human language interfaces to even sophisticated Prolog systems, with-
out the user having to resort to contrived paraphrases. Moreover, we have
observed that using NL front ends for a variety of sophisticated Prolog appli-
cations can go a long way towards making Prolog engaging and interesting to
learn. Automatic disambiguation capabilities can often be included, as usual,
taking advantage of context specificities9 .
9
Note, however, that not all NL ambiguities will be resolvable even by humans, e.g. the
ambiguity of “sentence” may remain even when a legal context is assumed (consider
for instance “This judge is famous for their kilometric sentences)”.
Introducing Prolog in Language-Informed Ways 161
But this is also true of many other curricular disciplines’ definitions, cf. for
instance the molecular biology definition “a tandem repeat is a DNA sequence
followed by the same sequence, any number of times”. Specialized controlled
languages can help students learn such concepts both informally (as expressed
in words) and formally (as translated automatically from words into Prolog
grammars or programs), as well as providing opportunities to drill.
Even the simplest of grammars can help children become acquainted not
only with linguistic concepts they understand but have not yet made conscious
or named, but also with a variety of other subject-matter concepts. For instance,
the philosophical concept of “meta” can be introduced by helping children dis-
tinguish words in the object language (such as “works”) vs. in the meta-
language of grammatical constituent names (such as “verb”). Most helpfully,
Prolog’s grammatical notation itself, once introduced, will clearly help them
distinguish object language words explicitly through square brackets.
explicit goal of meeting the needs of all within planetary limits. Many munici-
palities, such as Portland, Quebec, and Amsterdam, have already adopted this
goal and are trying to implement it.
Our grammar, intended as a computational enabler for DE, presents users
with possible solutions to a (quantifiable) problem in either an ecological or a
social area. Users must enter the quantity (in percentages or in units of measure-
ment) representing the present status and goal status, plus a name and number
for each of the those actions that would contribute, if taken, to solve it, as well
as the quantity. Quantities must, consistently, be stated in percentages or units
of measurement by which the action, if taken, would contribute to the problem’s
solution. For instance, for reducing carbon emissions in Canada from a current
level of 15 tonnes per capita to zero, sample actions (numbered from 1 to 6, and
respectively contributing CO2 decreases of 8, 7, 6, 4, 3 and 1 units of measure-
ment) might be:
social or ecological, and propose remedies to possible bad side effects. For example,
those left unemployed by reducing a high emitting sector could be retrained into
care work or green jobs. Initial prototypes have been explored through extending
the grammar shown in https://swish.swi-prolog.org/p/CO2.pl into including, in
the description of a possible action, also its (quantifiable) side effects on any eco-
logical or social indicator, plus including a calculation of how these side effects
might interact for overall balance. CLP alternatives are being explored as well, in
joint work by V. Dahl and S. Abreu.
10
These numbers do not include the many more cases either never reported, unprovable
or “mild” (with degree of “mildness” assessed from the incestor’s, not the victim’s
perspective, since legal systems and social order throughout the world tend to protect
the (typically male) incestors rather than their victims.
164 V. Dahl and L. A. Cecchi
11
https://www.safeatschool.ca/resources/resources-on-equity-and-inclusion/sexism/
tool-kits-and-activities.
12
For instance, redistributing unpaid/paid work; a basic universal income; a basic
equity income; professional daycare, etc.
Introducing Prolog in Language-Informed Ways 165
5 Conclusion
We have discussed how Prolog and logical/computational thinking can be intro-
duced most naturally and efficiently by tapping into students’ pre-existing
human language proficiency, if possible around themes that deeply concern or
affect them. Related research which also stresses language in teaching compu-
tational thinking for beginners suggests the optimal timing is before imperative
programming [4] –i.e., as soon as possible. We believe our proposed tack stands
to also help move AI into more than just “ethical” concerns, into a regenerative
and redistributive new phase. But in any case, it can provide a useful umbrella
and/or complement to other interesting Prolog teaching approaches and initia-
tives, some of which are described in this same volume ( [6–8,21,24,26,31,32])
or discussed in our closely related Prolog 50 Education initiative13 .
Last but not least, a heartfelt appeal to the Prolog community: please join
our efforts!
References
1. Bannour, N., Ghannay, S., Névéol, A., Ligozat, A.L.: Evaluating the carbon foot-
print of NLP methods: a survey and analysis of existing tools. In: Proceedings
of the Second Workshop on Simple and Efficient Natural Language Processing.
Association for Computational Linguistics, Virtual (Nov 2021)
2. Bavarian, M., Dahl, V.: Constraint-based methods for biological sequence analysis.
J. Univ. Comput. Sci. (2006)
3. Bender, E.M., Koller, A.: Climbing towards NLU: on meaning, form, and under-
standing in the age of data. In: Proceedings ACL, pp. 5185–5198. Association for
Computational Linguistics (2020)
4. Beux, S., et al.: Computational thinking for beginners: a successful experience
using prolog. In: CILC - Italian Conference on Computational Logic, vol. CEUR
1459, pp. 31–45 (2015)
5. Birhane, A.: Algorithmic injustice: a relational ethics approach. Patterns 2(2),
100205 (2021)
6. Cecchi, L.A., Rodrı́guez, J.P., Dahl, V.: Logic programming at elementary school:
why, what and how should we teach logic programming to children? In: Warren,
D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog
- The Next 50 Years. No. 13900 in LNCS, Springer (July 2023)
7. Cervoni, L., Brasseur, J.: Teaching prolog and python: the perfect match for artifi-
cial intelligence. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski,
R., Rossi, F. (eds.) Prolog - The Next 50 Years. No. 13900 in LNCS, Springer (July
2023)
13
https://prologyear.logicprogramming.org/Education.html.
166 V. Dahl and L. A. Cecchi
8. Cervoni, L., Brasseur, J., Rohmer, J.: Simultaneously teaching mathematics and
prolog in school curricula: a mutual benefit. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years. No.
13900 in LNCS, Springer (July 2023)
9. Clark, E.V., Casillas, M.: First language acquisition. In: The Routledge handbook
of linguistics, pp. 311–328. Routledge (2015)
10. Dahl, V.: Un système déductif d’interrogation de banques de données en espagnol.
Ph.D. thesis, Universite Aix-Marseille II (1977)
11. Dahl, V.: Doughnut computing: aiming at human and ecological well-being. In: 6th
International Conference on the History and Philosophy of Computing (HAPOC-6)
(2021)
12. Dahl, V., Fall, A., Thomas, M.C.: Driving robots through natural language. In:
1995 IEEE International Conference on Systems, Man and Cybernetics. Intelligent
Systems for the 21st Century, vol. 2, pp. 1904–1908 vol 2 (1995)
13. Dahl, V., Sambuc, R.: Un système de bases de données en logique du premier ordre,
en vue de sa consultation en langue naturelle. Universite Aix-Marseille II, Tech.
rep. (1976)
14. Dahl, V.: From speech to knowledge. In: Information Extraction: Towards Scal-
able, Adaptable Systems, vol. 1974, pp. 49–75. LNAI (Lecture Notes in Artificial
Intelligence (1999)
15. Dahl, V., Bel-Enguix, G., Tirado, V., Miralles, E.: Grammar induction for under-
resourced languages: the case of ch’ol. In: Gallagher, J., Giacobazzi, R., Lopez-
Garcia, P. (eds.) Analysis, Verification and Transformation for Declarative Pro-
gramming and Intelligent Systems. No. 1316 in LNCS, Springer, Cham (2023).
https://doi.org/10.1007/978-3-031-31476-6 6
16. Dahl, V.: Logical design of deductive, natural language consultable data bases.
In: Proceedings V International Conference on Very Large Data Bases, pp. 24–31
(1979)
17. Dahl, V.: Quantification in a three-valued logic for natural language question-
answering systems. In: Proceedings IJCAI’79, pp. 182–187 (1979)
18. Dillon, S., Craig, C.: Storylistening: Narrative Evidence and Public Reasoning.
Routledge, Oxford (2021)
19. Dussy, D.: Le berceau des dominations: Anthropologie de l’inceste, livre 1. Maury
Imprimeur, France (2022)
20. Eisler, R., Fry, D.P.: Nurturing Our Humanity: How Domination and Partnership
Shape Our Brains, Lives, and Future. Oxford University Press, Oxford (2019)
21. Genesereth, M.: Dynamic programming. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years. No.
13900 in LNCS, Springer (July 2023)
22. Gray, M., Siddharth, S.: Ghost Work: How to Stop Silicon Valley from Building a
New Global Underclass. Houghton Mifflin Harcourt (2019)
23. Gupta, G., et al.: Logic-based explainable and incremental machine learning. In:
Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.)
Prolog - The Next 50 Years. No. 13900 in LNCS, Springer (July 2023)
24. Hermenegildo, M.V., Morales, J.F., Garcia, P.L.: Some thoughts on how to teach
Prolog. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R.,
Rossi, F. (eds.) Prolog - The Next 50 Years. No. 13900 in LNCS, Springer (July
2023)
25. Kashmiri, Z., Masram, A.: Elements of research based pedagogical tools for teach-
ing science. Educ. Quest- Int. J. Educ. Appl. Soc. Sci. 11(3), 189–192 (2020)
Introducing Prolog in Language-Informed Ways 167
26. Kowalski, R., Davila, J., Sator, G., Calejo, M.: Logical english for law and educa-
tion. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi,
F. (eds.) Prolog - The Next 50 Years. No. 13900 in LNCS, Springer (July 2023)
27. Lakoff, R.: The Language War. University of California Press, California (2020)
28. Levesque, H.: Thinking as Computation: A First Course. The MIT Press, Cam-
bridge (2012)
29. McQuillan, D.: Resisting AI. Bristol University Press, Bristol (2022)
30. Raworth, K.: Doughnut Economics: Seven ways to think like a 21st-Century
Economist. Chelsea Green, White River Junction, Vermont March (2017)
31. Tabakova-Komsalova, V., Stoyanov, S., Stoyanova-Doycheva, A., Doukovska, L.:
Prolog education in selected high schools in Bulgaria. In: Warren, D.S., Dahl, V.,
Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50
Years. No. 13900 in LNCS, Springer (July 2023)
32. Tarau, P.: Reflections on automation, learnability and expressiveness in logic-based
programming languages. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M.,
Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years. No. 13900 in LNCS,
Springer (July 2023)
33. Tardif, T., Gelman, S.A., Xu, F.: Putting the “Noun Bias” in context: a comparison
of English and Mandarin. Child Development 70(3), 620–635 (1999)
34. Warren, D.S.: Introduction to Prolog. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years.
No. 13900 in LNCS, Springer (July 2023)
35. Wing, J.M.: Computational thinking. Commun. ACM 49(3), 33–35 (2006)
Tools for Teaching Prolog
Teaching Prolog with Active Logic
Documents
1 Introduction
Teaching programming languages traditionally relies on an array of dispersed
materials, such as textbooks, class notes, slides, or exercise sheets, as well as
Partially funded by MICINN projects PID2019-108528RB-C21 ProCode, TED2021-
132464B-I00 PRODIGY, and FJC2021-047102-I, by the Comunidad de Madrid pro-
gram P2018/TCS-4339 BLOQUES-CM, by FCT under strategic project UIDB/-
04516/2020 (NOVA LINCS) and by the Tezos foundation. The authors would also
like to thank the anonymous reviewers for very useful feedback on previous drafts of
this paper.
c The Author(s), under exclusive license to Springer Nature Switzerland AG 2023
D. S. Warren et al. (Eds.): Prolog: The Next 50 Years, LNAI 13900, pp. 171–183, 2023.
https://doi.org/10.1007/978-3-031-35254-6_14
172 J. F. Morales et al.
1
https://jupyter.org/.
Teaching Prolog with Active Logic Documents 173
– Some course developers prefer to develop (or have already developed) their
teaching materials with their own tools (such as, e.g., LaTeX, PowerPoint,
Pages, Word, etc.), which have been stable for a long time, and may be
reluctant to port these materials to a bespoke platform. For this case we offer
a “click-to-run” methodology, based on the Ciao Prolog playground, which
we describe in Sect. 2. This provides a very easy way to incorporate click-to-
run capabilities in any kind of teaching materials, independently of the tool
used to generate them or the generated format (pdf, html, etc.), and with no
dependencies on any central server.
– For course developers that are willing to port their materials, we offer a tool
(an extension of the LPdoc documenter) that greatly facilitates generating,
using any conventional editor, web-based materials with embedded examples
and exercises. These will run locally on the student’s browser, again with no
dependencies on any central server. We describe this part of our approach in
Sect. 3.
which opens up a wide degree of possibilities for interaction. With this step,
documents with embedded editable and runnable examples can be generated
easily using LPdoc. The source that the developer of the course, tutorial, etc.
works with is one or more LPdoc source files, in, e.g., markdown format. LPdoc
processes these files and generates html pages in which the code fragments in
the source are automatically rendered as editable panes that can be run in place
in an embedded playground (as well as loaded into the standalone playground
as before). The generated pages can then be published easily as any other web
pages, e.g., in a course web site, in a personal public_html area, etc. Everything
needed, including the runnable examples, queries, etc., is contained in the pages
themselves. When students visit these pages with their browser, all executions
occur locally in their browser.
Interaction facilities for Self-assessment. Especially in the context of
a self-taught Logic Programming course, the embedded playground approach
allows for very rich interactions. That is, code can be evaluated and edited
directly in the same document. This enables direct support for self-evaluation
and testing mechanisms. For example, code examples allow automated “seman-
tic” evaluation of user input, e.g., by running code tests on the solution provided
by the student. Document-level dependencies between examples, topics, and sec-
tions, allow “gamification” (e.g., evaluating your progress, obtaining points and
trophies, hiding/showing tasks, un-blocking levels, etc.) of the learning activi-
ties, ensuring that the reader can acquire the necessary confidence on basic topics
before going on to more advanced concepts.
Moreover, the Prolog top-level loop which underlies the Playground can inter-
pret terms which result from solutions to goals in more ways than just printing
them out. Similarly to Prolog’s display/1 predicate, some terms may be inter-
preted as giving rise to graphical or other user-interface components.
9
https://ciao-lang.org/ciao/build/doc/ciao_playground.html/.
Teaching Prolog with Active Logic Documents 177
This can be easily achieved with the following markdown with embedded Prolog
code:
In the resulting panel, the query may be edited and pressing on the triangle
executes it in place:
Regarding scoping, there is essentially one Ciao Prolog top level per page: all
programs in the page are loaded into this Ciao Prolog top level and all queries
in the same page are executed in that top level, against all the code (possibly
separate modules) that has been loaded into the top level up to that point. Code
can be (re)loaded anytime by clicking on the green tick mark in the top left of the
pane; this facility could be used, for example, to reset the state of the program.
After perhaps mentioning that the Peano approach is elegant but inefficient,
we could propose an actual exercise, which is to rewrite the code using Prolog’s
is/2 (or constraints!):
Here the pane is again editable and contains the original (Peano) code
adorned with comments, all of which act as hints or instructions on how to
proceed. Of course, this description could also be somewhere else, e.g., in the
surrounding text. Clicking on the yellow face will perform the evaluation, in this
case running some (hidden) unit tests [11], on the code in order to give feedback
to the student. Other evaluation methods (e.g., running a program analysis or a
Teaching Prolog with Active Logic Documents 179
mere syntactic comparison) can also be useful. It is also possible for the student
to give up and ask for the solution, in which case the proposed solution will be
shown and can be executed.
All this functionality can be generated using the following code:
The included unit tests are the ones that will be run to test the student’s code
(a small subset has been included for brevity). The segment within hint directives
behaves similarly to the focus segments but represents a hint or instructions, and
will be replaced by the solution, should it be asked for. The solution, if provided,
is marked with the corresponding directives.
The appendix provides a complete example of a class exercise based on the
code fragments above, showing the full source and the full output. The result-
ing, working Active Logic Document can be found, as mentioned before, as an
example in the Ciao playground manual [4].10
10
https://ciao-lang.org/ciao/build/doc/ciao_playground.html/.
180 J. F. Morales et al.
Our first attempt at this was the Ciao Prolog JavaScript compiler back-
end [12], that enabled the use of Prolog and, in general, (constraint) logic pro-
gramming to develop not just the server side, but also the client side of web
applications, running fully on the browser. Tau Prolog [14] and the tuProlog play-
ground11 are recent Prolog interpreters written in JavaScript which also make
it easy to run Prolog in a web page, serverless. While these JavaScript-based
approaches are attractive, they also have drawbacks. Compilation to JavaScript
was a good option at the time, since it was a client (i.e., browser)-based solution
and the resulting speed made it useful for many applications. However, perfor-
mance does suffer with respect to native implementations (see [12]). This is even
more pronounced in the case of the Prolog interpreters written in JavaScript
mentioned above. It is precisely this performance impact that has led to the
development of the WebAssembly virtual machine [6]12 , which is currently sup-
ported by all major browsers.
WebAssembly and the supporting compilation toolchains, such as
Emscripten [16], enable programs written in languages supported by LLVM to be
compiled to a form which can be executed entirely in the browser, i.e., without
any server-side intervention at runtime, all with very reasonable efficiency. This
is the approach used by the Ciao playground in order to be able to run Prolog
code in the browser. The playground uses the standard Ciao engine, compiled
to WebAssembly using the Emscripten C compiler and the Ciao library for C,
which offers functions for term creation, type conversions, term checks, queries,
and calls. The result is that in the playground Prolog code runs with performance
that is competitive with native Prolog implementations. Additionally, the Ciao
environment is comprised of several independent bundles (collections of modules)
which can be compiled independently and demand-loaded from WebAssembly.
The WebAssembly port of Ciao Prolog thus supports most of the system’s soft-
ware tools, such as LPdoc, CiaoPP (including the testing framework), etc., all
of which are written in Prolog.
Fig. 2. Adding gameplay functionality in a course for children. This task is accompa-
nied with introductory text (not shown here) that carefully explains that above(X,Y)
must be read as X is above Y, etc. Rather than introducing infix operators at this
very early stage, the course begins with trivial formalization tasks to get familiar with
syntax and abstraction.
with any editor. We argue that this approach has multiple advantages from the
point of view of scalability, maintenance cost, security, ease of packaging and
distribution, etc.
Our tools evolved as a side-effect of the development of our own materi-
als over the years for teaching logic programming13 , embedding runnable code
and exercises in tutorials14 , slides15 , manuals, etc., and they are currently being
used in other projects, such as for example in the development of a Program-
ming course for young children (around 10 years old) within the Year of Prolog
initiatives. The latter effort has implied the inclusion of additional useful fea-
tures in the toolset, such as a “gameplay” which progressively discloses more
advanced parts of the course while striving to keep the interaction interesting
and challenging (see Fig. 2). The JavaScript interface provided by the tools and
the access to Web technology enable endless possibilities for richer Web-based
interaction (e.g., SVG visualization of facts), media rich interactions, touch/click
inputs, audio, graphics or videos, etc.
13
See also [9] in this same book.
14
E.g., Interactive CiaoPP tutorials https://ciao-lang.org/ciao/build/doc/ciaopp_
tutorials.html/.
15
E.g., Course material in Computational Logic: https://cliplab.org/~logalg.
182 J. F. Morales et al.
Appendix
Fig. 3. The full source and LPdoc output for the Active Logic Document for the simple
factorial exercise.
References
1. Arias, J., Carro, M., Salazar, E., Marple, K., Gupta, G.: Constraint answer set
programming without grounding. Theory Pract. Logic Program. 18(3–4), 337–354
(2018). https://doi.org/10.1017/S1471068418000285
2. Brecklinghaus, A., Koerner, P.: A Jupyter kernel for Prolog. In: Proceedings 36th
Workshop on (Constraint) Logic Lrogramming (WLP 2022). Lecture Notes in
Informatics (LNI), Gesellschaft für Informatik, Bonn (2022)
3. Flach, P., Sokol, K., Wielemaker, J.: Simply logical - the first three decades. In:
Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.)
Prolog: The Next 50 Years. LNCS (LNAI), vol. 13900, pp. 184–193. Springer, Cham
(2023). https://doi.org/10.1007/978-3-031-35254-6_15
Teaching Prolog with Active Logic Documents 183
Abstract. This paper charts the evolution of the Prolog textbook Sim-
ply Logical – Intelligent Reasoning by Example from print with runnable
programmes on a 3.5-inch diskette published in 1994, via various inter-
mediate online versions, to the fully interactive online edition available
today. Our aim is to investigate – from both the writer’s and the reader’s
perspectives – the potential impact of technology on educational mate-
rials. The three authors of this paper present three distinct and com-
plementary points of view, which come together to shape an interactive
online book that offers an immersive learning experience. Peter describes
the philosophy behind the original book and experiences teaching from
it. Jan demonstrates how contemporary web programming has enabled
a fully interactive realisation of the book’s philosophy. Kacper reports
how different technologies can be adopted to build a versatile authoring
toolkit that underpins the interactive online edition of the book. Collat-
ing these distinct yet coherent perspectives allows us to reflect on future
opportunities for interactive Prolog teaching materials arising from the
continuous development of web technologies. The insights we offer should
be of interest to teachers of Prolog as well as those wanting to find out
about the latest educational technologies.
relieved him of the temptation to write a revised edition of his own book, Logic
for Problem Solving [9].
Rather than trying to cover all of AI, which even then had grown into a
diverse field, I decided to focus on reasoning in various forms: deductive, induc-
tive, reasoning with natural language, reasoning with incomplete information,
etc., which approach is captured by the “intelligent reasoning” part of the book’s
title. The “by example” moniker reflected the didactic philosophy: teaching by
showing, learning by doing. Every AI technique discussed was accompanied by
a Prolog programme implementing it. These programmes served two didactic
purposes. By running them, the student could get a feeling what a particular
technique is all about. But perhaps more importantly, the declarative reading
of each programme was an integral part of the explanation of the technique it
implemented. For the more elaborate programmes, special attention was paid to
their stepwise development, explaining key issues along the way. My hope was
that the reader would mount the accompanying 3.5” diskette into their com-
puter, and try out the Prolog programmes alongside reading the book. While
this was perhaps optimistic back then, the online format available today inte-
grates the “showing” and “doing” sides of the educational coin very well, as will
be demonstrated below.
I still use Simply Logical in my lectures as an introduction to both logic
programming and AI. Treating the subjects side by side allows for some pretty
powerful examples, such as a resolution prover for full clausal logic as a demon-
strator for breadth-first search. Rather than modernising the text I have opted
for modernising the demonstrators and programming assignments. For exam-
ple, the natural-language question-answering programme described in Chapter 7
of the book, which nowadays we would call a chatbot, was developed into an
Amazon Alexa skill. This demonstrates the power of a hybrid system consisting
of speech-to-text and text-to-speech components enabled by deep learning, and a
logical engine in the middle capable of explainable logical reasoning. Other mate-
rial developed over the years includes a Prolog-driven agent that needs to solve
a logical puzzle while moving around in a grid world and a Reveal.js slide deck
that can be used for self-study and trying out Prolog programmes without leav-
ing the browser. The latter is based on SWISH, which Jan introduces in the
following section.
Crucial for realising the fundamental philosophy behind Simply Logical is the
ability to run code interactively in the browser. Modern browser and service
technology allows for several designs:
2. Have a web service with dedicated support for Prolog that offers a sandboxed
Prolog execution environment. SWISH1 – SWI-Prolog for SHaring [16] – is
implemented based on this paradigm.
3. Execute Prolog in the browser. Some Prolog systems are written directly in
JavaScript, thus enabling them to be run in the browser, e.g., Tau-Prolog.
Another solution is to compile Prolog to JavaScript [13]. Alternatively,
Web Assembly (WASM) offers a low-level virtual machine for the browser,
which allows running more traditional C-based Prolog implementations; for
example, Emscripten provides a Clang-based compiler suite that targets
WASM, underpinning implementations such as the Ciao Playground and
SWI-Prolog WASM port.
The first category typically provides a batch-style interaction, making it diffi-
cult to adapt for (online and interactive) educational resources; the latter two
approaches are more suitable in this respect.
SWISH, originally developed by Torbjörn Lager, is based on Pengines – an
architecture to access Prolog on a remote server – and consists of a monolithic
multi-threaded SWI-Prolog server. Its user interface is written in JavaScript with
server-side assistance for semantic highlighting, feedback on predicates, template
insertion and other user-focused features. Pengines underlies Prolog execution,
which takes place in a sandboxed environment that consists of a Prolog thread
and a temporary module. Executing a query (1) transfers the code and query
to the server; (2) starts a thread and creates a temporary module; (3) loads
the code into the module; and (4) executes the code. This operationalisation
allows for rich interaction with the client, e.g., “read” from the client, ask for
alternative solutions or trace through the programme in a manner similar to
traditional Prolog tracers. Other noteworthy features of SWISH include:
– The ability to store programmes on the server under Git-style version man-
agement. Programmes can either be private or visible to the public, and may
include each other.
– Availability of render plugins that allow rendering Prolog terms – e.g., as
tables or graphics that represent chess boards, charts and graphs – using
modern web browser technology.
– An R Project interface offering access to R’s algorithms and chart outputs.
– Notebook format allowing for rich interactive pages that provide a sequence
of HTML, Markdown, programme and query cells.
SWISH has proved to be an invaluable resource for the Prolog commu-
nity, including educators. It facilitates (Prolog) learning, code discussion and
exchange as well as shared access to data for data science. After its launch in
2014 the system quickly gained popularity as an alternative to local installations
of Prolog systems used for exercises in Prolog classes at schools worldwide. As of
December 2022, it hosts over 150,000 programmes and nearly 15,000 notebooks,
many of which in several versions. It sustains on average hundreds of concur-
rent users, peaking at over one thousand users, usually towards the end of each
1
https://swish.swi-prolog.org/.
Simply Logical – The First Three Decades 187
The book was first made freely available in 2007, when the copyright reverted
back to Peter, who decided to release its PDF and associated programmes online
through a dedicated web page. In 2015, as a pilot, most of the original book
source – written in a now obsolete version of MS Word – was ported to a col-
lection of messy HTML files replete with superfluous markup; the figures were
extracted as non-standard EMZ files and later converted to the more modern
Scalable Vector Graphics (SVG) format. This provided a canvas for transforming
a static web page into an online, interactive resource using SWI-Prolog’s SWISH
platform.
From then on, the manual process of identifying Prolog code listings and their
associated queries took place, with appropriate HTML code being embedded
by hand to display interactive SWISH code boxes. Each SWISH box initially
renders the programme as text, as in the original book. Opening the box reveals
188 P. Flach et al.
Fig. 1. Interactive SWI-Prolog code box based on SWISH. A code listing (top) can
be turned into an interactive code box (bottom) by pressing the “play” button in its
top-right corner.
complemented with an index page, with the source hosted on GitHub2 . The
second release of the book’s online version was built with Jekyll to generate static
web pages, which were hosted on GitHub Pages under the dedicated https://
book.simply-logical.space/ domain.
However, the main text was now written in HTML, which was still fairly
limiting, especially when it comes to authoring (new or derived) content. With
Markdown being much simpler to write, edit and maintain as well as more
human-readable and covering all of the markup needs, I started porting and
cleaning the source. In the process, the diversity of the book’s building blocks –
text, figures and code listings – sparked an idea for a bespoke content genera-
tion system where the resources are split into prime elements that can be reused
to compose new material. Initially, this novel workflow relied on Jekyll, which
proved to be limiting and prompted me to embark in late 2018 on developing a
bespoke content generation platform. Its prototype, while versatile, required the
source documents to be extensively annotated, making the authoring experience
overly complicated and stalling the project.
When Jupyter Book [6] arrived in early 2020 it rekindled my hope for a
modular version of Simply Logical. The platform allowed composing content
from diverse building blocks and offered nearly all of the required function-
ality, apart from support for SWISH code boxes. While based on the Sphinx
Python documentation generation engine, it nonetheless could be extended with
a custom plugin. This encouraged me to finalise the separation, conversion and
cleaning of the book content, and to begin developing the necessary Jupyter
Book extensions. The result is a highly modular design, where the book is split
into a collection of files based on their type: text, exercise, solution, code and
figure. All of these materials are published on GitHub under the permissive
Creative Commons (BY-NC-SA) licence, which allows easy reuse, incorporating
into bespoke courses, or adapting into alternative educational resources such as
practical training sessions. This release marked the third instalment of the online
edition [8].
From a technical perspective, the latest version is realised through a collection
of bespoke Jupyter Book plugins (that also work with Sphinx) spanning func-
tionality specific to SWI-Prolog [17]. These extensions were later adapted to be
compatible with the cplint [14] and ProbLog [5] probabilistic programming lan-
guages. Specifically, I developed and released sphinx-prolog3 , which among other
things allows to embed interactive SWI-Prolog and cplint code boxes based on
their respective SWISH implementations. The sphinx-problog4 extension offers
analogous functionality for ProbLog facilitated by the online execution environ-
ment underpinning the code examples published as part of the ProbLog website.
2
https://github.com/simply-logical/simply-logical/.
3
https://github.com/simply-logical/sphinx-prolog/.
4
https://github.com/simply-logical/sphinx-problog/.
190 P. Flach et al.
11 optional : - content .
12
the clauses in the same query. One big advantage of avoiding permanent state,
nonetheless, is making all queries fully reproducible.
As noted in Sect. 2, there are JavaScript-based Prolog systems that explic-
itly target the web browser, in addition to more versatile WASM implemen-
tations. The ideal scenario would be for the Prolog community to come up
with a “standard” embedding interface to enable designing interactive web pages
using multiple Prolog backends. However, the browser control flow is inherently
incompatible with traditional single-threaded Prolog systems since one cannot
wait synchronously for input in a browser (e.g., call read/1). Instead, one must
return control to the browser and when the input becomes available as an event,
only then Prolog may resume. There are two ways to achieve that. One is to run
Prolog in a WebWorker (roughly, a browser task without a window) and make
it communicate with the browser task associated to a normal browser window.
The other is to yield from the Prolog virtual machine and resume Prolog when
the data become available. This leads to at least two interface designs:
– with a WebWorker we get interaction with a Prolog process that is not very
different from Prolog running on a remote server; whereas
– using a pure JavaScript or Prolog with yield allows running Prolog on the
same page and having bi-directional calls between Prolog and JavaScript,
which facilitates managing the browser directly from Prolog.
Running Prolog in the browser rather than remotely on a server solves several
problems. With Prolog being served as a static file, storage and chat services –
which are currently part of SWISH – can be offloaded to existing cloud services.
This creates a reliable distributed infrastructure that can be sustained by several
Prolog implementations. Such diversification is important as due to the use of
many features that are unique to SWI-Prolog, SWISH is practically not portable
to other Prolog implementations.8
All of these observations lead us to conclude that an in-browser solution is
probably the future of Prolog on the web, especially for educational use. The
SWISH design is good for offering a collaborative, interactive coding environ-
ment and (programmatic) access to (large) shared data sets, but its limitations
affect the design and capabilities of tools and resources that rely on it. When
combined with the envisaged authoring suite, both SWISH and in-browser Pro-
log implementations to come (such as the Active Logic Documents described in
Chapter 14 of this volume [12]) will offer immersive and engaging Prolog teaching
as well as learning materials.
8
Note that when Pengines is realised through processes rather than threads, imple-
menting Pengines based on other Prolog systems becomes much easier.
Simply Logical – The First Three Decades 193
References
1. Allaire, J., Teague, C., Scheidegger, C., Xie, Y., Dervieux, C.: Quarto (2022).
https://doi.org/10.5281/zenodo.5960048, https://github.com/quarto-dev/quarto-
cli
2. Blackburn, P., Bos, J., Striegnitz, K.: Learn Prolog Now! http://www.let.rug.nl/
bos/lpn/index.php (2001)
3. Blackburn, P., Bos, J., Striegnitz, K.: Learn Prolog Now! (Texts in Computing,
Vol. 7), College Publications, London (2006)
4. Blackburn, P., Bos, J., Striegnitz, K.: Learn Prolog Now! https://lpn.swi-prolog.
org/ (2014)
5. De Raedt, L., Kimmig, A., Toivonen, H.: ProbLog: A probabilistic Prolog and its
application in link discovery. In: IJCAI. vol. 7, pp. 2462–2467. Hyderabad (2007)
6. Executable Books Community: Jupyter Book (2020). https://doi.org/10.5281/
zenodo.4539666, https://github.com/executablebooks/jupyter-book
7. Flach, P.: Simply Logical – Intelligent Reasoning by Example. John Wiley & Sons,
Inc. (1994)
8. Flach, P., Sokol, K.: Simply Logical – Intelligent Reasoning by Example (Fully
Interactive Online Edition). https://book.simply-logical.space/ (2022)
9. Kowalski, R.: Logic for problem solving. Edinburgh University, Department of
Computational Logic (1974)
10. Luger, G., Stubblefield, W.: Artificial Intelligence: Structure and strategies for
complex problem solving. Benjamin/Cummings (1993)
11. Luhmann, N.: Zettelkasten, https://zettelkasten.de/introduction/
12. Morales, J.F., Abreu, S., Ferreiro, D., Hermenegildo, M.V.: Teaching prolog with
active logic documents. In: Warren, D.S., et al. (eds.) Prolog: The Next 50 Years.
LNAI, vol. 13900, pp. 171–183. Springer, Switzerland (2023). https://doi.org/10.
1007/978-3-031-35254-6 14
13. Morales, J.F., Haemmerlé, R., Carro, M., Hermenegildo, M.V.: Lightweight com-
pilation of (C)LP to JavaScript. Theory Pract. Log. Program. 12(4–5), 755–773
(2012). https://doi.org/10.1017/S1471068412000336
14. Riguzzi, F.: Foundations of Probabilistic Logic Programming. River Publishers,
New York (2018)
15. Sokol, K., Flach, P.: You only write thrice: Creating documents, computational
notebooks and presentations from a single source. In: Beyond static papers:
Rethinking how we share scientific understanding in Machine Learning – ICLR
Workshop (2021). https://doi.org/10.48550/arXiv.2107.06639
16. Wielemaker, J., Lager, T., Riguzzi, F.: SWISH: SWI-Prolog for sharing. In: Pro-
ceedings of the International Workshop on User-Oriented Logic Programming
(IULP 2015); 31st International Conference on Logic Programming (ICLP 2015),
pp. 99–113 (2015). https://doi.org/10.48550/arXiv.1511.00915
17. Wielemaker, J., Schrijvers, T., Triska, M., Lager, T.: SWI-Prolog. The-
ory Pract. Logic Program. 12(1–2), 67–96 (2012). https://doi.org/10.1017/
S1471068411000494
Prolog-Based Languages and Systems
Dynamic Logic Programming
Michael Genesereth(B)
1 Introduction
In Dynamic Logic Programming, the states of the application environment are
modeled as sets of ground atomic sentences (here called datasets), and definitions
are written in the form of rules that can be applied to these instances. View
definitions define higher level view relations in terms of lower level base relations,
and operation definitions specify how the world state changes in response to
external inputs (such as the actions of agents or the passage of time).
Views are defined by writing Prolog-style rules. For example, the rule below
says that g is true of x and z if there is a y such that p is true of x and y and p
is also true of y and z. (This is the view used in the preceding figure.)
Operations are defined using transition rules. For example, the following rule
says that when the action a is performed in any state, for any y such that p
holds of x and y, then p will be false of x and y in the next state and p will be
true of y and x. (This is the operation used in the preceding figure.)
This paper provides technical detail about the syntax and semantics of
Dynamic Logic Programming. Section 2 describes the concept of datasets; Sect. 3
gives details of view rules; and Sect. 4 covers transition rules and shows how they
are used in formalizing dynamic behavior. Section 5 gives an example of the use
of Dynamic Logic Programming in defining the game of Tic Tac Toe.
2 Datasets
in a dataset representing a state are assumed to be true in that state, and all
other factoids in the Herbrand base are typically assumed to be false.
3 View Definitions
A static logic program is a set of rules that define new relations in terms of
existing relations. Such view definitions take the form of Prolog-like rules with
the constraint that the rules are safe and stratified and side-effect-free.
The vocabulary of a static logic program is a superset of the vocabulary of
any dataset to which it is applied. It includes the object, function, and relation
constants used in the dataset, but it can include additional object, function, and
relation constants as well.
Static logic programs can also include a new type of symbol, called a variable.
Variables allow us to state relationships among objects without naming specific
objects. In what follows, we write variables as strings of alphanumeric characters
beginning with a capital letter, e.g. X, Y, Z, Mass, Speed, and so forth.
Atoms are analogous to dataset factoids except that they can optionally
contain variables as well as object constants. For example, if r is a binary relation
constant, if a is an object constant, and if X and Y are variables, then r(a,X) is
an atom, as is r(a,Y) and r(X,Y) and r(X,X).
A literal is either an atom or a negation of an atom (i.e. an expression stating
that the atom is false). A simple atom is called a positive literal, The negation
of an atom is called a negative literal. In what follows, we write negative literals
using the negation sign ~. For example, if p(a,b) is an atom, then ~p(a,b)
denotes the negation of this atom.
A rule is an expression consisting of a distinguished atom, called the head,
and zero or more literals, together called the body. The literals in the body are
called subgoals. The following expression is an example of a rule. Here, r(X) is
the head, the expression p(X,Y) & q(Y) is the body; and p(X,Y) and ~q(Y) are
subgoals.
By contrast, the two rules shown below are not safe. The first rule is not safe
because the variable Z appears in the head but does not appear in any positive
subgoal. The second rule is not safe because the variable Z appears in a negative
subgoal but not in any positive subgoal.
s(X,Y,Z) :- p(X,Y)
t(X,Y) :- p(X,Y) & ~q(Y,Z)
(Note that this condition is stronger than necessary. We do not need every
rule to be safe; we just require that the program as a whole is safe. The definition
of this broader notion of safety is a little complicated and the distinction is
unnecessary here, so we skip over this subtlety in the interests of simplicity.)
We say that a set of view definitions is stratified with respect to negation if
and only if its rules can be partitioned into strata in such a way that (1) every
stratum contains at least one rule, (2) the rules defining relations that appear in
positive goals of a rule appear in the same stratum as that rule or in some lower
stratum, and (3) the rules defining relations that appear in negative subgoals of
a rule occur in some lower stratum (not the same stratum).
As an example, assume we have a unary relation p that is true of all of
the objects in some application area, and assume that q is an arbitrary binary
relation. Now, consider the ruleset shown below. The first two rules define r to
be the transitive closure of q. The third rule defines s to be the complement of
the transitive closure.
r(X,Y) :- q(X,Y)
r(X,Z) :- q(X,Y) & r(Y,Z)
s(X,Y) :- p(X) & p(Y) & ~r(X,Y)
r(X,Y) :- q(X,Y)
r(X,Z) :- q(X,Y) & r(Y,Z)
There is no way of dividing the rules of this ruleset into strata in a way that
satisfies the definition above. Hence, the ruleset is not stratified with respect to
negation.
The problem with unstratified rulesets is that there is a potential ambigu-
ity. As an example, consider the rules above and assume that our dataset also
included the facts p(a), p(b), q(a,b), and q(b,a). From these facts, we can
conclude r(a,b) and r(b,a) are both true. So far, so good. But what can we say
about s? If we take s(a,b) to be true and s(b,a) to be false, then the second
rule is satisfied. If we take s(a,b) to be false and s(b,a) to be true, then the
second rule is again satisfied. The upshot is that there is ambiguity about s. By
concentrating exclusively on logic programs that are stratified with respect to
negation, we avoid such ambiguities.
View definitions in static logic programs are required to be both safe and
stratified with respect to negation. This is a departure from view definitions in
Logic Programming languages like Prolog, which permit rules that are unsafe
and logic programs that are not stratified.
The semantics of view definitions in static logic programs can be formalized
by defining the result of applying a static logic program to a dataset. The result-
ing extension is the set of all facts that can be”deduced” from the dataset on
the basis of the rules in the static logic program.
An instance of an expression (atom, literal, or rule) is one in which all vari-
ables have been consistently replaced by terms from the Herbrand universe.
For example, if we have a language with object constants a and b, then r(a)
:- p(a,a), r(a) :- p(a,b), r(b) :- p(b,a), and r(b) :- p(b,b) are all
instances of r(X) :- p(X,Y). (To be clear here, we use the word instance here
to refer exclusively to ground expressions.)
Given this notion, we can define the result of a single application of a single
rule to a dataset. Given a rule r and a dataset Δ, we define v(r, Δ) to be the
set of all ψ such that (1) ψ is the head of an arbitrary instance of r, (2) every
positive subgoal in the instance is a member of Δ, and (3) no negative subgoal
in the instance is a member of Δ.
202 M. Genesereth
Using this notion, we define the result of repeatedly applying the rules in a
single stratum Σ to a dataset Δ of factoids in the vocabulary of the stratum
below. Consider a sequence of datasets defined recursively as follows. Γ0 = Δ,
and Γn+1 = ∪v(r, Γn ) for all r in Σ. Finally, we define the closure of Σ on Δ to
be the union of the datasets in this sequence, i.e. C(Σ, Δ) = ∪Γi .
Finally, we define the extension of a static logic program Ω on dataset Δ as
follows. Our definition relies on a decomposition of Ω into strata Σ1 , ... , Σn . Let
Δ0 = Δ, and let Δn+1 = Δn ∪ C(Σn+1 , Δn ). Since there are only finitely many
rules in a static logic program and every stratum must contain at least one rule,
there are only finitely many sets to consider (though the sets themselves might
be infinite).
It can be shown that there is only one extension for any static logic program
applied to any dataset. Although it is sometimes possible to stratify the rules in
more than one way, this does not cause any problems. So long as a program is
stratified with respect to negation, the definition just given produces the same
extension no matter which stratification one uses.
Note that the extension of any function-free static logic program on a finite
dataset must be finite. Also, the extension of any non-recursive static logic pro-
gram applied to a finite dataset must be finite. In both cases, the extension can
be computed in time that is polynomial in the size of the dataset.
In the case of recursive programs without function constants, the result must
be finite. However, the cost of computing the extension may be exponential in
the size of the data, but the result can be computed in finite time.
For recursive programs with function constants, it is possible that the exten-
sion is infinite. In such cases, the extension is still well-defined; but in practice it
may be necessary to use a different algorithm to compute whether or not a given
atom is in the extension. There are multiple ways this can be done. See Ull-
man’s book on Database Systems and Knowledge Base Systems for a discussion
of some usable approaches.
4 Operation Definitions
The syntax of operation definitions is analogous to the syntax for view defini-
tions. The various types of constants are the same, and the notions of term and
atom and literal are also the same. However, to these, we add a few new items.
To denote operations, we designate some constants as operation constants.
As with function constants and relation constants, each operation constant has
a fixed arity - unary, binary, and so forth.
An action is an application of an operation to specific objects. In what fol-
lows, we denote actions using a syntax similar to that of atomic sentences, viz.
an n-ary operation constant followed by n terms enclosed in parentheses and
separated by commas. For example, if f is a binary operation constant and a
and b object constants, then f(a,b) denotes the action of applying the operation
f to a and b.
Dynamic Logic Programming 203
click(a) :: p(a,b) & ~q(a) ==> ~p(a,b) & q(a) & click(b)
click(X) :: p(X,Y) & ~q(X) ==> ~p(X,Y) & q(X) & click(Y)
As with view rules, safety is a consideration. Safety in this case means that
every variable among the effects of a rule or in negative conditions also appears
in the head of the rule or in the positive conditions.
The operation rules shown above are both safe. However, the rules shown
below are not. The second effect of the first rule contains a variable that does
not appear in the head or in any positive condition. In the second rule, there is
a variable that appears in a negative condition that does not appear in the head
or in any positive condition.
click(X) :: p(X,Y) & ~q(X) ==> ~p(X,Y) & q(Z) & click(Y)
click(X) :: p(X,Y) & ~q(Z) ==> ~p(X,Y) & q(X) & click(Y)
In some operation rules there is no condition, i.e. the effects of the transition
rule take place on all datasets. We can, of course, write such rules by using the
condition true, as in the following example.
edge(a,b)
edge(b,d)
edge(b,e)
The following operation definition defines a binary operation copy that copies
the outgoing arcs in the graph from its first argument to its second argument.
Given this operation definition and the dataset shown above, executing
copy(b,c) adds edge(c,d) and edge(c,e), resulting in the following dataset.
edge(a,b)
edge(b,d)
edge(b,e)
edge(c,d)
edge(c,e)
The following rule defines a unary operation invert that reverses the outgoing
arcs of the node specified as it argument.
Executing invert(c) removes the previous outgoing arcs from c and turns
them into incoming arcs, as shown below.
edge(a,b)
edge(b,d)
edge(b,e)
edge(d,c)
edge(e,c)
Finally, the following operation rules define a binary operation that inserts a
new node into the graph (the first argument) with an arc to the second argument
and arcs to all of the nodes that are reachable from the second argument.
insert(X,Y) :: edge(X,Y)
insert(X,Y) :: edge(Y,Z) ==> insert(X,Z)
Now consider the action insert(w,b). The first rule adds edge(w,b) to
the expansion. The second rule adds insert(w,d) and insert(w,e). On the
next round of expansion, the first rule adds edge(w,d) and edge(w,e), and the
second rules adds insert(w,c). On the third round, we get edge(w,c). At this
point, neither rule adds any items to our expansion. Applying the changes to
our dataset, we get the dataset shown below.
edge(a,b)
edge(b,d)
edge(b,e)
edge(d,c)
edge(e,c)
edge(w,b)
edge(w,d)
edge(w,e)
edge(w,c)
Note that it is possible to define insert in other ways. We could, for example,
define a view of edge that relates each node to every node that can be reached
206 M. Genesereth
from the node; and we could then use this view in a non-recursive definition of
insert. However, this would require us to introduce a new view into our vocab-
ulary; and, for many people, this is less clear than the definition shown above.
XO
XO
In our definition of Tic Tac Toe, states are characterized by the contents of
the cells on the Tic Tac Toe board and control (whose turn it is to play). (It
is true that control can be defined in terms of the contents of cells; but making
control explicit costs little and simplifies the description.) In what follows, we
use the ternary relation constant cell together with a row m and a column n
and a mark w to designate the fact that the cell in row m and column n contains
w where w is either an x or an o or a b (for blank). We use the unary relation
constant control to state that it is that role’s turn to mark a cell. The dataset
shown below uses this vocabulary to characterize the game state show above.
cell(1,1,x)
cell(1,2,o)
cell(1,3,b)
cell(2,1,b)
cell(2,2,x)
cell(2,3,o)
cell(3,1,b)
cell(3,2,b)
cell(3,3,b)
control(x)
Our first step is to define legality of moves. A player may mark a cell if that
cell is blank. Otherwise, it has no legal actions.
legal(M,N) :- cell(M,N,b)
Dynamic Logic Programming 207
Next, we define the physics of the world - how it changes in response to the
performance of legal actions. If a player has control and marks a cell, the cell is
then marked. Also, control switches to the other player.
line(Z) :- row(M,Z)
line(Z) :- column(M,Z)
line(Z) :- diagonal(Z)
A game is over whenever either player has a line of marks of the appropriate
type or if there are no cells containing blanks. We define the 0-ary relation open
here to mean that there is at least one cell containing a blank.
terminal :- line(x)
terminal :- line(o)
terminal :- ~open
open :- cell(M,N,b)
Our rules specify the states and physics of the game. They do not specify how
to play the game effectively. In order to decide this, a player needs to consider
the effects of his legal moves in order to decide a course of action that will lead
to a line of his marks while considering the possible moves of the other player.
Prolog’s assert and retract provide one way to model dynamics. The key
is a conceptualization of dynamics as destructive change of state - states are
modeled as sets of stored facts, and changes to state are modeled as applications
of assert and retract to these sets of facts. Unfortunately, the semantics of logic
programs involving assert and retract is unsatisfying because of the way the
execution of these actions gets mixed up with query evaluation in the standard
Prolog interpreter. Dynamic logic programming cleans things up by separating
the formalization of dynamics from the definition of relations using Prolog rules.
Production systems are another way of expressing dynamics. The transition
rules used to define operations in DLP are similar, but there are some important
differences. In most production systems, only one rule is applied at a time.
(Many rules may be “triggered”, but typically only one is “fired”.) In dynamic
logic programs, all transition rules are executed simultaneously, and all updates
(both deletions and additions) are applied to the dataset before the rules fire
again. This simplifies the specification of dynamics in many cases, and avoids
many problems endemic to sequential update systems, such as unintended race
conditions and deadlocks.
Finally, Carro and Hermenegildo [3] introduce a notion of “concurrent data
facts” to (Ciao) Prolog [4] which are used primarily for communication among
concurrent threads, but also to represent (possibly timestamped) changes in (or
to) the external world. This, by itself, is still ’mixed up with query evaluation’.
But, by using, e.g., time stamps as an argument in the facts, the reasoning
system can be kept monotonic and sound. More relevantly perhaps, the paper
also mentions that these concurrent facts facilitate the implementation of reac-
tive condition-action rules, i.e. the part that reacts and exerts the changes on the
facts can be encoded within condition-action rules, and these can be implemented
within the Prolog system with these building blocks. The reactivity comes from
the synchronization provided through the concurrent facts, i.e. execution can
wait for concrete facts to be present.
7 Conclusion
In practice, it is common to extend the simple version of Dynamic Logic Pro-
gramming described here to include “built-in” relations (e.g. arithmetic) and
other operators (e.g. aggregates). The syntax and semantics of such extensions
are a little messy. Luckily, they pose no significant theoretical challenges; and,
in the interest of brevity, they are not covered here.
The intent of this article is to provide a concise but reasonably rigorous
account of the syntax and semantics of Dynamic Logic Programming. For moti-
vation and examples of all of these concepts, see the textbook Dynamic Logic
Programming.
Dynamic Logic Programming 209
References
1. Clocksin, W.F., Mellish, C.S.: Programming in Prolog, 4th edn. Springer-Verlag,
New York (1994)
2. Bonner, A.J., Kifer, M.: Transaction logic programming, international conference
on logic programming (ICLP) (1993)
3. Carro, M., Hermenegildo, M.: Concurrency in prolog using threads and a shared
database (1999). International Conference on Logic Programming, pp. 320–334,
MIT Press, Cambridge, MA, USA, November (1999)
4. Cabeza, D., Hermenegildo, M.: Distributed www programming using (Ciao) Prolog
and the pillow library. Theory Pract. Logic Program. 1(3), 251–282 (2001)
5. Costantini, S., Tocchio, A.: The DALI logic programming agent-oriented language.
In: Alferes, J.J., Leite, J. (eds.) JELIA 2004. LNCS (LNAI), vol. 3229, pp. 685–688.
Springer, Heidelberg (2004). https://doi.org/10.1007/978-3-540-30227-8 57
6. Flesca, S., Greco, S.: Declarative semantics for active rules. Theory Pract. Logic
Program. 1(1), 43–69 (2001)
7. Fodor, P.: Practical reasoning with transaction logic programming for knowledge
base dynamics, PhD Thesis, Stonybrook University (2011)
8. Fruehwirth, T.: Constraint Handling Rules. Cambridge University Press. ISBN
9780521877763 (2009)
9. Genesereth, M., Love, N., Pell, B.: The international game playing competition.
AAAI Magazine (2005)
10. Genesereth, M.: Epilog. http://epilog.stanford.edu
11. Genesereth, M., Chaudhri, V.: Logic Programming. Synthesis Lectures on Artifi-
cial Intelligence and Machine Learning, February (2020). https://doi.org/10.2200/
S00966ED1V01Y201911AIM044
12. Hayes, P.: Computation and deduction. In: Proceedings Second Symposium on
Mathematical Foundations of Computer Science, Czechoslovakian Academy of Sci-
ences, Czechoslovakia, pp. 105–118 (1973)
13. Kifer, M., Liu, A.: Declarative logic programming, ACM Books (2018)
14. Kowalski, R.: Algorithm = Logic + Control. In: Communications of the ACM, vol.
22, No. 7, July (1979)
15. Kowalski, R., Sadri, F.: LPS-A logic-based production system framework (2009)
16. Kowalski, R., Sadri, F.: Integrating logic programming and production systems in
abductive logic programming agents (2009)
17. Slota, M., Leite, J.A.: EVOLP: an implementation. In: Computational Logic in
Multi-Agent Systems, 8th International Workshop, CLIMA VIII, Porto, Portugal,
September 10–11 (2007)
18. Warren, D.S.: Programming in tabled prolog. https://citeseerx.ist.psu.edu/
viewdoc/download?doi=10.1.1.49.4635
19. Zhou, N.-F.: The language features and architecture of B-Prolog. Theory Pract.
Logic Program. 12(1–2) (2011). https://doi.org/10.1017/S1471068411000445
Combining Logic Programming and Imperative
Programming in LPS
1 Introduction
On the one hand, it can be argued that logic programming (LP) is a Turing-complete
model of computation, which is well-suited for all computing tasks. It can also be argued
that the procedural interpretation of LP gives LP the computational capabilities of an
imperative computer language. On the other hand, despite such arguments, conventional
imperative languages dominate computing today.
In this paper, we take the position that, to have wider applicability, LP needs to be
extended with the ability of imperative languages, to generate actions to satisfy an agent’s
goals. Without this ability, LP can represent only an agent’s beliefs. The beliefs can be
queried, to determine whether they hold at a given point in time. But without extension,
LP cannot represent persistent goals that need to be satisfied over the course of time. For
truly general-purpose computing, LP needs to be extended to include persistence goals
and a treatment of time that is compatible with destructive change of state.
To support this position, we present the language LPS (Logic Production Systems)
[15–22], which combines the use of LP, to represent an agent’s beliefs, with the use
of reactive rules and constraints, formalised in first-order logic, to represent the agent’s
instead of deriving the logical consequence that you eat and sleep at the same time
(assuming that to be impossible), production systems use conflict resolution to choose
between eating or sleeping. One of the aims of LPS is to give such behaviour a logical
interpretation by associating times with actions, and by allowing, in this case, eating and
sleeping to occur at different times.
Integrity Constraints. In addition to giving CA rules a logical interpretation, LPS also
gives them a logical status as goals, distinct from the logical status of LP rules as beliefs.
Our understanding of this distinction between the logic of LP rules and the logic of CA
rules was influenced by Gallaire and Nicolas’ [28] work on deductive databases in the
late 1970s. They distinguished between two kinds of general laws in deductive databases:
general laws that are used (like logic programs) to derive implicit (or intensional) data
from explicit (or extensional) data, and general laws that are used as integrity constraints
to restrict and maintain database updates.
For example, the assumption that it is not possible to eat and sleep at the same time
could be represented by the integrity constraint:
212 R. Kowalski et al.
where you are eating and you are sleeping are “facts”, which are added to the database
when the actions of eating and sleeping are initiated respectively.
This distinction between two kinds of general laws in databases inspired our work
[32] on integrity checking for deductive databases, combining backward reasoning
using LP rules with forward reasoning using integrity constraints, triggered by database
updates. This combination of forward and backward reasoning is reflected in the
operational semantics of LPS today.
External Events and Actions. However, integrity checking in traditional database
systems only prevents database updates from violating integrity. It does not actively
change the database, to ensure that integrity is maintained. Active databases [37] remedy
this omission by using event-condition-action rules (ECA rules), to perform database-
changing actions triggered by events, when the corresponding conditions hold. But,
although it is natural to write such rules in the seemingly logical form if event and condi-
tion then action, ECA rules, like CA rules, do not have a logical interpretation as logical
implications.
LPS gives CA and ECA rules a logical interpretation, not only by associating times
with events, conditions and actions, but also by generating actions to make goals true.
In this respect, LPS can be viewed as a special case of abductive logic programming
(ALP) [12], which combines logic programs and integrity constraints with candidate
assumptions, which can be used to satisfy the integrity constraints. Whereas in the
philosophy of science abduction is used to generate assumptions to explain external
observations, abduction in LPS generates actions to make goals true.
Change of State. The final step in the logical development of LPS was to decide how
to represent and reason about change of state. It is common in AI to represent such
knowledge by means of frame axioms, such as those in the situation calculus [25] and
event calculus [23], reasoning, for example, that:
if a fact is true in a given state,
then it continues to be true in a later state,
unless it is terminated by an event (either an external event or action)
that occurs between the two states.
But reasoning with frame axioms is not practical for large scale computer applications.
To develop LPS as a practical system, we needed to replace the use of frame axioms by
destructive change of state. But we were committed to do so within a logical framework.
Models Instead of Theories. This last problem, of justifying destructive change of state
within a logical framework, was solved by abandoning the theoremhood view of LP and
replacing it with a model-theoretic view. The theoremhood view regards logic programs
as axioms, and regards computation as proving that an answer to a query is a theorem.
The model theoretic view regards logic programs as defining a unique, intended model,
and regards computation as showing that the model satisfies the query, viewed as a goal.
To employ destructive change of state within a theorem-proving approach, it would
be necessary to destructively change the axioms in the middle of a proof. But this would
Combining Logic Programming and Imperative Programming in LPS 213
also destroy the justification for arguing that the theorem is a logical consequence of
the axioms, because the axioms would not be well-defined. This problem does not arise
with the model-theoretic view, because there is no such restriction on the way in which
a model is defined.
We were influenced and encouraged in this model-generation view of computation
by its use in such LP languages as XSB Prolog [30], Transaction Logic [4] and Answer
Set Programming [24], as well as by the treatment of computation as model-generation
in the modal temporal language MetaTem [2].
The first clause defines the initial state at time 1, in which the fluent lightOn is true. The
clause is shorthand for the sentence holds(lightOn, 1), written in the syntax of the event
calculus [23].
The second and third clauses define observations of the external event switch, which
occurs instantaneously both in the transition between the state at time 1 and the next
214 R. Kowalski et al.
state at time 2, and between the state at time 3 and the next state at time 4. The clauses
are shorthand for happens(switch, 1, 2) and happens(switch, 3, 4) in a syntax similar to
that of the event calculus.
The fourth clause defines the intensional fluent lightOff in terms of the extensional
fluent lightOn. The clause is shorthand for holds(lightOff, T) if not holds(lightOn, T).
The fifth and sixth clauses are causal laws, which specify, in effect, that a switch
event turns the light on if the light is off and turns the light off if the light is on. The two
clauses are shorthand for:
initiates(switch, lightOn, T + 1) if holds(lightOff , T ).
terminates(switch, lightOn, T + 1) if holds(lightOn, T ).
Given these clauses, the general command go(Timeline) in the SWISH implemen-
tation displays the history of states and events generated by computation in LPS. Notice
that “times” are actually periods of time during which no change of state takes place.
Events, on the other hand, are instantaneous and take place between time periods.
Fig. 1. An initial history of states and events, displayed as a Gantt chart. https://demo.logicalco
ntracts.com/p/basic-switch.pl
The SWISH implementation of LPS includes other general predicates that dis-
play other views of the computation. For example, the command state_diagram(Graph)
generates the more abstract display in Fig. 2.
Combining Logic Programming and Imperative Programming in LPS 215
Logically, the history computed by LPS determines a model that satisfies the pro-
gram, by making all the sentences in the program true. It makes extensional fluents true
or false, by using causal laws, to initiate and terminate extensional fluents. It makes
intensional fluents true or false (as ramifications of changes to extensional fluents), by
using intensional fluent definitions.
In Fig. 1, the occurrence of the switch event between times 1 and 2 terminates the truth
of the extensional fluent lightOn, so that it is no longer true at time 2. As a consequence,
according to both negation as failure (NAF) and the classical meaning of negation, not
lightOn becomes true at time 2, and consequently lightOff also becomes true at time 2.
The sentences not lightOn and lightOff remain true at time 3, simply because they are
not made false by the occurrence of any terminating events. Similarly, the fluent lightOn
that becomes true at time 4 remains true indefinitely, unless and until some terminating
switch event occurs.
In general, computation in LPS satisfies an event-calculus-like causal theory:
Here the second sentence is a frame axiom, which asserts that a fluent that holds at a
time T continues to hold at the next time T + 1, unless an event that terminates the fluent
occurs between T and T + 1.
It is important to note that LPS does not reason explicitly with such frame axioms.
Forward reasoning with the frame axiom would entail the computational cost of reason-
ing that, for every fluent that holds at a time T and that is not terminated by an event that
occurs between T and T + 1, the fluent continues to hold at time T + 1. Backward reason-
ing is only marginally better. Backward reasoning, to determine whether a fluent holds at
a given time, entails the cost of chaining backwards in time until the time the fluent was
initiated, checking along the way that the fluent was not terminated in between times.
Both kinds of reasoning are intolerably inefficient compared with destructive change of
state.
216 R. Kowalski et al.
Instead, in LPS, when an event occurs, then any fluent initiated by the event is added
to the current state, and any fluent terminated by the event is (destructively) deleted from
the current state. However, although the causal theory is not used to reason explicitly
whether any fluents hold, the causal theory and its frame axiom are emergent properties
that are true in the model generated by the computation. This is like the way in which
the associativity of the append relation is used neither to generate a model of append,
nor to compute instances of append, but it is an emergent property, which is true in the
model generated by the recursive definition of append.
Notice that the logical interpretation of destructive change of state, as generating
extensional fluents in a timestamped model-theoretic structure, provides a logically pure
alternative to the logically impure use of assert and retract in Prolog, which is one of the
ways many Prolog programmers avoid the inefficiencies of the frame axiom in practice.
Unlike models in modal logic, which are collections of possible worlds connected
by accessibility relations, models in LPS are single models in which fluents are stamped
with the times at which they hold, and events are stamped with the times between which
they happen. In this example, the Herbrand model, which consists of all the facts that
are true in the model, is:
represents the goal of switching the light whenever the light is off.
An LPS agent uses its beliefs to determine when the antecedent of a rule becomes
true, and then it performs actions to make the consequent of the rule true. If time is
unbounded, then the model determined by the resulting history of states and events can
be infinite, and the computational process might never end.
The timeline in Fig. 3 displays an initial portion of the infinite model generated when
the reactive rule above is added to the previous example.
Here, instead of the intentional fluent lightOff persisting, as before, from state 2 to
state 3, the reactive rule recognises that lightOff is true at time 2 and generates the goal of
performing a switch action in the future. The switch action can be performed at any time
Combining Logic Programming and Imperative Programming in LPS 217
Fig. 3 https://demo.logicalcontracts.com/p/simple%20switch.pl
after time 2. However, in practice, LPS generates models in which goals are satisfied as
soon as possible. So, in this case, it performs the action immediately, between times 2
to 3.
Whereas, without the reactive rule, the second switch external event turned the light
on, now the same external event turns the light off. So, again, the reactive rule is triggered
and turns the light back on, as soon as possible.
Of course, leaving it to the LPS implementation to make reactive rules true as soon
as possible is a risky business. However, it is possible to specify the time at which the
consequent of the rule is made true explicitly, in this example by using any one of the
following equivalent notations:
if lightOff at T1 then switch from T1 to T2.
if lightOff at T then switch from T to T+1.
if lightOff at T then switch from T.
At the time of writing, we do not have a syntax for representing this temporal relationship
without writing time explicitly.
In general, both the antecedent and consequent of a reactive rule can be a conjunction
of (possibly negated) timeless predicates, such as the inequality relation ≤ and (possibly
negated) fluents and events. All variables, including time variables, in the antecedent
are universally quantified with scope the entire rule. All other variables are existentially
quantified with scope the consequent of the rule. All times in the consequent are later
than or at the same time as the latest time in the antecedent.
Goals in LPS can also include constraints of the form false conditions, which restrict
the actions that an agent can perform. In the current implementation, they also restrict
the external events that the agent will accept. For example, adding the constraint false
lightOn, switch, which is shorthand for:
not(holds(lightOn, T) and happens(switch, T, T + 1)).
The antecedents and consequents of reactive rules can also include complex events
defined by LP clauses of the form complex-event if conditions, where the conditions
have the same syntax as the antecedents and consequents of reactive rules. The start
time of the complex-event is the earliest time in the conditions of the clause, and the end
time of the complex-event is the latest time in the conditions.
For example, the following two LP clauses define a complex event, sos, which is a
simplified distress signal of a light flashing three times in succession. Each flash of light
takes place over two time steps and is separated from the next flash by one time step. At
the time of writing, we do not have a shorthand syntax without time for such clauses:
LPS can use the definition of the complex sos event both to recognise and to generate
distress signals. Moreover, it can both recognise and generate them at the same time using
a reactive rule such as:
Figure 5 displays a scenario in which LPS recognises an initial sos signal and
acknowledges it by generating an sos in response. But then it recognises its own response
as another sos signal, and responds to it as well, ad infinitum.
Combining Logic Programming and Imperative Programming in LPS 219
Fig. 5. Here the command go(Timeline, [composites]) displays the timeline together with
complex (composite) events. https://demo.logicalcontracts.com/p/new%20sos.pl
Fig. 6. https://demo.logicalcontracts.com/example/badlight.pl.
The animation is generated using purely declarative Prolog clauses that define the
rendering of fluents as two-dimensional objects, as shown in Fig. 7.
Fig. 7. The Prolog code for visualizing the locations of bob and dad.
220 R. Kowalski et al.
6 Related Work
In the Introduction, we focused on the historical development of LPS. But as we devel-
oped LPS, along the way we discovered many related, parallel developments, leading
in a similar direction. For example, David Harel, in his famous paper on Statecharts
[11], argues that there are two kinds of systems: transformational systems and reactive
systems. Transformational systems specify a transformation, function, or input/output
relation, as in LP and functional programming. Reactive systems, “which present the
more difficult cases”, describe dynamic behaviour, which “takes the general form ‘when
event Y occurs in state A, if condition C is true at the time, the system transfers to
state B”. This behaviour is a special case of the way reactive rules are executed in LPS.
Although Harel draws attention to these two kinds of systems, he does not consider how
they might be related and be combined.
Several other authors have also identified similar distinctions between different kinds
of systems or rules, and they have developed more comprehensive systems or logics to
combine them. For example, SBVR (Semantics of Business Vocabulary and Rules) [29]
combines alethic modal operators, representing structural business rules, with deontic
modal operators, representing operative business rules. Input-output logic [3] combines
constitutive norms, representing an agent’s beliefs, with regulative norms, representing
an agent’s goals. FO(ID) [8] combines first-order logic with definitions, similar to the
way in which ALP combines integrity constraints with logic programs. Formally, an
FO(ID) theory is a set of FO axioms and definitions. A model of such a theory is a
(2-valued) structure satisfying all FO axioms and being a well-founded model of all
definitions.
Other authors have also recognized the need to extend LP in similar ways. CHR [9]
extends LP with propagation rules, which behave like production rules. The original
semantics of CHR was given in terms of linear logic, which justifies destructive updates.
EVOLP [1] extends the syntax of LP rules, so that their conclusions update the rules
of the extended logic program. The semantics is given by the resulting sequence of
logic programs. Like LPS, DALI [6] extends LP by means of reaction rules. However, in
DALI, reaction rules are transformed into ordinary LP rules, and the semantics of a DALI
program is given by a sequence of logic programs, which is similar to the semantics of
EVOLP. Epilog [10] extends LP with operation rules of the form action:: conditions ⇒
effects, which means that if the conditions of the rule are true in a state, then the action is
performed by executing the effects of the rule to generate the next state. The semantics of
Epilog is given by the sequence of state transitions generated by executing all applicable
operation rules in parallel. Ciao includes a facility to timestamp data predicates, which
can be used in combination with concurrency, to implement condition-action rules [5].
The majority of the above systems and languages specify only one state transition
at a time. In contrast, Transaction Logic (TL) [4] extends LP with clauses that define
transactions (or complex events), which use destructive updates to generate sequences
of state transitions. Unlike models in LPS, which include all states in a single model by
associating explicit state (or time) parameters with events and fluents, models in TL are
like possible worlds in the semantics of modal logic, where each state is represented by
a separate Herbrand interpretation. However, unlike modal logics, where truth is defined
relative to a single possible world, truth (of a transaction fact) in TL is defined relative to
Combining Logic Programming and Imperative Programming in LPS 221
a path from one state to another. The TL semantics has been used to give an alternative
semantics for CHR [26]. TL was also one of the inspirations for complex events in LPS.
In addition to related work in computer science, logic and AI, we have been encour-
aged by related work in cognitive psychology, initiated by Stenning and van Lambalgen
[33]. They consider a variety of psychological tasks which seem to show that people do
not reason logically with rules expressed in natural language, and they argue that the
data can be explained by assuming that there are two kinds of rules, and that people
have trouble deciding between them. In the case of the Wason selection task [36], the
most widely cited psychological study of human reasoning, they claim that “by far the
most important determinant of ease of reasoning is whether interpretation of the rule
assigns it descriptive or deontic logical form”. In [13], this distinction between different
interpretations of a rule is reinterpreted as a distinction between LP rules representing
beliefs and first-order logic rules representing goals.
7 Future Prospects
References
1. Alferes, J.J., Brogi, A., Leite, J.A., Pereira, L.M.: Evolving logic programs. In: Flesca, S.,
Greco, S., Ianni, G., Leone, N. (eds.) JELIA 2002. LNCS (LNAI), vol. 2424, pp. 50–62.
Springer, Heidelberg (2002). https://doi.org/10.1007/3-540-45757-7_5
2. Barringer, H., Fisher, M., Gabbay, D., Owens, R., Reynolds, M.: The imperative future:
principles of executable temporal logic. John Wiley & Sons, Inc. (1996)
3. Boella, G., der Torre, L.V.: Regulative and constitutive norms in the design of normative mul-
tiagent systems. In International Workshop on Computational Logic in Multi-Agent Systems,
pp. 303–319, Springer (2005)
4. Bonner, A., Kifer, M.: Transaction logic programming. In: Warren D.S. (ed.) Logic
Programming: Proc. of the 10th International Conf., pp. 257–279 (1993)
5. Carro, M., Hermenegildo, M.: Concurrency in Prolog Using Threads and a Shared Database.
1999 International Conference on Logic Programming, pp. 320–334, MIT Press, Cambridge
(1999)
1 https://demo.logicalcontracts.com/example/fintechExamples.swinb.
222 R. Kowalski et al.
6. Flesca, S., Greco, S., Ianni, G., Leone, N. (eds.): JELIA 2002. LNCS (LNAI), vol. 2424.
Springer, Heidelberg (2002). https://doi.org/10.1007/3-540-45757-7
7. Davila, J.: Rock-Paper-Scissors (2017). https://demo.logicalcontracts.com/p/rps-gets.pl
8. Denecker, M., Vennekens, J.: Building a knowledge base system for an integration of logic
programming and classical logic. In: Garcia de la Banda, M., Pontelli, E. (eds.) ICLP 2008.
LNCS, vol. 5366, pp. 71–76. Springer, Heidelberg (2008). https://doi.org/10.1007/978-3-540-
89982-2_12
9. Schrijvers, T., Frühwirth, T. (eds.): Constraint Handling Rules. LNCS (LNAI), vol. 5388.
Springer, Heidelberg (2008). https://doi.org/10.1007/978-3-540-92243-8
10. Genesereth, M.: Dynamic Logic Programming. In: Warren, D., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R. and Rossi, F. (eds.) Prolog - The Next 50 Years. LNCS,
vol. 13900. Springer (2023)
11. Harel, D.: Statecharts: A Visual Formalism for Complex Systems. Sci. Comput. Programming
8, 231–274 (1987)
12. Kakas, A., Kowalski, R., Toni, F. : The Role of Logic Programming in Abduction. In: Gabbay,
D., Hogger, C.J., Robinson, J.A. (eds.) Handbook of Logic in Artificial Intelligence and
Programming 5, pp. 235–324. Oxford University Press (1998)
13. Kowalski, R.: Computational logic and human thinking: how to be artificially intelligent.
Cambridge University Press (2011)
14. Kowalski, R., Dávila, J., Sartor, G., Calejo, M.: Logical english for law and education. In:
Warren, D., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The
Next 50 Years. LNCS, vol. 13900. Springer (2023)
15. Kowalski, R., Sadri, F.: Logic Programming towards multi-agent systems. Ann. Math. Artif.
Intell. 25, 391–419 (1999)
16. Kowalski, R., Sadri, F.: Integrating logic programming and production systems in abductive
logic programming agents. In: Polleres, A., Swift, T. (eds.) RR 2009. LNCS, vol. 5837,
pp. 1–23. Springer, Heidelberg (2009). https://doi.org/10.1007/978-3-642-05082-4_1
17. Kowalski, R., Sadri, F.: An agent language with destructive assignment and model-theoretic
semantics. In: Dix, J., Leite, J., Governatori, G., Jamroga, W. (eds.) CLIMA 2010. LNCS
(LNAI), vol. 6245, pp. 200–218. Springer, Heidelberg (2010). https://doi.org/10.1007/978-
3-642-14977-1_16
18. Kowalski, R., Sadri, F.: Abductive logic programming agents with destructive databases. Ann.
Math. Artif. Intell. 62(1), 129–158 (2011)
19. Kowalski, R., Sadri, F.: A Logic-based framework for reactive systems. In: Bikakis, A., Giurca,
A. (eds.) RuleML 2012. LNCS, vol. 7438, pp. 1–15. Springer, Heidelberg (2012). https://doi.
org/10.1007/978-3-642-32689-9_1
20. Kowalski, R., Sadri, F.: A logical characterization of a reactive system language. In: Bikakis,
A., Fodor, P., Roman, D. (eds.) RuleML 2014. LNCS, vol. 8620, pp. 22–36. Springer, Cham
(2014). https://doi.org/10.1007/978-3-319-09870-8_2
21. Kowalski, R., Sadri, F.: Model-theoretic and operational semantics for Reactive Computing.
N. Gener. Comput. 33(1), 33–67 (2015)
22. Kowalski, R., Sadri, F.: Programming in logic without logic programming. Theory Pract.
Logic Program. 16(3), 269–295 (2016)
23. Kowalski, R., Sergot, M.: A Logic-based Calculus of Events. In: New Generation Computing,
Vol. 4, No.1, 67--95 (1986). Also in: Inderjeet Mani, J. Pustejovsky, and R. Gaizauskas (eds.)
The Language of Time: A Reader, Oxford University Press (2005)
24. Lifschitz, V. Answer set programming. Springer (2019)
25. McCarthy, J., Hayes, P.J.: Some philosophical problems from the standpoint of artificial
intelligence. In: Readings in artificial intelligence, pp. 431–450. Morgan Kaufmann (1981)
Combining Logic Programming and Imperative Programming in LPS 223
26. Meister, M., Djelloul, K., Robin, J.: Unified semantics for Constraint Handling Rules in
transaction logic. In International Conference on Logic Programming and Nonmonotonic
Reasoning, pp. 201–213. Springer (2007)
27. Newell, A., Simon, H.A.: Human problem solving vol. 104, No. 9. Prentice-Hall, Englewood
Cliffs, NJ (1972)
28. Nicolas, J.M., Gallaire, H.: Database: Theory vs. Interpretation. In: Gallaire, H., Minker, J.
(eds.) Logic and Databases, Plenum, New York (1978)
29. OMG. (Object Management Group): Semantics of Business Vocabulary and Rules (SBVR),
OMG Standard, v. 1.0. (2008)
30. Rao, P., Sagonas, K., Swift, T., Warren, D.S., Freire, J.: XSB: a system for efficiently com-
puting well-founded semantics. In: Dix, J., Furbach, U., Nerode, A. (eds.) LPNMR 1997.
LNCS, vol. 1265, pp. 430–440. Springer, Heidelberg (1997). https://doi.org/10.1007/3-540-
63255-7_33
31. Russell, S.J., Norvig, P.: Artificial intelligence: a modern approach, 2nd edn. Prentice Hall,
Upper Saddle River, NJ (2003)
32. Sadri F., Kowalski R.: A Theorem-Proving Approach to Database Integrity. In: Minker, J. (ed.)
Foundations of Deductive Databases and Logic Programming, Morgan Kaufmann, pp. 313–
362 (1988)
33. Stenning, K., van Lambalgen M.: Human Reasoning and Cognitive Science. MIT Press (2012)
34. Thagard, P.: Mind: Introduction to Cognitive Science. Second Edition. MIT Press (2005)
35. Warren, D. S., Denecker, M.: A better semantics for prolog. In: Warren, D., Dahl, V., Eiter,
T., Hermenegildo, M., Kowalski, R. and Rossi, F. (eds.) Prolog - The Next 50 Years. LNCS,
vol. 13900. Springer, Heidelberg (2023)
36. Wason, P.C.: Reasoning about a rule. Q. J. Exp. Psychol. 20(3), 273–281 (1968)
37. Widom, J., Ceri, S. (eds.) Active database systems: Triggers and rules for advanced database
processing. Morgan Kaufmann (1995)
38. Wielemaker, J., Riguzzi, F., Kowalski, R.A., Lager, T., Sadri, F., Calejo, M.: Using SWISH
to realise interactive web-based tutorials for logic-based languages. Theory Pract. Logic
Program. 19(2), 229–261 (2019)
Ergo: A Quest for Declarativity in Logic
Programming
1 Introduction
Declarativity has been a quest in Logic Programming since its inception. This
quest is shared by the Ergo language, which tries to make the declarative seman-
tics (what a program means) and the operational semantics (how a program
derives its results) as transparent as possible.
Ergo [7], or ErgoAI Reasoner, is part of the recently open-sourced ErgoAI
suite of tools developed by Coherent Knowledge Systems, LLC.1 The other
tools include an IDE and connectors to major programming languages and data
sources. Ergo is the successor of and a replacement for the well-known Flora-2
[19] system, developed at Stony Brook University, NY. Although modern Prologs
have developed far beyond Prolog’s original ISO specification [17], and although
1
http://coherentknowledge.com/.
Ergo is compiled into XSB, Ergo differs substantially from Prolog. First, while
Ergo supports predicate definitions using a syntax similar to Prolog, it also fully
supports HiLog [6] and the F-logic frame syntax [20] along with rule id’s, Skolem
symbols and much else. Second, like some Prologs, including XSB [29] and SWI
[37], Ergo supports the well-founded semantics (WFS) [31] along with SLDNF-
style resolution. But WFS is the core semantics of Ergo. Furthermore, Ergo sup-
ports defeasible reasoning, explicit negation, and quantifiers. Third, Ergo draws
from deductive databases default support for reactivity and transactionality. We
refer to languages and systems like Ergo and Flora-2 that are based on the well-
founded semantics as WFS-based Logic Programming, (WFSLP) to distinguish
them from Prolog-based systems and from Answer Set Programs (ASP), which is
based on the stable model semantics (SMS)). In order to leverage WFSLP, Ergo
makes heavy use of tabling in ways that are discussed throughout the paper.
Due to space limitations we cannot provide an introduction to tabling, but a
short overview of tabling can be found in the introduction to this volume [36],
while a much more detailed tutorial can be found in [30].
We believe that WFSLP is important for programmers who wish to program
at a high but computationally clear level; that systems such as Ergo serve as
a means to make available Logic Programming research results such as explicit
negation, HiLog, F-Logic; and that successes and failures of WFSLP systems
can inform the community of Prolog developers. Although Ergo is still evolving,
it has been used in commercial and research projects in financial compliance,
legal reasoning, healthcare, and battlefield assessment.
This paper describes some aspects of Ergo that we believe are both significant
and of potential interest to the Prolog community. Sections 3, and 4 describe a
few aspects of Ergo’s approach to declarativity. Section 5 discusses explainability.
Section 6 briefly describes interfaces and applications. The final Sect. 7 discusses
Ergo features that could, versus could not, be adopted by Prolog systems, as
well as summarizes current and future work.
2 Ergo Syntax
HiLog. Ergo has a rich syntax that includes HiLog predicates [6] and F-logic
frames and classes [20]. In forming either type of construct, variables and con-
stants in Ergo are denoted slightly differently than in Prolog. For instance
p(‘John’,goes,Where) is a Prolog term in which the first two arguments are
constants and the third a variable. The same term in Ergo is written as p(John,
goes,?Where). Variables in Ergo thus begin with the ? symbol, while capitalized
strings such as John are constants, rather than variables as in Prolog.
Unlike a Prolog term, which has the form: p(t1 , ..., tn ) where p is a function
symbol and t1 , ..., tn are logical terms, a HiLog term has the form t0 (t1 , ..., tn )
where t0 , ..., tn are also HiLog terms. For example,
closure(?Graph)(?From,?To)
226 B. Grosof et al.
F-logic Frames. F-logic frames differ significantly from Prolog and Hilog pred-
icates, and there is not enough space to fully describe frames in this paper. To
get a taste of F-logic frames, consider the frame molecule:
John:person[age->?A>30, child->Bob,
phone->?P \in [’111-222-3456’,’123-456-7890’]]
These frame facts say that John is a person, whose child is Bob, and whose age is
constrained to be greater than 30, and who has two particular phone numbers.
Frame rules may also make use of monotonic and non-monotonic (property-
overriding) inheritance. An Ergo rule can contain any HiLog term or F-logic
frame as a subgoal. Ergo also has support for user-defined functions and other
extensions, that cannot be discussed here due to space limitation.
Flora-2 and then Ergo were the result of a long series of efforts by us and other
researchers to develop technologies that could serve as building blocks for truly
declarative LP systems in the resolution-based computational paradigm. This
section and the next briefly discuss some of these building blocks.
The Well-Founded Semantics (WFS) in Ergo. WFS provides clear semantics
for Prolog-style LP, although when implemented in Prolog systems like XSB
and SWI, obtaining this semantics requires tabling declarations. Ergo’s strategy
is different: WFS is supported by default, making the first key step towards
declarativity, since termination and logical semantics are then assured for many
types of programs. For instance, in Ergo transitive closure may be written as:
reachable(?X,?Y):- reachable(?X,?Z),edge(?Z,?Y).
reachable(?X,?Y):- edge(?X,?Y).
edge(1,2). edge(2,3). edge(3,1).
Ergo depends on WFS in a critical way because the rules that arise from the
use of F-logic and HiLog (Sect. 2), and defeasible reasoning (Sect. 4.2), are often
non-stratified.
Ergo: A Quest for Declarativity in Logic Programming 227
p(?X) :- p(f(?X))
It is easy to see that PF inM od has a finite (empty) model. However in the
default WFS tabling of Ergo (as well as of Prolog) the query ?- p(?X) will
not terminate since an infinite number of subgoals will be created: p(?X),
p(f(?X)), p(f(f(?X))) and so on. Ergo can address this via subgoal abstrac-
tion [26], in which subgoals are abstracted once they reach a maximum size. In
PF inM od , abstraction with subgoal depth 2 or more would rewrite p(f(f(?X)))
to p(f(?X)) ensuring a finite number of subgoals. In fact, it can be shown than
when subgoal abstraction is added to WFS, any program with a finite model
(i.e., a finite number of true and undefined answers) will terminate. Second,
consider the program PInf M od , whose model is infinite:
p(f(?X)) :- p(?X).
p(a).
1. insert{p(a)},\false.
2. t insert{p(a)},\false.
After Sequence 1 is executed, p(a) will have been inserted into Ergo’s store
and will become true. In contrast, p(a) is not true after Sequence 2 in which the
transactional insert, t insert/1, was used. This is because a transactional insert
in Ergo is not “committed” if it is part of a larger goal that fails (and similarly
for transactional deletes). As with relational databases, integrity constraints can
be added, to prevent committing changes that violate an integrity constraint.
Deductions performed by Ergo react to change: by default, changes to the
underlying data are reactive (although Ergo provides a separate passive
mode). In reactive mode, if there is change to a fact or rule φ upon which
Ergo’s tables change, those tables that depend on φ and only on φ will change
as necessary in order to preserve correctness using XSB’s incremental tabling
[28]. Ergo also provides alerts that can notify or perform other actions if the
alert condition is violated. Common uses of alerts include monitoring whether
an atom A and its explicit negation \neg A are both derived, thereby making the
facts inconsistent.
Delay Quantifiers. The delay quantifiers wish/1 and must/1 cause dynamic
reordering of subgoals that do not fulfill argument binding requirements. These
quantifiers check conditions on variables in a subgoal and delay that subgoal if
these conditions fail. For example, evaluation of the subgoal must(ground(?X)
or nonvar(?Y))^?X[foo->?Y] delays evaluation of ?X[foo->?Y] until either ?X
is ground or ?Y is partially instantiated. If the desired instantiation is unachiev-
able, a runtime error results. With the wish/1 quantifier, the subgoal is executed
after the delay anyway, without issuing errors.
In a logic system, why should syntactic reordering make that be the case? And
how do we write “men who play no games”? Maybe like this?
answer(M) :- man(M), tnot((game(G),play(M,G))).
No, this also ends up with an error due to the non-ground G. Instead, one must
use a different operator in XSB:
answer(M) :- man(M), not_exists((game(G),play(M,G))).
It collects all the variables in game(G),play(M,G) and implicitly quantifies them
with ∃ in the scope of tnot. This, however, means that
answer(M) :- not_exists((game(G),play(M,G))), man(M).
would give a different answer, so both not exists and tnot have non-declarative
aspects and their semantics depends on the context where they occur. In con-
trast, Ergo allows explicit quantifiers \exist and \forall, which lets one express
the intent clearly and unambiguously. In addition, in the scope of negation, sub-
goals that have unquantified unbound variables are automatically delayed, so
the semantics does not depend on the order of the subgoals.
conclusions of some rules may conflict with conclusions of other rules and, if the
former rules have strength (or priority) at least as high as the latter rules then
the former conclusions may “defeat” the latter ones. In other words, inferences
made via some rules may be undone by other inferences. We will illustrate the
idea via an example from the U.S. tax code, simplified for didactic purposes. We
give, next, an informal syntax, partly in italics; then, later, actual Ergo code.
Default rule:
deductible(Expense) :- work related(Expense).
But (exception):
nondeductible(Expense) :- lobbying(Expense).
Nevertheless (exception to exception):
deductible(Expense):-lobbying(Expense),state permitted(Expense).
Constraint: can’t be both deductible and not.
false :- deductible(Expense), nondeductible(Expense).
Facts:
work related(expense123). lobbying(expense123).
state permitted(expense123).
Query: Is expense123 deductible?
?- deductible(expense123).
The above rules imply both deductible(expense123) and
nondeductible(expense123), which is inconsistent with the constraint. The
inconsistency problem could be repaired using negation:
deductible(Exp) :- work_related(Exp), not nondeductible(Exp).
nondeductible(Exp) :- lobbying(Exp), not state_permitted(Exp).
but the number of exceptions, exceptions to exceptions, exceptions to exceptions
to exceptions, etc., in a real situation can be large, making it hard to manage
the complexity of the rules and get them right.
Defeasible reasoning takes a different approach. Rather than piling up nega-
tion upon negation as above, we instead keep the original simple form of the rules
— pretty much pulling their formulation right out of the tax code — but organize
the rules according to a “priority” or “overriding” relationship, which, again, is
pulled out of the same tax code. Typically, this priority relation is acyclic but it
does not have to be. A rule inference can be defeated if inferences made by higher
priority rules contradict the lower priority inference. Defeat may have different
forms, like rebuttal, refutation, and others. What that means exactly is specified
via an argumentation theory and the version of defeasible reasoning implemented
in Ergo is called Logic Programming with Defaults and Argumentation Theories
(LPDA) [33]. Ergo comes with a dozen of different (mostly experimental) argu-
mentation theories, but we are aware of only 3 or 4 actually being employed
by the users. Note that an Ergo program can engage different argumentation
theories in different modules. Here is our tax code in the actual Ergo language:
:- use_argumentation_theory.
@default_rule1 deductible(?Expense) :- work_related(?Expense).
@exceptn1 nondeductible(?Expense) :- lobbying(?Expense).
Ergo: A Quest for Declarativity in Logic Programming 231
@exceptn_to_exceptn1 deductible(?Expense) :-
lobbying(?Expense), state_permitted(?Expense).
\overrides(exceptn1, default_rule1). // rule 2 beats rule 1
\overrides(exceptn_to_exceptn1, exceptn1). // rule 3 beats rule 2
// ?X can’t be both deductible & not
\opposes(deductible(?X), nondeductible(?X)).
work_related(expense123). lobbying(expense123).
Here the first statement tells Ergo to use the default argumentation theory.
The syntax @default rule1 and the like is used to assign tags to rules, and
these tags are then used to define the rule overriding relation. The \opposes
relation specifies facts that contradict each other and thus cannot be true in the
same consistent knowledge base. With the above program, we get that
expense123 is deductible because:
it is derived by default rule1, and
default rule1 is not defeated by exceptn1 because
exceptn1 is defeated by exceptn exceptn1.
These defeated/not-defeated conclusions hold in the default argumentation the-
ory, and all other argumentation theories, in Ergo.
LPDA is not the only theory for defeasible reasoning proposed over the years.
Here is just a small sample of other approaches: [1,4,5,8–11,15,22,23,27,35,40].
LPDA, however, distinguishes itself by its simplicity, flexibility, and declarativity.
It can simulate several other approaches to defeasibility and is available both
in the WFS and SMS versions [33,34], each being a natural extension of the
corresponding semantics in standard LP.
5 Explanations
Explainability of how a system reaches its conclusions and decisions is an impor-
tant capability, especially for AI. It aids the productivity and reliability of sys-
tem development, especially testing and debugging, for developers, domain sub-
ject matter experts, executives who sponsor development and operations, cus-
tomer/IT support, and regulatory/legal compliance. In some high-value applica-
tion arenas, such as defense, finance, and healthcare, explainability is a critical
requirement for trust in the system, across the lifecycle.
For every query answer, Ergo automatically generates and makes available a
full explanation for that particular answer, justified as proof in natural deduc-
tion (a.k.a. intuitionistic) style [16]. The structure of the justification is a set
of chained reasoning steps (each corresponding to an instantiated rule). The
overall collection of steps forms a directed graph, in which literals are nodes.
The (potentially cyclic) digraph is also available as a tree, including in graphical
human-computer interaction (HCI) via an expansible/collapsible tree metaphor
for presentation and navigation. Ergo has a capability for restricted natural lan-
guage processing (NLP), based on templates that map between logical syntax
and natural language sentences. Using this capability, Ergo automatically makes
available explanations in natural language, e.g., English.
232 B. Grosof et al.
The regulation was captured in Ergo via a corpus of defeasible rules that also
mapped between logical syntax and English phrases. Ergo automatically inferred
answers to decision/analytics questions about compliance with Regulation W,
and automatically provided an explanation for every answer. These questions
were asked in English and explanations were presented in English. This showed
that Ergo can be used directly by domain experts to encode, query and obtain
answers with justifications in natural language, in cooperation with knowledge
engineers, instead of the longer traditional process of developing an expert sys-
tem (where the domain expert is repetitively interviewed by the knowledge engi-
neer who constructs and validates the expert system). Ergo was particularly
useful in this application for providing fully detailed explanations of compliance
decisions, that constituted audit trails, and for analyzing relationships between
affiliates. Non-programmer compliance officers were able to understand these
explanations, straightforwardly and with confidence, including to validate rules
and conclusions as correct, incorrect, or missing – i.e., to help debug knowledge.
Support for Machine Learning. A key area for current and future work is to
combine Ergo more tightly with machine learning (ML) and with natural lan-
guage processing (which is largely based on ML). In one example of current work
for battlefield assessment, Ergo calls SpaCy [21], an NLP suite, to process nat-
ural language statements about a given situation which Ergo assembles into a
coherent whole for decision support. In another, Ergo processes natural language
questions via SpaCy to answer questions about a collection of scene graphs [18]
of visual images. Conversely, there are also important opportunities for ML and
NLP to leverage Ergo. ML training might call Ergo as part of a constraint in
a loss function. Such a setup would make heavy use of XSB’s efficient Python
interface, as well as Ergo’s nearly linear scalability.2 .
Uncertainty. The value of combining Ergo with ML will increase as Ergo incor-
porates uncertainty more fully. A starting point is the integration of XSB’s pack-
ages for uncertainty, PITA [25] and its extension PLOW [14]. These formalisms
handle not only full probabilistic reasoning, which has a high computational
complexity, but also T-Norm (a.k.a. fuzzy) style reasoning whose complexity is
much lower. As a use case of uncertain reasoning, the use of Ergo within loss
functions will often depend on the differentiability of Ergo’s inferences. Alter-
nately, for Ergo to say, resolve ambiguous natural language parses, it must be
able to weigh uncertainties arising out of a neural model. Unfortunately, this is
not always straightforward because neural models often have a topmost softmax
layer, which can artificially reduce the entropy of a result and its alternatives
(cf. e.g., [32]). More discussion on directions for future work appears in [12].
References
1. Baader, F., Hollunder, B.: Priorities on defaults with prerequisites, and their appli-
cation in treating specificity in terminological default logic. J. Autom. Reason.
15(1), 41–68 (1995)
2. Bonner, A., Kifer, M.: Transaction logic programming. In: Int’l Conference on
Logic Programming, pp. 257–282. MIT Press, Budapest, Hungary (June 1993)
3. Bonner, A., Kifer, M.: An overview of transaction logic. Theor. Comput. Sci. 133,
205–265 (1994)
4. Brewka, G., Eiter, T.: Preferred answer sets for extended logic programs. Artif.
Intell. 109, 297–356 (1999)
5. Brewka, G., Eiter, T.: Prioritizing default logic. In: Intellectics and Computational
Logic - Papers in Honour of Wolfgang Bibel, pp. 27–45. Kluwer Academic Pub-
lishers (2000)
6. Chen, W., Kifer, M., Warren, D.S.: HiLog: a foundation for higher-order logic
programming. JLP 15(3), 187–230 (1993)
7. Coherent Knowledge Systems: ErgoAI. A Website (2023). https://github.com/
ErgoAI
8. Delgrande, J., Schaub, T., Tompits, H.: A framework for compiling preferences in
logic programs. Theory Pract. Logic Program. 2, 129–187 (2003)
2
in the size of the ground program; a consequence of being based on the well-founded
semantics, plus the design of Ergo’s expressive extensions (e.g., HiLog).
Ergo: A Quest for Declarativity in Logic Programming 235
9. Dung, P., Son, T.: An argument-based approach to reasoning with specificity. Artif.
Intell. 133(1–2), 35–85 (2001). https://doi.org/10.1016/S0004-3702(01)00134-5
10. Eiter, T., Faber, W., Leone, N., Pfeifer, G.: Computing preferred answer sets by
meta-interpretation in answer set programming. Theory Pract. Logic Program.
3(4), 463–498 (2003)
11. Gelfond, M., Son, T.C.: Reasoning with prioritized defaults. In: Dix, J., Pereira,
L.M., Przymusinski, T.C. (eds.) LPKR 1997. LNCS, vol. 1471, pp. 164–223.
Springer, Heidelberg (1998). https://doi.org/10.1007/BFb0054795
12. Grosof, B.: Logic programming in AI: some directions. In: Programming with High-
Level Abstractions. Proceedings of the 3rd Workshop on Logic and Practice of
Programming (LPOP), pp. 66–70 (2022)
13. Grosof, B., Swift, T.: Radial Restraint: a semantically clean approach to bounded
rationality for logic programs. In: Proceedings of AAAI (2013)
14. Grosof, B., Swift, T.: PLOW: probabilistic logic over the well-founded semantics.
In: AAAI Spring Symposium on Combining Machine Learning with Knowledge
Engineering (AAAI-MAKE) (2019)
15. Grosof, B.: A courteous compiler from generalized courteous logic programs to ordi-
nary logic programs. Tech. Rep. Supplementary Update Follow-On to RC 21472,
IBM (July 1999)
16. IEP: Natural deduction. In: Feiser, J., Dowden, B. (eds.) Internet Encyclopedia
of Philosophy. IEP (2023), a Peer-Reviewed Academic Resource. https://iep.utm.
edu/natural-deduction
17. ISO working group JTC1/SC22: Prolog international standard ISO-IEC 13211–1.
Tech. Rep., International Standards Organization (1995)
18. Johnson, J., et al.: Image retrieval using scene graphs. In: Conference on Neural
Information Processing Systems, pp. 3668–3678 (2015)
19. Kifer, M.: Knowledge representation & reasoning with Flora-2. The Flora-2 Web
Site (2022). http://flora.sourceforge.net
20. Kifer, M., Lausen, G., Wu, J.: Logical foundations of object-oriented and frame-
based languages. J. ACM 42, 741–843 (1995)
21. Montani, M.H.I.: spaCy 2: natural language understanding with Bloom embed-
dings, convolutional neural networks and incremental parsing (2017). https://
spacy.io
22. Nute, D.: Defeasible logic. In: Handbook of logic in artificial intelligence and logic
programming, pp. 353–395. Oxford University Press (1994)
23. Prakken, H.: An argumentation framework in default logic. Ann. Math. Artif.
Intell. 9(1–2), 93–132 (1993)
24. Rezk, M., Kifer, M.: Transaction logic with partially defined actions. J. Data
Semantics 1(2), 99–131 (2012)
25. Riguzzi, F., Swift, T.: The PITA system: Tabling and answer subsumption for rea-
soning under uncertainty. Theory Pract. Logic Program. 11(4–5), 433–449 (2011)
26. Riguzzi, F., Swift, T.: Terminating evaluation of logic programs with finite three-
valued models. ACM Transactions on Computational Logic 15(4) (2014)
27. Sakama, C., Inoue, K.: Prioritized logic programming and its application to com-
monsense reasoning. Artif. Intell. 123(1–2), 185–222 (2000)
28. Swift, T.: Incremental tabling in support of knowledge representation and reason-
ing. Theory Pract. Logic Program. 14(4–5) (2014)
29. Swift, T., Warren, D.: XSB: extending the power of prolog using tabling. Theory
Pract. Logic Program. 12(1–2), 157–187 (2012)
30. Swift, T., Warren, D., et al.: The XSB Programmer’s Manual: Volume, Version 5.0
(2022). http://xsb.sourceforge.net
236 B. Grosof et al.
31. van Gelder, A., Ross, K., Schlipf, J.: Unfounded sets and well-founded semantics
for general logic programs. J. ACM 38(3), 620–650 (1991)
32. Vickram Rajendran, W.L.: Accurate layerwise interpretable competence estima-
tion. In: Conference on Neural Information Processing Systems (2019)
33. Wan, H., Grosof, B., Kifer, M., Fodor, P., Liang, S.: Logic programming with
defaults and argumentation theories. In: International Conference on Logic Pro-
gramming (July 2009)
34. Wan, H., Kifer, M., Grosof, B.: Defeasibility in answer set programs with defaults
and argumentation rules. Semantic Web J. (2014)
35. Wang, K., Zhou, L., Lin, F.: Alternating fixpoint theory for logic programs with
priority. In: Lloyd, J., Dahl, V., Furbach, U., Kerber, M., Lau, K.-K., Palamidessi,
C., Pereira, L.M., Sagiv, Y., Stuckey, P.J. (eds.) CL 2000. LNCS (LNAI), vol. 1861,
pp. 164–178. Springer, Heidelberg (2000). https://doi.org/10.1007/3-540-44957-
4 11
36. Warren, D.S.: Introduction to prolog. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years. No.
13900 in LNCS, Springer (July 2023)
37. Wielemaker, J., Schrijvers, T., Triska, M., Lager, T.: SWI-prolog. Theory Pract.
Logic Program. 12(1–2), 67–96 (2012)
38. Yang, G., Kifer, M.: Inheritance in rule-based frame systems: semantics and infer-
ence. J. Data Semantics 2800, 69–97 (2003)
39. Yang, G., Kifer, M., Zhao, C.: Flora-2: A rule-based knowledge representation and
inference infrastructure for the Semantic Web. In: International Conference on
Ontologies, Databases and Applications of Semantics (ODBASE-2003), pp. 671–
688 (2003)
40. Zhang, Y., Wu, C., Bai, Y.: Implementing prioritized logic programming. AI Com-
mun. 14(4), 183–196 (2001)
Prolog Applications: Finalists
for the Colmerauer Prize
P RO B: Harnessing the Power of Prolog
to Bring Formal Models and Mathematics
to Life
Michael Leuschel(B)
Z CSP
Event-B
B
Formal
Models &
Mathematics
Prolog
Over the years, the performance of ProB has continually been improved.
This enabled the use of ProB for data validation, i.e., verifying properties of
large data sets [22]. For this use, ProB has been certified [5] for use in safety-
critical applications according to the European norm EN 50128. In particular,
1
The letters “Pro” in the name ProB also makes allusion to its reliance on Prolog.
242 M. Leuschel
ProB has been used by companies like Siemens, Alstom, ClearSy and Thales,
to validate the safe configuration of train systems all over the world (e.g., Paris
metro line 1, São Paulo, Alger, Barcelona, Mexico, Toronto) [7]. The surveys
[11] and [4] provide some interesting feedback about ProB’s use in the railway
domain.
In another new direction, ProB was used in [15] for the first time to execute
a high-level formal B model in real-time to control trains (cf., Fig. 2). In this
case, a mathematical model was directly used as a runtime artefact, without the
need for code generation, be it manual or automated.
ProB has also been used by many academics in teaching and research.
Notably, it is itself being used within several other tools, ranging from domain
specific modeling tools (Meeduse [17], Coda [6]), university course planning
(Plues [30]), railway validation (SafeCap [18], RDV [26], DTVT [22], Olaf,
ClearSy Data Solver, Dave, Ovado [3]), security analyses (B4MSecure, VTG
[29]), UML modeling (iUML and UML-B [28], UseCasePro), to test case gener-
ation (BTestBox, Cucumber-Event-B [31]).
3 Implementation
ProB has been developed over around 20 years and was initially developed in
SICStus Prolog [8]. The core of ProB consists of about 400 Prolog files con-
taining over 150,000 lines of code along with a large test suite of almost 7 000 unit
tests and more than 2 000 integration tests. This strict testing regime is impor-
tant for certification. For end users, ProB provides multiple user interfaces:
a command-line interface (probcli) for batch verification and data validation,
a Jupyter kernel [12] for notebook usage, a Java API, and a set of graphical
user interfaces (Tcl/Tk, JavaFX) for interactive animation, visualisation, and
verification. All of these interfaces share the same Prolog core.
Although development began over 20 years ago, ProB is still actively devel-
oped. It is certainly a challenge to keep an academic tool in development for
that amount of time; but the task was eased by the support of many students
and researchers, the robustness of Prolog and the excellent support provided by
SICStus.
ProB has recently [13] been made compatible with SWI-Prolog and we are
working to make it compatible with Logtalk and other Prolog systems like Ciao
Prolog.
4 Challenge
First, we define the infinite set of primes and compute the intersection with
a finite set of candidate values:
Observe how the intersection is computed explicitly, while the set of all primes
is automatically kept symbolic. In a similar fashion, the variable f is automat-
ically recognised in the next example as a (higher-order) infinite function and
kept symbolic. Again, the set comprehension res of all square roots of 100 is com-
puted explicitly. Also note that f (2) denotes the (also infinite) squaring function
and that all solutions for res are computed, even though no finite bounds were
provided for x.
theoretical computer science books into Jupyter enabling students to bring the
mathematical definitions to life with ProB [12].
5 Conclusion
ProB is an animator, model checker and constraint solver for high-level formal
models. It has been developed over around 20 years and has harnessed the power
of Prolog to help users develop safe systems. ProB takes away tedious choices,
automatically detects subtle bugs, but still leaves users in control to interactively
validate their models. A friendly user experience was always more relevant for
ProB than raw benchmark figures. For example, ProB will catch overflows, deal
with divisions by zero, and keep track of source level information to visualise and
explain errors to end users.
We hope that we can keep on improving ProB and that we have not yet
reached the limit of what Prolog and formal methods have to offer. Indeed, we
want to drive the idea of formal models as runtime artefacts further (see Fig. 2).
The capabilities of the constraint solver of course could still be improved; maybe
we can one day reach a state of a human friendly “executable mathematics”
language which can be used by novice and expert alike. Safety will also play
a crucial role in the future, in particular with the increased use of artificial
intelligence in autonomous systems.
References
1. Abrial, J.R.: The B-Book. Cambridge University Press (1996). https://doi.org/10.
1017/CBO9780511624162
2. Abrial, J.R.: Modeling in Event-B: System and Software Engineering. Cambridge
University Press (2010)
3. Badeau, F., Chappelin, J., Lamare, J.: Generating and verifying configuration data
with OVADO. In: Dutilleul, S.C., Haxthausen, A.E., Lecomte, T. (eds.) Proceed-
ings RSSRail, pp. 143–148. LNCS, vol. 13294, Springer (2022). https://doi.org/10.
1007/978-3-031-05814-1 10
246 M. Leuschel
4. ter Beek, M.H., et al.: Adopting formal methods in an industrial setting: the rail-
ways case. In: ter Beek, M.H., McIver, A., Oliveira, J.N. (eds.) FM 2019. LNCS,
vol. 11800, pp. 762–772. Springer, Cham (2019). https://doi.org/10.1007/978-3-
030-30942-8 46
5. Bendisposto, J., Krings, S., Leuschel, M.: Who watches the watchers: Validating the
ProB validation tool. In: Proceedings of the 1st Workshop on Formal-IDE. EPTCS
XYZ, 2014, Electronic Proceedings in Theoretical Computer Science (2014)
6. Butler, M.J., et al.: Modelling and refinement in CODA. In: Derrick, J., Boiten,
E.A., Reeves, S. (eds.) Proceedings Refine@IFM 2013, Turku, Finland, 11th June
2013. EPTCS, vol. 115, pp. 36–51 (2013). https://doi.org/10.4204/EPTCS.115.3
7. Butler, M., Körner, P., Krings, S., Lecomte, T., Leuschel, M., Mejia, L.-F., Voisin,
L.: The first twenty-five years of industrial use of the B-Method. In: ter Beek, M.H.,
Ničković, D. (eds.) FMICS 2020. LNCS, vol. 12327, pp. 189–209. Springer, Cham
(2020). https://doi.org/10.1007/978-3-030-58298-2 8
8. Carlsson, M., Mildner, P.: SICStus Prolog - the first 25 years. Theory Pract. Log.
Program. 12(1-2), 35–66 (2012). https://doi.org/10.1017/S1471068411000482
9. Carlsson, M., Ottosson, G., Carlson, B.: An open-ended finite domain constraint
solver. In: Glaser, H., Hartel, P., Kuchen, H. (eds.) PLILP 1997. LNCS, vol. 1292,
pp. 191–206. Springer, Heidelberg (1997). https://doi.org/10.1007/BFb0033845
10. Dollé, D., Essamé, D., Falampin, J.: B dans le transport ferroviaire. L’expérience
de Siemens Transportation Systems. Technique et Science Informatiques 22(1),
11–32 (2003)
11. Ferrari, A., et al.: Survey on formal methods and tools in railways: the ASTRail
approach. In: Collart-Dutilleul, S., Lecomte, T., Romanovsky, A. (eds.) RSSRail
2019. LNCS, vol. 11495, pp. 226–241. Springer, Cham (2019). https://doi.org/10.
1007/978-3-030-18744-6 15
12. Geleßus, D., Leuschel, M.: ProB and Jupyter for logic, set theory, theoretical com-
puter science and formal methods. In: Raschke, A., Méry, D., Houdek, F. (eds.)
ABZ 2020. LNCS, vol. 12071, pp. 248–254. Springer, Cham (2020). https://doi.
org/10.1007/978-3-030-48077-6 19
13. Geleßus, D., Leuschel, M.: Making ProB compatible with SWI-Prolog. The-
ory Pract. Log. Program. 22(5), 755–769 (2022). https://doi.org/10.1017/
S1471068422000230
14. Hallerstede, S., Leuschel, M.: Constraint-based deadlock checking of high-level
specifications. Theory Pract. Log. Program. 11(4–5), 767–782 (2011)
15. Hansen, D., et al.: Validation and real-life demonstration of ETCS hybrid level 3
principles using a formal B model. Int. J. Softw. Tools Technol. Transfer 22(3),
315–332 (2020). https://doi.org/10.1007/s10009-020-00551-6
16. Hayes, I., Jones, C.B.: Specifications are not (necessarily) executable. Softw. Eng.
J. 4(6), 330–338 (1989). https://doi.org/10.1049/sej.1989.0045
17. Idani, A.: Meeduse: a tool to build and run proved DSLs. In: Dongol, B., Troubit-
syna, E. (eds.) IFM 2020. LNCS, vol. 12546, pp. 349–367. Springer, Cham (2020).
https://doi.org/10.1007/978-3-030-63461-2 19
18. Iliasov, A., Lopatkin, I., Romanovsky, A.: The SafeCap platform for modelling rail-
way safety and capacity. In: Bitsch, F., Guiochet, J., Kaâniche, M. (eds.) SAFE-
COMP 2013. LNCS, vol. 8153, pp. 130–137. Springer, Heidelberg (2013). https://
doi.org/10.1007/978-3-642-40793-2 12
19. Körner, P., Bendisposto, J., Dunkelau, J., Krings, S., Leuschel, M.: Integrating
formal specifications into applications: the ProB Java API. Formal Methods Syst.
Des. 58(1-2), 160–187 (2021). https://doi.org/10.1007/s10703-020-00351-3
ProB: Harnessing the Power of Prolog to Bring Formal Models 247
20. Krings, S., Leuschel, M.: Proof assisted bounded and unbounded symbolic model
checking of software and system models. Sci. Comput. Program. 158, 41–63 (2018).
https://doi.org/10.1016/j.scico.2017.08.013
21. Lamport, L.: Specifying Systems, The TLA+ Language and Tools for Hardware
and Software Engineers. Addison-Wesley (2002)
22. Lecomte, T., Burdy, L., Leuschel, M.: Formally checking large data sets in the
railways. CoRR abs/1210.6815 (2012), proceedings of DS-Event-B 2012, Kyoto
23. Lecomte, T., Deharbe, D., Prun, E., Mottin, E.: Applying a formal method in
industry: a 25-year trajectory. In: Cavalheiro, S., Fiadeiro, J. (eds.) SBMF 2017.
LNCS, vol. 10623, pp. 70–87. Springer, Cham (2017). https://doi.org/10.1007/978-
3-319-70848-5 6
24. Leuschel, M., Butler, M.: ProB: a model checker for B. In: Araki, K., Gnesi, S.,
Mandrioli, D. (eds.) FME 2003. LNCS, vol. 2805, pp. 855–874. Springer, Heidelberg
(2003). https://doi.org/10.1007/978-3-540-45236-2 46
25. Leuschel, M., Butler, M.J.: ProB: an automated analysis toolset for the B method.
STTT 10(2), 185–203 (2008)
26. Leuschel, M., Falampin, J., Fritz, F., Plagge, D.: Automated property verification
for large scale B models with ProB. Formal Asp. Comput. 23(6), 683–709 (2011).
https://doi.org/10.1007/s00165-010-0172-1
27. Morales, J., Abreu, S., Hermenegildo, M.V.: Teaching prolog with active logic
documents. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R.,
Rossi, F. (eds.) Prolog - The Next 50 Years. No. 13900. LNCS. Springer (July
2023)
28. Said, M.Y., Butler, M., Snook, C.: A method of refinement in UML-B. Softw. Syst.
Modeling 14(4), 1557–1580 (2013). https://doi.org/10.1007/s10270-013-0391-z
29. Savary, A., Frappier, M., Leuschel, M., Lanet, J.-L.: Model-based robustness testing
in Event-B using mutation. In: Calinescu, R., Rumpe, B. (eds.) SEFM 2015.
LNCS, vol. 9276, pp. 132–147. Springer, Cham (2015). https://doi.org/10.1007/
978-3-319-22969-0 10
30. Schneider, D., Leuschel, M., Witt, T.: Model-based problem solving for university
timetable validation and improvement. In: Bjørner, N., de Boer, F. (eds.) FM
2015. LNCS, vol. 9109, pp. 487–495. Springer, Cham (2015). https://doi.org/10.
1007/978-3-319-19249-9 30
31. Snook, C., et al.: Behaviour-driven formal model development. In: Sun, J., Sun, M.
(eds.) ICFEM 2018. LNCS, vol. 11232, pp. 21–36. Springer, Cham (2018). https://
doi.org/10.1007/978-3-030-02450-5 2
32. Spivey, J.M.: The Z Notation: a reference manual. Prentice-Hall (1992)
33. Vu, F., Leuschel, M., Mashkoor, A.: Validation of formal models by timed proba-
bilistic simulation. In: Raschke, A., Méry, D. (eds.) ABZ 2021. LNCS, vol. 12709,
pp. 81–96. Springer, Cham (2021). https://doi.org/10.1007/978-3-030-77543-8 6
34. Warren, D.S.: Introduction to Prolog. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years.
No. 13900. LNCS. Springer (July 2023)
35. Werth, M., Leuschel, M.: VisB: a lightweight tool to visualize formal models with
SVG graphics. In: Raschke, A., Méry, D., Houdek, F. (eds.) ABZ 2020. LNCS, vol.
12071, pp. 260–265. Springer, Cham (2020). https://doi.org/10.1007/978-3-030-
48077-6 21
Pacioli: A PROLOG System for Financial
Report Processing
1 Introduction
Financial information is reported by public/listed companies to regulators world-
wide using a standard rich structured data format, the “Extensible Business
Reporting Language”, or XBRL [1]. For example, in the United States public
companies submit XBRL-based financial reports to the Securities and Exchange
Commission (SEC). In Europe listed companies submit XBRL-based financial
reports to the European Single Market Authority (ESMA).
XBRL is an open standard, with specifications freely available and freely
licensed. XBRL is supported by a strong software development community that
has created a range of products, both free and commercial, covering the needs of
both end users and developers of XBRL-based reporting solutions and products.
For additional information about XBRL, please refer to the XBRL technical
specifications [2].
An XBRL report typically comprises two pieces:
– Report: A single XML (or alternatively “inline XBRL”, iXBRL [3] file with
instance data: fact values for financial concepts – such as sales or inventory
c The Author(s), under exclusive license to Springer Nature Switzerland AG 2023
D. S. Warren et al. (Eds.): Prolog: The Next 50 Years, LNAI 13900, pp. 248–259, 2023.
https://doi.org/10.1007/978-3-031-35254-6_20
Pacioli: A PROLOG System for Financial Report Processing 249
– contextualized with dates and multiple dimensions – such as sales per ter-
ritory, product, and salesperson; the instance data depends on:
– Report model: Background ontological information, in the form of a “Dis-
coverable Taxonomy Set” – potentially dozens of files with data element
schema and hypercube (dimensions) definitions, hierarchical presentation
directives, assertions - a tree graph of XML schema and linkbase resources
published on the web.
Pacioli1 is a logic and rules engine and toolkit that is purpose built and
understands global standard XBRL-based digital financial report models and
reports. Both API and GUI interfaces are available for Pacioli. Pacioli provides:
– Loading of XBRL reports and their Discoverable Taxonomy Sets into a self-
contained Prolog representation.
– Prolog-based XBRL formula processor, adding auditing and explanation
capabilities.
– Combination of a report model with user alterations of formulas and facts,
as well as additional XBRL linkbases.
– Fact mapping and derivation, thus providing an “ontology mapping” mech-
anism to align diverse facts into the same financial concepts for a report,
making it more easily comparable to others
– Detection of higher-level logical “blocks” of information, beyond XBRL, such
as roll ups, roll forwards, adjustments, disclosures, etc.
– Processing of XBRL and extra-XBRL rules for report validation, includ-
ing type-subtype associations, disclosure mechanics rules, reporting checklist
rules, report model structure rules, and “Fundamental Accounting Concepts”
continuity crosscheck rules; this is described by the Seattle Method [4] of pro-
cessing XBRL-based financial reports;
– All rule outcomes are persisted to the Pacioli model on IPFS2 , anchored to
a Merkle-like cryptographic hash of all the above ingredients, thus ensuring
immutability and reproducibility for posterity
– Multiple report rendering interfaces
So for example, given Apple’s 10K for 2021 inline XBRL filing3 , Pacioli pro-
duces an analysis4 ; one information block of which is shown in the pivot table
below:
1
Auditchain, Pacioli Logic and Rules Engine, https://docs.auditchain.finance/audit
chain-protocol/pacioli-logic-and-rules-engine.
2
https://ipfs.io, the “InterPlanetary File System”, a decentralized, redundant, robust
file storage service.
3
https://www.sec.gov/Archives/edgar/data/320193/000032019321000105/aapl-
20210925.htm.
4
Pacioli Technical Analysis report saved to IPFS, https://auditchain.infura-ipfs.io/
ipfs/QmSuMTNG1W98U3xTsJRX2cs1LxKQqGKqM9iq2w1HhsaCZB/.
250 M. Calejo and C. Hoffman
The above gory representation boils down to: we know that sales (as defined
in the “Fundamental Accounting Concepts” taxonomy [6]) is USD 365817000000
because that value was reported for US-GAAP taxonomy [7] concept ‘us-gaap’:
‘RevenueFromContractWithCustomerExcludingAssessedTax’.
5
“Model” being used here in the broad sense of representation, NOT of Herbrand
model. For the above example, https://auditchain.infura-ipfs.io/ipfs/QmSuMT
NG1W98U3xTsJRX2cs1LxKQqGKqM9iq2w1HhsaCZB/ReportAndModel.pl.gzip.
Pacioli: A PROLOG System for Financial Report Processing 251
For more examples, see the Pacioli batch report6 for recent Dow Jones top
30 company filings.
2 System Architecture
Pacioli has been available since early 2021 as a web application at
http://pacioli.auditchain.finance to support debugging, rule development and
training. Advanced users (developers, accountants) interact with SWISH [8]
notebooks7 : they submit financial report URLs and obtain report validation
analyses from Pacioli, as self-contained HTML mini sites generated by SWI-
Prolog’s “termerized” HTML templates [9], using Javascript frameworks8 for
browser client-side data rendering. The report analysis output includes machine-
readable (Prolog and JSON) files, all stored on IPFS:
But its main use case is embedded in “Pacioli nodes”, constituting the decen-
tralized Auditchain validator network, anchored over the Polygon blockchain
and monetary currency:
6
https://auditchain.infura-ipfs.io/ipfs/QmaATb3njmXgbbZVuUPuJweukyHNk2Wb
xGVJCSEUgqRt3o/.
7
For exemple https://pacioli.auditchain.finance/tools/PowerUserTool.swinb.
8
Namely https://pivottable.js.org/examples/ and http://tabulator.info, in addition
to SWISH-generated https://graphviz.org graphs.
252 M. Calejo and C. Hoffman
The AUDT token acts as currency for user fees, network operation and ruleset
rewards, and the future rule NFTs marketplace: financial report rulesets will be
(in late 2023) wrapped as Non Fungible Tokens; a ruleset may comprise (say)
mapping rules and derivation rules, plus value assertions verifying the result of
some unique financial analysis.
Since late 2021 this network went through alpha and beta programs with a
dozen validators across the planet, with (Polygon) mainnet deployment immi-
nent.
11
Potentially replacing the XULE expression language by PROLOG: https://pacioli.
auditchain.finance/example/XULE.swinb.
12
XBRL reports and rules translate into PROLOG unit clauses: the formula rule facts
are meta-interpreted.
13
Pacioli Analysis Summary, https://auditchain.infura-ipfs.io/ipfs/QmNUY15G1dhT
XYCpyUyvqYWZ33Nc6mKRUDz7GDgLFonaPs/.
254 M. Calejo and C. Hoffman
To render a financial XBRL report for a human, one needs to consider first the
presentation structure imparted by its author: a big tree of “XBRL presentation
links” with a root for the report itself, then all its XBRL networks as children,
each with a sub-tree of presented XBRL concepts. This hierarchical structure of
XBRL concepts, plus the time dimension, provide a basic scaffold for “hanging”
facts in context.
But within that massive XBRL structure and facts lie smaller “blocks”14
of information, fact chunks comprising something meaningful for an analyst or
accountant, such as a “rollUp” (where one fact aggregates others, say over a
quarter or year). These higher level pieces of information are not part of XBRL,
and are instead detected by Pacioli, using “concept arrangement pattern detec-
tion” meta-rules, determining the blocks list15 :
14
“Blocks” emerged from professional practice and are informally explained
in Sect. 3.3 of http://accounting.auditchain.finance/framework/LogicalTheoryDes
cribingFinancialReport.pdf.
15
Full list for this report in https://auditchain.infura-ipfs.io/ipfs/QmNUY15G1dhT
XYCpyUyvqYWZ33Nc6mKRUDz7GDgLFonaPs/blocks.html.
Pacioli: A PROLOG System for Financial Report Processing 255
This ain’t pretty because of all the details involved, but basically a PROLOG
goal query is generated and then queried over the report facts:
– A calculation rule is picked (on line 4)
– The Sum (total) of the calculation must be in the presented concepts for the
given Network (line 5) and the “Parcels” (added terms in Portuguese...) too
(line 7)
– After setting up a couple of unbound lists, these are constrained via simple
unification (lines 16–19)
– Then a fact binder goal generator predicate is called (line 20), applying XBRL
fact binding principles
– All solutions are found, and returned one by one on backtracking (line 23–25)
The detected block is identified by path of its root concept in the XBRL
presentation tree, by the rule supporting it, and has associated a set of financial
report SupportingFacts. The last (output) argument returns the internal block
structure, identifying in this case a total and a list of items.
4.5 Rules
Most rules define assertions on facts18 , for example:
– The above rules are XBRL “value assertions”, checking some arithmetic rela-
tionship, which must hold true for all facts in the report
– XBRL calculations are conceptually a specific subtype of value assertions,
where a total must be equal to a sum of terms
– Mapping rules are simple pairs, mapping one concept in one XBRL taxonomy
(such as a regulatory standard like US-GAAP) into its equivalent in another
taxonomy (such as a custom taxonomy for analysis, such as “Fundamental
Accounting Concepts”.
– Derivation rules compute one fact from others, and are encoded as XBRL
Formulas; they’re useful to align a financial report that uses its own tax-
onomies, to some other taxonomy framework for comparison with others.
– Disclosure rules, together with reporting checklist rules, identify blocks of
information which must be present (reported) in the financial report; these
typically express regulatory obligations
A major driver for the development of Pacioli was the need to analyze
financial reports from multiple perspectives. The same report can be validated
against different rules and even concept taxonomies: for example, an analyst may
wish to evaluate a company’s performance not in terms of the facts it reported
18
Full value assertions list in https://auditchain.infura-ipfs.io/ipfs/QmNUY15G1dhT
XYCpyUyvqYWZ33Nc6mKRUDz7GDgLFonaPs/valueAssertions.html.
Pacioli: A PROLOG System for Financial Report Processing 257
directly, but instead with her own rules defined over a custom taxonomy of XBRL
concepts. Hence the need for mapping and derivation rules (effectively solving
financial reporting’s “ontology mapping” problem), while applying specific value
assertions to check the facts.
Following is a fragment for the example report’s “Derivation Graph”, showing
mapping and derivation rules at work:
Nodes represent facts, with a XBRL concept (including its taxonomy prefix)
together with a value. Links denote rule instances. ‘proof’ (the XBRL taxonomy
of the financial report in question) facts are mapped to some ‘fac’ (Fundamental
Accounting Concepts, another XBRL taxonomy) facts, and complemented by
some other derived ‘fac’ facts further to the right.
The second example has simple variable declarations, indicating just their
XBRL concept.
The first example however is more complex, as it also refers an hypercube
defining a dimension for reported vs. corrected values: two of its variables are
for the same concept (Equity), but intended to be bound to different facts,
positioned elsewhere in the hypercube. The full rules list for this example report
can be found here.
258 M. Calejo and C. Hoffman
Acknowledgement. To Jason Meyers and the Auditchain early investors, for betting
Auditchain on Pacioli; to Jacinto Dávila for help with Pacioli and for his Auditchain
Explorer UI; to Bogdan Fiedur for the blockchain agent and Auditchain smartcon-
tracts; to Christopher Jastrzebski for all the support, and his Auditchain Web3 UI; to
Fuad Begic for the complementary Luca report editor under development; to Andrew
Noble and Dudley Gould for encouragement and suggestions. To Jan Wielemaker and
associates, for a software stack that finally made PROLOG feasible for business applica-
tions. And finally, to Bob Kowalski: for his vision half century ago, and all the personal
support in recent years.
References
1. XBRL International, Introduction to XBRL. https://www.xbrl.org/introduction/
2. XBRL International, XBRL Specifications. https://specifications.xbrl.org/
specifications.html
3. XBRL International, iXBRL, https://www.xbrl.org/the-standard/what/ixbrl/
4. Charles Hoffman, CPA, Seattle Method. https://xbrlsite.com/seattlemethod/
SeattleMethod.pdf
5. US Generally Accepted Accounting Principles. https://www.cfainstitute.org/en/
advocacy/issues/gaap#sort=%40pubbrowsedate%20descending
6. Fundamental Accounting Concepts. https://accounting.auditchain.finance/fac/
Index.html
7. US-GAAP taxonomy. https://accounting.auditchain.finance/reporting-scheme/
us-gaap/documentation/Index.html
Pacioli: A PROLOG System for Financial Report Processing 259
Pierre Dissaux(B)
Abstract. Model Driven Engineering (MDE) refers to the use of software models
to represent System or Software conceptual, logical, or physical abstractions,
aiming at standardizing and easing industrial engineering processes. MDE is more
and more applied in various domains of application and brings foundations for the
digitalization of the industry. The avionics and Space software industry started the
move several decades ago, which stimulated the emergence of many innovative
technologies to improve the elaboration of these models and their exploitation.
Logic Model Processing (LMP for short) is one of them.
Logic Model Processing is an adaptation of Logic Programming to Model
Driven Engineering using standard Prolog language. The LMP framework consists
of a methodology, a set of tools and Prolog libraries. This technology has been
progressively and continuously developed during the last thirty years and produced
many actionable outcomes, mostly under the form of tools that have been deployed
worldwide for industrial usages and a few non-academic publications.
This paper introduces the origin of the LMP solution, provides an overview
of its implementation, and gives a few examples of its practical use and of its most
recent developments.
1 Genesis of LMP
The Hierarchical Object-Oriented Design (HOOD) [1] method was elaborated in the
nineties under the sponsorship of the European Space Agency, to master the increasing
complexity of large real-time embedded software, most of them written in Ada. Although
Model Driven Engineering was not a common term at that time, HOOD was already
providing a strong support for pre-coding architectural and detailed design phases of soft-
ware development life cycles. Indeed, HOOD provides software modelling abstractions
thanks to dedicated graphical and textual notations and a methodology enforcing early
verifications as well as synchronized automatic code and documentation generation.
Practical use of such solutions on real scale industrial projects required the develop-
ment of efficient Computer Aided Software Engineering (CASE) tools providing both
model edition and model processing features. One of the possible solutions that were
studied to implement these model processing features was Prolog [19]. The outcome of
this almost 30-year-old experiment was later called Logic Model Processing.
The first realization of a HOOD model processing feature using Prolog was the
development of a HOOD Rule Checker. This is required to verify that the software
architecture (HOOD Design) is really compliant with the rules defined by the HOOD
standard. An illustrative subset of this feature is explained below.
Figure 1 shows a fragment of the HOOD meta-model. The main building blocks are
the HOOD Objects that are characterized by their Name, their Kind and the name of
their parent Object in the decomposition hierarchy. With LMP, such an abstraction is
handled by the Prolog fact specification isObject(Name,Kind,Parent)..
Each Object has a Provided Interface containing a list of provided Operations
that are characterized by their own Name and the one of the Object where they
are located. This can be represented by the other Prolog fact specification, isPro-
vided(Name,'OPERAT ION',Object)..
In a similar way, connection lines can also be described by means of Prolog
facts. In particular, the implemented_by links between Operations of a parent Object
and the ones of a child Object are described by facts of the form isImplement-
edBy(Op1,'OPERATION',Parent,Op2,Child,1)..
262 P. Dissaux
These fact specifications can thus be used to populate a Prolog fact base for a concrete
HOOD model, such as the one shown in Fig. 2. Objects are represented by boxes and
Operations are listed in their top-left compartment. Implemented_by links can be
identified on the diagram by the three arrows starting from the outer box Operations. A
fragment of the corresponding populated fact base is:
isObject('rocket','ACTIVE','NIL').
isObject('pilot','ACTIVE','rocket').
isProvided('start','OPERATION','rocket').
isProvided('start_engine','OPERATION','pilot').
isImplementedBy('start','OPERATION','rocket',
'start_engine','pilot',1).
Figure 3 provides a typical example of a design rule that is defined by the HOOD
Reference Manual and must be verified to ensure that the model shown in Fig. 2 is
correct.
This can easily be translated into a Prolog rule where model elements can be directly
referenced using the facts described above.
Logic Model Processing 263
errI6(X,U) :- isProvided(U,'OPERATION',X),
not(isImplementedBy(U,'OPERATION',X,_,_)).
/* utility rules */
isTerm(Y) :- hoodObject(Y,_,_), not(hoodObject(_,_,Y)).
isNotTerm(Y) :- hoodObject(Y,_,_), isParent(Y).
isParent(Y) :- hoodObject(_,_,Y), !.
A similar approach was followed to implement the complete set of HOOD rules,
as well as other advanced model processing features such as an automatic Ada code
generator and various documentation generators. This solution has been included into
the Stood [2] product and distributed to major aerospace industrial companies over
several decades. It is still actively supported and maintained.
2 Overview of LMP
The input modeling languages that have been addressed are either token-based lan-
guages (i.e., defined by a textual grammar), such as software programming languages
(Ada, C,…) or software architecture description languages (HOOD, AADL [4],…), or
tag-based languages (i.e., defined by an XML structure), such as UML [5], SysML [6]
and Domain Specific Languages (DSL).
The processing goals that have been developed until now, cover a large spectrum of
model verification and exploitation tools in a homogeneous way, whereas different ad-
hoc technologies are usually required with alternate implementations (e.g., Object Con-
straint Language [7], Atlas Transformation Language [8], Query View Transformation
[9], Acceleo [10], …). Typical LMP processing goals are:
• Model explorations or queries.
• Model verifications or rule checkers.
• Model transformations into other models, source code or documentation.
Two options are possible to convert an input model into the corresponding fact base.
If the input model has been serialized and stored into a file, a parser tool is required to
perform a syntactic translation between the original syntax into a list of Prolog facts. On
the other hand, if the model is stored in a data structure within a CASE tool, the best
solution consists in developing a Prolog facts generator.
3 Examples of Application
3.1 HOOD Processing Tools
Stood is a software design tool supporting the HOOD methodology that was presented
in Sect. 1. It intensively uses LMP to implement the design rule checkers, the various
code and documentation generators as well as a few reverse engineering tools. It has
Logic Model Processing 265
been deployed on many projects, and especially to support the development of embedded
software for the Airbus family of civilian aircraft (A340, A350, A380). Table 1 gives the
list of the LMP applications that have been customized or specifically created by Airbus
to support their software development process [13]. Moreover, as such software requires
proper certification by aviation authorities like DO 178 recommendations [11], some of
these tools have been qualified. The good characteristics of the Prolog language added
to the flexibility of the LMP framework made these realizations possible.
The more generic LMP tools that are included in the off-the-shelf “Stood for HOOD”
distribution have been used by many other industrial programs, such as military heli-
copters, driverless metro, railways signaling and a collection of spacecrafts. In particular,
they were used for the development of the control software embedded into the European
Robotic Arm that is docked at the International Space Station.
The Prolog rules can thus be embedded inside the model to be processed and poten-
tially modified by the designer at any time. This variant of LMP is called LAMP
[15] (Logical AADL Model Processing) and allows for Prolog code to be directly
inserted inside AADL as annexes and processing goals to be executed as Prolog queries.
LAMP comes with a library of reusable Prolog model exploration, verification, and
transformation rules that helps the user to set-up complete assurance case solutions.
4 Related Work
This section presents three related projects mentioning the use of the Prolog language
in the context of Model Driven Engineering. Other related work could be found in the
area of representing and processing ontologies with Prolog. This has not been explored
here.
4.1 D-MILS
The D-MILS European FP7 project [16], ended in October 2015, was focused on extend-
ing MILS (Multiple Independent Levels of Security) for use with distributed systems.
The similarities with LMP are that a) it addressed multi-domain assurance cases of dis-
tributed real-time architectures using a specific variant of the AADL language (MILS-
AADL) as input and b) one of its processing tools, the Configuration Compiler, was
written in Prolog and used the SWI Prolog constraint logic programming feature.
Logic Model Processing 267
However, D-MILS is much more specific than LMP due to the use of a non-
standard modelling language and requires the set-up of a complex tool chain to operate.
In particular, the Prolog facts representing the input model are not directly derived
from the MILS-AADL description but from the output of another intermediate model
transformation.
4.2 PTL
Prolog based Transformation Language (PTL) [17] addresses the development of model
transformations in a Model Driven Engineering context using the Prolog language. It is
not specific to any application domain but relies on an existing transformation language
(Atlas Transformation Language: ATL) and a particular modeling environment (Eclipse
Modeling Framework: EMF).
Compared to LMP, the scope of PTL appears to be much more restrictive, as it only
targets model transformations and not the other kinds of model processing. Moreover,
it is presented as a hybrid approach which mixes Prolog and ATL languages, whereas
everything is expressed in Prolog with LMP. Finally, the need for PTL to operate within
the Eclipse platform brings another limitation that LMP does not have.
in mind, there is no way to confirm that the software has been properly installed and
used. Moreover, the same user may have downloaded the products several times or with
different identifiers. On the other hand, a single download may sometimes correspond
to multiple users, like a full college lab class.
For the Stood product, the total number of free downloads during the last 17 years
reaches 2500 units. Its repartition over time is shown on Fig. 5.
AADL Inspector is a more recent product for which the free download metrics are
more precise and include an indication of the user location. Over the period 2011–2022,
more than 900 individual downloads of the product have been tracked. Figure 6 provides
more details about the location and date of these downloads.
In summary, we can conclude that the LMP technology has been disseminated
indirectly to more than 3000 people over the world during the two last decades.
Logic Model Processing 269
6 Conclusion
Taking advantage of the intrinsic good properties of the Prolog language and aug-
mented by a well-defined implementation approach, the benefits of the LMP solution
are multiple:
• Generic solution for various model processing functions.
• Standard Prolog language (ISO/IEC 13211–1).
• Independent: compatible with all meta-modelling formats.
• Interpreted: supports both off-line and online processing (e.g., LAMP).
• Declarative: fits well with multi-steps incremental development processes.
• Modular: multiple separate fact and rule bases.
• Explainable: appropriate for tool qualification.
• Flexible:
References
1. HOOD: Hierarchical Object Oriented Design. http://www.esa.int/TEC/Software_engine
ering_and_standardisation/TECKLAUXBQE_0.html
2. Stood. http://www.ellidiss.com/products/stood/
3. Sbprolog. Stony Brook Prolog. https://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/
prolog/impl/prolog/sbprolog/0.html
4. AADL: SAE AS-5506D, Architecture Analysis and Design Language. https://www.sae.org/
standards/content/as5506d/
5. UML: Unified Modeling Language. http://uml.org/
6. SysML: Systems Modeling Language. http://sysml.org/
7. OCL: Object Constraint language. http://www.omg.org/spec/OCL/
8. ATL: Atlas Transformation Language. http://www.eclipse.org/atl/
9. QVT: Query View Transformation. http://www.omg.org/spec/QVT/
10. Acceleo. https://www.eclipse.org/acceleo/
11. DO178. https://www.rtca.org/?s=DO+178
12. AADL Inspector. http://www.ellidiss.com/products/aadl-inspector/
13. Dissaux, P., Farail, P.: Model verification: return of experience. In: ERTS Conference
Proceedings (2014)
270 P. Dissaux
14. Dissaux, P., Hall, B.: Merging and processing heterogeneous models. In ERTS Conference
Proceedings (2016)
15. Dissaux, P.: LAMP: a new model processing language for AADL. In: ERTS Conference
Proceedings (2020)
16. D-MILS project. http://www.d-mils.org/page/related-links
17. Almendros-Jimenez, J., et al.: PTL: a model transformation language based on logic
programming. J. Logical Algebraic Meth. Program. 85, 332–366 (2016)
18. Rubini, S., et al.: Specification of schedulability assumptions to leverage multiprocessor
Analysis. J. Syst. Archit. 133 (2022)
19. Warren, D.S., et al.: Introduction to Prolog. In:Warren, D.S., Dahl, V., Eiter, T., Hermenegildo,
M., Kowalski, R., Rossi, F. (eds.) Prolog: 50 Years of Future, LNAI 13900, pp. 3–19. Springer,
Cham (2023)
Symbium: Using Logic Programming
to Streamline Citizen-to-Government
Interactions
1 Introduction
A recent study on replacement of home appliances (e.g., refrigerators, dishwash-
ers, air conditioners etc.) in the United States suggests that billions of hours are
lost each year (7.5 billion hours in 2021) [2,10] due to inefficiency in permit-
ting processes. More than 40% of available rebates or refunds for energy-efficient
appliances go unclaimed, $350M in 2021 [7,17], due to the complexity of regu-
lations and the associated rebate processes. Problems in securing permits and
inspections for appliance replacements frequently cause significant delays in com-
pleting projects (in many cases more than 100 days). And the numbers are much
larger when one looks beyond appliances to other types of home improvement.
Citizen’s Dashboard. Symbium’s solution to this problem is a service called
the Citizen’s Dashboard [9,15] - a web-based service that facilitates interactions
between citizens (e.g., homeowners, architects, and contractors), governmental
agencies (e.g., municipalities, counties, states) and other organizations (e.g., util-
ity providers, insurance providers). The initial focus of the service is residential
property(property1)
property.zone(property1,"RH-1(D)")
project(project1)
project.property(project1,property1)
project.new_unit(project1,new_unit1)
project.new_unit.size(project1,new_unit1,800)
violation(Property,size) :-
project.property(Project,Property) &
Symbium’s Citizen’s Dashboard 273
property.zone(Property,"RH-1(D)") &
project.new_unit.size(Project,Unit,NewUnitSize) &
greater_than(NewUnitSize,900)
message(Jurisdiction,code_violation(Property)) : :
project.property(Project,Property) &
property.jurisdiction(Property,Jurisdiction) &
project.submittable(Project)
==> ¬project.submittable(Project)
One of the main challenges which Symbium faces in the building and main-
tenance of the Citizen’s Dashboard is the large number of rules which affect
residential construction projects. San Francisco’s zoning code, for instance, has
thousands of regulations and is updated up to six times per month. Further-
more, the regulations applicable to a project may stem from multiple regulatory
274 T. Krueger et al.
bodies, e.g., environmental designations, state law, and national restrictions, and
the interactions between these regulations may increase the combinatorial com-
plexity of a project’s analysis. Symbium uses Logic Programming to solve the
above challenges in three ways.
1. Domain experts with no programming experience are trained to assist in the
writing and editing of logic programs. This onboarding process is aided by
the conciseness and readability of Epilog code.
2. Symbium takes advantage of the composability of rules by dividing large
rulesets into meaningful blocks and combining rules from overlapping sources.
3. The flexibility of Logic Programming allows Symbium to reuse regulations.
Take, for instance, the example relation provide above, which allows users
to ask questions such as “Can I build a 900 sqft cottage on my property?”
A slight variation of this same rule could, instead, empower users to answer
questions such as “What size of cottage can I build on my property?” or, even,
“In what zones can I build a 900 sqft cottage?”
Scaling Logic Programs Across Cities. Although zoning and building reg-
ulations vary from city to city, they share a common ontology, with simi-
lar regulations. Rather than authoring a new logic program for every city,
the Symbium team develops a ruleset which captures the regulations that
broadly apply to every city, with tunable relations to characterize the dif-
ference in regulations from city to city. In the following rule, the relation
jurisdiction.zone.max_size characterizes the maximum cottage size regu-
lations of a city.
project.noncompliant(Project) :-
project.property(Project,Property) &
property.jurisdiction(Property,Jurisdiction) &
property.zone(Property,Zone) &
project.new_unit.size(Project,Unit,NewUnitSize) &
jurisdiction.zone.max_size(Jurisdiction,Zone,MaxSize) &
greater_than(NewUnitSize,MaxSize)
To complete the logic program for a city, the above ruleset is supplemented
with Epilog facts and rules to encode the city-specific zoning and building code
regulations.
jurisdiction.zone.max_size("San Francisco","RH-1(D)",900)
jurisdiction.zone.max_size("Oakland","R1",1200)
The above authoring process enables the Symbium team to efficiently build
and scale logic programs.
This service has received favorable reviews from multiple journalists, with
articles appearing in Forbes [1], Government Technology [9], Builder Online [11],
and so forth. The company is also the recipient of multiple awards. In 2019,
Symbium was named a Hive 50 honoree [6]. In 2020, it won the prestigious
Ivory Prize for Housing Affordability [12]. In 2021, it received the American Bar
Association Women of Legal Tech award [13]. And, in both 2021 and 2022, it
was listed as a GovTech 100 company [16].
In the long term, the company aspires to apply the Citizen’s Dashboard to
other areas of regulatory compliance, such as property taxes, licenses, interstate
commerce, and so forth. The Citizen’s Dashboard is a technology that can facil-
itate many types of interactions between citizens and government agencies, all
part of a Government Relationship Management (GRM) [14].
References
1. Castenson, J.: Platform digitizes painful planning process to provide greater access
to affordable housing. https://www.forbes.com/sites/jennifercastenson/2022/03/
21/platform-digitizes-painful-planning-process-to-provide-greater-access-to-
affordable-housing/?sh=361fcaff171e (2022)
2. City of Oakland: average permit processing turnaround times. https://www.
oaklandca.gov/resources/average-permit-processing-turnaround-times (2021)
3. Genesereth, M.R.: Dynamic logic programming. Tech. rep., Stanford University
(2022). http://logicprogramming.stanford.edu/miscellaneous/dlp.html
276 T. Krueger et al.
4. Genesereth, M.R.: Dynamic logic programming. In: Warren, D.S., Dahl, V., Eiter,
T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years.
No. 13900 in LNCS, Springer (2023)
5. Genesereth, M.R., Chaudhri, V.: Introduction to logic programming. Synthesis
Lectures on Artificial Intelligence and Machine Learning, Morgan & Claypool Pub-
lishers (2020). https://doi.org/10.2200/S00966ED1V01Y201911AIM044
6. McManus, J.: Symbium: the permitter. https://www.builderonline.com/
recognition/symbium_o (Nov 2019)
7. Rewiring-America: High-Efficiency Electric Home Rebate Act (HEEHRA).
https://www.rewiringamerica.org/policy/high-efficiency-electric-home-rebate-act
(2022)
8. Sergot, M.J., Sadri, F., Kowalski, R.A., Kriwaczek, F., Hammond, P., Cory, H.T.:
The british nationality act as a logic program. Commun. ACM 29(5), 370–386
(1986). https://doi.org/10.1145/5689.5920
9. Staff, N.: Symbium creates property info lookup portal for California. https://www.
govtech.com/biz/symbium-creates-property-info-lookup-portal-for-california
(2021)
10. Statista: Household Appliances - United States. https://www.statista.com/
outlook/cmo/household-appliances/united-states (2021)
11. Strong, S.: Tech Innovator Symbium launches new property and Permit Informa-
tion Portal. https://www.builderonline.com/design/technology/tech-innovator-
symbium-launches-new-property-and-permit-information-portal_o (2021)
12. Symbium: Symbium wins the ivory prize for housing affordability. https://
symbium.com/press/symbium-wins-the-ivory-prize-for-housing-affordability
(2020)
13. Symbium: Symbium CEO and co-founder Leila Banijamali honored with ABA
LTRC. https://symbium.com/press/leila-banijamali-nominated-for-2021-women-
of-legal-tech-award (2021)
14. Symbium: Symbium’s vision of Government Relationship Management. https://
symbium.com/blog/symbiums-vision-of-government-relationship-management
(2021)
15. Symbium: How Complaw will revolutionize the public’s experience of property
data for cities and counties. https://symbium.com/blog/how-complaw-will-
revolutionize-the-publics-experience-of-property-data-for-cities-and-counties
(2022)
16. Symbium: Symbium is named a GovTech 100 company for the second consec-
utive year. https://symbium.com/press/symbium-is-named-a-2022-govtech-100-
company-for-the-second-consecutive-year (2022)
17. Todorova, A.: Goodbye, Mail-In Rebates. https://www.wsj.com/articles/
SB115663801471546598 (2006)
PROLEG: Practical Legal
Reasoning System
Ken Satoh(B)
1 Background
After many years of theoretical research on logic programming and nonmotonic
reasoning, I sought practical applications of my theoretical research and I entered
law school in 2006 and learned “Japanese presupposed ultimate fact theory” (we
write “JUF theory” for short in this paper) which was developed in lawyers
training center in Japan at the law school. This theory is to help judges to make
reasonable conclusions even under incomplete information environment due to
lack of evidence. I immediately understood the aim of this theory is exactly same
as nonmonotonic reasoning and am sure that I can implement the reasoning in
JUF theory [10].
My understanding of JUF theory is as follows:
In a litigation, the truth values of some facts which contribute to the judgement
might be unknown due to the sufficient evidence. Then, from the deductive
reasoning, the correct logical condition for the judgment is “unknown” as well.
However, judges are not allowed to give such unknown judgement but have to
give decisive answer. To solve this problem, JUF theory attach a default truth
value to every condition in Japanese Civil Code and let judges use the default
value when the condition is unknown in the litigation. Then all the conditions are
determined by real truth values or default truth values and therefore conclude
the decisive judgement. Actually, an attached default value is closely related
with the burden of proof. Since if the default value is favorable to the plaintiff
(the defendant, respectively), the defendant (the plaintiff, respectively) must
prove the negation of the default value otherwise the defendant (the plaintiff,
respectively) lose the litigation.
c The Author(s), under exclusive license to Springer Nature Switzerland AG 2023
D. S. Warren et al. (Eds.): Prolog: The Next 50 Years, LNAI 13900, pp. 277–283, 2023.
https://doi.org/10.1007/978-3-031-35254-6_23
278 K. Satoh
2 PROLEG
We firstly started to write legal rules in PROLOG [10] reflecting burden of proof
in a similar way to the British Nationality Act in PROLOG [13]. However, we
found that lawyers have difficulty to understand negation as failure in PROLOG
so we changed the syntax from negation as failure into a framework of general
rules and exceptions which is a common reasoning pattern among lawyers to
create PROLEG (PROlog based LEGal reasoning support system) [7].
Our main aim is to make lawyers to use legal reasoning system by providing
a minimum legal language sufficient for the reasoning so that lawyers understand
the behavior of the system. Our approach is quite opposite with academic trends
in AI and law in that researchers introduce many subtlty to express detailed
deontic modality. As far as pratical legal system is concerned, however, the legal
systems which usual AI and Law researchers provide is too complicated for
lawyers who do not have a background of logic and thus the lawyers do not use
them.
Although we have not conducted any psychological experiments, we had expe-
riences on PROLEG with law school graduates who write PROLEG solution for
Japanese bar exams for each year from 2009 to 2022 (total more than 60 grad-
uates) in that they can start to make a program in PROLEG after a few weeks
training of programming in PROLEG. I believe that PROLEG is the most famil-
iar legal knowledge representation language for lawyers and has a potential to
be a de fact standard.
Now, we introduce PROLEG. PROLEG system consists of a rulebase and a
fact base.
– A PROLEG rulebase consists of the following expression.
• A rule of the form of Horn clauses (without negation as failure):
exception(H, E).
f act(P ).
Then, we can traslate the above PROLEG program into the following pro-
gram:
C : −B11 , ..., B1n1 , not E1 , ..., not Em .
C: −B21 , ..., B2n2 , not E1 , ..., not Em .
..
.
C: −Bk1 , ..., Bknk , not E1 , ..., not Em .
Note that rules with the same head has the same negative literals. If we add
some facts in PROLEG and PROLOG, we can show that derived literals are
equivalent.
On the other hand, suppose that we have the following PROLOG program:
C: −B11 , ..., B1n1 , not E11 , ..., not E1m1 .
C: −B21 , ..., B2n2 , not E21 , ..., not E2m1 .
..
.
C: −Bk1 , ..., Bknk , not Ek1 , ..., not Ekmk .
Then, we can translate a PROLOG program into the following PROLEG pro-
gram using additional predicate Ci .
C ⇐ C1 . C1 ⇐ B11 , ..., B1n1 .
C ⇐ C2 . C2 ⇐ B21 , ..., B2n2 .
..
.
C ⇐ Ck . Ck ⇐ Bk1 , ..., Bknk .
exception(C1 , E11 ). ··· exception(C1 , E1m1 ).
280 K. Satoh
right_to_ask_payment(Seller,Buyer,Object,Price)<=
purchase_contract_establishment( Seller,Buyer,Object,Price).
% A seller has a right to force a buyer to make an payment
% over the object if a purchase contract is established.
purchase_contract_establishment(Seller,Buyer,Object,Price)<=
purchase_agreement(Seller,Buyer,Object,Price).
% A purchase contract is established if there is an agreement
% of purchase of the object.
exception(right_to_ask_payment(Seller,Buyer,Object,Price),
payment(Buyer,Seller,Object,Price)).
% There is an exception about sellerÂĄfs right to ask payment
% if buyer made payment.
payment(Buyer,Seller,Object,Price)<=
payment_fact(Buyer,Seller,Object,Price).
% Payment is made if there is a fact of payment.
fact(payment_fact(alice,bob,television,1000 euro)).
% Alice paid 1000 euro to Bob for television.
PROLEG provides an explanation of the reasoning process to a judgement
using a block diagram. We show a block diagram for the above case in Fig. 1.
The explanation of block diagram is as follows:
– Right-handside top-most block expresses a judgement.
PROLEG: Practical Legal Reasoning System 281
extracts necessary information for an input to PROLEG so that a lay user can
find out an outcome of his/her problem by PROLEG block diagram [2–4].
There are various directions for extension. Since PROLEG is a framework to
write legal rules in terms of general rules and exceptions so we could formalize
statutory laws in general [9]. We have investigated an application to criminal
law [5], GDPR [12] and an application to Private International Law [8].
4 Conclusion
We have described our activities of logic programming paradigm in legal domain
and explained our legal knowledge representation language PROLEG, which
has a lot of potential for supporting various legal activities. We strongly believe
that PROLEG would be one of the prominent practical application in logic
programming.
References
1. Fungwacharakorn, W., Tsushima, K., Satoh, K.: On the legal debugging in PRO-
LEG program. In: Advances in Intelligent Systems and Computing, vol. 1357, pp.
25–36 (2021). https://doi.org/10.1007/978-3-030-73113-7_3
2. Navas-Loro, M., Satoh, K., Rodríguez-Doncel, V.: ContractFrames: bridging the
gap between natural language and logics in contract law. In: Kojima, K., Sakamoto,
M., Mineshima, K., Satoh, K. (eds.) JSAI-isAI 2018. LNCS (LNAI), vol. 11717, pp.
101–114. Springer, Cham (2019). https://doi.org/10.1007/978-3-030-31605-1_9
3. Nguyen, H.T., Fungwacharakorn, W., Nishino, F., Satoh, K.: A multi-step approach
in translating natural language into logical formulas. In: Proceedings of the 35th
International Conference on Legal Knowledge and Information Systems (JURIX
2022), pp. 103–112 (2022). https://doi.org/10.3233/FAIA220453
4. Nguyen, H.T., Nishino, F., Fujita, M., Satoh, K.: An interactive natural language
interface for PROLEG. In: Proceedings of the 35th International Conference on
Legal Knowledge and Information Systems (JURIX 2022), pp. 294–297 (2022).
https://doi.org/10.3233/faia220484
5. Nishigai, Y., Satoh, K.: Programming of “Japanese presupposed ultimate fact the-
ory” in criminal law using PROLEG (in Japanese). Inf. Network Law Rev. 19,
81–120 (2021). https://doi.org/10.34374/inlaw.19.0_81
6. Nishioka, S., Satoh, K., Mori, Y.: Consumer dispute resolution system based on
PROLEG. In: Proceedings of the 35th International Conference on Legal Knowl-
edge and Information Systems (JURIX 2022), pp. 298–301 (2022). https://doi.org/
10.3233/FAIA220485
7. Satoh, K., et al.: PROLEG: an implementation of the presupposed ultimate fact
theory of Japanese civil code by PROLOG technology. In: Onada, T., Bekki,
D., McCready, E. (eds.) JSAI-isAI 2010. LNCS (LNAI), vol. 6797, pp. 153–164.
Springer, Heidelberg (2011). https://doi.org/10.1007/978-3-642-25655-4_14
PROLEG: Practical Legal Reasoning System 283
Abstract. In this paper we present the key features of Logical English as syntactic
sugar for logic programming languages such as pure Prolog, ASP and s(CASP);
and we highlight two application areas, coding legal rules, and teaching logic as
a computer language for children.
1 Introduction
Logical English (LE) [6–11, 15] exploits the unique feature of Prolog-like logic pro-
gramming (LP), that LP is the only programming paradigm based on the use of logic
for human thinking and communication. By exploiting this feature, LE becomes a wide-
spectrum computer language, which can be understood with only a reading knowledge
of English and without any technical training in computing, mathematics or logic.
LE is not only a Turing-complete computer programming language. It has the poten-
tial to represent and reason with a broad range of human knowledge, as shown by its
ability to codify the language of law. In an educational setting, it can be used to intro-
duce both computational and logical thinking across the whole range of subjects taught
in school, bridging STEM and non-STEM subjects alike.
Basic Syntax. LE differs from pure Prolog primarily in the syntax for atomic predicates.
In LE, predicates and their arguments are declared by means of templates, as in:
where asterisks delimit the argument places of the predicate. In the simplest case, an
argument place can be filled by a constant or a variable. For example:
A variable is a noun phrase ending with a common noun, such as “person” or “thing”
and starting with a determiner such as “a”, “an” or “the”. The indefinite determiner, “a”
or “an”, introduces the first occurrence of a variable in a sentence. The same noun phrase
with the indefinite determiner replaced the definite determiner, “the”, represents all later
occurrences of the same variable in the same sentence. Any other string of words in
the position of an argument place is a constant. Unlike in Prolog, upper and lower case
letters have no significance. Here is another example:
These examples illustrate some of the following characteristics of the basic syntax
of LE, which are inherited from LP:
• Sentences in LE have the form of facts or rules. Facts are atomic sentences, whereas
rules are sentences of the form conclusion if conditions, where the conclusion is an
atomic sentence and the conditions are a combination of atomic sentences, typically
connected by and.
• All variables are implicitly universally quantified with their scope being the sen-
tence in which they occur. This means that variables in different sentences have no
relationship with one another, even if they have the same name.
• The basic version of LE is untyped, like Prolog, and variable names are purely
mnemonic. So, the first example sentence above has the same translation into Prolog
as the meaningless sentence Alice likes a hat if the hat likes logic. We are developing
an extended version of LE in which types are represented by common nouns, and the
arguments of predicates are checked for compatibility with types that are declared in
the templates.
• LE is designed so that sentences in LE have a unique translation into pure Prolog. But
LE is also designed to be as unambiguous as possible, when understood by a human
reader. For this purpose, LE deliberately eliminates the use of pronouns, which are a
major source of ambiguity, as in the sentence A person is a parent of an other person
if she is the mother of her.
• The current, basic syntax of LE does not include relative clauses, as in Alice likes
anyone who likes logic. This is another deliberate choice, because relative clauses
are another source of ambiguity. For example, the relative clause which breathe fire
Logical English for Law and Education 289
is ambiguous in the sentence All dragons which breathe fire are scary. The relative
clause can be understood restrictively as meaning that a dragon is scary if the dragon
breathes fire. Or it can be understood non-restrictively, as meaning that, not only are
all dragons scary, but they also breathe fire.
Logically, restrictive relative clauses add extra conditions to a sentence, whereas non-
restrictive relative clauses add extra conclusions to the sentence. There are syntactic
conventions for distinguishing between restrictive and non-restrictive relative clauses
(such as the use of commas), but not everyone uses them correctly and consistently, and
they differ between American and British English.
Fig. 1. An LE program together with alternative scenarios and queries, displayed in a VS Code
editor. The editor provides syntax highlighting, predictive text, and a simple form of static type
checking. https://le.logicalcontracts.com/p/unvaccinated.pl.
290 R. Kowalski et al.
'An_arrest_warrant_is_issued_for'(A) :-
has_an_obligation_that(A, pays(A, B)), not pays(A, B).
Notice that the sentence expresses the deontic character of an obligation by representing
the less-than-ideal consequence of violating the obligation.
Scenarios and Queries. Figure 1 also includes a number of scenarios and queries,
which can be combined and posed to the system, as shown in Fig. 2.
In the combination of query one and scenario one, Novak is obligated to pay £100,
but Boris is not, because, although both have attended a prohibited party (thanks to
Novak), only Novak has been notified of the prohibition.
Logical English for Law and Education 291
In the combination of query one and scenario two, Boris is obligated to pay £100,
but Novak is not, because this time it is Boris, rather than Novak, who is notified of the
prohibition.
In the combination of query two with scenario one, no arrest warrant is issued,
because Novak, the only person obligated to pay £100, pays the required amount.
In the combination of query two with scenario two, Boris is issued an arrest warrant,
because he pays an incorrect amount. An explanation for issuing the arrest warrant to
Boris is displayed in Fig. 3.
Fig. 2. A log of combined queries and scenarios together with their answers.
Fig. 3. An explanation for the answer to query two with scenario two.
292 R. Kowalski et al.
We have been using LE to explore the representation of a wide range of legal texts,
helping to identify ambiguities, explore alternative representations of the same text,
and compare the logical consequences of the alternatives. The texts include portions
of loan agreements, accountancy law, Italian citizenship, EU law on criminal rights,
International Swaps and Derivative contracts, and insurance contracts.
The Italian Citizenship Example. We are also developing analogues of LE for other
natural languages, such as Italian and Spanish. Figure 4 shows both an LE and a Logical
Italian (LI) representation of Article 1 of Act No. 91 of 5 February 1992:
Both representations in Fig. 4 were generated manually. In contrast with the manually
generated LE representation in Fig. 4, google translate gives the following translation
of the original Italian text into English:
Both the Italian text and its English translation are ambiguous: In particular, both
the English condition “the child does not follow the citizenship of the parents according
to the law of the state to which these belong” and its Italian counterpart, taken literally,
seem to cover only the case where both parents have the same citizenship. Moreover,
both the Italian “ovvero se” and the corresponding English “or if” seem to relate to a
separate alternative from the alternatives that precede it. These readings of the natural
language texts leave uncovered such deserving cases as the child having one parent who
is stateless or unknown, and another parent who cannot pass on its citizenship to its
child. It seems doubtful that these omissions would have been intended by the law.
The LE and LI representations in Fig. 4 incorporate only one interpretation of Article
1.1. Of course, other interpretations are possible, and they could also be represented in
LE and LI. For comparison, see the similar case of children found abandoned in the UK,
covered by the 1981 British Nationality Act, as formulated both in the original English
and in an earlier, unimplemented variant of LE [7].
Figure 4 illustrates several features of LE that were not demonstrated earlier:
• LE uses indentation, rather than brackets, to represent the relative strength of binding
of the logical connectives and and or.
• Variables can be given symbolic names, such as A and B in this example.
Logical English for Law and Education 293
• Conditions can have the form for all cases in which conditions it is the case that
conclusion, which are translated into forall(conditions, conclusion).
In Fig. 4, the possibility that a parent is unknown is expressed positively (as a kind
of “strong” negation), to reflect the wording of the original legal text. Alternatively, the
same possibility could be expressed using negation as failure, to conclude that a parent
of a person is unknown if there is no information about the parent. In fact, with the
representation in Fig. 4, it is possible to know that a person is born in Italy, but not
to know who the parents are. In such a case, the for-all condition would be satisfied
vacuously, and the person would be an Italian citizen by default.
of English or Spanish. Figure 6 shows all answers to the LE query which set is a subset
of which other set, first with the scenario named facts, and then with the scenario named
lists.
The subset example illustrates several features that have not been seen earlier:
• Because in the current version of LE variable names are purely mnemonic, the con-
ditions that A and B are sets, on lines 13 and 14, need to be stated explicitly. These
conditions would not be necessary if common nouns were treated as types. We plan
to extend LE to include such types in the near future.
• The notion of set in lines 12–18 is an abstract notion, which is neutral with respect
to how sets are represented concretely. Scenarios one and two employ different con-
crete representations. Scenario sets represents sets by facts that define the belongs to
relation explicitly. Scenario lists represents sets by Prolog-style lists, and the belongs
to relation is defined in terms of the is in relation, which is LE syntax for the Prolog
member predicate. In both scenarios, there are only two sets. In both scenarios, there
is no empty set.
want to read programs, to convince themselves that the programs meet their requirements.
Some may want to understand explanations for answers to queries, and they may want
to modify assumptions to obtain better answers. But hardly anyone will need to write
programs themselves from scratch.
Focussing on teaching students how to write computer programs also overlooks the
fact that learning to write well in any language, whether it be a natural language or a
computer language, is much harder than learning to read. In this respect, LE has an
advantage over other computer languages, because it can exploit a much wider range of
examples requiring only a reading knowledge of natural language.
How to be a Happy Dragon. By focusing on reading rather than writing, examples
of programming language constructs that would ordinarily be considered too difficult
to teach at an introductory level can be included from the very beginning. Figure 7
illustrates such an example. Here the first sentence uses recursion, the second uses
Fig. 7. An LE program for introducing young children to logic and computing. https://le.logica
lcontracts.com/p/happy_dragon.pl
296 R. Kowalski et al.
negation as failure, and the third uses universal quantification, achieving the same effect
as iteration, while-loops or recursion in conventional programming languages.
Although this style of English may seem artificial, it can be made more natural, while
remaining unambiguous, by treating common nouns as types. For example, the sentence
on lines 13–16 could be written more naturally and more simply as:
All the examples we have seen until now can be understood without any knowledge
about how LE is executed. Moreover, that understanding can be enhanced by experi-
menting with different scenarios and queries, and by exploring the logical consequences.
In this example, a student can learn that alice is happy, because her only child, bob, is
healthy; bob is healthy because he does not smoke; and bob does not smoke, because
his parent alice does not smoke. It might be harder to convince a student that bob is a
happy dragon too. But at least it shows that Logic and Computing can be introduced
to children at an early age without having to use examples, such as controlling a robot
or manipulating images on a screen, which can be implemented just as well, or maybe
even better, in an imperative programming language.
The Euclidean Algorithm. As a computer language, LE combines in one language the
features of a programming language, database language, and knowledge representation
and problem-solving language. All the examples we have seen so far are examples of
its use for knowledge representation and problem solving. The representation in Fig. 8
of the Euclidean algorithm for computing the greatest common divisor (gcd) of two
numbers illustrates its use for programming. It uses the built-in Prolog predicates for
subtraction and for testing inequalities.
Notice that a query such as which number is the gcd of 1946 and which other number
cannot be answered, because the Prolog predicate for inequality can be used only when
the two numbers are both given as input. On the other hand, the same program can be
used both to test that a given number is the gcd of two other given numbers, as well as to
generate the gcd. This capability would need two separate programs in a conventional
imperative programming language.
On the other hand, the LE representation is not an algorithm. The Euclidean algo-
rithm is the behaviour obtained by using the LE representation to reason top-down (or
backward), as in Prolog. This behaviour can be described imperatively:
much easier to reason that the Euclidean algorithm correctly computes the gcd. As David
Warren points out [19], this can be done by using mathematical induction, exploiting
the fact that the bottom-up (inductive) interpretation of the program in Fig. 8 computes
the same gcd relation as the top-down (algorithmic) interpretation.
Notice that the specification of gcd, illustrated in Fig. 9, is also executable, although
it is much less efficient than the Euclidean algorithm.
in its current form, it is not entirely general-purpose. It lacks the ability of imperative
languages to represent an agent’s goals and the ability of an agent to satisfy goals by
executing actions in reaction to external events.
To remedy this disability, we developed the language LPS (Logic Production System)
[12–14] as an extension of LP. In fact, the earliest implementation of LE was for a smart
contract using the rock-paper-scissors game [2] written in LPS. We plan to extend LE to
include the reactive rules and causal laws of LPS. Other proposed extensions include a
more natural representation of rules and exceptions, following the approach of [16, 17],
as well as natural language analogues of object-oriented types and embedded functions
and relations as in Ciao [1, 5].
In the meanwhile, the current version of LE and its natural language cousins, such as
LI and LS, indicate the future potential of logic-based computer languages with a natural
language syntax. In this paper, we have highlighted legal applications and education as
two major areas in which the benefits of such languages can be exploited already today.
References
1. Casas, A., Cabeza, D., Hermenegildo, M.V.: A syntactic approach to combining functional
notation, lazy evaluation, and higher-order in LP systems. In: Hagiya, M., Wadler, P. (eds.)
FLOPS 2006. LNCS, vol. 3945, pp. 146–162. Springer, Heidelberg (2006). https://doi.org/
10.1007/11737414_11
2. Davila, J.: Rock-Paper-Scissors (2017). https://demo.logicalcontracts.com/p/rps-gets.pl
3. Fuchs, N.E., Schwitter, R.: Attempto controlled English (ACE). arXiv preprint cmp-
lg/9603003 (1996)
4. Guy, S.C., Schwitter, R.: The PENGASP system: architecture, language and authoring tool.
Lang. Resourc. Eval. 51, 67–92 (2017)
5. Hermenegildo, M., Morales, J., Lopez-Garcia P., Carro, M.: Types, modes and so much more –
the Prolog way. In: Warren, D., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi,
F. (eds.) Prolog - The Next 50 Years. LNAI, vol. 13900, pp. 23–37. Springer, Cham (2023)
6. Kowalski, R.: English as a logic programming language. N. Gener. Comput. 8(2), 91–93
(1990)
7. Kowalski, R.A.: Legislation as logic programs. In: Comyn, G., Fuchs, N. E., Ratcliffe, M.
J. (eds.) LPSS 1992. LNCS, vol. 636, pp. 203–230. Springer, Heidelberg (1992). https://doi.
org/10.1007/3-540-55930-2_15
8. Kowalski, R.: Logical English. In: Proceedings of Logic and Practice of Programming (LPOP)
(2020)
9. Kowalski, R., Datoo, A.: Logical English meets legal English for swaps and derivatives. Artif.
Intell. Law 30(2), 163–197 (2021). https://doi.org/10.1007/s10506-021-09295-3
10. Kowalski, R., Dávila, J., Calejo, M.: Logical English for legal applications. In: XAIF, Virtual
Workshop on Explainable AI in Finance (2021)
11. Kowalski, R., Dávila, J., Sartor, G., Calejo, M.: Logical English for law. In: Proceedings of
the Workshop on Methodologies for Translating Legal Norms into Formal Representations
(LN2FR), JURIX (2022)
12. Kowalski, R., Sadri, F.: Reactive computing as model generation. N. Gener. Comput. 33(1),
33–67 (2015). https://doi.org/10.1007/s00354-015-0103-z
13. Kowalski, R., Sadri, F.: Programming in logic without logic programming. Theory Pract.
Logic Program. 16(03), 269–295 (2016)
Logical English for Law and Education 299
14. Kowalski, R., Sadri, F., Calejo, M., Dávila, J.: Combining logic programming and imperative
programming in LPS. In: Warren, D., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R.,
Rossi, F. (eds.) Prolog - The Next 50 Years. LNAI, vol. 13900, pp. 210–223. Springer, Cham
(2023)
15. Sartor, G., Dávila, J., Billi, M., Contissa, G., Pisano, G., Kowalski, R.: Integration of logical
English and s(CASP). In: 2nd Workshop on Goal-directed Execution of Answer Set Programs
(GDE’22) (2022)
16. Satoh, K., et al.: PROLEG: an implementation of the presupposed ultimate fact theory of
Japanese civil code by PROLOG technology. In: Onada, T., Bekki, D., McCready, E. (eds.)
JSAI-isAI 2010. LNCS, vol. 6797, pp. 153–164. Springer, Heidelberg (2010). https://doi.org/
10.1007/978-3-642-25655-4_14
17. Satoh, K.: PROLEG: practical legal reasoning system. In: Warren, D., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years. LNAI, vol.
13900, pp. 277–283. Springer, Cham (2023)
18. Schwitter, R.: English as a formal specification language. In: Proceedings of 13th International
Workshop on Database and Expert Systems Applications, pp. 228–232. IEEE (2002)
19. Warren, D.S.: Writing correct prolog programs. In: Warren, D., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years. LNAI, vol.
13900, pp. 62–70. Springer, Cham (2023)
20. Wielemaker, J., Schrijvers, T., Triska, M., Lager, T.: SWI-Prolog. Theory Pract. Logic
Program. 12(1–2), 67–96 (2012)
Exploiting Logic Programming
for Runtime Verification: Current
and Future Perspectives
1 Introduction
multiagent system can observe messages exchanged among agents, and observes
the trace
invoice(20KgApples) okP ay(20KgApples)
S1 = < alice =⇒ bob, alice =⇒ bob >
invoice(G)
If M can recognize that the first event in the trace has type A =⇒ B, and
okP ay(G)
the second has type A =⇒ B, M can reason on S1 and on the expected
pattern P 1, and it can derive that Alice should have waited for Bob sending the
proof of payment for the apples, before sending an acknowledge to him. Hence,
M can warn either the agents or a (human) controller that a violation of the
commercial transaction rules took place.
This toy example includes all the main building blocks of Runtime Verifica-
tion, RV. RV [63] dynamically checks that event traces like S1 in our example,
generated by single runs of a System Under Scrutiny (SUS, the multiagent sys-
tem consisting of Alice and Bob in our example) are compliant with the formal
specification of its expected correct behavior, P 1 in our example. The external
observer M is named monitor: it is in charge for monitoring what is going on,
and for taking actions when needed. The formal specification P 1 corresponds to
the expected initial state of the SUS, and can be seen as the initial state of the
monitor. If the first observed event meets the first expected event, the monitor
moves to the successive expected state, that is
payP roof (G) okP ay(G)
P 1 = B =⇒ A : A =⇒ B :
The monitor behavior is driven by a transition relation involving representations
of states and observed events.
As we recently observed [15] “RV is complementary to other verification
methods: as formal verification, it is based on a specification formalism, but
scales well to real systems and complex properties, by forgoing exhaustiveness as
software testing”.
In order to be usable and useful, a RV system needs (i) a language for express-
ing the expected properties of the SUS that is powerful but easy to write and (ii)
an efficient and formally grounded mechanism to recognize violations of those
properties by the observed traces of events. In our example the mechanism is a
transition relation between states: if no transition is possible, a violation took
place.
We believe that Logic Programming (LP) is the right tool for addressing
both challenges above thanks to its ability to represent properties like P 1 in a
compact and understandable way, and to its declarative and operational reading.
In this paper we provide a short summary of our achievements in exploiting LP
for RV (Sect. 2), we analyse the state of the art (Sect. 3) and we draw some
perspectives on “what kinds of applications is Prolog most suited for” (Sect. 4).
(for example, Alice sends an invoice for 20 Kg of apples to Bob) and event type
(for example, one seller sends an invoice for some service or good to one buyer:
event types allow specifications to be more general, compact and readable). E
denotes the fixed universe of events subject to monitoring. An event trace over
E is a possibly infinite sequence of events in E, and a Trace Expression over E
denotes a set of event traces over E. Trace expressions are built on top of event
types (chosen from a set ET), each specifying a subset of events in E. A Trace
Expression τ ∈ T represents a set of possibly infinite event traces, and is defined
on top of the following operators:
– (empty trace, eps in Prolog notation), denoting the singleton set {} con-
taining the empty event trace .
– ϑ:τ (prefix, ET:T in Prolog notation), denoting the set of all traces whose first
event e matches the event type ϑ, and the remaining part is a trace of τ . For
example, the P 1 trace expression introduced in Sect. 1 contains prefix opera-
invoice(20KgApples) payP roof (20KgApples)
tors; the trace S2 = < alice =⇒ bob, bob =⇒
okP ay(20KgApples)
alice, alice =⇒ bob > is compliant with P 1 (it is a valid trace
invoice(20KgApples)
for the P 1 specification) because alice =⇒ bob matches the first
event type in P 1, and hence P 1 can rewrite into
payP roof (G) okP ay(G)
P 1 = B =⇒ A : A =⇒ B :
payP roof (20KgApples)
In a similar way, bob =⇒ alice matches the first event type in
P 1 , that rewrites in
okP ay(G)
P 1 = A =⇒ B :
and so on.
– τ1 ∨τ2 (union, T1\/T2 in Prolog notation), denoting the union of the traces
of τ1 and τ2 . For example,
invoice(G) payP roof (G)
P2 = A =⇒ B : B =⇒ A :
okP ay(G) invalidP ay(G)
((A =⇒ B : ) ∨ (A =⇒ B : ))
extends P 1 to cope with the case that the proof of payment is invalid: once
A receives it from B, it may either acknowledge the reception, meaning that
the payment was successful, or inform B that the payment was not valid.
invoice(20KgApples) payP roof (20KgApples)
S2 and S3 = < alice =⇒ bob, bob =⇒ alice,
invalidP ay(20KgApples)
alice =⇒ bob > are both valid traces for P 2. Instead,
invalidP ay(20KgApples)
S4 = < alice =⇒ bob > is not. In fact, the first allowed
invoice(G) invalidP ay(20KgApples)
message in P 2 must match A =⇒ B, and alice =⇒ bob
does not.
Exploiting LP for RV: Current and Future Perspectives 303
– τ1 ·τ2 (concatenation, T1*T2 in Prolog notation), denoting the set of all traces
obtained by concatenating the traces of τ1 with those of τ2 . For example,
assuming that P 3 specifies the correct interactions between a seller and a
wholesaler, P 4 = P 2 · P 3 models the pattern where, after concluding
an interaction with a buyer ruled by P 2, the seller can start interacting with
the wholesaler, according to the rules specified by P 3.
– τ1 |τ2 (shuffle, T1|T2 in Prolog notation), denoting the set obtained by shuf-
fling the traces of τ1 with the traces of τ2 . The fact that the seller can converse
at the same time with the buyer and with the wholesaler, freely interleaving
steps of the two conversations, can be modeled by P 5 = P 2 | P 3.
meaning that if the event E matches event type ET (body of the clause, namely
the part after the :- symbol2 ), a trace expression ET:T can be rewritten into
the trace expression T (head of the clause, where the first argument ET:T of the
delta predicate represents the trace expression, the second argument E is the
observed event, and the third argument T is the result of applying δ).
The clause
delta(eps,_,_) :- !, fail.
means that no transition is possible from the empty trace expression . This
rule prevents the Prolog interpreter from using other available definitions of
1
https://rmlatdibris.github.io/.
2
H :- B should be read as ‘if B holds, then H holds’.
304 D. Ancona et al.
delta (the ! ‘cut’ symbol in the body) and then forcing the failure of the
delta(eps, , ) goal (the built-in fail predicate).
Clauses
mean that if T1 can be rewritten into T1r upon observing E (body of the first
clause), then also the union T1\/T2 can (head of the first clause). This is true
also in case T2 can be rewritten into T2r, when T1\/T2 can be rewritten into
T2r (second clause). We should note here that if both T1 and T2 can be rewrit-
ten into a new trace expression upon observing E, nondeterminism occurs. The
actual implementation available in the RML repository is deterministic [12], and
forces the monitor to rewrite T1 and disregard the possibility to rewrite T2.
Clauses
deal with shuffle: if T1 can be rewritten into T1r, then T1|T2 can be rewritten in
T1r|T2 where, differently from union, T2 is kept in the rewritten trace expression
(first clause). Other clauses deal with concatenation and intersection3 .
closure_delta(T, []) :-
write(T), write(’\n no more events to consume’).
closure_delta(T, [Ev|Evs]) :-
delta(T, Ev, T1),
write(T), write(’ accepted ’), write(Ev), write(’ and moved on’),
closure_delta(T1, Evs).
3
The code of this simplified semantics is available from https://github.
com/VivianaMascardi/VivianaMascardi.github.io/blob/main/Software/
traceExprSimplifiedSemantics.pl.
4
In the real RV system, these events are generated by the SUS as a possibly infi-
nite stream; the example provided here aims at simulating how the monitor works,
assuming that events were previously logged and are hence a finite sequence.
5
In the first clause we should distinguish the case where the trace expression may halt
(for example, it is eps), which is fine, from the case where the trace expression expects
more events, which is instead a violation since the trace of events is the empty list
[]. The actual implementation in the RML repository provides a may halt predicate
to properly deal with these cases.
Exploiting LP for RV: Current and Future Perspectives 305
closure_delta(T, [Ev|_Evs]) :-
write(T),write(’ cannot accept ’),write(Ev),write(’ *FAILURE*’).
We provide the following definition for the match predicate, where 20 stands for
20KgApples
T = msg(A, B, invoice(G)) :
msg(B, A, payProof(G)):
((msg(A, B, okPay(G)) : eps) \/
(msg(A, B, invalidPay(G)) : eps)),
Evs = [msg(alice, bob, invoice(20)),
msg(bob, alice, payProof(20)),
msg(alice, bob, invalidPay(20))],
closure_delta(T, Evs).
msg(A,B,invoice(G)):
msg(B,A,payProof(G)):
( msg(A,B,okPay(G)):eps)\/
(msg(A,B,invalidPay(G)):eps)
consumed msg(alice,bob,invoice(20)) and moved on
msg(B,A,payProof(G)):
( msg(A,B,okPay(G)):eps)\/
(msg(A,B,invalidPay(G)):eps)
consumed msg(bob,alice,payProof(20)) and moved on
(msg(A,B,okPay(G)):eps)\/
(msg(A,B,invalidPay(G)):eps)
consumed msg(alice,bob,invalidPay(20)) and moved on
eps
no more events to consume
msg(A,B,invoice(G)):
msg(B,A,payProof(G)):
( msg(A,B,okPay(G)):eps)\/
(msg(A,B,invalidPay(G)):eps)
cannot accept msg(alice,bob,invalidPay(20)) *FAILURE*
invalidP ay(20KgApples)
because the trace expression P 2 does not expect alice =⇒ bob
as first exchanged message.
The actual code in the RML repository is much more complex: indeed, trace
expressions can support both parameters and recursion, and can model – in a
finite way – infinite traces of events. For example
ping(X) pong(X)
P 6 = A =⇒ B : B =⇒ A : P 6
specifies an infinite trace of ping and pong messages, with the same content,
exchanged by two agents A and B, while
ping(X) pong(X)
P 7 = A =⇒ B : B =⇒ A : (P 7 ∨ )
specifies all the traces that start with ping pong, and are followed by zero or
more – including infinite – repetitions of ping pong. The Prolog representations
are
P6 = msg(A, B, ping(X)):msg(A, B, pong(X)):P6
and
P7 = msg(A, B, ping(X)):msg(A, B, pong(X)):(P7\/eps)
respectively. 6 .
Our work on trace expressions started in 2012 and continued up to now
without interruptions. Our most recent achievements involve the challenging
problem of partial observability of events in the SUS, that we tackled both in a
centralized [14] and in a decentralized multiagent setting [13].
Although in this section we took a multiagent system as running example,
RV – and RML in particular – can be exploited to monitor any kind of system,
from robotics to Internet of Things.
6
In the code available from the RML repository, we re-implemented substitution to
manage the scope of a logical variable: hence, we can distinguish between the term
P6 where we want A, B, X to be unified with the same values forever, and P6’ where
we want them to remain the same in two consecutive ping pong events, but possibly
change in the next round. In the RML language we exploit the let keyword and curly
brackets to define the variable scope, as discussed in https://rmlatdibris.github.io/
rml.html, ‘Parametric specifications’. The Prolog representation of trace expressions
features the implementation of let. Given that terms can be cyclic, we used the
coinduction library of SWI Prolog [75], to recognize when two cyclic terms are
the same and manage them properly while applying substitutions, avoiding non-
termination.
Exploiting LP for RV: Current and Future Perspectives 307
Fig. 1. Distribution of papers over years (left) and co-authors of at least two papers
(right).
The distribution of papers over years is shown in Fig. 1 (left), while Fig. 1
(right) shows the authors that co-authored three or two papers among the
retrieved ones, excluding papers we were involved in. The words that occur
more often in the titles (resp. abstracts) are represented in Fig. 2 (resp. Fig. 3).
Besides RML and Trace Expressions, another work where computational logic
is used to monitor traces at runtime is EAGLE [17]. The work by Barringer
7
https://scholar.google.com/.
308 D. Ancona et al.
et al. was not returned by the search on Google Scholar but it is mentioned
by Havelund in [45], that we analyzed in depth. EAGLE is logic defined and
implemented by Barringer et al., it offers a succinct set of primitives supporting
recursive parameterized equations, with a minimal/maximal fix-point semantics
together with three temporal operators: next-time, previous-time, and concate-
nation. Prolog is definitely more powerful and expressive, but EAGLE received
a lot of attention, as it represents one of the first implemented logics for RV.
In [45], Havelund describes a rule-based RV system implemented in the
SCALA programming language8 . Although not using Prolog, his conclusions
hold for Prolog-based RV as well: “Rule-based systems seem natural for RV
and monitoring. From a specification notation point of view, rule-based systems
appear quite suitable for expressing the kind of properties the RV community
normally writes.”
Below we provide a critical analysis of two among those papers whose authors
are more active in the field according to Fig. 1. Their common feature is dis-
cussing how LP has been, or could be, exploited to cope with the problem of
partial information in the context of RV: the paper by Chesani et al. [27] deals
with the need of generating hypotheses, whereas the paper by Alberti et al. [3]
formalize the problem of whether it is possible to safely reduce the number of
necessary observations of the SUS.
Chesani et al. [27] presented the fundamentals of a reactive and logic-based
version of the Event Calculus [59] named REC for monitoring declarative proper-
ties, while maintaining a solid formal background. Monitoring calls for the ability
of carrying out ‘open’ reasoning, i.e., of reasoning upon the partial and incom-
plete information acquired during the execution, extending the inferred results
as new events occur. REC achieves the goal by taking advantage of the SCIFF
proof procedure [2], an extension of Fung and Kowalski’s IFF proof-procedure
for abductive LP [43]. In a paper building on top of [27], Bragaglia et al. [21]
studied a Prolog-based axiomatization of REC and discussed different runtime
monitoring application fields including Business Process Management, clinical
guidelines, and multiagent systems.
8
https://www.scala-lang.org/.
Exploiting LP for RV: Current and Future Perspectives 309
References
1. Alberti, M., et al.: Expressing and verifying business contracts with abductive logic
programming. Int. J. Electron. Commer. 12(4), 9–38 (2008). https://doi.org/10.
2753/JEC1086-4415120401
2. Alberti, M., Chesani, F., Gavanelli, M., Lamma, E., Mello, P., Torroni, P.: Veri-
fiable agent interaction in abductive logic programming: the SCIFF framework.
ACM Trans. Comput. Log. 9(4), 29:1–29:43 (2008). https://doi.org/10.1145/
1380572.1380578
3. Alberti, M., Dell’Acqua, P., Pereira, L.M.: Observation strategies for event detec-
tion with incidence on runtime verification: theory, algorithms, experimentation.
Ann. Math. Artif. Intell. 62(3–4), 161–186 (2011). https://doi.org/10.1007/s10472-
011-9259-5
4. Ancona, D., et al.: Behavioral types in programming languages. Found. Trends
Programm. Lang. 3(2–3), 95–230 (2016)
5. Ancona, D., Briola, D., El Fallah Seghrouchni, A., Mascardi, V., Taillibert, P.:
Exploiting Prolog for projecting agent interaction protocols. In: Giordano, L.,
Gliozzi, V., Pozzato, G.L. (eds.) Proceedings of the 29th Italian Conference on
Computational Logic, Torino, Italy, June 16–18, 2014. CEUR Workshop Proceed-
ings, vol. 1195, pp. 30–45. CEUR-WS.org (2014). http://ceur-ws.org/Vol-1195/
long2.pdf
6. Ancona, D., Briola, D., Ferrando, A., Mascardi, V.: Global protocols as first class
entities for self-adaptive agents. In: Weiss, G., Yolum, P., Bordini, R.H., Elkind,
E. (eds.) Proceedings of the 2015 International Conference on Autonomous Agents
and Multiagent Systems, AAMAS 2015, Istanbul, Turkey, May 4–8, 2015, pp. 1019–
1029. ACM (2015). http://dl.acm.org/citation.cfm?id=2773282
7. Ancona, D., Briola, D., Ferrando, A., Mascardi, V.: Runtime verification of fail-
uncontrolled and ambient intelligence systems: a uniform approach. Intelligenza
Artificiale 9(2), 131–148 (2015). https://doi.org/10.3233/IA-150084
Exploiting LP for RV: Current and Future Perspectives 311
8. Ancona, D., Briola, D., Ferrando, A., Mascardi, V.: MAS-DRiVe: a practical app-
roach to decentralized runtime verification of agent interaction protocols. In: San-
toro, C., Messina, F., Benedetti, M.D. (eds.) Proceedings of the 17th Workshop
“From Objects to Agents” co-located with 18th European Agent Systems Summer
School (EASSS 2016), Catania, Italy, July 29–30, 2016. CEUR Workshop Proceed-
ings, vol. 1664, pp. 35–43. CEUR-WS.org (2016). http://ceur-ws.org/Vol-1664/w7.
pdf
9. Ancona, D., Drossopoulou, S., Mascardi, V.: Automatic generation of self-
monitoring mass from multiparty global session types in jason. In: Baldoni, M.,
Dennis, L., Mascardi, V., Vasconcelos, W. (eds.) DALT 2012. LNCS (LNAI), vol.
7784, pp. 76–95. Springer, Heidelberg (2013). https://doi.org/10.1007/978-3-642-
37890-4 5
10. Ancona, D., Ferrando, A., Mascardi, V.: Comparing trace expressions and linear
temporal logic for runtime verification. In: Ábrahám, E., Bonsangue, M., Johnsen,
E.B. (eds.) Theory and Practice of Formal Methods. LNCS, vol. 9660, pp. 47–64.
Springer, Cham (2016). https://doi.org/10.1007/978-3-319-30734-3 6
11. Ancona, D., Ferrando, A., Mascardi, V.: Parametric runtime verification of multia-
gent systems. In: Larson, K., Winikoff, M., Das, S., Durfee, E.H. (eds.) Proceedings
of the 16th Conference on Autonomous Agents and MultiAgent Systems, AAMAS
2017, São Paulo, Brazil, May 8–12, 2017, pp. 1457–1459. ACM (2017). http://dl.
acm.org/citation.cfm?id=3091328
12. Ancona, D., Ferrando, A., Mascardi, V.: Can determinism and compositional-
ity coexist in RML? In: Dardha, O., Rot, J. (eds.) Proceedings Combined 27th
International Workshop on Expressiveness in Concurrency and 17th Workshop on
Structural Operational Semantics, EXPRESS/SOS 2020, and 17th Workshop on
Structural Operational SemanticsOnline, 31 August 2020. EPTCS, vol. 322, pp.
13–32 (2020). https://doi.org/10.4204/EPTCS.322.4
13. Ancona, D., Ferrando, A., Mascardi, V.: Exploiting probabilistic trace expres-
sions for decentralized runtime verification with gaps. In: Calegari, R., Ciatto, G.,
Omicini, A. (eds.) Proceedings of the 37th Italian Conference on Computational
Logic, Bologna, Italy, June 29–July 1, 2022. CEUR Workshop Proceedings, vol.
3204, pp. 154–170. CEUR-WS.org (2022). http://ceur-ws.org/Vol-3204/paper 17.
pdf
14. Ancona, D., Ferrando, A., Mascardi, V.: Mind the gap! Runtime verification of
partially observable MASs with probabilistic trace expressions. In: Baumeister,
D., Rothe, J. (eds.) EUMAS 2022. LNCS, vol. 13442, pp. 22–40. Springer, Cham
(2022). https://doi.org/10.1007/978-3-031-20614-6 2
15. Ancona, D., Franceschini, L., Ferrando, A., Mascardi, V.: RML: theory and practice
of a domain specific language for runtime verification. Sci. Comput. Program. 205,
102610 (2021). https://doi.org/10.1016/j.scico.2021.102610
16. Barnett, M., Schulte, W.: Spying on components: a runtime verification tech-
nique. In: Workshop on Specification and Verification of Component-Based Sys-
tems (2001)
17. Barringer, H., Goldberg, A., Havelund, K., Sen, K.: Rule-based runtime verifica-
tion. In: Steffen, B., Levi, G. (eds.) VMCAI 2004. LNCS, vol. 2937, pp. 44–57.
Springer, Heidelberg (2004). https://doi.org/10.1007/978-3-540-24622-0 5
18. Bodden, E., Chen, F., Rosu, G.: Dependent advice: a general approach to optimiz-
ing history-based aspects. In: Sullivan, K.J., Moreira, A., Schwanninger, C., Gray,
J. (eds.) Proceedings of the 8th International Conference on Aspect-Oriented Soft-
312 D. Ancona et al.
ware Development, AOSD 2009, Charlottesville, Virginia, USA, March 2–6, 2009,
pp. 3–14. ACM (2009). https://doi.org/10.1145/1509239.1509243
19. Bodden, E., Hendren, L.J.: The Clara framework for hybrid typestate analysis. Int.
J. Softw. Tools Technol. Transf. 14(3), 307–326 (2012). https://doi.org/10.1007/
s10009-010-0183-5
20. Bozzano, M., Delzanno, G., Martelli, M., Mascardi, V., Zini, F.: Logic programming
and multi-agent systems: a synergic combination for applications and semantics.
In: Apt, K.R., Marek, V.W., Truszczynski, M., Warren, D.S. (eds.) The Logic
Programming Paradigm - A 25-Year Perspective. Artificial Intelligence, pp. 5–32.
Springer, Heidelberg (1999). https://doi.org/10.1007/978-3-642-60085-2 1
21. Bragaglia, S., Chesani, F., Mello, P., Montali, M., Torroni, P.: Reactive event calcu-
lus for monitoring global computing applications. In: Artikis, A., Craven, R., Kesim
Çiçekli, N., Sadighi, B., Stathis, K. (eds.) Logic Programs, Norms and Action.
LNCS (LNAI), vol. 7360, pp. 123–146. Springer, Heidelberg (2012). https://doi.
org/10.1007/978-3-642-29414-3 8
22. Briola, D., Mascardi, V., Ancona, D.: Distributed runtime verification of JADE and
Jason multiagent systems with Prolog. In: Giordano, L., Gliozzi, V., Pozzato, G.L.
(eds.) Proceedings of the 29th Italian Conference on Computational Logic, Torino,
Italy, June 16–18, 2014. CEUR Workshop Proceedings, vol. 1195, pp. 319–323.
CEUR-WS.org (2014). http://ceur-ws.org/Vol-1195/short3.pdf
23. Briola, D., Mascardi, V., Ancona, D.: Distributed runtime verification of JADE
multiagent systems. In: Camacho, D., Braubach, L., Venticinque, S., Badica, C.
(eds.) Intelligent Distributed Computing VIII. SCI, vol. 570, pp. 81–91. Springer,
Cham (2015). https://doi.org/10.1007/978-3-319-10422-5 10
24. Bugiel, S., Davi, L., Dmitrienko, A., Fischer, T., Sadeghi, A., Shastry, B.: Towards
taming privilege-escalation attacks on android. In: 19th Annual Network and Dis-
tributed System Security Symposium, NDSS 2012, San Diego, California, USA,
February 5–8, 2012. The Internet Society (2012). https://www.ndss-symposium.
org/ndss2012/towards-taming-privilege-escalation-attacks-android
25. Búr, M., Szilágyi, G., Vörös, A., Varró, D.: Distributed graph queries for runtime
monitoring of cyber-physical systems. In: Russo, A., Schürr, A. (eds.) FASE 2018.
LNCS, vol. 10802, pp. 111–128. Springer, Cham (2018). https://doi.org/10.1007/
978-3-319-89363-1 7
26. Chesani, F., Masellis, R.D., Francescomarino, C.D., Ghidini, C., Mello, P., Mon-
tali, M., Tessaris, S.: Abducing workflow traces: a general framework to manage
incompleteness in business processes. In: Kaminka, G.A., Fox, M., Bouquet, P.,
Hüllermeier, E., Dignum, V., Dignum, F., van Harmelen, F. (eds.) ECAI 2016 -
22nd European Conference on Artificial Intelligence, 29 August-2 September 2016,
The Hague, The Netherlands - Including Prestigious Applications of Artificial Intel-
ligence (PAIS 2016). Frontiers in Artificial Intelligence and Applications, vol. 285,
pp. 1734–1735. IOS Press (2016). https://doi.org/10.3233/978-1-61499-672-9-1734
27. Chesani, F., Mello, P., Montali, M., Torroni, P.: A logic-based, reactive calculus of
events. Fundam. Informaticae 105(1-2), 135–161 (2010). https://doi.org/10.3233/
FI-2010-361
28. Chowdhury, O., Jia, L., Garg, D., Datta, A.: Temporal mode-checking for runtime
monitoring of privacy policies. In: Biere, A., Bloem, R. (eds.) CAV 2014. LNCS,
vol. 8559, pp. 131–149. Springer, Cham (2014). https://doi.org/10.1007/978-3-319-
08867-9 9
29. Costantini, S., Dell’Acqua, P., Pereira, L.M., Tsintza, P.: Runtime verification of
agent properties. In: Abreu, S., Seipel, D. (eds.) Proceedings of INAP 2009 18thIn-
Exploiting LP for RV: Current and Future Perspectives 313
41. Franceschini, L.: RML: runtime monitoring language: a system-agnostic DSL for
runtime verification. In: Marr, S., Cazzola, W. (eds.) Conference Companion of the
3rd International Conference on Art, Science, and Engineering of Programming,
Genova, Italy, April 1–4, 2019. pp. 28:1–28:3. ACM (2019). https://doi.org/10.
1145/3328433.3328462
42. Fredlund, L.Å., Mariño, J., Pérez, S., Tamarit, S.: Runtime verification in erlang
by using contracts. In: Silva, J. (ed.) WFLP 2018. LNCS, vol. 11285, pp. 56–73.
Springer, Cham (2019). https://doi.org/10.1007/978-3-030-16202-3 4
43. Fung, T.H., Kowalski, R.A.: The Iff proof procedure for abductive logic program-
ming. J. Log. Program. 33(2), 151–165 (1997). https://doi.org/10.1016/S0743-
1066(97)00026-5
44. Hamlen, K.W., Jones, M.M., Sridhar, M.: Aspect-oriented runtime monitor cer-
tification. In: Flanagan, C., König, B. (eds.) TACAS 2012. LNCS, vol. 7214, pp.
126–140. Springer, Heidelberg (2012). https://doi.org/10.1007/978-3-642-28756-
5 10
45. Havelund, K.: Rule-based runtime verification revisited. Int. J. Softw. Tools Tech-
nol. Transfer 17(2), 143–170 (2014). https://doi.org/10.1007/s10009-014-0309-2
46. Havelund, K., Reger, G., Roşu, G.: Runtime verification past experiences and
future projections. In: Steffen, B., Woeginger, G. (eds.) Computing and Software
Science. LNCS, vol. 10000, pp. 532–562. Springer, Cham (2019). https://doi.org/
10.1007/978-3-319-91908-9 25
47. Havelund, K., Roşu, G.: Runtime verification - 17 years later. In: Colombo, C.,
Leucker, M. (eds.) RV 2018. LNCS, vol. 11237, pp. 3–17. Springer, Cham (2018).
https://doi.org/10.1007/978-3-030-03769-7 1
48. Howar, F., Giannakopoulou, D., Mues, M., Navas, J.A.: Generating component
interfaces by integrating static and symbolic analysis, learning, and runtime mon-
itoring. In: Margaria, T., Steffen, B. (eds.) ISoLA 2018. LNCS, vol. 11245, pp.
120–136. Springer, Cham (2018). https://doi.org/10.1007/978-3-030-03421-4 9
49. Hu, R., Neykova, R., Yoshida, N., Demangeon, R., Honda, K.: Practical interrupt-
ible conversations. In: Legay, A., Bensalem, S. (eds.) RV 2013. LNCS, vol. 8174, pp.
130–148. Springer, Heidelberg (2013). https://doi.org/10.1007/978-3-642-40787-
18
50. Huang, T., Wu, G., Wei, J.: Runtime monitoring composite web services through
stateful aspect extension. J. Comput. Sci. Technol. 24(2), 294–308 (2009). https://
doi.org/10.1007/s11390-009-9225-4
51. Inçki, K., Ari, I., Sözer, H.: Runtime verification of IoT systems using complex
event processing. In: Fortino, G., et al. (eds.) 14th IEEE International Conference
on Networking, Sensing and Control, ICNSC 2017, Calabria, Italy, May 16–18,
2017, pp. 625–630. IEEE (2017). https://doi.org/10.1109/ICNSC.2017.8000163,
https://doi.org/10.1109/ICNSC.2017.8000163
52. Indiono, C., Mangler, J., Fdhila, W., Rinderle-Ma, S.: Rule-based runtime mon-
itoring of instance-spanning constraints in process-aware information systems.
In: Debruyne, C., Panetto, H., Meersman, R., Dillon, T., Kühn, O’Sullivan, D.,
Ardagna, C.A. (eds.) OTM 2016. LNCS, vol. 10033, pp. 381–399. Springer, Cham
(2016). https://doi.org/10.1007/978-3-319-48472-3 22
53. Kallel, S., Charfi, A., Dinkelaker, T., Mezini, M., Jmaiel, M.: Specifying and mon-
itoring temporal properties in web services compositions. In: Eshuis, R., Grefen,
P.W.P.J., Papadopoulos, G.A. (eds.) Seventh IEEE European Conference on Web
Services (ECOWS 2009), 9–11 November 2009, Eindhoven, The Netherlands, pp.
148–157. IEEE Computer Society (2009). https://doi.org/10.1109/ECOWS.2009.
15
Exploiting LP for RV: Current and Future Perspectives 315
54. Kallel, S., Charfi, A., Mezini, M., Jmaiel, M., Sewe, A.: A holistic approach for
access control policies: from formal specification to aspect-based enforcement. Int.
J. Inf. Comput. Secur. 3(3/4), 337–354 (2009). https://doi.org/10.1504/IJICS.
2009.031044
55. Klose, K., Ostermann, K.: A classification framework for pointcut languages in run-
time monitoring. In: Oriol, M., Meyer, B. (eds.) TOOLS EUROPE 2009. LNBIP,
vol. 33, pp. 289–307. Springer, Heidelberg (2009). https://doi.org/10.1007/978-3-
642-02571-6 17
56. Körner, P., et al.: Fifty years of Prolog and beyond. Theory Pract. Log. Program.
22(6), 776–858 (2022). https://doi.org/10.1017/S1471068422000102
57. Kowalski, R., Quintero, J.D., Sartor, G., Calejo, M.: Logical English for law and
education. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski,
R., Rossi, F. (eds.) Prolog - The Next 50 Years. LNCS, vol. 13900, pp. 287–299.
Springer, Cham (2023)
58. Kowalski, R., Sadri, F.: Towards a unified agent architecture that combines ratio-
nality with reactivity. In: Pedreschi, D., Zaniolo, C. (eds.) LID 1996. LNCS,
vol. 1154, pp. 135–149. Springer, Heidelberg (1996). https://doi.org/10.1007/
BFb0031739
59. Kowalski, R.A., Sergot, M.J.: A logic-based calculus of events. In: Mani, I., Puste-
jovsky, J., Gaizauskas, R.J. (eds.) The Language of Time - A Reader, pp. 217–240.
Oxford University Press (2005)
60. LeDoux, C.H., Jr., D.S.P.: Saving traces for Ada debugging. In: Barnes, J.G.P.,
Fisher, G.A. (eds.) Proceedings of the 1985 Annual ACM SIGAda International
Conference on Ada, SIGAda 1985, Paris, France, May 14–16, 1985, pp. 97–108.
Cambridge University Press (1985). https://doi.org/10.1145/324426.324385
61. Leotta, M., Ancona, D., Franceschini, L., Olianas, D., Ribaudo, M., Ricca, F.:
Towards a runtime verification approach for internet of things systems. In: Pau-
tasso, C., Sánchez-Figueroa, F., Systä, K., Murillo Rodrı́guez, J.M. (eds.) ICWE
2018. LNCS, vol. 11153, pp. 83–96. Springer, Cham (2018). https://doi.org/10.
1007/978-3-030-03056-8 8
62. Leotta, M., et al.: Comparing testing and runtime verification of IoT systems:
A preliminary evaluation based on a case study. In: Damiani, E., Spanoudakis,
G., Maciaszek, L.A. (eds.) Proceedings of the 14th International Conference on
Evaluation of Novel Approaches to Software Engineering, ENASE 2019, Heraklion,
Crete, Greece, May 4–5, 2019, pp. 434–441. SciTePress (2019). https://doi.org/10.
5220/0007745604340441
63. Leucker, M., Schallhart, C.: A brief account of runtime verification. J. Logic Alge-
braic Programm. 78(5), 293–303 (2009). https://doi.org/10.1016/j.jlap.2008.08.
004. https://www.sciencedirect.com/science/article/pii/S1567832608000775, the
1st Workshop on Formal Languages and Analysis of Contract-Oriented Software
(FLACOS’07)
64. Majma, N., Babamir, S.M., Monadjemi, A.: Runtime Verification of Pacemaker
Functionality Using Hierarchical Fuzzy Colored Petri-nets. J. Med. Syst. 41(2),
1–21 (2016). https://doi.org/10.1007/s10916-016-0664-5
65. Malakuti, S., Bockisch, C., Aksit, M.: Applying the composition filter model for
runtime verification of multiple-language software. In: ISSRE 2009, 20th Interna-
tional Symposium on Software Reliability Engineering, Mysuru, Karnataka, India,
16–19 November 2009, pp. 31–40. IEEE Computer Society (2009). https://doi.org/
10.1109/ISSRE.2009.12
316 D. Ancona et al.
66. Palù, A.D., Dovier, A., Formisano, A., Pontelli, E.: Prolog meets biology. In: War-
ren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.)
Prolog - The Next 50 Years. LNCS, vol. 13900, pp. 318–333. Springer, Cham (2023)
67. Perotti, A., Boella, G., Garcez, A.A.: Runtime verification through forward chain-
ing. In: Bartocci, E., Majumdar, R. (eds.) RV 2015. LNCS, vol. 9333, pp. 185–200.
Springer, Cham (2015). https://doi.org/10.1007/978-3-319-23820-3 12
68. Perotti, A., d’Avila Garcez, A.S., Boella, G.: Neural networks for runtime verifi-
cation. In: 2014 International Joint Conference on Neural Networks, IJCNN 2014,
Beijing, China, July 6–11, 2014, pp. 2637–2644. IEEE (2014). https://doi.org/10.
1109/IJCNN.2014.6889961
69. Pnueli, A.: The temporal logic of programs. In: 18th Annual Symposium on Foun-
dations of Computer Science, Providence, Rhode Island, USA, 31 October - 1
November 1977, pp. 46–57. IEEE Computer Society (1977)
70. Rabiser, R., Guinea, S., Vierhauser, M., Baresi, L., Grünbacher, P.: A comparison
framework for runtime monitoring approaches. J. Syst. Softw. 125, 309–321 (2017).
https://doi.org/10.1016/j.jss.2016.12.034
71. Ricca, F., Mascardi, V., Verri, A.: Test’n’Mo: a collaborative platform for human
testers and intelligent monitoring agents. In: Ahrendt, W., Ancona, D., Fran-
calanza, A. (eds.) VORTEX 2021: Proceedings of the 5th ACM International
Workshop on Verification and mOnitoring at Runtime EXecution, Virtual Event,
Denmark, 12 July 2021, pp. 17–21. ACM (2021). https://doi.org/10.1145/3464974.
3468446
72. Rosà, A., Zheng, Y., Sun, H., Javed, O., Binder, W.: Adaptable runtime monitoring
for the Java virtual machine. In: Margaria, T., Steffen, B. (eds.) ISoLA 2016. LNCS,
vol. 9953, pp. 531–546. Springer, Cham (2016). https://doi.org/10.1007/978-3-319-
47169-3 42
73. Sánchez, C., et al.: A survey of challenges for runtime verification from advanced
application domains (beyond software). Formal Methods Syst. Des. (2), 1–57
(2019). https://doi.org/10.1007/s10703-019-00337-w
74. Satoh, K.: PROLEG: Practical legal reasoning system. In: Warren, D.S., Dahl, V.,
Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50
Years. LNCS, vol. 13900, pp. 277-283. Springer, Cham (2023))
75. Simon, L., Mallya, A., Bansal, A., Gupta, G.: Coinductive logic programming.
In: Etalle, S., Truszczyński, M. (eds.) ICLP 2006. LNCS, vol. 4079, pp. 330–345.
Springer, Heidelberg (2006). https://doi.org/10.1007/11799573 25
76. Stolz, V., Huch, F.: Runtime verification of concurrent Haskell programs. Electron.
Notes Theor. Comput. Sci. 113, 201–216 (2005). https://doi.org/10.1016/j.entcs.
2004.01.026
77. Tsigkritis, T., Spanoudakis, G.: Diagnosing runtime violations of security &
dependability properties. In: Proceedings of the Twentieth International Confer-
ence on Software Engineering & Knowledge Engineering (SEKE’2008), San Fran-
cisco, CA, USA, July 1–3, 2008, pp. 661–666. Knowledge Systems Institute Grad-
uate School (2008)
78. Tsigkritis, T., Spanoudakis, G.: A temporal abductive diagnostic process for run-
time properties violations. In: Roth-Berghofer, T., Schulz, S., Leake, D.B., Bahls,
D. (eds.) Explanation-aware Computing, Papers from the 2008 ECAI Workshop,
Patras, Greece, July 21–22, 2008. University of Patras, pp. 49–60 (2008)
79. Wenger, M., Zoitl, A., Blech, J.O.: Behavioral type-based monitoring for IEC
61499. In: 20th IEEE Conference on Emerging Technologies & Factory Automation,
ETFA 2015, Luxembourg, September 8–11, 2015, pp. 1–8. IEEE (2015). https://
doi.org/10.1109/ETFA.2015.7301447
Exploiting LP for RV: Current and Future Perspectives 317
80. Wooldridge, M.J., Jennings, N.R.: Intelligent agents: theory and practice. Knowl.
Eng. Rev. 10(2), 115–152 (1995). https://doi.org/10.1017/S0269888900008122
81. Yu, B., Liu, J., Lei, M., Yu, Y., Chen, H.: Parallel runtime verification approach for
alternate execution of multiple threads. In: Miao, H., Tian, C., Liu, S., Duan, Z.
(eds.) SOFL+MSVL 2019. LNCS, vol. 12028, pp. 99–109. Springer, Cham (2020).
https://doi.org/10.1007/978-3-030-41418-4 8
82. Yu, B., Lu, X., Chen, H., Lei, M., Wang, X.: Runtime verification of ethereum
smart contracts based on MSVL. In: Xue, J., Nagoya, F., Liu, S., Duan, Z.
(eds.) SOFL+MSVL 2020. LNCS, vol. 12723, pp. 142–153. Springer, Cham (2021).
https://doi.org/10.1007/978-3-030-77474-5 10
83. Yu, B., Tian, C., Lu, X., Zhang, N., Duan, Z.: A distributed network-based runtime
verification of full regular temporal properties. IEEE Trans. Parallel Distributed
Syst. 34(1), 76–91 (2023). https://doi.org/10.1109/TPDS.2022.3215854
84. Zhao, Y., Oberthür, S., Kardos, M., Rammig, F.: Model-based runtime verifica-
tion framework for self-optimizing systems. Electron. Notes Theor. Comput. Sci.
144(4), 125–145 (2006). https://doi.org/10.1016/j.entcs.2006.02.008
Prolog Meets Biology
Alessandro Dal Palù1 , Agostino Dovier2(B) , Andrea Formisano2 , and Enrico Pontelli3
1
University of Parma, Parma, Italy
[email protected]
2
University of Udine, 33100 Udine, Italy
{agostino.dovier,andrea.formisano}@uniud.it
3
New Mexico State University, Las Cruces, NM, USA
[email protected]
Abstract. This paper provides an overview of the use of Prolog and its deriva-
tives to sustain research and development in the fields of bioinformatics and com-
putational biology. A number of applications in this domain have been enabled
by the declarative nature of Prolog and the combinatorial nature of the underly-
ing problems. The paper provides a summary of some relevant applications as
well as potential directions that the Prolog community can continue to pursue
in this important domain. The presentation is organized in two parts: “small,”
which explores studies in biological components and systems, and “large,” that
discusses the use of Prolog to handle biomedical knowledge and data. A con-
crete encoding example is presented and the effective implementation in Prolog
of a widely used approximated search technique, large neighborhood search, is
presented.
1 Introduction
Since their infancy, Logic Programming (LP) and Prolog have shown their suitability
to address problems from the domain of modern Biology and the broader realms of
medicine and life sciences. This was already evident in the first edition of the ICLP
conference in 1982 [43]; R.A. Overbeek offered a tutorial on LP and molecular anal-
ysis during JICSLP 1992 [61]. The use of Prolog-style technologies in the domain of
Biology has progressively increased over the years, especially thanks to the introduc-
tion of extensions like Constraint Logic Programming (CLP), which supports a more
effective use of mathematical constraints to guide the search process, and Inductive
Logic Programming (ILP), which supports learning of rules that represent biological
systems from observations. The literature that explores the use of LP technologies to
guide computational biology tasks is extensive. For a survey of contributions to compu-
tational biology using the framework of Answer Set Programming (ASP), we refer the
interested reader to [17, 23]. In the survey [17], in particular, bioinformatics problems
are clustered in three main areas: Genomics studies (e.g., Haplotype inference, Phylo-
genetic inference), Structural studies (e.g., problems associated with Protein structure
prediction), and Systems studies (e.g., reasoning about biological networks).
In this complementary paper, we present a review of applications of Prolog in the
realm of computational biology and bioinformatics. We use the term Prolog as an
c The Author(s), under exclusive license to Springer Nature Switzerland AG 2023
D. S. Warren et al. (Eds.): Prolog: The Next 50 Years, LNAI 13900, pp. 318–333, 2023.
https://doi.org/10.1007/978-3-031-35254-6_26
Prolog Meets Biology 319
umbrella that covers both the actual Prolog language as well as related frameworks,
like CLP, ILP, and Datalog (see also [81]). After a brief review of some of the older
contributions (up to 1995) in Sect. 2, the presentation is structured, in broad strokes,
along two major lines. Section 3 explores the use of Prolog to investigate biologi-
cal components and systems, such as molecular analysis or structure prediction, thus
dealing with “small” parts of nature. Section 4 explores the use of Prolog to handle
biomedical knowledge and data, thus dealing with “large” amount of data. This paper
also reflects the experience gained by organizing thirteen editions (2005–2018) of the
Workshop on Constraint-based methods in Bioinformatics, co-located with the Inter-
national Conference on Logic Programming and the International Conference on Con-
straint Programming (http://clp.dimi.uniud.it/wcb). We consider the various proposals
presented in such workshops using local search for dealing with optimization problems
with extensive search spaces; Sect. 5 explores how approximated local search with large
neighborhoods can be implemented in Prolog. Some conclusions and future perspec-
tives are presented in Sect. 6.
2 Early Days
Dating back to 1982, in the first edition of ICLP, Jouvert et al. presented a medical deci-
sion system, called SPHINX, that makes use of knowledge representation of medical
facts to implement first-order logical deductions [43]. This initial work was followed by
a number of Prolog-based applications devoted to the investigation of different aspects
of Biology. One of the first examples of the use of Prolog as a modeling and query lan-
guage for protein structure data can be found in [76], as an alternative to the relational
database exploited in a protein modeling facility developed at the U. K. Scientific Cen-
tre. In 1986, Rawlings et al. modelled the super secondary structure of proteins using
Prolog facts, in order to enable Prolog query-answering processes to identify admissi-
ble arrangements of secondary structure components (i.e., its local sub-structures such
as α-helices and β-strands) [65]. The problem of determining protein secondary struc-
ture from its primary sequence is addressed by Muggleton et al. [56] using Golem,
an ILP system capable of producing Prolog rules that describe topological relations
among residues. A Prolog-based representation and the inference of topological prop-
erties of proteins (e.g., bonds, secondary structures, spatial positions and angles) are
described in various works of Saldanha et al; among them, we mention the GENPRO
system [68], that generates Prolog rules from protein structure datasets. The approach
has been improved in [69, 70], where LP is used to describe the secondary structure of
amylin using a combination of knowledge sources. In the proposal of Barton et al. [5],
Prolog is used to implement a database of protein data and various functionalities to
process and analyze information on secondary structure of proteins, including an inter-
face to TOPOL [64], a system, implemented in Prolog, that enables reasoning about
protein topology. An elaborated technique, called analogy by abstraction and imple-
mented in Prolog, is proposed in [39] to predict the 3D structure of an unknown pro-
tein by heuristically transforming a known protein structure. The same authors propose
a Prolog implementation of a technique called inductive prediction by analogy [40],
applied to the function prediction of proteins’ structure from aminoacid sequences.
320 A. Dal Palù et al.
Kemp et al. [46] implemented in Prolog and C a database called P/FDM, used to store
protein data and to enable fast prototyping of Prolog applications. In particular, the
problem of identification of hydrophobic micro-domains is considered and solved in
Prolog thanks to its suitability in handling lists and tree structures. The advantage of
exploiting Prolog in P/FDM is also shown in [33, 47] by considering the problem of
protein structure analysis for the specific case of antibodies.
In the context of the Human Genome Project, various applications of deductive
databases and Prolog have been developed. The Prolog expert system ISCN Expert [13]
is designed to allow declarative representation of human chromosomal data and to
mechanize reasoning tasks about chromosomal abnormalities. The parallel capabili-
ties of Prolog have been exploited to determine pseudo-knots of DNA sequences [49].
Wong in 1993 used Prolog to model the Probed Partial Digestion of DNA, with the
goal of finding genomic maps compatible with experimental data [82]. The DNA-
ChartParser system uses the Prolog unification mechanism to implement parting of
DNA sequences; in particular, [48] dealt with the specific case of E. coli promoters.
One of the first uses of Prolog as a high-level language for representing and querying
biochemical data is proposed by Kazic in 1994, to circumvent impedance mismatch of
other hybrid systems existing at the time of publication [45]. The same ideas are applied
to create a declarative system to model and reason about metabolism of E. coli [44].
Several other applications in the early Nineties focused on logic-based representations
of genomic information and rules to describe their relationships have appeared in the
literature [30, 37, 77]. The BIOLOG system [50] is a Prolog implementation to automate
the analysis of nucleic acid sequences. BIOLOG features include access to databases,
querying, sequence alignment and rearrangement, search for homologies, etc. Yoshida
et al. developed Lucy [83], a system that takes advantage of Prolog representation, set
operations, and recursion, to organize genomic knowledge and enable querying and
reasoning on molecular biological data.
Almost twenty years before the breakthroughs provided by Deep Learning [2], Prolog
has been used to solve the protein tertiary structure prediction problem—i.e., predicting
the 3D shape of a protein given its primary structure. Even strong approximations of
the problem (with simplified lattice and energy models) are NP-complete [14]. How-
ever, an approach combining a Prolog modeling of the problem with learning tools to
predict selected fragments of the protein (e.g, helices and sheets) and exploiting a statis-
tical energy function to treat the different aminoacids, is able to fold proteins of length
greater than 100 in reasonable time [15]. An idea not very different from what current
deep learning tools do is used in [16] where, given as input the collection of possi-
ble 4-tuples of consecutive aminoacids, a Prolog program reconstructs minimal-energy
shapes of the protein by merging consecutive partially overlapping tuples.
A Prolog expert system is part of the FIGENIX platform [32], designed to automate
the pipeline of structural and functional annotation, in particular to detect genes and
their location in a biological sequence and predicting the proteins that they encode. The
3D genome reconstruction problem is addressed in [51] by combining Prolog and a
Prolog Meets Biology 321
solver for mixed integer programming. The system GenePath [84] supports the analysis
of genetic data, using user pre-defined patterns, to infer potential genetic networks regu-
lating biological processes, in particular the relations connecting mutations and changes
in system’s behavior. The core of GenePath is implemented in Prolog.
Logic programming has been used in [3] to implement a toolkit aimed at identify-
ing constrained portion of genes by integrating phylogenetic information and multiple
sequence alignments. The approach described in [4] solves the problem of identify-
ing orthologs (that is, genes across different genomes with the same function), groups
of orthologous genes in close proximity, and gene duplication, by means of a Prolog
application. Such system implements features such as access to gene-databases, text
processing of gene-data, gene alignment, homolog-pair detection, ortholog gene-groups
identification, etc.
The reconstruction of metabolic networks in presence of incomplete information
is the goal of the system described in [28]. The Prolog implementation combines a
declarative representation of data and algorithms for genome sequence analysis, also
exploiting metabolic and phylogenetic information, and handling incompleteness and
defaults. The reconstruction process can proceed autonomously or in interaction with
the user. A declarative approach to the automation of genome analysis is done with the
MAGPIE system which relies on SICStus Prolog, a full description of the system can
be found in [27]. BioSim [38] is a simulation environment developed in CLP for quali-
tative reasoning on biological regulatory processes. The detection of causality relations
in metabolic networks is the goal of [6] which present a system developed on top of
the SICStus Prolog interpreter. In [74] SWI-Prolog is used in combination with R to
determine and analyze signaling pathways and to identify markers of drug sensitivity
and resistance in a case study concerning breast cancer cells. Problog is used in [29]
to automate a model of gene regulatory networks combining declarative representation
and probabilistic/statistical inference. The proposed approach to modeling pathways
is validated by learning regulatory networks from time-series gene expression data on
Saccharomyces cerevisiae.
Extensions of Prolog capable of dealing with probabilistic inference under distribu-
tion semantics have been used for biological applications. The framework, combining
logic and probability, enables the identification of genes and other motifs, as proposed
in [12, 54] using the PRISM system [71]. The interpretation of gene networks in [18] is
implemented in Problog [26]. The ILP system Progol [55] has been used for learning
biological properties [57] and detecting constraint protein structure [78, 79]. The system
Cplint [66] is used in [75] to analyze evolution of cancer data.
In Systems Biology, logic programming has been used to analyze and simulate bio-
logical networks. For instance, the Biochamm system [8], supports the analysis and sim-
ulation of Boolean, kinetic, and stochastic models of biochemical systems (see, e.g., its
use in [19]). In [25] CLP is used to infer ranges of parameter values from observations
and to perform qualitative simulations in molecular networks.
A Glimpse of a Prolog Encoding. We would like to briefly introduce here a sim-
plification of the protein structure prediction problem and show the expressive power
of Prolog (with constraints) for its encoding. A protein is a sequence of aminoacids.
For the scope of this section we consider aminoacids as basic components (we do not
322 A. Dal Palù et al.
analyze their internal structure; the interested reader is referred, e.g., to [9]). The linear
distance between two consecutive aminoacids is assumed to be constant (3.8Å), while
the angles between sequences of two and three aminoacids can change and, thus, even
with discrete approximations, the protein might take a number of spatial forms, called
foldings, of huge size. Two different aminoacids cannot overlap in a folding. There are
20 different kinds of aminoacids; however they can be clustered in two families: h (for
hydrophobic) and p (for polar or hydrophilic). h aminoacids tends to stay internally
in a folding and to attract each other. In this presentation we use a very simple lattice
spatial model where analyzing the possible foldings: the 2D Cartesian plane where the
unit is taken as the linear distance between consecutive aminoacids. Angles admitted
are −90◦ , 0◦ , +90◦ . Two aminoacids are in “contact” if their distance is the lattice unit.
The problem is that of predicting the folding (one of them) that maximizes the number
of contacts between non consecutive hydrophobic (h) aminoacids.
Fig. 1. Some foldings of the “protein” hhphphhhh (h/p are represented as •/◦, respectively).
Contacts are emphasized using the symbol ≡
Let us see how this problem can be encoded in Prolog. The input is a list Protein
= [a1 , . . . , an ] of constant terms h and p. The output is a list of 2D positions Folding
= [X1 , Y1 , . . . , Xn , Yn ] and a variable Energy that will store the number of h-h con-
tacts. The length n of the protein is computed from the input list, and the list of 2n vari-
ables for the folding is generated and the interval 0..2n is assigned as domain to them.
In order to break symmetries, the first two aminoacids are set in (n, n) and (n + 1, n),
respectively. Using the classical constraint & generate programming style of CLP, con-
straints between the variables, modeling the problem, are added and then the built-in
predicate labeling that searches for the solutions, with the option of looking for
solution(s) maximizing the number of h-h contacts, is called.
1 pf(Protein,Folding,Energy) :-
2 length(Protein,N), M is 2*N, length(Folding,M),
3 Folding ins 0..M, N1 is N + 1,
4 Folding = [N,N,N1,N|_],
5 next_constraints(Folding),
6 avoid_self_loops(Folding),
7 contacts(Protein,Folding,Energy),
8 labeling([max(Energy)],Folding).
Prolog Meets Biology 323
In real-life optimization problems, and in biology in particular, the size of the search tree
makes any complete search technique practically unfeasible. Therefore a widespread
approach is the one of using approximate search techniques, globally referred to as
local search, as witnessed by several contributions in the series of workshops WCB (c.f.
Sect. 1). These techniques return a “good” solution, the best one found in a given time-
out. In this section we show how to implement a very effective technique, Large Neigh-
borhood Search (LNS) [72], in Prolog. This was exploited by some Prolog approaches
to biology problems (such as [16]) but it can be used for any optimization problem
encoded in Prolog.
LNS is a form of local search, where an admissible solution enables the exploration
of a “large” neighborhood in the search space. The neighborhood depends on moves,
that generalize the traditional local search move: here a large number of variables is
allowed to change subject to constraints. The basic LNS routine is the following:
A timeout mechanism is typically adopted to terminate the cycle between steps 2 and 3.
For example, the procedure is terminated if either a general time-out occurs or if k suc-
cessive iterations are performed without any improvement in the quality of the solution.
In these cases, the best solution found is returned. It is possible to occasionally allow-
ing a worsening move—this is important to enable the procedure to abandon a local
326 A. Dal Palù et al.
Let us focus on the rule implementing local search steps. The predicate local
looks for another solution and loops on that, since another_sol always ends with
failure. The call with negation does not instantiate the variables (that are however con-
strained between them by the lns definition) and thus a new fresh call is made.
1 local(Input, Solution, Cost):-
2 \+ another_sol(Input, Solution, Cost),
3 local(Input, Solution, Cost).
Let us focus on the predicate another_sol computing the successive solution.
The last solution found is retrieved. If it is “null” this means that we have not yet found
the first solution and we look for it. If a previous solution exists, we are exactly in the
point where we implement LNS. The call to a random generator assigns a random inte-
ger, in [1..10], to the variable Prob (line 5). Lines 6 and 7 decide the “new” constraint
for the variable Cost. If Prob = 1 (line 6, event with probability 10%) we look for
new solutions better than 56 of the last solution found. Thus, we are accepting worsen-
ing solutions. This is useful for escaping local minima. If Prob > 1 (line 7, event with
probability 90%) we look for improving solutions w.r.t. the last solution found.
Prolog Meets Biology 327
portion in the left part of the tree. LNS allows us to visit distant subtrees of the search
tree. When, by chance, a promising area is found, large moves in that area allow us to
improve the computed solution (see e.g., [80]).
6 Conclusions
Prolog has clearly demonstrated over the years its strengths as an ideal paradigm to
address a diversity of challenges in bioinformatics. This paper touches on several suc-
cessful examples of uses of Prolog frameworks in these domains. This line of research
is also important as it has led to techniques which have been generalized to other
domains—e.g., [16] explored a Prolog implementation of LNS, which has been later
applied to totally different contexts (e.g., [53]). The most recent literature in this area
has been primarily focused on the use of ASP for modeling a variety of inference
problems—surveys can be found in [17, 22]. This avails of the clear mathematical prop-
erties of ASP to guarantee provable properties of the encodings. Other techniques that
have been used and that are close to logic programming are integer linear programming,
local search, pseudo Boolean and SAT.
Prolog maintains an important edge for future uses in computational biology, thanks
to its ability to interact with numerical constraint solvers for solving complex combi-
natorial problems arising in biology and with machine learning in inductive logic pro-
gramming. In particular, a big data analysis based on logic programming might result
in a predicted model. This model contains a lot of information that can be analyzed,
verified using formal tools developed in the years for logic programming, facilitated by
the declarative semantics.
Although for string/substring problems arising in DNA analysis the lack of direct
access to vectors is a drawback for pure Prolog, the availability of interfaces with differ-
ent languages of modern Prolog releases allows the programmers to exploit Prolog as a
reasoning for complex distributed and parallel systems (see e.g. [21]). Thus, we share
the feeling of Gupta et al. [35] that Prolog and its dialects will maintain their key-role
for automated reasoning in the following 50 years.
Acknowledgements. We thank the anonymous reviewers that allowed us to greatly improve the
focus and the presentation of the paper. This research is partially supported by INdAM-GNCS
projects CUP E55F22000270001 and CUP E53C22001930001, by Interdepartmental Project on
AI (Strategic Plan UniUD-22-25), and by NSF grants 2151254 and 1914635.
References
1. Angelopoulos, N., Giamas, G.: Prolog Bioinformatic Pipelines: A Case Study in Gene Dys-
regulation. In: Workshop on Constraint-Based Methods in Bioinformatics (WCB14). Lyon,
France (2014)
2. Baek, M., Baker, D.: Deep learning and protein structure modeling. Nat. Methods 19, 13–14
(2022)
3. Bansal, A.K.: Establishing a framework for comparative analysis of genome sequences. In:
Proceedings of the International IEEE Symposium on Intelligence in Neural and Biological
Systems, pp. 84–91 (1995)
Prolog Meets Biology 329
4. Bansal, A.K., Bork, P.: Applying logic programming to derive novel functional information
of genomes. In: Gupta, G. (ed.) PADL 1999. LNCS, vol. 1551, pp. 275–289. Springer, Hei-
delberg (1998). https://doi.org/10.1007/3-540-49201-1_19
5. Barton, G.J., Rawlings, C.J.: A Prolog approach to analysing protein structure. Tetrahedron
Comput. Methodol. 3(6 PART C), 739–756 (1990)
6. Bodei, C., Bracciali, A., Chiarugi, D.: On deducing causality in metabolic networks. BMC
Bioinform. 9(S-4) (2008)
7. Burger, A., Davidson, D., Baldock, R.: Formalization of mouse embryo anatomy. Bioinfor-
matics 20, 259–267 (2004)
8. Calzone, L., Fages, F., Soliman, S.: Biocham: an environment for modeling biological sys-
tems and formalizing experimental knowledge. Bioinformatics 22(14), 1805–1807 (2006)
9. Campeotto, F., Dal Palù, A., Dovier, A., Fioretto, F., Pontelli, E.: A constraint solver for
flexible protein model. J. Artif. Intell. Res. (JAIR) 48, 953–1000 (2013)
10. Chisham, B., Pontelli, E., Son, T., Wright, B.: CDAOStore: a phylogenetic repository using
logic programming and web services. In: International Conference on Logic Programming,
pp. 209–219 (2011)
11. Chisham, B., Wright, B., Le, T., Son, T., Pontelli, E.: CDAO-Store: Ontology-driven Data
Integration for Phylogenetic Analysis. BMC Bioinform. 12, 98 (2011)
12. Christiansen, H., Have, C.T., Lassen, O.T., Petit, M.: Inference with constrained hidden
markov models in PRISM. Theory Pract. Logic Program. 10(4–6), 449–464 (2010)
13. Cooper, G., Friedman, J.M.: Interpreting chromosomal abnormalities using Prolog. Comput.
Biomed. Res. 23(2), 153–164 (1990)
14. Crescenzi, P., Goldman, D., Papadimitrou, C., Piccolboni, A., Yannakakis, M.: On the com-
plexity of protein folding. In: Proceedings of STOC, pp. 597–603 (1998)
15. Dal Palù, A., Dovier, A., Fogolari, F.: Constraint logic programming approach to protein
structure prediction. BMC Bioinform. 5, 186 (2004)
16. Dal Palù, A., Dovier, A., Fogolari, F., Pontelli, E.: CLP-based protein fragment assembly.
Theory Pract. Logic Program. 10(4–6), 709–724 (2010)
17. Dal Palù, A., Dovier, A., Formisano, A., Pontelli, E.: Exploring life: answer set programming
in bioinformatics. In: Kifer, M., Liu, Y.A. (eds.) Declarative Logic Programming: Theory,
Systems, and Applications, pp. 359–412. ACM / Morgan & Claypool (2018)
18. De Maeyer, D., Renkens, J., Cloots, L., De Raedt, L., Marchal, K.: PheNetic: network-based
interpretation of unstructured gene lists in E. coli. Mol. BioSyst. 9, 1594–1603 (2013)
19. Degrand, É., Fages, F., Soliman, S.: Graphical conditions for rate independence in chemical
reaction networks. In: Abate, A., Petrov, T., Wolf, V. (eds.) CMSB 2020. LNCS, vol. 12314,
pp. 61–78. Springer, Cham (2020). https://doi.org/10.1007/978-3-030-60327-4_4
20. Dong, S., Searls, D.B.: Gene structure prediction by linguistic methods. Genomics 23(3),
540–551 (1994)
21. Dovier, A., Formisano, A., Gupta, G., Hermenegildo, M.V., Pontelli, E., Rocha, R.: Parallel
logic programming: a sequel. Theory Pract. Log. Program. 22(6), 905–973 (2022)
22. Erdem, E.: Applications of answer set programming in phylogenetic systematics. In: Bal-
duccini, M., Son, T.C. (eds.) Logic Programming, Knowledge Representation, and Non-
monotonic Reasoning. LNCS (LNAI), vol. 6565, pp. 415–431. Springer, Heidelberg (2011).
https://doi.org/10.1007/978-3-642-20832-4_26
23. Erdem, E., Gelfond, M., Leone, N.: Applications of answer set programming. AI Mag. 37(3),
53–68 (2016)
24. EvoInfo Working Group: PhyloWS: Phyloinformatics Web Services API. https://evoinfo.
nescent.org/PhyloWS (2009)
25. Fanchon, E., Corblin, F., Trilling, L., Hermant, B., Gulino, D.: Modeling the molecular net-
work controlling adhesion between human endothelial cells: inference and simulation using
330 A. Dal Palù et al.
constraint logic programming. In: Danos, V., Schachter, V. (eds.) CMSB 2004. LNCS, vol.
3082, pp. 104–118. Springer, Heidelberg (2005). https://doi.org/10.1007/978-3-540-25974-
9_9
26. Fierens, D., et al.: Inference and learning in probabilistic logic programs using weighted
boolean formulas. Theory Pract. Logic Program. 15(3), 358–401 (2015)
27. Gaasterland, T., Sensen, C.W.: Fully automated genome analysis that reflects user needs and
preferences. A detailed introduction to the MAGPIE system architecture. Biochimie 78(5),
302–310 (1996)
28. Gaasterland, T., Selkov, E.: Reconstruction of metabolic networks using incomplete infor-
mation. In: Rawlings, C.J., Clark, D.A., Altman, R.B., Hunter, L., Lengauer, T., Wodak, S.J.
(eds.) Proceedings of the Third International Conference on Intelligent Systems for Molecu-
lar Biology, Cambridge, United Kingdom, 16–19 July 1995, pp. 127–135. AAAI (1995)
29. Gonçalves, A., Ong, I.M., Lewis, J.A., Santos Costa, V.: A Problog model for analyzing
gene regulatory networks. In: Riguzzi, F., Zelezný, F. (eds.) Late Breaking Papers of the
22nd International Conference on Inductive Logic Programming, Dubrovnik, Croatia, 17–19
September 2012. CEUR Workshop Proceedings, vol. 975, pp. 38–43. CEUR-WS.org (2012)
30. Goodman, N., Rozen, S., Stein, L.: Requirements for a deductive query language in the map-
base genome-mapping database. In: Ramakrishnan, R. (ed.) Proceedings of the Workshop
on Programming with Logic Databases. In Conjunction with ILPS, Vancouver, BC, Canada,
October 30, 1993. Technical Report, vol. 1183, pp. 18–32. University of Wisconsin (1993)
31. Gouret, P., Thompson, J.D., Pontarotti, P.: PhyloPattern: regular expressions to identify com-
plex patterns in phylogenetic trees. BMC Bioinformatics 10, 298 (2009)
32. Gouret, P., Vitiello, V., Balandraud, N., Gilles, A., Pontarotti, P., Danchin, E.G.J.: FIGENIX:
intelligent automation of genomic annotation: expertise integration in a new software plat-
form. BMC Bioinform. 6, 198 (2005)
33. Gray, P.M.D., Paton, N.W., Kemp, G.J.L., Fothergill, J.E.: An object-oriented database for
protein structure analysis. Protein Eng. Des. Sel. 3(4), 235–243 (1990)
34. Gupta, G., et al.: Semantics-based filtering: logic programming’s Killer app. In: Krishna-
murthi, S., Ramakrishnan, C.R. (eds.) PADL 2002. LNCS, vol. 2257, pp. 82–100. Springer,
Heidelberg (2002). https://doi.org/10.1007/3-540-45587-6_7
35. Gupta, G., et al.: Prolog: past, present, and future. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog: 50 Years of Future, LNAI 13900,
pp. 48–61. Springer, Cham (2023)
36. Hanekamp, K., Bohnebeck, U., Beszteri, B., Valentin, K.: PhyloGena - a user-friendly system
for automated phylogenetic annotation of unknown sequences. Bioinformatics 23(7), 793–
801 (2007)
37. Hearne, C., Cui, Z., Parsons, S., Hajnal, S., et al.: Prototyping a genetics deductive database.
In: ISMB. vol. 2, pp. 170–178 (1994)
38. Heidtke, K.R., Schulze-Kremer, S.: BioSim: a new qualitative simulation environment for
molecular biology. In: Glasgow, J.I., Littlejohn, T.G., Major, F., Lathrop, R.H., Sankoff, D.,
Sensen, C.W. (eds.) Proceedings of the 6th International Conference on Intelligent Systems
for Molecular Biology (ISMB-98), Montréal, Québec, Canada, 28 June - 1 July, 1998. pp.
85–94. AAAI (1998)
39. Ishikawa, T., Terano, T.: Using analogical reasoning to predict a protein structure. Genome
Inform. 4, 339–346 (1993)
40. Ishikawa, T., Terano, T.: How to predict it: inductive prediction by analogy using taxonomic
information. In: Proceedings of the Third International Conference on Multistrategy Learn-
ing, pp. 285–293. AAAI Press (1996)
41. Jamil, H.M.: A visual interface for querying heterogeneous phylogenetic databases. IEEE
ACM Trans. Comput. Biol. Bioinform. 14(1), 131–144 (2017)
Prolog Meets Biology 331
42. Jamil, H.M.: Optimizing phylogenetic queries for performance. IEEE ACM Trans. Comput.
Biol. Bioinform. 15(5), 1692–1705 (2018)
43. Joubert, M., Fieschi, M., Fieschi, D., Roux, M.: Medical decision aid: Logic bases of the
system SPHINX. In: Caneghem, M.V. (ed.) Proceedings of the First International Logic
Programming Conference, Faculté des Science de Luminy, ADDP-GIA, Marseille, France,
September, 14–17, 1982, pp. 210–214. ADDP-GIA (1982)
44. Kazic, T.: Representation, reasoning and the intermediary metabolism of Escherichia coli.
In: Proceedings of the Annual Hawaii International Conference on System Sciences, vol. 1,
pp. 853–862 (1993)
45. Kazic, T.: Representation of biochemistry for modeling organisms. In: Kumosinski, T.F.,
Liebman, M.N. (eds.) Molecular Modeling, pp. 486–494. American Chemical Society,
Washington, DC (1994)
46. Kemp, G.J.L., Gray, P.M.D.: Finding hydrophobic microdomains using an object-oriented
database. Comput. Appl. Biosci. 6(4), 357–363 (1990)
47. Kemp, G.J.L., Jiao, Z., Gray, P.M.D., Fothergill, J.E.: Combining computation with database
access in biomolecular computing. In: Litwin, W., Risch, T. (eds.) ADB 1994. LNCS, vol.
819, pp. 317–335. Springer, Heidelberg (1994). https://doi.org/10.1007/3-540-58183-9_57
48. Leung, S., Mellish, C., Robertson, D.: Basic Gene Grammars and DNA-ChartParser for lan-
guage processing of Escherichia coli promoter DNA sequences. Bioinform. 17(3), 226–236
(2001)
49. Lusk, E.L., Overbeek, R.A., Mudambi, S., Szeredi, P.: Applications of the aurora parallel
Prolog system to computational molecular biology. In: Workshop on Concurrent and Parallel
Implementations (sessions A and B), held at IJCSLP’92, Washington, DC, USA, November
1992 (1992)
50. Lyall, A., Hammond, P., Brough, D., Glover, D.: BIOLOG - a DNA sequence analysis system
in Prolog. Nucleic Acids Res. 12(1), 633–642 (1984)
51. MacKay, K., Carlsson, M., Kusalik, A.: GeneRHi-C: 3D GENomE reconstruction from Hi-
C data. In: Proceedings of the 10th International Conference on Computational Systems-
Biology and Bioinformatics, CSBIO 2019. ACM (2019)
52. Maddison, D., Swofford, D., Maddison, W.: NEXUS: an extensible file format for systematic
information. Syst. Biol. 46(4), 590–621 (1997)
53. Meneghetti, A.: Exploiting fashion features for floor storage systems in the shoe industry.
Int. J. Eng. Bus. Manage. 5, SPL.ISSUE (2013)
54. Mørk, S., Holmes, I.: Evaluating bacterial gene-finding hmm structures as probabilistic logic
programs. Bioinformatics 28(5), 636–642 (2012)
55. Muggleton, S.: Inverse entailment and Progol. N. Gener. Comput. 13(3–4), 245–286 (1995)
56. Muggleton, S., King, R.D., Sternberg, M.J.E.: Using logic for protein structure prediction.
In: Proceedings of the Twenty-Fifth Hawaii International Conference on System Sciences,
vol. 1, pp. 685–696 (1992)
57. Muggleton, S., Srinivasan, A., King, R.D., Sternberg, M.J.E.: Biochemical knowledge dis-
covery using inductive logic programming. In: Arikawa, S., Motoda, H. (eds.) DS 1998.
LNCS (LNAI), vol. 1532, pp. 326–341. Springer, Heidelberg (1998). https://doi.org/10.1007/
3-540-49292-5_29
58. Mungall, C.: Experiences using logic programming in bioinformatics. Lect. Notes Comput.
Sci. 5649, 1–21 (2009)
59. Nakhleh, L., Miranker, D.P., Barbançon, F., Piel, W.H., Donoghue, M.J.: Requirements of
phylogenetic databases. In: 3rd IEEE International Symposium on BioInformatics and Bio-
Engineering (BIBE) 2003, 10–12 March 2003, Bethesda, MD, USA, pp. 141–148. IEEE
Computer Society (2003)
332 A. Dal Palù et al.
60. Nguyen, T.H., Pontelli, E., Son, T.C.: Phylotastic: an experiment in creating, manipulating,
and evolving phylogenetic biology workflows using logic programming. Theory Pract. Logic
Program. 18(3–4), 656–672 (2018)
61. Overbeek, R.A.: Logic programming and genetic sequence analysis: a tutorial. In: Apt, K.R.
(ed.) Logic Programming, Proceedings of the Joint International Conference and Symposium
on Logic Programming, JICSLP 1992, Washington, DC, USA, November 1992, pp. 32–34.
MIT Press (1992)
62. Pan, Y., Pontelli, E., Son, T.: BSIS: an experiment in automating bioinformatics tasks through
intelligent workflow construction. In: Chen, H., Wang, Y., Cheung, K.H. (eds.) Semantic
e-Science, pp. 189–238. Springer, Cham (2010). https://doi.org/10.1007/978-1-4419-5908-
9_6
63. Prosdocimi, F., Chisham, B., Pontelli, E., Thompson, J., Stoltzfus, A.: Initial Implementation
of a Comparative Data Analysis Ontology. Evol. Bioinforma. 5, 47–66 (2009)
64. Rawlings, C.J., Taylor, W.R., Nyakairu, J., Fox, J., Sternberg, M.J.E.: Reasoning about pro-
tein topology using the logic programming language Prolog. J. Mol. Graph. 3(4), 151–157
(1985)
65. Rawlings, C.J., Taylor, W.R., Taylor, W.R., Nyakairu, J., Fox, J., Sternberg, M.J.E.: Using
prolog to represent and reason about protein structure. In: Shapiro, E. (ed.) ICLP 1986.
LNCS, vol. 225, pp. 536–543. Springer, Heidelberg (1986). https://doi.org/10.1007/3-540-
16492-8_101
66. Riguzzi, F., Cota, G., Bellodi, E., Zese, R.: Causal inference in cplint. Int. J. Approx. Reason.
91, 216–232 (2017)
67. Rosenblueth, D.A., Thieffry, D., Huerta, A.M., Salgado, H., Collado-Vides, J.: Syntactic
recognition of regulatory regions in Escherichia coli. Comput. Appl. Biosci. 12(5), 415–422
(1996)
68. Saldanha, J., Eccles, J.R.: GENPRO: automatic generation of Prolog clause files for
knowledge-based systems in the biomedical sciences. Comput. Methods Programs Biomed.
28(3), 207–214 (1989)
69. Saldanha, J., Eccles, J.R.: The application of SSADM to modelling the logical structure of
proteins. Bioinformatics 7(4), 515–524 (1991)
70. Saldanha, J., Mahadevan, D.: Molecular model-building of amylin and α-calcitonin gene-
related polypeptide hormones using a combination of knowledge sources. Protein Eng. Des.
Sel. 4(5), 539–544 (1991)
71. Sato, T.: A statistical learning method for logic programs with distribution semantics. In:
Proceedings of the 12th International Conference on Logic Programming (ICLP 95), pp.
715–729 (1995)
72. Shaw, P.: Using constraint programming and local search methods to solve vehicle rout-
ing problems. In: Maher, M., Puget, J.-F. (eds.) CP 1998. LNCS, vol. 1520, pp. 417–431.
Springer, Heidelberg (1998). https://doi.org/10.1007/3-540-49481-2_30
73. Shu, W., Lan, J.: Design a pathway/genome expert system using a Prolog machine incorpo-
rated with a parallel hardware searcher. In: Proceedings of the Asia Pacific Association of
Medical Informatics, APAMI, pp. 9–14 (2006)
74. Stebbing, J., et al.: Characterization of the tyrosine kinase-regulated proteome in breast can-
cer by combined use of RNA interference (rnai) and stable isotope labeling with amino
acids in cell culture (silac) quantitative proteomics. Mol. Cell. Proteomics 14(9), 2479–2492
(2015)
75. Tarzariol, A., Zanazzo, E., Dovier, A., Policriti, A.: Towards a logic programming tool for
cancer data analysis. Fundam. Informaticae 176(3–4), 299–319 (2020)
76. Todd, S., Morffew, A., Burridge, J.: Application of relational database and graphics to the
molecular sciences. In: Longstaff, J. (ed.) Proceedings of the Third British National Confer-
Prolog Meets Biology 333
ence on Databases (BNCOD) 3, Leeds, UK, July 11–13, 1984, pp. 1–13. Cambridge Univer-
sity Press (1984)
77. Tsur, S., Olken, F., Naor, D.: Deductive databases for genomic mapping (extended abstract).
In: Chomicki, J. (ed.) Proceedings of the Workshop on Deductive Databases held in con-
junction with the North American Conference on Logic Programming, Austin, Texas, USA,
November 1, 1990. Technical Report, vol. TR-CS-90-14. Kansas State University (1990)
78. Turcotte, M., Muggleton, S., Sternberg, M.J.E.: Use of inductive logic programming to learn
principles of protein structure. Electron. Trans. Artif. Intell. 4(B), 119–124 (2000)
79. Turcotte, M., Muggleton, S., Sternberg, M.J.E.: Generating protein three-dimensional fold
signatures using inductive logic programming. Comput. Chem. 26(1), 57–64 (2002)
80. Van Hentenryck, P., Michel, L.: Constraint-Based Local Search. MIT Press, Cambridge
(2005)
81. Warren, D.S.: Introduction to Prolog. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M.,
Kowalski, R., Rossi, F. (eds.) Prolog: 50 Years of Future, LNAI 13900, pp. 3–19. Springer,
Cham (2023)
82. Wong, W.K.C.: Logic programming and deductive databases for genomic computations: A
comparison between Prolog and LDL. In: Proceedings of the Annual Hawaii International
Conference on System Sciences. vol. 1, pp. 834–843. IEEE Computer Society (1993)
83. Yoshida, K., et al.: Toward a human genome encyclopedia. In: Proceedings of the Interna-
tional Conference on Fifth Generation Computer Systems. FGCS 1992, June 1–5, Tokyo,
Japan, pp. 307–320. IOS Press (1992)
84. Zupan, B., et al.: GenePath: a system for automated construction of genetic networks from
mutant data. Bioinform. 19(3), 383–389 (2003)
Prolog in Automated Reasoning
in Geometry
Vesna Marinković(B)
1 Introduction
Over the past several decades, the field of automated reasoning and automated
theorem proving has experienced substantial growth, and today many difficult
theorems can be proved automatically, without human guidance. Many chal-
lenges, however, still remain across the various domains to which it is frequently
applied. One such domain is geometry, where automated reasoning is mostly
aimed at proving theorems and solving construction problems. There, it finds its
applications in mathematical education, both for enriching the teaching process
as well as helping students acquiring knowledge on their own, but also in, for
example, robot kinematics and computer vision [4].
There are three different approaches to automated theorem proving in geom-
etry: the algebraic approach, based on translating geometry statements into a
set of algebraic equations and proving corresponding statements using algebraic
techniques [2,22]; the synthetic approach, which tries to automate traditional
geometry proofs [5,9]; and the semi-synthetic approach, which involves calcu-
lations over some geometric quantities [6]. Algebraic methods are known to be
very efficient, but the proofs obtained do not reflect the geometrical nature of
the problem being solved and are therefore unsuitable for educational purposes.
Synthetic methods, on the other hand, may produce geometry-style, human-
readable proofs, even proofs that are similar to proofs made by humans, but
This research is supported by the Serbian Ministry of Education, Science and Tech-
nological Development through the University of Belgrade, Faculty of Mathematics
(Grant No. 451-03-47/2023-01/s200104).
c The Author(s), under exclusive license to Springer Nature Switzerland AG 2023
D. S. Warren et al. (Eds.): Prolog: The Next 50 Years, LNAI 13900, pp. 334–345, 2023.
https://doi.org/10.1007/978-3-031-35254-6_27
Prolog in Automated Reasoning in Geometry 335
are, in general, not very efficient. By using semi-synthetic methods, one does
get somewhat readable proofs, but since the proofs are formulated in terms of
arithmetic expressions, they are not similar to the ones made by humans.
The goal of this paper is to give a short overview of geometry tools developed
in Prolog, with an emphasis on the system ArgoTriCS for automated solving of
construction problems, developed by the author.
Theorem Provers. GEOM [7] is a Prolog program that can solve problems from
high-school plane geometry. A problem statement in GEOM contains the hypoth-
esis and the goal of the conjecture being proved, but also optionally a specifi-
cation of a diagram (in the form of Cartesian coordinates of points appearing
in the problem) that illustrates the problem being solved. Providing such a dia-
gram can guide the proving process as well as help it detect counterexamples for
pruning unprovable goals. Note that any diagram provided represents just one
model, that is, one particular case for which the theorem being proved holds.
QED-Tutrix [8] is an intelligent geometry tutor that helps students prove
geometry statements. Its inference engine is implemented in Prolog. The key
element of its engine is a graph constructed in a forward-chaining man-
ner [20], developed in Prolog, which contains all possible proofs of the problem
being solved.
Geometry theorems can also be proved by theorem provers for first-order logic
or its fragments. Coherent logic [1] is a fragment of first-order logic that repre-
sents a framework suitable for generation of readable proofs. The first automated
1
There is a conference dedicated to automated deduction in geometry, held every two
years: https://adg2023.matf.bg.ac.rs/.
336 V. Marinković
theorem prover based on coherent logic, Euclid [11], was developed in Prolog and
its inference system relied on a forward-chaining mechanism. Euclid was able to
prove many theorems of lesser difficulty from geometry textbooks.
C
Hb
Ha
Tb H
Mb Ta
I Ma
G
O
A McTc Hc B
Example 1. Consider the problem to construct triangle ABC, given the locations
of its vertex A, the midpoint Mc of its side AB, and the midpoint Mb of its side
AC. Since the vertex A is already given, the problem is to construct the other two
vertices, B and C. One solution to this problem is straightforward: the vertex
B is constructed as a point symmetric to A wrt Mc and, similarly, the vertex C
as a point symmetric to A wrt Mb (Fig. 2).
Prolog in Automated Reasoning in Geometry 337
Mc Mb
B C
mb
1
G Mb
2
B Ma C
ma
circle over diameter AMc , or many other objects that are not needed for solving
this construction problem (Fig. 4). To achieve a higher level of control over the
objects constructed, before the search for a construction begins, general defi-
nitions are instantiated by relevant objects and added to the knowledge base.
Objects identified as relevant are the ones appearing in some definition or a
lemma. Similarly, general lemmas are instantiated by objects that satisfy their
hypotheses and their conclusions are added to the knowledge base. However,
primitive constructions are kept in non-instantiated form and get instantiated
only during the search for the construction (see Example 2, page 8).
X
Y
Mc Mb
B C
Fig. 4. One can apply many different primitive constructions to the set of known points
A, Mb , and Mc , some of them in different ways.
The construction starts from the points given, with the goal of construct-
ing the vertices A, B, and C of a triangle. The search procedure is systematic
and iterative: primitive constructions are ordered in a certain way2 and in each
2
Primitive constructions are ordered so that the ones used more frequently are listed
before the ones used rarely. Primitive constructions that are used more often are
identified by preliminary experimentation with different possible arrangements on a
large corpus of problems and by calculating aggregated statistics on the number of
times each one was used.
Prolog in Automated Reasoning in Geometry 339
step ArgoTriCS tries to apply some primitive construction to the objects already
constructed or given by the problem specification. If it succeeds, the search for
the next applicable primitive construction starts again from the first primitive
construction in a waterfall manner. A primitive construction is applicable if we
can find an instantiation such that all objects from its hypotheses are already
constructed, while the objects from its conclusion are not. Each successful appli-
cation of a primitive construction produces a new object, which is added to the
set of constructed objects maintained during the search. If all three vertices of
the triangle are constructed, the search terminates successfully. If at least one
vertex of the triangle is not yet constructed and no primitive constructions is
applicable, the search terminates unsuccessfully, meaning that this construction
problem cannot be solved using identified geometry knowledge. In order to make
the search procedure more efficient, additional techniques are employed, such as
limiting the objects being constructed to only those appearing in a definition or
a lemma involving an object not yet constructed.
where sratio is a relational symbol with six arguments denoting that the signed
ratio of the first four (points) is equal to the ratio of the last two (integers). As
another example, the median ma of a triangle ABC at the vertex A, described by
the Prolog term median(a,[b,c]), is defined as a line incident to the vertex A
and the midpoint Ma of the side BC (see Fig. 3):
def(median(a,[b,c]), [inc(a,median(a,[b,c])),
inc(midpoint([b,c],median(a,[b,c])))], ‘D17’).
lemma(sratio(a,centroid([a,b],c),a,midpoint([b,c]),2,3), ‘L55’).
update_known(Known,Unknown,Trace,Trace) :- sublist(Known,Unknown), !.
update_known(Known_old,Unknown,InputTrace,OutputTrace) :-
cons(Obj_known,Vals,Obj_constr,Props,NDG,DET,RuleId),
instantiate(Known_old,Obj_known),
all_defs_or_lemmas(Props,[],Props1),
relevant_not_constructed_some(Obj_constr,Props,Known_old),
not_in_list(Obj_constr,Known_old),
append(Known_old,Obj_constr,Known_new),
update_known(Known_new,Unknown,
[(Obj_known,Vals,Obj_constr,Props1,RuleId)|InputTrace],OutputTrace), !.
update_known(_,_,_,_,) :- !, write(‘Construction can not be found’), nl.
Advantages of Using Prolog. By using Prolog, we obtain for free the unification
and variable instantiation needed for the instantiation of general definitions and
lemmas and for the application of primitive construction. Maintaining the partial
construction, the set of constructed objects and the knowledge base, is also simple
to achieve. Namely, since the number of performed construction steps and the
number of constructed objects may vary, partial construction and the set of
constructed objects are represented as Prolog lists.
4
As seen for the signed ratio symbol, some symbols require numbers as arguments.
Prolog in Automated Reasoning in Geometry 341
Search tasks, such as the one appearing in the solving of construction prob-
lems, are well-handled using Prolog’s built-in query-driven search engine. Pro-
log’s automatic backtracking mechanism enables systematic testing of applicabil-
ity of different primitive constructions to different, already constructed objects.
This also makes it easy to obtain all of the solutions to a construction problem.
H
ha b
hb
B a C
Since there are three known points at the beginning: A, B, and H, three combi-
nations of points are tested for applicability: a line through points A and B, a
line through points A and H, and a line through points B and H. Since all three
lines considered (line c containing the side AB of a triangle, altitude ha from
vertex A and altitude hb from vertex B) are the important objects of a triangle,
this primitive construction is applicable to all mentioned combinations of points
and lines c, ha and hb are constructed, respectively. Once we have constructed
a line, the primitive construction that constructs a line through a known point
perpendicular to some known line is tested for applicability on all possible com-
binations of known points (A, B, H) and known lines (c, ha , hb ). However, some
lines of this type are not relevant for triangle construction problems, like the
line through point H perpendicular to line ha . The lines constructed are: the
line b as a perpendicular to line hb through point A, the line a as a perpendic-
ular to line ha through point B, and the line hc as a perpendicular to line c
through point H. At this point, the primitive construction that constructs an
intersection point of two lines is tested for applicability and the third vertex C
is constructed as an intersection point of lines a and b. At this point the search
for a construction terminates, as all three vertices of the triangle are known.
The construction trace automatically generated by ArgoTriCS consists of the
steps performed: for each construction step, seven elements are maintained: the
list of objects used for performing that step, the list of objects being constructed,
the properties that those objects satisfy, the list of non-degeneracy conditions,
the list of determination conditions, the label of the primitive construction per-
formed in the step and the list of definitions and lemmas used in the step. If the
search for construction succeeds, the obtained construction-trace is simplified by
keeping only the steps needed for performing the construction. The simplified
construction-trace of this construction problem is the following:
[
([A,H],[h_a],[inc(A,h_a),inc(H,h_a)],[],[not(eq_point(A,H))],W02,[D8,D3]),
([B,H],[h_b],[inc(B,h_b),inc(H,h_b)],[],[not(eq_point(B,H))],W02,[D9,D3]),
([A,h_b],[b],[perp(h_b,b),inc(A,b)],[],[],W10,[D9,GD01]),
([B,h_a],[a],[perp(h_a,a),inc(B,a)],[],[],W10,[D8,GD01]),
([a,b],[C],[inc(C,a),inc(C,b)],[not(parall(a,b))],
[not(eq_line(a,b))],W03,[D8,GD01])
]
The above construction trace can be exported to different formats; e.g. con-
struction in natural language, automatically generated by ArgoTriCS, is the
following:
1. Construct the line ha through the points A and H;
2. Construct the line hb through the points B and H;
3. Construct the perpendicular b to the line hb through the point A;
4. Construct the perpendicular a to the line ha through the point B;
5. Construct the intersection point C of the lines a and b.
Figure 5, automatically generated by ArgoTriCS, illustrates the construction
in the Euclidean and hyperbolic settings.
Prolog in Automated Reasoning in Geometry 343
4 Conclusion
In this paper we gave a brief overview of various geometry tools developed in Pro-
log and described in more details the system ArgoTriCS for automated solving
of construction problems. The system ArgoTriCS could have been implemented
in another programming language, but due to many conveniences, such as rapid
prototyping and easy maintainability, for us Prolog was an obvious choice.
Despite some beliefs that the golden age of Prolog passed, it still finds var-
ious areas of applications. We believe that because of already listed favourable
properties of Prolog that makes it suitable for automated reasoning applications,
it will keep its place in the years to come.
References
1. Bezem, M., Coquand, T.: Automating coherent logic. In: Sutcliffe, G., Voronkov,
A. (eds.) LPAR 2005. LNCS (LNAI), vol. 3835, pp. 246–260. Springer, Heidelberg
(2005). https://doi.org/10.1007/11591191_18
2. Buchberger, B.: An algorithm for finding the basis elements of the residue class
ring of a zero dimensional polynomial ideal. J. Symb. Comput. 41(3), 475–511
(2006)
3. Cervoni, L., Brasseur, J., Rohmer, J.: Simultaneously teaching Mathematics and
Prolog in School Curricula: a mutual benefit. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years. No.
13900. LNCS, Springer (July 2023)
4. Chou, S.C., Gao, X.S.: Automated reasoning in geometry. Handbook of Automated
Reasoning 5(1) (2001)
5. Chou, S.C., Gao, X.S., J.Z., Z.: Automated production of traditional proofs for
constructive geometry theorems. In: Proceedings of the Eighth Annual IEEE Sym-
posium on Logic in Computer Science LICS, pp. 48–56 (1993)
6. Chou, S.C., Gao, X.S., Zhang, J.Z.: Automated generation of readable proofs with
geometric invariants, ii. theorem proving with full-angles. J. Automated Reasoning
17 (1996)
7. Coelho, H., Pereira, L.M.: Automated reasoning in geometry theorem proving with
prolog. J. Autom. Reason. 2, 329–390 (1986)
8. Font, L., Cyr, S., Richard, P., Gagnon, M.: Automating the generation of high
school geometry proofs using prolog in an educational context. In: Electronic Pro-
ceedings in Theoretical Computer Science, vol. 313, pp. 1–16 (2020)
Prolog in Automated Reasoning in Geometry 345
Gopal Gupta(B) , Huaduo Wang, Kinjal Basu, Farhad Shakerin, Elmer Salazar,
Sarat Chandra Varanasi, Parth Padalkar, and Sopam Dasgupta
1 Introduction
Dramatic success of machine learning has led to a plethora of artificial intel-
ligence (AI) applications. The effectiveness of these machine learning systems,
however, is limited in several ways:
1. Lack of Interpretability: The models learned by machine learning sys-
tems are opaque, i.e., they are not comprehensible by humans. This is mainly
because these statistical machine learning methods produce models that are
complex algebraic solutions to optimization problems such as risk minimiza-
tion or likelihood maximization.
2. Lack of Explainability: These models are unable to produce a justification
for a prediction they compute for a new data sample.
3. Lack of Incrementality: These methods are unable to incrementally update
a learned model as new data is encountered.
4. Lack of Data Economy: These methods need large amounts of data to
compute a model. Humans, in contrast, are able to learn from a small number
of examples.
In this position paper we show that these problems are greatly alleviated if we
develop machine learning methods that learn default theories coded in logic pro-
gramming. The whole field of inductive logic programming (ILP) has been devel-
oped in which Horn clauses are learned from background knowledge, positive,
and negative examples [7]. Rules with negated goals in the body are also learned
in ILP as nonmonotonic logic programs and default rules [10,26]. Representing a
c The Author(s), under exclusive license to Springer Nature Switzerland AG 2023
D. S. Warren et al. (Eds.): Prolog: The Next 50 Years, LNAI 13900, pp. 346–358, 2023.
https://doi.org/10.1007/978-3-031-35254-6_28
Logic-Based Explainable and Incremental Machine Learning 347
2 Default Rules
Default Logic [20] is a non-monotonic logic to formalize commonsense reasoning.
A default D is an expression of the form
A : MB
Γ
which states that the conclusion Γ can be inferred if pre-requisite A holds and B
is justified. MB stands for “it is consistent to believe B”. If we restrict ourselves
to logic programming, then normal logic programs can encode a default theory
quite elegantly [12]. A default of the form:
α1 ∧ α2 ∧ · · · ∧ αn : M¬β1 , M¬β2 . . . M¬βm
γ
can be formalized as the normal logic programming rule:
where α’s and β’s are positive predicates and not represents negation-as-failure.
We call such rules default rules. Thus, the default
bird(X) : M ¬penguin(X)
f ly(X)
will be represented as the following default rule in normal logic programming:
fly(X) :- bird(X), not penguin(X).
We call bird(X), the condition that allows us to jump to the default conclusion
that X can fly, the default part of the rule, and not penguin(X) the exception
part of the rule.
We have developed the FOLD family of algorithms that output a model repre-
sented as default rules. Our inspiration is the FOIL algorithm of Quinlan [19].
The FOLD algorithm is a top-down rule-learning algorithm [25]. It starts with
the candidate rule
p(X,L) :- true.
where p(X,L) is the target predicate to learn, and states that record X has the
label L (for example, for the Titanic survival dataset, the target predicate will be
status(Passenger Id, S), where S is either survived or perished). It then
extends the body with a selected literal (predicate) from among the features so
as to cover maximum number of positive examples and avoid covering maximum
number of negative examples. The process of selecting a literal to add to the body
of the rule relies on heuristics. Traditionally, the Information Gain (IG) heuristic
has been used [19,25]. The IG heuristic was pioneered in the FOIL algorithm
[19] for learning logic programs and adapted by us for the FOLD algorithm
[25] to learn default theories. The FOLD algorithm learns a default theory, so
in the next step of learning, it swaps the remaining uncovered positive and
negative examples, and recursively applies the literal selection step to learn the
350 G. Gupta et al.
bird(tweety). bird(woody).
cat(kitty). penguin(polly).
bird(polly).
passengers as testing examples. The default rule-set learned by our FOLD algo-
rithm is shown below. It captures the underlying logic of the model, namely, that
female infant passengers, aged less than 5 in 3rd class who paid fare less than
or equal to 10.463 units, perished. Male passengers perished unless they paid a
fare in the range 26.25 to 26.387 units inclusive or they were younger than 12
with 2 or fewer relatives and were not without parents.
The rules represent a default theory with (nested) exceptions (ab1, ab2,
ab3). If a person did not perish, they survived. The model’s accuracy is 0.98,
precision is 1.0, recall is 0.96, and F1-score is 0.98 [1]. These rules are inter-
pretable by a human.
The learned program can be executed in a Prolog system or an answer set
programming system such as s(CASP) [2]. The s(CASP) system can also gener-
ate a proof tree that serves as an explanation for a prediction made for a given
input. The FOLD-SE system itself also generates an explanation. Explainability
is important for understanding the prediction made by a machine learned model.
The FOLD family of algorithms are comparable in accuracy to state-of-the-art
machine learning systems such as XGBoost [5] and Multi-Layer Perceptrons
(MLPs) [1]. XGBoost is a very popular machine learning method based on gra-
dient boosting, while MLPs are neural networks. The FOLD family of algorithms
is an order of magnitude faster than XGBoost and MLPs, and, in addition, is
explainable. The FOLD family algorithms perform minimal pre-processing of
the dataset (no need for one-hot encoding [1], for example). Table 1 shows the
performance of FOLD-SE compared to the most prominent rule-based machine
learning algorithm called RIPPER [6] on selected datasets (a more extensive
comparison can be found elsewhere [29]).
We can see that FOLD-SE outperforms RIPPER on the number of rules
generated and execution time. The number of rules may go down from approxi-
mately 180 to between 2 and 3 (for rain in Australia dataset, for example). The
results reported are an average over 10 runs, so the number of rules reported in
the table can be fractional. The scalability of FOLD-SE with respect to inter-
pretability, namely, the number of rules generated is small regardless of the size of
the dataset, shows the power of representing inductive generalizations as default
rules.
352 G. Gupta et al.
Default rules can also be used to make convolutional neural networks (CNNs)
explainable as done in the NeSyFOLD system [18], for example (CNNs are neural
networks designed for learning from image data).
sprinklerOff :- waterSupplyOff.
354 G. Gupta et al.
waterSupplyOff :- turnWaterSupplyOff.
turnWaterSupplyOff :- houseEmpty, waterLeakDetected.
waterLeakDetected :- water.
The
program is self-explanatory, and models fluents (sprinklerOn, sprinklerOff,
waterLeakDetected, fireDetected, fire, water, houseEmpty) and actua-
tors (turnWaterSupplyOff, turnSprinklerOn). The fluents fire, water, and
houseEmpty correspond to sensors. For simplicity, time is not considered. Note
that ‘fire’ means fire broke out in the house and ‘water’ means that a water leak
occurred in the house.
Given the theory above, if we add the fact fire. to it, we will find that the
property houseBurns defined above will succeed. This is because the occurrence
of fire eventually leads to sprinklers being turned on, which causes water to spill
on the floor, which, in turn, causes the flood protection system to turn on, and
turn off the water supply. We want houseBurns to fail. To ensure that it fails,
we have to recognize that the water supply should be turned off due to a water
leak in an empty house unless the house is on fire:
turnWaterSupplyOff :- houseEmpty, waterLeakDetected,
not fireDetected.
Therefore, a simple patch to the theory shown above will ensure that houseBurns
fails in all situations. By adding not fireDetected we are subtracting knowledge
preventing houseBurns from succeeding.
Note that to solve the theory revision problem we should be able to ana-
lyze the resulting search tree both when a query succeeds or when it fails. The
s(CASP) system provides access to the search tree through the justification tree
it produces [3]. For a failed query, we obtain the (successful) search tree of
the negated query, and analyze its corresponding justification tree. There has
been a significant amount of work done on theory revision (or knowledge refine-
ment) in the context of logic [11]. Most of this work assumes that the theory
is expressed using Horn clauses. Adding negation-as-failure through answer set
programming (along with the ability to obtain proof-trees for queries through
s(CASP)) allows for a more powerful theory revision framework [4]. This work
is a first step towards building more powerful theory revision frameworks based
on using logic programming, specifically, goal-directed answer set programming.
5 Explanation-Based Learning
Machine learning algorithms require large amounts of data to learn a model. Ide-
ally, a small amount of data should be sufficient for learning, including learning
or generalizing from just one example. In order to do so, however, background
Logic-Based Explainable and Incremental Machine Learning 355
and the domain theory explaining why it is safe to stack obj1 on obj2
safeToStack(obj1,obj2) :- lighter(obj1,obj2).
lighter(obj1,obj2) :- weight(obj1, W1), weight(obj2,W2), W1 < W2.
weight(X, W) :- volume(X, V), density(X,D), W =:= V*D.
weight(X,5) :- type(X, endtable).
The operational predicates are type, volume, density, on, <, >, =:=. Par-
tially evaluating the predicate call safeToStack(obj1,obj2) against the domain
theory while keeping only the operational predicates and generalizing will allow
us to learn the general rule:
safeToStack(X,Y) :- volume(X,V), density(X,D), WX =:= V*D,
type(Y, endtable), WX < 5.
Essentially, we have learned a rule that tells us when two objects can be safely
stacked, defined in terms of properties of the two objects. While this may appear
as simple partial evaluation, more intelligent reasoning can be incorporated by
bringing in additional background knowledge. For example, we know that an
end-table is similar to a center-table with respect to the stacking property, and
so we may further generalize our rule to work for more types of tables. We could
generalize it even further to any heavy table-like structure with a flat top, and
so on.
356 G. Gupta et al.
EBL can be made more powerful and flexible by representing the domain
theory in ASP. For example, constraints can be stated to block simplifica-
tion along certain evaluation paths during partial evaluation. More general
generalizations—represented as default rules—can be made that also account for
exceptions. In the example above, while generalizing, we may want to rule out
tables that have small-sized surface top, as there is a danger of tipping over upon
stacking. We may also want to avoid stacking on tables made of fragile material
(another exception). Of course, more knowledge about a table’s dimensions, the
material it is made of, etc., will have to be added to achieve this. Generalizing
to ASP, however, would require developing a partial evaluator for answer set
programming under some operational semantics. The s(CASP) system provides
one such operational semantics.
6 Conclusion
Default rules are an elegant way to represent knowledge underlying machine
learning models. We have developed very efficient machine learning algo-
rithms (e.g., FOLD-SE described above) that are competitive with state-of-
the-art methods. Likewise, knowledge represented as default rules can be incre-
mentally updated—via theory revision—as well as generalized/specialized—via
explanation-based learning. It is our position that logic-based approaches—
centered on representing knowledge as default theories—can lead to the design
of machine learning systems that can be competitive with state-of-the-art tradi-
tional machine learning systems, while providing advantages of interpretability,
explainability, incrementality, and data economy.
References
1. Aggarwal, C.C.: Neural Networks and Deep Learning - A Textbook. Springer,
Cham (2018). https://doi.org/10.1007/978-3-319-94463-0
2. Arias, J., et al.: Constraint answer set programming without grounding. TPLP
18(3–4), 337–354 (2018)
3. Arias, J., et al.: Justifications for goal-directed constraint answer set programming.
In: Proceedings 36th International Conference on Logic Programming (Technical
Communications), vol. 325. EPTCS, pp. 59–72 (2020)
4. Basu, K., et al.: Symbolic reinforcement learning framework with incremental learn-
ing of rule-based policy. In: Proceedings of ICLP GDE’22 Workshop, vol. 3193.
CEUR Workshop Proceedings. CEUR-WS.org (2022)
5. Chen, T., Guestrin, C.: XGBoost: a scalable tree boosting system. In: Proceedings
of the 22nd ACM SIGKDD. KDD ’16, San Francisco, California, USA, pp. 785–794
(2016). ISBN 978-1-4503-4232-2
Logic-Based Explainable and Incremental Machine Learning 357
6. Cohen, W.W.: Fast effective rule induction. In: Proceedings of ICML, San Fran-
cisco, CA, USA, pp. 115–123 (1995)
7. Cropper, A., Dumancic, S.: Inductive logic programming at 30: a new introduction.
arXiv:2008.07912 (2020)
8. DeJong, G., Mooney, R.J.: Explanation-based learning: an alternative view. Mach.
Learn. 1(2), 145–176 (1986)
9. Dietz Saldanha, E.A., Hölldobler, S., Pereira, L.M.: Our themes on abduction
in human reasoning: a synopsis. In: Abduction in Cognition and Action: Logical
Reasoning, Scientific Inquiry, and Social Practice, pp. 279–293 (2021)
10. Dimopoulos, Y., Kakas, A.: Learning non-monotonic logic programs: learning
exceptions. In: Lavrac, N., Wrobel, S. (eds.) ECML 1995. LNCS, vol. 912, pp.
122–137. Springer, Heidelberg (1995). https://doi.org/10.1007/3-540-59286-5 53
11. Richards, B.L., Mooney, R.J.: Automated refinement of first-order horn-clause
domain theories. Mach. Learn. 19(2), 95–131 (1995)
12. Gelfond, M., Kahl, Y.: Knowledge Representation, Reasoning, and the Design of
Intelligent Agents: the Answer-Set Programming Approach. Cambridge University
Press, Cambridge (2014)
13. van Harmelen, F., Bundy, A.: Explanation-based generalisation = partial evalua-
tion. Artif. Intell. 36(3), 401–412 (1988)
14. Laber, E., Molinaro, M., Pereira, F.M.: Binary partitions with approximate mini-
mum impurity. In: by Dy, J., Krause, A. (eds.) Proceedings of ICML, vol. 80, pp.
2854–2862. Proceedings of Machine Learning Research. PMLR (2018)
15. Lundberg, S.M., Lee, S.-I.: A unified approach to interpreting model predictions.
In: Advances in Neural Information Processing Systems, pp. 4765–4774 (2017)
16. Minton, S., et al.: Explanation-based learning: a problem solving perspective. Artif.
Intell. 40(1–3), 63–118 (1989)
17. Mitchener, L., et al.: Detect, understand, act: a neuro-symbolic hierarchical rein-
forcement learning framework. Mach. Learn. 111(4), 1523–1549 (2022)
18. Padalkar, P., Wang, H., Gupta, G.: NeSyFOLD: a system for generating logic-based
explanations from convolutional neural networks. arXiv:2301.12667 (2023)
19. Quinlan, J.R.: Learning logical definitions from relations. Mach. Learn. 5, 239–266
(1990)
20. Reiter, R.: A logic for default reasoning. Artif. Intell. 13(1–2), 81–132 (1980)
21. Ribeiro, M.T., Singh, S., Guestrin, C.: “Why should I trust you?”: explaining the
predictions of any classifier. In: Proceedings of KDD, pp. 1135–1144. ACM (2016)
22. Shakerin, F.: Logic programming-based approaches in explainable AI and natural
language processing. Ph.D. thesis, Department of Computer Science, The Univer-
sity of Texas at Dallas (2020)
23. Shakerin, F., Gupta, G.: Induction of non-monotonic logic programs to explain
boosted tree models using LIME. In: Proceeding of AAAI, pp. 3052–3059. AAAI
Press (2019)
24. Shakerin, F., Gupta, G.: Induction of non-monotonic rules from statistical learning
models using high-utility itemset mining. arXiv:1905.11226 (2019)
25. Shakerin, F., Salazar, E., Gupta, G.: A new algorithm to automate inductive learn-
ing of default theories. TPLP 17(5–6), 1010–1026 (2017)
26. Srinivasan, A., Muggleton, S.H., Bain, M.: Distinguishing exceptions from noise in
non-monotonic learning. In: Proceedings of International Workshop on Inductive
Logic Programming (1992)
27. Stenning, K., van Lambalgen, M.: Human Reasoning and Cognitive Science. MIT
Press, Cambridge (2008)
358 G. Gupta et al.
28. Wang, H., Gupta, G.: FOLD-R++: a scalable toolset for automated inductive
learning of default theories from mixed data. In: Hanus, M., Igarashi, A. (eds.)
FLOPS 2022. LNCS, vol. 13215, pp. 224–242. Springer, Cham (2022). https://doi.
org/10.1007/978-3-030-99461-7 13, isbn: 978-3-030-99460-0
29. Wang, H., Gupta, G.: FOLD-SE: scalable explainable AI (2022)
30. Wang, H., Gupta, G.: FOLD-TR: a scalable and efficient inductive learning algo-
rithm for learning to rank (2022). arXiv: 2206.07295
31. Wang, H., Shakerin, F., Gupta, G.: FOLD-RM: efficient scalable explainable AI.
TPLP 22(5), 658–677 (2022)
32. Wusteman, J.: Explanation-based learning: a survey. Artif. Intell. Rev. 6(3), 243–
262 (1992)
33. Yang, Z., Ishay, A., Lee, J.: NeurASP: embracing neural networks into answer set
programming. In: Bessiere, C. (ed.) IJCAI 2020, pp. 1755–1762 (2020)
Reflections on Automation, Learnability
and Expressiveness in Logic-Based
Programming Languages
Paul Tarau(B)
1 Introduction
Our short answer is that we need to focus on closeness to natural language, learn-
ability, flexible execution mechanisms and highly expressive language constructs.
© The Author(s), under exclusive license to Springer Nature Switzerland AG 2023
D. S. Warren et al. (Eds.): Prolog: The Next 50 Years, LNAI 13900, pp. 359–371, 2023.
https://doi.org/10.1007/978-3-031-35254-6_29
360 P. Tarau
We will elaborate in the next sections on why these features matter, with
hints on what language constructs are needed for implementing them.
As an application, we will show the effectiveness of some of our proposed
language constructs via definite clause-grammar based prompt generators for
today’s text-to-text and text-to-image deep learning systems.
2 The Challenges
Prolog shares some of those but it is usually via separate libraries or seman-
tically more intricate definitions (e.g., setof with care about how variables are
quantified as an implementation of set comprehensions). We will explore in the
next sections some language constructs covering features where logic-based lan-
guages are left behind.
362 P. Tarau
parent or ancestor P P.
parent or ancestor P A : ancestor of P A.
A Finite Function API. Finite functions (tuples, lists, dictionaries, sets) are
instrumental in getting things done with focus on the problem to solve rather
than its representation in the language.
In Natlog they are directly borrowed from Python and in systems like SWI-
Prolog dictionaries are a built-in datatype. They can be easily emulated in Prolog
but often with a different complexity than if natively implemented.
1
at https://github.com/ptarau/natlog, ready to install with “pip3 install natlog”.
2
but not closer, as unnecessary verbosity can hinder expressiveness.
Reflections on Automation, Learnability and Expressiveness 363
This simplification (as implemented in Natlog) would also make type checking
easier and enable type inference to propagate from the built-ins to predicates
sharing their arguments as a convenient mechanism to implement gradual typing.
While constraint solvers and related coroutining primitives are present in most
widely used logic-based languages, first class logic engines, seen as on-demand
reflection of the full execution mechanism, as implemented in BinProlog [9], have
been adopted only in SWI Prolog relative recently3 . Interestingly, similar con-
structs have been present as far as in [4], where they were efficiently implemented
at abstract machine level.
One can think about First Class Logic Engines as a way to ensure the full
meta-level reflection of the execution algorithm. As a result, they enable on-
demand computations in an engine rather than the usual eager execution mech-
anism of Prolog.
We will spend more time on them as we see them as “the path not taken” that
can bring significant expressiveness benefits to logic-based languages, similarly
3
https://www.swi-prolog.org/pldoc/man?section=engines.
364 P. Tarau
We will sketch here a few expressiveness improvements First Class Logic Engines
can bring to a logic-based programming language,
collect_all_ _ no ().
collect_all_ E (the X) (X Xs) : ask E Y, collect_all_ E Y Xs.
Note that the infinite loop’s results, when seen from the outside, show up as
a stream of answers as if produced on backtracking. With help of the library
predicate take, we extract the first 5 (seen as a Python dictionary with name
“X” of the variable as a key and the nested tuple representation of Natlog’s list
as a value), as follows:
?- fibo 5 Xs?
ANSWER: {'Xs': (1, (1, (2, (3, (5, ())))))}
With magic wands on a lease from text-to-text generators like GPT3 [2] and text-
to-image generators like DALL-E [7] or Stable Diffusion [12] we can introduce
Definite Clause Grammars (DCGs) as prompt generators for such systems.
As examples of the natural synergy between declarative constructs of a logic-
based language and the declarative features of today’s deep learning systems,
we will next overview Natlog applications for text-to-text and text-to-image
generation. We refer to the Natlog code4 and its Python companion5 for full
implementation details.
4
see https://github.com/ptarau/natlog/blob/main/apps/natgpt/chat.nat.
5
see https://github.com/ptarau/natlog/blob/main/apps/natgpt/chat.py.
366 P. Tarau
GPT3 is basically a text completion engine, which, when given an initial segment
of a sentence or paragraph as a prompt, it will complete it, often with highly
coherent and informative results.
Thus, to get from GPT3 the intended output (e.g., answer to a question,
elations extracted from a sentence, building analogies, etc.) one needs to rewrite
the original input into a prompt that fits GPT3’s text completion model.
We will use here Natlog’s syntactically lighter Definite Clause Grammars,
with one or more terminal symbols prefixed by “@” and “=>” replacing Prolog’s
“-->”. A prompt generator with ability to be specialized for several “kinds” of
prompts is described by the DCG rule:
prompt Kind QuestText => prefix Kind, sent QuestText, suffix Kind.
The predicate sent takes a question sentence originating from a user’s input and
maps it into a DCG non-terminal transforming cons-list Ws1 into cons-list Ws2:
sent QuestText Ws1 Ws2 :
`split QuestText List, to_cons_list List Ws, append Ws Ws2 Ws1.
The predicate query takes the DCG-generated prompt derived from user ques-
tion Q and converts it back to a string passed to GPT’3 completion API by a
call to the function complete, implemented in Python, with its answer returned
in variable A.
query Kind Q A:
prompt Kind Q Ps (), to_list Ps List, `join List P, `complete P A.
analogy X Y Z A:
trigger X Y Z Ps (), to_list Ps List, `join List P, `complete P A.
Reflections on Automation, Learnability and Expressiveness 367
We will next show interaction examples for all these use cases.
First, question answering:
?- query question 'why is logic programming declarative' R?
ANSWER: {'R': 'logic programming is declarative because it expresses the
logic of a problem without describing its control flow. This means that
the programmer does not need to specify the order in which the operations
should be performed, as the logic programming language will determine
the most efficient way to solve the problem.'}
Next, relation extraction. Note that after some preprocessing, the extracted
triplets can be used as building blocks for knowledge graphs.
?- query relation 'the quick brown fox jumps over the lazy dog' R?
ANSWER: {'R':'"quick brown fox",verb is "jumps" and object is "lazy dog"'}
Finally, some examples of analogical reasoning that show GPT3 finding the
missing component and explaining its reasoning.
?- analogy car wheel bird A?
ANSWER: {'A': 'wing by analogy. This is because both car and wheel
are used for transportation, while bird and wing are used for flight.'}
Fig. 1. paint ‘photo of a cat playing on the shiny moon with a trumpet’.
The Natlog DCG, in generation mode, will iterate over possible styles and
content elements of a desired painting as in the following example:
image => style, subject, verb, object.
This generates text ready to be passed via the OpenAI Python APIs to
DALL.E:
?- image Words (), `to_tuple Words Ws, #writeln Ws, nl, fail.
photorealistic rendering of shiny robot walking on planet Mars
with high hills and a blue purse
photorealistic rendering of shiny robot walking on planet Mars
with a sombrero hat
.....
depending on the purse vs. sombrero hat picked by the DCG, as can be seen
in the generated images6 .
6 Related Work
6
at https://github.com/ptarau/natlog/tree/main/apps/natgpt/pics.
7
https://chat.openai.com/chat.
370 P. Tarau
Acknowledgments. These reflections have been inspired by the live and deep-
probing Prolog’50 discussions lead by Bob Kowalski and Veronica Dahl with focus on
logical thinking and logic-based programming as well as on approaches to make logic-
based programming accessible to newcomers, including use cases for a first-contact
introduction to computing. I am thankful to the participants of these meetings for
sharing their thoughts on both the last 50 years and the next 50 years of logic program-
ming. Finally, many thanks go to the reviewers of the paper for their careful reading
and constructive suggestions that helped clarify and substantiate key concepts covered
in the paper.
References
1. Andersen, C., Swift, T.: The Janus System: a bridge to new prolog applications.
In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski, R., Rossi, F.
(eds.) Prolog - The Next 50 Years. No. 13900 in LNCS, Springer (July 2023)
2. Brown, T., et al.: Language models are few-shot learners. In: Larochelle, H.,
Ranzato, M., Hadsell, R., Balcan, M., Lin, H. (eds.) Advances in Neural Informa-
tion Processing Systems. vol. 33, pp. 1877–1901. Curran Associates, Inc. (2020).
https://proceedings.neurips.cc/paper/2020/file/1457c0d6bfcb4967418bfb8ac142f6
4a-Paper.pdf
3. Genesereth, M.: Prolog as a knowledge representation language the nature and
importance of prolog. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M.,
Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years. No. 13900 in LNCS,
Springer (July 2023)
4. Hermenegildo, M.V.: An abstract machine for restricted AND-parallel execution
of logic programs. In: Shapiro, E. (ed.) ICLP 1986. LNCS, vol. 225, pp. 25–39.
Springer, Heidelberg (1986). https://doi.org/10.1007/3-540-16492-8 62
5. Manhaeve, R., Dumancic, S., Kimmig, A., Demeester, T., De Raedt, L.: Deep-
problog: neural probabilistic logic programming. In: Bengio, S., Wallach, H.,
Larochelle, H., Grauman, K., Cesa-Bianchi, N., Garnett, R. (eds.) Advances in
Neural Information Processing Systems 31, pp. 3749–3759. Curran Associates, Inc.
(2018). http://papers.nips.cc/paper/7632-deepproblog-neural-probabilistic-logic-
programming.pdf
6. Ouyang, L., et al.: Training language models to follow instructions with human
feedback (2022). https://doi.org/10.48550/ARXIV.2203.02155, https://arxiv.org/
abs/2203.02155
7. Ramesh, A., et al.: Zero-shot text-to-image generation (2021). https://doi.org/10.
48550/ARXIV.2102.12092, https://arxiv.org/abs/2102.12092
8. Kowalski, R., Fariba Sadri, M.C., Davila, J.: Combining Logic Programming
and Imperative Programming in LPS. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years.
No. 13900 in LNCS, Springer (July 2023)
9. Tarau, P.: The BinProlog Experience: architecture and implementation choices
for continuation passing prolog and first-class logic engines. Theory Pract. Logic
Program. 12(1–2), 97–126 (2012). https://doi.org/10.1007/978-3-642-60085-2 2
10. Tarau, P.: Natlog: a Lightweight logic programming language with a neuro-
symbolic touch. In: Formisano, A., et al. (eds.) Proceedings 37th International
Conference on Logic Programming (Technical Communications), 20–27th Septem-
ber 2021 (2021)
Reflections on Automation, Learnability and Expressiveness 371
11. Vaswani, A., et al.: Attention is all you need. In: Guyon, I., et al. (eds.) Advances
in Neural Information Processing Systems, vol. 30. Curran Associates, Inc. (2017).
https://proceedings.neurips.cc/paper/2017/file/3f5ee243547dee91fbd053c1c4a845
aa-Paper.pdf
12. Vision, C.M., at LMU Munich, L.R.G.: Stable Diffusion (2018–2022). https://
github.com/CompVis/stable-diffusion
13. Warren, D.S.: Introduction to prolog. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years.
No. 13900 in LNCS, Springer (July 2023)
Prolog for Scientific Explanation
1 Introduction
“Explanation” can mean many different things. Here, however, we focus on sci-
entific explanation of the abductive kind. The term “abduction” was originally
coined by Charles Sanders Peirce who contrasted this mode of reasoning with
deduction and induction. Informally, abduction involves finding a hypothesis,
among candidates, that can explain a certain observation [17]. An extensive
body of research has developed the theoretical foundations of abduction in logic
programs, and to date, there are a number of algorithms that perform this kind
of inference (for an overview see [16,26]). More recently, abduction has also been
implemented in probabilistic logic programs, enabling inference with uncertain
information, e.g. [3,4,9,43].
Scientific explanation, in the traditional sense, employs the same kind of
deductive argument as in abductive logic programming, but it involves addi-
tional and important qualifications. Simplifying a bit, and barring some tricky
philosophical issues, scientific explanation can be characterized as follows, e.g.
[1,5,53]:
– L is a set of definite clauses that describe general causal laws which approxi-
mately mirror states of affairs (often probabilistically)
– C is a set of atoms that describe initial conditions which are necessary for
the occurrence of an effect (in a causal relation)
– A is a set of atoms that define potential candidate explanations (they are
abducible by having different possible groundings)
– O is an atom that describes an observation
the law L in the disease/2 predicate and the initial conditions in C, Δ entails
the observation O.
2 Related Work
The general idea to use abductive logic programming for scientific explanation
has been raised by others, e.g. [1,15,18]. Already in the 80’s, David Poole and his
colleagues saw the similarities between this technical solution and the scientific
problem when they were developing the Theorist system [36].
There are a number of practical use cases where abduction from logic pro-
grams has been leveraged for scientific explanation, mostly in biology. The Robot
Scientist [27] is an autonomous system that conducts genetic research. It gener-
ates hypotheses, runs experiments, evaluates data to falsify inconsistent hypothe-
ses, and then repeats this cycle. Abductive inference is used to automatically
Prolog for Scientific Explanation 375
formulate hypotheses for testing (with a version of Progol [35]). The Gene Path
system [54] abduces constraints in genetic networks and explains its answers
on the basis of expert knowledge and experimental data. It could be used by a
geneticist when planning experiments and reasoning about their outcomes. Sim-
ilarly, the ARNI system [31] employs abduction from a logical representation of
molecular interactions to explain how genes are affected in a causal network. In
a related discipline, medicine, [8] show how abductive logic programming can be
used to aid physicians to prescribe correct treatments for potential heart failure.
More information about Prolog applications in biology can be found in [13] in
this volume.
Proof trees play an essential role in our view of scientific explanation, since
we suggest that they are explanations. There are a number of technical solutions
that produce this kind of output. The s(CASP) system [2], which is available as
a SWI Prolog module and on the web with SWISH [51], generates justification
trees from answer set programs. The output can even contain natural language.
The SLDNF Draw system [19] produces high quality proof trees via LATEX
output. SLDNF Draw is also available as a SWI Prolog module and on the web
at the CPLINT [39] site. The possibility to obtain proof trees additionally exists
in TAU Prolog [48], which is an open-source Prolog interpreter in Javascript.
3 A Theory Example
1
Another way of encoding this would be to have a standard Prolog database where
all parent-child relations are enumerated, e.g. parent(a, b), parent(b, c) and
so on.
376 J.-C. Rohner and H. Kjellerstrand
and parent reproduction explain offspring traits (and not the other way around),
because offspring traits is the thing that is being entailed. Compare this to a
purely mathematical formalization which, given the description of explanation,
can’t differentiate between explanandum and explanans (that which explains
the explanandum). In O = C1 ∗ C2 ∗ ... ∗ Cn ∗ A1 ∗ A2 ∗ ... ∗ Am , for example,
the value of any one of O, C1 , C2 , Cn , A1 , A2 , ..., Am is entailed by the remaining
values. With this syntax, there is no way of saying that offspring traits is the
explanandum and that parent traits and parent reproduction are the explanans.
Another strength of Prolog, besides asymmetry, is that it naturally repre-
sents multiple many-one relations. An important insight from the debate on
causality, which is essential in scientific explanation, is that things often have
multiple causes. John Mackie’s take on causality [30], for example, is that any
given effect can occur because of several clusters of factors, where multiple fac-
tors within a single cluster are jointly necessary and sufficient for the effect to
occur, while no individual cluster is, in itself, necessary for the effect to occur. In
the natural selection program, for example, one cluster of factors that explains
offspring traits consists of parent traits and parent reproduction; within this clus-
ter these factors are necessary and sufficient. But it is possible to come up with
other clusters of factors that can explain why individuals have certain traits,
e.g. imitating friends that have these traits. Mackie’s pattern nicely matches
the structure of a Prolog program, where different clauses with the same head
correspond to different clusters of factors, and where the literals in the body of
each clause correspond to factors within a cluster. So if O is something that we
want to causally explain and the Cs and As are conditions that can make O
happen, we could have the three clauses O ← C1 , C2 , A1 and O ← C3 , A2 and
O ← C4 , C5 , A3 .
Before ending this section, we want to make an important remark: Being
able to represent this pattern is merely a semantic tool. Establishing cause-
effect hinges on empirically showing that the probability of O increases when
the probability of the Cs and As increase (everything else being equal) and that
the Cs and As precede O [44].
[12]. Encoding properties and relations that involve qualitative objects is harder
in a scheme that only allows large vectors of numbers.
There are specific advantages with using probability equations in a theory
(like in the natural selection example). The first advantage is that we can use
strong negation (see for example [49]) and distinguish between situations in
which a theory entails that O is false, in the sense that O is not the case,
and situations in which a theory does not entail O, in the sense that O cannot
be deduced from the theory. Consider the second clause in Fig. 2 and suppose
we had inhabits(adam, h1, 1), hasTrait(adam, t1, 1) and adaptiveIn(t1,
h1, 0) as initial conditions C. With this information the program would entail
reproduces(adam, 0); i.e. it is not the case that Adam reproduces. With the
same information, however, the query ?-reproduces(eve, X) would yield false
(meaning that it is false that the program entails this goal, since the system
doesn’t know anything about Eve). Being able to describe why things do not
happen (in addition to why they do happen) is an important part of scientific
explanation. Adding probability equations to a theory complements negation as
failure, p is false in the sense that the theory does not entail p, with strong
negation, p is false in the sense that the theory entails that p is not the case.
A second advantage, which is specific to CLP(R) and other constraint-based
systems (and missing in is/2), is that it is possible to get unique solutions to
equations that have multiple unknowns (more information about the advantages
of constraint logic programming can be found for example in [24]). In relation
to explanation, this means that we can use scientific laws to explain why a phe-
nomenon occurs (or does not occur), even when the probabilities of certain ini-
tial conditions are unknown. Consider the second clause in the natural selection
example and suppose we had inhabits(adam, h1, _), hasTrait(adam, t1, _)
and adaptiveIn(t1, h1, 0). Even if the probabilities of the first two facts are
unknown, we get reproduces(adam, 0), because a product that involves zero is
always zero. More examples of this technique can be found in the repository for
Theory Toolbox 2 [41].
4.3 Homoiconicity
Prolog is a homoiconic language, which means that Prolog programs can perform
computations on Prolog programs, including programs that represent scientific
theories. There are at least two advantages of homoiconicity with respect to
scientific explanation.
The first advantage is that we can write meta-interpreters that yield detailed
proof trees for all the steps that lead from laws L, initial conditions C, and
abduced facts Δ, to observations O. Because we have suggested that scientific
explanations consist of proof trees, this is essential. Let’s consider an example
observation that we wish to explain. Evidently, most humans are equipped with
some form of hair (in addition to several other traits). How can the natural selec-
tion theory in Fig. 2 explain this observation? Intuitively it seems that having
hair should be a heritable trait that is adaptive. Is this the case? Fig. 3 shows
a query q1 with prove/3. The observation O that we want to explain is that
Prolog for Scientific Explanation 379
an individual in generation 3 has hair; hence the probability for this is 1 (gen-
eration 3 is arbitrary). In the query the initial conditions C are that anybody
inhabits a human ecological niche and that any trait is heritable (an anonymous
variable unifies with anything). The abducible predicates in the list A describe
some things that are relevant to this particular example: Things happen with
probability 0 or 1, there are some traits, and a trait might or not be adaptive
in the ecological niche. The right part of Fig. 3 shows a proof tree for this goal,
which constitutes an explanation according to the theory.
First, note that the probability of each involved causal factor is high (each one
is 1). This makes sense given that having a trait was conditional on a conjunction
of factors. An interesting and, perhaps, unexpected aspect of this explanation is
that the adaptive value of having hair is not involved in explaining why people
have hair. Having hair is conditional on reproductive potential, and reproductive
potential - in this case - is instead supported by another trait (being endother-
mic). Incidentally, the explanation in Fig. 3 actually shows that having hair is a
so called vestigial trait: A trait that is inherited but that does not have any adap-
tive value. The query q1 also generates additional proofs; among others, one in
380 J.-C. Rohner and H. Kjellerstrand
which the adaptive value of having hair is involved in deducing the observation.
The reason for this incoherence will become apparent next.
The second advantage of homoiconicity is that we can write Prolog programs
that perform rational theory evaluation, i.e. appraising theories using other cri-
teria than empirical fit. For example, examining if a theory is free of internal
contradictions or examining how general it is [37]. In a general Prolog system,
such as Theory Toolbox 2 [41], it is fairly straightforward to write predicates
that perform such analyses. Let’s consider a final query example on the natural
selection theory in Fig. 2 that checks if the theory makes conflicting predictions.
In the query in Fig. 4 we use the predicate incoherence/7 (from Theory Toolbox
2). This predicate checks if a theory entails different probabilities for a certain
goal and, if so, shows explanations for the conflicting answers. As shown in Fig. 4,
there is an incoherence, and the explanation is that reproductive potential in the
individual in generation 1 can either occur because they have hair or because
they are endothermic. In the former case the probability of having hair for the
individual in generation 2 is 0; in the latter case the probability is 1. This means
that the theory has to be amended somehow. In what way is another matter.
4.4 Transparency
A final advantage of Prolog, and other symbolic approaches to AI, is that knowl-
edge representations in this format are often transparent and, potentially, under-
standable by humans, e.g. [6]. Clearly, this is an important desideratum for
scientific theories, which should be open to introspection and public scrutiny.
Scientific theories are often used to make decisions with real life implications,
e.g. in medicine, so transparent explanations become even more important. In
general, understanding why an argument is an explanation is an important step
towards explainable AI, e.g. [25]. Hopefully, Prolog can play an important role
in the progress towards achieving this long-term goal.
It is interesting to speculate about what the future holds for Prolog when it
comes to scientific explanation. When thinking about this, it seems that the
importance of induction often comes up. Because scientific theories are supposed
to reflect states of affairs, they should be based on empirical data. So, if we
want to be able to enjoy the benefits of Prolog for theory representation, and
therefore explanation, high quality algorithms for inducing programs from data
are essential.
To date, there are several available systems for inductive logic programming,
some of which have achieved important milestones like predicate invention, find-
ing recursive programs, and finding probabilistic programs (for overviews see
[10,38]). Certain features of such systems are probably important for wider adop-
tion in the scientific community (outside machine learning). The ability to handle
noise, the ability to induce numerical relations, good documentation, and easy
Prolog for Scientific Explanation 381
installation come to mind. And a dream scenario would be if these features can
be combined with predicate invention and recursion.
Another important area for development, towards the goal of using Prolog
for scientific explanation, is the integration of inductive logic programming with
other machine learning schemes, like deep neural networks. Being able to jointly
use neural networks, for encoding unstructured data (e.g. from natural language
texts) and inductive logic programming, for learning more complex relations
from structured data, leveraging background knowledge, seems very powerful.
Especially considering that a large amount of information, besides empirical
data, already exists in the form of scientific papers. There is already progress
when it comes to integrating neural networks and symbolic learning approaches
e.g. [32]. At the same time, we do not think that the importance of neural
networks should be overstated; at least not in the context of inducing scientific
theories. The primary advantage of neural networks is their ability to deal with
382 J.-C. Rohner and H. Kjellerstrand
6 Conclusions
We have discussed the advantages of Prolog for scientific abductive explanation,
suggesting that such explanations are proof trees for the derivation of an obser-
vation from a causal theory and initial conditions. Prolog can capture asym-
metric many-one relations, represent complex relational concepts that involve
qualitative as well as quantitative objects, flexibly reason with both known and
unknown information (e.g. probabilities), generate explanations in the form of
proof trees and perform rational theory evaluation. And beyond this, Prolog also
supports induction and deduction. All in all, our general impression is that all
the technical components needed for using Prolog as a great scientific tool are
in place. Maybe the keys to wider adoption in the scientific community are easy
access, good documentation, and user support. Prolog-on-the-web systems like
SWISH and CPLINT, are important steps in this direction.
References
1. Aliseda-Llera, A.: Logics in scientific discovery. Found. Sci. 9, 339–363 (2004).
https://doi.org/10.1023/B:FODA.0000042847.62285.81
2. Arias, J., Carro, M., Chen, Z., Gupta, G.: Justifications for goal-directed constraint
answer set programming. Electron. Proce. Theor. Comput. Sci. 325, 59–72 (2020).
https://doi.org/10.4204/EPTCS.325.12
3. Azzolini, D., Bellodi, E., Ferilli, S., Riguzzi, F., Zese, R.: Abduction with probabilis-
tic logic programming under the distribution semantics. Int. J. Approx. Reason.
142, 41–63 (2022). https://doi.org/10.1016/j.ijar.2021.11.003
4. Bellodi, E., Gavanelli, M., Zese, R., Lamma, E., Riguzzi, F.: Nonground abductive
logic programming with probabilistic integrity constraints. Theory Pract. Logic
Program. 21(5), 557–574 (2021). https://doi.org/10.1017/S1471068421000417
5. Bunnin, N., Yu, J.: E. In: The Blackwell Dictionary of Western Philosophy, chap.
5, pp. 197–245. Wiley, Hoboken (2004). https://doi.org/10.1002/9780470996379
6. Caroprese, L., Vocaturo, E., Zumpano, E.: Argumentation approaches for explain-
able AI in medical informatics. Intell. Syst. Appl. 16, 200109 (2022). https://doi.
org/10.1016/j.iswa.2022.200109
7. Carrault, G., Cordier, M.O., Quiniou, R., Wang, F.: Temporal abstraction
and inductive logic programming for arrhythmia recognition from electrocardio-
grams. Artif. Intell. Med. 28(3), 231–263 (2003). https://doi.org/10.1016/s0933-
3657(03)00066-6
8. Chen, Z., Salazar, E., Marple, K., Gupta, G., Tamil, L., Cheeran, D., Das, S., Amin,
A.: Improving adherence to heart failure management guidelines via abductive
reasoning. Theory Pract. Logic Program. 17(5–6), 764–779 (2017). https://doi.
org/10.1017/S1471068417000308
Prolog for Scientific Explanation 383
27. King, R.D., et al.: Functional genomic hypothesis generation and experimentation
by a robot scientist. Nature 427(6971), 247–52 (2004). https://doi.org/10.1038/
nature02236
28. Klingspor, V., Morik, K.J., Rieger, A.D.: Learning concepts from sensor data of
a mobile robot. Mach. Learn. 23(2–3), 305–332 (1996). https://doi.org/10.1007/
BF00117448
29. Lane, H.J., Blum, N., Fee, E.: Oliver Wendell Holmes (1809–1894) and Ignaz
Philipp Semmelweis (1818–1865): preventing the transmission of puerperal fever.
Am. J. Publ. Health 100(6), 1008–1009 (2010)
30. Mackie, J.L.: The Cement of the Universe: A Study of Causation. Clarendon Press
(1974). https://doi.org/10.1093/0198246420.001.0001
31. Maimari, N., Broda, K., Kakas, A., Krams, R., Russo, A.: Symbolic Representation
and Inference of Regulatory Network Structures, pp. 1–48. Wiley, Hoboken (2014).
https://doi.org/10.1002/9781119005223.ch1
32. Manhaeve, R., Dumancic, S., Kimmig, A., Demeester, T., De Raedt, L.: Deep-
problog: neural probabilistic logic programming. In: Advances in Neural Infor-
mation Processing Systems, vol. 31 (2018). https://doi.org/10.1016/j.artint.2021.
103504
33. Muggleton, S., King, R.D., Stenberg, M.J.E.: Protein secondary structure pre-
diction using logic-based machine learning. Protein Eng. Des. Sel. 5(7), 647–657
(1992). https://doi.org/10.1093/protein/5.7.647
34. Muggleton, S.: Inductive logic programming. N. Gener. Comput. 8(4), 295–318
(1991)
35. Muggleton, S.: Inverse entailment and Progol. N. Gener. Comput. 13(3), 245–286
(1995). https://doi.org/10.1007/BF03037089
36. Poole, D., Goebel, R., Aleliunas, R.: Theorist: a logical reasoning system for
defaults and diagnosis. In: Cercone, N., McCalla, G. (eds.) The Knowledge Fron-
tier: Essays in the Representation of Knowledge, pp. 331–352. Springer, New York
(1987). https://doi.org/10.1007/978-1-4612-4792-0_13
37. Popper, K.R.: The logic of scientific discovery. Hutchinson, London (1972). https://
doi.org/10.4324/9780203994627
38. Riguzzi, F.: Foundations of Probabilistic Logic Programming: Languages, Seman-
tics, Inference and Learning. CRC Press, Boca Raton (2018)
39. Riguzzi, F., Bellodi, E., Lamma, E., Zese, R., Cota, G.: Probabilistic logic pro-
gramming on the web. Software: Pract. Exp. 46(10), 1381–1396 (2016). https://
doi.org/10.1002/spe.2386
40. Robinson, J.A.: A machine-oriented logic based on the resolution principle. J. ACM
12(1), 23–41 (1965). https://doi.org/10.1145/321250.321253
41. Rohner, J.C.: Theory Toolbox 2 (2023). https://github.com/
JeanChristopheRohner/theory-toolbox-2
42. Rohner, J.C., Kjellerstrand, H.: Using logic programming for theory representation
and scientific inference. New Ideas Psychol. 61, 100838 (2021). https://doi.org/10.
1016/j.newideapsych.2020.100838
43. Rotella, F., Ferilli, S.: Probabilistic abductive logic programming using possible
worlds. In: CEUR Workshop Proceedings, vol. 1068, pp. 131–145 (2013)
44. Shadish, W.R., Cook, T.D., Campbell, D.T.: Experimental and Quasi-
Experimental Designs for Generalized Causal Inference. Houghton, Mifflin and
Company (2002)
45. Shaffner, S., Sabeti, P.: Evolutionary adaptation in the human lineage. Nat. Educ.
1(14) (2008)
Prolog for Scientific Explanation 385
46. Sozou, P.D., Lane, P.C.R., Addis, M., Gobet, F.: Computational scientific dis-
covery. In: Magnani, L., Bertolotti, T. (eds.) Springer Handbook of Model-Based
Science. SH, pp. 719–734. Springer, Cham (2017). https://doi.org/10.1007/978-3-
319-30526-4_33
47. Srinivasana, A., Muggleton, S.H., Sternberg, M.J.E., King, R.D.: Theories for
mutagenicity: a study in first-order and feature-based induction. Artif. Intell. 85(1),
277–299 (1996). https://doi.org/10.1016/0004-3702(95)00122-0
48. Valverde, R.: Tau Prolog (2022). http://tau-prolog.org/documentation
49. Wagner, G.: Web rules need two kinds of negation. In: Bry, F., Henze, N.,
Małuszyński, J. (eds.) PPSWR 2003. LNCS, vol. 2901, pp. 33–50. Springer, Hei-
delberg (2003). https://doi.org/10.1007/978-3-540-24572-8_3
50. Warren, D.S.: Introduction to prolog. In: Warren, D.S., Dahl, V., Eiter, T.,
Hermenegildo, M., Kowalski, R., Rossi, F. (eds.) Prolog - The Next 50 Years.
LNCS, vol. 13900, pp. 3–19. Springer, Cham (2023)
51. Wielemaker, J., Lager, T., Riguzzi, F.: SWISH: SWI-prolog for sharing. In: Ell-
mauthaler, S., Schulz, C. (eds.) Proceedings of the International Workshop on
User-Oriented Logic Programming (IULP 2015), pp. 99–113 (2015)
52. Wielemaker, J., Schrijvers, T., Triska, M., Lager, T.: SWI-prolog. The-
ory Pract. Logic Program. 12(1–2), 67–96 (2012). https://doi.org/10.1017/
S1471068411000494
53. Woodward, J., Ross, L.: Scientific explanation. In: Zalta, E. (ed.) The Stanford
Encyclopedia of Philosophy. Metaphysics Research Lab, Stanford University (2021)
54. Zupan, B., et al.: Discovery of genetic networks through abduction and qualita-
tive simulation. In: Džeroski, S., Todorovski, L. (eds.) Computational Discovery of
Scientific Knowledge. LNCS (LNAI), vol. 4660, pp. 228–247. Springer, Heidelberg
(2007). https://doi.org/10.1007/978-3-540-73920-3_11
Machines as Thought Partners: Reflections
on 50 Years of Prolog
1 Introduction
In his 1972 Turing Award Lecture, Edsger Dijkstra notes that LISP “has assisted
a number of our most gifted fellow humans in thinking previously impossible
thoughts.” Curiously, it was during that same year that Prolog was developed.
We do not know if it was felt at that time just how important the discovery of
the Prolog language was, but it is not surprising that the name of the language,
an acronym for “Programming in Logic”, is a homophone for prologue. Robert
Kowalski’s and Alain Colmerauer’s language was an introduction to a new way
of thinking about programming, one which in some ways is alluded to by an old
joke at the language’s expense:
Prolog is what you get when you create a language and system that has
the intelligence of a six-year-old - it simply says “no” to everything.
The joke hints at just how revolutionary the language was. For the first
time, we now had a language that rather than having a programmer answer
the question of “how”, we had one that enabled us to answer the question of
“what”. In other words, the language freed us from thinking about and describing
the mechanics of an algorithm, and allowed us to focus on describing the goal,
or specification that the algorithm was intended to meet. So, if we come back
c The Author(s), under exclusive license to Springer Nature Switzerland AG 2023
D. S. Warren et al. (Eds.): Prolog: The Next 50 Years, LNAI 13900, pp. 386–392, 2023.
https://doi.org/10.1007/978-3-031-35254-6_31
Machines as Thought Partners: Reflections on 50 Years of Prolog 387
1
https://ec.ai.
2
https://www.ibm.com/watson.
388 G. Gelfond et al.
3
https://www.billboard.com/pro/super-bowl-halftime-show-covid-safety-
coronavirus/.
4
https://ec.ai/case-travel.
5
https://ec.ai/cogent-features.
Machines as Thought Partners: Reflections on 50 Years of Prolog 389
The important aspect of the program in Listing 1.1 is that none of the state-
ments describe an algorithm for computing a potential solution. Rather, they
encode the specification itself. It’s worth reflecting and appreciating the power of
such a syntactically simple and elegant language. Compare for example this pro-
gram, against the equivalent programs written in an imperative language using
Google’s OR-Tools [8]. The difference is stark, and it raises an important ques-
tion: “Why has the logic programming approach not gained in momentum since
its discovery?”
There are many potential answers to this question. One possibility is that
in addition to the cognitive load incurred by switching from an imperative to a
declarative mindset, there is an additional cognitive load incurred by the close
relationship between logic programming languages and the notations of formal
logic. This dramatically increases the distance a potential user has to mentally
travel in order to get to the current state of the art. Another way to view this, is
that logic programming languages on some level, are still at the level of assembly
language. The declarative paradigm is a higher level paradigm than imperative
programming, but declarative languages by and large are still on too low a level
to be readily adopted. If this is true, then a natural question to ask is: “What
could a high-level, structured, declarative programming language look like?”
At EC, we believe that one potential answer to this question is structured
natural language, in particular our own version of this known as Cogent. Similar
work in this area exists, namely Kowalski’s own work on logical English [5,6],
but with Cogent we are able to leverage our expertise in both natural language
understanding and knowledge representation to build a more flexible, and user
friendly representation language. In particular, let’s revisit the program from
Listing 1.1, only this time in Cogent instead of ASP:
The reader will notice that with the exception of lines 7, 9, and 11, the text
of the program is the same as comments from the ASP encoding in Listing 1.1.
Given this program, our reasoning engine is capable of finding solutions just as
efficiently as the ASP encoding, yet the Cogent program is more accessible to
a reader. Not only that, but the fact that the language is a structured form of
natural language helps bridge the gap in terms of familiarity to aspiring users.
The notion of accessibility to a reader, however is of special importance, since at
EC, one of our motivating goals is to help develop explainable AI. One important
aspect of this is to render the axioms of a domain that an AI system represents
both inspectable and clear to as many users as possible. This kind of transparency
enables deeper human and AI partnerships which furthers our vision of artificial
agents as “thought partners” capable of collaborating with humans.
Cogent has features that overlap with those found in contemporary logic
programming languages, such as non-deterministic choice, aggregates, recursive
definitions, costs, preferences, a declarative semantics for negation, and contradic-
tion diagnosis. In addition however, it features numerous advanced term building
features that facilitate the construction of clear, concise natural language expres-
sions. Consider the solution to the N-Queens problem given in Listing 1.3
3 Conclusion
In 1972, Kowalski and Colmerauer started a revolution with the advent of the Pro-
log programming language. The ability to think “previously impossible thoughts”,
led the community to ask previously unthinkable question, sparking revolutions
in natural language understanding, knowledge representation, commonsense rea-
soning, and other diverse areas. For a time, these fields grew in isolation from
each other, and now are coming together rapidly and in profound ways. With
the development of Cogent, an ultimate grandchild of Prolog in some sense, we
at Elemental Cognition hope to carry forward the tradition and enable a new
392 G. Gelfond et al.
References
1. Gelfond, M., Lifschitz, V.: The stable model semantics for logic programming. In:
Kowalski, R., Bowen, Kenneth (eds.) Proceedings of International Logic Program-
ming Conference and Symposium, pp. 1070–1080. MIT Press (1988). http://www.
cs.utexas.edu/users/ai-lab?gel88
2. Gelfond, M., Lifschitz, V.: Classical negation in logic programs and disjunctive
databases. N. Gener. Comput. 9, 365–385 (1991)
3. Kalyanpur, A., Breloff, T., Ferrucci, D.A.: Braid: weaving symbolic and neural
knowledge into coherent logical explanations. Proceed. AAAI Conf. Artif. Intelli.
36(10), 10867–10874 (2022). https://doi.org/10.1609/aaai.v36i10.21333. https://
ojs.aaai.org/index.php/AAAI/article/view/21333
4. Kaufman, G.: How the NFL Pulled Off a Safe Super Bowl LV Halftime Show in
the Middle of a Pandemic (2 2021). https://www.billboard.com/pro/super-bowl-
halftime-show-covid-safety-coronavirus/, non paywalled version. https://www.
bioreference.com/how-the-nfl-pulled-off-a-safe-super-bowl-lv-halftime-show-in-
the-middle-of-a-pandemic/
5. Kowalski, R., Dávila Quintero, J., Calejo, M.: Logical English for legal applications
(11 2021)
6. Kowalski, R., Dávila Quintero, J., Sartor Galileo Calejo, M.: Logical English for law
and education. In: Warren, D.S., Dahl, V., Eiter, T., Hermenegildo, M., Kowalski,
R., Rossi, F. (eds.) Prolog - The Next 50 Years. No. 13900 in LNCS, Springer (2023)
7. Marriott, K., Stuckey, P.J., Wallace, M.: Handbook of constraint programming, chap.
12. Constraint Logic Programming, pp. 409–452. Foundations of Artificial Intelli-
gence, Elsevier (2006)
8. Perron, L., Furnon, V.: OR-Tools. https://developers.google.com/optimization/
9. Schlipf, J.S., Ross, K.A., Van Gelder, A.: The well-founded semantics for general
logic programs. J. Assoc. Comput. Mach. 38(3), 620–650 (1991)
Author Index
F M
Ferrando, Angelo 300 Marinković, Vesna 334
Ferreiro, Daniela 171 Mascardi, Viviana 300
Ferrucci, David 386 Mohapatra, Abhijeet 271
Flach, Peter 184 Morales, Jose F. 23, 107, 171
© The Editor(s) (if applicable) and The Author(s), under exclusive license
to Springer Nature Switzerland AG 2023
D. S. Warren et al. (Eds.): Prolog: The Next 50 Years, LNAI 13900, pp. 393–394, 2023.
https://doi.org/10.1007/978-3-031-35254-6
394 Author Index
R T
Rajasekharan, Abhiramon 48 Tabakova-Komsalova, Veneta 144
Rodríguez, Jorge P. 131 Tarau, Paul 359
Rohmer, Jean 124
Rohner, Jean-Christophe 372 V
Varanasi, Sarat Chandra 48, 346
S
Sadri, Fariba 210 W
Sakellariou, Ilias 71 Wang, Huaduo 48, 346
Salazar, Elmer 48, 346 Warren, David S. 3, 62, 82
Sartor, Galileo 287 Wielemaker, Jan 184
Satoh, Ken 277
Shakerin, Farhad 48, 346 Z
Sokol, Kacper 184 Zeng, Yankai 48