<commit_before>#lang racket

(require casio/simplify/enode)
(require casio/simplify/egraph)
(require casio/common)
(require casio/programs)

(provide match-e substitute-e)

;;################################################################################;;
;;# The matcher module that allows us to match enode structure against patterns,
;;# giving you the bindings between enodes and variables in the pattern, and
;;# substitute those bindings into patterns, creating new enodes.
;;#
;;# This is copy-pasted from the old egraphs file, with minor modifications,
;;# so it might not always work correctly.
;;#
;;################################################################################;;

(define (list-cartesian-product . lsts)
  (if (null? lsts)
      '(())
      (let ([tails (apply list-cartesian-product (cdr lsts))])
	(for*/list ([elt (car lsts)] [tail tails])
	  (cons elt tail)))))

(define (merge . bindings)
  ;; (list bindings) -> binding
  (foldl merge2 '() bindings))

(define (merge2 binding1 binding2)
  ;; binding binding -> binding
  (if (and binding1 binding2)
      (let loop ([acc binding1] [rest binding2])
	(if (null? rest)
	    acc
	    (let* ([curr (car rest)]
		   [lookup (assoc (car curr) acc)])
	      (if lookup
		  (if (equal? (cdr lookup) (cdr curr))
		      (loop acc (cdr rest))
		      #f)
		  (loop (cons curr acc) (cdr rest))))))
      #f))

(define (match-e pat e)
  (cond
   [(constant? pat)
    (call/ec
     (λ (k)
       (for ([var (enode-vars e)])
	 (when (and (number? var) (= pat var))
	   (k '(()))))
       '()))]
   [(symbol? pat)
    `(((,pat . ,e)))]
   [(list? pat)
    (apply append
	   (for/list ([var (enode-vars e)])
	     (if (and (list? var) (eq? (car var) (car pat))
		      (= (length var) (length pat)))
		 (filter identity
			 (map (curry apply merge)
			      (apply list-cartesian-product
				     (for/list ([subpat (cdr pat)] [sube (cdr var)])
				       (match-e subpat sube)))))
		 '())))]
   [else
    (error "WTF" pat)]))

(define (substitute-e eg pat bindings)
  (cond
   [(constant? pat)
    (mk-enode! eg pat)]
   [(symbol? pat)
    (let ([binden (cdr (assoc pat bindings))])
      binden)]
   [(list? pat)
    (mk-enode! eg (cons (car pat)
			(for/list ([subpat (cdr pat)])
			  (substitute-e eg subpat bindings))))]))<commit_msg>Type Annotataions For Performance<commit_after>#lang racket

(require casio/simplify/enode)
(require casio/simplify/egraph)
(require casio/common)
(require casio/programs)

(provide match-e substitute-e)

;;################################################################################;;
;;# The matcher module that allows us to match enode structure against patterns,
;;# giving you the bindings between enodes and variables in the pattern, and
;;# substitute those bindings into patterns, creating new enodes.
;;#
;;# This is copy-pasted from the old egraphs file, with minor modifications,
;;# so it might not always work correctly.
;;#
;;################################################################################;;

(define (list-cartesian-product . lsts)
  (if (null? lsts)
      '(())
      (let ([tails (apply list-cartesian-product (cdr lsts))])
	(for*/list ([elt (car lsts)] [tail tails])
	  (cons elt tail)))))

(define (merge . bindings)
  ;; (list bindings) -> binding
  (foldl merge2 '() bindings))

(define (merge2 binding1 binding2)
  ;; binding binding -> binding
  (if (and binding1 binding2)
      (let loop ([acc binding1] [rest binding2])
	(if (null? rest)
	    acc
	    (let* ([curr (car rest)]
		   [lookup (assoc (car curr) acc)])
	      (if lookup
		  (if (equal? (cdr lookup) (cdr curr))
		      (loop acc (cdr rest))
		      #f)
		  (loop (cons curr acc) (cdr rest))))))
      #f))

(define (match-e pat e)
  (cond
   [(constant? pat)
    (call/ec
     (λ (k)
       (for ([var (in-set (enode-vars e))])
	 (when (and (number? var) (= pat var))
	   (k '(()))))
       '()))]
   [(symbol? pat)
    `(((,pat . ,e)))]
   [(list? pat)
    (apply append
	   (for/list ([var (in-set (enode-vars e))])
	     (if (and (list? var) (eq? (car var) (car pat))
		      (= (length var) (length pat)))
		 (filter identity
			 (map (curry apply merge)
			      (apply list-cartesian-product
				     (for/list ([subpat (in-list (cdr pat))] [sube (in-list (cdr var))])
				       (match-e subpat sube)))))
		 '())))]
   [else
    (error "WTF" pat)]))

(define (substitute-e eg pat bindings)
  (cond
   [(constant? pat)
    (mk-enode! eg pat)]
   [(symbol? pat)
    (let ([binden (cdr (assoc pat bindings))])
      binden)]
   [(list? pat)
    (mk-enode! eg (cons (car pat)
			(for/list ([subpat (cdr pat)])
			  (substitute-e eg subpat bindings))))]))
{'fraction_non_alphanumeric': 0.23053892215568864, 'fraction_numerical': 0.003421727972626176, 'mean_word_length': 3.2441016333938295, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4978484', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1535, 'n_words': 498}