Commit | Line | Data |
---|---|---|
0f2d19dd JB |
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 | |
15328041 JB |
17 | ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
18 | ;;;; Boston, MA 02111-1307 USA | |
0f2d19dd JB |
19 | ;;;; |
20 | ||
21 | \f | |
8bb7330c JB |
22 | (define-module (ice-9 poe) |
23 | :use-module (ice-9 hcons)) | |
0f2d19dd JB |
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 | ||
ea7715eb | 49 | (define funcq-memo (make-weak-key-hash-table 523)) ; !!! randomly selected values |
0f2d19dd JB |
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-public (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-public (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 |