[m-rev.] for review: Add maybe.semidet_to_maybe

Paul Bone paul at bone.id.au
Wed Aug 10 16:01:16 AEST 2016


For review by anyone

After adding this function and preparing the commit I found that there are
some similar, but probably different enough predicates and functions in
std_util.m  Maybe they should be moved to maybe?

Please also consider the name.


---

Add maybe.semidet_to_maybe

library/maybe.m:
    As above.
---
 library/maybe.m | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/library/maybe.m b/library/maybe.m
index 81c9c8f..a865e4d 100644
--- a/library/maybe.m
+++ b/library/maybe.m
@@ -169,6 +169,13 @@
     %
 :- pred maybe_is_yes(maybe(T)::in, T::out) is semidet.
 
+    % semidet_to_maybe(Pred) = MaybeResult.
+    %
+    % Make a maybe value from a semidet predicate.
+    %
+:- func semidet_to_maybe(pred(X)) = maybe(X).
+:- mode semidet_to_maybe(pred(out) is semidet) = out is det.
+
     % Return the value from within the maybe or a default value if there is
     % none.
     %
@@ -210,6 +217,13 @@ map_fold3_maybe(P, yes(T0), yes(T), !A, !B, !C) :-
 
 maybe_is_yes(yes(X), X).
 
+semidet_to_maybe(Pred) = Result :-
+    ( if Pred(X) then
+        Result = yes(X)
+    else
+        Result = no
+    ).
+
 maybe_default(_, yes(X)) = X.
 maybe_default(D, no) = D.
 
-- 
2.7.4



More information about the reviews mailing list