Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type mismatch when using Floats with for/sum #1365

Open
citrusmunch opened this issue Mar 18, 2024 · 1 comment
Open

type mismatch when using Floats with for/sum #1365

citrusmunch opened this issue Mar 18, 2024 · 1 comment

Comments

@citrusmunch
Copy link

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.

;; ty has to include exact 0, exact 1, null (sum/product/list respectively),
;; the initial value of the accumulator
;; (to be consistent with Racket semantics).
;; We can't just change the initial value to be 0.0 if we expect a
;; Float result. This is problematic in some cases e.g:
;; (for/sum: : Float ([i : Float '(1.1)] #:when (zero? (random 1))) i)

(for*/sum: for*/fold: for*/sum #t + 0 #%expression)

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
Copy link
Sponsor Member

samth commented Mar 19, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants