Memoization cache contract (source code)

= Memoization cache contract

For a terminating <pure function>, a reusable entry must record its correct completed result, and equal <memoization keys> must imply equal results throughout the entry's <memoization cache lifetime>. Reusing that result must also preserve the behavior callers require. These conditions separate correctness from speed: a complete key can be correct even if it rarely repeats.

A procedure using <deterministic computation> can still have a required <side effect>. If a procedure returns $n^2$ and appends an audit record on every call, serving the second call from a table preserves the numerical answer but skips the second record. Likewise, if each call promises a fresh <mutable list>, returning the same stored <mutable list> changes observable sharing and allows a caller to damage later answers. Plain result reuse is unsuitable for those contracts.<Python 3.14 documentation/functools — Higher-order functions and operations on callable objects (Python 3.14 documentation)>[Python documentation, functools.lru_cache: paragraph warning against functions with side effects, distinct mutable results, or impure time/random results]{ref}

Completion matters for <recursion>. Suppose computing key $A$ needs key $B$, and computing $B$ needs $A$. A table containing only completed answers has no answer to return for the second $A$; remembering completed values does not make this <directed cycle> terminate. Marking a key `ACTIVE` before descending detects re-entry, as in <spreadsheet dependency evaluation>, but detection is not a solution of the circular equations. If computation fails, clear its active mark and do not publish an unfinished value. The example using <Fibonacci numbers> avoids this issue through a strictly decreasing argument.

The call counts above assume sequential execution and no eviction. Concurrent requests for an absent key can both begin evaluation before either publishes its result; keeping a table coherent alone does not ensure one body execution per key.<Python 3.14 documentation/functools — Higher-order functions and operations on callable objects (Python 3.14 documentation)>[Python documentation, functools.cache and functools.lru_cache: thread-safe cache updates can still allow repeated evaluation before completion]{ref}