rulesLib Overview

 

Overview

The rulesLib is a global Lambda providing a tiny rule-based forward production engine useful in small rule-based programming applications. The rulesLib is designed to be inserted within larger parent Lambdas for small rule-based machine learning tasks. A copy of the rulesLib is inserted into the math Lambda. A copy of the rulesLib is inserted into the javaScript compiler Lambda. Compiling the javaScript compiler automatically creates the rulesLib. The rulesLib is an important element of the Analytic Information Server Lambda library.

The rulesLib is needed by the JavaScript compiler to identify patterns during the code optimization phase of compilation. The rulesLib is needed by the math Lambda to perform symbolic math manipulations. The AIS programmer may also use the rulesLib in applications where a small set of complex list modifying rules are required. The rulesLib is not meant to be applied in applications with a large rule base.

The rulesLib creates, maintains, and applies a Dictionary of IF -> THEN list substitution rules for transforming a list and all its sub lists into a new list. Anywhere a sub list matches one of the IF rules, the THEN rule is substituted in place of the original sub list. The Lambda supports wild card substitution rules, indefinite arity pattern matching, forward chaining, user defined recognition functions, user defined substitution functions, and single or multiple pass substitution rule application.

The rulesLib can be used for semantic analysis, algebraic reduction, parse tree optimization, theorem proving, and a host of other applications. The Lambda operates with its rule base stored in RAM memory; and, is therefore, not useful for problems requiring huge rule bases.

Rule Format

The rulesLib supports a simple rule Language of IF -> THEN list substitution rules for transforming a list and all its sub lists into a new list. The IF - > THEN rules are all entered via the assert function. This Lambda accepts and saves a set of transformation rules in its internal rules dictionary. Each rule is entered, by the programmer, in two parts -- an IF form followed by a THEN form.

 

An example might be:

(rulesLib.assert '(x + y)  '(addi x y))

We may then apply the above rules against an input list as follows:

(rulesLib.apply  '(x + y)) ...Returns...  '(addi x y)

Constant Rules

The simplest rule form is the constant pattern. Both the IF and the THEN forms are constant patterns, containing no wild cards, or other special characters.
An example might be:

(rulesLib.assert '(x + y)  '(Add x to y))

We may then apply the above rules against a list as follows:

(rulesLib.apply '(x + y)) ...Returns...  '(Add x to y)

Whenever an exact match occurs with the IF pattern, the THEN pattern is substituted in the output list.

Wild Cards

This Lambda supports wild card variables to make rule definitions more flexible for the programmer. An example of wild card rule variables is as follows:

(rulesLib.assert '($X + $Y)  '(addi $X $Y))

We may then apply the above rules against a list as follows:

(rulesLib.apply '(m + 10)) ...Returns...  '(addi m 10)

The rules and wild card variables operate on sub lists as well as the whole list as follows:

(rulesLib.apply '((m + 10) + 20)) ...Returns...  '(addi (addi m 10) 20)

Lambda Rules

This Lambda supports named lambda rule definitions which allow more flexible actions to be taken by the programmer during the recognition phase and also during the production phase. Some examples of named lambda rule definitions is as follows:

(rulesLib.assert $FOLD:(lambda(op x y) vars:(f) (setq f (getGlobalValue (symbol op))) (f x y)))
(rulesLib.assert $NUM:(lambda(x) (if (isNumber x) x)))
(rulesLib.assert $OP:(lambda(x) vars:((d #{+ addi - subi * muli / divi})) (if (isMember x d) d[x])))
(rulesLib.assert '(<$X=$NUM> <$Y=$OP> <$Z=$NUM>) '(<$FOLD> $Y $X $Z))
(rulesLib.assert '($X <$Y=$OP> $Z) '($Y $X $Z))

We may then apply the above rules against a list as follows:

(rulesLib.apply '(m + 10)) ...Returns... '(addi m 10)
(rulesLib.apply '(5 + 10)) ...Returns... 15

Append Output Rules

Using the built in <$> function in a THEN rule causes the appendList function to be applied to the result as follows:

(rulesLib.assert '($X + $Y) '(<$> + $X $Y))

We may then apply the above rules against a list as follows:

(rulesLib.apply '((5 6 7) + (10 20))) ...Returns... '(+ 5 6 7 10 20)

Rule Precedence

This Lambda supports multiple rule definitions up to the limits of available memory. Rules asserted first have precedence over rules asserted later as follows:

(rulesLib.assert $OP:(lambda(x)
		
		    vars:((d #{+ addi - subi * muli / divi}))
		
		    (if (isMember x d) d[x])))
(rulesLib.assert '($X <$Y=$OP> $Z) '($Y $X $Z))
(rulesLib.assert '(addi $X $X) '(muli $X 2))
(rulesLib.assert '(addi $X 0) '$X)

We may then apply the above rules against a list as follows:

(rulesLib.apply '(m + m)) ...Returns... '(addi m m)

Single Pass Mode

This Lambda supports single pass mode, during rule application, as follows:

(setq rulesLib.singlePass true)
(rulesLib.assert '($X + $Y) '(+ $X  $Y))
(rulesLib.assert '(+ $X $X) '(* $X  2))

We may then apply the above rules against a list as follows:

(rulesLib.apply '(m + m)) ...Returns... '(+ m m)

The second rule, converting (+ m m) into (* 2 m), was never fired, because the rulesLib was set to single pass mode.

Asterisk Wild Card Rules

This Lambda supports asterisk wild card rules anywhere within lists, as follows:

(rulesLib.assert '(min ($X*)) '(<$FN> min $X))
(rulesLib.assert '($X* min)   '(<$FN> min $X))
(rulesLib.assert $FN:(lambda(fn x) (append (list fn) x)))

We may then apply the above rules against a list as follows:

(rulesLib.apply  '(min (2 3))) ...Returns... '(min 2 3)
(rulesLib.apply  '((2 3 4) min)) ...Returns... '(min 2 3 4)

Append List Function

The builtin appendList function allows multiple arguments to be append together to form a list. The appendList function is builtin and may be used in any IF->THEN rule by double quoting the THEN form as follows:

(rulesLib.assert '(min ($X $Y)) ''(min $X $Y))

We may then apply the above rules against a list as follows:

(rulesLib.apply  '(min (2 3) (4 5 6))) ...Returns... '(min 2 3 4 5 6)