[m-rev.] for post-commit review: change some predicate argument orders in the ranges module

Julien Fischer jfischer at opturion.com
Fri Jan 10 20:35:52 AEDT 2025


As discussed recently on the reviews list; the only part that might require
review is the change to the NEWS file.

----------------------------

Change some predicate argument orders in the ranges module.

library/ranges.m:
    Change the argument order of nondet_member/2 and nondet_range_member/3 so
    that their ranges argument is first.

NEWS.md:
    Announce the change.

tests/hard_coded/test_ranges.m:
    Conform to the above change.

Julien.

diff --git a/NEWS.md b/NEWS.md
index 0928ed6..4192940 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -55,6 +55,9 @@ Changes that may break compatibility
 * Two predicates in the `bt_array` module, `resize/4` and `shrink/4`,
   have had the order of their arguments changed.

+* One predicate in the `ranges` module, `nondet_member/2`, has had the order of
+  its arguments changed.
+
 * The character sequences `<<u` and `>>u` are now recognized as single tokens.
   (They are the names of new versions of the left and right shift operators
   that expect the shift amount to be specified by an unsigned integer;
@@ -776,6 +779,10 @@ Changes to the Mercury standard library

 ### Changes to the `ranges` module

+* The following predicate has had the order of its arguments changed:
+
+    - pred `nondet_member/2`
+
 * The following predicate and functions have been added:

     - func `make_singleton_set/1`
diff --git a/library/ranges.m b/library/ranges.m
index c1da8c2..7197dd2 100644
--- a/library/ranges.m
+++ b/library/ranges.m
@@ -89,20 +89,20 @@
     %
 :- pred contains(ranges::in, int::in) is semidet.

-    % nondet_member(X, Set):
+    % nondet_member(Set X):
     %
     % Nondeterministically produce each element in Set.
     % Each time this call succeeds, X will be bound to an element in Set.
     %
-:- pred nondet_member(int::out, ranges::in) is nondet.
+:- pred nondet_member(ranges::in, int::out) is nondet.

-    % nondet_range_member(Lo, Hi, Set):
+    % nondet_range_member(Set, Lo, Hi):
     %
     % Nondeterministically produce each range in Set.
     % Each time this call succeeds, Lo and Hi will be bound to
     % the smallest and largest integers respectively in a range in Set.
     %
-:- pred nondet_range_member(int::out, int::out, ranges::in) is nondet.
+:- pred nondet_range_member(ranges::in, int::out, int::out) is nondet.

     % Obsolete synonym for nondet_range_member/3.
     %
@@ -627,20 +627,20 @@ member(N, range(Lo, Hi, Rest)) :-
 contains(Set, E) :-
     member(E, Set).

-nondet_member(N, Set) :-
-    nondet_range_member(Lo, Hi, Set),
+nondet_member(Set, N) :-
+    nondet_range_member(Set, Lo, Hi),
     int.nondet_int_in_range(Lo, Hi, N).

-nondet_range_member(Lo, Hi, range(Lo0, Hi0, Rest)) :-
+nondet_range_member(range(Lo0, Hi0, Rest), Lo, Hi) :-
     (
         Lo = Lo0 + 1,
         Hi = Hi0
     ;
-        nondet_range_member(Lo, Hi, Rest)
+        nondet_range_member(Rest, Lo, Hi)
     ).

 range_member(Lo, Hi, Ranges) :-
-    nondet_range_member(Lo, Hi, Ranges).
+    nondet_range_member(Ranges, Lo, Hi).

 %---------------------------------------------------------------------------%

diff --git a/tests/hard_coded/test_ranges.m b/tests/hard_coded/test_ranges.m
index 8cb2a7a..c4bc7b5 100644
--- a/tests/hard_coded/test_ranges.m
+++ b/tests/hard_coded/test_ranges.m
@@ -439,7 +439,7 @@ do_test_member_2(Ranges, Value, !IO) :-

 test_nondet_range_member(Ranges, !IO) :-
     Pred = (pred(V::out) is nondet :-
-        nondet_range_member(Lo, Hi, Ranges),
+        nondet_range_member(Ranges, Lo, Hi),
         V = {Lo, Hi}
     ),
     solutions(Pred, Result),
@@ -449,10 +449,7 @@ test_nondet_range_member(Ranges, !IO) :-
 :- pred test_nondet_member(ranges::in, io::di, io::uo) is det.

 test_nondet_member(Ranges, !IO) :-
-    Pred = (pred(V::out) is nondet :-
-        nondet_member(V, Ranges)
-    ),
-    solutions(Pred, Result),
+    solutions(nondet_member(Ranges), Result),
     io.format("nondet_member(%s) ===> %s\n",
         [r(Ranges), s(string(Result))], !IO).


More information about the reviews mailing list