Skip to content

Commit 9cc322b

Browse files
authored
Merge pull request #462 from herbie-fp/deprecate-bessel
Deprecate Bessel functions
2 parents aca6619 + 30c3bc4 commit 9cc322b

File tree

8 files changed

+174
-117
lines changed

8 files changed

+174
-117
lines changed

src/errors.rkt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(require "config.rkt")
33
(provide raise-herbie-error raise-herbie-syntax-error
44
raise-herbie-sampling-error raise-herbie-missing-error
5+
syntax->error-format-string
56
herbie-error->string herbie-error-url
67
(struct-out exn:fail:user:herbie)
78
(struct-out exn:fail:user:herbie:syntax)
@@ -42,20 +43,23 @@
4243
(format "https://herbie.uwplse.org/doc/~a/~a"
4344
*herbie-version* (exn:fail:user:herbie-url exn)))
4445

46+
(define (syntax->error-format-string stx)
47+
(define file
48+
(if (path? (syntax-source stx))
49+
(let-values ([(base name dir?) (split-path (syntax-source stx))])
50+
(path->string name))
51+
(syntax-source stx)))
52+
(format "~a:~a:~a: ~~a" file (or (syntax-line stx) "")
53+
(or (syntax-column stx) (syntax-position stx))))
54+
4555
(define (herbie-error->string err)
4656
(with-output-to-string
4757
(λ ()
4858
(match err
4959
[(exn:fail:user:herbie:syntax message marks url locations)
5060
(eprintf "~a\n" message)
5161
(for ([(stx message) (in-dict locations)])
52-
(define file
53-
(if (path? (syntax-source stx))
54-
(let-values ([(base name dir?) (split-path (syntax-source stx))])
55-
(path->string name))
56-
(syntax-source stx)))
57-
(eprintf " ~a:~a:~a: ~a\n" file (or (syntax-line stx) "")
58-
(or (syntax-column stx) (syntax-position stx)) message))
62+
(eprintf " ~a\n" (format (syntax->error-format-string stx) message)))
5963
(when url
6064
(eprintf "See <https://herbie.uwplse.org/doc/~a/~a> for more.\n" *herbie-version* url))]
6165
[(exn:fail:user:herbie message marks url)

src/reprs/binary32.rkt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,7 @@
115115
#`(begin
116116
(define fl-proc
117117
(get-ffi-obj '#,cname #f (_fun #,@(build-list num-args (λ (_) #'_float)) -> _float)
118-
(λ () (warn 'unsupported #:url "faq.html#native-ops"
119-
"native `~a` not supported on your system, disabling operator. ~a"
120-
'#,cname
121-
"Consider using :precision racket for Racket-only operators.")
122-
#f)))
118+
(λ () #f)))
123119
(when fl-proc
124120
(define-operator-impl (op #,name #,@(build-list num-args (λ (_) #'binary32))) binary32
125121
[fl fl-proc] [key value] ...))))]))

src/reprs/binary64.rkt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,9 @@
4747
[sym2-append (λ (x y) (string->symbol (string-append (symbol->string x) (symbol->string y))))]
4848
[name (sym2-append (syntax-e (car (syntax-e (cadr (syntax-e stx))))) '.f64)])
4949
#`(begin
50-
(define fl-proc
50+
(define fl-proc
5151
(get-ffi-obj 'op #f (_fun #,@(build-list num-args (λ (_) #'_double)) -> _double)
52-
(λ () (warn 'unsupported #:url "faq.html#native-ops"
53-
"native `~a` not supported on your system, disabling operator. ~a"
54-
'op
55-
"Consider using :precision racket for Racket-only operators.")
56-
#f)))
52+
(λ () #f)))
5753
(when fl-proc
5854
(define-operator-impl (op #,name #,@(build-list num-args (λ (_) #'binary64))) binary64
5955
[fl fl-proc] [key value] ...))))]))

src/reprs/fallback.rkt

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@
9494
[expm1 (from-bigfloat bfexpm1)]
9595
[fabs abs]
9696
[floor floor]
97-
[j0 (from-bigfloat bfbesj0)]
98-
[j1 (from-bigfloat bfbesj1)]
9997
[lgamma log-gamma]
10098
[log (no-complex log)]
10199
[log10 (no-complex (λ (x) (log x 10)))]
@@ -110,9 +108,7 @@
110108
[tan tan]
111109
[tanh tanh]
112110
[tgamma gamma]
113-
[trunc truncate]
114-
[y0 (from-bigfloat bfbesy0)]
115-
[y1 (from-bigfloat bfbesy1)])
111+
[trunc truncate])
116112

