Changed license terms to the plain LGPL thru-out.
[bpt/guile.git] / ice-9 / poe.scm
1 ;;; installed-scm-file
2
3 ;;;; Copyright (C) 1996, 2001 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 2.1 of the License, or (at your option) any later version.
9 ;;;;
10 ;;;; This library 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 GNU
13 ;;;; Lesser General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ;;;;
19
20 \f
21 (define-module (ice-9 poe)
22 :use-module (ice-9 hcons)
23 :export (pure-funcq perfect-funcq))
24
25 \f
26
27
28 ;;; {Pure Functions}
29 ;;;
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.
34 ;;;
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.
38 ;;;
39
40 \f
41 ;;; {pure-funcq}
42 ;;;
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.
46 ;;;
47
48
49 (define funcq-memo (make-weak-key-hash-table 523)) ; !!! randomly selected values
50 (define funcq-buffer (make-gc-buffer 256))
51
52 (define (funcq-hash arg-list n)
53 (let ((it (let loop ((x 0)
54 (arg-list arg-list))
55 (if (null? arg-list)
56 (modulo x n)
57 (loop (logior x (hashq (car arg-list) 4194303))
58 (cdr arg-list))))))
59 it))
60
61 (define (funcq-assoc arg-list alist)
62 (let ((it (and alist
63 (let and-map ((key arg-list)
64 (entry (caar alist)))
65 (or (and (and (not key) (not entry))
66 (car alist))
67 (and key entry
68 (eq? (car key) (car entry))
69 (and-map (cdr key) (cdr entry))))))))
70 it))
71
72
73
74 (define (pure-funcq base-func)
75 (lambda args
76 (let ((cached (hashx-get-handle funcq-hash funcq-assoc funcq-memo (cons base-func args))))
77 (if cached
78 (begin
79 (funcq-buffer (car cached))
80 (cdr cached))
81
82 (let ((val (apply base-func args))
83 (key (cons base-func args)))
84 (funcq-buffer key)
85 (hashx-set! funcq-hash funcq-assoc funcq-memo key val)
86 val)))))
87
88 \f
89
90 ;;; {Perfect funq}
91 ;;;
92 ;;; A pure funq may sometimes forget its past but a perfect
93 ;;; funcq never does.
94 ;;;
95
96 (define (perfect-funcq size base-func)
97 (define funcq-memo (make-hash-table size))
98
99 (lambda args
100 (let ((cached (hashx-get-handle funcq-hash funcq-assoc funcq-memo (cons base-func args))))
101 (if cached
102 (begin
103 (funcq-buffer (car cached))
104 (cdr cached))
105
106 (let ((val (apply base-func args))
107 (key (cons base-func args)))
108 (funcq-buffer key)
109 (hashx-set! funcq-hash funcq-assoc funcq-memo key val)
110 val)))))
111
112
113
114
115
116
117
118