gnu: Add java-w3c-smil.
[jackhill/guix/guix.git] / guix / records.scm
CommitLineData
c0cd1b3e 1;;; GNU Guix --- Functional package management for GNU
7874bbbb 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
c0cd1b3e
LC
3;;;
4;;; This file is part of GNU Guix.
5;;;
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.
10;;;
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.
15;;;
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/>.
18
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)
fdc1bf65
LC
24 #:use-module (ice-9 regex)
25 #:use-module (ice-9 rdelim)
c0cd1b3e
LC
26 #:export (define-record-type*
27 alist->record
fdc1bf65 28 object->fields
6692d845 29 recutils->alist
30 match-record))
c0cd1b3e
LC
31
32;;; Commentary:
33;;;
34;;; Utilities for dealing with Scheme records.
35;;;
36;;; Code:
37
b1353e7a
LC
38(define-syntax record-error
39 (syntax-rules ()
40 "Report a syntactic error in use of CONSTRUCTOR."
41 ((_ constructor form fmt args ...)
42 (syntax-violation constructor
43 (format #f fmt args ...)
44 form))))
45
babc2c80
LC
46(define (report-invalid-field-specifier name bindings)
47 "Report the first invalid binding among BINDINGS."
48 (let loop ((bindings bindings))
49 (syntax-case bindings ()
50 (((field value) rest ...) ;good
51 (loop #'(rest ...)))
52 ((weird _ ...) ;weird!
53 (syntax-violation name "invalid field specifier" #'weird)))))
54
9768848a
LC
55(eval-when (expand load eval)
56 ;; The procedures below are needed both at run time and at expansion time.
57
58 (define (current-abi-identifier type)
59 "Return an identifier unhygienically derived from TYPE for use as its
7874bbbb 60\"current ABI\" variable."
9768848a
LC
61 (let ((type-name (syntax->datum type)))
62 (datum->syntax
63 type
64 (string->symbol
65 (string-append "% " (symbol->string type-name)
66 " abi-cookie")))))
7874bbbb 67
9768848a
LC
68 (define (abi-check type cookie)
69 "Return syntax that checks that the current \"application binary
7874bbbb 70interface\" (ABI) for TYPE is equal to COOKIE."
9768848a
LC
71 (with-syntax ((current-abi (current-abi-identifier type)))
72 #`(unless (eq? current-abi #,cookie)
de5cbd4a
LC
73 ;; The source file where this exception is thrown must be
74 ;; recompiled.
75 (throw 'record-abi-mismatch-error 'abi-check
76 "~a: record ABI mismatch; recompilation needed"
8e1395be 77 (list #,type) '())))))
7874bbbb 78
39fc041a
LC
79(define-syntax make-syntactic-constructor
80 (syntax-rules ()
81 "Make the syntactic constructor NAME for TYPE, that calls CTOR, and
82expects all of EXPECTED fields to be initialized. DEFAULTS is the list of
83FIELD/DEFAULT-VALUE tuples, THUNKED is the list of identifiers of thunked
7874bbbb
LC
84fields, and DELAYED is the list of identifiers of delayed fields.
85
86ABI-COOKIE is the cookie (an integer) against which to check the run-time ABI
87of TYPE matches the expansion-time ABI."
39fc041a 88 ((_ type name ctor (expected ...)
7874bbbb 89 #:abi-cookie abi-cookie
39fc041a
LC
90 #:thunked thunked
91 #:delayed delayed
8a16d064 92 #:innate innate
39fc041a
LC
93 #:defaults defaults)
94 (define-syntax name
95 (lambda (s)
96 (define (record-inheritance orig-record field+value)
97 ;; Produce code that returns a record identical to ORIG-RECORD,
98 ;; except that values for the FIELD+VALUE alist prevail.
99 (define (field-inherited-value f)
100 (and=> (find (lambda (x)
101 (eq? f (car (syntax->datum x))))
102 field+value)
103 car))
104
105 ;; Make sure there are no unknown field names.
106 (let* ((fields (map (compose car syntax->datum) field+value))
107 (unexpected (lset-difference eq? fields '(expected ...))))
108 (when (pair? unexpected)
109 (record-error 'name s "extraneous field initializers ~a"
110 unexpected)))
111
cea25b08 112 #`(make-struct/no-tail type
39fc041a
LC
113 #,@(map (lambda (field index)
114 (or (field-inherited-value field)
8a16d064
LC
115 (if (innate-field? field)
116 (wrap-field-value
117 field (field-default-value field))
118 #`(struct-ref #,orig-record
119 #,index))))
39fc041a
LC
120 '(expected ...)
121 (iota (length '(expected ...))))))
122
123 (define (thunked-field? f)
124 (memq (syntax->datum f) 'thunked))
125
126 (define (delayed-field? f)
127 (memq (syntax->datum f) 'delayed))
128
8a16d064
LC
129 (define (innate-field? f)
130 (memq (syntax->datum f) 'innate))
131
39fc041a
LC
132 (define (wrap-field-value f value)
133 (cond ((thunked-field? f)
134 #`(lambda () #,value))
135 ((delayed-field? f)
136 #`(delay #,value))
137 (else value)))
138
b9c86473
LC
139 (define default-values
140 ;; List of symbol/value tuples.
141 (map (match-lambda
142 ((f v)
143 (list (syntax->datum f) v)))
144 #'defaults))
145
146 (define (field-default-value f)
147 (car (assoc-ref default-values (syntax->datum f))))
148
39fc041a
LC
149 (define (field-bindings field+value)
150 ;; Return field to value bindings, for use in 'let*' below.
151 (map (lambda (field+value)
152 (syntax-case field+value ()
153 ((field value)
154 #`(field
155 #,(wrap-field-value #'field #'value)))))
156 field+value))
157
158 (syntax-case s (inherit expected ...)
159 ((_ (inherit orig-record) (field value) (... ...))
160 #`(let* #,(field-bindings #'((field value) (... ...)))
7874bbbb 161 #,(abi-check #'type abi-cookie)
39fc041a
LC
162 #,(record-inheritance #'orig-record
163 #'((field value) (... ...)))))
164 ((_ (field value) (... ...))
b9c86473 165 (let ((fields (map syntax->datum #'(field (... ...)))))
39fc041a 166 (define (field-value f)
94df39cc
LC
167 (or (find (lambda (x)
168 (eq? f (syntax->datum x)))
169 #'(field (... ...)))
b9c86473 170 (wrap-field-value f (field-default-value f))))
39fc041a 171
b9c86473 172 (let ((fields (append fields (map car default-values))))
39fc041a
LC
173 (cond ((lset= eq? fields '(expected ...))
174 #`(let* #,(field-bindings
175 #'((field value) (... ...)))
7874bbbb 176 #,(abi-check #'type abi-cookie)
39fc041a
LC
177 (ctor #,@(map field-value '(expected ...)))))
178 ((pair? (lset-difference eq? fields
179 '(expected ...)))
180 (record-error 'name s
181 "extraneous field initializers ~a"
182 (lset-difference eq? fields
183 '(expected ...))))
184 (else
185 (record-error 'name s
186 "missing field initializers ~a"
187 (lset-difference eq?
188 '(expected ...)
babc2c80
LC
189 fields)))))))
190 ((_ bindings (... ...))
191 ;; One of BINDINGS doesn't match the (field value) pattern.
192 ;; Report precisely which one is faulty, instead of letting the
193 ;; "source expression failed to match any pattern" error.
194 (report-invalid-field-specifier 'name
195 #'(bindings (... ...))))))))))
cf4efb39 196
faef3b6a
LC
197(define-syntax-rule (define-field-property-predicate predicate property)
198 "Define PREDICATE as a procedure that takes a syntax object and, when passed
199a field specification, returns the field name if it has the given PROPERTY."
200 (define (predicate s)
201 (syntax-case s (property)
202 ((field (property values (... ...)) _ (... ...))
203 #'field)
204 ((field _ properties (... ...))
205 (predicate #'(field properties (... ...))))
206 (_ #f))))
207
c0cd1b3e
LC
208(define-syntax define-record-type*
209 (lambda (s)
210 "Define the given record type such that an additional \"syntactic
211constructor\" is defined, which allows instances to be constructed with named
e2540884
LC
212field initializers, à la SRFI-35, as well as default values. An example use
213may look like this:
214
215 (define-record-type* <thing> thing make-thing
216 thing?
217 (name thing-name (default \"chbouib\"))
218 (port thing-port
8a16d064
LC
219 (default (current-output-port)) (thunked))
220 (loc thing-location (innate) (default (current-source-location))))
e2540884
LC
221
222This example defines a macro 'thing' that can be used to instantiate records
223of this type:
224
225 (thing
226 (name \"foo\")
227 (port (current-error-port)))
228
229The value of 'name' or 'port' could as well be omitted, in which case the
230default value specified in the 'define-record-type*' form is used:
231
232 (thing)
233
234The 'port' field is \"thunked\", meaning that calls like '(thing-port x)' will
235actually compute the field's value in the current dynamic extent, which is
236useful when referring to fluids in a field's value.
237
310b32a2
LC
238A field can also be marked as \"delayed\" instead of \"thunked\", in which
239case its value is effectively wrapped in a (delay …) form.
240
e2540884
LC
241It is possible to copy an object 'x' created with 'thing' like this:
242
243 (thing (inherit x) (name \"bar\"))
244
245This expression returns a new object equal to 'x' except for its 'name'
8a16d064
LC
246field and its 'loc' field---the latter is marked as \"innate\", so it is not
247inherited."
e2540884 248
c0cd1b3e
LC
249 (define (field-default-value s)
250 (syntax-case s (default)
251 ((field (default val) _ ...)
252 (list #'field #'val))
792798f4
LC
253 ((field _ properties ...)
254 (field-default-value #'(field properties ...)))
c0cd1b3e
LC
255 (_ #f)))
256
faef3b6a
LC
257 (define-field-property-predicate delayed-field? delayed)
258 (define-field-property-predicate thunked-field? thunked)
8a16d064 259 (define-field-property-predicate innate-field? innate)
c0cd1b3e 260
310b32a2
LC
261 (define (wrapped-field? s)
262 (or (thunked-field? s) (delayed-field? s)))
263
264 (define (wrapped-field-accessor-name field)
c0cd1b3e 265 ;; Return the name (an unhygienic syntax object) of the "real"
310b32a2 266 ;; getter for field, which is assumed to be a wrapped field.
c0cd1b3e 267 (syntax-case field ()
792798f4 268 ((field get properties ...)
c0cd1b3e
LC
269 (let* ((getter (syntax->datum #'get))
270 (real-getter (symbol-append '% getter '-real)))
271 (datum->syntax #'get real-getter)))))
272
273 (define (field-spec->srfi-9 field)
274 ;; Convert a field spec of our style to a SRFI-9 field spec of the
275 ;; form (field get).
276 (syntax-case field ()
792798f4 277 ((name get properties ...)
c0cd1b3e 278 #`(name
310b32a2
LC
279 #,(if (wrapped-field? field)
280 (wrapped-field-accessor-name field)
c0cd1b3e
LC
281 #'get)))))
282
283 (define (thunked-field-accessor-definition field)
284 ;; Return the real accessor for FIELD, which is assumed to be a
285 ;; thunked field.
286 (syntax-case field ()
287 ((name get _ ...)
310b32a2 288 (with-syntax ((real-get (wrapped-field-accessor-name field)))
c0cd1b3e
LC
289 #'(define-inlinable (get x)
290 ;; The real value of that field is a thunk, so call it.
291 ((real-get x)))))))
292
310b32a2
LC
293 (define (delayed-field-accessor-definition field)
294 ;; Return the real accessor for FIELD, which is assumed to be a
295 ;; delayed field.
296 (syntax-case field ()
297 ((name get _ ...)
298 (with-syntax ((real-get (wrapped-field-accessor-name field)))
299 #'(define-inlinable (get x)
300 ;; The real value of that field is a promise, so force it.
301 (force (real-get x)))))))
302
7874bbbb
LC
303 (define (compute-abi-cookie field-specs)
304 ;; Compute an "ABI cookie" for the given FIELD-SPECS. We use
305 ;; 'string-hash' because that's a better hash function that 'hash' on a
306 ;; list of symbols.
307 (syntax-case field-specs ()
308 (((field get properties ...) ...)
309 (string-hash (object->string
310 (syntax->datum #'((field properties ...) ...)))
311 most-positive-fixnum))))
312
c0cd1b3e
LC
313 (syntax-case s ()
314 ((_ type syntactic-ctor ctor pred
792798f4
LC
315 (field get properties ...) ...)
316 (let* ((field-spec #'((field get properties ...) ...))
9b543456 317 (thunked (filter-map thunked-field? field-spec))
310b32a2 318 (delayed (filter-map delayed-field? field-spec))
8a16d064 319 (innate (filter-map innate-field? field-spec))
9b543456 320 (defaults (filter-map field-default-value
7874bbbb
LC
321 #'((field properties ...) ...)))
322 (cookie (compute-abi-cookie field-spec)))
c0cd1b3e
LC
323 (with-syntax (((field-spec* ...)
324 (map field-spec->srfi-9 field-spec))
325 ((thunked-field-accessor ...)
326 (filter-map (lambda (field)
327 (and (thunked-field? field)
328 (thunked-field-accessor-definition
329 field)))
310b32a2
LC
330 field-spec))
331 ((delayed-field-accessor ...)
332 (filter-map (lambda (field)
333 (and (delayed-field? field)
334 (delayed-field-accessor-definition
335 field)))
c0cd1b3e
LC
336 field-spec)))
337 #`(begin
338 (define-record-type type
339 (ctor field ...)
340 pred
341 field-spec* ...)
7874bbbb
LC
342 (define #,(current-abi-identifier #'type)
343 #,cookie)
ad7c1a2c
LC
344 thunked-field-accessor ...
345 delayed-field-accessor ...
39fc041a
LC
346 (make-syntactic-constructor type syntactic-ctor ctor
347 (field ...)
7874bbbb 348 #:abi-cookie #,cookie
39fc041a
LC
349 #:thunked #,thunked
350 #:delayed #,delayed
8a16d064 351 #:innate #,innate
39fc041a 352 #:defaults #,defaults))))))))
c0cd1b3e 353
c8772a7a
LC
354(define* (alist->record alist make keys
355 #:optional (multiple-value-keys '()))
356 "Apply MAKE to the values associated with KEYS in ALIST. Items in KEYS that
357are also in MULTIPLE-VALUE-KEYS are considered to occur possibly multiple
358times in ALIST, and thus their value is a list."
359 (let ((args (map (lambda (key)
360 (if (member key multiple-value-keys)
361 (filter-map (match-lambda
362 ((k . v)
363 (and (equal? k key) v)))
364 alist)
365 (assoc-ref alist key)))
366 keys)))
c0cd1b3e
LC
367 (apply make args)))
368
369(define (object->fields object fields port)
370 "Write OBJECT (typically a record) as a series of recutils-style fields to
371PORT, according to FIELDS. FIELDS must be a list of field name/getter pairs."
372 (let loop ((fields fields))
373 (match fields
374 (()
375 object)
376 (((field . get) rest ...)
377 (format port "~a: ~a~%" field (get object))
378 (loop rest)))))
379
fb519bd8
LC
380(define %recutils-field-charset
381 ;; Valid characters starting a recutils field.
382 ;; info "(recutils) Fields"
383 (char-set-union char-set:upper-case
384 char-set:lower-case
385 (char-set #\%)))
836d10f1 386
fdc1bf65
LC
387(define (recutils->alist port)
388 "Read a recutils-style record from PORT and return it as a list of key/value
389pairs. Stop upon an empty line (after consuming it) or EOF."
390 (let loop ((line (read-line port))
391 (result '()))
b7b88288 392 (cond ((eof-object? line)
fdc1bf65 393 (reverse result))
b7b88288
LC
394 ((string-null? line)
395 (if (null? result)
396 (loop (read-line port) result) ; leading space: ignore it
397 (reverse result))) ; end-of-record marker
fdc1bf65 398 (else
fb519bd8
LC
399 ;; Now check the first character of LINE, since that's what the
400 ;; recutils manual says is enough.
401 (let ((first (string-ref line 0)))
402 (cond
403 ((char-set-contains? %recutils-field-charset first)
404 (let* ((colon (string-index line #\:))
405 (field (string-take line colon))
406 (value (string-trim (string-drop line (+ 1 colon)))))
407 (loop (read-line port)
408 (alist-cons field value result))))
409 ((eqv? first #\#) ;info "(recutils) Comments"
410 (loop (read-line port) result))
411 ((eqv? first #\+) ;info "(recutils) Fields"
412 (let ((new-line (if (string-prefix? "+ " line)
413 (string-drop line 2)
414 (string-drop line 1))))
415 (match result
416 (((field . value) rest ...)
417 (loop (read-line port)
418 `((,field . ,(string-append value "\n" new-line))
419 ,@rest))))))
420 (else
421 (error "unmatched line" line))))))))
fdc1bf65 422
6692d845 423(define-syntax match-record
424 (syntax-rules ()
425 "Bind each FIELD of a RECORD of the given TYPE to it's FIELD name.
426The current implementation does not support thunked and delayed fields."
427 ((_ record type (field fields ...) body ...)
428 (if (eq? (struct-vtable record) type)
429 ;; TODO compute indices and report wrong-field-name errors at
430 ;; expansion time
431 ;; TODO support thunked and delayed fields
432 (let ((field ((record-accessor type 'field) record)))
433 (match-record record type (fields ...) body ...))
434 (throw 'wrong-type-arg record)))
435 ((_ record type () body ...)
436 (begin body ...))))
437
c0cd1b3e 438;;; records.scm ends here