records: Report unknown field names in inheriting forms.
[jackhill/guix/guix.git] / guix / records.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
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)
24 #:use-module (ice-9 regex)
25 #:use-module (ice-9 rdelim)
26 #:export (define-record-type*
27 alist->record
28 object->fields
29 recutils->alist))
30
31 ;;; Commentary:
32 ;;;
33 ;;; Utilities for dealing with Scheme records.
34 ;;;
35 ;;; Code:
36
37 (define-syntax record-error
38 (syntax-rules ()
39 "Report a syntactic error in use of CONSTRUCTOR."
40 ((_ constructor form fmt args ...)
41 (syntax-violation constructor
42 (format #f fmt args ...)
43 form))))
44
45 (define-syntax define-record-type*
46 (lambda (s)
47 "Define the given record type such that an additional \"syntactic
48 constructor\" is defined, which allows instances to be constructed with named
49 field initializers, à la SRFI-35, as well as default values."
50 (define (make-syntactic-constructor type name ctor fields thunked defaults)
51 "Make the syntactic constructor NAME for TYPE, that calls CTOR, and
52 expects all of FIELDS to be initialized. DEFAULTS is the list of
53 FIELD/DEFAULT-VALUE tuples, and THUNKED is the list of identifiers of
54 thunked fields."
55 (with-syntax ((type type)
56 (name name)
57 (ctor ctor)
58 (expected fields)
59 (defaults defaults))
60 #`(define-syntax name
61 (lambda (s)
62 (define (record-inheritance orig-record field+value)
63 ;; Produce code that returns a record identical to
64 ;; ORIG-RECORD, except that values for the FIELD+VALUE alist
65 ;; prevail.
66 (define (field-inherited-value f)
67 (and=> (find (lambda (x)
68 (eq? f (car (syntax->datum x))))
69 field+value)
70 car))
71
72 ;; Make sure there are no unknown field names.
73 (let* ((fields (map (compose car syntax->datum)
74 field+value))
75 (unexpected (lset-difference eq? fields 'expected)))
76 (when (pair? unexpected)
77 (record-error 'name s "extraneous field initializers ~a"
78 unexpected)))
79
80 #`(make-struct type 0
81 #,@(map (lambda (field index)
82 (or (field-inherited-value field)
83 #`(struct-ref #,orig-record
84 #,index)))
85 'expected
86 (iota (length 'expected)))))
87
88 (define (thunked-field? f)
89 (memq (syntax->datum f) '#,thunked))
90
91 (define (field-bindings field+value)
92 ;; Return field to value bindings, for use in 'let*' below.
93 (map (lambda (field+value)
94 (syntax-case field+value ()
95 ((field value)
96 #`(field
97 #,(if (thunked-field? #'field)
98 #'(lambda () value)
99 #'value)))))
100 field+value))
101
102 (syntax-case s (inherit #,@fields)
103 ((_ (inherit orig-record) (field value) (... ...))
104 #`(let* #,(field-bindings #'((field value) (... ...)))
105 #,(record-inheritance #'orig-record
106 #'((field value) (... ...)))))
107 ((_ (field value) (... ...))
108 (let ((fields (map syntax->datum #'(field (... ...))))
109 (dflt (map (match-lambda
110 ((f v)
111 (list (syntax->datum f) v)))
112 #'defaults)))
113
114 (define (field-value f)
115 (or (and=> (find (lambda (x)
116 (eq? f (car (syntax->datum x))))
117 #'((field value) (... ...)))
118 car)
119 (let ((value
120 (car (assoc-ref dflt
121 (syntax->datum f)))))
122 (if (thunked-field? f)
123 #`(lambda () #,value)
124 value))))
125
126 (let ((fields (append fields (map car dflt))))
127 (cond ((lset= eq? fields 'expected)
128 #`(let* #,(field-bindings
129 #'((field value) (... ...)))
130 (ctor #,@(map field-value 'expected))))
131 ((pair? (lset-difference eq? fields 'expected))
132 (record-error 'name s
133 "extraneous field initializers ~a"
134 (lset-difference eq? fields
135 'expected)))
136 (else
137 (record-error 'name s
138 "missing field initializers ~a"
139 (lset-difference eq? 'expected
140 fields))))))))))))
141
142 (define (field-default-value s)
143 (syntax-case s (default)
144 ((field (default val) _ ...)
145 (list #'field #'val))
146 ((field _ options ...)
147 (field-default-value #'(field options ...)))
148 (_ #f)))
149
150 (define (thunked-field? s)
151 ;; Return the field name if the field defined by S is thunked.
152 (syntax-case s (thunked)
153 ((field (thunked) _ ...)
154 #'field)
155 ((field _ options ...)
156 (thunked-field? #'(field options ...)))
157 (_ #f)))
158
159 (define (thunked-field-accessor-name field)
160 ;; Return the name (an unhygienic syntax object) of the "real"
161 ;; getter for field, which is assumed to be a thunked field.
162 (syntax-case field ()
163 ((field get options ...)
164 (let* ((getter (syntax->datum #'get))
165 (real-getter (symbol-append '% getter '-real)))
166 (datum->syntax #'get real-getter)))))
167
168 (define (field-spec->srfi-9 field)
169 ;; Convert a field spec of our style to a SRFI-9 field spec of the
170 ;; form (field get).
171 (syntax-case field ()
172 ((name get options ...)
173 #`(name
174 #,(if (thunked-field? field)
175 (thunked-field-accessor-name field)
176 #'get)))))
177
178 (define (thunked-field-accessor-definition field)
179 ;; Return the real accessor for FIELD, which is assumed to be a
180 ;; thunked field.
181 (syntax-case field ()
182 ((name get _ ...)
183 (with-syntax ((real-get (thunked-field-accessor-name field)))
184 #'(define-inlinable (get x)
185 ;; The real value of that field is a thunk, so call it.
186 ((real-get x)))))))
187
188 (syntax-case s ()
189 ((_ type syntactic-ctor ctor pred
190 (field get options ...) ...)
191 (let* ((field-spec #'((field get options ...) ...)))
192 (with-syntax (((field-spec* ...)
193 (map field-spec->srfi-9 field-spec))
194 ((thunked-field-accessor ...)
195 (filter-map (lambda (field)
196 (and (thunked-field? field)
197 (thunked-field-accessor-definition
198 field)))
199 field-spec)))
200 #`(begin
201 (define-record-type type
202 (ctor field ...)
203 pred
204 field-spec* ...)
205 (begin thunked-field-accessor ...)
206 #,(make-syntactic-constructor #'type #'syntactic-ctor #'ctor
207 #'(field ...)
208 (filter-map thunked-field? field-spec)
209 (filter-map field-default-value
210 #'((field options ...)
211 ...))))))))))
212
213 (define* (alist->record alist make keys
214 #:optional (multiple-value-keys '()))
215 "Apply MAKE to the values associated with KEYS in ALIST. Items in KEYS that
216 are also in MULTIPLE-VALUE-KEYS are considered to occur possibly multiple
217 times in ALIST, and thus their value is a list."
218 (let ((args (map (lambda (key)
219 (if (member key multiple-value-keys)
220 (filter-map (match-lambda
221 ((k . v)
222 (and (equal? k key) v)))
223 alist)
224 (assoc-ref alist key)))
225 keys)))
226 (apply make args)))
227
228 (define (object->fields object fields port)
229 "Write OBJECT (typically a record) as a series of recutils-style fields to
230 PORT, according to FIELDS. FIELDS must be a list of field name/getter pairs."
231 (let loop ((fields fields))
232 (match fields
233 (()
234 object)
235 (((field . get) rest ...)
236 (format port "~a: ~a~%" field (get object))
237 (loop rest)))))
238
239 (define %recutils-field-rx
240 (make-regexp "^([[:graph:]]+): (.*)$"))
241
242 (define %recutils-comment-rx
243 ;; info "(recutils) Comments"
244 (make-regexp "^#"))
245
246 (define %recutils-plus-rx
247 (make-regexp "^\\+ ?(.*)$"))
248
249 (define (recutils->alist port)
250 "Read a recutils-style record from PORT and return it as a list of key/value
251 pairs. Stop upon an empty line (after consuming it) or EOF."
252 (let loop ((line (read-line port))
253 (result '()))
254 (cond ((eof-object? line)
255 (reverse result))
256 ((string-null? line)
257 (if (null? result)
258 (loop (read-line port) result) ; leading space: ignore it
259 (reverse result))) ; end-of-record marker
260 ((regexp-exec %recutils-comment-rx line)
261 (loop (read-line port) result))
262 ((regexp-exec %recutils-plus-rx line)
263 =>
264 (lambda (m)
265 (match result
266 (((field . value) rest ...)
267 (loop (read-line port)
268 `((,field . ,(string-append value "\n"
269 (match:substring m 1)))
270 ,@rest))))))
271 ((regexp-exec %recutils-field-rx line)
272 =>
273 (lambda (match)
274 (loop (read-line port)
275 (alist-cons (match:substring match 1)
276 (match:substring match 2)
277 result))))
278 (else
279 (error "unmatched line" line)))))
280
281 ;;; records.scm ends here