<commit_before>(module match-internal-func mzscheme
  
  (provide (all-defined))
  
  (require-for-syntax "gen-match.ss"                      
                      "match-helper.ss"
                      "match-error.ss") 
  
  (require (lib "etc.ss")           
           (lib "list.ss")
           "match-expander.ss"
           "match-error.ss")
  
  
  (define-syntax (match stx)
    (syntax-case stx ()
      [(_ exp . clauses)       
       (with-syntax ([body (gen-match #'x #'clauses stx)])
         #`(let ([x exp]) body))]))
  
  (define-syntax (match-lambda stx)
    (syntax-case stx ()
      [(k . clauses)
       #'(lambda (exp) (match exp . clauses))]))
  
  (define-syntax (match-lambda* stx)
    (syntax-case stx ()
      [(k . clauses)
       #'(lambda exp (match exp . clauses))]))
  
  ;; there's lots of duplication here to handle named let
  ;; some factoring out would do a lot of good
  (define-syntax (match-let stx)
    (syntax-case stx ()
      ;; an empty body is an error
      [(_ nm (clauses ...))
       (identifier? #'nm)
       (match:syntax-err stx "bad syntax (empty body)")]
      [(_ (clauses ...)) (match:syntax-err stx "bad syntax (empty body)")]
      ;; with no bindings, there's nothing to do
      [(_ name () body ...) 
       (identifier? #'name)
       #'(let name () body ...)]
      [(_ () body ...) #'(let () body ...)]  
      ;; optimize the all-variable case            
      [(_ ([pat exp]...) body ...)
       (andmap pattern-var? (syntax->list #'(pat ...)))
       #'(let name ([pat exp] ...) body ...)]           
      [(_ name ([pat exp]...) body ...)
       (and (identifier? (syntax name))
            (andmap pattern-var? (syntax->list #'(pat ...))))
       #'(let name ([pat exp] ...) body ...)]
      ;; now the real cases
      [(_ name ([pat exp] ...) . body)
       #'(letrec ([name (match-lambda* ((list pat ...) . body))])                             
           (name exp ...))]        
      [(_ ([pat exp] ...) . body)
       #'(match (list exp ...) [(list pat ...) . body])]))
  
  (define-syntax (match-let* stx)
    (syntax-case stx ()
      [(_ (clauses ...)) (match:syntax-err stx "bad syntax (empty body)")]
      ((_ () body ...)
       #'(let* () body ...))
      ((_ ([pat exp] rest ...) body ...)
       (if (pattern-var? (syntax pat))
           #'(let ([pat exp])
               (match-let* (rest ...) body ...))
           #'(match exp [pat (match-let* (rest ...) body ...)]))
       )))
  
  (define-syntax (match-letrec stx)
    (syntax-case stx ()
      [(_ (clauses ...)) (match:syntax-err stx "bad syntax (empty body)")]
      [(_ ([pat exp] ...) . body)
       (andmap pattern-var?
               (syntax->list #'(pat ...)))
       #'(letrec ([pat exp] ...) . body)]
      [(_ ([pat exp] ...) . body)
       #'(let ()
           (match-define (list pat ...) (list exp ...))
           . body)]))
  
  (define-syntax (match-define stx)
    (syntax-case stx ()
      [(_ pat exp)
       (identifier? #'pat)
       #'(define pat exp)]
      [(_ pat exp)
       (let ([**match-bound-vars** '()])
         (with-syntax ([compiled-match
                        (gen-match #'the-exp                        
                                   #'((pat never-used))
                                   stx
                                   (lambda (sf bv)
                                     (set! **match-bound-vars** bv)
                                     (with-syntax ([((vars . vals) ...) (reverse bv)])
                                       #'(begin (set! vars vals) ...))))]
                       [(vars ...) (reverse **match-bound-vars**)])
           #'(begin
               (define vars #f) ...
               (let ([the-exp exp])
                 compiled-match))))]))
  )<commit_msg>Fix bug in definition of match-define.<commit_after>(module match-internal-func mzscheme
  
  (provide (all-defined))
  
  (require-for-syntax "gen-match.ss"                      
                      "match-helper.ss"
                      "match-error.ss") 
  
  (require (lib "etc.ss")           
           (lib "list.ss")
           "match-expander.ss"
           "match-error.ss")
  
  
  (define-syntax (match stx)
    (syntax-case stx ()
      [(_ exp . clauses)       
       (with-syntax ([body (gen-match #'x #'clauses stx)])
         #`(let ([x exp]) body))]))
  
  (define-syntax (match-lambda stx)
    (syntax-case stx ()
      [(k . clauses)
       #'(lambda (exp) (match exp . clauses))]))
  
  (define-syntax (match-lambda* stx)
    (syntax-case stx ()
      [(k . clauses)
       #'(lambda exp (match exp . clauses))]))
  
  ;; there's lots of duplication here to handle named let
  ;; some factoring out would do a lot of good
  (define-syntax (match-let stx)
    (syntax-case stx ()
      ;; an empty body is an error
      [(_ nm (clauses ...))
       (identifier? #'nm)
       (match:syntax-err stx "bad syntax (empty body)")]
      [(_ (clauses ...)) (match:syntax-err stx "bad syntax (empty body)")]
      ;; with no bindings, there's nothing to do
      [(_ name () body ...) 
       (identifier? #'name)
       #'(let name () body ...)]
      [(_ () body ...) #'(let () body ...)]  
      ;; optimize the all-variable case            
      [(_ ([pat exp]...) body ...)
       (andmap pattern-var? (syntax->list #'(pat ...)))
       #'(let name ([pat exp] ...) body ...)]           
      [(_ name ([pat exp]...) body ...)
       (and (identifier? (syntax name))
            (andmap pattern-var? (syntax->list #'(pat ...))))
       #'(let name ([pat exp] ...) body ...)]
      ;; now the real cases
      [(_ name ([pat exp] ...) . body)
       #'(letrec ([name (match-lambda* ((list pat ...) . body))])                             
           (name exp ...))]        
      [(_ ([pat exp] ...) . body)
       #'(match (list exp ...) [(list pat ...) . body])]))
  
  (define-syntax (match-let* stx)
    (syntax-case stx ()
      [(_ (clauses ...)) (match:syntax-err stx "bad syntax (empty body)")]
      ((_ () body ...)
       #'(let* () body ...))
      ((_ ([pat exp] rest ...) body ...)
       (if (pattern-var? (syntax pat))
           #'(let ([pat exp])
               (match-let* (rest ...) body ...))
           #'(match exp [pat (match-let* (rest ...) body ...)]))
       )))
  
  (define-syntax (match-letrec stx)
    (syntax-case stx ()
      [(_ (clauses ...)) (match:syntax-err stx "bad syntax (empty body)")]
      [(_ ([pat exp] ...) . body)
       (andmap pattern-var?
               (syntax->list #'(pat ...)))
       #'(letrec ([pat exp] ...) . body)]
      [(_ ([pat exp] ...) . body)
       #'(let ()
           (match-define (list pat ...) (list exp ...))
           . body)]))
  
  (define-syntax (match-define stx)
    (syntax-case stx ()
      [(_ pat exp)
       (identifier? #'pat)
       #'(define pat exp)]
      [(_ pat exp)
       (let ([**match-bound-vars** '()])
         (with-syntax ([compiled-match
                        (gen-match #'the-exp                        
                                   #'((pat never-used))
                                   stx
                                   (lambda (sf bv)
                                     (set! **match-bound-vars** bv)
                                     (with-syntax ([((vars . vals) ...) (reverse bv)])
                                       #'(begin (set! vars vals) ...))))]
                       [(vars ...) (map car (reverse **match-bound-vars**))])
           #'(begin
               (define vars #f) ...
               (let ([the-exp exp])
                 compiled-match))))]))
  )
{'fraction_non_alphanumeric': 0.1963416238978813, 'fraction_numerical': 0.0, 'mean_word_length': 1.3399014778325122, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '524478', 'n_tokens_mistral': 2466, 'n_tokens_neox': 2252, 'n_words': 597}