srfi-17.scm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ;;; srfi-17.scm --- Generalized set!
  2. ;; Copyright (C) 2001, 2002, 2005 Free Software Foundation, Inc.
  3. ;;
  4. ;; This program is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU General Public License as
  6. ;; published by the Free Software Foundation; either version 2, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; This program 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. ;; General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this software; see the file COPYING. If not, write to
  16. ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. ;; Boston, MA 02110-1301 USA
  18. ;;
  19. ;; As a special exception, the Free Software Foundation gives permission
  20. ;; for additional uses of the text contained in its release of GUILE.
  21. ;;
  22. ;; The exception is that, if you link the GUILE library with other files
  23. ;; to produce an executable, this does not by itself cause the
  24. ;; resulting executable to be covered by the GNU General Public License.
  25. ;; Your use of that executable is in no way restricted on account of
  26. ;; linking the GUILE library code into it.
  27. ;;
  28. ;; This exception does not however invalidate any other reasons why
  29. ;; the executable file might be covered by the GNU General Public License.
  30. ;;
  31. ;; This exception applies only to the code released by the
  32. ;; Free Software Foundation under the name GUILE. If you copy
  33. ;; code from other Free Software Foundation releases into a copy of
  34. ;; GUILE, as the General Public License permits, the exception does
  35. ;; not apply to the code that you add in this way. To avoid misleading
  36. ;; anyone as to the status of such modified files, you must delete
  37. ;; this exception notice from them.
  38. ;;
  39. ;; If you write modifications of your own for GUILE, it is your choice
  40. ;; whether to permit this exception to apply to your modifications.
  41. ;; If you do not wish that, delete this exception notice.
  42. ;;; Author: Matthias Koeppe <mkoeppe@mail.math.uni-magdeburg.de>
  43. ;;; Commentary:
  44. ;; This is an implementation of SRFI-17: Generalized set!
  45. ;;
  46. ;; It exports the Guile procedure `make-procedure-with-setter' under
  47. ;; the SRFI name `getter-with-setter' and exports the standard
  48. ;; procedures `car', `cdr', ..., `cdddr', `string-ref' and
  49. ;; `vector-ref' as procedures with setters, as required by the SRFI.
  50. ;;
  51. ;; SRFI-17 was heavily criticized during its discussion period but it
  52. ;; was finalized anyway. One issue was its concept of globally
  53. ;; associating setter "properties" with (procedure) values, which is
  54. ;; non-Schemy. For this reason, this implementation chooses not to
  55. ;; provide a way to set the setter of a procedure. In fact, (set!
  56. ;; (setter PROC) SETTER) signals an error. The only way to attach a
  57. ;; setter to a procedure is to create a new object (a "procedure with
  58. ;; setter") via the `getter-with-setter' procedure. This procedure is
  59. ;; also specified in the SRFI. Using it avoids the described
  60. ;; problems.
  61. ;;
  62. ;; This module is fully documented in the Guile Reference Manual.
  63. ;;; Code:
  64. (define-module (srfi srfi-17)
  65. :export (getter-with-setter))
  66. (cond-expand-provide (current-module) '(srfi-17))
  67. ;;; Procedures
  68. (define getter-with-setter make-procedure-with-setter)
  69. (define setter
  70. (getter-with-setter
  71. setter
  72. (lambda args
  73. (error "Setting setters is not supported for a good reason."))))
  74. ;;; Redefine R5RS procedures to appropriate procedures with setters
  75. (define (compose-setter setter location)
  76. (lambda (obj value)
  77. (setter (location obj) value)))
  78. (define car (getter-with-setter car set-car!))
  79. (define cdr (getter-with-setter cdr set-cdr!))
  80. (define caar (getter-with-setter caar (compose-setter set-car! car)))
  81. (define cadr (getter-with-setter cadr (compose-setter set-car! cdr)))
  82. (define cdar (getter-with-setter cdar (compose-setter set-cdr! car)))
  83. (define cddr (getter-with-setter cddr (compose-setter set-cdr! cdr)))
  84. (define caaar (getter-with-setter caaar (compose-setter set-car! caar)))
  85. (define caadr (getter-with-setter caadr (compose-setter set-car! cadr)))
  86. (define cadar (getter-with-setter cadar (compose-setter set-car! cdar)))
  87. (define caddr (getter-with-setter caddr (compose-setter set-car! cddr)))
  88. (define cdaar (getter-with-setter cdaar (compose-setter set-cdr! caar)))
  89. (define cdadr (getter-with-setter cdadr (compose-setter set-cdr! cadr)))
  90. (define cddar (getter-with-setter cddar (compose-setter set-cdr! cdar)))
  91. (define cdddr (getter-with-setter cdddr (compose-setter set-cdr! cddr)))
  92. (define caaaar (getter-with-setter caaaar (compose-setter set-car! caaar)))
  93. (define caaadr (getter-with-setter caaadr (compose-setter set-car! caadr)))
  94. (define caadar (getter-with-setter caadar (compose-setter set-car! cadar)))
  95. (define caaddr (getter-with-setter caaddr (compose-setter set-car! caddr)))
  96. (define cadaar (getter-with-setter cadaar (compose-setter set-car! cdaar)))
  97. (define cadadr (getter-with-setter cadadr (compose-setter set-car! cdadr)))
  98. (define caddar (getter-with-setter caddar (compose-setter set-car! cddar)))
  99. (define cadddr (getter-with-setter cadddr (compose-setter set-car! cdddr)))
  100. (define cdaaar (getter-with-setter cdaaar (compose-setter set-cdr! caaar)))
  101. (define cdaadr (getter-with-setter cdaadr (compose-setter set-cdr! caadr)))
  102. (define cdadar (getter-with-setter cdadar (compose-setter set-cdr! cadar)))
  103. (define cdaddr (getter-with-setter cdaddr (compose-setter set-cdr! caddr)))
  104. (define cddaar (getter-with-setter cddaar (compose-setter set-cdr! cdaar)))
  105. (define cddadr (getter-with-setter cddadr (compose-setter set-cdr! cdadr)))
  106. (define cdddar (getter-with-setter cdddar (compose-setter set-cdr! cddar)))
  107. (define cddddr (getter-with-setter cddddr (compose-setter set-cdr! cdddr)))
  108. (define string-ref (getter-with-setter string-ref string-set!))
  109. (define vector-ref (getter-with-setter vector-ref vector-set!))
  110. (export setter
  111. ;; redefined standard procedures
  112. car cdr caar cadr cdar cddr caaar caadr cadar caddr cdaar
  113. cdadr cddar cdddr caaaar caaadr caadar caaddr cadaar cadadr
  114. caddar cadddr cdaaar cdaadr cdadar cdaddr cddaar cddadr
  115. cdddar cddddr string-ref vector-ref)
  116. ;;; srfi-17.scm ends here