srfi-9.scm 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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-syntax define-record-type
  89. (lambda (x)
  90. (define (field-identifiers field-specs)
  91. (syntax-case field-specs ()
  92. (()
  93. '())
  94. ((field-spec)
  95. (syntax-case #'field-spec ()
  96. ((name accessor) #'(name))
  97. ((name accessor modifier) #'(name))))
  98. ((field-spec rest ...)
  99. (append (field-identifiers #'(field-spec))
  100. (field-identifiers #'(rest ...))))))
  101. (define (field-indices fields)
  102. (fold (lambda (field result)
  103. (let ((i (if (null? result)
  104. 0
  105. (+ 1 (cdar result)))))
  106. (alist-cons field i result)))
  107. '()
  108. fields))
  109. (define (constructor type-name constructor-spec indices)
  110. (syntax-case constructor-spec ()
  111. ((ctor field ...)
  112. (let ((field-count (length indices))
  113. (ctor-args (map (lambda (field)
  114. (cons (syntax->datum field) field))
  115. #'(field ...))))
  116. #`(define-inlinable #,constructor-spec
  117. (make-struct #,type-name 0
  118. #,@(unfold
  119. (lambda (field-num)
  120. (>= field-num field-count))
  121. (lambda (field-num)
  122. (let* ((name
  123. (car (find (lambda (f+i)
  124. (= (cdr f+i) field-num))
  125. indices)))
  126. (arg (assq name ctor-args)))
  127. (if (pair? arg)
  128. (cdr arg)
  129. #'#f)))
  130. 1+
  131. 0)))))))
  132. (define (accessors type-name field-specs indices)
  133. (syntax-case field-specs ()
  134. (()
  135. #'())
  136. ((field-spec)
  137. (syntax-case #'field-spec ()
  138. ((name accessor)
  139. (with-syntax ((index (assoc-ref indices (syntax->datum #'name))))
  140. #`((define-inlinable (accessor s)
  141. (if (eq? (struct-vtable s) #,type-name)
  142. (struct-ref s index)
  143. (throw 'wrong-type-arg 'accessor
  144. "Wrong type argument: ~S" (list s)
  145. (list s)))))))
  146. ((name accessor modifier)
  147. (with-syntax ((index (assoc-ref indices (syntax->datum #'name))))
  148. #`(#,@(accessors type-name #'((name accessor)) indices)
  149. (define-inlinable (modifier s val)
  150. (if (eq? (struct-vtable s) #,type-name)
  151. (struct-set! s index val)
  152. (throw 'wrong-type-arg 'modifier
  153. "Wrong type argument: ~S" (list s)
  154. (list s)))))))))
  155. ((field-spec rest ...)
  156. #`(#,@(accessors type-name #'(field-spec) indices)
  157. #,@(accessors type-name #'(rest ...) indices)))))
  158. (syntax-case x ()
  159. ((_ type-name constructor-spec predicate-name field-spec ...)
  160. (let* ((fields (field-identifiers #'(field-spec ...)))
  161. (field-count (length fields))
  162. (layout (string-concatenate (make-list field-count "pw")))
  163. (indices (field-indices (map syntax->datum fields))))
  164. #`(begin
  165. (define type-name
  166. (make-vtable #,layout
  167. (lambda (obj port)
  168. (format port "#<~A" 'type-name)
  169. #,@(map (lambda (field)
  170. (let* ((f (syntax->datum field))
  171. (i (assoc-ref indices f)))
  172. #`(format port " ~A: ~S" '#,field
  173. (struct-ref obj #,i))))
  174. fields)
  175. (format port ">"))))
  176. (define-inlinable (predicate-name obj)
  177. (and (struct? obj)
  178. (eq? (struct-vtable obj) type-name)))
  179. #,(constructor #'type-name #'constructor-spec indices)
  180. #,@(accessors #'type-name #'(field-spec ...) indices)))))))
  181. ;;; srfi-9.scm ends here