1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix memoization)
24 (define-syntax-rule (call/mv thunk)
25 (call-with-values thunk list))
26 (define-syntax-rule (return/mv lst)
29 (define-syntax-rule (call/1 thunk)
31 (define-syntax-rule (return/1 value)
34 (define %nothing ;nothingness
35 (list 'this 'is 'nothing))
37 (define-syntax define-cache-procedure
39 "Define a procedure NAME that implements a cache using HASH-REF and
40 HASH-SET!. Use CALL to invoke the thunk and RETURN to return its value; CALL
41 and RETURN are used to distinguish between multiple-value and single-value
43 ((_ name hash-ref hash-set! call return)
44 (define (name cache key thunk)
45 "Cache the result of THUNK under KEY in CACHE, or return the
46 already-cached result."
47 (let ((results (hash-ref cache key %nothing)))
48 (if (eq? results %nothing)
49 (let ((results (call thunk)))
50 (hash-set! cache key results)
53 ((_ name hash-ref hash-set!)
54 (define-cache-procedure name hash-ref hash-set!
57 (define-cache-procedure cached/mv hash-ref hash-set!)
58 (define-cache-procedure cachedq/mv hashq-ref hashq-set!)
59 (define-cache-procedure cached hash-ref hash-set! call/1 return/1)
60 (define-cache-procedure cachedq hashq-ref hashq-set! call/1 return/1)
62 (define (memoize proc)
63 "Return a memoizing version of PROC.
65 This is a generic version of 'mlambda' what works regardless of the arity of
66 'proc'. It is more expensive since the argument list is always allocated, and
67 the result is returned via (apply values results)."
68 (let ((cache (make-hash-table)))
72 (apply proc args))))))
74 (define-syntax %mlambda
76 "Return a memoizing lambda. This is restricted to procedures that return
78 ((_ cached () body ...)
79 ;; The zero-argument case is equivalent to a promise.
80 (let ((result #f) (cached? #f))
83 (set! result (begin body ...))
87 ;; Optimize the fixed-arity case such that there's no argument list
88 ;; allocated. XXX: We can't really avoid the closure allocation since
89 ;; Guile 2.0's compiler will always keep it.
90 ((_ cached (arg) body ...) ;one argument
91 (let ((cache (make-hash-table))
92 (proc (lambda (arg) body ...)))
94 (cached cache arg (lambda () (proc arg))))))
95 ((_ _ (args ...) body ...) ;two or more arguments
96 (let ((cache (make-hash-table))
97 (proc (lambda (args ...) body ...)))
99 ;; XXX: Always use 'cached', which uses 'equal?', to compare the
101 (cached cache (list args ...)
103 (proc args ...))))))))
105 (define-syntax-rule (mlambda formals body ...)
106 "Define a memoizing lambda. The lambda's arguments are compared with
107 'equal?', and BODY is expected to yield a single return value."
108 (%mlambda cached formals body ...))
110 (define-syntax-rule (mlambdaq formals body ...)
111 "Define a memoizing lambda. If FORMALS lists a single argument, it is
112 compared using 'eq?'; otherwise, the argument list is compared using 'equal?'.
113 BODY is expected to yield a single return value."
114 (%mlambda cachedq formals body ...))