srfi-9.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. ;;; srfi-9.scm --- define-record-type
  2. ;; Copyright (C) 2001-2002, 2006, 2008-2014, 2018-2019, 2023
  3. ;; Free Software Foundation, Inc.
  4. ;;
  5. ;; This library is free software; you can redistribute it and/or
  6. ;; modify it under the terms of the GNU Lesser General Public
  7. ;; License as published by the Free Software Foundation; either
  8. ;; version 3 of the License, or (at your option) any later version.
  9. ;;
  10. ;; This library is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;; Lesser General Public License for more details.
  14. ;;
  15. ;; You should have received a copy of the GNU Lesser General Public
  16. ;; License along with this library; if not, write to the Free Software
  17. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;; Commentary:
  19. ;; This module exports the syntactic form `define-record-type', which
  20. ;; is the means for creating record types defined in SRFI-9.
  21. ;;
  22. ;; The syntax of a record type definition is:
  23. ;;
  24. ;; <record type definition>
  25. ;; -> (define-record-type <type name>
  26. ;; (<constructor name> <field tag> ...)
  27. ;; <predicate name>
  28. ;; <field spec> ...)
  29. ;;
  30. ;; <field spec> -> (<field tag> <getter name>)
  31. ;; -> (<field tag> <getter name> <setter name>)
  32. ;;
  33. ;; <field tag> -> <identifier>
  34. ;; <... name> -> <identifier>
  35. ;;
  36. ;; Usage example:
  37. ;;
  38. ;; guile> (use-modules (srfi srfi-9))
  39. ;; guile> (define-record-type :foo (make-foo x) foo?
  40. ;; (x get-x) (y get-y set-y!))
  41. ;; guile> (define f (make-foo 1))
  42. ;; guile> f
  43. ;; #<:foo x: 1 y: #f>
  44. ;; guile> (get-x f)
  45. ;; 1
  46. ;; guile> (set-y! f 2)
  47. ;; 2
  48. ;; guile> (get-y f)
  49. ;; 2
  50. ;; guile> f
  51. ;; #<:foo x: 1 y: 2>
  52. ;; guile> (foo? f)
  53. ;; #t
  54. ;; guile> (foo? 1)
  55. ;; #f
  56. ;;; Code:
  57. (define-module (srfi srfi-9)
  58. #:use-module (srfi srfi-1)
  59. #:use-module (system base ck)
  60. #:export (define-record-type))
  61. (cond-expand-provide (current-module) '(srfi-9))
  62. ;; Roll our own instead of using the public `define-inlinable'. This is
  63. ;; because the public one has a different `make-procedure-name', so
  64. ;; using it would require users to recompile code that uses SRFI-9. See
  65. ;; <http://lists.gnu.org/archive/html/guile-devel/2011-04/msg00111.html>.
  66. ;;
  67. (define-syntax-rule (define-inlinable (name formals ...) body ...)
  68. (define-tagged-inlinable () (name formals ...) body ...))
  69. ;; 'define-tagged-inlinable' has an additional feature: it stores a map
  70. ;; of keys to values that can be retrieved at expansion time. This is
  71. ;; currently used to retrieve the rtd id, field index, and record copier
  72. ;; macro for an arbitrary getter.
  73. (define-syntax-rule (%%on-error err) err)
  74. (define %%type #f) ; a private syntax literal
  75. (define-syntax getter-type
  76. (syntax-rules (quote)
  77. ((_ s 'getter 'err)
  78. (getter (%%on-error err) %%type s))))
  79. (define %%index #f) ; a private syntax literal
  80. (define-syntax getter-index
  81. (syntax-rules (quote)
  82. ((_ s 'getter 'err)
  83. (getter (%%on-error err) %%index s))))
  84. (define %%copier #f) ; a private syntax literal
  85. (define-syntax getter-copier
  86. (syntax-rules (quote)
  87. ((_ s 'getter 'err)
  88. (getter (%%on-error err) %%copier s))))
  89. (define-syntax define-tagged-inlinable
  90. (lambda (x)
  91. (define (make-procedure-name name)
  92. (datum->syntax name
  93. (symbol-append '% (syntax->datum name)
  94. '-procedure)))
  95. (syntax-case x ()
  96. ((_ ((key value) ...) (name formals ...) body0 body ...)
  97. (identifier? #'name)
  98. (with-syntax ((proc-name (make-procedure-name #'name))
  99. ((args ...) (generate-temporaries #'(formals ...))))
  100. #`(begin
  101. (define (proc-name formals ...)
  102. #((maybe-unused))
  103. body0 body ...)
  104. (define-syntax name
  105. (lambda (x)
  106. (syntax-case x (%%on-error key ...)
  107. ((_ (%%on-error err) key s) #'(ck s 'value)) ...
  108. ((_ args ...)
  109. #'((lambda (formals ...)
  110. body0 body ...)
  111. args ...))
  112. ((_ a (... ...))
  113. (syntax-violation 'name "Wrong number of arguments" x))
  114. (_
  115. (identifier? x)
  116. #'proc-name))))))))))
  117. (define (default-record-printer s p)
  118. (display "#<" p)
  119. (display (record-type-name (record-type-descriptor s)) p)
  120. (let loop ((fields (record-type-fields (record-type-descriptor s)))
  121. (off 0))
  122. (cond
  123. ((not (null? fields))
  124. (display " " p)
  125. (display (car fields) p)
  126. (display ": " p)
  127. (write (struct-ref s off) p)
  128. (loop (cdr fields) (+ 1 off)))))
  129. (display ">" p))
  130. (define-syntax-rule (throw-bad-struct s who)
  131. (let ((s* s))
  132. (throw 'wrong-type-arg who
  133. "Wrong type argument: ~S" (list s*)
  134. (list s*))))
  135. (define (make-copier-id type-name)
  136. (datum->syntax type-name
  137. (symbol-append '%% (syntax->datum type-name)
  138. '-set-fields)))
  139. (define-syntax %%set-fields
  140. (lambda (x)
  141. (syntax-case x ()
  142. ((_ type-name (getter-id ...) check? s (getter expr) ...)
  143. (every identifier? #'(getter ...))
  144. (let ((copier-name (syntax->datum (make-copier-id #'type-name)))
  145. (getter+exprs #'((getter expr) ...))
  146. (nfields (length #'(getter-id ...))))
  147. (define (lookup id default-expr)
  148. (let ((results
  149. (filter (lambda (g+e)
  150. (free-identifier=? id (car g+e)))
  151. getter+exprs)))
  152. (case (length results)
  153. ((0) default-expr)
  154. ((1) (cadar results))
  155. (else (syntax-violation
  156. copier-name "duplicate getter" x id)))))
  157. (for-each (lambda (id)
  158. (or (find (lambda (getter-id)
  159. (free-identifier=? id getter-id))
  160. #'(getter-id ...))
  161. (syntax-violation
  162. copier-name "unknown getter" x id)))
  163. #'(getter ...))
  164. (with-syntax ((unsafe-expr
  165. #`(make-struct/simple
  166. type-name
  167. #,@(map (lambda (getter index)
  168. (lookup getter #`(struct-ref s #,index)))
  169. #'(getter-id ...)
  170. (iota nfields)))))
  171. (if (syntax->datum #'check?)
  172. #`(if (eq? (struct-vtable s) type-name)
  173. unsafe-expr
  174. (throw-bad-struct
  175. s '#,(datum->syntax #'here copier-name)))
  176. #'unsafe-expr)))))))
  177. (define-syntax %define-record-type
  178. (lambda (x)
  179. (define (field-identifiers field-specs)
  180. (map (lambda (field-spec)
  181. (syntax-case field-spec ()
  182. ((name getter) #'name)
  183. ((name getter setter) #'name)))
  184. field-specs))
  185. (define (getter-identifiers field-specs)
  186. (map (lambda (field-spec)
  187. (syntax-case field-spec ()
  188. ((name getter) #'getter)
  189. ((name getter setter) #'getter)))
  190. field-specs))
  191. (define (constructor form type-name constructor-spec field-ids)
  192. (syntax-case constructor-spec ()
  193. ((ctor field ...)
  194. (every identifier? #'(field ...))
  195. (letrec* ((id-list-contains?
  196. (lambda (id-list id)
  197. (and (not (null? id-list))
  198. (or (free-identifier=? (car id-list) id)
  199. (id-list-contains? (cdr id-list) id)))))
  200. (inits (map (lambda (id)
  201. (and (id-list-contains? #'(field ...) id) id))
  202. field-ids)))
  203. (for-each
  204. (lambda (field)
  205. (unless (id-list-contains? field-ids field)
  206. (syntax-violation
  207. (syntax-case form () ((macro . args) (syntax->datum #'macro)))
  208. "unknown field in constructor spec"
  209. form field)))
  210. #'(field ...))
  211. #`(define-inlinable #,constructor-spec
  212. (make-struct/simple #,type-name #,@inits))))))
  213. (define (getters type-name getter-ids copier-id)
  214. (map (lambda (getter index)
  215. #`(define-tagged-inlinable
  216. ((%%type #,type-name)
  217. (%%index #,index)
  218. (%%copier #,copier-id))
  219. (#,getter s)
  220. (if (eq? (struct-vtable s) #,type-name)
  221. (struct-ref s #,index)
  222. (throw-bad-struct s '#,getter))))
  223. getter-ids
  224. (iota (length getter-ids))))
  225. (define (copier type-name getter-ids copier-id)
  226. #`(define-syntax-rule
  227. (#,copier-id check? s (getter expr) (... ...))
  228. (%%set-fields #,type-name #,getter-ids
  229. check? s (getter expr) (... ...))))
  230. (define (setters type-name field-specs)
  231. (filter-map (lambda (field-spec index)
  232. (syntax-case field-spec ()
  233. ((name getter) #f)
  234. ((name getter setter)
  235. #`(define-inlinable (setter s val)
  236. (if (eq? (struct-vtable s) #,type-name)
  237. (struct-set! s #,index val)
  238. (throw-bad-struct s 'setter))))))
  239. field-specs
  240. (iota (length field-specs))))
  241. (define (functional-setters copier-id field-specs)
  242. (filter-map (lambda (field-spec index)
  243. (syntax-case field-spec ()
  244. ((name getter) #f)
  245. ((name getter setter)
  246. #`(define-inlinable (setter s val)
  247. (#,copier-id #t s (getter val))))))
  248. field-specs
  249. (iota (length field-specs))))
  250. (define (record-layout immutable? count)
  251. ;; Mutability is expressed on the record level; all structs in the
  252. ;; future will be mutable.
  253. (string-concatenate (make-list count "pw")))
  254. (syntax-case x ()
  255. ((_ immutable? form type-name constructor-spec predicate-name
  256. field-spec ...)
  257. (let ()
  258. (define (syntax-error message subform)
  259. (syntax-violation (syntax-case #'form ()
  260. ((macro . args) (syntax->datum #'macro)))
  261. message #'form subform))
  262. (and (boolean? (syntax->datum #'immutable?))
  263. (or (identifier? #'type-name)
  264. (syntax-error "expected type name" #'type-name))
  265. (syntax-case #'constructor-spec ()
  266. ((ctor args ...)
  267. (every identifier? #'(ctor args ...))
  268. #t)
  269. (_ (syntax-error "invalid constructor spec"
  270. #'constructor-spec)))
  271. (or (identifier? #'predicate-name)
  272. (syntax-error "expected predicate name" #'predicate-name))
  273. (every (lambda (spec)
  274. (syntax-case spec ()
  275. ((field getter) #t)
  276. ((field getter setter) #t)
  277. (_ (syntax-error "invalid field spec" spec))))
  278. #'(field-spec ...))))
  279. (let* ((field-ids (field-identifiers #'(field-spec ...)))
  280. (getter-ids (getter-identifiers #'(field-spec ...)))
  281. (field-count (length field-ids))
  282. (immutable? (syntax->datum #'immutable?))
  283. (layout (record-layout immutable? field-count))
  284. (ctor-name (syntax-case #'constructor-spec ()
  285. ((ctor args ...) #'ctor)))
  286. (copier-id (make-copier-id #'type-name)))
  287. #`(begin
  288. #,(constructor #'form #'type-name #'constructor-spec field-ids)
  289. (define type-name
  290. (let ((rtd (make-record-type 'type-name
  291. '#,field-ids
  292. default-record-printer)))
  293. ;; Allow record-type-constructor to return our
  294. ;; constructor.
  295. (struct-set! rtd (+ 2 vtable-offset-user) #,ctor-name)
  296. rtd))
  297. (define-inlinable (predicate-name obj)
  298. (and (struct? obj)
  299. (eq? (struct-vtable obj) type-name)))
  300. #,@(getters #'type-name getter-ids copier-id)
  301. #,(copier #'type-name getter-ids copier-id)
  302. #,@(if immutable?
  303. (functional-setters copier-id #'(field-spec ...))
  304. (setters #'type-name #'(field-spec ...))))))
  305. ((_ immutable? form . rest)
  306. (syntax-violation
  307. (syntax-case #'form ()
  308. ((macro . args) (syntax->datum #'macro)))
  309. "invalid record definition syntax"
  310. #'form)))))
  311. (define-syntax-rule (define-record-type name ctor pred fields ...)
  312. (%define-record-type #f (define-record-type name ctor pred fields ...)
  313. name ctor pred fields ...))
  314. ;;; srfi-9.scm ends here