Thanks to both of you.<div><br>For the record, I added this in dictionary.m in interface :<div><div><font class="Apple-style-span" face="'courier new', monospace">:- func init = dictionary(Key, Value).</font></div>
<div><br></div><div>and this in implementation :</div><div><div><font class="Apple-style-span" face="'courier new', monospace">init = [].</font></div></div><div><br></div><div>and this is the dictionary_use.m that I ran :<div>
<br></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace; ">:- module dictionary_use .</span></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace; "><br>
</span></div><div><font class="Apple-style-span" face="'courier new', monospace"><div>:- interface.</div><div>:- import_module io.</div><div>:- pred main(io::di, io::uo) is det.</div><div><br></div><div>:- implementation.</div>
<div>:- import_module dictionary, int, list, string.</div><div>main(!IO) :-</div><div>(</div><div> Dic1 = init, %%facultative: : dictionary(string, string),</div><div> Dic2 = set( Dic1, "jmv", "JM Vanel" ),</div>
<div> search( Dic2, "jmv", V)</div><div>) -> (</div><div> io.write_string( "search( dic, jmv, V) : V= ", !IO),</div><div> io.write_string( V, !IO)</div><div>) ;</div><div> io.write_string( "fail\n",!IO) .</div>
</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br></font><div class="gmail_quote">2010/9/23 Raphael Collet <span dir="ltr"><<a href="mailto:rco@missioncriticalit.com">rco@missioncriticalit.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Dear Jean-Marc,<div class="im"><br>
<br>
On 09/23/2010 11:28 AM, Jean-Marc Vanel wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Thanks Tomas<br>
It works like this.<br>
<br>
This is very useful to use the map library like you showed, but my<br>
original question was related to the example in the (one and only:( )<br>
Mercury tutorial in paragraph 2. 7 :<br>
<a href="http://www.mercury.csse.unimelb.edu.au/information/papers/book.pdf" target="_blank">http://www.mercury.csse.unimelb.edu.au/information/papers/book.pdf</a><br>
<br>
:- module dictionary.<br>
:- interface.<br>
:- type dictionary(Key, Value).<br>
:- pred search(dictionary(Key, Value)::in, Key::in, Value::out) is semidet.<br>
:- func set(dictionary(Key, Value), Key, Value) = dictionary(Key, Value).<br>
:- implementation.<br>
:- import_module list.<br>
:- type dictionary(Key, Value) == list({Key, Value}).<br>
search([{K, V} | Dict], Key, Value) :-<br>
( if Key = K then Value = V else search(Dict, Key, Value) ).<br>
set(Dict, Key, Value) = [{Key, Value} | Dict].<br>
<br>
I tried to make use of this module (admitedly not computationally<br>
efficient but that's not the point).<br>
Can I use this module like is is , or is some init predicate necessary ?<br>
</blockquote>
<br></div>
If you don't provide an init predicate or function, the user of the module has no way to create an empty dictionary. As such, this module is useless... Therefore you have to provide one.<br>
<br>
One convention we regularly use is a polymorphic init function like<br>
<br>
:- func init = dictionary(Key, Value).<br>
init = [].<br>
<br>
You can usually call it without specifying the actual type parameters. In the following example, the compiler will be able to infer the type dictionary(string, string) with the second statement.<br>
<br>
Dic1 = init,<br>
Dic2 = set(Dic1, "foo", "bar"),<br>
...<br>
<br>
In case the compiler is unable to infer the actual type, you can help it by explicitly adding a type constraint:<br>
<br>
Dic1 = init : dictionary(string, string),<br>
<br>
This is sometimes required when the context does not allow the compiler to make this inference.<br>
<br>
I hope this will help you.<br>
<br>
<br>
Kind regards,<br><font color="#888888">
<br>
Raphael Collet<br>
Mission Critical IT<br>
<a href="http://www.missioncriticalit.com" target="_blank">http://www.missioncriticalit.com</a></font><div><div></div><div class="h5"><br>
--------------------------------------------------------------------------<br>
mercury-users mailing list<br>
Post messages to: <a href="mailto:mercury-users@csse.unimelb.edu.au" target="_blank">mercury-users@csse.unimelb.edu.au</a><br>
Administrative Queries: <a href="mailto:owner-mercury-users@csse.unimelb.edu.au" target="_blank">owner-mercury-users@csse.unimelb.edu.au</a><br>
Subscriptions: <a href="mailto:mercury-users-request@csse.unimelb.edu.au" target="_blank">mercury-users-request@csse.unimelb.edu.au</a><br>
--------------------------------------------------------------------------<br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Jean-Marc Vanel<br>Consulting, services, training,<br>Rule-based programming, Semantic Web<br><a href="http://jmvanel.free.fr/">http://jmvanel.free.fr/</a><br>
EulerGUI, a turntable GUI for Semantic Web + rules, XML, UML, eCore, Java bytecode<br>+33 (0)6 89 16 29 52 -- +33 (0)1 39 55 58 16<br>( we rarely listen to voice messages, please send a mail instead )<br>
</div></div></div></div>