[m-rev.] for review: Move some code from std_util.m to maybe.m
Paul Bone
paul at bone.id.au
Wed Aug 31 16:28:42 AEST 2016
For review by anyone.
---
Move some code from std_util.m to maybe.m
Deprecate the maybe_pred and maybe_func code in std_lib.m and add a
predicate version of pred_to_maybe and a new function func_to_maybe to
maybe.m.
library/std_util.m:
library/maybe.m:
As above.
NEWS:
Update news file.
---
NEWS | 7 +++++++
library/maybe.m | 15 ++++++++++++++-
library/std_util.m | 5 +++++
3 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/NEWS b/NEWS
index ec77eea..4f5ac05 100644
--- a/NEWS
+++ b/NEWS
@@ -279,6 +279,8 @@ Changes to the Mercury standard library:
- fold2_maybe/6
- maybe_default/2
- pred_to_maybe/1
+ - pred_to_maybe/2
+ - func_to_maybe/1
* The following predicates and functions have been added to the calendar
module:
@@ -393,6 +395,11 @@ Changes to the Mercury standard library:
to group the prettyprinter parameters together in a value of the type
that was designed for this purpose.
+* We have deprecated the following functions from the std_util module:
+
+ - maybe_pred/3
+ - maybe_func/2
+
Changes to the Mercury compiler:
* We have added a new option --warn-dead-preds. While the existing option
diff --git a/library/maybe.m b/library/maybe.m
index 89a5da1..2351d8e 100644
--- a/library/maybe.m
+++ b/library/maybe.m
@@ -2,6 +2,7 @@
% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
%---------------------------------------------------------------------------%
% Copyright (C) 1994-2006, 2010-2011 The University of Melbourne.
+% Copyright (C) 2016 The Mercury Team.
% This file may only be copied under the terms of the GNU Library General
% Public License - see the file COPYING.LIB in the Mercury distribution.
%---------------------------------------------------------------------------%
@@ -173,9 +174,15 @@
%
% Make a maybe value from a semidet predicate.
%
-:- func pred_to_maybe(pred(X)) = maybe(X).
+:- func pred_to_maybe(pred(T)) = maybe(T).
:- mode pred_to_maybe(pred(out) is semidet) = out is det.
+:- pred pred_to_maybe(pred(T), maybe(T)).
+:- mode pred_to_maybe(pred(out) is semidet, out) is det.
+
+:- func func_to_maybe((func) = T) = maybe(T).
+:- mode func_to_maybe((func) = out is semidet) = out is det.
+
% Return the value from within the maybe or a default value if there is
% none.
%
@@ -218,12 +225,18 @@ map_fold3_maybe(P, yes(T0), yes(T), !A, !B, !C) :-
maybe_is_yes(yes(X), X).
pred_to_maybe(Pred) = Result :-
+ pred_to_maybe(Pred, Result).
+
+pred_to_maybe(Pred, Result) :-
( if Pred(X) then
Result = yes(X)
else
Result = no
).
+func_to_maybe(PF) =
+ ( if Y = apply(PF) then yes(Y) else no ).
+
maybe_default(_, yes(X)) = X.
maybe_default(D, no) = D.
diff --git a/library/std_util.m b/library/std_util.m
index 39b3854..9b55de3 100644
--- a/library/std_util.m
+++ b/library/std_util.m
@@ -2,6 +2,7 @@
% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
%---------------------------------------------------------------------------%
% Copyright (C) 1994-2006, 2008 The University of Melbourne.
+% Copyright (C) 2016 The Mercury Team.
% This file may only be copied under the terms of the GNU Library General
% Public License - see the file COPYING.LIB in the Mercury distribution.
%---------------------------------------------------------------------------%
@@ -53,9 +54,13 @@
% X succeeds, Y is bound to `yes(Z)' where Z is the output of the
% call, or to `no' if the call fails.
%
+ % use maybe.pred_to_maybe instead
+:- pragma obsolete(maybe_pred/3).
:- pred maybe_pred(pred(T1, T2), T1, maybe(T2)).
:- mode maybe_pred(pred(in, out) is semidet, in, out) is det.
+ % use maybe.func_to_maybe instead
+:- pragma obsolete(maybe_func/2).
:- func maybe_func(func(T1) = T2, T1) = maybe(T2).
:- mode maybe_func(func(in) = out is semidet, in) = out is det.
--
2.7.4
More information about the reviews
mailing list