Aspen SCM‎ > ‎Expert System‎ > ‎1. Elements‎ > ‎

### 1.1 Aspen SCM Expert System as a Declarative Language

#### Nature and Purpose of Aspen SCM Expert System

Aspen SCM’s Expert System is used as its programming language. Along with macros (which are similar to the macro languages for operating systems) it forms the glue which is used to create complete applications. It is an interpreted language but is semi-compiled at load time and so executes much more efficiently than macros.

Unlike conventional procedural programming languages, the Aspen SCM Expert System is declarative and is modelled on expert system languages such as Prolog. Thus the basic unit of a program is a rule rather than a subroutine or a function. A rule consists of a series of logical conditions which take the form IF <condition_1> {AND|OR} <condition_2> {AND|OR} … THEN <conclusion>.

#### Example Rule: Adding Two Numbers Together

Here is a sample rule:

`IF      ?X = 1`
`AND     ?Y = 2`
`AND     ?Z = ?X + ?Y`
`THEN    SUM_OF ?X ?Y ?Z`

Apart from the syntax, this doesn’t appear to be doing anything different to what could be done in a procedural language. It sets local variables ?X to 1 and ?Y to 2, calculates ?Z as the sum of 1 + 2 and then concludes “SUM_OF 1 2 3”.

Aspen SCM Expert System rules are connected together by their conclusions, which act as procedure calls. The above rule is unrealistic in setting ?X and ?Y in the first two lines. A procedure to add two numbers together would be written as:

`IF      ?Z = ?X + ?Y`
`THEN    SUM_OF ?X ?Y ?Z`

where ?X and ?Y are input arguments and ?Z is an output argument. It would be called by a statement such as

`        SUM_OF 1 2 ?Z`

When called in this way it would resolve true as SUM_OF 1 2 3.

#### Goldbach's Conjecture

Goldbach’s conjecture is that every even number greater than 4 can be expressed as the sum of two odd primes.

Now consider this rule:

`IF      ?X = 3 OR ?X = 5 OR ?X = 7 OR ?X = 11 OR ?X = 13`
`AND     ?Y = 3 OR ?Y = 5 OR ?Y = 7 OR ?Y = 11 OR ?Y = 13`
`AND     ?A = ?X + ?Y`
`AND     ?A EQ ?Z`
`THEN    SUM_OF ?X ?Y ?Z`

This is designed to be called by a statement such as SUM_OF ?X ?Y 12. It finds primes ?X and ?Y such that ?X + ?Y equals the specified value ?Z, in this case 12. The first two statements declare possible values for ?X and ?Y. The statement ?A = ?X + ?Y then calculates their sum. The next statement ?A EQ ?Z tests whether the result equals the target ?Z and, if it does, proves the conclusion with the values of ?X and ?Y which sum to the result.

If you assert the statement SUM_OF ?X ?Y 12, Aspen SCM Expert System resolves it as true with the result SUM_OF 5 7 12. It does this without your needing to write any code to iterate through the potential solutions. You have made the declarations of the possible values of ?X and ?Y and it finds the solution, if one exists.

#### Backtracking through a Search Tree

Aspen SCM Expert System finds the primes which sum to 12 by a process known as backtracking. It starts by trying the first branch in each statement (e.g. ?X = 3, ?Y = 3) and proceeds forwards towards the conclusion. If it finds that it cannot go any further towards proving its conclusion, it goes back and tries other branches which it has not previously considered. It does this in a consistent way until it has tried all possible branches (in this case all possible combinations of ?X and ?Y). If none yields a route to proving the conclusion, it declares the conclusion to be false. It is because of this behaviour that the language is said to be declarative.

In practice, the search tree is usually so large that in order to exploit the inherent searching capabilities of Aspen SCM Expert System, you would need to ensure that it was searched efficiently. This requires an intimate knowledge of how Aspen SCM backtracks and very careful coding. It is usually easier to think procedurally and use the Aspen SCM Expert System to implement procedural code.

Despite its declarative nature, most of the code which is implemented using Aspen SCM Expert System is procedural. In order to do this successfully a particular programming style has to be adopted which guards against backtracking. Backtracking itself is discussed in section 2.7. Ways of writing code to prevent backtracking are set out in section 3.1.