Skip to content

Fix IntBinarySet's comparison operators to be like BinarySetFn#308

Merged
JohnReppy merged 2 commits into
smlnj:mainfrom
DarinM223:fix_intbinaryset
Jul 17, 2025
Merged

Fix IntBinarySet's comparison operators to be like BinarySetFn#308
JohnReppy merged 2 commits into
smlnj:mainfrom
DarinM223:fix_intbinaryset

Conversation

@DarinM223

@DarinM223 DarinM223 commented May 13, 2025

Copy link
Copy Markdown
Contributor

Description

In split_gt, IntBinarySet's comparison operators are reversed compared to the BinarySetFn's case expression.

Related Issue

#310

Motivation and Context

For the function:

fun foo () =
  let
    val set1 = IntBinarySet.singleton 5356
    val () = print ("Set1: " ^ showSet set1 ^ "\n")
    val set2 = IntBinarySet.fromList [4986, 5360, 5361]
    val () = print ("Set2: " ^ showSet set2 ^ "\n")
    val result = IntBinarySet.union (set1, set2)
  in
    print ("Set: " ^ showSet result ^ "\n")
  end

results in:

> foo ();
Set1: [5356]
Set2: [4986, 5360, 5361]
Set: [4986, 5356, 4986, 5360, 5361]
val it = (): unit

This results in a set with multiple duplicate numbers added in it. Also intersection doesn't work as expected. Since the SML.NET compiler uses IntBinarySet for its sets of symbols, the result is that after adding symbols to the set through unions, the same symbol appears 4 to 5 times in the set but does not show up when calling intersection with a set with that symbol. This results in it hanging forever when translating valid programs to MIL.

How Has This Been Tested?

This program:

structure Main =
struct
  val showSet = fn t0 =>
    "["
    ^ String.concatWith ", " (List.map Int.toString (IntBinarySet.listItems t0))
    ^ "]"
  fun foo () =
    let
      val set1 = IntBinarySet.singleton 5356
      val () = print ("Set1: " ^ showSet set1 ^ "\n")
      val set2 = IntBinarySet.fromList [4986, 5360, 5361]
      val () = print ("Set2: " ^ showSet set2 ^ "\n")
      val result = IntBinarySet.union (set1, set2)
      val intersect = IntBinarySet.intersection (result, set2)
    in
      print ("Set: " ^ showSet result ^ "\n");
      print ("Intersection: " ^ showSet intersect ^ "\n")
    end
end

results in this afterwards:

- Main.foo ();
Set1: [5356]
Set2: [4986, 5360, 5361]
Set: [4986, 5356, 5360, 5361]
Intersection: [4986, 5360, 5361]
val it = () : unit

SML.NET no longer hangs forever when compiling the raytrace and primes demo programs.

This issue also appears to be in the legacy SML/NJ repository.

@JohnReppy JohnReppy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks good

@JohnReppy
JohnReppy merged commit 9cdd824 into smlnj:main Jul 17, 2025
@JohnReppy JohnReppy changed the title Fix IntBinarySet's comparison operators to be like BinarySetFn Fix IntBinarySet's comparison operators to be like BinarySetFn Jul 17, 2025
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 this pull request may close these issues.

2 participants