Rescript-compiler: Polymorphic comparison : lexical

Created on 10 Feb 2017  路  12Comments  路  Source: rescript-lang/rescript-compiler

Taken from #1167

The below code returns "true" in OCaml
but returns "false" in bucklescript

if [2;6;1;1;2;1;4;2;1] < [2;6;1;1;2;1;4;2;1;409] then
  print_string "true"  
else  
  print_string "false"
GOOD FIRST TASK GOOD FOR PR non-critical-bug

All 12 comments

So what is the semantic here? I don't really understand why [2;6;1;1;2;1;4;2;1] < [2;6;1;1;2;1;4;2;1;409] is true.

The length is shorter, and a 'none' value is less than a '409' value.

Yeah it is weird, I really do not like OCaml's polymorphic compare.

(WhereTF are Implicit Modules already? They could fix this in OCaml...)

  1. So the semantic is if we are poly-comparing two lists, we compare by their length first?
  2. That's another thing I was wondering, so what is a '409' value?
    Thanks.

That's another thing I was wondering, so what is a '409' value?

Just a random value added on to the length of the list.

But yeah, I'm pretty sure OCaml compares list length first before comparing contents, like 80% sure?

No. our implementation compares length first, the ocaml implementation does not...

No. our implementation compares length first, the ocaml implementation does not...

Ah, oh wait, I was reading it backwards! ^.^;

Wtf is OCaml's doing?!

_/me still hates OCaml's polymorphic compare, need implicit modules already..._

Sorry I got confused here, isn't it because OCaml compares the list lengths first the reason why it thinks [2;6;1;1;2;1;4;2;1] < [2;6;1;1;2;1;4;2;1;409] is true?

@dorafmon ah, it is a bug in https://github.com/bloomberg/bucklescript/blob/f8271f7fbcbb0b4eba8087617c6aa63022339b25/jscomp/runtime/caml_obj.ml#L157
when typeof a = 'number' we can not assume typeof b = 'number'

here in js 0 < [409,0] returns false, Obj.magic is dangerous even for experienced programmers : )

So could you explain a bit more? I still don't quite understand what is the polymorphic comparison doing here.

polymorphic comparison is unsafe comparison, its type signature is 'a -> 'a -> int, depending on runtime, the runtime will walk through each value and do a comparison.
Here the old implementation is buggy since it assumes that if the lhs is number, then the lhs is number which is wrong, since both number and variants like None will be compiled into number

Implicit modules would fix this. ;-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cknitt picture cknitt  路  3Comments

andares picture andares  路  5Comments

tanaka-de-silva picture tanaka-de-silva  路  5Comments

glennsl picture glennsl  路  3Comments

jordwalke picture jordwalke  路  4Comments