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

Mutually defined functions are comparable in some circumstances. #9521

Closed
camlspotter opened this issue May 2, 2020 · 2 comments · Fixed by #9522
Closed

Mutually defined functions are comparable in some circumstances. #9521

camlspotter opened this issue May 2, 2020 · 2 comments · Fixed by #9522

Comments

@camlspotter
Copy link
Contributor

camlspotter commented May 2, 2020

A Tweet has reported a strange behaviour of OCaml structural comparison:
https://twitter.com/st_toHKR/status/1256270850434592770

Here is a simpler version:

$ 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)
@camlspotter 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
@gasche
Copy link
Member

gasche commented 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.

@gasche
Copy link
Member

gasche commented May 2, 2020

I found the issue in compare.c; I'll propose a PR and we will see whether there is consensus to consider this a bug and fix it.

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

Successfully merging a pull request may close this issue.

2 participants