1 Brief overview
is a rule-based system that supports programming with individual, sequence, function and context variables. It extends Prolog with rule-based programming capabilities to manipulate sequences of terms. The four kinds of variables help to traverse tree forms of expressions both in horizontal and vertical directions, in one or more steps. It facilitates to have expressive pattern matching that often helps to write short and intuitive code.
Another important feature of PLog is the use of strategies. They provide a mechanism to control complex rule-based computations in a highly declarative way. With the help of strategies, the user can combine simpler transformation rules into more complex ones. In this way, PLog conveniently combines the whole Prolog power with rule-based strategic programming features.
PLog is based on Log calculus 
, where the inference system basically is the SLDNF-resolution with normal logic program semantics. It has been successfully used in the extraction of frequent patterns from data mining workflows , XML transformation and web reasoning , modeling of rewriting strategies  and access control policies , etc.
The Log calculus has been influenced by the -calculus [5, 6], which, in itself, is a foundation for the rule-based programming system ELAN . There are some other languages for programming by rules, such as, e.g., ASF-SDF , CHR , Maude , Stratego , Tom . The Log calculus and, consequently, PLog differs from them, first of all, by its pattern matching capabilities. Besides, it adopts logic programming semantics (clauses are first class concepts, rules/strategies are expressed as clauses) and makes a heavy use of strategies to control transformations. Earlier works about Log and its implementation in Mathematica include [17, 20, 16].
PLog is available at https://www.risc.jku.at/people/tkutsia/software/prholog.
2 The PLog language
We write PLog constructs in typewriter font. They are terms (including a special kind of terms, called contexts) and sequences. These objects are constructed from function symbols without a fixed arity (so called unranked or variadic function symbols), a special constant (called the hole), and individual, functional, context and sequence variables. These variables are denoted by the identifiers whose names start respectively with , and (e.g., ). Terms t and sequences s are constructed in a standard way:
where is a function symbol and stands for the empty sequence and is omitted whenever it appears as a subsequence of another sequence. A context is a term with a single occurrence of the constant. Application of a context to a term is a term derived by replacing the hole in with . For instance, applying to gives another context , while applying it to gives a non-context term .
A substitution is a mapping from individual variables to hole-free terms, from sequence variables to hole-free sequences, from function variables to function variables and symbols, and from context variables to contexts, such that all but finitely many individual, sequence, and function variables are mapped to themselves, and all but finitely many context variables are mapped to themselves applied to the
hole. This mapping can be extended to terms and sequences in the standard way. For instance, for a substitution
(b,c)} and a sequence
(c_Ctx(i_Term),f_Funct(s_Seq1,a,s_Seq2)), by applying to we get the sequence
Matching problems are pairs of sequences, one of which is ground (i.e., does not contain variables). Such matching problems may have zero, one, or more (finitely many) solutions, called matching substitutions or matchers. For instance, the sequence
(f(a),f(b),c) in two different ways: one by the matcher
(f(b),c)} and other one by the matcher
c}. Similarly, the term
c_X(f_Y(a)) matches the term
f(a,g(a)) with the matchers
g}. Matching is the main computational mechanism in PLog.
Instantiations of sequence and context variables can be restricted by regular sequence and regular context languages, respectively. We do not go into the details of this feature of PLog matching here.
A Log atom (-atom) is a quadruple consisting of a hole-free term (a strategy), two hole-free sequence and , and a set of regular constraints R where each variable is constrained only once, written as
st :: s1 ==> s2 where R. Intuitively, it means that the strategy transforms to when the variables satisfy the constraint R. We call the left hand side and the right hand side of this atom. When R is empty, we omit it and write
st :: s1 ==> s2. The negated atom is written as
st :: s1 =\=> s2 where R. A Log literal (-literal) is a -atom or its negation. A PLog clause is either a Prolog clause, or a clause of the form
st :: s1 ==> s2 where R :- body (in the sequel called a -clause) where body is a (possibly empty) conjunction of - and Prolog literals.
A PLog program is a sequence of PLog clauses and a query is a conjunction of - and Prolog literals. There is a restriction on variable occurrence imposed on clauses: -clauses and queries can contain only Log variables, and Prolog clauses and queries can contain only Prolog variables. If a Prolog literal occurs in a -clause or query, it may contain only Log individual variables that internally get translated into Prolog variables.
3 Inference and strategies
PLog execution principle is based on depth-first inference with leftmost literal selection in the goal. If the selected literal is a Prolog literal, then it is evaluated in the standard way. If it is a PLog atom of the form , due to the syntactic restriction called well-modedness (formally defined in ), and do not contain variables. Then a (renamed copy of a) program clause is selected, such that the strategy matches and the sequence
s1’ matches with a substitution . Next, the selected literal in the query is replaced with the conjunction , where is the built-in strategy for identity: it succeeds iff its right-hand side matches the left-hand side. Evaluation continues further with this new query. Success and failure are defined in the standard way. Backtracking explores other alternatives that may come from matching the selected query literal to the head of the same program clause in a different way (since context/sequence matching is finitary, see, e.g., [9, 14]), or to the head of another program clause. Negative literals are processed by negation-as-failure.
When instead of the exact equality one uses proximity as in , then in place of , PLog introduces another built-in strategy in the new query: , which succeeds if its right-hand side matches the left-hand side approximately, at least with the degree .111This is an experimental feature, not yet included in the official distribution. Proximity relations indicate by which degree two expressions are close to each other, where the degree is a real number in . Proximity with degree 1 means that the terms are equal, while degree 0 means that they are distinct. Hence, can be seen as an abbreviation of . Proximity is a fuzzy reflexive, symmetric, non-transitive relation, suitable for modeling imprecise, incomplete information.
Some of the other predefined strategies of PLog and their intuitive meanings are the following:
, , first transforms the input sequence by and then transforms the result by (or by , if ). Via backtracking, all possible results can be obtained. The strategy fails if either or fails.
, , returns a result of a successful application of some strategy to the input sequence. It fails if all ’s fail. By backtracking it can return all outputs of the applications of each of the strategies .
first_one, , selects the first that does not fail on the input sequence and returns only one result of its application.
first_onefails if all ’s fail. Its variation,
first_all, returns via backtracking all the results of the application to the input sequence of the first strategy that does not fail.
maps the strategy st to each term in the input sequence and returns the result sequence. Backtracking generates all possible output sequences. st should operate on a single term and not on an arbitrary sequence. fails if st fails for at least one term from the input sequence.
In this section we bring some examples to illustrate features and the expressive power of PLog.
Example 1 (Sorting).
The following program illustrates how bubble sort can be implemented in PLog.
swap(f_Ordering) :: (s_X, i_I, i_J, s_Y) ==> (s_X, i_J, i_I, s_Y) :- not(f_Ordering(i_I, i_J)). bubble_sort(f_Ordering) := first_one(nf(swap(f_Ordering))).
In the first clause, the user-defined strategy swaps two neighboring elements in the given sequence if they violate the given ordering . The use of sequence variables helps to identify the violating place in the given sequence by pattern matching, without the need to explicitly define the corresponding recursive procedure. The sorting strategy is then defined as an exhaustive application of (via the built-in strategy ), which will lead to a sorted sequence. The strategy guarantees that only the first answer computed by is returned: it does not make sense to sort a sequence in different ways to get the same answer over and over again via backtracking.
The way how the strategy is defined above is just an abbreviation of the clause
bubble_sort(f_Ordering) :: s_X ==> s_Y :- first_one(nf(swap(f_Ordering))) :: s_X ==> s_Y.
PLog allows the user to write such abbreviations. To use this strategy for sorting a sequence, we could call, e.g.,
?(bubble_sort(=<) :: (1,3,4,3,2) ==> s_X, Result).
and PLog would return a single result in the form of a substitution:
Result = [s_X ---> (1,2,3,3,4)].
Example 2 (Rewriting).
One step of rewriting a term by some rule/strategy can be straightforwardly defined in PLog:
rewrite_step(i_Str) :: c_Ctx(i_X) ==> c_Ctx(i_Y) :- i_Str :: i_X ==> i_Y.
It finds a subterm in to which the strategy applies and rewrites it. Due to the built-in matching algorithm that finds the relevant instantiation of the context variable , this step corresponds to a leftmost-outermost rewriting step. In  we have illustrated how easily other rewriting strategies can be modeled in PLog.
Example 3 (Using proximity).
We assume that a proximity relation between function symbols is given. (Based on it, we can compute proximity between terms as well.) The task is to remove from a given sequence approximate duplicates, i.e., if the sequence contains two elements that are proximal to each other (by a predefined degree), we should get a sequence where only one of the proximal elements is retained. The strategy below does it. It checks whether the sequence contains somewhere two elements and that are close to each other at least (by the given degree) and removes . removes all such approximate duplicates and returns one answer:
merge_proximals :: (s_X, i_X, s_Y, i_Y, s_Z) ==> (s_X, s_Y, i_Y, s_Z) :- prox :: i_X ==> i_Y. merge_all_proximals := first_one(nf(merge_proximals)).
Assume our proximity relation is such that and are proximal with degree 0.6 and is close to with degree 0.8. (Every symbol is proximal to itself with degree 1.) Then we can ask to merge all proximals in the sequence
(a,b,d,b,c) first by degree 0.5 and then by degree 0.7. The resulted answers show the result sequence and the actual approximation degree (which is the given degree):
?(merge_all_proximals :: (a,b,d,b,c) ==> s_Ans, 0.5, Degree, Result). Degree = 0.6, Result = [s_Ans --> (d,c)] ; false. ?(merge_all_proximals :: (a,b,d,b,c) ==> s_Ans, 0.7, Degree, Result). Degree = 0.8, Result = [s_Ans --> (a,d,c)] ; false.
Due to nontransitivity of proximity relations and the fact that matching finds the first proximal pair (from the left), the order of sequence elements affects the answer. For instance, if we put
a at the end, we get
?(merge_all_proximals :: (b,d,b,c,a) ==> s_Ans, 0.5, Degree, Result). Degree = 0.6, Result = [s_Ans --> (d,c,a)] ; false. ?(merge_all_proximals :: (b,d,b,c,a) ==> s_Ans, 0.7, Degree, Result). Degree = 0.8, Result = [s_Ans --> (d,c,a)] ; false.
It happens because and are not close to each (although and as well as and are).
The main advantages of PLog are: compact and declarative code; capabilities of expression traversal without explicitly programming it; the ability to use clauses in a flexible order with the help of strategies. Besides, PLog has access to the whole infrastructure of its underline Prolog system. These features make PLog suitable for nondeterministic computations, manipulating XML documents, implementing rule-based algorithms and their control, etc.
This work has been supported by the Shota Rustaveli National Science Foundation of Georgia under the grant YS-18-1480.
-  Emilie Balland, Paul Brauner, Radu Kopetz, Pierre-Etienne Moreau & Antoine Reilles (2007): Tom: Piggybacking Rewriting on Java. In Franz Baader, editor: Term Rewriting and Applications, 18th International Conference, RTA 2007, Paris, France, June 26-28, 2007, Proceedings, Lecture Notes in Computer Science 4533, Springer, pp. 36–47, doi:http://dx.doi.org/10.1007/978-3-540-73449-9_5.
-  Peter Borovanský, Claude Kirchner, Hélène Kirchner, Pierre-Etienne Moreau & Marian Vittek (1996): ELAN: A logical framework based on computational systems. In José Meseguer, editor: First International Workshop on Rewriting Logic and its Applications, RWLW 1996, Asilomar Conference Center, Pacific Grove, CA, USA, September 3-6, 1996, Electronic Notes in Theoretical Computer Science 4, Elsevier, pp. 35–50, doi:http://dx.doi.org/10.1016/S1571-0661(04)00032-5.
-  Mark van den Brand, Arie van Deursen, Jan Heering, Hayco de Jong, Merijn de Jonge, Tobias Kuipers, Paul Klint, Leon Moonen, Pieter A. Olivier, Jeroen Scheerder, Jurgen J. Vinju, Eelco Visser & Joost Visser (2001): The Asf+Sdf Meta-Environment: a Component-Based Language Development Environment. Electron. Notes Theor. Comput. Sci. 44(2), pp. 3–8, doi:http://dx.doi.org/10.1016/S1571-0661(04)80917-4.
-  Horatiu Cirstea & Claude Kirchner (2001): The rewriting calculus - Part I. Log. J. IGPL 9(3), pp. 339–375, doi:http://dx.doi.org/10.1093/jigpal/9.3.339.
-  Horatiu Cirstea & Claude Kirchner (2001): The rewriting calculus - Part II. Log. J. IGPL 9(3), pp. 377–410, doi:http://dx.doi.org/10.1093/jigpal/9.3.377.
-  Manuel Clavel, Francisco Durán, Steven Eker, Patrick Lincoln, Narciso Martí-Oliet, José Meseguer & Jose F. Quesada (2002): Maude: specification and programming in rewriting logic. Theor. Comput. Sci. 285(2), pp. 187–243, doi:http://dx.doi.org/10.1016/S0304-3975(01)00359-0.
-  Jorge Coelho, Besik Dundua, Mário Florido & Temur Kutsia (2010): A Rule-Based Approach to XML Processing and Web Reasoning. In Pascal Hitzler & Thomas Lukasiewicz, editors: Web Reasoning and Rule Systems - Fourth International Conference, RR 2010, Bressanone/Brixen, Italy, September 22-24, 2010. Proceedings, Lecture Notes in Computer Science 6333, Springer, pp. 164–172, doi:http://dx.doi.org/10.1007/978-3-642-15918-3_13.
-  Hubert Comon (1998): Completion of Rewrite Systems with Membership Constraints. Part II: Constraint Solving. J. Symb. Comput. 25(4), pp. 421–453, doi:http://dx.doi.org/10.1006/jsco.1997.0186.
-  Besik Dundua, Temur Kutsia & Mircea Marin (2009): Strategies in PRholog. In Maribel Fernández, editor: Proceedings Ninth International Workshop on Reduction Strategies in Rewriting and Programming, WRS 2009, Brasilia, Brazil, 28th June 2009, EPTCS 15, pp. 32–43, doi:http://dx.doi.org/10.4204/EPTCS.15.3.
-  Besik Dundua, Temur Kutsia, Mircea Marin & Cleo Pau (2019): Extending the Log Calculus with Proximity Relations. In George Jaiani & David Natroshvili, editors: Applications of Mathematics and Informatics in Natural Sciences and Engineering, Springer, Cham, pp. 83–100, doi:http://dx.doi.org/10.1007/978-3-030-56356-1_6.
-  Besik Dundua, Temur Kutsia & Klaus Reisenberger-Hagmayer (2017): An Overview of PLog. In Yuliya Lierler & Walid Taha, editors: Practical Aspects of Declarative Languages - 19th International Symposium, PADL 2017, Paris, France, January 16-17, 2017, Proceedings, Lecture Notes in Computer Science 10137, Springer, pp. 34–49, doi:http://dx.doi.org/10.1007/978-3-319-51676-9_3.
-  Thom W. Frühwirth (1998): Theory and Practice of Constraint Handling Rules. J. Log. Program. 37(1-3), pp. 95–138, doi:http://dx.doi.org/10.1016/S0743-1066(98)10005-5.
Temur Kutsia &
(2005): Matching with Regular
In Geoff Sutcliffe &
Andrei Voronkov, editors:
Logic for Programming, Artificial Intelligence, and Reasoning, 12th International Conference, LPAR 2005, Montego Bay, Jamaica, December 2-6, 2005, Proceedings, Lecture Notes in Computer Science 3835, Springer, pp. 215–229, doi:http://dx.doi.org/10.1007/11591191_16.
-  John W. Lloyd (1987): Foundations of Logic Programming, 2nd Edition. Springer, doi:http://dx.doi.org/10.1007/978-3-642-83189-8.
-  Mircea Marin & Tetsuo Ida (2005): Rule-Based Programming with Log. In Daniela Zaharie, Dana Petcu, Viorel Negru, Tudor Jebelean, Gabriel Ciobanu, Alexandru Cicortas, Ajith Abraham & Marcin Paprzycki, editors: Seventh International Symposium on Symbolic and Numeric Algorithms for Scientific Computing (SYNASC 2005), 25-29 September 2005, Timisoara, Romania, IEEE Computer Society, pp. 31–38, doi:http://dx.doi.org/10.1109/SYNASC.2005.61.
-  Mircea Marin & Temur Kutsia (2003): On the Implementation of a Rule-Based Programming System and some of its Applications. In Boris Konev & Renate Schmidt, editors: Proc. 4th International Workshop on the Implementation of Logics, WIL’04, pp. 55–69.
-  Mircea Marin & Temur Kutsia (2006): Foundations of the rule-based system Log. J. Appl. Non Class. Logics 16(1-2), pp. 151–168, doi:http://dx.doi.org/10.3166/jancl.16.151-168.
-  Mircea Marin, Temur Kutsia & Besik Dundua (2019): A Rule-based Approach to the Decidability of Safety of ABAC. In Florian Kerschbaum, Atefeh Mashatan, Jianwei Niu & Adam J. Lee, editors: Proceedings of the 24th ACM Symposium on Access Control Models and Technologies, SACMAT 2019, Toronto, ON, Canada, June 03-06, 2019, ACM, pp. 173–178, doi:http://dx.doi.org/10.1145/3322431.3325416.
-  Mircea Marin & Florina Piroi (2004): Deduction and Presentation in Log. Electron. Notes Theor. Comput. Sci. 93, pp. 161–182, doi:http://dx.doi.org/10.1016/j.entcs.2003.12.033.
-  Phong Nguyen (2015): Meta-mining: a meta-learning framework to support the recommendation, planning and optimization of data mining workflows. Ph.D. thesis, University of Geneva, doi:http://dx.doi.org/10.13097/archive-ouverte/unige:86131.
-  Eelco Visser (2001): Stratego: A Language for Program Transformation Based on Rewriting Strategies. In Aart Middeldorp, editor: Rewriting Techniques and Applications, 12th International Conference, RTA 2001, Utrecht, The Netherlands, May 22-24, 2001, Proceedings, Lecture Notes in Computer Science 2051, Springer, pp. 357–362, doi:http://dx.doi.org/10.1007/3-540-45127-7_27.