стр. 58 |

string + "Compiling program SUM.FPS";

message "<string>";

:--------------------------------------------------------------------------------------------------

:DECLARATIONS

declare Number

num flt;

declare Sum

total flt;

:--------------------------------------------------------------------------------------------------

:RULES

:block 0 - accumulates sum recursively in many sequen-

tial steps

:rule r0

rule (goal Accumulates sum recursively in many sequen-

tial steps)

IF ( in Number num <N> )

( in Sum total <T> )

THEN

delete 1,

message "adding <N> to <T> getting \(<N> + <T>\)",

in 2 total (<T> + <N>);

:rule r1

rule rconf 0 (goal Prints out total when no more

instances of r0 are fireable)

IF ( in Sum total <T> )

THEN

message "total of all is <T>\n",

clear backtrack,

halt;

:--------------------------------------------------------------------------------------------------

:MAKES

make Number num 12.34 ;

make Number num 23.45 ;

make Number num 34.56 ;

make Number num 45.67 ;

make Number num 56.78 ;

make Number num 67.89 ;

make Number num 78.90 ;

make Number num 89.01 ;

make Number num 90.12 ;

make Number num 01.23 ;

TEAM LinG - Live, Informative, Non-cost and Genuine !

166 SIMPLE EXAMPLE PROGRAMS

make Number num 32.10 ;

make Number num 21.09 ;

make Number num 32.10 ;

make Number num 43.21 ;

make Number num 54.32 ;

make Number num 65.43 ;

make Sum total 0 ;

message "Ready to run sum.fps";

run;

message "Sum.fps finished";

:******************************************************

There are only two memory elements: Number, instances of which will hold the

numbers to be totaled; and Sum, to hold the sum as it is developed. There are also

only two rules, r0 to accumulate the sum, and r1 to print out the total when no more

instances of r1 are п¬Ѓreable.

Note that rule r0 is recursive; whatever the value of total was (<T>), we add the

value of num (<N>) to it to get a new total. Symbols in the antecedent enclosed in

angle brackets are called variables, and are assigned a value when they п¬Ѓrst appear

in the antecedent. Thus in rule r0 the value of num is assigned to <N>, and the value

of total is assigned to <T>. Variables retain these assigned values throughout the

п¬Ѓring of the rule; thus both <N> and <T> are available in the consequent for arith-

metic operations or printing.

10.3.2 Running sum.fps

When we run sum.fps, we see nothing out of the ordinary. Program SUM.FPS

computes a sum of numbers recursively in several sequential steps, one for each

number to be added. Here is the program output:

Compiling program SUM.FPS

Ready to run sum.fps

adding 12.34 to 0 getting 12.34

adding 23.45 to 12.34 getting 35.79

adding 34.56 to 35.79 getting 70.35

adding 45.67 to 70.35 getting 116.02

adding 56.78 to 116.02 getting 172.8

adding 67.89 to 172.8 getting 240.69

adding 78.9 to 240.69 getting 319.59

adding 89.01 to 319.59 getting 408.6

adding 90.12 to 408.6 getting 498.72

adding 1.23 to 498.72 getting 499.95

adding 32.1 to 499.95 getting 532.05

adding 21.09 to 532.05 getting 553.14

adding 32.1 to 553.14 getting 585.24

adding 43.21 to 585.24 getting 628.45

TEAM LinG - Live, Informative, Non-cost and Genuine !

167

10.3 SUM.FPS

adding 54.32 to 628.45 getting 682.77

adding 65.43 to 682.77 getting 748.2

total of all is 748.2

Sum.fps finished

If at the end of the run we enter the debug command history; we see that the

number of rule instances п¬Ѓred equals the number of data points plus one rule for

outputting the sum, which seems very reasonable.

>>history;

Fired Rule History, first to most recent

******** STACK TOP **********

rule r0 Time Tags 17 1 pconf 1000

rule r0 Time Tags 18 2 pconf 1000

rule r0 Time Tags 19 3 pconf 1000

rule r0 Time Tags 20 4 pconf 1000

rule r0 Time Tags 21 5 pconf 1000

rule r0 Time Tags 22 6 pconf 1000

rule r0 Time Tags 23 7 pconf 1000

rule r0 Time Tags 24 8 pconf 1000

rule r0 Time Tags 25 9 pconf 1000

rule r0 Time Tags 26 10 pconf 1000

rule r0 Time Tags 27 11 pconf 1000

rule r0 Time Tags 28 12 pconf 1000

rule r0 Time Tags 29 13 pconf 1000

rule r0 Time Tags 30 14 pconf 1000

rule r0 Time Tags 31 15 pconf 1000

rule r0 Time Tags 32 16 pconf 1000

rule r1 Time Tags 33 pconf 0

******** STACK BOTTOM **********

10.3.3 Running sum.fps with Debugging Commands prstack; and Run 1;

To get a picture of the systems overhead going on, comment out the run command at

the end of sum.fps by placing a colon in front of the command and then run the

program with a sequence of prstack; and run 1; commands.

The п¬Ѓrst prstack command produces a screen output like this:

LOCAL STACK (partially ordered)

******** STACK TOP **********

rule r0 Time Tags 17 16 pconf 1000

rule r0 Time Tags 17 15 pconf 1000

...

rule r0 Time Tags 17 1 pconf 1000

rule r1 Time Tags 17 pconf 0

******** STACK BOTTOM **********

TEAM LinG - Live, Informative, Non-cost and Genuine !

168 SIMPLE EXAMPLE PROGRAMS

стр. 58 |