From: AlaskanEmily Date: Mon, 13 May 2019 01:34:49 +0000 (+0000) Subject: Make maybe comparisons have expressive results X-Git-Url: http://git.osdn.net/view?p=transunit%2Ftransunit.git;a=commitdiff_plain;h=eef990a7f5780f3ed17e0df636c41d58acf91bc1 Make maybe comparisons have expressive results --- diff --git a/transunit.compare.m b/transunit.compare.m index 93f9eff..f1f3963 100644 --- a/transunit.compare.m +++ b/transunit.compare.m @@ -59,10 +59,6 @@ %------------------------------------------------------------------------------% -:- func simple_compare(T, T) = maybe.maybe_error. - -%------------------------------------------------------------------------------% - :- func negate(float) = float. %------------------------------------------------------------------------------% @@ -136,7 +132,8 @@ ]. :- instance to_string(maybe.maybe(T)) <= to_string(T) where [ - (to_string(maybe.yes(That)) = to_string(That)), + (to_string(maybe.yes(That)) = + string.append("maybe.yes(", string.append(to_string(That), ")"))), (to_string(maybe.no) = "maybe.no") ]. @@ -154,11 +151,6 @@ generic_compare(A, B) = Result :- %------------------------------------------------------------------------------% -simple_compare(A, B) = Result :- - ( A = B -> Result = maybe.ok ; Result = maybe.error("Not equal") ). - -%------------------------------------------------------------------------------% - :- pred accumulate_mismatch(T, T, list(string), list(string), int, int) <= compare(T). :- mode accumulate_mismatch(in, in, in, out, in, out) is det. @@ -245,7 +237,12 @@ accumulate_mismatch(A, B, !List, I, int.plus(I, 1)) :- ]. :- instance compare(maybe.maybe(T)) <= (to_string(T), compare(T)) where [ - func(compare/2) is generic_compare + ( compare(maybe.no, maybe.no) = maybe.ok ), + ( compare(maybe.no, maybe.yes(B)) = maybe.error( + string.append("maybe.no != maybe.yes(", string.append(to_string(B), ")")) )), + ( compare(maybe.yes(A), maybe.no) = maybe.error( + string.append("maybe.yes(", string.append(to_string(A), ") != maybe.no")) )), + ( compare(maybe.yes(A), maybe.yes(B)) = compare(A, B) ) ]. :- instance compare(array.array(T)) <= (to_string(T), compare(T)) where [