Skip to content

Commit

Permalink
Merge pull request #1139 from herbie-fp/autofix-13-1
Browse files Browse the repository at this point in the history
Automated Resyntax fixes
  • Loading branch information
pavpanchekha authored Jan 26, 2025
2 parents d6bc004 + 08418a7 commit c73a45c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
45 changes: 22 additions & 23 deletions src/core/localize.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,20 @@
(- a b))) ; This `if` statement handles `inf - inf`

; rank subexpressions by cost opportunity
(define localize-costss
(for/list ([subexprs (in-list subexprss)])
(sort (reap [sow]
(for ([subexpr (in-list subexprs)])
(match subexpr
[(? literal?) (void)]
[(? symbol?) (void)]
[(approx _ impl)
(define cost-opp (cost-opportunity subexpr (list impl)))
(sow (cons cost-opp subexpr))]
[(list _ args ...)
(define cost-opp (cost-opportunity subexpr args))
(sow (cons cost-opp subexpr))])))
>
#:key car)))

localize-costss)
(for/list ([subexprs (in-list subexprss)])
(sort (reap [sow]
(for ([subexpr (in-list subexprs)])
(match subexpr
[(? literal?) (void)]
[(? symbol?) (void)]
[(approx _ impl)
(define cost-opp (cost-opportunity subexpr (list impl)))
(sow (cons cost-opp subexpr))]
[(list _ args ...)
(define cost-opp (cost-opportunity subexpr args))
(sow (cons cost-opp subexpr))])))
>
#:key car)))

(define (batch-localize-errors exprs ctx)
(define subexprss (map all-subexpressions exprs))
Expand Down Expand Up @@ -364,12 +361,14 @@
(define exact-error (~s (translate-booleans (first (hash-ref data 'exact-values)))))
(define actual-error (~s (translate-booleans (first (hash-ref data 'approx-values)))))
(define percent-accurate
(if (nan? (first (hash-ref data 'absolute-error)))
'invalid ; HACK: should specify if invalid or unsamplable
(let* ([repr (repr-of expr ctx)]
[total-bits (representation-total-bits repr)]
[bits-error (ulps->bits (first (hash-ref data 'ulp-errs)))])
(* 100 (- 1 (/ bits-error total-bits))))))
(cond
[(nan? (first (hash-ref data 'absolute-error)))
'invalid] ; HACK: should specify if invalid or unsamplable
[else
(define repr (repr-of expr ctx))
(define total-bits (representation-total-bits repr))
(define bits-error (ulps->bits (first (hash-ref data 'ulp-errs))))
(* 100 (- 1 (/ bits-error total-bits)))]))
(hasheq 'ulps-error
ulp-error
'avg-error
Expand Down
15 changes: 8 additions & 7 deletions src/core/programs.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,13 @@
[else
(let loop ([a a]
[b b])
(if (null? a)
0
(let ([cmp (expr-cmp (car a) (car b))])
(if (zero? cmp)
(loop (cdr a) (cdr b))
cmp))))])]
(cond
[(null? a) 0]
[else
(define cmp (expr-cmp (car a) (car b)))
(if (zero? cmp)
(loop (cdr a) (cdr b))
cmp)]))])]
[((? list?) _) 1]
[(_ (? list?)) -1]
[((? approx?) (? approx?))
Expand All @@ -131,7 +132,7 @@
[else 1])]))

(define (expr<? a b)
(< (expr-cmp a b) 0))
(negative? (expr-cmp a b)))

;; Converting constants

Expand Down
10 changes: 6 additions & 4 deletions src/core/sampling.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,18 @@
(module+ test
(define rand-list
(let loop ([current 0])
(if (> current 200)
empty
(let ([r (+ current (random-integer 1 10))]) (cons r (loop r))))))
(cond
[(> current 200) empty]
[else
(define r (+ current (random-integer 1 10)))
(cons r (loop r))])))
(define arr (list->vector rand-list))
(for ([i (range 0 20)])
(define max-num (vector-ref arr (- (vector-length arr) 1)))
(define search-for (random-integer 0 max-num))
(define search-result (binary-search arr search-for))
(check-true (> (vector-ref arr search-result) search-for))
(when (> search-result 0)
(when (positive? search-result)
(check-true (<= (vector-ref arr (- search-result 1)) search-for)))))

(define (make-hyperrect-sampler hyperrects* hints* reprs)
Expand Down

0 comments on commit c73a45c

Please sign in to comment.