<div><b><u><font class="Apple-style-span" size="4">Issue #1</font></u></b></div><div><br></div>I've decided to get serious learning Mercury and I'm doing so by filling in snippets at <a href="http://rosettacode.org">Rosetta Code</a>. <a href="http://rosettacode.org/wiki/First-class_functions#Mercury">Today's exercise</a> has me a bit flummoxed. While it works, at issue is an ugly third clause on the predicate providing the desired functionality. Here's the code:<br>
<br><blockquote class="webkit-indent-blockquote" style="margin:0 0 0 40px;border:none;padding:0px"><font class="Apple-style-span" face="'courier new', monospace">:- module firstclass.<br> <br>:- interface.<br>:- import_module io.<br>
:- pred main(io::di, io::uo) is det.<br> <br>:- implementation.<br>:- import_module exception, list, math, std_util.<br> <br>main(!IO) :-<br> Forward = [math.sin, math.cos, (func(X) = math.ln(X))],<br> Reverse = [math.asin, math.acos, (func(X) = math.exp(X))],<br>
apply(list.zip(Forward, Reverse), [], Results),<br> io.write_list(Results, ", ", io.write_float, !IO),<br> io.write_string("\n", !IO).<br> <br>:- pred apply(list((func(float) = float)), list(float), list(float)).<br>
:- mode apply(in, in, out) is det.<br>apply([], A, Results) :- Results = A.<br>apply([F, R|Functions], A, Results) :-<br> apply(Functions, [compose(R, F, 0.5) | A], Results).<br>apply([_], _, _) :- throw("This can't happen!").</font></blockquote>
<br>The problem is that apply/3 without that third clause is deemed semidet because the second clause's head "can fail" (in that R may not always be bound according to the compiler). Of course this is nonsense in this given situation because the list being passed in will <b>always</b> come in pairs, but naturally the compiler can't know that. For me to get this code to compile I have to add that third clause (which will never be evaluated) just because.<div>
<br></div><div>Now this bugs me because I know that Mercury can do better. Indeed I suspect I'm missing something in the mode declaration that could tell the compiler that apply's first argument <b>must</b> have an even number of entries but I just can't figure out how to say it.</div>
<div><br></div><div>So how would I go about getting rid of that third clause?</div><div><br></div><div><font class="Apple-style-span" size="4"><b><u>Issue #2</u></b></font></div><div><br></div><div>In looking through the Library Reference, I keep seeing the <font class="Apple-style-span" face="'courier new', monospace">:- promise</font> statement used. For example:</div>
<div><br></div><div><div>:- promise all [A, B, C, ABC]</div><div> (</div><div> ( some [AB] (list.append(A, B, AB), list.append(AB, C, ABC)) )</div><div> <=></div><div> ( some [BC] (list.append(B, C, BC), list.append(A, BC, ABC)) )</div>
<div> ).</div></div><div><br></div><div>The language reference <a href="http://www.mercury.csse.unimelb.edu.au/information/doc-release/mercury_ref/Declarations.html#Declarations">only mentions this (and other declarations) in passing</a>, however, and then never talks about them again. Specifically section 2.6 says that <font class="Apple-style-span" face="'courier new', monospace">:- promise</font> should be documented in the <a href="http://www.mercury.csse.unimelb.edu.au/information/doc-release/mercury_ref/Modules.html#Modules">Modules section</a>, but I can't find that there.</div>
<div><br></div><div>So how do I decode such <font class="Apple-style-span" face="'courier new', monospace">:- promise</font> declarations, and where, if anywhere, are they documented?<br><div><br>--<br>"Perhaps people don't believe this, but throughout all of the discussions of entering China our focus has really been what's best for the Chinese people. It's not been about our revenue or profit or whatnot."<br>
--Sergey Brin, demonstrating the emptiness of the "don't be evil" mantra.</div></div>