11institutetext: West University of Timişoara, Timişoara, Romania
11email: mircea.marin@e-uvt.ro

ρLog and RhoLog

Mircea Marin

ρ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

https://staff.fmi.uvt.ro/~mircea.marin/rholog/RhoLog.wl

ρ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 𝑠𝑑𝑔⁒@⁒s, which express the application of strategy s⁒t⁒g to s. We are interested in the reducibility relation 𝑠𝑑𝑔⁒@⁒sβ†’β„›t which is induced by a set β„› of conditional reduction rules of the form

s⁒t⁒g⁒@⁒lβ†’r⇐L1⁒&&…⁒…⁒&&Ln

which we abbreviate by writing s⁒t⁒g⁒@⁒lβ†’r⇐&&i=1nLi. The informal reading of such a rule is β€œThe applicative term 𝑠𝑑𝑔⁒@⁒l can be reduced to r if conditions L1 and … and Ln hold.” For such a set β„› we want to find answers to queries

⇐L1⁒&&…⁒&&Lm

abbreviated ⇐&&i=1mLi, which express a conjunction of logical formulas Li of four kinds:

  1. (1)

    a constraint over a known constraint domain,

  2. (2)

    a reducibility formula 𝑠𝑑𝑔i⁒@⁒siβ†’ti,

  3. (3)

    The logical negation of 𝑠𝑑𝑔i⁒@⁒siβ†’ti, which we denote by 𝑠𝑑𝑔⁒@↛t,

  4. (4)

    A matching formula si≑ti.

An answer to such a query is a ground substitution Οƒ for the variables in the query such that every formula σ⁒(Li) holds in the reduction theory induced by β„›. For our kinds of constraints, this means that: (1)Β if Li is a constraint over a known constraint domain then σ⁒(Li) is valid in it; (2)Β if Li is 𝑠𝑑𝑔⁒@⁒sβ†’t then σ⁒(𝑠𝑑𝑔⁒@⁒s)→ℛσ⁒(t) holds; (3) if Li is 𝑠𝑑𝑔⁒@⁒s↛t then σ⁒(𝑠𝑑𝑔⁒@⁒s)→σ⁒(r) is unsatisfiable, that is, none of the ground instances of σ⁒(𝑠𝑑𝑔⁒@⁒s)→ℛσ⁒(t) 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. 1.

    We have shown that the matching problem is finitary, and defined matching calculi in this algebra of termsΒ [KUT04, KUT07].

  2. 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 V=ViβˆͺVfβˆͺVcβˆͺVs of variables of four kinds. The sets Vi,Vf,Vc, and Vs are countably infinite and mutually disjoint. The elements of Vi, called individual term variables, have names with superscript i, e.g., xi,yi,zi; those of Vf, called function variables, have names with superscript f, e.g., xf,yf,zf; those of Vc, called context variables, have names with superscript c, e.g., xc,yc,zc; and those of Vs, called sequence variables, have names with superscript c, e.g., xs,ys,zs. We use β„± and V to build three kinds of expressions: terms t∈T⁒(β„±,V), term sequences t⁒s,t⁒s1,t⁒s2∈T⁒S⁒(β„±,V), and contexts c⁒t⁒x∈C⁒t⁒x⁒(β„±,V). They are defined by the grammar
t::=xi⁒∣f⁒(t⁒s)∣⁒xf⁒(t⁒s)∣xc⁒[t]t⁒s::=πš—πš˜πš—πšŽβ’βˆ£xs∣⁒t∣t⁒s1,t⁒s2c⁒t⁒x::=βˆ™βˆ£f⁒(t⁒s1,c⁒t⁒x,t⁒s2)∣⁒xf⁒(t⁒s1,c⁒t⁒x,t⁒s2)∣xc⁒[c⁒t⁒x]
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 f instead of f⁒() whenever f is a function symbol.

Thus, the basic expressions of our language are elements of the syntactic domain

E⁒x⁒p⁒r⁒(β„±,V):=T⁒(β„±,V)βˆͺβ„±βˆͺC⁒t⁒x⁒(β„±,V)βˆͺT⁒S⁒(β„±,V).

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 s is a term or context, then the operation c⁒t⁒x⁒[s] of applying 𝑐𝑑π‘₯ to s is defined as follows:

