1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 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 records)
20 #:use-module (srfi srfi-1)
21 #:use-module (srfi srfi-9)
22 #:use-module (srfi srfi-26)
23 #:use-module (ice-9 match)
24 #:export (define-record-type*
30 ;;; Utilities for dealing with Scheme records.
34 (define-syntax define-record-type*
36 "Define the given record type such that an additional \"syntactic
37 constructor\" is defined, which allows instances to be constructed with named
38 field initializers, à la SRFI-35, as well as default values."
39 (define (make-syntactic-constructor type name ctor fields thunked defaults)
40 "Make the syntactic constructor NAME for TYPE, that calls CTOR, and
41 expects all of FIELDS to be initialized. DEFAULTS is the list of
42 FIELD/DEFAULT-VALUE tuples, and THUNKED is the list of identifiers of
44 (with-syntax ((type type)
51 (define (record-inheritance orig-record field+value)
52 ;; Produce code that returns a record identical to
53 ;; ORIG-RECORD, except that values for the FIELD+VALUE alist
55 (define (field-inherited-value f)
56 (and=> (find (lambda (x)
57 (eq? f (car (syntax->datum x))))
62 #,@(map (lambda (field index)
63 (or (field-inherited-value field)
64 #`(struct-ref #,orig-record
67 (iota (length 'expected)))))
69 (define (thunked-field? f)
70 (memq (syntax->datum f) '#,thunked))
72 (define (field-bindings field+value)
73 ;; Return field to value bindings, for use in `letrec*' below.
74 (map (lambda (field+value)
75 (syntax-case field+value ()
78 #,(if (thunked-field? #'field)
83 (syntax-case s (inherit #,@fields)
84 ((_ (inherit orig-record) (field value) (... ...))
85 #`(letrec* #,(field-bindings #'((field value) (... ...)))
86 #,(record-inheritance #'orig-record
87 #'((field value) (... ...)))))
88 ((_ (field value) (... ...))
89 (let ((fields (map syntax->datum #'(field (... ...))))
90 (dflt (map (match-lambda
92 (list (syntax->datum f) v)))
95 (define (field-value f)
96 (or (and=> (find (lambda (x)
97 (eq? f (car (syntax->datum x))))
98 #'((field value) (... ...)))
102 (syntax->datum f)))))
103 (if (thunked-field? f)
104 #`(lambda () #,value)
109 ((_ fmt args (... ...))
110 (syntax-violation 'name
114 (let ((fields (append fields (map car dflt))))
115 (cond ((lset= eq? fields 'expected)
116 #`(letrec* #,(field-bindings
117 #'((field value) (... ...)))
118 (ctor #,@(map field-value 'expected))))
119 ((pair? (lset-difference eq? fields 'expected))
120 (error* "extraneous field initializers ~a"
121 (lset-difference eq? fields 'expected)))
123 (error* "missing field initializers ~a"
124 (lset-difference eq? 'expected
127 (define (field-default-value s)
128 (syntax-case s (default)
129 ((field (default val) _ ...)
130 (list #'field #'val))
131 ((field _ options ...)
132 (field-default-value #'(field options ...)))
135 (define (thunked-field? s)
136 ;; Return the field name if the field defined by S is thunked.
137 (syntax-case s (thunked)
138 ((field (thunked) _ ...)
140 ((field _ options ...)
141 (thunked-field? #'(field options ...)))
144 (define (thunked-field-accessor-name field)
145 ;; Return the name (an unhygienic syntax object) of the "real"
146 ;; getter for field, which is assumed to be a thunked field.
147 (syntax-case field ()
148 ((field get options ...)
149 (let* ((getter (syntax->datum #'get))
150 (real-getter (symbol-append '% getter '-real)))
151 (datum->syntax #'get real-getter)))))
153 (define (field-spec->srfi-9 field)
154 ;; Convert a field spec of our style to a SRFI-9 field spec of the
156 (syntax-case field ()
157 ((name get options ...)
159 #,(if (thunked-field? field)
160 (thunked-field-accessor-name field)
163 (define (thunked-field-accessor-definition field)
164 ;; Return the real accessor for FIELD, which is assumed to be a
166 (syntax-case field ()
168 (with-syntax ((real-get (thunked-field-accessor-name field)))
169 #'(define-inlinable (get x)
170 ;; The real value of that field is a thunk, so call it.
174 ((_ type syntactic-ctor ctor pred
175 (field get options ...) ...)
176 (let* ((field-spec #'((field get options ...) ...)))
177 (with-syntax (((field-spec* ...)
178 (map field-spec->srfi-9 field-spec))
179 ((thunked-field-accessor ...)
180 (filter-map (lambda (field)
181 (and (thunked-field? field)
182 (thunked-field-accessor-definition
186 (define-record-type type
190 (begin thunked-field-accessor ...)
191 #,(make-syntactic-constructor #'type #'syntactic-ctor #'ctor
193 (filter-map thunked-field? field-spec)
194 (filter-map field-default-value
195 #'((field options ...)
198 (define (alist->record alist make keys)
199 "Apply MAKE to the values associated with KEYS in ALIST."
200 (let ((args (map (cut assoc-ref alist <>) keys)))
203 (define (object->fields object fields port)
204 "Write OBJECT (typically a record) as a series of recutils-style fields to
205 PORT, according to FIELDS. FIELDS must be a list of field name/getter pairs."
206 (let loop ((fields fields))
210 (((field . get) rest ...)
211 (format port "~a: ~a~%" field (get object))
214 ;;; records.scm ends here