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 apply function applies the IF -> THEN rules in the Lambda's rule base to the specified input list. The result of apply is a new list with all of the IF patterns substituted with THEN patterns.
Function
(rulesLib.apply aList)
Arguments | Explanation |
---|---|
aList | An input list to be modified according to the IF -> THEN rules asserted in the rulesLib's rule base. |
RETURN | The output list after modification by the IF -> THEN rules asserted in the rulesLib's rule base. |
The apply function performs the actual forward chaining pattern substitution provided by the rulesLib. Use the apply function when all rules have been asserted, and an input list is ready for pattern substitution.
An input list will contain algebraic expressions. By asserting a few simple rules, the rulesLib can perform symbolic algebra, reducing the input list to its simplest algebraic form as follows:
(setq x (new rulesLib)) |
|
(x.assert '($X + 0) '$X) |
;; [0] Adding zero does nothing. |
(x.assert '(0 + $X) '$X) |
;; [1] Adding zero does nothing. |
(x.assert '($X - 0) '$X) |
;; [2] Subtracting zero does nothing. |
(x.assert '(0 - $X) '$X) |
;; [3] Subtracting zero does nothing. |
(x.assert '($X * 0) 0) |
;; [4] Multiplying by zero destroys. |
(x.assert '(0 * $X) 0) |
;; [5] Multiplying by zero destroys. |
(x.assert '($X / 1) '$X) |
;; [6] Dividing by one does nothing. |
(x.assert '($X / $X) 1) |
;; [7] Dividing by itself leaves one. |
(x.assert '($X - $X) 0) |
;; [8] Subtracting itself leaves nothing. |
(x.apply '(x - (x + 0))) |
;; Returns 0 |
(x.apply '((x + y) / (x + y))) |
;; Returns 1 |
(x.apply '(x - (y + 0))) |
;; Returns '(x - y) |
The apply function performs its substitutions using forward rule chaining:
(x.apply '(x - (x + 0)))
First, fires rule [0], the Adding Zero rule, to produce: '(x - x)
Next, fires rule [8], the Subtracting Itself rule, to produce: 0
Hint: Setting the rulesLib to verbose mode, will cause a print out of each rule firing as it happens (see the set function for verbose).
The assert function enters IF -> THEN rules into the Lambda's rule base. Rules defined first have precedence over rules defined later. The result of assert is a new IF -> THEN rule has been added to the Lambda's rule base.
Function
(rulesLib.assert IFform THENform)
(rulesLib.assert $FUNCName:LAMBDAform)
Arguments | Explanation |
---|---|
IFform | The IF pattern of the rule being defined. A more detailed description of IF rule formats is described in the Overview section. |
THENform | The THEN pattern of the rule being defined. A more detailed description of THEN rule formats is described in the Overview section. |
RETURN | true or an error message |
Arguments | Explanation |
---|---|
$FUNCName | The $ function name of the user function being defined. A more detailed description of $ function name formats is described in the Overview section. |
LAMBDAform | The Lambda value of the user function being defined. A more detailed description of lambda value formats is described in the Overview section. |
RETURN | true or an error message |
The assert function performs the actual rule definition to the rulesLib. Use the assert function to define all rules before using the apply function to perform pattern substitution.
A simple English-like database query language is developed. By asserting a number of simple rules, the rulesLib can perform natural language database update and query as follows:
(setq x (new rulesLib))
;; Define simple database structures.
(define Child (new Dictionary:)) ;; Create Child database
(define Parent (new Dictionary:)) ;; Create Parent database
(define Gender (new Dictionary:)) ;; Create Gender database
;; Define database update and query functions.
(x.assert $ADDMOM:(lambda(mom child)
(if (isVoid Child[mom])
(setq Child[mom] (new Vector: 1 child))
(setq Child[mom][(length Child[mom])] child))
(if (isVoid Parent[mom])
(setq Parent[mom] (new Vector: 1 child))
(setq Parent[mom][(length Parent[mom])] child))
(setq Gender[mom] 'female)
'ok))
(x.assert $ADDDAD:(lambda(dad child)
(if (isVoid Child[dad])
(setq Child[dad] (new Vector: 1 child))
(setq Child[dad][(length Child[dad])] child))
(if (isVoid Parent[dad])
(setq Parent[dad] (new Vector: 1 child))
(setq Parent[dad][(length Parent[dad])] child))
(setq Gender[dad] 'male)
'ok))
(x.assert $ASKMOM:(lambda(mom child)
(and (isMember child Child[mom])
(= Gender[mom] 'female))))
(x.assert $ASKDAD:(lambda(dad child)
(and (isMember child Child[dad])
(= Gender[dad] 'male))))
(x.assert $GETPARENTS:(lambda(child) Parent[child]))
(x.assert $GETCHILDREN:(lambda(parent) Child[parent]))
(x.assert $ASKGENDER:(lambda(person gender) (= Gender[person] gender)))
(x.assert $IN:lambda(x y) (isMember x y)))
;; Define database Boolean logic rules.
(x.assert '(true and true) true) ;; Logical and rule
(x.assert '($X and false) false) ;; Logical and rule
(x.assert '(false and $X) false) ;; Logical and rule
(x.assert '(false or false) false) ;; Logical or rule
(x.assert '($X or true) true) ;; Logical or rule
(x.assert '(true or $X) true) ;; Logical or rule
(x.assert '(Is $X one of $Y ?) '(<$IN> $X $Y)) ;; Logical meber of a set rule
;; Define database natural language rules.
(x.assert '($M is the mother of $C) '(<$ADDMOM> $M $C)) ;; Add mothers to database
(x.assert '($D is the father of $C) '(<$ADDDAD> $D $C)) ;; Add fathers to database
(x.assert '(Is $D the father of $C ?) '(<$ASKDAD> $D $C)) ;; Query Parents database
(x.assert '(Is $M the mother of $C ?) '(<$ASKMOM> $M $C)) ;; Query Parents database
(x.assert '(Who are the parents of $C ?) '(<$GETPARENTS> $C))
(x.assert '(Who are the children of $P ?) '(<$GETCHILDREN> $P))
(x.assert '(Is $P $X ?) '(<$ASKGENDER> $P $X))
;; Use forward rule chaining to define complex logical relationships
(x.assert '(Is $G the grandmother of $C ?)
'(($G is one of (Who are the parents of $C)) and (Is $G female ?)))
;; Start updating and questioning the database.
(x.apply '(Mary is the mother of John)) Returns ok
(x.apply '(Sally is the mother of Bill)) Returns ok
(x.apply '(John is the father of Jane)) Returns ok
(x.apply '(Jack is the father of John)) Returns ok
(x.apply '(Is Sally female ?)) Returns true
(x.apply '(Is Mary the grandmother of Jane ?)) Returns true
(x.apply '(Who are the parents of John ?)) Returns #(Jack Mary)
The English-like imperatives update the database, and the interrogatives query the database. The assert function for the Grandmother rule assumes the use of forward rule chaining. The THEN side of the rule is substituted into the list, and forward rule chaining will reduce the resulting complex expression to true if and only if the original query is true.
The clear function resets the Lambda. The previously asserted rules are cleared, and the Lambda is made ready for new rule assertions.
Function
(clear rulesLib)
Arguments | Explanation |
---|---|
rulesLib | The rulesLib which is to be cleared. |
RETURN | true |
Use the clear function when the Lambda is to be reset, and a new set of rules are to be asserted.
Clearing the Lambda resets all of the rules. The Lambda is then ready for new rules to be asserted. In this example, the second apply returns the original input list because all the rules have been erased with the clear function.
(setq x (new rulesLib))
(x.assert '($X + 0) '$X) ;; [0] Adding zero does nothing.
(x.assert '(0 + $X) '$X) ;; [1] Adding zero does nothing.
(x.assert '($X - $X) 0) ;; [2] Subtracting itself leaves nothing.
(x.apply '(x - (x + 0))) Returns 0
(clear x) Returns true
(x.apply '(x - (x + 0))) Returns '(x - (x + 0))
The clear function resets the current rule base, and allows a new set of rules are to be asserted.
The length function returns the number of rules currently asserted to the Lambda.
Function
(length rulesLib)
Arguments | Explanation |
---|---|
rulesLib | The rulesLib whose length is to be determined. |
RETURN | The number of rules currently in the Lambda's rule base. |
Use the length function to determine the number of rules currently asserted to the Lambda.
The length function, shown here, returns the number of rules currently in the Lambda's rule base (see the assert function).
(setq x (new rulesLib))
(x.assert '($X + 0) '$X) ;; [0] Adding zero does nothing.
(x.assert '(0 + $X) '$X) ;; [1] Adding zero does nothing.
(x.assert '($X - $X) 0) ;; [2] Subtracting itself leaves nothing.
(length x) Returns 3
The length function returns the number of rules currently asserted to the Lambda.
The new function creates a new rulesLib from an existing rulesLib. The newly created rulesLib inherits all of the properties of the original Lambda, is a new individual, and is completely separate from the original Lambda. Rules asserted to the new Lambda do not effect the original Lambda and vice versa.
Function
(new rulesLib)
Arguments | Explanation |
---|---|
rulesLib | The original rulesLib to be copied. |
RETURN | A new individual rulesLib with its own identity. |
Use the new function when a rulesLib is to be created. New creates a genetic copy of the original Lambda, with its own separate existence and its own separate identity. New is usually the first function used in employing a rulesLib.
Creating a new individual rulesLib is usually the first step in employing a rulesLib to solve a problem. Once created, the new rulesLib may have its own rules asserted, and may be applied to solving pattern matching problems.
(setq x (new rulesLib))
(x.assert '($X + 0) '$X) ;; [0] Adding zero does nothing.
(x.assert '(0 + $X) '$X) ;; [1] Adding zero does nothing.
(x.assert '($X - $X) 0) ;; [2] Subtracting itself leaves nothing.
(x.apply '(x - (x + 0))) Returns 0
The new function creates a new rulesLib. New creates a genetic copy of the original Lambda, with its own separate existence and its own separate identity. New is usually the first function used in employing a rulesLib. Each new rulesLib can hold a distinct rule base. Multiple ruleLambdas can be used to manage separate rule bases, such as one rulesLib for precedence rules and a second rulesLib for normal arithmetic rules.
The ref function allows access to the Lambda's internal structures and memory. The internal structures, which can be accessed, include all of the member functions listed in this chapter, plus a number of important option switches such as: failureValue; maxPasses; singlePass; and verbose. Any IF -> THEN rule in the Lambda's memory can be accessed with an integer key. The rule is returned as a List containing the IFform followed by the THENform. Finally, any rule in the Lambda's memory can be accessed using its IFform as a key.
Function
(ref rulesLib memberName) also rulesLib.memberName
(ref rulesLib integer) also rulesLib[integer]
(ref rulesLib IFform) also rulesLib[IFform]
Arguments | Explanation |
---|---|
rulesLib | The rulesLib whose internals are to be accessed. |
memberName | The name of a member in the Lambda's internal structure. These include the member functions listed in this chapter including: apply, assert; and the Lambda's internal option switches including: failureValue, maxPasses, singlePass, and verbose. |
RETURN | The value of the specified internal structure. For member functions, such as apply and assert, the internal member function is returned for invocation. For failureValue, the essoteric symbol used to indicate rule failure is returned. For maxPasses, the integer value of the maximum allowed number of passes in multiple pass mode is returned. For singlePass, true indicates single pass mode is on and false otherwise. For verbose, true indicates verbose mode is on and false otherwise. |
Arguments | Explanation |
---|---|
rulesLib | The rulesLib whose internals are to be accessed. |
integer | The nth rule, in the Lambda's memory, is accessed. The integer must be zero or greater and less then the length of the Lambda (see the length function). |
RETURN | The previously asserted rule is returned as a List containing first the IFform followed by the THENform. |
Arguments | Explanation |
---|---|
rulesLib | The rulesLib whose internals are to be accessed. |
IFform | The IFform of a previously asserted rule in the Lambda's memory. |
RETURN | The THENform of a previously asserted rule, in the Lambda's memory, is returned. |
Use the ref function to access the Lambda's internal structures and memory.
The dot syntactical form of the ref function is used here to assert and apply rules to the Lambda. Since the member function references are at the head of a List, as soon as they are returned Lisp invokes them. Next the 2nd rule is returned, using the indexed access feature of the ref function. Finally, the THENform of the 1st rule is returned using its IFform as a key.
(setq x (new rulesLib))
(x.assert '($X + 0) '$X) ;; [0] Adding zero does nothing.
(x.assert '(0 + $X) '$X) ;; [1] Adding zero does nothing.
(x.assert '($X - $X) 0) ;; [2] Subtracting itself leaves nothing.
(x.apply '(x - (x + 0))) Returns 0
x[2] Returns '(($X - $X) 0)
x['(0 + $X)] Returns '$X
also
((ref x 'apply) '(x - (x + 0))) Returns 0
(ref x 2) Returns '(($X - $X) 0)
(ref x '(0 + $X)) Returns '$X
An often used syntactical shorthand ref function are the . (dot) operator and the [] operator.
(x.apply '(x - (x + 0))) is the same as ((ref x 'apply) '(x - (x + 0)))
x[2] is the same as (ref x 2)
x['(0 + $X)] is the same as (ref x '(0 + $X))
The set function allows updates to the Lambda's internal structures and memory. The internal structures, which can be updated, include a number of important option switches such as: failureValue; maxPasses; singlePass; and verbose.
Function
(set rulesLib memberName newValue)
(setq rulesLib.memberName newValue)
(setq rulesLib[memberName:] newValue)
Arguments | Explanation |
---|---|
rulesLib | The Lambda whose internal memory is to be updated. |
memberName | The name of a member in the Lambda's internal structure. These include the Lambda's internal option switches: failureValue, maxPasses, singlePass, and verbose. |
newValue | The new setting for the Lambda's internal structure. |
RETURN | The new value of the specified internal structure. |
Use the set function to update the Lambda's internal option switches.
The dot syntactical form of the set function is used here to assert and apply rules to the Lambda. Since the member function references are at the head of a List, as soon as they are returned Lisp invokes them.
(setq x (new rulesLib))
(x.assert '($X + 0) 0)
(x.assert '($X - $X) 0)
;; Multiple pass mode forward chains until no further substitution is possible
(x.apply '(m - (m + 0))) Returns 0
;; Single pass mode halts after the first substitution
(setq x.singlePass true)
(x.apply '(m - (m + 0))) Returns '(m - m)
An often used syntactical shorthand set function are the . (dot) operator and the [] operator.
(setq x.verbose true) is the same as (set (ref x 'verbose) true)
(setq x[verbose:] false) is the same as (set (ref x 'verbose) false)
Note: See the section on the Lambda Communication Language (ACL) for this Lambda to understand the meaning of the internal option switches: failureValue, maxPasses, singlePass, and verbose.
The unassert function deletes IF -> THEN rules from the Lambda's rule base. The result of unassert is a previously asserted IF -> THEN rule is deleted from the Lambda's memory.
Function
(rulesLib.unassert IFform)
Arguments | Explanation |
---|---|
IFform | The IF pattern of the rule being deleted. A more detailed description of IF rule formats is described in the Overview section. |
RETURN | true or an error message |
Use the unassert function to delete a previously defined rule from the ruleLambda's rule base.
This simple example demonstrates the effect of deleting a rule from the Lambda's memory.
(setq x (new rulesLib))
(x.assert '($X + 0) 0)
(x.apply '(m - (m + 0))) Returns '(m - m)
(x.assert '($X - $X) 0)
(x.apply '(m - (m + 0))) Returns 0
(x.unassert '($X - $X))
(x.apply '(m - (m + 0))) Returns '(m - m)
The unassert function performs rule deletion from the rulesLib. Use the unassert function to delete a previously defined rule from the Lambda's memory.