summaryrefslogtreecommitdiff
path: root/guix/utils.scm
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2013-01-23 22:24:47 +0100
committerLudovic Courtès <ludo@gnu.org>2013-01-23 23:33:09 +0100
commitbbb7a00e9a224d812a56c67956efb3e8a840cf0a (patch)
treee5fb41fe2fde5e57431cc472b2981538b266f655 /guix/utils.scm
parent6798a8e485281f855c0777d3f952b4e02953cfd2 (diff)
downloadgnu-guix-bbb7a00e9a224d812a56c67956efb3e8a840cf0a.tar
gnu-guix-bbb7a00e9a224d812a56c67956efb3e8a840cf0a.tar.gz
define-record-type*: Add the `thunked' field definition keyword.
* guix/utils.scm (define-record-type*)[make-syntactic-constructor]: Add a `thunked' parameter. (thunked-field?, field-bindings): New procedures. Use the latter when generating `letrec*' bindings. [thunked-field?, thunked-field-accessor-name, field-spec->srfi-9, thunked-field-accessor-name]: New procedures. Use them when generating the `define-record-type' form, and to generated thunk field accessors, along call to `make-syntactic-constructor' with the new argument. * tests/utils.scm ("define-record-type* & thunked", "define-record-type* & thunked & default", "define-record-type* & thunked & inherited"): New tests.
Diffstat (limited to 'guix/utils.scm')
-rw-r--r--guix/utils.scm101
1 files changed, 85 insertions, 16 deletions
diff --git a/guix/utils.scm b/guix/utils.scm
index 4d761f590d..7ab835e7f1 100644
--- a/guix/utils.scm
+++ b/guix/utils.scm
@@ -1,5 +1,5 @@
;;; GNU Guix --- Functional package management for GNU
-;;; Copyright © 2012 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
@@ -192,10 +192,11 @@ evaluate to a simple datum."
"Define the given record type such that an additional \"syntactic
constructor\" is defined, which allows instances to be constructed with named
field initializers, à la SRFI-35, as well as default values."
- (define (make-syntactic-constructor type name ctor fields defaults)
+ (define (make-syntactic-constructor type name ctor fields thunked defaults)
"Make the syntactic constructor NAME for TYPE, that calls CTOR, and
expects all of FIELDS to be initialized. DEFAULTS is the list of
-FIELD/DEFAULT-VALUE tuples."
+FIELD/DEFAULT-VALUE tuples, and THUNKED is the list of identifiers of
+thunked fields."
(with-syntax ((type type)
(name name)
(ctor ctor)
@@ -221,10 +222,23 @@ FIELD/DEFAULT-VALUE tuples."
'expected
(iota (length 'expected)))))
+ (define (thunked-field? f)
+ (memq (syntax->datum f) '#,thunked))
+
+ (define (field-bindings field+value)
+ ;; Return field to value bindings, for use in `letrec*' below.
+ (map (lambda (field+value)
+ (syntax-case field+value ()
+ ((field value)
+ #`(field
+ #,(if (thunked-field? #'field)
+ #'(lambda () value)
+ #'value)))))
+ field+value))
(syntax-case s (inherit #,@fields)
((_ (inherit orig-record) (field value) (... ...))
- #`(letrec* ((field value) (... ...))
+ #`(letrec* #,(field-bindings #'((field value) (... ...)))
#,(record-inheritance #'orig-record
#'((field value) (... ...)))))
((_ (field value) (... ...))
@@ -239,7 +253,12 @@ FIELD/DEFAULT-VALUE tuples."
(eq? f (car (syntax->datum x))))
#'((field value) (... ...)))
car)
- (car (assoc-ref dflt (syntax->datum f)))))
+ (let ((value
+ (car (assoc-ref dflt
+ (syntax->datum f)))))
+ (if (thunked-field? f)
+ #`(lambda () #,value)
+ value))))
(let-syntax ((error*
(syntax-rules ()
@@ -250,7 +269,8 @@ FIELD/DEFAULT-VALUE tuples."
s)))))
(let ((fields (append fields (map car dflt))))
(cond ((lset= eq? fields 'expected)
- #`(letrec* ((field value) (... ...))
+ #`(letrec* #,(field-bindings
+ #'((field value) (... ...)))
(ctor #,@(map field-value 'expected))))
((pair? (lset-difference eq? fields 'expected))
(error* "extraneous field initializers ~a"
@@ -268,19 +288,68 @@ FIELD/DEFAULT-VALUE tuples."
(field-default-value #'(field options ...)))
(_ #f)))
+ (define (thunked-field? s)
+ ;; Return the field name if the field defined by S is thunked.
+ (syntax-case s (thunked)
+ ((field (thunked) _ ...)
+ #'field)
+ ((field _ options ...)
+ (thunked-field? #'(field options ...)))
+ (_ #f)))
+
+ (define (thunked-field-accessor-name field)
+ ;; Return the name (an unhygienic syntax object) of the "real"
+ ;; getter for field, which is assumed to be a thunked field.
+ (syntax-case field ()
+ ((field get options ...)
+ (let* ((getter (syntax->datum #'get))
+ (real-getter (symbol-append '% getter '-real)))
+ (datum->syntax #'get real-getter)))))
+
+ (define (field-spec->srfi-9 field)
+ ;; Convert a field spec of our style to a SRFI-9 field spec of the
+ ;; form (field get).
+ (syntax-case field ()
+ ((name get options ...)
+ #`(name
+ #,(if (thunked-field? field)
+ (thunked-field-accessor-name field)
+ #'get)))))
+
+ (define (thunked-field-accessor-definition field)
+ ;; Return the real accessor for FIELD, which is assumed to be a
+ ;; thunked field.
+ (syntax-case field ()
+ ((name get _ ...)
+ (with-syntax ((real-get (thunked-field-accessor-name field)))
+ #'(define-inlinable (get x)
+ ;; The real value of that field is a thunk, so call it.
+ ((real-get x)))))))
+
(syntax-case s ()
((_ type syntactic-ctor ctor pred
(field get options ...) ...)
- #`(begin
- (define-record-type type
- (ctor field ...)
- pred
- (field get) ...)
- #,(make-syntactic-constructor #'type #'syntactic-ctor #'ctor
- #'(field ...)
- (filter-map field-default-value
- #'((field options ...)
- ...))))))))
+ (let* ((field-spec #'((field get options ...) ...)))
+ (with-syntax (((field-spec* ...)
+ (map field-spec->srfi-9 field-spec))
+ ((thunked-field-accessor ...)
+ (filter-map (lambda (field)
+ (and (thunked-field? field)
+ (thunked-field-accessor-definition
+ field)))
+ field-spec)))
+ #`(begin
+ (define-record-type type
+ (ctor field ...)
+ pred
+ field-spec* ...)
+ (begin thunked-field-accessor ...)
+ #,(make-syntactic-constructor #'type #'syntactic-ctor #'ctor
+ #'(field ...)
+ (filter-map thunked-field? field-spec)
+ (filter-map field-default-value
+ #'((field options ...)
+ ...))))))))))
(define (memoize proc)
"Return a memoizing version of PROC."