racket / typed-racket

Typed Racket
Other
521 stars 104 forks source link

type mismatch when using `Float`s with `for/sum` #1365

Closed citrusmunch closed 3 months ago

citrusmunch commented 6 months ago

What version of Racket are you using?

v8.12 [cs]

What program did you run?

mve

(for/sum : Float ([i : Float (in-range 0 1 0.1)]) i)

What should have happened?

same output as the following program

(for/sum : Real ([i : Real (in-range 0 1 0.1)]) i)

which returns 5.5.

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

string:1:0: Type Checker: type mismatch
  expected: Flonum
  given: Zero
  in: (for/sum : Float ((i : Float (in-range 0 1 0.1))) i)
 [,bt for context]

the backtrace, if it's useful :)

string:1:0: Type Checker: type mismatch
  expected: Flonum
  given: Zero
  in: (for/sum : Float ((i : Float (in-range 0 1 0.1))) i)
  location...:
   string:1:0
  context...:
   /Applications/Racket v8.12/share/pkgs/typed-racket-lib/typed-racket/typecheck/check-below.rkt:130:5: perform-check!
   /Applications/Racket v8.12/share/pkgs/typed-racket-lib/typed-racket/private/type-annotation.rkt:83:0: get-type/infer
   /Applications/Racket v8.12/share/pkgs/typed-racket-lib/typed-racket/typecheck/tc-let-unit.rkt:381:0: tc/let-values
   .../private/parse-interp.rkt:643:50
   /Applications/Racket v8.12/share/pkgs/typed-racket-lib/typed-racket/typecheck/tc-expr-unit.rkt:112:0: tc-expr/check
   /Applications/Racket v8.12/share/pkgs/typed-racket-lib/typed-racket/private/type-annotation.rkt:83:0: get-type/infer
   /Applications/Racket v8.12/share/pkgs/typed-racket-lib/typed-racket/typecheck/tc-let-unit.rkt:381:0: tc/let-values
   .../private/parse-interp.rkt:643:50
   /Applications/Racket v8.12/share/pkgs/typed-racket-lib/typed-racket/typecheck/tc-expr-unit.rkt:112:0: tc-expr/check
   [repeats 2 more times]
   .../private/parse-interp.rkt:643:50
   /Applications/Racket v8.12/share/pkgs/typed-racket-lib/typed-racket/typecheck/tc-expr-unit.rkt:112:0: tc-expr/check
   /Applications/Racket v8.12/share/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:724:0: tc-toplevel-form
   .../private/parse-interp.rkt:643:50
   /Applications/Racket v8.12/share/pkgs/xrepl-lib/xrepl/xrepl.rkt:1573:0
   /Applications/Racket v8.12/collects/racket/repl.rkt:11:26

from what I can tell, the literal zero in the definition seems to be the issue. The comment a little before seems to acknowledge this as intentional.

https://github.com/racket/typed-racket/blob/26534e7c8c27155cf1aa37e0390599bc5b56040e/typed-racket-lib/typed-racket/base-env/prims.rkt#L523-L528

https://github.com/racket/typed-racket/blob/26534e7c8c27155cf1aa37e0390599bc5b56040e/typed-racket-lib/typed-racket/base-env/prims.rkt#L553

My current workaround is to just use a foldl for now to avoid mangling Real and Float conversions in my actual program. Just making sure I'm not missing an obvious approach.

samth commented 6 months ago

Unfortunately this is the racket semantics for for/sum, so this is not fixable.

citrusmunch commented 3 months ago

Alrighty, thank you! I'll close this then.