Skip to content

Commit

Permalink
Ltac2: make argument order of fold combinators same as OCaml
Browse files Browse the repository at this point in the history
Fix #16485
  • Loading branch information
SkySkimmer committed Oct 26, 2023
1 parent 08dcbc5 commit 0ccec08
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
6 changes: 6 additions & 0 deletions doc/changelog/06-Ltac2-language/18197-ltac2-fold-order.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- **Changed:**
argument order for the Ltac2 combinators `List.fold_left` `List.fold_right`
and `Array.fold_right` changed to be the same as in OCaml
(`#18197 <https://github.com/coq/coq/pull/18197>`_,
fixes `#16485 <https://github.com/coq/coq/issues/16485>`_,
by Gaëtan Gilbert).
2 changes: 1 addition & 1 deletion test-suite/ltac2/array_lib.v
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Goal True.

(* test fold_right *)
let a := init 4 (fun i => (Int.add 10 i)) in
check_eq_int (of_list (fold_right (fun a b => b::a) [] a)) [10;11;12;13].
check_eq_int (of_list (fold_right (fun a b => a::b) a [])) [10;11;12;13].

(* test exist *)
let a := init 4 (fun i => (Int.add 10 i)) in
Expand Down
2 changes: 1 addition & 1 deletion test-suite/ltac2/map.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Ltac2 ensure b := if b then () else Control.throw Regression_Test_Failure.

Ltac2 sort_int_list l :=
FSet.elements
(List.fold_left (fun acc x => FSet.add x acc) l (FSet.empty FSet.Tags.int_tag)).
(List.fold_left (fun acc x => FSet.add x acc) (FSet.empty FSet.Tags.int_tag) l).

Ltac2 Eval
ensure (List.equal Int.equal [1;2;5;8] (sort_int_list [2;5;1;8])).
Expand Down
10 changes: 6 additions & 4 deletions user-contrib/Ltac2/Array.v
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,19 @@ Ltac2 rec fold_left_aux (f : 'a -> 'b -> 'a) (x : 'a) (a : 'b array) (pos : int)
| false => fold_left_aux f (f x (get a pos)) a (Int.add pos 1) (Int.sub len 1)
end.

Ltac2 fold_left (f : 'a -> 'b -> 'a) (x : 'a) (a : 'b array) := fold_left_aux f x a 0 (length a).
Ltac2 fold_left (f : 'a -> 'b -> 'a) (x : 'a) (a : 'b array) : 'a :=
fold_left_aux f x a 0 (length a).

Ltac2 rec fold_right_aux (f : 'a -> 'b -> 'a) (x : 'a) (a : 'b array) (pos : int) (len : int) :=
Ltac2 rec fold_right_aux (f : 'a -> 'b -> 'b) (a : 'a array) (x : 'b) (pos : int) (len : int) :=
(* Note: one could compare pos<0.
We keep an extra len parameter so that the function can be used for any sub array *)
match Int.equal len 0 with
| true => x
| false => fold_right_aux f (f x (get a pos)) a (Int.sub pos 1) (Int.sub len 1)
| false => fold_right_aux f a (f (get a pos) x) (Int.sub pos 1) (Int.sub len 1)
end.

Ltac2 fold_right (f : 'a -> 'b -> 'a) (x : 'a) (a : 'b array) := fold_right_aux f x a (Int.sub (length a) 1) (length a).
Ltac2 fold_right (f : 'a -> 'b -> 'b) (a : 'a array) (x : 'b) : 'b :=
fold_right_aux f a x (Int.sub (length a) 1) (length a).

Ltac2 rec exist_aux (p : 'a -> bool) (a : 'a array) (pos : int) (len : int) :=
match Int.equal len 0 with
Expand Down
8 changes: 4 additions & 4 deletions user-contrib/Ltac2/List.v
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,16 @@ Ltac2 rev_map (f : 'a -> 'b) (ls : 'a list) :=
end in
rmap_f [] ls.

Ltac2 rec fold_right (f : 'a -> 'b -> 'b) (a : 'b) (ls : 'a list) :=
Ltac2 rec fold_right (f : 'a -> 'b -> 'b) (ls : 'a list) (a : 'b) : 'b :=
match ls with
| [] => a
| l :: ls => f l (fold_right f a ls)
| l :: ls => f l (fold_right f ls a)
end.

Ltac2 rec fold_left (f : 'a -> 'b -> 'a) (xs : 'b list) (a : 'a) :=
Ltac2 rec fold_left (f : 'a -> 'b -> 'a) (a : 'a) (xs : 'b list) : 'a :=
match xs with
| [] => a
| x :: xs => fold_left f xs (f a x)
| x :: xs => fold_left f (f a x) xs
end.

Ltac2 fold_lefti (f : int -> 'a -> 'b -> 'a) (a : 'a) (xs : 'b list) : 'a :=
Expand Down

0 comments on commit 0ccec08

Please sign in to comment.