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-syntax define-cache-procedure
36 "Define a procedure NAME that implements a cache using HASH-REF and
37 HASH-SET!. Use CALL to invoke the thunk and RETURN to return its value; CALL
38 and RETURN are used to distinguish between multiple-value and single-value
40 ((_ name hash-ref hash-set! call return)
42 (let ((%nothing '(this is nothing)))
43 (lambda (cache key thunk)
44 "Cache the result of THUNK under KEY in CACHE, or return the
45 already-cached result."
46 (let ((results (hash-ref cache key %nothing)))
47 (if (eq? results %nothing)
48 (let ((results (call thunk)))
49 (hash-set! cache key results)
51 (return results)))))))
52 ((_ name hash-ref hash-set!)
53 (define-cache-procedure name hash-ref hash-set!
56 (define-cache-procedure cached/mv hash-ref hash-set!)
57 (define-cache-procedure cachedq/mv hashq-ref hashq-set!)
58 (define-cache-procedure cached hash-ref hash-set! call/1 return/1)
59 (define-cache-procedure cachedq hashq-ref hashq-set! call/1 return/1)
61 (define (memoize proc)
62 "Return a memoizing version of PROC.
64 This is a generic version of 'mlambda' what works regardless of the arity of
65 'proc'. It is more expensive since the argument list is always allocated, and
66 the result is returned via (apply values results)."
67 (let ((cache (make-hash-table)))
71 (apply proc args))))))
73 (define-syntax %mlambda
75 "Return a memoizing lambda. This is restricted to procedures that return
77 ((_ cached () body ...)
78 ;; The zero-argument case is equivalent to a promise.
79 (let ((result #f) (cached? #f)
80 (compute (lambda () body ...)))
83 (set! result (compute))
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 ...))