𝑐𝑑π‘₯⁒[s]:={sif ⁒𝑐𝑑π‘₯=βˆ™,f⁒(t⁒s1,𝑐𝑑π‘₯1⁒[s],t⁒s2)if ⁒𝑐𝑑π‘₯=f⁒(t⁒s1,𝑐𝑑π‘₯1,t⁒s2),xf⁒(t⁒s1,𝑐𝑑π‘₯1⁒[s],t⁒s2)if ⁒𝑐𝑑π‘₯=xf⁒(t⁒s1,𝑐𝑑π‘₯1,t⁒s2),xc⁒[𝑐𝑑π‘₯1⁒[s]]if ⁒𝑐𝑑π‘₯=xc⁒[𝑐𝑑π‘₯1].
Strategies

are represented by terms f⁒(…) where fβˆˆβ„± is the identifier of a strategy. The distinguished function symbol @ is used to construct terms @⁒(𝑠𝑑𝑔,s) to indicate the application of strategy 𝑠𝑑𝑔 to s. We call such terms applicative terms and allow the function symbol @ to occur only at the outermost position of an applicative term. We write T@⁒(β„±,V) for the set of applicative terms, T⁒(β„±,V) for the set of other terms without occurrences of @, and V⁒a⁒r⁒s⁒(e) for the set of variables in a syntactic construct e. Also, we use s⁒t⁒g⁒@⁒s as an alternative notation for the applicative term @⁒(s⁒t⁒g,s).

1.1.1 Substitutions.

A substitution is a mapping

Οƒ:Vβ†’T⁒(β„±,V)βˆͺβ„±βˆͺC⁒t⁒x⁒(β„±,V)βˆͺT⁒S⁒(β„±,V)

such that σ⁒(x)∈T⁒(β„±,V) for all x∈Vi, σ⁒(F)βˆˆβ„± for all F∈Vf, σ⁒(C)∈C⁒t⁒x⁒(β„±,V) for all C∈Vc, and σ⁒(xΒ―)∈T⁒S⁒(β„±,V) for all x¯∈Vs.

Every substitution Οƒ can be extended homomorphically to a mapping

Οƒ:E⁒x⁒p⁒r⁒(β„±,V)β†’E⁒x⁒p⁒r⁒(β„±,V).

An expression σ⁒(e) is called an instance of the expression e.

An important property of the term algebra used in our specifications is finitary matching. This means that, for any two terms s∈T⁒(β„±,V) and t∈T⁒(β„±,βˆ…) the set of substitutions

πš–πšŠπšπšŒπš‘πšŽπš›πšœβ’(sβ‰ͺt):={Οƒβˆ£Οƒβ’(s)=t}

is finite [KUT04, KUT07].

1.1.2 Reducibility and matching formulas.

A reducibility formula is an expression 𝑠𝑑𝑔⁒@⁒sβ†’t with 𝑠𝑑𝑔⁒@⁒s∈T@⁒(β„±,V) and t∈T⁒(β„±,V). The informal reading of this formula is β€œthe applicative term 𝑠𝑑𝑔⁒@⁒s is reducible to t”. An irreducibility formula is the logical negation of a reducibility formula 𝑠𝑑𝑔⁒@⁒sβ†’t, and is denoted by 𝑠𝑑𝑔⁒@⁒s↛t. A matching formula is an expression s≑t with s,t∈T⁒(β„±,V). The informal reading of s≑t is β€œs and t are identical”.

1.1.3 Rules and queries.

Strategies are represented by terms f⁒(…) where fβˆˆβ„± is the identifier of a strategy. They are defined by rules of the form

𝑠𝑑𝑔0⁒@⁒lβ†’r⇐L1⁒&&…⁒…⁒&&Ln (1)

abbreviated 𝑠𝑑𝑔0⁒@⁒lβ†’r⇐&&i=1nLi, where every condition Li is either

  1. 1.

    a term from T⁒(β„±,V) that expresses a constraint over a known constraint domain222The constraints of this kind are called atomic constraints., or

  2. 2.

    a reducibility formula 𝑠𝑑𝑔i⁒@⁒liβ†’ri, or

  3. 3.

    an irreducibility formula 𝑠𝑑𝑔i⁒@⁒li↛ri, or

  4. 4.

    a matching formula li≑ri.

The informal reading of (2) is β€œThe reducibility formula 𝑠𝑑𝑔⁒@⁒lβ†’r holds if the conditions L1, …, Ln hold.”. Rules can be unconditional, that is, of the form 𝑠𝑑𝑔⁒@⁒lβ†’r⇐&&i=10Li. We write them in the simplified form 𝑠𝑑𝑔⁒@⁒lβ†’r. 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 t we write 𝑠𝑑𝑔⁒@⁒s instead of 𝑠𝑑𝑔⁒@⁒sβ†’πšπš›πšžπšŽ, and may have rules of the form

𝑠𝑑𝑔0⁒@⁒l⇐L1⁒&&…⁒…⁒&&Ln (2)

A query is of the form

⇐L1⁒&&…⁒&&Lm

abbreviated ⇐&&i=1mLi, where every Li can be of the same form as a rule condition. Given a set V0 of variables, we say that the query ⇐&&i=1mLi is V0-admissible if we can define the sets of variables {Vi∣1≀i≀m} while observing the following restrictions:

  • β€’

    If Li is an atomic constraint then V⁒a⁒r⁒s⁒(Li)βŠ†Viβˆ’1 and Vi:=Viβˆ’1.

  • β€’

    If Li is 𝑠𝑑𝑔i⁒@⁒siβ†’ti then V⁒a⁒r⁒s⁒(𝑠𝑑𝑔i⁒@⁒si)βŠ†Viβˆ’1, and Vi:=Viβˆ’1βˆͺV⁒a⁒r⁒s⁒(ti).

  • β€’

    If Li is 𝑠𝑑𝑔i⁒@⁒si↛ti then V⁒a⁒r⁒s⁒(𝑠𝑑𝑔i⁒@⁒si)βŠ†Viβˆ’1 and Vi:=Viβˆ’1.

  • β€’

    If Li is si≑ti then V⁒a⁒r⁒s⁒(si)βŠ†Viβˆ’1 and Vi:=Viβˆ’1βˆͺV⁒a⁒r⁒s⁒(ti).

Notice that V0βŠ†V1βŠ†β€¦βŠ†Vm. We denote by Q⁒V⁒a⁒r⁒sV0⁒(Q) the set of variables Vm of a V0-admissible query Q of the form ⇐&&i=1mLi, and say that:

  • β€’

    Q is admissible if Q is βˆ…-admissible.

    In particular, the empty query ⇐&&i=10Li is admissible. We denote it by β–‘.

  • β€’

    A rule 𝑠𝑑𝑔0⁒@⁒lβ†’r⇐&&i=1nLi is admissible if the query ⇐&&i=1nLi is V⁒a⁒r⁒s⁒(𝑠𝑑𝑔0⁒@⁒l)-admissible.

1.2 Semantics

A set β„› of admissible rules induces a reduction relation

β†’β„›βŠ†T@(β„±,βˆ…)Γ—T(β„±,βˆ…)

together with a set of answer substitutions

A⁒n⁒s⁒(𝑠𝑑𝑔⁒@⁒sβ†’tβ€²)

for every 𝑠𝑑𝑔⁒@⁒s∈T⁒(β„±,βˆ…) and tβ€²βˆˆT⁒(β„±,V), such that 𝑠𝑑𝑔⁒@⁒sβ†’β„›t′⁒σ holds whenever ΟƒβˆˆA⁒n⁒s⁒(𝑠𝑑𝑔⁒@⁒sβ†’tβ€²). These notions are defined simultaneously as follows:

  • β€’

    𝑠𝑑𝑔⁒@⁒sβ†’β„›t holds if there exists a rule

    (𝑠𝑑𝑔0⁒@⁒lβ†’r⇐&&i=1nLi)βˆˆβ„›

    and ground substitutions Οƒ0βˆˆπš–πšŠπšπšŒπš‘πšŽπš›πšœβ’(𝑠𝑑𝑔0⁒@⁒lβ‰ͺ𝑠𝑑𝑔⁒@⁒s),Οƒ1,…,Οƒn such that

    1. 1.

      For all 1≀i≀n, if σ¯i is the substitution Οƒiβˆ’1βˆ˜β€¦βˆ˜Οƒ1βˆ˜Οƒ0 then

      1. (a)

        If Li is an atomic constraint then Οƒi=Ο΅ and σ¯i⁒(Li) is valid in its corresponding constraint domain.

      2. (b)

        If Li is 𝑠𝑑𝑔i⁒@⁒liβ†’ri then Οƒi∈Ans(σ¯i(𝑠𝑑𝑔i@li)→ℛσ¯i(ri).

      3. (c)

        If Li is 𝑠𝑑𝑔i⁒@⁒li↛ri then Οƒi=Ο΅ and A⁒n⁒s⁒(σ¯i⁒(𝑠𝑑𝑔i⁒@⁒li)→σ¯⁒(ri))=βˆ….

      4. (d)

        If Li is li≑ri then Οƒiβˆˆπš–πšŠπšπšŒπš‘πšŽπš›πšœβ’(σ¯i⁒(ri)β‰ͺσ¯i⁒(li)).

    2. 2.

      t=Οƒn⁒(σ¯n⁒(r)).

  • β€’

    A⁒n⁒s⁒(𝑠𝑑𝑔⁒@⁒sβ†’tβ€²) is a subset of {Οƒβˆˆπš–πšŠπšπšŒπš‘πšŽπš›πšœβ’(tβ€²β‰ͺt)βˆ£π‘ π‘‘π‘”β’@⁒sβ†’β„›t}.

ρLog has two distinguished strategy identifiers: Fst and Cut:

  • β€’

    A⁒n⁒s⁒(π™΅πšœπšβ’(𝑠𝑑𝑔1,…,s⁒t⁒gm)⁒@⁒sβ†’tβ€²) is either

    • –

      βˆ… if A⁒n⁒s⁒(𝑠𝑑𝑔i⁒@⁒sβ†’tβ€²)=βˆ… for all 1≀i≀m, or

    • –

      A⁒n⁒s⁒(𝑠𝑑𝑔i⁒@⁒sβ†’tβ€²) if A⁒n⁒s⁒(𝑠𝑑𝑔i⁒@⁒sβ†’tβ€²)β‰ βˆ… and A⁒n⁒s⁒(𝑠𝑑𝑔j⁒@⁒sβ†’tβ€²)=βˆ… for all 1≀j<i.

  • β€’

    A⁒n⁒s⁒(π™²πšžπšβ’(𝑠𝑑𝑔)⁒@⁒sβ†’tβ€²) is either βˆ… if A⁒n⁒s⁒(𝑠𝑑𝑔⁒@⁒sβ†’tβ€²)=βˆ…, or a singleton subset of A⁒n⁒s⁒(𝑠𝑑𝑔⁒@⁒sβ†’tβ€²) if A⁒n⁒s⁒(𝑠𝑑𝑔⁒@⁒sβ†’tβ€²)β‰ βˆ…. The choice of the singleton subset of A⁒n⁒s⁒(𝑠𝑑𝑔⁒@⁒sβ†’tβ€²) is don’t care, implementation-dependent.

For any strategy 𝑠𝑑𝑔 with identifier different from FstΒ and Cut, we have

  • β€’

    A⁒n⁒s⁒(𝑠𝑑𝑔⁒@⁒sβ†’tβ€²)={Οƒβˆ£π‘ π‘‘π‘”β’@⁒sβ†’β„›t,Οƒβˆˆπš–πšŠπšπšŒπš‘πšŽπš›πšœβ’(tβ€²β‰ͺt)}.

We say that s∈T⁒(β„±,βˆ…) is 𝑠𝑑𝑔-reducible if 𝑠𝑑𝑔⁒@⁒sβ†’β„›t for some term t∈T⁒(β„±,βˆ…). Otherwise, we say that s is 3 𝑠𝑑𝑔-redex.

Let Q be an admissible query of the form ⇐&&i=1mLi. An answer substitution of Q in the reduction theory induced by β„› is a composition of m ground substitutions Οƒ1, …, Οƒm such that, for all 1≀i≀m, if Οƒ0=Ο΅ and σ¯i is the composition of substitutions Οƒiβˆ’1βˆ˜β€¦βˆ˜Οƒ1βˆ˜Οƒ0 then

  1. 1.

    If Li is an atomic constraint then Οƒi=Ο΅ and σ¯i⁒(Li) is valid in its corresponding constraint domain.

  2. 2.

    If Li is 𝑠𝑑𝑔i⁒@⁒liβ†’ri then Οƒi∈A⁒n⁒s⁒(σ¯i⁒(𝑠𝑑𝑔i⁒@⁒li)→σ¯i⁒(ri)).

  3. 3.

    If Li is 𝑠𝑑𝑔i⁒@⁒li↛ri then Οƒi=Ο΅ and A⁒n⁒s⁒(σ¯i⁒(𝑠𝑑𝑔i⁒@⁒li)→σ¯⁒(ri))=βˆ….

  4. 4.

    If Li is li≑ri then Οƒiβˆˆπš–πšŠπšπšŒπš‘πšŽπš›πšœβ’(σ¯i⁒(ri)β‰ͺσ¯i⁒(li)).

We write A⁒n⁒s⁒(Q) for the set of answer substitutions of an admissible query Q.

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. 1.

    ⇐L⁒&&GG
    if L is a valid atomic constraint in its corresponding constraint domain.

  2. 2.

    ⇐(s≑t)⁒&&Gσ⁒(G)
    if Οƒβˆˆπš–πšŠπšπšŒπš‘πšŽπš›πšœβ’(tβ‰ͺs).

  3. 3.

    ⇐(π™΅πšœπšβ’(𝑠𝑑𝑔1,…,𝑠𝑑𝑔m)⁒@⁒sβ†’t)⁒&&G⇐(𝑠𝑑𝑔i⁒@⁒sβ†’t)⁒&&G
    if A⁒n⁒s⁒(𝑠𝑑𝑔i⁒@⁒sβ†’t)β‰ βˆ… and A⁒n⁒s⁒(𝑠𝑑𝑔j⁒@⁒sβ†’t)=βˆ… for all 1≀j<i.

  4. 4.

    ⇐(π™²πšžπšβ’(𝑠𝑑𝑔)⁒@⁒sβ†’t)⁒&&G⇐G⁒σ
    if A⁒n⁒s⁒(π™²πšžπšβ’(𝑠𝑑𝑔)⁒@⁒sβ†’t)={Οƒ}.

  5. 5.

    ⇐(𝑠𝑑𝑔⁒@⁒sβ†’t)⁒&&G⇐&&i=1nσ⁒(Li)⁒&&(σ⁒(r)≑t)⁒&&σ⁒(G)
    if the identifier of 𝑠𝑑𝑔 is neither FstΒ nor Cut,
    (𝑠𝑑𝑔0⁒@⁒lβ†’r⇐&&i=1nLi)βˆˆβ„› is Vars(𝑠𝑑𝑔@sβ†’t)&&G)-renamed apart, and Οƒβˆˆπš–πšŠπšπšŒπš‘πšŽπš›πšœβ’(𝑠𝑑𝑔0⁒@⁒lβ‰ͺ𝑠𝑑𝑔⁒@⁒s).

  6. 5’.

    ⇐𝑠𝑑𝑔⁒@⁒s⁒&&G⇐&&i=1nσ⁒(Li)⁒&&σ⁒(G)
    if the identifier of 𝑠𝑑𝑔 is neither FstΒ nor Cut,
    (𝑠𝑑𝑔0⁒@⁒l⇐&&i=1nLi)βˆˆβ„› is Vars(𝑠𝑑𝑔@sβ†’t)&&G)-renamed apart, and Οƒβˆˆπš–πšŠπšπšŒπš‘πšŽπš›πšœβ’(𝑠𝑑𝑔0⁒@⁒lβ‰ͺ𝑠𝑑𝑔⁒@⁒s).

  7. 6.

    ⇐(s↛𝑠𝑑𝑔t)⁒&&G⇐G
    if the atempt to prove the satisfiability of the query ⇐(s→𝑠𝑑𝑔t) fails finitely.

