A system for rule-based programming in Mathematica
Description
ρLog is a system for rule-based programming with reduction strategies. It is implemented in the Wolfram Language as an add-on package that extends the rule-based programming capabilities of Mathematica in significant ways. Its theoretical foundation is a calculus for rule-based programming designed by us in 2004, which was called ρLog too.
The ρLog calculus was also used to implement PρLog, an experimental tool that extends logic programming with strategic conditional transformation rules. PρLog combines Prolog with the ρLog calculus.
Rule-based programming with reduction strategies is a programming style similar to logic programming: Programs consists of general clauses of the form
where
- the head H of the clause is a reducibility atom of the form
- with the intended reading "s holds according to strategy stg", or
- with the intended reading "s is reducible to t with strategy stg".
- stg is of the form sId or sId[ts], where
- sId is a string called the identifier of stg, and
- ts can be any term sequence.
- the body of the clause is a conjunction of literals L1,...,Ln. A literal is an atom or the negation of an atom, where atom is either (1) a reducibility atom or , or (2) any boolean expression of Mathematica, that is, an expression that Mathematica evaluates to True or False
When n=0, the body is empty and we write the clause in the form H.
We call these kinds of program clauses
There are some important similarities and differences between Prolog and ρLog:strategic conditional transformation rules because they can have reducibility atoms which strategies that specify how to check their satisfiability.- Prolog is for Logic Programming, where program clauses are of the form
Such a clause provides a partial definition for the predicate whose name is p.
ρLog is for Rule-based Programming, where program clauses do not define predicates but strategies: A program clause
provides a partial definition which is associated with the identifier of strategy stg. - In Prolog, objects are denoted by ordinary terms t from first-order logic (FOL) defined by the grammar
where X is an ordinary variable and f is a function symbol of rank n.
In ρLog, objects are denoted by terms from a language which extends the language of FOL with more kinds of variables: ordinary variables x,y,z,...(for terms), function variables F,G,... (for function symbols), sequence variables X,Y,Z,... (for finite sequences of terms), and context variables Fo,Go,Ho,... (for terms with one hole). Terms t and term sequences ts are defined by mutual recursion as follows:
t ::= x | f[ts] | F[ts] | Co[t]
ts ::= t | X | ()
where () denotes the empty sequence of terms. Ordinary variables, function variables, and sequence variables are characteristic to the Mathematica language, whereas context variables are a novelty of ρLog. Therefore, in ρLog we can write very concise and easy-to-understand program specifications, which are cumbersome or impossible to express in Prolog. - In Prolog we rely on mgu-based SLDNF-resolution to answer queries, but in ρLog we can not because the unification problem of ρLog terms is not finitary. For example, the terms f(a,X) and f(X,a) have infinitely many most general unifiers:
, etc.
The good news is that matching in ρLog is finitary. Therefore, we impose a natural syntactic restriction on programs and queries called determinism, which guarantees that we can rely on matching-based SLDNF-resolution to answer queries.
ρLog is designed to check the satisfiability of queries with respect to a given program. A query is a logical conjunction of literals where all variables are existentially quantified by default. ρLog is designed to answer queries Q, that is, to find substitutions θ such that Qθ can be deduced from the definitions of strategy combinators and strategies defined so far. Like Prolog, ρLog computes so-called answer substitutions using a form of SLDNF-resolution which is adapted for rule-based programming with reduction strategies. The literals of the goal are answered from left to right.
To ensure that queries can be answered by matching instead of unification we impose a very natural syntactic restriction on queries and program clauses, called determinism:
- if W is a set of variables, we say that a query Q=L1∧...∧Li∧...∧Ln is W-deterministic if the following condition is satisfied for every :
- If Li is or or Not[] or Not[expr] where expr is a boolean expression of Mathematica, then
- If Li is Not[] then and
- A query is deterministic if it is ∅-deterministic.
- A program clause is deterministic if Q is a Vars(s,stg)-deterministic query.
- A program clause is deterministic if and Q is a Vars(s,stg)-deterministic query.
Examples. The following program clauses are deterministic and define strategy "qsort" to sort a list of numbers by quicksort:
and is a deterministic query which asks for the value of the ordinary variable x to which the list {4,1,5,2} gets reduced with strategy "qsort". The expected answer is .The strategy "rw"[s] defined below is for term rewriting with an arbitrary strategy s:
Remarks:- The strategy "qsort" is defined by two program clauses: one for sorting the empty list, and another for sorting a nonempty list.
- "qsort" depends on the auxiliary strategy "split" which splits a nonempty list into two lists: the list of elements smaller than the first element, and the list of elements smaller or equal to the first element, including the first element itself.
- We can use of the predefined operations of Mathematica. In this example we use Join to join lists, and Prepend to add an element in front of a list.
- We use the syntax of Mathematica to recognize the different kinds of variables:
- The ordinary variables and the function variables correspond to the occurrences of symbols whose first occurrences are followed by one underscore character '_'. In our example, x,y,l1,l2,xs,as,bs are ordinary variables.
- The sequence variables correspond to the occurrences of symbols whose first occurrences are followed by three underscore characters , that is, by '___'. In our example, ys is a sequence variable.
- The context variables correspond to occurrences of expressions of the form Co or C_o where C is a Mathematica symbol. Only the occurrences in the first term of the name C of the context variable should be in an expression C_o;
Strategies
Strategies are the core concept of rule-based programming. Every reducibility atom or has a component stg which controls how to check that the atom is satisfiable. The satisfiability test of a deterministic query always starts from L. A successful satisfiability test of L yields a computed answer θ which instantiates all variables in L with ground terms, and the computation continues by checking the satisfiability of the deterministic query Qθ. Thus, the satisfiability test of a deterministic query is performed by computing the derivations
, abbreviated
where is the answer for Q computed by this derivation.For programming purposes, the user must only know the meanings of the deterministic queries and by looking at the syntactic structure of stg. Note that determinism implies that the expressions stg and s are ground.
In ρLog, strategies are defined by the following grammar:
where sId is a strategy name (a Mathematica string), ts is a term sequence.Strategies specified by program clauses
The first two kinds of strategies, stg∈{sId,sId[ts]}, are specified by general program clauses
which are either predefined or declared by the user during a ρLog session.The following strategies are predefined:
- "Id" is the strategy for identity:
is satisfied by any substitution θ which is a matcher of t with gt, that is, tθ=gt. In ρLog we can write instead of . - "rw"[stg] is the strategy for term rewriting:
is satisfied by any substitution θ for which there is a subterm gt|p of gt which satisfies and tθ=gt[uθ]p. - "elem" is the strategy for list membership:
is satisfied by any substitution θ for which tθ is an element of list gt.
Strategy combinators
- "" is the combinator for strategy composition:
is satisfied by any substitution θ for which there is a ground term s such that θ satisfies both and . - "|" is the combinator for nondeterministic choice:
is satisfied by any substitution θ which satisfies a query for some . - "*" is the combinator for repetition:
is satisfied by any substitution θ which satisfies or a query for a finite number of ground terms s1,...,sn. - "NF" is the normalization combinator:
is satisfied by any substitution θ which satisfies and t is irreducible with stg, that is, there is no term u such that is satisfiable. - "Fst" is the strategy combinator which behaves as follows:
is satisfied by any substitution θ which satisfies and i is the smallest index for which gt is reducible with strategy stgi. - "CUT" is a combinator for don't care choice of a reduction:
- is satisfied by only one substitution θ (don't care which) which satisfies the query
- is satisfied by only one substitution θ (don't care which) which satisfies the query
It allows to use arbitrary Mathematica expressions to express conditions in requests and rules. The implementation of our strategy language is based on a revised version of the ρLog calculus.
The ρLog calculus was also used to implement PρLog, an experimental tool that extends logic programming with strategic conditional transformation rules. PρLog combines Prolog with the ρLog calculus.
ρLog is an Add-on package for Mathematica which can be downloaded for free from here. The download is the file RhoLog.wl which should be copied in
$UserBaseDirectory/Applications .To use ρLog , you should first load it with the command
<<RhoLog` ρLog commands
DeclareRules [rule1,...,rulen ]rule1,...,rulen to the set of rules of the current program.Rules [sId ]sId . For example,Rules ["rw"]Rules ["rw1"]Request [Q,options ]RequestAll [Q,options ]Q with respect to the current program. If the query is unsatisfiable, thenRequest returns "No solution found.", andRequestAll returns the empty list {}. Options:Trace → True|False. If True, it instructs the system to enable tracing, that is, to generate a notebook with a trace of the search for a solution.Depth → nnn imposes the limit nnn on the maximum number of transformation steps in the depth search of a solution forquery .MaxSols → nnn is an option forRequestAll . It imposes the limit nnn on the maximum number of solutions forquery .ProofSimplify → True|False. If True, it instructs the system to simplify the proof produced by the trace of the computation.
Request [f[g[a,b],g[a,c]]≡F_[C_o[x_],Co[y_]]]Request [f[g[a,b],g[a,c]]≡F_[C_o[x_],Co[y_]]]
Note that, in Mathematica, Identity is the identity function, and (g[a,#1]&) is the function that maps every term t to the term g[a,t]ApplyRule [stg,gt,options ]-
Trace ,Depth , andProofSimplify , with same meaning as forRequest .
ApplyRuleList [stg,gt,options ]instructs the system to compute all reducts of gt with respect to strategy stg, that is, the list of all ground terms t such that formula holds.
Options:-
Trace ,Depth , andProofSimplify , with same meaning as forRequest . -
MaxSols , with the same meaning as forRequestAll .
Publications
The first version of the ρLog calculus is described in
- Mircea Marin and Temur Kutsia. Foundations of the Rule-Based System ρLog. Journal of Applied Non-Classical Logics, 16(1–2):151–168, 2006. PDF.
- Mircea Marin, Florina Piroi: Deduction and Presentation in ρLog. Electr. Notes Theor. Comput. Sci. 93: 161-182 (2004)
- Temur Kutsia, Mircea Marin: Can Context Sequence Matching Be Used for XML Querying? Procs. of UNIF 2005, 77-95 (2005). PDF.
- Temur Kutsia, Mircea Marin: Matching with Regular Constraints. LPAR 2005: 215-229 (2005). PDF.
- Mircea Marin, Tetsuo Ida: Rule-Based Programming with ρLog. SYNASC 2005: 31-38 (2005). PDF.
- Mircea Marin and Temur Kutsia. Foundations of the Rule-Based System ρLog. Journal of Applied Non-Classical Logics, 16(1–2):151–168, 2006. PDF.
- Mircea Marin, Temur Kutsia, and Besik Dundua: A Rule-based Approach to the Decidability of Safety of ABACα. SACMAT 2019:173-178. PDF.
- Mircea Marin, Besik Dundua, and Temur Kutsia: A Rule-Based System for Computation and Deduction in Mathematica. WRLA@ETAPS 2020: 57-74. PDF.
Download and installation instructions
The current distribution is implemented in Wolfram Mathematica 12.3. To install it, do the following steps:
-
Open Mathematica, and evaluate
$UserBaseDirectoryto find out the base directory of your Mathematica installation. Then, close Mathematica.
- Download the file RhoLog.wl, and copy it in
$UserBaseDirectory/Applications . - Open a Mathematica notebook and load the
RhoLog package with the command<<RhoLog` - Check if the
RhoLog package is available, by evaluating?RequestIf everything worked smoothly, Mathematica will display information about the commandRequest ofRhoLog .
People