Computing technology is enabled by the interaction between computer hardware and software. The software, when executed by one or more processors, directs the functionality of computing systems through the appropriate use of system hardware, and interprets appropriate responses to events detected by the hardware.
Programming languages are a valuable tool that computer programmers often use to generate source code that, when compiled and/or interpreted, become the machine readable instructions that are actually executed by the computer. Programming languages define a set of concrete syntax that the computer programmer may use to describe the functionality of the computer program using source code. The syntactical rules followed by the programming language are designed to offer an intuitive mechanism to formulate the software.
There are, at present, a number of different categories of programming languages including, logic programming and rewrite programming. In this description and in the claims, “logic programming” is any programming that uses predicates that include only horn clauses, the predicates having arguments and the predicate being first order. For example, consider the following logical predicate described in equation 1.
P(X)̂ Q(x)→R(x) (1)
This predicate represents a horn clause because it is an implication (as represented by it being an equation that includes the symbol “→” that has only one result of the implication (in the example, R(x)). Furthermore, the predicate has arguments. Specifically, each of the functions P, Q and R has one argument x. Furthermore, the predicate is first order in that none of the arguments are themselves predicates. Therefore, equation 1 represents a valid example of logic programming.
The following Equations 2 through 4 are examples of clauses that do not represent logic programming under the definition provided above.
p→Q (2)
read as “P implies Q”
P(x)̂ Q(x)→R(x)̂ S(X) (3)
read as “P(x) and Q(x) implies R(x) and S(x)”
P(R(x))→Q(x) (4)
Equation 2 is not an example of logic programming as the term “logic programming” is defined herein since none of the predicates “P” and “Q” include an argument. Equation 3 is not an example of logic programming as defined herein since there are two results of the implication (i.e., R(x) and S(x)), and therefore Equation 3 is not a horn clause. Equation 4 is not an example of logic programming as defined herein since the argument of one of the predicates is itself a predicate. In other words, R(x) is a predicate and an argument of P(x). Therefore, Equation 4 is not first order.
Logic programming is quite powerful in that it permits unification to thereby support a higher level, more declarative programming model. Unification is an automated and sometimes quite complex logical deduction process in which bound and unbound variables are kept track of while solving for the unbound variables. The position of the variables within a concrete expression of logic programming does not necessarily imply anything about whether the variable is bound or unbound. While unification is powerful and allows for a lot of flexibility in the expression of bound and unbound variables within the programming language, the execution is often relatively slow.
Furthermore, logic programming and the associated unification processes may need special runtime support, and might not be supported by a standard runtime environment such as the common language runtime (CLR) or even runtimes for functional languages such as Lisp, F#, ML, and Haskell.
Rewrite programming, on the other hand, does not work on the principles of unification and therefore requires no such support from an underlying runtime. It is, in fact, its own runtime. Every runtime has an associated language, the CLR has MSIL, Warren Abstract Machine (WAM) has Prolog, Intel processors have x86, and so forth. Rewrite programs do not need to keep track of which variables are bound and which variables are unbound to thereby perform logic deduction unification processes. Instead, rewrite systems perform simple matching. In other words, one expression is searched for, and if a match is found, the expression is replaced by another expression.
Since rewrite programs do not require complex logic deduction-based unification, rewrite programs are typically executed much more quickly. Furthermore, since rewrite programs are their own common runtime environment, they do not need to rely on the system having other common runtime environments in order to operate properly.
Although not required, embodiments of the present invention relate to the transformation of logic programming into rewrite programming. First, a concrete expression of logic programming is converted into an abstract expression of logic programming or the abstract expression of logic programming is accessed in some other way. The abstract expression of logic programming is then transformed into an abstract expression of rewrite programming. The rewrite programming may ultimately be compiled or interpreted and executed. In one embodiment, the concrete expression of logic programming may be converted into a concrete expression of rewrite programming. Accordingly, rather than performing complex unification involved with logic programming, the processing of the programming may simply involve matching, which is typically faster from an execution perspective.
This Summary is provided to introduce a selection of concepts in a simplified form that are further described below in the Detailed Description. This Summary is not intended to identify key features or essential features of the claimed subject matter, nor is it intended to be used as an aid in determining the scope of the claimed subject matter.
The appended drawings are used in order to more particularly describe embodiments of the present invention. Understanding that these drawings depict only typical embodiments of the invention and are not therefore to be considered to be limiting of its scope, the embodiments will be described and explained with additional specificity and detail through the use of the accompanying drawings in which:
Embodiments of the present invention extend to the transformation of logic programming into rewrite programming. First, an example computing system in which the principles of the present invention may operate will be described with respect to
Computing systems are now increasingly taking a wide variety of forms. Computing systems may, for example, be handheld devices, appliances, laptop computers, desktop computers, mainframes, distributed computing systems, or even devices that have not conventionally considered a computing system. In this description and in the claims, the term “computing system” is defined broadly as including any device or system (or combination thereof) that includes at least one processor, and a memory capable of having thereon computer-executable instructions that may be executed by the processor. The memory may take any form and may depend on the nature and form of the computing system. A computing system may be distributed over a network environment and may include multiple constituent computing systems.
Referring to
As used herein, the term “module” or “component” can refer to software objects or routines that execute on the computing system. The different components, modules, engines, and services described herein may be implemented as objects or processes that execute on the computing system (e.g., as separate threads). While the system and methods described herein may be implemented in software, implementations in hardware, and in combinations of software and hardware are also possible and contemplated.
In the description that follows, embodiments of the invention are described with reference to acts that are performed by one or more computing systems. If such acts are implemented in software, one or more processors of the associated computing system that performs the act direct the operation of the computing system in response to having executed computer-executable instructions. An example of such an operation involves the manipulation of data. The computer-executable instructions (and the manipulated data) may be stored in the memory 104 of the computing system 100.
Computing system 100 may also contain communication channels 108 that allow the computing system 100 to communicate with other computing systems over, for example, network 110. Communication channels 108 are examples of communications media. Communications media typically embody computer-readable instructions, data structures, program modules, or other data in a modulated data signal such as a carrier wave or other transport mechanism and include any information-delivery media. By way of example, and not limitation, communications media include wired media, such as wired networks and direct-wired connections, and wireless media such as acoustic, radio, infrared, and other wireless media. The term computer-readable media as used herein includes both storage media and communications media.
Embodiments within the scope of the present invention also include computer-readable media for carrying or having computer-executable instructions or data structures stored thereon. Such computer-readable media can be any available media that can be accessed by a general purpose or special purpose computer. By way of example, and not limitation, such computer-readable media can comprise physical storage and/or memory media such as RAM, ROM, EEPROM, CD-ROM or other optical disk storage, magnetic disk storage or other magnetic storage devices, or any other medium which can be used to carry or store desired program code means in the form of computer-executable instructions or data structures and which can be accessed by a general purpose or special purpose computer. When information is transferred or provided over a network or another communications connection (either hardwired, wireless, or a combination of hardwired or wireless) to a computer, the computer properly views the connection as a computer-readable medium. Thus, any such connection is properly termed a computer-readable medium. Combinations of the above should also be included within the scope of computer-readable media.
Computer-executable instructions comprise, for example, instructions and data which cause a general purpose computer, special purpose computer, or special purpose processing device to perform a certain function or group of functions. Although the subject matter has been described in language specific to structural features and/or methodological acts, it is to be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or acts described herein. Rather, the specific features and acts described herein are disclosed as example forms of implementing the claims.
As mentioned above, logic programming permits bound and unbound variables to be included in a clause, where the position of the variables within the clause does not necessarily imply anything about whether the variable is bound or unbound. The logic programming may use a complex logic deduction process called unification to solve for the unbound variables. The following logic programming (with line numbering added for clarity) provides an example of the benefits of logic programming and will be referred to throughout the remainder of this description as the “logic programming example”:
The “Append” proposition of logical programming is an example of the benefits of logical programming and correspondingly the limitations of functional programming, which is a type of rewrite programming. The Append proposition is an example of a reversible proposition, and in this example, takes three arguments. The first three lines define three different modes. If the first two arguments are bound and the last is free, the Append proposition is in the first mode defined in line 1. If the first two arguments are free, and the last is bound, the Append proposition is in the second mode defined in line 2. Finally, if all of the three arguments are bound, the Append proposition is in the third mode defined in line 3. The example includes two Append propositions, one in line 4 and one in line 5.
The Append proposition is used to representing an appending of arguments together into a list. The first two arguments represent the components of the list, and the last argument represents the concatenation of the first two arguments.
If the first two arguments are bound as in mode 1, then the third argument is to be solved for, and will represent a concatenation of the first and second arguments. Since there is only one possible solution for this concatenation, the Append proposition will succeed exactly once (as represented by “1..1” in the mode expression of line 1).
The second mode uses Append in reverse to generate all possible partitions of a list. In this case, the first two arguments are free and the last is bound. Given a list of “n” components (where “n” is the number of indivisible components in the list), there will be n+1 possible divisions assuming that the first or second arguments could represent an empty list while the other of the first or second arguments represents the same list as the third argument.
The third mode uses the Append proposition to test whether the third argument indeed represents a concatenation of the first and second arguments. If it does not, then the proposition fails. Otherwise, the proposition succeeds.
An example of an Append query will now be provided to show how the logic programming above would be evaluated if operating in the first mode defined in line 1. The first example Append query is as follows in Equation 5:
?-Append (cons(1,cons(2,nil)), cons(3,nil),Z) (5)
In this example query, the expression “cons(A,B)” means that A is provided as a prefix for B. Thus, cons(2,nil) is merely 2 since 2 provided as a prefix to nil is simply 2. Furthermore, cons(l,cons(2,nil)) is merely the list (1,2) since 1 provided as a prefix to 2 is simply the list (1,2)
The query would invoke mode 1 of the logic programming logic since the first two arguments of the Append query are bound, and the third argument R is left free. The query will thus try to solve for Z. We can expect one possible result. The unification process tracks the bound and unbound arguments, and thus keeps state indicating that the first two arguments are bound, and the third is unbound.
To solve the query, each of the two Append propositions of the logic programming example are evaluated. The first Append proposition of line 4 does not apply since nil is not the first argument of the Append query. However, the second Append proposition of line 5 does apply since the first argument of the Append proposition and the first argument of the Append query have similar forms, and can be made equal by binding the argument values. The comparison of the values of the first argument (i.e., cons(1,cons(2,nil))) of the Append query and the values of the first argument (i.e., cons(H,T)) of the second Append proposition are used to assign the following bindings:
H1=1
T1=cons(2,nil)
Furthermore, the comparison of the second argument (i.e., cons(3,nil)) of the Append query and the second argument (i.e., Y) of the second Append proposition is used to make the following binding:
Y1=cons(3,nil)
In all of these examples, since recursion will be required, H1 represents the binding of H in the first execution of the Append proposition when the Append proposition is at the highest level of a recursion or perhaps not operating recursively at all. H2 represents the binding of H in the second execution of the Append proposition when operating in a second level of recursion, and so forth. Similar numerical suffices will be used for other variables as well to indicate the level of the recursion in which the binding occurs.
In evaluating the second Append proposition in the first level of recursion, the Append proposition must be evaluated again due to the expression Append(T,Y,Z) at the conclusion of the second proposition.
Calling Append(T,Y,Z) uses the arguments from the first recursion level to internally generate the following Append query:
?-Append(cons(2,nil),cons(3,nil),Z1)
In this second recursion, once again, the first two arguments are bound, and the third argument is free, causing the second recursion to execute in mode 1 defined in line 1 of the logic programming example above.
In this second recursion, the first Append proposition of line 4 is not applicable since the first argument is not nil. Once again, however, the second Append proposition of line 5 does apply since the first argument of the Append proposition and the first argument of the Append query have similar forms. The comparison of the first argument of the Append query (i.e., cons(2,nil)) and the first argument of the second Append proposition (i.e., cons(H,T)) is used to assign make the following bindings in the second recursion of the Append proposition:
H2=2
T2=nil
Furthermore, the comparison of the second argument of the Append query (i.e., cons(3,nil)) and the second argument of the second Append proposition (i.e., Y) is used to make the following binding in the second recursion of the Append proposition:
Y2=cons(3,nil)
The Append proposition must be evaluated again due to the expression Append(T,Y,Z) at the conclusion of the second proposition thereby entering a third recursion of the Append proposition.
Calling Append(T,Y,Z) for entering the third recursion uses the arguments from the second recursion level to internally generate the following Append query:
?-Append(nil,cons(3,nil),Z2)
Here, the Append query matches the Append proposition of line 4, but not the Append proposition of line 5. Also, once again, the Append proposition is evaluated in mode 1 since the first two arguments are bound, and the last argument is free. Accordingly, matching the second argument of the Append query to the Append proposition of line 4, the following binding is made:
Y3=cons(3,nil)
The third unbound argument of the Append proposition of line 4 is the same as the bound argument of the Append proposition of line 4. Therefore, the third recursion of the Append proposition returns the following binding to the second recursion of the append proposition:
Z2=cons(3,nil)
With Z2 being solved for, the unbound variable cons(H,Z) of the second recursion (equivalent to cons(H2,Z2)) may then be solved for and returned to the first recursion of the Append proposition. Specifically, the second recursion of the Append proposition returns the following binding to the first recursion of the append proposition:
Z1=cons(2,cons(3,nil))
With Z1 being solved for, the unbound variable cons(H,Z) of the first recursion (equivalent to cons(H1,Z1)) may then be solved for and returned as the query result. Specifically, the first recursion of the Append proposition returns the following binding to the first recursion of the append proposition:
Z=cons(1, cons(2,cons(3,nil)))
If the first two arguments are free as in mode 2, then the third argument is bound, and the first two arguments are to be solved for. In this case, the solutions for the first two arguments will be the set of possible components that, when concatenated, form the third argument. Since there are n+1 possible solution for this concatenation (wherein “n” is the length of the third argument), the Append proposition will succeed n+1 times (as represented by “1..*” in the mode expression of line 2 in the logic programming example).
An example of an Append query for mode 2 will now be provided as follows in Equation 6:
?-Append (A,B,cons(1,nil)) (6)
To solve the query, each of the two Append propositions of the logic programming example are again evaluated. The first Append proposition of line 4 applies since the third argument of the query may be considered to match that third argument of the first Append proposition. Thus, the following binding is performed:
Y1=cons(1,nil)
The first Append proposition states that the second argument is the same as the third argument, in which case the first argument is simply nil. Thus, evaluation of the first Append proposition against the query of Equation 6 results in the following two bindings as well:
B1=cons(1,nil)
A1=nil
The second Append proposition is then evaluated against the query of Equation 6. The third argument of the query (i.e., cons(1,nil)) matches the third argument of the second Append proposition (cons(H,Z)) by making the following bindings:
H1=1
Z1=nil
The Append(T,Y,Z) proposition represented in the last portion of the Second Append proposition is then called to enter the second recursion of the Append proposition. In this case, the following internal query is made substituting Z1 in for Z:
Append(T1,Y1,nil)
In this second recursion, the first Append proposition of line 4 matches (or can be made to match by making further bindings), but the second Append proposition of line 5 does not. The following bindings are thus made in the second recursion:
Y2=nil
T2=nil
The second recursion then returns, whereupon the value of T2 is assigned as to the value T1, and the value of Y2 is assigned to the value Y1. Accordingly, upon return of the second recursion of the Append proposition, the following bindings are made:
T1=nil
Y1=nil
Furthermore, the following binding had already been made:
H1=1
Z1=nil
From this information, the first two arguments of the first Append proposition may then be determined. The first argument (i.e., cons(H,T)) would be cons(1,nil). The second argument (i.e., Y) would be nil. Upon return of the first recursion of the Append proposition, thereby, the following bindings would be made:
A=cons(1,nil)
B=nil
Thus, the response to the query of Equation 6 would be (cons(1,nil),nil,cons(1,nil)) for the second Append proposition. Thus, the two solutions for the query would be:
(nil,cons(1,nil),cons(1,nil)) (solution 1)
(cons(1,nil),nil,cons(1,nil)) (solution 2)
Now a trace of an example Append query that follows the third mode of line 3 of the logic programming example will be provided. The following query represents an example of an Append query in which all three arguments are bound as in Equation 7A:
Append(cons(1,nil),cons(2,cons(3,nil)),cons(1,cons(2,cons(3,nil)))) (7A)
All three arguments of this Append query are bound. Thus, this Append query will simply test whether or not the third argument indeed represents a concatenation of the first two arguments.
Since the first argument is not nil, the first Append proposition of line 4 in the logic programming example does not apply. However, the second Append proposition of line 5 of the logic programming example does apply. Specifically, the first argument of the second Append proposition (i.e., cons(H,T)) can be made to match the first argument of the Append query of Equation 7A (i.e., cons(1,nil)) by making the following bindings.
H1=1
T1=nil
Furthermore, the second argument of the second Append proposition (i.e., Y) can be made to match the second argument of the Append query of Equation 7A (i.e., cons(2,cons(3,nil)) by making the following binding.
Y1=cons(2,cons(3,nil))
Lastly, the third argument of the second Append proposition (i.e., cons(H,Z) can be made to match the third argument of the Append query of Equation 7A (i.e., cons(1,cons(2,cons(3,nil)))) by making the following binding:
Z1=cons(2,cons(3,nil))
The Append proposition (i.e., Append(T,Y,Z)) is then called with the o m bound values of T1, Y1 and Z1 to enter the second recursion level. Specifically, the following internal Append query is made:
?-Append(nil,cons(2,cons(3,nil)),cons(2,cons(3 ,nil)))
This second recursion of the Append query matches the first Append proposition of line 4, since the first argument is nil, and the second and third arguments are equal. The first Append proposition is thus executed with no inconsistencies, and thus returns empty. The second Append proposition is not executed in the second recursion since the first argument of the Append query (i.e., nil) cannot be made to match the first argument of the second Append proposition (i.e., cons(H,Z)). Since there have been no inconsistencies during execution, the first recursion of the Append query will then return empty which indicates that the query is true. The third argument of the query of Equation 7A does indeed represent a concatenation of the first two arguments.
A trace of a second example Append query that follows the third mode of line 3 of the logic programming example will be provided. The following query represents an example of an Append query in which all three arguments are bound as in Equation 7B:
?-Append(cons(1,nil),nil, cons(2,nil)) (7B)
Since the first argument is not nil, the first Append proposition of line 4 in the logic programming example does not apply. However, the second Append proposition of line 5 of the logic programming example does apply. Specifically, the first argument of the second Append proposition (i.e., cons(H,T)) can be made to match the first argument of the Append query of Equation 7B (i.e., cons(1,nil)) by making the following bindings.
H1=1
T1=nil
Furthermore, the second argument of the second Append proposition (i.e., Y) can be made to match the second argument of the Append query of Equation 7B (i.e., nil) by making the following binding.
Y1=nil
Lastly, the third argument of the second Append proposition (i.e., cons(H,Z) can be made to match the third argument of the Append query of Equation 7B (i.e., cons(2,nil)) by making the following binding:
H1=2
Z1=nil
However, the binding of H1=2 used to match the third arguments is inconsistent with the binding of H1=1 used to match the first arguments. Therefore, the Append query will return false, indicating that the third argument is not a concatenation of the first two arguments.
As can be seen by these example query traces, logic programming involves careful consideration of which arguments are bound and which arguments are not bound. Through a complex process of unification, the logic programming solves for the unbound arguments, where the processing flow may differ considerably depending on which arguments are bound, and which are not. This complex processing often requires functionality that is not offered by some common runtime environments. Furthermore, runtime execution of logic programming is slower due to its complexity.
In accordance with embodiments of the present invention, logic programming is transformed into rewrite programming. Rewrite programming does not involve tracking which arguments are bound or unbound. Accordingly, the execution of the rewrite programming is less complex and faster. Furthermore, the rewrite programming need not rely on external common runtime environments other than that associated with the rewrite programming language.
According to the method 200 of
Conventionally, concrete logic programming is converted into a hierarchical tree where each node in the tree abstractly represents a variable or argument or other component of the logic programming. Branches in the hierarchical tree represent functional and/or structural relationships between the components represented by the node at each end of the branch. The hierarchical tree is an example of the abstract logic expression 302 of
The process 321 of converting the concrete logic expression 301 to the abstract logic expression 302 is often referred to as “parsing” and is a process that is known in the art. Having said this, embodiments of the present invention may not even perform this parsing process 321, but may instead simply access the abstract logic expression 302 to perform the transformation.
Regardless of how the abstract logic expression is accessed, the method 200 transforms (as represented by arrow 322) the abstract logic expression 302 into an abstract expression of rewrite programming 312 (act 202). This transformation will be described in much further detail below with respect to the logic programming example referred to above. This transformation 322 may be automated by programming a computing system to implement the features of the transformation process described below.
Once the abstract rewrite expression 312 is obtained through the transformation process 322, the abstract rewrite expression 312 may be compiled or interpreted (act 203 and arrow 323 in
Thus, operations that are provided by logic programming that may be alternatively provided by rewrite programming. An example of such functionality is Append, which is realized as two Append propositions in the logic programming example provided above. By transforming the logic programming into rewrite programming, the execution of the rewrite programming is much faster.
Referring to
After identifying the modes of operation (act 401), the clauses are broken into groups each with a single mode, arity, and clause name (act 402). This will sometimes require duplication. The append example, may result in three groups matching the three modes, since the arity (i.e., 3 arguments per proposition) is the same for each clause, and clause name “i.e., Append” is the same for each clause. As expressed in concrete syntax, the three groups for the logic programming example may as follows:
Following this grouping operation (act 402), the transformation program may generate a rule group (act 403) for each of the groups generated by the grouping operation. In the logic programming example, three rule groups would be generated. After sorting, each clause group has a single mode which stipulates a directed transformation of ground terms from the inputs (+) to the outputs (+). This meets the definition of a term rewrite system.
The intermediate phases of compilation use an abstraction that is similar to a functional programming language (which is typically limited to only possible normal form) to allow for zero or many normal forms. The pseudo code below is a convenient representation of the intermediate form. The code is intended to look something like functional code (specifically ML). The main difference is the use of “relation” and “+” rather than “function” and “|” to highlight the possibility of zero or many normal forms.
An example of rules generated during this rule generation process are provided below in XML format:
After rule generation (act 403), a relation is created for each group (act 404). For clarity the name is the clause's head's predicate name appended with the mode. Since “+” and “−” are not allowed in names in XML or most programming languages “i” and “o” are used in the pseudo code. Thus, under this naming syntax, the Append proposition operating in mode 2 would be called the Append.iio relation upon conversion to a rewrite system. The Append proposition operating in mode 2 would be called the Append.ooi relation upon conversion to rewrite. The Append proposition operating in mode 3 would be called Append.iii upon conversion to rewrite.
The input arguments are placed in a tuple on the left hand side of the relation. The output arguments are the return value on the right hand side. For a clause with no antecedents, this is all that is performed. If there are antecedents, these antecedents are translated as nested lets and the return value is the scope of the innermost let. Each antecedent is a predicate without a mode specification. An appropriate mode is determined based on the variables that are bound at its point of use.
In the second Append clause of the logic programming example, there is a recursive call as is apparent from the tracings set forth in detail above. Consider mode 1 (iio). The variables H, T, and Y are bound in the head and Z is unbound. T is used in the first position of the antecedent so this position is an input variable in the recursive call. Likewise Y in the second position is bound therefore an input. The third position uses Z. Since Z is unbound, the third position is an output. This results in a mode of iio, exactly the same as the head (which is not always the case for all multi-modal recursive calls). This sets up the call for the let. The input variables are passed as actuals. The return is the binding in the let. The scope is the next antecedent or the return value as appropriate.
Upon completion of this act, the concrete expression of the relations in the Append example would appear as follows:
This pseudo code is as it might be printed directly from the compiler's intermediate structures. It shows some flaws that will be corrected for later in the process. As an example, the iii mode of append has a useless let. The action of the second alternative says let append.iii( . . . ) equal nothing in nothing, so the let can be optimized away. The recursive call cannot be eliminated because success or failure of the containing relation.
Upon creation of the initial version of the rewrite relation using act 404, the let function is eliminated (act 405) since “let” is not supported in a rewrite system. In a functional language (recall that a functional language is a type of rewrite language), a let can be eliminated by creating a new function. The scope of the let is the body of the new function. The bindings are established by using the variables as formal arguments and using the call as an actual argument.
In the rewrite system, lets can be eliminated in a similar way. A new relation is created for every let. There are no nested relations, so a top level relation with a new name is created and a call is left in its place. So long as the name of the new relation is unique, the precise name does not matter. For readability, the generated relations are named from the relation name, clause index, and let index. For example, new relation generated for the first let, in the second, clause of the append.iio relation is append.iio.2.1.
The following represents relations generated by this process after let elimiation
This is sufficient to eliminate the lets, but it has created a new problem, which are solved in this transformation process. The generated relations will frequently contain free variables. In the example above, “H” is free in the new relation.
Before the new relation is separated representing the completion of the transformation from a logic programming to rewrite programming, all free variables are bound though a process called lambda lifting (act 406). Lambda lifting proceeds bottom up by adding a formal parameter for every free variable in every function and adding an actual parameter at every call sight.
The following are the relations produced by the compiler. The generated functions are at the end of the list. Note that they ignore some or all of their arguments.
In addition to the programming itself, logic queries are transformed into calls of the rewrite system prior to being evaluated by the transformed rewrite relations. Queries are thus translated into calls of the rewrite system. Since an antecedent is a query, the transformation of the query may be essentially the same as described for the antecedents.
Consider the following logic query:
?-append([1], [2,3], X).
The first two arguments are bound, so it will be translated into the call:
append.iio([1], [2,3])
and the return value will be bound to X.
Likewise:
?-append(X,Y,[1,2,3]).
will be translated into
append.ooi([1,2,3])
which will return the four pairs that could be appended to form [1,2,3] as follows:
The first term of each pair will successively be bound to X and the second to Y.
The query:
?-append(X,[2,3],[1,2,3]).
is translated into the call:
append.oii([2,3],[1,2,3])
Since there was no oil mode specified as in the logic programming example above, this query will produce no results. When the oii mode is specified, the result is:
The execution uses an innermost reduction strategy which corresponds to DFS in logic programming or call by value in programming language semantics.
Return values which contain relation symbols are not constructors and therefore not considered constructor normal forms. These are eliminated in a post processing phase.
The mode system cannot answer queries with arguments that are not either bound or free. For example, the following cannot be answered because the first argument is partially bound.
?-append([1|X],[4,5],[1,2,3,4,5]).
However, this is not a severe limitation. The query above could be represented as:
?-append([1],Y,[1,2,3,4,5]), append(Y,X,[4,5]).
Accordingly, the principles of the present invention provide a mechanism for automatically transforming logic programming into rewrite programming, allowing for the advantages of rewrite programming (such as speed of execution) while avoiding much of the complexities of logic programming.
The present invention may be embodied in other specific forms without departing from its spirit or essential characteristics. The described embodiments are to be considered in all respects only as illustrative and not restrictive. The scope of the invention is, therefore, indicated by the appended claims rather than by the foregoing description. All changes which come within the meaning and range of equivalency of the claims are to be embraced within their scope.