From 943430e07eb573a55c690ec571ae5ccb70671da5 Mon Sep 17 00:00:00 2001 From: Yann Hamdaoui Date: Tue, 8 Aug 2023 10:57:19 +0200 Subject: [PATCH] [Fix] Fix contract.Sequence reversing order (#1510) * Fix contract.Sequence reversing order This commit started by fixing tests that didn't fire, because they involved contracts but were never executed. On of those tests failed after being fixed, indicating that `std.contract.Sequence` wasn't preserving the order of applied contracts. This commit fixes this issue as well by using `fold_left` instead of `fold_right` in the implementation of `Sequence`. * Formatting (stdlib) --- core/stdlib/std.ncl | 6 +++++- .../integration/pass/contracts/contracts.ncl | 15 ++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/core/stdlib/std.ncl b/core/stdlib/std.ncl index 2010f902..0ef2c9d6 100644 --- a/core/stdlib/std.ncl +++ b/core/stdlib/std.ncl @@ -968,7 +968,11 @@ ``` "% = fun contracts label value => - std.array.fold_right (fun contract => std.contract.apply contract label) value contracts, + std.array.fold_left + (fun acc contract => std.contract.apply contract label acc) + value + contracts, + label | doc m%" The label submodule provides functions that manipulate the label diff --git a/core/tests/integration/pass/contracts/contracts.ncl b/core/tests/integration/pass/contracts/contracts.ncl index 04f92b97..5df47866 100644 --- a/core/tests/integration/pass/contracts/contracts.ncl +++ b/core/tests/integration/pass/contracts/contracts.ncl @@ -165,12 +165,17 @@ let {check, Assert, ..} = import "../lib/assert.ncl" in let f | forall r. { ; r } -> { g : forall r. { ; r } -> { z : Number ; r }; r } = fun r => %record_insert% "g" r g in let res = f { z = 3 } - in true, + in std.seq res true, # std.contract.Sequence - let three = 3 | std.contract.Sequence [ Number, std.contract.from_predicate (fun x => x < 5) ] in - # preserves order - let tag = "some_tag" | std.contract.Sequence [ std.enum.TagOrString, [| 'some_tag |] ] - in true + let SmallerThanFive = std.contract.from_predicate (fun x => x < 5) in + let three | std.contract.Sequence [ Number, SmallerThanFive] = 3 in + three == 3, + + # std.contract.Sequence preserves order + let tag | std.contract.Sequence [std.enum.TagOrString, [| 'some_tag |]] + = "some_tag" + in + tag == 'some_tag, ] |> check