[m-rev.] diff: add maybe.fold2_maybe/6
Julien Fischer
jfischer at opturion.com
Mon Dec 29 14:57:59 AEDT 2014
Add the predicate maybe.fold2_maybe/6 to the standard library.
Add a predicate version of ground_pseudo_type_desc_to_type_desc.
library/maybe.m:
Add the above predicate.
Re-order some existing declarations as per the coding standard.
library/type_desc.m:
Add a predicate verison of ground_pseudo_type_desc_to_type_desc.
NEWS:
Announce the above.
diff --git a/NEWS b/NEWS
index 4bf175c..0685004 100644
--- a/NEWS
+++ b/NEWS
@@ -116,6 +116,10 @@ Changes to the Mercury standard library:
* We have a added a new type, maybe_errors, to the maybe module.
+ The following predicate has been added to the maybe module:
+
+ - fold2_maybe/6
+
* The following predicates and functions have been added to the calendar module
- int_to_month/2
diff --git a/library/maybe.m b/library/maybe.m
index bf39dfd..31d951a 100644
--- a/library/maybe.m
+++ b/library/maybe.m
@@ -79,6 +79,11 @@
%
:- func map_maybe(func(T) = U, maybe(T)) = maybe(U).
+ % fold_maybe(_, no, Acc) = Acc.
+ % fold_maybe(F, yes(Value), Acc0) = F(Acc0).
+ %
+:- func fold_maybe(func(T, U) = U, maybe(T), U) = U.
+
% fold_maybe(_, no, !Acc).
% fold_maybe(P, yes(Value), !Acc) :- P(Value, !Acc).
%
@@ -90,10 +95,21 @@
:- mode fold_maybe(pred(in, mdi, muo) is semidet, in, mdi, muo) is semidet.
:- mode fold_maybe(pred(in, di, uo) is semidet, in, di, uo) is semidet.
- % fold_maybe(_, no, Acc) = Acc.
- % fold_maybe(F, yes(Value), Acc0) = F(Acc0).
+ % As above, but with two accumulators.
%
-:- func fold_maybe(func(T, U) = U, maybe(T), U) = U.
+:- pred fold2_maybe(pred(T, U, U, V, V), maybe(T), U, U, V, V).
+:- mode fold2_maybe(pred(in, in, out, in, out) is det, in, in, out,
+ in, out) is det.
+:- mode fold2_maybe(pred(in, in, out, mdi, muo) is det, in, in, out,
+ mdi, muo) is det.
+:- mode fold2_maybe(pred(in, in, out, di, uo) is det, in, in, out,
+ di, uo) is det.
+:- mode fold2_maybe(pred(in, in, out, in, out) is semidet, in, in, out,
+ in, out) is semidet.
+:- mode fold2_maybe(pred(in, in, out, mdi, muo) is semidet, in, in, out,
+ mdi, muo) is semidet.
+:- mode fold2_maybe(pred(in, in, out, di, uo) is semidet, in, in, out,
+ di, uo) is semidet.
% map_fold_maybe(_, no, no, !Acc).
% map_fold_maybe(P, yes(Value0), yes(Value), !Acc) :-
@@ -164,12 +180,17 @@ map_maybe(P, yes(T0), yes(T)) :- P(T0, T).
map_maybe(_, no) = no.
map_maybe(F, yes(T)) = yes(F(T)).
-fold_maybe(_, no, !Acc).
-fold_maybe(P, yes(Value), !Acc) :- P(Value, !Acc).
-
fold_maybe(_, no, Acc) = Acc.
fold_maybe(F, yes(Value), Acc0) = F(Value, Acc0).
+fold_maybe(_, no, !Acc).
+fold_maybe(P, yes(Value), !Acc) :-
+ P(Value, !Acc).
+
+fold2_maybe(_, no, !Acc1, !Acc2).
+fold2_maybe(P, yes(Value), !Acc1, !Acc2) :-
+ P(Value, !Acc1, !Acc2).
+
map_fold_maybe(_, no, no, Acc, Acc).
map_fold_maybe(P, yes(T0), yes(T), Acc0, Acc) :-
P(T0, T, Acc0, Acc).
diff --git a/library/type_desc.m b/library/type_desc.m
index d09b6db..bb96ac1 100644
--- a/library/type_desc.m
+++ b/library/type_desc.m
@@ -63,6 +63,8 @@
%
:- func ground_pseudo_type_desc_to_type_desc(pseudo_type_desc) = type_desc
is semidet.
+:- pred ground_pseudo_type_desc_to_type_desc(pseudo_type_desc::in,
+ type_desc::out) is semidet.
% Convert a pseudo_type_desc describing a ground type to a type_desc.
% If the pseudo_type_desc describes a non-ground type, abort.
@@ -438,6 +440,9 @@ type_desc_to_pseudo_type_desc(_TypeDesc) = _PseudoTypeDesc :-
private_builtin.sorry("type_desc_to_pseudo_type_desc").
ground_pseudo_type_desc_to_type_desc(PseudoTypeDesc) = TypeDesc :-
+ ground_pseudo_type_desc_to_type_desc(PseudoTypeDesc, TypeDesc).
+
+ground_pseudo_type_desc_to_type_desc(PseudoTypeDesc, TypeDesc) :-
( pseudo_type_desc_is_ground(PseudoTypeDesc) ->
private_builtin.unsafe_type_cast(PseudoTypeDesc, TypeDesc)
;
More information about the reviews
mailing list