From 403611e75287ba5b4f4f9e1bdd285ec759120e53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20R=C3=A4tzel?= Date: Fri, 2 Aug 2024 19:48:01 +0000 Subject: [PATCH] Fix stability in core library List sorting functions + 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. --- .../src/ElmInteractiveCoreModules.elm | 25 +++++++------------ .../steps/634/expected-value.txt | 1 + .../elm-core-list/steps/634/submission.txt | 2 ++ .../steps/635/expected-value.txt | 1 + .../elm-core-list/steps/635/submission.txt | 2 ++ 5 files changed, 15 insertions(+), 16 deletions(-) create mode 100644 implement/test-and-train/elm-interactive-scenarios-core/elm-core-list/steps/634/expected-value.txt create mode 100644 implement/test-and-train/elm-interactive-scenarios-core/elm-core-list/steps/634/submission.txt create mode 100644 implement/test-and-train/elm-interactive-scenarios-core/elm-core-list/steps/635/expected-value.txt create mode 100644 implement/test-and-train/elm-interactive-scenarios-core/elm-core-list/steps/635/submission.txt diff --git a/implement/pine/ElmTime/compile-elm-program/src/ElmInteractiveCoreModules.elm b/implement/pine/ElmTime/compile-elm-program/src/ElmInteractiveCoreModules.elm index cad497d8c..29eeeb971 100644 --- a/implement/pine/ElmTime/compile-elm-program/src/ElmInteractiveCoreModules.elm +++ b/implement/pine/ElmTime/compile-elm-program/src/ElmInteractiveCoreModules.elm @@ -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 @@ -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 ] """ , """ diff --git a/implement/test-and-train/elm-interactive-scenarios-core/elm-core-list/steps/634/expected-value.txt b/implement/test-and-train/elm-interactive-scenarios-core/elm-core-list/steps/634/expected-value.txt new file mode 100644 index 000000000..74bf9e48c --- /dev/null +++ b/implement/test-and-train/elm-interactive-scenarios-core/elm-core-list/steps/634/expected-value.txt @@ -0,0 +1 @@ +[(17,[]),(1,["c"]),(13,["d"]),(11,["e"]),(0,["a","b"])] \ No newline at end of file diff --git a/implement/test-and-train/elm-interactive-scenarios-core/elm-core-list/steps/634/submission.txt b/implement/test-and-train/elm-interactive-scenarios-core/elm-core-list/steps/634/submission.txt new file mode 100644 index 000000000..98db89cb0 --- /dev/null +++ b/implement/test-and-train/elm-interactive-scenarios-core/elm-core-list/steps/634/submission.txt @@ -0,0 +1,2 @@ +List.sortBy (\( _, dependencies ) -> List.length dependencies) + [ (0, ["a","b"]), (1, ["c"]), (13, ["d"]), (17, []), (11, ["e"]) ] \ No newline at end of file diff --git a/implement/test-and-train/elm-interactive-scenarios-core/elm-core-list/steps/635/expected-value.txt b/implement/test-and-train/elm-interactive-scenarios-core/elm-core-list/steps/635/expected-value.txt new file mode 100644 index 000000000..1ac6a6b80 --- /dev/null +++ b/implement/test-and-train/elm-interactive-scenarios-core/elm-core-list/steps/635/expected-value.txt @@ -0,0 +1 @@ +[(23,[]),(17,[]),(1,["c"]),(13,["d"]),(11,["e"]),(0,["a","b"])] \ No newline at end of file diff --git a/implement/test-and-train/elm-interactive-scenarios-core/elm-core-list/steps/635/submission.txt b/implement/test-and-train/elm-interactive-scenarios-core/elm-core-list/steps/635/submission.txt new file mode 100644 index 000000000..313459ef8 --- /dev/null +++ b/implement/test-and-train/elm-interactive-scenarios-core/elm-core-list/steps/635/submission.txt @@ -0,0 +1,2 @@ +List.sortBy (\( _, dependencies ) -> List.length dependencies) + [ (23, []), (0, ["a","b"]), (1, ["c"]), (13, ["d"]), (17, []), (11, ["e"]) ] \ No newline at end of file