[m-rev.] diff: add usual synonyms for conversion to ranges functions

Julien Fischer jfischer at opturion.com
Tue Jan 7 15:48:11 AEDT 2025


Add usual synonyms for conversion to ranges functions.

library/ranges.m:
    Add list_to_ranges and set_to_ranges as synonyms for the existing
    from_list and from_set functions.

NEWS.md:
    Announce the additions.

Julien.

diff --git a/NEWS.md b/NEWS.md
index 7f58fc9..0928ed6 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -788,11 +788,15 @@ Changes to the Mercury standard library
     - func `intersect/2`
     - pred `intersect/3`
     - pred `insert_list/3`
+    - func `list_to_ranges/1`
+    - pred `list_to_ranges/2`
     - pred `nondet_range_member/3`
     - pred `restrict_min/3`
     - pred `restrict_max/3`
     - pred `restrict_range/4`
     - pred `search_range/4`
+    - func `set_to_ranges/1`
+    - pred `set_to_ranges/2`
     - pred `union/3`

 * The following function and predicate have been marked obsolete:
diff --git a/library/ranges.m b/library/ranges.m
index 1a38a88..8db69f6 100644
--- a/library/ranges.m
+++ b/library/ranges.m
@@ -226,13 +226,21 @@
 %

     % Convert from a list of integers.
-    % NOTE_TO_IMPLEMENTORS XXX Should have list_to_ranges synonym.
     % NOTE_TO_IMPLEMENTORS XXX Should have sorted_list_to_ranges version.
     %
+:- func list_to_ranges(list(int)) = ranges.
+:- pred list_to_ranges(list(int)::in, ranges::out) is det.
+
+    % Synonym for list_to_ranges/1.
+    %
 :- func from_list(list(int)) = ranges.

     % Convert from a set of integers.
-    % NOTE_TO_IMPLEMENTORS XXX Should have set_to_ranges synonym.
+    %
+:- func set_to_ranges(set(int)) = ranges.
+:- pred set_to_ranges(set(int)::in, ranges::out) is det.
+
+    % Synonym for set_to_ranges/1.
     %
 :- func from_set(set(int)) = ranges.

@@ -1054,11 +1062,21 @@ prune_to_prev_non_member(range(LA, UA, As0),
Result, !N) :-

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

-from_list(List) =
-    list.foldl(ranges.insert, List, ranges.empty).
+list_to_ranges(List) = Set :-
+    list_to_ranges(List, Set).
+
+list_to_ranges(List, Set) :-
+    list.foldl(ranges.insert, List, ranges.empty, Set).
+
+from_list(List) = list_to_ranges(List).
+
+set_to_ranges(Set) = Ranges :-
+    set_to_ranges(Set, Ranges).
+
+set_to_ranges(Set, Ranges) :-
+    set.foldl(ranges.insert, Set, ranges.empty, Ranges).

-from_set(Set) =
-    ranges.from_list(set.to_sorted_list(Set)).
+from_set(Set) = set_to_ranges(Set).

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


More information about the reviews mailing list