Skip to content

Commit

Permalink
Fix stability in core library List sorting functions
Browse files Browse the repository at this point in the history
+ Add an automated test case for a scenario where we found `List.sortBy` violating the stability constraint.
+ Fix the list sorting function on the productive side to maintain stability.
  • Loading branch information
Viir committed Aug 2, 2024
1 parent 9e2de85 commit 403611e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -983,19 +983,12 @@ sortWith compareFunc list =
sortWithSplit : List a -> ( List a, List a )
sortWithSplit list =
case list of
[] ->
( [], [] )
[ x ] ->
( [ x ], [] )
x :: y :: rest ->
let
( left, right ) =
sortWithSplit rest
in
(Pine_kernel.concat [ [ x ], left], Pine_kernel.concat [ [ y ], right])
let
middleIndex = (Pine_kernel.length list) // 2
in
( Pine_kernel.take [ middleIndex, list ]
, Pine_kernel.skip [ middleIndex, list ]
)
sortWithMerge : List a -> List a -> (a -> a -> Order) -> List a
Expand All @@ -1009,11 +1002,11 @@ sortWithMerge left right compareFunc =
( x :: xs, y :: ys ) ->
case compareFunc x y of
LT ->
Pine_kernel.concat [ [ x ], sortWithMerge xs right compareFunc ]
GT ->
Pine_kernel.concat [ [ y ], sortWithMerge left ys compareFunc ]
_ ->
Pine_kernel.concat [ [ y ], sortWithMerge left ys compareFunc ]
Pine_kernel.concat [ [ x ], sortWithMerge xs right compareFunc ]
"""
, """
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[(17,[]),(1,["c"]),(13,["d"]),(11,["e"]),(0,["a","b"])]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
List.sortBy (\( _, dependencies ) -> List.length dependencies)
[ (0, ["a","b"]), (1, ["c"]), (13, ["d"]), (17, []), (11, ["e"]) ]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[(23,[]),(17,[]),(1,["c"]),(13,["d"]),(11,["e"]),(0,["a","b"])]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
List.sortBy (\( _, dependencies ) -> List.length dependencies)
[ (23, []), (0, ["a","b"]), (1, ["c"]), (13, ["d"]), (17, []), (11, ["e"]) ]

0 comments on commit 403611e

Please sign in to comment.