typed-racket icon indicating copy to clipboard operation
typed-racket copied to clipboard

`fl+` types shouldn't be binary

Open dannypsnl opened this issue 2 years ago • 3 comments

What version of Racket are you using?

v8.3[cs]

What program did you run?

#lang typed/racket
(require racket/flonum)
(fl+ 1. 1. 1.)

What should have happened?

It should work like the untyped version

#lang racket
(require racket/flonum)
(fl+ 1. 1. 1.)

If you got an error message, please include it here.

Type Checker: could not apply function;
 wrong number of arguments provided
  expected: 2
  given: 3
  in: (fl+ 1.0 1.0 1.0)

dannypsnl avatar Jan 05 '22 04:01 dannypsnl

I looked into this. Typed Racket has the ability to handle functions with uniform arity. Looking at flonum.rkt it looks like it's just exporting the functions provided in racket/flonum.rkt. I'm not sure how exactly it's annotating the types if it's just re-exporting the values, but If you are running into this issue you can manually annotate it as a function that takes 0 or more values

#lang typed/racket
(require/typed racket/flonum
               (fl+ (-> Flonum * Flonum )))
(fl+ 1. 1. 1.)
(fl+ 1. 1. 1. 1.)
(fl+)

Someone with more familiarity with the code base could chime in, but I think if you just do

(require/typed racket/flonum
               (fl+ (-> Flonum * Flonum ))
               (fl- (-> Flonum * Flonum ))
               ...)

in the typed-racket-more/typed/racket/flonum.rkt file that should fix everything.

diego-crespo avatar Apr 03 '22 16:04 diego-crespo

You can see the relevant types here: https://github.com/racket/typed-racket/blob/0236151e3b95d6d39276353cb5005197843e16e4/typed-racket-lib/typed-racket/base-env/base-env-numeric.rkt#L1961-L2007

samth avatar Apr 04 '22 18:04 samth

One nit: fl+ and fl* take 0 or more, while fl- and fl/ take 1 or more.

kengruven avatar Apr 27 '22 04:04 kengruven

I check into the code and find if I add the following line, then the code fixed

(->* (list -Fl -Fl) -Fl -Fl)

of course, this should not be the final solution at all, but it's good that it's able to fix.

File: typed-racket-lib/typed-racket/base-env/base-env-numeric.rkt

from

  (define fl+-type
    (fl-type-lambda
      (from-cases (map (lambda (t) (commutative-binop t -FlZero t))
                       ;; not all float types. singleton types are ruled out, since NaN can arise
                       (list -FlZero -FlNan -PosFl -NonNegFl
                             -NegFl -NonPosFl -Fl))
                  (commutative-binop -NonNegFl -PosFl -PosFl)
                  (map binop (list -NonNegFl -NegFl -NonPosFl -Fl)))))

to

  (define fl+-type
    (fl-type-lambda
      (from-cases (map (lambda (t) (commutative-binop t -FlZero t))
                       ;; not all float types. singleton types are ruled out, since NaN can arise
                       (list -FlZero -FlNan -PosFl -NonNegFl
                             -NegFl -NonPosFl -Fl))
                  (commutative-binop -NonNegFl -PosFl -PosFl)
                  (map binop (list -NonNegFl -NegFl -NonPosFl -Fl))
                  (->* (list -Fl -Fl) -Fl -Fl))))

dannypsnl avatar Nov 27 '22 11:11 dannypsnl