<commit_before>#lang scheme/base

(require (for-syntax scheme/base))

(provide prop:dict
         dict?

         dict-mutable?
         dict-can-remove-keys?
         dict-can-functional-set?
         
         dict-ref
         dict-set!
         dict-set
         dict-update!
         dict-update
         dict-remove!
         dict-remove
         dict-count
         dict-iterate-first
         dict-iterate-next
         dict-iterate-key
         dict-iterate-value
         
         dict-map
         dict-for-each

         in-dict
         in-dict-keys
         in-dict-values
         in-dict-pairs

         (rename-out [create-custom-hash make-custom-hash]
                     [create-immutable-custom-hash make-immutable-custom-hash])
         make-weak-custom-hash)

(define-values (prop:dict dict-struct? dict-struct-ref)
  (make-struct-type-property 'dict
                             (lambda (v info)
                               (unless (and 
                                        (vector? v)
                                        (= 10 (vector-length v))
                                        (let-values ([(ref set! set remove! remove count
                                                           iterate-first iterate-next
                                                           iterate-key iterate-value)
                                                      (vector->values v)])
                                          (and (procedure? ref)
                                               (and (procedure-arity-includes? ref 2)
                                                    (procedure-arity-includes? ref 3))
                                               (or (not set!)
                                                   (and (procedure? set!)
                                                        (procedure-arity-includes? set! 3)))
                                               (or (not set)
                                                   (and (procedure? set)
                                                        (procedure-arity-includes? set 3)))
                                               (or (not remove!)
                                                   (and (procedure? remove!)
                                                        (procedure-arity-includes? remove! 2)))
                                               (or (not remove)
                                                   (and (procedure? remove)
                                                        (procedure-arity-includes? remove 2)))
                                               (procedure? count)
                                               (procedure-arity-includes? count 1)
                                               (procedure? iterate-first)
                                               (procedure-arity-includes? iterate-first 1)
                                               (procedure? iterate-next)
                                               (procedure-arity-includes? iterate-next 2)
                                               (procedure? iterate-key)
                                               (procedure-arity-includes? iterate-key 2)
                                               (procedure? iterate-value)
                                               (procedure-arity-includes? iterate-value 2))))
                                 (raise-type-error 'prop:dict-guard
                                                   "vector of dict methods"
                                                   v))
                               v)))

(define (get-dict-ref v)
  (vector-ref v 0))
(define (get-dict-set! v)
  (vector-ref v 1))
(define (get-dict-set v)
  (vector-ref v 2))
(define (get-dict-remove! v)
  (vector-ref v 3))
(define (get-dict-remove v)
  (vector-ref v 4))
(define (get-dict-count v)
  (vector-ref v 5))
(define (get-dict-iterate-first v)
  (vector-ref v 6))
(define (get-dict-iterate-next v)
  (vector-ref v 7))
(define (get-dict-iterate-key v)
  (vector-ref v 8))
(define (get-dict-iterate-value v)
  (vector-ref v 9))

(define (assoc? v) 
  (and (list? v) (andmap pair? v)))

(define (dict? v)
  (or (hash? v)
      (vector? v)
      (assoc? v)
      (dict-struct? v)))

