| 1 |
;;; |
| 2 |
;;; SRFI-225 - Dictionaries |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2022-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(define-module srfi.225 |
| 35 |
(use gauche.dictionary) |
| 36 |
(use util.match) |
| 37 |
(export dictionary? dict-empty? dict-contains? dict=? dict-pure? |
| 38 |
dict-ref dict-ref/default dict-comparator |
| 39 |
dict-set! dict-adjoin! dict-delete! dict-delete-all! |
| 40 |
dict-intern! dict-update! dict-update/default! |
| 41 |
dict-pop! dict-find-update! |
| 42 |
dict-map dict-filter dict-remove |
| 43 |
dict-size dict-count dict-any dict-every |
| 44 |
dict-keys dict-values dict-fold dict-map->list |
| 45 |
dict->alist dict-for-each dict->generator |
| 46 |
dict-set!-accumulator dict-adjoin!-accumulator |
| 47 |
|
| 48 |
dto? make-dto dto-ref ; make-alist-dto |
| 49 |
|
| 50 |
dictionary-error dictionary-error? |
| 51 |
dictionary-message dictionary-irritants |
| 52 |
|
| 53 |
dictionary?-id dict-find-update!-id dict-comaprator-id |
| 54 |
dict-map-id dict-pure?-id dict-remove-id dict-size-id dict->alist-id |
| 55 |
dict-adjoin!-accumulator-id dict-adjoin!-id dict-any-id |
| 56 |
dict-contains?-id dict-count-id dict-delete-all!-id dict-delete!-id |
| 57 |
dict-empty?-id dict-entries-id dict-every-id dict-filter-id |
| 58 |
dict-fold-id dict-for-each-id dict-intern!-id dict-keys-id |
| 59 |
dict-map->list-id dict-map-id dict-pop!-id dict-ref-id |
| 60 |
dict-ref/default-id dict-remove-id dict-replace!-id |
| 61 |
dict-set!-accumulator-id dict-set!-id dict-update!-id |
| 62 |
dict-update/default!-id dict-values-id dict=?-id dict->generator-id |
| 63 |
|
| 64 |
;; srrfi-69-dto hash-table-dto srfi-126-dto |
| 65 |
;; mapping-dto hash-mapping-dto |
| 66 |
;; eqv-alist-dto equal-alist-dto |
| 67 |
)) |
| 68 |
(select-module srfi.225) |
| 69 |
|
| 70 |
(define-syntax define-proc-ids |
| 71 |
(syntax-rules () |
| 72 |
[(_ *proc-ids* id ...) |
| 73 |
(begin |
| 74 |
(define-constant id 'id) ... |
| 75 |
(define-constant *proc-ids* '(id ...)))])) |
| 76 |
|
| 77 |
(define-proc-ids |
| 78 |
*proc-ids* |
| 79 |
dictionary?-id dict-find-update!-id dict-comaprator-id |
| 80 |
dict-map-id dict-pure?-id dict-remove-id dict-size-id dict->alist-id |
| 81 |
dict-adjoin!-accumulator-id dict-adjoin!-id dict-any-id |
| 82 |
dict-contains?-id dict-count-id dict-delete-all!-id dict-delete!-id |
| 83 |
dict-empty?-id dict-entries-id dict-every-id dict-filter-id |
| 84 |
dict-fold-id dict-for-each-id dict-intern!-id dict-keys-id |
| 85 |
dict-map->list-id dict-map-id dict-pop!-id dict-ref-id |
| 86 |
dict-ref/default-id dict-remove-id dict-replace!-id |
| 87 |
dict-set!-accumulator-id dict-set!-id dict-update!-id |
| 88 |
dict-update/default!-id dict-values-id dict=?-id dict->generator-id) |
| 89 |
|
| 90 |
(define-class <dto> () |
| 91 |
(;; minimum neccessary proc-ids |
| 92 |
(dictionary?-id :init-keyword :dictionary?-id) |
| 93 |
(dict-find-update!-id :init-keyword :dict-find-update!-id) |
| 94 |
(dict-comaprator-id :init-keyword :dict-comaprator-id) |
| 95 |
(dict-map-id :init-keyword :dict-map-id) |
| 96 |
(dict-pure?-id :init-keyword :dict-pure?-id) |
| 97 |
(dict-remove-id :init-keyword :dict-remove-id) |
| 98 |
(dict-size-id :init-keyword :dict-size-id) |
| 99 |
;; optional proc-ids |
| 100 |
(dict->alist-id :init-keyword :dict->alist-id) |
| 101 |
(dict-adjoin!-accumulator-id :init-keyword :dict-adjoin!-accumulator-id) |
| 102 |
(dict-adjoin!-id :init-keyword :dict-adjoin!-id) |
| 103 |
(dict-any-id :init-keyword :dict-any-id) |
| 104 |
(dict-contains?-id :init-keyword :dict-contains?-id) |
| 105 |
(dict-count-id :init-keyword :dict-count-id) |
| 106 |
(dict-delete-all!-id :init-keyword :dict-delete-all!-id) |
| 107 |
(dict-delete!-id :init-keyword :dict-delete!-id) |
| 108 |
(dict-empty?-id :init-keyword :dict-empty?-id) |
| 109 |
(dict-entries-id :init-keyword :dict-entries-id) |
| 110 |
(dict-every-id :init-keyword :dict-every-id) |
| 111 |
(dict-filter-id :init-keyword :dict-filter-id) |
| 112 |
(dict-fold-id :init-keyword :dict-fold-id) |
| 113 |
(dict-for-each-id :init-keyword :dict-for-each-id) |
| 114 |
(dict-intern!-id :init-keyword :dict-intern!-id) |
| 115 |
(dict-keys-id :init-keyword :dict-keys-id) |
| 116 |
(dict-map->list-id :init-keyword :dict-map->list-id) |
| 117 |
(dict-map-id :init-keyword :dict-map-id) |
| 118 |
(dict-pop!-id :init-keyword :dict-pop!-id) |
| 119 |
(dict-ref-id :init-keyword :dict-ref-id) |
| 120 |
(dict-ref/default-id :init-keyword :dict-ref/default-id) |
| 121 |
(dict-remove-id :init-keyword :dict-remove-id) |
| 122 |
(dict-replace!-id :init-keyword :dict-replace!-id) |
| 123 |
(dict-set!-accumulator-id :init-keyword :dict-set!-accumulator-id) |
| 124 |
(dict-set!-id :init-keyword :dict-set!-id) |
| 125 |
(dict-update!-id :init-keyword :dict-update!-id) |
| 126 |
(dict-update/default!-id :init-keyword :dict-update/default!-id) |
| 127 |
(dict-values-id :init-keyword :dict-values-id) |
| 128 |
(dict=?-id :init-keyword :dict=?-id) |
| 129 |
(dict->generator-id :init-keyword :dict->generator-id) |
| 130 |
;; Gauche extension |
| 131 |
(name :init-keyword :name))) |
| 132 |
|
| 133 |
;; API |
| 134 |
;; portable constructor |
| 135 |
;; TODO: |
| 136 |
;; - provide default procedures for optional keywords |
| 137 |
;; - check mandatory proc-ids |
| 138 |
(define (make-dto . args) |
| 139 |
(assume (even? (length args)) |
| 140 |
"Argument list must have even elements:" args) |
| 141 |
(let loop ([args args] [r '()]) |
| 142 |
(if (null? args) |
| 143 |
(apply make <dto> (reverse r)) |
| 144 |
(match args |
| 145 |
[(proc-id proc . rest) |
| 146 |
(unless (memq proc-id *proc-ids*) |
| 147 |
(error "Unknown proc-id:" proc-id)) |
| 148 |
(loop rest (cons* proc (make-keyword proc-id) r))])))) |
| 149 |
|
| 150 |
;; API |
| 151 |
(define (dto? obj) (is-a? obj <dto>)) |
| 152 |
|
| 153 |
;; API |
| 154 |
(define (dto-ref dto proc-id) |
| 155 |
(assume-type dto <dto>) |
| 156 |
;; TODO: If proc-id is invalid, we may issue better error message |
| 157 |
(slot-ref dto proc-id)) |
| 158 |
|
| 159 |
;; make-alist-dto |
| 160 |
|
| 161 |
(define-condition-type <dictionary-error> <error> |
| 162 |
dictionary-error? |
| 163 |
(message dictionary-message) |
| 164 |
(irritants dictionary-irritants)) |
| 165 |
|
| 166 |
(define (dictionary-error message . irritants) |
| 167 |
(make <dictionary-error> :message message :irritants irritants)) |
| 168 |
|
| 169 |
;; API |
| 170 |
(define (dictionary? dto obj) |
| 171 |
(assume-type dto <dto>) |
| 172 |
((~ dto'dictionary?-id) obj)) |
| 173 |
|
| 174 |
|
| 175 |
(define-inline (assume-dict dto dict) |
| 176 |
(unless ((~ dto'dictionary?-id) dict) |
| 177 |
(if-let1 name (~ dto'name) |
| 178 |
(errorf "Argument is not a supposed dictionary (~a): ~s" name dict) |
| 179 |
(errorf "Argument is not a supposed dictionary: ~s" dict)))) |
| 180 |
|
| 181 |
(define-syntax define-dict-op |
| 182 |
(er-macro-transformer |
| 183 |
(^[f r c] |
| 184 |
(define (collect-assumptions argspecs) |
| 185 |
(filter-map (match-lambda |
| 186 |
[(arg ':dict) (quasirename r `(assume-dict dto ,arg))] |
| 187 |
[(arg type) (quasirename r `(assume-type ,arg ,type))] |
| 188 |
[_ #f]) |
| 189 |
argspecs)) |
| 190 |
(define (collect-args argspecs) |
| 191 |
(map (match-lambda |
| 192 |
[(arg _) arg] |
| 193 |
[arg arg]) |
| 194 |
argspecs)) |
| 195 |
(match f |
| 196 |
[(_ name argspec ...) |
| 197 |
(let ([assumptions (collect-assumptions argspec)] |
| 198 |
[args (collect-args argspec)] |
| 199 |
[id (symbol-append name '-id)]) |
| 200 |
(quasirename r |
| 201 |
`(define (,name dto ,@args) |
| 202 |
(assume-type dto <dto>) |
| 203 |
,@assumptions |
| 204 |
((~ dto ',id) ,@args))))] |
| 205 |
[_ (error "Malformed define-dict-op:" f)])))) |
| 206 |
|
| 207 |
;; Generic APIs |
| 208 |
(define-dict-op dict-empty? (dict :dict)) |
| 209 |
(define-dict-op dict-contains? (dict :dict) key) |
| 210 |
(define-dict-op dict=? eq (dict1 :dict) (dict2 :dict)) |
| 211 |
(define-dict-op dict-pure? (dict :dict)) |
| 212 |
|
| 213 |
(define-dict-op dict-ref (dict :dict) key :optional failure success) |
| 214 |
(define-dict-op dict-ref/default (dict :dict) key default) |
| 215 |
(define-dict-op dict-comparator (dict :dict)) |
| 216 |
|
| 217 |
(define-dict-op dict-set! (dict :dict) :rest objs) |
| 218 |
(define-dict-op dict-adjoin! (dict :dict) :rest objs) |
| 219 |
(define-dict-op dict-delete! (dict :dict) :rest keys) |
| 220 |
(define-dict-op dict-delete-all! (dict :dict) keylist) |
| 221 |
(define-dict-op dict-replace! (dict :dict) key value) |
| 222 |
(define-dict-op dict-intern! (dict :dict) key failure) |
| 223 |
(define-dict-op dict-update! (dict :dict) key updater :optional failure success) |
| 224 |
(define-dict-op dict-update/default! (dict :dict) key updater default) |
| 225 |
(define-dict-op dict-pop! (dict :dict)) |
| 226 |
(define-dict-op dict-find-update! (dict :dict) key failure success) |
| 227 |
|
| 228 |
(define-dict-op dict-map proc (dict :dict)) |
| 229 |
(define-dict-op dict-filter pred (dict :dict)) |
| 230 |
(define-dict-op dict-remove pred (dict :dict)) |
| 231 |
|
| 232 |
(define-dict-op dict-size (dict :dict)) |
| 233 |
(define-dict-op dict-count pred (dict :dict)) |
| 234 |
(define-dict-op dict-any pred (dict :dict)) |
| 235 |
(define-dict-op dict-every pred (dict :dict)) |
| 236 |
(define-dict-op dict-keys (dict :dict)) |
| 237 |
(define-dict-op dict-values (dict :dict)) |
| 238 |
(define-dict-op dict-entries (dict :dict)) |
| 239 |
(define-dict-op dict-map->list proc (dict :dict)) |
| 240 |
(define-dict-op dict->alist (dict :dict)) |
| 241 |
|
| 242 |
(define-dict-op dict-for-each proc (dict :dict) :optional start end) |
| 243 |
(define-dict-op dict->generator (dict :dict) :optional start end) |
| 244 |
(define-dict-op dict-set!-accumulator (dict :dict)) |
| 245 |
(define-dict-op dict-adjoin!-accumulator (dict :dict)) |