[m-rev.] for review: Add version_array.lookup predicate.

Peter Wang novalazy at gmail.com
Thu Dec 1 13:11:07 AEDT 2022


library/version_array.m:
    Add version_array.lookup predicate.

    Use it instead of the lookup function in some places,
    mostly because lookup(A, I) now has an ambiguous type.

NEWS:
    Announce change.
---
 NEWS                    |  6 ++++++
 library/version_array.m | 28 ++++++++++++++++++----------
 2 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/NEWS b/NEWS
index 19ce26872..36950e063 100644
--- a/NEWS
+++ b/NEWS
@@ -685,6 +685,12 @@ Changes to the Mercury standard library
 
    - pred `empty/1`
 
+### Changes to the `version_array` module
+
+* The following predicate has been added:
+
+    - pred `lookup/3`
+
 ### Changes to the `version_hash_table` module
 
 * The following obsolete predicates have been removed:
diff --git a/library/version_array.m b/library/version_array.m
index da6e7b6b8..463bf89e8 100644
--- a/library/version_array.m
+++ b/library/version_array.m
@@ -116,6 +116,7 @@
     % (The first item has index 0).
     %
 :- func lookup(version_array(T), int) = T.
+:- pred lookup(version_array(T)::in, int::in, T::out) is det.
 
     % A ^ elem(I) = lookup(A, I)
     %
@@ -458,6 +459,9 @@ from_reverse_list_loop(I, [X | Xs], !VA) :-
 %---------------------------------------------------------------------------%
 
 lookup(VA, I) = X :-
+    lookup(VA, I, X).
+
+lookup(VA, I, X) :-
     ( if get_if_in_range(VA, I, X0) then
         X = X0
     else
@@ -465,8 +469,8 @@ lookup(VA, I) = X :-
     ).
 
 :- pragma inline(func(version_array.elem/2)).
-VA ^ elem(I) =
-    lookup(VA, I).
+VA ^ elem(I) = X :-
+    lookup(VA, I, X).
 
 %---------------------------------------------------------------------------%
 
@@ -695,7 +699,7 @@ all_true(Pred, VA) :-
 
 do_all_true(Pred, I, N, VA) :-
     ( if I < N then
-        Elem = lookup(VA, I),
+        lookup(VA, I, Elem),
         Pred(Elem),
         do_all_true(Pred, I + 1, N, VA)
     else
@@ -710,7 +714,7 @@ all_false(Pred, VA) :-
 
 do_all_false(Pred, I, N, VA) :-
     ( if I < N then
-        Elem = lookup(VA, I),
+        lookup(VA, I, Elem),
         not Pred(Elem),
         do_all_false(Pred, I + 1, N, VA)
     else
@@ -764,7 +768,8 @@ eq_version_array(VAa, VAb) :-
 
 eq_version_array_2(I, VAa, VAb) :-
     ( if I >= 0 then
-        lookup(VAa, I) = lookup(VAb, I),
+        lookup(VAa, I, Elem),
+        lookup(VAb, I, Elem),
         eq_version_array_2(I - 1, VAa, VAb)
     else
         true
@@ -795,7 +800,9 @@ cmp_version_array_2(I, Size, VAa, VAb, R) :-
     ( if I >= Size then
         R = (=)
       else
-        compare(R0, lookup(VAa, I), lookup(VAb, I)),
+        lookup(VAa, I, ElemA),
+        lookup(VAb, I, ElemB),
+        compare(R0, ElemA, ElemB),
         (
             R0 = (=),
             cmp_version_array_2(I + 1, Size, VAa, VAb, R)
@@ -1990,12 +1997,13 @@ version_array_to_doc(A) =
 
 :- func version_array_to_doc_2(int, version_array(T)) = doc.
 
-version_array_to_doc_2(I, VA) =
+version_array_to_doc_2(I, VA) = Doc :-
     ( if I > version_array.max(VA) then
-        str("")
+        Doc = str("")
     else
-        docs([
-            format_arg(format(lookup(VA, I))),
+        lookup(VA, I, Elem),
+        Doc = docs([
+            format_arg(format(Elem)),
             ( if I = version_array.max(VA) then
                 str("")
             else
-- 
2.38.0



More information about the reviews mailing list