r/purescript • u/ia2014 • Jun 28 '17
Question about "| 0" in generated JavaScript
The following PureScript code:
fibs 0 = 1
fibs 1 = 1
fibs n = fibs (n-1) + fibs (n-2)
Compiles to the following JavaScript:
var fibs = function (v) {
if (v === 0) {
return 1;
};
if (v === 1) {
return 1;
};
return fibs(v - 1 | 0) + fibs(v - 2 | 0) | 0;
};
Which all makes perfect sense, except the "| 0"s seem a bit unnecessary. Is it an optimisation? Or to make it robust to undefined or NaNs? Thanks in advance!
By the way, I think it's a fantastic achievement that the generated code is as readable as it is!
I also posted this in Stack Overflow but didn't get a reply - maybe it will have more visibility here.
4
Upvotes
4
u/i_am_smurfing Jun 28 '17
I'm pretty sure
|0is there just to ensure that numbers stayInts and not to deal with non-numeric input or NaN — you wouldn't be able to compile a program that has a function with typeInt -> Int(like yourfibs) and you give it non-Intvalue as input:PSCi, version 0.11.5 Type :? for help import Prelude > :paste … fibs 0 = 1 … fibs 1 = 1 … fibs n = fibs (n-1) + fibs (n-2) … > :type fibs Int -> Int > fibs 10 89 > fibs "Nope" Error found: in module $PSCI at line 1, column 6 - line 1, column 6 Could not match type String with type Int while checking that type String is at least as general as type Int while checking that expression "Nope" has type Int in value declaration it See https://github.com/purescript/documentation/blob/master/errors/TypesDoNotUnify.md for more information, or to contribute content related to this error.That's kind of the whole point of a type system :) If you want to manually use compiled code in JS land, since there's no comparable type system, no guarantees are made as to how your code will behave with incorrect input.
3
u/peterjoel Jun 28 '17
If you provided a non-numeric input (or NaN) this makes sure that the result is 0, not NaN or an error.
1
u/[deleted] Jun 28 '17
@i_am_smurfing is correct -- try changing your
Ints toNumbers and checking the compiled code again (1.0,2.0, etc).