enums.scm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ;;; enums.scm --- The R6RS enumerations library
  2. ;; Copyright (C) 2010 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. (library (rnrs enums (6))
  18. (export make-enumeration enum-set-universe enum-set-indexer
  19. enum-set-constructor enum-set->list enum-set-member? enum-set-subset?
  20. enum-set=? enum-set-union enum-set-intersection enum-set-difference
  21. enum-set-complement enum-set-projection define-enumeration)
  22. (import (only (guile) and=>)
  23. (rnrs base (6))
  24. (rnrs conditions (6))
  25. (rnrs exceptions (6))
  26. (rnrs records procedural (6))
  27. (rnrs syntax-case (6))
  28. (srfi :1))
  29. (define enum-set-rtd (make-record-type-descriptor
  30. 'enum-set #f #f #f #f '#((mutable universe)
  31. (immutable set))))
  32. (define make-enum-set
  33. (record-constructor
  34. (make-record-constructor-descriptor enum-set-rtd #f #f)))
  35. (define enum-set-universe-internal (record-accessor enum-set-rtd 0))
  36. (define enum-set-universe-set! (record-mutator enum-set-rtd 0))
  37. (define enum-set-set (record-accessor enum-set-rtd 1))
  38. (define (make-enumeration symbol-list)
  39. (let ((es (make-enum-set #f symbol-list)))
  40. (enum-set-universe-set! es es)))
  41. (define (enum-set-universe enum-set)
  42. (or (enum-set-universe-internal enum-set)
  43. enum-set))
  44. (define (enum-set-indexer enum-set)
  45. (let* ((symbols (enum-set->list (enum-set-universe enum-set)))
  46. (cardinality (length symbols)))
  47. (lambda (x)
  48. (and=> (memq x symbols)
  49. (lambda (probe) (- cardinality (length probe)))))))
  50. (define (enum-set-constructor enum-set)
  51. (lambda (symbol-list)
  52. (make-enum-set (enum-set-universe enum-set)
  53. (list-copy symbol-list))))
  54. (define (enum-set->list enum-set)
  55. (lset-intersection eq?
  56. (enum-set-set (enum-set-universe enum-set))
  57. (enum-set-set enum-set)))
  58. (define (enum-set-member? symbol enum-set)
  59. (and (memq symbol (enum-set-set enum-set)) #t))
  60. (define (enum-set-subset? enum-set-1 enum-set-2)
  61. (and (lset<= eq?
  62. (enum-set-set (enum-set-universe enum-set-1))
  63. (enum-set-set (enum-set-universe enum-set-2)))
  64. (lset<= eq? (enum-set-set enum-set-1) (enum-set-set enum-set-2))))
  65. (define (enum-set=? enum-set-1 enum-set-2)
  66. (and (enum-set-subset? enum-set-1 enum-set-2)
  67. (enum-set-subset? enum-set-2 enum-set-1)))
  68. (define (enum-set-union enum-set-1 enum-set-2)
  69. (if (equal? (enum-set-universe enum-set-1)
  70. (enum-set-universe enum-set-2))
  71. (make-enum-set (enum-set-universe enum-set-1)
  72. (lset-union eq?
  73. (enum-set-set enum-set-1)
  74. (enum-set-set enum-set-2)))
  75. (raise (make-assertion-violation))))
  76. (define (enum-set-intersection enum-set-1 enum-set-2)
  77. (if (equal? (enum-set-universe enum-set-1)
  78. (enum-set-universe enum-set-2))
  79. (make-enum-set (enum-set-universe enum-set-1)
  80. (lset-intersection eq?
  81. (enum-set-set enum-set-1)
  82. (enum-set-set enum-set-2)))
  83. (raise (make-assertion-violation))))
  84. (define (enum-set-difference enum-set-1 enum-set-2)
  85. (if (equal? (enum-set-universe enum-set-1)
  86. (enum-set-universe enum-set-2))
  87. (make-enum-set (enum-set-universe enum-set-1)
  88. (lset-difference eq?
  89. (enum-set-set enum-set-1)
  90. (enum-set-set enum-set-2)))
  91. (raise (make-assertion-violation))))
  92. (define (enum-set-complement enum-set)
  93. (let ((universe (enum-set-universe enum-set)))
  94. (make-enum-set universe
  95. (lset-difference
  96. eq? (enum-set->list universe) (enum-set-set enum-set)))))
  97. (define (enum-set-projection enum-set-1 enum-set-2)
  98. (make-enum-set (enum-set-universe enum-set-2)
  99. (lset-intersection eq?
  100. (enum-set-set enum-set-1)
  101. (enum-set->list
  102. (enum-set-universe enum-set-2)))))
  103. (define-syntax define-enumeration
  104. (syntax-rules ()
  105. ((_ type-name (symbol ...) constructor-syntax)
  106. (begin
  107. (define-syntax type-name
  108. (lambda (s)
  109. (syntax-case s ()
  110. ((type-name sym)
  111. (if (memq (syntax->datum #'sym) '(symbol ...))
  112. #'(quote sym)
  113. (syntax-violation (symbol->string 'type-name)
  114. "not a member of the set"
  115. #f))))))
  116. (define-syntax constructor-syntax
  117. (lambda (s)
  118. (syntax-case s ()
  119. ((_ sym (... ...))
  120. (let* ((universe '(symbol ...))
  121. (syms (syntax->datum #'(sym (... ...))))
  122. (quoted-universe
  123. (datum->syntax s (list 'quote universe)))
  124. (quoted-syms (datum->syntax s (list 'quote syms))))
  125. (or (every (lambda (x) (memq x universe)) syms)
  126. (syntax-violation (symbol->string 'constructor-syntax)
  127. "not a subset of the universe"
  128. #f))
  129. #`((enum-set-constructor (make-enumeration #,quoted-universe))
  130. #,quoted-syms))))))))))
  131. )