No edit summary |
No edit summary |
||
Line 31: | Line 31: | ||
* function calls such as fib(30) (which in turn calls fib(29) and fib(28) which are also memoized) | * function calls such as fib(30) (which in turn calls fib(29) and fib(28) which are also memoized) | ||
* computation of relational image such as fib[28..30], which internally results in three function calls to fib | * computation of relational image such as fib[28..30], which internally results in three function calls to fib | ||
* membership predicates of the form x|->y:fib, where x is a known value | |||
The following points are relevant: | The following points are relevant: | ||
* Memoization is currently only possible for functions declared in the ABSTRACT_CONSTANTS section | * Memoization is currently only possible for functions declared in the ABSTRACT_CONSTANTS section | ||
* Memoization means that all results of function calls are stored. The memoization table is reset when another B machine is loaded or the same B machine is re-loaded. | * Memoization means that all results of function calls are stored. The memoization table is reset when another B machine is loaded or the same B machine is re-loaded. | ||
* Memoized functions are | * Memoized functions are often treated in a similar way to symbolic functions. If your function is finite and relatively small, it may be better to put the function into the CONCRETE_CONSTANTS section so that it gets computed in its entirety once. | ||
* Memoization of a function f is currently <b>not</b> active for computations such as dom(f), ran(f) or | * Memoization of a function f is currently <b>not</b> active for computations such as dom(f). | ||
* If a predicate requires the computation of the full function, e.g., ran(f), then the results will be stored and will be available for future function calls or in case the full value is needed again. | |||
With the command-line version probcli you can use the <tt>-profile</tt> command to obtain some statistics about memoization. | With the command-line version probcli you can use the <tt>-profile</tt> command to obtain some statistics about memoization. |
As of version 1.9.0-beta9 ProB allows you to annotate functions in the ABSTRACT_CONSTANTS section for memoization.
Memoization is a technique for storing results of function applications and reusing the result if possible to avoid re-computing the function for the same arguments again.
To enable memoization you either need to
Take the following example:
MACHINE MemoizationTests ABSTRACT_CONSTANTS fib /*@desc memo */, fact /*@desc memo */ PROPERTIES fib = %x.(x:NATURAL | (IF x=0 or x=1 THEN 1 ELSE fib(x-1)+fib(x-2) END)) & fact = %x.(x:NATURAL|(IF x=0 THEN 1 ELSE x*fact(x-1) END)) ASSERTIONS fib(30)=1346269; fib[28..30] = {514229,832040,1346269}; END
Memoization means that the recursive Fibonacci function now runs in linear time rather than in exponential time. Generally, memoization is useful for functions which are complex to compute but which are called repeatedly with the same arguments.
As can be seen above, memoization is active for
The following points are relevant:
With the command-line version probcli you can use the -profile command to obtain some statistics about memoization.