3 ;;;; Copyright (C) 1996 Free Software Foundation, Inc.
5 ;;;; This program is free software; you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation; either version 2, or (at your option)
8 ;;;; any later version.
10 ;;;; This program is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with this software; see the file COPYING. If not, write to
17 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 ;;;; Boston, MA 02111-1307 USA
22 (define-module #/ice-9/poe
23 :use-module #/ice-9/hcons)
30 ;;; A pure function (of some sort) is characterized by two equality
31 ;;; relations: one on argument lists and one on return values.
32 ;;; A pure function is one that when applied to equal arguments lists
33 ;;; yields equal results.
35 ;;; If the equality relationship on return values can be eq?, it may make
36 ;;; sense to cache values returned by the function. Choosing the right
37 ;;; equality relation on arguments is tricky.
43 ;;; The simplest case of pure functions are those in which results
44 ;;; are only certainly eq? if all of the arguments are. These functions
45 ;;; are called "pure-funcq", for obvious reasons.
49 (define funcq-memo (make-weak-key-hash-table 523)) ; !!! randomly selected values
50 (define funcq-buffer (make-gc-buffer 256))
52 (define (funcq-hash arg-list n)
53 (let ((it (let loop ((x 0)
57 (loop (logior x (hashq (car arg-list) 4194303))
61 (define (funcq-assoc arg-list alist)
63 (let and-map ((key arg-list)
65 (or (and (and (not key) (not entry))
68 (eq? (car key) (car entry))
69 (and-map (cdr key) (cdr entry))))))))
74 (define-public (pure-funcq base-func)
76 (let ((cached (hashx-get-handle funcq-hash funcq-assoc funcq-memo (cons base-func args))))
79 (funcq-buffer (car cached))
82 (let ((val (apply base-func args))
83 (key (cons base-func args)))
85 (hashx-set! funcq-hash funcq-assoc funcq-memo key val)
92 ;;; A pure funq may sometimes forget its past but a perfect
96 (define-public (perfect-funcq size base-func)
97 (define funcq-memo (make-hash-table size))
100 (let ((cached (hashx-get-handle funcq-hash funcq-assoc funcq-memo (cons base-func args))))
103 (funcq-buffer (car cached))
106 (let ((val (apply base-func args))
107 (key (cons base-func args)))
109 (hashx-set! funcq-hash funcq-assoc funcq-memo key val)