Every rule of inference is of the form GGβ€².

  • β€’

    Rules 2, 4, 5 and 5’ have as side effect the computation of a substitution Οƒ, and we depict this fact by writing G↝σGβ€².

  • β€’

    Rules 1, 3 and 6 check if something happens without producing a substitution as side effect. We depict them by writing G↝ϡGβ€² where Ο΅ denotes for the identity substitution.

A ρLog-derivation of a query Q is a sequence of reduction steps

Q↝σ1Q1↝σ2Q2↝…↝σnQn

and a ρLog-refutation is a ρLog-derivation which ends with the query β–‘. We abbreviate a ρLog-refutation by writing Qβ†Οƒβˆ—β–‘ where Οƒ is the restriction of the substitution Οƒnβˆ˜β€¦βˆ˜Οƒ2βˆ˜Οƒ1 to Q⁒V⁒a⁒r⁒sβˆ…β’(Q).

As usual for refutation-based calculi, the set of answers computed by ρLog is

A⁒n⁒sρLog⁒(Q):={Οƒβˆ£Qβ†Οƒβˆ—β–‘}.

It can be shown that A⁒n⁒sρLog⁒(Q)=A⁒n⁒s⁒(Q). This means that our calculus is sound and complete.

1.4 Some useful strategies

xiβ†’π™Έπšxi
xiβ†’π™²πš˜πš–πš™β’()xi
xiβ†’π™²πš˜πš–πš™β’(si,s⁒ss)zi⇐(xiβ†’siyi)⁒&&(yiβ†’π™²πš˜πš–πš™β’(s⁒ss)zi)
xi→𝙽𝙡⁒(si)xi⇐(xi↛si_i)
xi→𝙽𝙡⁒(si)zi⇐(xiβ†’siyi)⁒&&(yi→𝙽𝙡⁒(si)zi)
xiβ†’π™Ύπš›β’(si,_s)yi⇐(xiβ†’ssyi)
xiβ†’π™Ύπš›β’(_i,s⁒ss)yi⇐(xiβ†’π™Ύπš›β’(s⁒ss)yi)
xiβ†’πšπšŽπš™πšŽπšŠπšβ’(_i)xi
xiβ†’πšπšŽπš™πšŽπšŠπšβ’(si)yi⇐xiβ†’π™²πš˜πš–πš™β’(si,πšπšŽπš™πšŽπšŠπšβ’(si))yi)
xc⁒[yi]β†’πšπš β’(si)xc⁒[zi]⇐(yiβ†’sizi)
gf⁒(x⁒ss,xi,y⁒ss)β†’πšπš πŸ·β’(si)gf⁒(x⁒ss,yi,y⁒ss)⇐(xiβ†’siyi)
yiβ†’π™ΎπšžπšπšŽπš›β’(si)zi⇐(yiβ†’π™΅πšœπšβ’(si,𝚁𝚠𝟷⁒(π™ΎπšžπšπšŽπš›β’(si)))zi)
yiβ†’π™Έπš—πš—πšŽπš›β’(si)zi⇐(yiβ†’π™΅πšœπšβ’(𝚁𝚠𝟷⁒(π™Έπš—πš—πšŽπš›β’(si)),si)zi)
xiβ†’π™»πšŠπš£πš’π™΄πšŸπšŠπš•β’(si)yi⇐(xi→𝙽𝙡⁒(π™ΎπšžπšπšŽπš›β’(si))yi)
xiβ†’πš‚πšπš›πš’πšŒπšπ™΄πšŸπšŠπš•β’(si)yi⇐(xi→𝙽𝙡⁒(π™Έπš—πš—πšŽπš›β’(si))yi)
gf⁒()β†’π™΅πš–πšŠπš™β’(_i)gf⁒()
gf⁒(xi,x⁒ss)β†’π™΅πš–πšŠπš™β’(si)gf⁒(yi,y⁒ss)⇐(xiβ†’siyi)⁒&&(gf⁒(x⁒ss)β†’π™΅πš–πšŠπš™β’(si)gf⁒(y⁒ss))

