Compiling the javaScript Lambda automatically creates the javaScript runtime environment. The javaScript runtime environment consists of a number of global Lambdas, functions, string and text class methods.
The JavaScript runtime environment contains a number of string and text Class Methods which are useful in JavaScript programming. Compiling the javaScript compiler automatically creates these string and text class methods.
The Math Lambda is a global Lambda containing a number of mathematical constants and functions useful in JavaScript programming. Compiling the javaScript compiler automatically creates the Math Lambda. The Math Lambda is an important element of the JavaScript runtime environment.
The rulesLib is a global Lambda providing a tiny rule-based forward production engine useful in JavaScript programming. Compiling the javaScript compiler automatically creates the rulesLib. The rulesLib is an important element of the JavaScript runtime environment.
The JavaScript runtime environment contains a number of string and text Class Methods which are useful in JavaScript programming. Compiling the javaScript compiler automatically creates these string and text class methods.
String:charAt(self n) | Function | Returns the character at index n. |
---|---|---|
String:indexOf(self text) | Function | Returns the index of the first occurrence of text in self. |
String:lastIndexOf(self text) | Function | Returns the index of the last occurrence of text in self. |
String:length(self) | Function | Return the number of characters in the string. |
String:split(self text) | Function | Returns an array of substrings all split at text. |
String:substring(self n m) | Function | Returns substring from character n to one before character m. |
String:toLowerCase(self) | Function | Returns string in lower case. |
String:toUpperCase(self) | Function | Returns string in upper case. |
The Math Lambda is a global Lambda containing a number of mathematical constants and functions useful in JavaScript programming. Compiling the javaScript compiler automatically creates the Math Lambda. The Math Lambda is an important element of the JavaScript runtime environment.
Math | Lambda | JavaScript runtime Math constants and functions |
---|---|---|
Math.E | Constant | Base of natural logarithms (2.718...) |
Math.LN2 | Constant | Natural logarithm of 2 (0.693...) |
Math.LN10 | Constant | Natural logarithm of 10 (2.302...) |
Math.LOG2E | Constant | Base 2 logarithm of e (1.442...) |
Math.LOG10E | Constant | Base 10 logarithm of e (0.434...) |
Math.PI | Constant | Ratio of circumference to diameter (3.141...) |
Math.SQRT1_2 | Constant | Square root of one half (0.707...) |
Math.SQRT2 | Constant | Square root of two (1.414...) |
Math.exp(x) | Function | Returns E raised to the power of a single argument. |
Math.floor(x) | Function | Rounds down to next integer for one argument. |
Math.log(x) | Function | Returns the natural logarithm for one argument. |
Math.max(x,y) | Function | Returns the maximum of two arguments. |
Math.min(x,y) | Function | Returns the minimum of two arguments. |
Math.pow(x,p) | Function | Returns x raised to the power of p. |
Math.random(x) | Function | Returns a random number between zero and one. |
Math.round(x) | Function | Rounds to the closest integer. |
Math.sin(x) | Function | Returns sine of a single argument. |
Math.sqrt(x) | Function | Returns square root of a single argument. |
Math.tan(x) | Function | Returns tangent of a single argument. |
The rulesLib is a global Lambda providing a tiny rule-based forward production engine useful in JavaScript programming. Compiling the javaScript compiler automatically creates the rulesLib. The rulesLib is an important element of the JavaScript runtime environment.
The rulesLib is needed by the JavaScript compiler to identify patterns during the code optimization phase of compilation. The JavaScript 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.
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)
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.
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)
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
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)
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)
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.
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)
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)
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.
Type: Function
Syntax
(rulesLib.apply aList)
Arguments
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. |
When To Use
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.
Example
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) |
Notes & Hints
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.