I'm experimenting with typed/racket to see if it can speed up some lengthy numerical calculations. As a first step, I tried to sum all the square roots up to a million, first in plain Racket:
(for/sum ([i 1000000])
(sqrt i)))
This takes around 130ms on my computer, which isn't setting any speed records. Using racket/flonum
, the performance is much improved:
(for/sum ([i 1000000])
(flsqrt (->fl i)))
This is down to 8ms, which is pretty good! I tried a few other languages that I know, and the only ones to beat this were C, and JS (when using floats, but not with bignums).
Next I wanted to see if typed/racket could yield similar gains, perhaps without explicit types at each step. (Ideally, it would also optimize through function calls.) Unfortunately, I've been unable to figure out a valid syntax for using for/sum
in typed/racket.
I see lots of "type-ann-maybe" in the documentation (and are they the same in either position?), but I just can't figure out how to make the compiler happy. The documentation for this function even says "All annotations are optional", yet my first example (above) in typed/racket yields "Type Checker: Error in macro expansion -- insufficient type information to typecheck. please add more type annotations".
When I try to add annotations to help it out, such as:
(for/sum : Flonum ([i : Integer 1000000])
(flsqrt (->fl i)))
I get:
Type Checker: type mismatch
expected: Flonum
given: Zero
I've tried every combination I can think of, of typed functions, casts, hints, and so on. No matter the error, it points to the first character of the for/sum
line, and gives the "context" as typed-racket/typecheck/tc-toplevel.rkt, which seems unhelpful. There's nothing in the error message (at least, to this vanilla Racket user) which suggests an obvious fix.
I did see a comment somewhere (I've forgotten where) that aggregation loops are somehow special in typed/racket, perhaps due to the possibility of an empty list (even though this list can clearly never be empty), and require extra annotation, but the comment didn't actually say what to do.
Is there any tutorial which teaches how to read typed/racket error messages? Or can someone suggest a way to make for/sum
work? I am lost here. Thanks!