11email: mircea.marin@e-uvt.ro
LogΒ and RhoLog
LogΒ is a calculus designed by us for rule-based programming. RhoLog is a Mathematica package for rule-based programming, based on the LogΒ calculus. The current version of RhoLog is implemented in Mathematica 14 and can be downloaded freely from
Log
was designed in 2004 by some members of the Theorema group working on the implementation of an assistant system for doing mathematics by theorem proving and theory exploration, as envisioned by Bruno Buchberger. Its initial purpose was to assist mathematicians to explore theories and search for proofs by letting them specify and execute strategies that combine the capabilities of domain-specific provers, solvers, and computing methods. Our first publications about LogΒ [MP04a, MP04b] refer to RhoLog, a Mathematica package based on Log, which can be used for computation by deduction, and generation and presentation of proofs in human-readable format. Afterwards, we continued to develop LogΒ and the RhoLog package as a standalone systemΒ [KM05, MK06, MKD19, MDK20]. A Prolog implementation of LogΒ is also availableΒ [DKM09].
1 The LogΒ calculus
LogΒ is a calculus designed for reasoning about the reducibility of terms of the form , which express the application of strategy to . We are interested in the reducibility relation which is induced by a set of conditional reduction rules of the form
which we abbreviate by writing . The informal reading of such a rule is βThe applicative term can be reduced to if conditions and β¦Β and hold.β For such a set we want to find answers to queries
abbreviated , which express a conjunction of logical formulas of four kinds:
-
(1)
a constraint over a known constraint domain,
-
(2)
a reducibility formula ,
-
(3)
The logical negation of , which we denote by ,
-
(4)
A matching formula .
An answer to such a query is a ground substitution for the variables in the query such that every formula holds in the reduction theory induced by . For our kinds of constraints, this means that: (1)Β if is a constraint over a known constraint domain then is valid in it; (2)Β if is then holds; (3) if is then is unsatisfiable, that is, none of the ground instances of holds. By imposing some natural restrictions on the syntactic structure of rules and queries (Section 1.1.3), we can guarantee that LogΒ is sound (every computed substitution is an answer to the query) and complete (all awnswers are eventually computed).
LogΒ has some outstanding features:
- Expressivity:
-
Itβs specification language is in an algebra of terms with four kinds of variables: for terms, for functional symbols, for contexts, and for sequences of terms. As a result, a wide range of applications have natural and concise rule-based specifications.
- Performance:
-
LogΒ was designed to be sound and complete in any term algebra where the matching problem is finitary. The term language of LogΒ is very well suited from this point of view because:
- 1.
-
2.
MathematicaΒ [WOL99] is a language with efficient built-in support for rewriting based on matching with variables for terms, function symbols, and sequences of terms. It can be extended easily to perform finitary matching with context variables too. For this reason, we used Mathematica to implement Log.
1.1 Syntax
The rule-based programming style proposed by us is concerned with the outcome of applying strategies on objects of interest. The strategies and objects of interest are expressed as terms over a signature of variadic111A variadic function symbol can take any number of arguments. function symbols, and a set of variables of four kinds. The sets and are countably infinite and mutually disjoint.
The elements of , called individual term variables, have names with superscript , e.g., ; those of , called function variables, have names with superscript , e.g., ; those of , called context variables, have names with superscript , e.g., ;
and those of , called sequence variables, have names with superscript , e.g., . We use and to build three kinds of expressions: terms , term sequences , and contexts . They are defined by the grammar
subject to the following restriction: is a special symbol, called hole, which does not occur in terms or term sequences. The sequence constructor β,β is assumed to be associative and with identity element none. Terms and contexts are represented in a simplified form, by removing the occurrences of none and writing instead of whenever is a function symbol.
Thus, the basic expressions of our language are elements of the syntactic domain
Notice that a context is like a term, but with exactly one occurrence of the hole. Contexts can be applied to terms or other contexts. If is a context, and is a term or context, then the operation of applying to is defined as follows:
Strategies
are represented by terms where is the identifier of a strategy. The distinguished function symbol @ is used to construct terms to indicate the application of strategy to . We call such terms applicative terms and allow the function symbol @ to occur only at the outermost position of an applicative term. We write for the set of applicative terms, for the set of other terms without occurrences of @, and for the set of variables in a syntactic construct . Also, we use as an alternative notation for the applicative term .
1.1.1 Substitutions.
A substitution is a mapping
such that for all , for all , for all , and for all .
Every substitution can be extended homomorphically to a mapping
An expression is called an instance of the expression .
1.1.2 Reducibility and matching formulas.
A reducibility formula is an expression with and . The informal reading of this formula is βthe applicative term is reducible to β. An irreducibility formula is the logical negation of a reducibility formula , and is denoted by . A matching formula is an expression with . The informal reading of is β and are identicalβ.
1.1.3 Rules and queries.
Strategies are represented by terms where is the identifier of a strategy. They are defined by rules of the form
(1) |
abbreviated , where every condition is either
-
1.
a term from that expresses a constraint over a known constraint domain222The constraints of this kind are called atomic constraints., or
-
2.
a reducibility formula , or
-
3.
an irreducibility formula , or
-
4.
a matching formula .
The informal reading of (2) is βThe reducibility formula holds if the conditions , β¦, hold.β. Rules can be unconditional, that is, of the form . We write them in the simplified form . Occasionally, we write T for the conjunction of zero conditions. We view (2) as a partial definition for the strategy identified by .
We also consider terms for atomic formulas and strategies to prove their truth by reducing them to a constant true. For such terms we write instead of , and may have rules of the form
(2) |
A query is of the form
abbreviated , where every can be of the same form as a rule condition. Given a set of variables, we say that the query is -admissible if we can define the sets of variables while observing the following restrictions:
-
β’
If is an atomic constraint then and .
-
β’
If is then , and .
-
β’
If is then and .
-
β’
If is then and .
Notice that . We denote by the set of variables of a -admissible query of the form , and say that:
-
β’
is admissible if is -admissible.
In particular, the empty query is admissible. We denote it by .
-
β’
A rule is admissible if the query is -admissible.
1.2 Semantics
A set of admissible rules induces a reduction relation
together with a set of answer substitutions
for every and , such that holds whenever . These notions are defined simultaneously as follows:
-
β’
holds if there exists a rule
and ground substitutions such that
-
1.
For all , if is the substitution then
-
(a)
If is an atomic constraint then and is valid in its corresponding constraint domain.
-
(b)
If is then .
-
(c)
If is then and .
-
(d)
If is then .
-
(a)
-
2.
.
-
1.
-
β’
is a subset of .
LogΒ has two distinguished strategy identifiers: FstΒ and Cut:
-
β’
is either
-
β
if for all , or
-
β
if and for all .
-
β
-
β’
is either if , or a singleton subset of if . The choice of the singleton subset of is donβt care, implementation-dependent.
For any strategy with identifier different from FstΒ and Cut, we have
-
β’
We say that is -reducible if for some term . Otherwise, we say that is 3 -redex.
Let be an admissible query of the form . An answer substitution of in the reduction theory induced by is a composition of ground substitutions , β¦, such that, for all , if and is the composition of substitutions then
-
1.
If is an atomic constraint then and is valid in its corresponding constraint domain.
-
2.
If is then .
-
3.
If is then and .
-
4.
If is then .
We write for the set of answer substitutions of an admissible query .
1.3 Inference rules
The admissibility condition on rules and queries allows us to compute the answers of admissible queries by matching only. The calculus of LogΒ makes use of the following rules of inference:
-
1.
if is a valid atomic constraint in its corresponding constraint domain. -
2.
if . -
3.
if and for all . -
4.
if -
5.
if the identifier of is neither FstΒ nor Cut,
is -renamed apart, and . -
5β.
if the identifier of is neither FstΒ nor Cut,
is -renamed apart, and . -
6.
if the atempt to prove the satisfiability of the query fails finitely.
Every rule of inference is of the form .
-
β’
Rules 2, 4, 5 and 5β have as side effect the computation of a substitution , and we depict this fact by writing .
-
β’
Rules 1, 3 and 6 check if something happens without producing a substitution as side effect. We depict them by writing where denotes for the identity substitution.
A Log-derivation of a query is a sequence of reduction steps
and a Log-refutation is a Log-derivation which ends with the query . We abbreviate a Log-refutation by writing where is the restriction of the substitution to .
As usual for refutation-based calculi, the set of answers computed by LogΒ is
It can be shown that . This means that our calculus is sound and complete.
1.4 Some useful strategies
Like in Prolog, we make use of the anonymous variables , , and when we donβt care about their corresponding values.
We let the reader to figure out the meanings of the strategies induced by these reduction rules.
2 The RhoLog package
RhoLog is a Mathematica package for rule-based programming, based on the LogΒ calculus. The current implementation is for Mathematica 14.
The package is freely available for download from
2.1 Installation instructions
-
1.
Download the package RhoLog.wl from
https://staff.fmi.uvt.ro/~mircea.marin/rholog/RhoLog.wl
and copy it in the directory
$UserBaseDirectory/Applications
-
2.
Open a Mathematica notebook and load RhoLog with the command
<<RhoLogβ
Check if the RhoLog package is accessible, by evaluating
?Request
If everything worked smoothly, Mathematica will display information about the command Request of RhoLog.
2.2 Specification language
Since RhoLog is a Mathematica package, we use the syntax of Mathematica to write terms. Also, we write
-
β’
instead of , and instead of
-
β’
instead of .
For strategy identifiers, we use strings delimited by double quotes.
2.3 RhoLog commands
2.3.1 Commands for rule declarations
DeclareRule[]
declares a single rule , and
DeclareRules[]
declares rules .
For example, the strategies with identifiers
,
, , , ,
,
,
,
,
,
,
, ,
, and
are predefined in RhoLog:
Notice that
-
β’
holds iff .
-
β’
folds iff is a list and is an element of .
-
β’
"Comp" is for sequential composition of reductions:
"Comp" holds iff there exist ground terms such that , , β¦, hold and .
-
β’
"Or" is for parallel reduction:
"Or" holds iff holds for some .
-
β’
holds if there exist terms in such that the relations , , β¦, hold, and . holds if holds for an integer .
-
β’
"Rw" is for rewriting: holds if (1) has a subterm such that , and (2) is obtained from by replacing with .
-
β’
"NF" is for normalizing reduction:
"NF" holds if "Repeat" holds and is irreducible, that is, there is no such that .
-
β’
"Rw1"[] is for reducing a -redex immediately below the root of a term: holds if is of the form , is of the form , and holds.
-
β’
"Fmap" holds iff is a term of the form , is of the form , and hold for all .
-
β’
"Outer" is for reducing an outermost -redex, and "Inner" is for reducing an innermost redex.
-
β’
"LazyEval" is for lazy evaluation: it normalizes a term by repeated reductions of outermost -redexes.
-
β’
"StrictEval" is for strict evaluation: it normalizes a term by repeated reductions of inneermost -redexes.
-
β’
"Perm" holds iff is a list and is a permutation of .
-
β’
"Subset" holds iff is a set represented by a list and is a subset of .
-
β’
The defining rules of "Perm" and "Subset" rely on the built-in capabilities of Mathematica for computation and pattern matching:
-
β
Permutations and Subsets are built-in functions:
Permutations returns the list of all distinct permutations of list ;
Subsets returns the list of all subsets of .
-
β
in Mathematica is a conditional pattern, in the following sense: iff and is true.
For example, matches a ground term with matcher if t is a list. Notice the following implementation detail of RhoLog: when used in pattern conditions, the kind superscripts must be removed from the names of variables.
-
β
2.3.2 The #1 placeholder.
RhoLog has a special capability: we can use #1
in rules, such that, when we apply the inference rule
with the renamed-apart rule and matcher
then #1
is replaced with in This capability allows us to specify the behavior of "Fst" as follows:
Although the strategies with identifiers "Comp", "NF", "Or", and "Fst" can be defined correctly with the rules mentioned here, this approach would be inefficient. In RhoLog, their implementations are hard-coded.
Rules[ruleId]returns the list of defining rules for the rule identifier ruleId. For example:
ClearRules[]
clears the defining rules for the rule identifiers , β¦, .
2.3.3 Commands for queries
Request[]
instructs RhoLog to compute one answer substitution of the admissible query For example:
Request[] {}
RequestAll[]
instructs RhoLog to compute all answer substitution of the admissible query For example:
RequestAll[] {}
ApplyRule[]
instructs RhoLog to compute one such that holds.
ApplyRuleList[]
instructs RhoLog to compute the list of all terms such that holds. For example:
ApplyRule["Rw"["elem"],f[{a,{b}}]] f[a] ApplyRuleList["Rw"["elem"],f[{a,{b}}]] {f[a], f[{b}], f[{a,b}]}The runtime behaviors of the commands for queries can be controlled with the following options:
-
β’
Trace -> True | False. If True, a trace of the rule-based computation is generated and shown in a Mathematica nodebook, in human-readable form.
-
β’
Depth -> imposes the maximum length limit for Log=derivations. This option can force RhoLog to produce so-called pending derivations, which are too short to yield a computed answer.
-
β’
MaxSols -> imposes limit on the number of answers to be found. This means that, after finding solutions, RhoLog will stop searching for other answers to the given query.
-
β’
ProofSimplify -> True | False enables/disables the simplification of the explanations produced by tracing.
2.4 Examples and applications
Download and check the following examples:
-
1.
ABACSafety.nb: a tool to check the safety of configurations for the attribute-based access control model (ABAC)
-
2.
GraphColoring.nb: graph colorings
-
3.
EquationalReasoning.nb: reasoning in equational theories represented by terminating rewrite systems
-
4.
EvaluationStrategies.nb: evaluation strategies for functional programming
-
5.
PropositionalProver.nb: a rule-based implementation of a propositional prover, based on Genzenβs calculus.
References
- [DKM09] (2009) Strategies in PLog. ENTCS 15, pp.Β 32β43. Cited by: Log.
- [KM05] (2005) Can context sequence matching be used for xml querying?. In Procs. of UNIF 2005, Cited by: Log.
- [KUT04] (2004) Solving equations involving sequence variables and sequence functions. In 7th Intl. Conf. on Artificial Intelli- gence and Symbolic Computation (AISC 2004), B. Buchberger and J.A. Campbell (Eds.), LNCS, Vol. 3249, pp.Β 157β170. External Links: Document Cited by: itemΒ 1, Β§1.1.1.
- [KUT07] (2007) Solving equations with sequence variables and sequence functions. JSC (3), pp.Β 352β388. Cited by: itemΒ 1, Β§1.1.1.
- [MDK20] (2020) A Rule-Based System for Computation and Deduction in Mathematica. In Rewriting Logic and Its Applications (WRLA 2020), S. Escobar and N. Marti-Oliet (Eds.), LNCS, pp.Β 57β74. External Links: Document Cited by: Log.
- [MKD19] (2019) A Rule-based Approach to the Decidability of Safety of ABACΞ±. In Procs. of the 24th ACM Symposium on Access Control Models and Technologies (SACMAT), F. Kerschbaum, A. Mashatan, J. Niu, and A. J. Lee (Eds.), Toronto, ON, Canada, pp.Β 173β178. Cited by: Log.
- [MK06] (2006) Foundations of the Rule-Based System RhoLog. Journal of Applied Non-Classical Logics 16 (1β2), pp.Β 151β168 (english). Cited by: Log.
- [MP04a] (2004) Deduction and presentation in \rlog. Electronic Notes in Theoretical Computer Science 93, pp.Β 161β182. Cited by: Log.
- [MP04b] (2004) Rule-Based Programming with Mathematica. In International Mathematica Symposium (IMS 2004), P. Mitic, C. J. Jacob, and J. Carne (Eds.), Banff, Alberta, Canada, pp.Β 1β6. Cited by: Log.
- [WOL99] (1999) MATHEMATICA (r) book, version 4. Cambridge University Press. External Links: ISBN 0521643147 Cited by: itemΒ 2.