A generic way to define function that cache their values.
fricas
(1) -> <spad>
fricas
)abbrev domain CACHEDF CachedFunction
++ Author: Ralf Hemmecke
++ Date Created: 2009
++ Date Last Updated: Oct 27, 2009
++ Basic Operations:
++ Related Domains: Table, AssociationList
++ Also See:
++ AMS Classifications:
++ Keywords:
++ Examples:
++ References:
++ Description:
++ This domain is a domain of functions with associated cache.
CachedFunction(A: SetCategory, B: SetCategory): Exports == Implementation where
Exports ==> with
function: % -> (A -> B)
cachedFunction: (A -> B) -> %
apply: (%, A) -> B
recursiveDefine: (%, f: A -> B) -> %
Implementation ==> add
Rep := Record(cache: Table(A, B), fun: A -> B);
function(x: %): A -> B == x.fun
cachedFunction(f: A -> B): % == [empty()$Table(A, B), f]
apply(x: %, a: A): B ==
c := x.cache
u: Union(B, "failed") := search(a, c)
if u case B
then u :: B
else
f: A -> B := x.fun
c.a := f(a)
recursiveDefine(x: %, f: A -> B): % ==
x.fun := f;
x</spad>
fricas
Compiling FriCAS source code from file
/var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/8303643491408128431-25px001.spad
using old system compiler.
CACHEDF abbreviates domain CachedFunction
------------------------------------------------------------------------
initializing NRLIB CACHEDF for CachedFunction
compiling into NRLIB CACHEDF
compiling exported function : % -> A -> B
CACHEDF;function;%M;1 is replaced by QCDR
Time: 0 SEC.
compiling exported cachedFunction : A -> B -> %
Time: 0 SEC.
compiling exported apply : (%,A) -> B
Time: 0 SEC.
compiling exported recursiveDefine : (%,A -> B) -> %
Time: 0 SEC.
(time taken in buildFunctor: 0)
Time: 0 SEC.
Cumulative Statistics for Constructor CachedFunction
Time: 0 seconds
finalizing NRLIB CACHEDF
Processing CachedFunction for Browser database:
--------constructor---------
--->-->CachedFunction((function ((Mapping B A) %))): Not documented!!!!
--->-->CachedFunction((cachedFunction (% (Mapping B A)))): Not documented!!!!
--->-->CachedFunction((apply (B % A))): Not documented!!!!
--->-->CachedFunction((recursiveDefine (% % (Mapping B A)))): Not documented!!!!
; compiling file "/var/aw/var/LatexWiki/CACHEDF.NRLIB/CACHEDF.lsp" (written 14 SEP 2026 01:00:14 PM):
; wrote /var/aw/var/LatexWiki/CACHEDF.NRLIB/CACHEDF.fasl
; compilation finished in 0:00:00.008
------------------------------------------------------------------------
CachedFunction is now explicitly exposed in frame initial
CachedFunction will be automatically loaded when needed from
/var/aw/var/LatexWiki/CACHEDF.NRLIB/CACHEDF
fricas
)set message time on
I := Integer
Type: Type
fricas
Time: 0 sec
f(n:I): I ==1
Function declaration f : Integer -> Integer has been added to
workspace.
Type: Void
fricas
Time: 0 sec
fib: CachedFunction(I,I) := cachedFunction(f)
fricas
Compiling function f with type Integer -> Integer
LISP output:
(#<HASH-TABLE :TEST EQUAL :COUNT 0 {10021CABB3}> #<FUNCTION |*1;f;1;initial|>)
Type: CachedFunction
?(Integer,
Integer)
fricas
Time: 0 sec
recursiveDefine(fib,(n:I):I+->if n<2 then 1 else fib(n-1) + fib(n-2))
LISP output:
(#<HASH-TABLE :TEST EQUAL :COUNT 0 {10021CABB3}> #<FUNCTION |*1;anonymousFunction;0;initial;internal|>)
Type: CachedFunction
?(Integer,
Integer)
fricas
Time: 0 sec
fib 40
fricas
Time: 0 sec
g(n:I): I == if n<2 then 1 else g(n-1)+g(n-2)
Function declaration g : Integer -> Integer has been added to
workspace.
Type: Void
fricas
Time: 0 sec
g 40
fricas
Compiling function g with type Integer -> Integer
fricas
Time: 1.60 (EV) = 1.61 sec