117113
(define-2ary-fallback-operators
118114
[+ +]
@@ -149,3 +145,36 @@
149145

150146
(define-operator-impl (>= >=.rkt racket racket) bool
151147
[fl >=])
148+
149+
;; Deprecated
150+
151+
;; copied from <herbie>/syntax/syntax.rkt
152+
(module hairy racket/base
153+
(require ffi/unsafe)
154+
(provide check-native-1ary-exists?)
155+
156+
(define (check-native-1ary-exists? op)
157+
(let ([f32-name (string->symbol (string-append (symbol->string op) "f"))])
158+
(or (get-ffi-obj op #f (_fun _double -> _double) (λ () #f))
159+
(get-ffi-obj f32-name #f (_fun _float -> _float) (λ () #f)))))
160+
)
161+
162+
(require (submod "." hairy))
163+
164+
; can't load these without native support
165+
166+
(when (check-native-1ary-exists? 'j0)
167+
(define-operator-impl (j0 j0.rkt racket) racket
168+
[fl (from-bigfloat bfbesj0)]))
169+
170+
(when (check-native-1ary-exists? 'j1)
171+
(define-operator-impl (j1 j1.rkt racket) racket
172+
[fl (from-bigfloat bfbesj1)]))
173+
174+
(when (check-native-1ary-exists? 'y0)
175+
(define-operator-impl (y0 y0.rkt racket) racket
176+
[fl (from-bigfloat bfbesy0)]))
177+
178+
(when (check-native-1ary-exists? 'y1)
179+
(define-operator-impl (y1 y1.rkt racket) racket
180+
[fl (from-bigfloat bfbesy1)]))

src/syntax/syntax-check.rkt

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,69 @@
44
(require "../common.rkt" "../conversions.rkt" "../errors.rkt" "../interface.rkt" "syntax.rkt")
55
(provide assert-program!)
66

7-
(define (check-expression* stx vars error!)
8-
(match stx
9-
[#`,(? number?) (void)]
10-
[#`,(? constant-operator?) (void)]
11-
[#`,(? variable? var)
12-
(unless (set-member? vars stx)
13-
(error! stx "Unknown variable ~a" var))]
14-
[#`(let* ((#,vars* #,vals) ...) #,body)
15-
(define bindings
16-
(for/fold ([vars vars]) ([var vars*] [val vals])
17-
(unless (identifier? var)
18-
(error! var "Invalid variable name ~a" var))
19-
(check-expression* val vars error!)
20-
(bound-id-set-union vars (immutable-bound-id-set (list var)))))
21-
(check-expression* body bindings error!)]
22-
[#`(let ((#,vars* #,vals) ...) #,body)
23-
;; These are unfolded by desugaring
24-
(for ([var vars*] [val vals])
25-
(unless (identifier? var)
26-
(error! var "Invalid variable name ~a" var))
27-
(check-expression* val vars error!))
28-
(check-expression* body (bound-id-set-union vars (immutable-bound-id-set vars*)) error!)]
29-
[#`(let #,varlist #,body)
30-
(error! stx "Invalid `let` expression variable list ~a" (syntax->datum varlist))
31-
(check-expression* body vars error!)]
32-
[#`(let #,args ...)
33-
(error! stx "Invalid `let` expression with ~a arguments (expects 2)" (length args))
34-
(unless (null? args) (check-expression* (last args) vars error!))]
35-
[#`(if #,cond #,ift #,iff)
36-
(check-expression* cond vars error!)
37-
(check-expression* ift vars error!)
38-
(check-expression* iff vars error!)]
39-
[#`(if #,args ...)
40-
(error! stx "Invalid `if` expression with ~a arguments (expects 3)" (length args))
41-
(unless (null? args) (check-expression* (last args) vars error!))]
42-
[#`(! #,props ... #,body)
43-
(check-properties* props '() body)
44-
(check-expression* body vars error!)]
45-
[#`(,(? (curry set-member? '(+ - * / and or = != < > <= >=))) #,args ...)
46-
;; These expand by associativity so we don't check the number of arguments
47-
(for ([arg args]) (check-expression* arg vars error!))]
48-
[#`(#,f-syntax #,args ...)
49-
(define f (syntax->datum f-syntax))
50-
(cond
51-
[(operator-exists? f)
7+
8+
9+
(define (check-expression* stx vars error! deprecated-ops)
10+
(let loop ([stx stx] [vars vars])
11+
(match stx
12+
[#`,(? number?) (void)]
13+
[#`,(? constant-operator?) (void)]
14+
[#`,(? variable? var)
15+
(unless (set-member? vars stx)
16+
(error! stx "Unknown variable ~a" var))]
17+
[#`(let* ((#,vars* #,vals) ...) #,body)
18+
(define bindings
19+
(for/fold ([vars vars]) ([var vars*] [val vals])
20+
(unless (identifier? var)
21+
(error! var "Invalid variable name ~a" var))
22+
(loop val vars)
23+
(bound-id-set-union vars (immutable-bound-id-set (list var)))))
24+
(loop body bindings)]
25+
[#`(let ((#,vars* #,vals) ...) #,body)
26+
;; These are unfolded by desugaring
27+
(for ([var vars*] [val vals])
28+
(unless (identifier? var)
29+
(error! var "Invalid variable name ~a" var))
30+
(loop val vars))
31+
(loop body (bound-id-set-union vars (immutable-bound-id-set vars*)))]
32+
[#`(let #,varlist #,body)
33+
(error! stx "Invalid `let` expression variable list ~a" (syntax->datum varlist))
34+
(loop body vars)]
35+
[#`(let #,args ...)
36+
(error! stx "Invalid `let` expression with ~a arguments (expects 2)" (length args))
37+
(unless (null? args) (loop (last args) vars))]
38+
[#`(if #,cond #,ift #,iff)
39+
(loop cond vars)
40+
(loop ift vars)
41+
(loop iff vars)]
42+
[#`(if #,args ...)
43+
(error! stx "Invalid `if` expression with ~a arguments (expects 3)" (length args))
44+
(unless (null? args) (loop (last args) vars))]
45+
[#`(! #,props ... #,body)
46+
(check-properties* props '() body deprecated-ops)
47+
(loop body vars)]
48+
[#`(,(? (curry set-member? '(+ - * / and or = != < > <= >=))) #,args ...)
49+
;; These expand by associativity so we don't check the number of arguments
50+
(for ([arg args]) (loop arg vars))]
51+
[#`(#,f-syntax #,args ...)
52+
(define f (syntax->datum f-syntax))
53+
(cond
54+
[(operator-exists? f)
5255
(define arity (length (real-operator-info f 'itype)))
5356
(unless (= arity (length args))
5457
(error! stx "Operator ~a given ~a arguments (expects ~a)"
55-
f (length args) arity))]
56-
[(hash-has-key? (*functions*) f)
58+
f (length args) arity))
59+
(when (operator-deprecated? f)
60+
(set-add! deprecated-ops f))]
61+
[(hash-has-key? (*functions*) f)
5762
(match-define (list vars _ _) (hash-ref (*functions*) f))
5863
(unless (= (length vars) (length args))
5964
(error! stx "Function ~a given ~a arguments (expects ~a)"
6065
f (length args) (length vars)))]
61-
[else
66+
[else
6267
(error! stx "Unknown operator ~a" f)])
63-
(for ([arg args]) (check-expression* arg vars error!))]
64-
[_ (error! stx "Unknown syntax ~a" (syntax->datum stx))]))
68+
(for ([arg args]) (loop arg vars))]
69+
[_ (error! stx "Unknown syntax ~a" (syntax->datum stx))])))
6570

6671
(define (check-property* prop error!)
6772
(unless (identifier? prop)
@@ -70,7 +75,7 @@
7075
(unless (equal? (substring name 0 1) ":")
7176
(error! prop "Invalid property name ~a" prop)))
7277

73-
(define (check-properties* props vars error!)
78+
(define (check-properties* props vars error! deprecated-ops)
7479
(define prop-dict
7580
(let loop ([props props] [out '()])
7681
(match props
@@ -108,10 +113,10 @@
108113
(error! cite "Invalid :cite ~a; must be a list" cite)))
109114

110115
(when (dict-has-key? prop-dict ':pre)
111-
(check-expression* (dict-ref prop-dict ':pre) vars error!))
116+
(check-expression* (dict-ref prop-dict ':pre) vars error! deprecated-ops))
112117

113118
(when (dict-has-key? prop-dict ':herbie-target)
114-
(check-expression* (dict-ref prop-dict ':herbie-target) vars error!))
119+
(check-expression* (dict-ref prop-dict ':herbie-target) vars error! deprecated-ops))
115120

116121
(when (dict-has-key? prop-dict ':herbie-conversions)
117122
(define conversion-stx (dict-ref prop-dict ':herbie-conversions))
@@ -138,8 +143,14 @@
138143
(error! stx "Argument ~a is not a variable name" var))
139144
(when (check-duplicate-identifier vars*)
140145
(error! stx "Duplicate argument name ~a" (check-duplicate-identifier vars*))))
141-
(check-properties* props (immutable-bound-id-set vars*) error!)
142-
(check-expression* body (immutable-bound-id-set vars*) error!))
146+
(define deprecated-ops (mutable-set))
147+
(check-properties* props (immutable-bound-id-set vars*) error! deprecated-ops)
148+
(check-expression* body (immutable-bound-id-set vars*) error! deprecated-ops)
149+
(for ([op (in-set deprecated-ops)])
150+
(define message
151+
(format (syntax->error-format-string stx)
152+
(format "operator `~a` is deprecated." op)))
153+
(warn 'deprecated #:url "faq.html#deprecated-ops" message)))
143154

144155
(define (check-fpcore* stx error!)
145156
(match stx
@@ -154,16 +165,6 @@
154165
[_
155166
(error! stx "Not an FPCore: ~a" stx)]))
156167

157-
(define (assert-expression! stx vars)
158-
(define errs
159-
(reap [sow]
160-
(define (error! stx fmt . args)
161-
(define args* (map (λ (x) (if (syntax? x) (syntax->datum x) x)) args))
162-
(sow (cons stx (apply format fmt args*))))
163-
(check-expression* stx vars error!)))
164-
(unless (null? errs)
165-
(raise-herbie-syntax-error "Invalid expression" #:locations errs)))
166-
167168
(define (assert-program! stx)
168169
(define errs
169170
(reap [sow]
@@ -172,7 +173,8 @@
172173
(sow (cons stx (apply format fmt args*))))
173174
(check-fpcore* stx error!)))
174175
(unless (null? errs)
175-
(raise-herbie-syntax-error "Invalid program" #:locations errs)))
176+
(raise-herbie-syntax-error "Invalid program" #:locations errs))
177+
(print-warnings))
176178

177179
;; testing FPCore format
178180
(module+ test

0 commit comments

Comments
 (0)