1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2018 Mark H Weaver <mhw@netris.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (guix records)
21 #:use-module (srfi srfi-1)
22 #:use-module (srfi srfi-9)
23 #:use-module (srfi srfi-26)
24 #:use-module (ice-9 match)
25 #:use-module (ice-9 regex)
26 #:use-module (ice-9 rdelim)
27 #:autoload (system base target) (target-most-positive-fixnum)
28 #:export (define-record-type*
38 ;;; Utilities for dealing with Scheme records.
42 (define-syntax record-error
44 "Report a syntactic error in use of CONSTRUCTOR."
45 ((_ constructor form fmt args ...)
46 (syntax-violation constructor
47 (format #f fmt args ...)
50 (eval-when (expand load eval)
51 ;; The procedures below are needed both at run time and at expansion time.
53 (define (current-abi-identifier type)
54 "Return an identifier unhygienically derived from TYPE for use as its
55 \"current ABI\" variable."
56 (let ((type-name (syntax->datum type)))
60 (string-append "% " (symbol->string type-name)
63 (define (abi-check type cookie)
64 "Return syntax that checks that the current \"application binary
65 interface\" (ABI) for TYPE is equal to COOKIE."
66 (with-syntax ((current-abi (current-abi-identifier type)))
67 #`(unless (eq? current-abi #,cookie)
68 ;; The source file where this exception is thrown must be
70 (throw 'record-abi-mismatch-error 'abi-check
71 "~a: record ABI mismatch; recompilation needed"
74 (define* (report-invalid-field-specifier name bindings
75 #:optional parent-form)
76 "Report the first invalid binding among BINDINGS. PARENT-FORM is used for
77 error-reporting purposes."
78 (let loop ((bindings bindings))
79 (syntax-case bindings ()
80 (((field value) rest ...) ;good
82 ((weird _ ...) ;weird!
83 ;; WEIRD may be an identifier, thus lacking source location info, and
84 ;; BINDINGS is a list, also lacking source location info. Hopefully
85 ;; PARENT-FORM provides source location info.
86 (apply syntax-violation name "invalid field specifier"
88 (list parent-form #'weird)
91 (define (report-duplicate-field-specifier name ctor)
92 "Report the first duplicate identifier among the bindings in CTOR."
95 (let loop ((bindings #'(bindings ...))
97 (syntax-case bindings ()
98 (((field value) rest ...)
99 (not (memq (syntax->datum #'field) seen))
100 (loop #'(rest ...) (cons (syntax->datum #'field) seen)))
101 ((duplicate rest ...)
102 (syntax-violation name "duplicate field initializer"
107 (define-syntax-parameter this-record
109 "Return the record being defined. This macro may only be used in the
110 context of the definition of a thunked field."
114 (syntax-violation 'this-record
115 "cannot be used outside of a record instantiation"
118 (define-syntax make-syntactic-constructor
120 "Make the syntactic constructor NAME for TYPE, that calls CTOR, and
121 expects all of EXPECTED fields to be initialized. DEFAULTS is the list of
122 FIELD/DEFAULT-VALUE tuples, THUNKED is the list of identifiers of thunked
123 fields, and DELAYED is the list of identifiers of delayed fields.
125 ABI-COOKIE is the cookie (an integer) against which to check the run-time ABI
126 of TYPE matches the expansion-time ABI."
127 ((_ type name ctor (expected ...)
128 #:abi-cookie abi-cookie
130 #:this-identifier this-identifier
136 (define (record-inheritance orig-record field+value)
137 ;; Produce code that returns a record identical to ORIG-RECORD,
138 ;; except that values for the FIELD+VALUE alist prevail.
139 (define (field-inherited-value f)
140 (and=> (find (lambda (x)
141 (eq? f (car (syntax->datum x))))
145 ;; Make sure there are no unknown field names.
146 (let* ((fields (map (compose car syntax->datum) field+value))
147 (unexpected (lset-difference eq? fields '(expected ...))))
148 (when (pair? unexpected)
149 (record-error 'name s "extraneous field initializers ~a"
152 #`(make-struct/no-tail type
153 #,@(map (lambda (field index)
154 (or (field-inherited-value field)
155 (if (innate-field? field)
157 field (field-default-value field))
158 #`(struct-ref #,orig-record
161 (iota (length '(expected ...))))))
163 (define (thunked-field? f)
164 (memq (syntax->datum f) 'thunked))
166 (define (delayed-field? f)
167 (memq (syntax->datum f) 'delayed))
169 (define (innate-field? f)
170 (memq (syntax->datum f) 'innate))
172 (define (wrap-field-value f value)
173 (cond ((thunked-field? f)
175 (syntax-parameterize ((#,this-identifier
186 (define default-values
187 ;; List of symbol/value tuples.
190 (list (syntax->datum f) v)))
193 (define (field-default-value f)
194 (car (assoc-ref default-values (syntax->datum f))))
196 (define (field-bindings field+value)
197 ;; Return field to value bindings, for use in 'let*' below.
198 (map (lambda (field+value)
199 (syntax-case field+value ()
202 #,(wrap-field-value #'field #'value)))))
205 (syntax-case s (inherit expected ...)
206 ((_ (inherit orig-record) (field value) (... ...))
207 #`(let* #,(field-bindings #'((field value) (... ...)))
208 #,(abi-check #'type abi-cookie)
209 #,(record-inheritance #'orig-record
210 #'((field value) (... ...)))))
211 ((_ (field value) (... ...))
212 (let ((fields (map syntax->datum #'(field (... ...)))))
213 (define (field-value f)
214 (or (find (lambda (x)
215 (eq? f (syntax->datum x)))
217 (wrap-field-value f (field-default-value f))))
219 ;; Pass S to make sure source location info is preserved.
220 (report-duplicate-field-specifier 'name s)
222 (let ((fields (append fields (map car default-values))))
223 (cond ((lset= eq? fields '(expected ...))
224 #`(let* #,(field-bindings
225 #'((field value) (... ...)))
226 #,(abi-check #'type abi-cookie)
227 (ctor #,@(map field-value '(expected ...)))))
228 ((pair? (lset-difference eq? fields
230 (record-error 'name s
231 "extraneous field initializers ~a"
232 (lset-difference eq? fields
235 (record-error 'name s
236 "missing field initializers ~a"
240 ((_ bindings (... ...))
241 ;; One of BINDINGS doesn't match the (field value) pattern.
242 ;; Report precisely which one is faulty, instead of letting the
243 ;; "source expression failed to match any pattern" error.
244 (report-invalid-field-specifier 'name
245 #'(bindings (... ...))
248 (define-syntax-rule (define-field-property-predicate predicate property)
249 "Define PREDICATE as a procedure that takes a syntax object and, when passed
250 a field specification, returns the field name if it has the given PROPERTY."
251 (define (predicate s)
252 (syntax-case s (property)
253 ((field (property values (... ...)) _ (... ...))
255 ((field _ properties (... ...))
256 (predicate #'(field properties (... ...))))
259 (define-syntax define-record-type*
261 "Define the given record type such that an additional \"syntactic
262 constructor\" is defined, which allows instances to be constructed with named
263 field initializers, à la SRFI-35, as well as default values. An example use
266 (define-record-type* <thing> thing make-thing
269 (name thing-name (default \"chbouib\"))
271 (default (current-output-port)) (thunked))
272 (loc thing-location (innate) (default (current-source-location))))
274 This example defines a macro 'thing' that can be used to instantiate records
279 (port (current-error-port)))
281 The value of 'name' or 'port' could as well be omitted, in which case the
282 default value specified in the 'define-record-type*' form is used:
286 The 'port' field is \"thunked\", meaning that calls like '(thing-port x)' will
287 actually compute the field's value in the current dynamic extent, which is
288 useful when referring to fluids in a field's value. Furthermore, that thunk
289 can access the record it belongs to via the 'this-thing' identifier.
291 A field can also be marked as \"delayed\" instead of \"thunked\", in which
292 case its value is effectively wrapped in a (delay …) form.
294 It is possible to copy an object 'x' created with 'thing' like this:
296 (thing (inherit x) (name \"bar\"))
298 This expression returns a new object equal to 'x' except for its 'name'
299 field and its 'loc' field---the latter is marked as \"innate\", so it is not
302 (define (field-default-value s)
303 (syntax-case s (default)
304 ((field (default val) _ ...)
305 (list #'field #'val))
306 ((field _ properties ...)
307 (field-default-value #'(field properties ...)))
310 (define-field-property-predicate delayed-field? delayed)
311 (define-field-property-predicate thunked-field? thunked)
312 (define-field-property-predicate innate-field? innate)
314 (define (wrapped-field? s)
315 (or (thunked-field? s) (delayed-field? s)))
317 (define (wrapped-field-accessor-name field)
318 ;; Return the name (an unhygienic syntax object) of the "real"
319 ;; getter for field, which is assumed to be a wrapped field.
320 (syntax-case field ()
321 ((field get properties ...)
322 (let* ((getter (syntax->datum #'get))
323 (real-getter (symbol-append '% getter '-real)))
324 (datum->syntax #'get real-getter)))))
326 (define (field-spec->srfi-9 field)
327 ;; Convert a field spec of our style to a SRFI-9 field spec of the
329 (syntax-case field ()
330 ((name get properties ...)
332 #,(if (wrapped-field? field)
333 (wrapped-field-accessor-name field)
336 (define (thunked-field-accessor-definition field)
337 ;; Return the real accessor for FIELD, which is assumed to be a
339 (syntax-case field ()
341 (with-syntax ((real-get (wrapped-field-accessor-name field)))
342 #'(define-inlinable (get x)
343 ;; The real value of that field is a thunk, so call it.
344 ((real-get x) x))))))
346 (define (delayed-field-accessor-definition field)
347 ;; Return the real accessor for FIELD, which is assumed to be a
349 (syntax-case field ()
351 (with-syntax ((real-get (wrapped-field-accessor-name field)))
352 #'(define-inlinable (get x)
353 ;; The real value of that field is a promise, so force it.
354 (force (real-get x)))))))
356 (define (compute-abi-cookie field-specs)
357 ;; Compute an "ABI cookie" for the given FIELD-SPECS. We use
358 ;; 'string-hash' because that's a better hash function that 'hash' on a
360 (syntax-case field-specs ()
361 (((field get properties ...) ...)
362 (string-hash (object->string
363 (syntax->datum #'((field properties ...) ...)))
365 (guile-3 (target-most-positive-fixnum))
366 (else most-positive-fixnum))))))
369 ((_ type syntactic-ctor ctor pred
371 (field get properties ...) ...)
372 (identifier? #'this-identifier)
373 (let* ((field-spec #'((field get properties ...) ...))
374 (thunked (filter-map thunked-field? field-spec))
375 (delayed (filter-map delayed-field? field-spec))
376 (innate (filter-map innate-field? field-spec))
377 (defaults (filter-map field-default-value
378 #'((field properties ...) ...)))
379 (cookie (compute-abi-cookie field-spec)))
380 (with-syntax (((field-spec* ...)
381 (map field-spec->srfi-9 field-spec))
382 ((thunked-field-accessor ...)
383 (filter-map (lambda (field)
384 (and (thunked-field? field)
385 (thunked-field-accessor-definition
388 ((delayed-field-accessor ...)
389 (filter-map (lambda (field)
390 (and (delayed-field? field)
391 (delayed-field-accessor-definition
395 (define-record-type type
399 (define #,(current-abi-identifier #'type)
402 #,@(if (free-identifier=? #'this-identifier #'this-record)
404 #'((define-syntax-parameter this-identifier
406 "Return the record being defined. This macro may
407 only be used in the context of the definition of a thunked field."
411 (syntax-violation 'this-identifier
412 "cannot be used outside \
413 of a record instantiation"
415 thunked-field-accessor ...
416 delayed-field-accessor ...
417 (make-syntactic-constructor type syntactic-ctor ctor
419 #:abi-cookie #,cookie
421 #:this-identifier #'this-identifier
424 #:defaults #,defaults)))))
425 ((_ type syntactic-ctor ctor pred
426 (field get properties ...) ...)
427 ;; When no 'this' identifier was specified, use 'this-record'.
428 #'(define-record-type* type syntactic-ctor ctor pred
430 (field get properties ...) ...)))))
432 (define* (alist->record alist make keys
433 #:optional (multiple-value-keys '()))
434 "Apply MAKE to the values associated with KEYS in ALIST. Items in KEYS that
435 are also in MULTIPLE-VALUE-KEYS are considered to occur possibly multiple
436 times in ALIST, and thus their value is a list."
437 (let ((args (map (lambda (key)
438 (if (member key multiple-value-keys)
439 (filter-map (match-lambda
441 (and (equal? k key) v)))
443 (assoc-ref alist key)))
447 (define (object->fields object fields port)
448 "Write OBJECT (typically a record) as a series of recutils-style fields to
449 PORT, according to FIELDS. FIELDS must be a list of field name/getter pairs."
450 (let loop ((fields fields))
454 (((field . get) rest ...)
455 (format port "~a: ~a~%" field (get object))
458 (define %recutils-field-charset
459 ;; Valid characters starting a recutils field.
460 ;; info "(recutils) Fields"
461 (char-set-union char-set:upper-case
465 (define (recutils->alist port)
466 "Read a recutils-style record from PORT and return it as a list of key/value
467 pairs. Stop upon an empty line (after consuming it) or EOF."
468 (let loop ((line (read-line port))
470 (cond ((eof-object? line)
474 (loop (read-line port) result) ; leading space: ignore it
475 (reverse result))) ; end-of-record marker
477 ;; Now check the first character of LINE, since that's what the
478 ;; recutils manual says is enough.
479 (let ((first (string-ref line 0)))
481 ((char-set-contains? %recutils-field-charset first)
482 (let* ((colon (string-index line #\:))
483 (field (string-take line colon))
484 (value (string-trim (string-drop line (+ 1 colon)))))
485 (loop (read-line port)
486 (alist-cons field value result))))
487 ((eqv? first #\#) ;info "(recutils) Comments"
488 (loop (read-line port) result))
489 ((eqv? first #\+) ;info "(recutils) Fields"
490 (let ((new-line (if (string-prefix? "+ " line)
492 (string-drop line 1))))
494 (((field . value) rest ...)
495 (loop (read-line port)
496 `((,field . ,(string-append value "\n" new-line))
499 (error "unmatched line" line))))))))
501 (define-syntax match-record
503 "Bind each FIELD of a RECORD of the given TYPE to it's FIELD name.
504 The current implementation does not support thunked and delayed fields."
505 ((_ record type (field fields ...) body ...)
506 (if (eq? (struct-vtable record) type)
507 ;; TODO compute indices and report wrong-field-name errors at
509 ;; TODO support thunked and delayed fields
510 (let ((field ((record-accessor type 'field) record)))
511 (match-record record type (fields ...) body ...))
512 (throw 'wrong-type-arg record)))
513 ((_ record type () body ...)
516 ;;; records.scm ends here