You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$ ocaml
OCaml version 4.10.0
# let rec f x = x and g x = x in compare f g;;
- : int = -1
If we add another definition x = 1, the comparison fails as expected:
# let rec x = 1 and f x = x and g x = x in compare f g;;
Warning 26: unused variable x.
Exception: Invalid_argument "compare: functional value".
Raised by primitive operation at unknown location
Called from file "toplevel/toploop.ml", line 212, characters 17-27
The comparion of the first case unexpectedly succeeds because the second function does not have Tag_closure but Tag_infix. It seems that the second and later defined functions all have Tag_infix:
# let rec f x = x and g x = x and h x = x in
let obj_tag x = Obj.tag (Obj.repr x) in
(obj_tag f, obj_tag g, obj_tag h);;
- : int * int * int = (247, 249, 249)
This strangeness is gone if we have a non function definition in let rec:
# let rec f x = x and g x = x and x = 1 and h x = x in
let obj_tag x = Obj.tag (Obj.repr x) in
obj_tag f, obj_tag g, obj_tag h
;;
- : int * int * int = (247, 247, 247)
The text was updated successfully, but these errors were encountered:
camlspotter
changed the title
Mutually defined functions have Infix_tag instead of Closure_tag in some circumstances.
Mutually defined functions are comparable in some circumstances.
May 2, 2020
It is not clear to me that this is a bug. Yes, mutually-recursive functions use a specific comparison that uses Infix_tag, and the difference can be observed through Obj. If we consider comparison on function values undefined, then the behavior we observe is fine. It is a bug, however, if we consider that comparison of function values should always raise an error. Is this your assumption?
Two remarks:
Maybe the definition of runtime comparison operators on infix tags could be changed to achieve this (consistently raising) easily. In that case I think it would be nice to do the change.
There are discussions of getting rid of infix tags in the function representation in the future (to simplify the GC and in particular Multicore), so this behavior may change / solve itself.
A Tweet has reported a strange behaviour of OCaml structural comparison:
https://twitter.com/st_toHKR/status/1256270850434592770
Here is a simpler version:
If we add another definition
x = 1
, the comparison fails as expected:The comparion of the first case unexpectedly succeeds because the second function does not have
Tag_closure
butTag_infix
. It seems that the second and later defined functions all haveTag_infix
:This strangeness is gone if we have a non function definition in
let rec
:The text was updated successfully, but these errors were encountered: