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
20 ;;;; As a special exception, the Free Software Foundation gives permission
21 ;;;; for additional uses of the text contained in its release of GUILE.
23 ;;;; The exception is that, if you link the GUILE library with other files
24 ;;;; to produce an executable, this does not by itself cause the
25 ;;;; resulting executable to be covered by the GNU General Public License.
26 ;;;; Your use of that executable is in no way restricted on account of
27 ;;;; linking the GUILE library code into it.
29 ;;;; This exception does not however invalidate any other reasons why
30 ;;;; the executable file might be covered by the GNU General Public License.
32 ;;;; This exception applies only to the code released by the
33 ;;;; Free Software Foundation under the name GUILE. If you copy
34 ;;;; code from other Free Software Foundation releases into a copy of
35 ;;;; GUILE, as the General Public License permits, the exception does
36 ;;;; not apply to the code that you add in this way. To avoid misleading
37 ;;;; anyone as to the status of such modified files, you must delete
38 ;;;; this exception notice from them.
40 ;;;; If you write modifications of your own for GUILE, it is your choice
41 ;;;; whether to permit this exception to apply to your modifications.
42 ;;;; If you do not wish that, delete this exception notice.
46 (define-module (ice-9 poe)
47 :use-module (ice-9 hcons))
54 ;;; A pure function (of some sort) is characterized by two equality
55 ;;; relations: one on argument lists and one on return values.
56 ;;; A pure function is one that when applied to equal arguments lists
57 ;;; yields equal results.
59 ;;; If the equality relationship on return values can be eq?, it may make
60 ;;; sense to cache values returned by the function. Choosing the right
61 ;;; equality relation on arguments is tricky.
67 ;;; The simplest case of pure functions are those in which results
68 ;;; are only certainly eq? if all of the arguments are. These functions
69 ;;; are called "pure-funcq", for obvious reasons.
73 (define funcq-memo (make-weak-key-hash-table 523)) ; !!! randomly selected values
74 (define funcq-buffer (make-gc-buffer 256))
76 (define (funcq-hash arg-list n)
77 (let ((it (let loop ((x 0)
81 (loop (logior x (hashq (car arg-list) 4194303))
85 (define (funcq-assoc arg-list alist)
87 (let and-map ((key arg-list)
89 (or (and (and (not key) (not entry))
92 (eq? (car key) (car entry))
93 (and-map (cdr key) (cdr entry))))))))
98 (define-public (pure-funcq base-func)
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)
116 ;;; A pure funq may sometimes forget its past but a perfect
117 ;;; funcq never does.
120 (define-public (perfect-funcq size base-func)
121 (define funcq-memo (make-hash-table size))
124 (let ((cached (hashx-get-handle funcq-hash funcq-assoc funcq-memo (cons base-func args))))
127 (funcq-buffer (car cached))
130 (let ((val (apply base-func args))
131 (key (cons base-func args)))
133 (hashx-set! funcq-hash funcq-assoc funcq-memo key val)