next node: InterOpalEquality,
prev node: User Subsystem : Reflections,
up to node: User Subsystem : Reflections


Cache

Caching function results To speed up repeated calculation of a function result, especially when involving side effects, it is clever to use a cache. The functionality of such a cache should be as simple and transparent as possible. One might want to cache a function with or without precached results. And one should be able to access the function, using cached results if possible or computing and caching a previously unknown result.

Signature of Cache

A cache is implemented as an an abstract map-like object, mapping elements of dom, ordered by <, to elements of codom. It will only work correctly if used as the definition of a constant.

SIGNATURE Cache[dom, <, codom]

SORT dom codom
FUN < : dom ** dom -> bool

IMPORT Map[dom, <, codom] ONLY map

SORT cache

Initializing a cache

cache(f, m) yields a cache that will store results of f and has the initial precached results defined in m. It will never compute f(x) for an x in the domain of {m}. cache(f) is equivalent to cache(f, {}).

 
FUN cache : (dom -> codom) -> cache
FUN cache : (dom -> codom) ** map[dom, <, codom] -> cache

Accessing a cache

If the cache c was set up for function f, then c ! x yields f(x). Besides, the result will be stored in the cache, so that a second evaluation of c ! x will not cause f(x) to be evaluated again.

FUN ! : cache ** dom -> codom


next node: InterOpalEquality,
prev node: User Subsystem : Reflections,
up to node: User Subsystem : Reflections