From cbfd19d895ff1bae1ac7b6bbaae6e30c7b528256 Mon Sep 17 00:00:00 2001 From: Stephen Compall Date: Mon, 31 Jan 2022 15:42:24 -0500 Subject: [PATCH] match Foldable.foldl1's argument order to DA.List.foldl1's order List.foldl1 uses the first argument as the accumulator, whereas Foldable.foldl1 uses the second argument. I believe the latter is an oversight (though the documentation doesn't suggest one way or the other), so swap them here. As discovered and reported by @gyorgybalazsi on the Daml Forum: https://discuss.daml.com/t/in-daml-foldl1-works-differently-from-haskell-is-this-intentional/3819 CHANGELOG_BEGIN - [Daml Standard Library] An argument order in the default implementation of ``Foldable.foldl1`` was reversed from that of ``DA.List.foldl1``; this incompatibly changes the former to match the latter. CHANGELOG_END --- .../damlc/daml-stdlib-src/DA/Foldable.daml | 2 +- .../damlc/tests/daml-test-files/List.daml | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/compiler/damlc/daml-stdlib-src/DA/Foldable.daml b/compiler/damlc/daml-stdlib-src/DA/Foldable.daml index 910bf6b7d30b..5f8de215ea42 100644 --- a/compiler/damlc/daml-stdlib-src/DA/Foldable.daml +++ b/compiler/damlc/daml-stdlib-src/DA/Foldable.daml @@ -50,7 +50,7 @@ class Foldable t where foldl1 : (a -> a -> a) -> t a -> a foldl1 f xs = O.fromSomeNote "foldl1: empty input" (foldl mf None xs) where - mf m x = Some (case m of None -> x; Some y -> f x y) + mf m x = Some (case m of None -> x; Some y -> f y x) -- | List of elements of a structure, from left to right. toList : t a -> [a] diff --git a/compiler/damlc/tests/daml-test-files/List.daml b/compiler/damlc/tests/daml-test-files/List.daml index 340820c31ce3..37c0f6555199 100644 --- a/compiler/damlc/tests/daml-test-files/List.daml +++ b/compiler/damlc/tests/daml-test-files/List.daml @@ -1,19 +1,20 @@ -- Copyright (c) 2020, Digital Asset (Switzerland) GmbH and/or its affiliates. -- All rights reserved. --- @INFO range=127:17-127:32; Use dedup --- @INFO range=313:10-313:24; Evaluate --- @INFO range=328:38-328:48; Use sum --- @INFO range=329:43-329:53; Use sum --- @INFO range=330:29-330:39; Use sum --- @INFO range=333:3-333:23; Use head --- @INFO range=338:29-338:36; Use head +-- @INFO range=128:17-128:32; Use dedup +-- @INFO range=314:10-314:24; Evaluate +-- @INFO range=335:38-335:48; Use sum +-- @INFO range=336:43-336:53; Use sum +-- @INFO range=337:29-337:39; Use sum +-- @INFO range=340:3-340:23; Use head +-- @INFO range=345:29-345:36; Use head module List where import DA.List import DA.Assert import DA.Optional +import qualified DA.Foldable as Foldable data Foo = Foo with x : Int; y : Text deriving (Eq, Show) @@ -324,6 +325,12 @@ testFoldr1 = scenario do "abc" === foldr1 (<>) ["a", "b", "c"] 2 === foldr1 (-) [1, 2, 3] +testFoldable = scenario do + "abc" === Foldable.foldl1 (<>) ["a", "b", "c"] + -4 === Foldable.foldl1 (-) [1, 2, 3] + "abc" === Foldable.foldr1 (<>) ["a", "b", "c"] + 2 === Foldable.foldr1 (-) [1, 2, 3] + testRepeatedly = scenario do [15, 12, 5] === repeatedly (\x -> (foldl1 (+) x, drop 2 x)) [1..5] [10, 22, 34, 10] === repeatedly (\x -> (foldl1 (+) (take 4 x), drop 3 x)) [1..10]