(define (dict-mutable? d)
  (if (dict? d)
      (or (and (or (hash? d)
                   (vector? d))
               (not (immutable? d)))
          (and (dict-struct? d)
               (get-dict-set! (dict-struct-ref d))
               #t))
      (raise-type-error 'dict-mutable? "dict" d)))

(define (dict-can-remove-keys? d)
  (if (dict? d)
      (or (hash? d)
          (assoc? d)
          (and (dict-struct? d)
               (or (get-dict-remove! (dict-struct-ref d))
                   (get-dict-remove (dict-struct-ref d)))
               #t))
      (raise-type-error 'dict-can-remove-keys? "dict" d)))

(define (dict-can-functional-set? d)
  (if (dict? d)
      (or (and (hash? d) (immutable? d))
          (assoc? d)
          (and (dict-struct? d)
               (get-dict-set (dict-struct-ref d))
               #t))
      (raise-type-error 'dict-can-functional-set? "dict" d)))

(define dict-ref
  (case-lambda
   [(d key)
    (cond
     [(hash? d) (hash-ref d key)]
     [(vector? d) (vector-ref d key)]
     [(assoc? d)
      (let ([a (assoc key d)])
        (if a
            (cdr a)
            (raise-mismatch-error 'dict-ref
                                  (format "no value for key: ~e in: "
                                          key)
                                  d)))]
     [(dict-struct? d)
      ((get-dict-ref (dict-struct-ref d)) d key)]
     [else
      (raise-type-error 'dict-ref 'dict 0 d key)])]
   [(d key default)
    (cond
     [(hash? d) (hash-ref d key default)]
     [(vector? d) (if (and (exact-nonnegative-integer? key)
                           (key . < . (vector-length d)))
                      (vector-ref d key)
                      (if (procedure? default)
                          (default)
                          default))]
     [(assoc? d)
      (let ([a (assoc key d)])
        (if a
            (cdr a)
            (if (procedure? default)
                (default)
                default)))]
     [(dict-struct? d)
      ((get-dict-ref (dict-struct-ref d)) d key default)]
     [else
      (raise-type-error 'dict-ref 'dict 0 d key default)])]))

(define (dict-set! d key val)
  (cond
   [(hash? d) (hash-set! d key val)]<commit_msg>fixed two uses of raise-type-error (PR 10208)<commit_after>#lang scheme/base

(require (for-syntax scheme/base))

(provide prop:dict
         dict?

         dict-mutable?
         dict-can-remove-keys?
         dict-can-functional-set?
         
         dict-ref
         dict-set!
         dict-set
         dict-update!
         dict-update
         dict-remove!
         dict-remove
         dict-count
         dict-iterate-first
         dict-iterate-next
         dict-iterate-key
         dict-iterate-value
         
         dict-map
         dict-for-each

         in-dict
         in-dict-keys
         in-dict-values
         in-dict-pairs

         (rename-out [create-custom-hash make-custom-hash]
                     [create-immutable-custom-hash make-immutable-custom-hash])
         make-weak-custom-hash)

(define-values (prop:dict dict-struct? dict-struct-ref)
  (make-struct-type-property 'dict
                             (lambda (v info)
                               (unless (and 
                                        (vector? v)
                                        (= 10 (vector-length v))
                                        (let-values ([(ref set! set remove! remove count
                                                           iterate-first iterate-next
                                                           iterate-key iterate-value)
                                                      (vector->values v)])
                                          (and (procedure? ref)
                                               (and (procedure-arity-includes? ref 2)
                                                    (procedure-arity-includes? ref 3))
                                               (or (not set!)
                                                   (and (procedure? set!)
                                                        (procedure-arity-includes? set! 3)))
                                               (or (not set)
                                                   (and (procedure? set)
                                                        (procedure-arity-includes? set 3)))
                                               (or (not remove!)
                                                   (and (procedure? remove!)
                                                        (procedure-arity-includes? remove! 2)))
                                               (or (not remove)
                                                   (and (procedure? remove)
                                                        (procedure-arity-includes? remove 2)))
                                               (procedure? count)
                                               (procedure-arity-includes? count 1)
                                               (procedure? iterate-first)
                                               (procedure-arity-includes? iterate-first 1)
                                               (procedure? iterate-next)
                                               (procedure-arity-includes? iterate-next 2)
                                               (procedure? iterate-key)
                                               (procedure-arity-includes? iterate-key 2)
                                               (procedure? iterate-value)
                                               (procedure-arity-includes? iterate-value 2))))
                                 (raise-type-error 'prop:dict-guard
                                                   "vector of dict methods"
                                                   v))
                               v)))

(define (get-dict-ref v)
  (vector-ref v 0))
(define (get-dict-set! v)
  (vector-ref v 1))
(define (get-dict-set v)
  (vector-ref v 2))
(define (get-dict-remove! v)
  (vector-ref v 3))
(define (get-dict-remove v)
  (vector-ref v 4))
(define (get-dict-count v)
  (vector-ref v 5))
(define (get-dict-iterate-first v)
  (vector-ref v 6))
(define (get-dict-iterate-next v)
  (vector-ref v 7))
(define (get-dict-iterate-key v)
  (vector-ref v 8))
(define (get-dict-iterate-value v)
  (vector-ref v 9))

(define (assoc? v) 
  (and (list? v) (andmap pair? v)))

(define (dict? v)
  (or (hash? v)
      (vector? v)
      (assoc? v)
      (dict-struct? v)))

(define (dict-mutable? d)
  (if (dict? d)
      (or (and (or (hash? d)
                   (vector? d))
               (not (immutable? d)))
          (and (dict-struct? d)
               (get-dict-set! (dict-struct-ref d))
               #t))
      (raise-type-error 'dict-mutable? "dict" d)))

(define (dict-can-remove-keys? d)
  (if (dict? d)
      (or (hash? d)
          (assoc? d)
          (and (dict-struct? d)
               (or (get-dict-remove! (dict-struct-ref d))
                   (get-dict-remove (dict-struct-ref d)))
               #t))
      (raise-type-error 'dict-can-remove-keys? "dict" d)))

(define (dict-can-functional-set? d)
  (if (dict? d)
      (or (and (hash? d) (immutable? d))
          (assoc? d)
          (and (dict-struct? d)
               (get-dict-set (dict-struct-ref d))
               #t))
      (raise-type-error 'dict-can-functional-set? "dict" d)))

(define dict-ref
  (case-lambda
   [(d key)
    (cond
     [(hash? d) (hash-ref d key)]
     [(vector? d) (vector-ref d key)]
     [(assoc? d)
      (let ([a (assoc key d)])
        (if a
            (cdr a)
            (raise-mismatch-error 'dict-ref
                                  (format "no value for key: ~e in: "
                                          key)
                                  d)))]
     [(dict-struct? d)
      ((get-dict-ref (dict-struct-ref d)) d key)]
     [else
      (raise-type-error 'dict-ref "dict" 0 d key)])]
   [(d key default)
    (cond
     [(hash? d) (hash-ref d key default)]
     [(vector? d) (if (and (exact-nonnegative-integer? key)
                           (key . < . (vector-length d)))
                      (vector-ref d key)
                      (if (procedure? default)
                          (default)
                          default))]
     [(assoc? d)
      (let ([a (assoc key d)])
        (if a
            (cdr a)
            (if (procedure? default)
                (default)
                default)))]
     [(dict-struct? d)
      ((get-dict-ref (dict-struct-ref d)) d key default)]
     [else
      (raise-type-error 'dict-ref "dict" 0 d key default)])]))

(define (dict-set! d key val)
  (cond
   [(hash? d) (hash-set! d key val)]
{'fraction_non_alphanumeric': 0.11543789441280591, 'fraction_numerical': 0.004232722795136217, 'mean_word_length': 0.9283276450511946, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 138, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '201795', 'n_tokens_mistral': 3418, 'n_tokens_neox': 3222, 'n_words': 855}