srfi-9.scm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. ;;; srfi-9.scm --- define-record-type
  2. ;; Copyright (C) 2001, 2002, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 3 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;; Commentary:
  18. ;; This module exports the syntactic form `define-record-type', which
  19. ;; is the means for creating record types defined in SRFI-9.
  20. ;;
  21. ;; The syntax of a record type definition is:
  22. ;;
  23. ;; <record type definition>
  24. ;; -> (define-record-type <type name>
  25. ;; (<constructor name> <field tag> ...)
  26. ;; <predicate name>
  27. ;; <field spec> ...)
  28. ;;
  29. ;; <field spec> -> (<field tag> <accessor name>)
  30. ;; -> (<field tag> <accessor name> <modifier name>)
  31. ;;
  32. ;; <field tag> -> <identifier>
  33. ;; <... name> -> <identifier>
  34. ;;
  35. ;; Usage example:
  36. ;;
  37. ;; guile> (use-modules (srfi srfi-9))
  38. ;; guile> (define-record-type :foo (make-foo x) foo?
  39. ;; (x get-x) (y get-y set-y!))
  40. ;; guile> (define f (make-foo 1))
  41. ;; guile> f
  42. ;; #<:foo x: 1 y: #f>
  43. ;; guile> (get-x f)
  44. ;; 1
  45. ;; guile> (set-y! f 2)
  46. ;; 2
  47. ;; guile> (get-y f)
  48. ;; 2
  49. ;; guile> f
  50. ;; #<:foo x: 1 y: 2>
  51. ;; guile> (foo? f)
  52. ;; #t
  53. ;; guile> (foo? 1)
  54. ;; #f
  55. ;;; Code:
  56. (define-module (srfi srfi-9)
  57. #:use-module (srfi srfi-1)
  58. #:export (define-record-type))
  59. (cond-expand-provide (current-module) '(srfi-9))
  60. ;; Roll our own instead of using the public `define-inlinable'. This is
  61. ;; because the public one has a different `make-procedure-name', so
  62. ;; using it would require users to recompile code that uses SRFI-9. See
  63. ;; <http://lists.gnu.org/archive/html/guile-devel/2011-04/msg00111.html>.
  64. (define-syntax define-inlinable
  65. (lambda (x)
  66. (define (make-procedure-name name)
  67. (datum->syntax name
  68. (symbol-append '% (syntax->datum name)
  69. '-procedure)))
  70. (syntax-case x ()
  71. ((_ (name formals ...) body ...)
  72. (identifier? #'name)
  73. (with-syntax ((proc-name (make-procedure-name #'name))
  74. ((args ...) (generate-temporaries #'(formals ...))))
  75. #`(begin
  76. (define (proc-name formals ...)
  77. body ...)
  78. (define-syntax name
  79. (lambda (x)
  80. (syntax-case x ()
  81. ((_ args ...)
  82. #'((lambda (formals ...)
  83. body ...)
  84. args ...))
  85. (_
  86. (identifier? x)
  87. #'proc-name))))))))))
  88. (define (default-record-printer s p)
  89. (display "#<" p)
  90. (display (record-type-name (record-type-descriptor s)) p)
  91. (let loop ((fields (record-type-fields (record-type-descriptor s)))
  92. (off 0))
  93. (cond
  94. ((not (null? fields))
  95. (display " " p)
  96. (display (car fields) p)
  97. (display ": " p)
  98. (write (struct-ref s off) p)
  99. (loop (cdr fields) (+ 1 off)))))
  100. (display ">" p))
  101. (define-syntax define-record-type
  102. (lambda (x)
  103. (define (field-identifiers field-specs)
  104. (syntax-case field-specs ()
  105. (()
  106. '())
  107. ((field-spec)
  108. (syntax-case #'field-spec ()
  109. ((name accessor) #'(name))
  110. ((name accessor modifier) #'(name))))
  111. ((field-spec rest ...)
  112. (append (field-identifiers #'(field-spec))
  113. (field-identifiers #'(rest ...))))))
  114. (define (field-indices fields)
  115. (fold (lambda (field result)
  116. (let ((i (if (null? result)
  117. 0
  118. (+ 1 (cdar result)))))
  119. (alist-cons field i result)))
  120. '()
  121. fields))
  122. (define (constructor type-name constructor-spec indices)
  123. (syntax-case constructor-spec ()
  124. ((ctor field ...)
  125. (let ((field-count (length indices))
  126. (ctor-args (map (lambda (field)
  127. (cons (syntax->datum field) field))
  128. #'(field ...))))
  129. #`(define-inlinable #,constructor-spec
  130. (make-struct #,type-name 0
  131. #,@(unfold
  132. (lambda (field-num)
  133. (>= field-num field-count))
  134. (lambda (field-num)
  135. (let* ((name
  136. (car (find (lambda (f+i)
  137. (= (cdr f+i) field-num))
  138. indices)))
  139. (arg (assq name ctor-args)))
  140. (if (pair? arg)
  141. (cdr arg)
  142. #'#f)))
  143. 1+
  144. 0)))))))
  145. (define (accessors type-name field-specs indices)
  146. (syntax-case field-specs ()
  147. (()
  148. #'())
  149. ((field-spec)
  150. (syntax-case #'field-spec ()
  151. ((name accessor)
  152. (with-syntax ((index (assoc-ref indices (syntax->datum #'name))))
  153. #`((define-inlinable (accessor s)
  154. (if (eq? (struct-vtable s) #,type-name)
  155. (struct-ref s index)
  156. (throw 'wrong-type-arg 'accessor
  157. "Wrong type argument: ~S" (list s)
  158. (list s)))))))
  159. ((name accessor modifier)
  160. (with-syntax ((index (assoc-ref indices (syntax->datum #'name))))
  161. #`(#,@(accessors type-name #'((name accessor)) indices)
  162. (define-inlinable (modifier s val)
  163. (if (eq? (struct-vtable s) #,type-name)
  164. (struct-set! s index val)
  165. (throw 'wrong-type-arg 'modifier
  166. "Wrong type argument: ~S" (list s)
  167. (list s)))))))))
  168. ((field-spec rest ...)
  169. #`(#,@(accessors type-name #'(field-spec) indices)
  170. #,@(accessors type-name #'(rest ...) indices)))))
  171. (syntax-case x ()
  172. ((_ type-name constructor-spec predicate-name field-spec ...)
  173. (let* ((fields (field-identifiers #'(field-spec ...)))
  174. (field-count (length fields))
  175. (layout (string-concatenate (make-list field-count "pw")))
  176. (indices (field-indices (map syntax->datum fields))))
  177. #`(begin
  178. (define type-name
  179. (let ((rtd (make-struct/no-tail
  180. record-type-vtable
  181. '#,(datum->syntax #'here (make-struct-layout layout))
  182. default-record-printer
  183. 'type-name
  184. '#,fields)))
  185. (set-struct-vtable-name! rtd 'type-name)
  186. rtd))
  187. (define-inlinable (predicate-name obj)
  188. (and (struct? obj)
  189. (eq? (struct-vtable obj) type-name)))
  190. #,(constructor #'type-name #'constructor-spec indices)
  191. #,@(accessors #'type-name #'(field-spec ...) indices)))))))
  192. ;;; srfi-9.scm ends here