Like in Prolog, we make use of the anonymous variables _i, _f, _c and _s 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

https://staff.fmi.uvt.ro/~mircea.marin/rholog/RhoLog.wl

2.1 Installation instructions

  1. 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. 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

  • β€’

    s→𝑠𝑑𝑔t instead of s⁒t⁒g⁒@⁒sβ†’t, and s↛𝑠𝑑𝑔t instead of 𝑠𝑑𝑔⁒@⁒s↛t

  • β€’

    𝑠𝑑𝑔⁒::⁒s instead of 𝑠𝑑𝑔⁒@⁒sβ†’πšπš›πšžπšŽ.

For strategy identifiers, we use strings delimited by double quotes.

2.3 RhoLog commands

2.3.1 Commands for rule declarations

DeclareRule[r⁒u⁒l⁒e]

declares a single rule r⁒u⁒l⁒e, and

DeclareRules[r⁒u⁒l⁒e1,…,r⁒u⁒l⁒en]
declares rules r⁒u⁒l⁒e1,…,r⁒u⁒l⁒en. For example, the strategies with identifiers "β’π™Έπšβ’", "β’π™΄πš•πšŽπš–β’", "β’π™²πš˜πš–πš™β’", "⁒𝙽𝙡⁒", "β’π™Ύπš›β’", "β’πšπšŽπš™πšŽπšŠπšβ’", "⁒𝚁𝚠⁒", "⁒𝚁𝚠𝟷⁒", "β’π™΅πš–πšŠπš™β’", "β’π™ΎπšžπšπšŽπš›β’", "β’π™Έπš—πš—πšŽπš›β’", "β’π™»πšŠπš£πš’π™΄πšŸπšŠπš•β’", "β’πš‚πšπš›πš’πšŒπšπ™΄πšŸπšŠπš•β’", "β’π™ΏπšŽπš›πš–β’", and "β’πš‚πšžπš‹πšœπšŽπšβ’" are predefined in RhoLog:

