Transunit repository
Revision | eef990a7f5780f3ed17e0df636c41d58acf91bc1 (tree) |
---|---|
Zeit | 2019-05-13 10:34:49 |
Autor | AlaskanEmily <yoloshart@gmai...> |
Commiter | AlaskanEmily |
Make maybe comparisons have expressive results
@@ -59,10 +59,6 @@ | ||
59 | 59 | |
60 | 60 | %------------------------------------------------------------------------------% |
61 | 61 | |
62 | -:- func simple_compare(T, T) = maybe.maybe_error. | |
63 | - | |
64 | -%------------------------------------------------------------------------------% | |
65 | - | |
66 | 62 | :- func negate(float) = float. |
67 | 63 | |
68 | 64 | %------------------------------------------------------------------------------% |
@@ -136,7 +132,8 @@ | ||
136 | 132 | ]. |
137 | 133 | |
138 | 134 | :- instance to_string(maybe.maybe(T)) <= to_string(T) where [ |
139 | - (to_string(maybe.yes(That)) = to_string(That)), | |
135 | + (to_string(maybe.yes(That)) = | |
136 | + string.append("maybe.yes(", string.append(to_string(That), ")"))), | |
140 | 137 | (to_string(maybe.no) = "maybe.no") |
141 | 138 | ]. |
142 | 139 |
@@ -154,11 +151,6 @@ generic_compare(A, B) = Result :- | ||
154 | 151 | |
155 | 152 | %------------------------------------------------------------------------------% |
156 | 153 | |
157 | -simple_compare(A, B) = Result :- | |
158 | - ( A = B -> Result = maybe.ok ; Result = maybe.error("Not equal") ). | |
159 | - | |
160 | -%------------------------------------------------------------------------------% | |
161 | - | |
162 | 154 | :- pred accumulate_mismatch(T, T, list(string), list(string), int, int) |
163 | 155 | <= compare(T). |
164 | 156 | :- 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)) :- | ||
245 | 237 | ]. |
246 | 238 | |
247 | 239 | :- instance compare(maybe.maybe(T)) <= (to_string(T), compare(T)) where [ |
248 | - func(compare/2) is generic_compare | |
240 | + ( compare(maybe.no, maybe.no) = maybe.ok ), | |
241 | + ( compare(maybe.no, maybe.yes(B)) = maybe.error( | |
242 | + string.append("maybe.no != maybe.yes(", string.append(to_string(B), ")")) )), | |
243 | + ( compare(maybe.yes(A), maybe.no) = maybe.error( | |
244 | + string.append("maybe.yes(", string.append(to_string(A), ") != maybe.no")) )), | |
245 | + ( compare(maybe.yes(A), maybe.yes(B)) = compare(A, B) ) | |
249 | 246 | ]. |
250 | 247 | |
251 | 248 | :- instance compare(array.array(T)) <= (to_string(T), compare(T)) where [ |