Skip to content

Commit

Permalink
[Fix] Fix contract.Sequence reversing order (#1510)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
yannham authored Aug 8, 2023
1 parent 5572b44 commit 943430e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 5 additions & 1 deletion core/stdlib/std.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions core/tests/integration/pass/contracts/contracts.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 943430e

Please sign in to comment.