π™³πšŽπšŒπš•πšŠπš›πšŽπšπšžπš•πšŽπšœ[
πš‘πš’β†’"β’π™Έπšβ’"πš‘πš’
{_𝚜,πš‘πš’,_𝚜}β†’"β’π™΄πš•πšŽπš–β’"πš‘πš’
πš‘πš’β†’"β’π™²πš˜πš–πš™β’"⁒[]πš‘πš’
πš‘πš’β†’"β’π™²πš˜πš–πš™β’"⁒[πšœπš’,𝚜𝚜𝚜]πš£πš’β‡(πš‘πš’β†’πšœπš’πš’πš’)⁒&&(πš’πš’β†’"β’π™²πš˜πš–πš™β’"⁒[𝚜𝚜𝚜]πš£πš’)
πš‘πš’β†’"β’π™Ύπš›β’"⁒[πšœπš’,_𝚜]πš’πš’β‡(πš‘πš’β†’πšœπšœπš’πš’)
πš‘πš’β†’"β’π™Ύπš›β’"⁒[_πš’,𝚜𝚜𝚜]πš’πš’β‡(πš‘πš’β†’"β’π™Ύπš›β’"⁒[𝚜𝚜𝚜]πš’πš’)
πš‘πš’β†’"β’πšπšŽπš™πšŽπšŠπšβ’"⁒[_πš’]πš‘πš’
πš‘πš’β†’"β’πšπšŽπš™πšŽπšŠπšβ’"⁒[πšœπš’]πš’πš’β‡πš‘πš’β†’"β’π™²πš˜πš–πš™β’"⁒[πšœπš’,"β’πšπšŽπš™πšŽπšŠπšβ’"⁒[πšœπš’]]πš’πš’)
πš‘πš’β†’"β’πšπšŽπš™πšŽπšŠπšβ’"⁒[_πš’,𝟢]πš‘πš’
πš‘πš’β†’"πšπšŽπš™πšŽπšŠπš"[πšœπš’,πš—πš’/;π™Έπš—πšπšŽπšπšŽπš›πš€[πš—]&&(πš—>𝟢)]πš’πš’β‡πš‘πš’β†’"π™²πš˜πš–πš™"[πšœπš’,"πšπšŽπš™πšŽπšŠπš"[πšœπš’,πš—πš’βˆ’πŸ·]πš’πš’)
πš‘πš’β†’"⁒𝙽𝙡⁒"⁒[πšœπš’]πš‘πš’β‡(πš‘πš’β†›πšœπš’_πš’)
πš‘πš’β†’"⁒𝙽𝙡⁒"⁒[πšœπš’]πš£πš’β‡(πš‘πš’β†’πšœπš’πš’πš’)⁒&&(πš’πš’β†’"⁒𝙽𝙡⁒"⁒[πšœπš’]πš£πš’)
𝚑𝚌⁒[πš’πš’]β†’"⁒𝚁𝚠⁒"⁒[πšœπš’]𝚑𝚌⁒[πš£πš’]⇐(πš’πš’β†’πšœπš’πš£πš’)
𝚐𝚏⁒[𝚑𝚜𝚜,πš‘πš’,𝚒𝚜𝚜]β†’"⁒𝚁𝚠𝟷⁒"⁒[πšœπš’]𝚐𝚏⁒[𝚑𝚜𝚜,πš’πš’,𝚒𝚜𝚜]⇐(πš‘πš’β†’πšœπš’πš’πš’)
πš’πš’β†’"β’π™ΎπšžπšπšŽπš›β’"⁒[πšœπš’]πš£πš’β‡(πš’πš’β†’"β’π™΅πšœπšβ’"⁒[πšœπš’,"⁒𝚁𝚠𝟷⁒"⁒["β’π™ΎπšžπšπšŽπš›β’"⁒[πšœπš’]]]πš£πš’)
πš’πš’β†’"β’π™Έπš—πš—πšŽπš›β’"⁒[πšœπš’]πš£πš’β‡(πš’πš’β†’"β’π™΅πšœπšβ’"⁒["⁒𝚁𝚠𝟷⁒"⁒["β’π™Έπš—πš—πšŽπš›β’"⁒[πšœπš’]],πšœπš’]πš£πš’)
𝚐𝚏⁒[]β†’"β’π™΅πš–πšŠπš™β’"⁒[_πš’]𝚐𝚏⁒[]
𝚐𝚏[πš‘πš’,𝚑𝚜𝚜]β†’"β’π™΅πš–πšŠπš™β’"⁒[πšœπš’]𝚐𝚏[πš’πš’,𝚒𝚜𝚜]⇐(πš‘πš’β†’πšœπš’πš’πš’)&&(𝚐𝚏[𝚑𝚜𝚜]β†’"β’π™΅πš–πšŠπš™β’"⁒[πšœπš’]𝚐𝚏[𝚒𝚜𝚜]]
πš‘πš’β†’"β’π™»πšŠπš£πš’π™΄πšŸπšŠπš•β’"⁒[πšœπš’]πš’πš’β‡(πš‘πš’β†’"⁒𝙽𝙡⁒"⁒["β’π™ΎπšžπšπšŽπš›β’"⁒[πšœπš’]]πš’πš’)
πš‘πš’β†’"β’πš‚πšπš›πš’πšŒπšπ™΄πšŸπšŠπš•β’"⁒[πšœπš’]πš’πš’β‡(πš‘πš’β†’"⁒𝙽𝙡⁒"⁒["β’π™Έπš—πš—πšŽπš›β’"⁒[πšœπš’]]πš’πš’)
πš•πš’/;π™»πš’πšœπšπš€[πš•]β†’"β’π™ΏπšŽπš›πš–β’"πš›πš’β‡(πš•πš’β†’"β’πš™πšŽπš›πš–π™Έβ’"⁒[𝟷,π™»πšŽπš—πšπšπš‘β’[π™ΏπšŽπš›πš–πšžπšπšŠπšπš’πš˜πš—β’[πš•πš’]]]πš›πš’)
πš•πš’β†’"β’πš™πšŽπš›πš–π™Έβ’"⁒[πš—πš’,_πš’]π™ΏπšŽπš›πš–πšžπšπšŠπšπš’πš˜πš—πšœβ’[πš•πš’]⁒[[πš—πš’]]
πš•πš’β†’"β’πš™πšŽπš›πš–π™Έβ’"⁒[πš—πš’,πš–πš’]πš›πš’β‡((πš—πš’<πš–πš’)⁒&&(πš•πš’β†’"β’πš™πšŽπš›πš–π™Έβ’"⁒[πš—πš’+𝟷,πš–πš’]πš›πš’))
πš•πš’/;π™»πš’πšœπšπš€[πš•]β†’"β’πš‚πšžπš‹πšœπšŽπšβ’"πš›πš’β‡(πš•πš’β†’"πšœπšœπ™Έ"[𝟷,𝟸^π™»πšŽπš—πšπšπš‘[πš•πš’]πš›πš’)
πš•πš’β†’"β’πšœπšœπ™Έβ’"⁒[πš—πš’,_πš’]πš‚πšžπš‹πšœπšŽπšπšœβ’[πš•πš’]⁒[[πš—πš’]]
πš•πš’β†’"β’πšœπšœπ™Έβ’"⁒[πš—πš’,πš–πš’]πš›πš’β‡((πš—πš’<πš–πš’)⁒&&(πš•πš’β†’"β’πšœπšœπ™Έβ’"⁒[πš—πš’+𝟷,πš–πš’]πš›πš’))

Notice that

  • β€’

    "Id"⁒@⁒sβ†’β„›t holds iff s=t.

  • β€’

    "Elem"⁒@⁒l⁒s⁒tβ†’β„›t folds iff l⁒s⁒t is a list and t is an element of l⁒s⁒t.

  • β€’

    "Comp" is for sequential composition of reductions:

    "Comp"[𝑠𝑑𝑔1,…,𝑠𝑑𝑔n]⁒@⁒sβ†’β„›t holds iff there exist ground terms t1,…,tn such that 𝑠𝑑𝑔1⁒@⁒sβ†’β„›t1, 𝑠𝑑𝑔2⁒@⁒t1β†’β„›t2, …, 𝑠𝑑𝑔n⁒@⁒tnβˆ’1β†’β„›tn hold and tn=t.

  • β€’

    "Or" is for parallel reduction:

    "Or"[𝑠𝑑𝑔1,…,𝑠𝑑𝑔n]⁒@⁒sβ†’β„›t holds iff 𝑠𝑑𝑔i⁒@⁒sβ†’β„›t holds for some 1≀i≀n.

  • β€’

    "Repeat"⁒[𝑠𝑑𝑔,n]⁒@⁒sβ†’β„›t holds if there exist terms t1,…,tn in T⁒(β„±,βˆ…) such that the relations 𝑠𝑑𝑔⁒@⁒sβ†’β„›t1, 𝑠𝑑𝑔⁒@⁒t1β†’β„›t2, …, 𝑠𝑑𝑔⁒@⁒tnβˆ’1β†’β„›tn hold, and t=tn. "Repeat"⁒[𝑠𝑑𝑔]⁒@⁒sβ†’β„›t holds if "Repeat"⁒[𝑠𝑑𝑔,n]⁒@⁒sβ†’β„›t holds for an integer nβ‰₯0.

  • β€’

    "Rw" is for rewriting: "Rw"⁒[𝑠𝑑𝑔]⁒@⁒sβ†’β„›t holds if (1) s has a subterm l such that 𝑠𝑑𝑔⁒@⁒lβ†’β„›r, and (2) t is obtained from s by replacing l with r.

  • β€’

    "NF" is for normalizing reduction:

    "NF"[𝑠𝑑𝑔]⁒@⁒sβ†’β„›t holds if "Repeat"[𝑠𝑑𝑔]⁒@⁒sβ†’β„›t holds and 𝑠𝑑𝑔⁒@⁒t is irreducible, that is, there is no tβ€² such that 𝑠𝑑𝑔⁒@⁒tβ†’β„›tβ€².

  • β€’

    "Rw1"[𝑠𝑑𝑔] is for reducing a 𝑠𝑑𝑔-redex immediately below the root of a term: "Rw1"⁒[𝑠𝑑𝑔]⁒@⁒sβ†’β„›t holds if s is of the form f⁒[…,l,…], t is of the form f⁒[…,r,…], and 𝑠𝑑𝑔⁒@⁒lβ†’β„›r holds.

  • β€’

    "Fmap"[𝑠𝑑𝑔]⁒@⁒sβ†’β„›t holds iff s is a term of the form f⁒[s1,…,sn], t is of the form f⁒[t1,…,tn], and 𝑠𝑑𝑔⁒@⁒siβ†’β„›ti hold for all 1≀i≀n.

  • β€’

    "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"@⁒l⁒s⁒tβ†’t holds iff l⁒s⁒t is a list and t is a permutation of l⁒s⁒t.

  • β€’

    "Subset"@⁒sβ†’t holds iff s is a set represented by a list and t is a subset of l⁒s⁒t.

  • β€’

    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[l⁒s⁒t] returns the list of all distinct permutations of list l⁒s⁒t;

      Subsets[s] returns the list of all subsets of s.

    • –

      s/;cond in Mathematica is a conditional pattern, in the following sense: Οƒβˆˆπš–πšŠπšπšŒπš‘πšŽπš›πšœ(s/;condβ‰ͺt) iff σ⁒(s)=t and σ⁒(c⁒o⁒n⁒d) 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
⇐(𝑠𝑑𝑔⁒@⁒sβ†’t)⁒&&G⇐&&i=1nσ⁒(Li)⁒&&(σ⁒(r)≑t)⁒&&σ⁒(G)
with the renamed-apart rule (𝑠𝑑𝑔0@lβ†’r⇐&&i=1nLi and matcher

Οƒβˆˆπš–πšŠπšπšŒπš‘πšŽπš›πšœβ’(𝑠𝑑𝑔0⁒@⁒lβ‰ͺ𝑠𝑑𝑔⁒@⁒s)

then #1 is replaced with t in &&i=1nσ⁒(Li). This capability allows us to specify the behavior of "Fst" as follows:

π™³πšŽπšŒπš•πšŠπš›πšŽπšπšžπš•πšŽπšœ[
πš‘πš’β†’"β’π™΅πšœπšβ’"⁒[πšœπš’,_𝚜]πš’πš’β‡(πš‘πš’β†’πšœπš’πš’πš’)
πš‘πš’β†’"β’π™΅πšœπšβ’"⁒[πšœπš’,𝚑𝚜𝚜]πš’πš’β‡((πš‘πš’β†›πšœπš’#1)&&(πš‘πš’β†’"β’π™΅πšœπšβ’"⁒[𝚑𝚜𝚜]πš’πš’))]

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:
[Uncaptioned image]
ClearRules[r⁒u⁒l⁒e⁒I⁒d1,…,r⁒u⁒l⁒e⁒I⁒dn]
clears the defining rules for the rule identifiers r⁒u⁒l⁒e⁒I⁒d1, …, r⁒u⁒l⁒e⁒I⁒dn.

2.3.3 Commands for queries

Request[L1⁒&&…⁒&&Ln]

instructs RhoLog to compute one answer substitution of the admissible query ⇐L1⁒&&…⁒&&Ln. For example:

Request[{𝚊,πš‹,𝚌}β†’"β’π™΄πš•πšŽπš–β’"πš‘πš’]
{πš‘β†’πšŠ}
RequestAll[L1⁒&&…⁒&&Ln]
instructs RhoLog to compute all answer substitution of the admissible query ⇐L1⁒&&…⁒&&Ln. For example:
RequestAll[{𝚊,πš‹,πš‹}β†’"β’π™ΏπšŽπš›πš–β’"πš‘πš’]
{{πš‘β†’{𝚊,πš‹,πš‹}},{πš‘β†’{πš‹,𝚊,πš‹}},{πš‘β†’{πš‹,πš‹,𝚊}}}
ApplyRule[𝑠𝑑𝑔,s]
instructs RhoLog to compute one t such that 𝑠𝑑𝑔⁒@⁒sβ†’β„›t holds.
ApplyRuleList[𝑠𝑑𝑔,s]
instructs RhoLog to compute the list of all terms t such that 𝑠𝑑𝑔⁒@⁒sβ†’β„›t 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 -> n⁒n⁒n imposes the maximum length limit n⁒n⁒n for ρLog=derivations. This option can force RhoLog to produce so-called pending derivations, which are too short to yield a computed answer.

  • β€’

    MaxSols -> n⁒n⁒n imposes limit n⁒n⁒n on the number of answers to be found. This means that, after finding n⁒n⁒n 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. 1.

    ABACSafety.nb: a tool to check the safety of configurations for the attribute-based access control model (ABAC)

  2. 2.

    GraphColoring.nb: graph colorings

  3. 3.

    EquationalReasoning.nb: reasoning in equational theories represented by terminating rewrite systems

  4. 4.

    EvaluationStrategies.nb: evaluation strategies for functional programming

  5. 5.

    PropositionalProver.nb: a rule-based implementation of a propositional prover, based on Genzen’s calculus.

References

  • [DKM09] B. Dundua, T. Kutsia, and M. Marin (2009) Strategies in PρLog. ENTCS 15, pp.Β 32–43. Cited by: ρLog.
  • [KM05] T. Kutsia and M. Marin (2005) Can context sequence matching be used for xml querying?. In Procs. of UNIF 2005, Cited by: ρLog.
  • [KUT04] T. Kutsia (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] T. Kutsia (2007) Solving equations with sequence variables and sequence functions. JSC (3), pp.Β 352–388. Cited by: itemΒ 1, Β§1.1.1.
  • [MDK20] M. Marin, B. Dundua, and T. Kutsia (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] M. Marin, T. Kutsia, and B. Dundua (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] M. Marin and T. Kutsia (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] M. Marin and F. Piroi (2004) Deduction and presentation in \rlog. Electronic Notes in Theoretical Computer Science 93, pp.Β 161–182. Cited by: ρLog.
  • [MP04b] M. Marin and F. Piroi (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] S. Wolfram (1999) MATHEMATICA (r) book, version 4. Cambridge University Press. External Links: ISBN 0521643147 Cited by: itemΒ 2.