OSDN Git Service

Make maybe comparisons have expressive results
authorAlaskanEmily <yoloshart@gmail.com>
Mon, 13 May 2019 01:34:49 +0000 (01:34 +0000)
committerAlaskanEmily <yoloshart@gmail.com>
Mon, 13 May 2019 01:34:49 +0000 (01:34 +0000)
transunit.compare.m

index 93f9eff..f1f3963 100644 (file)
 
 %------------------------------------------------------------------------------%
 
-:- func simple_compare(T, T) = maybe.maybe_error.
-
-%------------------------------------------------------------------------------%
-
 :- func negate(float) = float.
 
 %------------------------------------------------------------------------------%
 ].
 
 :- 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 [