91acd11953786e8451b00c52085fa3ce8d0806d5
[bpt/guile.git] / ice-9 / poe.scm
1 ;;; installed-scm-file
2
3 ;;;; Copyright (C) 1996 Free Software Foundation, Inc.
4 ;;;;
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.
9 ;;;;
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.
14 ;;;;
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
19 ;;;;
20 ;;;; As a special exception, the Free Software Foundation gives permission
21 ;;;; for additional uses of the text contained in its release of GUILE.
22 ;;;;
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.
28 ;;;;
29 ;;;; This exception does not however invalidate any other reasons why
30 ;;;; the executable file might be covered by the GNU General Public License.
31 ;;;;
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.
39 ;;;;
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.
43 ;;;;
44
45 \f
46 (define-module (ice-9 poe)
47 :use-module (ice-9 hcons))
48
49 \f
50
51
52 ;;; {Pure Functions}
53 ;;;
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.
58 ;;;
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.
62 ;;;
63
64 \f
65 ;;; {pure-funcq}
66 ;;;
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.
70 ;;;
71
72
73 (define funcq-memo (make-weak-key-hash-table 523)) ; !!! randomly selected values
74 (define funcq-buffer (make-gc-buffer 256))
75
76 (define (funcq-hash arg-list n)
77 (let ((it (let loop ((x 0)
78 (arg-list arg-list))
79 (if (null? arg-list)
80 (modulo x n)
81 (loop (logior x (hashq (car arg-list) 4194303))
82 (cdr arg-list))))))
83 it))
84
85 (define (funcq-assoc arg-list alist)
86 (let ((it (and alist
87 (let and-map ((key arg-list)
88 (entry (caar alist)))
89 (or (and (and (not key) (not entry))
90 (car alist))
91 (and key entry
92 (eq? (car key) (car entry))
93 (and-map (cdr key) (cdr entry))))))))
94 it))
95
96
97
98 (define-public (pure-funcq base-func)
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 \f
113
114 ;;; {Perfect funq}
115 ;;;
116 ;;; A pure funq may sometimes forget its past but a perfect
117 ;;; funcq never does.
118 ;;;
119
120 (define-public (perfect-funcq size base-func)
121 (define funcq-memo (make-hash-table size))
122
123 (lambda args
124 (let ((cached (hashx-get-handle funcq-hash funcq-assoc funcq-memo (cons base-func args))))
125 (if cached
126 (begin
127 (funcq-buffer (car cached))
128 (cdr cached))
129
130 (let ((val (apply base-func args))
131 (key (cons base-func args)))
132 (funcq-buffer key)
133 (hashx-set! funcq-hash funcq-assoc funcq-memo key val)
134 val)))))
135
136
137
138
139
140
141
142